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