3 Copyright (c) 2006 - 2007, 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.
25 EFI_ARP_PROTOCOL mEfiArpProtocolTemplate
= {
37 Initialize the instance context data.
39 @param ArpService Pointer to the arp service context data this
41 @param Instance Pointer to the instance context data.
48 IN ARP_SERVICE_DATA
*ArpService
,
49 IN ARP_INSTANCE_DATA
*Instance
52 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
54 Instance
->Signature
= ARP_INSTANCE_DATA_SIGNATURE
;
55 Instance
->ArpService
= ArpService
;
57 CopyMem (&Instance
->ArpProto
, &mEfiArpProtocolTemplate
, sizeof (ARP_SERVICE_DATA
));
59 Instance
->Configured
= FALSE
;
60 Instance
->Destroyed
= FALSE
;
62 NetListInit (&Instance
->List
);
67 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
69 @param Event The Event this notify function registered to.
70 @param Context Pointer to the context data registerd to the
84 ARP_SERVICE_DATA
*ArpService
;
85 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*RxToken
;
86 EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
;
88 ARP_ADDRESS ArpAddress
;
89 ARP_CACHE_ENTRY
*CacheEntry
;
90 NET_LIST_ENTRY
*Entry
;
91 ARP_INSTANCE_DATA
*Instance
;
92 EFI_ARP_CONFIG_DATA
*ConfigData
;
93 NET_ARP_ADDRESS SenderAddress
[2];
98 ArpService
= (ARP_SERVICE_DATA
*)Context
;
99 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
101 RxToken
= &ArpService
->RxToken
;
103 if (RxToken
->Status
== EFI_ABORTED
) {
105 // The Token is aborted, possibly by arp itself, just return and the receiving
106 // process is stopped.
111 if (EFI_ERROR (RxToken
->Status
)) {
113 // Restart the receiving if any other error Status occurs.
115 goto RESTART_RECEIVE
;
119 // Status is EFI_SUCCESS, process the received frame.
121 RxData
= RxToken
->Packet
.RxData
;
122 Head
= (ARP_HEAD
*) RxData
->PacketData
;
125 // Convert the byte order of the multi-byte fields.
127 Head
->HwType
= NTOHS (Head
->HwType
);
128 Head
->ProtoType
= NTOHS (Head
->ProtoType
);
129 Head
->OpCode
= NTOHS (Head
->OpCode
);
131 if ((Head
->HwType
!= ArpService
->SnpMode
.IfType
) ||
132 (Head
->HwAddrLen
!= ArpService
->SnpMode
.HwAddressSize
) ||
133 (RxData
->ProtocolType
!= ARP_ETHER_PROTO_TYPE
)) {
135 // The hardware type or the hardware address length doesn't match.
136 // There is a sanity check for the protocol type too.
142 // Set the pointers to the addresses contained in the arp packet.
144 ArpAddress
.SenderHwAddr
= (UINT8
*)(Head
+ 1);
145 ArpAddress
.SenderProtoAddr
= ArpAddress
.SenderHwAddr
+ Head
->HwAddrLen
;
146 ArpAddress
.TargetHwAddr
= ArpAddress
.SenderProtoAddr
+ Head
->ProtoAddrLen
;
147 ArpAddress
.TargetProtoAddr
= ArpAddress
.TargetHwAddr
+ Head
->HwAddrLen
;
149 if (EFI_ERROR (NET_TRYLOCK (&ArpService
->Lock
))) {
150 ARP_DEBUG_ERROR (("ArpOnFrameRcvd: Faild to acquire the CacheTableLock.\n"));
154 SenderAddress
[Hardware
].Type
= Head
->HwType
;
155 SenderAddress
[Hardware
].Length
= Head
->HwAddrLen
;
156 SenderAddress
[Hardware
].AddressPtr
= ArpAddress
.SenderHwAddr
;
158 SenderAddress
[Protocol
].Type
= Head
->ProtoType
;
159 SenderAddress
[Protocol
].Length
= Head
->ProtoAddrLen
;
160 SenderAddress
[Protocol
].AddressPtr
= ArpAddress
.SenderProtoAddr
;
163 // First, check the denied cache table.
165 CacheEntry
= ArpFindDeniedCacheEntry (
167 &SenderAddress
[Protocol
],
168 &SenderAddress
[Hardware
]
170 if (CacheEntry
!= NULL
) {
172 // This address (either hardware or protocol address, or both) is configured to
173 // be a deny entry, silently skip the normal process.
178 ProtoMatched
= FALSE
;
181 NET_LIST_FOR_EACH (Entry
, &ArpService
->ChildrenList
) {
183 // Iterate all the children.
185 Instance
= NET_LIST_USER_STRUCT (Entry
, ARP_INSTANCE_DATA
, List
);
186 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
187 ConfigData
= &Instance
->ConfigData
;
189 if ((Instance
->Configured
) &&
190 (Head
->ProtoType
== ConfigData
->SwAddressType
) &&
191 (Head
->ProtoAddrLen
== ConfigData
->SwAddressLength
)) {
193 // The protocol type is matched for the received arp packet.
196 if (0 == NetCompareMem (
197 (VOID
*)ArpAddress
.TargetProtoAddr
,
198 ConfigData
->StationAddress
,
199 ConfigData
->SwAddressLength
202 // The arp driver has the target address required by the received arp packet.
212 // Protocol type unmatchable, skip.
218 // Check whether the sender's address information is already in the cache.
221 CacheEntry
= ArpFindNextCacheEntryInTable (
222 &ArpService
->ResolvedCacheTable
,
225 &SenderAddress
[Protocol
],
228 if (CacheEntry
!= NULL
) {
230 // Update the entry with the new information.
232 ArpFillAddressInCacheEntry (CacheEntry
, &SenderAddress
[Hardware
], NULL
);
233 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
239 // This arp packet isn't targeted to us, skip now.
246 // Add the triplet <protocol type, sender protocol address, sender hardware address>
247 // to the translation table.
249 CacheEntry
= ArpFindNextCacheEntryInTable (
250 &ArpService
->PendingRequestTable
,
253 &SenderAddress
[Protocol
],
256 if (CacheEntry
== NULL
) {
258 // Allocate a new CacheEntry.
260 CacheEntry
= ArpAllocCacheEntry (NULL
);
261 if (CacheEntry
== NULL
) {
266 NetListRemoveEntry (&CacheEntry
->List
);
269 // Fill the addresses into the CacheEntry.
271 ArpFillAddressInCacheEntry (
273 &SenderAddress
[Hardware
],
274 &SenderAddress
[Protocol
]
280 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
283 // Add this entry into the ResolvedCacheTable
285 NetListInsertHead (&ArpService
->ResolvedCacheTable
, &CacheEntry
->List
);
288 if (Head
->OpCode
== ARP_OPCODE_REQUEST
) {
290 // Send back the ARP Reply. If we reach here, Instance is not NULL and CacheEntry
293 ArpSendFrame (Instance
, CacheEntry
, ARP_OPCODE_REPLY
);
298 NET_UNLOCK (&ArpService
->Lock
);
303 // Signal Mnp to recycle the RxData.
305 gBS
->SignalEvent (RxData
->RecycleEvent
);
310 // Continue to receive packets from Mnp.
312 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, RxToken
);
315 if (EFI_ERROR (Status
)) {
316 ARP_DEBUG_ERROR (("ArpOnFrameRcvd: ArpService->Mnp->Receive "
317 "failed, %r\n.", Status
));
324 Process the already sent arp packets.
326 @param Event The Event this notify function registered to.
327 @param Context Pointer to the context data registerd to the
340 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
341 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
343 ASSERT (Context
!= NULL
);
345 TxToken
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*)Context
;
346 TxData
= TxToken
->Packet
.TxData
;
349 if (EFI_ERROR (TxToken
->Status
)) {
350 ARP_DEBUG_ERROR (("ArpOnFrameSent: TxToken->Status, %r.\n", TxToken
->Status
));
355 // Free the allocated memory and close the event.
357 NetFreePool (TxData
->FragmentTable
[0].FragmentBuffer
);
358 NetFreePool (TxData
);
359 gBS
->CloseEvent (TxToken
->Event
);
360 NetFreePool (TxToken
);
365 Process the arp cache olding and drive the retrying arp requests.
367 @param Event The Event this notify function registered to.
368 @param Context Pointer to the context data registerd to the
381 ARP_SERVICE_DATA
*ArpService
;
382 NET_LIST_ENTRY
*Entry
;
383 NET_LIST_ENTRY
*NextEntry
;
384 NET_LIST_ENTRY
*ContextEntry
;
385 ARP_CACHE_ENTRY
*CacheEntry
;
386 USER_REQUEST_CONTEXT
*RequestContext
;
388 ASSERT (Context
!= NULL
);
389 ArpService
= (ARP_SERVICE_DATA
*)Context
;
391 if (EFI_ERROR (NET_TRYLOCK (&ArpService
->Lock
))) {
396 // Iterate all the pending requests to see whether a retry is needed to send out
397 // or the request finally fails because the retry time reaches the limitation.
399 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
400 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
402 if (CacheEntry
->NextRetryTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
404 // Timeout, if we can retry more, send out the request again, otherwise abort
407 if (CacheEntry
->RetryCount
== 0) {
409 // Abort this request.
411 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
412 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
414 NetListRemoveEntry (&CacheEntry
->List
);
415 NetFreePool (CacheEntry
);
418 // resend the ARP request.
420 ASSERT (!NetListIsEmpty(&CacheEntry
->UserRequestList
));
422 ContextEntry
= CacheEntry
->UserRequestList
.ForwardLink
;
423 RequestContext
= NET_LIST_USER_STRUCT (ContextEntry
, USER_REQUEST_CONTEXT
, List
);
425 ArpSendFrame (RequestContext
->Instance
, CacheEntry
, ARP_OPCODE_REQUEST
);
427 CacheEntry
->RetryCount
--;
428 CacheEntry
->NextRetryTime
= RequestContext
->Instance
->ConfigData
.RetryTimeOut
;
432 // Update the NextRetryTime.
434 CacheEntry
->NextRetryTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
439 // Check the timeouts for the DeniedCacheTable.
441 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->DeniedCacheTable
) {
442 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
443 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
445 if (CacheEntry
->DefaultDecayTime
== 0) {
447 // It's a static entry, skip it.
452 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
454 // Time out, remove it.
456 NetListRemoveEntry (&CacheEntry
->List
);
457 NetFreePool (CacheEntry
);
460 // Update the DecayTime.
462 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
467 // Check the timeouts for the ResolvedCacheTable.
469 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->ResolvedCacheTable
) {
470 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
471 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
473 if (CacheEntry
->DefaultDecayTime
== 0) {
475 // It's a static entry, skip it.
480 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
482 // Time out, remove it.
484 NetListRemoveEntry (&CacheEntry
->List
);
485 NetFreePool (CacheEntry
);
488 // Update the DecayTime.
490 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
494 NET_UNLOCK (&ArpService
->Lock
);
499 Match the two NET_ARP_ADDRESSes.
501 @param AddressOne Pointer to the first address to match.
502 @param AddressTwo Pointer to the second address to match.
504 @return The two addresses match or not.
510 IN NET_ARP_ADDRESS
*AddressOne
,
511 IN NET_ARP_ADDRESS
*AddressTwo
514 if ((AddressOne
->Type
!= AddressTwo
->Type
) ||
515 (AddressOne
->Length
!= AddressTwo
->Length
)) {
517 // Either Type or Length doesn't match.
522 if ((AddressOne
->AddressPtr
!= NULL
) &&
524 AddressOne
->AddressPtr
,
525 AddressTwo
->AddressPtr
,
529 // The address is not the same.
539 Find the CacheEntry which matches the requirements in the specified CacheTable.
541 @param CacheTable Pointer to the arp cache table.
542 @param StartEntry Pointer to the start entry this search begins with
544 @param FindOpType The search type.
545 @param ProtocolAddress Pointer to the protocol address to match.
546 @param HardwareAddress Pointer to the hardware address to match.
548 @return Pointer to the matched arp cache entry, if NULL, no match is found.
552 ArpFindNextCacheEntryInTable (
553 IN NET_LIST_ENTRY
*CacheTable
,
554 IN NET_LIST_ENTRY
*StartEntry
,
555 IN FIND_OPTYPE FindOpType
,
556 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
557 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
560 NET_LIST_ENTRY
*Entry
;
561 ARP_CACHE_ENTRY
*CacheEntry
;
563 if (StartEntry
== NULL
) {
565 // Start from the beginning of the table if no StartEntry is specified.
567 StartEntry
= CacheTable
;
570 for (Entry
= StartEntry
->ForwardLink
; Entry
!= CacheTable
; Entry
= Entry
->ForwardLink
) {
571 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
573 if (FindOpType
& MATCH_SW_ADDRESS
) {
575 // Find by the software address.
577 if (!ArpMatchAddress (ProtocolAddress
, &CacheEntry
->Addresses
[Protocol
])) {
579 // The ProtocolAddress doesn't match, continue to the next cache entry.
585 if (FindOpType
& MATCH_HW_ADDRESS
) {
587 // Find by the hardware address.
589 if (!ArpMatchAddress (HardwareAddress
, &CacheEntry
->Addresses
[Hardware
])) {
591 // The HardwareAddress doesn't match, continue to the next cache entry.
598 // The CacheEntry meets the requirements now, return this entry.
611 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
612 in the DeniedCacheTable.
614 @param ArpService Pointer to the arp service context data.
615 @param ProtocolAddress Pointer to the protocol address.
616 @param HardwareAddress Pointer to the hardware address.
618 @return Pointer to the matched cache entry, if NULL no match is found.
622 ArpFindDeniedCacheEntry (
623 IN ARP_SERVICE_DATA
*ArpService
,
624 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
625 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
628 ARP_CACHE_ENTRY
*CacheEntry
;
630 ASSERT ((ProtocolAddress
!= NULL
) || (HardwareAddress
!= NULL
));
631 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
635 if ((ProtocolAddress
!= NULL
) && (ProtocolAddress
->AddressPtr
!= NULL
)) {
637 // Find the cache entry in the DeniedCacheTable by the protocol address.
639 CacheEntry
= ArpFindNextCacheEntryInTable (
640 &ArpService
->DeniedCacheTable
,
646 if (CacheEntry
!= NULL
) {
654 if ((HardwareAddress
!= NULL
) && (HardwareAddress
->AddressPtr
!= NULL
)) {
656 // Find the cache entry in the DeniedCacheTable by the hardware address.
658 CacheEntry
= ArpFindNextCacheEntryInTable (
659 &ArpService
->DeniedCacheTable
,
672 Allocate a cache entry and initialize it.
674 @param Instance Pointer to the instance context data.
676 @return Pointer to the new created cache entry.
681 IN ARP_INSTANCE_DATA
*Instance
684 ARP_CACHE_ENTRY
*CacheEntry
;
685 NET_ARP_ADDRESS
*Address
;
689 // Allocate memory for the cache entry.
691 CacheEntry
= NetAllocatePool (sizeof (ARP_CACHE_ENTRY
));
692 if (CacheEntry
== NULL
) {
699 NetListInit (&CacheEntry
->List
);
700 NetListInit (&CacheEntry
->UserRequestList
);
702 for (Index
= 0; Index
< 2; Index
++) {
704 // Init the address pointers to point to the concrete buffer.
706 Address
= &CacheEntry
->Addresses
[Index
];
707 Address
->AddressPtr
= Address
->Buffer
.ProtoAddress
;
711 // Zero the hardware address first.
713 NetZeroMem (CacheEntry
->Addresses
[Hardware
].AddressPtr
, ARP_MAX_HARDWARE_ADDRESS_LEN
);
715 if (Instance
!= NULL
) {
717 // Inherit the parameters from the instance configuration.
719 CacheEntry
->RetryCount
= Instance
->ConfigData
.RetryCount
;
720 CacheEntry
->NextRetryTime
= Instance
->ConfigData
.RetryTimeOut
;
721 CacheEntry
->DefaultDecayTime
= Instance
->ConfigData
.EntryTimeOut
;
722 CacheEntry
->DecayTime
= Instance
->ConfigData
.EntryTimeOut
;
725 // Use the default parameters if this cache entry isn't allocate in a
728 CacheEntry
->RetryCount
= ARP_DEFAULT_RETRY_COUNT
;
729 CacheEntry
->NextRetryTime
= ARP_DEFAULT_RETRY_INTERVAL
;
730 CacheEntry
->DefaultDecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
731 CacheEntry
->DecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
739 Turn the CacheEntry into the resolved status.
741 @param CacheEntry Pointer to the resolved cache entry.
742 @param Instance Pointer to the instance context data.
743 @param UserEvent Pointer to the UserEvent to notify.
745 @return The count of notifications sent to the instance.
750 IN ARP_CACHE_ENTRY
*CacheEntry
,
751 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
752 IN EFI_EVENT UserEvent OPTIONAL
755 NET_LIST_ENTRY
*Entry
;
756 NET_LIST_ENTRY
*NextEntry
;
757 USER_REQUEST_CONTEXT
*Context
;
763 // Iterate all the linked user requests to notify them.
765 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &CacheEntry
->UserRequestList
) {
766 Context
= NET_LIST_USER_STRUCT (Entry
, USER_REQUEST_CONTEXT
, List
);
768 if (((Instance
== NULL
) || (Context
->Instance
== Instance
)) &&
769 ((UserEvent
== NULL
) || (Context
->UserRequestEvent
== UserEvent
))) {
771 // Copy the address to the user-provided buffer and notify the user.
774 Context
->UserHwAddrBuffer
,
775 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
776 CacheEntry
->Addresses
[Hardware
].Length
778 gBS
->SignalEvent (Context
->UserRequestEvent
);
781 // Remove this user request and free the context data.
783 NetListRemoveEntry (&Context
->List
);
784 NetFreePool (Context
);
795 Fill the addresses in the CacheEntry using the information passed in by
798 @param CacheEntry Pointer to the cache entry.
799 @param HwAddr Pointer to the software address.
800 @param SwAddr Pointer to the hardware address.
806 ArpFillAddressInCacheEntry (
807 IN ARP_CACHE_ENTRY
*CacheEntry
,
808 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
809 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
812 NET_ARP_ADDRESS
*Address
[2];
813 NET_ARP_ADDRESS
*CacheAddress
;
816 Address
[Hardware
] = HwAddr
;
817 Address
[Protocol
] = SwAddr
;
819 for (Index
= 0; Index
< 2; Index
++) {
820 if (Address
[Index
] != NULL
) {
822 // Fill the address if the passed in pointer is not NULL.
824 CacheAddress
= &CacheEntry
->Addresses
[Index
];
826 CacheAddress
->Type
= Address
[Index
]->Type
;
827 CacheAddress
->Length
= Address
[Index
]->Length
;
829 if (Address
[Index
]->AddressPtr
!= NULL
) {
831 // Copy it if the AddressPtr points to some buffer.
834 CacheAddress
->AddressPtr
,
835 Address
[Index
]->AddressPtr
,
840 // Zero the corresponding address buffer in the CacheEntry.
842 NetZeroMem (CacheAddress
->AddressPtr
, CacheAddress
->Length
);
850 Configure the instance using the ConfigData. ConfigData is already validated.
852 @param Instance Pointer to the instance context data to be
854 @param ConfigData Pointer to the configuration data used to
855 configure the instance.
857 @retval EFI_SUCCESS The instance is configured with the ConfigData.
858 @retval EFI_ACCESS_DENIED The instance is already configured and the
859 ConfigData tries to reset some unchangeable
861 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
862 when the SwAddressType is IPv4.
863 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
868 ArpConfigureInstance (
869 IN ARP_INSTANCE_DATA
*Instance
,
870 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
873 EFI_ARP_CONFIG_DATA
*OldConfigData
;
876 OldConfigData
= &Instance
->ConfigData
;
878 if (ConfigData
!= NULL
) {
880 if (Instance
->Configured
) {
882 // The instance is configured, check the unchangeable fields.
884 if ((OldConfigData
->SwAddressType
!= ConfigData
->SwAddressType
) ||
885 (OldConfigData
->SwAddressLength
!= ConfigData
->SwAddressLength
) ||
887 OldConfigData
->StationAddress
,
888 ConfigData
->StationAddress
,
889 OldConfigData
->SwAddressLength
892 // Deny the unallowed changes.
894 return EFI_ACCESS_DENIED
;
898 // The instance is not configured.
901 if (ConfigData
->SwAddressType
== IPv4_ETHER_PROTO_TYPE
) {
902 NetCopyMem (&Ip
, ConfigData
->StationAddress
, sizeof (IP4_ADDR
));
904 if (!Ip4IsUnicast (NTOHL (Ip
), 0)) {
906 // The station address is not a valid IPv4 unicast address.
908 return EFI_INVALID_PARAMETER
;
913 // Save the configuration.
915 CopyMem (OldConfigData
, ConfigData
, sizeof (EFI_ARP_CONFIG_DATA
));
917 OldConfigData
->StationAddress
= NetAllocatePool (OldConfigData
->SwAddressLength
);
918 if (OldConfigData
->StationAddress
== NULL
) {
919 ARP_DEBUG_ERROR (("ArpConfigInstance: NetAllocatePool for the StationAddress "
921 return EFI_OUT_OF_RESOURCES
;
925 // Save the StationAddress.
928 OldConfigData
->StationAddress
,
929 ConfigData
->StationAddress
,
930 OldConfigData
->SwAddressLength
934 // Set the state to configured.
936 Instance
->Configured
= TRUE
;
940 // Use the implementation specific values if the following field is zero.
942 OldConfigData
->EntryTimeOut
= (ConfigData
->EntryTimeOut
== 0) ?
943 ARP_DEFAULT_TIMEOUT_VALUE
: ConfigData
->EntryTimeOut
;
945 OldConfigData
->RetryCount
= (ConfigData
->RetryCount
== 0) ?
946 ARP_DEFAULT_RETRY_COUNT
: ConfigData
->RetryCount
;
948 OldConfigData
->RetryTimeOut
= (ConfigData
->RetryTimeOut
== 0) ?
949 ARP_DEFAULT_RETRY_INTERVAL
: ConfigData
->RetryTimeOut
;
952 // Reset the configuration.
955 if (Instance
->Configured
) {
957 // Cancel the arp requests issued by this instance.
959 ArpCancelRequest (Instance
, NULL
, NULL
);
962 // Free the buffer previously allocated to hold the station address.
964 NetFreePool (OldConfigData
->StationAddress
);
967 Instance
->Configured
= FALSE
;
975 Send out an arp frame using the CachEntry and the ArpOpCode.
977 @param Instance Pointer to the instance context data.
978 @param CacheEntry Pointer to the configuration data used to
979 configure the instance.
980 @param ArpOpCode The opcode used to send out this Arp frame, either
988 IN ARP_INSTANCE_DATA
*Instance
,
989 IN ARP_CACHE_ENTRY
*CacheEntry
,
994 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
995 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
998 ARP_SERVICE_DATA
*ArpService
;
999 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1000 EFI_ARP_CONFIG_DATA
*ConfigData
;
1004 ASSERT ((Instance
!= NULL
) && (CacheEntry
!= NULL
));
1007 // Allocate memory for the TxToken.
1009 TxToken
= NetAllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN
));
1010 if (TxToken
== NULL
) {
1011 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for TxToken failed.\n"));
1015 TxToken
->Event
= NULL
;
1020 // Create the event for this TxToken.
1022 Status
= gBS
->CreateEvent (
1029 if (EFI_ERROR (Status
)) {
1030 ARP_DEBUG_ERROR (("ArpSendFrame: CreateEvent failed for TxToken->Event.\n"));
1035 // Allocate memory for the TxData used in the TxToken.
1037 TxData
= NetAllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA
));
1038 if (TxData
== NULL
) {
1039 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for TxData failed.\n"));
1043 ArpService
= Instance
->ArpService
;
1044 SnpMode
= &ArpService
->SnpMode
;
1045 ConfigData
= &Instance
->ConfigData
;
1048 // Calculate the buffer length for this arp frame.
1050 TotalLength
= SnpMode
->MediaHeaderSize
+ sizeof (ARP_HEAD
) +
1051 2 * (ConfigData
->SwAddressLength
+ SnpMode
->HwAddressSize
);
1054 // Allocate buffer for the arp frame.
1056 Packet
= NetAllocatePool (TotalLength
);
1057 if (Packet
== NULL
) {
1058 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for Packet failed.\n"));
1064 // The destination MAC address.
1066 if (ArpOpCode
== ARP_OPCODE_REQUEST
) {
1067 NetCopyMem (TmpPtr
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
);
1071 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1072 SnpMode
->HwAddressSize
1075 TmpPtr
+= SnpMode
->HwAddressSize
;
1078 // The source MAC address.
1080 NetCopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1081 TmpPtr
+= SnpMode
->HwAddressSize
;
1084 // The ethernet protocol type.
1086 *(UINT16
*)TmpPtr
= HTONS (ARP_ETHER_PROTO_TYPE
);
1092 ArpHead
= (ARP_HEAD
*) TmpPtr
;
1093 ArpHead
->HwType
= HTONS ((UINT16
)SnpMode
->IfType
);
1094 ArpHead
->ProtoType
= HTONS (ConfigData
->SwAddressType
);
1095 ArpHead
->HwAddrLen
= (UINT8
)SnpMode
->HwAddressSize
;
1096 ArpHead
->ProtoAddrLen
= ConfigData
->SwAddressLength
;
1097 ArpHead
->OpCode
= HTONS (ArpOpCode
);
1098 TmpPtr
+= sizeof (ARP_HEAD
);
1101 // The sender hardware address.
1103 NetCopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1104 TmpPtr
+= SnpMode
->HwAddressSize
;
1107 // The sender protocol address.
1109 NetCopyMem (TmpPtr
, ConfigData
->StationAddress
, ConfigData
->SwAddressLength
);
1110 TmpPtr
+= ConfigData
->SwAddressLength
;
1113 // The target hardware address.
1117 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1118 SnpMode
->HwAddressSize
1120 TmpPtr
+= SnpMode
->HwAddressSize
;
1123 // The target protocol address.
1127 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1128 ConfigData
->SwAddressLength
1132 // Set all the fields of the TxData.
1134 TxData
->DestinationAddress
= NULL
;
1135 TxData
->SourceAddress
= NULL
;
1136 TxData
->ProtocolType
= 0;
1137 TxData
->DataLength
= TotalLength
- SnpMode
->MediaHeaderSize
;
1138 TxData
->HeaderLength
= (UINT16
) SnpMode
->MediaHeaderSize
;
1139 TxData
->FragmentCount
= 1;
1141 TxData
->FragmentTable
[0].FragmentBuffer
= Packet
;
1142 TxData
->FragmentTable
[0].FragmentLength
= TotalLength
;
1145 // Associate the TxData with the TxToken.
1147 TxToken
->Packet
.TxData
= TxData
;
1148 TxToken
->Status
= EFI_NOT_READY
;
1151 // Send out this arp packet by Mnp.
1153 Status
= ArpService
->Mnp
->Transmit (ArpService
->Mnp
, TxToken
);
1154 if (EFI_ERROR (Status
)) {
1155 ARP_DEBUG_ERROR (("Mnp->Transmit failed, %r.\n", Status
));
1163 if (Packet
!= NULL
) {
1164 NetFreePool (Packet
);
1167 if (TxData
!= NULL
) {
1168 NetFreePool (TxData
);
1171 if (TxToken
->Event
!= NULL
) {
1172 gBS
->CloseEvent (TxToken
->Event
);
1175 NetFreePool (TxToken
);
1180 Delete the cache entries in the specified CacheTable, using the BySwAddress,
1181 SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE,
1182 the cache is deleted event it's a static entry.
1184 @param CacheTable Pointer to the cache table to do the deletion.
1185 @param BySwAddress Delete the cache entry by software address or by
1187 @param SwAddressType The software address used to do the deletion.
1188 @param AddressBuffer Pointer to the buffer containing the address to
1189 match for the deletion.
1190 @param Force This deletion is forced or not.
1192 @return The count of the deleted cache entries.
1197 ArpDeleteCacheEntryInTable (
1198 IN NET_LIST_ENTRY
*CacheTable
,
1199 IN BOOLEAN BySwAddress
,
1200 IN UINT16 SwAddressType
,
1201 IN UINT8
*AddressBuffer OPTIONAL
,
1205 NET_LIST_ENTRY
*Entry
;
1206 NET_LIST_ENTRY
*NextEntry
;
1207 ARP_CACHE_ENTRY
*CacheEntry
;
1212 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, CacheTable
) {
1213 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1215 if ((CacheEntry
->DefaultDecayTime
== 0) && !Force
) {
1217 // It's a static entry and we are not forced to delete it, skip.
1223 if (SwAddressType
== CacheEntry
->Addresses
[Protocol
].Type
) {
1225 // Protocol address type matched. Check the address.
1227 if ((AddressBuffer
== NULL
) ||
1230 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1231 CacheEntry
->Addresses
[Protocol
].Length
1240 if ((AddressBuffer
== NULL
) ||
1243 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1244 CacheEntry
->Addresses
[Hardware
].Length
1258 // Delete this entry.
1260 NetListRemoveEntry (&CacheEntry
->List
);
1261 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
1262 NetFreePool (CacheEntry
);
1272 Delete cache entries in all the cache tables.
1274 @param Instance Pointer to the instance context data.
1275 @param BySwAddress Delete the cache entry by software address or by
1277 @param AddressBuffer Pointer to the buffer containing the address to
1278 match for the deletion.
1279 @param Force This deletion is forced or not.
1281 @return The count of the deleted cache entries.
1285 ArpDeleteCacheEntry (
1286 IN ARP_INSTANCE_DATA
*Instance
,
1287 IN BOOLEAN BySwAddress
,
1288 IN UINT8
*AddressBuffer OPTIONAL
,
1292 ARP_SERVICE_DATA
*ArpService
;
1295 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1297 ArpService
= Instance
->ArpService
;
1300 // Delete the cache entries in the DeniedCacheTable.
1302 Count
= ArpDeleteCacheEntryInTable (
1303 &ArpService
->DeniedCacheTable
,
1305 Instance
->ConfigData
.SwAddressType
,
1311 // Delete the cache entries inthe ResolvedCacheTable.
1313 Count
+= ArpDeleteCacheEntryInTable (
1314 &ArpService
->ResolvedCacheTable
,
1316 Instance
->ConfigData
.SwAddressType
,
1326 Cancel the arp request.
1328 @param Instance Pointer to the instance context data.
1329 @param TargetSwAddress Pointer to the buffer containing the target
1330 software address to match the arp request.
1331 @param UserEvent The user event used to notify this request
1334 @return The count of the cancelled requests.
1339 IN ARP_INSTANCE_DATA
*Instance
,
1340 IN VOID
*TargetSwAddress OPTIONAL
,
1341 IN EFI_EVENT UserEvent OPTIONAL
1344 ARP_SERVICE_DATA
*ArpService
;
1345 NET_LIST_ENTRY
*Entry
;
1346 NET_LIST_ENTRY
*NextEntry
;
1347 ARP_CACHE_ENTRY
*CacheEntry
;
1350 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1352 ArpService
= Instance
->ArpService
;
1355 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
1356 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1358 if ((TargetSwAddress
== NULL
) ||
1361 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1362 CacheEntry
->Addresses
[Protocol
].Length
1365 // This request entry matches the TargetSwAddress or all requests are to be
1366 // cancelled as TargetSwAddress is NULL.
1368 Count
+= ArpAddressResolved (CacheEntry
, Instance
, UserEvent
);
1370 if (NetListIsEmpty (&CacheEntry
->UserRequestList
)) {
1372 // No user requests any more, remove this request cache entry.
1374 NetListRemoveEntry (&CacheEntry
->List
);
1375 NetFreePool (CacheEntry
);
1385 Find the cache entry in the cache table.
1387 @param Instance Pointer to the instance context data.
1388 @param BySwAddress Set to TRUE to look for matching software protocol
1389 addresses. Set to FALSE to look for matching
1390 hardware protocol addresses.
1391 @param AddressBuffer Pointer to address buffer. Set to NULL to match
1393 @param EntryLength The size of an entry in the entries buffer.
1394 @param EntryCount The number of ARP cache entries that are found by
1395 the specified criteria.
1396 @param Entries Pointer to the buffer that will receive the ARP
1398 @param Refresh Set to TRUE to refresh the timeout value of the
1399 matching ARP cache entry.
1401 @retval EFI_SUCCESS The requested ARP cache entries are copied into
1403 @retval EFI_NOT_FOUND No matching entries found.
1404 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
1409 IN ARP_INSTANCE_DATA
*Instance
,
1410 IN BOOLEAN BySwAddress
,
1411 IN VOID
*AddressBuffer OPTIONAL
,
1412 OUT UINT32
*EntryLength OPTIONAL
,
1413 OUT UINT32
*EntryCount OPTIONAL
,
1414 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
1419 ARP_SERVICE_DATA
*ArpService
;
1420 NET_ARP_ADDRESS MatchAddress
;
1421 FIND_OPTYPE FindOpType
;
1422 NET_LIST_ENTRY
*StartEntry
;
1423 ARP_CACHE_ENTRY
*CacheEntry
;
1424 NET_MAP FoundEntries
;
1426 EFI_ARP_FIND_DATA
*FindData
;
1427 NET_LIST_ENTRY
*CacheTable
;
1429 ArpService
= Instance
->ArpService
;
1432 // Init the FounEntries used to hold the found cache entries.
1434 NetMapInit (&FoundEntries
);
1437 // Set the MatchAddress.
1440 MatchAddress
.Type
= Instance
->ConfigData
.SwAddressType
;
1441 MatchAddress
.Length
= Instance
->ConfigData
.SwAddressLength
;
1442 FindOpType
= ByProtoAddress
;
1444 MatchAddress
.Type
= ArpService
->SnpMode
.IfType
;
1445 MatchAddress
.Length
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1446 FindOpType
= ByHwAddress
;
1449 MatchAddress
.AddressPtr
= AddressBuffer
;
1452 // Search the DeniedCacheTable
1457 // Try to find the matched entries in the DeniedCacheTable.
1459 CacheEntry
= ArpFindNextCacheEntryInTable (
1460 &ArpService
->DeniedCacheTable
,
1466 if (CacheEntry
== NULL
) {
1468 // Once the CacheEntry is NULL, there are no more matches.
1474 // Insert the found entry into the map.
1479 (VOID
*)&ArpService
->DeniedCacheTable
1483 // Let the next search start from this cache entry.
1485 StartEntry
= &CacheEntry
->List
;
1489 // Refresh the DecayTime if needed.
1491 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1496 // Search the ResolvedCacheTable
1500 CacheEntry
= ArpFindNextCacheEntryInTable (
1501 &ArpService
->ResolvedCacheTable
,
1507 if (CacheEntry
== NULL
) {
1509 // Once the CacheEntry is NULL, there are no more matches.
1515 // Insert the found entry into the map.
1520 (VOID
*)&ArpService
->ResolvedCacheTable
1524 // Let the next search start from this cache entry.
1526 StartEntry
= &CacheEntry
->List
;
1530 // Refresh the DecayTime if needed.
1532 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1536 Status
= EFI_SUCCESS
;
1538 FoundCount
= (UINT32
) NetMapGetCount (&FoundEntries
);
1539 if (FoundCount
== 0) {
1540 Status
= EFI_NOT_FOUND
;
1544 if (EntryLength
!= NULL
) {
1546 // Return the entry length.
1548 *EntryLength
= sizeof (EFI_ARP_FIND_DATA
) + Instance
->ConfigData
.SwAddressLength
+
1549 ArpService
->SnpMode
.HwAddressSize
;
1552 if (EntryCount
!= NULL
) {
1554 // Return the found entry count.
1556 *EntryCount
= FoundCount
;
1559 if (Entries
== NULL
) {
1564 // Allocate buffer to copy the found entries.
1566 FindData
= NetAllocatePool (FoundCount
* (*EntryLength
));
1567 if (FindData
== NULL
) {
1568 ARP_DEBUG_ERROR (("ArpFindCacheEntry: Failed to allocate memory.\n"));
1569 Status
= EFI_OUT_OF_RESOURCES
;
1574 // Return the address to the user.
1576 *Entries
= FindData
;
1579 // Dump the entries.
1581 while (!NetMapIsEmpty (&FoundEntries
)) {
1583 // Get a cache entry from the map.
1585 CacheEntry
= NetMapRemoveHead (&FoundEntries
, (VOID
**)&CacheTable
);
1588 // Set the fields in FindData.
1590 FindData
->Size
= *EntryLength
;
1591 FindData
->DenyFlag
= (BOOLEAN
)(CacheTable
== &ArpService
->DeniedCacheTable
);
1592 FindData
->StaticFlag
= (BOOLEAN
)(CacheEntry
->DefaultDecayTime
== 0);
1593 FindData
->HwAddressType
= ArpService
->SnpMode
.IfType
;
1594 FindData
->SwAddressType
= Instance
->ConfigData
.SwAddressType
;
1595 FindData
->HwAddressLength
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1596 FindData
->SwAddressLength
= Instance
->ConfigData
.SwAddressLength
;
1599 // Copy the software address.
1603 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1604 FindData
->SwAddressLength
1608 // Copy the hardware address.
1611 (UINT8
*)(FindData
+ 1) + FindData
->SwAddressLength
,
1612 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1613 FindData
->HwAddressLength
1617 // Slip to the next FindData.
1619 FindData
= (EFI_ARP_FIND_DATA
*)((UINT8
*)FindData
+ *EntryLength
);
1624 NetMapClean (&FoundEntries
);