[mle] centralize and unify address registration logic (#12864)

This commit introduces `Mle::ShouldRegisterUnicastAddrWithParent()` to
centralize the logic for determining which unicast addresses should be
registered with the parent.

Previously, the filtering logic for unicast addresses was duplicated
in `HasUnregisteredAddress()` and `AppendAddressRegistrationTlv()`. By
unifying this in a single helper method, the code ensures consistent
behavior between checking for unregistered addresses and actually
appending them to the MLE messages.

Additionally, this change:
- Marks `Mle::HasUnregisteredAddress()` as `const`.
- Updates `Mle::ShouldRegisterMulticastAddrsWithParent()` to improve
  readability and follow common coding patterns in the codebase.
This commit is contained in:
Abtin Keshavarzian
2026-04-09 15:54:54 -07:00
committed by GitHub
parent f2c081b5ae
commit 7210dd212b
2 changed files with 51 additions and 18 deletions
+49 -17
View File
@@ -923,32 +923,34 @@ const LeaderData &Mle::GetLeaderData(void)
return mLeaderData;
}
bool Mle::HasUnregisteredAddress(void)
bool Mle::HasUnregisteredAddress(void) const
{
bool retval = false;
bool has = false;
// Checks whether there are any addresses in addition to the mesh-local
// address that need to be registered.
// address that need to be registered with the parent
for (const Ip6::Netif::UnicastAddress &addr : Get<ThreadNetif>().GetUnicastAddresses())
{
if (!addr.GetAddress().IsLinkLocalUnicast() && !IsRoutingLocator(addr.GetAddress()) &&
!IsAnycastLocator(addr.GetAddress()) && addr.GetAddress() != GetMeshLocalEid())
if (addr.GetAddress() == GetMeshLocalEid())
{
ExitNow(retval = true);
continue;
}
if (ShouldRegisterUnicastAddrWithParent(addr))
{
ExitNow(has = true);
}
}
if (!IsRxOnWhenIdle())
if (ShouldRegisterMulticastAddrsWithParent())
{
// For sleepy end-device, we register any external multicast
// addresses.
retval = Get<ThreadNetif>().HasAnyExternalMulticastAddress();
// We register any external multicast addresses.
has = Get<ThreadNetif>().HasAnyExternalMulticastAddress();
}
exit:
return retval;
return has;
}
#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
@@ -1077,6 +1079,24 @@ exit:
return;
}
bool Mle::ShouldRegisterUnicastAddrWithParent(const Ip6::Netif::UnicastAddress &aUnicastAddress) const
{
bool shouldRegister = false;
const Ip6::Address &address = aUnicastAddress.GetAddress();
VerifyOrExit(!IsFullThreadDevice());
VerifyOrExit(!address.IsLoopback());
VerifyOrExit(!address.IsLinkLocalUnicast());
VerifyOrExit(!IsRoutingLocator(address));
VerifyOrExit(!IsAnycastLocator(address));
shouldRegister = true;
exit:
return shouldRegister;
}
bool Mle::ShouldRegisterMulticastAddrsWithParent(void) const
{
// When multicast subscription changes, SED always notifies
@@ -1085,12 +1105,21 @@ bool Mle::ShouldRegisterMulticastAddrsWithParent(void) const
// parent of 1.2 or higher version as it could depend on its
// parent to perform Multicast Listener Report.
bool shouldRegister = !IsRxOnWhenIdle();
bool shouldRegister = false;
if (!IsRxOnWhenIdle())
{
ExitNow(shouldRegister = true);
}
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
shouldRegister |= !IsFullThreadDevice() && GetParent().IsThreadVersion1p2OrHigher();
if (!IsFullThreadDevice() && GetParent().IsThreadVersion1p2OrHigher())
{
ExitNow(shouldRegister = true);
}
#endif
exit:
return shouldRegister;
}
@@ -3638,9 +3667,12 @@ Error Mle::TxMessage::AppendAddressRegistrationTlv(AddressRegistrationMode aMode
for (const Ip6::Netif::UnicastAddress &addr : Get<ThreadNetif>().GetUnicastAddresses())
{
if (addr.GetAddress().IsLoopback() || addr.GetAddress().IsLinkLocalUnicast() ||
Get<Mle>().IsRoutingLocator(addr.GetAddress()) || Get<Mle>().IsAnycastLocator(addr.GetAddress()) ||
addr.GetAddress() == Get<Mle>().GetMeshLocalEid())
if (!Get<Mle>().ShouldRegisterUnicastAddrWithParent(addr))
{
continue;
}
if (addr.GetAddress() == Get<Mle>().GetMeshLocalEid())
{
continue;
}
+2 -1
View File
@@ -2323,11 +2323,12 @@ private:
void HandleChildUpdateResponseOnChild(RxInfo &aRxInfo);
void HandleDataResponse(RxInfo &aRxInfo);
Error HandleLeaderData(RxInfo &aRxInfo);
bool HasUnregisteredAddress(void);
uint32_t GetAttachStartDelay(void) const;
void SendAnnounce(uint8_t aChannel, AnnounceMode aMode);
void SendAnnounce(uint8_t aChannel, const Ip6::Address &aDestination, AnnounceMode aMode = kNormalAnnounce);
bool IsNetworkDataNewer(const LeaderData &aLeaderData);
bool HasUnregisteredAddress(void) const;
bool ShouldRegisterUnicastAddrWithParent(const Ip6::Netif::UnicastAddress &aUnicastAddress) const;
bool ShouldRegisterMulticastAddrsWithParent(void) const;
Error ProcessMessageSecurity(Crypto::AesCcm::Mode aMode,
Message &aMessage,