]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/ArpDxe/ArpImpl.c
93b4c1020037373b77424c579c11cf468a826350
[mirror_edk2.git] / MdeModulePkg / Universal / Network / ArpDxe / ArpImpl.c
1 /** @file
2
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
8
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.
11
12 Module Name:
13
14 ArpImpl.c
15
16 Abstract:
17
18
19 **/
20
21
22 #include "ArpImpl.h"
23 #include "ArpDebug.h"
24
25 EFI_ARP_PROTOCOL mEfiArpProtocolTemplate = {
26 ArpConfigure,
27 ArpAdd,
28 ArpFind,
29 ArpDelete,
30 ArpFlush,
31 ArpRequest,
32 ArpCancel
33 };
34
35
36 /**
37 Initialize the instance context data.
38
39 @param ArpService Pointer to the arp service context data this
40 instance belongs to.
41 @param Instance Pointer to the instance context data.
42
43 @return None.
44
45 **/
46 VOID
47 ArpInitInstance (
48 IN ARP_SERVICE_DATA *ArpService,
49 IN ARP_INSTANCE_DATA *Instance
50 )
51 {
52 NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE);
53
54 Instance->Signature = ARP_INSTANCE_DATA_SIGNATURE;
55 Instance->ArpService = ArpService;
56
57 CopyMem (&Instance->ArpProto, &mEfiArpProtocolTemplate, sizeof (ARP_SERVICE_DATA));
58
59 Instance->Configured = FALSE;
60 Instance->Destroyed = FALSE;
61
62 NetListInit (&Instance->List);
63 }
64
65
66 /**
67 Process the Arp packets received from Mnp, the procedure conforms to RFC826.
68
69 @param Event The Event this notify function registered to.
70 @param Context Pointer to the context data registerd to the
71 Event.
72
73 @return None.
74
75 **/
76 VOID
77 EFIAPI
78 ArpOnFrameRcvd (
79 IN EFI_EVENT Event,
80 IN VOID *Context
81 )
82 {
83 EFI_STATUS Status;
84 ARP_SERVICE_DATA *ArpService;
85 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken;
86 EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData;
87 ARP_HEAD *Head;
88 ARP_ADDRESS ArpAddress;
89 ARP_CACHE_ENTRY *CacheEntry;
90 NET_LIST_ENTRY *Entry;
91 ARP_INSTANCE_DATA *Instance;
92 EFI_ARP_CONFIG_DATA *ConfigData;
93 NET_ARP_ADDRESS SenderAddress[2];
94 BOOLEAN ProtoMatched;
95 BOOLEAN IsTarget;
96 BOOLEAN MergeFlag;
97
98 ArpService = (ARP_SERVICE_DATA *)Context;
99 NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE);
100
101 RxToken = &ArpService->RxToken;
102
103 if (RxToken->Status == EFI_ABORTED) {
104 //
105 // The Token is aborted, possibly by arp itself, just return and the receiving
106 // process is stopped.
107 //
108 return;
109 }
110
111 if (EFI_ERROR (RxToken->Status)) {
112 //
113 // Restart the receiving if any other error Status occurs.
114 //
115 goto RESTART_RECEIVE;
116 }
117
118 //
119 // Status is EFI_SUCCESS, process the received frame.
120 //
121 RxData = RxToken->Packet.RxData;
122 Head = (ARP_HEAD *) RxData->PacketData;
123
124 //
125 // Convert the byte order of the multi-byte fields.
126 //
127 Head->HwType = NTOHS (Head->HwType);
128 Head->ProtoType = NTOHS (Head->ProtoType);
129 Head->OpCode = NTOHS (Head->OpCode);
130
131 if ((Head->HwType != ArpService->SnpMode.IfType) ||
132 (Head->HwAddrLen != ArpService->SnpMode.HwAddressSize) ||
133 (RxData->ProtocolType != ARP_ETHER_PROTO_TYPE)) {
134 //
135 // The hardware type or the hardware address length doesn't match.
136 // There is a sanity check for the protocol type too.
137 //
138 goto RECYCLE_RXDATA;
139 }
140
141 //
142 // Set the pointers to the addresses contained in the arp packet.
143 //
144 ArpAddress.SenderHwAddr = (UINT8 *)(Head + 1);
145 ArpAddress.SenderProtoAddr = ArpAddress.SenderHwAddr + Head->HwAddrLen;
146 ArpAddress.TargetHwAddr = ArpAddress.SenderProtoAddr + Head->ProtoAddrLen;
147 ArpAddress.TargetProtoAddr = ArpAddress.TargetHwAddr + Head->HwAddrLen;
148
149 if (EFI_ERROR (NET_TRYLOCK (&ArpService->Lock))) {
150 ARP_DEBUG_ERROR (("ArpOnFrameRcvd: Faild to acquire the CacheTableLock.\n"));
151 goto RECYCLE_RXDATA;
152 }
153
154 SenderAddress[Hardware].Type = Head->HwType;
155 SenderAddress[Hardware].Length = Head->HwAddrLen;
156 SenderAddress[Hardware].AddressPtr = ArpAddress.SenderHwAddr;
157
158 SenderAddress[Protocol].Type = Head->ProtoType;
159 SenderAddress[Protocol].Length = Head->ProtoAddrLen;
160 SenderAddress[Protocol].AddressPtr = ArpAddress.SenderProtoAddr;
161
162 //
163 // First, check the denied cache table.
164 //
165 CacheEntry = ArpFindDeniedCacheEntry (
166 ArpService,
167 &SenderAddress[Protocol],
168 &SenderAddress[Hardware]
169 );
170 if (CacheEntry != NULL) {
171 //
172 // This address (either hardware or protocol address, or both) is configured to
173 // be a deny entry, silently skip the normal process.
174 //
175 goto UNLOCK_EXIT;
176 }
177
178 ProtoMatched = FALSE;
179 IsTarget = FALSE;
180 Instance = NULL;
181 NET_LIST_FOR_EACH (Entry, &ArpService->ChildrenList) {
182 //
183 // Iterate all the children.
184 //
185 Instance = NET_LIST_USER_STRUCT (Entry, ARP_INSTANCE_DATA, List);
186 NET_CHECK_SIGNATURE (Instance, ARP_INSTANCE_DATA_SIGNATURE);
187 ConfigData = &Instance->ConfigData;
188
189 if ((Instance->Configured) &&
190 (Head->ProtoType == ConfigData->SwAddressType) &&
191 (Head->ProtoAddrLen == ConfigData->SwAddressLength)) {
192 //
193 // The protocol type is matched for the received arp packet.
194 //
195 ProtoMatched = TRUE;
196 if (0 == NetCompareMem (
197 (VOID *)ArpAddress.TargetProtoAddr,
198 ConfigData->StationAddress,
199 ConfigData->SwAddressLength
200 )) {
201 //
202 // The arp driver has the target address required by the received arp packet.
203 //
204 IsTarget = TRUE;
205 break;
206 }
207 }
208 }
209
210 if (!ProtoMatched) {
211 //
212 // Protocol type unmatchable, skip.
213 //
214 goto UNLOCK_EXIT;
215 }
216
217 //
218 // Check whether the sender's address information is already in the cache.
219 //
220 MergeFlag = FALSE;
221 CacheEntry = ArpFindNextCacheEntryInTable (
222 &ArpService->ResolvedCacheTable,
223 NULL,
224 ByProtoAddress,
225 &SenderAddress[Protocol],
226 NULL
227 );
228 if (CacheEntry != NULL) {
229 //
230 // Update the entry with the new information.
231 //
232 ArpFillAddressInCacheEntry (CacheEntry, &SenderAddress[Hardware], NULL);
233 CacheEntry->DecayTime = CacheEntry->DefaultDecayTime;
234 MergeFlag = TRUE;
235 }
236
237 if (!IsTarget) {
238 //
239 // This arp packet isn't targeted to us, skip now.
240 //
241 goto UNLOCK_EXIT;
242 }
243
244 if (!MergeFlag) {
245 //
246 // Add the triplet <protocol type, sender protocol address, sender hardware address>
247 // to the translation table.
248 //
249 CacheEntry = ArpFindNextCacheEntryInTable (
250 &ArpService->PendingRequestTable,
251 NULL,
252 ByProtoAddress,
253 &SenderAddress[Protocol],
254 NULL
255 );
256 if (CacheEntry == NULL) {
257 //
258 // Allocate a new CacheEntry.
259 //
260 CacheEntry = ArpAllocCacheEntry (NULL);
261 if (CacheEntry == NULL) {
262 goto UNLOCK_EXIT;
263 }
264 }
265
266 NetListRemoveEntry (&CacheEntry->List);
267
268 //
269 // Fill the addresses into the CacheEntry.
270 //
271 ArpFillAddressInCacheEntry (
272 CacheEntry,
273 &SenderAddress[Hardware],
274 &SenderAddress[Protocol]
275 );
276
277 //
278 // Inform the user.
279 //
280 ArpAddressResolved (CacheEntry, NULL, NULL);
281
282 //
283 // Add this entry into the ResolvedCacheTable
284 //
285 NetListInsertHead (&ArpService->ResolvedCacheTable, &CacheEntry->List);
286 }
287
288 if (Head->OpCode == ARP_OPCODE_REQUEST) {
289 //
290 // Send back the ARP Reply. If we reach here, Instance is not NULL and CacheEntry
291 // is not NULL.
292 //
293 ArpSendFrame (Instance, CacheEntry, ARP_OPCODE_REPLY);
294 }
295
296 UNLOCK_EXIT:
297
298 NET_UNLOCK (&ArpService->Lock);
299
300 RECYCLE_RXDATA:
301
302 //
303 // Signal Mnp to recycle the RxData.
304 //
305 gBS->SignalEvent (RxData->RecycleEvent);
306
307 RESTART_RECEIVE:
308
309 //
310 // Continue to receive packets from Mnp.
311 //
312 Status = ArpService->Mnp->Receive (ArpService->Mnp, RxToken);
313
314 DEBUG_CODE (
315 if (EFI_ERROR (Status)) {
316 ARP_DEBUG_ERROR (("ArpOnFrameRcvd: ArpService->Mnp->Receive "
317 "failed, %r\n.", Status));
318 }
319 );
320 }
321
322
323 /**
324 Process the already sent arp packets.
325
326 @param Event The Event this notify function registered to.
327 @param Context Pointer to the context data registerd to the
328 Event.
329
330 @return None.
331
332 **/
333 VOID
334 EFIAPI
335 ArpOnFrameSent (
336 IN EFI_EVENT Event,
337 IN VOID *Context
338 )
339 {
340 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TxToken;
341 EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
342
343 ASSERT (Context != NULL);
344
345 TxToken = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *)Context;
346 TxData = TxToken->Packet.TxData;
347
348 DEBUG_CODE (
349 if (EFI_ERROR (TxToken->Status)) {
350 ARP_DEBUG_ERROR (("ArpOnFrameSent: TxToken->Status, %r.\n", TxToken->Status));
351 }
352 );
353
354 //
355 // Free the allocated memory and close the event.
356 //
357 NetFreePool (TxData->FragmentTable[0].FragmentBuffer);
358 NetFreePool (TxData);
359 gBS->CloseEvent (TxToken->Event);
360 NetFreePool (TxToken);
361 }
362
363
364 /**
365 Process the arp cache olding and drive the retrying arp requests.
366
367 @param Event The Event this notify function registered to.
368 @param Context Pointer to the context data registerd to the
369 Event.
370
371 @return None.
372
373 **/
374 VOID
375 EFIAPI
376 ArpTimerHandler (
377 IN EFI_EVENT Event,
378 IN VOID *Context
379 )
380 {
381 ARP_SERVICE_DATA *ArpService;
382 NET_LIST_ENTRY *Entry;
383 NET_LIST_ENTRY *NextEntry;
384 NET_LIST_ENTRY *ContextEntry;
385 ARP_CACHE_ENTRY *CacheEntry;
386 USER_REQUEST_CONTEXT *RequestContext;
387
388 ASSERT (Context != NULL);
389 ArpService = (ARP_SERVICE_DATA *)Context;
390
391 if (EFI_ERROR (NET_TRYLOCK (&ArpService->Lock))) {
392 return;
393 }
394
395 //
396 // Iterate all the pending requests to see whether a retry is needed to send out
397 // or the request finally fails because the retry time reaches the limitation.
398 //
399 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->PendingRequestTable) {
400 CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List);
401
402 if (CacheEntry->NextRetryTime <= ARP_PERIODIC_TIMER_INTERVAL) {
403 //
404 // Timeout, if we can retry more, send out the request again, otherwise abort
405 // this request.
406 //
407 if (CacheEntry->RetryCount == 0) {
408 //
409 // Abort this request.
410 //
411 ArpAddressResolved (CacheEntry, NULL, NULL);
412 ASSERT (NetListIsEmpty (&CacheEntry->UserRequestList));
413
414 NetListRemoveEntry (&CacheEntry->List);
415 NetFreePool (CacheEntry);
416 } else {
417 //
418 // resend the ARP request.
419 //
420 ASSERT (!NetListIsEmpty(&CacheEntry->UserRequestList));
421
422 ContextEntry = CacheEntry->UserRequestList.ForwardLink;
423 RequestContext = NET_LIST_USER_STRUCT (ContextEntry, USER_REQUEST_CONTEXT, List);
424
425 ArpSendFrame (RequestContext->Instance, CacheEntry, ARP_OPCODE_REQUEST);
426
427 CacheEntry->RetryCount--;
428 CacheEntry->NextRetryTime = RequestContext->Instance->ConfigData.RetryTimeOut;
429 }
430 } else {
431 //
432 // Update the NextRetryTime.
433 //
434 CacheEntry->NextRetryTime -= ARP_PERIODIC_TIMER_INTERVAL;
435 }
436 }
437
438 //
439 // Check the timeouts for the DeniedCacheTable.
440 //
441 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->DeniedCacheTable) {
442 CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List);
443 ASSERT (NetListIsEmpty (&CacheEntry->UserRequestList));
444
445 if (CacheEntry->DefaultDecayTime == 0) {
446 //
447 // It's a static entry, skip it.
448 //
449 continue;
450 }
451
452 if (CacheEntry->DecayTime <= ARP_PERIODIC_TIMER_INTERVAL) {
453 //
454 // Time out, remove it.
455 //
456 NetListRemoveEntry (&CacheEntry->List);
457 NetFreePool (CacheEntry);
458 } else {
459 //
460 // Update the DecayTime.
461 //
462 CacheEntry->DecayTime -= ARP_PERIODIC_TIMER_INTERVAL;
463 }
464 }
465
466 //
467 // Check the timeouts for the ResolvedCacheTable.
468 //
469 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->ResolvedCacheTable) {
470 CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List);
471 ASSERT (NetListIsEmpty (&CacheEntry->UserRequestList));
472
473 if (CacheEntry->DefaultDecayTime == 0) {
474 //
475 // It's a static entry, skip it.
476 //
477 continue;
478 }
479
480 if (CacheEntry->DecayTime <= ARP_PERIODIC_TIMER_INTERVAL) {
481 //
482 // Time out, remove it.
483 //
484 NetListRemoveEntry (&CacheEntry->List);
485 NetFreePool (CacheEntry);
486 } else {
487 //
488 // Update the DecayTime.
489 //
490 CacheEntry->DecayTime -= ARP_PERIODIC_TIMER_INTERVAL;
491 }
492 }
493
494 NET_UNLOCK (&ArpService->Lock);
495 }
496
497
498 /**
499 Match the two NET_ARP_ADDRESSes.
500
501 @param AddressOne Pointer to the first address to match.
502 @param AddressTwo Pointer to the second address to match.
503
504 @return The two addresses match or not.
505
506 **/
507 STATIC
508 BOOLEAN
509 ArpMatchAddress (
510 IN NET_ARP_ADDRESS *AddressOne,
511 IN NET_ARP_ADDRESS *AddressTwo
512 )
513 {
514 if ((AddressOne->Type != AddressTwo->Type) ||
515 (AddressOne->Length != AddressTwo->Length)) {
516 //
517 // Either Type or Length doesn't match.
518 //
519 return FALSE;
520 }
521
522 if ((AddressOne->AddressPtr != NULL) &&
523 (NetCompareMem (
524 AddressOne->AddressPtr,
525 AddressTwo->AddressPtr,
526 AddressOne->Length
527 ) != 0)) {
528 //
529 // The address is not the same.
530 //
531 return FALSE;
532 }
533
534 return TRUE;
535 }
536
537
538 /**
539 Find the CacheEntry which matches the requirements in the specified CacheTable.
540
541 @param CacheTable Pointer to the arp cache table.
542 @param StartEntry Pointer to the start entry this search begins with
543 in the cache table.
544 @param FindOpType The search type.
545 @param ProtocolAddress Pointer to the protocol address to match.
546 @param HardwareAddress Pointer to the hardware address to match.
547
548 @return Pointer to the matched arp cache entry, if NULL, no match is found.
549
550 **/
551 ARP_CACHE_ENTRY *
552 ArpFindNextCacheEntryInTable (
553 IN NET_LIST_ENTRY *CacheTable,
554 IN NET_LIST_ENTRY *StartEntry,
555 IN FIND_OPTYPE FindOpType,
556 IN NET_ARP_ADDRESS *ProtocolAddress OPTIONAL,
557 IN NET_ARP_ADDRESS *HardwareAddress OPTIONAL
558 )
559 {
560 NET_LIST_ENTRY *Entry;
561 ARP_CACHE_ENTRY *CacheEntry;
562
563 if (StartEntry == NULL) {
564 //
565 // Start from the beginning of the table if no StartEntry is specified.
566 //
567 StartEntry = CacheTable;
568 }
569
570 for (Entry = StartEntry->ForwardLink; Entry != CacheTable; Entry = Entry->ForwardLink) {
571 CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List);
572
573 if (FindOpType & MATCH_SW_ADDRESS) {
574 //
575 // Find by the software address.
576 //
577 if (!ArpMatchAddress (ProtocolAddress, &CacheEntry->Addresses[Protocol])) {
578 //
579 // The ProtocolAddress doesn't match, continue to the next cache entry.
580 //
581 continue;
582 }
583 }
584
585 if (FindOpType & MATCH_HW_ADDRESS) {
586 //
587 // Find by the hardware address.
588 //
589 if (!ArpMatchAddress (HardwareAddress, &CacheEntry->Addresses[Hardware])) {
590 //
591 // The HardwareAddress doesn't match, continue to the next cache entry.
592 //
593 continue;
594 }
595 }
596
597 //
598 // The CacheEntry meets the requirements now, return this entry.
599 //
600 return CacheEntry;
601 }
602
603 //
604 // No matching.
605 //
606 return NULL;
607 }
608
609
610 /**
611 Find the CacheEntry, using ProtocolAddress or HardwareAddress or both, as the keyword,
612 in the DeniedCacheTable.
613
614 @param ArpService Pointer to the arp service context data.
615 @param ProtocolAddress Pointer to the protocol address.
616 @param HardwareAddress Pointer to the hardware address.
617
618 @return Pointer to the matched cache entry, if NULL no match is found.
619
620 **/
621 ARP_CACHE_ENTRY *
622 ArpFindDeniedCacheEntry (
623 IN ARP_SERVICE_DATA *ArpService,
624 IN NET_ARP_ADDRESS *ProtocolAddress OPTIONAL,
625 IN NET_ARP_ADDRESS *HardwareAddress OPTIONAL
626 )
627 {
628 ARP_CACHE_ENTRY *CacheEntry;
629
630 ASSERT ((ProtocolAddress != NULL) || (HardwareAddress != NULL));
631 NET_CHECK_SIGNATURE (ArpService, ARP_SERVICE_DATA_SIGNATURE);
632
633 CacheEntry = NULL;
634
635 if ((ProtocolAddress != NULL) && (ProtocolAddress->AddressPtr != NULL)) {
636 //
637 // Find the cache entry in the DeniedCacheTable by the protocol address.
638 //
639 CacheEntry = ArpFindNextCacheEntryInTable (
640 &ArpService->DeniedCacheTable,
641 NULL,
642 ByProtoAddress,
643 ProtocolAddress,
644 NULL
645 );
646 if (CacheEntry != NULL) {
647 //
648 // There is a match.
649 //
650 return CacheEntry;
651 }
652 }
653
654 if ((HardwareAddress != NULL) && (HardwareAddress->AddressPtr != NULL)) {
655 //
656 // Find the cache entry in the DeniedCacheTable by the hardware address.
657 //
658 CacheEntry = ArpFindNextCacheEntryInTable (
659 &ArpService->DeniedCacheTable,
660 NULL,
661 ByHwAddress,
662 NULL,
663 HardwareAddress
664 );
665 }
666
667 return CacheEntry;
668 }
669
670
671 /**
672 Allocate a cache entry and initialize it.
673
674 @param Instance Pointer to the instance context data.
675
676 @return Pointer to the new created cache entry.
677
678 **/
679 ARP_CACHE_ENTRY *
680 ArpAllocCacheEntry (
681 IN ARP_INSTANCE_DATA *Instance
682 )
683 {
684 ARP_CACHE_ENTRY *CacheEntry;
685 NET_ARP_ADDRESS *Address;
686 UINT16 Index;
687
688 //
689 // Allocate memory for the cache entry.
690 //
691 CacheEntry = NetAllocatePool (sizeof (ARP_CACHE_ENTRY));
692 if (CacheEntry == NULL) {
693 return NULL;
694 }
695
696 //
697 // Init the lists.
698 //
699 NetListInit (&CacheEntry->List);
700 NetListInit (&CacheEntry->UserRequestList);
701
702 for (Index = 0; Index < 2; Index++) {
703 //
704 // Init the address pointers to point to the concrete buffer.
705 //
706 Address = &CacheEntry->Addresses[Index];
707 Address->AddressPtr = Address->Buffer.ProtoAddress;
708 }
709
710 //
711 // Zero the hardware address first.
712 //
713 NetZeroMem (CacheEntry->Addresses[Hardware].AddressPtr, ARP_MAX_HARDWARE_ADDRESS_LEN);
714
715 if (Instance != NULL) {
716 //
717 // Inherit the parameters from the instance configuration.
718 //
719 CacheEntry->RetryCount = Instance->ConfigData.RetryCount;
720 CacheEntry->NextRetryTime = Instance->ConfigData.RetryTimeOut;
721 CacheEntry->DefaultDecayTime = Instance->ConfigData.EntryTimeOut;
722 CacheEntry->DecayTime = Instance->ConfigData.EntryTimeOut;
723 } else {
724 //
725 // Use the default parameters if this cache entry isn't allocate in a
726 // instance's scope.
727 //
728 CacheEntry->RetryCount = ARP_DEFAULT_RETRY_COUNT;
729 CacheEntry->NextRetryTime = ARP_DEFAULT_RETRY_INTERVAL;
730 CacheEntry->DefaultDecayTime = ARP_DEFAULT_TIMEOUT_VALUE;
731 CacheEntry->DecayTime = ARP_DEFAULT_TIMEOUT_VALUE;
732 }
733
734 return CacheEntry;
735 }
736
737
738 /**
739 Turn the CacheEntry into the resolved status.
740
741 @param CacheEntry Pointer to the resolved cache entry.
742 @param Instance Pointer to the instance context data.
743 @param UserEvent Pointer to the UserEvent to notify.
744
745 @return The count of notifications sent to the instance.
746
747 **/
748 UINTN
749 ArpAddressResolved (
750 IN ARP_CACHE_ENTRY *CacheEntry,
751 IN ARP_INSTANCE_DATA *Instance OPTIONAL,
752 IN EFI_EVENT UserEvent OPTIONAL
753 )
754 {
755 NET_LIST_ENTRY *Entry;
756 NET_LIST_ENTRY *NextEntry;
757 USER_REQUEST_CONTEXT *Context;
758 UINTN Count;
759
760 Count = 0;
761
762 //
763 // Iterate all the linked user requests to notify them.
764 //
765 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &CacheEntry->UserRequestList) {
766 Context = NET_LIST_USER_STRUCT (Entry, USER_REQUEST_CONTEXT, List);
767
768 if (((Instance == NULL) || (Context->Instance == Instance)) &&
769 ((UserEvent == NULL) || (Context->UserRequestEvent == UserEvent))) {
770 //
771 // Copy the address to the user-provided buffer and notify the user.
772 //
773 NetCopyMem (
774 Context->UserHwAddrBuffer,
775 CacheEntry->Addresses[Hardware].AddressPtr,
776 CacheEntry->Addresses[Hardware].Length
777 );
778 gBS->SignalEvent (Context->UserRequestEvent);
779
780 //
781 // Remove this user request and free the context data.
782 //
783 NetListRemoveEntry (&Context->List);
784 NetFreePool (Context);
785
786 Count++;
787 }
788 }
789
790 return Count;
791 }
792
793
794 /**
795 Fill the addresses in the CacheEntry using the information passed in by
796 HwAddr and SwAddr.
797
798 @param CacheEntry Pointer to the cache entry.
799 @param HwAddr Pointer to the software address.
800 @param SwAddr Pointer to the hardware address.
801
802 @return None.
803
804 **/
805 VOID
806 ArpFillAddressInCacheEntry (
807 IN ARP_CACHE_ENTRY *CacheEntry,
808 IN NET_ARP_ADDRESS *HwAddr OPTIONAL,
809 IN NET_ARP_ADDRESS *SwAddr OPTIONAL
810 )
811 {
812 NET_ARP_ADDRESS *Address[2];
813 NET_ARP_ADDRESS *CacheAddress;
814 UINT32 Index;
815
816 Address[Hardware] = HwAddr;
817 Address[Protocol] = SwAddr;
818
819 for (Index = 0; Index < 2; Index++) {
820 if (Address[Index] != NULL) {
821 //
822 // Fill the address if the passed in pointer is not NULL.
823 //
824 CacheAddress = &CacheEntry->Addresses[Index];
825
826 CacheAddress->Type = Address[Index]->Type;
827 CacheAddress->Length = Address[Index]->Length;
828
829 if (Address[Index]->AddressPtr != NULL) {
830 //
831 // Copy it if the AddressPtr points to some buffer.
832 //
833 NetCopyMem (
834 CacheAddress->AddressPtr,
835 Address[Index]->AddressPtr,
836 CacheAddress->Length
837 );
838 } else {
839 //
840 // Zero the corresponding address buffer in the CacheEntry.
841 //
842 NetZeroMem (CacheAddress->AddressPtr, CacheAddress->Length);
843 }
844 }
845 }
846 }
847
848
849 /**
850 Configure the instance using the ConfigData. ConfigData is already validated.
851
852 @param Instance Pointer to the instance context data to be
853 configured.
854 @param ConfigData Pointer to the configuration data used to
855 configure the instance.
856
857 @retval EFI_SUCCESS The instance is configured with the ConfigData.
858 @retval EFI_ACCESS_DENIED The instance is already configured and the
859 ConfigData tries to reset some unchangeable
860 fields.
861 @retval EFI_INVALID_PARAMETER The ConfigData provides a non-unicast IPv4 address
862 when the SwAddressType is IPv4.
863 @retval EFI_OUT_OF_RESOURCES The instance fails to configure due to memory
864 limitation.
865
866 **/
867 EFI_STATUS
868 ArpConfigureInstance (
869 IN ARP_INSTANCE_DATA *Instance,
870 IN EFI_ARP_CONFIG_DATA *ConfigData OPTIONAL
871 )
872 {
873 EFI_ARP_CONFIG_DATA *OldConfigData;
874 IP4_ADDR Ip;
875
876 OldConfigData = &Instance->ConfigData;
877
878 if (ConfigData != NULL) {
879
880 if (Instance->Configured) {
881 //
882 // The instance is configured, check the unchangeable fields.
883 //
884 if ((OldConfigData->SwAddressType != ConfigData->SwAddressType) ||
885 (OldConfigData->SwAddressLength != ConfigData->SwAddressLength) ||
886 (NetCompareMem (
887 OldConfigData->StationAddress,
888 ConfigData->StationAddress,
889 OldConfigData->SwAddressLength
890 ) != 0)) {
891 //
892 // Deny the unallowed changes.
893 //
894 return EFI_ACCESS_DENIED;
895 }
896 } else {
897 //
898 // The instance is not configured.
899 //
900
901 if (ConfigData->SwAddressType == IPv4_ETHER_PROTO_TYPE) {
902 NetCopyMem (&Ip, ConfigData->StationAddress, sizeof (IP4_ADDR));
903
904 if (!Ip4IsUnicast (NTOHL (Ip), 0)) {
905 //
906 // The station address is not a valid IPv4 unicast address.
907 //
908 return EFI_INVALID_PARAMETER;
909 }
910 }
911
912 //
913 // Save the configuration.
914 //
915 CopyMem (OldConfigData, ConfigData, sizeof (EFI_ARP_CONFIG_DATA));
916
917 OldConfigData->StationAddress = NetAllocatePool (OldConfigData->SwAddressLength);
918 if (OldConfigData->StationAddress == NULL) {
919 ARP_DEBUG_ERROR (("ArpConfigInstance: NetAllocatePool for the StationAddress "
920 "failed.\n"));
921 return EFI_OUT_OF_RESOURCES;
922 }
923
924 //
925 // Save the StationAddress.
926 //
927 NetCopyMem (
928 OldConfigData->StationAddress,
929 ConfigData->StationAddress,
930 OldConfigData->SwAddressLength
931 );
932
933 //
934 // Set the state to configured.
935 //
936 Instance->Configured = TRUE;
937 }
938
939 //
940 // Use the implementation specific values if the following field is zero.
941 //
942 OldConfigData->EntryTimeOut = (ConfigData->EntryTimeOut == 0) ?
943 ARP_DEFAULT_TIMEOUT_VALUE : ConfigData->EntryTimeOut;
944
945 OldConfigData->RetryCount = (ConfigData->RetryCount == 0) ?
946 ARP_DEFAULT_RETRY_COUNT : ConfigData->RetryCount;
947
948 OldConfigData->RetryTimeOut = (ConfigData->RetryTimeOut == 0) ?
949 ARP_DEFAULT_RETRY_INTERVAL : ConfigData->RetryTimeOut;
950 } else {
951 //
952 // Reset the configuration.
953 //
954
955 if (Instance->Configured) {
956 //
957 // Cancel the arp requests issued by this instance.
958 //
959 ArpCancelRequest (Instance, NULL, NULL);
960
961 //
962 // Free the buffer previously allocated to hold the station address.
963 //
964 NetFreePool (OldConfigData->StationAddress);
965 }
966
967 Instance->Configured = FALSE;
968 }
969
970 return EFI_SUCCESS;
971 }
972
973
974 /**
975 Send out an arp frame using the CachEntry and the ArpOpCode.
976
977 @param Instance Pointer to the instance context data.
978 @param CacheEntry Pointer to the configuration data used to
979 configure the instance.
980 @param ArpOpCode The opcode used to send out this Arp frame, either
981 request or reply.
982
983 @return None.
984
985 **/
986 VOID
987 ArpSendFrame (
988 IN ARP_INSTANCE_DATA *Instance,
989 IN ARP_CACHE_ENTRY *CacheEntry,
990 IN UINT16 ArpOpCode
991 )
992 {
993 EFI_STATUS Status;
994 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TxToken;
995 EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
996 UINT32 TotalLength;
997 UINT8 *Packet;
998 ARP_SERVICE_DATA *ArpService;
999 EFI_SIMPLE_NETWORK_MODE *SnpMode;
1000 EFI_ARP_CONFIG_DATA *ConfigData;
1001 UINT8 *TmpPtr;
1002 ARP_HEAD *ArpHead;
1003
1004 ASSERT ((Instance != NULL) && (CacheEntry != NULL));
1005
1006 //
1007 // Allocate memory for the TxToken.
1008 //
1009 TxToken = NetAllocatePool (sizeof(EFI_MANAGED_NETWORK_COMPLETION_TOKEN));
1010 if (TxToken == NULL) {
1011 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for TxToken failed.\n"));
1012 return;
1013 }
1014
1015 TxToken->Event = NULL;
1016 TxData = NULL;
1017 Packet = NULL;
1018
1019 //
1020 // Create the event for this TxToken.
1021 //
1022 Status = gBS->CreateEvent (
1023 EVT_NOTIFY_SIGNAL,
1024 NET_TPL_EVENT,
1025 ArpOnFrameSent,
1026 (VOID *)TxToken,
1027 &TxToken->Event
1028 );
1029 if (EFI_ERROR (Status)) {
1030 ARP_DEBUG_ERROR (("ArpSendFrame: CreateEvent failed for TxToken->Event.\n"));
1031 goto CLEAN_EXIT;
1032 }
1033
1034 //
1035 // Allocate memory for the TxData used in the TxToken.
1036 //
1037 TxData = NetAllocatePool (sizeof(EFI_MANAGED_NETWORK_TRANSMIT_DATA));
1038 if (TxData == NULL) {
1039 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for TxData failed.\n"));
1040 goto CLEAN_EXIT;
1041 }
1042
1043 ArpService = Instance->ArpService;
1044 SnpMode = &ArpService->SnpMode;
1045 ConfigData = &Instance->ConfigData;
1046
1047 //
1048 // Calculate the buffer length for this arp frame.
1049 //
1050 TotalLength = SnpMode->MediaHeaderSize + sizeof (ARP_HEAD) +
1051 2 * (ConfigData->SwAddressLength + SnpMode->HwAddressSize);
1052
1053 //
1054 // Allocate buffer for the arp frame.
1055 //
1056 Packet = NetAllocatePool (TotalLength);
1057 if (Packet == NULL) {
1058 ARP_DEBUG_ERROR (("ArpSendFrame: Allocate memory for Packet failed.\n"));
1059 }
1060
1061 TmpPtr = Packet;
1062
1063 //
1064 // The destination MAC address.
1065 //
1066 if (ArpOpCode == ARP_OPCODE_REQUEST) {
1067 NetCopyMem (TmpPtr, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize);
1068 } else {
1069 NetCopyMem (
1070 TmpPtr,
1071 CacheEntry->Addresses[Hardware].AddressPtr,
1072 SnpMode->HwAddressSize
1073 );
1074 }
1075 TmpPtr += SnpMode->HwAddressSize;
1076
1077 //
1078 // The source MAC address.
1079 //
1080 NetCopyMem (TmpPtr, &SnpMode->CurrentAddress, SnpMode->HwAddressSize);
1081 TmpPtr += SnpMode->HwAddressSize;
1082
1083 //
1084 // The ethernet protocol type.
1085 //
1086 *(UINT16 *)TmpPtr = HTONS (ARP_ETHER_PROTO_TYPE);
1087 TmpPtr += 2;
1088
1089 //
1090 // The ARP Head.
1091 //
1092 ArpHead = (ARP_HEAD *) TmpPtr;
1093 ArpHead->HwType = HTONS ((UINT16)SnpMode->IfType);
1094 ArpHead->ProtoType = HTONS (ConfigData->SwAddressType);
1095 ArpHead->HwAddrLen = (UINT8)SnpMode->HwAddressSize;
1096 ArpHead->ProtoAddrLen = ConfigData->SwAddressLength;
1097 ArpHead->OpCode = HTONS (ArpOpCode);
1098 TmpPtr += sizeof (ARP_HEAD);
1099
1100 //
1101 // The sender hardware address.
1102 //
1103 NetCopyMem (TmpPtr, &SnpMode->CurrentAddress, SnpMode->HwAddressSize);
1104 TmpPtr += SnpMode->HwAddressSize;
1105
1106 //
1107 // The sender protocol address.
1108 //
1109 NetCopyMem (TmpPtr, ConfigData->StationAddress, ConfigData->SwAddressLength);
1110 TmpPtr += ConfigData->SwAddressLength;
1111
1112 //
1113 // The target hardware address.
1114 //
1115 NetCopyMem (
1116 TmpPtr,
1117 CacheEntry->Addresses[Hardware].AddressPtr,
1118 SnpMode->HwAddressSize
1119 );
1120 TmpPtr += SnpMode->HwAddressSize;
1121
1122 //
1123 // The target protocol address.
1124 //
1125 NetCopyMem (
1126 TmpPtr,
1127 CacheEntry->Addresses[Protocol].AddressPtr,
1128 ConfigData->SwAddressLength
1129 );
1130
1131 //
1132 // Set all the fields of the TxData.
1133 //
1134 TxData->DestinationAddress = NULL;
1135 TxData->SourceAddress = NULL;
1136 TxData->ProtocolType = 0;
1137 TxData->DataLength = TotalLength - SnpMode->MediaHeaderSize;
1138 TxData->HeaderLength = (UINT16) SnpMode->MediaHeaderSize;
1139 TxData->FragmentCount = 1;
1140
1141 TxData->FragmentTable[0].FragmentBuffer = Packet;
1142 TxData->FragmentTable[0].FragmentLength = TotalLength;
1143
1144 //
1145 // Associate the TxData with the TxToken.
1146 //
1147 TxToken->Packet.TxData = TxData;
1148 TxToken->Status = EFI_NOT_READY;
1149
1150 //
1151 // Send out this arp packet by Mnp.
1152 //
1153 Status = ArpService->Mnp->Transmit (ArpService->Mnp, TxToken);
1154 if (EFI_ERROR (Status)) {
1155 ARP_DEBUG_ERROR (("Mnp->Transmit failed, %r.\n", Status));
1156 goto CLEAN_EXIT;
1157 }
1158
1159 return;
1160
1161 CLEAN_EXIT:
1162
1163 if (Packet != NULL) {
1164 NetFreePool (Packet);
1165 }
1166
1167 if (TxData != NULL) {
1168 NetFreePool (TxData);
1169 }
1170
1171 if (TxToken->Event != NULL) {
1172 gBS->CloseEvent (TxToken->Event);
1173 }
1174
1175 NetFreePool (TxToken);
1176 }
1177
1178
1179 /**
1180 Delete the cache entries in the specified CacheTable, using the BySwAddress,
1181 SwAddressType, AddressBuffer combination as the matching key, if Force is TRUE,
1182 the cache is deleted event it's a static entry.
1183
1184 @param CacheTable Pointer to the cache table to do the deletion.
1185 @param BySwAddress Delete the cache entry by software address or by
1186 hardware address.
1187 @param SwAddressType The software address used to do the deletion.
1188 @param AddressBuffer Pointer to the buffer containing the address to
1189 match for the deletion.
1190 @param Force This deletion is forced or not.
1191
1192 @return The count of the deleted cache entries.
1193
1194 **/
1195 STATIC
1196 UINTN
1197 ArpDeleteCacheEntryInTable (
1198 IN NET_LIST_ENTRY *CacheTable,
1199 IN BOOLEAN BySwAddress,
1200 IN UINT16 SwAddressType,
1201 IN UINT8 *AddressBuffer OPTIONAL,
1202 IN BOOLEAN Force
1203 )
1204 {
1205 NET_LIST_ENTRY *Entry;
1206 NET_LIST_ENTRY *NextEntry;
1207 ARP_CACHE_ENTRY *CacheEntry;
1208 UINTN Count;
1209
1210 Count = 0;
1211
1212 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, CacheTable) {
1213 CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List);
1214
1215 if ((CacheEntry->DefaultDecayTime == 0) && !Force) {
1216 //
1217 // It's a static entry and we are not forced to delete it, skip.
1218 //
1219 continue;
1220 }
1221
1222 if (BySwAddress) {
1223 if (SwAddressType == CacheEntry->Addresses[Protocol].Type) {
1224 //
1225 // Protocol address type matched. Check the address.
1226 //
1227 if ((AddressBuffer == NULL) ||
1228 (NetCompareMem (
1229 AddressBuffer,
1230 CacheEntry->Addresses[Protocol].AddressPtr,
1231 CacheEntry->Addresses[Protocol].Length
1232 ) == 0)) {
1233 //
1234 // Address matched.
1235 //
1236 goto MATCHED;
1237 }
1238 }
1239 } else {
1240 if ((AddressBuffer == NULL) ||
1241 (NetCompareMem (
1242 AddressBuffer,
1243 CacheEntry->Addresses[Hardware].AddressPtr,
1244 CacheEntry->Addresses[Hardware].Length
1245 ) == 0)) {
1246 //
1247 // Address matched.
1248 //
1249 goto MATCHED;
1250 }
1251 }
1252
1253 continue;
1254
1255 MATCHED:
1256
1257 //
1258 // Delete this entry.
1259 //
1260 NetListRemoveEntry (&CacheEntry->List);
1261 ASSERT (NetListIsEmpty (&CacheEntry->UserRequestList));
1262 NetFreePool (CacheEntry);
1263
1264 Count++;
1265 }
1266
1267 return Count;
1268 }
1269
1270
1271 /**
1272 Delete cache entries in all the cache tables.
1273
1274 @param Instance Pointer to the instance context data.
1275 @param BySwAddress Delete the cache entry by software address or by
1276 hardware address.
1277 @param AddressBuffer Pointer to the buffer containing the address to
1278 match for the deletion.
1279 @param Force This deletion is forced or not.
1280
1281 @return The count of the deleted cache entries.
1282
1283 **/
1284 UINTN
1285 ArpDeleteCacheEntry (
1286 IN ARP_INSTANCE_DATA *Instance,
1287 IN BOOLEAN BySwAddress,
1288 IN UINT8 *AddressBuffer OPTIONAL,
1289 IN BOOLEAN Force
1290 )
1291 {
1292 ARP_SERVICE_DATA *ArpService;
1293 UINTN Count;
1294
1295 NET_CHECK_SIGNATURE (Instance, ARP_INSTANCE_DATA_SIGNATURE);
1296
1297 ArpService = Instance->ArpService;
1298
1299 //
1300 // Delete the cache entries in the DeniedCacheTable.
1301 //
1302 Count = ArpDeleteCacheEntryInTable (
1303 &ArpService->DeniedCacheTable,
1304 BySwAddress,
1305 Instance->ConfigData.SwAddressType,
1306 AddressBuffer,
1307 Force
1308 );
1309
1310 //
1311 // Delete the cache entries inthe ResolvedCacheTable.
1312 //
1313 Count += ArpDeleteCacheEntryInTable (
1314 &ArpService->ResolvedCacheTable,
1315 BySwAddress,
1316 Instance->ConfigData.SwAddressType,
1317 AddressBuffer,
1318 Force
1319 );
1320
1321 return Count;
1322 }
1323
1324
1325 /**
1326 Cancel the arp request.
1327
1328 @param Instance Pointer to the instance context data.
1329 @param TargetSwAddress Pointer to the buffer containing the target
1330 software address to match the arp request.
1331 @param UserEvent The user event used to notify this request
1332 cancellation.
1333
1334 @return The count of the cancelled requests.
1335
1336 **/
1337 UINTN
1338 ArpCancelRequest (
1339 IN ARP_INSTANCE_DATA *Instance,
1340 IN VOID *TargetSwAddress OPTIONAL,
1341 IN EFI_EVENT UserEvent OPTIONAL
1342 )
1343 {
1344 ARP_SERVICE_DATA *ArpService;
1345 NET_LIST_ENTRY *Entry;
1346 NET_LIST_ENTRY *NextEntry;
1347 ARP_CACHE_ENTRY *CacheEntry;
1348 UINTN Count;
1349
1350 NET_CHECK_SIGNATURE (Instance, ARP_INSTANCE_DATA_SIGNATURE);
1351
1352 ArpService = Instance->ArpService;
1353
1354 Count = 0;
1355 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &ArpService->PendingRequestTable) {
1356 CacheEntry = NET_LIST_USER_STRUCT (Entry, ARP_CACHE_ENTRY, List);
1357
1358 if ((TargetSwAddress == NULL) ||
1359 (NetCompareMem (
1360 TargetSwAddress,
1361 CacheEntry->Addresses[Protocol].AddressPtr,
1362 CacheEntry->Addresses[Protocol].Length
1363 ) == 0)) {
1364 //
1365 // This request entry matches the TargetSwAddress or all requests are to be
1366 // cancelled as TargetSwAddress is NULL.
1367 //
1368 Count += ArpAddressResolved (CacheEntry, Instance, UserEvent);
1369
1370 if (NetListIsEmpty (&CacheEntry->UserRequestList)) {
1371 //
1372 // No user requests any more, remove this request cache entry.
1373 //
1374 NetListRemoveEntry (&CacheEntry->List);
1375 NetFreePool (CacheEntry);
1376 }
1377 }
1378 }
1379
1380 return Count;
1381 }
1382
1383
1384 /**
1385 Find the cache entry in the cache table.
1386
1387 @param Instance Pointer to the instance context data.
1388 @param BySwAddress Set to TRUE to look for matching software protocol
1389 addresses. Set to FALSE to look for matching
1390 hardware protocol addresses.
1391 @param AddressBuffer Pointer to address buffer. Set to NULL to match
1392 all addresses.
1393 @param EntryLength The size of an entry in the entries buffer.
1394 @param EntryCount The number of ARP cache entries that are found by
1395 the specified criteria.
1396 @param Entries Pointer to the buffer that will receive the ARP
1397 cache entries.
1398 @param Refresh Set to TRUE to refresh the timeout value of the
1399 matching ARP cache entry.
1400
1401 @retval EFI_SUCCESS The requested ARP cache entries are copied into
1402 the buffer.
1403 @retval EFI_NOT_FOUND No matching entries found.
1404 @retval EFI_OUT_OF_RESOURCE There is a memory allocation failure.
1405
1406 **/
1407 EFI_STATUS
1408 ArpFindCacheEntry (
1409 IN ARP_INSTANCE_DATA *Instance,
1410 IN BOOLEAN BySwAddress,
1411 IN VOID *AddressBuffer OPTIONAL,
1412 OUT UINT32 *EntryLength OPTIONAL,
1413 OUT UINT32 *EntryCount OPTIONAL,
1414 OUT EFI_ARP_FIND_DATA **Entries OPTIONAL,
1415 IN BOOLEAN Refresh
1416 )
1417 {
1418 EFI_STATUS Status;
1419 ARP_SERVICE_DATA *ArpService;
1420 NET_ARP_ADDRESS MatchAddress;
1421 FIND_OPTYPE FindOpType;
1422 NET_LIST_ENTRY *StartEntry;
1423 ARP_CACHE_ENTRY *CacheEntry;
1424 NET_MAP FoundEntries;
1425 UINT32 FoundCount;
1426 EFI_ARP_FIND_DATA *FindData;
1427 NET_LIST_ENTRY *CacheTable;
1428
1429 ArpService = Instance->ArpService;
1430
1431 //
1432 // Init the FounEntries used to hold the found cache entries.
1433 //
1434 NetMapInit (&FoundEntries);
1435
1436 //
1437 // Set the MatchAddress.
1438 //
1439 if (BySwAddress) {
1440 MatchAddress.Type = Instance->ConfigData.SwAddressType;
1441 MatchAddress.Length = Instance->ConfigData.SwAddressLength;
1442 FindOpType = ByProtoAddress;
1443 } else {
1444 MatchAddress.Type = ArpService->SnpMode.IfType;
1445 MatchAddress.Length = (UINT8)ArpService->SnpMode.HwAddressSize;
1446 FindOpType = ByHwAddress;
1447 }
1448
1449 MatchAddress.AddressPtr = AddressBuffer;
1450
1451 //
1452 // Search the DeniedCacheTable
1453 //
1454 StartEntry = NULL;
1455 while (TRUE) {
1456 //
1457 // Try to find the matched entries in the DeniedCacheTable.
1458 //
1459 CacheEntry = ArpFindNextCacheEntryInTable (
1460 &ArpService->DeniedCacheTable,
1461 StartEntry,
1462 FindOpType,
1463 &MatchAddress,
1464 &MatchAddress
1465 );
1466 if (CacheEntry == NULL) {
1467 //
1468 // Once the CacheEntry is NULL, there are no more matches.
1469 //
1470 break;
1471 }
1472
1473 //
1474 // Insert the found entry into the map.
1475 //
1476 NetMapInsertTail (
1477 &FoundEntries,
1478 (VOID *)CacheEntry,
1479 (VOID *)&ArpService->DeniedCacheTable
1480 );
1481
1482 //
1483 // Let the next search start from this cache entry.
1484 //
1485 StartEntry = &CacheEntry->List;
1486
1487 if (Refresh) {
1488 //
1489 // Refresh the DecayTime if needed.
1490 //
1491 CacheEntry->DecayTime = CacheEntry->DefaultDecayTime;
1492 }
1493 }
1494
1495 //
1496 // Search the ResolvedCacheTable
1497 //
1498 StartEntry = NULL;
1499 while (TRUE) {
1500 CacheEntry = ArpFindNextCacheEntryInTable (
1501 &ArpService->ResolvedCacheTable,
1502 StartEntry,
1503 FindOpType,
1504 &MatchAddress,
1505 &MatchAddress
1506 );
1507 if (CacheEntry == NULL) {
1508 //
1509 // Once the CacheEntry is NULL, there are no more matches.
1510 //
1511 break;
1512 }
1513
1514 //
1515 // Insert the found entry into the map.
1516 //
1517 NetMapInsertTail (
1518 &FoundEntries,
1519 (VOID *)CacheEntry,
1520 (VOID *)&ArpService->ResolvedCacheTable
1521 );
1522
1523 //
1524 // Let the next search start from this cache entry.
1525 //
1526 StartEntry = &CacheEntry->List;
1527
1528 if (Refresh) {
1529 //
1530 // Refresh the DecayTime if needed.
1531 //
1532 CacheEntry->DecayTime = CacheEntry->DefaultDecayTime;
1533 }
1534 }
1535
1536 Status = EFI_SUCCESS;
1537
1538 FoundCount = (UINT32) NetMapGetCount (&FoundEntries);
1539 if (FoundCount == 0) {
1540 Status = EFI_NOT_FOUND;
1541 goto CLEAN_EXIT;
1542 }
1543
1544 if (EntryLength != NULL) {
1545 //
1546 // Return the entry length.
1547 //
1548 *EntryLength = sizeof (EFI_ARP_FIND_DATA) + Instance->ConfigData.SwAddressLength +
1549 ArpService->SnpMode.HwAddressSize;
1550 }
1551
1552 if (EntryCount != NULL) {
1553 //
1554 // Return the found entry count.
1555 //
1556 *EntryCount = FoundCount;
1557 }
1558
1559 if (Entries == NULL) {
1560 goto CLEAN_EXIT;
1561 }
1562
1563 //
1564 // Allocate buffer to copy the found entries.
1565 //
1566 FindData = NetAllocatePool (FoundCount * (*EntryLength));
1567 if (FindData == NULL) {
1568 ARP_DEBUG_ERROR (("ArpFindCacheEntry: Failed to allocate memory.\n"));
1569 Status = EFI_OUT_OF_RESOURCES;
1570 goto CLEAN_EXIT;
1571 }
1572
1573 //
1574 // Return the address to the user.
1575 //
1576 *Entries = FindData;
1577
1578 //
1579 // Dump the entries.
1580 //
1581 while (!NetMapIsEmpty (&FoundEntries)) {
1582 //
1583 // Get a cache entry from the map.
1584 //
1585 CacheEntry = NetMapRemoveHead (&FoundEntries, (VOID **)&CacheTable);
1586
1587 //
1588 // Set the fields in FindData.
1589 //
1590 FindData->Size = *EntryLength;
1591 FindData->DenyFlag = (BOOLEAN)(CacheTable == &ArpService->DeniedCacheTable);
1592 FindData->StaticFlag = (BOOLEAN)(CacheEntry->DefaultDecayTime == 0);
1593 FindData->HwAddressType = ArpService->SnpMode.IfType;
1594 FindData->SwAddressType = Instance->ConfigData.SwAddressType;
1595 FindData->HwAddressLength = (UINT8)ArpService->SnpMode.HwAddressSize;
1596 FindData->SwAddressLength = Instance->ConfigData.SwAddressLength;
1597
1598 //
1599 // Copy the software address.
1600 //
1601 NetCopyMem (
1602 FindData + 1,
1603 CacheEntry->Addresses[Protocol].AddressPtr,
1604 FindData->SwAddressLength
1605 );
1606
1607 //
1608 // Copy the hardware address.
1609 //
1610 NetCopyMem (
1611 (UINT8 *)(FindData + 1) + FindData->SwAddressLength,
1612 CacheEntry->Addresses[Hardware].AddressPtr,
1613 FindData->HwAddressLength
1614 );
1615
1616 //
1617 // Slip to the next FindData.
1618 //
1619 FindData = (EFI_ARP_FIND_DATA *)((UINT8 *)FindData + *EntryLength);
1620 }
1621
1622 CLEAN_EXIT:
1623
1624 NetMapClean (&FoundEntries);
1625
1626 return Status;
1627 }
1628