Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1943 lines
73 KiB

  1. // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
  2. //
  3. // Copyright (c) 1985-2000 Microsoft Corporation
  4. //
  5. // This file is part of the Microsoft Research IPv6 Network Protocol Stack.
  6. // You should have received a copy of the Microsoft End-User License Agreement
  7. // for this software along with this release; see the file "license.txt".
  8. // If not, please see http://www.research.microsoft.com/msripv6/license.htm,
  9. // or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
  10. //
  11. // Abstract:
  12. //
  13. // TCP deliver data code.
  14. //
  15. // This file contains the code for delivering data to the user, including
  16. // putting data into recv. buffers and calling indication handlers.
  17. //
  18. #include "oscfg.h"
  19. #include "ndis.h"
  20. #include "ip6imp.h"
  21. #include "ip6def.h"
  22. #include "tdi.h"
  23. #include "tdint.h"
  24. #include "tdistat.h"
  25. #include "queue.h"
  26. #include "transprt.h"
  27. #include "addr.h"
  28. #include "tcp.h"
  29. #include "tcb.h"
  30. #include "tcprcv.h"
  31. #include "tcpsend.h"
  32. #include "tcpconn.h"
  33. #include "tcpdeliv.h"
  34. #include "route.h"
  35. extern KSPIN_LOCK AddrObjTableLock;
  36. extern uint
  37. PutOnRAQ(TCB *RcvTCB, TCPRcvInfo *RcvInfo, IPv6Packet *Packet, uint Size);
  38. extern IPv6Packet *
  39. TrimPacket(IPv6Packet *Packet, uint AmountToTrim);
  40. SLIST_HEADER TCPRcvReqFree; // Rcv req. free list.
  41. KSPIN_LOCK TCPRcvReqFreeLock; // Protects rcv req free list.
  42. uint NumTCPRcvReq = 0; // Current number of RcvReqs in system.
  43. uint MaxRcvReq = 0xffffffff; // Maximum allowed number of SendReqs.
  44. NTSTATUS TCPPrepareIrpForCancel(PTCP_CONTEXT TcpContext, PIRP Irp,
  45. PDRIVER_CANCEL CancelRoutine);
  46. ULONG TCPGetMdlChainByteCount(PMDL Mdl);
  47. void TCPDataRequestComplete(void *Context, unsigned int Status,
  48. unsigned int ByteCount);
  49. VOID TCPCancelRequest(PDEVICE_OBJECT Device, PIRP Irp);
  50. VOID CompleteRcvs(TCB *CmpltTCB);
  51. //* FreeRcvReq - Free a rcv request structure.
  52. //
  53. // Called to free a rcv request structure.
  54. //
  55. void // Returns: Nothing.
  56. FreeRcvReq(
  57. TCPRcvReq *FreedReq) // Rcv request structure to be freed.
  58. {
  59. PSLIST_ENTRY BufferLink;
  60. CHECK_STRUCT(FreedReq, trr);
  61. BufferLink = CONTAINING_RECORD(&(FreedReq->trr_next), SLIST_ENTRY,
  62. Next);
  63. ExInterlockedPushEntrySList(&TCPRcvReqFree, BufferLink,
  64. &TCPRcvReqFreeLock);
  65. }
  66. //* GetRcvReq - Get a recv. request structure.
  67. //
  68. // Called to get a rcv. request structure.
  69. //
  70. TCPRcvReq * // Returns: Pointer to RcvReq structure, or NULL if none.
  71. GetRcvReq(
  72. void) // Nothing.
  73. {
  74. TCPRcvReq *Temp;
  75. PSLIST_ENTRY BufferLink;
  76. BufferLink = ExInterlockedPopEntrySList(&TCPRcvReqFree,
  77. &TCPRcvReqFreeLock);
  78. if (BufferLink != NULL) {
  79. Temp = CONTAINING_RECORD(BufferLink, TCPRcvReq, trr_next);
  80. CHECK_STRUCT(Temp, trr);
  81. } else {
  82. if (NumTCPRcvReq < MaxRcvReq)
  83. Temp = ExAllocatePool(NonPagedPool, sizeof(TCPRcvReq));
  84. else
  85. Temp = NULL;
  86. if (Temp != NULL) {
  87. ExInterlockedAddUlong(&NumTCPRcvReq, 1, &TCPRcvReqFreeLock);
  88. #if DBG
  89. Temp->trr_sig = trr_signature;
  90. #endif
  91. }
  92. }
  93. return Temp;
  94. }
  95. //* FindLastPacket - Find the last packet in a chain.
  96. //
  97. // A utility routine to find the last packet in a packet chain.
  98. //
  99. IPv6Packet * // Returns: Pointer to last packet in chain.
  100. FindLastPacket(
  101. IPv6Packet *Packet) // Pointer to packet chain.
  102. {
  103. ASSERT(Packet != NULL);
  104. while (Packet->Next != NULL)
  105. Packet = Packet->Next;
  106. return Packet;
  107. }
  108. //* CovetPacketChain - Take owership of a chain of IP packets.
  109. //
  110. // Called to seize ownership of a chain of IP packets. We copy any
  111. // packets that are not already owned by us. We assume that all packets
  112. // not belonging to us start before those that do, so we quit copying
  113. // when we reach a packet we own.
  114. //
  115. IPv6Packet * // Returns: Pointer to new packet chain.
  116. CovetPacketChain(
  117. IPv6Packet *OrigPkt, // Packet chain to copy from.
  118. IPv6Packet **LastPkt, // Where to return pointer to last packet in chain.
  119. uint Size) // Maximum size in bytes to seize.
  120. {
  121. IPv6Packet *FirstPkt, *EndPkt;
  122. uint BytesToCopy;
  123. ASSERT(OrigPkt != NULL);
  124. ASSERT(Size > 0);
  125. if (!(OrigPkt->Flags & PACKET_OURS)) {
  126. BytesToCopy = MIN(Size, OrigPkt->TotalSize);
  127. FirstPkt = ExAllocatePoolWithTagPriority(NonPagedPool,
  128. sizeof(IPv6Packet) +
  129. BytesToCopy, TCP6_TAG,
  130. LowPoolPriority);
  131. if (FirstPkt != NULL) {
  132. EndPkt = FirstPkt;
  133. FirstPkt->Next = NULL;
  134. FirstPkt->Position = 0;
  135. FirstPkt->FlatData = (uchar *)(FirstPkt + 1);
  136. FirstPkt->Data = FirstPkt->FlatData;
  137. FirstPkt->ContigSize = BytesToCopy;
  138. FirstPkt->TotalSize = BytesToCopy;
  139. FirstPkt->NdisPacket = NULL;
  140. FirstPkt->AuxList = NULL;
  141. FirstPkt->Flags = PACKET_OURS;
  142. CopyPacketToBuffer(FirstPkt->Data, OrigPkt, BytesToCopy,
  143. OrigPkt->Position);
  144. Size -= BytesToCopy;
  145. OrigPkt = OrigPkt->Next;
  146. while (OrigPkt != NULL && !(OrigPkt->Flags & PACKET_OURS)
  147. && Size != 0) {
  148. IPv6Packet *NewPkt;
  149. BytesToCopy = MIN(Size, OrigPkt->TotalSize);
  150. NewPkt = ExAllocatePoolWithTagPriority(NonPagedPool,
  151. sizeof(IPv6Packet) +
  152. BytesToCopy, TCP6_TAG,
  153. LowPoolPriority);
  154. if (NewPkt != NULL) {
  155. NewPkt->Next = NULL;
  156. NewPkt->Position = 0;
  157. NewPkt->FlatData = (uchar *)(NewPkt + 1);
  158. NewPkt->Data = NewPkt->FlatData;
  159. NewPkt->ContigSize = BytesToCopy;
  160. NewPkt->TotalSize = BytesToCopy;
  161. NewPkt->Flags = PACKET_OURS;
  162. NewPkt->NdisPacket = NULL;
  163. NewPkt->AuxList = NULL;
  164. CopyPacketToBuffer(NewPkt->Data, OrigPkt, BytesToCopy,
  165. OrigPkt->Position);
  166. EndPkt->Next = NewPkt;
  167. EndPkt = NewPkt;
  168. Size -= BytesToCopy;
  169. OrigPkt = OrigPkt->Next;
  170. } else {
  171. FreePacketChain(FirstPkt);
  172. return NULL;
  173. }
  174. }
  175. EndPkt->Next = OrigPkt;
  176. } else
  177. return NULL;
  178. } else {
  179. FirstPkt = OrigPkt;
  180. EndPkt = OrigPkt;
  181. if (Size < OrigPkt->TotalSize) {
  182. OrigPkt->TotalSize = Size;
  183. OrigPkt->ContigSize = Size;
  184. }
  185. Size -= OrigPkt->TotalSize;
  186. }
  187. //
  188. // Now walk down the chain, until we run out of Size.
  189. // At this point, Size is the bytes left to 'seize' (it may be 0),
  190. // and the sizes in packets FirstPkt...EndPkt are correct.
  191. //
  192. while (Size != 0) {
  193. EndPkt = EndPkt->Next;
  194. ASSERT(EndPkt != NULL);
  195. if (Size < EndPkt->TotalSize) {
  196. EndPkt->TotalSize = Size;
  197. EndPkt->ContigSize = Size;
  198. }
  199. Size -= EndPkt->TotalSize;
  200. }
  201. // If there's anything left in the chain, free it now.
  202. if (EndPkt->Next != NULL) {
  203. FreePacketChain(EndPkt->Next);
  204. EndPkt->Next = NULL;
  205. }
  206. *LastPkt = EndPkt;
  207. return FirstPkt;
  208. }
  209. //* PendData - Pend incoming data to a client.
  210. //
  211. // Called when we need to buffer data for a client because there's no receive
  212. // down and we can't indicate.
  213. //
  214. // The TCB lock is held throughout this procedure. If this is to be changed,
  215. // make sure consistency of tcb_pendingcnt is preserved. This routine is
  216. // always called at DPC level.
  217. //
  218. uint // Returns: Number of bytes of data taken.
  219. PendData(
  220. TCB *RcvTCB, // TCB on which to receive the data.
  221. uint RcvFlags, // TCP flags for the incoming packet.
  222. IPv6Packet *InPacket, // Input buffer of packet.
  223. uint Size) // Size in bytes of data in InPacket.
  224. {
  225. IPv6Packet *NewPkt, *LastPkt;
  226. CHECK_STRUCT(RcvTCB, tcb);
  227. ASSERT(Size > 0);
  228. ASSERT(InPacket != NULL);
  229. ASSERT(RcvTCB->tcb_refcnt != 0);
  230. ASSERT(RcvTCB->tcb_fastchk & TCP_FLAG_IN_RCV);
  231. ASSERT(RcvTCB->tcb_currcv == NULL);
  232. ASSERT(RcvTCB->tcb_rcvhndlr == PendData);
  233. CheckPacketList(RcvTCB->tcb_pendhead, RcvTCB->tcb_pendingcnt);
  234. NewPkt = CovetPacketChain(InPacket, &LastPkt, Size);
  235. if (NewPkt != NULL) {
  236. //
  237. // We have a duplicate chain. Put it on the end of the pending q.
  238. //
  239. if (RcvTCB->tcb_pendhead == NULL) {
  240. RcvTCB->tcb_pendhead = NewPkt;
  241. RcvTCB->tcb_pendtail = LastPkt;
  242. } else {
  243. RcvTCB->tcb_pendtail->Next = NewPkt;
  244. RcvTCB->tcb_pendtail = LastPkt;
  245. }
  246. RcvTCB->tcb_pendingcnt += Size;
  247. } else {
  248. FreePacketChain(InPacket);
  249. Size = 0;
  250. }
  251. CheckPacketList(RcvTCB->tcb_pendhead, RcvTCB->tcb_pendingcnt);
  252. return Size;
  253. }
  254. //* BufferData - Put incoming data into client's buffer.
  255. //
  256. // Called when we believe we have a buffer into which we can put data. We put
  257. // it in there, and if we've filled the buffer or the incoming data has the
  258. // push flag set we'll mark the TCB to return the buffer. Otherwise we'll
  259. // get out and return the data later.
  260. //
  261. // In NT, this routine is called with the TCB lock held, and holds it for
  262. // the duration of the call. This is important to ensure consistency of
  263. // the tcb_pendingcnt field. If we need to change this to free the lock
  264. // partway through, make sure to take this into account. In particular,
  265. // TdiReceive zeros pendingcnt before calling this routine, and this routine
  266. // may update it. If the lock is freed in here there would be a window where
  267. // we really do have pending data, but it's not on the list or reflected in
  268. // pendingcnt. This could mess up our windowing computations, and we'd have
  269. // to be careful not to end up with more data pending than our window allows.
  270. //
  271. uint // Returns: Number of bytes of data taken.
  272. BufferData(
  273. TCB *RcvTCB, // TCB on which to receive the data.
  274. uint RcvFlags, // TCP rcv flags for the incoming packet.
  275. IPv6Packet *InPacket, // Input buffer of packet.
  276. uint Size) // Size in bytes of data in InPacket.
  277. {
  278. uchar *DestPtr; // Destination pointer.
  279. uchar *SrcPtr; // Src pointer.
  280. uint SrcSize; // Size of current source buffer.
  281. uint DestSize; // Size of current destination buffer.
  282. uint Copied; // Total bytes to copy.
  283. uint BytesToCopy; // Bytes of data to copy this time.
  284. TCPRcvReq *DestReq; // Current receive request.
  285. IPv6Packet *SrcPkt; // Current source packet.
  286. PNDIS_BUFFER DestBuf; // Current receive buffer.
  287. uint RcvCmpltd;
  288. uint Flags;
  289. CHECK_STRUCT(RcvTCB, tcb);
  290. ASSERT(Size > 0);
  291. ASSERT(InPacket != NULL);
  292. ASSERT(RcvTCB->tcb_refcnt != 0);
  293. ASSERT(RcvTCB->tcb_rcvhndlr == BufferData);
  294. //
  295. // In order to copy the received data to the application's buffers,
  296. // we now need to map those buffers into the system's address space.
  297. // Rather than attempting to map them below, where the going gets rough,
  298. // we do it up-front where errors may be more readily handled.
  299. //
  300. // N.B. We map one buffer beyond what we need, since the code below
  301. // will update the current receive-request to point beyond the data copied.
  302. //
  303. Copied = 0;
  304. for (DestReq = RcvTCB->tcb_currcv; DestReq; DestReq = DestReq->trr_next) {
  305. uint DestAvail = DestReq->trr_size - DestReq->trr_amt;
  306. for (DestBuf = DestReq->trr_buffer, DestSize = DestReq->trr_offset;
  307. DestBuf && DestAvail && Copied < Size;
  308. DestBuf = NDIS_BUFFER_LINKAGE(DestBuf), DestSize = 0) {
  309. if (!NdisBufferVirtualAddressSafe(DestBuf, NormalPagePriority)) {
  310. return 0;
  311. }
  312. DestSize = MIN(NdisBufferLength(DestBuf) - DestSize, DestAvail);
  313. DestAvail -= DestSize;
  314. Copied += DestSize;
  315. }
  316. if (Copied >= Size) {
  317. //
  318. // We've mapped the space into which we'll copy;
  319. // now map the space immediately beyond that.
  320. //
  321. if (DestAvail) {
  322. //
  323. // We believe space remains in the current receive-request;
  324. // DestBuf should point to the current buffer.
  325. //
  326. ASSERT(DestBuf);
  327. } else if ((DestReq = DestReq->trr_next) != NULL) {
  328. //
  329. // No more space in that receive-request, but there's another;
  330. // Move to this next one, and map the start of that.
  331. //
  332. DestBuf = DestReq->trr_buffer;
  333. } else {
  334. break;
  335. }
  336. if (!NdisBufferVirtualAddressSafe(DestBuf, NormalPagePriority)) {
  337. return 0;
  338. }
  339. break;
  340. }
  341. }
  342. Copied = 0;
  343. RcvCmpltd = 0;
  344. DestReq = RcvTCB->tcb_currcv;
  345. ASSERT(DestReq != NULL);
  346. CHECK_STRUCT(DestReq, trr);
  347. DestBuf = DestReq->trr_buffer;
  348. DestSize = MIN(NdisBufferLength(DestBuf) - DestReq->trr_offset,
  349. DestReq->trr_size - DestReq->trr_amt);
  350. DestPtr = (uchar *)NdisBufferVirtualAddress(DestBuf) + DestReq->trr_offset;
  351. SrcPkt = InPacket;
  352. SrcSize = SrcPkt->TotalSize;
  353. Flags = (RcvFlags & TCP_FLAG_PUSH) ? TRR_PUSHED : 0;
  354. RcvCmpltd = Flags;
  355. DestReq->trr_flags |= Flags;
  356. do {
  357. BytesToCopy = MIN(Size - Copied, MIN(SrcSize, DestSize));
  358. CopyPacketToBuffer(DestPtr, SrcPkt, BytesToCopy, SrcPkt->Position);
  359. Copied += BytesToCopy;
  360. DestReq->trr_amt += BytesToCopy;
  361. // Update our source pointers.
  362. if ((SrcSize -= BytesToCopy) == 0) {
  363. IPv6Packet *TempPkt;
  364. // We've copied everything in this packet.
  365. TempPkt = SrcPkt;
  366. SrcPkt = SrcPkt->Next;
  367. if (Size != Copied) {
  368. ASSERT(SrcPkt != NULL);
  369. SrcSize = SrcPkt->TotalSize;
  370. }
  371. TempPkt->Next = NULL;
  372. FreePacketChain(TempPkt);
  373. } else {
  374. if (BytesToCopy < SrcPkt->ContigSize) {
  375. //
  376. // We have a contiguous region, easy to skip forward.
  377. //
  378. AdjustPacketParams(SrcPkt, BytesToCopy);
  379. } else {
  380. //
  381. // REVIEW: This method isn't very efficient.
  382. //
  383. PositionPacketAt(SrcPkt, SrcPkt->Position + BytesToCopy);
  384. }
  385. }
  386. // Now check the destination pointer, and update it if we need to.
  387. if ((DestSize -= BytesToCopy) == 0) {
  388. uint DestAvail;
  389. // Exhausted this buffer. See if there's another one.
  390. DestAvail = DestReq->trr_size - DestReq->trr_amt;
  391. DestBuf = NDIS_BUFFER_LINKAGE(DestBuf);
  392. if (DestBuf != NULL && (DestAvail != 0)) {
  393. // Have another buffer in the chain. Update things.
  394. DestSize = MIN(NdisBufferLength(DestBuf), DestAvail);
  395. DestPtr = (uchar *)NdisBufferVirtualAddress(DestBuf);
  396. } else {
  397. // No more buffers in the chain. See if we have another buffer
  398. // on the list.
  399. DestReq->trr_flags |= TRR_PUSHED;
  400. // If we've been told there's to be no back traffic, get an ACK
  401. // going right away.
  402. if (DestReq->trr_flags & TDI_RECEIVE_NO_RESPONSE_EXP)
  403. DelayAction(RcvTCB, NEED_ACK);
  404. RcvCmpltd = TRUE;
  405. DestReq = DestReq->trr_next;
  406. if (DestReq != NULL) {
  407. DestBuf = DestReq->trr_buffer;
  408. DestSize = MIN(NdisBufferLength(DestBuf),
  409. DestReq->trr_size);
  410. DestPtr = (uchar *)NdisBufferVirtualAddress(DestBuf);
  411. // If we have more to put into here, set the flags.
  412. if (Copied != Size)
  413. DestReq->trr_flags |= Flags;
  414. } else {
  415. // All out of buffer space. Reset the data handler pointer.
  416. break;
  417. }
  418. }
  419. } else
  420. // Current buffer not empty yet.
  421. DestPtr += BytesToCopy;
  422. // If we've copied all that we need to, we're done.
  423. } while (Copied != Size);
  424. //
  425. // We've finished copying, and have a few more things to do. We need to
  426. // update the current rcv. pointer and possibly the offset in the
  427. // recv. request. If we need to complete any receives we have to schedule
  428. // that. If there's any data we couldn't copy we'll need to dispose of it.
  429. //
  430. RcvTCB->tcb_currcv = DestReq;
  431. if (DestReq != NULL) {
  432. DestReq->trr_buffer = DestBuf;
  433. DestReq->trr_offset = (uint) (DestPtr - (uchar *) NdisBufferVirtualAddress(DestBuf));
  434. RcvTCB->tcb_rcvhndlr = BufferData;
  435. } else
  436. RcvTCB->tcb_rcvhndlr = PendData;
  437. RcvTCB->tcb_indicated -= MIN(Copied, RcvTCB->tcb_indicated);
  438. if (Size != Copied) {
  439. IPv6Packet *NewPkt, *LastPkt;
  440. ASSERT(DestReq == NULL);
  441. // We have data to dispose of. Update the first buffer of the chain
  442. // with the current src pointer and size, and copy it.
  443. ASSERT(SrcSize <= SrcPkt->TotalSize);
  444. NewPkt = CovetPacketChain(SrcPkt, &LastPkt, Size - Copied);
  445. if (NewPkt != NULL) {
  446. // We managed to copy the chain. Push it on the pending queue.
  447. if (RcvTCB->tcb_pendhead == NULL) {
  448. RcvTCB->tcb_pendhead = NewPkt;
  449. RcvTCB->tcb_pendtail = LastPkt;
  450. } else {
  451. LastPkt->Next = RcvTCB->tcb_pendhead;
  452. RcvTCB->tcb_pendhead = NewPkt;
  453. }
  454. RcvTCB->tcb_pendingcnt += Size - Copied;
  455. Copied = Size;
  456. CheckPacketList(RcvTCB->tcb_pendhead, RcvTCB->tcb_pendingcnt);
  457. } else
  458. FreePacketChain(SrcPkt);
  459. } else {
  460. // We copied Size bytes, but the chain could be longer than that. Free
  461. // it if we need to.
  462. if (SrcPkt != NULL)
  463. FreePacketChain(SrcPkt);
  464. }
  465. if (RcvCmpltd != 0) {
  466. DelayAction(RcvTCB, NEED_RCV_CMPLT);
  467. } else {
  468. START_TCB_TIMER(RcvTCB->tcb_pushtimer, PUSH_TO);
  469. }
  470. return Copied;
  471. }
  472. //* IndicateData - Indicate incoming data to a client.
  473. //
  474. // Called when we need to indicate data to an upper layer client. We'll pass
  475. // up a pointer to whatever we have available, and the client may take some
  476. // or all of it.
  477. //
  478. uint // Returns: Number of bytes of data taken.
  479. IndicateData(
  480. TCB *RcvTCB, // TCB on which to receive the data.
  481. uint RcvFlags, // TCP receive flags for the incoming packet.
  482. IPv6Packet *InPacket, // Input buffer of packet.
  483. uint Size) // Size in bytes of data in InPacket.
  484. {
  485. TDI_STATUS Status;
  486. PRcvEvent Event;
  487. PVOID EventContext, ConnContext;
  488. uint BytesTaken = 0;
  489. EventRcvBuffer *ERB = NULL;
  490. PTDI_REQUEST_KERNEL_RECEIVE RequestInformation;
  491. PIO_STACK_LOCATION IrpSp;
  492. TCPRcvReq *RcvReq;
  493. ulong IndFlags;
  494. CHECK_STRUCT(RcvTCB, tcb);
  495. ASSERT(Size > 0);
  496. ASSERT(InPacket != NULL);
  497. ASSERT(RcvTCB->tcb_refcnt != 0);
  498. ASSERT(RcvTCB->tcb_fastchk & TCP_FLAG_IN_RCV);
  499. ASSERT(RcvTCB->tcb_rcvind != NULL);
  500. ASSERT(RcvTCB->tcb_rcvhead == NULL);
  501. ASSERT(RcvTCB->tcb_rcvhndlr == IndicateData);
  502. RcvReq = GetRcvReq();
  503. if (RcvReq != NULL) {
  504. //
  505. // The indicate handler is saved in the TCB. Just call up into it.
  506. //
  507. Event = RcvTCB->tcb_rcvind;
  508. EventContext = RcvTCB->tcb_ricontext;
  509. ConnContext = RcvTCB->tcb_conncontext;
  510. RcvTCB->tcb_indicated = Size;
  511. RcvTCB->tcb_flags |= IN_RCV_IND;
  512. KeReleaseSpinLockFromDpcLevel(&RcvTCB->tcb_lock);
  513. //
  514. // If we're at the end of a contigous data region,
  515. // move forward to the next one. This prevents us
  516. // from making nonsensical zero byte indications.
  517. //
  518. if (InPacket->ContigSize == 0) {
  519. PacketPullupSubr(InPacket, 0, 1, 0);
  520. }
  521. IF_TCPDBG(TCP_DEBUG_RECEIVE) {
  522. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  523. "Indicating %lu bytes, %lu available\n",
  524. MIN(InPacket->ContigSize, Size), Size));
  525. }
  526. #if TCP_FLAG_PUSH >= TDI_RECEIVE_ENTIRE_MESSAGE
  527. IndFlags = TDI_RECEIVE_COPY_LOOKAHEAD | TDI_RECEIVE_NORMAL |
  528. TDI_RECEIVE_AT_DISPATCH_LEVEL |
  529. ((RcvFlags & TCP_FLAG_PUSH) >>
  530. ((TCP_FLAG_PUSH / TDI_RECEIVE_ENTIRE_MESSAGE) - 1));
  531. #else
  532. IndFlags = TDI_RECEIVE_COPY_LOOKAHEAD | TDI_RECEIVE_NORMAL |
  533. TDI_RECEIVE_AT_DISPATCH_LEVEL |
  534. ((RcvFlags & TCP_FLAG_PUSH) <<
  535. ((TDI_RECEIVE_ENTIRE_MESSAGE / TCP_FLAG_PUSH) - 1));
  536. #endif
  537. Status = (*Event)(EventContext, ConnContext, IndFlags,
  538. MIN(InPacket->ContigSize, Size), Size, &BytesTaken,
  539. InPacket->Data, &ERB);
  540. IF_TCPDBG(TCP_DEBUG_RECEIVE) {
  541. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  542. "%lu bytes taken, status %lx\n", BytesTaken, Status));
  543. }
  544. //
  545. // See what the client did. If the return status is MORE_PROCESSING,
  546. // we've been given a buffer. In that case put it on the front of the
  547. // buffer queue, and if all the data wasn't taken go ahead and copy
  548. // it into the new buffer chain.
  549. //
  550. // Note that the size and buffer chain we're concerned with here is
  551. // the one that we passed to the client. Since we're in a recieve
  552. // handler, any data that has come in would have been put on the
  553. // reassembly queue.
  554. //
  555. if (Status == TDI_MORE_PROCESSING) {
  556. ASSERT(ERB != NULL);
  557. IrpSp = IoGetCurrentIrpStackLocation(ERB);
  558. Status = TCPPrepareIrpForCancel(
  559. (PTCP_CONTEXT) IrpSp->FileObject->FsContext, ERB,
  560. TCPCancelRequest);
  561. if (NT_SUCCESS(Status)) {
  562. RequestInformation = (PTDI_REQUEST_KERNEL_RECEIVE)
  563. &(IrpSp->Parameters);
  564. RcvReq->trr_rtn = TCPDataRequestComplete;
  565. RcvReq->trr_context = ERB;
  566. RcvReq->trr_buffer = ERB->MdlAddress;
  567. RcvReq->trr_size = RequestInformation->ReceiveLength;
  568. RcvReq->trr_uflags = (ushort *)
  569. &(RequestInformation->ReceiveFlags);
  570. RcvReq->trr_flags = (uint)(RequestInformation->ReceiveFlags);
  571. RcvReq->trr_offset = 0;
  572. RcvReq->trr_amt = 0;
  573. KeAcquireSpinLockAtDpcLevel(&RcvTCB->tcb_lock);
  574. RcvTCB->tcb_flags &= ~IN_RCV_IND;
  575. ASSERT(RcvTCB->tcb_rcvhndlr == IndicateData);
  576. // Push him on the front of the rcv. queue.
  577. ASSERT((RcvTCB->tcb_currcv == NULL) ||
  578. (RcvTCB->tcb_currcv->trr_amt == 0));
  579. if (RcvTCB->tcb_rcvhead == NULL) {
  580. RcvTCB->tcb_rcvhead = RcvReq;
  581. RcvTCB->tcb_rcvtail = RcvReq;
  582. RcvReq->trr_next = NULL;
  583. } else {
  584. RcvReq->trr_next = RcvTCB->tcb_rcvhead;
  585. RcvTCB->tcb_rcvhead = RcvReq;
  586. }
  587. RcvTCB->tcb_currcv = RcvReq;
  588. RcvTCB->tcb_rcvhndlr = BufferData;
  589. ASSERT(BytesTaken <= Size);
  590. RcvTCB->tcb_indicated -= BytesTaken;
  591. if ((Size -= BytesTaken) != 0) {
  592. //
  593. // Not everything was taken.
  594. // Adjust the buffer chain to point beyond what was taken.
  595. //
  596. InPacket = TrimPacket(InPacket, BytesTaken);
  597. ASSERT(InPacket != NULL);
  598. //
  599. // We've adjusted the buffer chain.
  600. // Call the BufferData handler.
  601. //
  602. BytesTaken += BufferData(RcvTCB, RcvFlags, InPacket, Size);
  603. } else {
  604. // All of the data was taken. Free the buffer chain.
  605. FreePacketChain(InPacket);
  606. }
  607. return BytesTaken;
  608. } else {
  609. //
  610. // The IRP was cancelled before it was handed back to us.
  611. // We'll pretend we never saw it. TCPPrepareIrpForCancel
  612. // already completed it. The client may have taken data,
  613. // so we will act as if success was returned.
  614. //
  615. ERB = NULL;
  616. Status = TDI_SUCCESS;
  617. }
  618. }
  619. KeAcquireSpinLockAtDpcLevel(&RcvTCB->tcb_lock);
  620. RcvTCB->tcb_flags &= ~IN_RCV_IND;
  621. //
  622. // Status is not more processing. If it's not SUCCESS, the client
  623. // didn't take any of the data. In either case we now need to
  624. // see if all of the data was taken. If it wasn't, we'll try and
  625. // push it onto the front of the pending queue.
  626. //
  627. FreeRcvReq(RcvReq); // This won't be needed.
  628. if (Status == TDI_NOT_ACCEPTED)
  629. BytesTaken = 0;
  630. ASSERT(BytesTaken <= Size);
  631. RcvTCB->tcb_indicated -= BytesTaken;
  632. ASSERT(RcvTCB->tcb_rcvhndlr == IndicateData);
  633. // Check to see if a rcv. buffer got posted during the indication.
  634. // If it did, reset the receive handler now.
  635. if (RcvTCB->tcb_rcvhead != NULL)
  636. RcvTCB->tcb_rcvhndlr = BufferData;
  637. // See if all of the data was taken.
  638. if (BytesTaken == Size) {
  639. ASSERT(RcvTCB->tcb_indicated == 0);
  640. FreePacketChain(InPacket);
  641. return BytesTaken; // It was all taken.
  642. }
  643. //
  644. // It wasn't all taken. Adjust for what was taken, and push
  645. // on the front of the pending queue. We also need to check to
  646. // see if a receive buffer got posted during the indication. This
  647. // would be weird, but not impossible.
  648. //
  649. InPacket = TrimPacket(InPacket, BytesTaken);
  650. if (RcvTCB->tcb_rcvhead == NULL) {
  651. IPv6Packet *LastPkt, *NewPkt;
  652. RcvTCB->tcb_rcvhndlr = PendData;
  653. NewPkt = CovetPacketChain(InPacket, &LastPkt, Size - BytesTaken);
  654. if (NewPkt != NULL) {
  655. // We have a duplicate chain. Push it on the front of the
  656. // pending q.
  657. if (RcvTCB->tcb_pendhead == NULL) {
  658. RcvTCB->tcb_pendhead = NewPkt;
  659. RcvTCB->tcb_pendtail = LastPkt;
  660. } else {
  661. LastPkt->Next = RcvTCB->tcb_pendhead;
  662. RcvTCB->tcb_pendhead = NewPkt;
  663. }
  664. RcvTCB->tcb_pendingcnt += Size - BytesTaken;
  665. BytesTaken = Size;
  666. } else {
  667. FreePacketChain(InPacket);
  668. }
  669. return BytesTaken;
  670. } else {
  671. //
  672. // Just great. There's now a receive buffer on the TCB.
  673. // Call the BufferData handler now.
  674. //
  675. ASSERT(RcvTCB->tcb_rcvhndlr = BufferData);
  676. BytesTaken += BufferData(RcvTCB, RcvFlags, InPacket,
  677. Size - BytesTaken);
  678. return BytesTaken;
  679. }
  680. } else {
  681. //
  682. // Couldn't get a receive request. We must be really low on resources,
  683. // so just bail out now.
  684. //
  685. FreePacketChain(InPacket);
  686. return 0;
  687. }
  688. }
  689. //* IndicatePendingData - Indicate pending data to a client.
  690. //
  691. // Called when we need to indicate pending data to an upper layer client,
  692. // usually because data arrived when we were in a state that it couldn't
  693. // be indicated.
  694. //
  695. // Many of the comments in the BufferData header about the consistency of
  696. // tcb_pendingcnt apply here also.
  697. //
  698. void // Returns: Nothing.
  699. IndicatePendingData(
  700. TCB *RcvTCB, // TCB on which to indicate the data.
  701. TCPRcvReq *RcvReq, // Receive request to use to indicate it.
  702. KIRQL PreLockIrql) // IRQL prior to acquiring TCB lock.
  703. {
  704. TDI_STATUS Status;
  705. PRcvEvent Event;
  706. PVOID EventContext, ConnContext;
  707. uint BytesTaken = 0;
  708. EventRcvBuffer *ERB = NULL;
  709. PTDI_REQUEST_KERNEL_RECEIVE RequestInformation;
  710. PIO_STACK_LOCATION IrpSp;
  711. IPv6Packet *NewPkt;
  712. uint Size;
  713. uint BytesIndicated;
  714. uint BytesAvailable;
  715. uchar* DataBuffer;
  716. CHECK_STRUCT(RcvTCB, tcb);
  717. ASSERT(RcvTCB->tcb_refcnt != 0);
  718. ASSERT(RcvTCB->tcb_rcvind != NULL);
  719. ASSERT(RcvTCB->tcb_rcvhead == NULL);
  720. ASSERT(RcvTCB->tcb_pendingcnt != 0);
  721. ASSERT(RcvReq != NULL);
  722. for (;;) {
  723. ASSERT(RcvTCB->tcb_rcvhndlr == PendData);
  724. // The indicate handler is saved in the TCB. Just call up into it.
  725. Event = RcvTCB->tcb_rcvind;
  726. EventContext = RcvTCB->tcb_ricontext;
  727. ConnContext = RcvTCB->tcb_conncontext;
  728. BytesIndicated = RcvTCB->tcb_pendhead->ContigSize;
  729. BytesAvailable = RcvTCB->tcb_pendingcnt;
  730. DataBuffer = RcvTCB->tcb_pendhead->Data;
  731. RcvTCB->tcb_indicated = RcvTCB->tcb_pendingcnt;
  732. RcvTCB->tcb_flags |= IN_RCV_IND;
  733. KeReleaseSpinLock(&RcvTCB->tcb_lock, PreLockIrql);
  734. IF_TCPDBG(TCPDebug & TCP_DEBUG_RECEIVE) {
  735. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  736. "Indicating pending %d bytes, %d available\n",
  737. BytesIndicated,
  738. BytesAvailable));
  739. }
  740. Status = (*Event)(EventContext, ConnContext,
  741. TDI_RECEIVE_COPY_LOOKAHEAD | TDI_RECEIVE_NORMAL |
  742. TDI_RECEIVE_ENTIRE_MESSAGE,
  743. BytesIndicated, BytesAvailable, &BytesTaken,
  744. DataBuffer, &ERB);
  745. IF_TCPDBG(TCPDebug & TCP_DEBUG_RECEIVE) {
  746. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  747. "%d bytes taken\n", BytesTaken));
  748. }
  749. //
  750. // See what the client did. If the return status is MORE_PROCESSING,
  751. // we've been given a buffer. In that case put it on the front of the
  752. // buffer queue, and if all the data wasn't taken go ahead and copy
  753. // it into the new buffer chain.
  754. //
  755. if (Status == TDI_MORE_PROCESSING) {
  756. IF_TCPDBG(TCP_DEBUG_RECEIVE) {
  757. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  758. "more processing on receive\n"));
  759. }
  760. ASSERT(ERB != NULL);
  761. IrpSp = IoGetCurrentIrpStackLocation(ERB);
  762. Status = TCPPrepareIrpForCancel(
  763. (PTCP_CONTEXT) IrpSp->FileObject->FsContext, ERB,
  764. TCPCancelRequest);
  765. if (NT_SUCCESS(Status)) {
  766. RequestInformation = (PTDI_REQUEST_KERNEL_RECEIVE)
  767. &(IrpSp->Parameters);
  768. RcvReq->trr_rtn = TCPDataRequestComplete;
  769. RcvReq->trr_context = ERB;
  770. RcvReq->trr_buffer = ERB->MdlAddress;
  771. RcvReq->trr_size = RequestInformation->ReceiveLength;
  772. RcvReq->trr_uflags = (ushort *)
  773. &(RequestInformation->ReceiveFlags);
  774. RcvReq->trr_flags = (uint)(RequestInformation->ReceiveFlags);
  775. RcvReq->trr_offset = 0;
  776. RcvReq->trr_amt = 0;
  777. KeAcquireSpinLock(&RcvTCB->tcb_lock, &PreLockIrql);
  778. RcvTCB->tcb_flags &= ~IN_RCV_IND;
  779. // Push him on the front of the receive queue.
  780. ASSERT((RcvTCB->tcb_currcv == NULL) ||
  781. (RcvTCB->tcb_currcv->trr_amt == 0));
  782. if (RcvTCB->tcb_rcvhead == NULL) {
  783. RcvTCB->tcb_rcvhead = RcvReq;
  784. RcvTCB->tcb_rcvtail = RcvReq;
  785. RcvReq->trr_next = NULL;
  786. } else {
  787. RcvReq->trr_next = RcvTCB->tcb_rcvhead;
  788. RcvTCB->tcb_rcvhead = RcvReq;
  789. }
  790. RcvTCB->tcb_currcv = RcvReq;
  791. RcvTCB->tcb_rcvhndlr = BufferData;
  792. //
  793. // Have to pick up the new size and pointers now, since things
  794. // could have changed during the upcall.
  795. //
  796. Size = RcvTCB->tcb_pendingcnt;
  797. NewPkt = RcvTCB->tcb_pendhead;
  798. RcvTCB->tcb_pendingcnt = 0;
  799. RcvTCB->tcb_pendhead = NULL;
  800. ASSERT(BytesTaken <= Size);
  801. RcvTCB->tcb_indicated -= BytesTaken;
  802. if ((Size -= BytesTaken) != 0) {
  803. //
  804. // Not everything was taken. Adjust the buffer chain to
  805. // point beyond what was taken.
  806. //
  807. NewPkt = TrimPacket(NewPkt, BytesTaken);
  808. ASSERT(NewPkt != NULL);
  809. //
  810. // We've adjusted the buffer chain.
  811. // Call the BufferData handler.
  812. //
  813. (void)BufferData(RcvTCB, TCP_FLAG_PUSH, NewPkt, Size);
  814. KeReleaseSpinLock(&RcvTCB->tcb_lock, PreLockIrql);
  815. } else {
  816. //
  817. // All of the data was taken. Free the buffer chain.
  818. // Since we were passed a buffer chain which we put on the
  819. // head of the list, leave the rcvhandler pointing at
  820. // BufferData.
  821. //
  822. ASSERT(RcvTCB->tcb_rcvhndlr == BufferData);
  823. ASSERT(RcvTCB->tcb_indicated == 0);
  824. ASSERT(RcvTCB->tcb_rcvhead != NULL);
  825. KeReleaseSpinLock(&RcvTCB->tcb_lock, PreLockIrql);
  826. FreePacketChain(NewPkt);
  827. }
  828. return;
  829. } else {
  830. //
  831. // The IRP was cancelled before it was handed back to us.
  832. // We'll pretend we never saw it. TCPPrepareIrpForCancel
  833. // already completed it. The client may have taken data,
  834. // so we will act as if success was returned.
  835. //
  836. ERB = NULL;
  837. Status = TDI_SUCCESS;
  838. }
  839. }
  840. KeAcquireSpinLock(&RcvTCB->tcb_lock, &PreLockIrql);
  841. RcvTCB->tcb_flags &= ~IN_RCV_IND;
  842. //
  843. // Status is not more processing. If it's not SUCCESS, the client
  844. // didn't take any of the data. In either case we now need to
  845. // see if all of the data was taken. If it wasn't, we're done.
  846. //
  847. if (Status == TDI_NOT_ACCEPTED)
  848. BytesTaken = 0;
  849. ASSERT(RcvTCB->tcb_rcvhndlr == PendData);
  850. RcvTCB->tcb_indicated -= BytesTaken;
  851. Size = RcvTCB->tcb_pendingcnt;
  852. NewPkt = RcvTCB->tcb_pendhead;
  853. ASSERT(BytesTaken <= Size);
  854. // See if all of the data was taken.
  855. if (BytesTaken == Size) {
  856. // It was all taken. Zap the pending data information.
  857. RcvTCB->tcb_pendingcnt = 0;
  858. RcvTCB->tcb_pendhead = NULL;
  859. ASSERT(RcvTCB->tcb_indicated == 0);
  860. if (RcvTCB->tcb_rcvhead == NULL) {
  861. if (RcvTCB->tcb_rcvind != NULL)
  862. RcvTCB->tcb_rcvhndlr = IndicateData;
  863. } else
  864. RcvTCB->tcb_rcvhndlr = BufferData;
  865. KeReleaseSpinLock(&RcvTCB->tcb_lock, PreLockIrql);
  866. FreePacketChain(NewPkt);
  867. break;
  868. }
  869. //
  870. // It wasn't all taken. Adjust for what was taken; we also need to
  871. // check to see if a receive buffer got posted during the indication.
  872. // This would be weird, but not impossible.
  873. //
  874. NewPkt = TrimPacket(NewPkt, BytesTaken);
  875. ASSERT(RcvTCB->tcb_rcvhndlr == PendData);
  876. if (RcvTCB->tcb_rcvhead == NULL) {
  877. RcvTCB->tcb_pendhead = NewPkt;
  878. RcvTCB->tcb_pendingcnt -= BytesTaken;
  879. if (RcvTCB->tcb_indicated != 0 || RcvTCB->tcb_rcvind == NULL) {
  880. KeReleaseSpinLock(&RcvTCB->tcb_lock, PreLockIrql);
  881. break;
  882. }
  883. // From here, we'll loop around and indicate the new data that
  884. // presumably came in during the previous indication.
  885. } else {
  886. //
  887. // Just great. There's now a receive buffer on the TCB.
  888. // Call the BufferData handler now.
  889. //
  890. RcvTCB->tcb_rcvhndlr = BufferData;
  891. RcvTCB->tcb_pendingcnt = 0;
  892. RcvTCB->tcb_pendhead = NULL;
  893. BytesTaken += BufferData(RcvTCB, TCP_FLAG_PUSH, NewPkt,
  894. Size - BytesTaken);
  895. KeReleaseSpinLock(&RcvTCB->tcb_lock, PreLockIrql);
  896. break;
  897. }
  898. } // for (;;)
  899. FreeRcvReq(RcvReq); // This isn't needed anymore.
  900. }
  901. //* DeliverUrgent - Deliver urgent data to a client.
  902. //
  903. // Called to deliver urgent data to a client. We assume the input
  904. // urgent data is in a buffer we can keep. The buffer can be NULL, in
  905. // which case we'll just look on the urgent pending queue for data.
  906. //
  907. void // Returns: Nothing.
  908. DeliverUrgent(
  909. TCB *RcvTCB, // TCB to deliver on.
  910. IPv6Packet *RcvPkt, // Packet for urgent data.
  911. uint Size, // Number of bytes of urgent data to deliver.
  912. KIRQL *pTCBIrql) // Location of KIRQL prior to acquiring TCB lock.
  913. {
  914. KIRQL Irql1, Irql2, Irql3; // One per lock nesting level.
  915. TCPRcvReq *RcvReq, *PrevReq;
  916. uint BytesTaken = 0;
  917. IPv6Packet *LastPkt;
  918. EventRcvBuffer *ERB;
  919. PRcvEvent ExpRcv;
  920. PVOID ExpRcvContext;
  921. PVOID ConnContext;
  922. TDI_STATUS Status;
  923. CHECK_STRUCT(RcvTCB, tcb);
  924. ASSERT(RcvTCB->tcb_refcnt != 0);
  925. CheckPacketList(RcvTCB->tcb_urgpending, RcvTCB->tcb_urgcnt);
  926. //
  927. // See if we have new data, or are processing old data.
  928. //
  929. if (RcvPkt != NULL) {
  930. //
  931. // We have new data. If the pending queue is not NULL, or we're
  932. // already in this routine, just put the buffer on the end of the
  933. // queue.
  934. //
  935. if (RcvTCB->tcb_urgpending != NULL ||
  936. (RcvTCB->tcb_flags & IN_DELIV_URG)) {
  937. IPv6Packet *PrevRcvPkt;
  938. // Put him on the end of the queue.
  939. PrevRcvPkt = CONTAINING_RECORD(&RcvTCB->tcb_urgpending, IPv6Packet,
  940. Next);
  941. while (PrevRcvPkt->Next != NULL)
  942. PrevRcvPkt = PrevRcvPkt->Next;
  943. PrevRcvPkt->Next = RcvPkt;
  944. return;
  945. }
  946. } else {
  947. //
  948. // The input buffer is NULL. See if we have existing data, or are in
  949. // this routine. If we have no existing data or are in this routine
  950. // just return.
  951. //
  952. if (RcvTCB->tcb_urgpending == NULL ||
  953. (RcvTCB->tcb_flags & IN_DELIV_URG)) {
  954. return;
  955. } else {
  956. RcvPkt = RcvTCB->tcb_urgpending;
  957. Size = RcvTCB->tcb_urgcnt;
  958. RcvTCB->tcb_urgpending = NULL;
  959. RcvTCB->tcb_urgcnt = 0;
  960. }
  961. }
  962. ASSERT(RcvPkt != NULL);
  963. ASSERT(!(RcvTCB->tcb_flags & IN_DELIV_URG));
  964. //
  965. // We know we have data to deliver, and we have a pointer and a size.
  966. // Go into a loop, trying to deliver the data. On each iteration, we'll
  967. // try to find a buffer for the data. If we find one, we'll copy and
  968. // complete it right away. Otherwise we'll try and indicate it. If we
  969. // can't indicate it, we'll put it on the pending queue and leave.
  970. //
  971. RcvTCB->tcb_flags |= IN_DELIV_URG;
  972. RcvTCB->tcb_slowcount++;
  973. RcvTCB->tcb_fastchk |= TCP_FLAG_SLOW;
  974. CheckTCBRcv(RcvTCB);
  975. do {
  976. CheckPacketList(RcvTCB->tcb_urgpending, RcvTCB->tcb_urgcnt);
  977. BytesTaken = 0;
  978. // First check the expedited queue.
  979. if ((RcvReq = RcvTCB->tcb_exprcv) != NULL)
  980. RcvTCB->tcb_exprcv = RcvReq->trr_next;
  981. else {
  982. //
  983. // Nothing in the expedited receive queue. Walk down the ordinary
  984. // receive queue, looking for a buffer that we can steal.
  985. //
  986. PrevReq = CONTAINING_RECORD(&RcvTCB->tcb_rcvhead, TCPRcvReq,
  987. trr_next);
  988. RcvReq = PrevReq->trr_next;
  989. while (RcvReq != NULL) {
  990. CHECK_STRUCT(RcvReq, trr);
  991. if (RcvReq->trr_flags & TDI_RECEIVE_EXPEDITED) {
  992. // This is a candidate.
  993. if (RcvReq->trr_amt == 0) {
  994. ASSERT(RcvTCB->tcb_rcvhndlr == BufferData);
  995. //
  996. // And he has nothing currently in him.
  997. // Pull him out of the queue.
  998. //
  999. if (RcvTCB->tcb_rcvtail == RcvReq) {
  1000. if (RcvTCB->tcb_rcvhead == RcvReq)
  1001. RcvTCB->tcb_rcvtail = NULL;
  1002. else
  1003. RcvTCB->tcb_rcvtail = PrevReq;
  1004. }
  1005. PrevReq->trr_next = RcvReq->trr_next;
  1006. if (RcvTCB->tcb_currcv == RcvReq) {
  1007. RcvTCB->tcb_currcv = RcvReq->trr_next;
  1008. if (RcvTCB->tcb_currcv == NULL) {
  1009. //
  1010. // We've taken the last receive from the list.
  1011. // Reset the rcvhndlr.
  1012. //
  1013. if (RcvTCB->tcb_rcvind != NULL &&
  1014. RcvTCB->tcb_indicated == 0)
  1015. RcvTCB->tcb_rcvhndlr = IndicateData;
  1016. else
  1017. RcvTCB->tcb_rcvhndlr = PendData;
  1018. }
  1019. }
  1020. break;
  1021. }
  1022. }
  1023. PrevReq = RcvReq;
  1024. RcvReq = PrevReq->trr_next;
  1025. }
  1026. }
  1027. //
  1028. // We've done our best to get a buffer. If we got one, copy into it
  1029. // now, and complete the request.
  1030. //
  1031. if (RcvReq != NULL) {
  1032. // Got a buffer.
  1033. KeReleaseSpinLock(&RcvTCB->tcb_lock, *pTCBIrql);
  1034. BytesTaken = CopyPacketToNdis(RcvReq->trr_buffer, RcvPkt,
  1035. Size, 0, RcvPkt->Position);
  1036. (*RcvReq->trr_rtn)(RcvReq->trr_context, TDI_SUCCESS, BytesTaken);
  1037. FreeRcvReq(RcvReq);
  1038. KeAcquireSpinLock(&RcvTCB->tcb_lock, pTCBIrql);
  1039. RcvTCB->tcb_urgind -= MIN(RcvTCB->tcb_urgind, BytesTaken);
  1040. } else {
  1041. // No posted buffer. If we can indicate, do so.
  1042. if (RcvTCB->tcb_urgind == 0) {
  1043. TCPConn *Conn;
  1044. // See if he has an expedited rcv handler.
  1045. ConnContext = RcvTCB->tcb_conncontext;
  1046. KeReleaseSpinLock(&RcvTCB->tcb_lock, *pTCBIrql);
  1047. KeAcquireSpinLock(&AddrObjTableLock, &Irql1);
  1048. KeAcquireSpinLock(
  1049. &ConnTable[CONN_BLOCKID(RcvTCB->tcb_connid)]->cb_lock,
  1050. &Irql2);
  1051. KeAcquireSpinLock(&RcvTCB->tcb_lock, pTCBIrql);
  1052. if ((Conn = RcvTCB->tcb_conn) != NULL) {
  1053. CHECK_STRUCT(Conn, tc);
  1054. ASSERT(Conn->tc_tcb == RcvTCB);
  1055. KeReleaseSpinLock(&RcvTCB->tcb_lock, *pTCBIrql);
  1056. if (Conn->tc_ao != NULL) {
  1057. AddrObj *AO;
  1058. AO = Conn->tc_ao;
  1059. KeAcquireSpinLock(&AO->ao_lock, &Irql3);
  1060. if (AO_VALID(AO) && (ExpRcv = AO->ao_exprcv) != NULL) {
  1061. ExpRcvContext = AO->ao_exprcvcontext;
  1062. KeReleaseSpinLock(&AO->ao_lock, Irql3);
  1063. // We're going to indicate.
  1064. RcvTCB->tcb_urgind = Size;
  1065. KeReleaseSpinLock(&Conn->tc_ConnBlock->cb_lock,
  1066. Irql2);
  1067. KeReleaseSpinLock(&AddrObjTableLock, Irql1);
  1068. Status = (*ExpRcv)(ExpRcvContext, ConnContext,
  1069. TDI_RECEIVE_COPY_LOOKAHEAD |
  1070. TDI_RECEIVE_ENTIRE_MESSAGE |
  1071. TDI_RECEIVE_EXPEDITED,
  1072. MIN(RcvPkt->ContigSize, Size),
  1073. Size, &BytesTaken, RcvPkt->Data,
  1074. &ERB);
  1075. KeAcquireSpinLock(&RcvTCB->tcb_lock, pTCBIrql);
  1076. // See what he did with it.
  1077. if (Status == TDI_MORE_PROCESSING) {
  1078. uint CopySize;
  1079. // He gave us a buffer.
  1080. if (BytesTaken == Size) {
  1081. //
  1082. // He gave us a buffer, but took all of
  1083. // it. We'll just return it to him.
  1084. //
  1085. CopySize = 0;
  1086. } else {
  1087. // We have some data to copy in.
  1088. RcvPkt = TrimPacket(RcvPkt, BytesTaken);
  1089. ASSERT(RcvPkt->TotalSize != 0);
  1090. CopySize = CopyPacketToNdis(
  1091. ERB->MdlAddress, RcvPkt,
  1092. MIN(Size - BytesTaken,
  1093. TCPGetMdlChainByteCount(
  1094. ERB->MdlAddress)), 0,
  1095. RcvPkt->Position);
  1096. }
  1097. BytesTaken += CopySize;
  1098. RcvTCB->tcb_urgind -= MIN(RcvTCB->tcb_urgind,
  1099. BytesTaken);
  1100. KeReleaseSpinLock(&RcvTCB->tcb_lock,
  1101. *pTCBIrql);
  1102. ERB->IoStatus.Status = TDI_SUCCESS;
  1103. ERB->IoStatus.Information = CopySize;
  1104. IoCompleteRequest(ERB, 2);
  1105. KeAcquireSpinLock(&RcvTCB->tcb_lock, pTCBIrql);
  1106. } else {
  1107. // No buffer to deal with.
  1108. if (Status == TDI_NOT_ACCEPTED)
  1109. BytesTaken = 0;
  1110. RcvTCB->tcb_urgind -= MIN(RcvTCB->tcb_urgind,
  1111. BytesTaken);
  1112. }
  1113. goto checksize;
  1114. } else {
  1115. // No receive handler.
  1116. KeReleaseSpinLock(&AO->ao_lock, Irql3);
  1117. }
  1118. }
  1119. // Conn->tc_ao == NULL.
  1120. KeReleaseSpinLock(&Conn->tc_ConnBlock->cb_lock, Irql2);
  1121. KeReleaseSpinLock(&AddrObjTableLock, Irql1);
  1122. KeAcquireSpinLock(&RcvTCB->tcb_lock, pTCBIrql);
  1123. } else {
  1124. // RcvTCB has invalid index.
  1125. KeReleaseSpinLock(
  1126. &ConnTable[CONN_BLOCKID(RcvTCB->tcb_connid)]->cb_lock,
  1127. *pTCBIrql);
  1128. KeReleaseSpinLock(&AddrObjTableLock, Irql2);
  1129. *pTCBIrql = Irql1;
  1130. }
  1131. }
  1132. //
  1133. // For whatever reason we couldn't indicate the data. At this
  1134. // point we hold the lock on the TCB. Push the buffer onto the
  1135. // pending queue and return.
  1136. //
  1137. CheckPacketList(RcvTCB->tcb_urgpending, RcvTCB->tcb_urgcnt);
  1138. LastPkt = FindLastPacket(RcvPkt);
  1139. LastPkt->Next = RcvTCB->tcb_urgpending;
  1140. RcvTCB->tcb_urgpending = RcvPkt;
  1141. RcvTCB->tcb_urgcnt += Size;
  1142. break;
  1143. }
  1144. checksize:
  1145. //
  1146. // See how much we took. If we took it all, check the pending queue.
  1147. // At this point, we should hold the lock on the TCB.
  1148. //
  1149. if (Size == BytesTaken) {
  1150. // Took it all.
  1151. FreePacketChain(RcvPkt);
  1152. RcvPkt = RcvTCB->tcb_urgpending;
  1153. Size = RcvTCB->tcb_urgcnt;
  1154. } else {
  1155. //
  1156. // We didn't manage to take it all. Free what we did take,
  1157. // and then merge with the pending queue.
  1158. //
  1159. RcvPkt = TrimPacket(RcvPkt, BytesTaken);
  1160. Size = Size - BytesTaken + RcvTCB->tcb_urgcnt;
  1161. if (RcvTCB->tcb_urgpending != NULL) {
  1162. //
  1163. // Find the end of the current Packet chain, so we can merge.
  1164. //
  1165. LastPkt = FindLastPacket(RcvPkt);
  1166. LastPkt->Next = RcvTCB->tcb_urgpending;
  1167. }
  1168. }
  1169. RcvTCB->tcb_urgpending = NULL;
  1170. RcvTCB->tcb_urgcnt = 0;
  1171. } while (RcvPkt != NULL);
  1172. CheckPacketList(RcvTCB->tcb_urgpending, RcvTCB->tcb_urgcnt);
  1173. RcvTCB->tcb_flags &= ~IN_DELIV_URG;
  1174. if (--(RcvTCB->tcb_slowcount) == 0) {
  1175. RcvTCB->tcb_fastchk &= ~TCP_FLAG_SLOW;
  1176. CheckTCBRcv(RcvTCB);
  1177. }
  1178. }
  1179. //* PushData - Push all data back to the client.
  1180. //
  1181. // Called when we've received a FIN and need to push data to the client.
  1182. //
  1183. void // Returns: Nothing.
  1184. PushData(
  1185. TCB *PushTCB) // TCB to be pushed.
  1186. {
  1187. TCPRcvReq *RcvReq;
  1188. CHECK_STRUCT(PushTCB, tcb);
  1189. RcvReq = PushTCB->tcb_rcvhead;
  1190. while (RcvReq != NULL) {
  1191. CHECK_STRUCT(RcvReq, trr);
  1192. RcvReq->trr_flags |= TRR_PUSHED;
  1193. RcvReq = RcvReq->trr_next;
  1194. }
  1195. RcvReq = PushTCB->tcb_exprcv;
  1196. while (RcvReq != NULL) {
  1197. CHECK_STRUCT(RcvReq, trr);
  1198. RcvReq->trr_flags |= TRR_PUSHED;
  1199. RcvReq = RcvReq->trr_next;
  1200. }
  1201. if (PushTCB->tcb_rcvhead != NULL || PushTCB->tcb_exprcv != NULL)
  1202. DelayAction(PushTCB, NEED_RCV_CMPLT);
  1203. }
  1204. //* SplitPacket - Split an IPv6Packet into three pieces.
  1205. //
  1206. // This function takes an input IPv6Packet and splits it into three pieces.
  1207. // The first piece is the input buffer, which we just skip over. The second
  1208. // and third pieces are actually copied, even if we already own them, so
  1209. // that they may go to different places.
  1210. //
  1211. // Note: *SecondBuf and *ThirdBuf are set to NULL if we can't allocate
  1212. // memory for them.
  1213. //
  1214. void // Returns: Nothing.
  1215. SplitPacket(
  1216. IPv6Packet *Packet, // Packet chain to be split.
  1217. uint Size, // Total size in bytes of packet chain.
  1218. uint Offset, // Offset to skip over.
  1219. uint SecondSize, // Size in bytes of second piece.
  1220. IPv6Packet **SecondPkt, // Where to return second packet pointer.
  1221. IPv6Packet **ThirdPkt) // Where to return third packet pointer.
  1222. {
  1223. IPv6Packet *Temp;
  1224. uint ThirdSize;
  1225. ASSERT(Offset < Size);
  1226. ASSERT(((Offset + SecondSize) < Size) ||
  1227. (((Offset + SecondSize) == Size) && ThirdPkt == NULL));
  1228. ASSERT(Packet != NULL);
  1229. //
  1230. // Packet points at the packet to copy from, and Offset is the offset into
  1231. // this packet to copy from.
  1232. //
  1233. if (SecondPkt != NULL) {
  1234. //
  1235. // We need to allocate memory for a second packet.
  1236. //
  1237. Temp = ExAllocatePoolWithTagPriority(NonPagedPool,
  1238. sizeof(IPv6Packet) + SecondSize,
  1239. TCP6_TAG, LowPoolPriority);
  1240. if (Temp != NULL) {
  1241. Temp->Next = NULL;
  1242. Temp->Position = 0;
  1243. Temp->FlatData = (uchar *)(Temp + 1);
  1244. Temp->Data = Temp->FlatData;
  1245. Temp->ContigSize = SecondSize;
  1246. Temp->TotalSize = SecondSize;
  1247. Temp->NdisPacket = NULL;
  1248. Temp->AuxList = NULL;
  1249. Temp->Flags = PACKET_OURS;
  1250. CopyPacketToBuffer(Temp->Data, Packet, SecondSize,
  1251. Packet->Position + Offset);
  1252. *SecondPkt = Temp;
  1253. } else {
  1254. *SecondPkt = NULL;
  1255. if (ThirdPkt != NULL)
  1256. *ThirdPkt = NULL;
  1257. return;
  1258. }
  1259. }
  1260. if (ThirdPkt != NULL) {
  1261. //
  1262. // We need to allocate memory for a third buffer.
  1263. //
  1264. ThirdSize = Size - (Offset + SecondSize);
  1265. Temp = ExAllocatePoolWithTagPriority(NonPagedPool,
  1266. sizeof(IPv6Packet) + ThirdSize,
  1267. TCP6_TAG, LowPoolPriority);
  1268. if (Temp != NULL) {
  1269. Temp->Next = NULL;
  1270. Temp->Position = 0;
  1271. Temp->FlatData = (uchar *)(Temp + 1);
  1272. Temp->Data = Temp->FlatData;
  1273. Temp->ContigSize = ThirdSize;
  1274. Temp->TotalSize = ThirdSize;
  1275. Temp->NdisPacket = NULL;
  1276. Temp->AuxList = NULL;
  1277. Temp->Flags = PACKET_OURS;
  1278. CopyPacketToBuffer(Temp->Data, Packet, ThirdSize,
  1279. Packet->Position + Offset + SecondSize);
  1280. *ThirdPkt = Temp;
  1281. } else
  1282. *ThirdPkt = NULL;
  1283. }
  1284. }
  1285. //* HandleUrgent - Handle urgent data.
  1286. //
  1287. // Called when an incoming segment has urgent data in it. We make sure there
  1288. // really is urgent data in the segment, and if there is we try to dispose
  1289. // of it either by putting it into a posted buffer or calling an exp. rcv.
  1290. // indication handler.
  1291. //
  1292. // This routine is called at DPC level, and with the TCP locked.
  1293. //
  1294. // Urgent data handling is a little complicated. Each TCB has the starting
  1295. // and ending sequence numbers of the 'current' (last received) bit of urgent
  1296. // data. It is possible that the start of the current urgent data might be
  1297. // greater than tcb_rcvnext, if urgent data came in, we handled it, and then
  1298. // couldn't take the preceding normal data. The urgent valid flag is cleared
  1299. // when the next byte of data the user would read (rcvnext - pendingcnt) is
  1300. // greater than the end of urgent data - we do this so that we can correctly
  1301. // support SIOCATMARK. We always seperate urgent data out of the data stream.
  1302. // If the urgent valid field is set when we get into this routing we have
  1303. // to play a couple of games. If the incoming segment starts in front of the
  1304. // current urgent data, we truncate it before the urgent data, and put any
  1305. // data after the urgent data on the reassemble queue. These gyrations are
  1306. // done to avoid delivering the same urgent data twice. If the urgent valid
  1307. // field in the TCB is set and the segment starts after the current urgent
  1308. // data the new urgent information will replace the current urgent
  1309. // information.
  1310. //
  1311. void // Returns: Nothing.
  1312. HandleUrgent(
  1313. TCB *RcvTCB, // TCB to recv the data on.
  1314. TCPRcvInfo *RcvInfo, // RcvInfo structure for the incoming segment.
  1315. IPv6Packet *RcvPkt, // Packet chain containing the incoming segment.
  1316. uint *Size) // Size in bytes of data in the segment.
  1317. {
  1318. uint BytesInFront; // Bytes in front of the urgent data.
  1319. uint BytesInBack; // Bytes in back of the urgent data.
  1320. uint UrgSize; // Size in bytes of urgent data.
  1321. SeqNum UrgStart, UrgEnd;
  1322. IPv6Packet *EndPkt, *UrgPkt;
  1323. TCPRcvInfo NewRcvInfo;
  1324. KIRQL TCBIrql;
  1325. CHECK_STRUCT(RcvTCB, tcb);
  1326. ASSERT(RcvTCB->tcb_refcnt != 0);
  1327. ASSERT(RcvInfo->tri_flags & TCP_FLAG_URG);
  1328. ASSERT(SEQ_EQ(RcvInfo->tri_seq, RcvTCB->tcb_rcvnext));
  1329. // First, validate the urgent pointer.
  1330. if (RcvTCB->tcb_flags & BSD_URGENT) {
  1331. //
  1332. // We're using BSD style urgent data. We assume that the urgent
  1333. // data is one byte long, and that the urgent pointer points one
  1334. // after the urgent data instead of at the last byte of urgent data.
  1335. // See if the urgent data is in this segment.
  1336. //
  1337. if (RcvInfo->tri_urgent == 0 || RcvInfo->tri_urgent > *Size) {
  1338. //
  1339. // Not in this segment. Clear the urgent flag and return.
  1340. //
  1341. RcvInfo->tri_flags &= ~TCP_FLAG_URG;
  1342. return;
  1343. }
  1344. UrgSize = 1;
  1345. BytesInFront = RcvInfo->tri_urgent - 1;
  1346. } else {
  1347. //
  1348. // This is not BSD style urgent. We assume that the urgent data
  1349. // starts at the front of the segment and the last byte is pointed
  1350. // to by the urgent data pointer.
  1351. //
  1352. BytesInFront = 0;
  1353. UrgSize = MIN(RcvInfo->tri_urgent + 1, *Size);
  1354. }
  1355. BytesInBack = *Size - BytesInFront - UrgSize;
  1356. //
  1357. // UrgStart and UrgEnd are the first and last sequence numbers of the
  1358. // urgent data in this segment.
  1359. //
  1360. UrgStart = RcvInfo->tri_seq + BytesInFront;
  1361. UrgEnd = UrgStart + UrgSize - 1;
  1362. if (!(RcvTCB->tcb_flags & URG_INLINE)) {
  1363. EndPkt = NULL;
  1364. // Now see if this overlaps with any urgent data we've already seen.
  1365. if (RcvTCB->tcb_flags & URG_VALID) {
  1366. //
  1367. // We have some urgent data still around. See if we've advanced
  1368. // rcvnext beyond the urgent data. If we have, this is new urgent
  1369. // data, and we can go ahead and process it (although anyone doing
  1370. // an SIOCATMARK socket command might get confused). If we haven't
  1371. // consumed the data in front of the existing urgent data yet,
  1372. // we'll truncate this seg. to that amount and push the rest onto
  1373. // the reassembly queue. Note that rcvnext should never fall
  1374. // between tcb_urgstart and tcb_urgend.
  1375. //
  1376. ASSERT(SEQ_LT(RcvTCB->tcb_rcvnext, RcvTCB->tcb_urgstart) ||
  1377. SEQ_GT(RcvTCB->tcb_rcvnext, RcvTCB->tcb_urgend));
  1378. if (SEQ_LT(RcvTCB->tcb_rcvnext, RcvTCB->tcb_urgstart)) {
  1379. //
  1380. // There appears to be some overlap in the data stream.
  1381. // Split the buffer up into pieces that come before the current
  1382. // urgent data and after the current urgent data, putting the
  1383. // latter on the reassembly queue.
  1384. //
  1385. UrgSize = RcvTCB->tcb_urgend - RcvTCB->tcb_urgstart + 1;
  1386. BytesInFront = MIN(RcvTCB->tcb_urgstart - RcvTCB->tcb_rcvnext,
  1387. (int) *Size);
  1388. if (SEQ_GT(RcvTCB->tcb_rcvnext + *Size, RcvTCB->tcb_urgend)) {
  1389. // We have data after this piece of urgent data.
  1390. BytesInBack = RcvTCB->tcb_rcvnext + *Size -
  1391. RcvTCB->tcb_urgend;
  1392. } else
  1393. BytesInBack = 0;
  1394. SplitPacket(RcvPkt, *Size, BytesInFront, UrgSize, NULL,
  1395. (BytesInBack ? &EndPkt : NULL));
  1396. if (EndPkt != NULL) {
  1397. NewRcvInfo.tri_seq = RcvTCB->tcb_urgend + 1;
  1398. if (UrgEnd > RcvTCB->tcb_urgend) {
  1399. NewRcvInfo.tri_flags = RcvInfo->tri_flags;
  1400. NewRcvInfo.tri_urgent = UrgEnd - NewRcvInfo.tri_seq;
  1401. if (RcvTCB->tcb_flags & BSD_URGENT)
  1402. NewRcvInfo.tri_urgent++;
  1403. } else {
  1404. NewRcvInfo.tri_flags = RcvInfo->tri_flags &
  1405. ~TCP_FLAG_URG;
  1406. }
  1407. NewRcvInfo.tri_ack = RcvInfo->tri_ack;
  1408. NewRcvInfo.tri_window = RcvInfo->tri_window;
  1409. PutOnRAQ(RcvTCB, &NewRcvInfo, EndPkt, BytesInBack);
  1410. FreePacketChain(EndPkt);
  1411. }
  1412. *Size = BytesInFront;
  1413. RcvInfo->tri_flags &= ~TCP_FLAG_URG;
  1414. return;
  1415. }
  1416. }
  1417. //
  1418. // We have urgent data we can process now. Split it into its component
  1419. // parts, the first part, the urgent data, and the stuff after the
  1420. // urgent data.
  1421. //
  1422. SplitPacket(RcvPkt, *Size, BytesInFront, UrgSize, &UrgPkt,
  1423. (BytesInBack ? &EndPkt : NULL));
  1424. //
  1425. // If we managed to split out the end stuff, put it on the queue now.
  1426. //
  1427. if (EndPkt != NULL) {
  1428. NewRcvInfo.tri_seq = RcvInfo->tri_seq + BytesInFront + UrgSize;
  1429. NewRcvInfo.tri_flags = RcvInfo->tri_flags & ~TCP_FLAG_URG;
  1430. NewRcvInfo.tri_ack = RcvInfo->tri_ack;
  1431. NewRcvInfo.tri_window = RcvInfo->tri_window;
  1432. PutOnRAQ(RcvTCB, &NewRcvInfo, EndPkt, BytesInBack);
  1433. FreePacketChain(EndPkt);
  1434. }
  1435. if (UrgPkt != NULL) {
  1436. // We succesfully split the urgent data out.
  1437. if (!(RcvTCB->tcb_flags & URG_VALID)) {
  1438. RcvTCB->tcb_flags |= URG_VALID;
  1439. RcvTCB->tcb_slowcount++;
  1440. RcvTCB->tcb_fastchk |= TCP_FLAG_SLOW;
  1441. CheckTCBRcv(RcvTCB);
  1442. }
  1443. RcvTCB->tcb_urgstart = UrgStart;
  1444. RcvTCB->tcb_urgend = UrgEnd;
  1445. TCBIrql = DISPATCH_LEVEL;
  1446. DeliverUrgent(RcvTCB, UrgPkt, UrgSize, &TCBIrql);
  1447. }
  1448. *Size = BytesInFront;
  1449. } else {
  1450. //
  1451. // Urgent data is to be processed inline. We just need to remember
  1452. // where it is and treat it as normal data. If there's already urgent
  1453. // data, we remember the latest urgent data.
  1454. //
  1455. RcvInfo->tri_flags &= ~TCP_FLAG_URG;
  1456. if (RcvTCB->tcb_flags & URG_VALID) {
  1457. //
  1458. // There is urgent data. See if this stuff comes after the
  1459. // existing urgent data.
  1460. //
  1461. if (SEQ_LTE(UrgEnd, RcvTCB->tcb_urgend)) {
  1462. //
  1463. // The existing urgent data completely overlaps this stuff,
  1464. // so ignore this.
  1465. //
  1466. return;
  1467. }
  1468. } else {
  1469. RcvTCB->tcb_flags |= URG_VALID;
  1470. RcvTCB->tcb_slowcount++;
  1471. RcvTCB->tcb_fastchk |= TCP_FLAG_SLOW;
  1472. CheckTCBRcv(RcvTCB);
  1473. }
  1474. RcvTCB->tcb_urgstart = UrgStart;
  1475. RcvTCB->tcb_urgend = UrgEnd;
  1476. }
  1477. return;
  1478. }
  1479. //* TdiReceive - Process a receive request.
  1480. //
  1481. // This is the main TDI receive request handler. We validate the connection
  1482. // and make sure that we have a TCB in the proper state, then we try to
  1483. // allocate a receive request structure. If that succeeds, we'll look and
  1484. // see what's happening on the TCB - if there's pending data, we'll put it
  1485. // in the buffer. Otherwise we'll just queue the receive for later.
  1486. //
  1487. TDI_STATUS // Returns: TDI_STATUS of request.
  1488. TdiReceive(
  1489. PTDI_REQUEST Request, // TDI_REQUEST structure for this request.
  1490. ushort *Flags, // Pointer to flags word.
  1491. uint *RcvLength, // Pointer to length in bytes of receive buffer.
  1492. PNDIS_BUFFER Buffer) // Pointer to buffer to take data.
  1493. {
  1494. TCPConn *Conn;
  1495. TCB *RcvTCB;
  1496. TCPRcvReq *RcvReq;
  1497. KIRQL Irql0, Irql1; // One per lock nesting level.
  1498. TDI_STATUS Error;
  1499. ushort UFlags;
  1500. Conn = GetConnFromConnID(PtrToUlong(Request->Handle.ConnectionContext),
  1501. &Irql0);
  1502. if (Conn != NULL) {
  1503. CHECK_STRUCT(Conn, tc);
  1504. RcvTCB = Conn->tc_tcb;
  1505. if (RcvTCB != NULL) {
  1506. CHECK_STRUCT(RcvTCB, tcb);
  1507. KeAcquireSpinLock(&RcvTCB->tcb_lock, &Irql1);
  1508. KeReleaseSpinLock(&Conn->tc_ConnBlock->cb_lock, Irql1);
  1509. UFlags = *Flags;
  1510. //
  1511. // Verify that the cached RCE is still valid.
  1512. //
  1513. RcvTCB->tcb_rce = ValidateRCE(RcvTCB->tcb_rce);
  1514. ASSERT(RcvTCB->tcb_rce != NULL);
  1515. //
  1516. // Fail new receive requests for TCBs in an invalid state
  1517. // and for TCBs with a disconnected outgoing interface
  1518. // (except when a loopback route is used).
  1519. //
  1520. if ((DATA_RCV_STATE(RcvTCB->tcb_state) ||
  1521. (RcvTCB->tcb_pendingcnt != 0 && (UFlags & TDI_RECEIVE_NORMAL)) ||
  1522. (RcvTCB->tcb_urgcnt != 0 && (UFlags & TDI_RECEIVE_EXPEDITED)))
  1523. && !CLOSING(RcvTCB)
  1524. && !IsDisconnectedAndNotLoopbackRCE(RcvTCB->tcb_rce)) {
  1525. //
  1526. // We have a TCB, and it's valid. Get a receive request now.
  1527. //
  1528. CheckPacketList(RcvTCB->tcb_pendhead, RcvTCB->tcb_pendingcnt);
  1529. RcvReq = GetRcvReq();
  1530. if (RcvReq != NULL) {
  1531. RcvReq->trr_rtn = Request->RequestNotifyObject;
  1532. RcvReq->trr_context = Request->RequestContext;
  1533. RcvReq->trr_buffer = Buffer;
  1534. RcvReq->trr_size = *RcvLength;
  1535. RcvReq->trr_uflags = Flags;
  1536. RcvReq->trr_offset = 0;
  1537. RcvReq->trr_amt = 0;
  1538. RcvReq->trr_flags = (uint)UFlags;
  1539. if ((UFlags & (TDI_RECEIVE_NORMAL | TDI_RECEIVE_EXPEDITED))
  1540. != TDI_RECEIVE_EXPEDITED) {
  1541. //
  1542. // This is not an expedited only receive.
  1543. // Put it on the normal receive queue.
  1544. //
  1545. RcvReq->trr_next = NULL;
  1546. if (RcvTCB->tcb_rcvhead == NULL) {
  1547. // The receive queue is empty.
  1548. // Put it on the front.
  1549. RcvTCB->tcb_rcvhead = RcvReq;
  1550. RcvTCB->tcb_rcvtail = RcvReq;
  1551. } else {
  1552. RcvTCB->tcb_rcvtail->trr_next = RcvReq;
  1553. RcvTCB->tcb_rcvtail = RcvReq;
  1554. }
  1555. //
  1556. // If this receive is for zero length, complete this
  1557. // and indicate pending data again, if any.
  1558. //
  1559. if (RcvReq->trr_size == 0) {
  1560. RcvTCB->tcb_refcnt++;
  1561. RcvReq->trr_flags |= TRR_PUSHED;
  1562. KeReleaseSpinLock(&RcvTCB->tcb_lock, Irql0);
  1563. CompleteRcvs(RcvTCB);
  1564. KeAcquireSpinLock(&RcvTCB->tcb_lock, &Irql0);
  1565. DerefTCB(RcvTCB, Irql0);
  1566. return TDI_PENDING;
  1567. }
  1568. //
  1569. // If this receive can't hold urgent data or there
  1570. // isn't any pending urgent data continue processing.
  1571. //
  1572. if (!(UFlags & TDI_RECEIVE_EXPEDITED) ||
  1573. RcvTCB->tcb_urgcnt == 0) {
  1574. //
  1575. // If tcb_currcv is NULL, there is no currently
  1576. // active receive. In this case, check to see if
  1577. // there is pending data and that we are not
  1578. // currently in a receive indication handler. If
  1579. // both of these are true then deal with the
  1580. // pending data.
  1581. //
  1582. if (RcvTCB->tcb_currcv == NULL) {
  1583. RcvTCB->tcb_currcv = RcvReq;
  1584. // No currently active receive.
  1585. if (!(RcvTCB->tcb_flags & IN_RCV_IND)) {
  1586. // Not in a rcv. indication.
  1587. RcvTCB->tcb_rcvhndlr = BufferData;
  1588. if (RcvTCB->tcb_pendhead == NULL) {
  1589. KeReleaseSpinLock(&RcvTCB->tcb_lock,
  1590. Irql0);
  1591. return TDI_PENDING;
  1592. } else {
  1593. IPv6Packet *PendPacket;
  1594. uint PendSize;
  1595. uint OldRcvWin;
  1596. // We have pending data to deal with.
  1597. PendPacket = RcvTCB->tcb_pendhead;
  1598. PendSize = RcvTCB->tcb_pendingcnt;
  1599. RcvTCB->tcb_pendhead = NULL;
  1600. RcvTCB->tcb_pendingcnt = 0;
  1601. RcvTCB->tcb_refcnt++;
  1602. //
  1603. // We assume that BufferData holds
  1604. // the lock (does not yield) during
  1605. // this call. If this changes for some
  1606. // reason, we'll have to fix the code
  1607. // below that does the window update
  1608. // check. See the comments in the
  1609. // BufferData() routine for more info.
  1610. //
  1611. (void)BufferData(RcvTCB, TCP_FLAG_PUSH,
  1612. PendPacket, PendSize);
  1613. CheckTCBRcv(RcvTCB);
  1614. //
  1615. // Now we need to see if the window
  1616. // has changed. If it has, send an
  1617. // ACK.
  1618. //
  1619. OldRcvWin = RcvTCB->tcb_rcvwin;
  1620. if (OldRcvWin != RcvWin(RcvTCB)) {
  1621. // The window has changed, so send
  1622. // an ACK.
  1623. DelayAction(RcvTCB, NEED_ACK);
  1624. }
  1625. DerefTCB(RcvTCB, Irql0);
  1626. ProcessTCBDelayQ();
  1627. return TDI_PENDING;
  1628. }
  1629. }
  1630. //
  1631. // In a receive indication. The receive request
  1632. // is now on the queue, so just fall through
  1633. // to the return.
  1634. //
  1635. }
  1636. //
  1637. // A receive is currently active. No need to do
  1638. // anything else.
  1639. //
  1640. KeReleaseSpinLock(&RcvTCB->tcb_lock, Irql0);
  1641. return TDI_PENDING;
  1642. } else {
  1643. //
  1644. // This buffer can hold urgent data and we have
  1645. // some pending. Deliver it now.
  1646. //
  1647. RcvTCB->tcb_refcnt++;
  1648. DeliverUrgent(RcvTCB, NULL, 0, &Irql0);
  1649. DerefTCB(RcvTCB, Irql0);
  1650. return TDI_PENDING;
  1651. }
  1652. } else {
  1653. TCPRcvReq *Temp;
  1654. //
  1655. // This is an expedited only receive. Just put it
  1656. // on the end of the expedited receive queue.
  1657. //
  1658. Temp = CONTAINING_RECORD(&RcvTCB->tcb_exprcv,
  1659. TCPRcvReq, trr_next);
  1660. while (Temp->trr_next != NULL)
  1661. Temp = Temp->trr_next;
  1662. RcvReq->trr_next = NULL;
  1663. Temp->trr_next = RcvReq;
  1664. if (RcvTCB->tcb_urgpending != NULL) {
  1665. RcvTCB->tcb_refcnt++;
  1666. DeliverUrgent(RcvTCB, NULL, 0, &Irql0);
  1667. DerefTCB(RcvTCB, Irql0);
  1668. return TDI_PENDING;
  1669. } else
  1670. Error = TDI_PENDING;
  1671. }
  1672. } else {
  1673. // Couldn't get a receive request.
  1674. Error = TDI_NO_RESOURCES;
  1675. }
  1676. } else {
  1677. // The TCB is in an invalid state.
  1678. Error = TDI_INVALID_STATE;
  1679. }
  1680. KeReleaseSpinLock(&RcvTCB->tcb_lock, Irql0);
  1681. return Error;
  1682. } else {
  1683. // No TCB for connection.
  1684. KeReleaseSpinLock(&Conn->tc_ConnBlock->cb_lock, Irql0);
  1685. Error = TDI_INVALID_STATE;
  1686. }
  1687. } else {
  1688. // No connection.
  1689. Error = TDI_INVALID_CONNECTION;
  1690. }
  1691. return Error;
  1692. }