2 Implementation of Neighbor Discovery support routines.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 EFI_MAC_ADDRESS mZeroMacAddress
;
15 Update the ReachableTime in IP6 service binding instance data, in milliseconds.
17 @param[in, out] IpSb Points to the IP6_SERVICE.
21 Ip6UpdateReachableTime (
22 IN OUT IP6_SERVICE
*IpSb
27 Random
= (NetRandomInitSeed () / 4294967295UL) * IP6_RANDOM_FACTOR_SCALE
;
28 Random
= Random
+ IP6_MIN_RANDOM_FACTOR_SCALED
;
29 IpSb
->ReachableTime
= (IpSb
->BaseReachableTime
* Random
) / IP6_RANDOM_FACTOR_SCALE
;
33 Build a array of EFI_IP6_NEIGHBOR_CACHE to be returned to the caller. The number
34 of EFI_IP6_NEIGHBOR_CACHE is also returned.
36 @param[in] IpInstance The pointer to IP6_PROTOCOL instance.
37 @param[out] NeighborCount The number of returned neighbor cache entries.
38 @param[out] NeighborCache The pointer to the array of EFI_IP6_NEIGHBOR_CACHE.
40 @retval EFI_SUCCESS The EFI_IP6_NEIGHBOR_CACHE successfully built.
41 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the route table.
45 Ip6BuildEfiNeighborCache (
46 IN IP6_PROTOCOL
*IpInstance
,
47 OUT UINT32
*NeighborCount
,
48 OUT EFI_IP6_NEIGHBOR_CACHE
**NeighborCache
51 IP6_NEIGHBOR_ENTRY
*Neighbor
;
55 EFI_IP6_NEIGHBOR_CACHE
*EfiNeighborCache
;
56 EFI_IP6_NEIGHBOR_CACHE
*NeighborCacheTmp
;
58 NET_CHECK_SIGNATURE (IpInstance
, IP6_PROTOCOL_SIGNATURE
);
59 ASSERT (NeighborCount
!= NULL
&& NeighborCache
!= NULL
);
61 IpSb
= IpInstance
->Service
;
64 NET_LIST_FOR_EACH (Entry
, &IpSb
->NeighborTable
) {
72 NeighborCacheTmp
= AllocatePool (Count
* sizeof (EFI_IP6_NEIGHBOR_CACHE
));
73 if (NeighborCacheTmp
== NULL
) {
74 return EFI_OUT_OF_RESOURCES
;
77 *NeighborCount
= Count
;
80 NET_LIST_FOR_EACH (Entry
, &IpSb
->NeighborTable
) {
81 Neighbor
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, Link
);
83 EfiNeighborCache
= NeighborCacheTmp
+ Count
;
85 EfiNeighborCache
->State
= Neighbor
->State
;
86 IP6_COPY_ADDRESS (&EfiNeighborCache
->Neighbor
, &Neighbor
->Neighbor
);
87 IP6_COPY_LINK_ADDRESS (&EfiNeighborCache
->LinkAddress
, &Neighbor
->LinkAddress
);
92 ASSERT (*NeighborCount
== Count
);
93 *NeighborCache
= NeighborCacheTmp
;
99 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
100 of prefix entries is also returned.
102 @param[in] IpInstance The pointer to IP6_PROTOCOL instance.
103 @param[out] PrefixCount The number of returned prefix entries.
104 @param[out] PrefixTable The pointer to the array of PrefixTable.
106 @retval EFI_SUCCESS The prefix table successfully built.
107 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the prefix table.
111 Ip6BuildPrefixTable (
112 IN IP6_PROTOCOL
*IpInstance
,
113 OUT UINT32
*PrefixCount
,
114 OUT EFI_IP6_ADDRESS_INFO
**PrefixTable
120 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
121 EFI_IP6_ADDRESS_INFO
*EfiPrefix
;
122 EFI_IP6_ADDRESS_INFO
*PrefixTableTmp
;
124 NET_CHECK_SIGNATURE (IpInstance
, IP6_PROTOCOL_SIGNATURE
);
125 ASSERT (PrefixCount
!= NULL
&& PrefixTable
!= NULL
);
127 IpSb
= IpInstance
->Service
;
130 NET_LIST_FOR_EACH (Entry
, &IpSb
->OnlinkPrefix
) {
138 PrefixTableTmp
= AllocatePool (Count
* sizeof (EFI_IP6_ADDRESS_INFO
));
139 if (PrefixTableTmp
== NULL
) {
140 return EFI_OUT_OF_RESOURCES
;
143 *PrefixCount
= Count
;
146 NET_LIST_FOR_EACH (Entry
, &IpSb
->OnlinkPrefix
) {
147 PrefixList
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
148 EfiPrefix
= PrefixTableTmp
+ Count
;
149 IP6_COPY_ADDRESS (&EfiPrefix
->Address
, &PrefixList
->Prefix
);
150 EfiPrefix
->PrefixLength
= PrefixList
->PrefixLength
;
155 ASSERT (*PrefixCount
== Count
);
156 *PrefixTable
= PrefixTableTmp
;
162 Allocate and initialize a IP6 prefix list entry.
164 @param[in] IpSb The pointer to IP6_SERVICE instance.
165 @param[in] OnLinkOrAuto If TRUE, the entry is created for the on link prefix list.
166 Otherwise, it is created for the autoconfiguration prefix list.
167 @param[in] ValidLifetime The length of time in seconds that the prefix
168 is valid for the purpose of on-link determination.
169 @param[in] PreferredLifetime The length of time in seconds that addresses
170 generated from the prefix via stateless address
171 autoconfiguration remain preferred.
172 @param[in] PrefixLength The prefix length of the Prefix.
173 @param[in] Prefix The prefix address.
175 @return NULL if it failed to allocate memory for the prefix node. Otherwise, point
176 to the created or existing prefix list entry.
179 IP6_PREFIX_LIST_ENTRY
*
180 Ip6CreatePrefixListEntry (
181 IN IP6_SERVICE
*IpSb
,
182 IN BOOLEAN OnLinkOrAuto
,
183 IN UINT32 ValidLifetime
,
184 IN UINT32 PreferredLifetime
,
185 IN UINT8 PrefixLength
,
186 IN EFI_IPv6_ADDRESS
*Prefix
189 IP6_PREFIX_LIST_ENTRY
*PrefixEntry
;
190 IP6_ROUTE_ENTRY
*RtEntry
;
191 LIST_ENTRY
*ListHead
;
193 IP6_PREFIX_LIST_ENTRY
*TmpPrefixEntry
;
195 if (Prefix
== NULL
|| PreferredLifetime
> ValidLifetime
|| PrefixLength
> IP6_PREFIX_MAX
) {
199 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
201 PrefixEntry
= Ip6FindPrefixListEntry (
207 if (PrefixEntry
!= NULL
) {
208 PrefixEntry
->RefCnt
++;
212 PrefixEntry
= AllocatePool (sizeof (IP6_PREFIX_LIST_ENTRY
));
213 if (PrefixEntry
== NULL
) {
217 PrefixEntry
->RefCnt
= 1;
218 PrefixEntry
->ValidLifetime
= ValidLifetime
;
219 PrefixEntry
->PreferredLifetime
= PreferredLifetime
;
220 PrefixEntry
->PrefixLength
= PrefixLength
;
221 IP6_COPY_ADDRESS (&PrefixEntry
->Prefix
, Prefix
);
223 ListHead
= OnLinkOrAuto
? &IpSb
->OnlinkPrefix
: &IpSb
->AutonomousPrefix
;
226 // Create a direct route entry for on-link prefix and insert to route area.
229 RtEntry
= Ip6CreateRouteEntry (Prefix
, PrefixLength
, NULL
);
230 if (RtEntry
== NULL
) {
231 FreePool (PrefixEntry
);
235 RtEntry
->Flag
= IP6_DIRECT_ROUTE
;
236 InsertHeadList (&IpSb
->RouteTable
->RouteArea
[PrefixLength
], &RtEntry
->Link
);
237 IpSb
->RouteTable
->TotalNum
++;
241 // Insert the prefix entry in the order that a prefix with longer prefix length
242 // is put ahead in the list.
244 NET_LIST_FOR_EACH (Entry
, ListHead
) {
245 TmpPrefixEntry
= NET_LIST_USER_STRUCT(Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
247 if (TmpPrefixEntry
->PrefixLength
< PrefixEntry
->PrefixLength
) {
252 NetListInsertBefore (Entry
, &PrefixEntry
->Link
);
258 Destroy a IP6 prefix list entry.
260 @param[in] IpSb The pointer to IP6_SERVICE instance.
261 @param[in] PrefixEntry The to be destroyed prefix list entry.
262 @param[in] OnLinkOrAuto If TRUE, the entry is removed from on link prefix list.
263 Otherwise remove from autoconfiguration prefix list.
264 @param[in] ImmediateDelete If TRUE, remove the entry directly.
265 Otherwise, check the reference count to see whether
266 it should be removed.
270 Ip6DestroyPrefixListEntry (
271 IN IP6_SERVICE
*IpSb
,
272 IN IP6_PREFIX_LIST_ENTRY
*PrefixEntry
,
273 IN BOOLEAN OnLinkOrAuto
,
274 IN BOOLEAN ImmediateDelete
281 if ((!ImmediateDelete
) && (PrefixEntry
->RefCnt
> 0) && ((--PrefixEntry
->RefCnt
) > 0)) {
287 // Remove the direct route for onlink prefix from route table.
290 Status
= Ip6DelRoute (
292 &PrefixEntry
->Prefix
,
293 PrefixEntry
->PrefixLength
,
296 } while (Status
!= EFI_NOT_FOUND
);
299 // Remove the corresponding addresses generated from this autonomous prefix.
301 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
302 IpIf
= NET_LIST_USER_STRUCT_S (Entry
, IP6_INTERFACE
, Link
, IP6_INTERFACE_SIGNATURE
);
304 Ip6RemoveAddr (IpSb
, &IpIf
->AddressList
, &IpIf
->AddressCount
, &PrefixEntry
->Prefix
, PrefixEntry
->PrefixLength
);
308 RemoveEntryList (&PrefixEntry
->Link
);
309 FreePool (PrefixEntry
);
313 Search the list array to find an IP6 prefix list entry.
315 @param[in] IpSb The pointer to IP6_SERVICE instance.
316 @param[in] OnLinkOrAuto If TRUE, the search the link prefix list,
317 Otherwise search the autoconfiguration prefix list.
318 @param[in] PrefixLength The prefix length of the Prefix
319 @param[in] Prefix The prefix address.
321 @return NULL if cannot find the IP6 prefix list entry. Otherwise, return the
322 pointer to the IP6 prefix list entry.
325 IP6_PREFIX_LIST_ENTRY
*
326 Ip6FindPrefixListEntry (
327 IN IP6_SERVICE
*IpSb
,
328 IN BOOLEAN OnLinkOrAuto
,
329 IN UINT8 PrefixLength
,
330 IN EFI_IPv6_ADDRESS
*Prefix
333 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
335 LIST_ENTRY
*ListHead
;
337 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
338 ASSERT (Prefix
!= NULL
);
341 ListHead
= &IpSb
->OnlinkPrefix
;
343 ListHead
= &IpSb
->AutonomousPrefix
;
346 NET_LIST_FOR_EACH (Entry
, ListHead
) {
347 PrefixList
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
348 if (PrefixLength
!= 255) {
350 // Perform exactly prefix match.
352 if (PrefixList
->PrefixLength
== PrefixLength
&&
353 NetIp6IsNetEqual (&PrefixList
->Prefix
, Prefix
, PrefixLength
)) {
358 // Perform the longest prefix match. The list is already sorted with
359 // the longest length prefix put at the head of the list.
361 if (NetIp6IsNetEqual (&PrefixList
->Prefix
, Prefix
, PrefixList
->PrefixLength
)) {
371 Release the resource in the prefix list table, and destroy the list entry and
372 corresponding addresses or route entries.
374 @param[in] IpSb The pointer to the IP6_SERVICE instance.
375 @param[in] ListHead The list entry head of the prefix list table.
379 Ip6CleanPrefixListTable (
380 IN IP6_SERVICE
*IpSb
,
381 IN LIST_ENTRY
*ListHead
384 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
387 OnLink
= (BOOLEAN
) (ListHead
== &IpSb
->OnlinkPrefix
);
389 while (!IsListEmpty (ListHead
)) {
390 PrefixList
= NET_LIST_HEAD (ListHead
, IP6_PREFIX_LIST_ENTRY
, Link
);
391 Ip6DestroyPrefixListEntry (IpSb
, PrefixList
, OnLink
, TRUE
);
396 Callback function when address resolution is finished. It will cancel
397 all the queued frames if the address resolution failed, or transmit them
398 if the request succeeded.
400 @param[in] Context The context of the callback, a pointer to IP6_NEIGHBOR_ENTRY.
410 IP6_NEIGHBOR_ENTRY
*ArpQue
;
412 IP6_LINK_TX_TOKEN
*Token
;
416 ArpQue
= (IP6_NEIGHBOR_ENTRY
*) Context
;
417 if ((ArpQue
== NULL
) || (ArpQue
->Interface
== NULL
)) {
421 IpSb
= ArpQue
->Interface
->Service
;
422 if ((IpSb
== NULL
) || (IpSb
->Signature
!= IP6_SERVICE_SIGNATURE
)) {
427 // ARP resolve failed for some reason. Release all the frame
428 // and ARP queue itself. Ip6FreeArpQue will call the frame's
431 if (NET_MAC_EQUAL (&ArpQue
->LinkAddress
, &mZeroMacAddress
, IpSb
->SnpMode
.HwAddressSize
)) {
432 Ip6FreeNeighborEntry (IpSb
, ArpQue
, FALSE
, TRUE
, EFI_NO_MAPPING
, NULL
, NULL
);
437 // ARP resolve succeeded, Transmit all the frame.
440 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &ArpQue
->Frames
) {
441 RemoveEntryList (Entry
);
443 Token
= NET_LIST_USER_STRUCT (Entry
, IP6_LINK_TX_TOKEN
, Link
);
444 IP6_COPY_LINK_ADDRESS (&Token
->DstMac
, &ArpQue
->LinkAddress
);
447 // Insert the tx token before transmitting it via MNP as the FrameSentDpc
448 // may be called before Mnp->Transmit returns which will remove this tx
449 // token from the SentFrames list. Remove it from the list if the returned
450 // Status of Mnp->Transmit is not EFI_SUCCESS as in this case the
451 // FrameSentDpc won't be queued.
453 InsertTailList (&ArpQue
->Interface
->SentFrames
, &Token
->Link
);
455 Status
= IpSb
->Mnp
->Transmit (IpSb
->Mnp
, &Token
->MnpToken
);
456 if (EFI_ERROR (Status
)) {
457 RemoveEntryList (&Token
->Link
);
458 Token
->CallBack (Token
->Packet
, Status
, 0, Token
->Context
);
460 Ip6FreeLinkTxToken (Token
);
468 // Free the ArpQue only but not the whole neighbor entry.
470 Ip6FreeNeighborEntry (IpSb
, ArpQue
, FALSE
, FALSE
, EFI_SUCCESS
, NULL
, NULL
);
472 if (Sent
&& (ArpQue
->State
== EfiNeighborStale
)) {
473 ArpQue
->State
= EfiNeighborDelay
;
474 ArpQue
->Ticks
= (UINT32
) IP6_GET_TICKS (IP6_DELAY_FIRST_PROBE_TIME
);
479 Allocate and initialize an IP6 neighbor cache entry.
481 @param[in] IpSb The pointer to the IP6_SERVICE instance.
482 @param[in] CallBack The callback function to be called when
483 address resolution is finished.
484 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
485 @param[in] LinkAddress Points to the MAC address of the neighbor.
488 @return NULL if failed to allocate memory for the neighbor cache entry.
489 Otherwise, point to the created neighbor cache entry.
493 Ip6CreateNeighborEntry (
494 IN IP6_SERVICE
*IpSb
,
495 IN IP6_ARP_CALLBACK CallBack
,
496 IN EFI_IPv6_ADDRESS
*Ip6Address
,
497 IN EFI_MAC_ADDRESS
*LinkAddress OPTIONAL
500 IP6_NEIGHBOR_ENTRY
*Entry
;
501 IP6_DEFAULT_ROUTER
*DefaultRouter
;
503 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
504 ASSERT (Ip6Address
!= NULL
);
506 Entry
= AllocateZeroPool (sizeof (IP6_NEIGHBOR_ENTRY
));
512 Entry
->IsRouter
= FALSE
;
513 Entry
->ArpFree
= FALSE
;
514 Entry
->Dynamic
= FALSE
;
515 Entry
->State
= EfiNeighborInComplete
;
516 Entry
->Transmit
= IP6_MAX_MULTICAST_SOLICIT
+ 1;
517 Entry
->CallBack
= CallBack
;
518 Entry
->Interface
= NULL
;
520 InitializeListHead (&Entry
->Frames
);
522 IP6_COPY_ADDRESS (&Entry
->Neighbor
, Ip6Address
);
524 if (LinkAddress
!= NULL
) {
525 IP6_COPY_LINK_ADDRESS (&Entry
->LinkAddress
, LinkAddress
);
527 IP6_COPY_LINK_ADDRESS (&Entry
->LinkAddress
, &mZeroMacAddress
);
530 InsertHeadList (&IpSb
->NeighborTable
, &Entry
->Link
);
533 // If corresponding default router entry exists, establish the relationship.
535 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, Ip6Address
);
536 if (DefaultRouter
!= NULL
) {
537 DefaultRouter
->NeighborCache
= Entry
;
544 Search a IP6 neighbor cache entry.
546 @param[in] IpSb The pointer to the IP6_SERVICE instance.
547 @param[in] Ip6Address Points to the IPv6 address of the neighbor.
549 @return NULL if it failed to find the matching neighbor cache entry.
550 Otherwise, point to the found neighbor cache entry.
554 Ip6FindNeighborEntry (
555 IN IP6_SERVICE
*IpSb
,
556 IN EFI_IPv6_ADDRESS
*Ip6Address
561 IP6_NEIGHBOR_ENTRY
*Neighbor
;
563 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
564 ASSERT (Ip6Address
!= NULL
);
566 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->NeighborTable
) {
567 Neighbor
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, Link
);
568 if (EFI_IP6_EQUAL (Ip6Address
, &Neighbor
->Neighbor
)) {
569 RemoveEntryList (Entry
);
570 InsertHeadList (&IpSb
->NeighborTable
, Entry
);
580 Free a IP6 neighbor cache entry and remove all the frames on the address
581 resolution queue that pass the FrameToCancel. That is, either FrameToCancel
582 is NULL, or it returns true for the frame.
584 @param[in] IpSb The pointer to the IP6_SERVICE instance.
585 @param[in] NeighborCache The to be free neighbor cache entry.
586 @param[in] SendIcmpError If TRUE, send out ICMP error.
587 @param[in] FullFree If TRUE, remove the neighbor cache entry.
588 Otherwise remove the pending frames.
589 @param[in] IoStatus The status returned to the cancelled frames'
591 @param[in] FrameToCancel Function to select which frame to cancel.
592 This is an optional parameter that may be NULL.
593 @param[in] Context Opaque parameter to the FrameToCancel.
594 Ignored if FrameToCancel is NULL.
596 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
597 @retval EFI_SUCCESS The operation finished successfully.
601 Ip6FreeNeighborEntry (
602 IN IP6_SERVICE
*IpSb
,
603 IN IP6_NEIGHBOR_ENTRY
*NeighborCache
,
604 IN BOOLEAN SendIcmpError
,
606 IN EFI_STATUS IoStatus
,
607 IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL
,
608 IN VOID
*Context OPTIONAL
611 IP6_LINK_TX_TOKEN
*TxToken
;
614 IP6_DEFAULT_ROUTER
*DefaultRouter
;
617 // If FrameToCancel fails, the token will not be released.
618 // To avoid the memory leak, stop this usage model.
620 if (FullFree
&& FrameToCancel
!= NULL
) {
621 return EFI_INVALID_PARAMETER
;
624 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &NeighborCache
->Frames
) {
625 TxToken
= NET_LIST_USER_STRUCT (Entry
, IP6_LINK_TX_TOKEN
, Link
);
627 if (SendIcmpError
&& !IP6_IS_MULTICAST (&TxToken
->Packet
->Ip
.Ip6
->DestinationAddress
)) {
632 &TxToken
->Packet
->Ip
.Ip6
->SourceAddress
,
633 ICMP_V6_DEST_UNREACHABLE
,
634 ICMP_V6_ADDR_UNREACHABLE
,
639 if ((FrameToCancel
== NULL
) || FrameToCancel (TxToken
, Context
)) {
640 RemoveEntryList (Entry
);
641 TxToken
->CallBack (TxToken
->Packet
, IoStatus
, 0, TxToken
->Context
);
642 Ip6FreeLinkTxToken (TxToken
);
646 if (NeighborCache
->ArpFree
&& IsListEmpty (&NeighborCache
->Frames
)) {
647 RemoveEntryList (&NeighborCache
->ArpList
);
648 NeighborCache
->ArpFree
= FALSE
;
652 if (NeighborCache
->IsRouter
) {
653 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &NeighborCache
->Neighbor
);
654 if (DefaultRouter
!= NULL
) {
655 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
659 RemoveEntryList (&NeighborCache
->Link
);
660 FreePool (NeighborCache
);
667 Allocate and initialize an IP6 default router entry.
669 @param[in] IpSb The pointer to the IP6_SERVICE instance.
670 @param[in] Ip6Address The IPv6 address of the default router.
671 @param[in] RouterLifetime The lifetime associated with the default
672 router, in units of seconds.
674 @return NULL if it failed to allocate memory for the default router node.
675 Otherwise, point to the created default router node.
679 Ip6CreateDefaultRouter (
680 IN IP6_SERVICE
*IpSb
,
681 IN EFI_IPv6_ADDRESS
*Ip6Address
,
682 IN UINT16 RouterLifetime
685 IP6_DEFAULT_ROUTER
*Entry
;
686 IP6_ROUTE_ENTRY
*RtEntry
;
688 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
689 ASSERT (Ip6Address
!= NULL
);
691 Entry
= AllocatePool (sizeof (IP6_DEFAULT_ROUTER
));
697 Entry
->Lifetime
= RouterLifetime
;
698 Entry
->NeighborCache
= Ip6FindNeighborEntry (IpSb
, Ip6Address
);
699 IP6_COPY_ADDRESS (&Entry
->Router
, Ip6Address
);
702 // Add a default route into route table with both Destination and PrefixLength set to zero.
704 RtEntry
= Ip6CreateRouteEntry (NULL
, 0, Ip6Address
);
705 if (RtEntry
== NULL
) {
710 InsertHeadList (&IpSb
->RouteTable
->RouteArea
[0], &RtEntry
->Link
);
711 IpSb
->RouteTable
->TotalNum
++;
713 InsertTailList (&IpSb
->DefaultRouterList
, &Entry
->Link
);
719 Destroy an IP6 default router entry.
721 @param[in] IpSb The pointer to the IP6_SERVICE instance.
722 @param[in] DefaultRouter The to be destroyed IP6_DEFAULT_ROUTER.
726 Ip6DestroyDefaultRouter (
727 IN IP6_SERVICE
*IpSb
,
728 IN IP6_DEFAULT_ROUTER
*DefaultRouter
733 RemoveEntryList (&DefaultRouter
->Link
);
736 // Update the Destination Cache - all entries using the time-out router as next-hop
737 // should perform next-hop determination again.
740 Status
= Ip6DelRoute (IpSb
->RouteTable
, NULL
, 0, &DefaultRouter
->Router
);
741 } while (Status
!= EFI_NOT_FOUND
);
743 FreePool (DefaultRouter
);
747 Clean an IP6 default router list.
749 @param[in] IpSb The pointer to the IP6_SERVICE instance.
753 Ip6CleanDefaultRouterList (
757 IP6_DEFAULT_ROUTER
*DefaultRouter
;
759 while (!IsListEmpty (&IpSb
->DefaultRouterList
)) {
760 DefaultRouter
= NET_LIST_HEAD (&IpSb
->DefaultRouterList
, IP6_DEFAULT_ROUTER
, Link
);
761 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
766 Search a default router node from an IP6 default router list.
768 @param[in] IpSb The pointer to the IP6_SERVICE instance.
769 @param[in] Ip6Address The IPv6 address of the to be searched default router node.
771 @return NULL if it failed to find the matching default router node.
772 Otherwise, point to the found default router node.
776 Ip6FindDefaultRouter (
777 IN IP6_SERVICE
*IpSb
,
778 IN EFI_IPv6_ADDRESS
*Ip6Address
782 IP6_DEFAULT_ROUTER
*DefaultRouter
;
784 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
785 ASSERT (Ip6Address
!= NULL
);
787 NET_LIST_FOR_EACH (Entry
, &IpSb
->DefaultRouterList
) {
788 DefaultRouter
= NET_LIST_USER_STRUCT (Entry
, IP6_DEFAULT_ROUTER
, Link
);
789 if (EFI_IP6_EQUAL (Ip6Address
, &DefaultRouter
->Router
)) {
790 return DefaultRouter
;
798 The function to be called after DAD (Duplicate Address Detection) is performed.
800 @param[in] IsDadPassed If TRUE, the DAD operation succeed. Otherwise, the DAD operation failed.
801 @param[in] IpIf Points to the IP6_INTERFACE.
802 @param[in] DadEntry The DAD entry which already performed DAD.
807 IN BOOLEAN IsDadPassed
,
808 IN IP6_INTERFACE
*IpIf
,
809 IN IP6_DAD_ENTRY
*DadEntry
813 IP6_ADDRESS_INFO
*AddrInfo
;
814 EFI_DHCP6_PROTOCOL
*Dhcp6
;
816 EFI_DHCP6_PACKET_OPTION
*Oro
;
817 EFI_DHCP6_RETRANSMISSION InfoReqReXmit
;
818 EFI_IPv6_ADDRESS AllNodes
;
820 IpSb
= IpIf
->Service
;
821 AddrInfo
= DadEntry
->AddressInfo
;
827 if (NetIp6IsLinkLocalAddr (&AddrInfo
->Address
)) {
828 ASSERT (!IpSb
->LinkLocalOk
);
830 IP6_COPY_ADDRESS (&IpSb
->LinkLocalAddr
, &AddrInfo
->Address
);
831 IpSb
->LinkLocalOk
= TRUE
;
832 IpIf
->Configured
= TRUE
;
835 // Check whether DHCP6 need to be started.
837 Dhcp6
= IpSb
->Ip6ConfigInstance
.Dhcp6
;
839 if (IpSb
->Dhcp6NeedStart
) {
840 Dhcp6
->Start (Dhcp6
);
841 IpSb
->Dhcp6NeedStart
= FALSE
;
844 if (IpSb
->Dhcp6NeedInfoRequest
) {
846 // Set the exta options to send. Here we only want the option request option
849 Oro
= (EFI_DHCP6_PACKET_OPTION
*) OptBuf
;
850 Oro
->OpCode
= HTONS (DHCP6_OPT_ORO
);
851 Oro
->OpLen
= HTONS (2);
852 *((UINT16
*) &Oro
->Data
[0]) = HTONS (DHCP6_OPT_DNS_SERVERS
);
854 InfoReqReXmit
.Irt
= 4;
855 InfoReqReXmit
.Mrc
= 64;
856 InfoReqReXmit
.Mrt
= 60;
857 InfoReqReXmit
.Mrd
= 0;
866 IpSb
->Ip6ConfigInstance
.Dhcp6Event
,
867 Ip6ConfigOnDhcp6Reply
,
868 &IpSb
->Ip6ConfigInstance
873 // Add an on-link prefix for link-local address.
875 Ip6CreatePrefixListEntry (
878 (UINT32
) IP6_INFINIT_LIFETIME
,
879 (UINT32
) IP6_INFINIT_LIFETIME
,
880 IP6_LINK_LOCAL_PREFIX_LENGTH
,
886 // Global scope unicast address.
888 Ip6AddAddr (IpIf
, AddrInfo
);
891 // Add an on-link prefix for this address.
893 Ip6CreatePrefixListEntry (
896 AddrInfo
->ValidLifetime
,
897 AddrInfo
->PreferredLifetime
,
898 AddrInfo
->PrefixLength
,
902 IpIf
->Configured
= TRUE
;
906 // Leave the group we joined before.
908 Ip6LeaveGroup (IpSb
, &DadEntry
->Destination
);
911 if (DadEntry
->Callback
!= NULL
) {
912 DadEntry
->Callback (IsDadPassed
, &AddrInfo
->Address
, DadEntry
->Context
);
915 if (!IsDadPassed
&& NetIp6IsLinkLocalAddr (&AddrInfo
->Address
)) {
917 RemoveEntryList (&DadEntry
->Link
);
920 // Leave link-scope all-nodes multicast address (FF02::1)
922 Ip6SetToAllNodeMulticast (FALSE
, IP6_LINK_LOCAL_SCOPE
, &AllNodes
);
923 Ip6LeaveGroup (IpSb
, &AllNodes
);
925 // Disable IP operation since link-local address is a duplicate address.
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);
934 if (!IsDadPassed
|| NetIp6IsLinkLocalAddr (&AddrInfo
->Address
)) {
936 // Free the AddressInfo we hold if DAD fails or it is a link-local address.
941 RemoveEntryList (&DadEntry
->Link
);
946 Create a DAD (Duplicate Address Detection) entry and queue it to be performed.
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
953 @param[in] Context The opaque parameter for a DAD callback routine.
954 This is an optional parameter that may be NULL.
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
964 IN IP6_INTERFACE
*IpIf
,
965 IN IP6_ADDRESS_INFO
*AddressInfo
,
966 IN IP6_DAD_CALLBACK Callback OPTIONAL
,
967 IN VOID
*Context OPTIONAL
970 IP6_DAD_ENTRY
*Entry
;
971 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS
*DadXmits
;
976 NET_CHECK_SIGNATURE (IpIf
, IP6_INTERFACE_SIGNATURE
);
977 ASSERT (AddressInfo
!= NULL
);
980 // Do nothing if we have already started DAD on the address.
982 if (Ip6FindDADEntry (IpIf
->Service
, &AddressInfo
->Address
, NULL
) != NULL
) {
986 Status
= EFI_SUCCESS
;
987 IpSb
= IpIf
->Service
;
988 DadXmits
= &IpSb
->Ip6ConfigInstance
.DadXmits
;
991 // Allocate the resources and insert info
993 Entry
= AllocatePool (sizeof (IP6_DAD_ENTRY
));
995 return EFI_OUT_OF_RESOURCES
;
999 // Map the incoming unicast address to solicited-node multicast address
1001 Ip6CreateSNMulticastAddr (&AddressInfo
->Address
, &Entry
->Destination
);
1004 // Join in the solicited-node multicast address.
1006 Status
= Ip6JoinGroup (IpSb
, IpIf
, &Entry
->Destination
);
1007 if (EFI_ERROR (Status
)) {
1012 Entry
->Signature
= IP6_DAD_ENTRY_SIGNATURE
;
1013 Entry
->MaxTransmit
= DadXmits
->DupAddrDetectTransmits
;
1014 Entry
->Transmit
= 0;
1016 MaxDelayTick
= IP6_MAX_RTR_SOLICITATION_DELAY
/ IP6_TIMER_INTERVAL_IN_MS
;
1017 Entry
->RetransTick
= (MaxDelayTick
* ((NET_RANDOM (NetRandomInitSeed ()) % 5) + 1)) / 5;
1018 Entry
->AddressInfo
= AddressInfo
;
1019 Entry
->Callback
= Callback
;
1020 Entry
->Context
= Context
;
1021 InsertTailList (&IpIf
->DupAddrDetectList
, &Entry
->Link
);
1023 if (Entry
->MaxTransmit
== 0) {
1025 // DAD is disabled on this interface, immediately mark this DAD successful.
1027 Ip6OnDADFinished (TRUE
, IpIf
, Entry
);
1034 Search IP6_DAD_ENTRY from the Duplicate Address Detection List.
1036 @param[in] IpSb The pointer to the IP6_SERVICE instance.
1037 @param[in] Target The address information which needs DAD performed .
1038 @param[out] Interface If not NULL, output the IP6 interface that configures
1039 the tentative address.
1041 @return NULL if failed to find the matching DAD entry.
1042 Otherwise, point to the found DAD entry.
1047 IN IP6_SERVICE
*IpSb
,
1048 IN EFI_IPv6_ADDRESS
*Target
,
1049 OUT IP6_INTERFACE
**Interface OPTIONAL
1054 IP6_INTERFACE
*IpIf
;
1055 IP6_DAD_ENTRY
*DupAddrDetect
;
1056 IP6_ADDRESS_INFO
*AddrInfo
;
1058 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1059 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP6_INTERFACE
, Link
);
1061 NET_LIST_FOR_EACH (Entry2
, &IpIf
->DupAddrDetectList
) {
1062 DupAddrDetect
= NET_LIST_USER_STRUCT_S (Entry2
, IP6_DAD_ENTRY
, Link
, IP6_DAD_ENTRY_SIGNATURE
);
1063 AddrInfo
= DupAddrDetect
->AddressInfo
;
1064 if (EFI_IP6_EQUAL (&AddrInfo
->Address
, Target
)) {
1065 if (Interface
!= NULL
) {
1068 return DupAddrDetect
;
1077 Generate router solicit message and send it out to Destination Address or
1078 All Router Link Local scope multicast address.
1080 @param[in] IpSb The IP service to send the packet.
1081 @param[in] Interface If not NULL, points to the IP6 interface to send
1083 @param[in] SourceAddress If not NULL, the source address of the message.
1084 @param[in] DestinationAddress If not NULL, the destination address of the message.
1085 @param[in] SourceLinkAddress If not NULL, the MAC address of the source.
1086 A source link-layer address option will be appended
1089 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1091 @retval EFI_SUCCESS The router solicit message was successfully sent.
1095 Ip6SendRouterSolicit (
1096 IN IP6_SERVICE
*IpSb
,
1097 IN IP6_INTERFACE
*Interface OPTIONAL
,
1098 IN EFI_IPv6_ADDRESS
*SourceAddress OPTIONAL
,
1099 IN EFI_IPv6_ADDRESS
*DestinationAddress OPTIONAL
,
1100 IN EFI_MAC_ADDRESS
*SourceLinkAddress OPTIONAL
1104 EFI_IP6_HEADER Head
;
1105 IP6_ICMP_INFORMATION_HEAD
*IcmpHead
;
1106 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
1108 IP6_INTERFACE
*IpIf
;
1110 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1113 if (IpIf
== NULL
&& IpSb
->DefaultInterface
!= NULL
) {
1114 IpIf
= IpSb
->DefaultInterface
;
1118 // Generate the packet to be sent
1121 PayloadLen
= (UINT16
) sizeof (IP6_ICMP_INFORMATION_HEAD
);
1122 if (SourceLinkAddress
!= NULL
) {
1123 PayloadLen
+= sizeof (IP6_ETHER_ADDR_OPTION
);
1126 Packet
= NetbufAlloc (sizeof (EFI_IP6_HEADER
) + (UINT32
) PayloadLen
);
1127 if (Packet
== NULL
) {
1128 return EFI_OUT_OF_RESOURCES
;
1132 // Create the basic IPv6 header.
1134 Head
.FlowLabelL
= 0;
1135 Head
.FlowLabelH
= 0;
1136 Head
.PayloadLength
= HTONS (PayloadLen
);
1137 Head
.NextHeader
= IP6_ICMP
;
1138 Head
.HopLimit
= IP6_HOP_LIMIT
;
1140 if (SourceAddress
!= NULL
) {
1141 IP6_COPY_ADDRESS (&Head
.SourceAddress
, SourceAddress
);
1143 ZeroMem (&Head
.SourceAddress
, sizeof (EFI_IPv6_ADDRESS
));
1147 if (DestinationAddress
!= NULL
) {
1148 IP6_COPY_ADDRESS (&Head
.DestinationAddress
, DestinationAddress
);
1150 Ip6SetToAllNodeMulticast (TRUE
, IP6_LINK_LOCAL_SCOPE
, &Head
.DestinationAddress
);
1153 NetbufReserve (Packet
, sizeof (EFI_IP6_HEADER
));
1156 // Fill in the ICMP header, and Source link-layer address if contained.
1159 IcmpHead
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufAllocSpace (Packet
, sizeof (IP6_ICMP_INFORMATION_HEAD
), FALSE
);
1160 ASSERT (IcmpHead
!= NULL
);
1161 ZeroMem (IcmpHead
, sizeof (IP6_ICMP_INFORMATION_HEAD
));
1162 IcmpHead
->Head
.Type
= ICMP_V6_ROUTER_SOLICIT
;
1163 IcmpHead
->Head
.Code
= 0;
1165 LinkLayerOption
= NULL
;
1166 if (SourceLinkAddress
!= NULL
) {
1167 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) NetbufAllocSpace (
1169 sizeof (IP6_ETHER_ADDR_OPTION
),
1172 ASSERT (LinkLayerOption
!= NULL
);
1173 LinkLayerOption
->Type
= Ip6OptionEtherSource
;
1174 LinkLayerOption
->Length
= (UINT8
) sizeof (IP6_ETHER_ADDR_OPTION
);
1175 CopyMem (LinkLayerOption
->EtherAddr
, SourceLinkAddress
, 6);
1179 // Transmit the packet
1181 return Ip6Output (IpSb
, IpIf
, NULL
, Packet
, &Head
, NULL
, 0, Ip6SysPacketSent
, NULL
);
1185 Generate a Neighbor Advertisement message and send it out to Destination Address.
1187 @param[in] IpSb The IP service to send the packet.
1188 @param[in] SourceAddress The source address of the message.
1189 @param[in] DestinationAddress The destination address of the message.
1190 @param[in] TargetIp6Address The target address field in the Neighbor Solicitation
1191 message that prompted this advertisement.
1192 @param[in] TargetLinkAddress The MAC address for the target, i.e. the sender
1193 of the advertisement.
1194 @param[in] IsRouter If TRUE, indicates the sender is a router.
1195 @param[in] Override If TRUE, indicates the advertisement should override
1196 an existing cache entry and update the MAC address.
1197 @param[in] Solicited If TRUE, indicates the advertisement was sent
1198 in response to a Neighbor Solicitation from
1199 the Destination address.
1201 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1203 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1207 Ip6SendNeighborAdvertise (
1208 IN IP6_SERVICE
*IpSb
,
1209 IN EFI_IPv6_ADDRESS
*SourceAddress
,
1210 IN EFI_IPv6_ADDRESS
*DestinationAddress
,
1211 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
1212 IN EFI_MAC_ADDRESS
*TargetLinkAddress
,
1213 IN BOOLEAN IsRouter
,
1214 IN BOOLEAN Override
,
1215 IN BOOLEAN Solicited
1219 EFI_IP6_HEADER Head
;
1220 IP6_ICMP_INFORMATION_HEAD
*IcmpHead
;
1221 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
1222 EFI_IPv6_ADDRESS
*Target
;
1225 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1228 // The Neighbor Advertisement message must include a Target link-layer address option
1229 // when responding to multicast solicitation and should include such option when
1230 // responding to unicast solicitation. It also must include such option as unsolicited
1233 ASSERT (DestinationAddress
!= NULL
&& TargetIp6Address
!= NULL
&& TargetLinkAddress
!= NULL
);
1235 PayloadLen
= (UINT16
) (sizeof (IP6_ICMP_INFORMATION_HEAD
) + sizeof (EFI_IPv6_ADDRESS
) + sizeof (IP6_ETHER_ADDR_OPTION
));
1238 // Generate the packet to be sent
1241 Packet
= NetbufAlloc (sizeof (EFI_IP6_HEADER
) + (UINT32
) PayloadLen
);
1242 if (Packet
== NULL
) {
1243 return EFI_OUT_OF_RESOURCES
;
1247 // Create the basic IPv6 header.
1249 Head
.FlowLabelL
= 0;
1250 Head
.FlowLabelH
= 0;
1251 Head
.PayloadLength
= HTONS (PayloadLen
);
1252 Head
.NextHeader
= IP6_ICMP
;
1253 Head
.HopLimit
= IP6_HOP_LIMIT
;
1255 IP6_COPY_ADDRESS (&Head
.SourceAddress
, SourceAddress
);
1256 IP6_COPY_ADDRESS (&Head
.DestinationAddress
, DestinationAddress
);
1258 NetbufReserve (Packet
, sizeof (EFI_IP6_HEADER
));
1261 // Fill in the ICMP header, Target address, and Target link-layer address.
1262 // Set the Router flag, Solicited flag and Override flag.
1265 IcmpHead
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufAllocSpace (Packet
, sizeof (IP6_ICMP_INFORMATION_HEAD
), FALSE
);
1266 ASSERT (IcmpHead
!= NULL
);
1267 ZeroMem (IcmpHead
, sizeof (IP6_ICMP_INFORMATION_HEAD
));
1268 IcmpHead
->Head
.Type
= ICMP_V6_NEIGHBOR_ADVERTISE
;
1269 IcmpHead
->Head
.Code
= 0;
1272 IcmpHead
->Fourth
|= IP6_IS_ROUTER_FLAG
;
1276 IcmpHead
->Fourth
|= IP6_SOLICITED_FLAG
;
1280 IcmpHead
->Fourth
|= IP6_OVERRIDE_FLAG
;
1283 Target
= (EFI_IPv6_ADDRESS
*) NetbufAllocSpace (Packet
, sizeof (EFI_IPv6_ADDRESS
), FALSE
);
1284 ASSERT (Target
!= NULL
);
1285 IP6_COPY_ADDRESS (Target
, TargetIp6Address
);
1287 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) NetbufAllocSpace (
1289 sizeof (IP6_ETHER_ADDR_OPTION
),
1292 ASSERT (LinkLayerOption
!= NULL
);
1293 LinkLayerOption
->Type
= Ip6OptionEtherTarget
;
1294 LinkLayerOption
->Length
= 1;
1295 CopyMem (LinkLayerOption
->EtherAddr
, TargetLinkAddress
, 6);
1298 // Transmit the packet
1300 return Ip6Output (IpSb
, NULL
, NULL
, Packet
, &Head
, NULL
, 0, Ip6SysPacketSent
, NULL
);
1304 Generate the Neighbor Solicitation message and send it to the Destination Address.
1306 @param[in] IpSb The IP service to send the packet
1307 @param[in] SourceAddress The source address of the message.
1308 @param[in] DestinationAddress The destination address of the message.
1309 @param[in] TargetIp6Address The IP address of the target of the solicitation.
1310 It must not be a multicast address.
1311 @param[in] SourceLinkAddress The MAC address for the sender. If not NULL,
1312 a source link-layer address option will be appended
1315 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
1316 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1318 @retval EFI_SUCCESS The Neighbor Advertise message was successfully sent.
1322 Ip6SendNeighborSolicit (
1323 IN IP6_SERVICE
*IpSb
,
1324 IN EFI_IPv6_ADDRESS
*SourceAddress
,
1325 IN EFI_IPv6_ADDRESS
*DestinationAddress
,
1326 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
1327 IN EFI_MAC_ADDRESS
*SourceLinkAddress OPTIONAL
1331 EFI_IP6_HEADER Head
;
1332 IP6_ICMP_INFORMATION_HEAD
*IcmpHead
;
1333 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
1334 EFI_IPv6_ADDRESS
*Target
;
1337 IP6_NEIGHBOR_ENTRY
*Neighbor
;
1340 // Check input parameters
1342 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1343 if (DestinationAddress
== NULL
|| TargetIp6Address
== NULL
) {
1344 return EFI_INVALID_PARAMETER
;
1349 if (SourceAddress
== NULL
|| (SourceAddress
!= NULL
&& NetIp6IsUnspecifiedAddr (SourceAddress
))) {
1354 // The Neighbor Solicitation message should include a source link-layer address option
1355 // if the solicitation is not sent by performing DAD - Duplicate Address Detection.
1356 // Otherwise must not include it.
1358 PayloadLen
= (UINT16
) (sizeof (IP6_ICMP_INFORMATION_HEAD
) + sizeof (EFI_IPv6_ADDRESS
));
1361 if (SourceLinkAddress
== NULL
) {
1362 return EFI_INVALID_PARAMETER
;
1365 PayloadLen
= (UINT16
) (PayloadLen
+ sizeof (IP6_ETHER_ADDR_OPTION
));
1369 // Generate the packet to be sent
1372 Packet
= NetbufAlloc (sizeof (EFI_IP6_HEADER
) + (UINT32
) PayloadLen
);
1373 if (Packet
== NULL
) {
1374 return EFI_OUT_OF_RESOURCES
;
1378 // Create the basic IPv6 header
1380 Head
.FlowLabelL
= 0;
1381 Head
.FlowLabelH
= 0;
1382 Head
.PayloadLength
= HTONS (PayloadLen
);
1383 Head
.NextHeader
= IP6_ICMP
;
1384 Head
.HopLimit
= IP6_HOP_LIMIT
;
1386 if (SourceAddress
!= NULL
) {
1387 IP6_COPY_ADDRESS (&Head
.SourceAddress
, SourceAddress
);
1389 ZeroMem (&Head
.SourceAddress
, sizeof (EFI_IPv6_ADDRESS
));
1392 IP6_COPY_ADDRESS (&Head
.DestinationAddress
, DestinationAddress
);
1394 NetbufReserve (Packet
, sizeof (EFI_IP6_HEADER
));
1397 // Fill in the ICMP header, Target address, and Source link-layer address.
1399 IcmpHead
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufAllocSpace (Packet
, sizeof (IP6_ICMP_INFORMATION_HEAD
), FALSE
);
1400 ASSERT (IcmpHead
!= NULL
);
1401 ZeroMem (IcmpHead
, sizeof (IP6_ICMP_INFORMATION_HEAD
));
1402 IcmpHead
->Head
.Type
= ICMP_V6_NEIGHBOR_SOLICIT
;
1403 IcmpHead
->Head
.Code
= 0;
1405 Target
= (EFI_IPv6_ADDRESS
*) NetbufAllocSpace (Packet
, sizeof (EFI_IPv6_ADDRESS
), FALSE
);
1406 ASSERT (Target
!= NULL
);
1407 IP6_COPY_ADDRESS (Target
, TargetIp6Address
);
1409 LinkLayerOption
= NULL
;
1413 // Fill in the source link-layer address option
1415 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) NetbufAllocSpace (
1417 sizeof (IP6_ETHER_ADDR_OPTION
),
1420 ASSERT (LinkLayerOption
!= NULL
);
1421 LinkLayerOption
->Type
= Ip6OptionEtherSource
;
1422 LinkLayerOption
->Length
= 1;
1423 CopyMem (LinkLayerOption
->EtherAddr
, SourceLinkAddress
, 6);
1427 // Create a Neighbor Cache entry in the INCOMPLETE state when performing
1428 // address resolution.
1430 if (!IsDAD
&& Ip6IsSNMulticastAddr (DestinationAddress
)) {
1431 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
1432 if (Neighbor
== NULL
) {
1433 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, TargetIp6Address
, NULL
);
1434 ASSERT (Neighbor
!= NULL
);
1439 // Transmit the packet
1441 return Ip6Output (IpSb
, IpSb
->DefaultInterface
, NULL
, Packet
, &Head
, NULL
, 0, Ip6SysPacketSent
, NULL
);
1445 Process the Neighbor Solicitation message. The message may be sent for Duplicate
1446 Address Detection or Address Resolution.
1448 @param[in] IpSb The IP service that received the packet.
1449 @param[in] Head The IP head of the message.
1450 @param[in] Packet The content of the message with IP head removed.
1452 @retval EFI_SUCCESS The packet processed successfully.
1453 @retval EFI_INVALID_PARAMETER The packet is invalid.
1454 @retval EFI_ICMP_ERROR The packet indicates that DAD is failed.
1455 @retval Others Failed to process the packet.
1459 Ip6ProcessNeighborSolicit (
1460 IN IP6_SERVICE
*IpSb
,
1461 IN EFI_IP6_HEADER
*Head
,
1465 IP6_ICMP_INFORMATION_HEAD Icmp
;
1466 EFI_IPv6_ADDRESS Target
;
1467 IP6_ETHER_ADDR_OPTION LinkLayerOption
;
1470 BOOLEAN IsMaintained
;
1471 IP6_DAD_ENTRY
*DupAddrDetect
;
1472 IP6_INTERFACE
*IpIf
;
1473 IP6_NEIGHBOR_ENTRY
*Neighbor
;
1475 BOOLEAN UpdateCache
;
1476 EFI_IPv6_ADDRESS Dest
;
1483 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1484 NetbufCopy (Packet
, sizeof (Icmp
), sizeof (Target
), Target
.Addr
);
1487 // Perform Message Validation:
1488 // The IP Hop Limit field has a value of 255, i.e., the packet
1489 // could not possibly have been forwarded by a router.
1491 // Target Address is not a multicast address.
1493 Status
= EFI_INVALID_PARAMETER
;
1495 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
.Head
.Code
!= 0 || !NetIp6IsValidUnicast (&Target
)) {
1500 // ICMP length is 24 or more octets.
1503 if (Head
->PayloadLength
< IP6_ND_LENGTH
) {
1506 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_ND_LENGTH
);
1507 if (OptionLen
!= 0) {
1508 Option
= NetbufGetByte (Packet
, IP6_ND_LENGTH
, NULL
);
1509 ASSERT (Option
!= NULL
);
1512 // All included options should have a length that is greater than zero.
1514 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
1520 IsDAD
= NetIp6IsUnspecifiedAddr (&Head
->SourceAddress
);
1521 IsUnicast
= (BOOLEAN
) !Ip6IsSNMulticastAddr (&Head
->DestinationAddress
);
1522 IsMaintained
= Ip6IsOneOfSetAddress (IpSb
, &Target
, &IpIf
, NULL
);
1525 if (OptionLen
>= sizeof (IP6_ETHER_ADDR_OPTION
)) {
1529 sizeof (IP6_ETHER_ADDR_OPTION
),
1530 (UINT8
*) &LinkLayerOption
1533 // The solicitation for neighbor discovery should include a source link-layer
1534 // address option. If the option is not recognized, silently ignore it.
1536 if (LinkLayerOption
.Type
== Ip6OptionEtherSource
) {
1539 // If the IP source address is the unspecified address, the source
1540 // link-layer address option must not be included in the message.
1550 // If the IP source address is the unspecified address, the IP
1551 // destination address is a solicited-node multicast address.
1553 if (IsDAD
&& IsUnicast
) {
1558 // If the target address is tentative, and the source address is a unicast address,
1559 // the solicitation's sender is performing address resolution on the target;
1560 // the solicitation should be silently ignored.
1562 if (!IsDAD
&& !IsMaintained
) {
1567 // If received unicast neighbor solicitation but destination is not this node,
1570 if (IsUnicast
&& !IsMaintained
) {
1575 // In DAD, when target address is a tentative address,
1576 // process the received neighbor solicitation message but not send out response.
1578 if (IsDAD
&& !IsMaintained
) {
1579 DupAddrDetect
= Ip6FindDADEntry (IpSb
, &Target
, &IpIf
);
1580 if (DupAddrDetect
!= NULL
) {
1582 // Check the MAC address of the incoming packet.
1584 if (IpSb
->RecvRequest
.MnpToken
.Packet
.RxData
== NULL
) {
1588 MacAddress
= IpSb
->RecvRequest
.MnpToken
.Packet
.RxData
->SourceAddress
;
1589 if (MacAddress
!= NULL
) {
1592 &IpSb
->SnpMode
.CurrentAddress
,
1593 IpSb
->SnpMode
.HwAddressSize
1596 // The NS is from another node to performing DAD on the same address.
1597 // Fail DAD for the tentative address.
1599 Ip6OnDADFinished (FALSE
, IpIf
, DupAddrDetect
);
1600 Status
= EFI_ICMP_ERROR
;
1603 // The below layer loopback the NS we sent. Record it and wait for more.
1605 DupAddrDetect
->Receive
++;
1606 Status
= EFI_SUCCESS
;
1614 // If the solicitation does not contain a link-layer address, DO NOT create or
1615 // update the neighbor cache entries.
1618 Neighbor
= Ip6FindNeighborEntry (IpSb
, &Head
->SourceAddress
);
1619 UpdateCache
= FALSE
;
1621 if (Neighbor
== NULL
) {
1622 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, &Head
->SourceAddress
, NULL
);
1623 if (Neighbor
== NULL
) {
1624 Status
= EFI_OUT_OF_RESOURCES
;
1629 if (CompareMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6) != 0) {
1635 Neighbor
->State
= EfiNeighborStale
;
1636 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1637 CopyMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1639 // Send queued packets if exist.
1641 Neighbor
->CallBack ((VOID
*) Neighbor
);
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.
1651 if (IsDAD
&& IsMaintained
) {
1653 Ip6SetToAllNodeMulticast (FALSE
, IP6_LINK_LOCAL_SCOPE
, &Dest
);
1656 IP6_COPY_ADDRESS (&Dest
, &Head
->SourceAddress
);
1659 Status
= Ip6SendNeighborAdvertise (
1664 &IpSb
->SnpMode
.CurrentAddress
,
1670 NetbufFree (Packet
);
1675 Process the Neighbor Advertisement message.
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.
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.
1688 Ip6ProcessNeighborAdvertise (
1689 IN IP6_SERVICE
*IpSb
,
1690 IN EFI_IP6_HEADER
*Head
,
1694 IP6_ICMP_INFORMATION_HEAD Icmp
;
1695 EFI_IPv6_ADDRESS Target
;
1696 IP6_ETHER_ADDR_OPTION LinkLayerOption
;
1699 IP6_NEIGHBOR_ENTRY
*Neighbor
;
1700 IP6_DEFAULT_ROUTER
*DefaultRouter
;
1704 IP6_DAD_ENTRY
*DupAddrDetect
;
1705 IP6_INTERFACE
*IpIf
;
1710 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1711 NetbufCopy (Packet
, sizeof (Icmp
), sizeof (Target
), Target
.Addr
);
1714 // Validate the incoming Neighbor Advertisement
1716 Status
= EFI_INVALID_PARAMETER
;
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.
1721 // Target Address is not a multicast address.
1723 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
.Head
.Code
!= 0 || !NetIp6IsValidUnicast (&Target
)) {
1728 // ICMP length is 24 or more octets.
1732 if (Head
->PayloadLength
< IP6_ND_LENGTH
) {
1735 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_ND_LENGTH
);
1736 if (OptionLen
!= 0) {
1737 Option
= NetbufGetByte (Packet
, IP6_ND_LENGTH
, NULL
);
1738 ASSERT (Option
!= NULL
);
1741 // All included options should have a length that is greater than zero.
1743 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
1750 // If the IP destination address is a multicast address, Solicited Flag is ZERO.
1753 if ((Icmp
.Fourth
& IP6_SOLICITED_FLAG
) == IP6_SOLICITED_FLAG
) {
1756 if (IP6_IS_MULTICAST (&Head
->DestinationAddress
) && Solicited
) {
1761 // DAD - Check whether the Target is one of our tentative address.
1763 DupAddrDetect
= Ip6FindDADEntry (IpSb
, &Target
, &IpIf
);
1764 if (DupAddrDetect
!= NULL
) {
1766 // DAD fails, some other node is using this address.
1768 NetbufFree (Packet
);
1769 Ip6OnDADFinished (FALSE
, IpIf
, DupAddrDetect
);
1770 return EFI_ICMP_ERROR
;
1774 // Search the Neighbor Cache for the target's entry. If no entry exists,
1775 // the advertisement should be silently discarded.
1777 Neighbor
= Ip6FindNeighborEntry (IpSb
, &Target
);
1778 if (Neighbor
== NULL
) {
1783 // Get IsRouter Flag and Override Flag
1787 if ((Icmp
.Fourth
& IP6_IS_ROUTER_FLAG
) == IP6_IS_ROUTER_FLAG
) {
1790 if ((Icmp
.Fourth
& IP6_OVERRIDE_FLAG
) == IP6_OVERRIDE_FLAG
) {
1795 // Check whether link layer option is included.
1797 if (OptionLen
>= sizeof (IP6_ETHER_ADDR_OPTION
)) {
1801 sizeof (IP6_ETHER_ADDR_OPTION
),
1802 (UINT8
*) &LinkLayerOption
1805 if (LinkLayerOption
.Type
== Ip6OptionEtherTarget
) {
1812 Compare
= CompareMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1815 if (!Neighbor
->IsRouter
&& IsRouter
) {
1816 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &Target
);
1817 if (DefaultRouter
!= NULL
) {
1818 DefaultRouter
->NeighborCache
= Neighbor
;
1822 if (Neighbor
->State
== EfiNeighborInComplete
) {
1824 // If the target's Neighbor Cache entry is in INCOMPLETE state and no
1825 // Target Link-Layer address option is included while link layer has
1826 // address, the message should be silently discarded.
1832 // Update the Neighbor Cache
1834 CopyMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1836 Neighbor
->State
= EfiNeighborReachable
;
1837 Neighbor
->Ticks
= IP6_GET_TICKS (IpSb
->ReachableTime
);
1839 Neighbor
->State
= EfiNeighborStale
;
1840 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1842 // Send any packets queued for the neighbor awaiting address resolution.
1844 Neighbor
->CallBack ((VOID
*) Neighbor
);
1847 Neighbor
->IsRouter
= IsRouter
;
1850 if (!Override
&& Compare
!= 0) {
1852 // When the Override Flag is clear and supplied link-layer address differs from
1853 // that in the cache, if the state of the entry is not REACHABLE, ignore the
1854 // message. Otherwise set it to STALE but do not update the entry in any
1857 if (Neighbor
->State
== EfiNeighborReachable
) {
1858 Neighbor
->State
= EfiNeighborStale
;
1859 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1863 CopyMem (Neighbor
->LinkAddress
.Addr
, LinkLayerOption
.EtherAddr
, 6);
1866 // Update the entry's state
1869 Neighbor
->State
= EfiNeighborReachable
;
1870 Neighbor
->Ticks
= IP6_GET_TICKS (IpSb
->ReachableTime
);
1873 Neighbor
->State
= EfiNeighborStale
;
1874 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
1879 // When IsRouter is changed from TRUE to FALSE, remove the router from the
1880 // Default Router List and remove the Destination Cache entries for all destinations
1881 // using the neighbor as a router.
1883 if (Neighbor
->IsRouter
&& !IsRouter
) {
1884 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &Target
);
1885 if (DefaultRouter
!= NULL
) {
1886 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
1890 Neighbor
->IsRouter
= IsRouter
;
1894 if (Neighbor
->State
== EfiNeighborReachable
) {
1895 Neighbor
->CallBack ((VOID
*) Neighbor
);
1898 Status
= EFI_SUCCESS
;
1901 NetbufFree (Packet
);
1906 Process the Router Advertisement message according to RFC4861.
1908 @param[in] IpSb The IP service that received the packet.
1909 @param[in] Head The IP head of the message.
1910 @param[in] Packet The content of the message with the IP head removed.
1912 @retval EFI_SUCCESS The packet processed successfully.
1913 @retval EFI_INVALID_PARAMETER The packet is invalid.
1914 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
1916 @retval Others Failed to process the packet.
1920 Ip6ProcessRouterAdvertise (
1921 IN IP6_SERVICE
*IpSb
,
1922 IN EFI_IP6_HEADER
*Head
,
1926 IP6_ICMP_INFORMATION_HEAD Icmp
;
1927 UINT32 ReachableTime
;
1928 UINT32 RetransTimer
;
1929 UINT16 RouterLifetime
;
1933 IP6_ETHER_ADDR_OPTION LinkLayerOption
;
1938 IP6_DEFAULT_ROUTER
*DefaultRouter
;
1939 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
1940 EFI_MAC_ADDRESS LinkLayerAddress
;
1941 IP6_MTU_OPTION MTUOption
;
1942 IP6_PREFIX_INFO_OPTION PrefixOption
;
1943 IP6_PREFIX_LIST_ENTRY
*PrefixList
;
1946 EFI_IPv6_ADDRESS StatelessAddress
;
1952 Status
= EFI_INVALID_PARAMETER
;
1954 if (IpSb
->Ip6ConfigInstance
.Policy
!= Ip6ConfigPolicyAutomatic
) {
1956 // Skip the process below as it's not required under the current policy.
1961 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1964 // Validate the incoming Router Advertisement
1968 // The IP source address must be a link-local address
1970 if (!NetIp6IsLinkLocalAddr (&Head
->SourceAddress
)) {
1974 // The IP Hop Limit field has a value of 255, i.e. the packet
1975 // could not possibly have been forwarded by a router.
1977 // ICMP length (derived from the IP length) is 16 or more octets.
1979 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
.Head
.Code
!= 0 ||
1980 Head
->PayloadLength
< IP6_RA_LENGTH
) {
1985 // All included options have a length that is greater than zero.
1987 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_RA_LENGTH
);
1988 if (OptionLen
!= 0) {
1989 Option
= NetbufGetByte (Packet
, IP6_RA_LENGTH
, NULL
);
1990 ASSERT (Option
!= NULL
);
1992 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
1998 // Process Fourth field.
1999 // In Router Advertisement, Fourth is composed of CurHopLimit (8bit), M flag, O flag,
2000 // and Router Lifetime (16 bit).
2003 Fourth
= NTOHL (Icmp
.Fourth
);
2004 CopyMem (&RouterLifetime
, &Fourth
, sizeof (UINT16
));
2007 // If the source address already in the default router list, update it.
2008 // Otherwise create a new entry.
2009 // A Lifetime of zero indicates that the router is not a default router.
2011 DefaultRouter
= Ip6FindDefaultRouter (IpSb
, &Head
->SourceAddress
);
2012 if (DefaultRouter
== NULL
) {
2013 if (RouterLifetime
!= 0) {
2014 DefaultRouter
= Ip6CreateDefaultRouter (IpSb
, &Head
->SourceAddress
, RouterLifetime
);
2015 if (DefaultRouter
== NULL
) {
2016 Status
= EFI_OUT_OF_RESOURCES
;
2021 if (RouterLifetime
!= 0) {
2022 DefaultRouter
->Lifetime
= RouterLifetime
;
2024 // Check the corresponding neighbor cache entry here.
2026 if (DefaultRouter
->NeighborCache
== NULL
) {
2027 DefaultRouter
->NeighborCache
= Ip6FindNeighborEntry (IpSb
, &Head
->SourceAddress
);
2031 // If the address is in the host's default router list and the router lifetime is zero,
2032 // immediately time-out the entry.
2034 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
2038 CurHopLimit
= *((UINT8
*) &Fourth
+ 3);
2039 if (CurHopLimit
!= 0) {
2040 IpSb
->CurHopLimit
= CurHopLimit
;
2045 if ((*((UINT8
*) &Fourth
+ 2) & IP6_M_ADDR_CONFIG_FLAG
) == IP6_M_ADDR_CONFIG_FLAG
) {
2048 if ((*((UINT8
*) &Fourth
+ 2) & IP6_O_CONFIG_FLAG
) == IP6_O_CONFIG_FLAG
) {
2053 if (Mflag
|| Oflag
) {
2055 // Use Ip6Config to get available addresses or other configuration from DHCP.
2057 Ip6ConfigStartStatefulAutoConfig (&IpSb
->Ip6ConfigInstance
, Oflag
);
2061 // Process Reachable Time and Retrans Timer fields.
2063 NetbufCopy (Packet
, sizeof (Icmp
), sizeof (UINT32
), (UINT8
*) &ReachableTime
);
2064 NetbufCopy (Packet
, sizeof (Icmp
) + sizeof (UINT32
), sizeof (UINT32
), (UINT8
*) &RetransTimer
);
2065 ReachableTime
= NTOHL (ReachableTime
);
2066 RetransTimer
= NTOHL (RetransTimer
);
2068 if (ReachableTime
!= 0 && ReachableTime
!= IpSb
->BaseReachableTime
) {
2070 // If new value is not unspecified and differs from the previous one, record it
2071 // in BaseReachableTime and recompute a ReachableTime.
2073 IpSb
->BaseReachableTime
= ReachableTime
;
2074 Ip6UpdateReachableTime (IpSb
);
2077 if (RetransTimer
!= 0) {
2078 IpSb
->RetransTimer
= RetransTimer
;
2082 // IsRouter flag must be set to TRUE if corresponding neighbor cache entry exists.
2084 NeighborCache
= Ip6FindNeighborEntry (IpSb
, &Head
->SourceAddress
);
2085 if (NeighborCache
!= NULL
) {
2086 NeighborCache
->IsRouter
= TRUE
;
2090 // If an valid router advertisment is received, stops router solicitation.
2092 IpSb
->RouterAdvertiseReceived
= TRUE
;
2095 // The only defined options that may appear are the Source
2096 // Link-Layer Address, Prefix information and MTU options.
2097 // All included options have a length that is greater than zero.
2100 while (Offset
< Head
->PayloadLength
) {
2101 NetbufCopy (Packet
, Offset
, sizeof (UINT8
), &Type
);
2103 case Ip6OptionEtherSource
:
2105 // Update the neighbor cache
2107 NetbufCopy (Packet
, Offset
, sizeof (IP6_ETHER_ADDR_OPTION
), (UINT8
*) &LinkLayerOption
);
2108 if (LinkLayerOption
.Length
<= 0) {
2112 ZeroMem (&LinkLayerAddress
, sizeof (EFI_MAC_ADDRESS
));
2113 CopyMem (&LinkLayerAddress
, LinkLayerOption
.EtherAddr
, 6);
2115 if (NeighborCache
== NULL
) {
2116 NeighborCache
= Ip6CreateNeighborEntry (
2119 &Head
->SourceAddress
,
2122 if (NeighborCache
== NULL
) {
2123 Status
= EFI_OUT_OF_RESOURCES
;
2126 NeighborCache
->IsRouter
= TRUE
;
2127 NeighborCache
->State
= EfiNeighborStale
;
2128 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2130 Result
= CompareMem (&LinkLayerAddress
, &NeighborCache
->LinkAddress
, 6);
2133 // If the link-local address is the same as that already in the cache,
2134 // the cache entry's state remains unchanged. Otherwise update the
2135 // reachability state to STALE.
2137 if ((NeighborCache
->State
== EfiNeighborInComplete
) || (Result
!= 0)) {
2138 CopyMem (&NeighborCache
->LinkAddress
, &LinkLayerAddress
, 6);
2140 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2142 if (NeighborCache
->State
== EfiNeighborInComplete
) {
2144 // Send queued packets if exist.
2146 NeighborCache
->State
= EfiNeighborStale
;
2147 NeighborCache
->CallBack ((VOID
*) NeighborCache
);
2149 NeighborCache
->State
= EfiNeighborStale
;
2154 Offset
= (UINT16
) (Offset
+ (UINT16
) LinkLayerOption
.Length
* 8);
2156 case Ip6OptionPrefixInfo
:
2157 NetbufCopy (Packet
, Offset
, sizeof (IP6_PREFIX_INFO_OPTION
), (UINT8
*) &PrefixOption
);
2158 if (PrefixOption
.Length
!= 4) {
2161 PrefixOption
.ValidLifetime
= NTOHL (PrefixOption
.ValidLifetime
);
2162 PrefixOption
.PreferredLifetime
= NTOHL (PrefixOption
.PreferredLifetime
);
2165 // Get L and A flag, recorded in the lower 2 bits of Reserved1
2168 if ((PrefixOption
.Reserved1
& IP6_ON_LINK_FLAG
) == IP6_ON_LINK_FLAG
) {
2172 if ((PrefixOption
.Reserved1
& IP6_AUTO_CONFIG_FLAG
) == IP6_AUTO_CONFIG_FLAG
) {
2177 // If the prefix is the link-local prefix, silently ignore the prefix option.
2179 if (PrefixOption
.PrefixLength
== IP6_LINK_LOCAL_PREFIX_LENGTH
&&
2180 NetIp6IsLinkLocalAddr (&PrefixOption
.Prefix
)
2182 Offset
+= sizeof (IP6_PREFIX_INFO_OPTION
);
2186 // Do following if on-link flag is set according to RFC4861.
2189 PrefixList
= Ip6FindPrefixListEntry (
2192 PrefixOption
.PrefixLength
,
2193 &PrefixOption
.Prefix
2196 // Create a new entry for the prefix, if the ValidLifetime is zero,
2197 // silently ignore the prefix option.
2199 if (PrefixList
== NULL
&& PrefixOption
.ValidLifetime
!= 0) {
2200 PrefixList
= Ip6CreatePrefixListEntry (
2203 PrefixOption
.ValidLifetime
,
2204 PrefixOption
.PreferredLifetime
,
2205 PrefixOption
.PrefixLength
,
2206 &PrefixOption
.Prefix
2208 if (PrefixList
== NULL
) {
2209 Status
= EFI_OUT_OF_RESOURCES
;
2212 } else if (PrefixList
!= NULL
) {
2213 if (PrefixOption
.ValidLifetime
!= 0) {
2214 PrefixList
->ValidLifetime
= PrefixOption
.ValidLifetime
;
2217 // If the prefix exists and incoming ValidLifetime is zero, immediately
2218 // remove the prefix.
2219 Ip6DestroyPrefixListEntry (IpSb
, PrefixList
, OnLink
, TRUE
);
2225 // Do following if Autonomous flag is set according to RFC4862.
2227 if (Autonomous
&& PrefixOption
.PreferredLifetime
<= PrefixOption
.ValidLifetime
) {
2228 PrefixList
= Ip6FindPrefixListEntry (
2231 PrefixOption
.PrefixLength
,
2232 &PrefixOption
.Prefix
2235 // Create a new entry for the prefix, and form an address by prefix + interface id
2236 // If the sum of the prefix length and interface identifier length
2237 // does not equal 128 bits, the Prefix Information option MUST be ignored.
2239 if (PrefixList
== NULL
&&
2240 PrefixOption
.ValidLifetime
!= 0 &&
2241 PrefixOption
.PrefixLength
+ IpSb
->InterfaceIdLen
* 8 == 128
2244 // Form the address in network order.
2246 CopyMem (&StatelessAddress
, &PrefixOption
.Prefix
, sizeof (UINT64
));
2247 CopyMem (&StatelessAddress
.Addr
[8], IpSb
->InterfaceId
, sizeof (UINT64
));
2250 // If the address is not yet in the assigned address list, adds it into.
2252 if (!Ip6IsOneOfSetAddress (IpSb
, &StatelessAddress
, NULL
, NULL
)) {
2254 // And also not in the DAD process, check its uniqeness firstly.
2256 if (Ip6FindDADEntry (IpSb
, &StatelessAddress
, NULL
) == NULL
) {
2257 Status
= Ip6SetAddress (
2258 IpSb
->DefaultInterface
,
2261 PrefixOption
.PrefixLength
,
2262 PrefixOption
.ValidLifetime
,
2263 PrefixOption
.PreferredLifetime
,
2267 if (EFI_ERROR (Status
)) {
2274 // Adds the prefix option to stateless prefix option list.
2276 PrefixList
= Ip6CreatePrefixListEntry (
2279 PrefixOption
.ValidLifetime
,
2280 PrefixOption
.PreferredLifetime
,
2281 PrefixOption
.PrefixLength
,
2282 &PrefixOption
.Prefix
2284 if (PrefixList
== NULL
) {
2285 Status
= EFI_OUT_OF_RESOURCES
;
2288 } else if (PrefixList
!= NULL
) {
2291 // Reset the preferred lifetime of the address if the advertised prefix exists.
2292 // Perform specific action to valid lifetime together.
2294 PrefixList
->PreferredLifetime
= PrefixOption
.PreferredLifetime
;
2295 if ((PrefixOption
.ValidLifetime
> 7200) ||
2296 (PrefixOption
.ValidLifetime
> PrefixList
->ValidLifetime
)) {
2298 // If the received Valid Lifetime is greater than 2 hours or
2299 // greater than RemainingLifetime, set the valid lifetime of the
2300 // corresponding address to the advertised Valid Lifetime.
2302 PrefixList
->ValidLifetime
= PrefixOption
.ValidLifetime
;
2304 } else if (PrefixList
->ValidLifetime
<= 7200) {
2306 // If RemainingLifetime is less than or equls to 2 hours, ignore the
2307 // Prefix Information option with regards to the valid lifetime.
2308 // TODO: If this option has been authenticated, set the valid lifetime.
2312 // Otherwise, reset the valid lifetime of the corresponding
2313 // address to 2 hours.
2315 PrefixList
->ValidLifetime
= 7200;
2320 Offset
+= sizeof (IP6_PREFIX_INFO_OPTION
);
2323 NetbufCopy (Packet
, Offset
, sizeof (IP6_MTU_OPTION
), (UINT8
*) &MTUOption
);
2324 if (MTUOption
.Length
!= 1) {
2329 // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order
2330 // to omit implementation of Path MTU Discovery. Thus ignore the MTU option
2331 // in Router Advertisement.
2334 Offset
+= sizeof (IP6_MTU_OPTION
);
2338 // Silently ignore unrecognized options
2340 NetbufCopy (Packet
, Offset
+ sizeof (UINT8
), sizeof (UINT8
), &Length
);
2345 Offset
= (UINT16
) (Offset
+ (UINT16
) Length
* 8);
2350 Status
= EFI_SUCCESS
;
2353 NetbufFree (Packet
);
2358 Process the ICMPv6 redirect message. Find the instance, then update
2361 @param[in] IpSb The IP6 service binding instance that received
2363 @param[in] Head The IP head of the received ICMPv6 packet.
2364 @param[in] Packet The content of the ICMPv6 redirect packet with
2365 the IP head removed.
2367 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2368 @retval EFI_OUT_OF_RESOURCES Insuffcient resources to complete the
2370 @retval EFI_SUCCESS Successfully updated the route caches.
2374 Ip6ProcessRedirect (
2375 IN IP6_SERVICE
*IpSb
,
2376 IN EFI_IP6_HEADER
*Head
,
2380 IP6_ICMP_INFORMATION_HEAD
*Icmp
;
2381 EFI_IPv6_ADDRESS
*Target
;
2382 EFI_IPv6_ADDRESS
*IcmpDest
;
2385 IP6_ROUTE_ENTRY
*RouteEntry
;
2386 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
2387 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
2390 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
2391 EFI_MAC_ADDRESS Mac
;
2397 Status
= EFI_INVALID_PARAMETER
;
2399 Icmp
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
2405 // Validate the incoming Redirect message
2409 // The IP Hop Limit field has a value of 255, i.e. the packet
2410 // could not possibly have been forwarded by a router.
2412 // ICMP length (derived from the IP length) is 40 or more octets.
2414 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
->Head
.Code
!= 0 ||
2415 Head
->PayloadLength
< IP6_REDITECT_LENGTH
) {
2420 // The IP source address must be a link-local address
2422 if (!NetIp6IsLinkLocalAddr (&Head
->SourceAddress
)) {
2427 // The dest of this ICMP redirect message is not us.
2429 if (!Ip6IsOneOfSetAddress (IpSb
, &Head
->DestinationAddress
, NULL
, NULL
)) {
2434 // All included options have a length that is greater than zero.
2436 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_REDITECT_LENGTH
);
2437 if (OptionLen
!= 0) {
2438 Option
= NetbufGetByte (Packet
, IP6_REDITECT_LENGTH
, NULL
);
2439 ASSERT (Option
!= NULL
);
2441 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
2446 Target
= (EFI_IPv6_ADDRESS
*) (Icmp
+ 1);
2447 IcmpDest
= Target
+ 1;
2450 // The ICMP Destination Address field in the redirect message does not contain
2451 // a multicast address.
2453 if (IP6_IS_MULTICAST (IcmpDest
)) {
2458 // The ICMP Target Address is either a link-local address (when redirected to
2459 // a router) or the same as the ICMP Destination Address (when redirected to
2460 // the on-link destination).
2462 IsRouter
= (BOOLEAN
) !EFI_IP6_EQUAL (Target
, IcmpDest
);
2463 if (!NetIp6IsLinkLocalAddr (Target
) && IsRouter
) {
2468 // Check the options. The only interested option here is the target-link layer
2471 Length
= Packet
->TotalSize
- 40;
2472 Option
= (UINT8
*) (IcmpDest
+ 1);
2473 LinkLayerOption
= NULL
;
2474 while (Length
> 0) {
2476 case Ip6OptionEtherTarget
:
2478 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) Option
;
2479 OptLen
= LinkLayerOption
->Length
;
2482 // For ethernet, the length must be 1.
2490 OptLen
= *(Option
+ 1);
2493 // A length of 0 is invalid.
2500 Length
-= 8 * OptLen
;
2501 Option
+= 8 * OptLen
;
2509 // The IP source address of the Redirect is the same as the current
2510 // first-hop router for the specified ICMP Destination Address.
2512 RouteCache
= Ip6FindRouteCache (IpSb
->RouteTable
, IcmpDest
, &Head
->DestinationAddress
);
2513 if (RouteCache
!= NULL
) {
2514 if (!EFI_IP6_EQUAL (&RouteCache
->NextHop
, &Head
->SourceAddress
)) {
2516 // The source of this Redirect message must match the NextHop of the
2517 // corresponding route cache entry.
2523 // Update the NextHop.
2525 IP6_COPY_ADDRESS (&RouteCache
->NextHop
, Target
);
2528 RouteEntry
= (IP6_ROUTE_ENTRY
*) RouteCache
->Tag
;
2529 RouteEntry
->Flag
= RouteEntry
->Flag
| IP6_DIRECT_ROUTE
;
2534 // Get the Route Entry.
2536 RouteEntry
= Ip6FindRouteEntry (IpSb
->RouteTable
, IcmpDest
, NULL
);
2537 if (RouteEntry
== NULL
) {
2538 RouteEntry
= Ip6CreateRouteEntry (IcmpDest
, 0, NULL
);
2539 if (RouteEntry
== NULL
) {
2540 Status
= EFI_OUT_OF_RESOURCES
;
2546 RouteEntry
->Flag
= IP6_DIRECT_ROUTE
;
2550 // Create a route cache for this.
2552 RouteCache
= Ip6CreateRouteCacheEntry (
2554 &Head
->DestinationAddress
,
2558 if (RouteCache
== NULL
) {
2559 Status
= EFI_OUT_OF_RESOURCES
;
2564 // Insert the newly created route cache entry.
2566 Index
= IP6_ROUTE_CACHE_HASH (IcmpDest
, &Head
->DestinationAddress
);
2567 InsertHeadList (&IpSb
->RouteTable
->Cache
.CacheBucket
[Index
], &RouteCache
->Link
);
2571 // Try to locate the neighbor cache for the Target.
2573 NeighborCache
= Ip6FindNeighborEntry (IpSb
, Target
);
2575 if (LinkLayerOption
!= NULL
) {
2576 if (NeighborCache
== NULL
) {
2578 // Create a neighbor cache for the Target.
2580 ZeroMem (&Mac
, sizeof (EFI_MAC_ADDRESS
));
2581 CopyMem (&Mac
, LinkLayerOption
->EtherAddr
, 6);
2582 NeighborCache
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, Target
, &Mac
);
2583 if (NeighborCache
== NULL
) {
2585 // Just report a success here. The neighbor cache can be created in
2586 // some other place.
2588 Status
= EFI_SUCCESS
;
2592 NeighborCache
->State
= EfiNeighborStale
;
2593 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2595 Result
= CompareMem (LinkLayerOption
->EtherAddr
, &NeighborCache
->LinkAddress
, 6);
2598 // If the link-local address is the same as that already in the cache,
2599 // the cache entry's state remains unchanged. Otherwise update the
2600 // reachability state to STALE.
2602 if ((NeighborCache
->State
== EfiNeighborInComplete
) || (Result
!= 0)) {
2603 CopyMem (&NeighborCache
->LinkAddress
, LinkLayerOption
->EtherAddr
, 6);
2605 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2607 if (NeighborCache
->State
== EfiNeighborInComplete
) {
2609 // Send queued packets if exist.
2611 NeighborCache
->State
= EfiNeighborStale
;
2612 NeighborCache
->CallBack ((VOID
*) NeighborCache
);
2614 NeighborCache
->State
= EfiNeighborStale
;
2620 if (NeighborCache
!= NULL
&& IsRouter
) {
2622 // The Target is a router, set IsRouter to TRUE.
2624 NeighborCache
->IsRouter
= TRUE
;
2627 Status
= EFI_SUCCESS
;
2630 NetbufFree (Packet
);
2635 Add Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2637 @param[in] IpSb The IP6 service binding instance.
2638 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2639 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2640 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2641 cache. It will be deleted after Timeout. A value of zero means that
2642 the entry is permanent. A non-zero value means that the entry is
2644 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2645 be overridden and updated; if FALSE, and if a
2646 corresponding cache entry already existed, EFI_ACCESS_DENIED
2649 @retval EFI_SUCCESS The neighbor cache entry has been added.
2650 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache
2651 due to insufficient resources.
2652 @retval EFI_NOT_FOUND TargetLinkAddress is NULL.
2653 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
2654 and that entry is tagged as un-overridden (when DeleteFlag
2660 IN IP6_SERVICE
*IpSb
,
2661 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
2662 IN EFI_MAC_ADDRESS
*TargetLinkAddress OPTIONAL
,
2667 IP6_NEIGHBOR_ENTRY
*Neighbor
;
2669 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
2670 if (Neighbor
!= NULL
) {
2672 return EFI_ACCESS_DENIED
;
2674 if (TargetLinkAddress
!= NULL
) {
2675 IP6_COPY_LINK_ADDRESS (&Neighbor
->LinkAddress
, TargetLinkAddress
);
2679 if (TargetLinkAddress
== NULL
) {
2680 return EFI_NOT_FOUND
;
2683 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, TargetIp6Address
, TargetLinkAddress
);
2684 if (Neighbor
== NULL
) {
2685 return EFI_OUT_OF_RESOURCES
;
2689 Neighbor
->State
= EfiNeighborReachable
;
2692 Neighbor
->Ticks
= IP6_GET_TICKS (Timeout
/ TICKS_PER_MS
);
2693 Neighbor
->Dynamic
= TRUE
;
2695 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2702 Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2704 @param[in] IpSb The IP6 service binding instance.
2705 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2706 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2707 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2708 cache. It will be deleted after Timeout. A value of zero means that
2709 the entry is permanent. A non-zero value means that the entry is
2711 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2712 be overridden and updated; if FALSE, and if a
2713 corresponding cache entry already existed, EFI_ACCESS_DENIED
2716 @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted.
2717 @retval EFI_NOT_FOUND This entry is not in the neighbor cache.
2722 IN IP6_SERVICE
*IpSb
,
2723 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
2724 IN EFI_MAC_ADDRESS
*TargetLinkAddress OPTIONAL
,
2729 IP6_NEIGHBOR_ENTRY
*Neighbor
;
2731 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
2732 if (Neighbor
== NULL
) {
2733 return EFI_NOT_FOUND
;
2736 RemoveEntryList (&Neighbor
->Link
);
2737 FreePool (Neighbor
);
2743 The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds.
2744 This time routine handles DAD module and neighbor state transition.
2745 It is also responsible for sending out router solicitations.
2747 @param[in] Event The IP6 service instance's heartbeat timer.
2748 @param[in] Context The IP6 service instance.
2753 Ip6NdFasterTimerTicking (
2761 IP6_INTERFACE
*IpIf
;
2762 IP6_DELAY_JOIN_LIST
*DelayNode
;
2763 EFI_IPv6_ADDRESS Source
;
2764 IP6_DAD_ENTRY
*DupAddrDetect
;
2766 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
2767 EFI_IPv6_ADDRESS Destination
;
2771 IpSb
= (IP6_SERVICE
*) Context
;
2772 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
2774 ZeroMem (&Source
, sizeof (EFI_IPv6_ADDRESS
));
2777 // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router
2778 // Solicitation messages, each separated by at least
2779 // RTR_SOLICITATION_INTERVAL (4) seconds.
2781 if ((IpSb
->Ip6ConfigInstance
.Policy
== Ip6ConfigPolicyAutomatic
) &&
2782 !IpSb
->RouterAdvertiseReceived
&&
2783 IpSb
->SolicitTimer
> 0
2785 if ((IpSb
->Ticks
== 0) || (--IpSb
->Ticks
== 0)) {
2786 Status
= Ip6SendRouterSolicit (IpSb
, NULL
, NULL
, NULL
, NULL
);
2787 if (!EFI_ERROR (Status
)) {
2788 IpSb
->SolicitTimer
--;
2789 IpSb
->Ticks
= (UINT32
) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL
);
2794 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
2795 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP6_INTERFACE
, Link
);
2798 // Process the delay list to join the solicited-node multicast address.
2800 NET_LIST_FOR_EACH_SAFE (Entry2
, Next
, &IpIf
->DelayJoinList
) {
2801 DelayNode
= NET_LIST_USER_STRUCT (Entry2
, IP6_DELAY_JOIN_LIST
, Link
);
2802 if ((DelayNode
->DelayTime
== 0) || (--DelayNode
->DelayTime
== 0)) {
2804 // The timer expires, init the duplicate address detection.
2807 DelayNode
->Interface
,
2808 DelayNode
->AddressInfo
,
2809 DelayNode
->DadCallback
,
2814 // Remove the delay node
2816 RemoveEntryList (&DelayNode
->Link
);
2817 FreePool (DelayNode
);
2822 // Process the duplicate address detection list.
2824 NET_LIST_FOR_EACH_SAFE (Entry2
, Next
, &IpIf
->DupAddrDetectList
) {
2825 DupAddrDetect
= NET_LIST_USER_STRUCT (Entry2
, IP6_DAD_ENTRY
, Link
);
2827 if ((DupAddrDetect
->RetransTick
== 0) || (--DupAddrDetect
->RetransTick
== 0)) {
2829 // The timer expires, check the remaining transmit counts.
2831 if (DupAddrDetect
->Transmit
< DupAddrDetect
->MaxTransmit
) {
2833 // Send the Neighbor Solicitation message with
2834 // Source - unspecified address, destination - solicited-node multicast address
2835 // Target - the address to be validated
2837 Status
= Ip6SendNeighborSolicit (
2840 &DupAddrDetect
->Destination
,
2841 &DupAddrDetect
->AddressInfo
->Address
,
2844 if (EFI_ERROR (Status
)) {
2848 DupAddrDetect
->Transmit
++;
2849 DupAddrDetect
->RetransTick
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2852 // All required solicitation has been sent out, and the RetransTime after the last
2853 // Neighbor Solicit is elapsed, finish the DAD process.
2856 if ((DupAddrDetect
->Receive
== 0) ||
2857 (DupAddrDetect
->Transmit
<= DupAddrDetect
->Receive
)) {
2861 Ip6OnDADFinished (Flag
, IpIf
, DupAddrDetect
);
2868 // Polling the state of Neighbor cache
2870 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->NeighborTable
) {
2871 NeighborCache
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, Link
);
2873 switch (NeighborCache
->State
) {
2874 case EfiNeighborInComplete
:
2875 if (NeighborCache
->Ticks
> 0) {
2876 --NeighborCache
->Ticks
;
2880 // Retransmit Neighbor Solicitation messages approximately every
2881 // RetransTimer milliseconds while awaiting a response.
2883 if (NeighborCache
->Ticks
== 0) {
2884 if (NeighborCache
->Transmit
> 1) {
2886 // Send out multicast neighbor solicitation for address resolution.
2887 // After last neighbor solicitation message has been sent out, wait
2888 // for RetransTimer and then remove entry if no response is received.
2890 Ip6CreateSNMulticastAddr (&NeighborCache
->Neighbor
, &Destination
);
2891 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
2892 if (EFI_ERROR (Status
)) {
2896 Status
= Ip6SendNeighborSolicit (
2900 &NeighborCache
->Neighbor
,
2901 &IpSb
->SnpMode
.CurrentAddress
2903 if (EFI_ERROR (Status
)) {
2909 // Update the retransmit times.
2911 if (NeighborCache
->Transmit
> 0) {
2912 --NeighborCache
->Transmit
;
2913 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2917 if (NeighborCache
->Transmit
== 0) {
2919 // Timeout, send ICMP destination unreachable packet and then remove entry
2921 Status
= Ip6FreeNeighborEntry (
2930 if (EFI_ERROR (Status
)) {
2937 case EfiNeighborReachable
:
2939 // This entry is inserted by EfiIp6Neighbors() as static entry
2940 // and will not timeout.
2942 if (!NeighborCache
->Dynamic
&& (NeighborCache
->Ticks
== IP6_INFINIT_LIFETIME
)) {
2946 if ((NeighborCache
->Ticks
== 0) || (--NeighborCache
->Ticks
== 0)) {
2947 if (NeighborCache
->Dynamic
) {
2949 // This entry is inserted by EfiIp6Neighbors() as dynamic entry
2950 // and will be deleted after timeout.
2952 Status
= Ip6FreeNeighborEntry (
2961 if (EFI_ERROR (Status
)) {
2965 NeighborCache
->State
= EfiNeighborStale
;
2966 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2972 case EfiNeighborDelay
:
2973 if ((NeighborCache
->Ticks
== 0) || (--NeighborCache
->Ticks
== 0)) {
2975 NeighborCache
->State
= EfiNeighborProbe
;
2976 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2977 NeighborCache
->Transmit
= IP6_MAX_UNICAST_SOLICIT
+ 1;
2979 // Send out unicast neighbor solicitation for Neighbor Unreachability Detection
2981 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
2982 if (EFI_ERROR (Status
)) {
2986 Status
= Ip6SendNeighborSolicit (
2989 &NeighborCache
->Neighbor
,
2990 &NeighborCache
->Neighbor
,
2991 &IpSb
->SnpMode
.CurrentAddress
2993 if (EFI_ERROR (Status
)) {
2997 NeighborCache
->Transmit
--;
3002 case EfiNeighborProbe
:
3003 if (NeighborCache
->Ticks
> 0) {
3004 --NeighborCache
->Ticks
;
3008 // Retransmit Neighbor Solicitation messages approximately every
3009 // RetransTimer milliseconds while awaiting a response.
3011 if (NeighborCache
->Ticks
== 0) {
3012 if (NeighborCache
->Transmit
> 1) {
3014 // Send out unicast neighbor solicitation for Neighbor Unreachability
3015 // Detection. After last neighbor solicitation message has been sent out,
3016 // wait for RetransTimer and then remove entry if no response is received.
3018 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
3019 if (EFI_ERROR (Status
)) {
3023 Status
= Ip6SendNeighborSolicit (
3026 &NeighborCache
->Neighbor
,
3027 &NeighborCache
->Neighbor
,
3028 &IpSb
->SnpMode
.CurrentAddress
3030 if (EFI_ERROR (Status
)) {
3036 // Update the retransmit times.
3038 if (NeighborCache
->Transmit
> 0) {
3039 --NeighborCache
->Transmit
;
3040 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
3044 if (NeighborCache
->Transmit
== 0) {
3046 // Delete the neighbor entry.
3048 Status
= Ip6FreeNeighborEntry (
3057 if (EFI_ERROR (Status
)) {
3071 The heartbeat timer of ND module in 1 second. This time routine handles following
3072 things: 1) maitain default router list; 2) maintain prefix options;
3073 3) maintain route caches.
3075 @param[in] IpSb The IP6 service binding instance.
3080 IN IP6_SERVICE
*IpSb
3085 IP6_DEFAULT_ROUTER
*DefaultRouter
;
3086 IP6_PREFIX_LIST_ENTRY
*PrefixOption
;
3088 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
3091 // Decrease the lifetime of default router, if expires remove it from default router list.
3093 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->DefaultRouterList
) {
3094 DefaultRouter
= NET_LIST_USER_STRUCT (Entry
, IP6_DEFAULT_ROUTER
, Link
);
3095 if (DefaultRouter
->Lifetime
!= IP6_INF_ROUTER_LIFETIME
) {
3096 if ((DefaultRouter
->Lifetime
== 0) || (--DefaultRouter
->Lifetime
== 0)) {
3097 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
3103 // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses.
3105 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->AutonomousPrefix
) {
3106 PrefixOption
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
3107 if (PrefixOption
->ValidLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) {
3108 if ((PrefixOption
->ValidLifetime
> 0) && (--PrefixOption
->ValidLifetime
> 0)) {
3109 if ((PrefixOption
->PreferredLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) &&
3110 (PrefixOption
->PreferredLifetime
> 0)
3112 --PrefixOption
->PreferredLifetime
;
3115 Ip6DestroyPrefixListEntry (IpSb
, PrefixOption
, FALSE
, TRUE
);
3120 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->OnlinkPrefix
) {
3121 PrefixOption
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
3122 if (PrefixOption
->ValidLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) {
3123 if ((PrefixOption
->ValidLifetime
== 0) || (--PrefixOption
->ValidLifetime
== 0)) {
3124 Ip6DestroyPrefixListEntry (IpSb
, PrefixOption
, TRUE
, TRUE
);
3130 // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries.
3131 // Remove the entries at the tail of the bucket. These entries
3132 // are likely to be used least.
3133 // Reclaim frequency is set to 1 second.
3135 for (Index
= 0; Index
< IP6_ROUTE_CACHE_HASH_SIZE
; Index
++) {
3136 while (IpSb
->RouteTable
->Cache
.CacheNum
[Index
] > IP6_ROUTE_CACHE_MAX
) {
3137 Entry
= NetListRemoveTail (&IpSb
->RouteTable
->Cache
.CacheBucket
[Index
]);
3138 if (Entry
== NULL
) {
3142 RouteCache
= NET_LIST_USER_STRUCT (Entry
, IP6_ROUTE_CACHE_ENTRY
, Link
);
3143 Ip6FreeRouteCacheEntry (RouteCache
);
3144 ASSERT (IpSb
->RouteTable
->Cache
.CacheNum
[Index
] > 0);
3145 IpSb
->RouteTable
->Cache
.CacheNum
[Index
]--;