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.

664 lines
20 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990-1993 **/
  4. /********************************************************************/
  5. /* :ts=4 */
  6. //** TLCOMMON.C - Common transport layer code.
  7. //
  8. // This file contains the code for routines that are common to
  9. // both TCP and UDP.
  10. //
  11. #include "precomp.h"
  12. #include "tlcommon.h"
  13. #include "tcpipbuf.h"
  14. extern TCPXSUM_ROUTINE tcpxsum_routine;
  15. //extern uint tcpxsum(uint Seed, void *Ptr, uint Length);
  16. extern IPInfo LocalNetInfo;
  17. //* TcpipCopyBufferToNdisBuffer
  18. //
  19. // This routine copies data described by the source buffer to the NDIS_BUFFER
  20. // chain described by DestinationNdisBuffer. On NT, this really translates
  21. // directly to TdiCopyBufferToMdl since an NDIS_BUFFER is an MDL.
  22. //
  23. // Input:
  24. //
  25. // SourceBuffer - pointer to the source buffer
  26. //
  27. // SourceOffset - Number of bytes to skip in the source data.
  28. //
  29. // SourceBytesToCopy - number of bytes to copy from the source buffer
  30. //
  31. // DestinationNdisBuffer - Pointer to a chain of NDIS_BUFFERs describing the
  32. // destination buffers.
  33. //
  34. // DestinationOffset - Number of bytes to skip in the destination data.
  35. //
  36. // BytesCopied - Pointer to a longword where the actual number of bytes
  37. // transferred will be returned.
  38. #if MILLEN
  39. NTSTATUS
  40. TcpipCopyBufferToNdisBuffer (
  41. IN PVOID SourceBuffer,
  42. IN ULONG SourceOffset,
  43. IN ULONG SourceBytesToCopy,
  44. IN PNDIS_BUFFER DestinationNdisBuffer,
  45. IN ULONG DestinationOffset,
  46. IN PULONG BytesCopied
  47. )
  48. {
  49. PUCHAR Dest, Src;
  50. ULONG DestBytesLeft, BytesSkipped=0;
  51. *BytesCopied = 0;
  52. if (SourceBytesToCopy == 0) {
  53. return STATUS_SUCCESS;
  54. }
  55. //
  56. // Skip Destination bytes.
  57. //
  58. Dest = NdisBufferVirtualAddress(DestinationNdisBuffer);
  59. DestBytesLeft = NdisBufferLength(DestinationNdisBuffer);
  60. while (BytesSkipped < DestinationOffset) {
  61. if (DestBytesLeft > (DestinationOffset - BytesSkipped)) {
  62. DestBytesLeft -= (DestinationOffset - BytesSkipped);
  63. Dest += (DestinationOffset - BytesSkipped);
  64. BytesSkipped = DestinationOffset;
  65. break;
  66. } else if (DestBytesLeft == (DestinationOffset - BytesSkipped)) {
  67. DestinationNdisBuffer = DestinationNdisBuffer->Next;
  68. if (DestinationNdisBuffer == NULL) {
  69. return STATUS_BUFFER_OVERFLOW; // no bytes copied.
  70. }
  71. BytesSkipped = DestinationOffset;
  72. Dest = NdisBufferVirtualAddress(DestinationNdisBuffer);
  73. DestBytesLeft = NdisBufferLength(DestinationNdisBuffer);
  74. break;
  75. } else {
  76. BytesSkipped += DestBytesLeft;
  77. DestinationNdisBuffer = DestinationNdisBuffer->Next;
  78. if (DestinationNdisBuffer == NULL) {
  79. return STATUS_BUFFER_OVERFLOW; // no bytes copied.
  80. }
  81. Dest = NdisBufferVirtualAddress(DestinationNdisBuffer);
  82. DestBytesLeft = NdisBufferLength(DestinationNdisBuffer);
  83. }
  84. }
  85. //
  86. // Skip source bytes.
  87. //
  88. Src = (PUCHAR)SourceBuffer + SourceOffset;
  89. //
  90. // Copy source data into the destination buffer until it's full or
  91. // we run out of data, whichever comes first.
  92. //
  93. while ((SourceBytesToCopy != 0) && (DestinationNdisBuffer != NULL)) {
  94. if (DestBytesLeft == 0) {
  95. DestinationNdisBuffer = DestinationNdisBuffer->Next;
  96. if (DestinationNdisBuffer == NULL) {
  97. return STATUS_BUFFER_OVERFLOW;
  98. }
  99. Dest = NdisBufferVirtualAddress(DestinationNdisBuffer);
  100. DestBytesLeft = NdisBufferLength(DestinationNdisBuffer);
  101. continue; // skip 0-length MDL's.
  102. }
  103. if (DestBytesLeft >= SourceBytesToCopy) {
  104. RtlCopyBytes (Dest, Src, SourceBytesToCopy);
  105. *BytesCopied += SourceBytesToCopy;
  106. return STATUS_SUCCESS;
  107. } else {
  108. RtlCopyBytes (Dest, Src, DestBytesLeft);
  109. *BytesCopied += DestBytesLeft;
  110. SourceBytesToCopy -= DestBytesLeft;
  111. Src += DestBytesLeft;
  112. DestBytesLeft = 0;
  113. }
  114. }
  115. return SourceBytesToCopy == 0 ? STATUS_SUCCESS : STATUS_BUFFER_OVERFLOW;
  116. }
  117. #endif // MILLEN
  118. //* PrefetchRcvbuf in to L1 cache
  119. // Called when received segment checksum is already computed by
  120. // the hardware
  121. //
  122. // Input: IPRcvBuf - Buffer chain indicated by IP
  123. // returns: None
  124. //
  125. //
  126. #if !MILLEN
  127. __inline
  128. void
  129. PrefetchRcvBuf(IPRcvBuf *BufChain)
  130. {
  131. while (BufChain) {
  132. RtlPrefetchMemoryNonTemporal(BufChain->ipr_buffer,BufChain->ipr_size);
  133. BufChain = BufChain->ipr_next;
  134. }
  135. }
  136. #endif // !MILLEN
  137. //* XsumSendChain - Checksum a chain of NDIS send buffers.
  138. //
  139. // Called to xsum a chain of NDIS send buffers. We're given the
  140. // pseudo-header xsum to start with, and we call xsum on each
  141. // buffer. We assume that this is a send chain, and that the
  142. // first buffer of the chain has room for an IP header that we
  143. // need to skip.
  144. //
  145. // Input: PHXsum - Pseudo-header xsum.
  146. // BufChain - Pointer to NDIS_BUFFER chain.
  147. //
  148. // Returns: The computed xsum.
  149. //
  150. ushort
  151. XsumSendChain(uint PHXsum, PNDIS_BUFFER BufChain)
  152. {
  153. uint HeaderSize;
  154. uint OldLength;
  155. uint SwapCount;
  156. uchar *Ptr;
  157. HeaderSize = LocalNetInfo.ipi_hsize;
  158. OldLength = 0;
  159. SwapCount = 0;
  160. //
  161. // ***** The following line of code can be removed if the pseudo
  162. // checksum never has any bits sets in the upper word.
  163. //
  164. PHXsum = (((PHXsum << 16) | (PHXsum >> 16)) + PHXsum) >> 16;
  165. do {
  166. //
  167. // If the length of the last buffer was odd, then swap the checksum.
  168. //
  169. if ((OldLength & 1) != 0) {
  170. PHXsum = ((PHXsum & 0xff) << 8) | (PHXsum >> 8);
  171. SwapCount ^= 1;
  172. }
  173. #if MILLEN
  174. //
  175. // Some TDI Clients on Windows ME have been known to end a buffer
  176. // chain with a 0 length buffer. Just continue to the next buffer.
  177. //
  178. if (NdisBufferLength(BufChain))
  179. #endif // MILLEN
  180. {
  181. Ptr = (uchar *) TcpipBufferVirtualAddress(BufChain, NormalPagePriority);
  182. if (Ptr == NULL) {
  183. // Return zero checksum. All should recover.
  184. return (0);
  185. }
  186. Ptr = Ptr + HeaderSize;
  187. //PHXsum = tcpxsum(PHXsum, Ptr, NdisBufferLength(BufChain));
  188. PHXsum = tcpxsum_routine(PHXsum, Ptr, NdisBufferLength(BufChain));
  189. HeaderSize = 0;
  190. OldLength = NdisBufferLength(BufChain);
  191. }
  192. BufChain = NDIS_BUFFER_LINKAGE(BufChain);
  193. } while (BufChain != NULL);
  194. //
  195. // If an odd number of swaps were done, then swap the xsum again.
  196. //
  197. // N.B. At this point the checksum is only a word.
  198. //
  199. if (SwapCount != 0) {
  200. PHXsum = ((PHXsum & 0xff) << 8) | (PHXsum >> 8);
  201. }
  202. return (ushort) PHXsum;
  203. }
  204. //* XsumRcvBuf - Checksum a chain of IP receive buffers.
  205. //
  206. // Called to xsum a chain of IP receive buffers. We're given the
  207. // pseudo-header xsum to start with, and we call xsum on each buffer.
  208. //
  209. // We assume that this rcv buf chain has no odd sized buffers, except
  210. // possibly the last one.
  211. //
  212. // Input: PHXsum - Pseudo-header xsum.
  213. // BufChain - Pointer to IPRcvBuf chain.
  214. //
  215. // Returns: The computed xsum.
  216. //
  217. ushort
  218. XsumRcvBuf(uint PHXsum, IPRcvBuf * BufChain)
  219. {
  220. //
  221. // ***** The following line of code can be removed if the pseudo
  222. // checksum never has any bits sets in the upper word.
  223. //
  224. PHXsum = (((PHXsum << 16) | (PHXsum >> 16)) + PHXsum) >> 16;
  225. do {
  226. //PHXsum = tcpxsum(PHXsum, BufChain->ipr_buffer, BufChain->ipr_size);
  227. PHXsum = tcpxsum_routine(PHXsum, BufChain->ipr_buffer, BufChain->ipr_size);
  228. BufChain = BufChain->ipr_next;
  229. } while (BufChain != NULL);
  230. return (ushort) (PHXsum);
  231. }
  232. //* CopyRcvToNdis - Copy from an IPRcvBuf chain to an NDIS buffer chain.
  233. //
  234. // This is the function we use to copy from a chain of IP receive buffers
  235. // to a chain of NDIS buffers. The caller specifies the source and destination,
  236. // a maximum size to copy, and an offset into the first buffer to start
  237. // copying from. We copy as much as possible up to the size, and return
  238. // the size copied.
  239. //
  240. // Input: RcvBuf - Pointer to receive buffer chain.
  241. // DestBuf - Pointer to NDIS buffer chain.
  242. // Size - Size in bytes to copy.
  243. // RcvOffset - Offset into first buffer to copy from.
  244. // DestOffset - Offset into dest buffer to start copying at.
  245. //
  246. // Returns: Bytes copied.
  247. //
  248. uint
  249. CopyRcvToNdis(IPRcvBuf * RcvBuf, PNDIS_BUFFER DestBuf, uint Size,
  250. uint RcvOffset, uint DestOffset)
  251. {
  252. uint TotalBytesCopied = 0; // Bytes we've copied so far.
  253. uint BytesCopied = 0; // Bytes copied out of each buffer.
  254. uint DestSize, RcvSize; // Size left in current destination and
  255. // recv. buffers, respectively.
  256. uint BytesToCopy; // How many bytes to copy this time.
  257. NTSTATUS Status;
  258. PNDIS_BUFFER pTempBuf;
  259. ASSERT(RcvBuf != NULL);
  260. ASSERT(RcvOffset <= RcvBuf->ipr_size);
  261. // The destination buffer can be NULL - this is valid, if odd.
  262. if (DestBuf != NULL) {
  263. RcvSize = RcvBuf->ipr_size - RcvOffset;
  264. //
  265. // Need to calculate length of full MDL chain. TdiCopyBufferToMdl
  266. // will do the right thing with multiple MDLs.
  267. //
  268. pTempBuf = DestBuf;
  269. DestSize = 0;
  270. do {
  271. DestSize += NdisBufferLength(pTempBuf);
  272. pTempBuf = NDIS_BUFFER_LINKAGE(pTempBuf);
  273. }
  274. while (pTempBuf);
  275. if (Size < DestSize) {
  276. DestSize = Size;
  277. }
  278. do {
  279. // Compute the amount to copy, and then copy from the
  280. // appropriate offsets.
  281. BytesToCopy = MIN(DestSize, RcvSize);
  282. Status = TcpipCopyBufferToNdisBuffer(RcvBuf->ipr_buffer, RcvOffset,
  283. BytesToCopy, DestBuf, DestOffset, &BytesCopied);
  284. if (!NT_SUCCESS(Status)) {
  285. break;
  286. }
  287. ASSERT(BytesCopied == BytesToCopy);
  288. TotalBytesCopied += BytesCopied;
  289. DestSize -= BytesCopied;
  290. DestOffset += BytesCopied;
  291. RcvSize -= BytesToCopy;
  292. if (!RcvSize) {
  293. // Exhausted this buffer.
  294. RcvBuf = RcvBuf->ipr_next;
  295. // If we have another one, use it.
  296. if (RcvBuf != NULL) {
  297. RcvOffset = 0;
  298. RcvSize = RcvBuf->ipr_size;
  299. } else {
  300. break;
  301. }
  302. } else { // Buffer not exhausted, update offset.
  303. RcvOffset += BytesToCopy;
  304. }
  305. } while (DestSize);
  306. }
  307. return TotalBytesCopied;
  308. }
  309. uint
  310. CopyRcvToMdl(IPRcvBuf * RcvBuf, PMDL DestBuf, uint Size,
  311. uint RcvOffset, uint DestOffset)
  312. {
  313. uint TotalBytesCopied = 0; // Bytes we've copied so far.
  314. uint BytesCopied = 0; // Bytes copied out of each buffer.
  315. uint DestSize, RcvSize; // Size left in current destination and
  316. // recv. buffers, respectively.
  317. uint BytesToCopy; // How many bytes to copy this time.
  318. NTSTATUS Status;
  319. PMDL pTempBuf;
  320. ASSERT(RcvBuf != NULL);
  321. ASSERT(RcvOffset <= RcvBuf->ipr_size);
  322. // The destination buffer can be NULL - this is valid, if odd.
  323. if (DestBuf != NULL) {
  324. RcvSize = RcvBuf->ipr_size - RcvOffset;
  325. //
  326. // Need to calculate length of full MDL chain. TdiCopyBufferToMdl
  327. // will do the right thing with multiple MDLs.
  328. //
  329. pTempBuf = DestBuf;
  330. DestSize = 0;
  331. do {
  332. DestSize += MmGetMdlByteCount(pTempBuf);
  333. pTempBuf = pTempBuf->Next;
  334. }
  335. while (pTempBuf);
  336. if (Size < DestSize) {
  337. DestSize = Size;
  338. }
  339. do {
  340. // Compute the amount to copy, and then copy from the
  341. // appropriate offsets.
  342. BytesToCopy = MIN(DestSize, RcvSize);
  343. Status = TdiCopyBufferToMdl(RcvBuf->ipr_buffer, RcvOffset,
  344. BytesToCopy, DestBuf, DestOffset, &BytesCopied);
  345. if (!NT_SUCCESS(Status)) {
  346. break;
  347. }
  348. ASSERT(BytesCopied == BytesToCopy);
  349. TotalBytesCopied += BytesCopied;
  350. DestSize -= BytesCopied;
  351. DestOffset += BytesCopied;
  352. RcvSize -= BytesToCopy;
  353. if (!RcvSize) {
  354. // Exhausted this buffer.
  355. RcvBuf = RcvBuf->ipr_next;
  356. // If we have another one, use it.
  357. if (RcvBuf != NULL) {
  358. RcvOffset = 0;
  359. RcvSize = RcvBuf->ipr_size;
  360. } else {
  361. break;
  362. }
  363. } else { // Buffer not exhausted, update offset.
  364. RcvOffset += BytesToCopy;
  365. }
  366. } while (DestSize);
  367. }
  368. return TotalBytesCopied;
  369. }
  370. //* CopyRcvToBuffer - Copy from an IPRcvBuf chain to a flat buffer.
  371. //
  372. // Called during receive processing to copy from an IPRcvBuffer chain to a
  373. // flag buffer. We skip Offset bytes in the src chain, and then
  374. // copy Size bytes.
  375. //
  376. // Input: DestBuf - Pointer to destination buffer.
  377. // SrcRB - Pointer to SrcRB chain.
  378. // Size - Size in bytes to copy.
  379. // SrcOffset - Offset in SrcRB to start copying from.
  380. //
  381. // Returns: Nothing.
  382. //
  383. void
  384. CopyRcvToBuffer(uchar * DestBuf, IPRcvBuf * SrcRB, uint Size, uint SrcOffset)
  385. {
  386. #if DBG
  387. IPRcvBuf *TempRB;
  388. uint TempSize;
  389. #endif
  390. ASSERT(DestBuf != NULL);
  391. ASSERT(SrcRB != NULL);
  392. // In debug versions check to make sure we're copying a reasonable size
  393. // and from a reasonable offset.
  394. #if DBG
  395. TempRB = SrcRB;
  396. TempSize = 0;
  397. while (TempRB != NULL) {
  398. TempSize += TempRB->ipr_size;
  399. TempRB = TempRB->ipr_next;
  400. }
  401. ASSERT(SrcOffset < TempSize);
  402. ASSERT((SrcOffset + Size) <= TempSize);
  403. #endif
  404. // First, skip Offset bytes.
  405. while (SrcOffset >= SrcRB->ipr_size) {
  406. SrcOffset -= SrcRB->ipr_size;
  407. SrcRB = SrcRB->ipr_next;
  408. }
  409. while (Size != 0) {
  410. uint BytesToCopy, SrcSize;
  411. ASSERT(SrcRB != NULL);
  412. SrcSize = SrcRB->ipr_size - SrcOffset;
  413. BytesToCopy = MIN(Size, SrcSize);
  414. RtlCopyMemory(DestBuf, SrcRB->ipr_buffer + SrcOffset, BytesToCopy);
  415. if (BytesToCopy == SrcSize) {
  416. // Copied everything from this buffer.
  417. SrcRB = SrcRB->ipr_next;
  418. SrcOffset = 0;
  419. }
  420. DestBuf += BytesToCopy;
  421. Size -= BytesToCopy;
  422. }
  423. }
  424. //* CopyFlatToNdis - Copy a flat buffer to an NDIS_BUFFER chain.
  425. //
  426. // A utility function to copy a flat buffer to an NDIS buffer chain. We
  427. // assume that the NDIS_BUFFER chain is big enough to hold the copy amount;
  428. // in a debug build we'll debugcheck if this isn't true. We return a pointer
  429. // to the buffer where we stopped copying, and an offset into that buffer.
  430. // This is useful for copying in pieces into the chain.
  431. //
  432. // Input: DestBuf - Destination NDIS_BUFFER chain.
  433. // SrcBuf - Src flat buffer.
  434. // Size - Size in bytes to copy.
  435. // StartOffset - Pointer to start of offset into first buffer in
  436. // chain. Filled in on return with the offset to
  437. // copy into next.
  438. // BytesCopied - Pointer to a variable into which to store the
  439. // number of bytes copied by this operation
  440. //
  441. // Returns: Pointer to next buffer in chain to copy into.
  442. //
  443. PNDIS_BUFFER
  444. CopyFlatToNdis(PNDIS_BUFFER DestBuf, uchar * SrcBuf, uint Size,
  445. uint * StartOffset, uint * BytesCopied)
  446. {
  447. NTSTATUS Status = 0;
  448. *BytesCopied = 0;
  449. Status = TcpipCopyBufferToNdisBuffer(SrcBuf, 0, Size, DestBuf, *StartOffset,
  450. BytesCopied);
  451. *StartOffset += *BytesCopied;
  452. //
  453. // Always return the first buffer, since the TdiCopy function handles
  454. // finding the appropriate buffer based on offset.
  455. //
  456. return (DestBuf);
  457. }
  458. PMDL
  459. CopyFlatToMdl(PMDL DestBuf, uchar *SrcBuf, uint Size,
  460. uint *StartOffset, uint *BytesCopied
  461. )
  462. {
  463. NTSTATUS Status = 0;
  464. *BytesCopied = 0;
  465. Status = TdiCopyBufferToMdl(
  466. SrcBuf,
  467. 0,
  468. Size,
  469. DestBuf,
  470. *StartOffset,
  471. BytesCopied);
  472. *StartOffset += *BytesCopied;
  473. return (DestBuf);
  474. }
  475. //* BuildTDIAddress - Build a TDI address structure.
  476. //
  477. // Called when we need to build a TDI address structure. We fill in
  478. // the specifed buffer with the correct information in the correct
  479. // format.
  480. //
  481. // Input: Buffer - Buffer to be filled in as TDI address structure.
  482. // Addr - IP Address to fill in.
  483. // Port - Port to be filled in.
  484. //
  485. // Returns: Nothing.
  486. //
  487. void
  488. BuildTDIAddress(uchar * Buffer, IPAddr Addr, ushort Port)
  489. {
  490. PTRANSPORT_ADDRESS XportAddr;
  491. PTA_ADDRESS TAAddr;
  492. XportAddr = (PTRANSPORT_ADDRESS) Buffer;
  493. XportAddr->TAAddressCount = 1;
  494. TAAddr = XportAddr->Address;
  495. TAAddr->AddressType = TDI_ADDRESS_TYPE_IP;
  496. TAAddr->AddressLength = sizeof(TDI_ADDRESS_IP);
  497. ((PTDI_ADDRESS_IP) TAAddr->Address)->sin_port = Port;
  498. ((PTDI_ADDRESS_IP) TAAddr->Address)->in_addr = Addr;
  499. memset(((PTDI_ADDRESS_IP) TAAddr->Address)->sin_zero,
  500. 0,
  501. sizeof(((PTDI_ADDRESS_IP) TAAddr->Address)->sin_zero));
  502. }
  503. //* UpdateConnInfo - Update a connection information structure.
  504. //
  505. // Called when we need to update a connection information structure. We
  506. // copy any options, and create a transport address. If any buffer is
  507. // too small we return an error.
  508. //
  509. // Input: ConnInfo - Pointer to TDI_CONNECTION_INFORMATION struc
  510. // to be filled in.
  511. // OptInfo - Pointer to IP options information.
  512. // SrcAddress - Source IP address.
  513. // SrcPort - Source port.
  514. //
  515. // Returns: TDI_SUCCESS if it worked, TDI_BUFFER_OVERFLOW for an error.
  516. //
  517. TDI_STATUS
  518. UpdateConnInfo(PTDI_CONNECTION_INFORMATION ConnInfo, IPOptInfo * OptInfo,
  519. IPAddr SrcAddress, ushort SrcPort)
  520. {
  521. TDI_STATUS Status = TDI_SUCCESS; // Default status to return.
  522. uint AddrLength, OptLength;
  523. if (ConnInfo != NULL) {
  524. ConnInfo->UserDataLength = 0; // No user data.
  525. // Fill in the options. If the provided buffer is too small,
  526. // we'll truncate the options and return an error. Otherwise
  527. // we'll copy the whole IP option buffer.
  528. if (ConnInfo->OptionsLength) {
  529. if (ConnInfo->OptionsLength < OptInfo->ioi_optlength) {
  530. Status = TDI_BUFFER_OVERFLOW;
  531. OptLength = ConnInfo->OptionsLength;
  532. } else
  533. OptLength = OptInfo->ioi_optlength;
  534. RtlCopyMemory(ConnInfo->Options, OptInfo->ioi_options, OptLength);
  535. ConnInfo->OptionsLength = OptLength;
  536. }
  537. // Options are copied. Build a TRANSPORT_ADDRESS structure in
  538. // the buffer.
  539. if (AddrLength = ConnInfo->RemoteAddressLength) {
  540. // Make sure we have at least enough to fill in the count and type.
  541. if (AddrLength >= TCP_TA_SIZE) {
  542. // The address fits. Fill it in.
  543. ConnInfo->RemoteAddressLength = TCP_TA_SIZE;
  544. BuildTDIAddress(ConnInfo->RemoteAddress, SrcAddress, SrcPort);
  545. } else {
  546. ConnInfo->RemoteAddressLength = 0;
  547. Status = TDI_INVALID_PARAMETER;
  548. }
  549. }
  550. }
  551. return Status;
  552. }