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 unspecified, 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 advertisement 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 and
2098 // fit within the input packet.
2101 while (Offset
< (UINT32
) Head
->PayloadLength
) {
2102 NetbufCopy (Packet
, Offset
, sizeof (UINT8
), &Type
);
2104 case Ip6OptionEtherSource
:
2106 // Update the neighbor cache
2108 NetbufCopy (Packet
, Offset
, sizeof (IP6_ETHER_ADDR_OPTION
), (UINT8
*) &LinkLayerOption
);
2111 // Option size validity ensured by Ip6IsNDOptionValid().
2113 ASSERT (LinkLayerOption
.Length
!= 0);
2114 ASSERT (Offset
+ (UINT32
) LinkLayerOption
.Length
* 8 <= (UINT32
) Head
->PayloadLength
);
2116 ZeroMem (&LinkLayerAddress
, sizeof (EFI_MAC_ADDRESS
));
2117 CopyMem (&LinkLayerAddress
, LinkLayerOption
.EtherAddr
, 6);
2119 if (NeighborCache
== NULL
) {
2120 NeighborCache
= Ip6CreateNeighborEntry (
2123 &Head
->SourceAddress
,
2126 if (NeighborCache
== NULL
) {
2127 Status
= EFI_OUT_OF_RESOURCES
;
2130 NeighborCache
->IsRouter
= TRUE
;
2131 NeighborCache
->State
= EfiNeighborStale
;
2132 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2134 Result
= CompareMem (&LinkLayerAddress
, &NeighborCache
->LinkAddress
, 6);
2137 // If the link-local address is the same as that already in the cache,
2138 // the cache entry's state remains unchanged. Otherwise update the
2139 // reachability state to STALE.
2141 if ((NeighborCache
->State
== EfiNeighborInComplete
) || (Result
!= 0)) {
2142 CopyMem (&NeighborCache
->LinkAddress
, &LinkLayerAddress
, 6);
2144 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2146 if (NeighborCache
->State
== EfiNeighborInComplete
) {
2148 // Send queued packets if exist.
2150 NeighborCache
->State
= EfiNeighborStale
;
2151 NeighborCache
->CallBack ((VOID
*) NeighborCache
);
2153 NeighborCache
->State
= EfiNeighborStale
;
2158 Offset
+= (UINT32
) LinkLayerOption
.Length
* 8;
2160 case Ip6OptionPrefixInfo
:
2161 NetbufCopy (Packet
, Offset
, sizeof (IP6_PREFIX_INFO_OPTION
), (UINT8
*) &PrefixOption
);
2164 // Option size validity ensured by Ip6IsNDOptionValid().
2166 ASSERT (PrefixOption
.Length
== 4);
2167 ASSERT (Offset
+ (UINT32
) PrefixOption
.Length
* 8 <= (UINT32
) Head
->PayloadLength
);
2169 PrefixOption
.ValidLifetime
= NTOHL (PrefixOption
.ValidLifetime
);
2170 PrefixOption
.PreferredLifetime
= NTOHL (PrefixOption
.PreferredLifetime
);
2173 // Get L and A flag, recorded in the lower 2 bits of Reserved1
2176 if ((PrefixOption
.Reserved1
& IP6_ON_LINK_FLAG
) == IP6_ON_LINK_FLAG
) {
2180 if ((PrefixOption
.Reserved1
& IP6_AUTO_CONFIG_FLAG
) == IP6_AUTO_CONFIG_FLAG
) {
2185 // If the prefix is the link-local prefix, silently ignore the prefix option.
2187 if (PrefixOption
.PrefixLength
== IP6_LINK_LOCAL_PREFIX_LENGTH
&&
2188 NetIp6IsLinkLocalAddr (&PrefixOption
.Prefix
)
2190 Offset
+= sizeof (IP6_PREFIX_INFO_OPTION
);
2194 // Do following if on-link flag is set according to RFC4861.
2197 PrefixList
= Ip6FindPrefixListEntry (
2200 PrefixOption
.PrefixLength
,
2201 &PrefixOption
.Prefix
2204 // Create a new entry for the prefix, if the ValidLifetime is zero,
2205 // silently ignore the prefix option.
2207 if (PrefixList
== NULL
&& PrefixOption
.ValidLifetime
!= 0) {
2208 PrefixList
= Ip6CreatePrefixListEntry (
2211 PrefixOption
.ValidLifetime
,
2212 PrefixOption
.PreferredLifetime
,
2213 PrefixOption
.PrefixLength
,
2214 &PrefixOption
.Prefix
2216 if (PrefixList
== NULL
) {
2217 Status
= EFI_OUT_OF_RESOURCES
;
2220 } else if (PrefixList
!= NULL
) {
2221 if (PrefixOption
.ValidLifetime
!= 0) {
2222 PrefixList
->ValidLifetime
= PrefixOption
.ValidLifetime
;
2225 // If the prefix exists and incoming ValidLifetime is zero, immediately
2226 // remove the prefix.
2227 Ip6DestroyPrefixListEntry (IpSb
, PrefixList
, OnLink
, TRUE
);
2233 // Do following if Autonomous flag is set according to RFC4862.
2235 if (Autonomous
&& PrefixOption
.PreferredLifetime
<= PrefixOption
.ValidLifetime
) {
2236 PrefixList
= Ip6FindPrefixListEntry (
2239 PrefixOption
.PrefixLength
,
2240 &PrefixOption
.Prefix
2243 // Create a new entry for the prefix, and form an address by prefix + interface id
2244 // If the sum of the prefix length and interface identifier length
2245 // does not equal 128 bits, the Prefix Information option MUST be ignored.
2247 if (PrefixList
== NULL
&&
2248 PrefixOption
.ValidLifetime
!= 0 &&
2249 PrefixOption
.PrefixLength
+ IpSb
->InterfaceIdLen
* 8 == 128
2252 // Form the address in network order.
2254 CopyMem (&StatelessAddress
, &PrefixOption
.Prefix
, sizeof (UINT64
));
2255 CopyMem (&StatelessAddress
.Addr
[8], IpSb
->InterfaceId
, sizeof (UINT64
));
2258 // If the address is not yet in the assigned address list, adds it into.
2260 if (!Ip6IsOneOfSetAddress (IpSb
, &StatelessAddress
, NULL
, NULL
)) {
2262 // And also not in the DAD process, check its uniqueness firstly.
2264 if (Ip6FindDADEntry (IpSb
, &StatelessAddress
, NULL
) == NULL
) {
2265 Status
= Ip6SetAddress (
2266 IpSb
->DefaultInterface
,
2269 PrefixOption
.PrefixLength
,
2270 PrefixOption
.ValidLifetime
,
2271 PrefixOption
.PreferredLifetime
,
2275 if (EFI_ERROR (Status
)) {
2282 // Adds the prefix option to stateless prefix option list.
2284 PrefixList
= Ip6CreatePrefixListEntry (
2287 PrefixOption
.ValidLifetime
,
2288 PrefixOption
.PreferredLifetime
,
2289 PrefixOption
.PrefixLength
,
2290 &PrefixOption
.Prefix
2292 if (PrefixList
== NULL
) {
2293 Status
= EFI_OUT_OF_RESOURCES
;
2296 } else if (PrefixList
!= NULL
) {
2299 // Reset the preferred lifetime of the address if the advertised prefix exists.
2300 // Perform specific action to valid lifetime together.
2302 PrefixList
->PreferredLifetime
= PrefixOption
.PreferredLifetime
;
2303 if ((PrefixOption
.ValidLifetime
> 7200) ||
2304 (PrefixOption
.ValidLifetime
> PrefixList
->ValidLifetime
)) {
2306 // If the received Valid Lifetime is greater than 2 hours or
2307 // greater than RemainingLifetime, set the valid lifetime of the
2308 // corresponding address to the advertised Valid Lifetime.
2310 PrefixList
->ValidLifetime
= PrefixOption
.ValidLifetime
;
2312 } else if (PrefixList
->ValidLifetime
<= 7200) {
2314 // If RemainingLifetime is less than or equals to 2 hours, ignore the
2315 // Prefix Information option with regards to the valid lifetime.
2316 // TODO: If this option has been authenticated, set the valid lifetime.
2320 // Otherwise, reset the valid lifetime of the corresponding
2321 // address to 2 hours.
2323 PrefixList
->ValidLifetime
= 7200;
2328 Offset
+= sizeof (IP6_PREFIX_INFO_OPTION
);
2331 NetbufCopy (Packet
, Offset
, sizeof (IP6_MTU_OPTION
), (UINT8
*) &MTUOption
);
2334 // Option size validity ensured by Ip6IsNDOptionValid().
2336 ASSERT (MTUOption
.Length
== 1);
2337 ASSERT (Offset
+ (UINT32
) MTUOption
.Length
* 8 <= (UINT32
) Head
->PayloadLength
);
2340 // Use IPv6 minimum link MTU 1280 bytes as the maximum packet size in order
2341 // to omit implementation of Path MTU Discovery. Thus ignore the MTU option
2342 // in Router Advertisement.
2345 Offset
+= sizeof (IP6_MTU_OPTION
);
2349 // Silently ignore unrecognized options
2351 NetbufCopy (Packet
, Offset
+ sizeof (UINT8
), sizeof (UINT8
), &Length
);
2353 ASSERT (Length
!= 0);
2355 Offset
+= (UINT32
) Length
* 8;
2360 Status
= EFI_SUCCESS
;
2363 NetbufFree (Packet
);
2368 Process the ICMPv6 redirect message. Find the instance, then update
2371 @param[in] IpSb The IP6 service binding instance that received
2373 @param[in] Head The IP head of the received ICMPv6 packet.
2374 @param[in] Packet The content of the ICMPv6 redirect packet with
2375 the IP head removed.
2377 @retval EFI_INVALID_PARAMETER The parameter is invalid.
2378 @retval EFI_OUT_OF_RESOURCES Insufficient resources to complete the
2380 @retval EFI_SUCCESS Successfully updated the route caches.
2384 Ip6ProcessRedirect (
2385 IN IP6_SERVICE
*IpSb
,
2386 IN EFI_IP6_HEADER
*Head
,
2390 IP6_ICMP_INFORMATION_HEAD
*Icmp
;
2391 EFI_IPv6_ADDRESS
*Target
;
2392 EFI_IPv6_ADDRESS
*IcmpDest
;
2395 IP6_ROUTE_ENTRY
*RouteEntry
;
2396 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
2397 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
2400 IP6_ETHER_ADDR_OPTION
*LinkLayerOption
;
2401 EFI_MAC_ADDRESS Mac
;
2407 Status
= EFI_INVALID_PARAMETER
;
2409 Icmp
= (IP6_ICMP_INFORMATION_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
2415 // Validate the incoming Redirect message
2419 // The IP Hop Limit field has a value of 255, i.e. the packet
2420 // could not possibly have been forwarded by a router.
2422 // ICMP length (derived from the IP length) is 40 or more octets.
2424 if (Head
->HopLimit
!= IP6_HOP_LIMIT
|| Icmp
->Head
.Code
!= 0 ||
2425 Head
->PayloadLength
< IP6_REDITECT_LENGTH
) {
2430 // The IP source address must be a link-local address
2432 if (!NetIp6IsLinkLocalAddr (&Head
->SourceAddress
)) {
2437 // The dest of this ICMP redirect message is not us.
2439 if (!Ip6IsOneOfSetAddress (IpSb
, &Head
->DestinationAddress
, NULL
, NULL
)) {
2444 // All included options have a length that is greater than zero.
2446 OptionLen
= (UINT16
) (Head
->PayloadLength
- IP6_REDITECT_LENGTH
);
2447 if (OptionLen
!= 0) {
2448 Option
= NetbufGetByte (Packet
, IP6_REDITECT_LENGTH
, NULL
);
2449 ASSERT (Option
!= NULL
);
2451 if (!Ip6IsNDOptionValid (Option
, OptionLen
)) {
2456 Target
= (EFI_IPv6_ADDRESS
*) (Icmp
+ 1);
2457 IcmpDest
= Target
+ 1;
2460 // The ICMP Destination Address field in the redirect message does not contain
2461 // a multicast address.
2463 if (IP6_IS_MULTICAST (IcmpDest
)) {
2468 // The ICMP Target Address is either a link-local address (when redirected to
2469 // a router) or the same as the ICMP Destination Address (when redirected to
2470 // the on-link destination).
2472 IsRouter
= (BOOLEAN
) !EFI_IP6_EQUAL (Target
, IcmpDest
);
2473 if (!NetIp6IsLinkLocalAddr (Target
) && IsRouter
) {
2478 // Check the options. The only interested option here is the target-link layer
2481 Length
= Packet
->TotalSize
- 40;
2482 Option
= (UINT8
*) (IcmpDest
+ 1);
2483 LinkLayerOption
= NULL
;
2484 while (Length
> 0) {
2486 case Ip6OptionEtherTarget
:
2488 LinkLayerOption
= (IP6_ETHER_ADDR_OPTION
*) Option
;
2489 OptLen
= LinkLayerOption
->Length
;
2492 // For ethernet, the length must be 1.
2500 OptLen
= *(Option
+ 1);
2503 // A length of 0 is invalid.
2510 Length
-= 8 * OptLen
;
2511 Option
+= 8 * OptLen
;
2519 // The IP source address of the Redirect is the same as the current
2520 // first-hop router for the specified ICMP Destination Address.
2522 RouteCache
= Ip6FindRouteCache (IpSb
->RouteTable
, IcmpDest
, &Head
->DestinationAddress
);
2523 if (RouteCache
!= NULL
) {
2524 if (!EFI_IP6_EQUAL (&RouteCache
->NextHop
, &Head
->SourceAddress
)) {
2526 // The source of this Redirect message must match the NextHop of the
2527 // corresponding route cache entry.
2533 // Update the NextHop.
2535 IP6_COPY_ADDRESS (&RouteCache
->NextHop
, Target
);
2538 RouteEntry
= (IP6_ROUTE_ENTRY
*) RouteCache
->Tag
;
2539 RouteEntry
->Flag
= RouteEntry
->Flag
| IP6_DIRECT_ROUTE
;
2544 // Get the Route Entry.
2546 RouteEntry
= Ip6FindRouteEntry (IpSb
->RouteTable
, IcmpDest
, NULL
);
2547 if (RouteEntry
== NULL
) {
2548 RouteEntry
= Ip6CreateRouteEntry (IcmpDest
, 0, NULL
);
2549 if (RouteEntry
== NULL
) {
2550 Status
= EFI_OUT_OF_RESOURCES
;
2556 RouteEntry
->Flag
= IP6_DIRECT_ROUTE
;
2560 // Create a route cache for this.
2562 RouteCache
= Ip6CreateRouteCacheEntry (
2564 &Head
->DestinationAddress
,
2568 if (RouteCache
== NULL
) {
2569 Status
= EFI_OUT_OF_RESOURCES
;
2574 // Insert the newly created route cache entry.
2576 Index
= IP6_ROUTE_CACHE_HASH (IcmpDest
, &Head
->DestinationAddress
);
2577 InsertHeadList (&IpSb
->RouteTable
->Cache
.CacheBucket
[Index
], &RouteCache
->Link
);
2581 // Try to locate the neighbor cache for the Target.
2583 NeighborCache
= Ip6FindNeighborEntry (IpSb
, Target
);
2585 if (LinkLayerOption
!= NULL
) {
2586 if (NeighborCache
== NULL
) {
2588 // Create a neighbor cache for the Target.
2590 ZeroMem (&Mac
, sizeof (EFI_MAC_ADDRESS
));
2591 CopyMem (&Mac
, LinkLayerOption
->EtherAddr
, 6);
2592 NeighborCache
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, Target
, &Mac
);
2593 if (NeighborCache
== NULL
) {
2595 // Just report a success here. The neighbor cache can be created in
2596 // some other place.
2598 Status
= EFI_SUCCESS
;
2602 NeighborCache
->State
= EfiNeighborStale
;
2603 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2605 Result
= CompareMem (LinkLayerOption
->EtherAddr
, &NeighborCache
->LinkAddress
, 6);
2608 // If the link-local address is the same as that already in the cache,
2609 // the cache entry's state remains unchanged. Otherwise update the
2610 // reachability state to STALE.
2612 if ((NeighborCache
->State
== EfiNeighborInComplete
) || (Result
!= 0)) {
2613 CopyMem (&NeighborCache
->LinkAddress
, LinkLayerOption
->EtherAddr
, 6);
2615 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2617 if (NeighborCache
->State
== EfiNeighborInComplete
) {
2619 // Send queued packets if exist.
2621 NeighborCache
->State
= EfiNeighborStale
;
2622 NeighborCache
->CallBack ((VOID
*) NeighborCache
);
2624 NeighborCache
->State
= EfiNeighborStale
;
2630 if (NeighborCache
!= NULL
&& IsRouter
) {
2632 // The Target is a router, set IsRouter to TRUE.
2634 NeighborCache
->IsRouter
= TRUE
;
2637 Status
= EFI_SUCCESS
;
2640 NetbufFree (Packet
);
2645 Add Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2647 @param[in] IpSb The IP6 service binding instance.
2648 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2649 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2650 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2651 cache. It will be deleted after Timeout. A value of zero means that
2652 the entry is permanent. A non-zero value means that the entry is
2654 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2655 be overridden and updated; if FALSE, and if a
2656 corresponding cache entry already existed, EFI_ACCESS_DENIED
2659 @retval EFI_SUCCESS The neighbor cache entry has been added.
2660 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache
2661 due to insufficient resources.
2662 @retval EFI_NOT_FOUND TargetLinkAddress is NULL.
2663 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
2664 and that entry is tagged as un-overridden (when DeleteFlag
2670 IN IP6_SERVICE
*IpSb
,
2671 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
2672 IN EFI_MAC_ADDRESS
*TargetLinkAddress OPTIONAL
,
2677 IP6_NEIGHBOR_ENTRY
*Neighbor
;
2679 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
2680 if (Neighbor
!= NULL
) {
2682 return EFI_ACCESS_DENIED
;
2684 if (TargetLinkAddress
!= NULL
) {
2685 IP6_COPY_LINK_ADDRESS (&Neighbor
->LinkAddress
, TargetLinkAddress
);
2689 if (TargetLinkAddress
== NULL
) {
2690 return EFI_NOT_FOUND
;
2693 Neighbor
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, TargetIp6Address
, TargetLinkAddress
);
2694 if (Neighbor
== NULL
) {
2695 return EFI_OUT_OF_RESOURCES
;
2699 Neighbor
->State
= EfiNeighborReachable
;
2702 Neighbor
->Ticks
= IP6_GET_TICKS (Timeout
/ TICKS_PER_MS
);
2703 Neighbor
->Dynamic
= TRUE
;
2705 Neighbor
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2712 Delete or update Neighbor cache entries. It is a work function for EfiIp6Neighbors().
2714 @param[in] IpSb The IP6 service binding instance.
2715 @param[in] TargetIp6Address Pointer to Target IPv6 address.
2716 @param[in] TargetLinkAddress Pointer to link-layer address of the target. Ignored if NULL.
2717 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
2718 cache. It will be deleted after Timeout. A value of zero means that
2719 the entry is permanent. A non-zero value means that the entry is
2721 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
2722 be overridden and updated; if FALSE, and if a
2723 corresponding cache entry already existed, EFI_ACCESS_DENIED
2726 @retval EFI_SUCCESS The neighbor cache entry has been updated or deleted.
2727 @retval EFI_NOT_FOUND This entry is not in the neighbor cache.
2732 IN IP6_SERVICE
*IpSb
,
2733 IN EFI_IPv6_ADDRESS
*TargetIp6Address
,
2734 IN EFI_MAC_ADDRESS
*TargetLinkAddress OPTIONAL
,
2739 IP6_NEIGHBOR_ENTRY
*Neighbor
;
2741 Neighbor
= Ip6FindNeighborEntry (IpSb
, TargetIp6Address
);
2742 if (Neighbor
== NULL
) {
2743 return EFI_NOT_FOUND
;
2746 RemoveEntryList (&Neighbor
->Link
);
2747 FreePool (Neighbor
);
2753 The heartbeat timer of ND module in IP6_TIMER_INTERVAL_IN_MS milliseconds.
2754 This time routine handles DAD module and neighbor state transition.
2755 It is also responsible for sending out router solicitations.
2757 @param[in] Event The IP6 service instance's heartbeat timer.
2758 @param[in] Context The IP6 service instance.
2763 Ip6NdFasterTimerTicking (
2771 IP6_INTERFACE
*IpIf
;
2772 IP6_DELAY_JOIN_LIST
*DelayNode
;
2773 EFI_IPv6_ADDRESS Source
;
2774 IP6_DAD_ENTRY
*DupAddrDetect
;
2776 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
2777 EFI_IPv6_ADDRESS Destination
;
2781 IpSb
= (IP6_SERVICE
*) Context
;
2782 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
2784 ZeroMem (&Source
, sizeof (EFI_IPv6_ADDRESS
));
2787 // A host SHOULD transmit up to MAX_RTR_SOLICITATIONS (3) Router
2788 // Solicitation messages, each separated by at least
2789 // RTR_SOLICITATION_INTERVAL (4) seconds.
2791 if ((IpSb
->Ip6ConfigInstance
.Policy
== Ip6ConfigPolicyAutomatic
) &&
2792 !IpSb
->RouterAdvertiseReceived
&&
2793 IpSb
->SolicitTimer
> 0
2795 if ((IpSb
->Ticks
== 0) || (--IpSb
->Ticks
== 0)) {
2796 Status
= Ip6SendRouterSolicit (IpSb
, NULL
, NULL
, NULL
, NULL
);
2797 if (!EFI_ERROR (Status
)) {
2798 IpSb
->SolicitTimer
--;
2799 IpSb
->Ticks
= (UINT32
) IP6_GET_TICKS (IP6_RTR_SOLICITATION_INTERVAL
);
2804 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
2805 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP6_INTERFACE
, Link
);
2808 // Process the delay list to join the solicited-node multicast address.
2810 NET_LIST_FOR_EACH_SAFE (Entry2
, Next
, &IpIf
->DelayJoinList
) {
2811 DelayNode
= NET_LIST_USER_STRUCT (Entry2
, IP6_DELAY_JOIN_LIST
, Link
);
2812 if ((DelayNode
->DelayTime
== 0) || (--DelayNode
->DelayTime
== 0)) {
2814 // The timer expires, init the duplicate address detection.
2817 DelayNode
->Interface
,
2818 DelayNode
->AddressInfo
,
2819 DelayNode
->DadCallback
,
2824 // Remove the delay node
2826 RemoveEntryList (&DelayNode
->Link
);
2827 FreePool (DelayNode
);
2832 // Process the duplicate address detection list.
2834 NET_LIST_FOR_EACH_SAFE (Entry2
, Next
, &IpIf
->DupAddrDetectList
) {
2835 DupAddrDetect
= NET_LIST_USER_STRUCT (Entry2
, IP6_DAD_ENTRY
, Link
);
2837 if ((DupAddrDetect
->RetransTick
== 0) || (--DupAddrDetect
->RetransTick
== 0)) {
2839 // The timer expires, check the remaining transmit counts.
2841 if (DupAddrDetect
->Transmit
< DupAddrDetect
->MaxTransmit
) {
2843 // Send the Neighbor Solicitation message with
2844 // Source - unspecified address, destination - solicited-node multicast address
2845 // Target - the address to be validated
2847 Status
= Ip6SendNeighborSolicit (
2850 &DupAddrDetect
->Destination
,
2851 &DupAddrDetect
->AddressInfo
->Address
,
2854 if (EFI_ERROR (Status
)) {
2858 DupAddrDetect
->Transmit
++;
2859 DupAddrDetect
->RetransTick
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2862 // All required solicitation has been sent out, and the RetransTime after the last
2863 // Neighbor Solicit is elapsed, finish the DAD process.
2866 if ((DupAddrDetect
->Receive
== 0) ||
2867 (DupAddrDetect
->Transmit
<= DupAddrDetect
->Receive
)) {
2871 Ip6OnDADFinished (Flag
, IpIf
, DupAddrDetect
);
2878 // Polling the state of Neighbor cache
2880 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->NeighborTable
) {
2881 NeighborCache
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, Link
);
2883 switch (NeighborCache
->State
) {
2884 case EfiNeighborInComplete
:
2885 if (NeighborCache
->Ticks
> 0) {
2886 --NeighborCache
->Ticks
;
2890 // Retransmit Neighbor Solicitation messages approximately every
2891 // RetransTimer milliseconds while awaiting a response.
2893 if (NeighborCache
->Ticks
== 0) {
2894 if (NeighborCache
->Transmit
> 1) {
2896 // Send out multicast neighbor solicitation for address resolution.
2897 // After last neighbor solicitation message has been sent out, wait
2898 // for RetransTimer and then remove entry if no response is received.
2900 Ip6CreateSNMulticastAddr (&NeighborCache
->Neighbor
, &Destination
);
2901 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
2902 if (EFI_ERROR (Status
)) {
2906 Status
= Ip6SendNeighborSolicit (
2910 &NeighborCache
->Neighbor
,
2911 &IpSb
->SnpMode
.CurrentAddress
2913 if (EFI_ERROR (Status
)) {
2919 // Update the retransmit times.
2921 if (NeighborCache
->Transmit
> 0) {
2922 --NeighborCache
->Transmit
;
2923 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2927 if (NeighborCache
->Transmit
== 0) {
2929 // Timeout, send ICMP destination unreachable packet and then remove entry
2931 Status
= Ip6FreeNeighborEntry (
2940 if (EFI_ERROR (Status
)) {
2947 case EfiNeighborReachable
:
2949 // This entry is inserted by EfiIp6Neighbors() as static entry
2950 // and will not timeout.
2952 if (!NeighborCache
->Dynamic
&& (NeighborCache
->Ticks
== IP6_INFINIT_LIFETIME
)) {
2956 if ((NeighborCache
->Ticks
== 0) || (--NeighborCache
->Ticks
== 0)) {
2957 if (NeighborCache
->Dynamic
) {
2959 // This entry is inserted by EfiIp6Neighbors() as dynamic entry
2960 // and will be deleted after timeout.
2962 Status
= Ip6FreeNeighborEntry (
2971 if (EFI_ERROR (Status
)) {
2975 NeighborCache
->State
= EfiNeighborStale
;
2976 NeighborCache
->Ticks
= (UINT32
) IP6_INFINIT_LIFETIME
;
2982 case EfiNeighborDelay
:
2983 if ((NeighborCache
->Ticks
== 0) || (--NeighborCache
->Ticks
== 0)) {
2985 NeighborCache
->State
= EfiNeighborProbe
;
2986 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
2987 NeighborCache
->Transmit
= IP6_MAX_UNICAST_SOLICIT
+ 1;
2989 // Send out unicast neighbor solicitation for Neighbor Unreachability Detection
2991 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
2992 if (EFI_ERROR (Status
)) {
2996 Status
= Ip6SendNeighborSolicit (
2999 &NeighborCache
->Neighbor
,
3000 &NeighborCache
->Neighbor
,
3001 &IpSb
->SnpMode
.CurrentAddress
3003 if (EFI_ERROR (Status
)) {
3007 NeighborCache
->Transmit
--;
3012 case EfiNeighborProbe
:
3013 if (NeighborCache
->Ticks
> 0) {
3014 --NeighborCache
->Ticks
;
3018 // Retransmit Neighbor Solicitation messages approximately every
3019 // RetransTimer milliseconds while awaiting a response.
3021 if (NeighborCache
->Ticks
== 0) {
3022 if (NeighborCache
->Transmit
> 1) {
3024 // Send out unicast neighbor solicitation for Neighbor Unreachability
3025 // Detection. After last neighbor solicitation message has been sent out,
3026 // wait for RetransTimer and then remove entry if no response is received.
3028 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
3029 if (EFI_ERROR (Status
)) {
3033 Status
= Ip6SendNeighborSolicit (
3036 &NeighborCache
->Neighbor
,
3037 &NeighborCache
->Neighbor
,
3038 &IpSb
->SnpMode
.CurrentAddress
3040 if (EFI_ERROR (Status
)) {
3046 // Update the retransmit times.
3048 if (NeighborCache
->Transmit
> 0) {
3049 --NeighborCache
->Transmit
;
3050 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
);
3054 if (NeighborCache
->Transmit
== 0) {
3056 // Delete the neighbor entry.
3058 Status
= Ip6FreeNeighborEntry (
3067 if (EFI_ERROR (Status
)) {
3081 The heartbeat timer of ND module in 1 second. This time routine handles following
3082 things: 1) maintain default router list; 2) maintain prefix options;
3083 3) maintain route caches.
3085 @param[in] IpSb The IP6 service binding instance.
3090 IN IP6_SERVICE
*IpSb
3095 IP6_DEFAULT_ROUTER
*DefaultRouter
;
3096 IP6_PREFIX_LIST_ENTRY
*PrefixOption
;
3098 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
3101 // Decrease the lifetime of default router, if expires remove it from default router list.
3103 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->DefaultRouterList
) {
3104 DefaultRouter
= NET_LIST_USER_STRUCT (Entry
, IP6_DEFAULT_ROUTER
, Link
);
3105 if (DefaultRouter
->Lifetime
!= IP6_INF_ROUTER_LIFETIME
) {
3106 if ((DefaultRouter
->Lifetime
== 0) || (--DefaultRouter
->Lifetime
== 0)) {
3107 Ip6DestroyDefaultRouter (IpSb
, DefaultRouter
);
3113 // Decrease Valid lifetime and Preferred lifetime of Prefix options and corresponding addresses.
3115 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->AutonomousPrefix
) {
3116 PrefixOption
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
3117 if (PrefixOption
->ValidLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) {
3118 if ((PrefixOption
->ValidLifetime
> 0) && (--PrefixOption
->ValidLifetime
> 0)) {
3119 if ((PrefixOption
->PreferredLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) &&
3120 (PrefixOption
->PreferredLifetime
> 0)
3122 --PrefixOption
->PreferredLifetime
;
3125 Ip6DestroyPrefixListEntry (IpSb
, PrefixOption
, FALSE
, TRUE
);
3130 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->OnlinkPrefix
) {
3131 PrefixOption
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
3132 if (PrefixOption
->ValidLifetime
!= (UINT32
) IP6_INFINIT_LIFETIME
) {
3133 if ((PrefixOption
->ValidLifetime
== 0) || (--PrefixOption
->ValidLifetime
== 0)) {
3134 Ip6DestroyPrefixListEntry (IpSb
, PrefixOption
, TRUE
, TRUE
);
3140 // Each bucket of route cache can contain at most IP6_ROUTE_CACHE_MAX entries.
3141 // Remove the entries at the tail of the bucket. These entries
3142 // are likely to be used least.
3143 // Reclaim frequency is set to 1 second.
3145 for (Index
= 0; Index
< IP6_ROUTE_CACHE_HASH_SIZE
; Index
++) {
3146 while (IpSb
->RouteTable
->Cache
.CacheNum
[Index
] > IP6_ROUTE_CACHE_MAX
) {
3147 Entry
= NetListRemoveTail (&IpSb
->RouteTable
->Cache
.CacheBucket
[Index
]);
3148 if (Entry
== NULL
) {
3152 RouteCache
= NET_LIST_USER_STRUCT (Entry
, IP6_ROUTE_CACHE_ENTRY
, Link
);
3153 Ip6FreeRouteCacheEntry (RouteCache
);
3154 ASSERT (IpSb
->RouteTable
->Cache
.CacheNum
[Index
] > 0);
3155 IpSb
->RouteTable
->Cache
.CacheNum
[Index
]--;