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.

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