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.

1449 lines
53 KiB

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. // Copyright (c) 1996, 1997 Microsoft Corporation
  5. //
  6. //
  7. // Module Name:
  8. // recv.c
  9. //
  10. // Abstract:
  11. //
  12. //
  13. // Author:
  14. //
  15. // P Porzuczek
  16. //
  17. // Environment:
  18. //
  19. // Revision History:
  20. //
  21. //
  22. //////////////////////////////////////////////////////////////////////////////
  23. #include <wdm.h>
  24. #include <strmini.h>
  25. #include <ksmedia.h>
  26. #include "slip.h"
  27. #include "main.h"
  28. #include "recv.h"
  29. //////////////////////////////////////////////////////////////////////////////
  30. //
  31. // Init 802.3 header template
  32. //
  33. Header802_3 h802_3Template =
  34. {
  35. {0x01, 0x00, 0x5e, 0, 0, 0}
  36. , {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  37. , {0x08, 0x00}
  38. };
  39. #if DBG
  40. UCHAR MyBuffer [1600] = {0};
  41. UCHAR TestBuffer [] = { 0xC0, 0xC0, 0x00, 0xC7, 0xD3, 0x97, 0x00, 0x00, 0x5E, 0x56,
  42. 0x23, 0x11, 0x07, 0x00, 0x00, 0x00, 0x2C, 0x01, 0x00, 0x00, 0xAA, 0x58, 0x00, 0x00, 0x3D, 0xC5,
  43. 0x00, 0x00, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  44. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  45. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  46. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  47. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  48. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  49. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  50. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  51. 0x22, 0x11, 0x44, 0x33, 0xDC, 0xA2, 0x3B, 0x82, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  52. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0xE9, 0xCE,
  53. 0xFA, 0x7D, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  54. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  55. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  56. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  57. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  58. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  59. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  60. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  61. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  62. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  63. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  64. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  65. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  66. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  67. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  68. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  69. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  70. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  71. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  72. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33,
  73. 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x44, 0x33, 0x22, 0x11, 0x53, 0x60, 0xBB, 0x03, 0xC0 };
  74. //////////////////////////////////////////////////////////////////////////////
  75. VOID
  76. DumpData (
  77. PUCHAR pData,
  78. ULONG ulSize
  79. )
  80. //////////////////////////////////////////////////////////////////////////////
  81. {
  82. ULONG ulCount;
  83. ULONG ul;
  84. UCHAR uc;
  85. DbgPrint("Dump - Data: %x, Size: %x\n", pData, ulSize);
  86. while (ulSize)
  87. {
  88. ulCount = 16 < ulSize ? 16 : ulSize;
  89. for (ul = 0; ul < ulCount; ul++)
  90. {
  91. uc = *pData;
  92. DbgPrint("%02X ", uc);
  93. ulSize -= 1;
  94. pData += 1;
  95. }
  96. DbgPrint("\n");
  97. }
  98. if(TestDebugFlag & TEST_DBG_ASSERT)
  99. {
  100. DEBUG_BREAKPOINT();
  101. }
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////////////
  104. VOID
  105. DumpNabStream (
  106. PNAB_STREAM pNabStream
  107. )
  108. ///////////////////////////////////////////////////////////////////////////////////////
  109. {
  110. TEST_DEBUG (TEST_DBG_NAB, ("pszBuffer......: %08X\n", pNabStream->pszBuffer));
  111. TEST_DEBUG (TEST_DBG_NAB, ("ulcbSize.......: %08X\n", pNabStream->ulcbSize));
  112. TEST_DEBUG (TEST_DBG_NAB, ("ulOffset.......: %08X\n", pNabStream->ulOffset));
  113. TEST_DEBUG (TEST_DBG_NAB, ("ulMpegCrc......: %08X\n", pNabStream->ulMpegCrc));
  114. TEST_DEBUG (TEST_DBG_NAB, ("ulCrcBytesIndex: %08X\n", pNabStream->ulCrcBytesIndex));
  115. TEST_DEBUG (TEST_DBG_NAB, ("ulLastCrcBytes.: %08X\n", pNabStream->ulLastCrcBytes));
  116. TEST_DEBUG (TEST_DBG_NAB, ("ulIPStreamIndex: %08X\n", pNabStream->ulIPStreamIndex));
  117. return;
  118. }
  119. ULONG
  120. Checksum ( char * psz, ULONG ulSize )
  121. //////////////////////////////////////////////////////////////////////////////
  122. {
  123. ULONG Checksum = 0;
  124. ULONG uli = 0;
  125. if(ulSize < 0x41d && ulSize)
  126. {
  127. for ( uli=0; uli <= ulSize-2; uli += 2)
  128. {
  129. Checksum += ((ULONG) (psz[uli]) << 8) + (ULONG) (psz[uli+1]);
  130. }
  131. Checksum = (Checksum >> 16) + (Checksum & 0xffff);
  132. Checksum += (Checksum >> 16);
  133. Checksum = ~Checksum;
  134. }
  135. return Checksum;
  136. }
  137. ULONG ulNumPacketsSent = 0;
  138. ULONG ulIndicateEvery = 10;
  139. ULONG ulIndicated = 0;
  140. #endif //DBG
  141. ///////////////////////////////////////////////////////////////////////////////////////
  142. VOID
  143. ResetNabStream (
  144. PSLIP_FILTER pFilter,
  145. PNAB_STREAM pNabStream,
  146. PHW_STREAM_REQUEST_BLOCK pSrb,
  147. PVOID pBuffer,
  148. ULONG ulBufSize
  149. )
  150. ///////////////////////////////////////////////////////////////////////////////////////
  151. {
  152. if (pNabStream->ulOffset > sizeof (Header802_3))
  153. {
  154. pFilter->Stats.ulTotalSlipFramesIncomplete += 1;
  155. pFilter->Stats.ulTotalSlipBytesDropped += pNabStream->ulOffset - sizeof (Header802_3);
  156. }
  157. //
  158. // Reset the NAB_STREAM structure for this group ID
  159. //
  160. pNabStream->pSrb = pSrb;
  161. pNabStream->pszBuffer = pBuffer;
  162. pNabStream->ulcbSize = ulBufSize;
  163. pNabStream->ulOffset = 0;
  164. pNabStream->ulMpegCrc = 0xFFFFFFFF;
  165. pNabStream->ulCrcBytesIndex = 0l;
  166. pNabStream->ulLastCrcBytes = 0l;
  167. if(pBuffer)
  168. {
  169. // Copy the 802.3 header template into the frame. We will replace
  170. // the destination address and protocol on receive.
  171. //
  172. RtlCopyMemory (pNabStream->pszBuffer, &h802_3Template, sizeof (Header802_3));
  173. //
  174. //$$PFP update buffer offset
  175. //
  176. pNabStream->ulOffset = sizeof (Header802_3);
  177. }
  178. return;
  179. }
  180. ///////////////////////////////////////////////////////////////////////////////////////
  181. VOID
  182. CancelNabStreamSrb (
  183. PSLIP_FILTER pFilter,
  184. PHW_STREAM_REQUEST_BLOCK pSrb
  185. )
  186. ///////////////////////////////////////////////////////////////////////////////////////
  187. {
  188. PLIST_ENTRY pFlink = NULL;
  189. PLIST_ENTRY pQueue = NULL;
  190. BOOLEAN bFound = FALSE;
  191. PNAB_STREAM pNSTemp = NULL;
  192. KIRQL Irql = {0};
  193. pQueue = &pFilter->StreamContxList;
  194. KeAcquireSpinLock(&pFilter->StreamUserSpinLock, &Irql);
  195. if ( !IsListEmpty (pQueue))
  196. {
  197. pFlink = pQueue->Flink;
  198. while ((pFlink != pQueue ) && !bFound)
  199. {
  200. pNSTemp = CONTAINING_RECORD (pFlink, NAB_STREAM, Linkage);
  201. if (pSrb && pSrb == pNSTemp->pSrb)
  202. {
  203. pNSTemp->pSrb->Status = STATUS_CANCELLED;
  204. StreamClassStreamNotification (StreamRequestComplete, pNSTemp->pSrb->StreamObject, pNSTemp->pSrb);
  205. TEST_DEBUG (TEST_DBG_TRACE, ("SLIP: StreamRequestComplete on pSrb: %08X\n", pNSTemp->pSrb));
  206. pNSTemp->pSrb = NULL;
  207. bFound = TRUE;
  208. }
  209. pFlink = pFlink->Flink;
  210. }
  211. if (bFound)
  212. {
  213. vDestroyNabStreamContext (pFilter, pNSTemp, FALSE);
  214. }
  215. }
  216. KeReleaseSpinLock(&pFilter->StreamUserSpinLock, Irql);
  217. }
  218. ///////////////////////////////////////////////////////////////////////////////////////
  219. VOID
  220. DeleteNabStreamQueue (
  221. PSLIP_FILTER pFilter
  222. )
  223. ///////////////////////////////////////////////////////////////////////////////////////
  224. {
  225. PLIST_ENTRY pFlink = NULL;
  226. PLIST_ENTRY pQueue = NULL;
  227. PNAB_STREAM pNSTemp = NULL;
  228. KIRQL Irql={0};
  229. pQueue = &pFilter->StreamContxList;
  230. KeAcquireSpinLock(&pFilter->StreamUserSpinLock, &Irql);
  231. while ( !IsListEmpty (pQueue))
  232. {
  233. pFlink = RemoveHeadList (pQueue);
  234. pNSTemp = CONTAINING_RECORD (pFlink, NAB_STREAM, Linkage);
  235. if(pNSTemp && pNSTemp->pSrb)
  236. {
  237. pNSTemp->pSrb->Status = STATUS_CANCELLED;
  238. StreamClassStreamNotification (StreamRequestComplete, pNSTemp->pSrb->StreamObject, pNSTemp->pSrb);
  239. TEST_DEBUG (TEST_DBG_TRACE, ("SLIP: StreamRequestComplete on pSrb: %08X\n", pNSTemp->pSrb));
  240. pNSTemp->pSrb = NULL;
  241. vDestroyNabStreamContext (pFilter, pNSTemp, FALSE);
  242. }
  243. }
  244. KeReleaseSpinLock(&pFilter->StreamUserSpinLock, Irql);
  245. return;
  246. }
  247. //////////////////////////////////////////////////////////////////////////////
  248. ULONG
  249. CalculateCrc (
  250. PUCHAR pPacket,
  251. ULONG ulSize
  252. )
  253. //////////////////////////////////////////////////////////////////////////////
  254. {
  255. ULONG ul = 0;
  256. ULONG ulLastCrcBytes = 0;
  257. ULONG ulMpegCrc = 0xFFFFFFFF;
  258. PUCHAR ptr = NULL;
  259. PCL pcl = (PCL)&ulLastCrcBytes;
  260. for (ptr = pPacket, ul = 0; ul < ulSize; ul++, ptr++)
  261. {
  262. //if (ul > 3)
  263. //{
  264. //MpegCrcUpdate (&ulMpegCrc, 1, &pcl->c.uc[3]);
  265. MpegCrcUpdate (&ulMpegCrc, 1, ptr);
  266. //}
  267. //pcl->l.ul = (ULONG)(pcl->l.ul) << 8;
  268. //pcl->c.uc[0] = *ptr;
  269. TEST_DEBUG( TEST_DBG_CRC, ("SLIP: char: %02X ul: %d MpegCrc: %08X\n", *ptr, ul, ulMpegCrc));
  270. }
  271. return ulMpegCrc;
  272. }
  273. //////////////////////////////////////////////////////////////////////////////
  274. VOID
  275. vNabtsUpdateCrc (
  276. PNAB_STREAM pNabStream,
  277. UCHAR ucToCopy
  278. )
  279. //////////////////////////////////////////////////////////////////////////////
  280. {
  281. PCL pcl = (PCL)&pNabStream->ulLastCrcBytes;
  282. if (pNabStream->ulCrcBytesIndex++ > 3)
  283. {
  284. MpegCrcUpdate (&pNabStream->ulMpegCrc, 1, &pcl->c.uc[3]);
  285. }
  286. pcl->l.ul = (ULONG)(pcl->l.ul) << 8;
  287. pcl->c.uc[0] = ucToCopy;
  288. #ifdef DUMP_CRC
  289. TEST_DEBUG( TEST_DBG_CRC, ("SLIP: char: %02X ulLastCrcBytes: %08X MpegCrc: %08X ulCrcBytesIndex: %d\n",
  290. ucToCopy, pNabStream->ulLastCrcBytes, pNabStream->ulMpegCrc, pNabStream->ulCrcBytesIndex));
  291. #endif // DUMP_CRC
  292. }
  293. //////////////////////////////////////////////////////////////////////////////
  294. VOID
  295. ComputeIPChecksum (
  296. PHeaderIP pIPHeader
  297. )
  298. //////////////////////////////////////////////////////////////////////////////
  299. {
  300. ULONG Checksum;
  301. PUCHAR NextChar;
  302. pIPHeader->ucChecksumHigh = pIPHeader->ucChecksumLow = 0;
  303. Checksum = 0;
  304. for ( NextChar = (PUCHAR) pIPHeader
  305. ; (NextChar - (PUCHAR) pIPHeader) <= (sizeof(HeaderIP) - 2)
  306. ; NextChar += 2)
  307. {
  308. Checksum += ((ULONG) (NextChar[0]) << 8) + (ULONG) (NextChar[1]);
  309. }
  310. Checksum = (Checksum >> 16) + (Checksum & 0xffff);
  311. Checksum += (Checksum >> 16);
  312. Checksum = ~Checksum;
  313. pIPHeader->ucChecksumHigh = (UCHAR) ((Checksum >> 8) & 0xff);
  314. pIPHeader->ucChecksumLow = (UCHAR) (Checksum & 0xff);
  315. }
  316. //////////////////////////////////////////////////////////////////////////////
  317. //
  318. // Looks in the user's StreamContxList for a matching
  319. // Nabts Group ID.
  320. // It uses it if it finds one - otherwise we allocate one.
  321. //
  322. NTSTATUS
  323. ntFindNabtsStream(
  324. PSLIP_FILTER pFilter,
  325. PNABTSFEC_BUFFER pNabData,
  326. PNAB_STREAM *ppNabStream
  327. )
  328. //////////////////////////////////////////////////////////////////////////////
  329. {
  330. NTSTATUS status = STATUS_SUCCESS;
  331. PLIST_ENTRY pFlink;
  332. PNAB_STREAM pNabStream = NULL;
  333. //
  334. // Check to see if the groupid is within the valid range.
  335. //
  336. if(pNabData->groupID > NABTSIP_GROUP_ID_RANGE_HI )
  337. {
  338. status = STATUS_INVALID_PARAMETER;
  339. ASSERT(status == STATUS_INVALID_PARAMETER);
  340. *ppNabStream = NULL;
  341. return status;
  342. }
  343. //
  344. // Go through the list one stream context at a time.
  345. //
  346. for (pFlink = pFilter->StreamContxList.Flink;
  347. pFlink != &pFilter->StreamContxList;
  348. pFlink = pFlink->Flink)
  349. {
  350. PNAB_STREAM pNSTemp;
  351. pNSTemp = CONTAINING_RECORD (pFlink, NAB_STREAM, Linkage);
  352. if(pNSTemp->groupID == pNabData->groupID)
  353. {
  354. pNabStream = pNSTemp;
  355. //
  356. // Mark the stream as having been used. This flag is checked
  357. // in vCheckNabStreamLife.
  358. //
  359. pNabStream->fUsed = TRUE;
  360. break;
  361. }
  362. }
  363. //
  364. // if we did not find a stream then create one.
  365. //
  366. if (pNabStream == NULL)
  367. {
  368. status = ntCreateNabStreamContext(pFilter, pNabData->groupID, &pNabStream);
  369. if(status == STATUS_SUCCESS)
  370. {
  371. #if DBG
  372. TEST_DEBUG( TEST_DBG_NAB, ("SLIP Creating new NAB_STREAM for data...Group ID: %08X\n", pNabStream->groupID));
  373. #ifdef TEST_DBG_NAB
  374. DumpNabStream (pNabStream);
  375. #endif
  376. #endif //DBG
  377. }
  378. }
  379. else
  380. {
  381. TEST_DEBUG( TEST_DBG_NAB, ("SLIP Using existing NAB_STREAM for data. Group ID: %08X\n", pNabStream->groupID));
  382. #ifdef TEST_DBG_NAB
  383. DumpNabStream (pNabStream);
  384. #endif
  385. }
  386. *ppNabStream = pNabStream;
  387. return status;
  388. }
  389. ///////////////////////////////////////////////////////////////////////////////////////
  390. NTSTATUS
  391. GetOutputSrbForStream (
  392. PSLIP_FILTER pFilter,
  393. PNAB_STREAM pNabStream
  394. )
  395. ///////////////////////////////////////////////////////////////////////////////////////
  396. {
  397. NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;
  398. PKSSTREAM_HEADER pStreamHdr = NULL;
  399. PHW_STREAM_REQUEST_BLOCK pSrbIPv4 = NULL;
  400. if (QueueRemove( &pSrbIPv4, &pFilter->IpV4StreamDataSpinLock, &pFilter->IpV4StreamDataQueue))
  401. {
  402. //
  403. // Save the SRB References.
  404. //
  405. pNabStream->pSrb = pSrbIPv4;
  406. pStreamHdr = pSrbIPv4->CommandData.DataBufferArray;
  407. TEST_DEBUG (TEST_DBG_WRITE_DATA, ("SLIP: OUTPUT SRB...FrameExtent: %d DataUsed: %d Data: %08X\n",
  408. pStreamHdr->FrameExtent,
  409. pStreamHdr->DataUsed,
  410. pStreamHdr->Data
  411. ));
  412. status = STATUS_SUCCESS;
  413. }
  414. return status;
  415. }
  416. //////////////////////////////////////////////////////////////////////////////
  417. void
  418. UpdateMACHeader (
  419. PHeader802_3 pMAC,
  420. PHeaderIP pIP
  421. )
  422. //////////////////////////////////////////////////////////////////////////////
  423. {
  424. ASSERT (pMAC);
  425. ASSERT (pIP);
  426. //
  427. // Now we copy the low order 23 bits of the IP destination addresss to the 802.3 Header
  428. //
  429. if (pMAC && pIP)
  430. {
  431. pMAC->DestAddress [3] = pIP->ipaddrDst.ucHighLSB & 0x7F;
  432. pMAC->DestAddress [4] = pIP->ipaddrDst.ucLowMSB & 0xFF;
  433. pMAC->DestAddress [5] = pIP->ipaddrDst.ucLowLSB & 0xFF;
  434. }
  435. }
  436. //////////////////////////////////////////////////////////////////////////////
  437. void
  438. vRebuildIPPacketHeader (
  439. PNAB_STREAM pNabStream
  440. )
  441. //////////////////////////////////////////////////////////////////////////////
  442. {
  443. PNAB_HEADER_CACHE pPacketHeader;
  444. PNAB_HEADER_CACHE pSavedHeader = &pNabStream->NabHeader[pNabStream->ulIPStreamIndex];
  445. PUCHAR psz;
  446. //
  447. // Copy the uncompressed packet header to the buffer.
  448. //
  449. //The following assert never fails since all of parameters are constants. So RtlCopyMemory is safe
  450. ASSERT(sizeof(Header802_3)+sizeof(HeaderIP) + sizeof(HeaderUDP)<MAX_STREAM_PAYLOAD);
  451. RtlCopyMemory((pNabStream->pszBuffer + sizeof(Header802_3)),
  452. ((PUCHAR)&pNabStream->NabHeader[pNabStream->ulIPStreamIndex]),
  453. sizeof(HeaderIP) + sizeof(HeaderUDP));
  454. //
  455. // Copy the compressed header items into the uncompressed packet header.
  456. //
  457. pPacketHeader = (PNAB_HEADER_CACHE)pNabStream->pszBuffer + sizeof(Header802_3);
  458. //
  459. // Copy IP Packet ID.
  460. //
  461. psz = (PUCHAR)pNabStream->NabCState[pNabStream->ulIPStreamIndex].usrgCompressedHeader;
  462. //NabCState[pNabStream->ulIPStreamIndex].usrgCompressedHeader is USHORT and RtlCopyMemory copies discrete number of bytes
  463. //No vulnerability is seen in the RtlCopyMemory blocks below
  464. RtlCopyMemory(&pPacketHeader->ipHeader.ucIDHigh, psz, IP_ID_SIZE);
  465. //
  466. // Copy UDP Check Sum.
  467. //
  468. psz = (PUCHAR)(pNabStream->NabCState[pNabStream->ulIPStreamIndex].usrgCompressedHeader + 2);
  469. RtlCopyMemory(&pPacketHeader->udpHeader.ucChecksumHigh, psz, UDP_CHKSUM_SIZE);
  470. }
  471. //////////////////////////////////////////////////////////////////////////////
  472. __inline VOID
  473. CopyNabToPacketNew(
  474. UCHAR uchToCopy,
  475. PNAB_STREAM pNabStream,
  476. PSLIP_FILTER pFilter
  477. )
  478. //////////////////////////////////////////////////////////////////////////////
  479. {
  480. if (pNabStream->ulOffset >= pNabStream->ulcbSize)
  481. {
  482. //
  483. // The packet is too big. Resync the SLIP stream.
  484. //
  485. pNabStream->ulFrameState = NABTS_FS_SYNC;
  486. pFilter->Stats.ulTotalSlipFramesTooBig += 1;
  487. }
  488. else
  489. {
  490. ULONG ulIPStream = pNabStream->ulIPStreamIndex;
  491. if(pNabStream->NabCState[ulIPStream].usCompressionState == NABTS_CS_UNCOMPRESSED)
  492. {
  493. //
  494. // Copy the byte to the actual Packet buffer.
  495. //
  496. pNabStream->pszBuffer[pNabStream->ulOffset] = uchToCopy;
  497. //
  498. // Update the MpegCrc check.
  499. //
  500. vNabtsUpdateCrc (pNabStream, uchToCopy);
  501. //
  502. // If we are collecting the IP Header data then copy it to
  503. // a buffer so that we can use it later for uncompression.
  504. //
  505. if(pNabStream->ulOffset < sizeof (Header802_3) + sizeof(HeaderIP) + sizeof(HeaderUDP))
  506. {
  507. PUCHAR psz = (PUCHAR)&pNabStream->NabHeader[pNabStream->ulIPStreamIndex].ipHeader;
  508. *(psz + pNabStream->ulOffset - sizeof (Header802_3)) = uchToCopy;
  509. }
  510. //
  511. // Increment the data pointer.
  512. //
  513. pNabStream->ulOffset++;
  514. }
  515. else if(pNabStream->NabCState[ulIPStream].usCompressionState == NABTS_CS_COMPRESSED)
  516. {
  517. if(pNabStream->NabCState[ulIPStream].uscbHeaderOffset <
  518. pNabStream->NabCState[ulIPStream].uscbRequiredSize)
  519. {
  520. PUCHAR psz = (PUCHAR)pNabStream->NabCState[ulIPStream].usrgCompressedHeader;
  521. *(psz + pNabStream->NabCState[ulIPStream].uscbHeaderOffset++) = uchToCopy;
  522. //
  523. // Update the MpegCrc check.
  524. //
  525. vNabtsUpdateCrc (pNabStream, uchToCopy);
  526. if(pNabStream->NabCState[ulIPStream].uscbHeaderOffset ==
  527. pNabStream->NabCState[ulIPStream].uscbRequiredSize)
  528. {
  529. ASSERT(pNabStream->ulOffset == sizeof(Header802_3));
  530. //
  531. // Use the saved IP Packet header and the compressed IP Header
  532. // to rebuil the IP Header to send up.
  533. //
  534. vRebuildIPPacketHeader( pNabStream );
  535. //
  536. // Set the buffer offset past the end of the IP/UDP headers
  537. // We should start coping data now.
  538. //
  539. pNabStream->ulOffset += (sizeof(HeaderIP) + sizeof(HeaderUDP));
  540. }
  541. }
  542. else
  543. {
  544. //
  545. // We already have the header rebuilt. Now copy the payload.
  546. //
  547. pNabStream->pszBuffer[pNabStream->ulOffset++] = uchToCopy;
  548. //
  549. // Update the MpegCrc check.
  550. //
  551. vNabtsUpdateCrc (pNabStream, uchToCopy);
  552. }
  553. }
  554. else
  555. {
  556. DbgBreakPoint();
  557. }
  558. }
  559. }
  560. //////////////////////////////////////////////////////////////////////////////
  561. __inline VOID
  562. CopyNabToPacketOld(
  563. UCHAR uchToCopy,
  564. PNAB_STREAM pNabStream,
  565. PSLIP_FILTER pFilter
  566. )
  567. //////////////////////////////////////////////////////////////////////////////
  568. {
  569. if (pNabStream->ulOffset >= pNabStream->ulcbSize)
  570. {
  571. //
  572. // The packet is too big. Resync the SLIP stream.
  573. //
  574. pNabStream->ulFrameState = NABTS_FS_SYNC;
  575. pFilter->Stats.ulTotalSlipFramesTooBig += 1;
  576. }
  577. else
  578. {
  579. // Copy the byte.
  580. //
  581. pNabStream->pszBuffer[pNabStream->ulOffset++] = uchToCopy;
  582. }
  583. }
  584. //////////////////////////////////////////////////////////////////////////////
  585. __inline VOID
  586. CopyNabToPacket(
  587. UCHAR uchToCopy,
  588. PNAB_STREAM pNabStream,
  589. PSLIP_FILTER pFilter
  590. )
  591. //////////////////////////////////////////////////////////////////////////////
  592. {
  593. if(pNabStream->ulProtoID == PROTO_ID)
  594. {
  595. CopyNabToPacketNew(uchToCopy, pNabStream, pFilter);
  596. }
  597. else
  598. {
  599. CopyNabToPacketOld(uchToCopy, pNabStream, pFilter);
  600. }
  601. }
  602. //////////////////////////////////////////////////////////////////////////////
  603. NTSTATUS
  604. ntNabtsRecv(
  605. PSLIP_FILTER pFilter,
  606. PNABTSFEC_BUFFER pNabData
  607. )
  608. //////////////////////////////////////////////////////////////////////////////
  609. {
  610. NTSTATUS status = STATUS_SUCCESS;
  611. PNAB_STREAM pNabStream = NULL;
  612. PUCHAR pszNabIn = NULL;
  613. PKSSTREAM_HEADER pStreamHdr = NULL;
  614. LARGE_INTEGER liCurrentTime = {0};
  615. LARGE_INTEGER liTimeToLive = {0};
  616. LARGE_INTEGER liStatusInterval = {0};
  617. KIRQL Irql = {0};
  618. ULONG ulIPStream = 0;
  619. ULONG ulNabIn = 0;
  620. TEST_DEBUG( TEST_DBG_RECV, ("\nEntering - ntNabtsRecv\n"));
  621. //
  622. // Get the current system time.
  623. //
  624. KeQuerySystemTime(&liCurrentTime);
  625. //
  626. // See if it is time the check for dead GroupIDs and Streams.
  627. // Get a lock so no one else is modifying the Stream list while we are looking.
  628. //
  629. KeAcquireSpinLock(&pFilter->StreamUserSpinLock, &Irql);
  630. liTimeToLive.QuadPart = NAB_STREAM_LIFE;
  631. if( (LONGLONG)(liCurrentTime.QuadPart - pFilter->liLastTimeChecked.QuadPart) > liTimeToLive.QuadPart)
  632. {
  633. vCheckNabStreamLife( pFilter );
  634. pFilter->liLastTimeChecked = liCurrentTime;
  635. }
  636. //
  637. // Find the Stream Context.
  638. //
  639. status = ntFindNabtsStream( pFilter, pNabData, &pNabStream );
  640. if(status != STATUS_SUCCESS)
  641. {
  642. ASSERT( status == STATUS_SUCCESS);
  643. pFilter->Stats.ulTotalSlipBuffersDropped += 1;
  644. KeReleaseSpinLock(&pFilter->StreamUserSpinLock, Irql);
  645. goto ret;
  646. }
  647. KeReleaseSpinLock(&pFilter->StreamUserSpinLock, Irql);
  648. liStatusInterval.QuadPart = NAB_STATUS_INTERVAL;
  649. if( (LONGLONG)(liCurrentTime.QuadPart - pFilter->liLastTimeStatsDumped.QuadPart) > liStatusInterval.QuadPart)
  650. {
  651. pFilter->liLastTimeStatsDumped = liCurrentTime;
  652. TEST_DEBUG (TEST_DBG_INFO, (" "));
  653. TEST_DEBUG (TEST_DBG_INFO, ("SLIP: ulTotalDataSRBWrites: %d\n.", pFilter->Stats.ulTotalDataSRBWrites));
  654. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalBadPinSRBWrites: %d\n.", pFilter->Stats.ulTotalBadPinSRBWrites));
  655. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalDataSRBReads: %d\n.", pFilter->Stats.ulTotalDataSRBReads));
  656. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalBadPinSRBReads: %d\n.", pFilter->Stats.ulTotalBadPinSRBReads));
  657. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipBuffersReceived: %d\n.", pFilter->Stats.ulTotalSlipBuffersReceived));
  658. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipBuffersDropped: %d\n.", pFilter->Stats.ulTotalSlipBuffersDropped));
  659. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipZeroLengthBuffers: %d\n.", pFilter->Stats.ulTotalSlipZeroLengthBuffers));
  660. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipBytesReceived: %d\n.", pFilter->Stats.ulTotalSlipBytesReceived));
  661. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipBytesDropped: %d\n.", pFilter->Stats.ulTotalSlipBytesDropped));
  662. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipFramesReceived: %d\n.", pFilter->Stats.ulTotalSlipFramesReceived));
  663. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipOldProtoFramesStarted: %d\n.", pFilter->Stats.ulTotalSlipOldProtoFramesStarted));
  664. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipNewProtoFramesStarted: %d\n.", pFilter->Stats.ulTotalSlipNewProtoFramesStarted));
  665. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipFramesIncomplete: %d\n.", pFilter->Stats.ulTotalSlipFramesIncomplete));
  666. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipFramesBadCRC: %d\n.", pFilter->Stats.ulTotalSlipFramesBadCRC));
  667. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipFramesTooBig: %d\n.", pFilter->Stats.ulTotalSlipFramesTooBig));
  668. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalSlipFramesTooSmall: %d\n.", pFilter->Stats.ulTotalSlipFramesTooSmall));
  669. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPPacketsFound: %d\n.", pFilter->Stats.ulTotalIPPacketsFound));
  670. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPBytesFound: %d\n.", pFilter->Stats.ulTotalIPBytesFound));
  671. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPPacketsSent: %d\n.", pFilter->Stats.ulTotalIPPacketsSent));
  672. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPBytesSent: %d\n.", pFilter->Stats.ulTotalIPBytesSent));
  673. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPPacketsTooBig: %d\n.", pFilter->Stats.ulTotalIPPacketsTooBig));
  674. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPPacketsTooSmall: %d\n.", pFilter->Stats.ulTotalIPPacketsTooSmall));
  675. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPPacketsDropped: %d\n.", pFilter->Stats.ulTotalIPPacketsDropped));
  676. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalIPBytesDropped: %d\n.", pFilter->Stats.ulTotalIPBytesDropped));
  677. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalNabStreamsCreated: %d\n.", pFilter->Stats.ulTotalNabStreamsCreated));
  678. TEST_DEBUG (TEST_DBG_INFO, (" ulTotalNabStreamsTimedOut: %d\n.", pFilter->Stats.ulTotalNabStreamsTimedOut));
  679. TEST_DEBUG (TEST_DBG_INFO, (" "));
  680. }
  681. //
  682. // Set the last time used for this stream.
  683. //
  684. pNabStream->liLastTimeUsed = liCurrentTime;
  685. //
  686. // Get a pointer to the input buffer. We copy data from this pointer to
  687. // the output buffer
  688. //
  689. pszNabIn = (LPSTR) pNabData->data;
  690. // Validate the data size and that the start+end of the buffer are accessible
  691. ASSERT(pNabData->dataSize <= sizeof(pNabData->data) );
  692. // What is really needed here is something like "MmIsValidAddress()", but for WDM drivers
  693. // These assert just look at the start & end addresses of the buffer w/o regard to values.
  694. // ASSERT( (*(pszNabIn) + 1 > 0) );
  695. // ASSERT( (*(pszNabIn+pNabData->dataSize-1) + 1 > 0) );
  696. for (ulNabIn = pNabData->dataSize; ulNabIn; ulNabIn--, pszNabIn++)
  697. {
  698. switch (pNabStream->ulFrameState)
  699. {
  700. case NABTS_FS_SYNC:
  701. switch (*pszNabIn)
  702. {
  703. case FRAME_END:
  704. //
  705. // We found the start of frame sync. Look for the
  706. // protocol character.
  707. //
  708. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Found Possible Start of Frame... pszNabIn %08X ulNabIn: %08X\n", pszNabIn, ulNabIn));
  709. pNabStream->ulFrameState = NABTS_FS_SYNC_PROTO;
  710. ResetNabStream (pFilter, pNabStream, NULL, NULL, 0);
  711. break;
  712. }
  713. break;
  714. case NABTS_FS_SYNC_PROTO:
  715. switch (*pszNabIn)
  716. {
  717. case PROTO_ID:
  718. case PROTO_ID_OLD:
  719. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Found Start of Protocol...Building Packet.... pszNabIn %08X ulNabIn: %08X\n", pszNabIn, ulNabIn));
  720. // Record the stream type.
  721. //
  722. pNabStream->ulProtoID = *pszNabIn;
  723. // This is our protocol. Setup NAB_STREAM with an output
  724. // data buffer from the output SRB Queue
  725. //
  726. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Setting Up Output buffer\n"));
  727. ResetNabStream( pFilter, pNabStream, NULL, pNabStream->rgBuf, sizeof(pNabStream->rgBuf));
  728. // Copy the 802.3 header template into the frame. We will replace
  729. // the destination address and protocol on receive.
  730. //
  731. RtlCopyMemory (pNabStream->pszBuffer, &h802_3Template, sizeof (Header802_3));
  732. // Update buffer offset
  733. //
  734. pNabStream->ulOffset = sizeof (Header802_3);
  735. if(pNabStream->ulProtoID == PROTO_ID)
  736. {
  737. //
  738. // Set the state to check the IP compression.
  739. //
  740. pFilter->Stats.ulTotalSlipNewProtoFramesStarted += 1;
  741. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Protocol ID is Compressed\n"));
  742. pNabStream->ulFrameState = NABTS_FS_COMPRESSION;
  743. }
  744. else
  745. {
  746. //
  747. // Start collecting data.
  748. //
  749. pFilter->Stats.ulTotalSlipOldProtoFramesStarted += 1;
  750. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Protocol ID is not Compressed\n"));
  751. pNabStream->ulFrameState = NABTS_FS_COLLECT;
  752. }
  753. // Update the MpegCrc check.
  754. //
  755. vNabtsUpdateCrc( pNabStream, *pszNabIn);
  756. break;
  757. case FRAME_END:
  758. TEST_DEBUG( TEST_DBG_RECV, ("SLIP State is FRAME_END....going to FS_SYNC_PROTO\n"));
  759. pNabStream->ulFrameState = NABTS_FS_SYNC_PROTO;
  760. break;
  761. default:
  762. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Protocol Not Found...ReSyncing... pszNabIn %08X ulNabIn: %08X\n", pszNabIn, ulNabIn));
  763. pNabStream->ulFrameState = NABTS_FS_SYNC;
  764. break;
  765. }
  766. break;
  767. case NABTS_FS_COMPRESSION:
  768. {
  769. TEST_DEBUG( TEST_DBG_RECV, ("SLIP State is NABTS_FS_COMPRESSION\n"));
  770. //
  771. // Get the index to the IP Compression Stream.
  772. //
  773. ulIPStream = IP_STREAM_INDEX(*pszNabIn);
  774. //
  775. // Check to see if this IP Packet has a compressed header.
  776. //
  777. if(!PACKET_COMPRESSED(*pszNabIn))
  778. {
  779. pNabStream->NabCState[ulIPStream].usCompressionState = NABTS_CS_UNCOMPRESSED;
  780. }
  781. else if (PACKET_COMPRESSED (*pszNabIn))
  782. {
  783. pNabStream->NabCState[ulIPStream].usCompressionState = NABTS_CS_COMPRESSED;
  784. pNabStream->NabCState[ulIPStream].uscbRequiredSize = NORMAL_COMPRESSED_HEADER;
  785. }
  786. //
  787. // Retain the IP Stream Index.
  788. //
  789. pNabStream->ulIPStreamIndex = ulIPStream;
  790. //
  791. // Set the stats Last Used Time for this stream.
  792. //
  793. pNabStream->NabCState[pNabStream->ulIPStreamIndex].liLastUsed = liCurrentTime;
  794. //
  795. // Set the IP Header Data Length to zero.
  796. //
  797. pNabStream->NabCState[pNabStream->ulIPStreamIndex].uscbHeaderOffset = 0;
  798. //
  799. // Start collecting data.
  800. //
  801. pNabStream->ulFrameState = NABTS_FS_COLLECT;
  802. //
  803. // Update the MpegCrc check.
  804. //
  805. vNabtsUpdateCrc (pNabStream, *pszNabIn);
  806. break;
  807. }
  808. case NABTS_FS_COLLECT:
  809. switch (*pszNabIn)
  810. {
  811. case FRAME_ESCAPE:
  812. //
  813. // We want to escape the next character.
  814. //
  815. TEST_DEBUG( TEST_DBG_RECV, ("SLIP NABTS_FS_COLLECT_ESCAPE\n"));
  816. pNabStream->ulFrameState = NABTS_FS_COLLECT_ESCAPE;
  817. break;
  818. case FRAME_END:
  819. if (pNabStream->ulOffset >= sizeof(HeaderIP))
  820. {
  821. PHeaderIP pHeaderIp = (PHeaderIP)(PUCHAR)(pNabStream->pszBuffer + sizeof(Header802_3));
  822. PUSHORT pusIpLen = (PUSHORT)&pHeaderIp->ucTotalLenHigh;
  823. TEST_DEBUG( TEST_DBG_RECV, ("SLIP End of Packet Found... pszNabIn %08X ulNabIn: %08X ulOffset: %d\n", pszNabIn, ulNabIn, pNabStream->ulOffset));
  824. TEST_DEBUG( TEST_DBG_RECV, ("SLIP ulProtoID %d ulMpegCrc: %08X ulLastCrcBytes: %08X IpLen: %d\n",
  825. pNabStream->ulProtoID, pNabStream->ulMpegCrc, pNabStream->ulLastCrcBytes, *pusIpLen));
  826. // If header compression is being used, we must
  827. // calculate the IP and UDP lengths and regenerate
  828. // the packet checksums.
  829. //
  830. if (pNabStream->ulProtoID == PROTO_ID)
  831. {
  832. PHeaderUDP pHeaderUDP = (PHeaderUDP)(PUCHAR)(pNabStream->pszBuffer + sizeof(Header802_3) + sizeof(HeaderIP));
  833. PUSHORT pusUdpLen = (PUSHORT)&pHeaderUDP->ucMsgLenHigh;
  834. TEST_DEBUG( TEST_DBG_CRC, ("SLIP: GroupID: %d Stream CRC: %08X Calculated CRC: %08X", pNabStream->groupID, pNabStream->ulLastCrcBytes, pNabStream->ulMpegCrc));
  835. // All PROTO_ID packets have an MpegCrc on the end. It is not
  836. // part of the IP packet and needs to be stripped off.
  837. //
  838. pNabStream->ulOffset -= 4;
  839. if (pNabStream->NabCState[pNabStream->ulIPStreamIndex].usCompressionState == NABTS_CS_COMPRESSED)
  840. {
  841. // We use the ulOffset less the MAC Header and IP Header
  842. // sizes for the UDP Packet length.
  843. //
  844. // Note! Fragmented UDP datagrams cannot be compressed
  845. //
  846. *pusUdpLen = htons ((USHORT)(pNabStream->ulOffset - sizeof(Header802_3) - sizeof(HeaderIP)));
  847. // We use the ulOffset less the MAC Header size for the
  848. // IP Packet length.
  849. //
  850. *pusIpLen = htons ((USHORT)(pNabStream->ulOffset - sizeof(Header802_3)));
  851. // Recalculate the IP header Checksum
  852. //
  853. ComputeIPChecksum (pHeaderIp);
  854. }
  855. // If the CRC was bad then invalidate
  856. // the IP Checksum
  857. //
  858. if (pNabStream->ulMpegCrc != pNabStream->ulLastCrcBytes)
  859. {
  860. TEST_DEBUG (TEST_DBG_CRC, (" FAILED*****\n"));
  861. pFilter->Stats.ulTotalSlipFramesBadCRC += 1;
  862. pHeaderIp->ucChecksumHigh = ~(pHeaderIp->ucChecksumHigh);
  863. pHeaderIp->ucChecksumLow = 0xff;
  864. }
  865. else
  866. {
  867. TEST_DEBUG (TEST_DBG_CRC, (" PASSED\n"));
  868. }
  869. }
  870. else if (pNabStream->ulProtoID != PROTO_ID_OLD)
  871. {
  872. TEST_DEBUG( TEST_DBG_RECV, ("SLIP End of Packet Found....Bad PROTO_ID... pszNabIn %08X ulNabIn: %08X ulOffset: %d\n", pszNabIn, ulNabIn, pNabStream->ulOffset));
  873. ASSERT( (pNabStream->ulProtoID == PROTO_ID_OLD)
  874. || (pNabStream->ulProtoID == PROTO_ID)
  875. );
  876. ResetNabStream (pFilter, pNabStream, NULL, NULL, 0);
  877. pNabStream->ulFrameState = NABTS_FS_SYNC;
  878. goto ret;
  879. }
  880. if (NabtsNtoHs(*pusIpLen) <= NABTSIP_MAX_LOOKAHEAD)
  881. {
  882. // Update the MAC address
  883. //
  884. UpdateMACHeader(
  885. (PHeader802_3)(pNabStream->pszBuffer),
  886. pHeaderIp
  887. );
  888. // Get an SRB for outputting the data.
  889. //
  890. status = GetOutputSrbForStream(pFilter,
  891. pNabStream
  892. );
  893. if(status != STATUS_SUCCESS)
  894. {
  895. ASSERT(status == STATUS_SUCCESS);
  896. pFilter->Stats.ulTotalIPPacketsDropped += 1;
  897. ResetNabStream( pFilter, pNabStream, NULL, NULL, 0);
  898. pNabStream->ulFrameState = NABTS_FS_SYNC;
  899. goto ret;
  900. }
  901. ASSERT(pNabStream->pSrb);
  902. if (!pNabStream->pSrb)
  903. {
  904. pFilter->Stats.ulTotalIPPacketsDropped += 1;
  905. ResetNabStream (pFilter, pNabStream, NULL, NULL, 0);
  906. pNabStream->ulFrameState = NABTS_FS_SYNC;
  907. goto ret;
  908. }
  909. // Get the StreamHdr.
  910. //
  911. pStreamHdr = (PKSSTREAM_HEADER) pNabStream->pSrb->CommandData.DataBufferArray;
  912. ASSERT( pStreamHdr);
  913. if (!pStreamHdr)
  914. {
  915. pFilter->Stats.ulTotalIPPacketsDropped += 1;
  916. ResetNabStream (pFilter, pNabStream, NULL, NULL, 0);
  917. pNabStream->ulFrameState = NABTS_FS_SYNC;
  918. goto ret;
  919. }
  920. // If we had a discontinuity, we flag it.
  921. //
  922. if (pFilter->bDiscontinuity)
  923. {
  924. pStreamHdr->OptionsFlags |= KSSTREAM_HEADER_OPTIONSF_DATADISCONTINUITY;
  925. pFilter->bDiscontinuity = FALSE;
  926. }
  927. // Copy the data from the rgBuf to the Srb.
  928. //
  929. RtlCopyMemory (pStreamHdr->Data,
  930. pNabStream->pszBuffer,
  931. pNabStream->ulOffset
  932. );
  933. // Update the datasize field of the Output SRB
  934. //
  935. pStreamHdr->DataUsed = pNabStream->ulOffset;
  936. // Complete the output SRB
  937. //
  938. pFilter->Stats.ulTotalIPPacketsSent += 1;
  939. pNabStream->ulOffset = 0;
  940. StreamClassStreamNotification(
  941. StreamRequestComplete,
  942. pNabStream->pSrb->StreamObject,
  943. pNabStream->pSrb
  944. );
  945. TEST_DEBUG (TEST_DBG_SRB, ("SLIP: Completed SRB....Ptr %08X Size %d\n", pStreamHdr->Data, pStreamHdr->DataUsed));
  946. TEST_DEBUG (TEST_DBG_TRACE, ("SLIP: StreamRequestComplete on pSrb: %08X\n", pNabStream->pSrb));
  947. }
  948. else
  949. {
  950. // The packet is too big. Resync the SLIP stream.
  951. //
  952. pFilter->Stats.ulTotalIPPacketsDropped += 1;
  953. pFilter->Stats.ulTotalIPPacketsTooBig += 1;
  954. TEST_DEBUG( TEST_DBG_RECV, ("SLIP End of Packet Found....Packet Too BIG... pszNabIn %08X ulNabIn: %08X ulOffset: %d\n", pszNabIn, ulNabIn, pNabStream->ulOffset));
  955. }
  956. }
  957. else
  958. {
  959. // The packet is too small. Resync the SLIP stream.
  960. //
  961. pFilter->Stats.ulTotalIPPacketsDropped += 1;
  962. pFilter->Stats.ulTotalIPPacketsTooSmall += 1;
  963. TEST_DEBUG( TEST_DBG_RECV, ("SLIP End of Packet Found....Packet Too SMALL... pszNabIn %08X ulNabIn: %08X ulOffset: %d\n", pszNabIn, ulNabIn, pNabStream->ulOffset));
  964. }
  965. // Reset state for new packet.
  966. //
  967. ResetNabStream (pFilter, pNabStream, NULL, NULL, 0);
  968. pNabStream->ulFrameState = NABTS_FS_SYNC;
  969. break;
  970. default:
  971. // Just copy the byte to the NDIS packet.
  972. //
  973. CopyNabToPacket( *pszNabIn, pNabStream, pFilter);
  974. break;
  975. }
  976. break;
  977. case NABTS_FS_COLLECT_ESCAPE:
  978. pNabStream->ulFrameState = NABTS_FS_COLLECT;
  979. switch (*pszNabIn)
  980. {
  981. case TRANS_FRAME_ESCAPE:
  982. //
  983. // Special way to insert a FRAME_ESCAPE
  984. // character as part of the data.
  985. //
  986. TEST_DEBUG( TEST_DBG_RECV, ("SLIP NABTS_FS_COLLECT_ESCAPE....TRANS_FRAME_ESCAPE\n"));
  987. CopyNabToPacket( (UCHAR) FRAME_ESCAPE, pNabStream, pFilter);
  988. break;
  989. case TRANS_FRAME_END:
  990. //
  991. // Special way to insert a FRAME_END
  992. // character as part of the data.
  993. //
  994. TEST_DEBUG( TEST_DBG_RECV, ("SLIP NABTS_FS_COLLECT_ESCAPE.....TRANS_FRAME_END\n"));
  995. CopyNabToPacket( (UCHAR) FRAME_END, pNabStream, pFilter);
  996. break;
  997. default:
  998. // Any other character that follows FRAME_ESCAPE
  999. // is just inserted into the packet.
  1000. //
  1001. CopyNabToPacket( *pszNabIn, pNabStream, pFilter);
  1002. break;
  1003. }
  1004. break;
  1005. default:
  1006. //
  1007. // We should never be in an unknown state.
  1008. //
  1009. TEST_DEBUG( TEST_DBG_RECV, ("SLIP UNKNOWN STATE.....ReSyncing\n"));
  1010. ASSERT( pNabStream->ulFrameState);
  1011. ResetNabStream (pFilter, pNabStream, NULL, NULL, 0);
  1012. pNabStream->ulFrameState = NABTS_FS_SYNC;
  1013. break;
  1014. }
  1015. }
  1016. ret:
  1017. TEST_DEBUG( TEST_DBG_RECV, ("SLIP Completed ntNabtsRecv\n"));
  1018. return status;
  1019. }
  1020. //////////////////////////////////////////////////////////////////////////////
  1021. //
  1022. // Create a NAB Stream Context.
  1023. //
  1024. NTSTATUS
  1025. ntCreateNabStreamContext(
  1026. PSLIP_FILTER pFilter,
  1027. ULONG groupID,
  1028. PNAB_STREAM *ppNabStream
  1029. )
  1030. //////////////////////////////////////////////////////////////////////////////
  1031. {
  1032. NTSTATUS status = STATUS_SUCCESS;
  1033. PNAB_STREAM pNabStream = NULL;
  1034. TEST_DEBUG (TEST_DBG_NAB, ("******************************** Creating NAB STREAM for group ID %d\n", groupID));
  1035. //
  1036. // Initialize output paramter
  1037. //
  1038. *ppNabStream = NULL;
  1039. //
  1040. // Allocate a Nab Stream structure.
  1041. //
  1042. status = ntAllocateNabStreamContext (&pNabStream);
  1043. if(status == STATUS_SUCCESS)
  1044. {
  1045. pNabStream->ulType = (ULONG)NAB_STREAM_SIGNATURE;
  1046. pNabStream->ulSize = sizeof (NAB_STREAM);
  1047. pNabStream->ulFrameState = NABTS_FS_SYNC;
  1048. pNabStream->ulMpegCrc = 0xFFFFFFFF;
  1049. pNabStream->fUsed = TRUE;
  1050. pNabStream->groupID = groupID;
  1051. //
  1052. // Add the new Stream Context to the User's Stream Context List.
  1053. //
  1054. InsertTailList (&pFilter->StreamContxList, &pNabStream->Linkage);
  1055. *ppNabStream = pNabStream;
  1056. }
  1057. return status;
  1058. }
  1059. //////////////////////////////////////////////////////////////////////////////
  1060. NTSTATUS
  1061. ntAllocateNabStreamContext(
  1062. PNAB_STREAM *ppNabStream
  1063. )
  1064. //////////////////////////////////////////////////////////////////////////////
  1065. {
  1066. NTSTATUS status = STATUS_SUCCESS;
  1067. PNAB_STREAM pNabStream = NULL;
  1068. pNabStream = ExAllocatePool (NonPagedPool, sizeof(NAB_STREAM));
  1069. if (pNabStream == NULL)
  1070. {
  1071. *ppNabStream = NULL;
  1072. return (STATUS_NO_MEMORY);
  1073. }
  1074. RtlZeroMemory (pNabStream, sizeof(NAB_STREAM));
  1075. *ppNabStream = pNabStream;
  1076. return status;
  1077. }
  1078. //////////////////////////////////////////////////////////////////////////////
  1079. VOID
  1080. vDestroyNabStreamContext(
  1081. PSLIP_FILTER pFilter,
  1082. PNAB_STREAM pNabStream,
  1083. BOOLEAN fUseSpinLock
  1084. )
  1085. //////////////////////////////////////////////////////////////////////////////
  1086. {
  1087. KIRQL Irql;
  1088. if(fUseSpinLock)
  1089. {
  1090. //
  1091. // Lock the user.
  1092. //
  1093. KeAcquireSpinLock( &pFilter->StreamUserSpinLock, &Irql);
  1094. }
  1095. //
  1096. // Remove the stream context from the user's list.
  1097. //
  1098. RemoveEntryList (&pNabStream->Linkage);
  1099. if(fUseSpinLock)
  1100. {
  1101. //
  1102. // UnLock the user.
  1103. //
  1104. KeReleaseSpinLock( &pFilter->StreamUserSpinLock, Irql);
  1105. }
  1106. //
  1107. // Free the stream's SRB, if any.
  1108. //
  1109. if (pNabStream->pSrb)
  1110. {
  1111. StreamClassStreamNotification (StreamRequestComplete, pNabStream->pSrb->StreamObject, pNabStream->pSrb);
  1112. TEST_DEBUG (TEST_DBG_TRACE, ("SLIP: StreamRequestComplete on pSrb: %08X\n", pNabStream->pSrb));
  1113. pNabStream->pSrb = NULL;
  1114. }
  1115. //
  1116. // Free the stream context memory.
  1117. //
  1118. TEST_DEBUG (TEST_DBG_NAB, ("Deleting NAB STREAM for group ID: %d... ", pNabStream->groupID));
  1119. ExFreePool (pNabStream);
  1120. }
  1121. //////////////////////////////////////////////////////////////////////////////
  1122. VOID
  1123. vCheckNabStreamLife (
  1124. PSLIP_FILTER pFilter
  1125. )
  1126. //////////////////////////////////////////////////////////////////////////////
  1127. {
  1128. PNAB_STREAM pNabStream;
  1129. PLIST_ENTRY pFlink;
  1130. TEST_DEBUG( TEST_DBG_RECV, ("Entering - vCheckNabStreamLife - pFilter: %x\n", pFilter));
  1131. //
  1132. // Go through the StreamContextList. Remove any stream context structures that have
  1133. // expired their life span.
  1134. //
  1135. for (pFlink = pFilter->StreamContxList.Flink;
  1136. pFlink != &pFilter->StreamContxList;
  1137. pFlink = pFlink->Flink)
  1138. {
  1139. pNabStream = CONTAINING_RECORD (pFlink, NAB_STREAM, Linkage);
  1140. TEST_DEBUG (TEST_DBG_NAB, ("Checking NAB STREAM life for group ID %d ... ", pNabStream->groupID));
  1141. if (pNabStream->fUsed)
  1142. {
  1143. TEST_DEBUG (TEST_DBG_NAB, (" USED\n"));
  1144. }
  1145. else
  1146. {
  1147. TEST_DEBUG (TEST_DBG_NAB, (" NOT USED\n"));
  1148. }
  1149. if(!pNabStream->fUsed)
  1150. {
  1151. // Point at the previous Stream;
  1152. pFlink = pFlink->Blink;
  1153. //
  1154. // Remove the stream from the User's stream context list.
  1155. //
  1156. //
  1157. // vDestroyNabStreamContext returns the active NDIS Packet (if any) to
  1158. // the adapter's free list, remove the stream context from the user list
  1159. // (if specified) and free the stream context structure memory.
  1160. //
  1161. vDestroyNabStreamContext( pFilter, pNabStream, FALSE);
  1162. pFilter->Stats.ulTotalNabStreamsTimedOut += 1;
  1163. }
  1164. else
  1165. {
  1166. //
  1167. // This flag must be set back to TRUE before the next DPC fires or
  1168. // this stream will be removed.
  1169. //
  1170. pNabStream->fUsed = FALSE;
  1171. }
  1172. }
  1173. TEST_DEBUG ( TEST_DBG_RECV, ("Leaving - vCheckNabStreamLife\n"));
  1174. }