From 7210dd212bce679fa41ea4e0f442d54a77e50ceb Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 9 Apr 2026 15:54:54 -0700 Subject: [PATCH] [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. --- src/core/thread/mle.cpp | 66 ++++++++++++++++++++++++++++++----------- src/core/thread/mle.hpp | 3 +- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 77e84c7db..8ad0d009f 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -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().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().HasAnyExternalMulticastAddress(); + // We register any external multicast addresses. + has = Get().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().GetUnicastAddresses()) { - if (addr.GetAddress().IsLoopback() || addr.GetAddress().IsLinkLocalUnicast() || - Get().IsRoutingLocator(addr.GetAddress()) || Get().IsAnycastLocator(addr.GetAddress()) || - addr.GetAddress() == Get().GetMeshLocalEid()) + if (!Get().ShouldRegisterUnicastAddrWithParent(addr)) + { + continue; + } + + if (addr.GetAddress() == Get().GetMeshLocalEid()) { continue; } diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 3ad1edd20..5b4fd44f0 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -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,