2 Implementation of Neighbor Discovery support routines.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
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.
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.
18 EFI_MAC_ADDRESS mZeroMacAddress
;
21 Update the ReachableTime in IP6 service binding instance data, in milliseconds.
23 @param[in, out] IpSb Points to the IP6_SERVICE.
27 Ip6UpdateReachableTime (
28 IN OUT IP6_SERVICE
*IpSb
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
;
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.
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.
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.
51 Ip6BuildEfiNeighborCache (
52 IN IP6_PROTOCOL
*IpInstance
,
53 OUT UINT32
*NeighborCount
,
54 OUT EFI_IP6_NEIGHBOR_CACHE
**NeighborCache
57 IP6_NEIGHBOR_ENTRY
*Neighbor
;
61 EFI_IP6_NEIGHBOR_CACHE
*EfiNeighborCache
;
62 EFI_IP6_NEIGHBOR_CACHE
*NeighborCacheTmp
;
64 NET_CHECK_SIGNATURE (IpInstance
, IP6_PROTOCOL_SIGNATURE
);
65 ASSERT (NeighborCount
!= NULL
&& NeighborCache
!= NULL
);
67 IpSb
= IpInstance
->Service
;
70 NET_LIST_FOR_EACH (Entry
, &IpSb
->NeighborTable
) {
78 NeighborCacheTmp
= AllocatePool (Count
* sizeof (EFI_IP6_NEIGHBOR_CACHE
));
79 if (NeighborCacheTmp
== NULL
) {
80 return EFI_OUT_OF_RESOURCES
;
83 *NeighborCount
= Count
;
86 NET_LIST_FOR_EACH (Entry
, &IpSb
->NeighborTable
) {
87 Neighbor
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, Link
);
89 EfiNeighborCache
= NeighborCacheTmp
+ Count
;
91 EfiNeighborCache
->State
= Neighbor
->State
;
92 IP6_COPY_ADDRESS (&EfiNeighborCache
->Neighbor
, &Neighbor
->Neighbor
);
93 IP6_COPY_LINK_ADDRESS (&EfiNeighborCache
->LinkAddress
, &Neighbor
->LinkAddress
);
98 ASSERT (*NeighborCount
== Count
);
99 *NeighborCache
= NeighborCacheTmp
;
105 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
106 of prefix entries is also returned.
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.
112 @retval EFI_SUCCESS The prefix table successfully built.
113 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the prefix table.
117 Ip6BuildPrefixTable (
118 IN IP6_PROTOCOL
*IpInstance
,
119 OUT UINT32
*PrefixCount
,
120 OUT EFI_IP6_ADDRESS_INFO
**PrefixTable
126 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
127 EFI_IP6_ADDRESS_INFO
*EfiPrefix
;
128 EFI_IP6_ADDRESS_INFO
*PrefixTableTmp
;
130 NET_CHECK_SIGNATURE (IpInstance
, IP6_PROTOCOL_SIGNATURE
);
131 ASSERT (PrefixCount
!= NULL
&& PrefixTable
!= NULL
);
133 IpSb
= IpInstance
->Service
;
136 NET_LIST_FOR_EACH (Entry
, &IpSb
->OnlinkPrefix
) {
144 PrefixTableTmp
= AllocatePool (Count
* sizeof (EFI_IP6_ADDRESS_INFO
));
145 if (PrefixTableTmp
== NULL
) {
146 return EFI_OUT_OF_RESOURCES
;
149 *PrefixCount
= Count
;
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
;
161 ASSERT (*PrefixCount
== Count
);
162 *PrefixTable
= PrefixTableTmp
;
168 Allocate and initialize a IP6 prefix list entry.
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.
181 @return NULL if it failed to allocate memory for the prefix node. Otherwise, point
182 to the created or existing prefix list entry.
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
195 IP6_PREFIX_LIST_ENTRY
*PrefixEntry
;
196 IP6_ROUTE_ENTRY
*RtEntry
;
197 LIST_ENTRY
*ListHead
;
199 IP6_PREFIX_LIST_ENTRY
*TmpPrefixEntry
;
201 if (Prefix
== NULL
|| PreferredLifetime
> ValidLifetime
|| PrefixLength
>= IP6_PREFIX_NUM
) {
205 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
207 PrefixEntry
= Ip6FindPrefixListEntry (
213 if (PrefixEntry
!= NULL
) {
214 PrefixEntry
->RefCnt
++;
218 PrefixEntry
= AllocatePool (sizeof (IP6_PREFIX_LIST_ENTRY
));
219 if (PrefixEntry
== NULL
) {
223 PrefixEntry
->RefCnt
= 1;
224 PrefixEntry
->ValidLifetime
= ValidLifetime
;
225 PrefixEntry
->PreferredLifetime
= PreferredLifetime
;
226 PrefixEntry
->PrefixLength
= PrefixLength
;
227 IP6_COPY_ADDRESS (&PrefixEntry
->Prefix
, Prefix
);
229 ListHead
= OnLinkOrAuto
? &IpSb
->OnlinkPrefix
: &IpSb
->AutonomousPrefix
;
232 // Create a direct route entry for on-link prefix and insert to route area.
235 RtEntry
= Ip6CreateRouteEntry (Prefix
, PrefixLength
, NULL
);
236 if (RtEntry
== NULL
) {
237 FreePool (PrefixEntry
);
241 RtEntry
->Flag
= IP6_DIRECT_ROUTE
;
242 InsertHeadList (&IpSb
->RouteTable
->RouteArea
[PrefixLength
], &RtEntry
->Link
);
243 IpSb
->RouteTable
->TotalNum
++;
247 // Insert the prefix entry in the order that a prefix with longer prefix length
248 // is put ahead in the list.
250 NET_LIST_FOR_EACH (Entry
, ListHead
) {
251 TmpPrefixEntry
= NET_LIST_USER_STRUCT(Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
253 if (TmpPrefixEntry
->PrefixLength
< PrefixEntry
->PrefixLength
) {
258 NetListInsertBefore (Entry
, &PrefixEntry
->Link
);
264 Destroy a IP6 prefix list entry.
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.
276 Ip6DestroyPrefixListEntry (
277 IN IP6_SERVICE
*IpSb
,
278 IN IP6_PREFIX_LIST_ENTRY
*PrefixEntry
,
279 IN BOOLEAN OnLinkOrAuto
,
280 IN BOOLEAN ImmediateDelete
287 if ((!ImmediateDelete
) && (PrefixEntry
->RefCnt
> 0) && ((--PrefixEntry
->RefCnt
) > 0)) {
293 // Remove the direct route for onlink prefix from route table.
296 Status
= Ip6DelRoute (
298 &PrefixEntry
->Prefix
,
299 PrefixEntry
->PrefixLength
,
302 } while (Status
!= EFI_NOT_FOUND
);
305 // Remove the corresponding addresses generated from this autonomous prefix.
307 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
308 IpIf
= NET_LIST_USER_STRUCT_S (Entry
, IP6_INTERFACE
, Link
, IP6_INTERFACE_SIGNATURE
);
310 Ip6RemoveAddr (IpSb
, &IpIf
->AddressList
, &IpIf
->AddressCount
, &PrefixEntry
->Prefix
, PrefixEntry
->PrefixLength
);
314 RemoveEntryList (&PrefixEntry
->Link
);
315 FreePool (PrefixEntry
);
319 Search the list array to find an IP6 prefix list entry.
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.
327 @return NULL if cannot find the IP6 prefix list entry. Otherwise, return the
328 pointer to the IP6 prefix list entry.
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
339 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
341 LIST_ENTRY
*ListHead
;
343 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
344 ASSERT (Prefix
!= NULL
);
347 ListHead
= &IpSb
->OnlinkPrefix
;
349 ListHead
= &IpSb
->AutonomousPrefix
;
352 NET_LIST_FOR_EACH (Entry
, ListHead
) {
353 PrefixList
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
354 if (PrefixLength
!= 255) {
356 // Perform exactly prefix match.
358 if (PrefixList
->PrefixLength
== PrefixLength
&&
359 NetIp6IsNetEqual (&PrefixList
->Prefix
, Prefix
, PrefixLength
)) {
364 // Perform the longest prefix match. The list is already sorted with
365 // the longest length prefix put at the head of the list.
367 if (NetIp6IsNetEqual (&PrefixList
->Prefix
, Prefix
, PrefixList
->PrefixLength
)) {
377 Release the resource in the prefix list table, and destroy the list entry and
378 corresponding addresses or route entries.
380 @param[in] IpSb The pointer to the IP6_SERVICE instance.
381 @param[in] ListHead The list entry head of the prefix list table.
385 Ip6CleanPrefixListTable (
386 IN IP6_SERVICE
*IpSb
,
387 IN LIST_ENTRY
*ListHead
390 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
393 OnLink
= (BOOLEAN
) (ListHead
== &IpSb
->OnlinkPrefix
);
395 while (!IsListEmpty (ListHead
)) {
396 PrefixList
= NET_LIST_HEAD (ListHead
, IP6_PREFIX_LIST_ENTRY
, Link
);
397 Ip6DestroyPrefixListEntry (IpSb
, PrefixList
, OnLink
, TRUE
);
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.
406 @param[in] Context The context of the callback, a pointer to IP6_NEIGHBOR_ENTRY.
416 IP6_NEIGHBOR_ENTRY
*ArpQue
;
418 IP6_LINK_TX_TOKEN
*Token
;
422 ArpQue
= (IP6_NEIGHBOR_ENTRY
*) Context
;
423 if ((ArpQue
== NULL
) || (ArpQue
->Interface
== NULL
)) {
427 IpSb
= ArpQue
->Interface
->Service
;
428 if ((IpSb
== NULL
) || (IpSb
->Signature
!= IP6_SERVICE_SIGNATURE
)) {
433 // ARP resolve failed for some reason. Release all the frame
434 // and ARP queue itself. Ip6FreeArpQue will call the frame's
437 if (NET_MAC_EQUAL (&ArpQue
->LinkAddress
, &mZeroMacAddress
, IpSb
->SnpMode
.HwAddressSize
)) {
438 Ip6FreeNeighborEntry (IpSb
, ArpQue
, FALSE
, TRUE
, EFI_NO_MAPPING
, NULL
, NULL
);
443 // ARP resolve succeeded, Transmit all the frame.
446 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &ArpQue
->Frames
) {
447 RemoveEntryList (Entry
);
449 Token
= NET_LIST_USER_STRUCT (Entry
, IP6_LINK_TX_TOKEN
, Link
);
450 IP6_COPY_LINK_ADDRESS (&Token
->DstMac
, &ArpQue
->LinkAddress
);
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.
459 InsertTailList (&ArpQue
->Interface
->SentFrames
, &Token
->Link
);
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
);
466 Ip6FreeLinkTxToken (Token
);
474 // Free the ArpQue only but not the whole neighbor entry.
476 Ip6FreeNeighborEntry (IpSb
, ArpQue
, FALSE
, FALSE
, EFI_SUCCESS
, NULL
, NULL
);
478 if (Sent
&& (ArpQue
->State
== EfiNeighborStale
)) {
479 ArpQue
->State
= EfiNeighborDelay
;
480 ArpQue
->Ticks
= (UINT32
) IP6_GET_TICKS (IP6_DELAY_FIRST_PROBE_TIME
);
485 Allocate and initialize an IP6 neighbor cache entry.
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.
494 @return NULL if failed to allocate memory for the neighbor cache entry.
495 Otherwise, point to the created neighbor cache 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
506 IP6_NEIGHBOR_ENTRY
*Entry
;
507 IP6_DEFAULT_ROUTER
*DefaultRouter
;
509 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
510 ASSERT (Ip6Address
!= NULL
);
512 Entry
= AllocateZeroPool (sizeof (IP6_NEIGHBOR_ENTRY
));
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
;
526 InitializeListHead (&Entry
->Frames
);
528 IP6_COPY_ADDRESS (&Entry
->Neighbor
, Ip6Address
);
530 if (LinkAddress
!= NULL
) {
531 IP6_COPY_LINK_ADDRESS (&Entry
->LinkAddress
, LinkAddress
);
533 IP6_COPY_LINK_ADDRESS (&Entry
->LinkAddress
, &mZeroMacAddress
);
536 InsertHeadList (&IpSb
->NeighborTable
, &Entry
->Link
);
539 // If corresponding default router entry exists, establish the relationship.
541 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, Ip6Address
);
542 if (DefaultRouter
!= NULL
) {
543 DefaultRouter
->NeighborCache
= Entry
;
550 Search a IP6 neighbor cache entry.
552 @param[in] IpSb The pointer to the IP6_SERVICE instance.
553 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
555 @return NULL if it failed to find the matching neighbor cache entry.
556 Otherwise, point to the found neighbor cache entry.
560 Ip6FindNeighborEntry (
561 IN IP6_SERVICE
*IpSb
,
562 IN EFI_IPv6_ADDRESS
*Ip6Address
567 IP6_NEIGHBOR_ENTRY
*Neighbor
;
569 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
570 ASSERT (Ip6Address
!= NULL
);
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
);
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.
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'
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.
602 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
603 @retval EFI_SUCCESS The operation finished successfully.
607 Ip6FreeNeighborEntry (
608 IN IP6_SERVICE
*IpSb
,
609 IN IP6_NEIGHBOR_ENTRY
*NeighborCache
,
610 IN BOOLEAN SendIcmpError
,
612 IN EFI_STATUS IoStatus
,
613 IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL
,
614 IN VOID
*Context OPTIONAL
617 IP6_LINK_TX_TOKEN
*TxToken
;
620 IP6_DEFAULT_ROUTER
*DefaultRouter
;
623 // If FrameToCancel fails, the token will not be released.
624 // To avoid the memory leak, stop this usage model.
626 if (FullFree
&& FrameToCancel
!= NULL
) {
627 return EFI_INVALID_PARAMETER
;
630 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &NeighborCache
->Frames
) {
631 TxToken
= NET_LIST_USER_STRUCT (Entry
, IP6_LINK_TX_TOKEN
, Link
);
633 if (SendIcmpError
&& !IP6_IS_MULTICAST (&TxToken
->Packet
->Ip
.Ip6
->DestinationAddress
)) {
638 &TxToken
->Packet
->Ip
.Ip6
->SourceAddress
,
639 ICMP_V6_DEST_UNREACHABLE
,
640 ICMP_V6_ADDR_UNREACHABLE
,
645 if ((FrameToCancel
== NULL
) || FrameToCancel (TxToken
, Context
)) {
646 RemoveEntryList (Entry
);
647 TxToken
->CallBack (TxToken
->Packet
, IoStatus
, 0, TxToken
->Context
);
648 Ip6FreeLinkTxToken (TxToken
);
652 if (NeighborCache
->ArpFree
&& IsListEmpty (&NeighborCache
->Frames
)) {
653 RemoveEntryList (&NeighborCache
->ArpList
);
654 NeighborCache
->ArpFree
= FALSE
;
658 if (NeighborCache
->IsRouter
) {
659 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &NeighborCache
->Neighbor
);
660 if (DefaultRouter
!= NULL
) {
661 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
665 RemoveEntryList (&NeighborCache
->Link
);
666 FreePool (NeighborCache
);
673 Allocate and initialize an IP6 default router entry.
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.
680 @return NULL if it failed to allocate memory for the default router node.
681 Otherwise, point to the created default router node.
685 Ip6CreateDefaultRouter (
686 IN IP6_SERVICE
*IpSb
,
687 IN EFI_IPv6_ADDRESS
*Ip6Address
,
688 IN UINT16 RouterLifetime
691 IP6_DEFAULT_ROUTER
*Entry
;
692 IP6_ROUTE_ENTRY
*RtEntry
;
694 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
695 ASSERT (Ip6Address
!= NULL
);
697 Entry
= AllocatePool (sizeof (IP6_DEFAULT_ROUTER
));
703 Entry
->Lifetime
= RouterLifetime
;
704 Entry
->NeighborCache
= Ip6FindNeighborEntry (IpSb
, Ip6Address
);
705 IP6_COPY_ADDRESS (&Entry
->Router
, Ip6Address
);
708 // Add a default route into route table with both Destination and PrefixLength set to zero.
710 RtEntry
= Ip6CreateRouteEntry (NULL
, 0, Ip6Address
);
711 if (RtEntry
== NULL
) {
716 InsertHeadList (&IpSb
->RouteTable
->RouteArea
[0], &RtEntry
->Link
);
717 IpSb
->RouteTable
->TotalNum
++;
719 InsertTailList (&IpSb
->DefaultRouterList
, &Entry
->Link
);
725 Destroy an IP6 default router entry.
727 @param[in] IpSb The pointer to the IP6_SERVICE instance.
728 @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER.
732 Ip6DestroyDefaultRouter (
733 IN IP6_SERVICE
*IpSb
,
734 IN IP6_DEFAULT_ROUTER
*DefaultRouter
739 RemoveEntryList (&DefaultRouter
->Link
);
742 // Update the Destination Cache - all entries using the time-out router as next-hop
743 // should perform next-hop determination again.
746 Status
= Ip6DelRoute (IpSb
->RouteTable
, NULL
, 0, &DefaultRouter
->Router
);
747 } while (Status
!= EFI_NOT_FOUND
);
749 FreePool (DefaultRouter
);
753 Clean an IP6 default router list.
755 @param[in] IpSb The pointer to the IP6_SERVICE instance.
759 Ip6CleanDefaultRouterList (
763 IP6_DEFAULT_ROUTER
*DefaultRouter
;
765 while (!IsListEmpty (&IpSb
->DefaultRouterList
)) {
766 DefaultRouter
= NET_LIST_HEAD (&IpSb
->DefaultRouterList
, IP6_DEFAULT_ROUTER
, Link
);
767 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
772 Search a default router node from an IP6 default router list.
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.
777 @return NULL if it failed to find the matching default router node.
778 Otherwise, point to the found default router node.
782 Ip6FindDefaultRouter (
783 IN IP6_SERVICE
*IpSb
,
784 IN EFI_IPv6_ADDRESS
*Ip6Address
788 IP6_DEFAULT_ROUTER
*DefaultRouter
;
790 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
791 ASSERT (Ip6Address
!= NULL
);
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
;
804 The function to be called after DAD (Duplicate Address Detection) is performed.
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.
813 IN BOOLEAN IsDadPassed
,
814 IN IP6_INTERFACE
*IpIf
,
815 IN IP6_DAD_ENTRY
*DadEntry
819 IP6_ADDRESS_INFO
*AddrInfo
;
820 EFI_DHCP6_PROTOCOL
*Dhcp6
;
822 EFI_DHCP6_PACKET_OPTION
*Oro
;
823 EFI_DHCP6_RETRANSMISSION InfoReqReXmit
;
824 EFI_IPv6_ADDRESS AllNodes
;
826 IpSb
= IpIf
->Service
;
827 AddrInfo
= DadEntry
->AddressInfo
;
833 if (NetIp6IsLinkLocalAddr (&AddrInfo
->Address
)) {
834 ASSERT (!IpSb
->LinkLocalOk
);
836 IP6_COPY_ADDRESS (&IpSb
->LinkLocalAddr
, &AddrInfo
->Address
);
837 IpSb
->LinkLocalOk
= TRUE
;
838 IpIf
->Configured
= TRUE
;
841 // Check whether DHCP6 need to be started.
843 Dhcp6
= IpSb
->Ip6ConfigInstance
.Dhcp6
;
845 if (IpSb
->Dhcp6NeedStart
) {
846 Dhcp6
->Start (Dhcp6
);
847 IpSb
->Dhcp6NeedStart
= FALSE
;
850 if (IpSb
->Dhcp6NeedInfoRequest
) {
852 // Set the exta options to send. Here we only want the option request option
855 Oro
= (EFI_DHCP6_PACKET_OPTION
*) OptBuf
;
856 Oro
->OpCode
= HTONS (IP6_CONFIG_DHCP6_OPTION_ORO
);
857 Oro
->OpLen
= HTONS (2);
858 *((UINT16
*) &Oro
->Data
[0]) = HTONS (IP6_CONFIG_DHCP6_OPTION_DNS_SERVERS
);
860 InfoReqReXmit
.Irt
= 4;
861 InfoReqReXmit
.Mrc
= 64;
862 InfoReqReXmit
.Mrt
= 60;
863 InfoReqReXmit
.Mrd
= 0;
872 IpSb
->Ip6ConfigInstance
.Dhcp6Event
,
873 Ip6ConfigOnDhcp6Reply
,
874 &IpSb
->Ip6ConfigInstance
879 // Add an on-link prefix for link-local address.
881 Ip6CreatePrefixListEntry (
884 (UINT32
) IP6_INFINIT_LIFETIME
,
885 (UINT32
) IP6_INFINIT_LIFETIME
,
886 IP6_LINK_LOCAL_PREFIX_LENGTH
,
892 // Global scope unicast address.
894 Ip6AddAddr (IpIf
, AddrInfo
);
897 // Add an on-link prefix for this address.
899 Ip6CreatePrefixListEntry (
902 AddrInfo
->ValidLifetime
,
903 AddrInfo
->PreferredLifetime
,
904 AddrInfo
->PrefixLength
,
908 IpIf
->Configured
= TRUE
;
912 // Leave the group we joined before.
914 Ip6LeaveGroup (IpSb
, &DadEntry
->Destination
);
917 if (DadEntry
->Callback
!= NULL
) {
918 DadEntry
->Callback (IsDadPassed
, &AddrInfo
->Address
, DadEntry
->Context
);
921 if (!IsDadPassed
&& NetIp6IsLinkLocalAddr (&AddrInfo
->Address
)) {
923 RemoveEntryList (&DadEntry
->Link
);
926 // Leave link-scope all-nodes multicast address (FF02::1)
928 Ip6SetToAllNodeMulticast (FALSE
, IP6_LINK_LOCAL_SCOPE
, &AllNodes
);
929 Ip6LeaveGroup (IpSb
, &AllNodes
);
931 // Disable IP operation since link-local address is a duplicate address.
933 IpSb
->LinkLocalDadFail
= TRUE
;
934 IpSb
->Mnp
->Configure (IpSb
->Mnp
, NULL
);
935 gBS
->SetTimer (IpSb
->Timer
, TimerCancel
, 0);
936 gBS
->SetTimer (IpSb
->FasterTimer
, TimerCancel
, 0);
940 if (!IsDadPassed
|| NetIp6IsLinkLocalAddr (&AddrInfo
->Address
)) {
942 // Free the AddressInfo we hold if DAD fails or it is a link-local address.
947 RemoveEntryList (&DadEntry
->Link
);
952 Create a DAD (Duplicate Address Detection) entry and queue it to be performed.
954 @param[in] IpIf Points to the IP6_INTERFACE.
955 @param[in] AddressInfo The address information which needs DAD performed.
956 @param[in] Callback The callback routine that will be called after DAD
957 is performed. This is an optional parameter that
959 @param[in] Context The opaque parameter for a DAD callback routine.
960 This is an optional parameter that may be NULL.
962 @retval EFI_SUCCESS The DAD entry was created and queued.
963 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory to complete the
970 IN IP6_INTERFACE
*IpIf
,
971 IN IP6_ADDRESS_INFO
*AddressInfo
,
972 IN IP6_DAD_CALLBACK Callback OPTIONAL
,
973 IN VOID
*Context OPTIONAL
976 IP6_DAD_ENTRY
*Entry
;
977 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS
*DadXmits
;
982 NET_CHECK_SIGNATURE (IpIf
, IP6_INTERFACE_SIGNATURE
);
983 ASSERT (AddressInfo
!= NULL
);
986 // Do nothing if we have already started DAD on the address.
988 if (Ip6FindDADEntry (IpIf
->Service
, &AddressInfo
->Address
, NULL
) != NULL
) {
992 Status
= EFI_SUCCESS
;
993 IpSb
= IpIf
->Service
;
994 DadXmits
= &IpSb
->Ip6ConfigInstance
.DadXmits
;
997 // Allocate the resources and insert info
999 Entry
= AllocatePool (sizeof (IP6_DAD_ENTRY
));
1000 if (Entry
== NULL
) {
1001 return EFI_OUT_OF_RESOURCES
;
1005 // Map the incoming unicast address to solicited-node multicast address
1007 Ip6CreateSNMulticastAddr (&AddressInfo
->Address
, &Entry
->Destination
);
1010 // Join in the solicited-node multicast address.
1012 Status
= Ip6JoinGroup (IpSb
, IpIf
, &Entry
->Destination
);
1013 if (EFI_ERROR (Status
)) {
1018 Entry
->Signature
= IP6_DAD_ENTRY_SIGNATURE
;
1019 Entry
->MaxTransmit
= DadXmits
->DupAddrDetectTransmits
;
1020 Entry
->Transmit
= 0;
1022 MaxDelayTick
= IP6_MAX_RTR_SOLICITATION_DELAY
/ IP6_TIMER_INTERVAL_IN_MS
;
1023 Entry
->RetransTick
= (MaxDelayTick
* ((NET_RANDOM (NetRandomInitSeed ()) % 5) + 1)) / 5;
1024 Entry
->AddressInfo
= AddressInfo
;
1025 Entry
->Callback
= Callback
;
1026 Entry
->Context
= Context
;
1027 InsertTailList (&IpIf
->DupAddrDetectList
, &Entry
->Link
);
1029 if (Entry
->MaxTransmit
== 0) {
1031 // DAD is disabled on this interface, immediately mark this DAD successful.
1033 Ip6OnDADFinished (TRUE
, IpIf
, Entry
);
1040 Search IP6_DAD_ENTRY from the Duplicate Address Detection List.
1042 @param[in] IpSb The pointer to the IP6_SERVICE instance.
1043 @param[in] Target The address information which needs DAD performed .
1044 @param[out] Interface If not NULL, output the IP6 interface that configures
1045 the tentative address.
1047 @return NULL if failed to find the matching DAD entry.
1048 Otherwise, point to the found DAD entry.
1053 IN IP6_SERVICE
*IpSb
,
1054 IN EFI_IPv6_ADDRESS
*Target
,
1055 OUT IP6_INTERFACE
**Interface OPTIONAL
1060 IP6_INTERFACE
*IpIf
;
1061 IP6_DAD_ENTRY
*DupAddrDetect
;
1062 IP6_ADDRESS_INFO
*AddrInfo
;
1064 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1065 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP6_INTERFACE
, Link
);
1067 NET_LIST_FOR_EACH (Entry2
, &IpIf
->DupAddrDetectList
) {
1068 DupAddrDetect
= NET_LIST_USER_STRUCT_S (Entry2
, IP6_DAD_ENTRY
, Link
, IP6_DAD_ENTRY_SIGNATURE
);
1069 AddrInfo
= DupAddrDetect
->AddressInfo
;
1070 if (EFI_IP6_EQUAL (&AddrInfo
->Address
, Target
)) {
1071 if (Interface
!= NULL
) {
1074 return DupAddrDetect
;
1083 Generate router solicit message and send it out to Destination Address or
1084 All Router Link Local scope multicast address.
1086 @param[in] IpSb The IP service to send the packet.
1087 @param[in] Interface If not NULL, points to the IP6 interface to send
1089 @param[in] SourceAddress If not NULL, the source address of the message.
1090 @param[in] DestinationAddress If not NULL, the destination address of the message.
1091 @param[in] SourceLinkAddress If not NULL, the MAC address of the source.
1092 A source link-layer address option will be appended
1095 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1097 @retval EFI_SUCCESS The router solicit message was successfully sent.
1101 Ip6SendRouterSolicit (
1102 IN IP6_SERVICE
*IpSb
,
1103 IN IP6_INTERFACE
*Interface OPTIONAL
,
1104 IN EFI_IPv6_ADDRESS
*SourceAddress OPTIONAL
,
1105 IN EFI_IPv6_ADDRESS
*DestinationAddress OPTIONAL
,
1106 IN EFI_MAC_ADDRESS
*SourceLinkAddress OPTIONAL
1110 EFI_IP6_HEADER Head
;
1111 IP6_ICMP_INFORMATION_HEAD
*IcmpHead
;
1112 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
1114 IP6_INTERFACE
*IpIf
;
1116 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1119 if (IpIf
== NULL
&& IpSb
->DefaultInterface
!= NULL
) {
1120 IpIf
= IpSb
->DefaultInterface
;
1124 // Generate the packet to be sent
1127 PayloadLen
= (UINT16
) sizeof (IP6_ICMP_INFORMATION_HEAD
);
1128 if (SourceLinkAddress
!= NULL
) {
1129 PayloadLen
+= sizeof (IP6_ETHER_ADDR_OPTION
);
1132 Packet
= NetbufAlloc (sizeof (EFI_IP6_HEADER
) + (UINT32
) PayloadLen
);
1133 if (Packet
== NULL
) {
1134 return EFI_OUT_OF_RESOURCES
;
1138 // Create the basic IPv6 header.
1140 Head
.FlowLabelL
= 0;
1141 Head
.FlowLabelH
= 0;
1142 Head
.PayloadLength
= HTONS (PayloadLen
);
1143 Head
.NextHeader
= IP6_ICMP
;
1144 Head
.HopLimit
= IP6_HOP_LIMIT
;
1146 if (SourceAddress
!= NULL
) {
1147 IP6_COPY_ADDRESS (&Head
.SourceAddress
, SourceAddress
);
1149 ZeroMem (&Head
.SourceAddress
, sizeof (EFI_IPv6_ADDRESS
));
1153 if (DestinationAddress
!= NULL
) {
1154 IP6_COPY_ADDRESS (&Head
.DestinationAddress
, DestinationAddress
);
1156 Ip6SetToAllNodeMulticast (TRUE
, IP6_LINK_LOCAL_SCOPE
, &Head
.DestinationAddress
);
1159 NetbufReserve (Packet
, sizeof (EFI_IP6_HEADER
));
1162 // Fill in the ICMP header, and Source link-layer address if contained.
1165 IcmpHead
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufAllocSpace (Packet
, sizeof (IP6_ICMP_INFORMATION_HEAD
), FALSE
);
1166 ASSERT (IcmpHead
!= NULL
);
1167 ZeroMem (IcmpHead
, sizeof (IP6_ICMP_INFORMATION_HEAD
));
1168 IcmpHead
->Head
.Type
= ICMP_V6_ROUTER_SOLICIT
;
1169 IcmpHead
->Head
.Code
= 0;
1171 LinkLayerOption
= NULL
;
1172 if (SourceLinkAddress
!= NULL
) {
1173 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) NetbufAllocSpace (
1175 sizeof (IP6_ETHER_ADDR_OPTION
),
1178 ASSERT (LinkLayerOption
!= NULL
);
1179 LinkLayerOption
->Type
= Ip6OptionEtherSource
;
1180 LinkLayerOption
->Length
= (UINT8
) sizeof (IP6_ETHER_ADDR_OPTION
);
1181 CopyMem (LinkLayerOption
->EtherAddr
, SourceLinkAddress
, 6);
1185 // Transmit the packet
1187 return Ip6Output (IpSb
, IpIf
, NULL
, Packet
, &Head
, NULL
, 0, Ip6SysPacketSent
, NULL
);
1191 Generate a Neighbor Advertisement message and send it out to Destination Address.
1193 @param[in] IpSb The IP service to send the packet.
1194 @param[in] SourceAddress The source address of the message.
1195 @param[in] DestinationAddress The destination address of the message.
1196 @param[in] TargetIp6Address The target address field in the Neighbor Solicitation
1197 message that prompted this advertisement.
1198 @param[in] TargetLinkAddress The MAC address for the target, i.e. the sender
1199 of the advertisement.
1200 @param[in] IsRouter If TRUE, indicates the sender is a router.
1201 @param[in] Override If TRUE, indicates the advertisement should override
1202 an existing cache entry and update the MAC address.
1203 @param[in] Solicited If TRUE, indicates the advertisement was sent
1204 in response to a Neighbor Solicitation from
1205 the Destination address.
1207 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1209 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1213 Ip6SendNeighborAdvertise (
1214 IN IP6_SERVICE
*IpSb
,
1215 IN EFI_IPv6_ADDRESS
*SourceAddress
,
1216 IN EFI_IPv6_ADDRESS
*DestinationAddress
,
1217 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
1218 IN EFI_MAC_ADDRESS
*TargetLinkAddress
,
1219 IN BOOLEAN IsRouter
,
1220 IN BOOLEAN Override
,
1221 IN BOOLEAN Solicited
1225 EFI_IP6_HEADER Head
;
1226 IP6_ICMP_INFORMATION_HEAD
*IcmpHead
;
1227 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
1228 EFI_IPv6_ADDRESS
*Target
;
1231 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1234 // The Neighbor Advertisement message must include a Target link-layer address option
1235 // when responding to multicast solicitation and should include such option when
1236 // responding to unicast solicitation. It also must include such option as unsolicited
1239 ASSERT (DestinationAddress
!= NULL
&& TargetIp6Address
!= NULL
&& TargetLinkAddress
!= NULL
);
1241 PayloadLen
= (UINT16
) (sizeof (IP6_ICMP_INFORMATION_HEAD
) + sizeof (EFI_IPv6_ADDRESS
) + sizeof (IP6_ETHER_ADDR_OPTION
));
1244 // Generate the packet to be sent
1247 Packet
= NetbufAlloc (sizeof (EFI_IP6_HEADER
) + (UINT32
) PayloadLen
);
1248 if (Packet
== NULL
) {
1249 return EFI_OUT_OF_RESOURCES
;
1253 // Create the basic IPv6 header.
1255 Head
.FlowLabelL
= 0;
1256 Head
.FlowLabelH
= 0;
1257 Head
.PayloadLength
= HTONS (PayloadLen
);
1258 Head
.NextHeader
= IP6_ICMP
;
1259 Head
.HopLimit
= IP6_HOP_LIMIT
;
1261 IP6_COPY_ADDRESS (&Head
.SourceAddress
, SourceAddress
);
1262 IP6_COPY_ADDRESS (&Head
.DestinationAddress
, DestinationAddress
);
1264 NetbufReserve (Packet
, sizeof (EFI_IP6_HEADER
));
1267 // Fill in the ICMP header, Target address, and Target link-layer address.
1268 // Set the Router flag, Solicited flag and Override flag.
1271 IcmpHead
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufAllocSpace (Packet
, sizeof (IP6_ICMP_INFORMATION_HEAD
), FALSE
);
1272 ASSERT (IcmpHead
!= NULL
);
1273 ZeroMem (IcmpHead
, sizeof (IP6_ICMP_INFORMATION_HEAD
));
1274 IcmpHead
->Head
.Type
= ICMP_V6_NEIGHBOR_ADVERTISE
;
1275 IcmpHead
->Head
.Code
= 0;
1278 IcmpHead
->Fourth
|= IP6_IS_ROUTER_FLAG
;
1282 IcmpHead
->Fourth
|= IP6_SOLICITED_FLAG
;
1286 IcmpHead
->Fourth
|= IP6_OVERRIDE_FLAG
;
1289 Target
= (EFI_IPv6_ADDRESS
*) NetbufAllocSpace (Packet
, sizeof (EFI_IPv6_ADDRESS
), FALSE
);
1290 ASSERT (Target
!= NULL
);
1291 IP6_COPY_ADDRESS (Target
, TargetIp6Address
);
1293 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) NetbufAllocSpace (
1295 sizeof (IP6_ETHER_ADDR_OPTION
),
1298 ASSERT (LinkLayerOption
!= NULL
);
1299 LinkLayerOption
->Type
= Ip6OptionEtherTarget
;
1300 LinkLayerOption
->Length
= 1;
1301 CopyMem (LinkLayerOption
->EtherAddr
, TargetLinkAddress
, 6);
1304 // Transmit the packet
1306 return Ip6Output (IpSb
, NULL
, NULL
, Packet
, &Head
, NULL
, 0, Ip6SysPacketSent
, NULL
);
1310 Generate the Neighbor Solicitation message and send it to the Destination Address.
1312 @param[in] IpSb The IP service to send the packet
1313 @param[in] SourceAddress The source address of the message.
1314 @param[in] DestinationAddress The destination address of the message.
1315 @param[in] TargetIp6Address The IP address of the target of the solicitation.
1316 It must not be a multicast address.
1317 @param[in] SourceLinkAddress The MAC address for the sender. If not NULL,
1318 a source link-layer address option will be appended
1321 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1322 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1324 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1328 Ip6SendNeighborSolicit (
1329 IN IP6_SERVICE
*IpSb
,
1330 IN EFI_IPv6_ADDRESS
*SourceAddress
,
1331 IN EFI_IPv6_ADDRESS
*DestinationAddress
,
1332 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
1333 IN EFI_MAC_ADDRESS
*SourceLinkAddress OPTIONAL
1337 EFI_IP6_HEADER Head
;
1338 IP6_ICMP_INFORMATION_HEAD
*IcmpHead
;
1339 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
1340 EFI_IPv6_ADDRESS
*Target
;
1343 IP6_NEIGHBOR_ENTRY
*Neighbor
;
1346 // Check input parameters
1348 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1349 if (DestinationAddress
== NULL
|| TargetIp6Address
== NULL
) {
1350 return EFI_INVALID_PARAMETER
;
1355 if (SourceAddress
== NULL
|| (SourceAddress
!= NULL
&& NetIp6IsUnspecifiedAddr (SourceAddress
))) {
1360 // The Neighbor Solicitation message should include a source link-layer address option
1361 // if the solicitation is not sent by performing DAD - Duplicate Address Detection.
1362 // Otherwise must not include it.
1364 PayloadLen
= (UINT16
) (sizeof (IP6_ICMP_INFORMATION_HEAD
) + sizeof (EFI_IPv6_ADDRESS
));
1367 if (SourceLinkAddress
== NULL
) {
1368 return EFI_INVALID_PARAMETER
;
1371 PayloadLen
= (UINT16
) (PayloadLen
+ sizeof (IP6_ETHER_ADDR_OPTION
));
1375 // Generate the packet to be sent
1378 Packet
= NetbufAlloc (sizeof (EFI_IP6_HEADER
) + (UINT32
) PayloadLen
);
1379 if (Packet
== NULL
) {
1380 return EFI_OUT_OF_RESOURCES
;
1384 // Create the basic IPv6 header
1386 Head
.FlowLabelL
= 0;
1387 Head
.FlowLabelH
= 0;
1388 Head
.PayloadLength
= HTONS (PayloadLen
);
1389 Head
.NextHeader
= IP6_ICMP
;
1390 Head
.HopLimit
= IP6_HOP_LIMIT
;
1392 if (SourceAddress
!= NULL
) {
1393 IP6_COPY_ADDRESS (&Head
.SourceAddress
, SourceAddress
);
1395 ZeroMem (&Head
.SourceAddress
, sizeof (EFI_IPv6_ADDRESS
));
1398 IP6_COPY_ADDRESS (&Head
.DestinationAddress
, DestinationAddress
);
1400 NetbufReserve (Packet
, sizeof (EFI_IP6_HEADER
));
1403 // Fill in the ICMP header, Target address, and Source link-layer address.
1405 IcmpHead
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufAllocSpace (Packet
, sizeof (IP6_ICMP_INFORMATION_HEAD
), FALSE
);
1406 ASSERT (IcmpHead
!= NULL
);
1407 ZeroMem (IcmpHead
, sizeof (IP6_ICMP_INFORMATION_HEAD
));
1408 IcmpHead
->Head
.Type
= ICMP_V6_NEIGHBOR_SOLICIT
;
1409 IcmpHead
->Head
.Code
= 0;
1411 Target
= (EFI_IPv6_ADDRESS
*) NetbufAllocSpace (Packet
, sizeof (EFI_IPv6_ADDRESS
), FALSE
);
1412 ASSERT (Target
!= NULL
);
1413 IP6_COPY_ADDRESS (Target
, TargetIp6Address
);
1415 LinkLayerOption
= NULL
;
1419 // Fill in the source link-layer address option
1421 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) NetbufAllocSpace (
1423 sizeof (IP6_ETHER_ADDR_OPTION
),
1426 ASSERT (LinkLayerOption
!= NULL
);
1427 LinkLayerOption
->Type
= Ip6OptionEtherSource
;
1428 LinkLayerOption
->Length
= 1;
1429 CopyMem (LinkLayerOption
->EtherAddr
, SourceLinkAddress
, 6);
1433 // Create a Neighbor Cache entry in the INCOMPLETE state when performing
1434 // address resolution.
1436 if (!IsDAD
&& Ip6IsSNMulticastAddr (DestinationAddress
)) {
1437 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
1438 if (Neighbor
== NULL
) {
1439 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, TargetIp6Address
, NULL
);
1440 ASSERT (Neighbor
!= NULL
);
1445 // Transmit the packet
1447 return Ip6Output (IpSb
, IpSb
->DefaultInterface
, NULL
, Packet
, &Head
, NULL
, 0, Ip6SysPacketSent
, NULL
);
1451 Process the Neighbor Solicitation message. The message may be sent for Duplicate
1452 Address Detection or Address Resolution.
1454 @param[in] IpSb The IP service that received the packet.
1455 @param[in] Head The IP head of the message.
1456 @param[in] Packet The content of the message with IP head removed.
1458 @retval EFI_SUCCESS The packet processed successfully.
1459 @retval EFI_INVALID_PARAMETER The packet is invalid.
1460 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1461 @retval Others Failed to process the packet.
1465 Ip6ProcessNeighborSolicit (
1466 IN IP6_SERVICE
*IpSb
,
1467 IN EFI_IP6_HEADER
*Head
,
1471 IP6_ICMP_INFORMATION_HEAD Icmp
;
1472 EFI_IPv6_ADDRESS Target
;
1473 IP6_ETHER_ADDR_OPTION LinkLayerOption
;
1476 BOOLEAN IsMaintained
;
1477 IP6_DAD_ENTRY
*DupAddrDetect
;
1478 IP6_INTERFACE
*IpIf
;
1479 IP6_NEIGHBOR_ENTRY
*Neighbor
;
1481 BOOLEAN UpdateCache
;
1482 EFI_IPv6_ADDRESS Dest
;
1489 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1490 NetbufCopy (Packet
, sizeof (Icmp
), sizeof (Target
), Target
.Addr
);
1493 // Perform Message Validation:
1494 // The IP Hop Limit field has a value of 255, i.e., the packet
1495 // could not possibly have been forwarded by a router.
1497 // Target Address is not a multicast address.
1499 Status
= EFI_INVALID_PARAMETER
;
1501 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
.Head
.Code
!= 0 || !NetIp6IsValidUnicast (&Target
)) {
1506 // ICMP length is 24 or more octets.
1509 if (Head
->PayloadLength
< IP6_ND_LENGTH
) {
1512 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_ND_LENGTH
);
1513 if (OptionLen
!= 0) {
1514 Option
= NetbufGetByte (Packet
, IP6_ND_LENGTH
, NULL
);
1515 ASSERT (Option
!= NULL
);
1518 // All included options should have a length that is greater than zero.
1520 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
1526 IsDAD
= NetIp6IsUnspecifiedAddr (&Head
->SourceAddress
);
1527 IsUnicast
= (BOOLEAN
) !Ip6IsSNMulticastAddr (&Head
->DestinationAddress
);
1528 IsMaintained
= Ip6IsOneOfSetAddress (IpSb
, &Target
, &IpIf
, NULL
);
1531 if (OptionLen
>= sizeof (IP6_ETHER_ADDR_OPTION
)) {
1535 sizeof (IP6_ETHER_ADDR_OPTION
),
1536 (UINT8
*) &LinkLayerOption
1539 // The solicitation for neighbor discovery should include a source link-layer
1540 // address option. If the option is not recognized, silently ignore it.
1542 if (LinkLayerOption
.Type
== Ip6OptionEtherSource
) {
1545 // If the IP source address is the unspecified address, the source
1546 // link-layer address option must not be included in the message.
1556 // If the IP source address is the unspecified address, the IP
1557 // destination address is a solicited-node multicast address.
1559 if (IsDAD
&& IsUnicast
) {
1564 // If the target address is tentative, and the source address is a unicast address,
1565 // the solicitation's sender is performing address resolution on the target;
1566 // the solicitation should be silently ignored.
1568 if (!IsDAD
&& !IsMaintained
) {
1573 // If received unicast neighbor solicitation but destination is not this node,
1576 if (IsUnicast
&& !IsMaintained
) {
1581 // In DAD, when target address is a tentative address,
1582 // process the received neighbor solicitation message but not send out response.
1584 if (IsDAD
&& !IsMaintained
) {
1585 DupAddrDetect
= Ip6FindDADEntry (IpSb
, &Target
, &IpIf
);
1586 if (DupAddrDetect
!= NULL
) {
1588 // Check the MAC address of the incoming packet.
1590 if (IpSb
->RecvRequest
.MnpToken
.Packet
.RxData
== NULL
) {
1594 MacAddress
= IpSb
->RecvRequest
.MnpToken
.Packet
.RxData
->SourceAddress
;
1595 if (MacAddress
!= NULL
) {
1598 &IpSb
->SnpMode
.CurrentAddress
,
1599 IpSb
->SnpMode
.HwAddressSize
1602 // The NS is from another node to performing DAD on the same address.
1603 // Fail DAD for the tentative address.
1605 Ip6OnDADFinished (FALSE
, IpIf
, DupAddrDetect
);
1606 Status
= EFI_ICMP_ERROR
;
1609 // The below layer loopback the NS we sent. Record it and wait for more.
1611 DupAddrDetect
->Receive
++;
1612 Status
= EFI_SUCCESS
;
1620 // If the solicitation does not contain a link-layer address, DO NOT create or
1621 // update the neighbor cache entries.
1624 Neighbor
= Ip6FindNeighborEntry (IpSb
, &Head
->SourceAddress
);
1625 UpdateCache
= FALSE
;
1627 if (Neighbor
== NULL
) {
1628 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, &Head
->SourceAddress
, NULL
);
1629 if (Neighbor
== NULL
) {
1630 Status
= EFI_OUT_OF_RESOURCES
;
1635 if (CompareMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6) != 0) {
1641 Neighbor
->State
= EfiNeighborStale
;
1642 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1643 CopyMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1645 // Send queued packets if exist.
1647 Neighbor
->CallBack ((VOID
*) Neighbor
);
1652 // Sends a Neighbor Advertisement as response.
1653 // Set the Router flag to zero since the node is a host.
1654 // If the source address of the solicitation is unspeicifed, and target address
1655 // is one of the maintained address, reply a unsolicited multicast advertisement.
1657 if (IsDAD
&& IsMaintained
) {
1659 Ip6SetToAllNodeMulticast (FALSE
, IP6_LINK_LOCAL_SCOPE
, &Dest
);
1662 IP6_COPY_ADDRESS (&Dest
, &Head
->SourceAddress
);
1665 Status
= Ip6SendNeighborAdvertise (
1670 &IpSb
->SnpMode
.CurrentAddress
,
1676 NetbufFree (Packet
);
1681 Process the Neighbor Advertisement message.
1683 @param[in] IpSb The IP service that received the packet.
1684 @param[in] Head The IP head of the message.
1685 @param[in] Packet The content of the message with IP head removed.
1687 @retval EFI_SUCCESS The packet processed successfully.
1688 @retval EFI_INVALID_PARAMETER The packet is invalid.
1689 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1690 @retval Others Failed to process the packet.
1694 Ip6ProcessNeighborAdvertise (
1695 IN IP6_SERVICE
*IpSb
,
1696 IN EFI_IP6_HEADER
*Head
,
1700 IP6_ICMP_INFORMATION_HEAD Icmp
;
1701 EFI_IPv6_ADDRESS Target
;
1702 IP6_ETHER_ADDR_OPTION LinkLayerOption
;
1705 IP6_NEIGHBOR_ENTRY
*Neighbor
;
1706 IP6_DEFAULT_ROUTER
*DefaultRouter
;
1710 IP6_DAD_ENTRY
*DupAddrDetect
;
1711 IP6_INTERFACE
*IpIf
;
1716 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1717 NetbufCopy (Packet
, sizeof (Icmp
), sizeof (Target
), Target
.Addr
);
1720 // Validate the incoming Neighbor Advertisement
1722 Status
= EFI_INVALID_PARAMETER
;
1724 // The IP Hop Limit field has a value of 255, i.e., the packet
1725 // could not possibly have been forwarded by a router.
1727 // Target Address is not a multicast address.
1729 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
.Head
.Code
!= 0 || !NetIp6IsValidUnicast (&Target
)) {
1734 // ICMP length is 24 or more octets.
1738 if (Head
->PayloadLength
< IP6_ND_LENGTH
) {
1741 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_ND_LENGTH
);
1742 if (OptionLen
!= 0) {
1743 Option
= NetbufGetByte (Packet
, IP6_ND_LENGTH
, NULL
);
1744 ASSERT (Option
!= NULL
);
1747 // All included options should have a length that is greater than zero.
1749 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
1756 // If the IP destination address is a multicast address, Solicited Flag is ZERO.
1759 if ((Icmp
.Fourth
& IP6_SOLICITED_FLAG
) == IP6_SOLICITED_FLAG
) {
1762 if (IP6_IS_MULTICAST (&Head
->DestinationAddress
) && Solicited
) {
1767 // DAD - Check whether the Target is one of our tentative address.
1769 DupAddrDetect
= Ip6FindDADEntry (IpSb
, &Target
, &IpIf
);
1770 if (DupAddrDetect
!= NULL
) {
1772 // DAD fails, some other node is using this address.
1774 NetbufFree (Packet
);
1775 Ip6OnDADFinished (FALSE
, IpIf
, DupAddrDetect
);
1776 return EFI_ICMP_ERROR
;
1780 // Search the Neighbor Cache for the target's entry. If no entry exists,
1781 // the advertisement should be silently discarded.
1783 Neighbor
= Ip6FindNeighborEntry (IpSb
, &Target
);
1784 if (Neighbor
== NULL
) {
1789 // Get IsRouter Flag and Override Flag
1793 if ((Icmp
.Fourth
& IP6_IS_ROUTER_FLAG
) == IP6_IS_ROUTER_FLAG
) {
1796 if ((Icmp
.Fourth
& IP6_OVERRIDE_FLAG
) == IP6_OVERRIDE_FLAG
) {
1801 // Check whether link layer option is included.
1803 if (OptionLen
>= sizeof (IP6_ETHER_ADDR_OPTION
)) {
1807 sizeof (IP6_ETHER_ADDR_OPTION
),
1808 (UINT8
*) &LinkLayerOption
1811 if (LinkLayerOption
.Type
== Ip6OptionEtherTarget
) {
1818 Compare
= CompareMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1821 if (!Neighbor
->IsRouter
&& IsRouter
) {
1822 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &Target
);
1823 if (DefaultRouter
!= NULL
) {
1824 DefaultRouter
->NeighborCache
= Neighbor
;
1828 if (Neighbor
->State
== EfiNeighborInComplete
) {
1830 // If the target's Neighbor Cache entry is in INCOMPLETE state and no
1831 // Target Link-Layer address option is included while link layer has
1832 // address, the message should be silently discarded.
1838 // Update the Neighbor Cache
1840 CopyMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1842 Neighbor
->State
= EfiNeighborReachable
;
1843 Neighbor
->Ticks
= IP6_GET_TICKS (IpSb
->ReachableTime
);
1845 Neighbor
->State
= EfiNeighborStale
;
1846 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1848 // Send any packets queued for the neighbor awaiting address resolution.
1850 Neighbor
->CallBack ((VOID
*) Neighbor
);
1853 Neighbor
->IsRouter
= IsRouter
;
1856 if (!Override
&& Compare
!= 0) {
1858 // When the Override Flag is clear and supplied link-layer address differs from
1859 // that in the cache, if the state of the entry is not REACHABLE, ignore the
1860 // message. Otherwise set it to STALE but do not update the entry in any
1863 if (Neighbor
->State
== EfiNeighborReachable
) {
1864 Neighbor
->State
= EfiNeighborStale
;
1865 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1869 CopyMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1872 // Update the entry's state
1875 Neighbor
->State
= EfiNeighborReachable
;
1876 Neighbor
->Ticks
= IP6_GET_TICKS (IpSb
->ReachableTime
);
1879 Neighbor
->State
= EfiNeighborStale
;
1880 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1885 // When IsRouter is changed from TRUE to FALSE, remove the router from the
1886 // Default Router List and remove the Destination Cache entries for all destinations
1887 // using the neighbor as a router.
1889 if (Neighbor
->IsRouter
&& !IsRouter
) {
1890 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &Target
);
1891 if (DefaultRouter
!= NULL
) {
1892 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
1896 Neighbor
->IsRouter
= IsRouter
;
1900 if (Neighbor
->State
== EfiNeighborReachable
) {
1901 Neighbor
->CallBack ((VOID
*) Neighbor
);
1904 Status
= EFI_SUCCESS
;
1907 NetbufFree (Packet
);
1912 Process the Router Advertisement message according to RFC4861.
1914 @param[in] IpSb The IP service that received the packet.
1915 @param[in] Head The IP head of the message.
1916 @param[in] Packet The content of the message with the IP head removed.
1918 @retval EFI_SUCCESS The packet processed successfully.
1919 @retval EFI_INVALID_PARAMETER The packet is invalid.
1920 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1922 @retval Others Failed to process the packet.
1926 Ip6ProcessRouterAdvertise (
1927 IN IP6_SERVICE
*IpSb
,
1928 IN EFI_IP6_HEADER
*Head
,
1932 IP6_ICMP_INFORMATION_HEAD Icmp
;
1933 UINT32 ReachableTime
;
1934 UINT32 RetransTimer
;
1935 UINT16 RouterLifetime
;
1939 IP6_ETHER_ADDR_OPTION LinkLayerOption
;
1944 IP6_DEFAULT_ROUTER
*DefaultRouter
;
1945 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
1946 EFI_MAC_ADDRESS LinkLayerAddress
;
1947 IP6_MTU_OPTION MTUOption
;
1948 IP6_PREFIX_INFO_OPTION PrefixOption
;
1949 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
1952 EFI_IPv6_ADDRESS StatelessAddress
;
1958 Status
= EFI_INVALID_PARAMETER
;
1960 if (IpSb
->Ip6ConfigInstance
.Policy
!= Ip6ConfigPolicyAutomatic
) {
1962 // Skip the process below as it's not required under the current policy.
1967 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1970 // Validate the incoming Router Advertisement
1974 // The IP source address must be a link-local address
1976 if (!NetIp6IsLinkLocalAddr (&Head
->SourceAddress
)) {
1980 // The IP Hop Limit field has a value of 255, i.e. the packet
1981 // could not possibly have been forwarded by a router.
1983 // ICMP length (derived from the IP length) is 16 or more octets.
1985 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
.Head
.Code
!= 0 ||
1986 Head
->PayloadLength
< IP6_RA_LENGTH
) {
1991 // All included options have a length that is greater than zero.
1993 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_RA_LENGTH
);
1994 if (OptionLen
!= 0) {
1995 Option
= NetbufGetByte (Packet
, IP6_RA_LENGTH
, NULL
);
1996 ASSERT (Option
!= NULL
);
1998 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
2004 // Process Fourth field.
2005 // In Router Advertisement, Fourth is composed of CurHopLimit (8bit), M flag, O flag,
2006 // and Router Lifetime (16 bit).
2009 Fourth
= NTOHL (Icmp
.Fourth
);
2010 CopyMem (&RouterLifetime
, &Fourth
, sizeof (UINT16
));
2013 // If the source address already in the default router list, update it.
2014 // Otherwise create a new entry.
2015 // A Lifetime of zero indicates that the router is not a default router.
2017 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &Head
->SourceAddress
);
2018 if (DefaultRouter
== NULL
) {
2019 if (RouterLifetime
!= 0) {
2020 DefaultRouter
= Ip6CreateDefaultRouter (IpSb
, &Head
->SourceAddress
, RouterLifetime
);
2021 if (DefaultRouter
== NULL
) {
2022 Status
= EFI_OUT_OF_RESOURCES
;
2027 if (RouterLifetime
!= 0) {
2028 DefaultRouter
->Lifetime
= RouterLifetime
;
2030 // Check the corresponding neighbor cache entry here.
2032 if (DefaultRouter
->NeighborCache
== NULL
) {
2033 DefaultRouter
->NeighborCache
= Ip6FindNeighborEntry (IpSb
, &Head
->SourceAddress
);
2037 // If the address is in the host's default router list and the router lifetime is zero,
2038 // immediately time-out the entry.
2040 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
2044 CurHopLimit
= *((UINT8
*) &Fourth
+ 3);
2045 if (CurHopLimit
!= 0) {
2046 IpSb
->CurHopLimit
= CurHopLimit
;
2051 if ((*((UINT8
*) &Fourth
+ 2) & IP6_M_ADDR_CONFIG_FLAG
) == IP6_M_ADDR_CONFIG_FLAG
) {
2054 if ((*((UINT8
*) &Fourth
+ 2) & IP6_O_CONFIG_FLAG
) == IP6_O_CONFIG_FLAG
) {
2059 if (Mflag
|| Oflag
) {
2061 // Use Ip6Config to get available addresses or other configuration from DHCP.
2063 Ip6ConfigStartStatefulAutoConfig (&IpSb
->Ip6ConfigInstance
, Oflag
);
2067 // Process Reachable Time and Retrans Timer fields.
2069 NetbufCopy (Packet
, sizeof (Icmp
), sizeof (UINT32
), (UINT8
*) &ReachableTime
);
2070 NetbufCopy (Packet
, sizeof (Icmp
) + sizeof (UINT32
), sizeof (UINT32
), (UINT8
*) &RetransTimer
);
2071 ReachableTime
= NTOHL (ReachableTime
);
2072 RetransTimer
= NTOHL (RetransTimer
);
2074 if (ReachableTime
!= 0 && ReachableTime
!= IpSb
->BaseReachableTime
) {
2076 // If new value is not unspecified and differs from the previous one, record it
2077 // in BaseReachableTime and recompute a ReachableTime.
2079 IpSb
->BaseReachableTime
= ReachableTime
;
2080 Ip6UpdateReachableTime (IpSb
);
2083 if (RetransTimer
!= 0) {
2084 IpSb
->RetransTimer
= RetransTimer
;
2088 // IsRouter flag must be set to TRUE if corresponding neighbor cache entry exists.
2090 NeighborCache
= Ip6FindNeighborEntry (IpSb
, &Head
->SourceAddress
);
2091 if (NeighborCache
!= NULL
) {
2092 NeighborCache
->IsRouter
= TRUE
;
2096 // If an valid router advertisment is received, stops router solicitation.
2098 IpSb
->RouterAdvertiseReceived
= TRUE
;
2101 // The only defined options that may appear are the Source
2102 // Link-Layer Address, Prefix information and MTU options.
2103 // All included options have a length that is greater than zero.
2106 while (Offset
< Head
->PayloadLength
) {
2107 NetbufCopy (Packet
, Offset
, sizeof (UINT8
), &Type
);
2109 case Ip6OptionEtherSource
:
2111 // Update the neighbor cache
2113 NetbufCopy (Packet
, Offset
, sizeof (IP6_ETHER_ADDR_OPTION
), (UINT8
*) &LinkLayerOption
);
2114 if (LinkLayerOption
.Length
<= 0) {
2118 ZeroMem (&LinkLayerAddress
, sizeof (EFI_MAC_ADDRESS
));
2119 CopyMem (&LinkLayerAddress
, LinkLayerOption
.EtherAddr
, 6);
2121 if (NeighborCache
== NULL
) {
2122 NeighborCache
= Ip6CreateNeighborEntry (
2125 &Head
->SourceAddress
,
2128 if (NeighborCache
== NULL
) {
2129 Status
= EFI_OUT_OF_RESOURCES
;
2132 NeighborCache
->IsRouter
= TRUE
;
2133 NeighborCache
->State
= EfiNeighborStale
;
2134 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2136 Result
= CompareMem (&LinkLayerAddress
, &NeighborCache
->LinkAddress
, 6);
2139 // If the link-local address is the same as that already in the cache,
2140 // the cache entry's state remains unchanged. Otherwise update the
2141 // reachability state to STALE.
2143 if ((NeighborCache
->State
== EfiNeighborInComplete
) || (Result
!= 0)) {
2144 CopyMem (&NeighborCache
->LinkAddress
, &LinkLayerAddress
, 6);
2146 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2148 if (NeighborCache
->State
== EfiNeighborInComplete
) {
2150 // Send queued packets if exist.
2152 NeighborCache
->State
= EfiNeighborStale
;
2153 NeighborCache
->CallBack ((VOID
*) NeighborCache
);
2155 NeighborCache
->State
= EfiNeighborStale
;
2160 Offset
= (UINT16
) (Offset
+ (UINT16
) LinkLayerOption
.Length
* 8);
2162 case Ip6OptionPrefixInfo
:
2163 NetbufCopy (Packet
, Offset
, sizeof (IP6_PREFIX_INFO_OPTION
), (UINT8
*) &PrefixOption
);
2164 if (PrefixOption
.Length
!= 4) {
2167 PrefixOption
.ValidLifetime
= NTOHL (PrefixOption
.ValidLifetime
);
2168 PrefixOption
.PreferredLifetime
= NTOHL (PrefixOption
.PreferredLifetime
);
2171 // Get L and A flag, recorded in the lower 2 bits of Reserved1
2174 if ((PrefixOption
.Reserved1
& IP6_ON_LINK_FLAG
) == IP6_ON_LINK_FLAG
) {
2178 if ((PrefixOption
.Reserved1
& IP6_AUTO_CONFIG_FLAG
) == IP6_AUTO_CONFIG_FLAG
) {
2183 // If the prefix is the link-local prefix, silently ignore the prefix option.
2185 if (PrefixOption
.PrefixLength
== IP6_LINK_LOCAL_PREFIX_LENGTH
&&
2186 NetIp6IsLinkLocalAddr (&PrefixOption
.Prefix
)
2188 Offset
+= sizeof (IP6_PREFIX_INFO_OPTION
);
2192 // Do following if on-link flag is set according to RFC4861.
2195 PrefixList
= Ip6FindPrefixListEntry (
2198 PrefixOption
.PrefixLength
,
2199 &PrefixOption
.Prefix
2202 // Create a new entry for the prefix, if the ValidLifetime is zero,
2203 // silently ignore the prefix option.
2205 if (PrefixList
== NULL
&& PrefixOption
.ValidLifetime
!= 0) {
2206 PrefixList
= Ip6CreatePrefixListEntry (
2209 PrefixOption
.ValidLifetime
,
2210 PrefixOption
.PreferredLifetime
,
2211 PrefixOption
.PrefixLength
,
2212 &PrefixOption
.Prefix
2214 if (PrefixList
== NULL
) {
2215 Status
= EFI_OUT_OF_RESOURCES
;
2218 } else if (PrefixList
!= NULL
) {
2219 if (PrefixOption
.ValidLifetime
!= 0) {
2220 PrefixList
->ValidLifetime
= PrefixOption
.ValidLifetime
;
2223 // If the prefix exists and incoming ValidLifetime is zero, immediately
2224 // remove the prefix.
2225 Ip6DestroyPrefixListEntry (IpSb
, PrefixList
, OnLink
, TRUE
);
2231 // Do following if Autonomous flag is set according to RFC4862.
2233 if (Autonomous
&& PrefixOption
.PreferredLifetime
<= PrefixOption
.ValidLifetime
) {
2234 PrefixList
= Ip6FindPrefixListEntry (
2237 PrefixOption
.PrefixLength
,
2238 &PrefixOption
.Prefix
2241 // Create a new entry for the prefix, and form an address by prefix + interface id
2242 // If the sum of the prefix length and interface identifier length
2243 // does not equal 128 bits, the Prefix Information option MUST be ignored.
2245 if (PrefixList
== NULL
&&
2246 PrefixOption
.ValidLifetime
!= 0 &&
2247 PrefixOption
.PrefixLength
+ IpSb
->InterfaceIdLen
* 8 == 128
2250 // Form the address in network order.
2252 CopyMem (&StatelessAddress
, &PrefixOption
.Prefix
, sizeof (UINT64
));
2253 CopyMem (&StatelessAddress
.Addr
[8], IpSb
->InterfaceId
, sizeof (UINT64
));
2256 // If the address is not yet in the assigned address list, adds it into.
2258 if (!Ip6IsOneOfSetAddress (IpSb
, &StatelessAddress
, NULL
, NULL
)) {
2260 // And also not in the DAD process, check its uniqeness firstly.
2262 if (Ip6FindDADEntry (IpSb
, &StatelessAddress
, NULL
) == NULL
) {
2263 Status
= Ip6SetAddress (
2264 IpSb
->DefaultInterface
,
2267 PrefixOption
.PrefixLength
,
2268 PrefixOption
.ValidLifetime
,
2269 PrefixOption
.PreferredLifetime
,
2273 if (EFI_ERROR (Status
)) {
2280 // Adds the prefix option to stateless prefix option list.
2282 PrefixList
= Ip6CreatePrefixListEntry (
2285 PrefixOption
.ValidLifetime
,
2286 PrefixOption
.PreferredLifetime
,
2287 PrefixOption
.PrefixLength
,
2288 &PrefixOption
.Prefix
2290 if (PrefixList
== NULL
) {
2291 Status
= EFI_OUT_OF_RESOURCES
;
2294 } else if (PrefixList
!= NULL
) {
2297 // Reset the preferred lifetime of the address if the advertised prefix exists.
2298 // Perform specific action to valid lifetime together.
2300 PrefixList
->PreferredLifetime
= PrefixOption
.PreferredLifetime
;
2301 if ((PrefixOption
.ValidLifetime
> 7200) ||
2302 (PrefixOption
.ValidLifetime
> PrefixList
->ValidLifetime
)) {
2304 // If the received Valid Lifetime is greater than 2 hours or
2305 // greater than RemainingLifetime, set the valid lifetime of the
2306 // corresponding address to the advertised Valid Lifetime.
2308 PrefixList
->ValidLifetime
= PrefixOption
.ValidLifetime
;
2310 } else if (PrefixList
->ValidLifetime
<= 7200) {
2312 // If RemainingLifetime is less than or equls to 2 hours, ignore the
2313 // Prefix Information option with regards to the valid lifetime.
2314 // TODO: If this option has been authenticated, set the valid lifetime.
2318 // Otherwise, reset the valid lifetime of the corresponding
2319 // address to 2 hours.
2321 PrefixList
->ValidLifetime
= 7200;
2326 Offset
+= sizeof (IP6_PREFIX_INFO_OPTION
);
2329 NetbufCopy (Packet
, Offset
, sizeof (IP6_MTU_OPTION
), (UINT8
*) &MTUOption
);
2330 if (MTUOption
.Length
!= 1) {
2335 // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order
2336 // to omit implementation of Path MTU Discovery. Thus ignore the MTU option
2337 // in Router Advertisement.
2340 Offset
+= sizeof (IP6_MTU_OPTION
);
2344 // Silently ignore unrecognized options
2346 NetbufCopy (Packet
, Offset
+ sizeof (UINT8
), sizeof (UINT8
), &Length
);
2351 Offset
= (UINT16
) (Offset
+ (UINT16
) Length
* 8);
2356 Status
= EFI_SUCCESS
;
2359 NetbufFree (Packet
);
2364 Process the ICMPv6 redirect message. Find the instance, then update
2367 @param[in] IpSb The IP6 service binding instance that received
2369 @param[in] Head The IP head of the received ICMPv6 packet.
2370 @param[in] Packet The content of the ICMPv6 redirect packet with
2371 the IP head removed.
2373 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2374 @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the
2376 @retval EFI_SUCCESS Successfully updated the route caches.
2380 Ip6ProcessRedirect (
2381 IN IP6_SERVICE
*IpSb
,
2382 IN EFI_IP6_HEADER
*Head
,
2386 IP6_ICMP_INFORMATION_HEAD
*Icmp
;
2387 EFI_IPv6_ADDRESS
*Target
;
2388 EFI_IPv6_ADDRESS
*IcmpDest
;
2391 IP6_ROUTE_ENTRY
*RouteEntry
;
2392 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
2393 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
2396 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
2397 EFI_MAC_ADDRESS Mac
;
2403 Status
= EFI_INVALID_PARAMETER
;
2405 Icmp
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
2411 // Validate the incoming Redirect message
2415 // The IP Hop Limit field has a value of 255, i.e. the packet
2416 // could not possibly have been forwarded by a router.
2418 // ICMP length (derived from the IP length) is 40 or more octets.
2420 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
->Head
.Code
!= 0 ||
2421 Head
->PayloadLength
< IP6_REDITECT_LENGTH
) {
2426 // The IP source address must be a link-local address
2428 if (!NetIp6IsLinkLocalAddr (&Head
->SourceAddress
)) {
2433 // The dest of this ICMP redirect message is not us.
2435 if (!Ip6IsOneOfSetAddress (IpSb
, &Head
->DestinationAddress
, NULL
, NULL
)) {
2440 // All included options have a length that is greater than zero.
2442 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_REDITECT_LENGTH
);
2443 if (OptionLen
!= 0) {
2444 Option
= NetbufGetByte (Packet
, IP6_REDITECT_LENGTH
, NULL
);
2445 ASSERT (Option
!= NULL
);
2447 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
2452 Target
= (EFI_IPv6_ADDRESS
*) (Icmp
+ 1);
2453 IcmpDest
= Target
+ 1;
2456 // The ICMP Destination Address field in the redirect message does not contain
2457 // a multicast address.
2459 if (IP6_IS_MULTICAST (IcmpDest
)) {
2464 // The ICMP Target Address is either a link-local address (when redirected to
2465 // a router) or the same as the ICMP Destination Address (when redirected to
2466 // the on-link destination).
2468 IsRouter
= (BOOLEAN
) !EFI_IP6_EQUAL (Target
, IcmpDest
);
2469 if (!NetIp6IsLinkLocalAddr (Target
) && IsRouter
) {
2474 // Check the options. The only interested option here is the target-link layer
2477 Length
= Packet
->TotalSize
- 40;
2478 Option
= (UINT8
*) (IcmpDest
+ 1);
2479 LinkLayerOption
= NULL
;
2480 while (Length
> 0) {
2482 case Ip6OptionEtherTarget
:
2484 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) Option
;
2485 OptLen
= LinkLayerOption
->Length
;
2488 // For ethernet, the length must be 1.
2496 OptLen
= *(Option
+ 1);
2499 // A length of 0 is invalid.
2506 Length
-= 8 * OptLen
;
2507 Option
+= 8 * OptLen
;
2515 // The IP source address of the Redirect is the same as the current
2516 // first-hop router for the specified ICMP Destination Address.
2518 RouteCache
= Ip6FindRouteCache (IpSb
->RouteTable
, IcmpDest
, &Head
->DestinationAddress
);
2519 if (RouteCache
!= NULL
) {
2520 if (!EFI_IP6_EQUAL (&RouteCache
->NextHop
, &Head
->SourceAddress
)) {
2522 // The source of this Redirect message must match the NextHop of the
2523 // corresponding route cache entry.
2529 // Update the NextHop.
2531 IP6_COPY_ADDRESS (&RouteCache
->NextHop
, Target
);
2534 RouteEntry
= (IP6_ROUTE_ENTRY
*) RouteCache
->Tag
;
2535 RouteEntry
->Flag
= RouteEntry
->Flag
| IP6_DIRECT_ROUTE
;
2540 // Get the Route Entry.
2542 RouteEntry
= Ip6FindRouteEntry (IpSb
->RouteTable
, IcmpDest
, NULL
);
2543 if (RouteEntry
== NULL
) {
2544 RouteEntry
= Ip6CreateRouteEntry (IcmpDest
, 0, NULL
);
2545 if (RouteEntry
== NULL
) {
2546 Status
= EFI_OUT_OF_RESOURCES
;
2552 RouteEntry
->Flag
= IP6_DIRECT_ROUTE
;
2556 // Create a route cache for this.
2558 RouteCache
= Ip6CreateRouteCacheEntry (
2560 &Head
->DestinationAddress
,
2564 if (RouteCache
== NULL
) {
2565 Status
= EFI_OUT_OF_RESOURCES
;
2570 // Insert the newly created route cache entry.
2572 Index
= IP6_ROUTE_CACHE_HASH (IcmpDest
, &Head
->DestinationAddress
);
2573 InsertHeadList (&IpSb
->RouteTable
->Cache
.CacheBucket
[Index
], &RouteCache
->Link
);
2577 // Try to locate the neighbor cache for the Target.
2579 NeighborCache
= Ip6FindNeighborEntry (IpSb
, Target
);
2581 if (LinkLayerOption
!= NULL
) {
2582 if (NeighborCache
== NULL
) {
2584 // Create a neighbor cache for the Target.
2586 ZeroMem (&Mac
, sizeof (EFI_MAC_ADDRESS
));
2587 CopyMem (&Mac
, LinkLayerOption
->EtherAddr
, 6);
2588 NeighborCache
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, Target
, &Mac
);
2589 if (NeighborCache
== NULL
) {
2591 // Just report a success here. The neighbor cache can be created in
2592 // some other place.
2594 Status
= EFI_SUCCESS
;
2598 NeighborCache
->State
= EfiNeighborStale
;
2599 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2601 Result
= CompareMem (LinkLayerOption
->EtherAddr
, &NeighborCache
->LinkAddress
, 6);
2604 // If the link-local address is the same as that already in the cache,
2605 // the cache entry's state remains unchanged. Otherwise update the
2606 // reachability state to STALE.
2608 if ((NeighborCache
->State
== EfiNeighborInComplete
) || (Result
!= 0)) {
2609 CopyMem (&NeighborCache
->LinkAddress
, LinkLayerOption
->EtherAddr
, 6);
2611 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2613 if (NeighborCache
->State
== EfiNeighborInComplete
) {
2615 // Send queued packets if exist.
2617 NeighborCache
->State
= EfiNeighborStale
;
2618 NeighborCache
->CallBack ((VOID
*) NeighborCache
);
2620 NeighborCache
->State
= EfiNeighborStale
;
2626 if (NeighborCache
!= NULL
&& IsRouter
) {
2628 // The Target is a router, set IsRouter to TRUE.
2630 NeighborCache
->IsRouter
= TRUE
;
2633 Status
= EFI_SUCCESS
;
2636 NetbufFree (Packet
);
2641 Add Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2643 @param[in] IpSb The IP6 service binding instance.
2644 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2645 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2646 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2647 cache. It will be deleted after Timeout. A value of zero means that
2648 the entry is permanent. A non-zero value means that the entry is
2650 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2651 be overridden and updated; if FALSE, and if a
2652 corresponding cache entry already existed, EFI_ACCESS_DENIED
2655 @retval EFI_SUCCESS The neighbor cache entry has been added.
2656 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache
2657 due to insufficient resources.
2658 @retval EFI_NOT_FOUND TargetLinkAddress is NULL.
2659 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
2660 and that entry is tagged as un-overridden (when DeleteFlag
2666 IN IP6_SERVICE
*IpSb
,
2667 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
2668 IN EFI_MAC_ADDRESS
*TargetLinkAddress OPTIONAL
,
2673 IP6_NEIGHBOR_ENTRY
*Neighbor
;
2675 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
2676 if (Neighbor
!= NULL
) {
2678 return EFI_ACCESS_DENIED
;
2680 if (TargetLinkAddress
!= NULL
) {
2681 IP6_COPY_LINK_ADDRESS (&Neighbor
->LinkAddress
, TargetLinkAddress
);
2685 if (TargetLinkAddress
== NULL
) {
2686 return EFI_NOT_FOUND
;
2689 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, TargetIp6Address
, TargetLinkAddress
);
2690 if (Neighbor
== NULL
) {
2691 return EFI_OUT_OF_RESOURCES
;
2695 Neighbor
->State
= EfiNeighborReachable
;
2698 Neighbor
->Ticks
= IP6_GET_TICKS (Timeout
/ TICKS_PER_MS
);
2699 Neighbor
->Dynamic
= TRUE
;
2701 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2708 Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2710 @param[in] IpSb The IP6 service binding instance.
2711 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2712 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2713 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2714 cache. It will be deleted after Timeout. A value of zero means that
2715 the entry is permanent. A non-zero value means that the entry is
2717 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2718 be overridden and updated; if FALSE, and if a
2719 corresponding cache entry already existed, EFI_ACCESS_DENIED
2722 @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted.
2723 @retval EFI_NOT_FOUND This entry is not in the neighbor cache.
2728 IN IP6_SERVICE
*IpSb
,
2729 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
2730 IN EFI_MAC_ADDRESS
*TargetLinkAddress OPTIONAL
,
2735 IP6_NEIGHBOR_ENTRY
*Neighbor
;
2737 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
2738 if (Neighbor
== NULL
) {
2739 return EFI_NOT_FOUND
;
2742 RemoveEntryList (&Neighbor
->Link
);
2743 FreePool (Neighbor
);
2749 The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds.
2750 This time routine handles DAD module and neighbor state transition.
2751 It is also responsible for sending out router solicitations.
2753 @param[in] Event The IP6 service instance's heartbeat timer.
2754 @param[in] Context The IP6 service instance.
2759 Ip6NdFasterTimerTicking (
2767 IP6_INTERFACE
*IpIf
;
2768 IP6_DELAY_JOIN_LIST
*DelayNode
;
2769 EFI_IPv6_ADDRESS Source
;
2770 IP6_DAD_ENTRY
*DupAddrDetect
;
2772 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
2773 EFI_IPv6_ADDRESS Destination
;
2777 IpSb
= (IP6_SERVICE
*) Context
;
2778 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
2780 ZeroMem (&Source
, sizeof (EFI_IPv6_ADDRESS
));
2783 // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router
2784 // Solicitation messages, each separated by at least
2785 // RTR_SOLICITATION_INTERVAL (4) seconds.
2787 if ((IpSb
->Ip6ConfigInstance
.Policy
== Ip6ConfigPolicyAutomatic
) &&
2788 !IpSb
->RouterAdvertiseReceived
&&
2789 IpSb
->SolicitTimer
> 0
2791 if ((IpSb
->Ticks
== 0) || (--IpSb
->Ticks
== 0)) {
2792 Status
= Ip6SendRouterSolicit (IpSb
, NULL
, NULL
, NULL
, NULL
);
2793 if (!EFI_ERROR (Status
)) {
2794 IpSb
->SolicitTimer
--;
2795 IpSb
->Ticks
= (UINT32
) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL
);
2800 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
2801 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP6_INTERFACE
, Link
);
2804 // Process the delay list to join the solicited-node multicast address.
2806 NET_LIST_FOR_EACH_SAFE (Entry2
, Next
, &IpIf
->DelayJoinList
) {
2807 DelayNode
= NET_LIST_USER_STRUCT (Entry2
, IP6_DELAY_JOIN_LIST
, Link
);
2808 if ((DelayNode
->DelayTime
== 0) || (--DelayNode
->DelayTime
== 0)) {
2810 // The timer expires, init the duplicate address detection.
2813 DelayNode
->Interface
,
2814 DelayNode
->AddressInfo
,
2815 DelayNode
->DadCallback
,
2820 // Remove the delay node
2822 RemoveEntryList (&DelayNode
->Link
);
2823 FreePool (DelayNode
);
2828 // Process the duplicate address detection list.
2830 NET_LIST_FOR_EACH_SAFE (Entry2
, Next
, &IpIf
->DupAddrDetectList
) {
2831 DupAddrDetect
= NET_LIST_USER_STRUCT (Entry2
, IP6_DAD_ENTRY
, Link
);
2833 if ((DupAddrDetect
->RetransTick
== 0) || (--DupAddrDetect
->RetransTick
== 0)) {
2835 // The timer expires, check the remaining transmit counts.
2837 if (DupAddrDetect
->Transmit
< DupAddrDetect
->MaxTransmit
) {
2839 // Send the Neighbor Solicitation message with
2840 // Source - unspecified address, destination - solicited-node multicast address
2841 // Target - the address to be validated
2843 Status
= Ip6SendNeighborSolicit (
2846 &DupAddrDetect
->Destination
,
2847 &DupAddrDetect
->AddressInfo
->Address
,
2850 if (EFI_ERROR (Status
)) {
2854 DupAddrDetect
->Transmit
++;
2855 DupAddrDetect
->RetransTick
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2858 // All required solicitation has been sent out, and the RetransTime after the last
2859 // Neighbor Solicit is elapsed, finish the DAD process.
2862 if ((DupAddrDetect
->Receive
== 0) ||
2863 (DupAddrDetect
->Transmit
<= DupAddrDetect
->Receive
)) {
2867 Ip6OnDADFinished (Flag
, IpIf
, DupAddrDetect
);
2874 // Polling the state of Neighbor cache
2876 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->NeighborTable
) {
2877 NeighborCache
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, Link
);
2879 switch (NeighborCache
->State
) {
2880 case EfiNeighborInComplete
:
2881 if (NeighborCache
->Ticks
> 0) {
2882 --NeighborCache
->Ticks
;
2886 // Retransmit Neighbor Solicitation messages approximately every
2887 // RetransTimer milliseconds while awaiting a response.
2889 if (NeighborCache
->Ticks
== 0) {
2890 if (NeighborCache
->Transmit
> 1) {
2892 // Send out multicast neighbor solicitation for address resolution.
2893 // After last neighbor solicitation message has been sent out, wait
2894 // for RetransTimer and then remove entry if no response is received.
2896 Ip6CreateSNMulticastAddr (&NeighborCache
->Neighbor
, &Destination
);
2897 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
2898 if (EFI_ERROR (Status
)) {
2902 Status
= Ip6SendNeighborSolicit (
2906 &NeighborCache
->Neighbor
,
2907 &IpSb
->SnpMode
.CurrentAddress
2909 if (EFI_ERROR (Status
)) {
2915 // Update the retransmit times.
2917 if (NeighborCache
->Transmit
> 0) {
2918 --NeighborCache
->Transmit
;
2919 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2923 if (NeighborCache
->Transmit
== 0) {
2925 // Timeout, send ICMP destination unreachable packet and then remove entry
2927 Status
= Ip6FreeNeighborEntry (
2936 if (EFI_ERROR (Status
)) {
2943 case EfiNeighborReachable
:
2945 // This entry is inserted by EfiIp6Neighbors() as static entry
2946 // and will not timeout.
2948 if (!NeighborCache
->Dynamic
&& (NeighborCache
->Ticks
== IP6_INFINIT_LIFETIME
)) {
2952 if ((NeighborCache
->Ticks
== 0) || (--NeighborCache
->Ticks
== 0)) {
2953 if (NeighborCache
->Dynamic
) {
2955 // This entry is inserted by EfiIp6Neighbors() as dynamic entry
2956 // and will be deleted after timeout.
2958 Status
= Ip6FreeNeighborEntry (
2967 if (EFI_ERROR (Status
)) {
2971 NeighborCache
->State
= EfiNeighborStale
;
2972 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2978 case EfiNeighborDelay
:
2979 if ((NeighborCache
->Ticks
== 0) || (--NeighborCache
->Ticks
== 0)) {
2981 NeighborCache
->State
= EfiNeighborProbe
;
2982 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2983 NeighborCache
->Transmit
= IP6_MAX_UNICAST_SOLICIT
+ 1;
2985 // Send out unicast neighbor solicitation for Neighbor Unreachability Detection
2987 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
2988 if (EFI_ERROR (Status
)) {
2992 Status
= Ip6SendNeighborSolicit (
2995 &NeighborCache
->Neighbor
,
2996 &NeighborCache
->Neighbor
,
2997 &IpSb
->SnpMode
.CurrentAddress
2999 if (EFI_ERROR (Status
)) {
3003 NeighborCache
->Transmit
--;
3008 case EfiNeighborProbe
:
3009 if (NeighborCache
->Ticks
> 0) {
3010 --NeighborCache
->Ticks
;
3014 // Retransmit Neighbor Solicitation messages approximately every
3015 // RetransTimer milliseconds while awaiting a response.
3017 if (NeighborCache
->Ticks
== 0) {
3018 if (NeighborCache
->Transmit
> 1) {
3020 // Send out unicast neighbor solicitation for Neighbor Unreachability
3021 // Detection. After last neighbor solicitation message has been sent out,
3022 // wait for RetransTimer and then remove entry if no response is received.
3024 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
3025 if (EFI_ERROR (Status
)) {
3029 Status
= Ip6SendNeighborSolicit (
3032 &NeighborCache
->Neighbor
,
3033 &NeighborCache
->Neighbor
,
3034 &IpSb
->SnpMode
.CurrentAddress
3036 if (EFI_ERROR (Status
)) {
3042 // Update the retransmit times.
3044 if (NeighborCache
->Transmit
> 0) {
3045 --NeighborCache
->Transmit
;
3046 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
3050 if (NeighborCache
->Transmit
== 0) {
3052 // Delete the neighbor entry.
3054 Status
= Ip6FreeNeighborEntry (
3063 if (EFI_ERROR (Status
)) {
3077 The heartbeat timer of ND module in 1 second. This time routine handles following
3078 things: 1) maitain default router list; 2) maintain prefix options;
3079 3) maintain route caches.
3081 @param[in] IpSb The IP6 service binding instance.
3086 IN IP6_SERVICE
*IpSb
3091 IP6_DEFAULT_ROUTER
*DefaultRouter
;
3092 IP6_PREFIX_LIST_ENTRY
*PrefixOption
;
3094 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
3097 // Decrease the lifetime of default router, if expires remove it from default router list.
3099 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->DefaultRouterList
) {
3100 DefaultRouter
= NET_LIST_USER_STRUCT (Entry
, IP6_DEFAULT_ROUTER
, Link
);
3101 if (DefaultRouter
->Lifetime
!= IP6_INF_ROUTER_LIFETIME
) {
3102 if ((DefaultRouter
->Lifetime
== 0) || (--DefaultRouter
->Lifetime
== 0)) {
3103 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
3109 // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses.
3111 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->AutonomousPrefix
) {
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 if ((PrefixOption
->PreferredLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) &&
3116 (PrefixOption
->PreferredLifetime
> 0)
3118 --PrefixOption
->PreferredLifetime
;
3121 Ip6DestroyPrefixListEntry (IpSb
, PrefixOption
, FALSE
, TRUE
);
3126 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->OnlinkPrefix
) {
3127 PrefixOption
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
3128 if (PrefixOption
->ValidLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) {
3129 if ((PrefixOption
->ValidLifetime
== 0) || (--PrefixOption
->ValidLifetime
== 0)) {
3130 Ip6DestroyPrefixListEntry (IpSb
, PrefixOption
, TRUE
, TRUE
);
3136 // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries.
3137 // Remove the entries at the tail of the bucket. These entries
3138 // are likely to be used least.
3139 // Reclaim frequency is set to 1 second.
3141 for (Index
= 0; Index
< IP6_ROUTE_CACHE_HASH_SIZE
; Index
++) {
3142 while (IpSb
->RouteTable
->Cache
.CacheNum
[Index
] > IP6_ROUTE_CACHE_MAX
) {
3143 Entry
= NetListRemoveTail (&IpSb
->RouteTable
->Cache
.CacheBucket
[Index
]);
3144 if (Entry
== NULL
) {
3148 RouteCache
= NET_LIST_USER_STRUCT (Entry
, IP6_ROUTE_CACHE_ENTRY
, Link
);
3149 Ip6FreeRouteCacheEntry (RouteCache
);
3150 ASSERT (IpSb
->RouteTable
->Cache
.CacheNum
[Index
] > 0);
3151 IpSb
->RouteTable
->Cache
.CacheNum
[Index
]--;