BerkeleyPacketFilter.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /**@file
  2. Berkeley Packet Filter implementation of the EMU_SNP_PROTOCOL that allows the
  3. emulator to get on real networks.
  4. Tested on Mac OS X.
  5. Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>
  6. Portions copyright (c) 2011, Apple Inc. All rights reserved.
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include "Host.h"
  10. #ifdef __APPLE__
  11. #include <Library/NetLib.h>
  12. #define EMU_SNP_PRIVATE_SIGNATURE SIGNATURE_32('E', 'M', 's', 'n')
  13. typedef struct {
  14. UINTN Signature;
  15. EMU_IO_THUNK_PROTOCOL *Thunk;
  16. EMU_SNP_PROTOCOL EmuSnp;
  17. EFI_SIMPLE_NETWORK_MODE *Mode;
  18. int BpfFd;
  19. char *InterfaceName;
  20. EFI_MAC_ADDRESS MacAddress;
  21. u_int ReadBufferSize;
  22. VOID *ReadBuffer;
  23. //
  24. // Two walking pointers to manage the multiple packets that can be returned
  25. // in a single read.
  26. //
  27. VOID *CurrentReadPointer;
  28. VOID *EndReadPointer;
  29. UINT32 ReceivedPackets;
  30. UINT32 DroppedPackets;
  31. } EMU_SNP_PRIVATE;
  32. #define EMU_SNP_PRIVATE_DATA_FROM_THIS(a) \
  33. CR(a, EMU_SNP_PRIVATE, EmuSnp, EMU_SNP_PRIVATE_SIGNATURE)
  34. //
  35. // Strange, but there doesn't appear to be any structure for the Ethernet header in edk2...
  36. //
  37. typedef struct {
  38. UINT8 DstAddr[NET_ETHER_ADDR_LEN];
  39. UINT8 SrcAddr[NET_ETHER_ADDR_LEN];
  40. UINT16 Type;
  41. } ETHERNET_HEADER;
  42. /**
  43. Register storage for SNP Mode.
  44. @param This Protocol instance pointer.
  45. @param Mode SimpleNetworkProtocol Mode structure passed into driver.
  46. @retval EFI_SUCCESS The network interface was started.
  47. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  48. **/
  49. EFI_STATUS
  50. EmuSnpCreateMapping (
  51. IN EMU_SNP_PROTOCOL *This,
  52. IN EFI_SIMPLE_NETWORK_MODE *Mode
  53. )
  54. {
  55. EMU_SNP_PRIVATE *Private;
  56. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  57. Private->Mode = Mode;
  58. //
  59. // Set the broadcast address.
  60. //
  61. SetMem (&Mode->BroadcastAddress, sizeof (EFI_MAC_ADDRESS), 0xFF);
  62. CopyMem (&Mode->CurrentAddress, &Private->MacAddress, sizeof (EFI_MAC_ADDRESS));
  63. CopyMem (&Mode->PermanentAddress, &Private->MacAddress, sizeof (EFI_MAC_ADDRESS));
  64. //
  65. // Since the fake SNP is based on a real NIC, to avoid conflict with the host NIC
  66. // network stack, we use a different MAC address.
  67. // So just change the last byte of the MAC address for the real NIC.
  68. //
  69. Mode->CurrentAddress.Addr[NET_ETHER_ADDR_LEN - 1]++;
  70. return EFI_SUCCESS;
  71. }
  72. static struct bpf_insn mFilterInstructionTemplate[] = {
  73. // Load 4 bytes from the destination MAC address.
  74. BPF_STMT (BPF_LD + BPF_W + BPF_ABS, OFFSET_OF (ETHERNET_HEADER, DstAddr[0])),
  75. // Compare to first 4 bytes of fake MAC address.
  76. BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0x12345678, 0, 3),
  77. // Load remaining 2 bytes from the destination MAC address.
  78. BPF_STMT (BPF_LD + BPF_H + BPF_ABS, OFFSET_OF (ETHERNET_HEADER, DstAddr[4])),
  79. // Compare to remaining 2 bytes of fake MAC address.
  80. BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0x9ABC, 5, 0),
  81. // Load 4 bytes from the destination MAC address.
  82. BPF_STMT (BPF_LD + BPF_W + BPF_ABS, OFFSET_OF (ETHERNET_HEADER, DstAddr[0])),
  83. // Compare to first 4 bytes of broadcast MAC address.
  84. BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0xFFFFFFFF, 0, 2),
  85. // Load remaining 2 bytes from the destination MAC address.
  86. BPF_STMT (BPF_LD + BPF_H + BPF_ABS, OFFSET_OF (ETHERNET_HEADER, DstAddr[4])),
  87. // Compare to remaining 2 bytes of broadcast MAC address.
  88. BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0xFFFF, 1, 0),
  89. // Reject packet.
  90. BPF_STMT (BPF_RET + BPF_K, 0),
  91. // Receive entire packet.
  92. BPF_STMT (BPF_RET + BPF_K, -1)
  93. };
  94. EFI_STATUS
  95. OpenBpfFileDescriptor (
  96. IN EMU_SNP_PRIVATE *Private,
  97. OUT int *Fd
  98. )
  99. {
  100. char BfpDeviceName[256];
  101. int Index;
  102. //
  103. // Open a Berkeley Packet Filter device. This must be done as root, so this is probably
  104. // the place which is most likely to fail...
  105. //
  106. for (Index = 0; TRUE; Index++ ) {
  107. snprintf (BfpDeviceName, sizeof (BfpDeviceName), "/dev/bpf%d", Index);
  108. *Fd = open (BfpDeviceName, O_RDWR, 0);
  109. if ( *Fd >= 0 ) {
  110. return EFI_SUCCESS;
  111. }
  112. if (errno == EACCES) {
  113. printf (
  114. "SNP: Permissions on '%s' are incorrect. Fix with 'sudo chmod 666 %s'.\n",
  115. BfpDeviceName,
  116. BfpDeviceName
  117. );
  118. }
  119. if (errno != EBUSY) {
  120. break;
  121. }
  122. }
  123. return EFI_OUT_OF_RESOURCES;
  124. }
  125. /**
  126. Changes the state of a network interface from "stopped" to "started".
  127. @param This Protocol instance pointer.
  128. @retval EFI_SUCCESS The network interface was started.
  129. @retval EFI_ALREADY_STARTED The network interface is already in the started state.
  130. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  131. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  132. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  133. **/
  134. EFI_STATUS
  135. EmuSnpStart (
  136. IN EMU_SNP_PROTOCOL *This
  137. )
  138. {
  139. EFI_STATUS Status;
  140. EMU_SNP_PRIVATE *Private;
  141. struct ifreq BoundIf;
  142. struct bpf_program BpfProgram;
  143. struct bpf_insn *FilterProgram;
  144. u_int Value;
  145. u_int ReadBufferSize;
  146. UINT16 Temp16;
  147. UINT32 Temp32;
  148. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  149. switch (Private->Mode->State) {
  150. case EfiSimpleNetworkStopped:
  151. break;
  152. case EfiSimpleNetworkStarted:
  153. case EfiSimpleNetworkInitialized:
  154. return EFI_ALREADY_STARTED;
  155. break;
  156. default:
  157. return EFI_DEVICE_ERROR;
  158. break;
  159. }
  160. Status = EFI_SUCCESS;
  161. Private->ReadBuffer = NULL;
  162. if (Private->BpfFd == 0) {
  163. Status = OpenBpfFileDescriptor (Private, &Private->BpfFd);
  164. if (EFI_ERROR (Status)) {
  165. goto DeviceErrorExit;
  166. }
  167. //
  168. // Get the read buffer size.
  169. //
  170. if (ioctl (Private->BpfFd, BIOCGBLEN, &ReadBufferSize) < 0) {
  171. goto DeviceErrorExit;
  172. }
  173. //
  174. // Default value from BIOCGBLEN is usually too small, so use a much larger size, if necessary.
  175. //
  176. if (ReadBufferSize < FixedPcdGet32 (PcdNetworkPacketFilterSize)) {
  177. ReadBufferSize = FixedPcdGet32 (PcdNetworkPacketFilterSize);
  178. if (ioctl (Private->BpfFd, BIOCSBLEN, &ReadBufferSize) < 0) {
  179. goto DeviceErrorExit;
  180. }
  181. }
  182. //
  183. // Associate our interface with this BPF file descriptor.
  184. //
  185. AsciiStrCpyS (BoundIf.ifr_name, sizeof (BoundIf.ifr_name), Private->InterfaceName);
  186. if (ioctl (Private->BpfFd, BIOCSETIF, &BoundIf) < 0) {
  187. goto DeviceErrorExit;
  188. }
  189. //
  190. // Enable immediate mode.
  191. //
  192. Value = 1;
  193. if (ioctl (Private->BpfFd, BIOCIMMEDIATE, &Value) < 0) {
  194. goto DeviceErrorExit;
  195. }
  196. //
  197. // Enable non-blocking I/O.
  198. //
  199. if (fcntl (Private->BpfFd, F_GETFL, 0) == -1) {
  200. goto DeviceErrorExit;
  201. }
  202. Value |= O_NONBLOCK;
  203. if (fcntl (Private->BpfFd, F_SETFL, Value) == -1) {
  204. goto DeviceErrorExit;
  205. }
  206. //
  207. // Disable "header complete" flag. This means the supplied source MAC address is
  208. // what goes on the wire.
  209. //
  210. Value = 1;
  211. if (ioctl (Private->BpfFd, BIOCSHDRCMPLT, &Value) < 0) {
  212. goto DeviceErrorExit;
  213. }
  214. //
  215. // Allocate read buffer.
  216. //
  217. Private->ReadBufferSize = ReadBufferSize;
  218. Private->ReadBuffer = malloc (Private->ReadBufferSize);
  219. if (Private->ReadBuffer == NULL) {
  220. goto ErrorExit;
  221. }
  222. Private->CurrentReadPointer = Private->EndReadPointer = Private->ReadBuffer;
  223. //
  224. // Install our packet filter: successful reads should only produce broadcast or unicast
  225. // packets directed to our fake MAC address.
  226. //
  227. FilterProgram = malloc (sizeof (mFilterInstructionTemplate));
  228. if ( FilterProgram == NULL ) {
  229. goto ErrorExit;
  230. }
  231. CopyMem (FilterProgram, &mFilterInstructionTemplate, sizeof (mFilterInstructionTemplate));
  232. //
  233. // Insert out fake MAC address into the filter. The data has to be host endian.
  234. //
  235. CopyMem (&Temp32, &Private->Mode->CurrentAddress.Addr[0], sizeof (UINT32));
  236. FilterProgram[1].k = NTOHL (Temp32);
  237. CopyMem (&Temp16, &Private->Mode->CurrentAddress.Addr[4], sizeof (UINT16));
  238. FilterProgram[3].k = NTOHS (Temp16);
  239. BpfProgram.bf_len = sizeof (mFilterInstructionTemplate) / sizeof (struct bpf_insn);
  240. BpfProgram.bf_insns = FilterProgram;
  241. if (ioctl (Private->BpfFd, BIOCSETF, &BpfProgram) < 0) {
  242. goto DeviceErrorExit;
  243. }
  244. free (FilterProgram);
  245. //
  246. // Enable promiscuous mode.
  247. //
  248. if (ioctl (Private->BpfFd, BIOCPROMISC, 0) < 0) {
  249. goto DeviceErrorExit;
  250. }
  251. Private->Mode->State = EfiSimpleNetworkStarted;
  252. }
  253. return Status;
  254. DeviceErrorExit:
  255. Status = EFI_DEVICE_ERROR;
  256. ErrorExit:
  257. if (Private->ReadBuffer != NULL) {
  258. free (Private->ReadBuffer);
  259. Private->ReadBuffer = NULL;
  260. }
  261. return Status;
  262. }
  263. /**
  264. Changes the state of a network interface from "started" to "stopped".
  265. @param This Protocol instance pointer.
  266. @retval EFI_SUCCESS The network interface was stopped.
  267. @retval EFI_ALREADY_STARTED The network interface is already in the stopped state.
  268. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  269. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  270. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  271. **/
  272. EFI_STATUS
  273. EmuSnpStop (
  274. IN EMU_SNP_PROTOCOL *This
  275. )
  276. {
  277. EMU_SNP_PRIVATE *Private;
  278. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  279. switch ( Private->Mode->State ) {
  280. case EfiSimpleNetworkStarted:
  281. break;
  282. case EfiSimpleNetworkStopped:
  283. return EFI_NOT_STARTED;
  284. break;
  285. default:
  286. return EFI_DEVICE_ERROR;
  287. break;
  288. }
  289. if (Private->BpfFd != 0) {
  290. close (Private->BpfFd);
  291. Private->BpfFd = 0;
  292. }
  293. if (Private->ReadBuffer != NULL) {
  294. free (Private->ReadBuffer);
  295. Private->CurrentReadPointer = Private->EndReadPointer = Private->ReadBuffer = NULL;
  296. }
  297. Private->Mode->State = EfiSimpleNetworkStopped;
  298. return EFI_SUCCESS;
  299. }
  300. /**
  301. Resets a network adapter and allocates the transmit and receive buffers
  302. required by the network interface; optionally, also requests allocation
  303. of additional transmit and receive buffers.
  304. @param This The protocol instance pointer.
  305. @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
  306. that the driver should allocate for the network interface.
  307. Some network interfaces will not be able to use the extra
  308. buffer, and the caller will not know if it is actually
  309. being used.
  310. @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
  311. that the driver should allocate for the network interface.
  312. Some network interfaces will not be able to use the extra
  313. buffer, and the caller will not know if it is actually
  314. being used.
  315. @retval EFI_SUCCESS The network interface was initialized.
  316. @retval EFI_NOT_STARTED The network interface has not been started.
  317. @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and
  318. receive buffers.
  319. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  320. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  321. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  322. **/
  323. EFI_STATUS
  324. EmuSnpInitialize (
  325. IN EMU_SNP_PROTOCOL *This,
  326. IN UINTN ExtraRxBufferSize OPTIONAL,
  327. IN UINTN ExtraTxBufferSize OPTIONAL
  328. )
  329. {
  330. EMU_SNP_PRIVATE *Private;
  331. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  332. switch ( Private->Mode->State ) {
  333. case EfiSimpleNetworkStarted:
  334. break;
  335. case EfiSimpleNetworkStopped:
  336. return EFI_NOT_STARTED;
  337. break;
  338. default:
  339. return EFI_DEVICE_ERROR;
  340. break;
  341. }
  342. Private->Mode->MCastFilterCount = 0;
  343. Private->Mode->ReceiveFilterSetting = 0;
  344. ZeroMem (Private->Mode->MCastFilter, sizeof (Private->Mode->MCastFilter));
  345. Private->Mode->State = EfiSimpleNetworkInitialized;
  346. return EFI_SUCCESS;
  347. }
  348. /**
  349. Resets a network adapter and re-initializes it with the parameters that were
  350. provided in the previous call to Initialize().
  351. @param This The protocol instance pointer.
  352. @param ExtendedVerification Indicates that the driver may perform a more
  353. exhaustive verification operation of the device
  354. during reset.
  355. @retval EFI_SUCCESS The network interface was reset.
  356. @retval EFI_NOT_STARTED The network interface has not been started.
  357. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  358. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  359. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  360. **/
  361. EFI_STATUS
  362. EmuSnpReset (
  363. IN EMU_SNP_PROTOCOL *This,
  364. IN BOOLEAN ExtendedVerification
  365. )
  366. {
  367. EMU_SNP_PRIVATE *Private;
  368. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  369. switch ( Private->Mode->State ) {
  370. case EfiSimpleNetworkInitialized:
  371. break;
  372. case EfiSimpleNetworkStopped:
  373. return EFI_NOT_STARTED;
  374. break;
  375. default:
  376. return EFI_DEVICE_ERROR;
  377. break;
  378. }
  379. return EFI_SUCCESS;
  380. }
  381. /**
  382. Resets a network adapter and leaves it in a state that is safe for
  383. another driver to initialize.
  384. @param This Protocol instance pointer.
  385. @retval EFI_SUCCESS The network interface was shutdown.
  386. @retval EFI_NOT_STARTED The network interface has not been started.
  387. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  388. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  389. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  390. **/
  391. EFI_STATUS
  392. EmuSnpShutdown (
  393. IN EMU_SNP_PROTOCOL *This
  394. )
  395. {
  396. EMU_SNP_PRIVATE *Private;
  397. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  398. switch ( Private->Mode->State ) {
  399. case EfiSimpleNetworkInitialized:
  400. break;
  401. case EfiSimpleNetworkStopped:
  402. return EFI_NOT_STARTED;
  403. break;
  404. default:
  405. return EFI_DEVICE_ERROR;
  406. break;
  407. }
  408. Private->Mode->State = EfiSimpleNetworkStarted;
  409. Private->Mode->ReceiveFilterSetting = 0;
  410. Private->Mode->MCastFilterCount = 0;
  411. ZeroMem (Private->Mode->MCastFilter, sizeof (Private->Mode->MCastFilter));
  412. if (Private->BpfFd != 0) {
  413. close (Private->BpfFd);
  414. Private->BpfFd = 0;
  415. }
  416. if (Private->ReadBuffer != NULL) {
  417. free (Private->ReadBuffer);
  418. Private->CurrentReadPointer = Private->EndReadPointer = Private->ReadBuffer = NULL;
  419. }
  420. return EFI_SUCCESS;
  421. }
  422. /**
  423. Manages the multicast receive filters of a network interface.
  424. @param This The protocol instance pointer.
  425. @param Enable A bit mask of receive filters to enable on the network interface.
  426. @param Disable A bit mask of receive filters to disable on the network interface.
  427. @param ResetMCastFilter Set to TRUE to reset the contents of the multicast receive
  428. filters on the network interface to their default values.
  429. @param McastFilterCnt Number of multicast HW MAC addresses in the new
  430. MCastFilter list. This value must be less than or equal to
  431. the MCastFilterCnt field of EMU_SNP_MODE. This
  432. field is optional if ResetMCastFilter is TRUE.
  433. @param MCastFilter A pointer to a list of new multicast receive filter HW MAC
  434. addresses. This list will replace any existing multicast
  435. HW MAC address list. This field is optional if
  436. ResetMCastFilter is TRUE.
  437. @retval EFI_SUCCESS The multicast receive filter list was updated.
  438. @retval EFI_NOT_STARTED The network interface has not been started.
  439. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  440. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  441. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  442. **/
  443. EFI_STATUS
  444. EmuSnpReceiveFilters (
  445. IN EMU_SNP_PROTOCOL *This,
  446. IN UINT32 Enable,
  447. IN UINT32 Disable,
  448. IN BOOLEAN ResetMCastFilter,
  449. IN UINTN MCastFilterCnt OPTIONAL,
  450. IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
  451. )
  452. {
  453. EMU_SNP_PRIVATE *Private;
  454. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  455. // For now, just succeed...
  456. return EFI_SUCCESS;
  457. }
  458. /**
  459. Modifies or resets the current station address, if supported.
  460. @param This The protocol instance pointer.
  461. @param Reset Flag used to reset the station address to the network interfaces
  462. permanent address.
  463. @param New The new station address to be used for the network interface.
  464. @retval EFI_SUCCESS The network interfaces station address was updated.
  465. @retval EFI_NOT_STARTED The network interface has not been started.
  466. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  467. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  468. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  469. **/
  470. EFI_STATUS
  471. EmuSnpStationAddress (
  472. IN EMU_SNP_PROTOCOL *This,
  473. IN BOOLEAN Reset,
  474. IN EFI_MAC_ADDRESS *New OPTIONAL
  475. )
  476. {
  477. EMU_SNP_PRIVATE *Private;
  478. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  479. return EFI_UNSUPPORTED;
  480. }
  481. /**
  482. Resets or collects the statistics on a network interface.
  483. @param This Protocol instance pointer.
  484. @param Reset Set to TRUE to reset the statistics for the network interface.
  485. @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
  486. output the size, in bytes, of the resulting table of
  487. statistics.
  488. @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
  489. contains the statistics.
  490. @retval EFI_SUCCESS The statistics were collected from the network interface.
  491. @retval EFI_NOT_STARTED The network interface has not been started.
  492. @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
  493. size needed to hold the statistics is returned in
  494. StatisticsSize.
  495. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  496. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  497. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  498. **/
  499. EFI_STATUS
  500. EmuSnpStatistics (
  501. IN EMU_SNP_PROTOCOL *This,
  502. IN BOOLEAN Reset,
  503. IN OUT UINTN *StatisticsSize OPTIONAL,
  504. OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL
  505. )
  506. {
  507. EMU_SNP_PRIVATE *Private;
  508. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  509. return EFI_UNSUPPORTED;
  510. }
  511. /**
  512. Converts a multicast IP address to a multicast HW MAC address.
  513. @param This The protocol instance pointer.
  514. @param IPv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
  515. to FALSE if the multicast IP address is IPv4 [RFC 791].
  516. @param IP The multicast IP address that is to be converted to a multicast
  517. HW MAC address.
  518. @param MAC The multicast HW MAC address that is to be generated from IP.
  519. @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
  520. HW MAC address.
  521. @retval EFI_NOT_STARTED The network interface has not been started.
  522. @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
  523. size needed to hold the statistics is returned in
  524. StatisticsSize.
  525. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  526. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  527. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  528. **/
  529. EFI_STATUS
  530. EmuSnpMCastIpToMac (
  531. IN EMU_SNP_PROTOCOL *This,
  532. IN BOOLEAN IPv6,
  533. IN EFI_IP_ADDRESS *IP,
  534. OUT EFI_MAC_ADDRESS *MAC
  535. )
  536. {
  537. EMU_SNP_PRIVATE *Private;
  538. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  539. return EFI_UNSUPPORTED;
  540. }
  541. /**
  542. Performs read and write operations on the NVRAM device attached to a
  543. network interface.
  544. @param This The protocol instance pointer.
  545. @param ReadWrite TRUE for read operations, FALSE for write operations.
  546. @param Offset Byte offset in the NVRAM device at which to start the read or
  547. write operation. This must be a multiple of NvRamAccessSize and
  548. less than NvRamSize.
  549. @param BufferSize The number of bytes to read or write from the NVRAM device.
  550. This must also be a multiple of NvramAccessSize.
  551. @param Buffer A pointer to the data buffer.
  552. @retval EFI_SUCCESS The NVRAM access was performed.
  553. @retval EFI_NOT_STARTED The network interface has not been started.
  554. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  555. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  556. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  557. **/
  558. EFI_STATUS
  559. EmuSnpNvData (
  560. IN EMU_SNP_PROTOCOL *This,
  561. IN BOOLEAN ReadWrite,
  562. IN UINTN Offset,
  563. IN UINTN BufferSize,
  564. IN OUT VOID *Buffer
  565. )
  566. {
  567. EMU_SNP_PRIVATE *Private;
  568. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  569. return EFI_UNSUPPORTED;
  570. }
  571. /**
  572. Reads the current interrupt status and recycled transmit buffer status from
  573. a network interface.
  574. @param This The protocol instance pointer.
  575. @param InterruptStatus A pointer to the bit mask of the currently active interrupts
  576. If this is NULL, the interrupt status will not be read from
  577. the device. If this is not NULL, the interrupt status will
  578. be read from the device. When the interrupt status is read,
  579. it will also be cleared. Clearing the transmit interrupt
  580. does not empty the recycled transmit buffer array.
  581. @param TxBuf Recycled transmit buffer address. The network interface will
  582. not transmit if its internal recycled transmit buffer array
  583. is full. Reading the transmit buffer does not clear the
  584. transmit interrupt. If this is NULL, then the transmit buffer
  585. status will not be read. If there are no transmit buffers to
  586. recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
  587. @retval EFI_SUCCESS The status of the network interface was retrieved.
  588. @retval EFI_NOT_STARTED The network interface has not been started.
  589. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  590. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  591. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  592. **/
  593. EFI_STATUS
  594. EmuSnpGetStatus (
  595. IN EMU_SNP_PROTOCOL *This,
  596. OUT UINT32 *InterruptStatus OPTIONAL,
  597. OUT VOID **TxBuf OPTIONAL
  598. )
  599. {
  600. EMU_SNP_PRIVATE *Private;
  601. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  602. if ( InterruptStatus != NULL ) {
  603. *InterruptStatus = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  604. }
  605. return EFI_SUCCESS;
  606. }
  607. /**
  608. Places a packet in the transmit queue of a network interface.
  609. @param This The protocol instance pointer.
  610. @param HeaderSize The size, in bytes, of the media header to be filled in by
  611. the Transmit() function. If HeaderSize is non-zero, then it
  612. must be equal to This->Mode->MediaHeaderSize and the DestAddr
  613. and Protocol parameters must not be NULL.
  614. @param BufferSize The size, in bytes, of the entire packet (media header and
  615. data) to be transmitted through the network interface.
  616. @param Buffer A pointer to the packet (media header followed by data) to be
  617. transmitted. This parameter cannot be NULL. If HeaderSize is zero,
  618. then the media header in Buffer must already be filled in by the
  619. caller. If HeaderSize is non-zero, then the media header will be
  620. filled in by the Transmit() function.
  621. @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
  622. is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
  623. This->Mode->CurrentAddress is used for the source HW MAC address.
  624. @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
  625. parameter is ignored.
  626. @param Protocol The type of header to build. If HeaderSize is zero, then this
  627. parameter is ignored. See RFC 1700, section "Ether Types", for
  628. examples.
  629. @retval EFI_SUCCESS The packet was placed on the transmit queue.
  630. @retval EFI_NOT_STARTED The network interface has not been started.
  631. @retval EFI_NOT_READY The network interface is too busy to accept this transmit request.
  632. @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
  633. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  634. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  635. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  636. **/
  637. EFI_STATUS
  638. EmuSnpTransmit (
  639. IN EMU_SNP_PROTOCOL *This,
  640. IN UINTN HeaderSize,
  641. IN UINTN BufferSize,
  642. IN VOID *Buffer,
  643. IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  644. IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  645. IN UINT16 *Protocol OPTIONAL
  646. )
  647. {
  648. EMU_SNP_PRIVATE *Private;
  649. ETHERNET_HEADER *EnetHeader;
  650. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  651. if (Private->Mode->State < EfiSimpleNetworkStarted) {
  652. return EFI_NOT_STARTED;
  653. }
  654. if ( HeaderSize != 0 ) {
  655. if ((DestAddr == NULL) || (Protocol == NULL) || (HeaderSize != Private->Mode->MediaHeaderSize)) {
  656. return EFI_INVALID_PARAMETER;
  657. }
  658. if (SrcAddr == NULL) {
  659. SrcAddr = &Private->Mode->CurrentAddress;
  660. }
  661. EnetHeader = (ETHERNET_HEADER *)Buffer;
  662. CopyMem (EnetHeader->DstAddr, DestAddr, NET_ETHER_ADDR_LEN);
  663. CopyMem (EnetHeader->SrcAddr, SrcAddr, NET_ETHER_ADDR_LEN);
  664. EnetHeader->Type = HTONS (*Protocol);
  665. }
  666. if (write (Private->BpfFd, Buffer, BufferSize) < 0) {
  667. return EFI_DEVICE_ERROR;
  668. }
  669. return EFI_SUCCESS;
  670. }
  671. /**
  672. Receives a packet from a network interface.
  673. @param This The protocol instance pointer.
  674. @param HeaderSize The size, in bytes, of the media header received on the network
  675. interface. If this parameter is NULL, then the media header size
  676. will not be returned.
  677. @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the size, in
  678. bytes, of the packet that was received on the network interface.
  679. @param Buffer A pointer to the data buffer to receive both the media header and
  680. the data.
  681. @param SrcAddr The source HW MAC address. If this parameter is NULL, the
  682. HW MAC source address will not be extracted from the media
  683. header.
  684. @param DestAddr The destination HW MAC address. If this parameter is NULL,
  685. the HW MAC destination address will not be extracted from the
  686. media header.
  687. @param Protocol The media header type. If this parameter is NULL, then the
  688. protocol will not be extracted from the media header. See
  689. RFC 1700 section "Ether Types" for examples.
  690. @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
  691. been updated to the number of bytes received.
  692. @retval EFI_NOT_STARTED The network interface has not been started.
  693. @retval EFI_NOT_READY The network interface is too busy to accept this transmit
  694. request.
  695. @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
  696. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  697. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  698. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  699. **/
  700. EFI_STATUS
  701. EmuSnpReceive (
  702. IN EMU_SNP_PROTOCOL *This,
  703. OUT UINTN *HeaderSize OPTIONAL,
  704. IN OUT UINTN *BufferSize,
  705. OUT VOID *Buffer,
  706. OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  707. OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  708. OUT UINT16 *Protocol OPTIONAL
  709. )
  710. {
  711. EMU_SNP_PRIVATE *Private;
  712. struct bpf_hdr *BpfHeader;
  713. struct bpf_stat BpfStats;
  714. ETHERNET_HEADER *EnetHeader;
  715. ssize_t Result;
  716. Private = EMU_SNP_PRIVATE_DATA_FROM_THIS (This);
  717. if (Private->Mode->State < EfiSimpleNetworkStarted) {
  718. return EFI_NOT_STARTED;
  719. }
  720. ZeroMem (&BpfStats, sizeof (BpfStats));
  721. if (ioctl (Private->BpfFd, BIOCGSTATS, &BpfStats) == 0) {
  722. Private->ReceivedPackets += BpfStats.bs_recv;
  723. if (BpfStats.bs_drop > Private->DroppedPackets) {
  724. printf (
  725. "SNP: STATS: RCVD = %d DROPPED = %d. Probably need to increase BPF PcdNetworkPacketFilterSize?\n",
  726. BpfStats.bs_recv,
  727. BpfStats.bs_drop - Private->DroppedPackets
  728. );
  729. Private->DroppedPackets = BpfStats.bs_drop;
  730. }
  731. }
  732. //
  733. // Do we have any remaining packets from the previous read?
  734. //
  735. if (Private->CurrentReadPointer >= Private->EndReadPointer) {
  736. Result = read (Private->BpfFd, Private->ReadBuffer, Private->ReadBufferSize);
  737. if (Result < 0) {
  738. // EAGAIN means that there's no I/O outstanding against this file descriptor.
  739. return (errno == EAGAIN) ? EFI_NOT_READY : EFI_DEVICE_ERROR;
  740. }
  741. if (Result == 0) {
  742. return EFI_NOT_READY;
  743. }
  744. Private->CurrentReadPointer = Private->ReadBuffer;
  745. Private->EndReadPointer = Private->CurrentReadPointer + Result;
  746. }
  747. BpfHeader = Private->CurrentReadPointer;
  748. EnetHeader = Private->CurrentReadPointer + BpfHeader->bh_hdrlen;
  749. if (BpfHeader->bh_caplen > *BufferSize) {
  750. *BufferSize = BpfHeader->bh_caplen;
  751. return EFI_BUFFER_TOO_SMALL;
  752. }
  753. CopyMem (Buffer, EnetHeader, BpfHeader->bh_caplen);
  754. *BufferSize = BpfHeader->bh_caplen;
  755. if (HeaderSize != NULL) {
  756. *HeaderSize = sizeof (ETHERNET_HEADER);
  757. }
  758. if (DestAddr != NULL) {
  759. ZeroMem (DestAddr, sizeof (EFI_MAC_ADDRESS));
  760. CopyMem (DestAddr, EnetHeader->DstAddr, NET_ETHER_ADDR_LEN);
  761. }
  762. if (SrcAddr != NULL) {
  763. ZeroMem (SrcAddr, sizeof (EFI_MAC_ADDRESS));
  764. CopyMem (SrcAddr, EnetHeader->SrcAddr, NET_ETHER_ADDR_LEN);
  765. }
  766. if (Protocol != NULL) {
  767. *Protocol = NTOHS (EnetHeader->Type);
  768. }
  769. Private->CurrentReadPointer += BPF_WORDALIGN (BpfHeader->bh_hdrlen + BpfHeader->bh_caplen);
  770. return EFI_SUCCESS;
  771. }
  772. EMU_SNP_PROTOCOL gEmuSnpProtocol = {
  773. GasketSnpCreateMapping,
  774. GasketSnpStart,
  775. GasketSnpStop,
  776. GasketSnpInitialize,
  777. GasketSnpReset,
  778. GasketSnpShutdown,
  779. GasketSnpReceiveFilters,
  780. GasketSnpStationAddress,
  781. GasketSnpStatistics,
  782. GasketSnpMCastIpToMac,
  783. GasketSnpNvData,
  784. GasketSnpGetStatus,
  785. GasketSnpTransmit,
  786. GasketSnpReceive
  787. };
  788. EFI_STATUS
  789. GetInterfaceMacAddr (
  790. EMU_SNP_PRIVATE *Private
  791. )
  792. {
  793. EFI_STATUS Status;
  794. struct ifaddrs *IfAddrs;
  795. struct ifaddrs *If;
  796. struct sockaddr_dl *IfSdl;
  797. if (getifaddrs (&IfAddrs) != 0) {
  798. return EFI_UNSUPPORTED;
  799. }
  800. //
  801. // Convert the interface name to ASCII so we can find it.
  802. //
  803. Private->InterfaceName = malloc (StrSize (Private->Thunk->ConfigString));
  804. if (Private->InterfaceName == NULL) {
  805. Status = EFI_OUT_OF_RESOURCES;
  806. goto Exit;
  807. }
  808. UnicodeStrToAsciiStrS (
  809. Private->Thunk->ConfigString,
  810. Private->InterfaceName,
  811. StrSize (Private->Thunk->ConfigString)
  812. );
  813. Status = EFI_NOT_FOUND;
  814. If = IfAddrs;
  815. while (If != NULL) {
  816. IfSdl = (struct sockaddr_dl *)If->ifa_addr;
  817. if (IfSdl->sdl_family == AF_LINK) {
  818. if (!AsciiStrCmp (Private->InterfaceName, If->ifa_name)) {
  819. CopyMem (&Private->MacAddress, LLADDR (IfSdl), NET_ETHER_ADDR_LEN);
  820. Status = EFI_SUCCESS;
  821. break;
  822. }
  823. }
  824. If = If->ifa_next;
  825. }
  826. Exit:
  827. freeifaddrs (IfAddrs);
  828. return Status;
  829. }
  830. EFI_STATUS
  831. EmuSnpThunkOpen (
  832. IN EMU_IO_THUNK_PROTOCOL *This
  833. )
  834. {
  835. EMU_SNP_PRIVATE *Private;
  836. if (This->Private != NULL) {
  837. return EFI_ALREADY_STARTED;
  838. }
  839. if (!CompareGuid (This->Protocol, &gEmuSnpProtocolGuid)) {
  840. return EFI_UNSUPPORTED;
  841. }
  842. Private = malloc (sizeof (EMU_SNP_PRIVATE));
  843. if (Private == NULL) {
  844. return EFI_OUT_OF_RESOURCES;
  845. }
  846. Private->Signature = EMU_SNP_PRIVATE_SIGNATURE;
  847. Private->Thunk = This;
  848. CopyMem (&Private->EmuSnp, &gEmuSnpProtocol, sizeof (gEmuSnpProtocol));
  849. GetInterfaceMacAddr (Private);
  850. This->Interface = &Private->EmuSnp;
  851. This->Private = Private;
  852. return EFI_SUCCESS;
  853. }
  854. EFI_STATUS
  855. EmuSnpThunkClose (
  856. IN EMU_IO_THUNK_PROTOCOL *This
  857. )
  858. {
  859. EMU_SNP_PRIVATE *Private;
  860. if (!CompareGuid (This->Protocol, &gEmuSnpProtocolGuid)) {
  861. return EFI_UNSUPPORTED;
  862. }
  863. Private = This->Private;
  864. free (Private);
  865. return EFI_SUCCESS;
  866. }
  867. EMU_IO_THUNK_PROTOCOL gSnpThunkIo = {
  868. &gEmuSnpProtocolGuid,
  869. NULL,
  870. NULL,
  871. 0,
  872. GasketSnpThunkOpen,
  873. GasketSnpThunkClose,
  874. NULL
  875. };
  876. #endif