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.

3789 lines
128 KiB

  1. /*++ BUILD Version: 0009 // Increment this if a change has global effect
  2. Copyright (c) 1987-1993 Microsoft Corporation
  3. Module Name:
  4. smbcxchng.c
  5. Abstract:
  6. This is the include file that implements the SMB_*_EXCHANGE creation, deletion and
  7. dispatch routines.
  8. Author:
  9. Balan Sethu Raman (SethuR) 06-Mar-95 Created
  10. Notes:
  11. The exchange engine supports two kinds of changes, timed and untimed exhanges.
  12. The timed exchanges are distinguished by the SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION.
  13. In addition all exchanges are finalized if the transport is not able to push out
  14. the data within a specific period of time. This enables us to throttle back
  15. traffic to a overloaded server. Currently this is a global constant for all exchanges
  16. and is set to 300 seconds.
  17. This time limit only comes into play only when a send complete operation is outstanding
  18. The exchanges are put on a timed exchange list ( one for each type of exchange)
  19. when it is initiated. When a network operation, i.e., tranceive/send/copydata is
  20. initiated the corresponding expiry time in the exchange is updated by invoking the
  21. routine SmbCeSetExpiryTime.
  22. The echo probes are initiated is invoked through the context of a recurrent service
  23. (recursvc.c/recursvc.h). Every time this service is invoked (SmbCeProbeServers) it
  24. in turn invokes SmbCeDetectAndResumeExpiredExchanges. This routine detects those
  25. exchanges for which the wait for a response has exceeded the time limit and marks
  26. them for finalization.
  27. The finalization is done by SmbCeScavengeTimedOutExchanges in the context of a worker
  28. thread. Notice that due to the granularity mismatch we treat timeout intervals as
  29. soft deadlines.
  30. --*/
  31. #include "precomp.h"
  32. #pragma hdrstop
  33. #include "exsessup.h"
  34. #ifdef ALLOC_PRAGMA
  35. #pragma alloc_text(PAGE, MRxSmbInitializeSmbCe)
  36. #pragma alloc_text(PAGE, SmbCeSerializeSessionSetupRequests)
  37. #pragma alloc_text(PAGE, SmbCeUnblockSerializedSessionSetupRequests)
  38. #pragma alloc_text(PAGE, SmbCeUnblockSerializedSessionSetupRequests)
  39. #pragma alloc_text(PAGE, SmbCeInitiateExchange)
  40. #pragma alloc_text(PAGE, SmbCeInitiateAssociatedExchange)
  41. #pragma alloc_text(PAGE, SmbCeExchangeAbort)
  42. #pragma alloc_text(PAGE, SmbCeBuildSmbHeader)
  43. #pragma alloc_text(PAGE, SmbCeResumeExchange)
  44. #pragma alloc_text(PAGE, SmbCepInitializeExchange)
  45. #pragma alloc_text(PAGE, SmbCeInitializeAssociatedExchange)
  46. #pragma alloc_text(PAGE, SmbCeTransformExchange)
  47. #pragma alloc_text(PAGE, SmbCePrepareExchangeForReuse)
  48. #pragma alloc_text(PAGE, SmbCeDiscardExchange)
  49. #pragma alloc_text(PAGE, SmbCeFinalizeExchangeWorkerThreadRoutine)
  50. #pragma alloc_text(PAGE, SmbCeFinalizeExchangeOnDisconnect)
  51. #pragma alloc_text(PAGE, SmbCeDetectExpiredExchanges)
  52. #pragma alloc_text(PAGE, DefaultSmbExchangeIndError)
  53. #pragma alloc_text(PAGE, DefaultSmbExchangeIndReceive)
  54. #pragma alloc_text(PAGE, DefaultSmbExchangeIndSendCallback)
  55. #endif
  56. #define CANCEL_BUFFER_SIZE (sizeof(SMB_HEADER) + sizeof(REQ_NT_CANCEL))
  57. ULONG SmbCeTraceExchangeReferenceCount = 0;
  58. extern BOOLEAN MRxSmbSecuritySignaturesRequired;
  59. extern BOOLEAN MRxSmbSecuritySignaturesEnabled;
  60. RXDT_DefineCategory(SMBXCHNG);
  61. #define Dbg (DEBUG_TRACE_SMBXCHNG)
  62. // The exchange engine in the mini redirector requires to maintain enough state
  63. // to ensure that all the active exchanges are completed correctly when a shut down
  64. // occurs. Since the exchanges can be finalized by different threads, including
  65. // posted completions the exchange engine on startup initializes an event upon startup
  66. // which is subsequently used to signal the terminating condition.
  67. //
  68. // The count of active changes has to be tracked continously and the signalling
  69. // of the event depends upon the number of active exchanges reaching the count of
  70. // zero and the exchange engine being in a stopped state.
  71. SMBCE_STARTSTOP_CONTEXT SmbCeStartStopContext;
  72. NTSTATUS
  73. MRxSmbInitializeSmbCe()
  74. /*++
  75. Routine Description:
  76. This routine initializes the connection engine
  77. Return Value:
  78. NXSTATUS - The return status for the operation
  79. Notes:
  80. --*/
  81. {
  82. LONG i;
  83. PAGED_CODE();
  84. KeInitializeEvent(
  85. &SmbCeStartStopContext.StopEvent,
  86. NotificationEvent,
  87. FALSE);
  88. SmbCeStartStopContext.ActiveExchanges = 0;
  89. SmbCeStartStopContext.State = SMBCE_STARTED;
  90. SmbCeStartStopContext.pServerEntryTearDownEvent = NULL;
  91. InitializeListHead(
  92. &SmbCeStartStopContext.SessionSetupRequests);
  93. return STATUS_SUCCESS;
  94. }
  95. NTSTATUS
  96. MRxSmbTearDownSmbCe()
  97. /*++
  98. Routine Description:
  99. This routine tears down the connection engine
  100. Return Value:
  101. NXSTATUS - The return status for the operation
  102. Notes:
  103. --*/
  104. {
  105. BOOLEAN fWait;
  106. if (SmbCeStartStopContext.State == SMBCE_STARTED) {
  107. SmbCeAcquireSpinLock();
  108. SmbCeStartStopContext.State = SMBCE_STOPPED;
  109. fWait = (SmbCeStartStopContext.ActiveExchanges > 0);
  110. SmbCeReleaseSpinLock();
  111. if (fWait) {
  112. KeWaitForSingleObject(
  113. &SmbCeStartStopContext.StopEvent,
  114. Executive,
  115. KernelMode,
  116. FALSE,
  117. NULL);
  118. }
  119. }
  120. return STATUS_SUCCESS;
  121. }
  122. NTSTATUS
  123. SmbCeIncrementActiveExchangeCount()
  124. /*++
  125. Routine Description:
  126. This routine increments the active exchange count
  127. Return Value:
  128. NXSTATUS - The return status for the operation
  129. Notes:
  130. --*/
  131. {
  132. NTSTATUS Status = STATUS_SUCCESS;
  133. SmbCeAcquireSpinLock();
  134. if (SmbCeStartStopContext.State != SMBCE_STARTED) {
  135. Status = STATUS_UNSUCCESSFUL;
  136. } else {
  137. InterlockedIncrement(&SmbCeStartStopContext.ActiveExchanges);
  138. }
  139. SmbCeReleaseSpinLock();
  140. return Status;
  141. }
  142. VOID
  143. SmbCeDecrementActiveExchangeCount()
  144. /*++
  145. Routine Description:
  146. This routine decrements the active exchange count
  147. Return Value:
  148. NXSTATUS - The return status for the operation
  149. Notes:
  150. --*/
  151. {
  152. LONG FinalRefCount;
  153. ASSERT(SmbCeStartStopContext.ActiveExchanges > 0);
  154. if (InterlockedDecrement(&SmbCeStartStopContext.ActiveExchanges) == 0) {
  155. SmbCeAcquireSpinLock();
  156. if (SmbCeStartStopContext.State == SMBCE_STOPPED) {
  157. KeSetEvent(&SmbCeStartStopContext.StopEvent,0,FALSE);
  158. }
  159. SmbCeReleaseSpinLock();
  160. }
  161. }
  162. NTSTATUS
  163. SmbCeReferenceServer(
  164. PSMB_EXCHANGE pExchange)
  165. /*++
  166. Routine Description:
  167. This routine initializes the server associated with an exchange.
  168. Arguments:
  169. pExchange - the exchange to be initialized.
  170. Return Value:
  171. RXSTATUS - The return status for the operation
  172. Notes:
  173. The initiation of an exchange proceeds in multiple steps. The first step involves
  174. referencing the corresponding server,session and netroot entries. Subsequently the
  175. exchange is placed in a SMB_EXCHANGE_START state and the exchange is dispatched to the
  176. Start method. The act of referencing the session or the net root may suspend the exchange.
  177. The session and net roots are aliased entities, i.e., there is more then one reference
  178. to it. It is conceivable that the construction is in progress when a reference is made.
  179. In such cases the exchange is suspended and resumed when the construction is complete.
  180. On some transports a reconnect is possible without having to tear down an existing
  181. connection, i.e. attempting to send a packet reestablishes the connection at the
  182. lower level. Since this is not supported by all the transports ( with the exception
  183. of TCP/IP) the reference server entry initiates this process by tearing down the
  184. existing transport and reinitialising it.
  185. --*/
  186. {
  187. NTSTATUS Status = STATUS_SUCCESS;
  188. LONG CscState;
  189. PSMBCEDB_SERVER_ENTRY pServerEntry;
  190. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  191. ASSERT(SmbCeIsResourceOwned());
  192. ASSERT(pExchange->SmbCeState == SMBCE_EXCHANGE_INITIALIZATION_START);
  193. if (SmbCeGetServerType(pServerEntry) == SMBCEDB_MAILSLOT_SERVER &&
  194. !FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_MAILSLOT_OPERATION)) {
  195. // if the serve entry was created for mailslot operation, and a non-maislot operation
  196. // comes, the server entry needs to establish a VC transport. Therefore we invalidate
  197. // the server entry and set it to FILE SERVER.
  198. pServerEntry->Header.State = SMBCEDB_INVALID;
  199. SmbCeSetServerType(pServerEntry,SMBCEDB_FILE_SERVER);
  200. SetFlag(pExchange->SmbCeFlags,SMBCE_EXCHANGE_ATTEMPT_RECONNECTS);
  201. }
  202. if ((pExchange->SmbCeFlags & SMBCE_EXCHANGE_ATTEMPT_RECONNECTS) &&
  203. (SmbCeGetServerType(pServerEntry) == SMBCEDB_FILE_SERVER)) {
  204. CscState = InterlockedCompareExchange(
  205. &pServerEntry->Server.CscState,
  206. ServerCscShadowing,
  207. ServerCscTransitioningToShadowing);
  208. if (CscState == ServerCscTransitioningToShadowing) {
  209. ASSERT(!pServerEntry->NegotiateInProgress);
  210. pServerEntry->Header.State = SMBCEDB_INVALID;
  211. }
  212. }
  213. if (pServerEntry->Header.State != SMBCEDB_ACTIVE) {
  214. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_ATTEMPT_RECONNECTS) {
  215. switch (pServerEntry->Header.State) {
  216. case SMBCEDB_INVALID:
  217. {
  218. BOOLEAN ServerInDisconnectedModeBeforeInit;
  219. SMBCEDB_OBJECT_STATE State;
  220. ServerInDisconnectedModeBeforeInit = SmbCeIsServerInDisconnectedMode(
  221. pServerEntry);
  222. ASSERT(!pServerEntry->NegotiateInProgress);
  223. pServerEntry->NegotiateInProgress = TRUE;
  224. SmbCeUpdateServerEntryState(
  225. pServerEntry,
  226. SMBCEDB_CONSTRUCTION_IN_PROGRESS);
  227. SmbCeReleaseResource();
  228. // Initialize the transport associated with the server
  229. Status = SmbCeInitializeServerTransport(pServerEntry,NULL,NULL);
  230. if (Status == STATUS_SUCCESS) {
  231. if (SmbCeIsServerInDisconnectedMode(pServerEntry)) {
  232. if (!ServerInDisconnectedModeBeforeInit) {
  233. // A transition has occurred from connected mode of
  234. // operation to a disconnected mode. retry the
  235. // operation
  236. Status = STATUS_RETRY;
  237. }
  238. } else {
  239. if (ServerInDisconnectedModeBeforeInit) {
  240. DbgPrint("Transitioning SE %lx from DC to CO\n",pServerEntry);
  241. }
  242. }
  243. }
  244. if (Status == STATUS_SUCCESS) {
  245. PSMBCEDB_SESSION_ENTRY pSessionEntry =
  246. SmbCeGetExchangeSessionEntry(pExchange);
  247. BOOLEAN RemoteBootSession;
  248. if ((pSessionEntry != NULL) &&
  249. (FlagOn(pSessionEntry->Session.Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION) ||
  250. MRxSmbUseKernelModeSecurity)) {
  251. RemoteBootSession = TRUE;
  252. } else {
  253. RemoteBootSession = FALSE;
  254. }
  255. Status = SmbCeNegotiate(
  256. pServerEntry,
  257. pServerEntry->pRdbssSrvCall,
  258. RemoteBootSession
  259. );
  260. }
  261. SmbCeCompleteServerEntryInitialization(pServerEntry,Status);
  262. if (Status != STATUS_SUCCESS) {
  263. // Either the transport initialization failed or the NEGOTIATE
  264. // SMB could not be sent ....
  265. InterlockedIncrement(&MRxSmbStatistics.Reconnects);
  266. }
  267. SmbCeAcquireResource();
  268. }
  269. break;
  270. case SMBCEDB_CONSTRUCTION_IN_PROGRESS :
  271. {
  272. PSMBCEDB_REQUEST_ENTRY pRequestEntry;
  273. pRequestEntry = (PSMBCEDB_REQUEST_ENTRY)
  274. SmbMmAllocateObject(SMBCEDB_OT_REQUEST);
  275. if (pRequestEntry != NULL) {
  276. // Enqueue the request entry.
  277. pRequestEntry->ReconnectRequest.Type = RECONNECT_REQUEST;
  278. pRequestEntry->ReconnectRequest.pExchange = pExchange;
  279. SmbCeIncrementPendingLocalOperations(pExchange);
  280. SmbCeAddRequestEntry(&pServerEntry->OutstandingRequests,pRequestEntry);
  281. Status = STATUS_PENDING;
  282. } else {
  283. Status = STATUS_INSUFFICIENT_RESOURCES;
  284. }
  285. }
  286. break;
  287. default :
  288. Status = STATUS_CONNECTION_DISCONNECTED;
  289. break;
  290. }
  291. } else {
  292. Status = STATUS_CONNECTION_DISCONNECTED;
  293. }
  294. }
  295. if ((Status == STATUS_SUCCESS) || (Status == STATUS_PENDING)) {
  296. pExchange->SmbCeState = SMBCE_EXCHANGE_SERVER_INITIALIZED;
  297. }
  298. ASSERT(SmbCeIsResourceOwned());
  299. return Status;
  300. }
  301. VOID
  302. SmbCeSerializeSessionSetupRequests(
  303. PSMBCEDB_SESSION_ENTRY pSessionEntry)
  304. /*++
  305. Routine Description:
  306. This routine serializes the session setup requests to a server
  307. Arguments:
  308. pSessionEntry - the session entry.
  309. Notes:
  310. The session setup request with a VC number of zero has a special significance
  311. for the server. It is the clue for the server to tear down any existing
  312. data structures and rebuild ( client reboot ). When two aliased connections to
  313. a server are established it is important to ensure that no connections with VC
  314. number zero are outstanding while a non zero VC numbered session is sent. This
  315. is because of the potential for out of order request processing that exists
  316. on the server.
  317. In order to garantee the sequence of session setup, we put the outstanding session
  318. setup requests on a waiting list. If there is a new sesstion setup against the
  319. aliased server, it will be held until the first session setup finished.
  320. --*/
  321. {
  322. PSMBCEDB_SERVER_ENTRY pServerEntry;
  323. BOOLEAN DelayedRequest = FALSE;
  324. PAGED_CODE();
  325. RemoveEntryList(&pSessionEntry->SerializationList);
  326. InitializeListHead(&pSessionEntry->SerializationList);
  327. pServerEntry = pSessionEntry->pServerEntry;
  328. pSessionEntry->SessionVCNumber = 0;
  329. if ((pServerEntry->Server.Dialect >= NTLANMAN_DIALECT) &&
  330. (FlagOn(pServerEntry->Server.DialectFlags,DF_NT_STATUS))) {
  331. PSMBCEDB_SESSION_ENTRY pTempSessionEntry;
  332. pTempSessionEntry = SmbCeGetFirstSessionEntry(pServerEntry);
  333. while (pTempSessionEntry != NULL) {
  334. if ((pTempSessionEntry != pSessionEntry) &&
  335. (pTempSessionEntry->Header.State != SMBCEDB_INVALID) &&
  336. (pTempSessionEntry->Header.State != SMBCEDB_MARKED_FOR_DELETION)) {
  337. pSessionEntry->SessionVCNumber = 1;
  338. break;
  339. }
  340. pTempSessionEntry = SmbCeGetNextSessionEntry(pServerEntry,pTempSessionEntry);
  341. }
  342. if (pServerEntry->Server.AliasedServers) {
  343. PLIST_ENTRY pListEntry;
  344. BOOLEAN DelaySessionSetupRequest = FALSE;
  345. PSMBCEDB_SERVER_ENTRY pTempServerEntry;
  346. // Figure out the VC number for aliased servers by walking
  347. // through the list of server entries
  348. pTempServerEntry = SmbCeGetFirstServerEntry();
  349. while ((pTempServerEntry != NULL) &&
  350. (pSessionEntry->SessionVCNumber == 0)) {
  351. if (SmbCeAreServerEntriesAliased(pServerEntry,pTempServerEntry)) {
  352. pTempSessionEntry = SmbCeGetFirstSessionEntry(pServerEntry);
  353. while (pTempSessionEntry != NULL) {
  354. if ((pTempSessionEntry->Header.State != SMBCEDB_INVALID) &&
  355. (pTempSessionEntry->Header.State != SMBCEDB_MARKED_FOR_DELETION)) {
  356. pSessionEntry->SessionVCNumber = 1;
  357. break;
  358. }
  359. pTempSessionEntry = SmbCeGetNextSessionEntry(pServerEntry,pTempSessionEntry);
  360. }
  361. }
  362. pTempServerEntry = SmbCeGetNextServerEntry(pTempServerEntry);
  363. }
  364. pListEntry = SmbCeStartStopContext.SessionSetupRequests.Flink;
  365. while (pListEntry != &SmbCeStartStopContext.SessionSetupRequests) {
  366. PSMBCEDB_SESSION_ENTRY pTempSessionEntry;
  367. pTempSessionEntry = (PSMBCEDB_SESSION_ENTRY)
  368. CONTAINING_RECORD(
  369. pListEntry,
  370. SMBCEDB_SESSION_ENTRY,
  371. SerializationList);
  372. pTempServerEntry = pTempSessionEntry->pServerEntry;
  373. if (SmbCeAreServerEntriesAliased(pServerEntry,pTempServerEntry) &&
  374. (pTempSessionEntry->SessionVCNumber == 0)) {
  375. DelaySessionSetupRequest = TRUE;
  376. break;
  377. } else {
  378. pListEntry = pListEntry->Flink;
  379. }
  380. }
  381. if (DelaySessionSetupRequest) {
  382. KEVENT Event;
  383. KeInitializeEvent(
  384. &Event,
  385. NotificationEvent,
  386. FALSE);
  387. pSessionEntry->pSerializationEvent = &Event;
  388. InsertTailList(
  389. &SmbCeStartStopContext.SessionSetupRequests,
  390. &pSessionEntry->SerializationList);
  391. SmbCeReleaseResource();
  392. KeWaitForSingleObject(
  393. &Event,
  394. Executive,
  395. KernelMode,
  396. FALSE,
  397. NULL);
  398. SmbCeAcquireResource();
  399. pSessionEntry->pSerializationEvent = NULL;
  400. DelayedRequest = TRUE;
  401. }
  402. }
  403. } else {
  404. pSessionEntry->SessionVCNumber = 0;
  405. }
  406. if (!DelayedRequest) {
  407. InsertTailList(
  408. &SmbCeStartStopContext.SessionSetupRequests,
  409. &pSessionEntry->SerializationList);
  410. }
  411. }
  412. VOID
  413. SmbCeUnblockSerializedSessionSetupRequests(
  414. PSMBCEDB_SESSION_ENTRY pSessionEntry)
  415. /*++
  416. Routine Description:
  417. This routine unblocks non zero VC number session setup requests on completion
  418. of zero VC number session setup requests
  419. Arguments:
  420. pSessionEntry - the session entry.
  421. Notes:
  422. The session setup request with a VC number of zero has a special significance
  423. for the server. It is the cure for the server to tear down any existing
  424. data structures and rebuild ( cliet reboot ). When two aliased connections to
  425. a server are established it is important to ensure that no connections with VC
  426. number zero are outstanding while a non zero VC numbered session is sent. This
  427. is because of the potential for out of order request processing that exists
  428. on the server.
  429. --*/
  430. {
  431. PLIST_ENTRY pListEntry;
  432. PAGED_CODE();
  433. RemoveEntryList(&pSessionEntry->SerializationList);
  434. InitializeListHead(&pSessionEntry->SerializationList);
  435. pListEntry = SmbCeStartStopContext.SessionSetupRequests.Flink;
  436. while (pListEntry != &SmbCeStartStopContext.SessionSetupRequests) {
  437. PSMBCEDB_SESSION_ENTRY pTempSessionEntry;
  438. pTempSessionEntry = (PSMBCEDB_SESSION_ENTRY)
  439. CONTAINING_RECORD(
  440. pListEntry,
  441. SMBCEDB_SESSION_ENTRY,
  442. SerializationList);
  443. pListEntry = pListEntry->Flink;
  444. if (SmbCeAreServerEntriesAliased(
  445. pSessionEntry->pServerEntry,
  446. pTempSessionEntry->pServerEntry)) {
  447. RemoveEntryList(&pTempSessionEntry->SerializationList);
  448. InitializeListHead(&pTempSessionEntry->SerializationList);
  449. if (pTempSessionEntry->pSerializationEvent != NULL) {
  450. KeSetEvent(
  451. pTempSessionEntry->pSerializationEvent,
  452. 0,
  453. FALSE);
  454. }
  455. }
  456. }
  457. }
  458. NTSTATUS
  459. SmbCeReferenceSession(
  460. PSMB_EXCHANGE pExchange)
  461. /*++
  462. Routine Description:
  463. This routine initializes the session associated with an exchange.
  464. Arguments:
  465. pExchange - the exchange to be initialized.
  466. Return Value:
  467. RXSTATUS - The return status for the operation
  468. Notes:
  469. The initiation of an exchange proceeds in multiple steps. The first step involves
  470. referencing the corresponding server,session and netroot entries. Subsequently the
  471. exchange is placed in a SMB_EXCHANGE_START state and the exchange is dispatched to the
  472. Start method. The act of referencing the session or the net root may suspend the exchange.
  473. The session and net roots are aliased entities, i.e., there is more then one reference
  474. to it. It is conceivable that the construction is in progress when a reference is made.
  475. In such cases the exchange is suspended and resumed when the construction is complete.
  476. --*/
  477. {
  478. NTSTATUS Status;
  479. BOOLEAN fReestablishSession;
  480. BOOLEAN UnInitializeSecurityContext = FALSE;
  481. PMRX_V_NET_ROOT pVNetRoot;
  482. PSMBCEDB_SERVER_ENTRY pServerEntry;
  483. PSMBCEDB_SESSION_ENTRY pSessionEntry;
  484. pVNetRoot = SmbCeGetExchangeVNetRoot(pExchange);
  485. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  486. pSessionEntry = SmbCeGetExchangeSessionEntry(pExchange);
  487. fReestablishSession = (pSessionEntry->Header.State == SMBCEDB_RECOVER) |
  488. BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_ATTEMPT_RECONNECTS);
  489. for (;;) {
  490. ASSERT(pServerEntry->Header.ObjectType == SMBCEDB_OT_SERVER);
  491. ASSERT(pExchange->SmbCeState == SMBCE_EXCHANGE_SERVER_INITIALIZED);
  492. ASSERT(SmbCeGetServerType(pServerEntry) == SMBCEDB_FILE_SERVER);
  493. ASSERT(SmbCeIsResourceOwned());
  494. Status = STATUS_USER_SESSION_DELETED;
  495. if (pSessionEntry == NULL) {
  496. break;
  497. }
  498. switch (pSessionEntry->Header.State) {
  499. case SMBCEDB_ACTIVE:
  500. Status = STATUS_SUCCESS;
  501. break;
  502. case SMBCEDB_INVALID:
  503. if (!fReestablishSession) {
  504. break;
  505. }
  506. pSessionEntry->Session.UserId = 0;
  507. // fall thru ...
  508. case SMBCEDB_RECOVER:
  509. UnInitializeSecurityContext = TRUE;
  510. if (pSessionEntry->Header.State == SMBCEDB_RECOVER) {
  511. ASSERT(pSessionEntry->SessionRecoverInProgress == FALSE);
  512. pSessionEntry->SessionRecoverInProgress = TRUE;
  513. RxLog(("Mark Sess Rec %lx\n",pSessionEntry));
  514. }
  515. if (pSessionEntry->Session.Type == EXTENDED_NT_SESSION){
  516. pSessionEntry->Header.State = SMBCEDB_START_CONSTRUCTION;
  517. if (pExchange->Type != EXTENDED_SESSION_SETUP_EXCHANGE) {
  518. break;
  519. }
  520. }
  521. RxDbgTrace( 0, Dbg, ("SmbCeReferenceSession: Reestablishing session\n"));
  522. // fall thru ...
  523. case SMBCEDB_START_CONSTRUCTION:
  524. if (pSessionEntry->Session.Type != EXTENDED_NT_SESSION ||
  525. pExchange->Type == EXTENDED_SESSION_SETUP_EXCHANGE) {
  526. RxDbgTrace( 0, Dbg, ("SmbCeReferenceSession: Reestablishing session\n"));
  527. ASSERT(SmbCeGetServerType(pServerEntry) == SMBCEDB_FILE_SERVER);
  528. pExchange->SmbCeFlags |= SMBCE_EXCHANGE_SESSION_CONSTRUCTOR;
  529. pSessionEntry->pExchange = pExchange;
  530. pSessionEntry->Header.State = SMBCEDB_CONSTRUCTION_IN_PROGRESS;
  531. SmbCeSerializeSessionSetupRequests(
  532. pSessionEntry);
  533. Status = STATUS_SUCCESS;
  534. if (pExchange->Type == EXTENDED_SESSION_SETUP_EXCHANGE) {
  535. PSMB_EXTENDED_SESSION_SETUP_EXCHANGE pExtSSExchange;
  536. pExtSSExchange = (PSMB_EXTENDED_SESSION_SETUP_EXCHANGE)pExchange;
  537. pExtSSExchange->FirstSessionSetup = TRUE;
  538. }
  539. break;
  540. }
  541. // fall thru ...
  542. case SMBCEDB_CONSTRUCTION_IN_PROGRESS:
  543. if (fReestablishSession) {
  544. // The construction of the session is already in progress ....
  545. // Queue up the request to resume this exchange when the session
  546. // construction is complete.
  547. PSMBCEDB_REQUEST_ENTRY pRequestEntry;
  548. ASSERT(SmbCeGetServerType(pServerEntry) == SMBCEDB_FILE_SERVER);
  549. pRequestEntry = (PSMBCEDB_REQUEST_ENTRY)
  550. SmbMmAllocateObject(SMBCEDB_OT_REQUEST);
  551. if (pRequestEntry != NULL) {
  552. pRequestEntry->Request.pExchange = pExchange;
  553. SmbCeIncrementPendingLocalOperations(pExchange);
  554. SmbCeAddRequestEntry(&pSessionEntry->Requests,pRequestEntry);
  555. Status = STATUS_PENDING;
  556. } else {
  557. Status = STATUS_INSUFFICIENT_RESOURCES;
  558. }
  559. fReestablishSession = FALSE;
  560. }
  561. break;
  562. case SMBCEDB_MARKED_FOR_DELETION:
  563. Status = STATUS_USER_SESSION_DELETED;
  564. break;
  565. default:
  566. ASSERT(!"Valid Session State, SmbCe database corrupt");
  567. Status = STATUS_USER_SESSION_DELETED;
  568. }
  569. if (fReestablishSession &&
  570. (pSessionEntry->Session.Type == EXTENDED_NT_SESSION) &&
  571. (pExchange->Type != EXTENDED_SESSION_SETUP_EXCHANGE) &&
  572. (pSessionEntry->Header.State == SMBCEDB_START_CONSTRUCTION)) {
  573. // Reestablishing a NT50 session cannot be compounded currently. Therefor
  574. // this exchange is suspended till we can reestablish the session. Therefore
  575. PSMB_EXCHANGE pSessionSetupExchange;
  576. SMBCE_RESUMPTION_CONTEXT ExchangeResumptionContext;
  577. RxDbgTrace(0 , Dbg, ("Reestablishing an Extended session %lx\n",pSessionEntry));
  578. pSessionSetupExchange = SmbMmAllocateExchange(EXTENDED_SESSION_SETUP_EXCHANGE,NULL);
  579. SmbCeReleaseResource();
  580. ExchangeResumptionContext.SecuritySignatureReturned = FALSE;
  581. if (pSessionSetupExchange != NULL) {
  582. UninitializeSecurityContextsForSession(&pSessionEntry->Session);
  583. SmbCeInitializeResumptionContext(&ExchangeResumptionContext);
  584. Status = SmbCeInitializeExtendedSessionSetupExchange(
  585. &pSessionSetupExchange,
  586. pExchange->SmbCeContext.pVNetRoot);
  587. if (Status == STATUS_SUCCESS) {
  588. // Attempt to reconnect( In this case it amounts to establishing the
  589. // connection/session)
  590. pSessionSetupExchange->SmbCeFlags |= SMBCE_EXCHANGE_ATTEMPT_RECONNECTS;
  591. pSessionSetupExchange->RxContext = pExchange->RxContext;
  592. ((PSMB_EXTENDED_SESSION_SETUP_EXCHANGE)pSessionSetupExchange)->pResumptionContext
  593. = &ExchangeResumptionContext;
  594. Status = SmbCeInitiateExchange(pSessionSetupExchange);
  595. if (Status == STATUS_PENDING) {
  596. SmbCeSuspend(&ExchangeResumptionContext);
  597. Status = ExchangeResumptionContext.Status;
  598. } else {
  599. SmbCeDiscardExtendedSessionSetupExchange(
  600. (PSMB_EXTENDED_SESSION_SETUP_EXCHANGE)pSessionSetupExchange);
  601. }
  602. } else {
  603. SmbMmFreeExchange(pSessionSetupExchange);
  604. }
  605. RxDbgTrace(0, Dbg, ("Reestablishing a NT50 Session %lx returning STATUS %lx\n",pSessionEntry,Status));
  606. } else {
  607. Status = STATUS_INSUFFICIENT_RESOURCES;
  608. }
  609. SmbCeReferenceSessionEntry(pSessionEntry);
  610. ASSERT(Status != STATUS_SUCCESS ||
  611. pSessionEntry->Header.State == SMBCEDB_CONSTRUCTION_IN_PROGRESS);
  612. pVNetRoot->ConstructionStatus = Status;
  613. SmbCeCompleteSessionEntryInitialization(
  614. pSessionEntry,
  615. Status,
  616. ExchangeResumptionContext.SecuritySignatureReturned);
  617. SmbCeAcquireResource();
  618. if (Status != STATUS_RETRY) {
  619. break;
  620. }
  621. } else {
  622. if (UnInitializeSecurityContext) {
  623. SmbCeReleaseResource();
  624. UninitializeSecurityContextsForSession(&pSessionEntry->Session);
  625. SmbCeAcquireResource();
  626. }
  627. break;
  628. }
  629. }
  630. if ((Status == STATUS_SUCCESS) || (Status == STATUS_PENDING)) {
  631. pExchange->SmbCeState = SMBCE_EXCHANGE_SESSION_INITIALIZED;
  632. }
  633. ASSERT(SmbCeIsResourceOwned());
  634. //ASSERT(Status != STATUS_USER_SESSION_DELETED);
  635. return Status;
  636. }
  637. NTSTATUS
  638. SmbCeReferenceNetRoot(
  639. PSMB_EXCHANGE pExchange)
  640. /*++
  641. Routine Description:
  642. This routine initializes the net root associated with an exchange.
  643. Arguments:
  644. pExchange - the exchange to be initialized.
  645. Return Value:
  646. RXSTATUS - The return status for the operation
  647. Notes:
  648. The initiation of an exchange proceeds in multiple steps. The first step involves
  649. referencing the corresponding server,session and netroot entries. Subsequently the
  650. exchange is placed in a SMB_EXCHANGE_START state and the exchange is dispatched to the
  651. Start method. The act of referencing the session or the net root may suspend the exchange.
  652. The session and net roots are aliased entities, i.e., there is more then one reference
  653. to it. It is conceivable that the construction is in progress when a reference is made.
  654. In such cases the exchange is suspended and resumed when the construction is complete.
  655. --*/
  656. {
  657. NTSTATUS Status;
  658. BOOLEAN fReconnectNetRoot;
  659. PSMBCEDB_SERVER_ENTRY pServerEntry;
  660. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  661. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  662. pVNetRootContext = SmbCeGetAssociatedVNetRootContext(pExchange->SmbCeContext.pVNetRoot);
  663. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  664. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pExchange);
  665. ASSERT(pExchange->SmbCeState == SMBCE_EXCHANGE_SESSION_INITIALIZED);
  666. ASSERT(SmbCeIsResourceOwned());
  667. Status = STATUS_CONNECTION_DISCONNECTED;
  668. fReconnectNetRoot = BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_ATTEMPT_RECONNECTS);
  669. switch (pVNetRootContext->Header.State) {
  670. case SMBCEDB_ACTIVE:
  671. ASSERT(pNetRootEntry->Header.ObjectType == SMBCEDB_OT_NETROOT);
  672. ASSERT(pServerEntry->Header.ObjectType == SMBCEDB_OT_SERVER);
  673. Status = STATUS_SUCCESS;
  674. break;
  675. case SMBCEDB_INVALID:
  676. RxDbgTrace( 0, Dbg, ("SmbCeReferenceNetRoot: Reestablishing net root\n"));
  677. if (!fReconnectNetRoot) {
  678. break;
  679. }
  680. ClearFlag(
  681. pVNetRootContext->Flags,
  682. SMBCE_V_NET_ROOT_CONTEXT_FLAG_VALID_TID);
  683. pVNetRootContext->TreeId = 0;
  684. // fall thru
  685. case SMBCEDB_START_CONSTRUCTION:
  686. pExchange->SmbCeFlags |= SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR;
  687. pVNetRootContext->pExchange = pExchange;
  688. pVNetRootContext->Header.State = SMBCEDB_CONSTRUCTION_IN_PROGRESS;
  689. Status = STATUS_SUCCESS;
  690. break;
  691. case SMBCEDB_CONSTRUCTION_IN_PROGRESS:
  692. if (fReconnectNetRoot) {
  693. // The construction of the net root is already in progress ....
  694. // Queue up the request to resume this exchange when the session
  695. // construction is complete.
  696. PSMBCEDB_REQUEST_ENTRY pRequestEntry;
  697. pRequestEntry = (PSMBCEDB_REQUEST_ENTRY)
  698. SmbMmAllocateObject(SMBCEDB_OT_REQUEST);
  699. if (pRequestEntry != NULL) {
  700. pRequestEntry->Request.pExchange = pExchange;
  701. SmbCeIncrementPendingLocalOperations(pExchange);
  702. SmbCeAddRequestEntry(&pVNetRootContext->Requests,pRequestEntry);
  703. Status = STATUS_PENDING;
  704. } else {
  705. Status = STATUS_INSUFFICIENT_RESOURCES;
  706. }
  707. }
  708. break;
  709. case SMBCEDB_MARKED_FOR_DELETION:
  710. break;
  711. default:
  712. ASSERT(!"Valid NetRoot State, SmbCe database corrupt");
  713. break;
  714. }
  715. if ((Status == STATUS_SUCCESS) || (Status == STATUS_PENDING)) {
  716. pExchange->SmbCeState = SMBCE_EXCHANGE_NETROOT_INITIALIZED;
  717. }
  718. ASSERT(SmbCeIsResourceOwned());
  719. return Status;
  720. }
  721. NTSTATUS
  722. SmbCeInitiateExchange(
  723. PSMB_EXCHANGE pExchange)
  724. /*++
  725. Routine Description:
  726. This routine inititaes a exchange.
  727. Arguments:
  728. pExchange - the exchange to be initiated.
  729. Return Value:
  730. RXSTATUS - The return status for the operation
  731. Notes:
  732. The initiation of an exchange proceeds in multiple steps. The first step involves
  733. referencing the corresponding server,session and netroot entries. Subsequently the
  734. exchange is placed in a SMB_EXCHANGE_START state and the exchange is dispatched to the
  735. Start method. The act of referencing the session or the net root may suspend the exchange.
  736. --*/
  737. {
  738. NTSTATUS Status = STATUS_SUCCESS;
  739. PSMBCEDB_SERVER_ENTRY pServerEntry;
  740. PSMBCEDB_SESSION_ENTRY pSessionEntry;
  741. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  742. PKEVENT pSmbCeSynchronizationEvent;
  743. PAGED_CODE();
  744. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  745. pSessionEntry = SmbCeGetExchangeSessionEntry(pExchange);
  746. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pExchange);
  747. ASSERT(pServerEntry != NULL);
  748. ASSERT(!FlagOn(pExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE));
  749. switch (SmbCeGetServerType(pServerEntry)) {
  750. case SMBCEDB_FILE_SERVER:
  751. // If this is a mailslot write, then don't abort......
  752. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_MAILSLOT_OPERATION) {
  753. break;
  754. }
  755. // Admin exchanges do not have these fields filled in. All the three
  756. // entries must be valid for all other exchanges.
  757. if ((pExchange->NodeTypeCode != SMB_EXCHANGE_NTC(ADMIN_EXCHANGE)) &&
  758. ((pNetRootEntry == NULL) ||
  759. (pSessionEntry == NULL))) {
  760. Status = STATUS_REQUEST_ABORTED;
  761. break;
  762. }
  763. case SMBCEDB_MAILSLOT_SERVER:
  764. break;
  765. default:
  766. // Prepare for aborting the request if either the server type is invalid
  767. // or if the netroot entry or the session entry is invalid.
  768. Status = STATUS_REQUEST_ABORTED;
  769. }
  770. if (Status != STATUS_SUCCESS) {
  771. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: Exchange %lx Status %lx\n",pExchange,Status));
  772. return Status;
  773. }
  774. pSmbCeSynchronizationEvent = pExchange->pSmbCeSynchronizationEvent;
  775. if (pSmbCeSynchronizationEvent != NULL) {
  776. KeInitializeEvent(
  777. pSmbCeSynchronizationEvent,
  778. SynchronizationEvent,
  779. FALSE);
  780. }
  781. for (;;) {
  782. SmbCeAcquireResource();
  783. switch (pExchange->SmbCeState) {
  784. case SMBCE_EXCHANGE_INITIALIZATION_START:
  785. {
  786. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: Exchange %lx State %lx\n",pExchange,pExchange->SmbCeState));
  787. Status = SmbCeReferenceServer(pExchange);
  788. if (Status != STATUS_SUCCESS) {
  789. // this covers the case when the SERVER_ENTRY is under construction
  790. // and RxStatus(PENDING) is returned.
  791. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: SmbCeReferenceServer returned %lx\n",Status));
  792. break;
  793. }
  794. }
  795. // fall through
  796. case SMBCE_EXCHANGE_SERVER_INITIALIZED:
  797. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_MAILSLOT_OPERATION) {
  798. // Mailslot servers do not have any netroot/session associated with them.
  799. pExchange->SmbCeState = SMBCE_EXCHANGE_INITIATED;
  800. Status = STATUS_SUCCESS;
  801. break;
  802. } else {
  803. if (pSessionEntry->SessionRecoverInProgress ||
  804. pServerEntry->SecuritySignaturesEnabled &&
  805. !pServerEntry->SecuritySignaturesActive) {
  806. // if security signature is enabled and not yet turned on, exchange should wait for
  807. // outstanding extended session setup to finish before resume in order to avoid index mismatch.
  808. RxLog(("Sync for Sess %lx\n",pExchange));
  809. Status = SmbCeSyncExchangeForSecuritySignature(pExchange);
  810. }
  811. if (Status == STATUS_SUCCESS) {
  812. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: Exchange %lx State %lx\n",pExchange,pExchange->SmbCeState));
  813. Status = SmbCeReferenceSession(pExchange);
  814. if (!NT_SUCCESS(Status)) {
  815. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: SmbCeReferenceSession returned %lx\n",Status));
  816. break;
  817. } if ((Status == STATUS_PENDING) &&
  818. !(pExchange->SmbCeFlags & SMBCE_EXCHANGE_SESSION_CONSTRUCTOR)) {
  819. break;
  820. }
  821. } else {
  822. break;
  823. }
  824. }
  825. // fall through
  826. case SMBCE_EXCHANGE_SESSION_INITIALIZED:
  827. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: Exchange %lx State %lx\n",pExchange,pExchange->SmbCeState));
  828. if (pExchange->Type != EXTENDED_SESSION_SETUP_EXCHANGE) {
  829. Status = SmbCeReferenceNetRoot(pExchange);
  830. if (!NT_SUCCESS(Status)) {
  831. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: SmbCeReferenceNetRoot returned %lx\n",Status));
  832. break;
  833. } else if ((Status == STATUS_PENDING) &&
  834. !(pExchange->SmbCeFlags & SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR)) {
  835. break;
  836. }
  837. }
  838. // else fall through
  839. case SMBCE_EXCHANGE_NETROOT_INITIALIZED:
  840. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: Exchange %lx State %lx\n",pExchange,pExchange->SmbCeState));
  841. if (pServerEntry->SecuritySignaturesEnabled &&
  842. !(pExchange->SmbCeFlags & SMBCE_EXCHANGE_MAILSLOT_OPERATION)) {
  843. Status = SmbCeAllocateBufferForServerResponse(pExchange);
  844. if (Status != STATUS_SUCCESS) {
  845. // this covers the case when the buffer for server response cannot be allocated
  846. // and RxStatus(PENDING) is returned.
  847. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: SmbCeAllocateBufferForServerResponse returned %lx\n",Status));
  848. break;
  849. }
  850. }
  851. // else fall throungh
  852. case SMBCE_EXCHANGE_SECURITYBUFFER_INITIALIZED:
  853. {
  854. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  855. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pExchange);
  856. // Exchange should have timeout flag set unless this is a pipe operation
  857. // or the SMBCE_ECXCHANGE_INDEFINITE_DELAY_IN_RESPONSE flag is set
  858. if(((pNetRootEntry == NULL) || (pNetRootEntry->NetRoot.NetRootType != NET_ROOT_PIPE)) &&
  859. !BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_INDEFINITE_DELAY_IN_RESPONSE)) {
  860. pExchange->SmbCeFlags |= SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION;
  861. }
  862. pExchange->SmbCeState = SMBCE_EXCHANGE_INITIATED;
  863. Status = STATUS_SUCCESS;
  864. }
  865. break;
  866. default:
  867. ASSERT(!"Valid State for a SMB exchange, exchange Initiation aborted");
  868. break;
  869. }
  870. SmbCeReleaseResource();
  871. if ((pSmbCeSynchronizationEvent != NULL) &&
  872. (pExchange->SmbCeState != SMBCE_EXCHANGE_INITIATED) &&
  873. (Status == STATUS_PENDING)) {
  874. KeWaitForSingleObject(
  875. pSmbCeSynchronizationEvent,
  876. Executive,
  877. KernelMode,
  878. FALSE,
  879. NULL );
  880. ASSERT(pExchange->Status != RX_MAP_STATUS(PENDING));
  881. Status = pExchange->Status;
  882. if (Status != RX_MAP_STATUS(SUCCESS)) {
  883. break;
  884. }
  885. } else {
  886. break;
  887. }
  888. }
  889. ASSERT((Status != STATUS_PENDING) ||
  890. (pSmbCeSynchronizationEvent == NULL));
  891. RxDbgTrace(0,Dbg,("Exchange (%lx) Type (%lx) State(%lx) Status %lx \n",pExchange,pExchange->Type,pExchange->SmbCeState,Status));
  892. RxDbgTrace(0,Dbg,
  893. ("ServerEntry(%lx) SessionEntry(%lx) NetRootEntry(%lx) \n",
  894. pServerEntry,
  895. pSessionEntry,
  896. pNetRootEntry));
  897. // Note: Once the exchange has been initiated no further reference of the exchange
  898. // can be done since the state of the exchange is non-deterministic, i.e., depends upon
  899. // the scheduler.
  900. if (Status == STATUS_SUCCESS) {
  901. BOOLEAN ResourceReleased = FALSE;
  902. // Start the exchange
  903. ASSERT(pExchange->SmbCeState == SMBCE_EXCHANGE_INITIATED);
  904. SmbCeAcquireResource();
  905. if ((pServerEntry->Header.State == SMBCEDB_ACTIVE) ||
  906. (pExchange->NodeTypeCode == SMB_EXCHANGE_NTC(ADMIN_EXCHANGE)) ||
  907. (FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_MAILSLOT_OPERATION))) {
  908. Status = SmbCeInitializeExchangeTransport(pExchange);
  909. } else {
  910. Status = STATUS_CONNECTION_DISCONNECTED;
  911. }
  912. if (Status == STATUS_SUCCESS) {
  913. if (pExchange->RxContext != NULL) {
  914. PMRXSMB_RX_CONTEXT pMRxSmbContext;
  915. // Set up the cancellation routine ..
  916. pMRxSmbContext = MRxSmbGetMinirdrContext(pExchange->RxContext);
  917. pMRxSmbContext->pCancelContext = pExchange;
  918. Status = RxSetMinirdrCancelRoutine(
  919. pExchange->RxContext,
  920. SmbCeCancelExchange);
  921. }
  922. if (Status == STATUS_SUCCESS) {
  923. if (!IsListEmpty(&pExchange->ExchangeList)) {
  924. RemoveEntryList(&pExchange->ExchangeList);
  925. }
  926. InsertTailList(
  927. &pServerEntry->ActiveExchanges,
  928. &pExchange->ExchangeList);
  929. SmbCeReleaseResource();
  930. ResourceReleased = TRUE;
  931. pExchange->SmbStatus = STATUS_SUCCESS;
  932. pExchange->ServerVersion = pServerEntry->Server.Version;
  933. Status = SMB_EXCHANGE_DISPATCH(pExchange,Start,((PSMB_EXCHANGE)pExchange));
  934. }
  935. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: SMB_EXCHANGE_DISPATCH(Start) returned %lx\n",Status));
  936. }
  937. if (!ResourceReleased) {
  938. SmbCeReleaseResource();
  939. }
  940. } else if (Status != STATUS_PENDING) {
  941. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: Exchange(%lx) Initiation failed %lx \n",pExchange,Status));
  942. }
  943. return Status;
  944. }
  945. NTSTATUS
  946. SmbCeInitiateAssociatedExchange(
  947. PSMB_EXCHANGE pExchange,
  948. BOOLEAN EnableCompletionHandlerInMasterExchange)
  949. /*++
  950. Routine Description:
  951. This routine inititaes an associated exchange.
  952. Arguments:
  953. pExchange - the exchange to be initiated.
  954. Return Value:
  955. NTSTATUS - The return status for the operation
  956. Notes:
  957. --*/
  958. {
  959. NTSTATUS Status = STATUS_SUCCESS;
  960. PSMB_EXCHANGE pMasterExchange;
  961. PSMBCEDB_SERVER_ENTRY pServerEntry;
  962. PAGED_CODE();
  963. ASSERT(pExchange->SmbCeState == SMBCE_EXCHANGE_INITIATED);
  964. ASSERT(FlagOn(pExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE));
  965. pMasterExchange = pExchange->Associated.pMasterExchange;
  966. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  967. ASSERT(pServerEntry != NULL);
  968. // Note: Once the exchange has been initiated no further reference of the exchange
  969. // can be done since the state of the exchange is non-deterministic, i.e., depends upon
  970. // the scheduler.
  971. Status = SmbCeInitializeExchangeTransport(pExchange);
  972. SmbCeAcquireResource();
  973. if (!IsListEmpty(&pExchange->ExchangeList)) {
  974. RemoveEntryList(&pExchange->ExchangeList);
  975. }
  976. InsertTailList(
  977. &pServerEntry->ActiveExchanges,
  978. &pExchange->ExchangeList);
  979. if (EnableCompletionHandlerInMasterExchange) {
  980. ASSERT(!FlagOn(
  981. pMasterExchange->SmbCeFlags,
  982. SMBCE_ASSOCIATED_EXCHANGES_COMPLETION_HANDLER_ACTIVATED));
  983. SetFlag(
  984. pMasterExchange->SmbCeFlags,
  985. SMBCE_ASSOCIATED_EXCHANGES_COMPLETION_HANDLER_ACTIVATED);
  986. }
  987. pExchange->SmbStatus = STATUS_SUCCESS;
  988. pExchange->ServerVersion = pServerEntry->Server.Version;
  989. SmbCeReleaseResource();
  990. if (Status == STATUS_SUCCESS) {
  991. Status = SMB_EXCHANGE_DISPATCH(pExchange,Start,((PSMB_EXCHANGE)pExchange));
  992. RxDbgTrace( 0, Dbg, ("SmbCeInitiateExchange: SMB_EXCHANGE_DISPATCH(Start) returned %lx\n",Status));
  993. } else {
  994. SmbCeFinalizeExchange(pExchange);
  995. Status = STATUS_PENDING;
  996. }
  997. return Status;
  998. }
  999. NTSTATUS
  1000. SmbCeExchangeAbort(
  1001. PSMB_EXCHANGE pExchange)
  1002. /*++
  1003. Routine Description:
  1004. This routine aborts an exchange.
  1005. Arguments:
  1006. pExchange - the exchange to be aborted.
  1007. Return Value:
  1008. RXSTATUS - The return status for the operation
  1009. Notes:
  1010. --*/
  1011. {
  1012. PAGED_CODE();
  1013. RxDbgTrace( 0, Dbg, ("SmbCeExchangeAbort: Exchange %lx aborted\n",pExchange));
  1014. SmbCeDiscardExchange(pExchange);
  1015. return STATUS_SUCCESS;
  1016. }
  1017. NTSTATUS
  1018. SmbCeBuildSmbHeader(
  1019. IN OUT PSMB_EXCHANGE pExchange,
  1020. IN OUT PVOID pBuffer,
  1021. IN ULONG BufferLength,
  1022. OUT PULONG pBufferConsumed,
  1023. OUT PUCHAR pLastCommandInHeader,
  1024. OUT PUCHAR *pNextCommandPtr)
  1025. /*++
  1026. Routine Description:
  1027. This routine constructs the SMB header associated with any SMB sent as part of
  1028. an exchange.
  1029. Arguments:
  1030. pExchange - the exchange for which the SMB is to be constructed.
  1031. pBuffer - the buffer in whihc the SMB header is to be constructed
  1032. BufferLength - length of the buffer
  1033. pBufferConsumed - the buffer consumed
  1034. pLastCommandInHeader - the last command in header, SMB_COM_NO_ANDX_COMMAND if none
  1035. pNextCommandPtr - the ptr to the place in the buffer where the next command
  1036. code should be copied.
  1037. Return Value:
  1038. STATUS_SUCCESS - if the header construction was successful
  1039. Notes:
  1040. This routine is called to build the SMB header. This centralization allows us to
  1041. compound the SMB operation with other SMB's required for the maintenance of the
  1042. SMB connection engine data structures. It also provides us with a centralized facility
  1043. for profiling SMB's as well as a one place mechanism for filling in all the header
  1044. fields associated with a SMB.
  1045. --*/
  1046. {
  1047. NTSTATUS Status = STATUS_SUCCESS;
  1048. PSMB_HEADER pSmbHeader = (PSMB_HEADER)pBuffer;
  1049. PGENERIC_ANDX pSmbBuffer;
  1050. ULONG SmbBufferUnconsumed = BufferLength;
  1051. PUCHAR pSmbCommand;
  1052. PRX_CONTEXT RxContext;
  1053. UCHAR LastCommandInHeader = SMB_COM_NO_ANDX_COMMAND;
  1054. UCHAR Flags = SMB_FLAGS_CASE_INSENSITIVE | SMB_FLAGS_CANONICALIZED_PATHS;
  1055. USHORT Flags2 = 0;
  1056. PSMBCEDB_SERVER_ENTRY pServerEntry;
  1057. PSMBCEDB_SESSION_ENTRY pSessionEntry;
  1058. PSMBCE_SERVER pServer;
  1059. PAGED_CODE();
  1060. if (BufferLength < sizeof(SMB_HEADER)) {
  1061. RxDbgTrace( 0, Dbg, ("SmbCeBuildSmbHeader: BufferLength too small %d\n",BufferLength));
  1062. ASSERT(!"Buffer too small");
  1063. return STATUS_BUFFER_TOO_SMALL;
  1064. }
  1065. SmbBufferUnconsumed = BufferLength - sizeof(SMB_HEADER);
  1066. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  1067. pSessionEntry = SmbCeGetExchangeSessionEntry(pExchange);
  1068. pServer = SmbCeGetExchangeServer(pExchange);
  1069. RxContext = pExchange->RxContext;
  1070. if (pServer->Dialect == NTLANMAN_DIALECT) {
  1071. if (FlagOn(pServer->DialectFlags,DF_NT_SMBS)) {
  1072. Flags2 |= (SMB_FLAGS2_KNOWS_EAS | SMB_FLAGS2_EXTENDED_SECURITY);
  1073. if ((pSessionEntry != NULL) &&
  1074. (FlagOn(pSessionEntry->Session.Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION) ||
  1075. MRxSmbUseKernelModeSecurity)) {
  1076. Flags2 &= ~SMB_FLAGS2_EXTENDED_SECURITY;
  1077. }
  1078. }
  1079. if (FlagOn(pServer->DialectFlags,DF_NT_STATUS)) {
  1080. Flags2 |= SMB_FLAGS2_NT_STATUS;
  1081. }
  1082. if( RxContext &&
  1083. (RxContext->pFcb) &&
  1084. (RxContext->pFcb->FcbState & FCB_STATE_SPECIAL_PATH) )
  1085. {
  1086. Flags2 |= SMB_FLAGS2_REPARSE_PATH;
  1087. }
  1088. }
  1089. if (!FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_MAILSLOT_OPERATION)) {
  1090. if (FlagOn(pServer->DialectFlags,DF_UNICODE)) {
  1091. Flags2 |= SMB_FLAGS2_UNICODE;
  1092. }
  1093. }
  1094. if (FlagOn(pServer->DialectFlags,DF_LONGNAME)) {
  1095. Flags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
  1096. }
  1097. if (FlagOn(pServer->DialectFlags,DF_SUPPORTEA)) {
  1098. Flags2 |= SMB_FLAGS2_KNOWS_EAS;
  1099. }
  1100. if (MRxSmbSecuritySignaturesEnabled) {
  1101. Flags2 |= SMB_FLAGS2_SMB_SECURITY_SIGNATURE;
  1102. }
  1103. //DOWNLEVEL.NOTCORE flags for lanman10
  1104. RtlZeroMemory(pSmbHeader,sizeof(SMB_HEADER));
  1105. *((PULONG)&pSmbHeader->Protocol) = SMB_HEADER_PROTOCOL;
  1106. pSmbHeader->Flags = Flags;
  1107. pSmbHeader->Flags2 = Flags2;
  1108. pSmbHeader->Pid = MRXSMB_PROCESS_ID;
  1109. pSmbHeader->Uid = 0;
  1110. pSmbHeader->Tid = 0;
  1111. pSmbHeader->ErrorClass = 0;
  1112. pSmbHeader->Reserved = 0;
  1113. pSmbCommand = &pSmbHeader->Command;
  1114. SmbPutUshort(&pSmbHeader->Error,0);
  1115. switch (SmbCeGetServerType(pServerEntry)) {
  1116. case SMBCEDB_MAILSLOT_SERVER :
  1117. break;
  1118. case SMBCEDB_FILE_SERVER:
  1119. {
  1120. BOOLEAN fValidTid;
  1121. if (pSessionEntry != NULL) {
  1122. pSmbHeader->Uid = pSessionEntry->Session.UserId;
  1123. }
  1124. if (pExchange->SmbCeContext.pVNetRoot != NULL) {
  1125. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  1126. pVNetRootContext = SmbCeGetAssociatedVNetRootContext(
  1127. pExchange->SmbCeContext.pVNetRoot);
  1128. fValidTid = BooleanFlagOn(
  1129. pVNetRootContext->Flags,
  1130. SMBCE_V_NET_ROOT_CONTEXT_FLAG_VALID_TID);
  1131. pSmbHeader->Tid = pVNetRootContext->TreeId;
  1132. } else {
  1133. fValidTid = TRUE;
  1134. }
  1135. pSmbBuffer = (PGENERIC_ANDX)(pSmbHeader + 1);
  1136. if ((pExchange->SmbCeFlags & SMBCE_EXCHANGE_SESSION_CONSTRUCTOR) ||
  1137. (pExchange->SmbCeFlags & SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR)) {
  1138. // There is an oppurtunity to compound some SessionSetup/TreeConnect SMB with the
  1139. // given SMB command.
  1140. if ((pExchange->SmbCeFlags & SMBCE_EXCHANGE_SESSION_CONSTRUCTOR) &&
  1141. (pSessionEntry->Header.State == SMBCEDB_CONSTRUCTION_IN_PROGRESS)) {
  1142. if (( pServer->DialectFlags & DF_EXTENDNEGOT) ||
  1143. ( pServer->DialectFlags & DF_NTNEGOTIATE)) {
  1144. RxDbgTrace( 0, Dbg, ("SmbCeBuildSmbHeader: Building Session setup And X\n"));
  1145. *pSmbCommand = SMB_COM_SESSION_SETUP_ANDX;
  1146. LastCommandInHeader = *pSmbCommand;
  1147. pSmbCommand = &pSmbBuffer->AndXCommand;
  1148. pSmbHeader->Tid = 0;
  1149. Status = SMBCE_SERVER_DIALECT_DISPATCH(
  1150. pServer,
  1151. BuildSessionSetup,
  1152. (pExchange,
  1153. pSmbBuffer,
  1154. &SmbBufferUnconsumed));
  1155. if (NT_SUCCESS(Status)) {
  1156. // Update the buffer for the construction of the following SMB.
  1157. SmbPutUshort(
  1158. &pSmbBuffer->AndXOffset,
  1159. (USHORT)(BufferLength - SmbBufferUnconsumed));
  1160. pSmbBuffer = (PGENERIC_ANDX)((PBYTE)pBuffer + BufferLength - SmbBufferUnconsumed);
  1161. if (pServerEntry->SecuritySignaturesEnabled &&
  1162. !pServerEntry->SecuritySignaturesActive) {
  1163. RtlCopyMemory(pSmbHeader->SecuritySignature,InitialSecuritySignature,SMB_SECURITY_SIGNATURE_LENGTH);
  1164. }
  1165. }
  1166. }
  1167. } else {
  1168. NOTHING; //no sess for share level AT LEAST NOT FOR CORE!!!
  1169. }
  1170. if (NT_SUCCESS(Status) &&
  1171. (pExchange->SmbCeFlags & SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR) &&
  1172. !fValidTid) {
  1173. BOOLEAN BuildingTreeConnectAndX = BooleanFlagOn(pServer->DialectFlags,DF_LANMAN10);
  1174. //CODE.IMPROVEMENT this is not wholly satisfactory....we have encapsulated which smb we're building
  1175. // in the dialect dispatch vector and yet we're setting the smb externally.
  1176. if (BuildingTreeConnectAndX) {
  1177. RxDbgTrace( 0, Dbg, ("SmbCeBuildSmbHeader: Building Tree Connect And X\n"));
  1178. *pSmbCommand = SMB_COM_TREE_CONNECT_ANDX;
  1179. LastCommandInHeader = *pSmbCommand;
  1180. } else {
  1181. RxDbgTrace( 0, Dbg, ("SmbCeBuildSmbHeader: Building Tree Connect No X\n"));
  1182. *pSmbCommand = SMB_COM_TREE_CONNECT;
  1183. LastCommandInHeader = *pSmbCommand;
  1184. }
  1185. Status = SMBCE_SERVER_DIALECT_DISPATCH(
  1186. pServer,
  1187. BuildTreeConnect,
  1188. (pExchange,
  1189. pSmbBuffer,
  1190. &SmbBufferUnconsumed));
  1191. if (NT_SUCCESS(Status)) {
  1192. // Update the buffer for the construction of the following SMB.
  1193. if (BuildingTreeConnectAndX) {
  1194. pSmbCommand = &pSmbBuffer->AndXCommand;
  1195. SmbPutUshort(&pSmbBuffer->AndXOffset,(USHORT)(BufferLength - SmbBufferUnconsumed));
  1196. } else {
  1197. pSmbCommand = NULL;
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. break;
  1204. default:
  1205. {
  1206. ASSERT(!"Valid Server Type");
  1207. Status = STATUS_INVALID_HANDLE;
  1208. }
  1209. break;
  1210. }
  1211. *pNextCommandPtr = pSmbCommand;
  1212. *pBufferConsumed = BufferLength - SmbBufferUnconsumed;
  1213. *pLastCommandInHeader = LastCommandInHeader;
  1214. RxDbgTrace( 0, Dbg, ("SmbCeBuildSmbHeader: Buffer Consumed %lx\n",*pBufferConsumed));
  1215. if (Status != STATUS_SUCCESS) {
  1216. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_SESSION_CONSTRUCTOR) {
  1217. pExchange->SessionSetupStatus = Status;
  1218. }
  1219. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR) {
  1220. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  1221. pVNetRootContext = SmbCeGetExchangeVNetRootContext(pExchange);
  1222. SmbCeUpdateVNetRootContextState(
  1223. pVNetRootContext,
  1224. SMBCEDB_INVALID);
  1225. }
  1226. }
  1227. return Status;
  1228. }
  1229. typedef struct __Service_Name_Entry {
  1230. NET_ROOT_TYPE NetRootType;
  1231. USHORT NameLength;
  1232. PBYTE Name;
  1233. };
  1234. struct __Service_Name_Entry ServiceNameTable[] = {
  1235. {NET_ROOT_DISK,sizeof(SHARE_TYPE_NAME_DISK),SHARE_TYPE_NAME_DISK},
  1236. {NET_ROOT_PIPE,sizeof(SHARE_TYPE_NAME_PIPE),SHARE_TYPE_NAME_PIPE},
  1237. {NET_ROOT_PRINT,sizeof(SHARE_TYPE_NAME_PRINT),SHARE_TYPE_NAME_PRINT},
  1238. {NET_ROOT_COMM,sizeof(SHARE_TYPE_NAME_COMM),SHARE_TYPE_NAME_COMM} //COMM must be last
  1239. };
  1240. UNICODE_STRING FileSystem_NTFS_UNICODE = {8,8,L"NTFS"};
  1241. UNICODE_STRING FileSystem_FAT_UNICODE = {6,6,L"FAT"};
  1242. CHAR FileSystem_NTFS[] = "NTFS";
  1243. CHAR FileSystem_FAT[] = "FAT";
  1244. NTSTATUS
  1245. SmbCeParseSmbHeader(
  1246. PSMB_EXCHANGE pExchange,
  1247. PSMB_HEADER pSmbHeader,
  1248. PGENERIC_ANDX pCommandToProcess,
  1249. NTSTATUS *pSmbResponseStatus,
  1250. ULONG BytesAvailable,
  1251. ULONG BytesIndicated,
  1252. PULONG pBytesConsumed)
  1253. /*++
  1254. Routine Description:
  1255. This routine validates the SMB header associated with any SMB received as part of
  1256. an exchange.
  1257. Arguments:
  1258. pExchange - the exchange for which the SMB is to be constructed.
  1259. pSmbHeader - the header of the SMB received
  1260. pCommandToProcess - the SMB command to be processed after the header ( Can be NULL )
  1261. pSmbResponseStatus - the status in the SMB response header (Can be NULL)
  1262. BytesAvailable - the bytes available for processing but not necesarily indicated.
  1263. BytesIndicated - the length of the SMB buffer avcailable for perusal
  1264. pBytesConsumed - the buffer consumed
  1265. Return Value:
  1266. RXSTATUS - The return status for the operation
  1267. STATUS_MORE_PROCESSING_REQUIRED -- if a copy of the data needs to be done before
  1268. processing can be completed. This occurs because sufficient data was not
  1269. indicated to process the header.
  1270. STATUS_SUCCESS -- the header was processed succesfully. In such cases the GENERIC_ANDX
  1271. if not NULL will contain the offset from the start of the buffer and the command
  1272. to be processed.
  1273. STATUS_* -- They indicate an error which would normally lead to the abortion of the
  1274. exchange.
  1275. Notes:
  1276. This routine is called to parse the SMB header. This centralization allows us to
  1277. implement a one stop mechanism for updateing/validating the header fields as well as
  1278. resuming the exchanges waiting for the construction of session/net root entry
  1279. associated with this exchange
  1280. --*/
  1281. {
  1282. NTSTATUS Status = STATUS_SUCCESS;
  1283. NTSTATUS SmbResponseStatus;
  1284. PBYTE pSmbBuffer = (PBYTE)pSmbHeader;
  1285. UCHAR SmbCommand;
  1286. BOOLEAN fUpdateVNetRootContext = FALSE;
  1287. SMBCEDB_OBJECT_STATE SessionState;
  1288. SMBCEDB_OBJECT_STATE NetRootState;
  1289. PMRX_V_NET_ROOT pVNetRoot;
  1290. PSMBCEDB_SERVER_ENTRY pServerEntry;
  1291. PSMBCEDB_SESSION_ENTRY pSessionEntry;
  1292. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  1293. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  1294. pVNetRoot = SmbCeGetExchangeVNetRoot(pExchange);
  1295. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  1296. pSessionEntry = SmbCeGetExchangeSessionEntry(pExchange);
  1297. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pExchange);
  1298. pVNetRootContext = SmbCeGetExchangeVNetRootContext(pExchange);
  1299. // Return Immediately if bytes indicated is less then the size of a SMB header.
  1300. if (BytesIndicated < sizeof(SMB_HEADER)) {
  1301. *pBytesConsumed = BytesIndicated;
  1302. return STATUS_INVALID_NETWORK_RESPONSE;
  1303. }
  1304. SmbResponseStatus = GetSmbResponseNtStatus(pSmbHeader,pExchange);
  1305. if (!NT_SUCCESS(SmbResponseStatus)) {
  1306. RxDbgTrace( 0, Dbg, ("SmbCeParseSmbHeader::SMB Response Error %lx\n",SmbResponseStatus));
  1307. }
  1308. SmbCommand = pSmbHeader->Command;
  1309. *pBytesConsumed = sizeof(SMB_HEADER);
  1310. pSmbBuffer += *pBytesConsumed;
  1311. if (SmbResponseStatus == STATUS_NETWORK_SESSION_EXPIRED) {
  1312. // if the session has been timed out on the server, establish the session and retry the operation
  1313. SmbResponseStatus = STATUS_RETRY;
  1314. InterlockedCompareExchange(&(pSessionEntry->Header.State),
  1315. SMBCEDB_RECOVER,
  1316. SMBCEDB_ACTIVE);
  1317. //DbgPrint("Session timed out on request %x\n", SmbCommand);
  1318. }
  1319. // There are certain SMB's that effect the connection engine data structures as
  1320. // well as the exchange that has been suspended. These are the SMB's used for tree
  1321. // connect and session setup.
  1322. // In all the other cases no special action is required for the maintenance of the
  1323. // connection engine data structures. The Exchange that was suspended needs to be
  1324. // resumed.
  1325. if (SmbCommand == SMB_COM_SESSION_SETUP_ANDX) {
  1326. if (SmbResponseStatus != RX_MAP_STATUS(SUCCESS)) {
  1327. if ((FIELD_OFFSET(GENERIC_ANDX,AndXReserved) + *pBytesConsumed) <= BytesIndicated) {
  1328. PGENERIC_ANDX pGenericAndX = (PGENERIC_ANDX)pSmbBuffer;
  1329. if (pGenericAndX->WordCount == 0) {
  1330. Status = SmbResponseStatus;
  1331. }
  1332. pExchange->SessionSetupStatus = SmbResponseStatus;
  1333. }
  1334. // Note that the case wherein sufficient bytes are not indicated for the
  1335. // GENERIC_ANDX response is handled by the if statement below which
  1336. // imposes a more stringent test.
  1337. }
  1338. if ((Status == STATUS_SUCCESS) &&
  1339. (FIELD_OFFSET(RESP_SESSION_SETUP_ANDX,Buffer) + *pBytesConsumed) > BytesIndicated) {
  1340. Status = STATUS_MORE_PROCESSING_REQUIRED;
  1341. }
  1342. if (Status == STATUS_SUCCESS) {
  1343. PRESP_SESSION_SETUP_ANDX pSessionSetupResponse;
  1344. ULONG SessionSetupResponseLength,ByteCount;
  1345. RxDbgTrace( 0, Dbg, ("Processing Session Setup ANd X\n"));
  1346. pSessionSetupResponse = (PRESP_SESSION_SETUP_ANDX)(pSmbBuffer);
  1347. ByteCount = SmbGetUshort(&pSessionSetupResponse->ByteCount);
  1348. if (pSessionSetupResponse->WordCount == 3) {
  1349. SmbCommand = pSessionSetupResponse->AndXCommand;
  1350. if (SmbCommand == SMB_COM_NO_ANDX_COMMAND) {
  1351. SessionSetupResponseLength =
  1352. FIELD_OFFSET(RESP_SESSION_SETUP_ANDX,Buffer) + ByteCount;
  1353. Status = SmbResponseStatus;
  1354. } else {
  1355. SessionSetupResponseLength =
  1356. SmbGetUshort(&pSessionSetupResponse->AndXOffset) - *pBytesConsumed;
  1357. }
  1358. //if (ByteCount == 0) {
  1359. // //bytecount==0 and NTDIALECT means that this is really w95...change the flags
  1360. // PSMBCE_SERVER pServer = &pExchange->SmbCeContext.pServerEntry->Server;
  1361. // if (FlagOn(pServer->DialectFlags,DF_NTPROTOCOL)) {
  1362. // pServer->DialectFlags &= ~(DF_MIXEDCASEPW);
  1363. // pServer->DialectFlags |= DF_W95;
  1364. // }
  1365. //}
  1366. } else {
  1367. // NT session setup is handled by another routine.
  1368. Status = STATUS_INVALID_NETWORK_RESPONSE;
  1369. }
  1370. if (NT_SUCCESS(Status)) {
  1371. if (SessionSetupResponseLength + *pBytesConsumed <= BytesIndicated) {
  1372. *pBytesConsumed += SessionSetupResponseLength;
  1373. pSmbBuffer += SessionSetupResponseLength;
  1374. pSessionEntry->Session.UserId = pSmbHeader->Uid;
  1375. if (FlagOn(SmbGetUshort(&pSessionSetupResponse->Action), SMB_SETUP_USE_LANMAN_KEY)) {
  1376. pSessionEntry->Session.Flags |=
  1377. SMBCE_SESSION_FLAGS_LANMAN_SESSION_KEY_USED;
  1378. }
  1379. if (FlagOn(SmbGetUshort(&pSessionSetupResponse->Action), SMB_SETUP_GUEST)) {
  1380. pSessionEntry->Session.Flags |=
  1381. SMBCE_SESSION_FLAGS_GUEST_SESSION;
  1382. }
  1383. if (pServerEntry->SecuritySignaturesEnabled &&
  1384. !pServerEntry->SecuritySignaturesActive &&
  1385. RtlCompareMemory(pSmbHeader->SecuritySignature,
  1386. InitialSecuritySignature,
  1387. SMB_SECURITY_SIGNATURE_LENGTH) != SMB_SECURITY_SIGNATURE_LENGTH) {
  1388. pExchange->SecuritySignatureReturned = TRUE;
  1389. }
  1390. InterlockedIncrement(&MRxSmbStatistics.Sessions);
  1391. } else {
  1392. Status = STATUS_MORE_PROCESSING_REQUIRED;
  1393. }
  1394. } else {
  1395. RxDbgTrace( 0, Dbg, ("SmbCeParseSmbHeader::Session setup and X Response %lx\n",Status));
  1396. pExchange->SessionSetupStatus = Status;
  1397. InterlockedIncrement(&MRxSmbStatistics.FailedSessions);
  1398. if ((SmbCommand == SMB_COM_TREE_CONNECT_ANDX) ||
  1399. (SmbCommand == SMB_COM_TREE_CONNECT)) {
  1400. RxDbgTrace( 0, Dbg, ("SmbCeParseSmbHeader:: Tearing down a tree connection\n"));
  1401. fUpdateVNetRootContext = TRUE;
  1402. NetRootState = SMBCEDB_INVALID;
  1403. }
  1404. }
  1405. }
  1406. }
  1407. if ((SmbCommand == SMB_COM_TREE_CONNECT_ANDX) &&
  1408. NT_SUCCESS(Status)) {
  1409. if (SmbResponseStatus != RX_MAP_STATUS(SUCCESS)) {
  1410. if ((FIELD_OFFSET(GENERIC_ANDX,AndXReserved) + *pBytesConsumed) <= BytesIndicated) {
  1411. PGENERIC_ANDX pGenericAndX = (PGENERIC_ANDX)pSmbBuffer;
  1412. if (pGenericAndX->WordCount == 0) {
  1413. Status = SmbResponseStatus;
  1414. }
  1415. fUpdateVNetRootContext = TRUE;
  1416. NetRootState = SMBCEDB_INVALID;
  1417. }
  1418. // Note that the case wherein sufficient bytes are not indicated for the
  1419. // GENERIC_ANDX response is handled by the if statement below which
  1420. // imposes a more stringent test.
  1421. }
  1422. if ((Status == RX_MAP_STATUS(SUCCESS)) &&
  1423. (FIELD_OFFSET(RESP_21_TREE_CONNECT_ANDX,Buffer) + *pBytesConsumed) > BytesIndicated) {
  1424. Status = STATUS_MORE_PROCESSING_REQUIRED;
  1425. }
  1426. if (Status == RX_MAP_STATUS(SUCCESS)) {
  1427. USHORT ResponseWordCount;
  1428. ULONG TreeConnectResponseLength,TreeConnectByteCount,ServiceStringLength;
  1429. PUCHAR pShareTypeResponseString = NULL;
  1430. PRESP_21_TREE_CONNECT_ANDX p21TreeConnectAndXResponse;
  1431. PUCHAR NativeFileSystem = NULL;
  1432. p21TreeConnectAndXResponse = (PRESP_21_TREE_CONNECT_ANDX)(pSmbBuffer);
  1433. SmbCommand = p21TreeConnectAndXResponse->AndXCommand;
  1434. TreeConnectByteCount = 0;
  1435. RxDbgTrace( 0, Dbg, ("Processing Tree Connect and X\n"));
  1436. // case out based on the actual response length. Lanman 21 clients or NT clients
  1437. // have a longer response.....win95 negotiates NT dialect but uses a <lm21 response format
  1438. ResponseWordCount = p21TreeConnectAndXResponse->WordCount;
  1439. switch (ResponseWordCount) {
  1440. case 0:
  1441. Status = SmbResponseStatus;
  1442. break;
  1443. case 3:
  1444. case 7:
  1445. {
  1446. PRESP_EXTENDED_TREE_CONNECT_ANDX pExtendedTreeConnectAndXResponse;
  1447. if (ResponseWordCount == 7) {
  1448. pExtendedTreeConnectAndXResponse = (PRESP_EXTENDED_TREE_CONNECT_ANDX)(pSmbBuffer);
  1449. pNetRootEntry->MaximalAccessRights =
  1450. SmbGetUlong(
  1451. &pExtendedTreeConnectAndXResponse->MaximalShareAccessRights);
  1452. pNetRootEntry->GuestMaximalAccessRights =
  1453. SmbGetUlong(
  1454. &pExtendedTreeConnectAndXResponse->GuestMaximalShareAccessRights);
  1455. ASSERT(FIELD_OFFSET(RESP_EXTENDED_TREE_CONNECT_ANDX,AndXCommand)
  1456. ==FIELD_OFFSET(RESP_21_TREE_CONNECT_ANDX,AndXCommand));
  1457. pShareTypeResponseString = (PUCHAR)&pExtendedTreeConnectAndXResponse->Buffer;
  1458. TreeConnectByteCount = SmbGetUshort(&pExtendedTreeConnectAndXResponse->ByteCount);
  1459. TreeConnectResponseLength =
  1460. FIELD_OFFSET(RESP_EXTENDED_TREE_CONNECT_ANDX,Buffer) + TreeConnectByteCount;
  1461. pNetRootEntry->NetRoot.ChunkShift = 0xC;
  1462. pNetRootEntry->NetRoot.ChunkSize =
  1463. (1 << pNetRootEntry->NetRoot.ChunkShift);
  1464. pNetRootEntry->NetRoot.ClusterShift = 0x9;
  1465. pNetRootEntry->NetRoot.CompressionUnitShift = 0xD;
  1466. pNetRootEntry->NetRoot.CompressionFormatAndEngine =
  1467. COMPRESSION_FORMAT_LZNT1;
  1468. NativeFileSystem = &pExtendedTreeConnectAndXResponse->Buffer[3];
  1469. } else {
  1470. pNetRootEntry->MaximalAccessRights = FILE_ALL_ACCESS;
  1471. pNetRootEntry->GuestMaximalAccessRights = 0;
  1472. pShareTypeResponseString = (PUCHAR)&p21TreeConnectAndXResponse->Buffer;
  1473. TreeConnectByteCount = SmbGetUshort(&p21TreeConnectAndXResponse->ByteCount);
  1474. TreeConnectResponseLength =
  1475. FIELD_OFFSET(RESP_21_TREE_CONNECT_ANDX,Buffer) + TreeConnectByteCount;
  1476. NativeFileSystem = &p21TreeConnectAndXResponse->Buffer[3];
  1477. }
  1478. pNetRootEntry->NetRoot.UpdateCscShareRights = TRUE;
  1479. // Parse and update the optional support bits returned by
  1480. // the server
  1481. if (pServerEntry->Server.Dialect >= NTLANMAN_DIALECT ) {
  1482. USHORT OptionalSupport;
  1483. PMRX_NET_ROOT pNetRoot = pVNetRoot->pNetRoot;
  1484. OptionalSupport = SmbGetUshort(
  1485. &p21TreeConnectAndXResponse->OptionalSupport);
  1486. if (FlagOn(OptionalSupport,SMB_SHARE_IS_IN_DFS)) {
  1487. pNetRootEntry->NetRoot.DfsAware = TRUE;
  1488. SetFlag(pNetRoot->Flags,NETROOT_FLAG_DFS_AWARE_NETROOT);
  1489. }
  1490. if (FlagOn(OptionalSupport,SMB_UNIQUE_FILE_NAME)) {
  1491. SetFlag(pNetRoot->Flags,NETROOT_FLAG_UNIQUE_FILE_NAME);
  1492. }
  1493. pNetRootEntry->NetRoot.CscFlags = (OptionalSupport & SMB_CSC_MASK);
  1494. switch (pNetRootEntry->NetRoot.CscFlags) {
  1495. case SMB_CSC_CACHE_AUTO_REINT:
  1496. case SMB_CSC_CACHE_VDO:
  1497. pNetRootEntry->NetRoot.CscEnabled = TRUE;
  1498. pNetRootEntry->NetRoot.CscShadowable = TRUE;
  1499. break;
  1500. case SMB_CSC_CACHE_MANUAL_REINT:
  1501. pNetRootEntry->NetRoot.CscEnabled = TRUE;
  1502. pNetRootEntry->NetRoot.CscShadowable = FALSE;
  1503. break;
  1504. case SMB_CSC_NO_CACHING:
  1505. pNetRootEntry->NetRoot.CscEnabled = FALSE;
  1506. pNetRootEntry->NetRoot.CscShadowable = FALSE;
  1507. }
  1508. }
  1509. if (SmbCommand == SMB_COM_NO_ANDX_COMMAND) {
  1510. Status = SmbResponseStatus;
  1511. } else {
  1512. TreeConnectResponseLength =
  1513. SmbGetUshort(&p21TreeConnectAndXResponse->AndXOffset) -
  1514. *pBytesConsumed;
  1515. }
  1516. }
  1517. break;
  1518. case 2:
  1519. {
  1520. PRESP_TREE_CONNECT_ANDX pTreeConnectAndXResponse;
  1521. pTreeConnectAndXResponse = (PRESP_TREE_CONNECT_ANDX)(pSmbBuffer);
  1522. ASSERT(FIELD_OFFSET(RESP_TREE_CONNECT_ANDX,AndXCommand)
  1523. ==FIELD_OFFSET(RESP_21_TREE_CONNECT_ANDX,AndXCommand));
  1524. pShareTypeResponseString = (PUCHAR)&pTreeConnectAndXResponse->Buffer;
  1525. TreeConnectByteCount = SmbGetUshort(&pTreeConnectAndXResponse->ByteCount);
  1526. TreeConnectResponseLength =
  1527. FIELD_OFFSET(RESP_TREE_CONNECT_ANDX,Buffer) + TreeConnectByteCount;
  1528. if (SmbCommand == SMB_COM_NO_ANDX_COMMAND) {
  1529. Status = SmbResponseStatus;
  1530. } else {
  1531. TreeConnectResponseLength =
  1532. SmbGetUshort(&pTreeConnectAndXResponse->AndXOffset) -
  1533. *pBytesConsumed;
  1534. }
  1535. // win9x server, returns wordcount of 2 yet has the dialect of NTLANMAN
  1536. // which is a bug, but we will work around it.
  1537. if (pServerEntry->Server.Dialect >= NTLANMAN_DIALECT ) {
  1538. pNetRootEntry->NetRoot.UpdateCscShareRights = TRUE;
  1539. pNetRootEntry->MaximalAccessRights = FILE_ALL_ACCESS;
  1540. pNetRootEntry->GuestMaximalAccessRights = 0;
  1541. // make it look like a MANUAL_REINT guy
  1542. pNetRootEntry->NetRoot.CscEnabled = TRUE;
  1543. pNetRootEntry->NetRoot.CscShadowable = FALSE;
  1544. }
  1545. }
  1546. break;
  1547. default :
  1548. Status = STATUS_INVALID_NETWORK_RESPONSE;
  1549. }
  1550. RxDbgTrace( 0, Dbg, ("SmbCeParseSmbHeader::Tree connect and X Response %lx\n",Status));
  1551. if (NT_SUCCESS(Status)) {
  1552. PSMBCE_NET_ROOT psmbNetRoot = &(pNetRootEntry->NetRoot);
  1553. PSMBCE_SERVER psmbServer = &(pServerEntry->Server);
  1554. if (TreeConnectResponseLength + *pBytesConsumed <= BytesIndicated) {
  1555. *pBytesConsumed += TreeConnectResponseLength;
  1556. // Update the NetRoot fields based on the response.
  1557. SetFlag(
  1558. pVNetRootContext->Flags,
  1559. SMBCE_V_NET_ROOT_CONTEXT_FLAG_VALID_TID);
  1560. RtlCopyMemory(
  1561. &pVNetRootContext->TreeId,
  1562. &pSmbHeader->Tid,
  1563. sizeof(pSmbHeader->Tid));
  1564. { struct __Service_Name_Entry *i;
  1565. for (i=ServiceNameTable;;i++) {
  1566. ServiceStringLength = i->NameLength;
  1567. if (TreeConnectByteCount >= ServiceStringLength) {
  1568. if (RtlCompareMemory(
  1569. pShareTypeResponseString,
  1570. i->Name,
  1571. ServiceStringLength)
  1572. == ServiceStringLength) {
  1573. psmbNetRoot->NetRootType = i->NetRootType;
  1574. if (FALSE) DbgPrint("FoundServiceStrng %s len %d type %d\n",i->Name,i->NameLength,i->NetRootType);
  1575. break;
  1576. }
  1577. }
  1578. if (i->NetRootType==NET_ROOT_COMM) {
  1579. ASSERT(!"Valid Share Type returned in TREE COnnect And X response");
  1580. psmbNetRoot->NetRootType = NET_ROOT_DISK;
  1581. ServiceStringLength = TreeConnectByteCount;
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. if (psmbNetRoot->NetRootType == NET_ROOT_DISK) {
  1587. if (NativeFileSystem != NULL) {
  1588. if (BooleanFlagOn(pServerEntry->Server.DialectFlags,DF_UNICODE)) {
  1589. if (RtlCompareMemory(
  1590. NativeFileSystem,
  1591. FileSystem_NTFS_UNICODE.Buffer,
  1592. FileSystem_NTFS_UNICODE.Length)
  1593. == FileSystem_NTFS_UNICODE.Length) {
  1594. psmbNetRoot->NetRootFileSystem = NET_ROOT_FILESYSTEM_NTFS;
  1595. } else if (RtlCompareMemory(
  1596. NativeFileSystem,
  1597. FileSystem_FAT_UNICODE.Buffer,
  1598. FileSystem_FAT_UNICODE.Length)
  1599. == FileSystem_FAT_UNICODE.Length) {
  1600. psmbNetRoot->NetRootFileSystem = NET_ROOT_FILESYSTEM_FAT;
  1601. }
  1602. } else {
  1603. if (RtlCompareMemory(
  1604. NativeFileSystem,
  1605. FileSystem_NTFS,
  1606. 4*sizeof(CHAR))
  1607. == 4*sizeof(CHAR)) {
  1608. psmbNetRoot->NetRootFileSystem = NET_ROOT_FILESYSTEM_NTFS;
  1609. } else if (RtlCompareMemory(
  1610. NativeFileSystem,
  1611. FileSystem_FAT,
  1612. 3*sizeof(CHAR))
  1613. == 3*sizeof(CHAR)) {
  1614. psmbNetRoot->NetRootFileSystem = NET_ROOT_FILESYSTEM_FAT;
  1615. }
  1616. }
  1617. }
  1618. psmbNetRoot->MaximumReadBufferSize = psmbServer->MaximumDiskFileReadBufferSize;
  1619. psmbNetRoot->MaximumWriteBufferSize = psmbServer->MaximumDiskFileWriteBufferSize;
  1620. } else {
  1621. psmbNetRoot->MaximumWriteBufferSize = psmbServer->MaximumNonDiskFileWriteBufferSize;
  1622. psmbNetRoot->MaximumReadBufferSize = psmbServer->MaximumNonDiskFileReadBufferSize;
  1623. }
  1624. //if !(NT was negotiated) and bytecount>servicelength, we may have a NativeFs name
  1625. if (!FlagOn(psmbServer->DialectFlags,DF_NTNEGOTIATE)
  1626. && (TreeConnectByteCount>ServiceStringLength)) {
  1627. PBYTE NativeFs = pShareTypeResponseString+ServiceStringLength;
  1628. if (*NativeFs != 0) {
  1629. ULONG i;
  1630. ULONG maxlenpersmb = TreeConnectByteCount-ServiceStringLength;
  1631. ULONG maxlenperarraysize = SMB_MAXIMUM_SUPPORTED_VOLUME_LABEL;
  1632. PCHAR p = (PCHAR)(&psmbNetRoot->FileSystemNameA[0]); //dont write into the 0th char
  1633. //DbgPrint("we may have one...\n");
  1634. for (i=1;;i++){
  1635. if (i==maxlenpersmb) {
  1636. break;
  1637. }
  1638. if (i==maxlenperarraysize) {
  1639. break;
  1640. }
  1641. if (NativeFs[i]==0) {
  1642. break;
  1643. }
  1644. }
  1645. //save away the name for processing later
  1646. RtlCopyMemory(p,NativeFs,i);
  1647. p[i] = 0;
  1648. //DbgPrint("NativeFs = %s (%d)\n",p,i);
  1649. psmbNetRoot->FileSystemNameALength = (UCHAR)i;
  1650. }
  1651. }
  1652. pSmbBuffer += TreeConnectResponseLength;
  1653. fUpdateVNetRootContext = TRUE;
  1654. NetRootState = SMBCEDB_ACTIVE;
  1655. } else {
  1656. Status = STATUS_MORE_PROCESSING_REQUIRED;
  1657. }
  1658. } else {
  1659. fUpdateVNetRootContext = TRUE;
  1660. NetRootState = SMBCEDB_INVALID;
  1661. }
  1662. }
  1663. }
  1664. if ((SmbCommand == SMB_COM_TREE_CONNECT) &&
  1665. NT_SUCCESS(Status)) {
  1666. PRESP_TREE_CONNECT pTreeConnectResponse;
  1667. ULONG TreeConnectResponseLength;
  1668. RxDbgTrace( 0, Dbg, ("Processing Tree Connect\n"));
  1669. pTreeConnectResponse = (PRESP_TREE_CONNECT)pSmbBuffer;
  1670. TreeConnectResponseLength = FIELD_OFFSET(RESP_TREE_CONNECT,Buffer);
  1671. SmbCommand = SMB_COM_NO_ANDX_COMMAND;
  1672. if (NT_SUCCESS(SmbResponseStatus)) {
  1673. PSMBCE_NET_ROOT psmbNetRoot = &(pNetRootEntry->NetRoot);
  1674. PSMBCE_SERVER psmbServer = &(pServerEntry->Server);
  1675. if (TreeConnectResponseLength + *pBytesConsumed <= BytesIndicated) {
  1676. // Update the NetRoot fields based on the response.
  1677. SetFlag(
  1678. pVNetRootContext->Flags,
  1679. SMBCE_V_NET_ROOT_CONTEXT_FLAG_VALID_TID);
  1680. RtlCopyMemory(
  1681. &pVNetRootContext->TreeId,
  1682. &pTreeConnectResponse->Tid,
  1683. sizeof(pTreeConnectResponse->Tid));
  1684. if (psmbServer->Dialect == PCNET1_DIALECT) {
  1685. psmbNetRoot->NetRootType = NET_ROOT_DISK;
  1686. }
  1687. else {
  1688. psmbNetRoot->NetRootType = NET_ROOT_WILD;
  1689. }
  1690. if (psmbServer->MaximumBufferSize == 0){
  1691. ULONG MaxBuf = SmbGetUshort(&pTreeConnectResponse->MaxBufferSize);
  1692. RxDbgTrace( 0, Dbg, ("SmbCeParseSmbHeader:: setting srvmaxbufsize %ld\n", MaxBuf));
  1693. psmbServer->MaximumBufferSize = MaxBuf;
  1694. //psmbServer->MaximumDiskFileReadBufferSize =
  1695. psmbNetRoot->MaximumWriteBufferSize =
  1696. psmbNetRoot->MaximumReadBufferSize =
  1697. MaxBuf -
  1698. QuadAlign(
  1699. sizeof(SMB_HEADER) +
  1700. FIELD_OFFSET(
  1701. RESP_READ,
  1702. Buffer[0]));
  1703. }
  1704. *pBytesConsumed += TreeConnectResponseLength;
  1705. pSmbBuffer += *pBytesConsumed;
  1706. fUpdateVNetRootContext = TRUE;
  1707. NetRootState = SMBCEDB_ACTIVE;
  1708. //for CORE, this counts as a successful session setup as well!
  1709. pSessionEntry->Session.UserId = pSmbHeader->Uid;
  1710. } else {
  1711. Status = STATUS_MORE_PROCESSING_REQUIRED;
  1712. }
  1713. } else {
  1714. Status = SmbResponseStatus;
  1715. fUpdateVNetRootContext = TRUE;
  1716. NetRootState = SMBCEDB_MARKED_FOR_DELETION;
  1717. }
  1718. RxDbgTrace( 0, Dbg, ("SmbCeParseSmbHeader::Tree connect Response %lx\n",Status));
  1719. }
  1720. if ((SmbResponseStatus == STATUS_USER_SESSION_DELETED) ||
  1721. (SmbResponseStatus == STATUS_NETWORK_NAME_DELETED)) {
  1722. ClearFlag(
  1723. pVNetRootContext->Flags,
  1724. SMBCE_V_NET_ROOT_CONTEXT_FLAG_VALID_TID);
  1725. InterlockedCompareExchange(
  1726. &(pVNetRootContext->Header.State),
  1727. SMBCEDB_INVALID,
  1728. SMBCEDB_ACTIVE);
  1729. InterlockedCompareExchange(
  1730. &(pSessionEntry->Header.State),
  1731. SMBCEDB_INVALID,
  1732. SMBCEDB_ACTIVE);
  1733. fUpdateVNetRootContext = TRUE;
  1734. NetRootState = SMBCEDB_INVALID;
  1735. }
  1736. // Initiate further action if the status of the exchange/conenction engine can be
  1737. // updated based on the data available.
  1738. if (fUpdateVNetRootContext) {
  1739. PMRX_NET_ROOT pNetRoot = pExchange->SmbCeContext.pVNetRoot->pNetRoot;
  1740. SmbCeUpdateVNetRootContextState(
  1741. pVNetRootContext,
  1742. NetRootState);
  1743. switch (NetRootState) {
  1744. case SMBCEDB_ACTIVE:
  1745. pNetRoot->MRxNetRootState = MRX_NET_ROOT_STATE_GOOD;
  1746. break;
  1747. case SMBCEDB_INVALID:
  1748. pNetRoot->MRxNetRootState = MRX_NET_ROOT_STATE_DISCONNECTED;
  1749. break;
  1750. case SMBCEDB_CONSTRUCTION_IN_PROGRESS:
  1751. pNetRoot->MRxNetRootState = MRX_NET_ROOT_STATE_RECONN;
  1752. break;
  1753. default:
  1754. pNetRoot->MRxNetRootState = MRX_NET_ROOT_STATE_ERROR;
  1755. break;
  1756. }
  1757. RxDbgTrace( 0, Dbg, ("Dispatching Net root Entry Finalization\n"));
  1758. }
  1759. if (!(pExchange->SmbCeFlags & SMBCE_EXCHANGE_SESSION_CONSTRUCTOR) &&
  1760. !(pExchange->SmbCeFlags & SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR)) {
  1761. if ((pSmbHeader->Uid != pSessionEntry->Session.UserId) ||
  1762. (pSmbHeader->Tid != pVNetRootContext->TreeId)) {
  1763. RxLog(("Srvr %lx Xchg %lx RUid %ld RTid %ld\n SUid %ld STid %ld\n",
  1764. pServerEntry,pExchange,
  1765. pSmbHeader->Uid,pSmbHeader->Tid,
  1766. pSessionEntry->Session.UserId,pVNetRootContext->TreeId));
  1767. SmbLogError(STATUS_UNSUCCESSFUL,
  1768. LOG,
  1769. SmbCeParseSmbHeader,
  1770. LOGPTR(pServerEntry)
  1771. LOGPTR(pExchange)
  1772. LOGXSHORT(pSmbHeader->Uid)
  1773. LOGXSHORT(pSmbHeader->Tid)
  1774. LOGXSHORT(pSessionEntry->Session.UserId)
  1775. LOGXSHORT(pVNetRootContext->TreeId));
  1776. }
  1777. }
  1778. pExchange->SmbStatus = SmbResponseStatus; //N.B. no spinlock!
  1779. if (Status == STATUS_MORE_PROCESSING_REQUIRED) {
  1780. *pBytesConsumed = 0;
  1781. } else if (!NT_SUCCESS(Status)) {
  1782. *pBytesConsumed = BytesAvailable;
  1783. } else {
  1784. if (pSmbResponseStatus != NULL) {
  1785. *pSmbResponseStatus = SmbResponseStatus;
  1786. }
  1787. if (pCommandToProcess != NULL) {
  1788. PGENERIC_ANDX pGenericAndX = (PGENERIC_ANDX)((PBYTE)pSmbHeader + *pBytesConsumed);
  1789. pCommandToProcess->AndXCommand = SmbCommand;
  1790. SmbPutUshort(&pCommandToProcess->AndXOffset, (USHORT)*pBytesConsumed);
  1791. if ((sizeof(GENERIC_ANDX) + *pBytesConsumed) <= BytesAvailable) {
  1792. pCommandToProcess->WordCount = pGenericAndX->WordCount;
  1793. } else {
  1794. pCommandToProcess->WordCount = 0;
  1795. }
  1796. }
  1797. }
  1798. return Status;
  1799. }
  1800. NTSTATUS
  1801. SmbCeResumeExchange(
  1802. PSMB_EXCHANGE pExchange)
  1803. /*++
  1804. Routine Description:
  1805. This routine resumes an exchange that was suspended in the connection
  1806. engine
  1807. Arguments:
  1808. pExchange - the exchange Instance
  1809. Return Value:
  1810. The return status for the operation
  1811. --*/
  1812. {
  1813. NTSTATUS Status;
  1814. PAGED_CODE();
  1815. SmbCeIncrementPendingLocalOperations(pExchange);
  1816. // Initiate the exchange
  1817. Status = SmbCeInitiateExchange(pExchange);
  1818. SmbCeDecrementPendingLocalOperationsAndFinalize(pExchange);
  1819. return Status;
  1820. }
  1821. NTSTATUS
  1822. SmbCepInitializeExchange(
  1823. PSMB_EXCHANGE *pExchangePointer,
  1824. PRX_CONTEXT pRxContext,
  1825. PSMBCEDB_SERVER_ENTRY pServerEntry,
  1826. PMRX_V_NET_ROOT pVNetRoot,
  1827. SMB_EXCHANGE_TYPE Type,
  1828. PSMB_EXCHANGE_DISPATCH_VECTOR pDispatchVector)
  1829. /*++
  1830. Routine Description:
  1831. This routine initializes the given exchange instanece
  1832. Arguments:
  1833. pExchangePointer - the placeholder for the exchange instance. If it is NULL a new one
  1834. is allocated.
  1835. pRxContext - the associated RxContext
  1836. pServerEntry - the associated server entry
  1837. pVirtualNetRoot - the virtual net root
  1838. Type - the type of the exchange
  1839. pDispatchVector - the dispatch vector asscoiated with this instance.
  1840. Return Value:
  1841. RXSTATUS - The return status for the operation
  1842. --*/
  1843. {
  1844. NTSTATUS Status = STATUS_SUCCESS;
  1845. PSMB_EXCHANGE pExchange = NULL;
  1846. PAGED_CODE();
  1847. RxDbgTrace( 0, Dbg, ("SmbCeInitializeExchange: Invoked\n"));
  1848. if (*pExchangePointer == NULL) {
  1849. // Allocate a new exchange instance.
  1850. pExchange = SmbMmAllocateExchange(Type,NULL);
  1851. if (pExchange == NULL) {
  1852. return STATUS_INSUFFICIENT_RESOURCES;
  1853. }
  1854. *pExchangePointer = pExchange;
  1855. }
  1856. if ((Status = SmbCeIncrementActiveExchangeCount()) == STATUS_SUCCESS) {
  1857. PSMB_EXCHANGE LocalExchangePointer = *pExchangePointer;
  1858. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  1859. LocalExchangePointer->CancellationStatus = SMBCE_EXCHANGE_NOT_CANCELLED;
  1860. LocalExchangePointer->RxContext = pRxContext;
  1861. if (Status == STATUS_SUCCESS) {
  1862. if (pVNetRoot != NULL) {
  1863. pVNetRootContext = SmbCeGetAssociatedVNetRootContext(pVNetRoot);
  1864. LocalExchangePointer->SmbCeContext.pVNetRoot = pVNetRoot;
  1865. pServerEntry = SmbCeGetAssociatedServerEntry(pVNetRoot->pNetRoot->pSrvCall);
  1866. } else {
  1867. ASSERT(pServerEntry != NULL);
  1868. pVNetRootContext = NULL;
  1869. }
  1870. if (pVNetRootContext != NULL) {
  1871. SmbCeReferenceVNetRootContext(pVNetRootContext);
  1872. LocalExchangePointer->SmbCeContext.pVNetRootContext =
  1873. pVNetRootContext;
  1874. LocalExchangePointer->SmbCeContext.pServerEntry =
  1875. pVNetRootContext->pServerEntry;
  1876. } else {
  1877. SmbCeReferenceServerEntry(pServerEntry);
  1878. LocalExchangePointer->SmbCeContext.pServerEntry =
  1879. pServerEntry;
  1880. LocalExchangePointer->SmbCeContext.pVNetRootContext = NULL;
  1881. }
  1882. LocalExchangePointer->SmbCeState = SMBCE_EXCHANGE_INITIALIZATION_START;
  1883. LocalExchangePointer->pDispatchVector = pDispatchVector;
  1884. LocalExchangePointer->SmbCeFlags &= (SMBCE_EXCHANGE_FLAGS_TO_PRESERVE);
  1885. LocalExchangePointer->SmbCeFlags |= (SMBCE_EXCHANGE_REUSE_MID | SMBCE_EXCHANGE_ATTEMPT_RECONNECTS);
  1886. }
  1887. if (Status != STATUS_SUCCESS) {
  1888. SmbCeDecrementActiveExchangeCount();
  1889. }
  1890. } else {
  1891. (*pExchangePointer)->SmbCeFlags |= SMBCE_EXCHANGE_SMBCE_STOPPED;
  1892. }
  1893. if ((Status == STATUS_SUCCESS) &&
  1894. (pRxContext != NULL)) {
  1895. PFOBX pFobx = (PFOBX)(pRxContext->pFobx);
  1896. PMRX_FCB pFcb = (pRxContext->pFcb);
  1897. PSMBCE_SESSION pSession = SmbCeGetExchangeSession(*pExchangePointer);
  1898. if ((pSession != NULL) &&
  1899. FlagOn(pSession->Flags,SMBCE_SESSION_FLAGS_REMOTE_BOOT_SESSION) &&
  1900. (pRxContext->MajorFunction != IRP_MJ_CREATE) &&
  1901. (pRxContext->MajorFunction != IRP_MJ_CLOSE) &&
  1902. (pFobx != NULL) &&
  1903. (pFcb->pNetRoot != NULL) &&
  1904. (pFcb->pNetRoot->Type == NET_ROOT_DISK)) {
  1905. PMRX_SRV_OPEN SrvOpen = pFobx->pSrvOpen;
  1906. PMRX_SMB_SRV_OPEN smbSrvOpen = MRxSmbGetSrvOpenExtension(SrvOpen);
  1907. if ((smbSrvOpen != NULL) &&
  1908. (smbSrvOpen->Version != pServerEntry->Server.Version)) {
  1909. if (smbSrvOpen->DeferredOpenContext != NULL) {
  1910. Status = SmbCeRemoteBootReconnect(*pExchangePointer, pRxContext);
  1911. } else {
  1912. Status = STATUS_CONNECTION_DISCONNECTED;
  1913. pFcb->fShouldBeOrphaned = TRUE;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. if (pRxContext != NULL &&
  1919. pRxContext->MajorFunction != IRP_MJ_CREATE &&
  1920. pRxContext->pFcb->Attributes & FILE_ATTRIBUTE_OFFLINE) {
  1921. (*pExchangePointer)->IsOffLineFile = TRUE;
  1922. }
  1923. if (!NT_SUCCESS(Status)) {
  1924. if (pExchange != NULL) {
  1925. SmbMmFreeExchange(pExchange);
  1926. *pExchangePointer = NULL;
  1927. }
  1928. }
  1929. return Status;
  1930. }
  1931. NTSTATUS
  1932. SmbCeInitializeAssociatedExchange(
  1933. PSMB_EXCHANGE *pAssociatedExchangePointer,
  1934. PSMB_EXCHANGE pMasterExchange,
  1935. SMB_EXCHANGE_TYPE Type,
  1936. PSMB_EXCHANGE_DISPATCH_VECTOR pDispatchVector)
  1937. /*++
  1938. Routine Description:
  1939. This routine initializes the given exchange instanece
  1940. Arguments:
  1941. pAssociatedExchangePointer - the placeholder for the exchange instance. If it is NULL a new one
  1942. is allocated.
  1943. pMasterExchange - the master exchange
  1944. Type - the type of the exchange
  1945. pDispatchVector - the dispatch vector asscoiated with this instance.
  1946. Return Value:
  1947. NTSTATUS - The return status for the operation
  1948. Notes:
  1949. --*/
  1950. {
  1951. NTSTATUS Status = STATUS_SUCCESS;
  1952. PAGED_CODE();
  1953. if ((pMasterExchange->SmbCeState == SMBCE_EXCHANGE_INITIATED) &&
  1954. !FlagOn(pMasterExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE)) {
  1955. Status = SmbCeInitializeExchange(
  1956. pAssociatedExchangePointer,
  1957. NULL,
  1958. pMasterExchange->SmbCeContext.pVNetRoot,
  1959. Type,
  1960. pDispatchVector);
  1961. if (Status == STATUS_SUCCESS) {
  1962. PSMB_EXCHANGE pAssociatedExchange;
  1963. pAssociatedExchange = *pAssociatedExchangePointer;
  1964. pAssociatedExchange->SmbCeState = SMBCE_EXCHANGE_INITIATED;
  1965. pAssociatedExchange->SmbCeFlags |= SMBCE_ASSOCIATED_EXCHANGE;
  1966. SmbCeIncrementPendingLocalOperations(pMasterExchange);
  1967. InterlockedIncrement(&pMasterExchange->Master.PendingAssociatedExchanges);
  1968. pAssociatedExchange->Associated.pMasterExchange = pMasterExchange;
  1969. InitializeListHead(&pAssociatedExchange->WorkQueueItem.List);
  1970. }
  1971. } else {
  1972. Status = STATUS_INVALID_PARAMETER;
  1973. }
  1974. return Status;
  1975. }
  1976. NTSTATUS
  1977. SmbCeTransformExchange(
  1978. PSMB_EXCHANGE pExchange,
  1979. SMB_EXCHANGE_TYPE NewType,
  1980. PSMB_EXCHANGE_DISPATCH_VECTOR pDispatchVector)
  1981. /*++
  1982. Routine Description:
  1983. This routine transforms an exchange instance of one kind to an exchange instance
  1984. of another kind ( A sophisticated form of casting )
  1985. Arguments:
  1986. pExchange - the exchange instance.
  1987. Type - the new type of the exchange
  1988. pDispatchVector - the dispatch vector asscoiated with this instance.
  1989. Return Value:
  1990. RXSTATUS - The return status for the operation
  1991. Notes:
  1992. As it is currently implemented no restrictions are imposed. Once the number of exchanges
  1993. have been established further restrictions will be imposed barring certain kinds of
  1994. transformations. The transformation merely switches the dispatch vector associated
  1995. with the exchange but the context is left intact.
  1996. --*/
  1997. {
  1998. PAGED_CODE();
  1999. pExchange->Type = (UCHAR)NewType;
  2000. pExchange->pDispatchVector = pDispatchVector;
  2001. return STATUS_SUCCESS;
  2002. }
  2003. NTSTATUS
  2004. SmbCeUpdateSessionEntryAndVNetRootContext(
  2005. PSMB_EXCHANGE pExchange)
  2006. /*++
  2007. Routine Description:
  2008. This routine updates the session entry and/or vnetrootcontext if this exchange has
  2009. been marked as a constructor for a session and/or netroot.
  2010. Arguments:
  2011. pExchange - the exchange instance.
  2012. Return Value:
  2013. RXSTATUS - The return status for the operation
  2014. --*/
  2015. {
  2016. PMRX_V_NET_ROOT pVNetRoot = SmbCeGetExchangeVNetRoot(pExchange);
  2017. PSMBCEDB_SESSION_ENTRY pSessionEntry = SmbCeGetExchangeSessionEntry(pExchange);
  2018. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext = SmbCeGetExchangeVNetRootContext(pExchange);
  2019. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_SESSION_CONSTRUCTOR) {
  2020. ASSERT(pSessionEntry != NULL);
  2021. RxDbgTrace( 0, Dbg, ("Dispatching Session Entry Finalization\n"));
  2022. SmbCeReferenceSessionEntry(pSessionEntry);
  2023. // ASSERT(pExchange->SessionSetupStatus != STATUS_SUCCESS ||
  2024. // pSessionEntry->Header.State == SMBCEDB_CONSTRUCTION_IN_PROGRESS);
  2025. pVNetRoot->ConstructionStatus = pExchange->SessionSetupStatus;
  2026. SmbCeCompleteSessionEntryInitialization(pSessionEntry,
  2027. pExchange->SessionSetupStatus,
  2028. pExchange->SecuritySignatureReturned);
  2029. pExchange->SmbCeFlags &= ~SMBCE_EXCHANGE_SESSION_CONSTRUCTOR;
  2030. }
  2031. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR) {
  2032. ASSERT(pVNetRootContext != NULL);
  2033. RxDbgTrace( 0, Dbg, ("Dispatching Net root Entry Finalization\n"));
  2034. SmbCeReferenceVNetRootContext(pVNetRootContext);
  2035. SmbCeCompleteVNetRootContextInitialization(pVNetRootContext);
  2036. pExchange->SmbCeFlags &= ~SMBCE_EXCHANGE_NETROOT_CONSTRUCTOR;
  2037. }
  2038. return STATUS_SUCCESS;
  2039. }
  2040. NTSTATUS
  2041. SmbCePrepareExchangeForReuse(
  2042. PSMB_EXCHANGE pExchange)
  2043. /*++
  2044. Routine Description:
  2045. This routine transforms an exchange instance of one kind to an exchange instance
  2046. of another kind ( A sophisticated form of casting )
  2047. Arguments:
  2048. pExchange - the exchange instance.
  2049. Return Value:
  2050. RXSTATUS - The return status for the operation
  2051. --*/
  2052. {
  2053. PSMBCEDB_SERVER_ENTRY pServerEntry = NULL;
  2054. PSMBCEDB_SESSION_ENTRY pSessionEntry = NULL;
  2055. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry = NULL;
  2056. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext = NULL;
  2057. PAGED_CODE();
  2058. RxDbgTrace( 0, Dbg, ("SmbCePrepareExchangeForReuse: Invoked\n"));
  2059. if (!FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_SMBCE_STOPPED)) {
  2060. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pExchange);
  2061. pSessionEntry = SmbCeGetExchangeSessionEntry(pExchange);
  2062. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  2063. pVNetRootContext = SmbCeGetExchangeVNetRootContext(pExchange);
  2064. if (pServerEntry != NULL) {
  2065. // Disassociate the MID associated with the exchange
  2066. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_MID_VALID) {
  2067. SmbCeDissociateMidFromExchange(pServerEntry,pExchange);
  2068. }
  2069. // Tear down all the copy data requests associated with this exchange
  2070. SmbCePurgeBuffersAssociatedWithExchange(pServerEntry,pExchange);
  2071. // Uninitialize the transport associated with the exchange
  2072. SmbCeUninitializeExchangeTransport(pExchange);
  2073. }
  2074. // If this exchange has been marked as a constructor for either a
  2075. // session or netroot finalize the appropriate entries. ( mark
  2076. // them for deletion so that other exchanges can be resumed )
  2077. SmbCeUpdateSessionEntryAndVNetRootContext(pExchange);
  2078. if (pVNetRootContext != NULL) {
  2079. SmbCeDereferenceVNetRootContext(pVNetRootContext);
  2080. } else {
  2081. if (pServerEntry != NULL) {
  2082. SmbCeDereferenceServerEntry(pServerEntry);
  2083. }
  2084. }
  2085. }
  2086. SmbCeFreeBufferForServerResponse(pExchange);
  2087. if (FlagOn(pExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE)) {
  2088. PSMB_EXCHANGE pMasterExchange;
  2089. LONG AssociatedExchangeCount;
  2090. pMasterExchange = pExchange->Associated.pMasterExchange;
  2091. AssociatedExchangeCount = InterlockedDecrement(
  2092. &pMasterExchange->Master.PendingAssociatedExchanges);
  2093. if (FlagOn(
  2094. pMasterExchange->SmbCeFlags,
  2095. SMBCE_ASSOCIATED_EXCHANGES_COMPLETION_HANDLER_ACTIVATED) &&
  2096. (AssociatedExchangeCount == 0)){
  2097. NTSTATUS Status;
  2098. BOOLEAN PostRequest;
  2099. ClearFlag(
  2100. pMasterExchange->SmbCeFlags,
  2101. SMBCE_ASSOCIATED_EXCHANGES_COMPLETION_HANDLER_ACTIVATED);
  2102. Status = SMB_EXCHANGE_DISPATCH(
  2103. pMasterExchange,
  2104. AssociatedExchangesCompletionHandler,
  2105. (pMasterExchange,&PostRequest));
  2106. RxDbgTrace(0,Dbg,("Master Exchange %lx Assoc. Completion Status %lx\n",pMasterExchange,Status));
  2107. }
  2108. SmbCeDecrementPendingLocalOperationsAndFinalize(pMasterExchange);
  2109. }
  2110. return STATUS_SUCCESS;
  2111. }
  2112. VOID
  2113. SmbCeDiscardExchangeWorkerThreadRoutine(PVOID pExchange)
  2114. /*++
  2115. Routine Description:
  2116. This routine discards an exchange.
  2117. Arguments:
  2118. pExchange - the exchange to be discarded.
  2119. Return Value:
  2120. RXSTATUS - The return status for the operation
  2121. Notes:
  2122. Even though this is simple, it cannot be inlined since the destruction of an
  2123. exchange instance can be posted to a waorker thread.
  2124. --*/
  2125. {
  2126. PSMB_EXCHANGE pSmbExchange = pExchange;
  2127. PAGED_CODE();
  2128. RxDbgTrace( 0, Dbg, ("SmbCeDiscardExchange: Invoked\n"));
  2129. //RxLog((">>>Discard %lx",pSmbExchange));
  2130. // Destory the context
  2131. if (pSmbExchange->ReferenceCount == 0) {
  2132. SmbCeAcquireResource();
  2133. RemoveEntryList(&pSmbExchange->ExchangeList);
  2134. SmbCeReleaseResource();
  2135. SmbCePrepareExchangeForReuse(pSmbExchange);
  2136. SmbCeDecrementActiveExchangeCount();
  2137. // Discard the memory associated with the exchange
  2138. SmbMmFreeExchange(pSmbExchange);
  2139. } else {
  2140. RxDbgTrace(
  2141. 0,
  2142. Dbg,
  2143. ("SmbCeDiscardExchange: Exchange %lx not discarded %ld\n",
  2144. pSmbExchange,pSmbExchange->ReferenceCount)
  2145. );
  2146. }
  2147. }
  2148. VOID
  2149. SmbCeDiscardExchange(PVOID pExchange)
  2150. /*++
  2151. Routine Description:
  2152. This routine discards an exchange.
  2153. Arguments:
  2154. pExchange - the exchange to be discarded.
  2155. Notes:
  2156. The destruction of an exchange instance is posted to a worker thread in order to
  2157. avoid deadlock in transport.
  2158. --*/
  2159. {
  2160. PSMB_EXCHANGE pSmbExchange = pExchange;
  2161. PSMBCEDB_SERVER_ENTRY pServerEntry = SmbCeGetExchangeServerEntry(pSmbExchange);
  2162. // Disassociate the MID associated with the exchange
  2163. if (pSmbExchange->SmbCeFlags & SMBCE_EXCHANGE_MID_VALID) {
  2164. SmbCeDissociateMidFromExchange(pServerEntry,pSmbExchange);
  2165. }
  2166. RxPostToWorkerThread(
  2167. MRxSmbDeviceObject,
  2168. CriticalWorkQueue,
  2169. &((PSMB_EXCHANGE)pExchange)->WorkQueueItem,
  2170. SmbCeDiscardExchangeWorkerThreadRoutine,
  2171. (PSMB_EXCHANGE)pExchange);
  2172. }
  2173. NTSTATUS
  2174. SmbCeCancelExchange(
  2175. PRX_CONTEXT pRxContext)
  2176. /*++
  2177. Routine Description:
  2178. This routine initiates the cancellation of an exchange.
  2179. Arguments:
  2180. pRxContext - the RX_CONTEXT instance for which cancellation needs to be
  2181. initiated.
  2182. Return Value:
  2183. NTSTATUS - The return status for the operation
  2184. Notes:
  2185. The cancellation policy that has been implemented is a "best effort" policy.
  2186. Since the server has already committed resources to an operation at its end
  2187. the best that we can do within the scope of the SMB protocol is to initiate
  2188. a cancellation operation by sending the appropriate SMB_COM_NT_CANCEL command
  2189. Not all dialects of SMB support this command. For the downlevel dialects the
  2190. best that we can do is to ensure that the MID is not reused during the lifetime
  2191. of the connection. This will result in a gradual degradation of performance.
  2192. The difficulty in detecting the end of operations is that there are MIDS
  2193. --*/
  2194. {
  2195. NTSTATUS Status = STATUS_SUCCESS;
  2196. PSMB_EXCHANGE pExchange;
  2197. LIST_ENTRY CancelledExchanges;
  2198. PLIST_ENTRY pListEntry;
  2199. PMRXSMB_RX_CONTEXT pMRxSmbContext;
  2200. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry = SmbCeGetAssociatedNetRootEntry(pRxContext->pFcb->pNetRoot);
  2201. PSMBCEDB_SERVER_ENTRY pServerEntry = pNetRootEntry->pServerEntry;
  2202. SmbCeLog(("SmbCe Cancel %lx\n",pRxContext));
  2203. SmbLog(LOG,
  2204. SmbCeCancelExchange_1,
  2205. LOGPTR(pRxContext));
  2206. InitializeListHead(&CancelledExchanges);
  2207. SmbCeAcquireSpinLock();
  2208. pListEntry = pServerEntry->ActiveExchanges.Flink;
  2209. //
  2210. // With the pipeline write, multiple exchanges can be outstanding for a single RxContext.
  2211. // We need to walk through the active exchanges list to find and cancel all of them.
  2212. //
  2213. while (pListEntry != &pServerEntry->ActiveExchanges) {
  2214. PLIST_ENTRY pNextListEntry;
  2215. pNextListEntry = pListEntry->Flink;
  2216. pExchange = (PSMB_EXCHANGE)CONTAINING_RECORD(pListEntry,SMB_EXCHANGE,ExchangeList);
  2217. pListEntry = pNextListEntry;
  2218. if (pExchange->RxContext == pRxContext) {
  2219. if (!FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_FINALIZED)) {
  2220. if (pExchange->ReceivePendingOperations > 0) {
  2221. // This exchange is awaiting a response from the server. In all
  2222. // these cases a CANCEL command needs to be sent to the server
  2223. // This command can only be sent to NT servers. For non NT
  2224. // servers this exchange can be terminated with the detrimental
  2225. // side effect of reducing the maximum number of commands by 1.
  2226. InsertTailList(&CancelledExchanges,&pExchange->CancelledList);
  2227. InterlockedIncrement(&pExchange->LocalPendingOperations);
  2228. //DbgPrint("Exchange to be cancelled %x %x\n",pExchange,pRxContext);
  2229. if (!FlagOn(pServerEntry->Server.DialectFlags,DF_NT_SMBS)) {
  2230. if (pExchange->SmbCeFlags & SMBCE_EXCHANGE_MID_VALID) {
  2231. NTSTATUS LocalStatus;
  2232. LocalStatus = SmbCepDiscardMidAssociatedWithExchange(
  2233. pExchange);
  2234. ASSERT(LocalStatus == STATUS_SUCCESS);
  2235. }
  2236. }
  2237. } else {
  2238. InterlockedCompareExchange(
  2239. &pExchange->CancellationStatus,
  2240. SMBCE_EXCHANGE_CANCELLED,
  2241. SMBCE_EXCHANGE_NOT_CANCELLED);
  2242. }
  2243. }
  2244. }
  2245. }
  2246. SmbCeReleaseSpinLock();
  2247. pListEntry = CancelledExchanges.Flink;
  2248. while (pListEntry != &CancelledExchanges) {
  2249. PLIST_ENTRY pNextListEntry;
  2250. pNextListEntry = pListEntry->Flink;
  2251. pExchange = (PSMB_EXCHANGE)CONTAINING_RECORD(pListEntry,SMB_EXCHANGE,CancelledList);
  2252. RemoveEntryList(&pExchange->CancelledList);
  2253. pListEntry = pNextListEntry;
  2254. //DbgPrint("Exchange cancelled %x %x\n",pExchange,pRxContext);
  2255. SmbCeLog(("SmbCeCancel Initiate %lx\n",pExchange));
  2256. SmbLog(LOG,
  2257. SmbCeCancelExchange_2,
  2258. LOGPTR(pExchange));
  2259. if (FlagOn(pServerEntry->Server.DialectFlags,DF_NT_SMBS)) {
  2260. UCHAR LastCommandInHeader;
  2261. PUCHAR pCommand;
  2262. PSMB_HEADER pSmbHeader;
  2263. PNT_SMB_HEADER pNtSmbHeader;
  2264. BYTE SmbBuffer[TRANSPORT_HEADER_SIZE + CANCEL_BUFFER_SIZE];
  2265. PBYTE CancelRequestBuffer = SmbBuffer + TRANSPORT_HEADER_SIZE;
  2266. ULONG CancelRequestBufferSize = CANCEL_BUFFER_SIZE;
  2267. pSmbHeader = (PSMB_HEADER)CancelRequestBuffer;
  2268. pNtSmbHeader = (PNT_SMB_HEADER)pSmbHeader;
  2269. // Before issuing the cancel request ensure that if this exchange
  2270. // is set as a timed receive operation. This will ensure that if
  2271. // the cancel is delayed at the server we will initiate a tear down
  2272. // of the connection.
  2273. if (!FlagOn(
  2274. pExchange->SmbCeFlags,
  2275. SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION)) {
  2276. SmbCeAcquireResource();
  2277. SmbCeSetExpiryTime(pExchange);
  2278. pExchange->SmbCeFlags |= SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION;
  2279. SmbCeReleaseResource();
  2280. }
  2281. // Build the Cancel request and send it across to the server.
  2282. Status = SmbCeBuildSmbHeader(
  2283. pExchange,
  2284. CancelRequestBuffer,
  2285. CancelRequestBufferSize,
  2286. &CancelRequestBufferSize,
  2287. &LastCommandInHeader,
  2288. &pCommand);
  2289. ASSERT(LastCommandInHeader == SMB_COM_NO_ANDX_COMMAND);
  2290. if (Status == STATUS_SUCCESS) {
  2291. PREQ_NT_CANCEL pCancelRequest = (PREQ_NT_CANCEL)(&CancelRequestBuffer[sizeof(SMB_HEADER)]);
  2292. PMDL pCancelSmbMdl;
  2293. *pCommand = SMB_COM_NT_CANCEL;
  2294. SmbPutUshort(&pSmbHeader->Mid,pExchange->Mid);
  2295. if (BooleanFlagOn(
  2296. pExchange->SmbCeFlags,
  2297. SMBCE_EXCHANGE_FULL_PROCESSID_SPECIFIED)) {
  2298. ULONG ProcessId;
  2299. ProcessId = RxGetRequestorProcessId(pRxContext);
  2300. SmbPutUshort(&pNtSmbHeader->Pid, (USHORT)((ProcessId) & 0xFFFF));
  2301. SmbPutUshort(&pNtSmbHeader->PidHigh, (USHORT)((ProcessId) >> 16));
  2302. }
  2303. SmbPutUshort(&pCancelRequest->WordCount,0);
  2304. pCancelRequest->ByteCount = 0;
  2305. CancelRequestBufferSize = CANCEL_BUFFER_SIZE;
  2306. RxAllocateHeaderMdl(
  2307. CancelRequestBuffer,
  2308. CancelRequestBufferSize,
  2309. pCancelSmbMdl
  2310. );
  2311. if (pCancelSmbMdl != NULL) {
  2312. RxProbeAndLockHeaderPages(
  2313. pCancelSmbMdl,
  2314. KernelMode,
  2315. IoModifyAccess,
  2316. Status);
  2317. if (Status == STATUS_SUCCESS) {
  2318. Status = SmbCeSendToServer(
  2319. pServerEntry,
  2320. RXCE_SEND_SYNCHRONOUS,
  2321. pCancelSmbMdl,
  2322. CancelRequestBufferSize);
  2323. RxUnlockHeaderPages(pCancelSmbMdl);
  2324. }
  2325. IoFreeMdl(pCancelSmbMdl);
  2326. }
  2327. }
  2328. } else {
  2329. SmbCeFinalizeExchangeOnDisconnect(pExchange);
  2330. }
  2331. InterlockedCompareExchange(
  2332. &pExchange->CancellationStatus,
  2333. SMBCE_EXCHANGE_CANCELLED,
  2334. SMBCE_EXCHANGE_NOT_CANCELLED);
  2335. SmbCeDecrementPendingLocalOperationsAndFinalize(pExchange);
  2336. }
  2337. return Status;
  2338. }
  2339. NTSTATUS
  2340. SmbCeIncrementPendingOperations(
  2341. PSMB_EXCHANGE pExchange,
  2342. ULONG PendingOperationMask,
  2343. PVOID FileName,
  2344. ULONG FileLine)
  2345. /*++
  2346. Routine Description:
  2347. This routine increments the appropriate pending operation count
  2348. Arguments:
  2349. pExchange - the exchange to be finalized.
  2350. PendingOperationsMask -- the pending operations to be incremented
  2351. Return Value:
  2352. RxStatus(SUCCESS) if successful
  2353. --*/
  2354. {
  2355. NTSTATUS Status;
  2356. PSMBCEDB_SERVER_ENTRY pServerEntry;
  2357. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  2358. SmbCeAcquireSpinLock();
  2359. if (!BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_FINALIZED)) {
  2360. if ((pServerEntry != NULL) &&
  2361. ((pServerEntry->ServerStatus == STATUS_SUCCESS) ||
  2362. (pExchange->NodeTypeCode == SMB_EXCHANGE_NTC(ADMIN_EXCHANGE)) ||
  2363. FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_MAILSLOT_OPERATION))) {
  2364. if (PendingOperationMask & SMBCE_LOCAL_OPERATION) {
  2365. pExchange->LocalPendingOperations++;
  2366. }
  2367. if (PendingOperationMask & SMBCE_SEND_COMPLETE_OPERATION) {
  2368. pExchange->SendCompletePendingOperations++;
  2369. }
  2370. if (PendingOperationMask & SMBCE_COPY_DATA_OPERATION) {
  2371. pExchange->CopyDataPendingOperations++;
  2372. }
  2373. if (PendingOperationMask & SMBCE_RECEIVE_OPERATION) {
  2374. pExchange->ReceivePendingOperations++;
  2375. }
  2376. Status = STATUS_SUCCESS;
  2377. } else {
  2378. if ((PendingOperationMask & SMBCE_LOCAL_OPERATION) &&
  2379. (PendingOperationMask & ~SMBCE_LOCAL_OPERATION) == 0) {
  2380. pExchange->LocalPendingOperations++;
  2381. Status = STATUS_SUCCESS;
  2382. } else {
  2383. Status = STATUS_CONNECTION_DISCONNECTED;
  2384. }
  2385. }
  2386. } else {
  2387. Status = STATUS_UNSUCCESSFUL;
  2388. }
  2389. SmbCeReleaseSpinLock();
  2390. return Status;
  2391. }
  2392. VOID
  2393. SmbCeFinalizeExchangeWorkerThreadRoutine(
  2394. PSMB_EXCHANGE pExchange)
  2395. /*++
  2396. Routine Description:
  2397. This is the worker thread exchange finalization routine.
  2398. Arguments:
  2399. pExchange - the exchange to be finalized.
  2400. --*/
  2401. {
  2402. BOOLEAN fPostFinalize;
  2403. NTSTATUS Status;
  2404. PAGED_CODE();
  2405. Status = SMB_EXCHANGE_DISPATCH(
  2406. pExchange,
  2407. Finalize,
  2408. (pExchange,&fPostFinalize));
  2409. ASSERT(!fPostFinalize && (Status == STATUS_SUCCESS));
  2410. }
  2411. VOID
  2412. SmbCepFinalizeExchange(
  2413. PSMB_EXCHANGE pExchange)
  2414. /*++
  2415. Routine Description:
  2416. This is the common finalization routine used by both the routines below
  2417. Arguments:
  2418. pExchange - the exchange to be finalized.
  2419. --*/
  2420. {
  2421. BOOLEAN fAssociatedExchange;
  2422. ASSERT(FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_FINALIZED));
  2423. fAssociatedExchange = BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE);
  2424. if (fAssociatedExchange) {
  2425. PSMB_EXCHANGE pMasterExchange;
  2426. // The local operation will be decremented on resumption of
  2427. // the finalization routine
  2428. pMasterExchange = pExchange->Associated.pMasterExchange;
  2429. SmbCeIncrementPendingLocalOperations(pMasterExchange);
  2430. RxPostToWorkerThread(
  2431. MRxSmbDeviceObject,
  2432. CriticalWorkQueue,
  2433. &pExchange->WorkQueueItem,
  2434. SmbCepFinalizeAssociatedExchange,
  2435. pExchange);
  2436. } else {
  2437. NTSTATUS Status;
  2438. BOOLEAN fPostFinalize = FALSE;
  2439. PSMBCEDB_SERVER_ENTRY pServerEntry;
  2440. pServerEntry = SmbCeGetExchangeServerEntry(pExchange);
  2441. pExchange->ExpiryTime.QuadPart = 0;
  2442. if (!FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_RETAIN_MID)) {
  2443. SmbCeDissociateMidFromExchange(
  2444. pServerEntry,
  2445. pExchange);
  2446. }
  2447. Status = SMB_EXCHANGE_DISPATCH(
  2448. pExchange,
  2449. Finalize,
  2450. (pExchange,&fPostFinalize));
  2451. if ((Status == STATUS_SUCCESS) &&
  2452. fPostFinalize) {
  2453. // Post the request to a worker thread so that the finalization can be completed
  2454. // at a lower IRQL.
  2455. RxPostToWorkerThread(
  2456. MRxSmbDeviceObject,
  2457. CriticalWorkQueue,
  2458. &pExchange->WorkQueueItem,
  2459. SmbCeFinalizeExchangeWorkerThreadRoutine,
  2460. pExchange);
  2461. }
  2462. }
  2463. }
  2464. #define SENTINEL_ENTRY ((PSINGLE_LIST_ENTRY)IntToPtr(0xffffffff))
  2465. VOID
  2466. SmbCepFinalizeAssociatedExchange(
  2467. PSMB_EXCHANGE pExchange)
  2468. /*++
  2469. Routine Description:
  2470. This is the common finalization routine used by both the routines below
  2471. Arguments:
  2472. pExchange - the exchange to be finalized.
  2473. --*/
  2474. {
  2475. PSMB_EXCHANGE pMasterExchange;
  2476. PSMB_EXCHANGE pAssociatedExchange;
  2477. SINGLE_LIST_ENTRY AssociatedExchangeList;
  2478. ASSERT(FlagOn(pExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE));
  2479. pMasterExchange = pExchange->Associated.pMasterExchange;
  2480. ASSERT(pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next != NULL);
  2481. for (;;) {
  2482. BOOLEAN fAllAssociatedExchangesFinalized = FALSE;
  2483. SmbCeAcquireSpinLock();
  2484. if (pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next == SENTINEL_ENTRY) {
  2485. pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next = NULL;
  2486. fAllAssociatedExchangesFinalized = TRUE;
  2487. } else if (pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next == NULL) {
  2488. fAllAssociatedExchangesFinalized = TRUE;
  2489. } else {
  2490. AssociatedExchangeList.Next =
  2491. pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next;
  2492. pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next =
  2493. SENTINEL_ENTRY;
  2494. }
  2495. SmbCeReleaseSpinLock();
  2496. if (!fAllAssociatedExchangesFinalized) {
  2497. for (;;) {
  2498. PSINGLE_LIST_ENTRY pAssociatedExchangeEntry;
  2499. pAssociatedExchangeEntry = AssociatedExchangeList.Next;
  2500. if ((pAssociatedExchangeEntry != NULL) &&
  2501. (pAssociatedExchangeEntry != SENTINEL_ENTRY)) {
  2502. NTSTATUS Status;
  2503. BOOLEAN fPostFinalize = FALSE;
  2504. AssociatedExchangeList.Next = pAssociatedExchangeEntry->Next;
  2505. pAssociatedExchange = (PSMB_EXCHANGE)
  2506. CONTAINING_RECORD(
  2507. pAssociatedExchangeEntry,
  2508. SMB_EXCHANGE,
  2509. Associated.NextAssociatedExchange);
  2510. ASSERT(IsListEmpty(&pAssociatedExchange->WorkQueueItem.List));
  2511. Status = SMB_EXCHANGE_DISPATCH(
  2512. pAssociatedExchange,
  2513. Finalize,
  2514. (pAssociatedExchange,&fPostFinalize));
  2515. } else {
  2516. break;
  2517. }
  2518. };
  2519. } else {
  2520. break;
  2521. }
  2522. }
  2523. SmbCeDecrementPendingLocalOperationsAndFinalize(pMasterExchange);
  2524. }
  2525. BOOLEAN
  2526. SmbCeCanExchangeBeFinalized(
  2527. PSMB_EXCHANGE pExchange,
  2528. PSMBCE_EXCHANGE_STATUS pExchangeStatus)
  2529. /*++
  2530. Routine Description:
  2531. This routine determines if the exchange instance can be finalized.
  2532. Arguments:
  2533. pExchange - the exchange to be finalized.
  2534. pExchangeStatus - the finalization status
  2535. Return Value:
  2536. TRUE if the exchange can be finalized
  2537. Notes:
  2538. As a side effect it also sets the SMBCE_EXCHANGE_FINALIZED flag
  2539. The SmbCe spin lock must have been acquire on entry
  2540. --*/
  2541. {
  2542. BOOLEAN fFinalizeExchange = FALSE;
  2543. BOOLEAN fAssociatedExchange;
  2544. fAssociatedExchange = BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_ASSOCIATED_EXCHANGE);
  2545. if (!(pExchange->SmbCeFlags & SMBCE_EXCHANGE_FINALIZED)) {
  2546. if ((pExchange->ReceivePendingOperations == 0) &&
  2547. (pExchange->CopyDataPendingOperations == 0) &&
  2548. (pExchange->SendCompletePendingOperations == 0) &&
  2549. (pExchange->LocalPendingOperations == 0)) {
  2550. fFinalizeExchange = TRUE;
  2551. *pExchangeStatus = SmbCeExchangeFinalized;
  2552. pExchange->SmbCeFlags |= SMBCE_EXCHANGE_FINALIZED;
  2553. if (fAssociatedExchange) {
  2554. PSMB_EXCHANGE pMasterExchange = pExchange->Associated.pMasterExchange;
  2555. if (pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next != NULL) {
  2556. fFinalizeExchange = FALSE;
  2557. }
  2558. pExchange->Associated.NextAssociatedExchange.Next =
  2559. pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next;
  2560. pMasterExchange->Master.AssociatedExchangesToBeFinalized.Next =
  2561. &pExchange->Associated.NextAssociatedExchange;
  2562. }
  2563. } else {
  2564. *pExchangeStatus = SmbCeExchangeNotFinalized;
  2565. }
  2566. } else {
  2567. *pExchangeStatus = SmbCeExchangeAlreadyFinalized;
  2568. }
  2569. if (fFinalizeExchange &&
  2570. (pExchange->RxContext != NULL)) {
  2571. NTSTATUS Status;
  2572. PMRXSMB_RX_CONTEXT pMRxSmbContext;
  2573. pMRxSmbContext = MRxSmbGetMinirdrContext(pExchange->RxContext);
  2574. pMRxSmbContext->pCancelContext = NULL;
  2575. Status = RxSetMinirdrCancelRoutine(
  2576. pExchange->RxContext,
  2577. NULL);
  2578. }
  2579. return fFinalizeExchange;
  2580. }
  2581. SMBCE_EXCHANGE_STATUS
  2582. SmbCeFinalizeExchange(
  2583. PSMB_EXCHANGE pExchange)
  2584. /*++
  2585. Routine Description:
  2586. This routine finalizes an exchange instance.
  2587. Arguments:
  2588. pExchange - the exchange to be finalized.
  2589. Return Value:
  2590. appropriate exchange status
  2591. Notes:
  2592. When an exchange is initiated and the start routine is invoked a number of
  2593. SMB's are sent. This routine is invoked when all processing pertaining to the
  2594. SMB's that have been sent has ceased.
  2595. This routine encapsulates all the idiosyncratic behaviour associated with the
  2596. transports.
  2597. --*/
  2598. {
  2599. BOOLEAN fFinalizeExchange = FALSE;
  2600. SMBCE_EXCHANGE_STATUS ExchangeStatus;
  2601. SmbCeAcquireSpinLock();
  2602. fFinalizeExchange = SmbCeCanExchangeBeFinalized(
  2603. pExchange,
  2604. &ExchangeStatus);
  2605. SmbCeReleaseSpinLock();
  2606. if (fFinalizeExchange) {
  2607. SmbCepFinalizeExchange(pExchange);
  2608. }
  2609. return ExchangeStatus;
  2610. }
  2611. NTSTATUS
  2612. SmbCeDecrementPendingOperations(
  2613. PSMB_EXCHANGE pExchange,
  2614. ULONG PendingOperationMask,
  2615. PVOID FileName,
  2616. ULONG FileLine)
  2617. /*++
  2618. Routine Description:
  2619. This routine decrements the corresponding pending operation count
  2620. and finalizes an exchange instance if required
  2621. Arguments:
  2622. pExchange - the exchange to be finalized.
  2623. PendingOperationsMask -- the pending operations to be decremented.
  2624. Return Value:
  2625. appropriate exchange status
  2626. Notes:
  2627. When an exchange is initiated and the start routine is invoked a number of
  2628. SMB's are sent. This routine is invoked when all processing pertaining to the
  2629. SMB's that have been sent has ceased.
  2630. This routine encapsulates all the idiosyncratic behaviour associated with the
  2631. transports.
  2632. --*/
  2633. {
  2634. SmbCeAcquireSpinLock();
  2635. ASSERT(!BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_FINALIZED));
  2636. if (PendingOperationMask & SMBCE_LOCAL_OPERATION) {
  2637. ASSERT(pExchange->LocalPendingOperations > 0);
  2638. pExchange->LocalPendingOperations--;
  2639. }
  2640. if (PendingOperationMask & SMBCE_SEND_COMPLETE_OPERATION) {
  2641. ASSERT(pExchange->SendCompletePendingOperations > 0);
  2642. pExchange->SendCompletePendingOperations--;
  2643. }
  2644. if (PendingOperationMask & SMBCE_COPY_DATA_OPERATION) {
  2645. ASSERT(pExchange->CopyDataPendingOperations > 0);
  2646. pExchange->CopyDataPendingOperations--;
  2647. }
  2648. if ((PendingOperationMask & SMBCE_RECEIVE_OPERATION) &&
  2649. (pExchange->ReceivePendingOperations > 0)) {
  2650. pExchange->ReceivePendingOperations--;
  2651. }
  2652. SmbCeReleaseSpinLock();
  2653. return STATUS_SUCCESS;
  2654. }
  2655. SMBCE_EXCHANGE_STATUS
  2656. SmbCeDecrementPendingOperationsAndFinalize(
  2657. PSMB_EXCHANGE pExchange,
  2658. ULONG PendingOperationMask,
  2659. PVOID FileName,
  2660. ULONG FileLine)
  2661. /*++
  2662. Routine Description:
  2663. This routine decrements the corresponding pending operation count
  2664. and finalizes an exchange instance if required
  2665. Arguments:
  2666. pExchange - the exchange to be finalized.
  2667. PendingOperationsMask -- the pending operations to be decremented.
  2668. Return Value:
  2669. appropriate exchange status
  2670. Notes:
  2671. When an exchange is initiated and the start routine is invoked a number of
  2672. SMB's are sent. This routine is invoked when all processing pertaining to the
  2673. SMB's that have been sent has ceased.
  2674. This routine encapsulates all the idiosyncratic behaviour associated with the
  2675. transports.
  2676. --*/
  2677. {
  2678. BOOLEAN fFinalizeExchange = FALSE;
  2679. SMBCE_EXCHANGE_STATUS ExchangeStatus;
  2680. SmbCeAcquireSpinLock();
  2681. ASSERT(!BooleanFlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_FINALIZED));
  2682. if (PendingOperationMask & SMBCE_LOCAL_OPERATION) {
  2683. ASSERT(pExchange->LocalPendingOperations > 0);
  2684. pExchange->LocalPendingOperations--;
  2685. }
  2686. if (PendingOperationMask & SMBCE_SEND_COMPLETE_OPERATION) {
  2687. ASSERT(pExchange->SendCompletePendingOperations > 0);
  2688. pExchange->SendCompletePendingOperations--;
  2689. }
  2690. if (PendingOperationMask & SMBCE_COPY_DATA_OPERATION) {
  2691. ASSERT(pExchange->CopyDataPendingOperations > 0);
  2692. pExchange->CopyDataPendingOperations--;
  2693. }
  2694. if ((PendingOperationMask & SMBCE_RECEIVE_OPERATION) &&
  2695. (pExchange->ReceivePendingOperations > 0)) {
  2696. pExchange->ReceivePendingOperations--;
  2697. }
  2698. fFinalizeExchange = SmbCeCanExchangeBeFinalized(
  2699. pExchange,
  2700. &ExchangeStatus);
  2701. SmbCeReleaseSpinLock();
  2702. if (fFinalizeExchange) {
  2703. SmbCepFinalizeExchange(pExchange);
  2704. }
  2705. return ExchangeStatus;
  2706. }
  2707. VOID
  2708. SmbCeFinalizeExchangeOnDisconnect(
  2709. PSMB_EXCHANGE pExchange)
  2710. /*++
  2711. Routine Description:
  2712. This routine handles the finalization of an exchange instance during transport disconnects
  2713. Arguments:
  2714. pExchange - the exchange instance
  2715. --*/
  2716. {
  2717. PAGED_CODE();
  2718. if (pExchange != NULL) {
  2719. pExchange->Status = STATUS_CONNECTION_DISCONNECTED;
  2720. pExchange->SmbStatus = STATUS_CONNECTION_DISCONNECTED;
  2721. pExchange->ReceivePendingOperations = 0;
  2722. SmbCeFinalizeExchange(pExchange);
  2723. }
  2724. }
  2725. extern ULONG OffLineFileTimeoutInterval;
  2726. extern ULONG ExtendedSessTimeoutInterval;
  2727. VOID
  2728. SmbCeSetExpiryTime(
  2729. PSMB_EXCHANGE pExchange)
  2730. /*++
  2731. Routine Description:
  2732. This routine sets the expiry time for a timed exchange,
  2733. i.e., SMBCE_EXCHANGE_TIMED_OPERATION must be set
  2734. Arguments:
  2735. pExchange - the exchange instance.
  2736. Notes:
  2737. --*/
  2738. {
  2739. LARGE_INTEGER CurrentTime;
  2740. LARGE_INTEGER ExpiryTimeInTicks;
  2741. KeQueryTickCount( &CurrentTime );
  2742. ExpiryTimeInTicks.QuadPart = (1000 * 1000 * 10) / KeQueryTimeIncrement();
  2743. if (pExchange->IsOffLineFile) {
  2744. ExpiryTimeInTicks.QuadPart = OffLineFileTimeoutInterval * ExpiryTimeInTicks.QuadPart;
  2745. } else if (pExchange->SmbCeContext.pServerEntry->Server.ExtendedSessTimeout) {
  2746. ExpiryTimeInTicks.QuadPart = ExtendedSessTimeoutInterval * ExpiryTimeInTicks.QuadPart;
  2747. //DbgPrint("Set extended sesstimeout for %x %d\n",pExchange,ExtendedSessTimeoutInterval);
  2748. } else {
  2749. ExpiryTimeInTicks.QuadPart = MRxSmbConfiguration.SessionTimeoutInterval * ExpiryTimeInTicks.QuadPart;
  2750. }
  2751. pExchange->ExpiryTime.QuadPart = CurrentTime.QuadPart + ExpiryTimeInTicks.QuadPart;
  2752. }
  2753. BOOLEAN
  2754. SmbCeDetectExpiredExchanges(
  2755. PSMBCEDB_SERVER_ENTRY pServerEntry)
  2756. /*++
  2757. Routine Description:
  2758. This routine periodically walks the list of timed exchanges and chooses the
  2759. instances for finalization.
  2760. A timed exchange choosen by this routine will have waited for some network
  2761. response for the given time interval
  2762. Arguments:
  2763. pServerEntry -- the server entry for which this needs to be done
  2764. Notes:
  2765. --*/
  2766. {
  2767. BOOLEAN ExpiredExchangesDetected = FALSE;
  2768. PSMB_EXCHANGE pExchange;
  2769. PLIST_ENTRY pListHead;
  2770. PLIST_ENTRY pListEntry;
  2771. LARGE_INTEGER CurrentTime;
  2772. PAGED_CODE();
  2773. KeQueryTickCount( &CurrentTime );
  2774. SmbCeAcquireResource();
  2775. pListHead = &pServerEntry->ActiveExchanges;
  2776. pListEntry = pListHead->Flink;
  2777. while (pListEntry != pListHead) {
  2778. PLIST_ENTRY pNextListEntry;
  2779. pNextListEntry = pListEntry->Flink;
  2780. pExchange = (PSMB_EXCHANGE)CONTAINING_RECORD(pListEntry,SMB_EXCHANGE,ExchangeList);
  2781. // There are two kinds of exchanges that are candidates for
  2782. // time out finalization.
  2783. // (1) Any exchange which has a outstanding send complete
  2784. // operation which has not completed.
  2785. // (2) timed network operation exchanges which have a
  2786. // receive or copy data operation pending.
  2787. //
  2788. // In all such cases the associated server entry is marked
  2789. // for tear down and further processing is terminated.
  2790. //
  2791. if ((pExchange->SendCompletePendingOperations > 0) ||
  2792. (FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION) &&
  2793. ((pExchange->CopyDataPendingOperations > 0) ||
  2794. (pExchange->ReceivePendingOperations > 0)))) {
  2795. if ((pExchange->ExpiryTime.QuadPart != 0) &&
  2796. (pExchange->ExpiryTime.QuadPart < CurrentTime.QuadPart) &&
  2797. !FlagOn(pExchange->SmbCeFlags,SMBCE_EXCHANGE_FINALIZED)) {
  2798. RxLog(("Marking server for tear down %lx \n",pServerEntry));
  2799. SmbLogError(STATUS_UNSUCCESSFUL,
  2800. LOG,
  2801. SmbCeDetectExpiredExchanges,
  2802. LOGPTR(pExchange)
  2803. LOGPTR(pServerEntry)
  2804. LOGUSTR(pServerEntry->Name));
  2805. ExpiredExchangesDetected = TRUE;
  2806. RxLogRetail(("Exp Exch on %x (Com %x State %x)\n", pServerEntry, pExchange->SmbCommand, pExchange->SmbCeState ));
  2807. RxLogRetail(("Rcv %x Loc %x SnCo %x Copy %x\n", pExchange->ReceivePendingOperations, pExchange->LocalPendingOperations,
  2808. pExchange->SendCompletePendingOperations, pExchange->CopyDataPendingOperations ));
  2809. if( pExchange->Type == TRANSACT_EXCHANGE )
  2810. {
  2811. PSMB_TRANSACT_EXCHANGE pTransExchange = (PSMB_TRANSACT_EXCHANGE)pExchange;
  2812. PRX_CONTEXT RxContext = pTransExchange->RxContext;
  2813. RxLogRetail(("TrCmd %x NtTrans %x FID Flags %x Setup %x\n", pTransExchange->TransactSmbCommand,
  2814. pTransExchange->NtTransactFunction, pTransExchange->Flags, pTransExchange->SendSetupBufferSize ));
  2815. RxLogRetail(("Transact %x (%x,%x)\n", RxContext->ResumeRoutine, RxContext->MajorFunction, RxContext->MinorFunction ));
  2816. }
  2817. break;
  2818. }
  2819. }
  2820. pListEntry = pNextListEntry;
  2821. }
  2822. SmbCeReleaseResource();
  2823. return ExpiredExchangesDetected;
  2824. }
  2825. //
  2826. // Default handler implementation of exchange handler functions.
  2827. //
  2828. NTSTATUS
  2829. DefaultSmbExchangeIndError(
  2830. IN PSMB_EXCHANGE pExchange) // the SMB exchange instance
  2831. {
  2832. PAGED_CODE();
  2833. UNREFERENCED_PARAMETER(pExchange);
  2834. return STATUS_NOT_IMPLEMENTED;
  2835. }
  2836. NTSTATUS
  2837. DefaultSmbExchangeIndReceive(
  2838. IN PSMB_EXCHANGE pExchange) // The exchange instance
  2839. {
  2840. PAGED_CODE();
  2841. UNREFERENCED_PARAMETER(pExchange);
  2842. return STATUS_NOT_IMPLEMENTED;
  2843. }
  2844. NTSTATUS
  2845. DefaultSmbExchangeIndSendCallback(
  2846. IN PSMB_EXCHANGE pExchange) // The exchange instance
  2847. {
  2848. PAGED_CODE();
  2849. UNREFERENCED_PARAMETER(pExchange);
  2850. return STATUS_NOT_IMPLEMENTED;
  2851. }