2 The implementation of the ARP protocol.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Global variable of EFI ARP Protocol Interface.
20 EFI_ARP_PROTOCOL mEfiArpProtocolTemplate
= {
32 Initialize the instance context data.
34 @param[in] ArpService Pointer to the arp service context data this
36 @param[out] Instance Pointer to the instance context data.
43 IN ARP_SERVICE_DATA
*ArpService
,
44 OUT ARP_INSTANCE_DATA
*Instance
47 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
49 Instance
->Signature
= ARP_INSTANCE_DATA_SIGNATURE
;
50 Instance
->ArpService
= ArpService
;
52 CopyMem (&Instance
->ArpProto
, &mEfiArpProtocolTemplate
, sizeof (Instance
->ArpProto
));
54 Instance
->Configured
= FALSE
;
55 Instance
->InDestroy
= FALSE
;
57 InitializeListHead (&Instance
->List
);
62 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
64 @param[in] Context Pointer to the context data registerd to the
77 ARP_SERVICE_DATA
*ArpService
;
78 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*RxToken
;
79 EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
;
81 ARP_ADDRESS ArpAddress
;
82 ARP_CACHE_ENTRY
*CacheEntry
;
84 ARP_INSTANCE_DATA
*Instance
;
85 EFI_ARP_CONFIG_DATA
*ConfigData
;
86 NET_ARP_ADDRESS SenderAddress
[2];
91 ArpService
= (ARP_SERVICE_DATA
*)Context
;
92 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
94 RxToken
= &ArpService
->RxToken
;
96 if (RxToken
->Status
== EFI_ABORTED
) {
98 // The Token is aborted, possibly by arp itself, just return and the receiving
99 // process is stopped.
104 if (EFI_ERROR (RxToken
->Status
)) {
106 // Restart the receiving if any other error Status occurs.
108 goto RESTART_RECEIVE
;
112 // Status is EFI_SUCCESS, process the received frame.
114 RxData
= RxToken
->Packet
.RxData
;
115 Head
= (ARP_HEAD
*) RxData
->PacketData
;
118 // Convert the byte order of the multi-byte fields.
120 Head
->HwType
= NTOHS (Head
->HwType
);
121 Head
->ProtoType
= NTOHS (Head
->ProtoType
);
122 Head
->OpCode
= NTOHS (Head
->OpCode
);
124 if ((Head
->HwType
!= ArpService
->SnpMode
.IfType
) ||
125 (Head
->HwAddrLen
!= ArpService
->SnpMode
.HwAddressSize
) ||
126 (RxData
->ProtocolType
!= ARP_ETHER_PROTO_TYPE
)) {
128 // The hardware type or the hardware address length doesn't match.
129 // There is a sanity check for the protocol type too.
135 // Set the pointers to the addresses contained in the arp packet.
137 ArpAddress
.SenderHwAddr
= (UINT8
*)(Head
+ 1);
138 ArpAddress
.SenderProtoAddr
= ArpAddress
.SenderHwAddr
+ Head
->HwAddrLen
;
139 ArpAddress
.TargetHwAddr
= ArpAddress
.SenderProtoAddr
+ Head
->ProtoAddrLen
;
140 ArpAddress
.TargetProtoAddr
= ArpAddress
.TargetHwAddr
+ Head
->HwAddrLen
;
142 SenderAddress
[Hardware
].Type
= Head
->HwType
;
143 SenderAddress
[Hardware
].Length
= Head
->HwAddrLen
;
144 SenderAddress
[Hardware
].AddressPtr
= ArpAddress
.SenderHwAddr
;
146 SenderAddress
[Protocol
].Type
= Head
->ProtoType
;
147 SenderAddress
[Protocol
].Length
= Head
->ProtoAddrLen
;
148 SenderAddress
[Protocol
].AddressPtr
= ArpAddress
.SenderProtoAddr
;
151 // First, check the denied cache table.
153 CacheEntry
= ArpFindDeniedCacheEntry (
155 &SenderAddress
[Protocol
],
156 &SenderAddress
[Hardware
]
158 if (CacheEntry
!= NULL
) {
160 // This address (either hardware or protocol address, or both) is configured to
161 // be a deny entry, silently skip the normal process.
166 ProtoMatched
= FALSE
;
169 NET_LIST_FOR_EACH (Entry
, &ArpService
->ChildrenList
) {
171 // Iterate all the children.
173 Instance
= NET_LIST_USER_STRUCT (Entry
, ARP_INSTANCE_DATA
, List
);
174 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
175 ConfigData
= &Instance
->ConfigData
;
177 if ((Instance
->Configured
) &&
178 (Head
->ProtoType
== ConfigData
->SwAddressType
) &&
179 (Head
->ProtoAddrLen
== ConfigData
->SwAddressLength
)) {
181 // The protocol type is matched for the received arp packet.
184 if (0 == CompareMem (
185 (VOID
*)ArpAddress
.TargetProtoAddr
,
186 ConfigData
->StationAddress
,
187 ConfigData
->SwAddressLength
190 // The arp driver has the target address required by the received arp packet.
200 // Protocol type unmatchable, skip.
206 // Check whether the sender's address information is already in the cache.
209 CacheEntry
= ArpFindNextCacheEntryInTable (
210 &ArpService
->ResolvedCacheTable
,
213 &SenderAddress
[Protocol
],
216 if (CacheEntry
!= NULL
) {
218 // Update the entry with the new information.
220 ArpFillAddressInCacheEntry (CacheEntry
, &SenderAddress
[Hardware
], NULL
);
221 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
227 // This arp packet isn't targeted to us, skip now.
234 // Add the triplet <protocol type, sender protocol address, sender hardware address>
235 // to the translation table.
237 CacheEntry
= ArpFindNextCacheEntryInTable (
238 &ArpService
->PendingRequestTable
,
241 &SenderAddress
[Protocol
],
244 if (CacheEntry
== NULL
) {
246 // Allocate a new CacheEntry.
248 CacheEntry
= ArpAllocCacheEntry (NULL
);
249 if (CacheEntry
== NULL
) {
254 if (!IsListEmpty (&CacheEntry
->List
)) {
255 RemoveEntryList (&CacheEntry
->List
);
259 // Fill the addresses into the CacheEntry.
261 ArpFillAddressInCacheEntry (
263 &SenderAddress
[Hardware
],
264 &SenderAddress
[Protocol
]
270 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
273 // Add this entry into the ResolvedCacheTable
275 InsertHeadList (&ArpService
->ResolvedCacheTable
, &CacheEntry
->List
);
278 if (Head
->OpCode
== ARP_OPCODE_REQUEST
) {
280 // Send back the ARP Reply. If we reach here, Instance is not NULL and CacheEntry
283 ArpSendFrame (Instance
, CacheEntry
, ARP_OPCODE_REPLY
);
289 // Signal Mnp to recycle the RxData.
291 gBS
->SignalEvent (RxData
->RecycleEvent
);
296 // Continue to receive packets from Mnp.
298 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, RxToken
);
301 if (EFI_ERROR (Status
)) {
302 DEBUG ((EFI_D_ERROR
, "ArpOnFrameRcvd: ArpService->Mnp->Receive "
303 "failed, %r\n.", Status
));
309 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
311 @param[in] Event The Event this notify function registered to.
312 @param[in] Context Pointer to the context data registerd to the
326 // Request ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK
328 QueueDpc (TPL_CALLBACK
, ArpOnFrameRcvdDpc
, Context
);
332 Process the already sent arp packets.
334 @param[in] Context Pointer to the context data registerd to the
346 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
347 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
349 ASSERT (Context
!= NULL
);
351 TxToken
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*)Context
;
352 TxData
= TxToken
->Packet
.TxData
;
355 if (EFI_ERROR (TxToken
->Status
)) {
356 DEBUG ((EFI_D_ERROR
, "ArpOnFrameSent: TxToken->Status, %r.\n", TxToken
->Status
));
361 // Free the allocated memory and close the event.
363 FreePool (TxData
->FragmentTable
[0].FragmentBuffer
);
365 gBS
->CloseEvent (TxToken
->Event
);
370 Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK.
372 @param[in] Event The Event this notify function registered to.
373 @param[in] Context Pointer to the context data registerd to the
387 // Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK
389 QueueDpc (TPL_CALLBACK
, ArpOnFrameSentDpc
, Context
);
394 Process the arp cache olding and drive the retrying arp requests.
396 @param[in] Event The Event this notify function registered to.
397 @param[in] Context Pointer to the context data registerd to the
410 ARP_SERVICE_DATA
*ArpService
;
412 LIST_ENTRY
*NextEntry
;
413 LIST_ENTRY
*ContextEntry
;
414 ARP_CACHE_ENTRY
*CacheEntry
;
415 USER_REQUEST_CONTEXT
*RequestContext
;
417 ASSERT (Context
!= NULL
);
418 ArpService
= (ARP_SERVICE_DATA
*)Context
;
421 // Iterate all the pending requests to see whether a retry is needed to send out
422 // or the request finally fails because the retry time reaches the limitation.
424 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
425 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
427 if (CacheEntry
->NextRetryTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
429 // Timeout, if we can retry more, send out the request again, otherwise abort
432 if (CacheEntry
->RetryCount
== 0) {
434 // Abort this request.
436 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
437 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
439 RemoveEntryList (&CacheEntry
->List
);
440 FreePool (CacheEntry
);
443 // resend the ARP request.
445 ASSERT (!IsListEmpty(&CacheEntry
->UserRequestList
));
447 ContextEntry
= CacheEntry
->UserRequestList
.ForwardLink
;
448 RequestContext
= NET_LIST_USER_STRUCT (ContextEntry
, USER_REQUEST_CONTEXT
, List
);
450 ArpSendFrame (RequestContext
->Instance
, CacheEntry
, ARP_OPCODE_REQUEST
);
452 CacheEntry
->RetryCount
--;
453 CacheEntry
->NextRetryTime
= RequestContext
->Instance
->ConfigData
.RetryTimeOut
;
457 // Update the NextRetryTime.
459 CacheEntry
->NextRetryTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
464 // Check the timeouts for the DeniedCacheTable.
466 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->DeniedCacheTable
) {
467 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
468 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
470 if (CacheEntry
->DefaultDecayTime
== 0) {
472 // It's a static entry, skip it.
477 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
479 // Time out, remove it.
481 RemoveEntryList (&CacheEntry
->List
);
482 FreePool (CacheEntry
);
485 // Update the DecayTime.
487 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
492 // Check the timeouts for the ResolvedCacheTable.
494 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->ResolvedCacheTable
) {
495 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
496 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
498 if (CacheEntry
->DefaultDecayTime
== 0) {
500 // It's a static entry, skip it.
505 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
507 // Time out, remove it.
509 RemoveEntryList (&CacheEntry
->List
);
510 FreePool (CacheEntry
);
513 // Update the DecayTime.
515 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
522 Match the two NET_ARP_ADDRESSes.
524 @param[in] AddressOne Pointer to the first address to match.
525 @param[in] AddressTwo Pointer to the second address to match.
527 @return The two addresses match or not.
532 IN NET_ARP_ADDRESS
*AddressOne
,
533 IN NET_ARP_ADDRESS
*AddressTwo
536 ASSERT (AddressOne
!= NULL
&& AddressTwo
!= NULL
);
538 if ((AddressOne
->Type
!= AddressTwo
->Type
) ||
539 (AddressOne
->Length
!= AddressTwo
->Length
)) {
541 // Either Type or Length doesn't match.
546 if ((AddressOne
->AddressPtr
!= NULL
) &&
548 AddressOne
->AddressPtr
,
549 AddressTwo
->AddressPtr
,
553 // The address is not the same.
563 Find the CacheEntry which matches the requirements in the specified CacheTable.
565 @param[in] CacheTable Pointer to the arp cache table.
566 @param[in] StartEntry Pointer to the start entry this search begins with
568 @param[in] FindOpType The search type.
569 @param[in] ProtocolAddress Pointer to the protocol address to match.
570 @param[in] HardwareAddress Pointer to the hardware address to match.
572 @return Pointer to the matched arp cache entry, if NULL, no match is found.
576 ArpFindNextCacheEntryInTable (
577 IN LIST_ENTRY
*CacheTable
,
578 IN LIST_ENTRY
*StartEntry
,
579 IN FIND_OPTYPE FindOpType
,
580 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
581 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
585 ARP_CACHE_ENTRY
*CacheEntry
;
587 if (StartEntry
== NULL
) {
589 // Start from the beginning of the table if no StartEntry is specified.
591 StartEntry
= CacheTable
;
594 for (Entry
= StartEntry
->ForwardLink
; Entry
!= CacheTable
; Entry
= Entry
->ForwardLink
) {
595 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
597 if ((FindOpType
& MATCH_SW_ADDRESS
) != 0) {
599 // Find by the software address.
601 if (!ArpMatchAddress (ProtocolAddress
, &CacheEntry
->Addresses
[Protocol
])) {
603 // The ProtocolAddress doesn't match, continue to the next cache entry.
609 if ((FindOpType
& MATCH_HW_ADDRESS
) != 0) {
611 // Find by the hardware address.
613 if (!ArpMatchAddress (HardwareAddress
, &CacheEntry
->Addresses
[Hardware
])) {
615 // The HardwareAddress doesn't match, continue to the next cache entry.
622 // The CacheEntry meets the requirements now, return this entry.
635 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
636 in the DeniedCacheTable.
638 @param[in] ArpService Pointer to the arp service context data.
639 @param[in] ProtocolAddress Pointer to the protocol address.
640 @param[in] HardwareAddress Pointer to the hardware address.
642 @return Pointer to the matched cache entry, if NULL no match is found.
646 ArpFindDeniedCacheEntry (
647 IN ARP_SERVICE_DATA
*ArpService
,
648 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
649 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
652 ARP_CACHE_ENTRY
*CacheEntry
;
654 ASSERT ((ProtocolAddress
!= NULL
) || (HardwareAddress
!= NULL
));
655 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
659 if ((ProtocolAddress
!= NULL
) && (ProtocolAddress
->AddressPtr
!= NULL
)) {
661 // Find the cache entry in the DeniedCacheTable by the protocol address.
663 CacheEntry
= ArpFindNextCacheEntryInTable (
664 &ArpService
->DeniedCacheTable
,
670 if (CacheEntry
!= NULL
) {
678 if ((HardwareAddress
!= NULL
) && (HardwareAddress
->AddressPtr
!= NULL
)) {
680 // Find the cache entry in the DeniedCacheTable by the hardware address.
682 CacheEntry
= ArpFindNextCacheEntryInTable (
683 &ArpService
->DeniedCacheTable
,
696 Allocate a cache entry and initialize it.
698 @param[in] Instance Pointer to the instance context data.
700 @return Pointer to the new created cache entry.
705 IN ARP_INSTANCE_DATA
*Instance
708 ARP_CACHE_ENTRY
*CacheEntry
;
709 NET_ARP_ADDRESS
*Address
;
713 // Allocate memory for the cache entry.
715 CacheEntry
= AllocatePool (sizeof (ARP_CACHE_ENTRY
));
716 if (CacheEntry
== NULL
) {
723 InitializeListHead (&CacheEntry
->List
);
724 InitializeListHead (&CacheEntry
->UserRequestList
);
726 for (Index
= 0; Index
< 2; Index
++) {
728 // Init the address pointers to point to the concrete buffer.
730 Address
= &CacheEntry
->Addresses
[Index
];
731 Address
->AddressPtr
= Address
->Buffer
.ProtoAddress
;
735 // Zero the hardware address first.
737 ZeroMem (CacheEntry
->Addresses
[Hardware
].AddressPtr
, ARP_MAX_HARDWARE_ADDRESS_LEN
);
739 if (Instance
!= NULL
) {
741 // Inherit the parameters from the instance configuration.
743 CacheEntry
->RetryCount
= Instance
->ConfigData
.RetryCount
;
744 CacheEntry
->NextRetryTime
= Instance
->ConfigData
.RetryTimeOut
;
745 CacheEntry
->DefaultDecayTime
= Instance
->ConfigData
.EntryTimeOut
;
746 CacheEntry
->DecayTime
= Instance
->ConfigData
.EntryTimeOut
;
749 // Use the default parameters if this cache entry isn't allocate in a
752 CacheEntry
->RetryCount
= ARP_DEFAULT_RETRY_COUNT
;
753 CacheEntry
->NextRetryTime
= ARP_DEFAULT_RETRY_INTERVAL
;
754 CacheEntry
->DefaultDecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
755 CacheEntry
->DecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
763 Turn the CacheEntry into the resolved status.
765 @param[in] CacheEntry Pointer to the resolved cache entry.
766 @param[in] Instance Pointer to the instance context data.
767 @param[in] UserEvent Pointer to the UserEvent to notify.
769 @return The count of notifications sent to the instance.
774 IN ARP_CACHE_ENTRY
*CacheEntry
,
775 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
776 IN EFI_EVENT UserEvent OPTIONAL
780 LIST_ENTRY
*NextEntry
;
781 USER_REQUEST_CONTEXT
*Context
;
787 // Iterate all the linked user requests to notify them.
789 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &CacheEntry
->UserRequestList
) {
790 Context
= NET_LIST_USER_STRUCT (Entry
, USER_REQUEST_CONTEXT
, List
);
792 if (((Instance
== NULL
) || (Context
->Instance
== Instance
)) &&
793 ((UserEvent
== NULL
) || (Context
->UserRequestEvent
== UserEvent
))) {
795 // Copy the address to the user-provided buffer and notify the user.
798 Context
->UserHwAddrBuffer
,
799 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
800 CacheEntry
->Addresses
[Hardware
].Length
802 gBS
->SignalEvent (Context
->UserRequestEvent
);
805 // Remove this user request and free the context data.
807 RemoveEntryList (&Context
->List
);
815 // Dispatch the DPCs queued by the NotifyFunction of the Context->UserRequestEvent.
824 Fill the addresses in the CacheEntry using the information passed in by
827 @param[in] CacheEntry Pointer to the cache entry.
828 @param[in] HwAddr Pointer to the software address.
829 @param[in] SwAddr Pointer to the hardware address.
835 ArpFillAddressInCacheEntry (
836 IN ARP_CACHE_ENTRY
*CacheEntry
,
837 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
838 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
841 NET_ARP_ADDRESS
*Address
[2];
842 NET_ARP_ADDRESS
*CacheAddress
;
845 Address
[Hardware
] = HwAddr
;
846 Address
[Protocol
] = SwAddr
;
848 for (Index
= 0; Index
< 2; Index
++) {
849 if (Address
[Index
] != NULL
) {
851 // Fill the address if the passed in pointer is not NULL.
853 CacheAddress
= &CacheEntry
->Addresses
[Index
];
855 CacheAddress
->Type
= Address
[Index
]->Type
;
856 CacheAddress
->Length
= Address
[Index
]->Length
;
858 if (Address
[Index
]->AddressPtr
!= NULL
) {
860 // Copy it if the AddressPtr points to some buffer.
863 CacheAddress
->AddressPtr
,
864 Address
[Index
]->AddressPtr
,
869 // Zero the corresponding address buffer in the CacheEntry.
871 ZeroMem (CacheAddress
->AddressPtr
, CacheAddress
->Length
);
879 Configure the instance using the ConfigData. ConfigData is already validated.
881 @param[in] Instance Pointer to the instance context data to be
883 @param[in] ConfigData Pointer to the configuration data used to
884 configure the instance.
886 @retval EFI_SUCCESS The instance is configured with the ConfigData.
887 @retval EFI_ACCESS_DENIED The instance is already configured and the
888 ConfigData tries to reset some unchangeable
890 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
891 when the SwAddressType is IPv4.
892 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
897 ArpConfigureInstance (
898 IN ARP_INSTANCE_DATA
*Instance
,
899 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
902 EFI_ARP_CONFIG_DATA
*OldConfigData
;
905 OldConfigData
= &Instance
->ConfigData
;
907 if (ConfigData
!= NULL
) {
909 if (Instance
->Configured
) {
911 // The instance is configured, check the unchangeable fields.
913 if ((OldConfigData
->SwAddressType
!= ConfigData
->SwAddressType
) ||
914 (OldConfigData
->SwAddressLength
!= ConfigData
->SwAddressLength
) ||
916 OldConfigData
->StationAddress
,
917 ConfigData
->StationAddress
,
918 OldConfigData
->SwAddressLength
921 // Deny the unallowed changes.
923 return EFI_ACCESS_DENIED
;
927 // The instance is not configured.
930 if (ConfigData
->SwAddressType
== IPV4_ETHER_PROTO_TYPE
) {
931 CopyMem (&Ip
, ConfigData
->StationAddress
, sizeof (IP4_ADDR
));
933 if (!NetIp4IsUnicast (NTOHL (Ip
), 0)) {
935 // The station address is not a valid IPv4 unicast address.
937 return EFI_INVALID_PARAMETER
;
942 // Save the configuration.
944 CopyMem (OldConfigData
, ConfigData
, sizeof (*OldConfigData
));
946 OldConfigData
->StationAddress
= AllocatePool (OldConfigData
->SwAddressLength
);
947 if (OldConfigData
->StationAddress
== NULL
) {
948 DEBUG ((EFI_D_ERROR
, "ArpConfigInstance: AllocatePool for the StationAddress "
950 return EFI_OUT_OF_RESOURCES
;
954 // Save the StationAddress.
957 OldConfigData
->StationAddress
,
958 ConfigData
->StationAddress
,
959 OldConfigData
->SwAddressLength
963 // Set the state to configured.
965 Instance
->Configured
= TRUE
;
969 // Use the implementation specific values if the following field is zero.
971 OldConfigData
->EntryTimeOut
= (ConfigData
->EntryTimeOut
== 0) ?
972 ARP_DEFAULT_TIMEOUT_VALUE
: ConfigData
->EntryTimeOut
;
974 OldConfigData
->RetryCount
= (ConfigData
->RetryCount
== 0) ?
975 ARP_DEFAULT_RETRY_COUNT
: ConfigData
->RetryCount
;
977 OldConfigData
->RetryTimeOut
= (ConfigData
->RetryTimeOut
== 0) ?
978 ARP_DEFAULT_RETRY_INTERVAL
: ConfigData
->RetryTimeOut
;
981 // Reset the configuration.
984 if (Instance
->Configured
) {
986 // Cancel the arp requests issued by this instance.
988 Instance
->ArpProto
.Cancel (&Instance
->ArpProto
, NULL
, NULL
);
991 // Free the buffer previously allocated to hold the station address.
993 FreePool (OldConfigData
->StationAddress
);
996 Instance
->Configured
= FALSE
;
1004 Send out an arp frame using the CachEntry and the ArpOpCode.
1006 @param[in] Instance Pointer to the instance context data.
1007 @param[in] CacheEntry Pointer to the configuration data used to
1008 configure the instance.
1009 @param[in] ArpOpCode The opcode used to send out this Arp frame, either
1017 IN ARP_INSTANCE_DATA
*Instance
,
1018 IN ARP_CACHE_ENTRY
*CacheEntry
,
1023 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
1024 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
1027 ARP_SERVICE_DATA
*ArpService
;
1028 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1029 EFI_ARP_CONFIG_DATA
*ConfigData
;
1033 ASSERT ((Instance
!= NULL
) && (CacheEntry
!= NULL
));
1036 // Allocate memory for the TxToken.
1038 TxToken
= AllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN
));
1039 if (TxToken
== NULL
) {
1040 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for TxToken failed.\n"));
1044 TxToken
->Event
= NULL
;
1049 // Create the event for this TxToken.
1051 Status
= gBS
->CreateEvent (
1058 if (EFI_ERROR (Status
)) {
1059 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: CreateEvent failed for TxToken->Event.\n"));
1064 // Allocate memory for the TxData used in the TxToken.
1066 TxData
= AllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA
));
1067 if (TxData
== NULL
) {
1068 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for TxData failed.\n"));
1072 ArpService
= Instance
->ArpService
;
1073 SnpMode
= &ArpService
->SnpMode
;
1074 ConfigData
= &Instance
->ConfigData
;
1077 // Calculate the buffer length for this arp frame.
1079 TotalLength
= SnpMode
->MediaHeaderSize
+ sizeof (ARP_HEAD
) +
1080 2 * (ConfigData
->SwAddressLength
+ SnpMode
->HwAddressSize
);
1083 // Allocate buffer for the arp frame.
1085 Packet
= AllocatePool (TotalLength
);
1086 if (Packet
== NULL
) {
1087 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for Packet failed.\n"));
1088 ASSERT (Packet
!= NULL
);
1094 // The destination MAC address.
1096 if (ArpOpCode
== ARP_OPCODE_REQUEST
) {
1097 CopyMem (TmpPtr
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
);
1101 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1102 SnpMode
->HwAddressSize
1105 TmpPtr
+= SnpMode
->HwAddressSize
;
1108 // The source MAC address.
1110 CopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1111 TmpPtr
+= SnpMode
->HwAddressSize
;
1114 // The ethernet protocol type.
1116 *(UINT16
*)TmpPtr
= HTONS (ARP_ETHER_PROTO_TYPE
);
1122 ArpHead
= (ARP_HEAD
*) TmpPtr
;
1123 ArpHead
->HwType
= HTONS ((UINT16
)SnpMode
->IfType
);
1124 ArpHead
->ProtoType
= HTONS (ConfigData
->SwAddressType
);
1125 ArpHead
->HwAddrLen
= (UINT8
)SnpMode
->HwAddressSize
;
1126 ArpHead
->ProtoAddrLen
= ConfigData
->SwAddressLength
;
1127 ArpHead
->OpCode
= HTONS (ArpOpCode
);
1128 TmpPtr
+= sizeof (ARP_HEAD
);
1131 // The sender hardware address.
1133 CopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1134 TmpPtr
+= SnpMode
->HwAddressSize
;
1137 // The sender protocol address.
1139 CopyMem (TmpPtr
, ConfigData
->StationAddress
, ConfigData
->SwAddressLength
);
1140 TmpPtr
+= ConfigData
->SwAddressLength
;
1143 // The target hardware address.
1147 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1148 SnpMode
->HwAddressSize
1150 TmpPtr
+= SnpMode
->HwAddressSize
;
1153 // The target protocol address.
1157 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1158 ConfigData
->SwAddressLength
1162 // Set all the fields of the TxData.
1164 TxData
->DestinationAddress
= NULL
;
1165 TxData
->SourceAddress
= NULL
;
1166 TxData
->ProtocolType
= 0;
1167 TxData
->DataLength
= TotalLength
- SnpMode
->MediaHeaderSize
;
1168 TxData
->HeaderLength
= (UINT16
) SnpMode
->MediaHeaderSize
;
1169 TxData
->FragmentCount
= 1;
1171 TxData
->FragmentTable
[0].FragmentBuffer
= Packet
;
1172 TxData
->FragmentTable
[0].FragmentLength
= TotalLength
;
1175 // Associate the TxData with the TxToken.
1177 TxToken
->Packet
.TxData
= TxData
;
1178 TxToken
->Status
= EFI_NOT_READY
;
1181 // Send out this arp packet by Mnp.
1183 Status
= ArpService
->Mnp
->Transmit (ArpService
->Mnp
, TxToken
);
1184 if (EFI_ERROR (Status
)) {
1185 DEBUG ((EFI_D_ERROR
, "Mnp->Transmit failed, %r.\n", Status
));
1193 if (Packet
!= NULL
) {
1197 if (TxData
!= NULL
) {
1201 if (TxToken
->Event
!= NULL
) {
1202 gBS
->CloseEvent (TxToken
->Event
);
1210 Delete the cache entries in the specified CacheTable, using the BySwAddress,
1211 SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE,
1212 the cache is deleted event it's a static entry.
1214 @param[in] CacheTable Pointer to the cache table to do the deletion.
1215 @param[in] BySwAddress Delete the cache entry by software address or by
1217 @param[in] SwAddressType The software address used to do the deletion.
1218 @param[in] AddressBuffer Pointer to the buffer containing the address to
1219 match for the deletion.
1220 @param[in] Force This deletion is forced or not.
1222 @return The count of the deleted cache entries.
1226 ArpDeleteCacheEntryInTable (
1227 IN LIST_ENTRY
*CacheTable
,
1228 IN BOOLEAN BySwAddress
,
1229 IN UINT16 SwAddressType
,
1230 IN UINT8
*AddressBuffer OPTIONAL
,
1235 LIST_ENTRY
*NextEntry
;
1236 ARP_CACHE_ENTRY
*CacheEntry
;
1241 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, CacheTable
) {
1242 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1244 if ((CacheEntry
->DefaultDecayTime
== 0) && !Force
) {
1246 // It's a static entry and we are not forced to delete it, skip.
1252 if (SwAddressType
== CacheEntry
->Addresses
[Protocol
].Type
) {
1254 // Protocol address type matched. Check the address.
1256 if ((AddressBuffer
== NULL
) ||
1259 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1260 CacheEntry
->Addresses
[Protocol
].Length
1269 if ((AddressBuffer
== NULL
) ||
1272 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1273 CacheEntry
->Addresses
[Hardware
].Length
1287 // Delete this entry.
1289 RemoveEntryList (&CacheEntry
->List
);
1290 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
1291 FreePool (CacheEntry
);
1301 Delete cache entries in all the cache tables.
1303 @param[in] Instance Pointer to the instance context data.
1304 @param[in] BySwAddress Delete the cache entry by software address or by
1306 @param[in] AddressBuffer Pointer to the buffer containing the address to
1307 match for the deletion.
1308 @param[in] Force This deletion is forced or not.
1310 @return The count of the deleted cache entries.
1314 ArpDeleteCacheEntry (
1315 IN ARP_INSTANCE_DATA
*Instance
,
1316 IN BOOLEAN BySwAddress
,
1317 IN UINT8
*AddressBuffer OPTIONAL
,
1321 ARP_SERVICE_DATA
*ArpService
;
1324 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1326 ArpService
= Instance
->ArpService
;
1329 // Delete the cache entries in the DeniedCacheTable.
1331 Count
= ArpDeleteCacheEntryInTable (
1332 &ArpService
->DeniedCacheTable
,
1334 Instance
->ConfigData
.SwAddressType
,
1340 // Delete the cache entries inthe ResolvedCacheTable.
1342 Count
+= ArpDeleteCacheEntryInTable (
1343 &ArpService
->ResolvedCacheTable
,
1345 Instance
->ConfigData
.SwAddressType
,
1355 Cancel the arp request.
1357 @param[in] Instance Pointer to the instance context data.
1358 @param[in] TargetSwAddress Pointer to the buffer containing the target
1359 software address to match the arp request.
1360 @param[in] UserEvent The user event used to notify this request
1363 @return The count of the cancelled requests.
1368 IN ARP_INSTANCE_DATA
*Instance
,
1369 IN VOID
*TargetSwAddress OPTIONAL
,
1370 IN EFI_EVENT UserEvent OPTIONAL
1373 ARP_SERVICE_DATA
*ArpService
;
1375 LIST_ENTRY
*NextEntry
;
1376 ARP_CACHE_ENTRY
*CacheEntry
;
1379 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1381 ArpService
= Instance
->ArpService
;
1384 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
1385 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1387 if ((TargetSwAddress
== NULL
) ||
1390 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1391 CacheEntry
->Addresses
[Protocol
].Length
1394 // This request entry matches the TargetSwAddress or all requests are to be
1395 // cancelled as TargetSwAddress is NULL.
1397 Count
+= ArpAddressResolved (CacheEntry
, Instance
, UserEvent
);
1399 if (IsListEmpty (&CacheEntry
->UserRequestList
)) {
1401 // No user requests any more, remove this request cache entry.
1403 RemoveEntryList (&CacheEntry
->List
);
1404 FreePool (CacheEntry
);
1414 Find the cache entry in the cache table.
1416 @param[in] Instance Pointer to the instance context data.
1417 @param[in] BySwAddress Set to TRUE to look for matching software protocol
1418 addresses. Set to FALSE to look for matching
1419 hardware protocol addresses.
1420 @param[in] AddressBuffer Pointer to address buffer. Set to NULL to match
1422 @param[out] EntryLength The size of an entry in the entries buffer.
1423 @param[out] EntryCount The number of ARP cache entries that are found by
1424 the specified criteria.
1425 @param[out] Entries Pointer to the buffer that will receive the ARP
1427 @param[in] Refresh Set to TRUE to refresh the timeout value of the
1428 matching ARP cache entry.
1430 @retval EFI_SUCCESS The requested ARP cache entries are copied into
1432 @retval EFI_NOT_FOUND No matching entries found.
1433 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
1438 IN ARP_INSTANCE_DATA
*Instance
,
1439 IN BOOLEAN BySwAddress
,
1440 IN VOID
*AddressBuffer OPTIONAL
,
1441 OUT UINT32
*EntryLength OPTIONAL
,
1442 OUT UINT32
*EntryCount OPTIONAL
,
1443 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
1448 ARP_SERVICE_DATA
*ArpService
;
1449 NET_ARP_ADDRESS MatchAddress
;
1450 FIND_OPTYPE FindOpType
;
1451 LIST_ENTRY
*StartEntry
;
1452 ARP_CACHE_ENTRY
*CacheEntry
;
1453 NET_MAP FoundEntries
;
1455 EFI_ARP_FIND_DATA
*FindData
;
1456 LIST_ENTRY
*CacheTable
;
1457 UINT32 FoundEntryLength
;
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
;
1575 // Found the entry length, make sure its 8 bytes alignment.
1577 FoundEntryLength
= (((sizeof (EFI_ARP_FIND_DATA
) + Instance
->ConfigData
.SwAddressLength
+
1578 ArpService
->SnpMode
.HwAddressSize
) + 3) & ~(0x3));
1580 if (EntryLength
!= NULL
) {
1581 *EntryLength
= FoundEntryLength
;
1584 if (EntryCount
!= NULL
) {
1586 // Return the found entry count.
1588 *EntryCount
= FoundCount
;
1591 if (Entries
== NULL
) {
1596 // Allocate buffer to copy the found entries.
1598 FindData
= AllocatePool (FoundCount
* FoundEntryLength
);
1599 if (FindData
== NULL
) {
1600 DEBUG ((EFI_D_ERROR
, "ArpFindCacheEntry: Failed to allocate memory.\n"));
1601 Status
= EFI_OUT_OF_RESOURCES
;
1606 // Return the address to the user.
1608 *Entries
= FindData
;
1611 // Dump the entries.
1613 while (!NetMapIsEmpty (&FoundEntries
)) {
1615 // Get a cache entry from the map.
1617 CacheEntry
= NetMapRemoveHead (&FoundEntries
, (VOID
**)&CacheTable
);
1620 // Set the fields in FindData.
1622 FindData
->Size
= FoundEntryLength
;
1623 FindData
->DenyFlag
= (BOOLEAN
)(CacheTable
== &ArpService
->DeniedCacheTable
);
1624 FindData
->StaticFlag
= (BOOLEAN
)(CacheEntry
->DefaultDecayTime
== 0);
1625 FindData
->HwAddressType
= ArpService
->SnpMode
.IfType
;
1626 FindData
->SwAddressType
= Instance
->ConfigData
.SwAddressType
;
1627 FindData
->HwAddressLength
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1628 FindData
->SwAddressLength
= Instance
->ConfigData
.SwAddressLength
;
1631 // Copy the software address.
1635 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1636 FindData
->SwAddressLength
1640 // Copy the hardware address.
1643 (UINT8
*)(FindData
+ 1) + FindData
->SwAddressLength
,
1644 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1645 FindData
->HwAddressLength
1649 // Slip to the next FindData.
1651 FindData
= (EFI_ARP_FIND_DATA
*)((UINT8
*)FindData
+ FoundEntryLength
);
1656 NetMapClean (&FoundEntries
);