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