3 Copyright (c) 2006 - 2008, 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.
27 #include <Protocol/Arp.h>
28 #include <Protocol/ManagedNetwork.h>
29 #include <Protocol/ServiceBinding.h>
31 #include <Library/DebugLib.h>
32 #include <Library/UefiDriverEntryPoint.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/UefiLib.h>
35 #include <Library/NetLib.h>
36 #include <Library/BaseLib.h>
37 #include <Library/BaseMemoryLib.h>
38 #include <Library/MemoryAllocationLib.h>
41 #define ARP_ETHER_PROTO_TYPE 0x0806
42 #define IPV4_ETHER_PROTO_TYPE 0x0800
43 #define IPV6_ETHER_PROTO_TYPE 0x86DD
45 #define ARP_OPCODE_REQUEST 0x0001
46 #define ARP_OPCODE_REPLY 0x0002
48 #define ARP_DEFAULT_TIMEOUT_VALUE (400 * TICKS_PER_SECOND)
49 #define ARP_DEFAULT_RETRY_COUNT 2
50 #define ARP_DEFAULT_RETRY_INTERVAL (5 * TICKS_PER_MS)
51 #define ARP_PERIODIC_TIMER_INTERVAL (500 * TICKS_PER_MS)
54 typedef struct _ARP_HEAD
{
63 typedef struct _ARP_ADDRESS
{
65 UINT8
*SenderProtoAddr
;
67 UINT8
*TargetProtoAddr
;
70 #define MATCH_SW_ADDRESS 0x1
71 #define MATCH_HW_ADDRESS 0x2
75 ByProtoAddress
= MATCH_SW_ADDRESS
,
76 ByHwAddress
= MATCH_HW_ADDRESS
,
77 ByBoth
= MATCH_SW_ADDRESS
| MATCH_HW_ADDRESS
80 #define ARP_INSTANCE_DATA_SIGNATURE EFI_SIGNATURE_32('A', 'R', 'P', 'I')
82 #define ARP_INSTANCE_DATA_FROM_THIS(a) \
87 ARP_INSTANCE_DATA_SIGNATURE \
90 typedef struct _ARP_SERVICE_DATA ARP_SERVICE_DATA
;
92 typedef struct _ARP_INSTANCE_DATA
{
94 ARP_SERVICE_DATA
*ArpService
;
96 EFI_ARP_PROTOCOL ArpProto
;
98 EFI_ARP_CONFIG_DATA ConfigData
;
103 #define ARP_SERVICE_DATA_SIGNATURE EFI_SIGNATURE_32('A', 'R', 'P', 'S')
105 #define ARP_SERVICE_DATA_FROM_THIS(a) \
110 ARP_SERVICE_DATA_SIGNATURE \
113 struct _ARP_SERVICE_DATA
{
115 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding
;
117 EFI_HANDLE MnpChildHandle
;
118 EFI_HANDLE ImageHandle
;
119 EFI_HANDLE ControllerHandle
;
121 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
122 EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData
;
123 EFI_MANAGED_NETWORK_COMPLETION_TOKEN RxToken
;
125 EFI_SIMPLE_NETWORK_MODE SnpMode
;
127 UINTN ChildrenNumber
;
128 LIST_ENTRY ChildrenList
;
130 LIST_ENTRY PendingRequestTable
;
131 LIST_ENTRY DeniedCacheTable
;
132 LIST_ENTRY ResolvedCacheTable
;
134 EFI_EVENT PeriodicTimer
;
137 typedef struct _USER_REQUEST_CONTEXT
{
139 ARP_INSTANCE_DATA
*Instance
;
140 EFI_EVENT UserRequestEvent
;
141 VOID
*UserHwAddrBuffer
;
142 } USER_REQUEST_CONTEXT
;
144 #define ARP_MAX_PROTOCOL_ADDRESS_LEN sizeof(EFI_IP_ADDRESS)
145 #define ARP_MAX_HARDWARE_ADDRESS_LEN sizeof(EFI_MAC_ADDRESS)
147 typedef struct _NET_ARP_ADDRESS
{
152 UINT8 ProtoAddress
[ARP_MAX_PROTOCOL_ADDRESS_LEN
];
153 UINT8 HwAddress
[ARP_MAX_HARDWARE_ADDRESS_LEN
];
162 typedef struct _ARP_CACHE_ENTRY
{
166 UINT32 DefaultDecayTime
;
168 UINT32 NextRetryTime
;
170 NET_ARP_ADDRESS Addresses
[2];
172 LIST_ENTRY UserRequestList
;
176 This function is used to assign a station address to the ARP cache for this instance
177 of the ARP driver. A call to this function with the ConfigData field set to NULL
178 will reset this ARP instance.
180 @param This Pointer to the EFI_ARP_PROTOCOL instance.
181 @param ConfigData Pointer to the EFI_ARP_CONFIG_DATA structure.
183 @retval EFI_SUCCESS The new station address was successfully
185 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
186 This is NULL. SwAddressLength is zero when
187 ConfigData is not NULL. StationAddress is NULL
188 when ConfigData is not NULL.
189 @retval EFI_ACCESS_DENIED The SwAddressType, SwAddressLength, or
190 StationAddress is different from the one that is
192 @retval EFI_OUT_OF_RESOURCES Storage for the new StationAddress could not be
199 IN EFI_ARP_PROTOCOL
*This
,
200 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
204 This function is used to insert entries into the ARP cache.
206 @param This Pointer to the EFI_ARP_PROTOCOL instance.
207 @param DenyFlag Set to TRUE if this entry is a deny entry. Set to
208 FALSE if this entry is a normal entry.
209 @param TargetSwAddress Pointer to a protocol address to add (or deny).
210 May be set to NULL if DenyFlag is TRUE.
211 @param TargetHwAddress Pointer to a hardware address to add (or deny).
212 May be set to NULL if DenyFlag is TRUE.
213 @param TimeoutValue Time in 100-ns units that this entry will remain
214 in the ARP cache. A value of zero means that the
215 entry is permanent. A nonzero value will override
216 the one given by Configure() if the entry to be
217 added is a dynamic entry.
218 @param Overwrite If TRUE, the matching cache entry will be
219 overwritten with the supplied parameters. If
220 FALSE, EFI_ACCESS_DENIED is returned if the
221 corresponding cache entry already exists.
223 @retval EFI_SUCCESS The entry has been added or updated.
224 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
225 This is NULL. DenyFlag is FALSE and
226 TargetHwAddress is NULL. DenyFlag is FALSE and
227 TargetSwAddress is NULL. TargetHwAddress is NULL
228 and TargetSwAddress is NULL. Both TargetSwAddress
229 and TargetHwAddress are not NULL when DenyFlag is
231 @retval EFI_OUT_OF_RESOURCES The new ARP cache entry could not be allocated.
232 @retval EFI_ACCESS_DENIED The ARP cache entry already exists and Overwrite
234 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
240 IN EFI_ARP_PROTOCOL
*This
,
242 IN VOID
*TargetSwAddress OPTIONAL
,
243 IN VOID
*TargetHwAddress OPTIONAL
,
244 IN UINT32 TimeoutValue
,
249 This function searches the ARP cache for matching entries and allocates a buffer into
250 which those entries are copied.
252 @param This Pointer to the EFI_ARP_PROTOCOL instance.
253 @param BySwAddress Set to TRUE to look for matching software protocol
254 addresses. Set to FALSE to look for matching
255 hardware protocol addresses.
256 @param AddressBuffer Pointer to address buffer. Set to NULL to match
258 @param EntryLength The size of an entry in the entries buffer.
259 @param EntryCount The number of ARP cache entries that are found by
260 the specified criteria.
261 @param Entries Pointer to the buffer that will receive the ARP
263 @param Refresh Set to TRUE to refresh the timeout value of the
264 matching ARP cache entry.
266 @retval EFI_SUCCESS The requested ARP cache entries were copied into
268 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
269 This is NULL. Both EntryCount and EntryLength are
270 NULL, when Refresh is FALSE.
271 @retval EFI_NOT_FOUND No matching entries were found.
272 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
278 IN EFI_ARP_PROTOCOL
*This
,
279 IN BOOLEAN BySwAddress
,
280 IN VOID
*AddressBuffer OPTIONAL
,
281 OUT UINT32
*EntryLength OPTIONAL
,
282 OUT UINT32
*EntryCount OPTIONAL
,
283 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
288 This function removes specified ARP cache entries.
290 @param This Pointer to the EFI_ARP_PROTOCOL instance.
291 @param BySwAddress Set to TRUE to delete matching protocol addresses.
292 Set to FALSE to delete matching hardware
294 @param AddressBuffer Pointer to the address buffer that is used as a
295 key to look for the cache entry. Set to NULL to
298 @retval EFI_SUCCESS The entry was removed from the ARP cache.
299 @retval EFI_INVALID_PARAMETER This is NULL.
300 @retval EFI_NOT_FOUND The specified deletion key was not found.
301 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
307 IN EFI_ARP_PROTOCOL
*This
,
308 IN BOOLEAN BySwAddress
,
309 IN VOID
*AddressBuffer OPTIONAL
313 This function delete all dynamic entries from the ARP cache that match the specified
314 software protocol type.
316 @param This Pointer to the EFI_ARP_PROTOCOL instance.
318 @retval EFI_SUCCESS The cache has been flushed.
319 @retval EFI_INVALID_PARAMETER This is NULL.
320 @retval EFI_NOT_FOUND There are no matching dynamic cache entries.
321 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
327 IN EFI_ARP_PROTOCOL
*This
331 This function tries to resolve the TargetSwAddress and optionally returns a
332 TargetHwAddress if it already exists in the ARP cache.
334 @param This Pointer to the EFI_ARP_PROTOCOL instance.
335 @param TargetSwAddress Pointer to the protocol address to resolve.
336 @param ResolvedEvent Pointer to the event that will be signaled when
337 the address is resolved or some error occurs.
338 @param TargetHwAddress Pointer to the buffer for the resolved hardware
339 address in network byte order.
341 @retval EFI_SUCCESS The data is copied from the ARP cache into the
342 TargetHwAddress buffer.
343 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
344 This is NULL. TargetHwAddress is NULL.
345 @retval EFI_ACCESS_DENIED The requested address is not present in the normal
346 ARP cache but is present in the deny address list.
347 Outgoing traffic to that address is forbidden.
348 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
349 @retval EFI_NOT_READY The request has been started and is not finished.
355 IN EFI_ARP_PROTOCOL
*This
,
356 IN VOID
*TargetSwAddress OPTIONAL
,
357 IN EFI_EVENT ResolvedEvent OPTIONAL
,
358 OUT VOID
*TargetHwAddress
362 This function aborts the previous ARP request (identified by This, TargetSwAddress
363 and ResolvedEvent) that is issued by EFI_ARP_PROTOCOL.Request().
365 @param This Pointer to the EFI_ARP_PROTOCOL instance.
366 @param TargetSwAddress Pointer to the protocol address in previous
368 @param ResolvedEvent Pointer to the event that is used as the
369 notification event in previous request session.
371 @retval EFI_SUCCESS The pending request session(s) is/are aborted and
372 corresponding event(s) is/are signaled.
373 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
374 This is NULL. TargetSwAddress is not NULL and
375 ResolvedEvent is NULL. TargetSwAddress is NULL and
376 ResolvedEvent is not NULL.
377 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
378 @retval EFI_NOT_FOUND The request is not issued by
379 EFI_ARP_PROTOCOL.Request().
385 IN EFI_ARP_PROTOCOL
*This
,
386 IN VOID
*TargetSwAddress OPTIONAL
,
387 IN EFI_EVENT ResolvedEvent OPTIONAL
391 Configure the instance using the ConfigData. ConfigData is already validated.
393 @param Instance Pointer to the instance context data to be
395 @param ConfigData Pointer to the configuration data used to
396 configure the instance.
398 @retval EFI_SUCCESS The instance is configured with the ConfigData.
399 @retval EFI_ACCESS_DENIED The instance is already configured and the
400 ConfigData tries to reset some unchangeable
402 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
403 when the SwAddressType is IPv4.
404 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
409 ArpConfigureInstance (
410 IN ARP_INSTANCE_DATA
*Instance
,
411 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
415 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
416 in the DeniedCacheTable.
418 @param ArpService Pointer to the arp service context data.
419 @param ProtocolAddress Pointer to the protocol address.
420 @param HardwareAddress Pointer to the hardware address.
422 @return Pointer to the matched cache entry, if NULL no match is found.
426 ArpFindDeniedCacheEntry (
427 IN ARP_SERVICE_DATA
*ArpService
,
428 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
429 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
433 Find the CacheEntry which matches the requirements in the specified CacheTable.
435 @param CacheTable Pointer to the arp cache table.
436 @param StartEntry Pointer to the start entry this search begins with
438 @param FindOpType The search type.
439 @param ProtocolAddress Pointer to the protocol address to match.
440 @param HardwareAddress Pointer to the hardware address to match.
442 @return Pointer to the matched arp cache entry, if NULL, no match is found.
446 ArpFindNextCacheEntryInTable (
447 IN LIST_ENTRY
*CacheTable
,
448 IN LIST_ENTRY
*StartEntry
,
449 IN FIND_OPTYPE FindOpType
,
450 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
451 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
455 Allocate a cache entry and initialize it.
457 @param Instance Pointer to the instance context data.
459 @return Pointer to the new created cache entry.
464 IN ARP_INSTANCE_DATA
*Instance
468 Fill the addresses in the CacheEntry using the information passed in by
471 @param CacheEntry Pointer to the cache entry.
472 @param HwAddr Pointer to the software address.
473 @param SwAddr Pointer to the hardware address.
479 ArpFillAddressInCacheEntry (
480 IN ARP_CACHE_ENTRY
*CacheEntry
,
481 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
482 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
486 Turn the CacheEntry into the resolved status.
488 @param CacheEntry Pointer to the resolved cache entry.
489 @param Instance Pointer to the instance context data.
490 @param UserEvent Pointer to the UserEvent to notify.
492 @return The count of notifications sent to the instance.
497 IN ARP_CACHE_ENTRY
*CacheEntry
,
498 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
499 IN EFI_EVENT UserEvent OPTIONAL
503 Delete cache entries in all the cache tables.
505 @param Instance Pointer to the instance context data.
506 @param BySwAddress Delete the cache entry by software address or by
508 @param AddressBuffer Pointer to the buffer containing the address to
509 match for the deletion.
510 @param Force This deletion is forced or not.
512 @return The count of the deleted cache entries.
516 ArpDeleteCacheEntry (
517 IN ARP_INSTANCE_DATA
*Instance
,
518 IN BOOLEAN BySwAddress
,
519 IN UINT8
*AddressBuffer OPTIONAL
,
524 Send out an arp frame using the CachEntry and the ArpOpCode.
526 @param Instance Pointer to the instance context data.
527 @param CacheEntry Pointer to the configuration data used to
528 configure the instance.
529 @param ArpOpCode The opcode used to send out this Arp frame, either
537 IN ARP_INSTANCE_DATA
*Instance
,
538 IN ARP_CACHE_ENTRY
*CacheEntry
,
543 Initialize the instance context data.
545 @param ArpService Pointer to the arp service context data this
547 @param Instance Pointer to the instance context data.
554 IN ARP_SERVICE_DATA
*ArpService
,
555 IN ARP_INSTANCE_DATA
*Instance
559 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
561 @param Context Pointer to the context data registerd to the
574 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
576 @param Event The Event this notify function registered to.
577 @param Context Pointer to the context data registerd to the
591 Process the already sent arp packets.
593 @param Context Pointer to the context data registerd to the
606 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
608 @param Event The Event this notify function registered to.
609 @param Context Pointer to the context data registerd to the
623 Process the arp cache olding and drive the retrying arp requests.
625 @param Event The Event this notify function registered to.
626 @param Context Pointer to the context data registerd to the
640 Cancel the arp request.
642 @param Instance Pointer to the instance context data.
643 @param TargetSwAddress Pointer to the buffer containing the target
644 software address to match the arp request.
645 @param UserEvent The user event used to notify this request
648 @return The count of the cancelled requests.
653 IN ARP_INSTANCE_DATA
*Instance
,
654 IN VOID
*TargetSwAddress OPTIONAL
,
655 IN EFI_EVENT UserEvent OPTIONAL
659 Find the cache entry in the cache table.
661 @param Instance Pointer to the instance context data.
662 @param BySwAddress Set to TRUE to look for matching software protocol
663 addresses. Set to FALSE to look for matching
664 hardware protocol addresses.
665 @param AddressBuffer Pointer to address buffer. Set to NULL to match
667 @param EntryLength The size of an entry in the entries buffer.
668 @param EntryCount The number of ARP cache entries that are found by
669 the specified criteria.
670 @param Entries Pointer to the buffer that will receive the ARP
672 @param Refresh Set to TRUE to refresh the timeout value of the
673 matching ARP cache entry.
675 @retval EFI_SUCCESS The requested ARP cache entries are copied into
677 @retval EFI_NOT_FOUND No matching entries found.
678 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
683 IN ARP_INSTANCE_DATA
*Instance
,
684 IN BOOLEAN BySwAddress
,
685 IN VOID
*AddressBuffer OPTIONAL
,
686 OUT UINT32
*EntryLength OPTIONAL
,
687 OUT UINT32
*EntryCount OPTIONAL
,
688 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,