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

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