2 EFI Address Resolution Protocol (ARP) Protocol interface header file.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include <Protocol/Arp.h>
16 #include <Protocol/ManagedNetwork.h>
17 #include <Protocol/ServiceBinding.h>
19 #include <Library/DebugLib.h>
20 #include <Library/UefiDriverEntryPoint.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiLib.h>
23 #include <Library/NetLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/DpcLib.h>
30 // Ethernet protocol type definitions.
32 #define ARP_ETHER_PROTO_TYPE 0x0806
33 #define IPV4_ETHER_PROTO_TYPE 0x0800
34 #define IPV6_ETHER_PROTO_TYPE 0x86DD
37 // ARP opcode definitions.
39 #define ARP_OPCODE_REQUEST 0x0001
40 #define ARP_OPCODE_REPLY 0x0002
43 // ARP timeout, retry count and interval definitions.
45 #define ARP_DEFAULT_TIMEOUT_VALUE (400 * TICKS_PER_SECOND)
46 #define ARP_DEFAULT_RETRY_COUNT 2
47 #define ARP_DEFAULT_RETRY_INTERVAL (5 * TICKS_PER_MS)
48 #define ARP_PERIODIC_TIMER_INTERVAL (500 * TICKS_PER_MS)
51 // ARP packet head definition.
64 // ARP Address definition for internal use.
68 UINT8
*SenderProtoAddr
;
70 UINT8
*TargetProtoAddr
;
73 #define MATCH_SW_ADDRESS 0x1
74 #define MATCH_HW_ADDRESS 0x2
77 // Enumeration for the search type. A search type is specified as the keyword to find
78 // a cache entry in the cache table.
82 ByProtoAddress
= MATCH_SW_ADDRESS
,
83 ByHwAddress
= MATCH_HW_ADDRESS
,
84 ByBoth
= MATCH_SW_ADDRESS
| MATCH_HW_ADDRESS
87 #define ARP_INSTANCE_DATA_SIGNATURE SIGNATURE_32('A', 'R', 'P', 'I')
90 Returns a pointer to the ARP_INSTANCE_DATA structure from the input a.
92 If the signatures matches, then a pointer to the data structure that contains
93 a specified field of that data structure is returned.
95 @param a Pointer to the field specified by ArpProto within a data
96 structure of type ARP_INSTANCE_DATA.
99 #define ARP_INSTANCE_DATA_FROM_THIS(a) \
104 ARP_INSTANCE_DATA_SIGNATURE \
107 typedef struct _ARP_SERVICE_DATA ARP_SERVICE_DATA
;
110 // ARP instance context data structure.
114 ARP_SERVICE_DATA
*ArpService
;
116 EFI_ARP_PROTOCOL ArpProto
;
118 EFI_ARP_CONFIG_DATA ConfigData
;
123 #define ARP_SERVICE_DATA_SIGNATURE SIGNATURE_32('A', 'R', 'P', 'S')
126 Returns a pointer to the ARP_SERVICE_DATA structure from the input a.
128 If the signatures matches, then a pointer to the data structure that contains
129 a specified field of that data structure is returned.
131 @param a Pointer to the field specified by ServiceBinding within
132 a data structure of type ARP_SERVICE_DATA.
135 #define ARP_SERVICE_DATA_FROM_THIS(a) \
140 ARP_SERVICE_DATA_SIGNATURE \
144 // ARP service data structure.
146 struct _ARP_SERVICE_DATA
{
148 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding
;
150 EFI_HANDLE MnpChildHandle
;
151 EFI_HANDLE ImageHandle
;
152 EFI_HANDLE ControllerHandle
;
154 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
155 EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData
;
156 EFI_MANAGED_NETWORK_COMPLETION_TOKEN RxToken
;
158 EFI_SIMPLE_NETWORK_MODE SnpMode
;
160 UINTN ChildrenNumber
;
161 LIST_ENTRY ChildrenList
;
163 LIST_ENTRY PendingRequestTable
;
164 LIST_ENTRY DeniedCacheTable
;
165 LIST_ENTRY ResolvedCacheTable
;
167 EFI_EVENT PeriodicTimer
;
171 // User request context structure.
175 ARP_INSTANCE_DATA
*Instance
;
176 EFI_EVENT UserRequestEvent
;
177 VOID
*UserHwAddrBuffer
;
178 } USER_REQUEST_CONTEXT
;
180 #define ARP_MAX_PROTOCOL_ADDRESS_LEN sizeof(EFI_IP_ADDRESS)
181 #define ARP_MAX_HARDWARE_ADDRESS_LEN sizeof(EFI_MAC_ADDRESS)
184 UINT8 ProtoAddress
[ARP_MAX_PROTOCOL_ADDRESS_LEN
];
185 UINT8 HwAddress
[ARP_MAX_HARDWARE_ADDRESS_LEN
];
186 } NET_ARP_ADDRESS_UNION
;
189 // ARP address structure in an ARP packet.
195 NET_ARP_ADDRESS_UNION Buffer
;
199 // Enumeration for ARP address type.
207 // ARP cache entry definition.
213 UINT32 DefaultDecayTime
;
215 UINT32 NextRetryTime
;
217 NET_ARP_ADDRESS Addresses
[2];
219 LIST_ENTRY UserRequestList
;
223 This function is used to assign a station address to the ARP cache for this instance
226 Each ARP instance has one station address. The EFI_ARP_PROTOCOL driver will
227 respond to ARP requests that match this registered station address. A call to
228 this function with the ConfigData field set to NULL will reset this ARP instance.
230 Once a protocol type and station address have been assigned to this ARP instance,
231 all the following ARP functions will use this information. Attempting to change
232 the protocol type or station address to a configured ARP instance will result in errors.
234 @param This Pointer to the EFI_ARP_PROTOCOL instance.
235 @param ConfigData Pointer to the EFI_ARP_CONFIG_DATA structure.
237 @retval EFI_SUCCESS The new station address was successfully
239 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
240 This is NULL. SwAddressLength is zero when
241 ConfigData is not NULL. StationAddress is NULL
242 when ConfigData is not NULL.
243 @retval EFI_ACCESS_DENIED The SwAddressType, SwAddressLength, or
244 StationAddress is different from the one that is
246 @retval EFI_OUT_OF_RESOURCES Storage for the new StationAddress could not be
253 IN EFI_ARP_PROTOCOL
*This
,
254 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
258 This function is used to insert entries into the ARP cache.
260 ARP cache entries are typically inserted and updated by network protocol drivers
261 as network traffic is processed. Most ARP cache entries will time out and be
262 deleted if the network traffic stops. ARP cache entries that were inserted
263 by the Add() function may be static (will not time out) or dynamic (will time out).
264 Default ARP cache timeout values are not covered in most network protocol
265 specifications (although RFC 1122 comes pretty close) and will only be
266 discussed in general in this specification. The timeout values that are
267 used in the EFI Sample Implementation should be used only as a guideline.
268 Final product implementations of the EFI network stack should be tuned for
269 their expected network environments.
271 @param This Pointer to the EFI_ARP_PROTOCOL instance.
272 @param DenyFlag Set to TRUE if this entry is a deny entry. Set to
273 FALSE if this entry is a normal entry.
274 @param TargetSwAddress Pointer to a protocol address to add (or deny).
275 May be set to NULL if DenyFlag is TRUE.
276 @param TargetHwAddress Pointer to a hardware address to add (or deny).
277 May be set to NULL if DenyFlag is TRUE.
278 @param TimeoutValue Time in 100-ns units that this entry will remain
279 in the ARP cache. A value of zero means that the
280 entry is permanent. A nonzero value will override
281 the one given by Configure() if the entry to be
282 added is a dynamic entry.
283 @param Overwrite If TRUE, the matching cache entry will be
284 overwritten with the supplied parameters. If
285 FALSE, EFI_ACCESS_DENIED is returned if the
286 corresponding cache entry already exists.
288 @retval EFI_SUCCESS The entry has been added or updated.
289 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
290 This is NULL. DenyFlag is FALSE and
291 TargetHwAddress is NULL. DenyFlag is FALSE and
292 TargetSwAddress is NULL. TargetHwAddress is NULL
293 and TargetSwAddress is NULL. Both TargetSwAddress
294 and TargetHwAddress are not NULL when DenyFlag is
296 @retval EFI_OUT_OF_RESOURCES The new ARP cache entry could not be allocated.
297 @retval EFI_ACCESS_DENIED The ARP cache entry already exists and Overwrite
299 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
305 IN EFI_ARP_PROTOCOL
*This
,
307 IN VOID
*TargetSwAddress OPTIONAL
,
308 IN VOID
*TargetHwAddress OPTIONAL
,
309 IN UINT32 TimeoutValue
,
314 This function searches the ARP cache for matching entries and allocates a buffer into
315 which those entries are copied.
317 The first part of the allocated buffer is EFI_ARP_FIND_DATA, following which
318 are protocol address pairs and hardware address pairs.
319 When finding a specific protocol address (BySwAddress is TRUE and AddressBuffer
320 is not NULL), the ARP cache timeout for the found entry is reset if Refresh is
321 set to TRUE. If the found ARP cache entry is a permanent entry, it is not
324 @param This Pointer to the EFI_ARP_PROTOCOL instance.
325 @param BySwAddress Set to TRUE to look for matching software protocol
326 addresses. Set to FALSE to look for matching
327 hardware protocol addresses.
328 @param AddressBuffer Pointer to address buffer. Set to NULL to match
330 @param EntryLength The size of an entry in the entries buffer.
331 @param EntryCount The number of ARP cache entries that are found by
332 the specified criteria.
333 @param Entries Pointer to the buffer that will receive the ARP
335 @param Refresh Set to TRUE to refresh the timeout value of the
336 matching ARP cache entry.
338 @retval EFI_SUCCESS The requested ARP cache entries were copied into
340 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
341 This is NULL. Both EntryCount and EntryLength are
342 NULL, when Refresh is FALSE.
343 @retval EFI_NOT_FOUND No matching entries were found.
344 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
350 IN EFI_ARP_PROTOCOL
*This
,
351 IN BOOLEAN BySwAddress
,
352 IN VOID
*AddressBuffer OPTIONAL
,
353 OUT UINT32
*EntryLength OPTIONAL
,
354 OUT UINT32
*EntryCount OPTIONAL
,
355 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
360 This function removes specified ARP cache entries.
362 @param This Pointer to the EFI_ARP_PROTOCOL instance.
363 @param BySwAddress Set to TRUE to delete matching protocol addresses.
364 Set to FALSE to delete matching hardware
366 @param AddressBuffer Pointer to the address buffer that is used as a
367 key to look for the cache entry. Set to NULL to
370 @retval EFI_SUCCESS The entry was removed from the ARP cache.
371 @retval EFI_INVALID_PARAMETER This is NULL.
372 @retval EFI_NOT_FOUND The specified deletion key was not found.
373 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
379 IN EFI_ARP_PROTOCOL
*This
,
380 IN BOOLEAN BySwAddress
,
381 IN VOID
*AddressBuffer OPTIONAL
385 This function delete all dynamic entries from the ARP cache that match the specified
386 software protocol type.
388 @param This Pointer to the EFI_ARP_PROTOCOL instance.
390 @retval EFI_SUCCESS The cache has been flushed.
391 @retval EFI_INVALID_PARAMETER This is NULL.
392 @retval EFI_NOT_FOUND There are no matching dynamic cache entries.
393 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
399 IN EFI_ARP_PROTOCOL
*This
403 This function tries to resolve the TargetSwAddress and optionally returns a
404 TargetHwAddress if it already exists in the ARP cache.
406 @param This Pointer to the EFI_ARP_PROTOCOL instance.
407 @param TargetSwAddress Pointer to the protocol address to resolve.
408 @param ResolvedEvent Pointer to the event that will be signaled when
409 the address is resolved or some error occurs.
410 @param TargetHwAddress Pointer to the buffer for the resolved hardware
411 address in network byte order.
413 @retval EFI_SUCCESS The data is copied from the ARP cache into the
414 TargetHwAddress buffer.
415 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
416 This is NULL. TargetHwAddress is NULL.
417 @retval EFI_ACCESS_DENIED The requested address is not present in the normal
418 ARP cache but is present in the deny address list.
419 Outgoing traffic to that address is forbidden.
420 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
421 @retval EFI_NOT_READY The request has been started and is not finished.
427 IN EFI_ARP_PROTOCOL
*This
,
428 IN VOID
*TargetSwAddress OPTIONAL
,
429 IN EFI_EVENT ResolvedEvent OPTIONAL
,
430 OUT VOID
*TargetHwAddress
434 This function aborts the previous ARP request (identified by This, TargetSwAddress
435 and ResolvedEvent) that is issued by EFI_ARP_PROTOCOL.Request().
437 If the request is in the internal ARP request queue, the request is aborted
438 immediately and its ResolvedEvent is signaled. Only an asynchronous address
439 request needs to be canceled. If TargetSwAddress and ResolvedEvent are both
440 NULL, all the pending asynchronous requests that have been issued by This
441 instance will be cancelled and their corresponding events will be signaled.
443 @param This Pointer to the EFI_ARP_PROTOCOL instance.
444 @param TargetSwAddress Pointer to the protocol address in previous
446 @param ResolvedEvent Pointer to the event that is used as the
447 notification event in previous request session.
449 @retval EFI_SUCCESS The pending request session(s) is/are aborted and
450 corresponding event(s) is/are signaled.
451 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
452 This is NULL. TargetSwAddress is not NULL and
453 ResolvedEvent is NULL. TargetSwAddress is NULL and
454 ResolvedEvent is not NULL.
455 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
456 @retval EFI_NOT_FOUND The request is not issued by
457 EFI_ARP_PROTOCOL.Request().
463 IN EFI_ARP_PROTOCOL
*This
,
464 IN VOID
*TargetSwAddress OPTIONAL
,
465 IN EFI_EVENT ResolvedEvent OPTIONAL
469 Configure the instance using the ConfigData. ConfigData is already validated.
471 @param[in] Instance Pointer to the instance context data to be
473 @param[in] ConfigData Pointer to the configuration data used to
474 configure the instance.
476 @retval EFI_SUCCESS The instance is configured with the ConfigData.
477 @retval EFI_ACCESS_DENIED The instance is already configured and the
478 ConfigData tries to reset some unchangeable
480 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
481 when the SwAddressType is IPv4.
482 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
487 ArpConfigureInstance (
488 IN ARP_INSTANCE_DATA
*Instance
,
489 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
493 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
494 in the DeniedCacheTable.
496 @param[in] ArpService Pointer to the arp service context data.
497 @param[in] ProtocolAddress Pointer to the protocol address.
498 @param[in] HardwareAddress Pointer to the hardware address.
500 @return Pointer to the matched cache entry, if NULL no match is found.
504 ArpFindDeniedCacheEntry (
505 IN ARP_SERVICE_DATA
*ArpService
,
506 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
507 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
511 Find the CacheEntry which matches the requirements in the specified CacheTable.
513 @param[in] CacheTable Pointer to the arp cache table.
514 @param[in] StartEntry Pointer to the start entry this search begins with
516 @param[in] FindOpType The search type.
517 @param[in] ProtocolAddress Pointer to the protocol address to match.
518 @param[in] HardwareAddress Pointer to the hardware address to match.
520 @return Pointer to the matched arp cache entry, if NULL, no match is found.
524 ArpFindNextCacheEntryInTable (
525 IN LIST_ENTRY
*CacheTable
,
526 IN LIST_ENTRY
*StartEntry
,
527 IN FIND_OPTYPE FindOpType
,
528 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
529 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
533 Allocate a cache entry and initialize it.
535 @param[in] Instance Pointer to the instance context data.
537 @return Pointer to the new created cache entry.
542 IN ARP_INSTANCE_DATA
*Instance
546 Fill the addresses in the CacheEntry using the information passed in by
549 @param[in] CacheEntry Pointer to the cache entry.
550 @param[in] HwAddr Pointer to the software address.
551 @param[in] SwAddr Pointer to the hardware address.
557 ArpFillAddressInCacheEntry (
558 IN ARP_CACHE_ENTRY
*CacheEntry
,
559 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
560 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
564 Turn the CacheEntry into the resolved status.
566 @param[in] CacheEntry Pointer to the resolved cache entry.
567 @param[in] Instance Pointer to the instance context data.
568 @param[in] UserEvent Pointer to the UserEvent to notify.
570 @return The count of notifications sent to the instance.
575 IN ARP_CACHE_ENTRY
*CacheEntry
,
576 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
577 IN EFI_EVENT UserEvent OPTIONAL
581 Delete cache entries in all the cache tables.
583 @param[in] Instance Pointer to the instance context data.
584 @param[in] BySwAddress Delete the cache entry by software address or by
586 @param[in] AddressBuffer Pointer to the buffer containing the address to
587 match for the deletion.
588 @param[in] Force This deletion is forced or not.
590 @return The count of the deleted cache entries.
594 ArpDeleteCacheEntry (
595 IN ARP_INSTANCE_DATA
*Instance
,
596 IN BOOLEAN BySwAddress
,
597 IN UINT8
*AddressBuffer OPTIONAL
,
602 Send out an arp frame using the CacheEntry and the ArpOpCode.
604 @param[in] Instance Pointer to the instance context data.
605 @param[in] CacheEntry Pointer to the configuration data used to
606 configure the instance.
607 @param[in] ArpOpCode The opcode used to send out this Arp frame, either
615 IN ARP_INSTANCE_DATA
*Instance
,
616 IN ARP_CACHE_ENTRY
*CacheEntry
,
621 Initialize the instance context data.
623 @param[in] ArpService Pointer to the arp service context data this
625 @param[out] Instance Pointer to the instance context data.
632 IN ARP_SERVICE_DATA
*ArpService
,
633 OUT ARP_INSTANCE_DATA
*Instance
637 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
639 @param[in] Context Pointer to the context data registered to the
652 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
654 @param[in] Event The Event this notify function registered to.
655 @param[in] Context Pointer to the context data registered to the
669 Process the already sent arp packets.
671 @param[in] Context Pointer to the context data registered to the
684 Request ArpOnFrameSentDpc as a DPC at TPL_CALLBACK.
686 @param[in] Event The Event this notify function registered to.
687 @param[in] Context Pointer to the context data registered to the
701 Process the arp cache olding and drive the retrying arp requests.
703 @param[in] Event The Event this notify function registered to.
704 @param[in] Context Pointer to the context data registered to the
718 Cancel the arp request.
720 @param[in] Instance Pointer to the instance context data.
721 @param[in] TargetSwAddress Pointer to the buffer containing the target
722 software address to match the arp request.
723 @param[in] UserEvent The user event used to notify this request
726 @return The count of the cancelled requests.
731 IN ARP_INSTANCE_DATA
*Instance
,
732 IN VOID
*TargetSwAddress OPTIONAL
,
733 IN EFI_EVENT UserEvent OPTIONAL
737 Find the cache entry in the cache table.
739 @param[in] Instance Pointer to the instance context data.
740 @param[in] BySwAddress Set to TRUE to look for matching software protocol
741 addresses. Set to FALSE to look for matching
742 hardware protocol addresses.
743 @param[in] AddressBuffer Pointer to address buffer. Set to NULL to match
745 @param[out] EntryLength The size of an entry in the entries buffer.
746 @param[out] EntryCount The number of ARP cache entries that are found by
747 the specified criteria.
748 @param[out] Entries Pointer to the buffer that will receive the ARP
750 @param[in] Refresh Set to TRUE to refresh the timeout value of the
751 matching ARP cache entry.
753 @retval EFI_SUCCESS The requested ARP cache entries are copied into
755 @retval EFI_NOT_FOUND No matching entries found.
756 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
761 IN ARP_INSTANCE_DATA
*Instance
,
762 IN BOOLEAN BySwAddress
,
763 IN VOID
*AddressBuffer OPTIONAL
,
764 OUT UINT32
*EntryLength OPTIONAL
,
765 OUT UINT32
*EntryCount OPTIONAL
,
766 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,