2 Berkeley Packet Filter implementation of the EMU_SNP_PROTOCOL that allows the
3 emulator to get on real networks.
7 Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
8 Portitions copyright (c) 2011, Apple Inc. All rights reserved.
10 SPDX-License-Identifier: BSD-2-Clause-Patent
20 #include <Library/NetLib.h>
23 #define EMU_SNP_PRIVATE_SIGNATURE SIGNATURE_32('E', 'M', 's', 'n')
27 EMU_IO_THUNK_PROTOCOL
*Thunk
;
28 EMU_SNP_PROTOCOL EmuSnp
;
29 EFI_SIMPLE_NETWORK_MODE
*Mode
;
33 EFI_MAC_ADDRESS MacAddress
;
38 // Two walking pointers to manage the multiple packets that can be returned
41 VOID
*CurrentReadPointer
;
44 UINT32 ReceivedPackets
;
45 UINT32 DroppedPackets
;
49 #define EMU_SNP_PRIVATE_DATA_FROM_THIS(a) \
50 CR(a, EMU_SNP_PRIVATE, EmuSnp, EMU_SNP_PRIVATE_SIGNATURE)
54 // Strange, but there doesn't appear to be any structure for the Ethernet header in edk2...
58 UINT8 DstAddr
[NET_ETHER_ADDR_LEN
];
59 UINT8 SrcAddr
[NET_ETHER_ADDR_LEN
];
64 Register storage for SNP Mode.
66 @param This Protocol instance pointer.
67 @param Mode SimpleNetworkProtocol Mode structure passed into driver.
69 @retval EFI_SUCCESS The network interface was started.
70 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
75 IN EMU_SNP_PROTOCOL
*This
,
76 IN EFI_SIMPLE_NETWORK_MODE
*Mode
79 EMU_SNP_PRIVATE
*Private
;
81 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
86 // Set the broadcast address.
88 SetMem (&Mode
->BroadcastAddress
, sizeof (EFI_MAC_ADDRESS
), 0xFF);
90 CopyMem (&Mode
->CurrentAddress
, &Private
->MacAddress
, sizeof (EFI_MAC_ADDRESS
));
91 CopyMem (&Mode
->PermanentAddress
, &Private
->MacAddress
, sizeof (EFI_MAC_ADDRESS
));
94 // Since the fake SNP is based on a real NIC, to avoid conflict with the host NIC
95 // network stack, we use a different MAC address.
96 // So just change the last byte of the MAC address for the real NIC.
98 Mode
->CurrentAddress
.Addr
[NET_ETHER_ADDR_LEN
- 1]++;
104 static struct bpf_insn mFilterInstructionTemplate
[] = {
105 // Load 4 bytes from the destination MAC address.
106 BPF_STMT (BPF_LD
+ BPF_W
+ BPF_ABS
, OFFSET_OF (ETHERNET_HEADER
, DstAddr
[0])),
108 // Compare to first 4 bytes of fake MAC address.
109 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x12345678, 0, 3 ),
111 // Load remaining 2 bytes from the destination MAC address.
112 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_ABS
, OFFSET_OF( ETHERNET_HEADER
, DstAddr
[4])),
114 // Compare to remaining 2 bytes of fake MAC address.
115 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x9ABC, 5, 0 ),
117 // Load 4 bytes from the destination MAC address.
118 BPF_STMT (BPF_LD
+ BPF_W
+ BPF_ABS
, OFFSET_OF (ETHERNET_HEADER
, DstAddr
[0])),
120 // Compare to first 4 bytes of broadcast MAC address.
121 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0xFFFFFFFF, 0, 2),
123 // Load remaining 2 bytes from the destination MAC address.
124 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_ABS
, OFFSET_OF( ETHERNET_HEADER
, DstAddr
[4])),
126 // Compare to remaining 2 bytes of broadcast MAC address.
127 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0xFFFF, 1, 0),
130 BPF_STMT (BPF_RET
+ BPF_K
, 0),
132 // Receive entire packet.
133 BPF_STMT (BPF_RET
+ BPF_K
, -1)
138 OpenBpfFileDescriptor (
139 IN EMU_SNP_PRIVATE
*Private
,
143 char BfpDeviceName
[256];
147 // Open a Berkeley Packet Filter device. This must be done as root, so this is probably
148 // the place which is most likely to fail...
150 for (Index
= 0; TRUE
; Index
++ ) {
151 snprintf (BfpDeviceName
, sizeof (BfpDeviceName
), "/dev/bpf%d", Index
);
153 *Fd
= open (BfpDeviceName
, O_RDWR
, 0);
158 if (errno
== EACCES
) {
160 "SNP: Permissions on '%s' are incorrect. Fix with 'sudo chmod 666 %s'.\n",
166 if (errno
!= EBUSY
) {
171 return EFI_OUT_OF_RESOURCES
;
176 Changes the state of a network interface from "stopped" to "started".
178 @param This Protocol instance pointer.
180 @retval EFI_SUCCESS The network interface was started.
181 @retval EFI_ALREADY_STARTED The network interface is already in the started state.
182 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
183 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
184 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
189 IN EMU_SNP_PROTOCOL
*This
193 EMU_SNP_PRIVATE
*Private
;
194 struct ifreq BoundIf
;
195 struct bpf_program BpfProgram
;
196 struct bpf_insn
*FilterProgram
;
198 u_int ReadBufferSize
;
202 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
204 switch (Private
->Mode
->State
) {
205 case EfiSimpleNetworkStopped
:
208 case EfiSimpleNetworkStarted
:
209 case EfiSimpleNetworkInitialized
:
210 return EFI_ALREADY_STARTED
;
214 return EFI_DEVICE_ERROR
;
218 Status
= EFI_SUCCESS
;
219 if (Private
->BpfFd
== 0) {
220 Status
= OpenBpfFileDescriptor (Private
, &Private
->BpfFd
);
221 if (EFI_ERROR (Status
)) {
222 goto DeviceErrorExit
;
226 // Get the read buffer size.
228 if (ioctl (Private
->BpfFd
, BIOCGBLEN
, &ReadBufferSize
) < 0) {
229 goto DeviceErrorExit
;
233 // Default value from BIOCGBLEN is usually too small, so use a much larger size, if necessary.
235 if (ReadBufferSize
< FixedPcdGet32 (PcdNetworkPacketFilterSize
)) {
236 ReadBufferSize
= FixedPcdGet32 (PcdNetworkPacketFilterSize
);
237 if (ioctl (Private
->BpfFd
, BIOCSBLEN
, &ReadBufferSize
) < 0) {
238 goto DeviceErrorExit
;
243 // Associate our interface with this BPF file descriptor.
245 AsciiStrCpy (BoundIf
.ifr_name
, Private
->InterfaceName
);
246 if (ioctl (Private
->BpfFd
, BIOCSETIF
, &BoundIf
) < 0) {
247 goto DeviceErrorExit
;
251 // Enable immediate mode.
254 if (ioctl (Private
->BpfFd
, BIOCIMMEDIATE
, &Value
) < 0) {
255 goto DeviceErrorExit
;
259 // Enable non-blocking I/O.
261 if (fcntl (Private
->BpfFd
, F_GETFL
, 0) == -1) {
262 goto DeviceErrorExit
;
267 if (fcntl (Private
->BpfFd
, F_SETFL
, Value
) == -1) {
268 goto DeviceErrorExit
;
272 // Disable "header complete" flag. This means the supplied source MAC address is
273 // what goes on the wire.
276 if (ioctl (Private
->BpfFd
, BIOCSHDRCMPLT
, &Value
) < 0) {
277 goto DeviceErrorExit
;
281 // Allocate read buffer.
283 Private
->ReadBufferSize
= ReadBufferSize
;
284 Private
->ReadBuffer
= malloc (Private
->ReadBufferSize
);
285 if (Private
->ReadBuffer
== NULL
) {
289 Private
->CurrentReadPointer
= Private
->EndReadPointer
= Private
->ReadBuffer
;
292 // Install our packet filter: successful reads should only produce broadcast or unicast
293 // packets directed to our fake MAC address.
295 FilterProgram
= malloc (sizeof (mFilterInstructionTemplate
)) ;
296 if ( FilterProgram
== NULL
) {
300 CopyMem (FilterProgram
, &mFilterInstructionTemplate
, sizeof (mFilterInstructionTemplate
));
303 // Insert out fake MAC address into the filter. The data has to be host endian.
305 CopyMem (&Temp32
, &Private
->Mode
->CurrentAddress
.Addr
[0], sizeof (UINT32
));
306 FilterProgram
[1].k
= NTOHL (Temp32
);
307 CopyMem (&Temp16
, &Private
->Mode
->CurrentAddress
.Addr
[4], sizeof (UINT16
));
308 FilterProgram
[3].k
= NTOHS (Temp16
);
310 BpfProgram
.bf_len
= sizeof (mFilterInstructionTemplate
) / sizeof (struct bpf_insn
);
311 BpfProgram
.bf_insns
= FilterProgram
;
313 if (ioctl (Private
->BpfFd
, BIOCSETF
, &BpfProgram
) < 0) {
314 goto DeviceErrorExit
;
317 free (FilterProgram
);
320 // Enable promiscuous mode.
322 if (ioctl (Private
->BpfFd
, BIOCPROMISC
, 0) < 0) {
323 goto DeviceErrorExit
;
327 Private
->Mode
->State
= EfiSimpleNetworkStarted
;
333 Status
= EFI_DEVICE_ERROR
;
335 if (Private
->ReadBuffer
!= NULL
) {
336 free (Private
->ReadBuffer
);
337 Private
->ReadBuffer
= NULL
;
344 Changes the state of a network interface from "started" to "stopped".
346 @param This Protocol instance pointer.
348 @retval EFI_SUCCESS The network interface was stopped.
349 @retval EFI_ALREADY_STARTED The network interface is already in the stopped state.
350 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
351 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
352 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
357 IN EMU_SNP_PROTOCOL
*This
360 EMU_SNP_PRIVATE
*Private
;
362 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
364 switch ( Private
->Mode
->State
) {
365 case EfiSimpleNetworkStarted
:
368 case EfiSimpleNetworkStopped
:
369 return EFI_NOT_STARTED
;
373 return EFI_DEVICE_ERROR
;
377 if (Private
->BpfFd
!= 0) {
378 close (Private
->BpfFd
);
382 if (Private
->ReadBuffer
!= NULL
) {
383 free (Private
->ReadBuffer
);
384 Private
->CurrentReadPointer
= Private
->EndReadPointer
= Private
->ReadBuffer
= NULL
;
387 Private
->Mode
->State
= EfiSimpleNetworkStopped
;
394 Resets a network adapter and allocates the transmit and receive buffers
395 required by the network interface; optionally, also requests allocation
396 of additional transmit and receive buffers.
398 @param This The protocol instance pointer.
399 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
400 that the driver should allocate for the network interface.
401 Some network interfaces will not be able to use the extra
402 buffer, and the caller will not know if it is actually
404 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
405 that the driver should allocate for the network interface.
406 Some network interfaces will not be able to use the extra
407 buffer, and the caller will not know if it is actually
410 @retval EFI_SUCCESS The network interface was initialized.
411 @retval EFI_NOT_STARTED The network interface has not been started.
412 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and
414 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
415 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
416 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
421 IN EMU_SNP_PROTOCOL
*This
,
422 IN UINTN ExtraRxBufferSize OPTIONAL
,
423 IN UINTN ExtraTxBufferSize OPTIONAL
426 EMU_SNP_PRIVATE
*Private
;
428 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
430 switch ( Private
->Mode
->State
) {
431 case EfiSimpleNetworkStarted
:
434 case EfiSimpleNetworkStopped
:
435 return EFI_NOT_STARTED
;
439 return EFI_DEVICE_ERROR
;
443 Private
->Mode
->MCastFilterCount
= 0;
444 Private
->Mode
->ReceiveFilterSetting
= 0;
445 ZeroMem (Private
->Mode
->MCastFilter
, sizeof (Private
->Mode
->MCastFilter
));
447 Private
->Mode
->State
= EfiSimpleNetworkInitialized
;
454 Resets a network adapter and re-initializes it with the parameters that were
455 provided in the previous call to Initialize().
457 @param This The protocol instance pointer.
458 @param ExtendedVerification Indicates that the driver may perform a more
459 exhaustive verification operation of the device
462 @retval EFI_SUCCESS The network interface was reset.
463 @retval EFI_NOT_STARTED The network interface has not been started.
464 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
465 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
466 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
471 IN EMU_SNP_PROTOCOL
*This
,
472 IN BOOLEAN ExtendedVerification
475 EMU_SNP_PRIVATE
*Private
;
477 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
479 switch ( Private
->Mode
->State
) {
480 case EfiSimpleNetworkInitialized
:
483 case EfiSimpleNetworkStopped
:
484 return EFI_NOT_STARTED
;
488 return EFI_DEVICE_ERROR
;
497 Resets a network adapter and leaves it in a state that is safe for
498 another driver to initialize.
500 @param This Protocol instance pointer.
502 @retval EFI_SUCCESS The network interface was shutdown.
503 @retval EFI_NOT_STARTED The network interface has not been started.
504 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
505 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
506 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
511 IN EMU_SNP_PROTOCOL
*This
514 EMU_SNP_PRIVATE
*Private
;
516 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
518 switch ( Private
->Mode
->State
) {
519 case EfiSimpleNetworkInitialized
:
522 case EfiSimpleNetworkStopped
:
523 return EFI_NOT_STARTED
;
527 return EFI_DEVICE_ERROR
;
531 Private
->Mode
->State
= EfiSimpleNetworkStarted
;
533 Private
->Mode
->ReceiveFilterSetting
= 0;
534 Private
->Mode
->MCastFilterCount
= 0;
535 ZeroMem (Private
->Mode
->MCastFilter
, sizeof (Private
->Mode
->MCastFilter
));
537 if (Private
->BpfFd
!= 0) {
538 close (Private
->BpfFd
);
542 if (Private
->ReadBuffer
!= NULL
) {
543 free (Private
->ReadBuffer
);
544 Private
->CurrentReadPointer
= Private
->EndReadPointer
= Private
->ReadBuffer
= NULL
;
551 Manages the multicast receive filters of a network interface.
553 @param This The protocol instance pointer.
554 @param Enable A bit mask of receive filters to enable on the network interface.
555 @param Disable A bit mask of receive filters to disable on the network interface.
556 @param ResetMCastFilter Set to TRUE to reset the contents of the multicast receive
557 filters on the network interface to their default values.
558 @param McastFilterCnt Number of multicast HW MAC addresses in the new
559 MCastFilter list. This value must be less than or equal to
560 the MCastFilterCnt field of EMU_SNP_MODE. This
561 field is optional if ResetMCastFilter is TRUE.
562 @param MCastFilter A pointer to a list of new multicast receive filter HW MAC
563 addresses. This list will replace any existing multicast
564 HW MAC address list. This field is optional if
565 ResetMCastFilter is TRUE.
567 @retval EFI_SUCCESS The multicast receive filter list was updated.
568 @retval EFI_NOT_STARTED The network interface has not been started.
569 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
570 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
571 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
575 EmuSnpReceiveFilters (
576 IN EMU_SNP_PROTOCOL
*This
,
579 IN BOOLEAN ResetMCastFilter
,
580 IN UINTN MCastFilterCnt OPTIONAL
,
581 IN EFI_MAC_ADDRESS
*MCastFilter OPTIONAL
584 EMU_SNP_PRIVATE
*Private
;
586 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
588 // For now, just succeed...
594 Modifies or resets the current station address, if supported.
596 @param This The protocol instance pointer.
597 @param Reset Flag used to reset the station address to the network interfaces
599 @param New The new station address to be used for the network interface.
601 @retval EFI_SUCCESS The network interfaces station address was updated.
602 @retval EFI_NOT_STARTED The network interface has not been started.
603 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
604 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
605 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
609 EmuSnpStationAddress (
610 IN EMU_SNP_PROTOCOL
*This
,
612 IN EFI_MAC_ADDRESS
*New OPTIONAL
615 EMU_SNP_PRIVATE
*Private
;
617 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
619 return EFI_UNSUPPORTED
;
624 Resets or collects the statistics on a network interface.
626 @param This Protocol instance pointer.
627 @param Reset Set to TRUE to reset the statistics for the network interface.
628 @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
629 output the size, in bytes, of the resulting table of
631 @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
632 contains the statistics.
634 @retval EFI_SUCCESS The statistics were collected from the network interface.
635 @retval EFI_NOT_STARTED The network interface has not been started.
636 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
637 size needed to hold the statistics is returned in
639 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
640 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
641 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
646 IN EMU_SNP_PROTOCOL
*This
,
648 IN OUT UINTN
*StatisticsSize OPTIONAL
,
649 OUT EFI_NETWORK_STATISTICS
*StatisticsTable OPTIONAL
652 EMU_SNP_PRIVATE
*Private
;
654 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
656 return EFI_UNSUPPORTED
;
661 Converts a multicast IP address to a multicast HW MAC address.
663 @param This The protocol instance pointer.
664 @param IPv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
665 to FALSE if the multicast IP address is IPv4 [RFC 791].
666 @param IP The multicast IP address that is to be converted to a multicast
668 @param MAC The multicast HW MAC address that is to be generated from IP.
670 @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
672 @retval EFI_NOT_STARTED The network interface has not been started.
673 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
674 size needed to hold the statistics is returned in
676 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
677 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
678 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
683 IN EMU_SNP_PROTOCOL
*This
,
685 IN EFI_IP_ADDRESS
*IP
,
686 OUT EFI_MAC_ADDRESS
*MAC
689 EMU_SNP_PRIVATE
*Private
;
691 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
693 return EFI_UNSUPPORTED
;
698 Performs read and write operations on the NVRAM device attached to a
701 @param This The protocol instance pointer.
702 @param ReadWrite TRUE for read operations, FALSE for write operations.
703 @param Offset Byte offset in the NVRAM device at which to start the read or
704 write operation. This must be a multiple of NvRamAccessSize and
706 @param BufferSize The number of bytes to read or write from the NVRAM device.
707 This must also be a multiple of NvramAccessSize.
708 @param Buffer A pointer to the data buffer.
710 @retval EFI_SUCCESS The NVRAM access was performed.
711 @retval EFI_NOT_STARTED The network interface has not been started.
712 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
713 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
714 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
719 IN EMU_SNP_PROTOCOL
*This
,
720 IN BOOLEAN ReadWrite
,
726 EMU_SNP_PRIVATE
*Private
;
728 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
730 return EFI_UNSUPPORTED
;
734 Reads the current interrupt status and recycled transmit buffer status from
737 @param This The protocol instance pointer.
738 @param InterruptStatus A pointer to the bit mask of the currently active interrupts
739 If this is NULL, the interrupt status will not be read from
740 the device. If this is not NULL, the interrupt status will
741 be read from the device. When the interrupt status is read,
742 it will also be cleared. Clearing the transmit interrupt
743 does not empty the recycled transmit buffer array.
744 @param TxBuf Recycled transmit buffer address. The network interface will
745 not transmit if its internal recycled transmit buffer array
746 is full. Reading the transmit buffer does not clear the
747 transmit interrupt. If this is NULL, then the transmit buffer
748 status will not be read. If there are no transmit buffers to
749 recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
751 @retval EFI_SUCCESS The status of the network interface was retrieved.
752 @retval EFI_NOT_STARTED The network interface has not been started.
753 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
754 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
755 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
760 IN EMU_SNP_PROTOCOL
*This
,
761 OUT UINT32
*InterruptStatus OPTIONAL
,
762 OUT VOID
**TxBuf OPTIONAL
765 EMU_SNP_PRIVATE
*Private
;
767 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
770 *((UINT8
**)TxBuf
) = (UINT8
*)1;
773 if ( InterruptStatus
!= NULL
) {
774 *InterruptStatus
= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT
;
782 Places a packet in the transmit queue of a network interface.
784 @param This The protocol instance pointer.
785 @param HeaderSize The size, in bytes, of the media header to be filled in by
786 the Transmit() function. If HeaderSize is non-zero, then it
787 must be equal to This->Mode->MediaHeaderSize and the DestAddr
788 and Protocol parameters must not be NULL.
789 @param BufferSize The size, in bytes, of the entire packet (media header and
790 data) to be transmitted through the network interface.
791 @param Buffer A pointer to the packet (media header followed by data) to be
792 transmitted. This parameter cannot be NULL. If HeaderSize is zero,
793 then the media header in Buffer must already be filled in by the
794 caller. If HeaderSize is non-zero, then the media header will be
795 filled in by the Transmit() function.
796 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
797 is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
798 This->Mode->CurrentAddress is used for the source HW MAC address.
799 @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
800 parameter is ignored.
801 @param Protocol The type of header to build. If HeaderSize is zero, then this
802 parameter is ignored. See RFC 1700, section "Ether Types", for
805 @retval EFI_SUCCESS The packet was placed on the transmit queue.
806 @retval EFI_NOT_STARTED The network interface has not been started.
807 @retval EFI_NOT_READY The network interface is too busy to accept this transmit request.
808 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
809 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
810 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
811 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
816 IN EMU_SNP_PROTOCOL
*This
,
820 IN EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
821 IN EFI_MAC_ADDRESS
*DestAddr OPTIONAL
,
822 IN UINT16
*Protocol OPTIONAL
825 EMU_SNP_PRIVATE
*Private
;
826 ETHERNET_HEADER
*EnetHeader
;
828 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
830 if (Private
->Mode
->State
< EfiSimpleNetworkStarted
) {
831 return EFI_NOT_STARTED
;
834 if ( HeaderSize
!= 0 ) {
835 if ((DestAddr
== NULL
) || (Protocol
== NULL
) || (HeaderSize
!= Private
->Mode
->MediaHeaderSize
)) {
836 return EFI_INVALID_PARAMETER
;
839 if (SrcAddr
== NULL
) {
840 SrcAddr
= &Private
->Mode
->CurrentAddress
;
843 EnetHeader
= (ETHERNET_HEADER
*) Buffer
;
845 CopyMem (EnetHeader
->DstAddr
, DestAddr
, NET_ETHER_ADDR_LEN
);
846 CopyMem (EnetHeader
->SrcAddr
, SrcAddr
, NET_ETHER_ADDR_LEN
);
848 EnetHeader
->Type
= HTONS(*Protocol
);
851 if (write (Private
->BpfFd
, Buffer
, BufferSize
) < 0) {
852 return EFI_DEVICE_ERROR
;
859 Receives a packet from a network interface.
861 @param This The protocol instance pointer.
862 @param HeaderSize The size, in bytes, of the media header received on the network
863 interface. If this parameter is NULL, then the media header size
864 will not be returned.
865 @param BufferSize On entry, the size, in bytes, of Buffer. On exit, the size, in
866 bytes, of the packet that was received on the network interface.
867 @param Buffer A pointer to the data buffer to receive both the media header and
869 @param SrcAddr The source HW MAC address. If this parameter is NULL, the
870 HW MAC source address will not be extracted from the media
872 @param DestAddr The destination HW MAC address. If this parameter is NULL,
873 the HW MAC destination address will not be extracted from the
875 @param Protocol The media header type. If this parameter is NULL, then the
876 protocol will not be extracted from the media header. See
877 RFC 1700 section "Ether Types" for examples.
879 @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
880 been updated to the number of bytes received.
881 @retval EFI_NOT_STARTED The network interface has not been started.
882 @retval EFI_NOT_READY The network interface is too busy to accept this transmit
884 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
885 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
886 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
887 @retval EFI_UNSUPPORTED This function is not supported by the network interface.
892 IN EMU_SNP_PROTOCOL
*This
,
893 OUT UINTN
*HeaderSize OPTIONAL
,
894 IN OUT UINTN
*BufferSize
,
896 OUT EFI_MAC_ADDRESS
*SrcAddr OPTIONAL
,
897 OUT EFI_MAC_ADDRESS
*DestAddr OPTIONAL
,
898 OUT UINT16
*Protocol OPTIONAL
901 EMU_SNP_PRIVATE
*Private
;
902 struct bpf_hdr
*BpfHeader
;
903 struct bpf_stat BpfStats
;
904 ETHERNET_HEADER
*EnetHeader
;
907 Private
= EMU_SNP_PRIVATE_DATA_FROM_THIS (This
);
909 if (Private
->Mode
->State
< EfiSimpleNetworkStarted
) {
910 return EFI_NOT_STARTED
;
913 ZeroMem (&BpfStats
, sizeof( BpfStats
));
915 if (ioctl (Private
->BpfFd
, BIOCGSTATS
, &BpfStats
) == 0) {
916 Private
->ReceivedPackets
+= BpfStats
.bs_recv
;
917 if (BpfStats
.bs_drop
> Private
->DroppedPackets
) {
919 "SNP: STATS: RCVD = %d DROPPED = %d. Probably need to increase BPF PcdNetworkPacketFilterSize?\n",
921 BpfStats
.bs_drop
- Private
->DroppedPackets
923 Private
->DroppedPackets
= BpfStats
.bs_drop
;
928 // Do we have any remaining packets from the previous read?
930 if (Private
->CurrentReadPointer
>= Private
->EndReadPointer
) {
931 Result
= read (Private
->BpfFd
, Private
->ReadBuffer
, Private
->ReadBufferSize
);
933 // EAGAIN means that there's no I/O outstanding against this file descriptor.
934 return (errno
== EAGAIN
) ? EFI_NOT_READY
: EFI_DEVICE_ERROR
;
938 return EFI_NOT_READY
;
941 Private
->CurrentReadPointer
= Private
->ReadBuffer
;
942 Private
->EndReadPointer
= Private
->CurrentReadPointer
+ Result
;
945 BpfHeader
= Private
->CurrentReadPointer
;
946 EnetHeader
= Private
->CurrentReadPointer
+ BpfHeader
->bh_hdrlen
;
948 if (BpfHeader
->bh_caplen
> *BufferSize
) {
949 *BufferSize
= BpfHeader
->bh_caplen
;
950 return EFI_BUFFER_TOO_SMALL
;
953 CopyMem (Buffer
, EnetHeader
, BpfHeader
->bh_caplen
);
954 *BufferSize
= BpfHeader
->bh_caplen
;
956 if (HeaderSize
!= NULL
) {
957 *HeaderSize
= sizeof (ETHERNET_HEADER
);
960 if (DestAddr
!= NULL
) {
961 ZeroMem (DestAddr
, sizeof (EFI_MAC_ADDRESS
));
962 CopyMem (DestAddr
, EnetHeader
->DstAddr
, NET_ETHER_ADDR_LEN
);
965 if (SrcAddr
!= NULL
) {
966 ZeroMem (SrcAddr
, sizeof (EFI_MAC_ADDRESS
));
967 CopyMem (SrcAddr
, EnetHeader
->SrcAddr
, NET_ETHER_ADDR_LEN
);
970 if (Protocol
!= NULL
) {
971 *Protocol
= NTOHS (EnetHeader
->Type
);
974 Private
->CurrentReadPointer
+= BPF_WORDALIGN (BpfHeader
->bh_hdrlen
+ BpfHeader
->bh_caplen
);
979 EMU_SNP_PROTOCOL gEmuSnpProtocol
= {
980 GasketSnpCreateMapping
,
986 GasketSnpReceiveFilters
,
987 GasketSnpStationAddress
,
989 GasketSnpMCastIpToMac
,
997 GetInterfaceMacAddr (
998 EMU_SNP_PRIVATE
*Private
1002 struct ifaddrs
*IfAddrs
;
1004 struct sockaddr_dl
*IfSdl
;
1006 if (getifaddrs (&IfAddrs
) != 0) {
1007 return EFI_UNSUPPORTED
;
1011 // Convert the interface name to ASCII so we can find it.
1013 Private
->InterfaceName
= malloc (StrSize (Private
->Thunk
->ConfigString
));
1014 if (Private
->InterfaceName
== NULL
) {
1015 Status
= EFI_OUT_OF_RESOURCES
;
1019 UnicodeStrToAsciiStr (Private
->Thunk
->ConfigString
, Private
->InterfaceName
);
1021 Status
= EFI_NOT_FOUND
;
1023 while (If
!= NULL
) {
1024 IfSdl
= (struct sockaddr_dl
*)If
->ifa_addr
;
1026 if (IfSdl
->sdl_family
== AF_LINK
) {
1027 if (!AsciiStrCmp( Private
->InterfaceName
, If
->ifa_name
)) {
1028 CopyMem (&Private
->MacAddress
, LLADDR (IfSdl
), NET_ETHER_ADDR_LEN
);
1030 Status
= EFI_SUCCESS
;
1039 freeifaddrs (IfAddrs
);
1046 IN EMU_IO_THUNK_PROTOCOL
*This
1049 EMU_SNP_PRIVATE
*Private
;
1051 if (This
->Private
!= NULL
) {
1052 return EFI_ALREADY_STARTED
;
1055 if (!CompareGuid (This
->Protocol
, &gEmuSnpProtocolGuid
)) {
1056 return EFI_UNSUPPORTED
;
1059 Private
= malloc (sizeof (EMU_SNP_PRIVATE
));
1060 if (Private
== NULL
) {
1061 return EFI_OUT_OF_RESOURCES
;
1065 Private
->Signature
= EMU_SNP_PRIVATE_SIGNATURE
;
1066 Private
->Thunk
= This
;
1067 CopyMem (&Private
->EmuSnp
, &gEmuSnpProtocol
, sizeof (gEmuSnpProtocol
));
1068 GetInterfaceMacAddr (Private
);
1070 This
->Interface
= &Private
->EmuSnp
;
1071 This
->Private
= Private
;
1078 IN EMU_IO_THUNK_PROTOCOL
*This
1081 EMU_SNP_PRIVATE
*Private
;
1083 if (!CompareGuid (This
->Protocol
, &gEmuSnpProtocolGuid
)) {
1084 return EFI_UNSUPPORTED
;
1087 Private
= This
->Private
;
1095 EMU_IO_THUNK_PROTOCOL gSnpThunkIo
= {
1096 &gEmuSnpProtocolGuid
,
1101 GasketSnpThunkClose
,