WinPacketFilter.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /**@file
  2. Windows Packet Filter implementation of the EMU_SNP_PROTOCOL that allows the
  3. emulator to get on real networks.
  4. Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
  5. Portions copyright (c) 2011, Apple Inc. All rights reserved.
  6. (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include "WinHost.h"
  10. #define NETWORK_LIBRARY_NAME_U L"SnpNt32Io.dll"
  11. #define NETWORK_LIBRARY_INITIALIZE "SnpInitialize"
  12. #define NETWORK_LIBRARY_FINALIZE "SnpFinalize"
  13. #define NETWORK_LIBRARY_SET_RCV_FILTER "SnpSetReceiveFilter"
  14. #define NETWORK_LIBRARY_RECEIVE "SnpReceive"
  15. #define NETWORK_LIBRARY_TRANSMIT "SnpTransmit"
  16. #pragma pack(1)
  17. typedef struct _NT_NET_INTERFACE_INFO {
  18. UINT32 InterfaceIndex;
  19. EFI_MAC_ADDRESS MacAddr;
  20. } NT_NET_INTERFACE_INFO;
  21. #pragma pack()
  22. #define NET_ETHER_HEADER_SIZE 14
  23. #define MAX_INTERFACE_INFO_NUMBER 16
  24. #define SNP_MAX_TX_BUFFER_NUM 65536
  25. #define SNP_TX_BUFFER_INCREASEMENT 32
  26. #define DEFAULT_SELECTED_NIC_INDEX 0
  27. //
  28. // Functions in Net Library
  29. //
  30. typedef
  31. INT32
  32. (*NT_NET_INITIALIZE) (
  33. IN OUT UINT32 *InterfaceCount,
  34. IN OUT NT_NET_INTERFACE_INFO *InterfaceInfoBuffer
  35. );
  36. typedef
  37. INT32
  38. (*NT_NET_FINALIZE) (
  39. VOID
  40. );
  41. typedef
  42. INT32
  43. (*NT_NET_SET_RECEIVE_FILTER) (
  44. IN UINT32 Index,
  45. IN UINT32 EnableFilter,
  46. IN UINT32 MCastFilterCnt,
  47. IN EFI_MAC_ADDRESS *MCastFilter
  48. );
  49. typedef
  50. INT32
  51. (*NT_NET_RECEIVE) (
  52. IN UINT32 Index,
  53. IN OUT UINT32 *BufferSize,
  54. OUT VOID *Buffer
  55. );
  56. typedef
  57. INT32
  58. (*NT_NET_TRANSMIT) (
  59. IN UINT32 Index,
  60. IN UINT32 HeaderSize,
  61. IN UINT32 BufferSize,
  62. IN VOID *Buffer,
  63. IN EFI_MAC_ADDRESS *SrcAddr,
  64. IN EFI_MAC_ADDRESS *DestAddr,
  65. IN UINT16 *Protocol
  66. );
  67. typedef struct _NT_NET_UTILITY_TABLE {
  68. NT_NET_INITIALIZE Initialize;
  69. NT_NET_FINALIZE Finalize;
  70. NT_NET_SET_RECEIVE_FILTER SetReceiveFilter;
  71. NT_NET_RECEIVE Receive;
  72. NT_NET_TRANSMIT Transmit;
  73. } NT_NET_UTILITY_TABLE;
  74. //
  75. // Instance data for each fake SNP instance
  76. //
  77. #define WIN_NT_INSTANCE_SIGNATURE SIGNATURE_32 ('N', 'T', 'I', 'S')
  78. typedef struct {
  79. UINT32 Signature;
  80. //
  81. // Array of the recycled transmit buffer address.
  82. //
  83. UINT64 *RecycledTxBuf;
  84. //
  85. // Current number of recycled buffer pointers in RecycledTxBuf.
  86. //
  87. UINT32 RecycledTxBufCount;
  88. //
  89. // The maximum number of recycled buffer pointers in RecycledTxBuf.
  90. //
  91. UINT32 MaxRecycledTxBuf;
  92. EFI_SIMPLE_NETWORK_MODE Mode;
  93. NT_NET_INTERFACE_INFO InterfaceInfo;
  94. } WIN_NT_INSTANCE_DATA;
  95. //
  96. // Instance data for each SNP private instance
  97. //
  98. #define WIN_NT_SIMPLE_NETWORK_PRIVATE_SIGNATURE SIGNATURE_32 ('N', 'T', 's', 'n')
  99. typedef struct {
  100. UINTN Signature;
  101. EMU_IO_THUNK_PROTOCOL *Thunk;
  102. EMU_SNP_PROTOCOL EmuSnp;
  103. EFI_SIMPLE_NETWORK_MODE *Mode;
  104. HMODULE NetworkLibraryHandle;
  105. NT_NET_UTILITY_TABLE NtNetUtilityTable;
  106. WIN_NT_INSTANCE_DATA Instance;
  107. } WIN_NT_SNP_PRIVATE;
  108. #define WIN_NT_SNP_PRIVATE_DATA_FROM_THIS(a) \
  109. CR(a, WIN_NT_SNP_PRIVATE, EmuSnp, WIN_NT_SIMPLE_NETWORK_PRIVATE_SIGNATURE)
  110. /**
  111. Register storage for SNP Mode.
  112. @param This Protocol instance pointer.
  113. @param Mode SimpleNetworkProtocol Mode structure passed into driver.
  114. @retval EFI_SUCCESS The network interface was started.
  115. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  116. **/
  117. EFI_STATUS
  118. WinNtSnpCreateMapping (
  119. IN EMU_SNP_PROTOCOL *This,
  120. IN EFI_SIMPLE_NETWORK_MODE *Mode
  121. )
  122. {
  123. WIN_NT_SNP_PRIVATE *Private;
  124. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  125. Private->Mode = Mode;
  126. //
  127. // Set the broadcast address.
  128. //
  129. CopyMem (&Mode->BroadcastAddress, &Private->Instance.Mode.BroadcastAddress, sizeof (EFI_MAC_ADDRESS));
  130. //
  131. // Set the MAC address.
  132. //
  133. CopyMem (&Mode->CurrentAddress, &Private->Instance.Mode.CurrentAddress, sizeof (EFI_MAC_ADDRESS));
  134. CopyMem (&Mode->PermanentAddress, &Private->Instance.Mode.PermanentAddress, sizeof (EFI_MAC_ADDRESS));
  135. return EFI_SUCCESS;
  136. }
  137. /**
  138. Changes the state of a network interface from "stopped" to "started".
  139. @param This Protocol instance pointer.
  140. @retval EFI_SUCCESS The network interface was started.
  141. @retval EFI_ALREADY_STARTED The network interface is already in the started state.
  142. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  143. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  144. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  145. **/
  146. EFI_STATUS
  147. WinNtSnpStart (
  148. IN EMU_SNP_PROTOCOL *This
  149. )
  150. {
  151. WIN_NT_SNP_PRIVATE *Private;
  152. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  153. switch (Private->Mode->State) {
  154. case EfiSimpleNetworkStopped:
  155. break;
  156. case EfiSimpleNetworkStarted:
  157. case EfiSimpleNetworkInitialized:
  158. return EFI_ALREADY_STARTED;
  159. break;
  160. default:
  161. return EFI_DEVICE_ERROR;
  162. break;
  163. }
  164. Private->Mode->State = EfiSimpleNetworkStarted;
  165. return EFI_SUCCESS;
  166. }
  167. /**
  168. Changes the state of a network interface from "started" to "stopped".
  169. @param This Protocol instance pointer.
  170. @retval EFI_SUCCESS The network interface was stopped.
  171. @retval EFI_ALREADY_STARTED The network interface is already in the stopped state.
  172. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  173. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  174. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  175. **/
  176. EFI_STATUS
  177. WinNtSnpStop (
  178. IN EMU_SNP_PROTOCOL *This
  179. )
  180. {
  181. WIN_NT_SNP_PRIVATE *Private;
  182. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  183. switch ( Private->Mode->State ) {
  184. case EfiSimpleNetworkStarted:
  185. break;
  186. case EfiSimpleNetworkStopped:
  187. return EFI_NOT_STARTED;
  188. break;
  189. default:
  190. return EFI_DEVICE_ERROR;
  191. break;
  192. }
  193. Private->Mode->State = EfiSimpleNetworkStopped;
  194. return EFI_SUCCESS;
  195. }
  196. /**
  197. Resets a network adapter and allocates the transmit and receive buffers
  198. required by the network interface; optionally, also requests allocation
  199. of additional transmit and receive buffers.
  200. @param This The protocol instance pointer.
  201. @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
  202. that the driver should allocate for the network interface.
  203. Some network interfaces will not be able to use the extra
  204. buffer, and the caller will not know if it is actually
  205. being used.
  206. @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
  207. that the driver should allocate for the network interface.
  208. Some network interfaces will not be able to use the extra
  209. buffer, and the caller will not know if it is actually
  210. being used.
  211. @retval EFI_SUCCESS The network interface was initialized.
  212. @retval EFI_NOT_STARTED The network interface has not been started.
  213. @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and
  214. receive buffers.
  215. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  216. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  217. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  218. **/
  219. EFI_STATUS
  220. WinNtSnpInitialize (
  221. IN EMU_SNP_PROTOCOL *This,
  222. IN UINTN ExtraRxBufferSize OPTIONAL,
  223. IN UINTN ExtraTxBufferSize OPTIONAL
  224. )
  225. {
  226. WIN_NT_SNP_PRIVATE *Private;
  227. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  228. switch ( Private->Mode->State ) {
  229. case EfiSimpleNetworkStarted:
  230. break;
  231. case EfiSimpleNetworkStopped:
  232. return EFI_NOT_STARTED;
  233. break;
  234. default:
  235. return EFI_DEVICE_ERROR;
  236. break;
  237. }
  238. Private->Mode->MCastFilterCount = 0;
  239. Private->Mode->ReceiveFilterSetting = 0;
  240. ZeroMem (Private->Mode->MCastFilter, sizeof (Private->Mode->MCastFilter));
  241. Private->Mode->State = EfiSimpleNetworkInitialized;
  242. return EFI_SUCCESS;
  243. }
  244. /**
  245. Resets a network adapter and re-initializes it with the parameters that were
  246. provided in the previous call to Initialize().
  247. @param This The protocol instance pointer.
  248. @param ExtendedVerification Indicates that the driver may perform a more
  249. exhaustive verification operation of the device
  250. during reset.
  251. @retval EFI_SUCCESS The network interface was reset.
  252. @retval EFI_NOT_STARTED The network interface has not been started.
  253. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  254. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  255. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  256. **/
  257. EFI_STATUS
  258. WinNtSnpReset (
  259. IN EMU_SNP_PROTOCOL *This,
  260. IN BOOLEAN ExtendedVerification
  261. )
  262. {
  263. WIN_NT_SNP_PRIVATE *Private;
  264. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  265. switch ( Private->Mode->State ) {
  266. case EfiSimpleNetworkInitialized:
  267. break;
  268. case EfiSimpleNetworkStopped:
  269. return EFI_NOT_STARTED;
  270. break;
  271. default:
  272. return EFI_DEVICE_ERROR;
  273. break;
  274. }
  275. return EFI_SUCCESS;
  276. }
  277. /**
  278. Resets a network adapter and leaves it in a state that is safe for
  279. another driver to initialize.
  280. @param This Protocol instance pointer.
  281. @retval EFI_SUCCESS The network interface was shutdown.
  282. @retval EFI_NOT_STARTED The network interface has not been started.
  283. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  284. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  285. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  286. **/
  287. EFI_STATUS
  288. WinNtSnpShutdown (
  289. IN EMU_SNP_PROTOCOL *This
  290. )
  291. {
  292. WIN_NT_SNP_PRIVATE *Private;
  293. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  294. switch ( Private->Mode->State ) {
  295. case EfiSimpleNetworkInitialized:
  296. break;
  297. case EfiSimpleNetworkStopped:
  298. return EFI_NOT_STARTED;
  299. break;
  300. default:
  301. return EFI_DEVICE_ERROR;
  302. break;
  303. }
  304. Private->Mode->State = EfiSimpleNetworkStarted;
  305. Private->Mode->ReceiveFilterSetting = 0;
  306. Private->Mode->MCastFilterCount = 0;
  307. ZeroMem (Private->Mode->MCastFilter, sizeof (Private->Mode->MCastFilter));
  308. return EFI_SUCCESS;
  309. }
  310. /**
  311. Manages the multicast receive filters of a network interface.
  312. @param This The protocol instance pointer.
  313. @param Enable A bit mask of receive filters to enable on the network interface.
  314. @param Disable A bit mask of receive filters to disable on the network interface.
  315. @param ResetMCastFilter Set to TRUE to reset the contents of the multicast receive
  316. filters on the network interface to their default values.
  317. @param McastFilterCnt Number of multicast HW MAC addresses in the new
  318. MCastFilter list. This value must be less than or equal to
  319. the MCastFilterCnt field of EMU_SNP_MODE. This
  320. field is optional if ResetMCastFilter is TRUE.
  321. @param MCastFilter A pointer to a list of new multicast receive filter HW MAC
  322. addresses. This list will replace any existing multicast
  323. HW MAC address list. This field is optional if
  324. ResetMCastFilter is TRUE.
  325. @retval EFI_SUCCESS The multicast receive filter list was updated.
  326. @retval EFI_NOT_STARTED The network interface has not been started.
  327. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  328. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  329. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  330. **/
  331. EFI_STATUS
  332. WinNtSnpReceiveFilters (
  333. IN EMU_SNP_PROTOCOL *This,
  334. IN UINT32 Enable,
  335. IN UINT32 Disable,
  336. IN BOOLEAN ResetMCastFilter,
  337. IN UINTN MCastFilterCnt OPTIONAL,
  338. IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
  339. )
  340. {
  341. WIN_NT_SNP_PRIVATE *Private;
  342. INT32 ReturnValue;
  343. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  344. ReturnValue = Private->NtNetUtilityTable.SetReceiveFilter (
  345. Private->Instance.InterfaceInfo.InterfaceIndex,
  346. Enable,
  347. (UINT32)MCastFilterCnt,
  348. MCastFilter
  349. );
  350. if (ReturnValue <= 0) {
  351. return EFI_DEVICE_ERROR;
  352. }
  353. return EFI_SUCCESS;
  354. }
  355. /**
  356. Modifies or resets the current station address, if supported.
  357. @param This The protocol instance pointer.
  358. @param Reset Flag used to reset the station address to the network interfaces
  359. permanent address.
  360. @param New The new station address to be used for the network interface.
  361. @retval EFI_SUCCESS The network interfaces station address was updated.
  362. @retval EFI_NOT_STARTED The network interface has not been started.
  363. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  364. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  365. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  366. **/
  367. EFI_STATUS
  368. WinNtSnpStationAddress (
  369. IN EMU_SNP_PROTOCOL *This,
  370. IN BOOLEAN Reset,
  371. IN EFI_MAC_ADDRESS *New OPTIONAL
  372. )
  373. {
  374. WIN_NT_SNP_PRIVATE *Private;
  375. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  376. return EFI_UNSUPPORTED;
  377. }
  378. /**
  379. Resets or collects the statistics on a network interface.
  380. @param This Protocol instance pointer.
  381. @param Reset Set to TRUE to reset the statistics for the network interface.
  382. @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
  383. output the size, in bytes, of the resulting table of
  384. statistics.
  385. @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
  386. contains the statistics.
  387. @retval EFI_SUCCESS The statistics were collected from the network interface.
  388. @retval EFI_NOT_STARTED The network interface has not been started.
  389. @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
  390. size needed to hold the statistics is returned in
  391. StatisticsSize.
  392. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  393. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  394. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  395. **/
  396. EFI_STATUS
  397. WinNtSnpStatistics (
  398. IN EMU_SNP_PROTOCOL *This,
  399. IN BOOLEAN Reset,
  400. IN OUT UINTN *StatisticsSize OPTIONAL,
  401. OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL
  402. )
  403. {
  404. WIN_NT_SNP_PRIVATE *Private;
  405. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  406. return EFI_UNSUPPORTED;
  407. }
  408. /**
  409. Converts a multicast IP address to a multicast HW MAC address.
  410. @param This The protocol instance pointer.
  411. @param IPv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
  412. to FALSE if the multicast IP address is IPv4 [RFC 791].
  413. @param IP The multicast IP address that is to be converted to a multicast
  414. HW MAC address.
  415. @param MAC The multicast HW MAC address that is to be generated from IP.
  416. @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
  417. HW MAC address.
  418. @retval EFI_NOT_STARTED The network interface has not been started.
  419. @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
  420. size needed to hold the statistics is returned in
  421. StatisticsSize.
  422. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  423. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  424. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  425. **/
  426. EFI_STATUS
  427. WinNtSnpMCastIpToMac (
  428. IN EMU_SNP_PROTOCOL *This,
  429. IN BOOLEAN IPv6,
  430. IN EFI_IP_ADDRESS *IP,
  431. OUT EFI_MAC_ADDRESS *MAC
  432. )
  433. {
  434. WIN_NT_SNP_PRIVATE *Private;
  435. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  436. return EFI_UNSUPPORTED;
  437. }
  438. /**
  439. Performs read and write operations on the NVRAM device attached to a
  440. network interface.
  441. @param This The protocol instance pointer.
  442. @param ReadWrite TRUE for read operations, FALSE for write operations.
  443. @param Offset Byte offset in the NVRAM device at which to start the read or
  444. write operation. This must be a multiple of NvRamAccessSize and
  445. less than NvRamSize.
  446. @param BufferSize The number of bytes to read or write from the NVRAM device.
  447. This must also be a multiple of NvramAccessSize.
  448. @param Buffer A pointer to the data buffer.
  449. @retval EFI_SUCCESS The NVRAM access was performed.
  450. @retval EFI_NOT_STARTED The network interface has not been started.
  451. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  452. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  453. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  454. **/
  455. EFI_STATUS
  456. WinNtSnpNvData (
  457. IN EMU_SNP_PROTOCOL *This,
  458. IN BOOLEAN ReadWrite,
  459. IN UINTN Offset,
  460. IN UINTN BufferSize,
  461. IN OUT VOID *Buffer
  462. )
  463. {
  464. WIN_NT_SNP_PRIVATE *Private;
  465. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  466. return EFI_UNSUPPORTED;
  467. }
  468. /**
  469. Reads the current interrupt status and recycled transmit buffer status from
  470. a network interface.
  471. @param This The protocol instance pointer.
  472. @param InterruptStatus A pointer to the bit mask of the currently active interrupts
  473. If this is NULL, the interrupt status will not be read from
  474. the device. If this is not NULL, the interrupt status will
  475. be read from the device. When the interrupt status is read,
  476. it will also be cleared. Clearing the transmit interrupt
  477. does not empty the recycled transmit buffer array.
  478. @param TxBuf Recycled transmit buffer address. The network interface will
  479. not transmit if its internal recycled transmit buffer array
  480. is full. Reading the transmit buffer does not clear the
  481. transmit interrupt. If this is NULL, then the transmit buffer
  482. status will not be read. If there are no transmit buffers to
  483. recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
  484. @retval EFI_SUCCESS The status of the network interface was retrieved.
  485. @retval EFI_NOT_STARTED The network interface has not been started.
  486. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  487. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  488. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  489. **/
  490. EFI_STATUS
  491. WinNtSnpGetStatus (
  492. IN EMU_SNP_PROTOCOL *This,
  493. OUT UINT32 *InterruptStatus OPTIONAL,
  494. OUT VOID **TxBuf OPTIONAL
  495. )
  496. {
  497. WIN_NT_SNP_PRIVATE *Private;
  498. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  499. if (TxBuf != NULL) {
  500. if (Private->Instance.RecycledTxBufCount != 0) {
  501. Private->Instance.RecycledTxBufCount--;
  502. *((UINT8 **)TxBuf) = (UINT8 *)(UINTN)Private->Instance.RecycledTxBuf[Private->Instance.RecycledTxBufCount];
  503. } else {
  504. *((UINT8 **)TxBuf) = NULL;
  505. }
  506. }
  507. if (InterruptStatus != NULL) {
  508. *InterruptStatus = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  509. }
  510. return EFI_SUCCESS;
  511. }
  512. /**
  513. Places a packet in the transmit queue of a network interface.
  514. @param This The protocol instance pointer.
  515. @param HeaderSize The size, in bytes, of the media header to be filled in by
  516. the Transmit() function. If HeaderSize is non-zero, then it
  517. must be equal to This->Mode->MediaHeaderSize and the DestAddr
  518. and Protocol parameters must not be NULL.
  519. @param BufferSize The size, in bytes, of the entire packet (media header and
  520. data) to be transmitted through the network interface.
  521. @param Buffer A pointer to the packet (media header followed by data) to be
  522. transmitted. This parameter cannot be NULL. If HeaderSize is zero,
  523. then the media header in Buffer must already be filled in by the
  524. caller. If HeaderSize is non-zero, then the media header will be
  525. filled in by the Transmit() function.
  526. @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
  527. is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
  528. This->Mode->CurrentAddress is used for the source HW MAC address.
  529. @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
  530. parameter is ignored.
  531. @param Protocol The type of header to build. If HeaderSize is zero, then this
  532. parameter is ignored. See RFC 1700, section "Ether Types", for
  533. examples.
  534. @retval EFI_SUCCESS The packet was placed on the transmit queue.
  535. @retval EFI_NOT_STARTED The network interface has not been started.
  536. @retval EFI_NOT_READY The network interface is too busy to accept this transmit request.
  537. @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
  538. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  539. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  540. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  541. **/
  542. EFI_STATUS
  543. WinNtSnpTransmit (
  544. IN EMU_SNP_PROTOCOL *This,
  545. IN UINTN HeaderSize,
  546. IN UINTN BufferSize,
  547. IN VOID *Buffer,
  548. IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  549. IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  550. IN UINT16 *Protocol OPTIONAL
  551. )
  552. {
  553. WIN_NT_SNP_PRIVATE *Private;
  554. INT32 ReturnValue;
  555. UINT64 *Tmp;
  556. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  557. if ((HeaderSize != 0) && (SrcAddr == NULL)) {
  558. SrcAddr = &Private->Instance.Mode.CurrentAddress;
  559. }
  560. ReturnValue = Private->NtNetUtilityTable.Transmit (
  561. Private->Instance.InterfaceInfo.InterfaceIndex,
  562. (UINT32)HeaderSize,
  563. (UINT32)BufferSize,
  564. Buffer,
  565. SrcAddr,
  566. DestAddr,
  567. Protocol
  568. );
  569. if (ReturnValue < 0) {
  570. return EFI_DEVICE_ERROR;
  571. } else {
  572. if ((Private->Instance.MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT) >= SNP_MAX_TX_BUFFER_NUM) {
  573. return EFI_NOT_READY;
  574. }
  575. if (Private->Instance.RecycledTxBufCount < Private->Instance.MaxRecycledTxBuf) {
  576. Private->Instance.RecycledTxBuf[Private->Instance.RecycledTxBufCount] = (UINT64)Buffer;
  577. Private->Instance.RecycledTxBufCount++;
  578. } else {
  579. Tmp = malloc (sizeof (UINT64) * (Private->Instance.MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT));
  580. if (Tmp == NULL) {
  581. return EFI_DEVICE_ERROR;
  582. }
  583. CopyMem (Tmp, Private->Instance.RecycledTxBuf, sizeof (UINT64) * Private->Instance.RecycledTxBufCount);
  584. free (Private->Instance.RecycledTxBuf);
  585. Private->Instance.RecycledTxBuf = Tmp;
  586. Private->Instance.MaxRecycledTxBuf += SNP_TX_BUFFER_INCREASEMENT;
  587. }
  588. }
  589. return EFI_SUCCESS;
  590. }
  591. /**
  592. Receives a packet from a network interface.
  593. @param This The protocol instance pointer.
  594. @param HeaderSize The size, in bytes, of the media header received on the network
  595. interface. If this parameter is NULL, then the media header size
  596. will not be returned.
  597. @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the size, in
  598. bytes, of the packet that was received on the network interface.
  599. @param Buffer A pointer to the data buffer to receive both the media header and
  600. the data.
  601. @param SrcAddr The source HW MAC address. If this parameter is NULL, the
  602. HW MAC source address will not be extracted from the media
  603. header.
  604. @param DestAddr The destination HW MAC address. If this parameter is NULL,
  605. the HW MAC destination address will not be extracted from the
  606. media header.
  607. @param Protocol The media header type. If this parameter is NULL, then the
  608. protocol will not be extracted from the media header. See
  609. RFC 1700 section "Ether Types" for examples.
  610. @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
  611. been updated to the number of bytes received.
  612. @retval EFI_NOT_STARTED The network interface has not been started.
  613. @retval EFI_NOT_READY The network interface is too busy to accept this transmit
  614. request.
  615. @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
  616. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  617. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  618. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  619. **/
  620. EFI_STATUS
  621. WinNtSnpReceive (
  622. IN EMU_SNP_PROTOCOL *This,
  623. OUT UINTN *HeaderSize OPTIONAL,
  624. IN OUT UINTN *BufferSize,
  625. OUT VOID *Buffer,
  626. OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  627. OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  628. OUT UINT16 *Protocol OPTIONAL
  629. )
  630. {
  631. WIN_NT_SNP_PRIVATE *Private;
  632. INT32 ReturnValue;
  633. UINTN BufSize;
  634. Private = WIN_NT_SNP_PRIVATE_DATA_FROM_THIS (This);
  635. BufSize = *BufferSize;
  636. ASSERT (Private->NtNetUtilityTable.Receive != NULL);
  637. ReturnValue = Private->NtNetUtilityTable.Receive (
  638. Private->Instance.InterfaceInfo.InterfaceIndex,
  639. BufferSize,
  640. Buffer
  641. );
  642. if (ReturnValue < 0) {
  643. if (ReturnValue == -100) {
  644. return EFI_BUFFER_TOO_SMALL;
  645. }
  646. return EFI_DEVICE_ERROR;
  647. } else if (ReturnValue == 0) {
  648. return EFI_NOT_READY;
  649. }
  650. if (HeaderSize != NULL) {
  651. *HeaderSize = 14;
  652. }
  653. if (SrcAddr != NULL) {
  654. ZeroMem (SrcAddr, sizeof (EFI_MAC_ADDRESS));
  655. CopyMem (SrcAddr, ((UINT8 *)Buffer) + 6, 6);
  656. }
  657. if (DestAddr != NULL) {
  658. ZeroMem (DestAddr, sizeof (EFI_MAC_ADDRESS));
  659. CopyMem (DestAddr, ((UINT8 *)Buffer), 6);
  660. }
  661. if (Protocol != NULL) {
  662. *Protocol = NTOHS (*((UINT16 *)(((UINT8 *)Buffer) + 12)));
  663. }
  664. return (*BufferSize <= BufSize) ? EFI_SUCCESS : EFI_BUFFER_TOO_SMALL;
  665. }
  666. /**
  667. Initialize the snpnt32 driver instance.
  668. @param Instance Pointer to the instance context data.
  669. @param NetInfo Pointer to the interface info.
  670. @retval EFI_SUCCESS The driver instance is initialized.
  671. @retval other Initialization errors.
  672. **/
  673. EFI_STATUS
  674. WinNtInitializeInstanceData (
  675. IN OUT WIN_NT_INSTANCE_DATA *Instance,
  676. IN NT_NET_INTERFACE_INFO *NetInfo
  677. )
  678. {
  679. if ((Instance == NULL) || (NetInfo == NULL)) {
  680. return EFI_INVALID_PARAMETER;
  681. }
  682. ZeroMem (Instance, sizeof (WIN_NT_INSTANCE_DATA));
  683. Instance->Signature = WIN_NT_INSTANCE_SIGNATURE;
  684. Instance->RecycledTxBufCount = 0;
  685. Instance->MaxRecycledTxBuf = 32;
  686. Instance->Mode.State = EfiSimpleNetworkInitialized;
  687. Instance->Mode.HwAddressSize = NET_ETHER_ADDR_LEN;
  688. Instance->Mode.MediaHeaderSize = NET_ETHER_HEADER_SIZE;
  689. Instance->Mode.MaxPacketSize = 1500;
  690. Instance->Mode.MaxMCastFilterCount = MAX_MCAST_FILTER_CNT;
  691. Instance->Mode.IfType = NET_IFTYPE_ETHERNET;
  692. Instance->Mode.MediaPresentSupported = TRUE;
  693. Instance->Mode.MediaPresent = TRUE;
  694. //
  695. // Allocate the RecycledTxBuf.
  696. //
  697. Instance->RecycledTxBuf = malloc (sizeof (UINT64) * Instance->MaxRecycledTxBuf);
  698. if (Instance->RecycledTxBuf == NULL) {
  699. return EFI_OUT_OF_RESOURCES;
  700. }
  701. //
  702. // Set the interface information.
  703. //
  704. CopyMem (&Instance->InterfaceInfo, NetInfo, sizeof (Instance->InterfaceInfo));
  705. //
  706. // Set broadcast address
  707. //
  708. SetMem (&Instance->Mode.BroadcastAddress, sizeof (EFI_MAC_ADDRESS), 0xFF);
  709. //
  710. // Copy Current/PermanentAddress MAC address
  711. //
  712. CopyMem (&Instance->Mode.CurrentAddress, &Instance->InterfaceInfo.MacAddr, sizeof (Instance->Mode.CurrentAddress));
  713. CopyMem (&Instance->Mode.PermanentAddress, &Instance->InterfaceInfo.MacAddr, sizeof (Instance->Mode.PermanentAddress));
  714. //
  715. // Since the fake SNP is based on a real NIC, to avoid conflict with the host
  716. // NIC network stack, we use a different MAC address.
  717. // So just change the last byte of the MAC address for the real NIC.
  718. //
  719. Instance->Mode.CurrentAddress.Addr[NET_ETHER_ADDR_LEN - 1]++;
  720. return EFI_SUCCESS;
  721. }
  722. /**
  723. Initialize the net utility data.
  724. @param This Pointer to the private data.
  725. @param ActiveInstance The active network interface.
  726. @retval EFI_SUCCESS The global data is initialized.
  727. @retval EFI_NOT_FOUND The required DLL is not found.
  728. @retval EFI_DEVICE_ERROR Error initialize network utility library.
  729. @retval other Other errors.
  730. **/
  731. EFI_STATUS
  732. WintNtInitializeNetUtilityData (
  733. IN OUT WIN_NT_SNP_PRIVATE *Private,
  734. IN UINT8 ActiveInstance
  735. )
  736. {
  737. EFI_STATUS Status;
  738. CHAR16 *DllFileNameU;
  739. INT32 ReturnValue;
  740. BOOLEAN NetUtilityLibInitDone;
  741. NT_NET_INTERFACE_INFO NetInterfaceInfoBuffer[MAX_INTERFACE_INFO_NUMBER];
  742. UINT32 InterfaceCount;
  743. UINT8 ActiveInterfaceIndex;
  744. if (Private == NULL) {
  745. return EFI_INVALID_PARAMETER;
  746. }
  747. NetUtilityLibInitDone = FALSE;
  748. InterfaceCount = MAX_INTERFACE_INFO_NUMBER;
  749. DllFileNameU = NETWORK_LIBRARY_NAME_U;
  750. //
  751. // Load network utility library
  752. //
  753. Private->NetworkLibraryHandle = LoadLibraryEx (DllFileNameU, NULL, 0);
  754. if (NULL == Private->NetworkLibraryHandle) {
  755. return EFI_NOT_FOUND;
  756. }
  757. Private->NtNetUtilityTable.Initialize = (NT_NET_INITIALIZE)GetProcAddress (Private->NetworkLibraryHandle, NETWORK_LIBRARY_INITIALIZE);
  758. if (NULL == Private->NtNetUtilityTable.Initialize) {
  759. Status = EFI_NOT_FOUND;
  760. goto ErrorReturn;
  761. }
  762. Private->NtNetUtilityTable.Finalize = (NT_NET_FINALIZE)GetProcAddress (Private->NetworkLibraryHandle, NETWORK_LIBRARY_FINALIZE);
  763. if (NULL == Private->NtNetUtilityTable.Finalize) {
  764. Status = EFI_NOT_FOUND;
  765. goto ErrorReturn;
  766. }
  767. Private->NtNetUtilityTable.SetReceiveFilter = (NT_NET_SET_RECEIVE_FILTER)GetProcAddress (Private->NetworkLibraryHandle, NETWORK_LIBRARY_SET_RCV_FILTER);
  768. if (NULL == Private->NtNetUtilityTable.SetReceiveFilter) {
  769. Status = EFI_NOT_FOUND;
  770. goto ErrorReturn;
  771. }
  772. Private->NtNetUtilityTable.Receive = (NT_NET_RECEIVE)GetProcAddress (Private->NetworkLibraryHandle, NETWORK_LIBRARY_RECEIVE);
  773. if (NULL == Private->NtNetUtilityTable.Receive) {
  774. Status = EFI_NOT_FOUND;
  775. goto ErrorReturn;
  776. }
  777. Private->NtNetUtilityTable.Transmit = (NT_NET_TRANSMIT)GetProcAddress (Private->NetworkLibraryHandle, NETWORK_LIBRARY_TRANSMIT);
  778. if (NULL == Private->NtNetUtilityTable.Transmit) {
  779. Status = EFI_NOT_FOUND;
  780. goto ErrorReturn;
  781. }
  782. //
  783. // Initialize the network utility library
  784. // And enumerate the interfaces in emulator host
  785. //
  786. ReturnValue = Private->NtNetUtilityTable.Initialize (&InterfaceCount, &NetInterfaceInfoBuffer[0]);
  787. if (ReturnValue <= 0) {
  788. Status = EFI_DEVICE_ERROR;
  789. goto ErrorReturn;
  790. }
  791. NetUtilityLibInitDone = TRUE;
  792. if (InterfaceCount == 0) {
  793. Status = EFI_NOT_FOUND;
  794. goto ErrorReturn;
  795. }
  796. DEBUG ((DEBUG_INFO, "%a, total %d interface(s) found\n", __FUNCTION__, InterfaceCount));
  797. //
  798. // Active interface index is set to first interface if given instance does
  799. // not exist.
  800. //
  801. ActiveInterfaceIndex = (ActiveInstance >= InterfaceCount ? DEFAULT_SELECTED_NIC_INDEX : ActiveInstance);
  802. //
  803. // Initialize instance
  804. //
  805. Status = WinNtInitializeInstanceData (&Private->Instance, &NetInterfaceInfoBuffer[ActiveInterfaceIndex]);
  806. if (EFI_ERROR (Status)) {
  807. goto ErrorReturn;
  808. }
  809. return EFI_SUCCESS;
  810. ErrorReturn:
  811. if (Private->Instance.RecycledTxBuf != NULL) {
  812. free (Private->Instance.RecycledTxBuf);
  813. }
  814. if (NetUtilityLibInitDone) {
  815. if (Private->NtNetUtilityTable.Finalize != NULL) {
  816. Private->NtNetUtilityTable.Finalize ();
  817. Private->NtNetUtilityTable.Finalize = NULL;
  818. }
  819. }
  820. return Status;
  821. }
  822. /**
  823. Release the net utility data.
  824. @param This Pointer to the private data.
  825. @retval EFI_SUCCESS The global data is released.
  826. @retval other Other errors.
  827. **/
  828. EFI_STATUS
  829. WintNtReleaseNetUtilityData (
  830. IN OUT WIN_NT_SNP_PRIVATE *Private
  831. )
  832. {
  833. if (Private == NULL) {
  834. return EFI_INVALID_PARAMETER;
  835. }
  836. if (Private->Instance.RecycledTxBuf != NULL) {
  837. free (Private->Instance.RecycledTxBuf);
  838. }
  839. if (Private->NtNetUtilityTable.Finalize != NULL) {
  840. Private->NtNetUtilityTable.Finalize ();
  841. }
  842. FreeLibrary (Private->NetworkLibraryHandle);
  843. return EFI_SUCCESS;
  844. }
  845. EMU_SNP_PROTOCOL mWinNtSnpProtocol = {
  846. WinNtSnpCreateMapping,
  847. WinNtSnpStart,
  848. WinNtSnpStop,
  849. WinNtSnpInitialize,
  850. WinNtSnpReset,
  851. WinNtSnpShutdown,
  852. WinNtSnpReceiveFilters,
  853. WinNtSnpStationAddress,
  854. WinNtSnpStatistics,
  855. WinNtSnpMCastIpToMac,
  856. WinNtSnpNvData,
  857. WinNtSnpGetStatus,
  858. WinNtSnpTransmit,
  859. WinNtSnpReceive
  860. };
  861. /**
  862. Open SNP thunk protocol.
  863. @param This Pointer to the thunk protocol instance.
  864. @retval EFI_SUCCESS SNP thunk protocol is opened successfully.
  865. @retval EFI_UNSUPPORTED This is not SNP thunk protocol.
  866. @retval EFI_OUT_OF_RESOURCES Not enough memory.
  867. @retval other Other errors.
  868. **/
  869. EFI_STATUS
  870. WinNtSnpThunkOpen (
  871. IN EMU_IO_THUNK_PROTOCOL *This
  872. )
  873. {
  874. WIN_NT_SNP_PRIVATE *Private;
  875. UINT8 HostInterfaceIndex;
  876. HostInterfaceIndex = 0;
  877. if (This->Private != NULL) {
  878. return EFI_ALREADY_STARTED;
  879. }
  880. if (!CompareGuid (This->Protocol, &gEmuSnpProtocolGuid)) {
  881. return EFI_UNSUPPORTED;
  882. }
  883. Private = malloc (sizeof (WIN_NT_SNP_PRIVATE));
  884. if (Private == NULL) {
  885. return EFI_OUT_OF_RESOURCES;
  886. }
  887. Private->Signature = WIN_NT_SIMPLE_NETWORK_PRIVATE_SIGNATURE;
  888. Private->Thunk = This;
  889. CopyMem (&Private->EmuSnp, &mWinNtSnpProtocol, sizeof (mWinNtSnpProtocol));
  890. This->Interface = &Private->EmuSnp;
  891. This->Private = Private;
  892. if ((This->ConfigString != NULL) && (This->ConfigString[0] != '\0')) {
  893. HostInterfaceIndex = (UINT8)StrDecimalToUintn (This->ConfigString);
  894. }
  895. return WintNtInitializeNetUtilityData (Private, HostInterfaceIndex);
  896. }
  897. /**
  898. Close SNP thunk protocol.
  899. @param This Pointer to the thunk protocol instance.
  900. @retval EFI_SUCCESS SNP thunk protocol is closed successfully.
  901. @retval EFI_UNSUPPORTED This is not SNP thunk protocol.
  902. @retval other Other errors.
  903. **/
  904. EFI_STATUS
  905. WinNtSnpThunkClose (
  906. IN EMU_IO_THUNK_PROTOCOL *This
  907. )
  908. {
  909. WIN_NT_SNP_PRIVATE *Private;
  910. if (!CompareGuid (This->Protocol, &gEmuSnpProtocolGuid)) {
  911. return EFI_UNSUPPORTED;
  912. }
  913. Private = This->Private;
  914. WintNtReleaseNetUtilityData (Private);
  915. free (Private);
  916. return EFI_SUCCESS;
  917. }
  918. EMU_IO_THUNK_PROTOCOL mWinNtSnpThunkIo = {
  919. &gEmuSnpProtocolGuid,
  920. NULL,
  921. NULL,
  922. 0,
  923. WinNtSnpThunkOpen,
  924. WinNtSnpThunkClose,
  925. NULL
  926. };