2 Implementation of EFI Address Resolution Protocol (ARP) Protocol interface functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 This function is used to assign a station address to the ARP cache for this instance
16 Each ARP instance has one station address. The EFI_ARP_PROTOCOL driver will
17 respond to ARP requests that match this registered station address. A call to
18 this function with the ConfigData field set to NULL will reset this ARP instance.
20 Once a protocol type and station address have been assigned to this ARP instance,
21 all the following ARP functions will use this information. Attempting to change
22 the protocol type or station address to a configured ARP instance will result in errors.
24 @param This Pointer to the EFI_ARP_PROTOCOL instance.
25 @param ConfigData Pointer to the EFI_ARP_CONFIG_DATA structure.
27 @retval EFI_SUCCESS The new station address was successfully
29 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
30 This is NULL. SwAddressLength is zero when
31 ConfigData is not NULL. StationAddress is NULL
32 when ConfigData is not NULL.
33 @retval EFI_ACCESS_DENIED The SwAddressType, SwAddressLength, or
34 StationAddress is different from the one that is
36 @retval EFI_OUT_OF_RESOURCES Storage for the new StationAddress could not be
43 IN EFI_ARP_PROTOCOL
*This
,
44 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
48 ARP_INSTANCE_DATA
*Instance
;
52 return EFI_INVALID_PARAMETER
;
55 if ((ConfigData
!= NULL
) &&
56 ((ConfigData
->SwAddressLength
== 0) ||
57 (ConfigData
->StationAddress
== NULL
) ||
58 (ConfigData
->SwAddressType
<= 1500))) {
59 return EFI_INVALID_PARAMETER
;
62 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
64 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
67 // Configure this instance, the ConfigData has already passed the basic checks.
69 Status
= ArpConfigureInstance (Instance
, ConfigData
);
71 gBS
->RestoreTPL (OldTpl
);
78 This function is used to insert entries into the ARP cache.
80 ARP cache entries are typically inserted and updated by network protocol drivers
81 as network traffic is processed. Most ARP cache entries will time out and be
82 deleted if the network traffic stops. ARP cache entries that were inserted
83 by the Add() function may be static (will not time out) or dynamic (will time out).
84 Default ARP cache timeout values are not covered in most network protocol
85 specifications (although RFC 1122 comes pretty close) and will only be
86 discussed in general in this specification. The timeout values that are
87 used in the EFI Sample Implementation should be used only as a guideline.
88 Final product implementations of the EFI network stack should be tuned for
89 their expected network environments.
91 @param This Pointer to the EFI_ARP_PROTOCOL instance.
92 @param DenyFlag Set to TRUE if this entry is a deny entry. Set to
93 FALSE if this entry is a normal entry.
94 @param TargetSwAddress Pointer to a protocol address to add (or deny).
95 May be set to NULL if DenyFlag is TRUE.
96 @param TargetHwAddress Pointer to a hardware address to add (or deny).
97 May be set to NULL if DenyFlag is TRUE.
98 @param TimeoutValue Time in 100-ns units that this entry will remain
99 in the ARP cache. A value of zero means that the
100 entry is permanent. A nonzero value will override
101 the one given by Configure() if the entry to be
102 added is a dynamic entry.
103 @param Overwrite If TRUE, the matching cache entry will be
104 overwritten with the supplied parameters. If
105 FALSE, EFI_ACCESS_DENIED is returned if the
106 corresponding cache entry already exists.
108 @retval EFI_SUCCESS The entry has been added or updated.
109 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
110 This is NULL. DenyFlag is FALSE and
111 TargetHwAddress is NULL. DenyFlag is FALSE and
112 TargetSwAddress is NULL. TargetHwAddress is NULL
113 and TargetSwAddress is NULL. Both TargetSwAddress
114 and TargetHwAddress are not NULL when DenyFlag is
116 @retval EFI_OUT_OF_RESOURCES The new ARP cache entry could not be allocated.
117 @retval EFI_ACCESS_DENIED The ARP cache entry already exists and Overwrite
119 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
125 IN EFI_ARP_PROTOCOL
*This
,
127 IN VOID
*TargetSwAddress OPTIONAL
,
128 IN VOID
*TargetHwAddress OPTIONAL
,
129 IN UINT32 TimeoutValue
,
134 ARP_INSTANCE_DATA
*Instance
;
135 ARP_SERVICE_DATA
*ArpService
;
136 ARP_CACHE_ENTRY
*CacheEntry
;
137 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
138 NET_ARP_ADDRESS MatchAddress
[2];
142 return EFI_INVALID_PARAMETER
;
145 if (((!DenyFlag
) && ((TargetHwAddress
== NULL
) || (TargetSwAddress
== NULL
))) ||
146 (DenyFlag
&& (TargetHwAddress
!= NULL
) && (TargetSwAddress
!= NULL
)) ||
147 ((TargetHwAddress
== NULL
) && (TargetSwAddress
== NULL
))) {
148 return EFI_INVALID_PARAMETER
;
151 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
153 if (!Instance
->Configured
) {
154 return EFI_NOT_STARTED
;
157 Status
= EFI_SUCCESS
;
158 ArpService
= Instance
->ArpService
;
159 SnpMode
= &Instance
->ArpService
->SnpMode
;
162 // Fill the hardware address part in the MatchAddress.
164 MatchAddress
[Hardware
].Type
= SnpMode
->IfType
;
165 MatchAddress
[Hardware
].Length
= (UINT8
) SnpMode
->HwAddressSize
;
166 MatchAddress
[Hardware
].AddressPtr
= TargetHwAddress
;
169 // Fill the software address part in the MatchAddress.
171 MatchAddress
[Protocol
].Type
= Instance
->ConfigData
.SwAddressType
;
172 MatchAddress
[Protocol
].Length
= Instance
->ConfigData
.SwAddressLength
;
173 MatchAddress
[Protocol
].AddressPtr
= TargetSwAddress
;
175 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
178 // See whether the entry to add exists. Check the DeinedCacheTable first.
180 CacheEntry
= ArpFindDeniedCacheEntry (
182 &MatchAddress
[Protocol
],
183 &MatchAddress
[Hardware
]
186 if (CacheEntry
== NULL
) {
188 // Check the ResolvedCacheTable
190 CacheEntry
= ArpFindNextCacheEntryInTable (
191 &ArpService
->ResolvedCacheTable
,
194 &MatchAddress
[Protocol
],
195 &MatchAddress
[Hardware
]
199 if ((CacheEntry
!= NULL
) && !Overwrite
) {
201 // The entry to add exists, if not Overwirte, deny this add request.
203 Status
= EFI_ACCESS_DENIED
;
207 if ((CacheEntry
== NULL
) && (TargetSwAddress
!= NULL
)) {
209 // Check whether there are pending requests matching the entry to be added.
211 CacheEntry
= ArpFindNextCacheEntryInTable (
212 &ArpService
->PendingRequestTable
,
215 &MatchAddress
[Protocol
],
220 if (CacheEntry
!= NULL
) {
222 // Remove it from the Table.
224 RemoveEntryList (&CacheEntry
->List
);
227 // It's a new entry, allocate memory for the entry.
229 CacheEntry
= ArpAllocCacheEntry (Instance
);
231 if (CacheEntry
== NULL
) {
232 DEBUG ((EFI_D_ERROR
, "ArpAdd: Failed to allocate pool for CacheEntry.\n"));
233 Status
= EFI_OUT_OF_RESOURCES
;
239 // Overwrite these parameters.
241 CacheEntry
->DefaultDecayTime
= TimeoutValue
;
242 CacheEntry
->DecayTime
= TimeoutValue
;
245 // Fill in the addresses.
247 ArpFillAddressInCacheEntry (
249 &MatchAddress
[Hardware
],
250 &MatchAddress
[Protocol
]
254 // Inform the user if there is any.
256 ArpAddressResolved (CacheEntry
, NULL
, NULL
);
259 // Add this CacheEntry to the corresponding CacheTable.
262 InsertHeadList (&ArpService
->DeniedCacheTable
, &CacheEntry
->List
);
264 InsertHeadList (&ArpService
->ResolvedCacheTable
, &CacheEntry
->List
);
269 gBS
->RestoreTPL (OldTpl
);
276 This function searches the ARP cache for matching entries and allocates a buffer into
277 which those entries are copied.
279 The first part of the allocated buffer is EFI_ARP_FIND_DATA, following which
280 are protocol address pairs and hardware address pairs.
281 When finding a specific protocol address (BySwAddress is TRUE and AddressBuffer
282 is not NULL), the ARP cache timeout for the found entry is reset if Refresh is
283 set to TRUE. If the found ARP cache entry is a permanent entry, it is not
286 @param This Pointer to the EFI_ARP_PROTOCOL instance.
287 @param BySwAddress Set to TRUE to look for matching software protocol
288 addresses. Set to FALSE to look for matching
289 hardware protocol addresses.
290 @param AddressBuffer Pointer to address buffer. Set to NULL to match
292 @param EntryLength The size of an entry in the entries buffer.
293 @param EntryCount The number of ARP cache entries that are found by
294 the specified criteria.
295 @param Entries Pointer to the buffer that will receive the ARP
297 @param Refresh Set to TRUE to refresh the timeout value of the
298 matching ARP cache entry.
300 @retval EFI_SUCCESS The requested ARP cache entries were copied into
302 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
303 This is NULL. Both EntryCount and EntryLength are
304 NULL, when Refresh is FALSE.
305 @retval EFI_NOT_FOUND No matching entries were found.
306 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
312 IN EFI_ARP_PROTOCOL
*This
,
313 IN BOOLEAN BySwAddress
,
314 IN VOID
*AddressBuffer OPTIONAL
,
315 OUT UINT32
*EntryLength OPTIONAL
,
316 OUT UINT32
*EntryCount OPTIONAL
,
317 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
322 ARP_INSTANCE_DATA
*Instance
;
325 if ((This
== NULL
) ||
326 (!Refresh
&& (EntryCount
== NULL
) && (EntryLength
== NULL
)) ||
327 ((Entries
!= NULL
) && ((EntryLength
== NULL
) || (EntryCount
== NULL
)))) {
328 return EFI_INVALID_PARAMETER
;
331 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
333 if (!Instance
->Configured
) {
334 return EFI_NOT_STARTED
;
337 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
340 // All the check passed, find the cache entries now.
342 Status
= ArpFindCacheEntry (
352 gBS
->RestoreTPL (OldTpl
);
359 This function removes specified ARP cache entries.
361 @param This Pointer to the EFI_ARP_PROTOCOL instance.
362 @param BySwAddress Set to TRUE to delete matching protocol addresses.
363 Set to FALSE to delete matching hardware
365 @param AddressBuffer Pointer to the address buffer that is used as a
366 key to look for the cache entry. Set to NULL to
369 @retval EFI_SUCCESS The entry was removed from the ARP cache.
370 @retval EFI_INVALID_PARAMETER This is NULL.
371 @retval EFI_NOT_FOUND The specified deletion key was not found.
372 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
378 IN EFI_ARP_PROTOCOL
*This
,
379 IN BOOLEAN BySwAddress
,
380 IN VOID
*AddressBuffer OPTIONAL
383 ARP_INSTANCE_DATA
*Instance
;
388 return EFI_INVALID_PARAMETER
;
391 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
393 if (!Instance
->Configured
) {
394 return EFI_NOT_STARTED
;
397 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
400 // Delete the specified cache entries.
402 Count
= ArpDeleteCacheEntry (Instance
, BySwAddress
, AddressBuffer
, TRUE
);
404 gBS
->RestoreTPL (OldTpl
);
406 return (Count
== 0) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
411 This function delete all dynamic entries from the ARP cache that match the specified
412 software protocol type.
414 @param This Pointer to the EFI_ARP_PROTOCOL instance.
416 @retval EFI_SUCCESS The cache has been flushed.
417 @retval EFI_INVALID_PARAMETER This is NULL.
418 @retval EFI_NOT_FOUND There are no matching dynamic cache entries.
419 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
425 IN EFI_ARP_PROTOCOL
*This
428 ARP_INSTANCE_DATA
*Instance
;
433 return EFI_INVALID_PARAMETER
;
436 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
438 if (!Instance
->Configured
) {
439 return EFI_NOT_STARTED
;
442 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
445 // Delete the dynamic entries from the cache table.
447 Count
= ArpDeleteCacheEntry (Instance
, FALSE
, NULL
, FALSE
);
449 gBS
->RestoreTPL (OldTpl
);
451 return (Count
== 0) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
456 This function tries to resolve the TargetSwAddress and optionally returns a
457 TargetHwAddress if it already exists in the ARP cache.
459 @param This Pointer to the EFI_ARP_PROTOCOL instance.
460 @param TargetSwAddress Pointer to the protocol address to resolve.
461 @param ResolvedEvent Pointer to the event that will be signaled when
462 the address is resolved or some error occurs.
463 @param TargetHwAddress Pointer to the buffer for the resolved hardware
464 address in network byte order.
466 @retval EFI_SUCCESS The data is copied from the ARP cache into the
467 TargetHwAddress buffer.
468 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
469 This is NULL. TargetHwAddress is NULL.
470 @retval EFI_ACCESS_DENIED The requested address is not present in the normal
471 ARP cache but is present in the deny address list.
472 Outgoing traffic to that address is forbidden.
473 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
474 @retval EFI_NOT_READY The request has been started and is not finished.
480 IN EFI_ARP_PROTOCOL
*This
,
481 IN VOID
*TargetSwAddress OPTIONAL
,
482 IN EFI_EVENT ResolvedEvent OPTIONAL
,
483 OUT VOID
*TargetHwAddress
487 ARP_INSTANCE_DATA
*Instance
;
488 ARP_SERVICE_DATA
*ArpService
;
489 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
490 ARP_CACHE_ENTRY
*CacheEntry
;
491 NET_ARP_ADDRESS HardwareAddress
;
492 NET_ARP_ADDRESS ProtocolAddress
;
493 USER_REQUEST_CONTEXT
*RequestContext
;
496 if ((This
== NULL
) || (TargetHwAddress
== NULL
)) {
497 return EFI_INVALID_PARAMETER
;
500 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
502 if (!Instance
->Configured
) {
503 return EFI_NOT_STARTED
;
506 Status
= EFI_SUCCESS
;
507 ArpService
= Instance
->ArpService
;
508 SnpMode
= &ArpService
->SnpMode
;
510 if ((TargetSwAddress
== NULL
) ||
511 ((Instance
->ConfigData
.SwAddressType
== IPV4_ETHER_PROTO_TYPE
) &&
512 IP4_IS_LOCAL_BROADCAST (*((UINT32
*)TargetSwAddress
)))) {
514 // Return the hardware broadcast address.
516 CopyMem (TargetHwAddress
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
);
521 if ((Instance
->ConfigData
.SwAddressType
== IPV4_ETHER_PROTO_TYPE
) &&
522 IP4_IS_MULTICAST (NTOHL (*((UINT32
*)TargetSwAddress
)))) {
524 // If the software address is an IPv4 multicast address, invoke Mnp to
525 // resolve the address.
527 Status
= ArpService
->Mnp
->McastIpToMac (
536 HardwareAddress
.Type
= SnpMode
->IfType
;
537 HardwareAddress
.Length
= (UINT8
)SnpMode
->HwAddressSize
;
538 HardwareAddress
.AddressPtr
= NULL
;
540 ProtocolAddress
.Type
= Instance
->ConfigData
.SwAddressType
;
541 ProtocolAddress
.Length
= Instance
->ConfigData
.SwAddressLength
;
542 ProtocolAddress
.AddressPtr
= TargetSwAddress
;
545 // Initialize the TargetHwAddrss to a zero address.
547 ZeroMem (TargetHwAddress
, SnpMode
->HwAddressSize
);
549 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
552 // Check whether the software address is in the denied table.
554 CacheEntry
= ArpFindDeniedCacheEntry (ArpService
, &ProtocolAddress
, NULL
);
555 if (CacheEntry
!= NULL
) {
556 Status
= EFI_ACCESS_DENIED
;
561 // Check whether the software address is already resolved.
563 CacheEntry
= ArpFindNextCacheEntryInTable (
564 &ArpService
->ResolvedCacheTable
,
570 if (CacheEntry
!= NULL
) {
572 // Resolved, copy the address into the user buffer.
576 CacheEntry
->Addresses
[Hardware
].AddressPtr
,
577 CacheEntry
->Addresses
[Hardware
].Length
583 if (ResolvedEvent
== NULL
) {
584 Status
= EFI_NOT_READY
;
589 // Create a request context for this arp request.
591 RequestContext
= AllocatePool (sizeof(USER_REQUEST_CONTEXT
));
592 if (RequestContext
== NULL
) {
593 DEBUG ((EFI_D_ERROR
, "ArpRequest: Allocate memory for RequestContext failed.\n"));
595 Status
= EFI_OUT_OF_RESOURCES
;
599 RequestContext
->Instance
= Instance
;
600 RequestContext
->UserRequestEvent
= ResolvedEvent
;
601 RequestContext
->UserHwAddrBuffer
= TargetHwAddress
;
602 InitializeListHead (&RequestContext
->List
);
605 // Check whether there is a same request.
607 CacheEntry
= ArpFindNextCacheEntryInTable (
608 &ArpService
->PendingRequestTable
,
614 if (CacheEntry
!= NULL
) {
616 CacheEntry
->NextRetryTime
= Instance
->ConfigData
.RetryTimeOut
;
617 CacheEntry
->RetryCount
= Instance
->ConfigData
.RetryCount
;
620 // Allocate a cache entry for this request.
622 CacheEntry
= ArpAllocCacheEntry (Instance
);
623 if (CacheEntry
== NULL
) {
624 DEBUG ((EFI_D_ERROR
, "ArpRequest: Allocate memory for CacheEntry failed.\n"));
625 FreePool (RequestContext
);
627 Status
= EFI_OUT_OF_RESOURCES
;
632 // Fill the software address.
634 ArpFillAddressInCacheEntry (CacheEntry
, &HardwareAddress
, &ProtocolAddress
);
637 // Add this entry into the PendingRequestTable.
639 InsertTailList (&ArpService
->PendingRequestTable
, &CacheEntry
->List
);
643 // Link this request context into the cache entry.
645 InsertHeadList (&CacheEntry
->UserRequestList
, &RequestContext
->List
);
648 // Send out the ARP Request frame.
650 ArpSendFrame (Instance
, CacheEntry
, ARP_OPCODE_REQUEST
);
651 Status
= EFI_NOT_READY
;
655 gBS
->RestoreTPL (OldTpl
);
659 if ((ResolvedEvent
!= NULL
) && (Status
== EFI_SUCCESS
)) {
660 gBS
->SignalEvent (ResolvedEvent
);
663 // Dispatch the DPC queued by the NotifyFunction of ResolvedEvent.
673 This function aborts the previous ARP request (identified by This, TargetSwAddress
674 and ResolvedEvent) that is issued by EFI_ARP_PROTOCOL.Request().
676 If the request is in the internal ARP request queue, the request is aborted
677 immediately and its ResolvedEvent is signaled. Only an asynchronous address
678 request needs to be canceled. If TargeSwAddress and ResolveEvent are both
679 NULL, all the pending asynchronous requests that have been issued by This
680 instance will be cancelled and their corresponding events will be signaled.
682 @param This Pointer to the EFI_ARP_PROTOCOL instance.
683 @param TargetSwAddress Pointer to the protocol address in previous
685 @param ResolvedEvent Pointer to the event that is used as the
686 notification event in previous request session.
688 @retval EFI_SUCCESS The pending request session(s) is/are aborted and
689 corresponding event(s) is/are signaled.
690 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
691 This is NULL. TargetSwAddress is not NULL and
692 ResolvedEvent is NULL. TargetSwAddress is NULL and
693 ResolvedEvent is not NULL.
694 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
695 @retval EFI_NOT_FOUND The request is not issued by
696 EFI_ARP_PROTOCOL.Request().
702 IN EFI_ARP_PROTOCOL
*This
,
703 IN VOID
*TargetSwAddress OPTIONAL
,
704 IN EFI_EVENT ResolvedEvent OPTIONAL
707 ARP_INSTANCE_DATA
*Instance
;
711 if ((This
== NULL
) ||
712 ((TargetSwAddress
!= NULL
) && (ResolvedEvent
== NULL
)) ||
713 ((TargetSwAddress
== NULL
) && (ResolvedEvent
!= NULL
))) {
714 return EFI_INVALID_PARAMETER
;
717 Instance
= ARP_INSTANCE_DATA_FROM_THIS (This
);
719 if (!Instance
->Configured
) {
720 return EFI_NOT_STARTED
;
723 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
726 // Cancel the specified request.
728 Count
= ArpCancelRequest (Instance
, TargetSwAddress
, ResolvedEvent
);
731 // Dispatch the DPCs queued by the NotifyFunction of the events signaled
732 // by ArpCancleRequest.
736 gBS
->RestoreTPL (OldTpl
);
738 return (Count
== 0) ? EFI_NOT_FOUND
: EFI_SUCCESS
;