Receive_filters.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /** @file
  2. Implementation of managing the multicast receive filters of a network
  3. interface.
  4. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Snp.h"
  8. /**
  9. Call undi to enable the receive filters.
  10. @param Snp Pointer to snp driver structure.
  11. @param EnableFlags Bit mask for enabling the receive filters.
  12. @param MCastAddressCount Multicast address count for a new multicast address
  13. list.
  14. @param MCastAddressList List of new multicast addresses.
  15. @retval EFI_SUCCESS The multicast receive filter list was updated.
  16. @retval EFI_INVALID_PARAMETER Invalid UNDI command.
  17. @retval EFI_UNSUPPORTED Command is not supported by UNDI.
  18. @retval EFI_DEVICE_ERROR Fail to execute UNDI command.
  19. **/
  20. EFI_STATUS
  21. PxeRecvFilterEnable (
  22. SNP_DRIVER *Snp,
  23. UINT32 EnableFlags,
  24. UINTN MCastAddressCount,
  25. EFI_MAC_ADDRESS *MCastAddressList
  26. )
  27. {
  28. Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS;
  29. Snp->Cdb.OpFlags = PXE_OPFLAGS_RECEIVE_FILTER_ENABLE;
  30. Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
  31. Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
  32. Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
  33. Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
  34. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  35. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  36. Snp->Cdb.IFnum = Snp->IfNum;
  37. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  38. if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) != 0) {
  39. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_UNICAST;
  40. }
  41. if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) != 0) {
  42. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
  43. }
  44. if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) != 0) {
  45. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
  46. }
  47. if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) {
  48. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
  49. }
  50. if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) {
  51. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST;
  52. }
  53. if (MCastAddressCount != 0) {
  54. Snp->Cdb.CPBsize = (UINT16)(MCastAddressCount * sizeof (EFI_MAC_ADDRESS));
  55. Snp->Cdb.CPBaddr = (UINT64)(UINTN)Snp->Cpb;
  56. CopyMem (Snp->Cpb, MCastAddressList, Snp->Cdb.CPBsize);
  57. }
  58. //
  59. // Issue UNDI command and check result.
  60. //
  61. DEBUG ((DEBUG_NET, "\nsnp->undi.receive_filters() "));
  62. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  63. if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
  64. //
  65. // UNDI command failed. Return UNDI status to caller.
  66. //
  67. DEBUG (
  68. (DEBUG_ERROR,
  69. "\nsnp->undi.receive_filters() %xh:%xh\n",
  70. Snp->Cdb.StatFlags,
  71. Snp->Cdb.StatCode)
  72. );
  73. switch (Snp->Cdb.StatCode) {
  74. case PXE_STATCODE_INVALID_CDB:
  75. case PXE_STATCODE_INVALID_CPB:
  76. case PXE_STATCODE_INVALID_PARAMETER:
  77. return EFI_INVALID_PARAMETER;
  78. case PXE_STATCODE_UNSUPPORTED:
  79. return EFI_UNSUPPORTED;
  80. }
  81. return EFI_DEVICE_ERROR;
  82. }
  83. return EFI_SUCCESS;
  84. }
  85. /**
  86. Call undi to disable the receive filters.
  87. @param Snp Pointer to snp driver structure
  88. @param DisableFlags Bit mask for disabling the receive filters
  89. @param ResetMCastList Boolean flag to reset/delete the multicast filter
  90. list.
  91. @retval EFI_SUCCESS The multicast receive filter list was updated.
  92. @retval EFI_DEVICE_ERROR Fail to execute UNDI command.
  93. **/
  94. EFI_STATUS
  95. PxeRecvFilterDisable (
  96. SNP_DRIVER *Snp,
  97. UINT32 DisableFlags,
  98. BOOLEAN ResetMCastList
  99. )
  100. {
  101. Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS;
  102. Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
  103. Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
  104. Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
  105. Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
  106. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  107. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  108. Snp->Cdb.IFnum = Snp->IfNum;
  109. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  110. Snp->Cdb.OpFlags = (UINT16)((DisableFlags != 0) ? PXE_OPFLAGS_RECEIVE_FILTER_DISABLE : PXE_OPFLAGS_NOT_USED);
  111. if (ResetMCastList) {
  112. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_RESET_MCAST_LIST;
  113. }
  114. if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) != 0) {
  115. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_UNICAST;
  116. }
  117. if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) != 0) {
  118. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
  119. }
  120. if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) != 0) {
  121. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
  122. }
  123. if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) {
  124. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
  125. }
  126. if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) {
  127. Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST;
  128. }
  129. //
  130. // Issue UNDI command and check result.
  131. //
  132. DEBUG ((DEBUG_NET, "\nsnp->undi.receive_filters() "));
  133. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  134. if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
  135. //
  136. // UNDI command failed. Return UNDI status to caller.
  137. //
  138. DEBUG (
  139. (DEBUG_ERROR,
  140. "\nsnp->undi.receive_filters() %xh:%xh\n",
  141. Snp->Cdb.StatFlags,
  142. Snp->Cdb.StatCode)
  143. );
  144. return EFI_DEVICE_ERROR;
  145. }
  146. return EFI_SUCCESS;
  147. }
  148. /**
  149. Call undi to read the receive filters.
  150. @param Snp Pointer to snp driver structure.
  151. @retval EFI_SUCCESS The receive filter was read.
  152. @retval EFI_DEVICE_ERROR Fail to execute UNDI command.
  153. **/
  154. EFI_STATUS
  155. PxeRecvFilterRead (
  156. SNP_DRIVER *Snp
  157. )
  158. {
  159. Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS;
  160. Snp->Cdb.OpFlags = PXE_OPFLAGS_RECEIVE_FILTER_READ;
  161. Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
  162. Snp->Cdb.DBsize = (UINT16)(Snp->Mode.MaxMCastFilterCount * sizeof (EFI_MAC_ADDRESS));
  163. Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
  164. if (Snp->Cdb.DBsize == 0) {
  165. Snp->Cdb.DBaddr = (UINT64)(UINTN)NULL;
  166. } else {
  167. Snp->Cdb.DBaddr = (UINT64)(UINTN)Snp->Db;
  168. ZeroMem (Snp->Db, Snp->Cdb.DBsize);
  169. }
  170. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  171. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  172. Snp->Cdb.IFnum = Snp->IfNum;
  173. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  174. DEBUG ((DEBUG_NET, "\nsnp->undi.receive_filters() "));
  175. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  176. if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
  177. //
  178. // UNDI command failed. Return UNDI status to caller.
  179. //
  180. DEBUG (
  181. (DEBUG_ERROR,
  182. "\nsnp->undi.receive_filters() %xh:%xh\n",
  183. Snp->Cdb.StatFlags,
  184. Snp->Cdb.StatCode)
  185. );
  186. return EFI_DEVICE_ERROR;
  187. }
  188. //
  189. // Convert UNDI32 StatFlags to EFI SNP filter flags.
  190. //
  191. Snp->Mode.ReceiveFilterSetting = 0;
  192. if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_UNICAST) != 0) {
  193. Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
  194. }
  195. if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_BROADCAST) != 0) {
  196. Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
  197. }
  198. if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_PROMISCUOUS) != 0) {
  199. Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
  200. }
  201. if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_ALL_MULTICAST) != 0) {
  202. Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
  203. }
  204. if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST) != 0) {
  205. Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
  206. }
  207. CopyMem (Snp->Mode.MCastFilter, Snp->Db, Snp->Cdb.DBsize);
  208. //
  209. // Count number of active entries in multicast filter list.
  210. //
  211. {
  212. EFI_MAC_ADDRESS ZeroMacAddr;
  213. SetMem (&ZeroMacAddr, sizeof ZeroMacAddr, 0);
  214. for (Snp->Mode.MCastFilterCount = 0;
  215. Snp->Mode.MCastFilterCount < Snp->Mode.MaxMCastFilterCount;
  216. Snp->Mode.MCastFilterCount++
  217. )
  218. {
  219. if (CompareMem (
  220. &Snp->Mode.MCastFilter[Snp->Mode.MCastFilterCount],
  221. &ZeroMacAddr,
  222. sizeof ZeroMacAddr
  223. ) == 0)
  224. {
  225. break;
  226. }
  227. }
  228. }
  229. return EFI_SUCCESS;
  230. }
  231. /**
  232. Manages the multicast receive filters of a network interface.
  233. This function is used enable and disable the hardware and software receive
  234. filters for the underlying network device.
  235. The receive filter change is broken down into three steps:
  236. * The filter mask bits that are set (ON) in the Enable parameter are added to
  237. the current receive filter settings.
  238. * The filter mask bits that are set (ON) in the Disable parameter are subtracted
  239. from the updated receive filter settings.
  240. * If the resulting receive filter setting is not supported by the hardware a
  241. more liberal setting is selected.
  242. If the same bits are set in the Enable and Disable parameters, then the bits
  243. in the Disable parameter takes precedence.
  244. If the ResetMCastFilter parameter is TRUE, then the multicast address list
  245. filter is disabled (irregardless of what other multicast bits are set in the
  246. Enable and Disable parameters). The SNP->Mode->MCastFilterCount field is set
  247. to zero. The Snp->Mode->MCastFilter contents are undefined.
  248. After enabling or disabling receive filter settings, software should verify
  249. the new settings by checking the Snp->Mode->ReceiveFilterSettings,
  250. Snp->Mode->MCastFilterCount and Snp->Mode->MCastFilter fields.
  251. Note: Some network drivers and/or devices will automatically promote receive
  252. filter settings if the requested setting can not be honored. For example, if
  253. a request for four multicast addresses is made and the underlying hardware
  254. only supports two multicast addresses the driver might set the promiscuous
  255. or promiscuous multicast receive filters instead. The receiving software is
  256. responsible for discarding any extra packets that get through the hardware
  257. receive filters.
  258. Note: Note: To disable all receive filter hardware, the network driver must
  259. be Shutdown() and Stopped(). Calling ReceiveFilters() with Disable set to
  260. Snp->Mode->ReceiveFilterSettings will make it so no more packets are
  261. returned by the Receive() function, but the receive hardware may still be
  262. moving packets into system memory before inspecting and discarding them.
  263. Unexpected system errors, reboots and hangs can occur if an OS is loaded
  264. and the network devices are not Shutdown() and Stopped().
  265. If ResetMCastFilter is TRUE, then the multicast receive filter list on the
  266. network interface will be reset to the default multicast receive filter list.
  267. If ResetMCastFilter is FALSE, and this network interface allows the multicast
  268. receive filter list to be modified, then the MCastFilterCnt and MCastFilter
  269. are used to update the current multicast receive filter list. The modified
  270. receive filter list settings can be found in the MCastFilter field of
  271. EFI_SIMPLE_NETWORK_MODE. If the network interface does not allow the multicast
  272. receive filter list to be modified, then EFI_INVALID_PARAMETER will be returned.
  273. If the driver has not been initialized, EFI_DEVICE_ERROR will be returned.
  274. If the receive filter mask and multicast receive filter list have been
  275. successfully updated on the network interface, EFI_SUCCESS will be returned.
  276. @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
  277. @param Enable A bit mask of receive filters to enable on the network
  278. interface.
  279. @param Disable A bit mask of receive filters to disable on the network
  280. interface. For backward compatibility with EFI 1.1
  281. platforms, the EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit
  282. must be set when the ResetMCastFilter parameter is TRUE.
  283. @param ResetMCastFilter Set to TRUE to reset the contents of the multicast
  284. receive filters on the network interface to their
  285. default values.
  286. @param MCastFilterCnt Number of multicast HW MAC addresses in the new MCastFilter
  287. list. This value must be less than or equal to the
  288. MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE.
  289. This field is optional if ResetMCastFilter is TRUE.
  290. @param MCastFilter A pointer to a list of new multicast receive filter HW
  291. MAC addresses. This list will replace any existing
  292. multicast HW MAC address list. This field is optional
  293. if ResetMCastFilter is TRUE.
  294. @retval EFI_SUCCESS The multicast receive filter list was updated.
  295. @retval EFI_NOT_STARTED The network interface has not been started.
  296. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  297. * This is NULL
  298. * There are bits set in Enable that are not set
  299. in Snp->Mode->ReceiveFilterMask
  300. * There are bits set in Disable that are not set
  301. in Snp->Mode->ReceiveFilterMask
  302. * Multicast is being enabled (the
  303. EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit is
  304. set in Enable, it is not set in Disable, and
  305. ResetMCastFilter is FALSE) and MCastFilterCount
  306. is zero
  307. * Multicast is being enabled and MCastFilterCount
  308. is greater than Snp->Mode->MaxMCastFilterCount
  309. * Multicast is being enabled and MCastFilter is NULL
  310. * Multicast is being enabled and one or more of
  311. the addresses in the MCastFilter list are not
  312. valid multicast MAC addresses
  313. @retval EFI_DEVICE_ERROR One or more of the following conditions is TRUE:
  314. * The network interface has been started but has
  315. not been initialized
  316. * An unexpected error was returned by the
  317. underlying network driver or device
  318. @retval EFI_UNSUPPORTED This function is not supported by the network
  319. interface.
  320. **/
  321. EFI_STATUS
  322. EFIAPI
  323. SnpUndi32ReceiveFilters (
  324. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  325. IN UINT32 Enable,
  326. IN UINT32 Disable,
  327. IN BOOLEAN ResetMCastFilter,
  328. IN UINTN MCastFilterCnt OPTIONAL,
  329. IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
  330. )
  331. {
  332. SNP_DRIVER *Snp;
  333. EFI_STATUS Status;
  334. EFI_TPL OldTpl;
  335. if (This == NULL) {
  336. return EFI_INVALID_PARAMETER;
  337. }
  338. Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
  339. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  340. switch (Snp->Mode.State) {
  341. case EfiSimpleNetworkInitialized:
  342. break;
  343. case EfiSimpleNetworkStopped:
  344. Status = EFI_NOT_STARTED;
  345. goto ON_EXIT;
  346. default:
  347. Status = EFI_DEVICE_ERROR;
  348. goto ON_EXIT;
  349. }
  350. //
  351. // check if we are asked to enable or disable something that the UNDI
  352. // does not even support!
  353. //
  354. if (((Enable &~Snp->Mode.ReceiveFilterMask) != 0) ||
  355. ((Disable &~Snp->Mode.ReceiveFilterMask) != 0))
  356. {
  357. Status = EFI_INVALID_PARAMETER;
  358. goto ON_EXIT;
  359. }
  360. if (ResetMCastFilter) {
  361. Disable |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST & Snp->Mode.ReceiveFilterMask;
  362. MCastFilterCnt = 0;
  363. MCastFilter = NULL;
  364. } else {
  365. if (MCastFilterCnt != 0) {
  366. if ((MCastFilterCnt > Snp->Mode.MaxMCastFilterCount) ||
  367. (MCastFilter == NULL))
  368. {
  369. Status = EFI_INVALID_PARAMETER;
  370. goto ON_EXIT;
  371. }
  372. }
  373. }
  374. if ((Enable == 0) && (Disable == 0) && !ResetMCastFilter && (MCastFilterCnt == 0)) {
  375. Status = EFI_SUCCESS;
  376. goto ON_EXIT;
  377. }
  378. if (((Enable & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) && (MCastFilterCnt == 0)) {
  379. Status = EFI_INVALID_PARAMETER;
  380. goto ON_EXIT;
  381. }
  382. if ((Enable != 0) || (MCastFilterCnt != 0)) {
  383. Status = PxeRecvFilterEnable (
  384. Snp,
  385. Enable,
  386. MCastFilterCnt,
  387. MCastFilter
  388. );
  389. if (EFI_ERROR (Status)) {
  390. goto ON_EXIT;
  391. }
  392. }
  393. if ((Disable != 0) || ResetMCastFilter) {
  394. Status = PxeRecvFilterDisable (Snp, Disable, ResetMCastFilter);
  395. if (EFI_ERROR (Status)) {
  396. goto ON_EXIT;
  397. }
  398. }
  399. Status = PxeRecvFilterRead (Snp);
  400. ON_EXIT:
  401. gBS->RestoreTPL (OldTpl);
  402. return Status;
  403. }