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