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.
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.
537 IN NET_ARP_ADDRESS
*AddressOne
,
538 IN NET_ARP_ADDRESS
*AddressTwo
541 if ((AddressOne
->Type
!= AddressTwo
->Type
) ||
542 (AddressOne
->Length
!= AddressTwo
->Length
)) {
544 // Either Type or Length doesn't match.
549 if ((AddressOne
->AddressPtr
!= NULL
) &&
551 AddressOne
->AddressPtr
,
552 AddressTwo
->AddressPtr
,
556 // The address is not the same.
566 Find the CacheEntry which matches the requirements in the specified CacheTable.
568 @param CacheTable Pointer to the arp cache table.
569 @param StartEntry Pointer to the start entry this search begins with
571 @param FindOpType The search type.
572 @param ProtocolAddress Pointer to the protocol address to match.
573 @param HardwareAddress Pointer to the hardware address to match.
575 @return Pointer to the matched arp cache entry, if NULL, no match is found.
579 ArpFindNextCacheEntryInTable (
580 IN LIST_ENTRY
*CacheTable
,
581 IN LIST_ENTRY
*StartEntry
,
582 IN FIND_OPTYPE FindOpType
,
583 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
584 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
588 ARP_CACHE_ENTRY
*CacheEntry
;
590 if (StartEntry
== NULL
) {
592 // Start from the beginning of the table if no StartEntry is specified.
594 StartEntry
= CacheTable
;
597 for (Entry
= StartEntry
->ForwardLink
; Entry
!= CacheTable
; Entry
= Entry
->ForwardLink
) {
598 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
600 if (FindOpType
& MATCH_SW_ADDRESS
) {
602 // Find by the software address.
604 if (!ArpMatchAddress (ProtocolAddress
, &CacheEntry
->Addresses
[Protocol
])) {
606 // The ProtocolAddress doesn't match, continue to the next cache entry.
612 if (FindOpType
& MATCH_HW_ADDRESS
) {
614 // Find by the hardware address.
616 if (!ArpMatchAddress (HardwareAddress
, &CacheEntry
->Addresses
[Hardware
])) {
618 // The HardwareAddress doesn't match, continue to the next cache entry.
625 // The CacheEntry meets the requirements now, return this entry.
638 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
639 in the DeniedCacheTable.
641 @param ArpService Pointer to the arp service context data.
642 @param ProtocolAddress Pointer to the protocol address.
643 @param HardwareAddress Pointer to the hardware address.
645 @return Pointer to the matched cache entry, if NULL no match is found.
649 ArpFindDeniedCacheEntry (
650 IN ARP_SERVICE_DATA
*ArpService
,
651 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
652 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
655 ARP_CACHE_ENTRY
*CacheEntry
;
657 ASSERT ((ProtocolAddress
!= NULL
) || (HardwareAddress
!= NULL
));
658 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
662 if ((ProtocolAddress
!= NULL
) && (ProtocolAddress
->AddressPtr
!= NULL
)) {
664 // Find the cache entry in the DeniedCacheTable by the protocol address.
666 CacheEntry
= ArpFindNextCacheEntryInTable (
667 &ArpService
->DeniedCacheTable
,
673 if (CacheEntry
!= NULL
) {
681 if ((HardwareAddress
!= NULL
) && (HardwareAddress
->AddressPtr
!= NULL
)) {
683 // Find the cache entry in the DeniedCacheTable by the hardware address.
685 CacheEntry
= ArpFindNextCacheEntryInTable (
686 &ArpService
->DeniedCacheTable
,
699 Allocate a cache entry and initialize it.
701 @param Instance Pointer to the instance context data.
703 @return Pointer to the new created cache entry.
708 IN ARP_INSTANCE_DATA
*Instance
711 ARP_CACHE_ENTRY
*CacheEntry
;
712 NET_ARP_ADDRESS
*Address
;
716 // Allocate memory for the cache entry.
718 CacheEntry
= AllocatePool (sizeof (ARP_CACHE_ENTRY
));
719 if (CacheEntry
== NULL
) {
726 InitializeListHead (&CacheEntry
->List
);
727 InitializeListHead (&CacheEntry
->UserRequestList
);
729 for (Index
= 0; Index
< 2; Index
++) {
731 // Init the address pointers to point to the concrete buffer.
733 Address
= &CacheEntry
->Addresses
[Index
];
734 Address
->AddressPtr
= Address
->Buffer
.ProtoAddress
;
738 // Zero the hardware address first.
740 ZeroMem (CacheEntry
->Addresses
[Hardware
].AddressPtr
, ARP_MAX_HARDWARE_ADDRESS_LEN
);
742 if (Instance
!= NULL
) {
744 // Inherit the parameters from the instance configuration.
746 CacheEntry
->RetryCount
= Instance
->ConfigData
.RetryCount
;
747 CacheEntry
->NextRetryTime
= Instance
->ConfigData
.RetryTimeOut
;
748 CacheEntry
->DefaultDecayTime
= Instance
->ConfigData
.EntryTimeOut
;
749 CacheEntry
->DecayTime
= Instance
->ConfigData
.EntryTimeOut
;
752 // Use the default parameters if this cache entry isn't allocate in a
755 CacheEntry
->RetryCount
= ARP_DEFAULT_RETRY_COUNT
;
756 CacheEntry
->NextRetryTime
= ARP_DEFAULT_RETRY_INTERVAL
;
757 CacheEntry
->DefaultDecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
758 CacheEntry
->DecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
766 Turn the CacheEntry into the resolved status.
768 @param CacheEntry Pointer to the resolved cache entry.
769 @param Instance Pointer to the instance context data.
770 @param UserEvent Pointer to the UserEvent to notify.
772 @return The count of notifications sent to the instance.
777 IN ARP_CACHE_ENTRY
*CacheEntry
,
778 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
779 IN EFI_EVENT UserEvent OPTIONAL
783 LIST_ENTRY
*NextEntry
;
784 USER_REQUEST_CONTEXT
*Context
;
790 // Iterate all the linked user requests to notify them.
792 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &CacheEntry
->UserRequestList
) {
793 Context
= NET_LIST_USER_STRUCT (Entry
, USER_REQUEST_CONTEXT
, List
);
795 if (((Instance
== NULL
) || (Context
->Instance
== Instance
)) &&
796 ((UserEvent
== NULL
) || (Context
->UserRequestEvent
== UserEvent
))) {
798 // Copy the address to the user-provided buffer and notify the user.
801 Context
->UserHwAddrBuffer
,
802 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
803 CacheEntry
->Addresses
[Hardware
].Length
805 gBS
->SignalEvent (Context
->UserRequestEvent
);
808 // Remove this user request and free the context data.
810 RemoveEntryList (&Context
->List
);
811 gBS
->FreePool (Context
);
818 // Dispatch the DPCs queued by the NotifyFunction of the Context->UserRequestEvent.
820 NetLibDispatchDpc ();
827 Fill the addresses in the CacheEntry using the information passed in by
830 @param CacheEntry Pointer to the cache entry.
831 @param HwAddr Pointer to the software address.
832 @param SwAddr Pointer to the hardware address.
838 ArpFillAddressInCacheEntry (
839 IN ARP_CACHE_ENTRY
*CacheEntry
,
840 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
841 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
844 NET_ARP_ADDRESS
*Address
[2];
845 NET_ARP_ADDRESS
*CacheAddress
;
848 Address
[Hardware
] = HwAddr
;
849 Address
[Protocol
] = SwAddr
;
851 for (Index
= 0; Index
< 2; Index
++) {
852 if (Address
[Index
] != NULL
) {
854 // Fill the address if the passed in pointer is not NULL.
856 CacheAddress
= &CacheEntry
->Addresses
[Index
];
858 CacheAddress
->Type
= Address
[Index
]->Type
;
859 CacheAddress
->Length
= Address
[Index
]->Length
;
861 if (Address
[Index
]->AddressPtr
!= NULL
) {
863 // Copy it if the AddressPtr points to some buffer.
866 CacheAddress
->AddressPtr
,
867 Address
[Index
]->AddressPtr
,
872 // Zero the corresponding address buffer in the CacheEntry.
874 ZeroMem (CacheAddress
->AddressPtr
, CacheAddress
->Length
);
882 Configure the instance using the ConfigData. ConfigData is already validated.
884 @param Instance Pointer to the instance context data to be
886 @param ConfigData Pointer to the configuration data used to
887 configure the instance.
889 @retval EFI_SUCCESS The instance is configured with the ConfigData.
890 @retval EFI_ACCESS_DENIED The instance is already configured and the
891 ConfigData tries to reset some unchangeable
893 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
894 when the SwAddressType is IPv4.
895 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
900 ArpConfigureInstance (
901 IN ARP_INSTANCE_DATA
*Instance
,
902 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
905 EFI_ARP_CONFIG_DATA
*OldConfigData
;
908 OldConfigData
= &Instance
->ConfigData
;
910 if (ConfigData
!= NULL
) {
912 if (Instance
->Configured
) {
914 // The instance is configured, check the unchangeable fields.
916 if ((OldConfigData
->SwAddressType
!= ConfigData
->SwAddressType
) ||
917 (OldConfigData
->SwAddressLength
!= ConfigData
->SwAddressLength
) ||
919 OldConfigData
->StationAddress
,
920 ConfigData
->StationAddress
,
921 OldConfigData
->SwAddressLength
924 // Deny the unallowed changes.
926 return EFI_ACCESS_DENIED
;
930 // The instance is not configured.
933 if (ConfigData
->SwAddressType
== IPv4_ETHER_PROTO_TYPE
) {
934 CopyMem (&Ip
, ConfigData
->StationAddress
, sizeof (IP4_ADDR
));
936 if (!Ip4IsUnicast (NTOHL (Ip
), 0)) {
938 // The station address is not a valid IPv4 unicast address.
940 return EFI_INVALID_PARAMETER
;
945 // Save the configuration.
947 CopyMem (OldConfigData
, ConfigData
, sizeof (*OldConfigData
));
949 OldConfigData
->StationAddress
= AllocatePool (OldConfigData
->SwAddressLength
);
950 if (OldConfigData
->StationAddress
== NULL
) {
951 DEBUG ((EFI_D_ERROR
, "ArpConfigInstance: AllocatePool for the StationAddress "
953 return EFI_OUT_OF_RESOURCES
;
957 // Save the StationAddress.
960 OldConfigData
->StationAddress
,
961 ConfigData
->StationAddress
,
962 OldConfigData
->SwAddressLength
966 // Set the state to configured.
968 Instance
->Configured
= TRUE
;
972 // Use the implementation specific values if the following field is zero.
974 OldConfigData
->EntryTimeOut
= (ConfigData
->EntryTimeOut
== 0) ?
975 ARP_DEFAULT_TIMEOUT_VALUE
: ConfigData
->EntryTimeOut
;
977 OldConfigData
->RetryCount
= (ConfigData
->RetryCount
== 0) ?
978 ARP_DEFAULT_RETRY_COUNT
: ConfigData
->RetryCount
;
980 OldConfigData
->RetryTimeOut
= (ConfigData
->RetryTimeOut
== 0) ?
981 ARP_DEFAULT_RETRY_INTERVAL
: ConfigData
->RetryTimeOut
;
984 // Reset the configuration.
987 if (Instance
->Configured
) {
989 // Cancel the arp requests issued by this instance.
991 Instance
->ArpProto
.Cancel (&Instance
->ArpProto
, NULL
, NULL
);
994 // Free the buffer previously allocated to hold the station address.
996 gBS
->FreePool (OldConfigData
->StationAddress
);
999 Instance
->Configured
= FALSE
;
1007 Send out an arp frame using the CachEntry and the ArpOpCode.
1009 @param Instance Pointer to the instance context data.
1010 @param CacheEntry Pointer to the configuration data used to
1011 configure the instance.
1012 @param ArpOpCode The opcode used to send out this Arp frame, either
1020 IN ARP_INSTANCE_DATA
*Instance
,
1021 IN ARP_CACHE_ENTRY
*CacheEntry
,
1026 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
1027 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
1030 ARP_SERVICE_DATA
*ArpService
;
1031 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1032 EFI_ARP_CONFIG_DATA
*ConfigData
;
1036 ASSERT ((Instance
!= NULL
) && (CacheEntry
!= NULL
));
1039 // Allocate memory for the TxToken.
1041 TxToken
= AllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN
));
1042 if (TxToken
== NULL
) {
1043 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for TxToken failed.\n"));
1047 TxToken
->Event
= NULL
;
1052 // Create the event for this TxToken.
1054 Status
= gBS
->CreateEvent (
1061 if (EFI_ERROR (Status
)) {
1062 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: CreateEvent failed for TxToken->Event.\n"));
1067 // Allocate memory for the TxData used in the TxToken.
1069 TxData
= AllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA
));
1070 if (TxData
== NULL
) {
1071 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for TxData failed.\n"));
1075 ArpService
= Instance
->ArpService
;
1076 SnpMode
= &ArpService
->SnpMode
;
1077 ConfigData
= &Instance
->ConfigData
;
1080 // Calculate the buffer length for this arp frame.
1082 TotalLength
= SnpMode
->MediaHeaderSize
+ sizeof (ARP_HEAD
) +
1083 2 * (ConfigData
->SwAddressLength
+ SnpMode
->HwAddressSize
);
1086 // Allocate buffer for the arp frame.
1088 Packet
= AllocatePool (TotalLength
);
1089 if (Packet
== NULL
) {
1090 DEBUG ((EFI_D_ERROR
, "ArpSendFrame: Allocate memory for Packet failed.\n"));
1096 // The destination MAC address.
1098 if (ArpOpCode
== ARP_OPCODE_REQUEST
) {
1099 CopyMem (TmpPtr
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
);
1103 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1104 SnpMode
->HwAddressSize
1107 TmpPtr
+= SnpMode
->HwAddressSize
;
1110 // The source MAC address.
1112 CopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1113 TmpPtr
+= SnpMode
->HwAddressSize
;
1116 // The ethernet protocol type.
1118 *(UINT16
*)TmpPtr
= HTONS (ARP_ETHER_PROTO_TYPE
);
1124 ArpHead
= (ARP_HEAD
*) TmpPtr
;
1125 ArpHead
->HwType
= HTONS ((UINT16
)SnpMode
->IfType
);
1126 ArpHead
->ProtoType
= HTONS (ConfigData
->SwAddressType
);
1127 ArpHead
->HwAddrLen
= (UINT8
)SnpMode
->HwAddressSize
;
1128 ArpHead
->ProtoAddrLen
= ConfigData
->SwAddressLength
;
1129 ArpHead
->OpCode
= HTONS (ArpOpCode
);
1130 TmpPtr
+= sizeof (ARP_HEAD
);
1133 // The sender hardware address.
1135 CopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1136 TmpPtr
+= SnpMode
->HwAddressSize
;
1139 // The sender protocol address.
1141 CopyMem (TmpPtr
, ConfigData
->StationAddress
, ConfigData
->SwAddressLength
);
1142 TmpPtr
+= ConfigData
->SwAddressLength
;
1145 // The target hardware address.
1149 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1150 SnpMode
->HwAddressSize
1152 TmpPtr
+= SnpMode
->HwAddressSize
;
1155 // The target protocol address.
1159 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1160 ConfigData
->SwAddressLength
1164 // Set all the fields of the TxData.
1166 TxData
->DestinationAddress
= NULL
;
1167 TxData
->SourceAddress
= NULL
;
1168 TxData
->ProtocolType
= 0;
1169 TxData
->DataLength
= TotalLength
- SnpMode
->MediaHeaderSize
;
1170 TxData
->HeaderLength
= (UINT16
) SnpMode
->MediaHeaderSize
;
1171 TxData
->FragmentCount
= 1;
1173 TxData
->FragmentTable
[0].FragmentBuffer
= Packet
;
1174 TxData
->FragmentTable
[0].FragmentLength
= TotalLength
;
1177 // Associate the TxData with the TxToken.
1179 TxToken
->Packet
.TxData
= TxData
;
1180 TxToken
->Status
= EFI_NOT_READY
;
1183 // Send out this arp packet by Mnp.
1185 Status
= ArpService
->Mnp
->Transmit (ArpService
->Mnp
, TxToken
);
1186 if (EFI_ERROR (Status
)) {
1187 DEBUG ((EFI_D_ERROR
, "Mnp->Transmit failed, %r.\n", Status
));
1195 if (Packet
!= NULL
) {
1196 gBS
->FreePool (Packet
);
1199 if (TxData
!= NULL
) {
1200 gBS
->FreePool (TxData
);
1203 if (TxToken
->Event
!= NULL
) {
1204 gBS
->CloseEvent (TxToken
->Event
);
1207 gBS
->FreePool (TxToken
);
1212 Delete the cache entries in the specified CacheTable, using the BySwAddress,
1213 SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE,
1214 the cache is deleted event it's a static entry.
1216 @param CacheTable Pointer to the cache table to do the deletion.
1217 @param BySwAddress Delete the cache entry by software address or by
1219 @param SwAddressType The software address used to do the deletion.
1220 @param AddressBuffer Pointer to the buffer containing the address to
1221 match for the deletion.
1222 @param Force This deletion is forced or not.
1224 @return The count of the deleted cache entries.
1229 ArpDeleteCacheEntryInTable (
1230 IN LIST_ENTRY
*CacheTable
,
1231 IN BOOLEAN BySwAddress
,
1232 IN UINT16 SwAddressType
,
1233 IN UINT8
*AddressBuffer OPTIONAL
,
1238 LIST_ENTRY
*NextEntry
;
1239 ARP_CACHE_ENTRY
*CacheEntry
;
1244 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, CacheTable
) {
1245 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1247 if ((CacheEntry
->DefaultDecayTime
== 0) && !Force
) {
1249 // It's a static entry and we are not forced to delete it, skip.
1255 if (SwAddressType
== CacheEntry
->Addresses
[Protocol
].Type
) {
1257 // Protocol address type matched. Check the address.
1259 if ((AddressBuffer
== NULL
) ||
1262 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1263 CacheEntry
->Addresses
[Protocol
].Length
1272 if ((AddressBuffer
== NULL
) ||
1275 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1276 CacheEntry
->Addresses
[Hardware
].Length
1290 // Delete this entry.
1292 RemoveEntryList (&CacheEntry
->List
);
1293 ASSERT (IsListEmpty (&CacheEntry
->UserRequestList
));
1294 gBS
->FreePool (CacheEntry
);
1304 Delete cache entries in all the cache tables.
1306 @param Instance Pointer to the instance context data.
1307 @param BySwAddress Delete the cache entry by software address or by
1309 @param AddressBuffer Pointer to the buffer containing the address to
1310 match for the deletion.
1311 @param Force This deletion is forced or not.
1313 @return The count of the deleted cache entries.
1317 ArpDeleteCacheEntry (
1318 IN ARP_INSTANCE_DATA
*Instance
,
1319 IN BOOLEAN BySwAddress
,
1320 IN UINT8
*AddressBuffer OPTIONAL
,
1324 ARP_SERVICE_DATA
*ArpService
;
1327 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1329 ArpService
= Instance
->ArpService
;
1332 // Delete the cache entries in the DeniedCacheTable.
1334 Count
= ArpDeleteCacheEntryInTable (
1335 &ArpService
->DeniedCacheTable
,
1337 Instance
->ConfigData
.SwAddressType
,
1343 // Delete the cache entries inthe ResolvedCacheTable.
1345 Count
+= ArpDeleteCacheEntryInTable (
1346 &ArpService
->ResolvedCacheTable
,
1348 Instance
->ConfigData
.SwAddressType
,
1358 Cancel the arp request.
1360 @param Instance Pointer to the instance context data.
1361 @param TargetSwAddress Pointer to the buffer containing the target
1362 software address to match the arp request.
1363 @param UserEvent The user event used to notify this request
1366 @return The count of the cancelled requests.
1371 IN ARP_INSTANCE_DATA
*Instance
,
1372 IN VOID
*TargetSwAddress OPTIONAL
,
1373 IN EFI_EVENT UserEvent OPTIONAL
1376 ARP_SERVICE_DATA
*ArpService
;
1378 LIST_ENTRY
*NextEntry
;
1379 ARP_CACHE_ENTRY
*CacheEntry
;
1382 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1384 ArpService
= Instance
->ArpService
;
1387 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
1388 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1390 if ((TargetSwAddress
== NULL
) ||
1393 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1394 CacheEntry
->Addresses
[Protocol
].Length
1397 // This request entry matches the TargetSwAddress or all requests are to be
1398 // cancelled as TargetSwAddress is NULL.
1400 Count
+= ArpAddressResolved (CacheEntry
, Instance
, UserEvent
);
1402 if (IsListEmpty (&CacheEntry
->UserRequestList
)) {
1404 // No user requests any more, remove this request cache entry.
1406 RemoveEntryList (&CacheEntry
->List
);
1407 gBS
->FreePool (CacheEntry
);
1417 Find the cache entry in the cache table.
1419 @param Instance Pointer to the instance context data.
1420 @param BySwAddress Set to TRUE to look for matching software protocol
1421 addresses. Set to FALSE to look for matching
1422 hardware protocol addresses.
1423 @param AddressBuffer Pointer to address buffer. Set to NULL to match
1425 @param EntryLength The size of an entry in the entries buffer.
1426 @param EntryCount The number of ARP cache entries that are found by
1427 the specified criteria.
1428 @param Entries Pointer to the buffer that will receive the ARP
1430 @param Refresh Set to TRUE to refresh the timeout value of the
1431 matching ARP cache entry.
1433 @retval EFI_SUCCESS The requested ARP cache entries are copied into
1435 @retval EFI_NOT_FOUND No matching entries found.
1436 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
1441 IN ARP_INSTANCE_DATA
*Instance
,
1442 IN BOOLEAN BySwAddress
,
1443 IN VOID
*AddressBuffer OPTIONAL
,
1444 OUT UINT32
*EntryLength OPTIONAL
,
1445 OUT UINT32
*EntryCount OPTIONAL
,
1446 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
1451 ARP_SERVICE_DATA
*ArpService
;
1452 NET_ARP_ADDRESS MatchAddress
;
1453 FIND_OPTYPE FindOpType
;
1454 LIST_ENTRY
*StartEntry
;
1455 ARP_CACHE_ENTRY
*CacheEntry
;
1456 NET_MAP FoundEntries
;
1458 EFI_ARP_FIND_DATA
*FindData
;
1459 LIST_ENTRY
*CacheTable
;
1461 ArpService
= Instance
->ArpService
;
1464 // Init the FounEntries used to hold the found cache entries.
1466 NetMapInit (&FoundEntries
);
1469 // Set the MatchAddress.
1472 MatchAddress
.Type
= Instance
->ConfigData
.SwAddressType
;
1473 MatchAddress
.Length
= Instance
->ConfigData
.SwAddressLength
;
1474 FindOpType
= ByProtoAddress
;
1476 MatchAddress
.Type
= ArpService
->SnpMode
.IfType
;
1477 MatchAddress
.Length
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1478 FindOpType
= ByHwAddress
;
1481 MatchAddress
.AddressPtr
= AddressBuffer
;
1484 // Search the DeniedCacheTable
1489 // Try to find the matched entries in the DeniedCacheTable.
1491 CacheEntry
= ArpFindNextCacheEntryInTable (
1492 &ArpService
->DeniedCacheTable
,
1498 if (CacheEntry
== NULL
) {
1500 // Once the CacheEntry is NULL, there are no more matches.
1506 // Insert the found entry into the map.
1511 (VOID
*)&ArpService
->DeniedCacheTable
1515 // Let the next search start from this cache entry.
1517 StartEntry
= &CacheEntry
->List
;
1521 // Refresh the DecayTime if needed.
1523 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1528 // Search the ResolvedCacheTable
1532 CacheEntry
= ArpFindNextCacheEntryInTable (
1533 &ArpService
->ResolvedCacheTable
,
1539 if (CacheEntry
== NULL
) {
1541 // Once the CacheEntry is NULL, there are no more matches.
1547 // Insert the found entry into the map.
1552 (VOID
*)&ArpService
->ResolvedCacheTable
1556 // Let the next search start from this cache entry.
1558 StartEntry
= &CacheEntry
->List
;
1562 // Refresh the DecayTime if needed.
1564 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1568 Status
= EFI_SUCCESS
;
1570 FoundCount
= (UINT32
) NetMapGetCount (&FoundEntries
);
1571 if (FoundCount
== 0) {
1572 Status
= EFI_NOT_FOUND
;
1576 if (EntryLength
!= NULL
) {
1578 // Return the entry length, make sure its 8 bytes alignment.
1580 *EntryLength
= (((sizeof (EFI_ARP_FIND_DATA
) + Instance
->ConfigData
.SwAddressLength
+
1581 ArpService
->SnpMode
.HwAddressSize
) + 3) & ~(0x3));
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
* (*EntryLength
));
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
= *EntryLength
;
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
+ *EntryLength
);
1656 NetMapClean (&FoundEntries
);