4 Copyright (c) 2006 - 2008, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Protocol/Arp.h>
22 #include <Protocol/ManagedNetwork.h>
23 #include <Protocol/ServiceBinding.h>
25 #include <Library/DebugLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/UefiLib.h>
29 #include <Library/NetLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/MemoryAllocationLib.h>
35 #define ARP_ETHER_PROTO_TYPE 0x0806
36 #define IPV4_ETHER_PROTO_TYPE 0x0800
37 #define IPV6_ETHER_PROTO_TYPE 0x86DD
39 #define ARP_OPCODE_REQUEST 0x0001
40 #define ARP_OPCODE_REPLY 0x0002
42 #define ARP_DEFAULT_TIMEOUT_VALUE (400 * TICKS_PER_SECOND)
43 #define ARP_DEFAULT_RETRY_COUNT 2
44 #define ARP_DEFAULT_RETRY_INTERVAL (5 * TICKS_PER_MS)
45 #define ARP_PERIODIC_TIMER_INTERVAL (500 * TICKS_PER_MS)
59 UINT8
*SenderProtoAddr
;
61 UINT8
*TargetProtoAddr
;
64 #define MATCH_SW_ADDRESS 0x1
65 #define MATCH_HW_ADDRESS 0x2
69 ByProtoAddress
= MATCH_SW_ADDRESS
,
70 ByHwAddress
= MATCH_HW_ADDRESS
,
71 ByBoth
= MATCH_SW_ADDRESS
| MATCH_HW_ADDRESS
74 #define ARP_INSTANCE_DATA_SIGNATURE SIGNATURE_32('A', 'R', 'P', 'I')
79 #define ARP_INSTANCE_DATA_FROM_THIS(a) \
84 ARP_INSTANCE_DATA_SIGNATURE \
87 typedef struct _ARP_SERVICE_DATA ARP_SERVICE_DATA
;
91 ARP_SERVICE_DATA
*ArpService
;
93 EFI_ARP_PROTOCOL ArpProto
;
95 EFI_ARP_CONFIG_DATA ConfigData
;
100 #define ARP_SERVICE_DATA_SIGNATURE 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
;
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)
152 UINT8 ProtoAddress
[ARP_MAX_PROTOCOL_ADDRESS_LEN
];
153 UINT8 HwAddress
[ARP_MAX_HARDWARE_ADDRESS_LEN
];
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. Each ARP instance has one station address. The EFI_ARP_PROTOCOL
178 driver will respond to ARP requests that match this registered station address.
179 A call to this function with the ConfigData field set to NULL will reset this
182 Once a protocol type and station address have been assigned to this ARP instance,
183 all the following ARP functions will use this information. Attempting to change
184 the protocol type or station address to a configured ARP instance will result in errors.
186 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
187 @param[in] ConfigData Pointer to the EFI_ARP_CONFIG_DATA structure.
189 @retval EFI_SUCCESS The new station address was successfully
191 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
192 This is NULL. SwAddressLength is zero when
193 ConfigData is not NULL. StationAddress is NULL
194 when ConfigData is not NULL.
195 @retval EFI_ACCESS_DENIED The SwAddressType, SwAddressLength, or
196 StationAddress is different from the one that is
198 @retval EFI_OUT_OF_RESOURCES Storage for the new StationAddress could not be
205 IN EFI_ARP_PROTOCOL
*This
,
206 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
210 This function is used to insert entries into the ARP cache.
212 ARP cache entries are typically inserted and updated by network protocol drivers
213 as network traffic is processed. Most ARP cache entries will time out and be
214 deleted if the network traffic stops. ARP cache entries that were inserted
215 by the Add() function may be static (will not time out) or dynamic (will time out).
216 Default ARP cache timeout values are not covered in most network protocol
217 specifications (although RFC 1122 comes pretty close) and will only be
218 discussed in general in this specification. The timeout values that are
219 used in the EFI Sample Implementation should be used only as a guideline.
220 Final product implementations of the EFI network stack should be tuned for
221 their expected network environments.
223 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
224 @param[in] DenyFlag Set to TRUE if this entry is a deny entry. Set to
225 FALSE if this entry is a normal entry.
226 @param[in] TargetSwAddress Pointer to a protocol address to add (or deny).
227 May be set to NULL if DenyFlag is TRUE.
228 @param[in] TargetHwAddress Pointer to a hardware address to add (or deny).
229 May be set to NULL if DenyFlag is TRUE.
230 @param[in] TimeoutValue Time in 100-ns units that this entry will remain
231 in the ARP cache. A value of zero means that the
232 entry is permanent. A nonzero value will override
233 the one given by Configure() if the entry to be
234 added is a dynamic entry.
235 @param[in] Overwrite If TRUE, the matching cache entry will be
236 overwritten with the supplied parameters. If
237 FALSE, EFI_ACCESS_DENIED is returned if the
238 corresponding cache entry already exists.
240 @retval EFI_SUCCESS The entry has been added or updated.
241 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
242 This is NULL. DenyFlag is FALSE and
243 TargetHwAddress is NULL. DenyFlag is FALSE and
244 TargetSwAddress is NULL. TargetHwAddress is NULL
245 and TargetSwAddress is NULL. Both TargetSwAddress
246 and TargetHwAddress are not NULL when DenyFlag is
248 @retval EFI_OUT_OF_RESOURCES The new ARP cache entry could not be allocated.
249 @retval EFI_ACCESS_DENIED The ARP cache entry already exists and Overwrite
251 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
257 IN EFI_ARP_PROTOCOL
*This
,
259 IN VOID
*TargetSwAddress OPTIONAL
,
260 IN VOID
*TargetHwAddress OPTIONAL
,
261 IN UINT32 TimeoutValue
,
266 This function searches the ARP cache for matching entries and allocates a buffer into
267 which those entries are copied.
269 The first part of the allocated buffer is EFI_ARP_FIND_DATA, following which
270 are protocol address pairs and hardware address pairs.
271 When finding a specific protocol address (BySwAddress is TRUE and AddressBuffer
272 is not NULL), the ARP cache timeout for the found entry is reset if Refresh is
273 set to TRUE. If the found ARP cache entry is a permanent entry, it is not
276 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
277 @param[in] BySwAddress Set to TRUE to look for matching software protocol
278 addresses. Set to FALSE to look for matching
279 hardware protocol addresses.
280 @param[in] AddressBuffer Pointer to address buffer. Set to NULL to match
282 @param[out] EntryLength The size of an entry in the entries buffer.
283 @param[out] EntryCount The number of ARP cache entries that are found by
284 the specified criteria.
285 @param[out] Entries Pointer to the buffer that will receive the ARP
287 @param[in] Refresh Set to TRUE to refresh the timeout value of the
288 matching ARP cache entry.
290 @retval EFI_SUCCESS The requested ARP cache entries were copied into
292 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
293 This is NULL. Both EntryCount and EntryLength are
294 NULL, when Refresh is FALSE.
295 @retval EFI_NOT_FOUND No matching entries were found.
296 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
302 IN EFI_ARP_PROTOCOL
*This
,
303 IN BOOLEAN BySwAddress
,
304 IN VOID
*AddressBuffer OPTIONAL
,
305 OUT UINT32
*EntryLength OPTIONAL
,
306 OUT UINT32
*EntryCount OPTIONAL
,
307 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,
312 This function removes specified ARP cache entries.
314 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
315 @param[in] BySwAddress Set to TRUE to delete matching protocol addresses.
316 Set to FALSE to delete matching hardware
318 @param[in] AddressBuffer Pointer to the address buffer that is used as a
319 key to look for the cache entry. Set to NULL to
322 @retval EFI_SUCCESS The entry was removed from the ARP cache.
323 @retval EFI_INVALID_PARAMETER This is NULL.
324 @retval EFI_NOT_FOUND The specified deletion key was not found.
325 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
331 IN EFI_ARP_PROTOCOL
*This
,
332 IN BOOLEAN BySwAddress
,
333 IN VOID
*AddressBuffer OPTIONAL
337 This function delete all dynamic entries from the ARP cache that match the specified
338 software protocol type.
340 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
342 @retval EFI_SUCCESS The cache has been flushed.
343 @retval EFI_INVALID_PARAMETER This is NULL.
344 @retval EFI_NOT_FOUND There are no matching dynamic cache entries.
345 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
351 IN EFI_ARP_PROTOCOL
*This
355 This function tries to resolve the TargetSwAddress and optionally returns a
356 TargetHwAddress if it already exists in the ARP cache.
358 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
359 @param[in] TargetSwAddress Pointer to the protocol address to resolve.
360 @param[in] ResolvedEvent Pointer to the event that will be signaled when
361 the address is resolved or some error occurs.
362 @param[out] TargetHwAddress Pointer to the buffer for the resolved hardware
363 address in network byte order.
365 @retval EFI_SUCCESS The data is copied from the ARP cache into the
366 TargetHwAddress buffer.
367 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
368 This is NULL. TargetHwAddress is NULL.
369 @retval EFI_ACCESS_DENIED The requested address is not present in the normal
370 ARP cache but is present in the deny address list.
371 Outgoing traffic to that address is forbidden.
372 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
373 @retval EFI_NOT_READY The request has been started and is not finished.
379 IN EFI_ARP_PROTOCOL
*This
,
380 IN VOID
*TargetSwAddress OPTIONAL
,
381 IN EFI_EVENT ResolvedEvent OPTIONAL
,
382 OUT VOID
*TargetHwAddress
386 This function aborts the previous ARP request (identified by This, TargetSwAddress
387 and ResolvedEvent) that is issued by EFI_ARP_PROTOCOL.Request().
389 If the request is in the internal ARP request queue, the request is aborted
390 immediately and its ResolvedEvent is signaled. Only an asynchronous address
391 request needs to be canceled. If TargeSwAddress and ResolveEvent are both
392 NULL, all the pending asynchronous requests that have been issued by This
393 instance will be cancelled and their corresponding events will be signaled.
395 @param[in] This Pointer to the EFI_ARP_PROTOCOL instance.
396 @param[in] TargetSwAddress Pointer to the protocol address in previous
398 @param[in] ResolvedEvent Pointer to the event that is used as the
399 notification event in previous request session.
401 @retval EFI_SUCCESS The pending request session(s) is/are aborted and
402 corresponding event(s) is/are signaled.
403 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
404 This is NULL. TargetSwAddress is not NULL and
405 ResolvedEvent is NULL. TargetSwAddress is NULL and
406 ResolvedEvent is not NULL.
407 @retval EFI_NOT_STARTED The ARP driver instance has not been configured.
408 @retval EFI_NOT_FOUND The request is not issued by
409 EFI_ARP_PROTOCOL.Request().
415 IN EFI_ARP_PROTOCOL
*This
,
416 IN VOID
*TargetSwAddress OPTIONAL
,
417 IN EFI_EVENT ResolvedEvent OPTIONAL
421 Configure the instance using the ConfigData. ConfigData is already validated.
423 @param[in] Instance Pointer to the instance context data to be
425 @param[in] ConfigData Pointer to the configuration data used to
426 configure the instance.
428 @retval EFI_SUCCESS The instance is configured with the ConfigData.
429 @retval EFI_ACCESS_DENIED The instance is already configured and the
430 ConfigData tries to reset some unchangeable
432 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
433 when the SwAddressType is IPv4.
434 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
439 ArpConfigureInstance (
440 IN ARP_INSTANCE_DATA
*Instance
,
441 IN EFI_ARP_CONFIG_DATA
*ConfigData OPTIONAL
445 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
446 in the DeniedCacheTable.
448 @param[in] ArpService Pointer to the arp service context data.
449 @param[in] ProtocolAddress Pointer to the protocol address.
450 @param[in] HardwareAddress Pointer to the hardware address.
452 @return Pointer to the matched cache entry, if NULL no match is found.
456 ArpFindDeniedCacheEntry (
457 IN ARP_SERVICE_DATA
*ArpService
,
458 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
459 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
463 Find the CacheEntry which matches the requirements in the specified CacheTable.
465 @param[in] CacheTable Pointer to the arp cache table.
466 @param[in] StartEntry Pointer to the start entry this search begins with
468 @param[in] FindOpType The search type.
469 @param[in] ProtocolAddress Pointer to the protocol address to match.
470 @param[in] HardwareAddress Pointer to the hardware address to match.
472 @return Pointer to the matched arp cache entry, if NULL, no match is found.
476 ArpFindNextCacheEntryInTable (
477 IN LIST_ENTRY
*CacheTable
,
478 IN LIST_ENTRY
*StartEntry
,
479 IN FIND_OPTYPE FindOpType
,
480 IN NET_ARP_ADDRESS
*ProtocolAddress OPTIONAL
,
481 IN NET_ARP_ADDRESS
*HardwareAddress OPTIONAL
485 Allocate a cache entry and initialize it.
487 @param[in] Instance Pointer to the instance context data.
489 @return Pointer to the new created cache entry.
494 IN ARP_INSTANCE_DATA
*Instance
498 Fill the addresses in the CacheEntry using the information passed in by
501 @param[in] CacheEntry Pointer to the cache entry.
502 @param[in] HwAddr Pointer to the software address.
503 @param[in] SwAddr Pointer to the hardware address.
509 ArpFillAddressInCacheEntry (
510 IN ARP_CACHE_ENTRY
*CacheEntry
,
511 IN NET_ARP_ADDRESS
*HwAddr OPTIONAL
,
512 IN NET_ARP_ADDRESS
*SwAddr OPTIONAL
516 Turn the CacheEntry into the resolved status.
518 @param[in] CacheEntry Pointer to the resolved cache entry.
519 @param[in] Instance Pointer to the instance context data.
520 @param[in] UserEvent Pointer to the UserEvent to notify.
522 @return The count of notifications sent to the instance.
527 IN ARP_CACHE_ENTRY
*CacheEntry
,
528 IN ARP_INSTANCE_DATA
*Instance OPTIONAL
,
529 IN EFI_EVENT UserEvent OPTIONAL
533 Delete cache entries in all the cache tables.
535 @param[in] Instance Pointer to the instance context data.
536 @param[in] BySwAddress Delete the cache entry by software address or by
538 @param[in] AddressBuffer Pointer to the buffer containing the address to
539 match for the deletion.
540 @param[in] Force This deletion is forced or not.
542 @return The count of the deleted cache entries.
546 ArpDeleteCacheEntry (
547 IN ARP_INSTANCE_DATA
*Instance
,
548 IN BOOLEAN BySwAddress
,
549 IN UINT8
*AddressBuffer OPTIONAL
,
554 Send out an arp frame using the CachEntry and the ArpOpCode.
556 @param[in] Instance Pointer to the instance context data.
557 @param[in] CacheEntry Pointer to the configuration data used to
558 configure the instance.
559 @param[in] ArpOpCode The opcode used to send out this Arp frame, either
567 IN ARP_INSTANCE_DATA
*Instance
,
568 IN ARP_CACHE_ENTRY
*CacheEntry
,
573 Initialize the instance context data.
575 @param[in] ArpService Pointer to the arp service context data this
577 @param[in] Instance Pointer to the instance context data.
584 IN ARP_SERVICE_DATA
*ArpService
,
585 IN ARP_INSTANCE_DATA
*Instance
589 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
591 @param[in] Context Pointer to the context data registerd to the
604 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
606 @param[in] Event The Event this notify function registered to.
607 @param[in] Context Pointer to the context data registerd to the
621 Process the already sent arp packets.
623 @param[in] Context Pointer to the context data registerd to the
636 Queue ArpOnFrameRcvdDpc as a DPC at TPL_CALLBACK.
638 @param[in] Event The Event this notify function registered to.
639 @param[in] Context Pointer to the context data registerd to the
653 Process the arp cache olding and drive the retrying arp requests.
655 @param[in] Event The Event this notify function registered to.
656 @param[in] Context Pointer to the context data registerd to the
670 Cancel the arp request.
672 @param[in] Instance Pointer to the instance context data.
673 @param[in] TargetSwAddress Pointer to the buffer containing the target
674 software address to match the arp request.
675 @param[in] UserEvent The user event used to notify this request
678 @return The count of the cancelled requests.
683 IN ARP_INSTANCE_DATA
*Instance
,
684 IN VOID
*TargetSwAddress OPTIONAL
,
685 IN EFI_EVENT UserEvent OPTIONAL
689 Find the cache entry in the cache table.
691 @param[in] Instance Pointer to the instance context data.
692 @param[in] BySwAddress Set to TRUE to look for matching software protocol
693 addresses. Set to FALSE to look for matching
694 hardware protocol addresses.
695 @param[in] AddressBuffer Pointer to address buffer. Set to NULL to match
697 @param[out] EntryLength The size of an entry in the entries buffer.
698 @param[out] EntryCount The number of ARP cache entries that are found by
699 the specified criteria.
700 @param[out] Entries Pointer to the buffer that will receive the ARP
702 @param[in] Refresh Set to TRUE to refresh the timeout value of the
703 matching ARP cache entry.
705 @retval EFI_SUCCESS The requested ARP cache entries are copied into
707 @retval EFI_NOT_FOUND No matching entries found.
708 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
713 IN ARP_INSTANCE_DATA
*Instance
,
714 IN BOOLEAN BySwAddress
,
715 IN VOID
*AddressBuffer OPTIONAL
,
716 OUT UINT32
*EntryLength OPTIONAL
,
717 OUT UINT32
*EntryCount OPTIONAL
,
718 OUT EFI_ARP_FIND_DATA
**Entries OPTIONAL
,