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.

623 lines
16 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. // Copyright (c) 1996, 1997 Microsoft Corporation
  5. //
  6. //
  7. // Module Name:
  8. // test.c
  9. //
  10. // Abstract:
  11. //
  12. // This file is a test to find out if dual binding to NDIS and KS works
  13. //
  14. // Author:
  15. //
  16. // P Porzuczek
  17. //
  18. // Environment:
  19. //
  20. // Revision History:
  21. //
  22. //
  23. //////////////////////////////////////////////////////////////////////////////
  24. #include <forward.h>
  25. #include <memory.h>
  26. #include <ndis.h>
  27. #include <link.h>
  28. #include <ipsink.h>
  29. #include "device.h"
  30. #include "NdisApi.h"
  31. #include "frame.h"
  32. #include "mem.h"
  33. #include "adapter.h"
  34. #include "main.h"
  35. //////////////////////////////////////////////////////////
  36. //
  37. //
  38. //
  39. PADAPTER global_pAdapter;
  40. UCHAR achGlobalVendorDescription [] = "Microsoft TV/Video Connection";
  41. ULONG ulGlobalInstance = 1;
  42. //////////////////////////////////////////////////////////
  43. //
  44. //
  45. const ADAPTER_VTABLE AdapterVTable =
  46. {
  47. Adapter_QueryInterface,
  48. Adapter_AddRef,
  49. Adapter_Release,
  50. Adapter_IndicateData,
  51. Adapter_IndicateReset,
  52. Adapter_GetDescription,
  53. Adapter_CloseLink
  54. };
  55. //////////////////////////////////////////////////////////
  56. //
  57. //
  58. #pragma pack (push, 1)
  59. typedef ULONG CHECKSUM;
  60. typedef struct _MAC_ADDRESS_
  61. {
  62. UCHAR Address [6];
  63. } MAC_ADDRESS, *PMAC_ADDRESS;
  64. typedef struct _HEADER_802_3
  65. {
  66. MAC_ADDRESS DestAddress;
  67. MAC_ADDRESS SourceAddress;
  68. UCHAR Type[2];
  69. } HEADER_802_3, *PHEADER_802_3;
  70. typedef struct _HEADER_IP_
  71. {
  72. UCHAR ucVersion_Length;
  73. UCHAR ucTOS;
  74. USHORT usLength;
  75. USHORT usId;
  76. USHORT usFlags_Offset;
  77. UCHAR ucTTL;
  78. UCHAR ucProtocol;
  79. USHORT usHdrChecksum;
  80. UCHAR ucSrcAddress [4];
  81. UCHAR ucDestAddress [4];
  82. } HEADER_IP, *PHEADER_IP;
  83. #pragma pack (pop)
  84. //////////////////////////////////////////////////////////
  85. //
  86. //
  87. const HEADER_802_3 h802_3Template =
  88. {
  89. {0x01, 0x00, 0x5e, 0, 0, 0}
  90. , {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  91. , {0x08, 0x00}
  92. };
  93. #if DBG
  94. //////////////////////////////////////////////////////////////////////////////
  95. VOID
  96. DumpData (
  97. PUCHAR pData,
  98. ULONG ulSize
  99. )
  100. //////////////////////////////////////////////////////////////////////////////
  101. {
  102. ULONG ulCount;
  103. ULONG ul;
  104. UCHAR uc;
  105. while (ulSize)
  106. {
  107. ulCount = 16 < ulSize ? 16 : ulSize;
  108. for (ul = 0; ul < ulCount; ul++)
  109. {
  110. uc = *pData;
  111. TEST_DEBUG (TEST_DBG_TRACE, ("%02X ", uc));
  112. ulSize -= 1;
  113. pData += 1;
  114. }
  115. TEST_DEBUG (TEST_DBG_TRACE, ("\n"));
  116. }
  117. }
  118. #endif
  119. //////////////////////////////////////////////////////////////////////////////
  120. NTSTATUS
  121. CreateAdapter (
  122. PADAPTER *ppAdapter,
  123. NDIS_HANDLE ndishWrapper,
  124. NDIS_HANDLE ndishAdapterContext
  125. )
  126. //////////////////////////////////////////////////////////////////////////////
  127. {
  128. NTSTATUS nsResult;
  129. PADAPTER pAdapter;
  130. UCHAR tmp_buffer [32] = {0};
  131. //
  132. // Init the output paramter
  133. //
  134. *ppAdapter = NULL;
  135. //
  136. // Allocate memory for the adapter block now.
  137. //
  138. nsResult = AllocateMemory (&pAdapter, sizeof(ADAPTER));
  139. if (nsResult != NDIS_STATUS_SUCCESS)
  140. {
  141. return nsResult;
  142. }
  143. NdisZeroMemory (pAdapter, sizeof (ADAPTER));
  144. //
  145. // Init the reference count
  146. //
  147. pAdapter->ulRefCount = 1;
  148. //
  149. // Save the pAdapter into global storage
  150. //
  151. global_pAdapter = pAdapter;
  152. //
  153. // Initialize the adapter structure fields
  154. //
  155. pAdapter->ndishMiniport = ndishAdapterContext;
  156. //
  157. // Initialize the adapter vtable
  158. //
  159. pAdapter->lpVTable = (PADAPTER_VTABLE) &AdapterVTable;
  160. //
  161. // Save off the instance for this adapter
  162. //
  163. pAdapter->ulInstance = ulGlobalInstance++;
  164. //
  165. // init the spinlock for this adapter
  166. //
  167. NdisAllocateSpinLock(&(pAdapter->ndisSpinLock));
  168. //Enable adapter
  169. pAdapter->BDAAdapterEnable =1 ;
  170. //
  171. // Setup the vendor description string for this instance
  172. //
  173. nsResult = AllocateMemory (&pAdapter->pVendorDescription, sizeof(achGlobalVendorDescription) + 8);
  174. if (nsResult != NDIS_STATUS_SUCCESS)
  175. {
  176. return nsResult;
  177. }
  178. NdisZeroMemory (pAdapter->pVendorDescription, sizeof (achGlobalVendorDescription) + 8);
  179. NdisMoveMemory (pAdapter->pVendorDescription, (PVOID) achGlobalVendorDescription, sizeof (achGlobalVendorDescription));
  180. /*
  181. #if DBG
  182. MyStrCat (pAdapter->pVendorDescription, "(");
  183. MyStrCat (pAdapter->pVendorDescription, MyUlToA (pAdapter->ulInstance, tmp_buffer, 10));
  184. MyStrCat (pAdapter->pVendorDescription, ")");
  185. DbgPrint ("Vendor description: %s\n", pAdapter->pVendorDescription);
  186. #endif // DEBUG
  187. */
  188. //
  189. // Set default completion timeout to IGNORE
  190. //
  191. // WARNING! The interface type is not optional!
  192. //
  193. NdisMSetAttributesEx (
  194. ndishAdapterContext,
  195. pAdapter,
  196. 4,
  197. NDIS_ATTRIBUTE_IGNORE_PACKET_TIMEOUT |
  198. NDIS_ATTRIBUTE_IGNORE_REQUEST_TIMEOUT |
  199. NDIS_ATTRIBUTE_DESERIALIZE ,
  200. NdisInterfaceInternal);
  201. #ifndef WIN9X
  202. //
  203. // Create a device so other drivers (ie Streaming minidriver) can
  204. // link up with us
  205. //
  206. nsResult = (NTSTATUS) ntInitializeDeviceObject (
  207. ndishWrapper,
  208. pAdapter,
  209. &pAdapter->pDeviceObject,
  210. &pAdapter->ndisDeviceHandle);
  211. if (nsResult != NDIS_STATUS_SUCCESS)
  212. {
  213. return nsResult;
  214. }
  215. #endif
  216. ///////////////////////////////////////////////////
  217. //
  218. // Allocate a buffer pool. This pool will be used
  219. // to indicate the streaming data frames.
  220. //
  221. CreateFramePool (pAdapter,
  222. &pAdapter->pFramePool,
  223. IPSINK_NDIS_MAX_BUFFERS,
  224. IPSINK_NDIS_BUFFER_SIZE,
  225. sizeof (IPSINK_MEDIA_SPECIFIC_INFORMATION)
  226. );
  227. return nsResult;
  228. }
  229. ///////////////////////////////////////////////////////////////////////////////////
  230. NTSTATUS
  231. Adapter_QueryInterface (
  232. PADAPTER pAdapter
  233. )
  234. ///////////////////////////////////////////////////////////////////////////////////
  235. {
  236. return STATUS_NOT_IMPLEMENTED;
  237. }
  238. ///////////////////////////////////////////////////////////////////////////////////
  239. ULONG
  240. Adapter_AddRef (
  241. PADAPTER pAdapter
  242. )
  243. ///////////////////////////////////////////////////////////////////////////////////
  244. {
  245. if (pAdapter)
  246. {
  247. pAdapter->ulRefCount += 1;
  248. return pAdapter->ulRefCount;
  249. }
  250. return 0;
  251. }
  252. ///////////////////////////////////////////////////////////////////////////////////
  253. ULONG
  254. Adapter_Release (
  255. PADAPTER pAdapter
  256. )
  257. ///////////////////////////////////////////////////////////////////////////////////
  258. {
  259. ULONG ulRefCount = 0L;
  260. if (pAdapter)
  261. {
  262. pAdapter->ulRefCount -= 1;
  263. ulRefCount = pAdapter->ulRefCount;
  264. if (pAdapter->ulRefCount == 0)
  265. {
  266. FreeMemory (pAdapter, sizeof (ADAPTER));
  267. }
  268. }
  269. return ulRefCount;
  270. }
  271. //////////////////////////////////////////////////////////////////////////////
  272. VOID
  273. Adapter_IndicateReset (
  274. PADAPTER pAdapter
  275. )
  276. //////////////////////////////////////////////////////////////////////////////
  277. {
  278. if (pAdapter)
  279. {
  280. if (pAdapter->pCurrentFrame != NULL)
  281. {
  282. if (pAdapter->pCurrentFrame->pFramePool)
  283. {
  284. TEST_DEBUG (TEST_DBG_TRACE, ("Putting Current Frame %08X back on Available Queue\n", pAdapter->pCurrentFrame));
  285. PutFrame (pAdapter->pCurrentFrame->pFramePool, &pAdapter->pCurrentFrame->pFramePool->leAvailableQueue, pAdapter->pCurrentFrame);
  286. }
  287. pAdapter->pCurrentFrame = NULL;
  288. pAdapter->pIn = NULL;
  289. pAdapter->ulPR = 0;
  290. }
  291. }
  292. }
  293. //////////////////////////////////////////////////////////////////////////////
  294. ULONG
  295. Adapter_GetDescription (
  296. PADAPTER pAdapter,
  297. PUCHAR pDescription
  298. )
  299. //////////////////////////////////////////////////////////////////////////////
  300. {
  301. ULONG ulLength;
  302. ulLength = MyStrLen (pAdapter->pVendorDescription) + 1; // add 1 to include terminator
  303. //
  304. // If the description pointer is NULL, then pass back the length only
  305. //
  306. if (pDescription != NULL)
  307. {
  308. NdisMoveMemory (pDescription, pAdapter->pVendorDescription, ulLength);
  309. }
  310. return ulLength;
  311. }
  312. //////////////////////////////////////////////////////////////////////////////
  313. VOID
  314. Adapter_CloseLink (
  315. PADAPTER pAdapter
  316. )
  317. //////////////////////////////////////////////////////////////////////////////
  318. {
  319. if (pAdapter)
  320. {
  321. if (pAdapter->pFilter != NULL)
  322. {
  323. pAdapter->pFilter->lpVTable->Release (pAdapter->pFilter);
  324. pAdapter->pFilter = NULL;
  325. }
  326. }
  327. }
  328. //////////////////////////////////////////////////////////////////////////////
  329. NTSTATUS
  330. GetNdisFrame (
  331. PADAPTER pAdapter,
  332. PFRAME *ppFrame
  333. )
  334. //////////////////////////////////////////////////////////////////////////////
  335. {
  336. PFRAME pFrame = NULL;
  337. NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
  338. PHEADER_802_3 pEthHeader = NULL;
  339. PHEADER_IP pIPHeader = NULL;
  340. *ppFrame = NULL;
  341. pFrame = GetFrame (pAdapter->pFramePool, &pAdapter->pFramePool->leAvailableQueue);
  342. TEST_DEBUG (TEST_DBG_TRACE, ("Getting Frame %08X from the Available Queue\n", pFrame));
  343. if (pFrame)
  344. {
  345. ntStatus = STATUS_SUCCESS;
  346. *ppFrame = pFrame;
  347. }
  348. return ntStatus;
  349. }
  350. #define SWAP_WORD(A) ((A >> 8) & 0x00FF) + ((A << 8) & 0xFF00)
  351. //////////////////////////////////////////////////////////////////////////////
  352. USHORT
  353. sizeof_packet (
  354. PHEADER_IP pIpHdr
  355. )
  356. //////////////////////////////////////////////////////////////////////////////
  357. {
  358. USHORT usLength;
  359. usLength = pIpHdr->usLength;
  360. usLength = SWAP_WORD (usLength);
  361. return usLength;
  362. }
  363. //////////////////////////////////////////////////////////////////////////////
  364. NTSTATUS
  365. TranslateAndIndicate (
  366. PADAPTER pAdapter,
  367. PUCHAR pOut,
  368. ULONG ulSR
  369. )
  370. //////////////////////////////////////////////////////////////////////////////
  371. {
  372. NTSTATUS nsResult = STATUS_SUCCESS;
  373. ULONG ulAmtToCopy;
  374. ULONG uliNextByte;
  375. ASSERT (pAdapter);
  376. ASSERT (pOut);
  377. for ( uliNextByte = 0; uliNextByte < ulSR; )
  378. {
  379. HEADER_802_3 * pHeader802_3;
  380. HEADER_IP * pHeaderIP=NULL;
  381. ulAmtToCopy = 0;
  382. // If there is no current frame then sync up to a new
  383. // 802.3 (RFC 894) ethernet frame.
  384. //
  385. if (pAdapter->pCurrentFrame == NULL)
  386. {
  387. // Sync to a valid looking 802.3 frame
  388. //
  389. while ((ulSR - uliNextByte) >= (sizeof (HEADER_802_3) + sizeof (HEADER_IP)))
  390. {
  391. pHeader802_3 = (HEADER_802_3 *) &(pOut[uliNextByte]);
  392. pHeaderIP = (HEADER_IP *) &(pOut[uliNextByte + sizeof(HEADER_802_3)]);
  393. if ( (pHeader802_3->Type[0] == 0x08)
  394. && (pHeader802_3->Type[1] == 0x00)
  395. && (pHeaderIP->ucVersion_Length == 0x45)
  396. && (sizeof_packet( pHeaderIP) <= MAX_IP_PACKET_SIZE)
  397. )
  398. {
  399. break;
  400. }
  401. uliNextByte++;
  402. }
  403. if ((ulSR - uliNextByte) < (sizeof (HEADER_802_3) + sizeof (HEADER_IP)))
  404. {
  405. TEST_DEBUG (TEST_DBG_INFO, ("Stream buffer consumed while searching for valid IP packet\n"));
  406. nsResult = STATUS_SUCCESS;
  407. goto ret;
  408. }
  409. //
  410. // Everything looks good...get a new frame and start data transfer
  411. //
  412. nsResult = GetNdisFrame( pAdapter,
  413. &pAdapter->pCurrentFrame
  414. );
  415. if (nsResult != STATUS_SUCCESS)
  416. {
  417. TEST_DEBUG (TEST_DBG_ERROR, ("Get NDIS frame failed. No more NDIS frames available. No Frame built\n"));
  418. nsResult = STATUS_SUCCESS;
  419. pAdapter->stats.ulOID_GEN_RCV_NO_BUFFER += 1;
  420. pAdapter->pIn = NULL;
  421. pAdapter->pCurrentFrame = NULL;
  422. pAdapter->ulPR = 0;
  423. goto ret;
  424. }
  425. //Assert((pHeaderIP)!=NULL) can be used for nonretail builds here
  426. //check if any valid null pHeaderIP case ever exists
  427. if(!pHeaderIP)
  428. {
  429. nsResult = !(STATUS_SUCCESS);
  430. goto ret;
  431. }
  432. //
  433. // Update the reference count for this frame
  434. //
  435. pAdapter->pCurrentFrame->lpVTable->AddRef( pAdapter->pCurrentFrame);
  436. //
  437. // define pointers to the data in and out buffers, and init the packet size field
  438. //
  439. pAdapter->pIn = (PUCHAR) (pAdapter->pCurrentFrame->pvMemory);
  440. pAdapter->ulPR = sizeof_packet( pHeaderIP) + sizeof (HEADER_802_3);
  441. pAdapter->pCurrentFrame->ulcbData = pAdapter->ulPR;
  442. TEST_DEBUG (TEST_DBG_TRACE, ("CREATING NEW NDIS FRAME %08X, packet size %d\n", pAdapter->pCurrentFrame, pAdapter->ulPR));
  443. }
  444. if (pAdapter->ulPR <= (ulSR - uliNextByte))
  445. {
  446. ulAmtToCopy = pAdapter->ulPR;
  447. }
  448. else
  449. {
  450. ulAmtToCopy = ulSR - uliNextByte;
  451. }
  452. NdisMoveMemory( pAdapter->pIn,
  453. &(pOut[uliNextByte]),
  454. ulAmtToCopy
  455. );
  456. pAdapter->pIn += ulAmtToCopy;
  457. pAdapter->ulPR -= ulAmtToCopy;
  458. uliNextByte += ulAmtToCopy;
  459. if (pAdapter->ulPR == 0)
  460. {
  461. BOOLEAN bResult;
  462. PINDICATE_CONTEXT pIndicateContext = NULL;
  463. NDIS_HANDLE SwitchHandle = NULL;
  464. AllocateMemory (&pIndicateContext, sizeof (INDICATE_CONTEXT));
  465. if(!pIndicateContext)
  466. {
  467. nsResult = STATUS_NO_MEMORY;
  468. goto ret;
  469. }
  470. pIndicateContext->pAdapter = pAdapter;
  471. //
  472. // Place the frame on the indicateQueue
  473. //
  474. TEST_DEBUG (TEST_DBG_TRACE, ("Putting Frame %08X on Indicate Queue\n", pAdapter->pCurrentFrame));
  475. PutFrame (pAdapter->pFramePool, &pAdapter->pFramePool->leIndicateQueue, pAdapter->pCurrentFrame);
  476. pAdapter->pCurrentFrame = NULL;
  477. //Check status of SourceRouting flag
  478. SourceRoutingStatusPoll();
  479. //
  480. //
  481. // Switch to a state which allows us to call NDIS functions
  482. //
  483. NdisAcquireSpinLock(&(pAdapter->ndisSpinLock));
  484. IndicateCallbackHandler (pAdapter->ndishMiniport, (PVOID) pIndicateContext);
  485. NdisReleaseSpinLock(&(pAdapter->ndisSpinLock));
  486. }
  487. }
  488. ret:
  489. return nsResult;
  490. }
  491. //////////////////////////////////////////////////////////////////////////////
  492. NTSTATUS
  493. Adapter_IndicateData (
  494. IN PADAPTER pAdapter,
  495. IN PVOID pvData,
  496. IN ULONG ulcbData
  497. )
  498. //////////////////////////////////////////////////////////////////////////////
  499. {
  500. NTSTATUS ntStatus = STATUS_SUCCESS;
  501. ntStatus = TranslateAndIndicate (pAdapter, pvData, ulcbData);
  502. return ntStatus;
  503. }