]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Nd.c
1. Fix bugs for PXE-IPv6 to accommodate the situation:
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Nd.c
1 /** @file
2 Implementation of Neighbor Discovery support routines.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Ip6Impl.h"
17
18 EFI_MAC_ADDRESS mZeroMacAddress;
19
20 /**
21 Update the ReachableTime in IP6 service binding instance data, in milliseconds.
22
23 @param[in, out] IpSb Points to the IP6_SERVICE.
24
25 **/
26 VOID
27 Ip6UpdateReachableTime (
28 IN OUT IP6_SERVICE *IpSb
29 )
30 {
31 UINT32 Random;
32
33 Random = (NetRandomInitSeed () / 4294967295UL) * IP6_RANDOM_FACTOR_SCALE;
34 Random = Random + IP6_MIN_RANDOM_FACTOR_SCALED;
35 IpSb->ReachableTime = (IpSb->BaseReachableTime * Random) / IP6_RANDOM_FACTOR_SCALE;
36 }
37
38 /**
39 Build a array of EFI_IP6_NEIGHBOR_CACHE to be returned to the caller. The number
40 of EFI_IP6_NEIGHBOR_CACHE is also returned.
41
42 @param[in] IpInstance The pointer to IP6_PROTOCOL instance.
43 @param[out] NeighborCount The number of returned neighbor cache entries.
44 @param[out] NeighborCache The pointer to the array of EFI_IP6_NEIGHBOR_CACHE.
45
46 @retval EFI_SUCCESS The EFI_IP6_NEIGHBOR_CACHE successfully built.
47 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table.
48
49 **/
50 EFI_STATUS
51 Ip6BuildEfiNeighborCache (
52 IN IP6_PROTOCOL *IpInstance,
53 OUT UINT32 *NeighborCount,
54 OUT EFI_IP6_NEIGHBOR_CACHE **NeighborCache
55 )
56 {
57 IP6_NEIGHBOR_ENTRY *Neighbor;
58 LIST_ENTRY *Entry;
59 IP6_SERVICE *IpSb;
60 UINT32 Count;
61 EFI_IP6_NEIGHBOR_CACHE *EfiNeighborCache;
62 EFI_IP6_NEIGHBOR_CACHE *NeighborCacheTmp;
63
64 NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE);
65 ASSERT (NeighborCount != NULL && NeighborCache != NULL);
66
67 IpSb = IpInstance->Service;
68 Count = 0;
69
70 NET_LIST_FOR_EACH (Entry, &IpSb->NeighborTable) {
71 Count++;
72 }
73
74 if (Count == 0) {
75 return EFI_SUCCESS;
76 }
77
78 NeighborCacheTmp = AllocatePool (Count * sizeof (EFI_IP6_NEIGHBOR_CACHE));
79 if (NeighborCacheTmp == NULL) {
80 return EFI_OUT_OF_RESOURCES;
81 }
82
83 *NeighborCount = Count;
84 Count = 0;
85
86 NET_LIST_FOR_EACH (Entry, &IpSb->NeighborTable) {
87 Neighbor = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
88
89 EfiNeighborCache = NeighborCacheTmp + Count;
90
91 EfiNeighborCache->State = Neighbor->State;
92 IP6_COPY_ADDRESS (&EfiNeighborCache->Neighbor, &Neighbor->Neighbor);
93 IP6_COPY_LINK_ADDRESS (&EfiNeighborCache->LinkAddress, &Neighbor->LinkAddress);
94
95 Count++;
96 }
97
98 ASSERT (*NeighborCount == Count);
99 *NeighborCache = NeighborCacheTmp;
100
101 return EFI_SUCCESS;
102 }
103
104 /**
105 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
106 of prefix entries is also returned.
107
108 @param[in] IpInstance The pointer to IP6_PROTOCOL instance.
109 @param[out] PrefixCount The number of returned prefix entries.
110 @param[out] PrefixTable The pointer to the array of PrefixTable.
111
112 @retval EFI_SUCCESS The prefix table successfully built.
113 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the prefix table.
114
115 **/
116 EFI_STATUS
117 Ip6BuildPrefixTable (
118 IN IP6_PROTOCOL *IpInstance,
119 OUT UINT32 *PrefixCount,
120 OUT EFI_IP6_ADDRESS_INFO **PrefixTable
121 )
122 {
123 LIST_ENTRY *Entry;
124 IP6_SERVICE *IpSb;
125 UINT32 Count;
126 IP6_PREFIX_LIST_ENTRY *PrefixList;
127 EFI_IP6_ADDRESS_INFO *EfiPrefix;
128 EFI_IP6_ADDRESS_INFO *PrefixTableTmp;
129
130 NET_CHECK_SIGNATURE (IpInstance, IP6_PROTOCOL_SIGNATURE);
131 ASSERT (PrefixCount != NULL && PrefixTable != NULL);
132
133 IpSb = IpInstance->Service;
134 Count = 0;
135
136 NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) {
137 Count++;
138 }
139
140 if (Count == 0) {
141 return EFI_SUCCESS;
142 }
143
144 PrefixTableTmp = AllocatePool (Count * sizeof (EFI_IP6_ADDRESS_INFO));
145 if (PrefixTableTmp == NULL) {
146 return EFI_OUT_OF_RESOURCES;
147 }
148
149 *PrefixCount = Count;
150 Count = 0;
151
152 NET_LIST_FOR_EACH (Entry, &IpSb->OnlinkPrefix) {
153 PrefixList = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
154 EfiPrefix = PrefixTableTmp + Count;
155 IP6_COPY_ADDRESS (&EfiPrefix->Address, &PrefixList->Prefix);
156 EfiPrefix->PrefixLength = PrefixList->PrefixLength;
157
158 Count++;
159 }
160
161 ASSERT (*PrefixCount == Count);
162 *PrefixTable = PrefixTableTmp;
163
164 return EFI_SUCCESS;
165 }
166
167 /**
168 Allocate and initialize a IP6 prefix list entry.
169
170 @param[in] IpSb The pointer to IP6_SERVICE instance.
171 @param[in] OnLinkOrAuto If TRUE, the entry is created for the on link prefix list.
172 Otherwise, it is created for the autoconfiguration prefix list.
173 @param[in] ValidLifetime The length of time in seconds that the prefix
174 is valid for the purpose of on-link determination.
175 @param[in] PreferredLifetime The length of time in seconds that addresses
176 generated from the prefix via stateless address
177 autoconfiguration remain preferred.
178 @param[in] PrefixLength The prefix length of the Prefix.
179 @param[in] Prefix The prefix address.
180
181 @return NULL if it failed to allocate memory for the prefix node. Otherwise, point
182 to the created or existing prefix list entry.
183
184 **/
185 IP6_PREFIX_LIST_ENTRY *
186 Ip6CreatePrefixListEntry (
187 IN IP6_SERVICE *IpSb,
188 IN BOOLEAN OnLinkOrAuto,
189 IN UINT32 ValidLifetime,
190 IN UINT32 PreferredLifetime,
191 IN UINT8 PrefixLength,
192 IN EFI_IPv6_ADDRESS *Prefix
193 )
194 {
195 IP6_PREFIX_LIST_ENTRY *PrefixEntry;
196 IP6_ROUTE_ENTRY *RtEntry;
197 LIST_ENTRY *ListHead;
198 LIST_ENTRY *Entry;
199 IP6_PREFIX_LIST_ENTRY *TmpPrefixEntry;
200
201 if (Prefix == NULL || PreferredLifetime > ValidLifetime || PrefixLength >= IP6_PREFIX_NUM) {
202 return NULL;
203 }
204
205 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
206
207 PrefixEntry = Ip6FindPrefixListEntry (
208 IpSb,
209 OnLinkOrAuto,
210 PrefixLength,
211 Prefix
212 );
213 if (PrefixEntry != NULL) {
214 PrefixEntry->RefCnt ++;
215 return PrefixEntry;
216 }
217
218 PrefixEntry = AllocatePool (sizeof (IP6_PREFIX_LIST_ENTRY));
219 if (PrefixEntry == NULL) {
220 return NULL;
221 }
222
223 PrefixEntry->RefCnt = 1;
224 PrefixEntry->ValidLifetime = ValidLifetime;
225 PrefixEntry->PreferredLifetime = PreferredLifetime;
226 PrefixEntry->PrefixLength = PrefixLength;
227 IP6_COPY_ADDRESS (&PrefixEntry->Prefix, Prefix);
228
229 ListHead = OnLinkOrAuto ? &IpSb->OnlinkPrefix : &IpSb->AutonomousPrefix;
230
231 //
232 // Create a direct route entry for on-link prefix and insert to route area.
233 //
234 if (OnLinkOrAuto) {
235 RtEntry = Ip6CreateRouteEntry (Prefix, PrefixLength, NULL);
236 if (RtEntry == NULL) {
237 FreePool (PrefixEntry);
238 return NULL;
239 }
240
241 RtEntry->Flag = IP6_DIRECT_ROUTE;
242 InsertHeadList (&IpSb->RouteTable->RouteArea[PrefixLength], &RtEntry->Link);
243 IpSb->RouteTable->TotalNum++;
244 }
245
246 //
247 // Insert the prefix entry in the order that a prefix with longer prefix length
248 // is put ahead in the list.
249 //
250 NET_LIST_FOR_EACH (Entry, ListHead) {
251 TmpPrefixEntry = NET_LIST_USER_STRUCT(Entry, IP6_PREFIX_LIST_ENTRY, Link);
252
253 if (TmpPrefixEntry->PrefixLength < PrefixEntry->PrefixLength) {
254 break;
255 }
256 }
257
258 NetListInsertBefore (Entry, &PrefixEntry->Link);
259
260 return PrefixEntry;
261 }
262
263 /**
264 Destory a IP6 prefix list entry.
265
266 @param[in] IpSb The pointer to IP6_SERVICE instance.
267 @param[in] PrefixEntry The to be destroyed prefix list entry.
268 @param[in] OnLinkOrAuto If TRUE, the entry is removed from on link prefix list.
269 Otherwise remove from autoconfiguration prefix list.
270 @param[in] ImmediateDelete If TRUE, remove the entry directly.
271 Otherwise, check the reference count to see whether
272 it should be removed.
273
274 **/
275 VOID
276 Ip6DestroyPrefixListEntry (
277 IN IP6_SERVICE *IpSb,
278 IN IP6_PREFIX_LIST_ENTRY *PrefixEntry,
279 IN BOOLEAN OnLinkOrAuto,
280 IN BOOLEAN ImmediateDelete
281 )
282 {
283 LIST_ENTRY *Entry;
284 IP6_INTERFACE *IpIf;
285 EFI_STATUS Status;
286
287 if ((!ImmediateDelete) && (PrefixEntry->RefCnt > 0) && ((--PrefixEntry->RefCnt) > 0)) {
288 return ;
289 }
290
291 if (OnLinkOrAuto) {
292 //
293 // Remove the direct route for onlink prefix from route table.
294 //
295 do {
296 Status = Ip6DelRoute (
297 IpSb->RouteTable,
298 &PrefixEntry->Prefix,
299 PrefixEntry->PrefixLength,
300 NULL
301 );
302 } while (Status != EFI_NOT_FOUND);
303 } else {
304 //
305 // Remove the corresponding addresses generated from this autonomous prefix.
306 //
307 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
308 IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
309
310 Ip6RemoveAddr (IpSb, &IpIf->AddressList, &IpIf->AddressCount, &PrefixEntry->Prefix, PrefixEntry->PrefixLength);
311 }
312 }
313
314 RemoveEntryList (&PrefixEntry->Link);
315 FreePool (PrefixEntry);
316 }
317
318 /**
319 Search the list array to find an IP6 prefix list entry.
320
321 @param[in] IpSb The pointer to IP6_SERVICE instance.
322 @param[in] OnLinkOrAuto If TRUE, the search the link prefix list,
323 Otherwise search the autoconfiguration prefix list.
324 @param[in] PrefixLength The prefix length of the Prefix
325 @param[in] Prefix The prefix address.
326
327 @return NULL if cannot find the IP6 prefix list entry. Otherwise, return the
328 pointer to the IP6 prefix list entry.
329
330 **/
331 IP6_PREFIX_LIST_ENTRY *
332 Ip6FindPrefixListEntry (
333 IN IP6_SERVICE *IpSb,
334 IN BOOLEAN OnLinkOrAuto,
335 IN UINT8 PrefixLength,
336 IN EFI_IPv6_ADDRESS *Prefix
337 )
338 {
339 IP6_PREFIX_LIST_ENTRY *PrefixList;
340 LIST_ENTRY *Entry;
341 LIST_ENTRY *ListHead;
342
343 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
344 ASSERT (Prefix != NULL);
345
346 if (OnLinkOrAuto) {
347 ListHead = &IpSb->OnlinkPrefix;
348 } else {
349 ListHead = &IpSb->AutonomousPrefix;
350 }
351
352 NET_LIST_FOR_EACH (Entry, ListHead) {
353 PrefixList = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
354 if (PrefixLength != 255) {
355 //
356 // Perform exactly prefix match.
357 //
358 if (PrefixList->PrefixLength == PrefixLength &&
359 NetIp6IsNetEqual (&PrefixList->Prefix, Prefix, PrefixLength)) {
360 return PrefixList;
361 }
362 } else {
363 //
364 // Perform the longest prefix match. The list is already sorted with
365 // the longest length prefix put at the head of the list.
366 //
367 if (NetIp6IsNetEqual (&PrefixList->Prefix, Prefix, PrefixList->PrefixLength)) {
368 return PrefixList;
369 }
370 }
371 }
372
373 return NULL;
374 }
375
376 /**
377 Release the resource in the prefix list table, and destroy the list entry and
378 corresponding addresses or route entries.
379
380 @param[in] IpSb The pointer to the IP6_SERVICE instance.
381 @param[in] ListHead The list entry head of the prefix list table.
382
383 **/
384 VOID
385 Ip6CleanPrefixListTable (
386 IN IP6_SERVICE *IpSb,
387 IN LIST_ENTRY *ListHead
388 )
389 {
390 IP6_PREFIX_LIST_ENTRY *PrefixList;
391 BOOLEAN OnLink;
392
393 OnLink = (BOOLEAN) (ListHead == &IpSb->OnlinkPrefix);
394
395 while (!IsListEmpty (ListHead)) {
396 PrefixList = NET_LIST_HEAD (ListHead, IP6_PREFIX_LIST_ENTRY, Link);
397 Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE);
398 }
399 }
400
401 /**
402 Callback function when address resolution is finished. It will cancel
403 all the queued frames if the address resolution failed, or transmit them
404 if the request succeeded.
405
406 @param[in] Context The context of the callback, a pointer to IP6_NEIGHBOR_ENTRY.
407
408 **/
409 VOID
410 Ip6OnArpResolved (
411 IN VOID *Context
412 )
413 {
414 LIST_ENTRY *Entry;
415 LIST_ENTRY *Next;
416 IP6_NEIGHBOR_ENTRY *ArpQue;
417 IP6_SERVICE *IpSb;
418 IP6_LINK_TX_TOKEN *Token;
419 EFI_STATUS Status;
420 BOOLEAN Sent;
421
422 ArpQue = (IP6_NEIGHBOR_ENTRY *) Context;
423 if ((ArpQue == NULL) || (ArpQue->Interface == NULL)) {
424 return ;
425 }
426
427 IpSb = ArpQue->Interface->Service;
428 if ((IpSb == NULL) || (IpSb->Signature != IP6_SERVICE_SIGNATURE)) {
429 return ;
430 }
431
432 //
433 // ARP resolve failed for some reason. Release all the frame
434 // and ARP queue itself. Ip6FreeArpQue will call the frame's
435 // owner back.
436 //
437 if (NET_MAC_EQUAL (&ArpQue->LinkAddress, &mZeroMacAddress, IpSb->SnpMode.HwAddressSize)) {
438 Ip6FreeNeighborEntry (IpSb, ArpQue, FALSE, TRUE, EFI_NO_MAPPING, NULL, NULL);
439 return ;
440 }
441
442 //
443 // ARP resolve succeeded, Transmit all the frame.
444 //
445 Sent = FALSE;
446 NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) {
447 RemoveEntryList (Entry);
448
449 Token = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link);
450 IP6_COPY_LINK_ADDRESS (&Token->DstMac, &ArpQue->LinkAddress);
451
452 //
453 // Insert the tx token before transmitting it via MNP as the FrameSentDpc
454 // may be called before Mnp->Transmit returns which will remove this tx
455 // token from the SentFrames list. Remove it from the list if the returned
456 // Status of Mnp->Transmit is not EFI_SUCCESS as in this case the
457 // FrameSentDpc won't be queued.
458 //
459 InsertTailList (&ArpQue->Interface->SentFrames, &Token->Link);
460
461 Status = IpSb->Mnp->Transmit (IpSb->Mnp, &Token->MnpToken);
462 if (EFI_ERROR (Status)) {
463 RemoveEntryList (&Token->Link);
464 Token->CallBack (Token->Packet, Status, 0, Token->Context);
465
466 Ip6FreeLinkTxToken (Token);
467 continue;
468 } else {
469 Sent = TRUE;
470 }
471 }
472
473 //
474 // Free the ArpQue only but not the whole neighbor entry.
475 //
476 Ip6FreeNeighborEntry (IpSb, ArpQue, FALSE, FALSE, EFI_SUCCESS, NULL, NULL);
477
478 if (Sent && (ArpQue->State == EfiNeighborStale)) {
479 ArpQue->State = EfiNeighborDelay;
480 ArpQue->Ticks = (UINT32) IP6_GET_TICKS (IP6_DELAY_FIRST_PROBE_TIME);
481 }
482 }
483
484 /**
485 Allocate and initialize an IP6 neighbor cache entry.
486
487 @param[in] IpSb The pointer to the IP6_SERVICE instance.
488 @param[in] CallBack The callback function to be called when
489 address resolution is finished.
490 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
491 @param[in] LinkAddress Points to the MAC address of the neighbor.
492 Ignored if NULL.
493
494 @return NULL if failed to allocate memory for the neighbor cache entry.
495 Otherwise, point to the created neighbor cache entry.
496
497 **/
498 IP6_NEIGHBOR_ENTRY *
499 Ip6CreateNeighborEntry (
500 IN IP6_SERVICE *IpSb,
501 IN IP6_ARP_CALLBACK CallBack,
502 IN EFI_IPv6_ADDRESS *Ip6Address,
503 IN EFI_MAC_ADDRESS *LinkAddress OPTIONAL
504 )
505 {
506 IP6_NEIGHBOR_ENTRY *Entry;
507 IP6_DEFAULT_ROUTER *DefaultRouter;
508
509 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
510 ASSERT (Ip6Address!= NULL);
511
512 Entry = AllocateZeroPool (sizeof (IP6_NEIGHBOR_ENTRY));
513 if (Entry == NULL) {
514 return NULL;
515 }
516
517 Entry->RefCnt = 1;
518 Entry->IsRouter = FALSE;
519 Entry->ArpFree = FALSE;
520 Entry->Dynamic = FALSE;
521 Entry->State = EfiNeighborInComplete;
522 Entry->Transmit = IP6_MAX_MULTICAST_SOLICIT + 1;
523 Entry->CallBack = CallBack;
524 Entry->Interface = NULL;
525
526 InitializeListHead (&Entry->Frames);
527
528 IP6_COPY_ADDRESS (&Entry->Neighbor, Ip6Address);
529
530 if (LinkAddress != NULL) {
531 IP6_COPY_LINK_ADDRESS (&Entry->LinkAddress, LinkAddress);
532 } else {
533 IP6_COPY_LINK_ADDRESS (&Entry->LinkAddress, &mZeroMacAddress);
534 }
535
536 InsertHeadList (&IpSb->NeighborTable, &Entry->Link);
537
538 //
539 // If corresponding default router entry exists, establish the relationship.
540 //
541 DefaultRouter = Ip6FindDefaultRouter (IpSb, Ip6Address);
542 if (DefaultRouter != NULL) {
543 DefaultRouter->NeighborCache = Entry;
544 }
545
546 return Entry;
547 }
548
549 /**
550 Search a IP6 neighbor cache entry.
551
552 @param[in] IpSb The pointer to the IP6_SERVICE instance.
553 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
554
555 @return NULL if it failed to find the matching neighbor cache entry.
556 Otherwise, point to the found neighbor cache entry.
557
558 **/
559 IP6_NEIGHBOR_ENTRY *
560 Ip6FindNeighborEntry (
561 IN IP6_SERVICE *IpSb,
562 IN EFI_IPv6_ADDRESS *Ip6Address
563 )
564 {
565 LIST_ENTRY *Entry;
566 LIST_ENTRY *Next;
567 IP6_NEIGHBOR_ENTRY *Neighbor;
568
569 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
570 ASSERT (Ip6Address != NULL);
571
572 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) {
573 Neighbor = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
574 if (EFI_IP6_EQUAL (Ip6Address, &Neighbor->Neighbor)) {
575 RemoveEntryList (Entry);
576 InsertHeadList (&IpSb->NeighborTable, Entry);
577
578 return Neighbor;
579 }
580 }
581
582 return NULL;
583 }
584
585 /**
586 Free a IP6 neighbor cache entry and remove all the frames on the address
587 resolution queue that pass the FrameToCancel. That is, either FrameToCancel
588 is NULL, or it returns true for the frame.
589
590 @param[in] IpSb The pointer to the IP6_SERVICE instance.
591 @param[in] NeighborCache The to be free neighbor cache entry.
592 @param[in] SendIcmpError If TRUE, send out ICMP error.
593 @param[in] FullFree If TRUE, remove the neighbor cache entry.
594 Otherwise remove the pending frames.
595 @param[in] IoStatus The status returned to the cancelled frames'
596 callback function.
597 @param[in] FrameToCancel Function to select which frame to cancel.
598 This is an optional parameter that may be NULL.
599 @param[in] Context Opaque parameter to the FrameToCancel.
600 Ignored if FrameToCancel is NULL.
601
602 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
603 @retval EFI_SUCCESS The operation finished successfully.
604
605 **/
606 EFI_STATUS
607 Ip6FreeNeighborEntry (
608 IN IP6_SERVICE *IpSb,
609 IN IP6_NEIGHBOR_ENTRY *NeighborCache,
610 IN BOOLEAN SendIcmpError,
611 IN BOOLEAN FullFree,
612 IN EFI_STATUS IoStatus,
613 IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL,
614 IN VOID *Context OPTIONAL
615 )
616 {
617 IP6_LINK_TX_TOKEN *TxToken;
618 LIST_ENTRY *Entry;
619 LIST_ENTRY *Next;
620 IP6_DEFAULT_ROUTER *DefaultRouter;
621
622 //
623 // If FrameToCancel fails, the token will not be released.
624 // To avoid the memory leak, stop this usage model.
625 //
626 if (FullFree && FrameToCancel != NULL) {
627 return EFI_INVALID_PARAMETER;
628 }
629
630 NET_LIST_FOR_EACH_SAFE (Entry, Next, &NeighborCache->Frames) {
631 TxToken = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link);
632
633 if (SendIcmpError && !IP6_IS_MULTICAST (&TxToken->Packet->Ip.Ip6->DestinationAddress)) {
634 Ip6SendIcmpError (
635 IpSb,
636 TxToken->Packet,
637 NULL,
638 &TxToken->Packet->Ip.Ip6->SourceAddress,
639 ICMP_V6_DEST_UNREACHABLE,
640 ICMP_V6_ADDR_UNREACHABLE,
641 NULL
642 );
643 }
644
645 if ((FrameToCancel == NULL) || FrameToCancel (TxToken, Context)) {
646 RemoveEntryList (Entry);
647 TxToken->CallBack (TxToken->Packet, IoStatus, 0, TxToken->Context);
648 Ip6FreeLinkTxToken (TxToken);
649 }
650 }
651
652 if (NeighborCache->ArpFree && IsListEmpty (&NeighborCache->Frames)) {
653 RemoveEntryList (&NeighborCache->ArpList);
654 NeighborCache->ArpFree = FALSE;
655 }
656
657 if (FullFree) {
658 if (NeighborCache->IsRouter) {
659 DefaultRouter = Ip6FindDefaultRouter (IpSb, &NeighborCache->Neighbor);
660 if (DefaultRouter != NULL) {
661 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
662 }
663 }
664
665 RemoveEntryList (&NeighborCache->Link);
666 FreePool (NeighborCache);
667 }
668
669 return EFI_SUCCESS;
670 }
671
672 /**
673 Allocate and initialize an IP6 default router entry.
674
675 @param[in] IpSb The pointer to the IP6_SERVICE instance.
676 @param[in] Ip6Address The IPv6 address of the default router.
677 @param[in] RouterLifetime The lifetime associated with the default
678 router, in units of seconds.
679
680 @return NULL if it failed to allocate memory for the default router node.
681 Otherwise, point to the created default router node.
682
683 **/
684 IP6_DEFAULT_ROUTER *
685 Ip6CreateDefaultRouter (
686 IN IP6_SERVICE *IpSb,
687 IN EFI_IPv6_ADDRESS *Ip6Address,
688 IN UINT16 RouterLifetime
689 )
690 {
691 IP6_DEFAULT_ROUTER *Entry;
692 IP6_ROUTE_ENTRY *RtEntry;
693
694 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
695 ASSERT (Ip6Address != NULL);
696
697 Entry = AllocatePool (sizeof (IP6_DEFAULT_ROUTER));
698 if (Entry == NULL) {
699 return NULL;
700 }
701
702 Entry->RefCnt = 1;
703 Entry->Lifetime = RouterLifetime;
704 Entry->NeighborCache = Ip6FindNeighborEntry (IpSb, Ip6Address);
705 IP6_COPY_ADDRESS (&Entry->Router, Ip6Address);
706
707 //
708 // Add a default route into route table with both Destination and PrefixLength set to zero.
709 //
710 RtEntry = Ip6CreateRouteEntry (NULL, 0, Ip6Address);
711 if (RtEntry == NULL) {
712 FreePool (Entry);
713 return NULL;
714 }
715
716 InsertHeadList (&IpSb->RouteTable->RouteArea[0], &RtEntry->Link);
717 IpSb->RouteTable->TotalNum++;
718
719 InsertTailList (&IpSb->DefaultRouterList, &Entry->Link);
720
721 return Entry;
722 }
723
724 /**
725 Destroy an IP6 default router entry.
726
727 @param[in] IpSb The pointer to the IP6_SERVICE instance.
728 @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER.
729
730 **/
731 VOID
732 Ip6DestroyDefaultRouter (
733 IN IP6_SERVICE *IpSb,
734 IN IP6_DEFAULT_ROUTER *DefaultRouter
735 )
736 {
737 EFI_STATUS Status;
738
739 RemoveEntryList (&DefaultRouter->Link);
740
741 //
742 // Update the Destination Cache - all entries using the time-out router as next-hop
743 // should perform next-hop determination again.
744 //
745 do {
746 Status = Ip6DelRoute (IpSb->RouteTable, NULL, 0, &DefaultRouter->Router);
747 } while (Status != EFI_NOT_FOUND);
748
749 FreePool (DefaultRouter);
750 }
751
752 /**
753 Clean an IP6 default router list.
754
755 @param[in] IpSb The pointer to the IP6_SERVICE instance.
756
757 **/
758 VOID
759 Ip6CleanDefaultRouterList (
760 IN IP6_SERVICE *IpSb
761 )
762 {
763 IP6_DEFAULT_ROUTER *DefaultRouter;
764
765 while (!IsListEmpty (&IpSb->DefaultRouterList)) {
766 DefaultRouter = NET_LIST_HEAD (&IpSb->DefaultRouterList, IP6_DEFAULT_ROUTER, Link);
767 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
768 }
769 }
770
771 /**
772 Search a default router node from an IP6 default router list.
773
774 @param[in] IpSb The pointer to the IP6_SERVICE instance.
775 @param[in] Ip6Address The IPv6 address of the to be searched default router node.
776
777 @return NULL if it failed to find the matching default router node.
778 Otherwise, point to the found default router node.
779
780 **/
781 IP6_DEFAULT_ROUTER *
782 Ip6FindDefaultRouter (
783 IN IP6_SERVICE *IpSb,
784 IN EFI_IPv6_ADDRESS *Ip6Address
785 )
786 {
787 LIST_ENTRY *Entry;
788 IP6_DEFAULT_ROUTER *DefaultRouter;
789
790 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
791 ASSERT (Ip6Address != NULL);
792
793 NET_LIST_FOR_EACH (Entry, &IpSb->DefaultRouterList) {
794 DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link);
795 if (EFI_IP6_EQUAL (Ip6Address, &DefaultRouter->Router)) {
796 return DefaultRouter;
797 }
798 }
799
800 return NULL;
801 }
802
803 /**
804 The function to be called after DAD (Duplicate Address Detection) is performed.
805
806 @param[in] IsDadPassed If TRUE, the DAD operation succeed. Otherwise, the DAD operation failed.
807 @param[in] IpIf Points to the IP6_INTERFACE.
808 @param[in] DadEntry The DAD entry which already performed DAD.
809
810 **/
811 VOID
812 Ip6OnDADFinished (
813 IN BOOLEAN IsDadPassed,
814 IN IP6_INTERFACE *IpIf,
815 IN IP6_DAD_ENTRY *DadEntry
816 )
817 {
818 IP6_SERVICE *IpSb;
819 IP6_ADDRESS_INFO *AddrInfo;
820 EFI_DHCP6_PROTOCOL *Dhcp6;
821 UINT16 OptBuf[4];
822 EFI_DHCP6_PACKET_OPTION *Oro;
823 EFI_DHCP6_RETRANSMISSION InfoReqReXmit;
824
825 IpSb = IpIf->Service;
826 AddrInfo = DadEntry->AddressInfo;
827
828 if (IsDadPassed) {
829 //
830 // DAD succeed.
831 //
832 if (NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
833 ASSERT (!IpSb->LinkLocalOk);
834
835 IP6_COPY_ADDRESS (&IpSb->LinkLocalAddr, &AddrInfo->Address);
836 IpSb->LinkLocalOk = TRUE;
837 IpIf->Configured = TRUE;
838
839 //
840 // Check whether DHCP6 need to be started.
841 //
842 Dhcp6 = IpSb->Ip6ConfigInstance.Dhcp6;
843
844 if (IpSb->Dhcp6NeedStart) {
845 Dhcp6->Start (Dhcp6);
846 IpSb->Dhcp6NeedStart = FALSE;
847 }
848
849 if (IpSb->Dhcp6NeedInfoRequest) {
850 //
851 // Set the exta options to send. Here we only want the option request option
852 // with DNS SERVERS.
853 //
854 Oro = (EFI_DHCP6_PACKET_OPTION *) OptBuf;
855 Oro->OpCode = HTONS (IP6_CONFIG_DHCP6_OPTION_ORO);
856 Oro->OpLen = HTONS (2);
857 *((UINT16 *) &Oro->Data[0]) = HTONS (IP6_CONFIG_DHCP6_OPTION_DNS_SERVERS);
858
859 InfoReqReXmit.Irt = 4;
860 InfoReqReXmit.Mrc = 64;
861 InfoReqReXmit.Mrt = 60;
862 InfoReqReXmit.Mrd = 0;
863
864 Dhcp6->InfoRequest (
865 Dhcp6,
866 TRUE,
867 Oro,
868 0,
869 NULL,
870 &InfoReqReXmit,
871 IpSb->Ip6ConfigInstance.Dhcp6Event,
872 Ip6ConfigOnDhcp6Reply,
873 &IpSb->Ip6ConfigInstance
874 );
875 }
876
877 //
878 // Add an on-link prefix for link-local address.
879 //
880 Ip6CreatePrefixListEntry (
881 IpSb,
882 TRUE,
883 (UINT32) IP6_INFINIT_LIFETIME,
884 (UINT32) IP6_INFINIT_LIFETIME,
885 IP6_LINK_LOCAL_PREFIX_LENGTH,
886 &IpSb->LinkLocalAddr
887 );
888
889 } else {
890 //
891 // Global scope unicast address.
892 //
893 Ip6AddAddr (IpIf, AddrInfo);
894
895 //
896 // Add an on-link prefix for this address.
897 //
898 Ip6CreatePrefixListEntry (
899 IpSb,
900 TRUE,
901 AddrInfo->ValidLifetime,
902 AddrInfo->PreferredLifetime,
903 AddrInfo->PrefixLength,
904 &AddrInfo->Address
905 );
906
907 IpIf->Configured = TRUE;
908 }
909 } else {
910 //
911 // Leave the group we joined before.
912 //
913 Ip6LeaveGroup (IpSb, &DadEntry->Destination);
914 }
915
916 if (DadEntry->Callback != NULL) {
917 DadEntry->Callback (IsDadPassed, &AddrInfo->Address, DadEntry->Context);
918 }
919
920 if (!IsDadPassed && NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
921 FreePool (AddrInfo);
922 RemoveEntryList (&DadEntry->Link);
923 FreePool (DadEntry);
924 //
925 // Disable IP operation since link-local address is a duplicate address.
926 //
927 IpSb->LinkLocalDadFail = TRUE;
928 IpSb->Mnp->Configure (IpSb->Mnp, NULL);
929 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
930 gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0);
931 return ;
932 }
933
934 if (!IsDadPassed || NetIp6IsLinkLocalAddr (&AddrInfo->Address)) {
935 //
936 // Free the AddressInfo we hold if DAD fails or it is a link-local address.
937 //
938 FreePool (AddrInfo);
939 }
940
941 RemoveEntryList (&DadEntry->Link);
942 FreePool (DadEntry);
943 }
944
945 /**
946 Create a DAD (Duplicate Address Detection) entry and queue it to be performed.
947
948 @param[in] IpIf Points to the IP6_INTERFACE.
949 @param[in] AddressInfo The address information which needs DAD performed.
950 @param[in] Callback The callback routine that will be called after DAD
951 is performed. This is an optional parameter that
952 may be NULL.
953 @param[in] Context The opaque parameter for a DAD callback routine.
954 This is an optional parameter that may be NULL.
955
956 @retval EFI_SUCCESS The DAD entry was created and queued.
957 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory to complete the
958 operation.
959
960
961 **/
962 EFI_STATUS
963 Ip6InitDADProcess (
964 IN IP6_INTERFACE *IpIf,
965 IN IP6_ADDRESS_INFO *AddressInfo,
966 IN IP6_DAD_CALLBACK Callback OPTIONAL,
967 IN VOID *Context OPTIONAL
968 )
969 {
970 IP6_DAD_ENTRY *Entry;
971 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS *DadXmits;
972 IP6_SERVICE *IpSb;
973 EFI_STATUS Status;
974 UINT32 MaxDelayTick;
975
976 NET_CHECK_SIGNATURE (IpIf, IP6_INTERFACE_SIGNATURE);
977 ASSERT (AddressInfo != NULL);
978
979 Status = EFI_SUCCESS;
980 IpSb = IpIf->Service;
981 DadXmits = &IpSb->Ip6ConfigInstance.DadXmits;
982
983 //
984 // Allocate the resources and insert info
985 //
986 Entry = AllocatePool (sizeof (IP6_DAD_ENTRY));
987 if (Entry == NULL) {
988 return EFI_OUT_OF_RESOURCES;
989 }
990
991 //
992 // Map the incoming unicast address to solicited-node multicast address
993 //
994 Ip6CreateSNMulticastAddr (&AddressInfo->Address, &Entry->Destination);
995
996 //
997 // Join in the solicited-node multicast address.
998 //
999 Status = Ip6JoinGroup (IpSb, IpIf, &Entry->Destination);
1000 if (EFI_ERROR (Status)) {
1001 FreePool (Entry);
1002 return Status;
1003 }
1004
1005 Entry->Signature = IP6_DAD_ENTRY_SIGNATURE;
1006 Entry->MaxTransmit = DadXmits->DupAddrDetectTransmits;
1007 Entry->Transmit = 0;
1008 Entry->Receive = 0;
1009 MaxDelayTick = IP6_MAX_RTR_SOLICITATION_DELAY / IP6_TIMER_INTERVAL_IN_MS;
1010 Entry->RetransTick = (MaxDelayTick * ((NET_RANDOM (NetRandomInitSeed ()) % 5) + 1)) / 5;
1011 Entry->AddressInfo = AddressInfo;
1012 Entry->Callback = Callback;
1013 Entry->Context = Context;
1014 InsertTailList (&IpIf->DupAddrDetectList, &Entry->Link);
1015
1016 if (Entry->MaxTransmit == 0) {
1017 //
1018 // DAD is disabled on this interface, immediately mark this DAD successful.
1019 //
1020 Ip6OnDADFinished (TRUE, IpIf, Entry);
1021 }
1022
1023 return EFI_SUCCESS;
1024 }
1025
1026 /**
1027 Search IP6_DAD_ENTRY from the Duplicate Address Detection List.
1028
1029 @param[in] IpSb The pointer to the IP6_SERVICE instance.
1030 @param[in] Target The address information which needs DAD performed .
1031 @param[out] Interface If not NULL, output the IP6 interface that configures
1032 the tentative address.
1033
1034 @return NULL if failed to find the matching DAD entry.
1035 Otherwise, point to the found DAD entry.
1036
1037 **/
1038 IP6_DAD_ENTRY *
1039 Ip6FindDADEntry (
1040 IN IP6_SERVICE *IpSb,
1041 IN EFI_IPv6_ADDRESS *Target,
1042 OUT IP6_INTERFACE **Interface OPTIONAL
1043 )
1044 {
1045 LIST_ENTRY *Entry;
1046 LIST_ENTRY *Entry2;
1047 IP6_INTERFACE *IpIf;
1048 IP6_DAD_ENTRY *DupAddrDetect;
1049 IP6_ADDRESS_INFO *AddrInfo;
1050
1051 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
1052 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
1053
1054 NET_LIST_FOR_EACH (Entry2, &IpIf->DupAddrDetectList) {
1055 DupAddrDetect = NET_LIST_USER_STRUCT_S (Entry2, IP6_DAD_ENTRY, Link, IP6_DAD_ENTRY_SIGNATURE);
1056 AddrInfo = DupAddrDetect->AddressInfo;
1057 if (EFI_IP6_EQUAL (&AddrInfo->Address, Target)) {
1058 if (Interface != NULL) {
1059 *Interface = IpIf;
1060 }
1061 return DupAddrDetect;
1062 }
1063 }
1064 }
1065
1066 return NULL;
1067 }
1068
1069 /**
1070 Generate router solicit message and send it out to Destination Address or
1071 All Router Link Local scope multicast address.
1072
1073 @param[in] IpSb The IP service to send the packet.
1074 @param[in] Interface If not NULL, points to the IP6 interface to send
1075 the packet.
1076 @param[in] SourceAddress If not NULL, the source address of the message.
1077 @param[in] DestinationAddress If not NULL, the destination address of the message.
1078 @param[in] SourceLinkAddress If not NULL, the MAC address of the source.
1079 A source link-layer address option will be appended
1080 to the message.
1081
1082 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1083 operation.
1084 @retval EFI_SUCCESS The router solicit message was successfully sent.
1085
1086 **/
1087 EFI_STATUS
1088 Ip6SendRouterSolicit (
1089 IN IP6_SERVICE *IpSb,
1090 IN IP6_INTERFACE *Interface OPTIONAL,
1091 IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL,
1092 IN EFI_IPv6_ADDRESS *DestinationAddress OPTIONAL,
1093 IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL
1094 )
1095 {
1096 NET_BUF *Packet;
1097 EFI_IP6_HEADER Head;
1098 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1099 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1100 UINT16 PayloadLen;
1101 IP6_INTERFACE *IpIf;
1102
1103 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1104
1105 IpIf = Interface;
1106 if (IpIf == NULL && IpSb->DefaultInterface != NULL) {
1107 IpIf = IpSb->DefaultInterface;
1108 }
1109
1110 //
1111 // Generate the packet to be sent
1112 //
1113
1114 PayloadLen = (UINT16) sizeof (IP6_ICMP_INFORMATION_HEAD);
1115 if (SourceLinkAddress != NULL) {
1116 PayloadLen += sizeof (IP6_ETHER_ADDR_OPTION);
1117 }
1118
1119 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1120 if (Packet == NULL) {
1121 return EFI_OUT_OF_RESOURCES;
1122 }
1123
1124 //
1125 // Create the basic IPv6 header.
1126 //
1127 Head.FlowLabelL = 0;
1128 Head.FlowLabelH = 0;
1129 Head.PayloadLength = HTONS (PayloadLen);
1130 Head.NextHeader = IP6_ICMP;
1131 Head.HopLimit = IP6_HOP_LIMIT;
1132
1133 if (SourceAddress != NULL) {
1134 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1135 } else {
1136 ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
1137 }
1138
1139
1140 if (DestinationAddress != NULL) {
1141 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1142 } else {
1143 Ip6SetToAllNodeMulticast (TRUE, IP6_LINK_LOCAL_SCOPE, &Head.DestinationAddress);
1144 }
1145
1146 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1147
1148 //
1149 // Fill in the ICMP header, and Source link-layer address if contained.
1150 //
1151
1152 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1153 ASSERT (IcmpHead != NULL);
1154 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1155 IcmpHead->Head.Type = ICMP_V6_ROUTER_SOLICIT;
1156 IcmpHead->Head.Code = 0;
1157
1158 LinkLayerOption = NULL;
1159 if (SourceLinkAddress != NULL) {
1160 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1161 Packet,
1162 sizeof (IP6_ETHER_ADDR_OPTION),
1163 FALSE
1164 );
1165 ASSERT (LinkLayerOption != NULL);
1166 LinkLayerOption->Type = Ip6OptionEtherSource;
1167 LinkLayerOption->Length = (UINT8) sizeof (IP6_ETHER_ADDR_OPTION);
1168 CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6);
1169 }
1170
1171 //
1172 // Transmit the packet
1173 //
1174 return Ip6Output (IpSb, IpIf, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1175 }
1176
1177 /**
1178 Generate a Neighbor Advertisement message and send it out to Destination Address.
1179
1180 @param[in] IpSb The IP service to send the packet.
1181 @param[in] SourceAddress The source address of the message.
1182 @param[in] DestinationAddress The destination address of the message.
1183 @param[in] TargetIp6Address The target address field in the Neighbor Solicitation
1184 message that prompted this advertisement.
1185 @param[in] TargetLinkAddress The MAC address for the target, i.e. the sender
1186 of the advertisement.
1187 @param[in] IsRouter If TRUE, indicates the sender is a router.
1188 @param[in] Override If TRUE, indicates the advertisement should override
1189 an existing cache entry and update the MAC address.
1190 @param[in] Solicited If TRUE, indicates the advertisement was sent
1191 in response to a Neighbor Solicitation from
1192 the Destination address.
1193
1194 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1195 operation.
1196 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1197
1198 **/
1199 EFI_STATUS
1200 Ip6SendNeighborAdvertise (
1201 IN IP6_SERVICE *IpSb,
1202 IN EFI_IPv6_ADDRESS *SourceAddress,
1203 IN EFI_IPv6_ADDRESS *DestinationAddress,
1204 IN EFI_IPv6_ADDRESS *TargetIp6Address,
1205 IN EFI_MAC_ADDRESS *TargetLinkAddress,
1206 IN BOOLEAN IsRouter,
1207 IN BOOLEAN Override,
1208 IN BOOLEAN Solicited
1209 )
1210 {
1211 NET_BUF *Packet;
1212 EFI_IP6_HEADER Head;
1213 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1214 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1215 EFI_IPv6_ADDRESS *Target;
1216 UINT16 PayloadLen;
1217
1218 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1219
1220 //
1221 // The Neighbor Advertisement message must include a Target link-layer address option
1222 // when responding to multicast solicitation and should include such option when
1223 // responding to unicast solicitation. It also must include such option as unsolicited
1224 // advertisement.
1225 //
1226 ASSERT (DestinationAddress != NULL && TargetIp6Address != NULL && TargetLinkAddress != NULL);
1227
1228 PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS) + sizeof (IP6_ETHER_ADDR_OPTION));
1229
1230 //
1231 // Generate the packet to be sent
1232 //
1233
1234 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1235 if (Packet == NULL) {
1236 return EFI_OUT_OF_RESOURCES;
1237 }
1238
1239 //
1240 // Create the basic IPv6 header.
1241 //
1242 Head.FlowLabelL = 0;
1243 Head.FlowLabelH = 0;
1244 Head.PayloadLength = HTONS (PayloadLen);
1245 Head.NextHeader = IP6_ICMP;
1246 Head.HopLimit = IP6_HOP_LIMIT;
1247
1248 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1249 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1250
1251 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1252
1253 //
1254 // Fill in the ICMP header, Target address, and Target link-layer address.
1255 // Set the Router flag, Solicited flag and Override flag.
1256 //
1257
1258 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1259 ASSERT (IcmpHead != NULL);
1260 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1261 IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_ADVERTISE;
1262 IcmpHead->Head.Code = 0;
1263
1264 if (IsRouter) {
1265 IcmpHead->Fourth |= IP6_IS_ROUTER_FLAG;
1266 }
1267
1268 if (Solicited) {
1269 IcmpHead->Fourth |= IP6_SOLICITED_FLAG;
1270 }
1271
1272 if (Override) {
1273 IcmpHead->Fourth |= IP6_OVERRIDE_FLAG;
1274 }
1275
1276 Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE);
1277 ASSERT (Target != NULL);
1278 IP6_COPY_ADDRESS (Target, TargetIp6Address);
1279
1280 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1281 Packet,
1282 sizeof (IP6_ETHER_ADDR_OPTION),
1283 FALSE
1284 );
1285 ASSERT (LinkLayerOption != NULL);
1286 LinkLayerOption->Type = Ip6OptionEtherTarget;
1287 LinkLayerOption->Length = 1;
1288 CopyMem (LinkLayerOption->EtherAddr, TargetLinkAddress, 6);
1289
1290 //
1291 // Transmit the packet
1292 //
1293 return Ip6Output (IpSb, NULL, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1294 }
1295
1296 /**
1297 Generate the Neighbor Solicitation message and send it to the Destination Address.
1298
1299 @param[in] IpSb The IP service to send the packet
1300 @param[in] SourceAddress The source address of the message.
1301 @param[in] DestinationAddress The destination address of the message.
1302 @param[in] TargetIp6Address The IP address of the target of the solicitation.
1303 It must not be a multicast address.
1304 @param[in] SourceLinkAddress The MAC address for the sender. If not NULL,
1305 a source link-layer address option will be appended
1306 to the message.
1307
1308 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1309 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1310 operation.
1311 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1312
1313 **/
1314 EFI_STATUS
1315 Ip6SendNeighborSolicit (
1316 IN IP6_SERVICE *IpSb,
1317 IN EFI_IPv6_ADDRESS *SourceAddress,
1318 IN EFI_IPv6_ADDRESS *DestinationAddress,
1319 IN EFI_IPv6_ADDRESS *TargetIp6Address,
1320 IN EFI_MAC_ADDRESS *SourceLinkAddress OPTIONAL
1321 )
1322 {
1323 NET_BUF *Packet;
1324 EFI_IP6_HEADER Head;
1325 IP6_ICMP_INFORMATION_HEAD *IcmpHead;
1326 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
1327 EFI_IPv6_ADDRESS *Target;
1328 BOOLEAN IsDAD;
1329 UINT16 PayloadLen;
1330 IP6_NEIGHBOR_ENTRY *Neighbor;
1331
1332 //
1333 // Check input parameters
1334 //
1335 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
1336 if (DestinationAddress == NULL || TargetIp6Address == NULL) {
1337 return EFI_INVALID_PARAMETER;
1338 }
1339
1340 IsDAD = FALSE;
1341
1342 if (SourceAddress == NULL || (SourceAddress != NULL && NetIp6IsUnspecifiedAddr (SourceAddress))) {
1343 IsDAD = TRUE;
1344 }
1345
1346 //
1347 // The Neighbor Solicitation message should include a source link-layer address option
1348 // if the solicitation is not sent by performing DAD - Duplicate Address Detection.
1349 // Otherwise must not include it.
1350 //
1351 PayloadLen = (UINT16) (sizeof (IP6_ICMP_INFORMATION_HEAD) + sizeof (EFI_IPv6_ADDRESS));
1352
1353 if (!IsDAD) {
1354 if (SourceLinkAddress == NULL) {
1355 return EFI_INVALID_PARAMETER;
1356 }
1357
1358 PayloadLen = (UINT16) (PayloadLen + sizeof (IP6_ETHER_ADDR_OPTION));
1359 }
1360
1361 //
1362 // Generate the packet to be sent
1363 //
1364
1365 Packet = NetbufAlloc (sizeof (EFI_IP6_HEADER) + (UINT32) PayloadLen);
1366 if (Packet == NULL) {
1367 return EFI_OUT_OF_RESOURCES;
1368 }
1369
1370 //
1371 // Create the basic IPv6 header
1372 //
1373 Head.FlowLabelL = 0;
1374 Head.FlowLabelH = 0;
1375 Head.PayloadLength = HTONS (PayloadLen);
1376 Head.NextHeader = IP6_ICMP;
1377 Head.HopLimit = IP6_HOP_LIMIT;
1378
1379 if (SourceAddress != NULL) {
1380 IP6_COPY_ADDRESS (&Head.SourceAddress, SourceAddress);
1381 } else {
1382 ZeroMem (&Head.SourceAddress, sizeof (EFI_IPv6_ADDRESS));
1383 }
1384
1385 IP6_COPY_ADDRESS (&Head.DestinationAddress, DestinationAddress);
1386
1387 NetbufReserve (Packet, sizeof (EFI_IP6_HEADER));
1388
1389 //
1390 // Fill in the ICMP header, Target address, and Source link-layer address.
1391 //
1392 IcmpHead = (IP6_ICMP_INFORMATION_HEAD *) NetbufAllocSpace (Packet, sizeof (IP6_ICMP_INFORMATION_HEAD), FALSE);
1393 ASSERT (IcmpHead != NULL);
1394 ZeroMem (IcmpHead, sizeof (IP6_ICMP_INFORMATION_HEAD));
1395 IcmpHead->Head.Type = ICMP_V6_NEIGHBOR_SOLICIT;
1396 IcmpHead->Head.Code = 0;
1397
1398 Target = (EFI_IPv6_ADDRESS *) NetbufAllocSpace (Packet, sizeof (EFI_IPv6_ADDRESS), FALSE);
1399 ASSERT (Target != NULL);
1400 IP6_COPY_ADDRESS (Target, TargetIp6Address);
1401
1402 LinkLayerOption = NULL;
1403 if (!IsDAD) {
1404
1405 //
1406 // Fill in the source link-layer address option
1407 //
1408 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) NetbufAllocSpace (
1409 Packet,
1410 sizeof (IP6_ETHER_ADDR_OPTION),
1411 FALSE
1412 );
1413 ASSERT (LinkLayerOption != NULL);
1414 LinkLayerOption->Type = Ip6OptionEtherSource;
1415 LinkLayerOption->Length = 1;
1416 CopyMem (LinkLayerOption->EtherAddr, SourceLinkAddress, 6);
1417 }
1418
1419 //
1420 // Create a Neighbor Cache entry in the INCOMPLETE state when performing
1421 // address resolution.
1422 //
1423 if (!IsDAD && Ip6IsSNMulticastAddr (DestinationAddress)) {
1424 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
1425 if (Neighbor == NULL) {
1426 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, NULL);
1427 ASSERT (Neighbor != NULL);
1428 }
1429 }
1430
1431 //
1432 // Transmit the packet
1433 //
1434 return Ip6Output (IpSb, IpSb->DefaultInterface, NULL, Packet, &Head, NULL, 0, Ip6SysPacketSent, NULL);
1435 }
1436
1437 /**
1438 Process the Neighbor Solicitation message. The message may be sent for Duplicate
1439 Address Detection or Address Resolution.
1440
1441 @param[in] IpSb The IP service that received the packet.
1442 @param[in] Head The IP head of the message.
1443 @param[in] Packet The content of the message with IP head removed.
1444
1445 @retval EFI_SUCCESS The packet processed successfully.
1446 @retval EFI_INVALID_PARAMETER The packet is invalid.
1447 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1448 @retval Others Failed to process the packet.
1449
1450 **/
1451 EFI_STATUS
1452 Ip6ProcessNeighborSolicit (
1453 IN IP6_SERVICE *IpSb,
1454 IN EFI_IP6_HEADER *Head,
1455 IN NET_BUF *Packet
1456 )
1457 {
1458 IP6_ICMP_INFORMATION_HEAD Icmp;
1459 EFI_IPv6_ADDRESS Target;
1460 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1461 BOOLEAN IsDAD;
1462 BOOLEAN IsUnicast;
1463 BOOLEAN IsMaintained;
1464 IP6_DAD_ENTRY *DupAddrDetect;
1465 IP6_INTERFACE *IpIf;
1466 IP6_NEIGHBOR_ENTRY *Neighbor;
1467 BOOLEAN Solicited;
1468 BOOLEAN UpdateCache;
1469 EFI_IPv6_ADDRESS Dest;
1470 UINT16 OptionLen;
1471 UINT8 *Option;
1472 BOOLEAN Provided;
1473 EFI_STATUS Status;
1474 VOID *MacAddress;
1475
1476 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1477 NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr);
1478
1479 //
1480 // Perform Message Validation:
1481 // The IP Hop Limit field has a value of 255, i.e., the packet
1482 // could not possibly have been forwarded by a router.
1483 // ICMP Code is 0.
1484 // Target Address is not a multicast address.
1485 //
1486 Status = EFI_INVALID_PARAMETER;
1487
1488 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) {
1489 goto Exit;
1490 }
1491
1492 //
1493 // ICMP length is 24 or more octets.
1494 //
1495 OptionLen = 0;
1496 if (Head->PayloadLength < IP6_ND_LENGTH) {
1497 goto Exit;
1498 } else {
1499 OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH);
1500 Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL);
1501
1502 //
1503 // All included options should have a length that is greater than zero.
1504 //
1505 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1506 goto Exit;
1507 }
1508 }
1509
1510 IsDAD = NetIp6IsUnspecifiedAddr (&Head->SourceAddress);
1511 IsUnicast = (BOOLEAN) !Ip6IsSNMulticastAddr (&Head->DestinationAddress);
1512 IsMaintained = Ip6IsOneOfSetAddress (IpSb, &Target, &IpIf, NULL);
1513
1514 Provided = FALSE;
1515 if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) {
1516 NetbufCopy (
1517 Packet,
1518 IP6_ND_LENGTH,
1519 sizeof (IP6_ETHER_ADDR_OPTION),
1520 (UINT8 *) &LinkLayerOption
1521 );
1522 //
1523 // The solicitation for neighbor discovery should include a source link-layer
1524 // address option. If the option is not recognized, silently ignore it.
1525 //
1526 if (LinkLayerOption.Type == Ip6OptionEtherSource) {
1527 if (IsDAD) {
1528 //
1529 // If the IP source address is the unspecified address, the source
1530 // link-layer address option must not be included in the message.
1531 //
1532 goto Exit;
1533 }
1534
1535 Provided = TRUE;
1536 }
1537 }
1538
1539 //
1540 // If the IP source address is the unspecified address, the IP
1541 // destination address is a solicited-node multicast address.
1542 //
1543 if (IsDAD && IsUnicast) {
1544 goto Exit;
1545 }
1546
1547 //
1548 // If the target address is tentative, and the source address is a unicast address,
1549 // the solicitation's sender is performing address resolution on the target;
1550 // the solicitation should be silently ignored.
1551 //
1552 if (!IsDAD && !IsMaintained) {
1553 goto Exit;
1554 }
1555
1556 //
1557 // If received unicast neighbor solicitation but destination is not this node,
1558 // drop the packet.
1559 //
1560 if (IsUnicast && !IsMaintained) {
1561 goto Exit;
1562 }
1563
1564 //
1565 // In DAD, when target address is a tentative address,
1566 // process the received neighbor solicitation message but not send out response.
1567 //
1568 if (IsDAD && !IsMaintained) {
1569 DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf);
1570 if (DupAddrDetect != NULL) {
1571 if (DupAddrDetect->Transmit == 0) {
1572 //
1573 // The NS is from another node to performing DAD on the same address since
1574 // we haven't send out any NS yet. Fail DAD for the tentative address.
1575 //
1576 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1577 Status = EFI_ICMP_ERROR;
1578 goto Exit;
1579 }
1580
1581 //
1582 // Check the MAC address of the incoming packet.
1583 //
1584 if (IpSb->RecvRequest.MnpToken.Packet.RxData == NULL) {
1585 goto Exit;
1586 }
1587
1588 MacAddress = IpSb->RecvRequest.MnpToken.Packet.RxData->SourceAddress;
1589 if (MacAddress != NULL) {
1590 if (CompareMem (
1591 MacAddress,
1592 &IpSb->SnpMode.CurrentAddress,
1593 IpSb->SnpMode.HwAddressSize
1594 ) != 0) {
1595 //
1596 // The NS is from another node to performing DAD on the same address.
1597 // Fail DAD for the tentative address.
1598 //
1599 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1600 Status = EFI_ICMP_ERROR;
1601 } else {
1602 //
1603 // The below layer loopback the NS we sent. Record it and wait for more.
1604 //
1605 DupAddrDetect->Receive++;
1606 Status = EFI_SUCCESS;
1607 }
1608 }
1609 }
1610 goto Exit;
1611 }
1612
1613 //
1614 // If the solicitation does not contain a link-layer address, DO NOT create or
1615 // update the neighbor cache entries.
1616 //
1617 if (Provided) {
1618 Neighbor = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
1619 UpdateCache = FALSE;
1620
1621 if (Neighbor == NULL) {
1622 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, &Head->SourceAddress, NULL);
1623 if (Neighbor == NULL) {
1624 Status = EFI_OUT_OF_RESOURCES;
1625 goto Exit;
1626 }
1627 UpdateCache = TRUE;
1628 } else {
1629 if (CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6) != 0) {
1630 UpdateCache = TRUE;
1631 }
1632 }
1633
1634 if (UpdateCache) {
1635 Neighbor->State = EfiNeighborStale;
1636 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1637 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1638 //
1639 // Send queued packets if exist.
1640 //
1641 Neighbor->CallBack ((VOID *) Neighbor);
1642 }
1643 }
1644
1645 //
1646 // Sends a Neighbor Advertisement as response.
1647 // Set the Router flag to zero since the node is a host.
1648 // If the source address of the solicitation is unspeicifed, and target address
1649 // is one of the maintained address, reply a unsolicited multicast advertisement.
1650 //
1651 if (IsDAD && IsMaintained) {
1652 Solicited = FALSE;
1653 Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &Dest);
1654 } else {
1655 Solicited = TRUE;
1656 IP6_COPY_ADDRESS (&Dest, &Head->SourceAddress);
1657 }
1658
1659 Status = Ip6SendNeighborAdvertise (
1660 IpSb,
1661 &Target,
1662 &Dest,
1663 &Target,
1664 &IpSb->SnpMode.CurrentAddress,
1665 FALSE,
1666 TRUE,
1667 Solicited
1668 );
1669 Exit:
1670 NetbufFree (Packet);
1671 return Status;
1672 }
1673
1674 /**
1675 Process the Neighbor Advertisement message.
1676
1677 @param[in] IpSb The IP service that received the packet.
1678 @param[in] Head The IP head of the message.
1679 @param[in] Packet The content of the message with IP head removed.
1680
1681 @retval EFI_SUCCESS The packet processed successfully.
1682 @retval EFI_INVALID_PARAMETER The packet is invalid.
1683 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1684 @retval Others Failed to process the packet.
1685
1686 **/
1687 EFI_STATUS
1688 Ip6ProcessNeighborAdvertise (
1689 IN IP6_SERVICE *IpSb,
1690 IN EFI_IP6_HEADER *Head,
1691 IN NET_BUF *Packet
1692 )
1693 {
1694 IP6_ICMP_INFORMATION_HEAD Icmp;
1695 EFI_IPv6_ADDRESS Target;
1696 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1697 BOOLEAN Provided;
1698 INTN Compare;
1699 IP6_NEIGHBOR_ENTRY *Neighbor;
1700 IP6_DEFAULT_ROUTER *DefaultRouter;
1701 BOOLEAN Solicited;
1702 BOOLEAN IsRouter;
1703 BOOLEAN Override;
1704 IP6_DAD_ENTRY *DupAddrDetect;
1705 IP6_INTERFACE *IpIf;
1706 UINT16 OptionLen;
1707 UINT8 *Option;
1708 EFI_STATUS Status;
1709
1710 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1711 NetbufCopy (Packet, sizeof (Icmp), sizeof (Target), Target.Addr);
1712
1713 //
1714 // Validate the incoming Neighbor Advertisement
1715 //
1716 Status = EFI_INVALID_PARAMETER;
1717 //
1718 // The IP Hop Limit field has a value of 255, i.e., the packet
1719 // could not possibly have been forwarded by a router.
1720 // ICMP Code is 0.
1721 // Target Address is not a multicast address.
1722 //
1723 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 || !NetIp6IsValidUnicast (&Target)) {
1724 goto Exit;
1725 }
1726
1727 //
1728 // ICMP length is 24 or more octets.
1729 //
1730 Provided = FALSE;
1731 OptionLen = 0;
1732 if (Head->PayloadLength < IP6_ND_LENGTH) {
1733 goto Exit;
1734 } else {
1735 OptionLen = (UINT16) (Head->PayloadLength - IP6_ND_LENGTH);
1736 Option = NetbufGetByte (Packet, IP6_ND_LENGTH, NULL);
1737
1738 //
1739 // All included options should have a length that is greater than zero.
1740 //
1741 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1742 goto Exit;
1743 }
1744 }
1745
1746 //
1747 // If the IP destination address is a multicast address, Solicited Flag is ZERO.
1748 //
1749 Solicited = FALSE;
1750 if ((Icmp.Fourth & IP6_SOLICITED_FLAG) == IP6_SOLICITED_FLAG) {
1751 Solicited = TRUE;
1752 }
1753 if (IP6_IS_MULTICAST (&Head->DestinationAddress) && Solicited) {
1754 goto Exit;
1755 }
1756
1757 //
1758 // DAD - Check whether the Target is one of our tentative address.
1759 //
1760 DupAddrDetect = Ip6FindDADEntry (IpSb, &Target, &IpIf);
1761 if (DupAddrDetect != NULL) {
1762 //
1763 // DAD fails, some other node is using this address.
1764 //
1765 NetbufFree (Packet);
1766 Ip6OnDADFinished (FALSE, IpIf, DupAddrDetect);
1767 return EFI_ICMP_ERROR;
1768 }
1769
1770 //
1771 // Search the Neighbor Cache for the target's entry. If no entry exists,
1772 // the advertisement should be silently discarded.
1773 //
1774 Neighbor = Ip6FindNeighborEntry (IpSb, &Target);
1775 if (Neighbor == NULL) {
1776 goto Exit;
1777 }
1778
1779 //
1780 // Get IsRouter Flag and Override Flag
1781 //
1782 IsRouter = FALSE;
1783 Override = FALSE;
1784 if ((Icmp.Fourth & IP6_IS_ROUTER_FLAG) == IP6_IS_ROUTER_FLAG) {
1785 IsRouter = TRUE;
1786 }
1787 if ((Icmp.Fourth & IP6_OVERRIDE_FLAG) == IP6_OVERRIDE_FLAG) {
1788 Override = TRUE;
1789 }
1790
1791 //
1792 // Check whether link layer option is included.
1793 //
1794 if (OptionLen >= sizeof (IP6_ETHER_ADDR_OPTION)) {
1795 NetbufCopy (
1796 Packet,
1797 IP6_ND_LENGTH,
1798 sizeof (IP6_ETHER_ADDR_OPTION),
1799 (UINT8 *) &LinkLayerOption
1800 );
1801
1802 if (LinkLayerOption.Type == Ip6OptionEtherTarget) {
1803 Provided = TRUE;
1804 }
1805 }
1806
1807 Compare = 0;
1808 if (Provided) {
1809 Compare = CompareMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1810 }
1811
1812 if (!Neighbor->IsRouter && IsRouter) {
1813 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target);
1814 if (DefaultRouter != NULL) {
1815 DefaultRouter->NeighborCache = Neighbor;
1816 }
1817 }
1818
1819 if (Neighbor->State == EfiNeighborInComplete) {
1820 //
1821 // If the target's Neighbor Cache entry is in INCOMPLETE state and no
1822 // Target Link-Layer address option is included while link layer has
1823 // address, the message should be silently discarded.
1824 //
1825 if (!Provided) {
1826 goto Exit;
1827 }
1828 //
1829 // Update the Neighbor Cache
1830 //
1831 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1832 if (Solicited) {
1833 Neighbor->State = EfiNeighborReachable;
1834 Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime);
1835 } else {
1836 Neighbor->State = EfiNeighborStale;
1837 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1838 //
1839 // Send any packets queued for the neighbor awaiting address resolution.
1840 //
1841 Neighbor->CallBack ((VOID *) Neighbor);
1842 }
1843
1844 Neighbor->IsRouter = IsRouter;
1845
1846 } else {
1847 if (!Override && Compare != 0) {
1848 //
1849 // When the Override Flag is clear and supplied link-layer address differs from
1850 // that in the cache, if the state of the entry is not REACHABLE, ignore the
1851 // message. Otherwise set it to STALE but do not update the entry in any
1852 // other way.
1853 //
1854 if (Neighbor->State == EfiNeighborReachable) {
1855 Neighbor->State = EfiNeighborStale;
1856 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1857 }
1858 } else {
1859 if (Compare != 0) {
1860 CopyMem (Neighbor->LinkAddress.Addr, LinkLayerOption.EtherAddr, 6);
1861 }
1862 //
1863 // Update the entry's state
1864 //
1865 if (Solicited) {
1866 Neighbor->State = EfiNeighborReachable;
1867 Neighbor->Ticks = IP6_GET_TICKS (IpSb->ReachableTime);
1868 } else {
1869 if (Compare != 0) {
1870 Neighbor->State = EfiNeighborStale;
1871 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
1872 }
1873 }
1874
1875 //
1876 // When IsRouter is changed from TRUE to FALSE, remove the router from the
1877 // Default Router List and remove the Destination Cache entries for all destinations
1878 // using the neighbor as a router.
1879 //
1880 if (Neighbor->IsRouter && !IsRouter) {
1881 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Target);
1882 if (DefaultRouter != NULL) {
1883 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
1884 }
1885 }
1886
1887 Neighbor->IsRouter = IsRouter;
1888 }
1889 }
1890
1891 if (Neighbor->State == EfiNeighborReachable) {
1892 Neighbor->CallBack ((VOID *) Neighbor);
1893 }
1894
1895 Status = EFI_SUCCESS;
1896
1897 Exit:
1898 NetbufFree (Packet);
1899 return Status;
1900 }
1901
1902 /**
1903 Process the Router Advertisement message according to RFC4861.
1904
1905 @param[in] IpSb The IP service that received the packet.
1906 @param[in] Head The IP head of the message.
1907 @param[in] Packet The content of the message with the IP head removed.
1908
1909 @retval EFI_SUCCESS The packet processed successfully.
1910 @retval EFI_INVALID_PARAMETER The packet is invalid.
1911 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1912 operation.
1913 @retval Others Failed to process the packet.
1914
1915 **/
1916 EFI_STATUS
1917 Ip6ProcessRouterAdvertise (
1918 IN IP6_SERVICE *IpSb,
1919 IN EFI_IP6_HEADER *Head,
1920 IN NET_BUF *Packet
1921 )
1922 {
1923 IP6_ICMP_INFORMATION_HEAD Icmp;
1924 UINT32 ReachableTime;
1925 UINT32 RetransTimer;
1926 UINT16 RouterLifetime;
1927 UINT16 Offset;
1928 UINT8 Type;
1929 UINT8 Length;
1930 IP6_ETHER_ADDR_OPTION LinkLayerOption;
1931 UINT32 Fourth;
1932 UINT8 CurHopLimit;
1933 BOOLEAN Mflag;
1934 BOOLEAN Oflag;
1935 IP6_DEFAULT_ROUTER *DefaultRouter;
1936 IP6_NEIGHBOR_ENTRY *NeighborCache;
1937 EFI_MAC_ADDRESS LinkLayerAddress;
1938 IP6_MTU_OPTION MTUOption;
1939 IP6_PREFIX_INFO_OPTION PrefixOption;
1940 IP6_PREFIX_LIST_ENTRY *PrefixList;
1941 BOOLEAN OnLink;
1942 BOOLEAN Autonomous;
1943 EFI_IPv6_ADDRESS StatelessAddress;
1944 EFI_STATUS Status;
1945 UINT16 OptionLen;
1946 UINT8 *Option;
1947 INTN Result;
1948
1949 Status = EFI_INVALID_PARAMETER;
1950
1951 if (IpSb->Ip6ConfigInstance.Policy != Ip6ConfigPolicyAutomatic) {
1952 //
1953 // Skip the process below as it's not required under the current policy.
1954 //
1955 goto Exit;
1956 }
1957
1958 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);
1959
1960 //
1961 // Validate the incoming Router Advertisement
1962 //
1963
1964 //
1965 // The IP source address must be a link-local address
1966 //
1967 if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
1968 goto Exit;
1969 }
1970 //
1971 // The IP Hop Limit field has a value of 255, i.e. the packet
1972 // could not possibly have been forwarded by a router.
1973 // ICMP Code is 0.
1974 // ICMP length (derived from the IP length) is 16 or more octets.
1975 //
1976 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp.Head.Code != 0 ||
1977 Head->PayloadLength < IP6_RA_LENGTH) {
1978 goto Exit;
1979 }
1980
1981 //
1982 // All included options have a length that is greater than zero.
1983 //
1984 OptionLen = (UINT16) (Head->PayloadLength - IP6_RA_LENGTH);
1985 Option = NetbufGetByte (Packet, IP6_RA_LENGTH, NULL);
1986
1987 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
1988 goto Exit;
1989 }
1990
1991 //
1992 // Process Fourth field.
1993 // In Router Advertisement, Fourth is composed of CurHopLimit (8bit), M flag, O flag,
1994 // and Router Lifetime (16 bit).
1995 //
1996
1997 Fourth = NTOHL (Icmp.Fourth);
1998 CopyMem (&RouterLifetime, &Fourth, sizeof (UINT16));
1999
2000 //
2001 // If the source address already in the default router list, update it.
2002 // Otherwise create a new entry.
2003 // A Lifetime of zero indicates that the router is not a default router.
2004 //
2005 DefaultRouter = Ip6FindDefaultRouter (IpSb, &Head->SourceAddress);
2006 if (DefaultRouter == NULL) {
2007 if (RouterLifetime != 0) {
2008 DefaultRouter = Ip6CreateDefaultRouter (IpSb, &Head->SourceAddress, RouterLifetime);
2009 if (DefaultRouter == NULL) {
2010 Status = EFI_OUT_OF_RESOURCES;
2011 goto Exit;
2012 }
2013 }
2014 } else {
2015 if (RouterLifetime != 0) {
2016 DefaultRouter->Lifetime = RouterLifetime;
2017 //
2018 // Check the corresponding neighbor cache entry here.
2019 //
2020 if (DefaultRouter->NeighborCache == NULL) {
2021 DefaultRouter->NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
2022 }
2023 } else {
2024 //
2025 // If the address is in the host's default router list and the router lifetime is zero,
2026 // immediately time-out the entry.
2027 //
2028 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
2029 }
2030 }
2031
2032 CurHopLimit = *((UINT8 *) &Fourth + 3);
2033 if (CurHopLimit != 0) {
2034 IpSb->CurHopLimit = CurHopLimit;
2035 }
2036
2037 Mflag = FALSE;
2038 Oflag = FALSE;
2039 if ((*((UINT8 *) &Fourth + 2) & IP6_M_ADDR_CONFIG_FLAG) == IP6_M_ADDR_CONFIG_FLAG) {
2040 Mflag = TRUE;
2041 } else {
2042 if ((*((UINT8 *) &Fourth + 2) & IP6_O_CONFIG_FLAG) == IP6_O_CONFIG_FLAG) {
2043 Oflag = TRUE;
2044 }
2045 }
2046
2047 if (Mflag || Oflag) {
2048 //
2049 // Use Ip6Config to get available addresses or other configuration from DHCP.
2050 //
2051 Ip6ConfigStartStatefulAutoConfig (&IpSb->Ip6ConfigInstance, Oflag);
2052 }
2053
2054 //
2055 // Process Reachable Time and Retrans Timer fields.
2056 //
2057 NetbufCopy (Packet, sizeof (Icmp), sizeof (UINT32), (UINT8 *) &ReachableTime);
2058 NetbufCopy (Packet, sizeof (Icmp) + sizeof (UINT32), sizeof (UINT32), (UINT8 *) &RetransTimer);
2059 ReachableTime = NTOHL (ReachableTime);
2060 RetransTimer = NTOHL (RetransTimer);
2061
2062 if (ReachableTime != 0 && ReachableTime != IpSb->BaseReachableTime) {
2063 //
2064 // If new value is not unspecified and differs from the previous one, record it
2065 // in BaseReachableTime and recompute a ReachableTime.
2066 //
2067 IpSb->BaseReachableTime = ReachableTime;
2068 Ip6UpdateReachableTime (IpSb);
2069 }
2070
2071 if (RetransTimer != 0) {
2072 IpSb->RetransTimer = RetransTimer;
2073 }
2074
2075 //
2076 // IsRouter flag must be set to TRUE if corresponding neighbor cache entry exists.
2077 //
2078 NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->SourceAddress);
2079 if (NeighborCache != NULL) {
2080 NeighborCache->IsRouter = TRUE;
2081 }
2082
2083 //
2084 // If an valid router advertisment is received, stops router solicitation.
2085 //
2086 IpSb->RouterAdvertiseReceived = TRUE;
2087
2088 //
2089 // The only defined options that may appear are the Source
2090 // Link-Layer Address, Prefix information and MTU options.
2091 // All included options have a length that is greater than zero.
2092 //
2093 Offset = 16;
2094 while (Offset < Head->PayloadLength) {
2095 NetbufCopy (Packet, Offset, sizeof (UINT8), &Type);
2096 switch (Type) {
2097 case Ip6OptionEtherSource:
2098 //
2099 // Update the neighbor cache
2100 //
2101 NetbufCopy (Packet, Offset, sizeof (IP6_ETHER_ADDR_OPTION), (UINT8 *) &LinkLayerOption);
2102 if (LinkLayerOption.Length <= 0) {
2103 goto Exit;
2104 }
2105
2106 ZeroMem (&LinkLayerAddress, sizeof (EFI_MAC_ADDRESS));
2107 CopyMem (&LinkLayerAddress, LinkLayerOption.EtherAddr, 6);
2108
2109 if (NeighborCache == NULL) {
2110 NeighborCache = Ip6CreateNeighborEntry (
2111 IpSb,
2112 Ip6OnArpResolved,
2113 &Head->SourceAddress,
2114 &LinkLayerAddress
2115 );
2116 if (NeighborCache == NULL) {
2117 Status = EFI_OUT_OF_RESOURCES;
2118 goto Exit;
2119 }
2120 NeighborCache->IsRouter = TRUE;
2121 NeighborCache->State = EfiNeighborStale;
2122 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2123 } else {
2124 Result = CompareMem (&LinkLayerAddress, &NeighborCache->LinkAddress, 6);
2125
2126 //
2127 // If the link-local address is the same as that already in the cache,
2128 // the cache entry's state remains unchanged. Otherwise update the
2129 // reachability state to STALE.
2130 //
2131 if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) {
2132 CopyMem (&NeighborCache->LinkAddress, &LinkLayerAddress, 6);
2133
2134 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2135
2136 if (NeighborCache->State == EfiNeighborInComplete) {
2137 //
2138 // Send queued packets if exist.
2139 //
2140 NeighborCache->State = EfiNeighborStale;
2141 NeighborCache->CallBack ((VOID *) NeighborCache);
2142 } else {
2143 NeighborCache->State = EfiNeighborStale;
2144 }
2145 }
2146 }
2147
2148 Offset = (UINT16) (Offset + (UINT16) LinkLayerOption.Length * 8);
2149 break;
2150 case Ip6OptionPrefixInfo:
2151 NetbufCopy (Packet, Offset, sizeof (IP6_PREFIX_INFO_OPTION), (UINT8 *) &PrefixOption);
2152 if (PrefixOption.Length != 4) {
2153 goto Exit;
2154 }
2155 PrefixOption.ValidLifetime = NTOHL (PrefixOption.ValidLifetime);
2156 PrefixOption.PreferredLifetime = NTOHL (PrefixOption.PreferredLifetime);
2157
2158 //
2159 // Get L and A flag, recorded in the lower 2 bits of Reserved1
2160 //
2161 OnLink = FALSE;
2162 if ((PrefixOption.Reserved1 & IP6_ON_LINK_FLAG) == IP6_ON_LINK_FLAG) {
2163 OnLink = TRUE;
2164 }
2165 Autonomous = FALSE;
2166 if ((PrefixOption.Reserved1 & IP6_AUTO_CONFIG_FLAG) == IP6_AUTO_CONFIG_FLAG) {
2167 Autonomous = TRUE;
2168 }
2169
2170 //
2171 // If the prefix is the link-local prefix, silently ignore the prefix option.
2172 //
2173 if (PrefixOption.PrefixLength == IP6_LINK_LOCAL_PREFIX_LENGTH &&
2174 NetIp6IsLinkLocalAddr (&PrefixOption.Prefix)
2175 ) {
2176 Offset += sizeof (IP6_PREFIX_INFO_OPTION);
2177 break;
2178 }
2179 //
2180 // Do following if on-link flag is set according to RFC4861.
2181 //
2182 if (OnLink) {
2183 PrefixList = Ip6FindPrefixListEntry (
2184 IpSb,
2185 TRUE,
2186 PrefixOption.PrefixLength,
2187 &PrefixOption.Prefix
2188 );
2189 //
2190 // Create a new entry for the prefix, if the ValidLifetime is zero,
2191 // silently ignore the prefix option.
2192 //
2193 if (PrefixList == NULL && PrefixOption.ValidLifetime != 0) {
2194 PrefixList = Ip6CreatePrefixListEntry (
2195 IpSb,
2196 TRUE,
2197 PrefixOption.ValidLifetime,
2198 PrefixOption.PreferredLifetime,
2199 PrefixOption.PrefixLength,
2200 &PrefixOption.Prefix
2201 );
2202 if (PrefixList == NULL) {
2203 Status = EFI_OUT_OF_RESOURCES;
2204 goto Exit;
2205 }
2206 } else if (PrefixList != NULL) {
2207 if (PrefixOption.ValidLifetime != 0) {
2208 PrefixList->ValidLifetime = PrefixOption.ValidLifetime;
2209 } else {
2210 //
2211 // If the prefix exists and incoming ValidLifetime is zero, immediately
2212 // remove the prefix.
2213 Ip6DestroyPrefixListEntry (IpSb, PrefixList, OnLink, TRUE);
2214 }
2215 }
2216 }
2217
2218 //
2219 // Do following if Autonomous flag is set according to RFC4862.
2220 //
2221 if (Autonomous && PrefixOption.PreferredLifetime <= PrefixOption.ValidLifetime) {
2222 PrefixList = Ip6FindPrefixListEntry (
2223 IpSb,
2224 FALSE,
2225 PrefixOption.PrefixLength,
2226 &PrefixOption.Prefix
2227 );
2228 //
2229 // Create a new entry for the prefix, and form an address by prefix + interface id
2230 // If the sum of the prefix length and interface identifier length
2231 // does not equal 128 bits, the Prefix Information option MUST be ignored.
2232 //
2233 if (PrefixList == NULL &&
2234 PrefixOption.ValidLifetime != 0 &&
2235 PrefixOption.PrefixLength + IpSb->InterfaceIdLen * 8 == 128
2236 ) {
2237 //
2238 // Form the address in network order.
2239 //
2240 CopyMem (&StatelessAddress, &PrefixOption.Prefix, sizeof (UINT64));
2241 CopyMem (&StatelessAddress.Addr[8], IpSb->InterfaceId, sizeof (UINT64));
2242
2243 //
2244 // If the address is not yet in the assigned address list, adds it into.
2245 //
2246 if (!Ip6IsOneOfSetAddress (IpSb, &StatelessAddress, NULL, NULL)) {
2247 //
2248 // And also not in the DAD process, check its uniqeness firstly.
2249 //
2250 if (Ip6FindDADEntry (IpSb, &StatelessAddress, NULL) == NULL) {
2251 Status = Ip6SetAddress (
2252 IpSb->DefaultInterface,
2253 &StatelessAddress,
2254 FALSE,
2255 PrefixOption.PrefixLength,
2256 PrefixOption.ValidLifetime,
2257 PrefixOption.PreferredLifetime,
2258 NULL,
2259 NULL
2260 );
2261 if (EFI_ERROR (Status)) {
2262 goto Exit;
2263 }
2264 }
2265 }
2266
2267 //
2268 // Adds the prefix option to stateless prefix option list.
2269 //
2270 PrefixList = Ip6CreatePrefixListEntry (
2271 IpSb,
2272 FALSE,
2273 PrefixOption.ValidLifetime,
2274 PrefixOption.PreferredLifetime,
2275 PrefixOption.PrefixLength,
2276 &PrefixOption.Prefix
2277 );
2278 if (PrefixList == NULL) {
2279 Status = EFI_OUT_OF_RESOURCES;
2280 goto Exit;
2281 }
2282 } else if (PrefixList != NULL) {
2283
2284 //
2285 // Reset the preferred lifetime of the address if the advertised prefix exists.
2286 // Perform specific action to valid lifetime together.
2287 //
2288 PrefixList->PreferredLifetime = PrefixOption.PreferredLifetime;
2289 if ((PrefixOption.ValidLifetime > 7200) ||
2290 (PrefixOption.ValidLifetime > PrefixList->ValidLifetime)) {
2291 //
2292 // If the received Valid Lifetime is greater than 2 hours or
2293 // greater than RemainingLifetime, set the valid lifetime of the
2294 // corresponding address to the advertised Valid Lifetime.
2295 //
2296 PrefixList->ValidLifetime = PrefixOption.ValidLifetime;
2297
2298 } else if (PrefixList->ValidLifetime <= 7200) {
2299 //
2300 // If RemainingLifetime is less than or equls to 2 hours, ignore the
2301 // Prefix Information option with regards to the valid lifetime.
2302 // TODO: If this option has been authenticated, set the valid lifetime.
2303 //
2304 } else {
2305 //
2306 // Otherwise, reset the valid lifetime of the corresponding
2307 // address to 2 hours.
2308 //
2309 PrefixList->ValidLifetime = 7200;
2310 }
2311 }
2312 }
2313
2314 Offset += sizeof (IP6_PREFIX_INFO_OPTION);
2315 break;
2316 case Ip6OptionMtu:
2317 NetbufCopy (Packet, Offset, sizeof (IP6_MTU_OPTION), (UINT8 *) &MTUOption);
2318 if (MTUOption.Length != 1) {
2319 goto Exit;
2320 }
2321
2322 //
2323 // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order
2324 // to omit implementation of Path MTU Discovery. Thus ignore the MTU option
2325 // in Router Advertisement.
2326 //
2327
2328 Offset += sizeof (IP6_MTU_OPTION);
2329 break;
2330 default:
2331 //
2332 // Silently ignore unrecognized options
2333 //
2334 NetbufCopy (Packet, Offset + sizeof (UINT8), sizeof (UINT8), &Length);
2335 if (Length <= 0) {
2336 goto Exit;
2337 }
2338
2339 Offset = (UINT16) (Offset + (UINT16) Length * 8);
2340 break;
2341 }
2342 }
2343
2344 Status = EFI_SUCCESS;
2345
2346 Exit:
2347 NetbufFree (Packet);
2348 return Status;
2349 }
2350
2351 /**
2352 Process the ICMPv6 redirect message. Find the instance, then update
2353 its route cache.
2354
2355 @param[in] IpSb The IP6 service binding instance that received
2356 the packet.
2357 @param[in] Head The IP head of the received ICMPv6 packet.
2358 @param[in] Packet The content of the ICMPv6 redirect packet with
2359 the IP head removed.
2360
2361 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2362 @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the
2363 operation.
2364 @retval EFI_SUCCESS Successfully updated the route caches.
2365
2366 **/
2367 EFI_STATUS
2368 Ip6ProcessRedirect (
2369 IN IP6_SERVICE *IpSb,
2370 IN EFI_IP6_HEADER *Head,
2371 IN NET_BUF *Packet
2372 )
2373 {
2374 IP6_ICMP_INFORMATION_HEAD *Icmp;
2375 EFI_IPv6_ADDRESS *Target;
2376 EFI_IPv6_ADDRESS *IcmpDest;
2377 UINT8 *Option;
2378 UINT16 OptionLen;
2379 IP6_ROUTE_ENTRY *RouteEntry;
2380 IP6_ROUTE_CACHE_ENTRY *RouteCache;
2381 IP6_NEIGHBOR_ENTRY *NeighborCache;
2382 INT32 Length;
2383 UINT8 OptLen;
2384 IP6_ETHER_ADDR_OPTION *LinkLayerOption;
2385 EFI_MAC_ADDRESS Mac;
2386 UINT32 Index;
2387 BOOLEAN IsRouter;
2388 EFI_STATUS Status;
2389 INTN Result;
2390
2391 Status = EFI_INVALID_PARAMETER;
2392
2393 Icmp = (IP6_ICMP_INFORMATION_HEAD *) NetbufGetByte (Packet, 0, NULL);
2394 if (Icmp == NULL) {
2395 goto Exit;
2396 }
2397
2398 //
2399 // Validate the incoming Redirect message
2400 //
2401
2402 //
2403 // The IP Hop Limit field has a value of 255, i.e. the packet
2404 // could not possibly have been forwarded by a router.
2405 // ICMP Code is 0.
2406 // ICMP length (derived from the IP length) is 40 or more octets.
2407 //
2408 if (Head->HopLimit != IP6_HOP_LIMIT || Icmp->Head.Code != 0 ||
2409 Head->PayloadLength < IP6_REDITECT_LENGTH) {
2410 goto Exit;
2411 }
2412
2413 //
2414 // The IP source address must be a link-local address
2415 //
2416 if (!NetIp6IsLinkLocalAddr (&Head->SourceAddress)) {
2417 goto Exit;
2418 }
2419
2420 //
2421 // The dest of this ICMP redirect message is not us.
2422 //
2423 if (!Ip6IsOneOfSetAddress (IpSb, &Head->DestinationAddress, NULL, NULL)) {
2424 goto Exit;
2425 }
2426
2427 //
2428 // All included options have a length that is greater than zero.
2429 //
2430 OptionLen = (UINT16) (Head->PayloadLength - IP6_REDITECT_LENGTH);
2431 Option = NetbufGetByte (Packet, IP6_REDITECT_LENGTH, NULL);
2432
2433 if (!Ip6IsNDOptionValid (Option, OptionLen)) {
2434 goto Exit;
2435 }
2436
2437 Target = (EFI_IPv6_ADDRESS *) (Icmp + 1);
2438 IcmpDest = Target + 1;
2439
2440 //
2441 // The ICMP Destination Address field in the redirect message does not contain
2442 // a multicast address.
2443 //
2444 if (IP6_IS_MULTICAST (IcmpDest)) {
2445 goto Exit;
2446 }
2447
2448 //
2449 // The ICMP Target Address is either a link-local address (when redirected to
2450 // a router) or the same as the ICMP Destination Address (when redirected to
2451 // the on-link destination).
2452 //
2453 IsRouter = (BOOLEAN) !EFI_IP6_EQUAL (Target, IcmpDest);
2454 if (!NetIp6IsLinkLocalAddr (Target) && IsRouter) {
2455 goto Exit;
2456 }
2457
2458 //
2459 // Check the options. The only interested option here is the target-link layer
2460 // address option.
2461 //
2462 Length = Packet->TotalSize - 40;
2463 Option = (UINT8 *) (IcmpDest + 1);
2464 LinkLayerOption = NULL;
2465 while (Length > 0) {
2466 switch (*Option) {
2467 case Ip6OptionEtherTarget:
2468
2469 LinkLayerOption = (IP6_ETHER_ADDR_OPTION *) Option;
2470 OptLen = LinkLayerOption->Length;
2471 if (OptLen != 1) {
2472 //
2473 // For ethernet, the length must be 1.
2474 //
2475 goto Exit;
2476 }
2477 break;
2478
2479 default:
2480
2481 OptLen = *(Option + 1);
2482 if (OptLen == 0) {
2483 //
2484 // A length of 0 is invalid.
2485 //
2486 goto Exit;
2487 }
2488 break;
2489 }
2490
2491 Length -= 8 * OptLen;
2492 Option += 8 * OptLen;
2493 }
2494
2495 if (Length != 0) {
2496 goto Exit;
2497 }
2498
2499 //
2500 // The IP source address of the Redirect is the same as the current
2501 // first-hop router for the specified ICMP Destination Address.
2502 //
2503 RouteCache = Ip6FindRouteCache (IpSb->RouteTable, IcmpDest, &Head->DestinationAddress);
2504 if (RouteCache != NULL) {
2505 if (!EFI_IP6_EQUAL (&RouteCache->NextHop, &Head->SourceAddress)) {
2506 //
2507 // The source of this Redirect message must match the NextHop of the
2508 // corresponding route cache entry.
2509 //
2510 goto Exit;
2511 }
2512
2513 //
2514 // Update the NextHop.
2515 //
2516 IP6_COPY_ADDRESS (&RouteCache->NextHop, Target);
2517
2518 if (!IsRouter) {
2519 RouteEntry = (IP6_ROUTE_ENTRY *) RouteCache->Tag;
2520 RouteEntry->Flag = RouteEntry->Flag | IP6_DIRECT_ROUTE;
2521 }
2522
2523 } else {
2524 //
2525 // Get the Route Entry.
2526 //
2527 RouteEntry = Ip6FindRouteEntry (IpSb->RouteTable, IcmpDest, NULL);
2528 if (RouteEntry == NULL) {
2529 RouteEntry = Ip6CreateRouteEntry (IcmpDest, 0, NULL);
2530 if (RouteEntry == NULL) {
2531 Status = EFI_OUT_OF_RESOURCES;
2532 goto Exit;
2533 }
2534 }
2535
2536 if (!IsRouter) {
2537 RouteEntry->Flag = IP6_DIRECT_ROUTE;
2538 }
2539
2540 //
2541 // Create a route cache for this.
2542 //
2543 RouteCache = Ip6CreateRouteCacheEntry (
2544 IcmpDest,
2545 &Head->DestinationAddress,
2546 Target,
2547 (UINTN) RouteEntry
2548 );
2549 if (RouteCache == NULL) {
2550 Status = EFI_OUT_OF_RESOURCES;
2551 goto Exit;
2552 }
2553
2554 //
2555 // Insert the newly created route cache entry.
2556 //
2557 Index = IP6_ROUTE_CACHE_HASH (IcmpDest, &Head->DestinationAddress);
2558 InsertHeadList (&IpSb->RouteTable->Cache.CacheBucket[Index], &RouteCache->Link);
2559 }
2560
2561 //
2562 // Try to locate the neighbor cache for the Target.
2563 //
2564 NeighborCache = Ip6FindNeighborEntry (IpSb, Target);
2565
2566 if (LinkLayerOption != NULL) {
2567 if (NeighborCache == NULL) {
2568 //
2569 // Create a neighbor cache for the Target.
2570 //
2571 ZeroMem (&Mac, sizeof (EFI_MAC_ADDRESS));
2572 CopyMem (&Mac, LinkLayerOption->EtherAddr, 6);
2573 NeighborCache = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, Target, &Mac);
2574 if (NeighborCache == NULL) {
2575 //
2576 // Just report a success here. The neighbor cache can be created in
2577 // some other place.
2578 //
2579 Status = EFI_SUCCESS;
2580 goto Exit;
2581 }
2582
2583 NeighborCache->State = EfiNeighborStale;
2584 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2585 } else {
2586 Result = CompareMem (LinkLayerOption->EtherAddr, &NeighborCache->LinkAddress, 6);
2587
2588 //
2589 // If the link-local address is the same as that already in the cache,
2590 // the cache entry's state remains unchanged. Otherwise update the
2591 // reachability state to STALE.
2592 //
2593 if ((NeighborCache->State == EfiNeighborInComplete) || (Result != 0)) {
2594 CopyMem (&NeighborCache->LinkAddress, LinkLayerOption->EtherAddr, 6);
2595
2596 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2597
2598 if (NeighborCache->State == EfiNeighborInComplete) {
2599 //
2600 // Send queued packets if exist.
2601 //
2602 NeighborCache->State = EfiNeighborStale;
2603 NeighborCache->CallBack ((VOID *) NeighborCache);
2604 } else {
2605 NeighborCache->State = EfiNeighborStale;
2606 }
2607 }
2608 }
2609 }
2610
2611 if (NeighborCache != NULL && IsRouter) {
2612 //
2613 // The Target is a router, set IsRouter to TRUE.
2614 //
2615 NeighborCache->IsRouter = TRUE;
2616 }
2617
2618 Status = EFI_SUCCESS;
2619
2620 Exit:
2621 NetbufFree (Packet);
2622 return Status;
2623 }
2624
2625 /**
2626 Add Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2627
2628 @param[in] IpSb The IP6 service binding instance.
2629 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2630 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2631 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2632 cache. It will be deleted after Timeout. A value of zero means that
2633 the entry is permanent. A non-zero value means that the entry is
2634 dynamic.
2635 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2636 be overridden and updated; if FALSE, and if a
2637 corresponding cache entry already existed, EFI_ACCESS_DENIED
2638 will be returned.
2639
2640 @retval EFI_SUCCESS The neighbor cache entry has been added.
2641 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache
2642 due to insufficient resources.
2643 @retval EFI_NOT_FOUND TargetLinkAddress is NULL.
2644 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
2645 and that entry is tagged as un-overridden (when DeleteFlag
2646 is FALSE).
2647
2648 **/
2649 EFI_STATUS
2650 Ip6AddNeighbor (
2651 IN IP6_SERVICE *IpSb,
2652 IN EFI_IPv6_ADDRESS *TargetIp6Address,
2653 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
2654 IN UINT32 Timeout,
2655 IN BOOLEAN Override
2656 )
2657 {
2658 IP6_NEIGHBOR_ENTRY *Neighbor;
2659
2660 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
2661 if (Neighbor != NULL) {
2662 if (!Override) {
2663 return EFI_ACCESS_DENIED;
2664 } else {
2665 if (TargetLinkAddress != NULL) {
2666 IP6_COPY_LINK_ADDRESS (&Neighbor->LinkAddress, TargetLinkAddress);
2667 }
2668 }
2669 } else {
2670 if (TargetLinkAddress == NULL) {
2671 return EFI_NOT_FOUND;
2672 }
2673
2674 Neighbor = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, TargetIp6Address, TargetLinkAddress);
2675 if (Neighbor == NULL) {
2676 return EFI_OUT_OF_RESOURCES;
2677 }
2678 }
2679
2680 Neighbor->State = EfiNeighborReachable;
2681
2682 if (Timeout != 0) {
2683 Neighbor->Ticks = IP6_GET_TICKS (Timeout / TICKS_PER_MS);
2684 Neighbor->Dynamic = TRUE;
2685 } else {
2686 Neighbor->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2687 }
2688
2689 return EFI_SUCCESS;
2690 }
2691
2692 /**
2693 Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2694
2695 @param[in] IpSb The IP6 service binding instance.
2696 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2697 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2698 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2699 cache. It will be deleted after Timeout. A value of zero means that
2700 the entry is permanent. A non-zero value means that the entry is
2701 dynamic.
2702 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2703 be overridden and updated; if FALSE, and if a
2704 corresponding cache entry already existed, EFI_ACCESS_DENIED
2705 will be returned.
2706
2707 @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted.
2708 @retval EFI_NOT_FOUND This entry is not in the neighbor cache.
2709
2710 **/
2711 EFI_STATUS
2712 Ip6DelNeighbor (
2713 IN IP6_SERVICE *IpSb,
2714 IN EFI_IPv6_ADDRESS *TargetIp6Address,
2715 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
2716 IN UINT32 Timeout,
2717 IN BOOLEAN Override
2718 )
2719 {
2720 IP6_NEIGHBOR_ENTRY *Neighbor;
2721
2722 Neighbor = Ip6FindNeighborEntry (IpSb, TargetIp6Address);
2723 if (Neighbor == NULL) {
2724 return EFI_NOT_FOUND;
2725 }
2726
2727 RemoveEntryList (&Neighbor->Link);
2728 FreePool (Neighbor);
2729
2730 return EFI_SUCCESS;
2731 }
2732
2733 /**
2734 The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds.
2735 This time routine handles DAD module and neighbor state transition.
2736 It is also responsible for sending out router solicitations.
2737
2738 @param[in] Event The IP6 service instance's heartbeat timer.
2739 @param[in] Context The IP6 service instance.
2740
2741 **/
2742 VOID
2743 EFIAPI
2744 Ip6NdFasterTimerTicking (
2745 IN EFI_EVENT Event,
2746 IN VOID *Context
2747 )
2748 {
2749 LIST_ENTRY *Entry;
2750 LIST_ENTRY *Next;
2751 LIST_ENTRY *Entry2;
2752 IP6_INTERFACE *IpIf;
2753 IP6_DELAY_JOIN_LIST *DelayNode;
2754 EFI_IPv6_ADDRESS Source;
2755 IP6_DAD_ENTRY *DupAddrDetect;
2756 EFI_STATUS Status;
2757 IP6_NEIGHBOR_ENTRY *NeighborCache;
2758 EFI_IPv6_ADDRESS Destination;
2759 IP6_SERVICE *IpSb;
2760 BOOLEAN Flag;
2761
2762 IpSb = (IP6_SERVICE *) Context;
2763 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
2764
2765 ZeroMem (&Source, sizeof (EFI_IPv6_ADDRESS));
2766
2767 //
2768 // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router
2769 // Solicitation messages, each separated by at least
2770 // RTR_SOLICITATION_INTERVAL (4) seconds.
2771 //
2772 if ((IpSb->Ip6ConfigInstance.Policy == Ip6ConfigPolicyAutomatic) &&
2773 !IpSb->RouterAdvertiseReceived &&
2774 IpSb->SolicitTimer > 0
2775 ) {
2776 if ((IpSb->Ticks == 0) || (--IpSb->Ticks == 0)) {
2777 Status = Ip6SendRouterSolicit (IpSb, NULL, NULL, NULL, NULL);
2778 if (!EFI_ERROR (Status)) {
2779 IpSb->SolicitTimer--;
2780 IpSb->Ticks = (UINT32) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL);
2781 }
2782 }
2783 }
2784
2785 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
2786 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
2787
2788 //
2789 // Process the delay list to join the solicited-node multicast address.
2790 //
2791 NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DelayJoinList) {
2792 DelayNode = NET_LIST_USER_STRUCT (Entry2, IP6_DELAY_JOIN_LIST, Link);
2793 if ((DelayNode->DelayTime == 0) || (--DelayNode->DelayTime == 0)) {
2794 //
2795 // The timer expires, init the duplicate address detection.
2796 //
2797 Ip6InitDADProcess (
2798 DelayNode->Interface,
2799 DelayNode->AddressInfo,
2800 DelayNode->DadCallback,
2801 DelayNode->Context
2802 );
2803
2804 //
2805 // Remove the delay node
2806 //
2807 RemoveEntryList (&DelayNode->Link);
2808 FreePool (DelayNode);
2809 }
2810 }
2811
2812 //
2813 // Process the duplicate address detection list.
2814 //
2815 NET_LIST_FOR_EACH_SAFE (Entry2, Next, &IpIf->DupAddrDetectList) {
2816 DupAddrDetect = NET_LIST_USER_STRUCT (Entry2, IP6_DAD_ENTRY, Link);
2817
2818 if ((DupAddrDetect->RetransTick == 0) || (--DupAddrDetect->RetransTick == 0)) {
2819 //
2820 // The timer expires, check the remaining transmit counts.
2821 //
2822 if (DupAddrDetect->Transmit < DupAddrDetect->MaxTransmit) {
2823 //
2824 // Send the Neighbor Solicitation message with
2825 // Source - unspecified address, destination - solicited-node multicast address
2826 // Target - the address to be validated
2827 //
2828 Status = Ip6SendNeighborSolicit (
2829 IpSb,
2830 NULL,
2831 &DupAddrDetect->Destination,
2832 &DupAddrDetect->AddressInfo->Address,
2833 NULL
2834 );
2835 if (EFI_ERROR (Status)) {
2836 return;
2837 }
2838
2839 DupAddrDetect->Transmit++;
2840 DupAddrDetect->RetransTick = IP6_GET_TICKS (IpSb->RetransTimer);
2841 } else {
2842 //
2843 // All required solicitation has been sent out, and the RetransTime after the last
2844 // Neighbor Solicit is elapsed, finish the DAD process.
2845 //
2846 Flag = FALSE;
2847 if ((DupAddrDetect->Receive == 0) ||
2848 (DupAddrDetect->Transmit == DupAddrDetect->Receive)) {
2849 Flag = TRUE;
2850 }
2851
2852 Ip6OnDADFinished (Flag, IpIf, DupAddrDetect);
2853 }
2854 }
2855 }
2856 }
2857
2858 //
2859 // Polling the state of Neighbor cache
2860 //
2861 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->NeighborTable) {
2862 NeighborCache = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, Link);
2863
2864 switch (NeighborCache->State) {
2865 case EfiNeighborInComplete:
2866 if (NeighborCache->Ticks > 0) {
2867 --NeighborCache->Ticks;
2868 }
2869
2870 //
2871 // Retransmit Neighbor Solicitation messages approximately every
2872 // RetransTimer milliseconds while awaiting a response.
2873 //
2874 if (NeighborCache->Ticks == 0) {
2875 if (NeighborCache->Transmit > 1) {
2876 //
2877 // Send out multicast neighbor solicitation for address resolution.
2878 // After last neighbor solicitation message has been sent out, wait
2879 // for RetransTimer and then remove entry if no response is received.
2880 //
2881 Ip6CreateSNMulticastAddr (&NeighborCache->Neighbor, &Destination);
2882 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
2883 if (EFI_ERROR (Status)) {
2884 return;
2885 }
2886
2887 Status = Ip6SendNeighborSolicit (
2888 IpSb,
2889 &Source,
2890 &Destination,
2891 &NeighborCache->Neighbor,
2892 &IpSb->SnpMode.CurrentAddress
2893 );
2894 if (EFI_ERROR (Status)) {
2895 return;
2896 }
2897 }
2898
2899 //
2900 // Update the retransmit times.
2901 //
2902 if (NeighborCache->Transmit > 0) {
2903 --NeighborCache->Transmit;
2904 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
2905 }
2906 }
2907
2908 if (NeighborCache->Transmit == 0) {
2909 //
2910 // Timeout, send ICMP destination unreachable packet and then remove entry
2911 //
2912 Status = Ip6FreeNeighborEntry (
2913 IpSb,
2914 NeighborCache,
2915 TRUE,
2916 TRUE,
2917 EFI_ICMP_ERROR,
2918 NULL,
2919 NULL
2920 );
2921 if (EFI_ERROR (Status)) {
2922 return;
2923 }
2924 }
2925
2926 break;
2927
2928 case EfiNeighborReachable:
2929 //
2930 // This entry is inserted by EfiIp6Neighbors() as static entry
2931 // and will not timeout.
2932 //
2933 if (!NeighborCache->Dynamic && (NeighborCache->Ticks == IP6_INFINIT_LIFETIME)) {
2934 break;
2935 }
2936
2937 if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) {
2938 if (NeighborCache->Dynamic) {
2939 //
2940 // This entry is inserted by EfiIp6Neighbors() as dynamic entry
2941 // and will be deleted after timeout.
2942 //
2943 Status = Ip6FreeNeighborEntry (
2944 IpSb,
2945 NeighborCache,
2946 FALSE,
2947 TRUE,
2948 EFI_TIMEOUT,
2949 NULL,
2950 NULL
2951 );
2952 if (EFI_ERROR (Status)) {
2953 return;
2954 }
2955 } else {
2956 NeighborCache->State = EfiNeighborStale;
2957 NeighborCache->Ticks = (UINT32) IP6_INFINIT_LIFETIME;
2958 }
2959 }
2960
2961 break;
2962
2963 case EfiNeighborDelay:
2964 if ((NeighborCache->Ticks == 0) || (--NeighborCache->Ticks == 0)) {
2965
2966 NeighborCache->State = EfiNeighborProbe;
2967 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
2968 NeighborCache->Transmit = IP6_MAX_UNICAST_SOLICIT + 1;
2969 //
2970 // Send out unicast neighbor solicitation for Neighbor Unreachability Detection
2971 //
2972 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
2973 if (EFI_ERROR (Status)) {
2974 return;
2975 }
2976
2977 Status = Ip6SendNeighborSolicit (
2978 IpSb,
2979 &Source,
2980 &NeighborCache->Neighbor,
2981 &NeighborCache->Neighbor,
2982 &IpSb->SnpMode.CurrentAddress
2983 );
2984 if (EFI_ERROR (Status)) {
2985 return;
2986 }
2987
2988 NeighborCache->Transmit--;
2989 }
2990
2991 break;
2992
2993 case EfiNeighborProbe:
2994 if (NeighborCache->Ticks > 0) {
2995 --NeighborCache->Ticks;
2996 }
2997
2998 //
2999 // Retransmit Neighbor Solicitation messages approximately every
3000 // RetransTimer milliseconds while awaiting a response.
3001 //
3002 if (NeighborCache->Ticks == 0) {
3003 if (NeighborCache->Transmit > 1) {
3004 //
3005 // Send out unicast neighbor solicitation for Neighbor Unreachability
3006 // Detection. After last neighbor solicitation message has been sent out,
3007 // wait for RetransTimer and then remove entry if no response is received.
3008 //
3009 Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
3010 if (EFI_ERROR (Status)) {
3011 return;
3012 }
3013
3014 Status = Ip6SendNeighborSolicit (
3015 IpSb,
3016 &Source,
3017 &NeighborCache->Neighbor,
3018 &NeighborCache->Neighbor,
3019 &IpSb->SnpMode.CurrentAddress
3020 );
3021 if (EFI_ERROR (Status)) {
3022 return;
3023 }
3024 }
3025
3026 //
3027 // Update the retransmit times.
3028 //
3029 if (NeighborCache->Transmit > 0) {
3030 --NeighborCache->Transmit;
3031 NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer);
3032 }
3033 }
3034
3035 if (NeighborCache->Transmit == 0) {
3036 //
3037 // Delete the neighbor entry.
3038 //
3039 Status = Ip6FreeNeighborEntry (
3040 IpSb,
3041 NeighborCache,
3042 FALSE,
3043 TRUE,
3044 EFI_TIMEOUT,
3045 NULL,
3046 NULL
3047 );
3048 if (EFI_ERROR (Status)) {
3049 return;
3050 }
3051 }
3052
3053 break;
3054
3055 default:
3056 break;
3057 }
3058 }
3059 }
3060
3061 /**
3062 The heartbeat timer of ND module in 1 second. This time routine handles following
3063 things: 1) maitain default router list; 2) maintain prefix options;
3064 3) maintain route caches.
3065
3066 @param[in] IpSb The IP6 service binding instance.
3067
3068 **/
3069 VOID
3070 Ip6NdTimerTicking (
3071 IN IP6_SERVICE *IpSb
3072 )
3073 {
3074 LIST_ENTRY *Entry;
3075 LIST_ENTRY *Next;
3076 IP6_DEFAULT_ROUTER *DefaultRouter;
3077 IP6_PREFIX_LIST_ENTRY *PrefixOption;
3078 UINT8 Index;
3079 IP6_ROUTE_CACHE_ENTRY *RouteCache;
3080
3081 //
3082 // Decrease the lifetime of default router, if expires remove it from default router list.
3083 //
3084 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->DefaultRouterList) {
3085 DefaultRouter = NET_LIST_USER_STRUCT (Entry, IP6_DEFAULT_ROUTER, Link);
3086 if (DefaultRouter->Lifetime != IP6_INF_ROUTER_LIFETIME) {
3087 if ((DefaultRouter->Lifetime == 0) || (--DefaultRouter->Lifetime == 0)) {
3088 Ip6DestroyDefaultRouter (IpSb, DefaultRouter);
3089 }
3090 }
3091 }
3092
3093 //
3094 // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses.
3095 //
3096 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->AutonomousPrefix) {
3097 PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
3098 if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) {
3099 if ((PrefixOption->ValidLifetime > 0) && (--PrefixOption->ValidLifetime > 0)) {
3100 if ((PrefixOption->PreferredLifetime != (UINT32) IP6_INFINIT_LIFETIME) &&
3101 (PrefixOption->PreferredLifetime > 0)
3102 ) {
3103 --PrefixOption->PreferredLifetime;
3104 }
3105 } else {
3106 Ip6DestroyPrefixListEntry (IpSb, PrefixOption, FALSE, TRUE);
3107 }
3108 }
3109 }
3110
3111 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->OnlinkPrefix) {
3112 PrefixOption = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
3113 if (PrefixOption->ValidLifetime != (UINT32) IP6_INFINIT_LIFETIME) {
3114 if ((PrefixOption->ValidLifetime == 0) || (--PrefixOption->ValidLifetime == 0)) {
3115 Ip6DestroyPrefixListEntry (IpSb, PrefixOption, TRUE, TRUE);
3116 }
3117 }
3118 }
3119
3120 //
3121 // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries.
3122 // Remove the entries at the tail of the bucket. These entries
3123 // are likely to be used least.
3124 // Reclaim frequency is set to 1 second.
3125 //
3126 for (Index = 0; Index < IP6_ROUTE_CACHE_HASH_SIZE; Index++) {
3127 while (IpSb->RouteTable->Cache.CacheNum[Index] > IP6_ROUTE_CACHE_MAX) {
3128 Entry = NetListRemoveTail (&IpSb->RouteTable->Cache.CacheBucket[Index]);
3129 if (Entry == NULL) {
3130 break;
3131 }
3132
3133 RouteCache = NET_LIST_USER_STRUCT (Entry, IP6_ROUTE_CACHE_ENTRY, Link);
3134 Ip6FreeRouteCacheEntry (RouteCache);
3135 ASSERT (IpSb->RouteTable->Cache.CacheNum[Index] > 0);
3136 IpSb->RouteTable->Cache.CacheNum[Index]--;
3137 }
3138 }
3139 }
3140