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