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