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 (Instance
->ArpProto
));
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 Context Pointer to the context data registerd to the
82 ARP_SERVICE_DATA
*ArpService
;
83 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*RxToken
;
84 EFI_MANAGED_NETWORK_RECEIVE_DATA
*RxData
;
86 ARP_ADDRESS ArpAddress
;
87 ARP_CACHE_ENTRY
*CacheEntry
;
88 NET_LIST_ENTRY
*Entry
;
89 ARP_INSTANCE_DATA
*Instance
;
90 EFI_ARP_CONFIG_DATA
*ConfigData
;
91 NET_ARP_ADDRESS SenderAddress
[2];
96 ArpService
= (ARP_SERVICE_DATA
*)Context
;
97 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
99 RxToken
= &ArpService
->RxToken
;
101 if (RxToken
->Status
== EFI_ABORTED
) {
103 // The Token is aborted, possibly by arp itself, just return and the receiving
104 // process is stopped.
109 if (EFI_ERROR (RxToken
->Status
)) {
111 // Restart the receiving if any other error Status occurs.
113 goto RESTART_RECEIVE
;
117 // Status is EFI_SUCCESS, process the received frame.
119 RxData
= RxToken
->Packet
.RxData
;
120 Head
= (ARP_HEAD
*) RxData
->PacketData
;
123 // Convert the byte order of the multi-byte fields.
125 Head
->HwType
= NTOHS (Head
->HwType
);
126 Head
->ProtoType
= NTOHS (Head
->ProtoType
);
127 Head
->OpCode
= NTOHS (Head
->OpCode
);
129 if ((Head
->HwType
!= ArpService
->SnpMode
.IfType
) ||
130 (Head
->HwAddrLen
!= ArpService
->SnpMode
.HwAddressSize
) ||
131 (RxData
->ProtocolType
!= ARP_ETHER_PROTO_TYPE
)) {
133 // The hardware type or the hardware address length doesn't match.
134 // There is a sanity check for the protocol type too.
140 // Set the pointers to the addresses contained in the arp packet.
142 ArpAddress
.SenderHwAddr
= (UINT8
*)(Head
+ 1);
143 ArpAddress
.SenderProtoAddr
= ArpAddress
.SenderHwAddr
+ Head
->HwAddrLen
;
144 ArpAddress
.TargetHwAddr
= ArpAddress
.SenderProtoAddr
+ Head
->ProtoAddrLen
;
145 ArpAddress
.TargetProtoAddr
= ArpAddress
.TargetHwAddr
+ Head
->HwAddrLen
;
147 SenderAddress
[Hardware
].Type
= Head
->HwType
;
148 SenderAddress
[Hardware
].Length
= Head
->HwAddrLen
;
149 SenderAddress
[Hardware
].AddressPtr
= ArpAddress
.SenderHwAddr
;
151 SenderAddress
[Protocol
].Type
= Head
->ProtoType
;
152 SenderAddress
[Protocol
].Length
= Head
->ProtoAddrLen
;
153 SenderAddress
[Protocol
].AddressPtr
= ArpAddress
.SenderProtoAddr
;
156 // First, check the denied cache table.
158 CacheEntry
= ArpFindDeniedCacheEntry (
160 &SenderAddress
[Protocol
],
161 &SenderAddress
[Hardware
]
163 if (CacheEntry
!= NULL
) {
165 // This address (either hardware or protocol address, or both) is configured to
166 // be a deny entry, silently skip the normal process.
171 ProtoMatched
= FALSE
;
174 NET_LIST_FOR_EACH (Entry
, &ArpService
->ChildrenList
) {
176 // Iterate all the children.
178 Instance
= NET_LIST_USER_STRUCT (Entry
, ARP_INSTANCE_DATA
, List
);
179 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
180 ConfigData
= &Instance
->ConfigData
;
182 if ((Instance
->Configured
) &&
183 (Head
->ProtoType
== ConfigData
->SwAddressType
) &&
184 (Head
->ProtoAddrLen
== ConfigData
->SwAddressLength
)) {
186 // The protocol type is matched for the received arp packet.
189 if (0 == NetCompareMem (
190 (VOID
*)ArpAddress
.TargetProtoAddr
,
191 ConfigData
->StationAddress
,
192 ConfigData
->SwAddressLength
195 // The arp driver has the target address required by the received arp packet.
205 // Protocol type unmatchable, skip.
211 // Check whether the sender's address information is already in the cache.
214 CacheEntry
= ArpFindNextCacheEntryInTable (
215 &ArpService
->ResolvedCacheTable
,
218 &SenderAddress
[Protocol
],
221 if (CacheEntry
!= NULL
) {
223 // Update the entry with the new information.
225 ArpFillAddressInCacheEntry (CacheEntry
, &SenderAddress
[Hardware
], NULL
);
226 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
232 // This arp packet isn't targeted to us, skip now.
239 // Add the triplet <protocol type, sender protocol address, sender hardware address>
240 // to the translation table.
242 CacheEntry
= ArpFindNextCacheEntryInTable (
243 &ArpService
->PendingRequestTable
,
246 &SenderAddress
[Protocol
],
249 if (CacheEntry
== NULL
) {
251 // Allocate a new CacheEntry.
253 CacheEntry
= ArpAllocCacheEntry (NULL
);
254 if (CacheEntry
== NULL
) {
259 if (!IsListEmpty (&CacheEntry
->List
)) {
260 NetListRemoveEntry (&CacheEntry
->List
);
264 // Fill the addresses into the CacheEntry.
266 ArpFillAddressInCacheEntry (
268 &SenderAddress
[Hardware
],
269 &SenderAddress
[Protocol
]
275 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
278 // Add this entry into the ResolvedCacheTable
280 NetListInsertHead (&ArpService
->ResolvedCacheTable
, &CacheEntry
->List
);
283 if (Head
->OpCode
== ARP_OPCODE_REQUEST
) {
285 // Send back the ARP Reply. If we reach here, Instance is not NULL and CacheEntry
288 ArpSendFrame (Instance
, CacheEntry
, ARP_OPCODE_REPLY
);
294 // Signal Mnp to recycle the RxData.
296 gBS
->SignalEvent (RxData
->RecycleEvent
);
301 // Continue to receive packets from Mnp.
303 Status
= ArpService
->Mnp
->Receive (ArpService
->Mnp
, RxToken
);
306 if (EFI_ERROR (Status
)) {
307 ARP_DEBUG_ERROR (("ArpOnFrameRcvd: ArpService->Mnp->Receive "
308 "failed, %r\n.", Status
));
314 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
316 @param Event The Event this notify function registered to.
317 @param Context Pointer to the context data registerd to the
331 // Request ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK
333 NetLibQueueDpc (TPL_CALLBACK
, ArpOnFrameRcvdDpc
, Context
);
337 Process the already sent arp packets.
339 @param Context Pointer to the context data registerd to the
351 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
352 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
354 ASSERT (Context
!= NULL
);
356 TxToken
= (EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*)Context
;
357 TxData
= TxToken
->Packet
.TxData
;
360 if (EFI_ERROR (TxToken
->Status
)) {
361 ARP_DEBUG_ERROR (("ArpOnFrameSent: TxToken->Status, %r.\n", TxToken
->Status
));
366 // Free the allocated memory and close the event.
368 NetFreePool (TxData
->FragmentTable
[0].FragmentBuffer
);
369 NetFreePool (TxData
);
370 gBS
->CloseEvent (TxToken
->Event
);
371 NetFreePool (TxToken
);
375 Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK.
377 @param Event The Event this notify function registered to.
378 @param Context Pointer to the context data registerd to the
392 // Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK
394 NetLibQueueDpc (TPL_CALLBACK
, ArpOnFrameSentDpc
, Context
);
399 Process the arp cache olding and drive the retrying arp requests.
401 @param Event The Event this notify function registered to.
402 @param Context Pointer to the context data registerd to the
415 ARP_SERVICE_DATA
*ArpService
;
416 NET_LIST_ENTRY
*Entry
;
417 NET_LIST_ENTRY
*NextEntry
;
418 NET_LIST_ENTRY
*ContextEntry
;
419 ARP_CACHE_ENTRY
*CacheEntry
;
420 USER_REQUEST_CONTEXT
*RequestContext
;
422 ASSERT (Context
!= NULL
);
423 ArpService
= (ARP_SERVICE_DATA
*)Context
;
426 // Iterate all the pending requests to see whether a retry is needed to send out
427 // or the request finally fails because the retry time reaches the limitation.
429 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
430 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
432 if (CacheEntry
->NextRetryTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
434 // Timeout, if we can retry more, send out the request again, otherwise abort
437 if (CacheEntry
->RetryCount
== 0) {
439 // Abort this request.
441 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
442 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
444 NetListRemoveEntry (&CacheEntry
->List
);
445 NetFreePool (CacheEntry
);
448 // resend the ARP request.
450 ASSERT (!NetListIsEmpty(&CacheEntry
->UserRequestList
));
452 ContextEntry
= CacheEntry
->UserRequestList
.ForwardLink
;
453 RequestContext
= NET_LIST_USER_STRUCT (ContextEntry
, USER_REQUEST_CONTEXT
, List
);
455 ArpSendFrame (RequestContext
->Instance
, CacheEntry
, ARP_OPCODE_REQUEST
);
457 CacheEntry
->RetryCount
--;
458 CacheEntry
->NextRetryTime
= RequestContext
->Instance
->ConfigData
.RetryTimeOut
;
462 // Update the NextRetryTime.
464 CacheEntry
->NextRetryTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
469 // Check the timeouts for the DeniedCacheTable.
471 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->DeniedCacheTable
) {
472 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
473 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
475 if (CacheEntry
->DefaultDecayTime
== 0) {
477 // It's a static entry, skip it.
482 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
484 // Time out, remove it.
486 NetListRemoveEntry (&CacheEntry
->List
);
487 NetFreePool (CacheEntry
);
490 // Update the DecayTime.
492 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
497 // Check the timeouts for the ResolvedCacheTable.
499 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->ResolvedCacheTable
) {
500 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
501 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
503 if (CacheEntry
->DefaultDecayTime
== 0) {
505 // It's a static entry, skip it.
510 if (CacheEntry
->DecayTime
<= ARP_PERIODIC_TIMER_INTERVAL
) {
512 // Time out, remove it.
514 NetListRemoveEntry (&CacheEntry
->List
);
515 NetFreePool (CacheEntry
);
518 // Update the DecayTime.
520 CacheEntry
->DecayTime
-= ARP_PERIODIC_TIMER_INTERVAL
;
527 Match the two NET_ARP_ADDRESSes.
529 @param AddressOne Pointer to the first address to match.
530 @param AddressTwo Pointer to the second address to match.
532 @return The two addresses match or not.
538 IN NET_ARP_ADDRESS
*AddressOne
,
539 IN NET_ARP_ADDRESS
*AddressTwo
542 if ((AddressOne
->Type
!= AddressTwo
->Type
) ||
543 (AddressOne
->Length
!= AddressTwo
->Length
)) {
545 // Either Type or Length doesn't match.
550 if ((AddressOne
->AddressPtr
!= NULL
) &&
552 AddressOne
->AddressPtr
,
553 AddressTwo
->AddressPtr
,
557 // The address is not the same.
567 Find the CacheEntry which matches the requirements in the specified CacheTable.
569 @param CacheTable Pointer to the arp cache table.
570 @param StartEntry Pointer to the start entry this search begins with
572 @param FindOpType The search type.
573 @param ProtocolAddress Pointer to the protocol address to match.
574 @param HardwareAddress Pointer to the hardware address to match.
576 @return Pointer to the matched arp cache entry, if NULL, no match is found.
580 ArpFindNextCacheEntryInTable (
581 IN NET_LIST_ENTRY
*CacheTable
,
582 IN NET_LIST_ENTRY
*StartEntry
,
583 IN FIND_OPTYPE FindOpType
,
584 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
585 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
588 NET_LIST_ENTRY
*Entry
;
589 ARP_CACHE_ENTRY
*CacheEntry
;
591 if (StartEntry
== NULL
) {
593 // Start from the beginning of the table if no StartEntry is specified.
595 StartEntry
= CacheTable
;
598 for (Entry
= StartEntry
->ForwardLink
; Entry
!= CacheTable
; Entry
= Entry
->ForwardLink
) {
599 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
601 if (FindOpType
& MATCH_SW_ADDRESS
) {
603 // Find by the software address.
605 if (!ArpMatchAddress (ProtocolAddress
, &CacheEntry
->Addresses
[Protocol
])) {
607 // The ProtocolAddress doesn't match, continue to the next cache entry.
613 if (FindOpType
& MATCH_HW_ADDRESS
) {
615 // Find by the hardware address.
617 if (!ArpMatchAddress (HardwareAddress
, &CacheEntry
->Addresses
[Hardware
])) {
619 // The HardwareAddress doesn't match, continue to the next cache entry.
626 // The CacheEntry meets the requirements now, return this entry.
639 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
640 in the DeniedCacheTable.
642 @param ArpService Pointer to the arp service context data.
643 @param ProtocolAddress Pointer to the protocol address.
644 @param HardwareAddress Pointer to the hardware address.
646 @return Pointer to the matched cache entry, if NULL no match is found.
650 ArpFindDeniedCacheEntry (
651 IN ARP_SERVICE_DATA
*ArpService
,
652 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
653 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
656 ARP_CACHE_ENTRY
*CacheEntry
;
658 ASSERT ((ProtocolAddress
!= NULL
) || (HardwareAddress
!= NULL
));
659 NET_CHECK_SIGNATURE (ArpService
, ARP_SERVICE_DATA_SIGNATURE
);
663 if ((ProtocolAddress
!= NULL
) && (ProtocolAddress
->AddressPtr
!= NULL
)) {
665 // Find the cache entry in the DeniedCacheTable by the protocol address.
667 CacheEntry
= ArpFindNextCacheEntryInTable (
668 &ArpService
->DeniedCacheTable
,
674 if (CacheEntry
!= NULL
) {
682 if ((HardwareAddress
!= NULL
) && (HardwareAddress
->AddressPtr
!= NULL
)) {
684 // Find the cache entry in the DeniedCacheTable by the hardware address.
686 CacheEntry
= ArpFindNextCacheEntryInTable (
687 &ArpService
->DeniedCacheTable
,
700 Allocate a cache entry and initialize it.
702 @param Instance Pointer to the instance context data.
704 @return Pointer to the new created cache entry.
709 IN ARP_INSTANCE_DATA
*Instance
712 ARP_CACHE_ENTRY
*CacheEntry
;
713 NET_ARP_ADDRESS
*Address
;
717 // Allocate memory for the cache entry.
719 CacheEntry
= NetAllocatePool (sizeof (ARP_CACHE_ENTRY
));
720 if (CacheEntry
== NULL
) {
727 NetListInit (&CacheEntry
->List
);
728 NetListInit (&CacheEntry
->UserRequestList
);
730 for (Index
= 0; Index
< 2; Index
++) {
732 // Init the address pointers to point to the concrete buffer.
734 Address
= &CacheEntry
->Addresses
[Index
];
735 Address
->AddressPtr
= Address
->Buffer
.ProtoAddress
;
739 // Zero the hardware address first.
741 NetZeroMem (CacheEntry
->Addresses
[Hardware
].AddressPtr
, ARP_MAX_HARDWARE_ADDRESS_LEN
);
743 if (Instance
!= NULL
) {
745 // Inherit the parameters from the instance configuration.
747 CacheEntry
->RetryCount
= Instance
->ConfigData
.RetryCount
;
748 CacheEntry
->NextRetryTime
= Instance
->ConfigData
.RetryTimeOut
;
749 CacheEntry
->DefaultDecayTime
= Instance
->ConfigData
.EntryTimeOut
;
750 CacheEntry
->DecayTime
= Instance
->ConfigData
.EntryTimeOut
;
753 // Use the default parameters if this cache entry isn't allocate in a
756 CacheEntry
->RetryCount
= ARP_DEFAULT_RETRY_COUNT
;
757 CacheEntry
->NextRetryTime
= ARP_DEFAULT_RETRY_INTERVAL
;
758 CacheEntry
->DefaultDecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
759 CacheEntry
->DecayTime
= ARP_DEFAULT_TIMEOUT_VALUE
;
767 Turn the CacheEntry into the resolved status.
769 @param CacheEntry Pointer to the resolved cache entry.
770 @param Instance Pointer to the instance context data.
771 @param UserEvent Pointer to the UserEvent to notify.
773 @return The count of notifications sent to the instance.
778 IN ARP_CACHE_ENTRY
*CacheEntry
,
779 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
780 IN EFI_EVENT UserEvent OPTIONAL
783 NET_LIST_ENTRY
*Entry
;
784 NET_LIST_ENTRY
*NextEntry
;
785 USER_REQUEST_CONTEXT
*Context
;
791 // Iterate all the linked user requests to notify them.
793 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &CacheEntry
->UserRequestList
) {
794 Context
= NET_LIST_USER_STRUCT (Entry
, USER_REQUEST_CONTEXT
, List
);
796 if (((Instance
== NULL
) || (Context
->Instance
== Instance
)) &&
797 ((UserEvent
== NULL
) || (Context
->UserRequestEvent
== UserEvent
))) {
799 // Copy the address to the user-provided buffer and notify the user.
802 Context
->UserHwAddrBuffer
,
803 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
804 CacheEntry
->Addresses
[Hardware
].Length
806 gBS
->SignalEvent (Context
->UserRequestEvent
);
809 // Remove this user request and free the context data.
811 NetListRemoveEntry (&Context
->List
);
812 NetFreePool (Context
);
819 // Dispatch the DPCs queued by the NotifyFunction of the Context->UserRequestEvent.
821 NetLibDispatchDpc ();
828 Fill the addresses in the CacheEntry using the information passed in by
831 @param CacheEntry Pointer to the cache entry.
832 @param HwAddr Pointer to the software address.
833 @param SwAddr Pointer to the hardware address.
839 ArpFillAddressInCacheEntry (
840 IN ARP_CACHE_ENTRY
*CacheEntry
,
841 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
842 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
845 NET_ARP_ADDRESS
*Address
[2];
846 NET_ARP_ADDRESS
*CacheAddress
;
849 Address
[Hardware
] = HwAddr
;
850 Address
[Protocol
] = SwAddr
;
852 for (Index
= 0; Index
< 2; Index
++) {
853 if (Address
[Index
] != NULL
) {
855 // Fill the address if the passed in pointer is not NULL.
857 CacheAddress
= &CacheEntry
->Addresses
[Index
];
859 CacheAddress
->Type
= Address
[Index
]->Type
;
860 CacheAddress
->Length
= Address
[Index
]->Length
;
862 if (Address
[Index
]->AddressPtr
!= NULL
) {
864 // Copy it if the AddressPtr points to some buffer.
867 CacheAddress
->AddressPtr
,
868 Address
[Index
]->AddressPtr
,
873 // Zero the corresponding address buffer in the CacheEntry.
875 NetZeroMem (CacheAddress
->AddressPtr
, CacheAddress
->Length
);
883 Configure the instance using the ConfigData. ConfigData is already validated.
885 @param Instance Pointer to the instance context data to be
887 @param ConfigData Pointer to the configuration data used to
888 configure the instance.
890 @retval EFI_SUCCESS The instance is configured with the ConfigData.
891 @retval EFI_ACCESS_DENIED The instance is already configured and the
892 ConfigData tries to reset some unchangeable
894 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
895 when the SwAddressType is IPv4.
896 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
901 ArpConfigureInstance (
902 IN ARP_INSTANCE_DATA
*Instance
,
903 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
906 EFI_ARP_CONFIG_DATA
*OldConfigData
;
909 OldConfigData
= &Instance
->ConfigData
;
911 if (ConfigData
!= NULL
) {
913 if (Instance
->Configured
) {
915 // The instance is configured, check the unchangeable fields.
917 if ((OldConfigData
->SwAddressType
!= ConfigData
->SwAddressType
) ||
918 (OldConfigData
->SwAddressLength
!= ConfigData
->SwAddressLength
) ||
920 OldConfigData
->StationAddress
,
921 ConfigData
->StationAddress
,
922 OldConfigData
->SwAddressLength
925 // Deny the unallowed changes.
927 return EFI_ACCESS_DENIED
;
931 // The instance is not configured.
934 if (ConfigData
->SwAddressType
== IPv4_ETHER_PROTO_TYPE
) {
935 NetCopyMem (&Ip
, ConfigData
->StationAddress
, sizeof (IP4_ADDR
));
937 if (!Ip4IsUnicast (NTOHL (Ip
), 0)) {
939 // The station address is not a valid IPv4 unicast address.
941 return EFI_INVALID_PARAMETER
;
946 // Save the configuration.
948 CopyMem (OldConfigData
, ConfigData
, sizeof (*OldConfigData
));
950 OldConfigData
->StationAddress
= NetAllocatePool (OldConfigData
->SwAddressLength
);
951 if (OldConfigData
->StationAddress
== NULL
) {
952 ARP_DEBUG_ERROR (("ArpConfigInstance: NetAllocatePool for the StationAddress "
954 return EFI_OUT_OF_RESOURCES
;
958 // Save the StationAddress.
961 OldConfigData
->StationAddress
,
962 ConfigData
->StationAddress
,
963 OldConfigData
->SwAddressLength
967 // Set the state to configured.
969 Instance
->Configured
= TRUE
;
973 // Use the implementation specific values if the following field is zero.
975 OldConfigData
->EntryTimeOut
= (ConfigData
->EntryTimeOut
== 0) ?
976 ARP_DEFAULT_TIMEOUT_VALUE
: ConfigData
->EntryTimeOut
;
978 OldConfigData
->RetryCount
= (ConfigData
->RetryCount
== 0) ?
979 ARP_DEFAULT_RETRY_COUNT
: ConfigData
->RetryCount
;
981 OldConfigData
->RetryTimeOut
= (ConfigData
->RetryTimeOut
== 0) ?
982 ARP_DEFAULT_RETRY_INTERVAL
: ConfigData
->RetryTimeOut
;
985 // Reset the configuration.
988 if (Instance
->Configured
) {
990 // Cancel the arp requests issued by this instance.
992 Instance
->ArpProto
.Cancel (&Instance
->ArpProto
, NULL
, NULL
);
995 // Free the buffer previously allocated to hold the station address.
997 NetFreePool (OldConfigData
->StationAddress
);
1000 Instance
->Configured
= FALSE
;
1008 Send out an arp frame using the CachEntry and the ArpOpCode.
1010 @param Instance Pointer to the instance context data.
1011 @param CacheEntry Pointer to the configuration data used to
1012 configure the instance.
1013 @param ArpOpCode The opcode used to send out this Arp frame, either
1021 IN ARP_INSTANCE_DATA
*Instance
,
1022 IN ARP_CACHE_ENTRY
*CacheEntry
,
1027 EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*TxToken
;
1028 EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
;
1031 ARP_SERVICE_DATA
*ArpService
;
1032 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
1033 EFI_ARP_CONFIG_DATA
*ConfigData
;
1037 ASSERT ((Instance
!= NULL
) && (CacheEntry
!= NULL
));
1040 // Allocate memory for the TxToken.
1042 TxToken
= NetAllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN
));
1043 if (TxToken
== NULL
) {
1044 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for TxToken failed.\n"));
1048 TxToken
->Event
= NULL
;
1053 // Create the event for this TxToken.
1055 Status
= gBS
->CreateEvent (
1062 if (EFI_ERROR (Status
)) {
1063 ARP_DEBUG_ERROR (("ArpSendFrame: CreateEvent failed for TxToken->Event.\n"));
1068 // Allocate memory for the TxData used in the TxToken.
1070 TxData
= NetAllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA
));
1071 if (TxData
== NULL
) {
1072 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for TxData failed.\n"));
1076 ArpService
= Instance
->ArpService
;
1077 SnpMode
= &ArpService
->SnpMode
;
1078 ConfigData
= &Instance
->ConfigData
;
1081 // Calculate the buffer length for this arp frame.
1083 TotalLength
= SnpMode
->MediaHeaderSize
+ sizeof (ARP_HEAD
) +
1084 2 * (ConfigData
->SwAddressLength
+ SnpMode
->HwAddressSize
);
1087 // Allocate buffer for the arp frame.
1089 Packet
= NetAllocatePool (TotalLength
);
1090 if (Packet
== NULL
) {
1091 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for Packet failed.\n"));
1097 // The destination MAC address.
1099 if (ArpOpCode
== ARP_OPCODE_REQUEST
) {
1100 NetCopyMem (TmpPtr
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
);
1104 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1105 SnpMode
->HwAddressSize
1108 TmpPtr
+= SnpMode
->HwAddressSize
;
1111 // The source MAC address.
1113 NetCopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1114 TmpPtr
+= SnpMode
->HwAddressSize
;
1117 // The ethernet protocol type.
1119 *(UINT16
*)TmpPtr
= HTONS (ARP_ETHER_PROTO_TYPE
);
1125 ArpHead
= (ARP_HEAD
*) TmpPtr
;
1126 ArpHead
->HwType
= HTONS ((UINT16
)SnpMode
->IfType
);
1127 ArpHead
->ProtoType
= HTONS (ConfigData
->SwAddressType
);
1128 ArpHead
->HwAddrLen
= (UINT8
)SnpMode
->HwAddressSize
;
1129 ArpHead
->ProtoAddrLen
= ConfigData
->SwAddressLength
;
1130 ArpHead
->OpCode
= HTONS (ArpOpCode
);
1131 TmpPtr
+= sizeof (ARP_HEAD
);
1134 // The sender hardware address.
1136 NetCopyMem (TmpPtr
, &SnpMode
->CurrentAddress
, SnpMode
->HwAddressSize
);
1137 TmpPtr
+= SnpMode
->HwAddressSize
;
1140 // The sender protocol address.
1142 NetCopyMem (TmpPtr
, ConfigData
->StationAddress
, ConfigData
->SwAddressLength
);
1143 TmpPtr
+= ConfigData
->SwAddressLength
;
1146 // The target hardware address.
1150 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1151 SnpMode
->HwAddressSize
1153 TmpPtr
+= SnpMode
->HwAddressSize
;
1156 // The target protocol address.
1160 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1161 ConfigData
->SwAddressLength
1165 // Set all the fields of the TxData.
1167 TxData
->DestinationAddress
= NULL
;
1168 TxData
->SourceAddress
= NULL
;
1169 TxData
->ProtocolType
= 0;
1170 TxData
->DataLength
= TotalLength
- SnpMode
->MediaHeaderSize
;
1171 TxData
->HeaderLength
= (UINT16
) SnpMode
->MediaHeaderSize
;
1172 TxData
->FragmentCount
= 1;
1174 TxData
->FragmentTable
[0].FragmentBuffer
= Packet
;
1175 TxData
->FragmentTable
[0].FragmentLength
= TotalLength
;
1178 // Associate the TxData with the TxToken.
1180 TxToken
->Packet
.TxData
= TxData
;
1181 TxToken
->Status
= EFI_NOT_READY
;
1184 // Send out this arp packet by Mnp.
1186 Status
= ArpService
->Mnp
->Transmit (ArpService
->Mnp
, TxToken
);
1187 if (EFI_ERROR (Status
)) {
1188 ARP_DEBUG_ERROR (("Mnp->Transmit failed, %r.\n", Status
));
1196 if (Packet
!= NULL
) {
1197 NetFreePool (Packet
);
1200 if (TxData
!= NULL
) {
1201 NetFreePool (TxData
);
1204 if (TxToken
->Event
!= NULL
) {
1205 gBS
->CloseEvent (TxToken
->Event
);
1208 NetFreePool (TxToken
);
1213 Delete the cache entries in the specified CacheTable, using the BySwAddress,
1214 SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE,
1215 the cache is deleted event it's a static entry.
1217 @param CacheTable Pointer to the cache table to do the deletion.
1218 @param BySwAddress Delete the cache entry by software address or by
1220 @param SwAddressType The software address used to do the deletion.
1221 @param AddressBuffer Pointer to the buffer containing the address to
1222 match for the deletion.
1223 @param Force This deletion is forced or not.
1225 @return The count of the deleted cache entries.
1230 ArpDeleteCacheEntryInTable (
1231 IN NET_LIST_ENTRY
*CacheTable
,
1232 IN BOOLEAN BySwAddress
,
1233 IN UINT16 SwAddressType
,
1234 IN UINT8
*AddressBuffer OPTIONAL
,
1238 NET_LIST_ENTRY
*Entry
;
1239 NET_LIST_ENTRY
*NextEntry
;
1240 ARP_CACHE_ENTRY
*CacheEntry
;
1245 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, CacheTable
) {
1246 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1248 if ((CacheEntry
->DefaultDecayTime
== 0) && !Force
) {
1250 // It's a static entry and we are not forced to delete it, skip.
1256 if (SwAddressType
== CacheEntry
->Addresses
[Protocol
].Type
) {
1258 // Protocol address type matched. Check the address.
1260 if ((AddressBuffer
== NULL
) ||
1263 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1264 CacheEntry
->Addresses
[Protocol
].Length
1273 if ((AddressBuffer
== NULL
) ||
1276 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1277 CacheEntry
->Addresses
[Hardware
].Length
1291 // Delete this entry.
1293 NetListRemoveEntry (&CacheEntry
->List
);
1294 ASSERT (NetListIsEmpty (&CacheEntry
->UserRequestList
));
1295 NetFreePool (CacheEntry
);
1305 Delete cache entries in all the cache tables.
1307 @param Instance Pointer to the instance context data.
1308 @param BySwAddress Delete the cache entry by software address or by
1310 @param AddressBuffer Pointer to the buffer containing the address to
1311 match for the deletion.
1312 @param Force This deletion is forced or not.
1314 @return The count of the deleted cache entries.
1318 ArpDeleteCacheEntry (
1319 IN ARP_INSTANCE_DATA
*Instance
,
1320 IN BOOLEAN BySwAddress
,
1321 IN UINT8
*AddressBuffer OPTIONAL
,
1325 ARP_SERVICE_DATA
*ArpService
;
1328 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1330 ArpService
= Instance
->ArpService
;
1333 // Delete the cache entries in the DeniedCacheTable.
1335 Count
= ArpDeleteCacheEntryInTable (
1336 &ArpService
->DeniedCacheTable
,
1338 Instance
->ConfigData
.SwAddressType
,
1344 // Delete the cache entries inthe ResolvedCacheTable.
1346 Count
+= ArpDeleteCacheEntryInTable (
1347 &ArpService
->ResolvedCacheTable
,
1349 Instance
->ConfigData
.SwAddressType
,
1359 Cancel the arp request.
1361 @param Instance Pointer to the instance context data.
1362 @param TargetSwAddress Pointer to the buffer containing the target
1363 software address to match the arp request.
1364 @param UserEvent The user event used to notify this request
1367 @return The count of the cancelled requests.
1372 IN ARP_INSTANCE_DATA
*Instance
,
1373 IN VOID
*TargetSwAddress OPTIONAL
,
1374 IN EFI_EVENT UserEvent OPTIONAL
1377 ARP_SERVICE_DATA
*ArpService
;
1378 NET_LIST_ENTRY
*Entry
;
1379 NET_LIST_ENTRY
*NextEntry
;
1380 ARP_CACHE_ENTRY
*CacheEntry
;
1383 NET_CHECK_SIGNATURE (Instance
, ARP_INSTANCE_DATA_SIGNATURE
);
1385 ArpService
= Instance
->ArpService
;
1388 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &ArpService
->PendingRequestTable
) {
1389 CacheEntry
= NET_LIST_USER_STRUCT (Entry
, ARP_CACHE_ENTRY
, List
);
1391 if ((TargetSwAddress
== NULL
) ||
1394 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1395 CacheEntry
->Addresses
[Protocol
].Length
1398 // This request entry matches the TargetSwAddress or all requests are to be
1399 // cancelled as TargetSwAddress is NULL.
1401 Count
+= ArpAddressResolved (CacheEntry
, Instance
, UserEvent
);
1403 if (NetListIsEmpty (&CacheEntry
->UserRequestList
)) {
1405 // No user requests any more, remove this request cache entry.
1407 NetListRemoveEntry (&CacheEntry
->List
);
1408 NetFreePool (CacheEntry
);
1418 Find the cache entry in the cache table.
1420 @param Instance Pointer to the instance context data.
1421 @param BySwAddress Set to TRUE to look for matching software protocol
1422 addresses. Set to FALSE to look for matching
1423 hardware protocol addresses.
1424 @param AddressBuffer Pointer to address buffer. Set to NULL to match
1426 @param EntryLength The size of an entry in the entries buffer.
1427 @param EntryCount The number of ARP cache entries that are found by
1428 the specified criteria.
1429 @param Entries Pointer to the buffer that will receive the ARP
1431 @param Refresh Set to TRUE to refresh the timeout value of the
1432 matching ARP cache entry.
1434 @retval EFI_SUCCESS The requested ARP cache entries are copied into
1436 @retval EFI_NOT_FOUND No matching entries found.
1437 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
1442 IN ARP_INSTANCE_DATA
*Instance
,
1443 IN BOOLEAN BySwAddress
,
1444 IN VOID
*AddressBuffer OPTIONAL
,
1445 OUT UINT32
*EntryLength OPTIONAL
,
1446 OUT UINT32
*EntryCount OPTIONAL
,
1447 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
1452 ARP_SERVICE_DATA
*ArpService
;
1453 NET_ARP_ADDRESS MatchAddress
;
1454 FIND_OPTYPE FindOpType
;
1455 NET_LIST_ENTRY
*StartEntry
;
1456 ARP_CACHE_ENTRY
*CacheEntry
;
1457 NET_MAP FoundEntries
;
1459 EFI_ARP_FIND_DATA
*FindData
;
1460 NET_LIST_ENTRY
*CacheTable
;
1462 ArpService
= Instance
->ArpService
;
1465 // Init the FounEntries used to hold the found cache entries.
1467 NetMapInit (&FoundEntries
);
1470 // Set the MatchAddress.
1473 MatchAddress
.Type
= Instance
->ConfigData
.SwAddressType
;
1474 MatchAddress
.Length
= Instance
->ConfigData
.SwAddressLength
;
1475 FindOpType
= ByProtoAddress
;
1477 MatchAddress
.Type
= ArpService
->SnpMode
.IfType
;
1478 MatchAddress
.Length
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1479 FindOpType
= ByHwAddress
;
1482 MatchAddress
.AddressPtr
= AddressBuffer
;
1485 // Search the DeniedCacheTable
1490 // Try to find the matched entries in the DeniedCacheTable.
1492 CacheEntry
= ArpFindNextCacheEntryInTable (
1493 &ArpService
->DeniedCacheTable
,
1499 if (CacheEntry
== NULL
) {
1501 // Once the CacheEntry is NULL, there are no more matches.
1507 // Insert the found entry into the map.
1512 (VOID
*)&ArpService
->DeniedCacheTable
1516 // Let the next search start from this cache entry.
1518 StartEntry
= &CacheEntry
->List
;
1522 // Refresh the DecayTime if needed.
1524 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1529 // Search the ResolvedCacheTable
1533 CacheEntry
= ArpFindNextCacheEntryInTable (
1534 &ArpService
->ResolvedCacheTable
,
1540 if (CacheEntry
== NULL
) {
1542 // Once the CacheEntry is NULL, there are no more matches.
1548 // Insert the found entry into the map.
1553 (VOID
*)&ArpService
->ResolvedCacheTable
1557 // Let the next search start from this cache entry.
1559 StartEntry
= &CacheEntry
->List
;
1563 // Refresh the DecayTime if needed.
1565 CacheEntry
->DecayTime
= CacheEntry
->DefaultDecayTime
;
1569 Status
= EFI_SUCCESS
;
1571 FoundCount
= (UINT32
) NetMapGetCount (&FoundEntries
);
1572 if (FoundCount
== 0) {
1573 Status
= EFI_NOT_FOUND
;
1577 if (EntryLength
!= NULL
) {
1579 // Return the entry length, make sure its 8 bytes alignment.
1581 *EntryLength
= (((sizeof (EFI_ARP_FIND_DATA
) + Instance
->ConfigData
.SwAddressLength
+
1582 ArpService
->SnpMode
.HwAddressSize
) + 3) & ~(0x3));
1585 if (EntryCount
!= NULL
) {
1587 // Return the found entry count.
1589 *EntryCount
= FoundCount
;
1592 if (Entries
== NULL
) {
1597 // Allocate buffer to copy the found entries.
1599 FindData
= NetAllocatePool (FoundCount
* (*EntryLength
));
1600 if (FindData
== NULL
) {
1601 ARP_DEBUG_ERROR (("ArpFindCacheEntry: Failed to allocate memory.\n"));
1602 Status
= EFI_OUT_OF_RESOURCES
;
1607 // Return the address to the user.
1609 *Entries
= FindData
;
1612 // Dump the entries.
1614 while (!NetMapIsEmpty (&FoundEntries
)) {
1616 // Get a cache entry from the map.
1618 CacheEntry
= NetMapRemoveHead (&FoundEntries
, (VOID
**)&CacheTable
);
1621 // Set the fields in FindData.
1623 FindData
->Size
= *EntryLength
;
1624 FindData
->DenyFlag
= (BOOLEAN
)(CacheTable
== &ArpService
->DeniedCacheTable
);
1625 FindData
->StaticFlag
= (BOOLEAN
)(CacheEntry
->DefaultDecayTime
== 0);
1626 FindData
->HwAddressType
= ArpService
->SnpMode
.IfType
;
1627 FindData
->SwAddressType
= Instance
->ConfigData
.SwAddressType
;
1628 FindData
->HwAddressLength
= (UINT8
)ArpService
->SnpMode
.HwAddressSize
;
1629 FindData
->SwAddressLength
= Instance
->ConfigData
.SwAddressLength
;
1632 // Copy the software address.
1636 CacheEntry
->Addresses
[Protocol
].AddressPtr
,
1637 FindData
->SwAddressLength
1641 // Copy the hardware address.
1644 (UINT8
*)(FindData
+ 1) + FindData
->SwAddressLength
,
1645 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
1646 FindData
->HwAddressLength
1650 // Slip to the next FindData.
1652 FindData
= (EFI_ARP_FIND_DATA
*)((UINT8
*)FindData
+ *EntryLength
);
1657 NetMapClean (&FoundEntries
);