3 Copyright (c) 2006 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
24 EFI_ARP_PROTOCOL mEfiArpProtocolTemplate
= {
36 Initialize the instance context data.
38 @param ArpService Pointer to the arp service context data this
40 @param Instance Pointer to the instance context data.
47 IN ARP_SERVICE_DATA
*ArpService
,
48 IN ARP_INSTANCE_DATA
*Instance
51 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
53 Instance
->Signature
= ARP_INSTANCE_DATA_SIGNATURE
;
54 Instance
->ArpService
= ArpService
;
56 CopyMem (&Instance
->ArpProto
, &mEfiArpProtocolTemplate
, sizeof (Instance
->ArpProto
));
58 Instance
->Configured
= FALSE
;
59 Instance
->Destroyed
= FALSE
;
61 InitializeListHead (&Instance
->List
);
66 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
68 @param Context Pointer to the context data registerd to the
81 ARP_SERVICE_DATA
*ArpService
;
82 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*RxToken
;
83 EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
;
85 ARP_ADDRESS ArpAddress
;
86 ARP_CACHE_ENTRY
*CacheEntry
;
88 ARP_INSTANCE_DATA
*Instance
;
89 EFI_ARP_CONFIG_DATA
*ConfigData
;
90 NET_ARP_ADDRESS SenderAddress
[2];
95 ArpService
= (ARP_SERVICE_DATA
*)Context
;
96 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
98 RxToken
= &ArpService
->RxToken
;
100 if (RxToken
->Status
== EFI_ABORTED
) {
102 // The Token is aborted, possibly by arp itself, just return and the receiving
103 // process is stopped.
108 if (EFI_ERROR (RxToken
->Status
)) {
110 // Restart the receiving if any other error Status occurs.
112 goto RESTART_RECEIVE
;
116 // Status is EFI_SUCCESS, process the received frame.
118 RxData
= RxToken
->Packet
.RxData
;
119 Head
= (ARP_HEAD
*) RxData
->PacketData
;
122 // Convert the byte order of the multi-byte fields.
124 Head
->HwType
= NTOHS (Head
->HwType
);
125 Head
->ProtoType
= NTOHS (Head
->ProtoType
);
126 Head
->OpCode
= NTOHS (Head
->OpCode
);
128 if ((Head
->HwType
!= ArpService
->SnpMode
.IfType
) ||
129 (Head
->HwAddrLen
!= ArpService
->SnpMode
.HwAddressSize
) ||
130 (RxData
->ProtocolType
!= ARP_ETHER_PROTO_TYPE
)) {
132 // The hardware type or the hardware address length doesn't match.
133 // There is a sanity check for the protocol type too.
139 // Set the pointers to the addresses contained in the arp packet.
141 ArpAddress
.SenderHwAddr
= (UINT8
*)(Head
+ 1);
142 ArpAddress
.SenderProtoAddr
= ArpAddress
.SenderHwAddr
+ Head
->HwAddrLen
;
143 ArpAddress
.TargetHwAddr
= ArpAddress
.SenderProtoAddr
+ Head
->ProtoAddrLen
;
144 ArpAddress
.TargetProtoAddr
= ArpAddress
.TargetHwAddr
+ Head
->HwAddrLen
;
146 SenderAddress
[Hardware
].Type
= Head
->HwType
;
147 SenderAddress
[Hardware
].Length
= Head
->HwAddrLen
;
148 SenderAddress
[Hardware
].AddressPtr
= ArpAddress
.SenderHwAddr
;
150 SenderAddress
[Protocol
].Type
= Head
->ProtoType
;
151 SenderAddress
[Protocol
].Length
= Head
->ProtoAddrLen
;
152 SenderAddress
[Protocol
].AddressPtr
= ArpAddress
.SenderProtoAddr
;
155 // First, check the denied cache table.
157 CacheEntry
= ArpFindDeniedCacheEntry (
159 &SenderAddress
[Protocol
],
160 &SenderAddress
[Hardware
]
162 if (CacheEntry
!= NULL
) {
164 // This address (either hardware or protocol address, or both) is configured to
165 // be a deny entry, silently skip the normal process.
170 ProtoMatched
= FALSE
;
173 NET_LIST_FOR_EACH (Entry
, &ArpService
->ChildrenList
) {
175 // Iterate all the children.
177 Instance
= NET_LIST_USER_STRUCT (Entry
, ARP_INSTANCE_DATA
, List
);
178 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
179 ConfigData
= &Instance
->ConfigData
;
181 if ((Instance
->Configured
) &&
182 (Head
->ProtoType
== ConfigData
->SwAddressType
) &&
183 (Head
->ProtoAddrLen
== ConfigData
->SwAddressLength
)) {
185 // The protocol type is matched for the received arp packet.
188 if (0 == CompareMem (
189 (VOID
*)ArpAddress
.TargetProtoAddr
,
190 ConfigData
->StationAddress
,
191 ConfigData
->SwAddressLength
194 // The arp driver has the target address required by the received arp packet.
204 // Protocol type unmatchable, skip.
210 // Check whether the sender's address information is already in the cache.
213 CacheEntry
= ArpFindNextCacheEntryInTable (
214 &ArpService
->ResolvedCacheTable
,
217 &SenderAddress
[Protocol
],
220 if (CacheEntry
!= NULL
) {
222 // Update the entry with the new information.
224 ArpFillAddressInCacheEntry (CacheEntry
, &SenderAddress
[Hardware
], NULL
);
225 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
231 // This arp packet isn't targeted to us, skip now.
238 // Add the triplet <protocol type, sender protocol address, sender hardware address>
239 // to the translation table.
241 CacheEntry
= ArpFindNextCacheEntryInTable (
242 &ArpService
->PendingRequestTable
,
245 &SenderAddress
[Protocol
],
248 if (CacheEntry
== NULL
) {
250 // Allocate a new CacheEntry.
252 CacheEntry
= ArpAllocCacheEntry (NULL
);
253 if (CacheEntry
== NULL
) {
258 if (!IsListEmpty (&CacheEntry
->List
)) {
259 RemoveEntryList (&CacheEntry
->List
);
263 // Fill the addresses into the CacheEntry.
265 ArpFillAddressInCacheEntry (
267 &SenderAddress
[Hardware
],
268 &SenderAddress
[Protocol
]
274 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
277 // Add this entry into the ResolvedCacheTable
279 InsertHeadList (&ArpService
->ResolvedCacheTable
, &CacheEntry
->List
);
282 if (Head
->OpCode
== ARP_OPCODE_REQUEST
) {
284 // Send back the ARP Reply. If we reach here, Instance is not NULL and CacheEntry
287 ArpSendFrame (Instance
, CacheEntry
, ARP_OPCODE_REPLY
);
293 // Signal Mnp to recycle the RxData.
295 gBS
->SignalEvent (RxData
->RecycleEvent
);
300 // Continue to receive packets from Mnp.
302 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, RxToken
);
305 if (EFI_ERROR (Status
)) {
306 DEBUG ((EFI_D_ERROR
, "ArpOnFrameRcvd: ArpService->Mnp->Receive "
307 "failed, %r\n.", Status
));
313 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
315 @param Event The Event this notify function registered to.
316 @param Context Pointer to the context data registerd to the
330 // Request ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK
332 NetLibQueueDpc (TPL_CALLBACK
, ArpOnFrameRcvdDpc
, Context
);
336 Process the already sent arp packets.
338 @param Context Pointer to the context data registerd to the
350 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
351 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
353 ASSERT (Context
!= NULL
);
355 TxToken
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*)Context
;
356 TxData
= TxToken
->Packet
.TxData
;
359 if (EFI_ERROR (TxToken
->Status
)) {
360 DEBUG ((EFI_D_ERROR
, "ArpOnFrameSent: TxToken->Status, %r.\n", TxToken
->Status
));
365 // Free the allocated memory and close the event.
367 gBS
->FreePool (TxData
->FragmentTable
[0].FragmentBuffer
);
368 gBS
->FreePool (TxData
);
369 gBS
->CloseEvent (TxToken
->Event
);
370 gBS
->FreePool (TxToken
);
374 Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK.
376 @param Event The Event this notify function registered to.
377 @param Context Pointer to the context data registerd to the
391 // Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK
393 NetLibQueueDpc (TPL_CALLBACK
, ArpOnFrameSentDpc
, Context
);
398 Process the arp cache olding and drive the retrying arp requests.
400 @param Event The Event this notify function registered to.
401 @param Context Pointer to the context data registerd to the
414 ARP_SERVICE_DATA
*ArpService
;
416 LIST_ENTRY
*NextEntry
;
417 LIST_ENTRY
*ContextEntry
;
418 ARP_CACHE_ENTRY
*CacheEntry
;
419 USER_REQUEST_CONTEXT
*RequestContext
;
421 ASSERT (Context
!= NULL
);
422 ArpService
= (ARP_SERVICE_DATA
*)Context
;
425 // Iterate all the pending requests to see whether a retry is needed to send out
426 // or the request finally fails because the retry time reaches the limitation.
428 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
429 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
431 if (CacheEntry
->NextRetryTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
433 // Timeout, if we can retry more, send out the request again, otherwise abort
436 if (CacheEntry
->RetryCount
== 0) {
438 // Abort this request.
440 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
441 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
443 RemoveEntryList (&CacheEntry
->List
);
444 gBS
->FreePool (CacheEntry
);
447 // resend the ARP request.
449 ASSERT (!IsListEmpty(&CacheEntry
->UserRequestList
));
451 ContextEntry
= CacheEntry
->UserRequestList
.ForwardLink
;
452 RequestContext
= NET_LIST_USER_STRUCT (ContextEntry
, USER_REQUEST_CONTEXT
, List
);
454 ArpSendFrame (RequestContext
->Instance
, CacheEntry
, ARP_OPCODE_REQUEST
);
456 CacheEntry
->RetryCount
--;
457 CacheEntry
->NextRetryTime
= RequestContext
->Instance
->ConfigData
.RetryTimeOut
;
461 // Update the NextRetryTime.
463 CacheEntry
->NextRetryTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
468 // Check the timeouts for the DeniedCacheTable.
470 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->DeniedCacheTable
) {
471 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
472 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
474 if (CacheEntry
->DefaultDecayTime
== 0) {
476 // It's a static entry, skip it.
481 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
483 // Time out, remove it.
485 RemoveEntryList (&CacheEntry
->List
);
486 gBS
->FreePool (CacheEntry
);
489 // Update the DecayTime.
491 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
496 // Check the timeouts for the ResolvedCacheTable.
498 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->ResolvedCacheTable
) {
499 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
500 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
502 if (CacheEntry
->DefaultDecayTime
== 0) {
504 // It's a static entry, skip it.
509 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
511 // Time out, remove it.
513 RemoveEntryList (&CacheEntry
->List
);
514 gBS
->FreePool (CacheEntry
);
517 // Update the DecayTime.
519 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
526 Match the two NET_ARP_ADDRESSes.
528 @param AddressOne Pointer to the first address to match.
529 @param AddressTwo Pointer to the second address to match.
531 @return The two addresses match or not.
536 IN NET_ARP_ADDRESS
*AddressOne
,
537 IN NET_ARP_ADDRESS
*AddressTwo
540 if ((AddressOne
->Type
!= AddressTwo
->Type
) ||
541 (AddressOne
->Length
!= AddressTwo
->Length
)) {
543 // Either Type or Length doesn't match.
548 if ((AddressOne
->AddressPtr
!= NULL
) &&
550 AddressOne
->AddressPtr
,
551 AddressTwo
->AddressPtr
,
555 // The address is not the same.
565 Find the CacheEntry which matches the requirements in the specified CacheTable.
567 @param CacheTable Pointer to the arp cache table.
568 @param StartEntry Pointer to the start entry this search begins with
570 @param FindOpType The search type.
571 @param ProtocolAddress Pointer to the protocol address to match.
572 @param HardwareAddress Pointer to the hardware address to match.
574 @return Pointer to the matched arp cache entry, if NULL, no match is found.
578 ArpFindNextCacheEntryInTable (
579 IN LIST_ENTRY
*CacheTable
,
580 IN LIST_ENTRY
*StartEntry
,
581 IN FIND_OPTYPE FindOpType
,
582 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
583 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
587 ARP_CACHE_ENTRY
*CacheEntry
;
589 if (StartEntry
== NULL
) {
591 // Start from the beginning of the table if no StartEntry is specified.
593 StartEntry
= CacheTable
;
596 for (Entry
= StartEntry
->ForwardLink
; Entry
!= CacheTable
; Entry
= Entry
->ForwardLink
) {
597 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
599 if ((FindOpType
& MATCH_SW_ADDRESS
) != 0) {
601 // Find by the software address.
603 if (!ArpMatchAddress (ProtocolAddress
, &CacheEntry
->Addresses
[Protocol
])) {
605 // The ProtocolAddress doesn't match, continue to the next cache entry.
611 if ((FindOpType
& MATCH_HW_ADDRESS
) != 0) {
613 // Find by the hardware address.
615 if (!ArpMatchAddress (HardwareAddress
, &CacheEntry
->Addresses
[Hardware
])) {
617 // The HardwareAddress doesn't match, continue to the next cache entry.
624 // The CacheEntry meets the requirements now, return this entry.
637 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
638 in the DeniedCacheTable.
640 @param ArpService Pointer to the arp service context data.
641 @param ProtocolAddress Pointer to the protocol address.
642 @param HardwareAddress Pointer to the hardware address.
644 @return Pointer to the matched cache entry, if NULL no match is found.
648 ArpFindDeniedCacheEntry (
649 IN ARP_SERVICE_DATA
*ArpService
,
650 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
651 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
654 ARP_CACHE_ENTRY
*CacheEntry
;
656 ASSERT ((ProtocolAddress
!= NULL
) || (HardwareAddress
!= NULL
));
657 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
661 if ((ProtocolAddress
!= NULL
) && (ProtocolAddress
->AddressPtr
!= NULL
)) {
663 // Find the cache entry in the DeniedCacheTable by the protocol address.
665 CacheEntry
= ArpFindNextCacheEntryInTable (
666 &ArpService
->DeniedCacheTable
,
672 if (CacheEntry
!= NULL
) {
680 if ((HardwareAddress
!= NULL
) && (HardwareAddress
->AddressPtr
!= NULL
)) {
682 // Find the cache entry in the DeniedCacheTable by the hardware address.
684 CacheEntry
= ArpFindNextCacheEntryInTable (
685 &ArpService
->DeniedCacheTable
,
698 Allocate a cache entry and initialize it.
700 @param Instance Pointer to the instance context data.
702 @return Pointer to the new created cache entry.
707 IN ARP_INSTANCE_DATA
*Instance
710 ARP_CACHE_ENTRY
*CacheEntry
;
711 NET_ARP_ADDRESS
*Address
;
715 // Allocate memory for the cache entry.
717 CacheEntry
= AllocatePool (sizeof (ARP_CACHE_ENTRY
));
718 if (CacheEntry
== NULL
) {
725 InitializeListHead (&CacheEntry
->List
);
726 InitializeListHead (&CacheEntry
->UserRequestList
);
728 for (Index
= 0; Index
< 2; Index
++) {
730 // Init the address pointers to point to the concrete buffer.
732 Address
= &CacheEntry
->Addresses
[Index
];
733 Address
->AddressPtr
= Address
->Buffer
.ProtoAddress
;
737 // Zero the hardware address first.
739 ZeroMem (CacheEntry
->Addresses
[Hardware
].AddressPtr
, ARP_MAX_HARDWARE_ADDRESS_LEN
);
741 if (Instance
!= NULL
) {
743 // Inherit the parameters from the instance configuration.
745 CacheEntry
->RetryCount
= Instance
->ConfigData
.RetryCount
;
746 CacheEntry
->NextRetryTime
= Instance
->ConfigData
.RetryTimeOut
;
747 CacheEntry
->DefaultDecayTime
= Instance
->ConfigData
.EntryTimeOut
;
748 CacheEntry
->DecayTime
= Instance
->ConfigData
.EntryTimeOut
;
751 // Use the default parameters if this cache entry isn't allocate in a
754 CacheEntry
->RetryCount
= ARP_DEFAULT_RETRY_COUNT
;
755 CacheEntry
->NextRetryTime
= ARP_DEFAULT_RETRY_INTERVAL
;
756 CacheEntry
->DefaultDecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
757 CacheEntry
->DecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
765 Turn the CacheEntry into the resolved status.
767 @param CacheEntry Pointer to the resolved cache entry.
768 @param Instance Pointer to the instance context data.
769 @param UserEvent Pointer to the UserEvent to notify.
771 @return The count of notifications sent to the instance.
776 IN ARP_CACHE_ENTRY
*CacheEntry
,
777 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
778 IN EFI_EVENT UserEvent OPTIONAL
782 LIST_ENTRY
*NextEntry
;
783 USER_REQUEST_CONTEXT
*Context
;
789 // Iterate all the linked user requests to notify them.
791 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &CacheEntry
->UserRequestList
) {
792 Context
= NET_LIST_USER_STRUCT (Entry
, USER_REQUEST_CONTEXT
, List
);
794 if (((Instance
== NULL
) || (Context
->Instance
== Instance
)) &&
795 ((UserEvent
== NULL
) || (Context
->UserRequestEvent
== UserEvent
))) {
797 // Copy the address to the user-provided buffer and notify the user.
800 Context
->UserHwAddrBuffer
,
801 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
802 CacheEntry
->Addresses
[Hardware
].Length
804 gBS
->SignalEvent (Context
->UserRequestEvent
);
807 // Remove this user request and free the context data.
809 RemoveEntryList (&Context
->List
);
810 gBS
->FreePool (Context
);
817 // Dispatch the DPCs queued by the NotifyFunction of the Context->UserRequestEvent.
819 NetLibDispatchDpc ();
826 Fill the addresses in the CacheEntry using the information passed in by
829 @param CacheEntry Pointer to the cache entry.
830 @param HwAddr Pointer to the software address.
831 @param SwAddr Pointer to the hardware address.
837 ArpFillAddressInCacheEntry (
838 IN ARP_CACHE_ENTRY
*CacheEntry
,
839 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
840 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
843 NET_ARP_ADDRESS
*Address
[2];
844 NET_ARP_ADDRESS
*CacheAddress
;
847 Address
[Hardware
] = HwAddr
;
848 Address
[Protocol
] = SwAddr
;
850 for (Index
= 0; Index
< 2; Index
++) {
851 if (Address
[Index
] != NULL
) {
853 // Fill the address if the passed in pointer is not NULL.
855 CacheAddress
= &CacheEntry
->Addresses
[Index
];
857 CacheAddress
->Type
= Address
[Index
]->Type
;
858 CacheAddress
->Length
= Address
[Index
]->Length
;
860 if (Address
[Index
]->AddressPtr
!= NULL
) {
862 // Copy it if the AddressPtr points to some buffer.
865 CacheAddress
->AddressPtr
,
866 Address
[Index
]->AddressPtr
,
871 // Zero the corresponding address buffer in the CacheEntry.
873 ZeroMem (CacheAddress
->AddressPtr
, CacheAddress
->Length
);
881 Configure the instance using the ConfigData. ConfigData is already validated.
883 @param Instance Pointer to the instance context data to be
885 @param ConfigData Pointer to the configuration data used to
886 configure the instance.
888 @retval EFI_SUCCESS The instance is configured with the ConfigData.
889 @retval EFI_ACCESS_DENIED The instance is already configured and the
890 ConfigData tries to reset some unchangeable
892 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
893 when the SwAddressType is IPv4.
894 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
899 ArpConfigureInstance (
900 IN ARP_INSTANCE_DATA
*Instance
,
901 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
904 EFI_ARP_CONFIG_DATA
*OldConfigData
;
907 OldConfigData
= &Instance
->ConfigData
;
909 if (ConfigData
!= NULL
) {
911 if (Instance
->Configured
) {
913 // The instance is configured, check the unchangeable fields.
915 if ((OldConfigData
->SwAddressType
!= ConfigData
->SwAddressType
) ||
916 (OldConfigData
->SwAddressLength
!= ConfigData
->SwAddressLength
) ||
918 OldConfigData
->StationAddress
,
919 ConfigData
->StationAddress
,
920 OldConfigData
->SwAddressLength
923 // Deny the unallowed changes.
925 return EFI_ACCESS_DENIED
;
929 // The instance is not configured.
932 if (ConfigData
->SwAddressType
== IPV4_ETHER_PROTO_TYPE
) {
933 CopyMem (&Ip
, ConfigData
->StationAddress
, sizeof (IP4_ADDR
));
935 if (!Ip4IsUnicast (NTOHL (Ip
), 0)) {
937 // The station address is not a valid IPv4 unicast address.
939 return EFI_INVALID_PARAMETER
;
944 // Save the configuration.
946 CopyMem (OldConfigData
, ConfigData
, sizeof (*OldConfigData
));
948 OldConfigData
->StationAddress
= AllocatePool (OldConfigData
->SwAddressLength
);
949 if (OldConfigData
->StationAddress
== NULL
) {
950 DEBUG ((EFI_D_ERROR
, "ArpConfigInstance: AllocatePool for the StationAddress "
952 return EFI_OUT_OF_RESOURCES
;
956 // Save the StationAddress.
959 OldConfigData
->StationAddress
,
960 ConfigData
->StationAddress
,
961 OldConfigData
->SwAddressLength
965 // Set the state to configured.
967 Instance
->Configured
= TRUE
;
971 // Use the implementation specific values if the following field is zero.
973 OldConfigData
->EntryTimeOut
= (ConfigData
->EntryTimeOut
== 0) ?
974 ARP_DEFAULT_TIMEOUT_VALUE
: ConfigData
->EntryTimeOut
;
976 OldConfigData
->RetryCount
= (ConfigData
->RetryCount
== 0) ?
977 ARP_DEFAULT_RETRY_COUNT
: ConfigData
->RetryCount
;
979 OldConfigData
->RetryTimeOut
= (ConfigData
->RetryTimeOut
== 0) ?
980 ARP_DEFAULT_RETRY_INTERVAL
: ConfigData
->RetryTimeOut
;
983 // Reset the configuration.
986 if (Instance
->Configured
) {
988 // Cancel the arp requests issued by this instance.
990 Instance
->ArpProto
.Cancel (&Instance
->ArpProto
, NULL
, NULL
);
993 // Free the buffer previously allocated to hold the station address.
995 gBS
->FreePool (OldConfigData
->StationAddress
);
998 Instance
->Configured
= FALSE
;
1006 Send out an arp frame using the CachEntry and the ArpOpCode.
1008 @param Instance Pointer to the instance context data.
1009 @param CacheEntry Pointer to the configuration data used to
1010 configure the instance.
1011 @param ArpOpCode The opcode used to send out this Arp frame, either
1019 IN ARP_INSTANCE_DATA
*Instance
,
1020 IN ARP_CACHE_ENTRY
*CacheEntry
,
1025 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
1026 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
1029 ARP_SERVICE_DATA
*ArpService
;
1030 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1031 EFI_ARP_CONFIG_DATA
*ConfigData
;
1035 ASSERT ((Instance
!= NULL
) && (CacheEntry
!= NULL
));
1038 // Allocate memory for the TxToken.
1040 TxToken
= AllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN
));
1041 if (TxToken
== NULL
) {
1042 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for TxToken failed.\n"));
1046 TxToken
->Event
= NULL
;
1051 // Create the event for this TxToken.
1053 Status
= gBS
->CreateEvent (
1060 if (EFI_ERROR (Status
)) {
1061 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: CreateEvent failed for TxToken->Event.\n"));
1066 // Allocate memory for the TxData used in the TxToken.
1068 TxData
= AllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA
));
1069 if (TxData
== NULL
) {
1070 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for TxData failed.\n"));
1074 ArpService
= Instance
->ArpService
;
1075 SnpMode
= &ArpService
->SnpMode
;
1076 ConfigData
= &Instance
->ConfigData
;
1079 // Calculate the buffer length for this arp frame.
1081 TotalLength
= SnpMode
->MediaHeaderSize
+ sizeof (ARP_HEAD
) +
1082 2 * (ConfigData
->SwAddressLength
+ SnpMode
->HwAddressSize
);
1085 // Allocate buffer for the arp frame.
1087 Packet
= AllocatePool (TotalLength
);
1088 if (Packet
== NULL
) {
1089 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for Packet failed.\n"));
1095 // The destination MAC address.
1097 if (ArpOpCode
== ARP_OPCODE_REQUEST
) {
1098 CopyMem (TmpPtr
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
);
1102 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1103 SnpMode
->HwAddressSize
1106 TmpPtr
+= SnpMode
->HwAddressSize
;
1109 // The source MAC address.
1111 CopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1112 TmpPtr
+= SnpMode
->HwAddressSize
;
1115 // The ethernet protocol type.
1117 *(UINT16
*)TmpPtr
= HTONS (ARP_ETHER_PROTO_TYPE
);
1123 ArpHead
= (ARP_HEAD
*) TmpPtr
;
1124 ArpHead
->HwType
= HTONS ((UINT16
)SnpMode
->IfType
);
1125 ArpHead
->ProtoType
= HTONS (ConfigData
->SwAddressType
);
1126 ArpHead
->HwAddrLen
= (UINT8
)SnpMode
->HwAddressSize
;
1127 ArpHead
->ProtoAddrLen
= ConfigData
->SwAddressLength
;
1128 ArpHead
->OpCode
= HTONS (ArpOpCode
);
1129 TmpPtr
+= sizeof (ARP_HEAD
);
1132 // The sender hardware address.
1134 CopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1135 TmpPtr
+= SnpMode
->HwAddressSize
;
1138 // The sender protocol address.
1140 CopyMem (TmpPtr
, ConfigData
->StationAddress
, ConfigData
->SwAddressLength
);
1141 TmpPtr
+= ConfigData
->SwAddressLength
;
1144 // The target hardware address.
1148 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1149 SnpMode
->HwAddressSize
1151 TmpPtr
+= SnpMode
->HwAddressSize
;
1154 // The target protocol address.
1158 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1159 ConfigData
->SwAddressLength
1163 // Set all the fields of the TxData.
1165 TxData
->DestinationAddress
= NULL
;
1166 TxData
->SourceAddress
= NULL
;
1167 TxData
->ProtocolType
= 0;
1168 TxData
->DataLength
= TotalLength
- SnpMode
->MediaHeaderSize
;
1169 TxData
->HeaderLength
= (UINT16
) SnpMode
->MediaHeaderSize
;
1170 TxData
->FragmentCount
= 1;
1172 TxData
->FragmentTable
[0].FragmentBuffer
= Packet
;
1173 TxData
->FragmentTable
[0].FragmentLength
= TotalLength
;
1176 // Associate the TxData with the TxToken.
1178 TxToken
->Packet
.TxData
= TxData
;
1179 TxToken
->Status
= EFI_NOT_READY
;
1182 // Send out this arp packet by Mnp.
1184 Status
= ArpService
->Mnp
->Transmit (ArpService
->Mnp
, TxToken
);
1185 if (EFI_ERROR (Status
)) {
1186 DEBUG ((EFI_D_ERROR
, "Mnp->Transmit failed, %r.\n", Status
));
1194 if (Packet
!= NULL
) {
1195 gBS
->FreePool (Packet
);
1198 if (TxData
!= NULL
) {
1199 gBS
->FreePool (TxData
);
1202 if (TxToken
->Event
!= NULL
) {
1203 gBS
->CloseEvent (TxToken
->Event
);
1206 gBS
->FreePool (TxToken
);
1211 Delete the cache entries in the specified CacheTable, using the BySwAddress,
1212 SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE,
1213 the cache is deleted event it's a static entry.
1215 @param CacheTable Pointer to the cache table to do the deletion.
1216 @param BySwAddress Delete the cache entry by software address or by
1218 @param SwAddressType The software address used to do the deletion.
1219 @param AddressBuffer Pointer to the buffer containing the address to
1220 match for the deletion.
1221 @param Force This deletion is forced or not.
1223 @return The count of the deleted cache entries.
1227 ArpDeleteCacheEntryInTable (
1228 IN LIST_ENTRY
*CacheTable
,
1229 IN BOOLEAN BySwAddress
,
1230 IN UINT16 SwAddressType
,
1231 IN UINT8
*AddressBuffer OPTIONAL
,
1236 LIST_ENTRY
*NextEntry
;
1237 ARP_CACHE_ENTRY
*CacheEntry
;
1242 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, CacheTable
) {
1243 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1245 if ((CacheEntry
->DefaultDecayTime
== 0) && !Force
) {
1247 // It's a static entry and we are not forced to delete it, skip.
1253 if (SwAddressType
== CacheEntry
->Addresses
[Protocol
].Type
) {
1255 // Protocol address type matched. Check the address.
1257 if ((AddressBuffer
== NULL
) ||
1260 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1261 CacheEntry
->Addresses
[Protocol
].Length
1270 if ((AddressBuffer
== NULL
) ||
1273 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1274 CacheEntry
->Addresses
[Hardware
].Length
1288 // Delete this entry.
1290 RemoveEntryList (&CacheEntry
->List
);
1291 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
1292 gBS
->FreePool (CacheEntry
);
1302 Delete cache entries in all the cache tables.
1304 @param Instance Pointer to the instance context data.
1305 @param BySwAddress Delete the cache entry by software address or by
1307 @param AddressBuffer Pointer to the buffer containing the address to
1308 match for the deletion.
1309 @param Force This deletion is forced or not.
1311 @return The count of the deleted cache entries.
1315 ArpDeleteCacheEntry (
1316 IN ARP_INSTANCE_DATA
*Instance
,
1317 IN BOOLEAN BySwAddress
,
1318 IN UINT8
*AddressBuffer OPTIONAL
,
1322 ARP_SERVICE_DATA
*ArpService
;
1325 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1327 ArpService
= Instance
->ArpService
;
1330 // Delete the cache entries in the DeniedCacheTable.
1332 Count
= ArpDeleteCacheEntryInTable (
1333 &ArpService
->DeniedCacheTable
,
1335 Instance
->ConfigData
.SwAddressType
,
1341 // Delete the cache entries inthe ResolvedCacheTable.
1343 Count
+= ArpDeleteCacheEntryInTable (
1344 &ArpService
->ResolvedCacheTable
,
1346 Instance
->ConfigData
.SwAddressType
,
1356 Cancel the arp request.
1358 @param Instance Pointer to the instance context data.
1359 @param TargetSwAddress Pointer to the buffer containing the target
1360 software address to match the arp request.
1361 @param UserEvent The user event used to notify this request
1364 @return The count of the cancelled requests.
1369 IN ARP_INSTANCE_DATA
*Instance
,
1370 IN VOID
*TargetSwAddress OPTIONAL
,
1371 IN EFI_EVENT UserEvent OPTIONAL
1374 ARP_SERVICE_DATA
*ArpService
;
1376 LIST_ENTRY
*NextEntry
;
1377 ARP_CACHE_ENTRY
*CacheEntry
;
1380 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1382 ArpService
= Instance
->ArpService
;
1385 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
1386 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1388 if ((TargetSwAddress
== NULL
) ||
1391 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1392 CacheEntry
->Addresses
[Protocol
].Length
1395 // This request entry matches the TargetSwAddress or all requests are to be
1396 // cancelled as TargetSwAddress is NULL.
1398 Count
+= ArpAddressResolved (CacheEntry
, Instance
, UserEvent
);
1400 if (IsListEmpty (&CacheEntry
->UserRequestList
)) {
1402 // No user requests any more, remove this request cache entry.
1404 RemoveEntryList (&CacheEntry
->List
);
1405 gBS
->FreePool (CacheEntry
);
1415 Find the cache entry in the cache table.
1417 @param Instance Pointer to the instance context data.
1418 @param BySwAddress Set to TRUE to look for matching software protocol
1419 addresses. Set to FALSE to look for matching
1420 hardware protocol addresses.
1421 @param AddressBuffer Pointer to address buffer. Set to NULL to match
1423 @param EntryLength The size of an entry in the entries buffer.
1424 @param EntryCount The number of ARP cache entries that are found by
1425 the specified criteria.
1426 @param Entries Pointer to the buffer that will receive the ARP
1428 @param Refresh Set to TRUE to refresh the timeout value of the
1429 matching ARP cache entry.
1431 @retval EFI_SUCCESS The requested ARP cache entries are copied into
1433 @retval EFI_NOT_FOUND No matching entries found.
1434 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
1439 IN ARP_INSTANCE_DATA
*Instance
,
1440 IN BOOLEAN BySwAddress
,
1441 IN VOID
*AddressBuffer OPTIONAL
,
1442 OUT UINT32
*EntryLength OPTIONAL
,
1443 OUT UINT32
*EntryCount OPTIONAL
,
1444 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
1449 ARP_SERVICE_DATA
*ArpService
;
1450 NET_ARP_ADDRESS MatchAddress
;
1451 FIND_OPTYPE FindOpType
;
1452 LIST_ENTRY
*StartEntry
;
1453 ARP_CACHE_ENTRY
*CacheEntry
;
1454 NET_MAP FoundEntries
;
1456 EFI_ARP_FIND_DATA
*FindData
;
1457 LIST_ENTRY
*CacheTable
;
1459 ArpService
= Instance
->ArpService
;
1462 // Init the FounEntries used to hold the found cache entries.
1464 NetMapInit (&FoundEntries
);
1467 // Set the MatchAddress.
1470 MatchAddress
.Type
= Instance
->ConfigData
.SwAddressType
;
1471 MatchAddress
.Length
= Instance
->ConfigData
.SwAddressLength
;
1472 FindOpType
= ByProtoAddress
;
1474 MatchAddress
.Type
= ArpService
->SnpMode
.IfType
;
1475 MatchAddress
.Length
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1476 FindOpType
= ByHwAddress
;
1479 MatchAddress
.AddressPtr
= AddressBuffer
;
1482 // Search the DeniedCacheTable
1487 // Try to find the matched entries in the DeniedCacheTable.
1489 CacheEntry
= ArpFindNextCacheEntryInTable (
1490 &ArpService
->DeniedCacheTable
,
1496 if (CacheEntry
== NULL
) {
1498 // Once the CacheEntry is NULL, there are no more matches.
1504 // Insert the found entry into the map.
1509 (VOID
*)&ArpService
->DeniedCacheTable
1513 // Let the next search start from this cache entry.
1515 StartEntry
= &CacheEntry
->List
;
1519 // Refresh the DecayTime if needed.
1521 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1526 // Search the ResolvedCacheTable
1530 CacheEntry
= ArpFindNextCacheEntryInTable (
1531 &ArpService
->ResolvedCacheTable
,
1537 if (CacheEntry
== NULL
) {
1539 // Once the CacheEntry is NULL, there are no more matches.
1545 // Insert the found entry into the map.
1550 (VOID
*)&ArpService
->ResolvedCacheTable
1554 // Let the next search start from this cache entry.
1556 StartEntry
= &CacheEntry
->List
;
1560 // Refresh the DecayTime if needed.
1562 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1566 Status
= EFI_SUCCESS
;
1568 FoundCount
= (UINT32
) NetMapGetCount (&FoundEntries
);
1569 if (FoundCount
== 0) {
1570 Status
= EFI_NOT_FOUND
;
1574 if (EntryLength
!= NULL
) {
1576 // Return the entry length, make sure its 8 bytes alignment.
1578 *EntryLength
= (((sizeof (EFI_ARP_FIND_DATA
) + Instance
->ConfigData
.SwAddressLength
+
1579 ArpService
->SnpMode
.HwAddressSize
) + 3) & ~(0x3));
1582 if (EntryCount
!= NULL
) {
1584 // Return the found entry count.
1586 *EntryCount
= FoundCount
;
1589 if (Entries
== NULL
) {
1594 // Allocate buffer to copy the found entries.
1596 FindData
= AllocatePool (FoundCount
* (*EntryLength
));
1597 if (FindData
== NULL
) {
1598 DEBUG ((EFI_D_ERROR
, "ArpFindCacheEntry: Failed to allocate memory.\n"));
1599 Status
= EFI_OUT_OF_RESOURCES
;
1604 // Return the address to the user.
1606 *Entries
= FindData
;
1609 // Dump the entries.
1611 while (!NetMapIsEmpty (&FoundEntries
)) {
1613 // Get a cache entry from the map.
1615 CacheEntry
= NetMapRemoveHead (&FoundEntries
, (VOID
**)&CacheTable
);
1618 // Set the fields in FindData.
1620 FindData
->Size
= *EntryLength
;
1621 FindData
->DenyFlag
= (BOOLEAN
)(CacheTable
== &ArpService
->DeniedCacheTable
);
1622 FindData
->StaticFlag
= (BOOLEAN
)(CacheEntry
->DefaultDecayTime
== 0);
1623 FindData
->HwAddressType
= ArpService
->SnpMode
.IfType
;
1624 FindData
->SwAddressType
= Instance
->ConfigData
.SwAddressType
;
1625 FindData
->HwAddressLength
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1626 FindData
->SwAddressLength
= Instance
->ConfigData
.SwAddressLength
;
1629 // Copy the software address.
1633 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1634 FindData
->SwAddressLength
1638 // Copy the hardware address.
1641 (UINT8
*)(FindData
+ 1) + FindData
->SwAddressLength
,
1642 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1643 FindData
->HwAddressLength
1647 // Slip to the next FindData.
1649 FindData
= (EFI_ARP_FIND_DATA
*)((UINT8
*)FindData
+ *EntryLength
);
1654 NetMapClean (&FoundEntries
);