diff --git a/src/core/backbone_router/leader.cpp b/src/core/backbone_router/leader.cpp index 4311132fa..e7181dae7 100644 --- a/src/core/backbone_router/leader.cpp +++ b/src/core/backbone_router/leader.cpp @@ -280,7 +280,7 @@ void Leader::UpdateDomainPrefixConfig(void) LogDomainPrefix(state, mDomainPrefix); #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - Get().UpdateAllDomainBackboneRouters(state); + Get().UpdateAllDomainBackboneRouters(state); #endif } diff --git a/src/core/backbone_router/local.cpp b/src/core/backbone_router/local.cpp index 6c4844274..a5402d152 100644 --- a/src/core/backbone_router/local.cpp +++ b/src/core/backbone_router/local.cpp @@ -57,6 +57,32 @@ Local::Local(Instance &aInstance) , mIsServiceAdded(false) { mDomainPrefixConfig.mPrefix.mLength = 0; + + // Primary Backbone Router Aloc + mBackboneRouterPrimaryAloc.Clear(); + + mBackboneRouterPrimaryAloc.mPrefixLength = Mle::MeshLocalPrefix::kLength; + mBackboneRouterPrimaryAloc.mPreferred = true; + mBackboneRouterPrimaryAloc.mValid = true; + mBackboneRouterPrimaryAloc.mScopeOverride = Ip6::Address::kRealmLocalScope; + mBackboneRouterPrimaryAloc.mScopeOverrideValid = true; + mBackboneRouterPrimaryAloc.GetAddress().SetLocator(Mle::kAloc16BackboneRouterPrimary); + + // All Network Backbone Routers Multicast Address. + mAllNetworkBackboneRouters.Clear(); + + mAllNetworkBackboneRouters.GetAddress().mFields.m8[0] = 0xff; // Multicast + mAllNetworkBackboneRouters.GetAddress().mFields.m8[1] = 0x32; // Flags = 3, Scope = 2 + mAllNetworkBackboneRouters.GetAddress().mFields.m8[2] = 0; // Reserved + mAllNetworkBackboneRouters.GetAddress().mFields.m8[15] = 3; // Group ID = 3 + + // All Domain Backbone Routers Multicast Address. + mAllDomainBackboneRouters.Clear(); + + mAllDomainBackboneRouters.GetAddress().mFields.m8[0] = 0xff; // Multicast + mAllDomainBackboneRouters.GetAddress().mFields.m8[1] = 0x32; // Flags = 3, Scope = 2 + mAllDomainBackboneRouters.GetAddress().mFields.m8[2] = 0; // Reserved + mAllDomainBackboneRouters.GetAddress().mFields.m8[15] = 3; // Group ID = 3 } void Local::SetEnabled(bool aEnable) @@ -194,7 +220,30 @@ void Local::SetState(BackboneRouterState aState) { VerifyOrExit(mState != aState, OT_NOOP); + if (mState == OT_BACKBONE_ROUTER_STATE_DISABLED) + { + // Subscribe All Network Backbone Routers Multicast Address for both Secondary and Primary state. + mAllNetworkBackboneRouters.GetAddress().SetMulticastNetworkPrefix(Get().GetMeshLocalPrefix()); + Get().SubscribeMulticast(mAllNetworkBackboneRouters); + } + else if (aState == OT_BACKBONE_ROUTER_STATE_DISABLED) + { + Get().UnsubscribeMulticast(mAllNetworkBackboneRouters); + } + + if (mState == OT_BACKBONE_ROUTER_STATE_PRIMARY) + { + Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); + } + else if (aState == OT_BACKBONE_ROUTER_STATE_PRIMARY) + { + // Add Primary Backbone Router Aloc for Primary Backbone Router. + mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(Get().GetMeshLocalPrefix()); + Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); + } + mState = aState; + Get().Signal(OT_CHANGED_THREAD_BACKBONE_ROUTER_STATE); exit: @@ -298,6 +347,48 @@ void Local::SetDomainPrefix(const NetworkData::OnMeshPrefixConfig &aConfig) } } +void Local::ApplyMeshLocalPrefix(void) +{ + VerifyOrExit(IsEnabled(), OT_NOOP); + + Get().UnsubscribeMulticast(mAllNetworkBackboneRouters); + mAllNetworkBackboneRouters.GetAddress().SetMulticastNetworkPrefix(Get().GetMeshLocalPrefix()); + Get().SubscribeMulticast(mAllNetworkBackboneRouters); + + if (IsPrimary()) + { + Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); + mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(Get().GetMeshLocalPrefix()); + Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); + } + +exit: + return; +} + +void Local::UpdateAllDomainBackboneRouters(Leader::DomainPrefixState aState) +{ + if (!IsEnabled()) + { + Get().UnsubscribeMulticast(mAllDomainBackboneRouters); + ExitNow(); + } + + if (aState == Leader::kDomainPrefixRemoved || aState == Leader::kDomainPrefixRefreshed) + { + Get().UnsubscribeMulticast(mAllDomainBackboneRouters); + } + + if (aState == Leader::kDomainPrefixAdded || aState == Leader::kDomainPrefixRefreshed) + { + mAllDomainBackboneRouters.GetAddress().SetMulticastNetworkPrefix(*Get().GetDomainPrefix()); + Get().SubscribeMulticast(mAllDomainBackboneRouters); + } + +exit: + return; +} + void Local::RemoveDomainPrefixFromNetworkData(void) { otError error = OT_ERROR_NOT_FOUND; // only used for logging. diff --git a/src/core/backbone_router/local.hpp b/src/core/backbone_router/local.hpp index ed30811b5..3451981d7 100644 --- a/src/core/backbone_router/local.hpp +++ b/src/core/backbone_router/local.hpp @@ -195,6 +195,42 @@ public: */ void SetDomainPrefix(const NetworkData::OnMeshPrefixConfig &aConfig); + /** + * This method returns a reference to the All Network Backbone Routers Multicast Address. + * + * @returns A reference to the All Network Backbone Routers Multicast Address. + * + */ + const Ip6::Address &GetAllNetworkBackboneRoutersAddress(void) const + { + return mAllNetworkBackboneRouters.GetAddress(); + } + + /** + * This method returns a reference to the All Domain Backbone Routers Multicast Address. + * + * @returns A reference to the All Domain Backbone Routers Multicast Address. + * + */ + const Ip6::Address &GetAllDomainBackboneRoutersAddress(void) const + { + return mAllDomainBackboneRouters.GetAddress(); + } + + /** + * This method applies the Mesh Local Prefix. + * + */ + void ApplyMeshLocalPrefix(void); + + /** + * This method updates the subscription of All Domain Backbone Routers Multicast Address. + * + * @param[in] aState The Domain Prefix state or state change. + * + */ + void UpdateAllDomainBackboneRouters(Leader::DomainPrefixState aState); + private: void SetState(BackboneRouterState aState); otError RemoveService(void); @@ -220,6 +256,10 @@ private: bool mIsServiceAdded; NetworkData::OnMeshPrefixConfig mDomainPrefixConfig; + + Ip6::NetifUnicastAddress mBackboneRouterPrimaryAloc; + Ip6::NetifMulticastAddress mAllNetworkBackboneRouters; + Ip6::NetifMulticastAddress mAllDomainBackboneRouters; }; } // namespace BackboneRouter diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index f8d6b462d..ee40273a0 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -156,35 +156,6 @@ Mle::Mle(Instance &aInstance) #endif -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - - // Primary Backbone Router Aloc - mBackboneRouterPrimaryAloc.Clear(); - - mBackboneRouterPrimaryAloc.mPrefixLength = MeshLocalPrefix::kLength; - mBackboneRouterPrimaryAloc.mPreferred = true; - mBackboneRouterPrimaryAloc.mValid = true; - mBackboneRouterPrimaryAloc.mScopeOverride = Ip6::Address::kRealmLocalScope; - mBackboneRouterPrimaryAloc.mScopeOverrideValid = true; - mBackboneRouterPrimaryAloc.GetAddress().SetLocator(kAloc16BackboneRouterPrimary); - - // All Network Backbone Routers Multicast Address. - mAllNetworkBackboneRouters.Clear(); - - mAllNetworkBackboneRouters.GetAddress().mFields.m8[0] = 0xff; // Multicast - mAllNetworkBackboneRouters.GetAddress().mFields.m8[1] = 0x32; // Flags = 3, Scope = 2 - mAllNetworkBackboneRouters.GetAddress().mFields.m8[2] = 0; // Reserved - mAllNetworkBackboneRouters.GetAddress().mFields.m8[15] = 3; // Group ID = 3 - - // All Domain Backbone Routers Multicast Address. - mAllDomainBackboneRouters.Clear(); - - mAllDomainBackboneRouters.GetAddress().mFields.m8[0] = 0xff; // Multicast - mAllDomainBackboneRouters.GetAddress().mFields.m8[1] = 0x32; // Flags = 3, Scope = 2 - mAllDomainBackboneRouters.GetAddress().mFields.m8[2] = 0; // Reserved - mAllDomainBackboneRouters.GetAddress().mFields.m8[15] = 3; // Group ID = 3 -#endif - // initialize Mesh Local Prefix meshLocalPrefix.SetFromExtendedPanId(Get().GetExtendedPanId()); @@ -944,30 +915,12 @@ void Mle::SetMeshLocalPrefix(const MeshLocalPrefix &aMeshLocalPrefix) Get().RemoveUnicastAddress(mMeshLocal16); Get().UnsubscribeMulticast(mLinkLocalAllThreadNodes); Get().UnsubscribeMulticast(mRealmLocalAllThreadNodes); - -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - - if (Get().IsPrimary()) - { - Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); - } - - if (Get().IsEnabled()) - { - Get().UnsubscribeMulticast(mAllNetworkBackboneRouters); - } - -#endif } mMeshLocal64.GetAddress().SetPrefix(aMeshLocalPrefix); mMeshLocal16.GetAddress().SetPrefix(aMeshLocalPrefix); mLeaderAloc.GetAddress().SetPrefix(aMeshLocalPrefix); -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(aMeshLocalPrefix); -#endif - // Just keep mesh local prefix if network interface is down VerifyOrExit(Get().IsUp(), OT_NOOP); @@ -1015,20 +968,7 @@ void Mle::ApplyMeshLocalPrefix(void) #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - - if (Get().IsPrimary()) - { - Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); - } - - mAllNetworkBackboneRouters.GetAddress().SetMulticastNetworkPrefix(GetMeshLocalPrefix()); - - if (Get().IsEnabled()) - { - // Subscribe All Network Backbone Routers Multicast Address for both Secondary and Primary state. - Get().SubscribeMulticast(mAllNetworkBackboneRouters); - } - + Get().ApplyMeshLocalPrefix(); #endif exit: @@ -1651,33 +1591,6 @@ void Mle::HandleStateChanged(otChangedFlags aFlags) Get().AllowNativeCommissioner(Get().IsNativeCommissioningAllowed()); } -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - - if (aFlags & OT_CHANGED_THREAD_BACKBONE_ROUTER_STATE) - { - if (Get().IsPrimary()) - { - // Add Primary Backbone Router Aloc for Primary Backbone Router. - Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); - } - else - { - Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); - } - - if (Get().IsEnabled()) - { - // Subscribe All Network Backbone Routers Multicast Address for both Secondary and Primary state. - Get().SubscribeMulticast(mAllNetworkBackboneRouters); - } - else - { - Get().UnsubscribeMulticast(mAllNetworkBackboneRouters); - } - } - -#endif - exit: return; } @@ -4442,33 +4355,5 @@ void Mle::DelayedResponseMetadata::RemoveFrom(Message &aMessage) const OT_UNUSED_VARIABLE(error); } -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE -void Mle::UpdateAllDomainBackboneRouters(BackboneRouter::Leader::DomainPrefixState aState) -{ - if (!Get().IsEnabled()) - { - Get().UnsubscribeMulticast(mAllDomainBackboneRouters); - ExitNow(); - } - - if (aState == BackboneRouter::Leader::kDomainPrefixRemoved || - aState == BackboneRouter::Leader::kDomainPrefixRefreshed) - { - Get().UnsubscribeMulticast(mAllDomainBackboneRouters); - } - - if (aState == BackboneRouter::Leader::kDomainPrefixAdded || - aState == BackboneRouter::Leader::kDomainPrefixRefreshed) - { - mAllDomainBackboneRouters.GetAddress().SetMulticastNetworkPrefix( - *Get().GetDomainPrefix()); - Get().SubscribeMulticast(mAllDomainBackboneRouters); - } - -exit: - return; -} -#endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - } // namespace Mle } // namespace ot diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 5754cd024..76d111ab7 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -45,9 +45,6 @@ #include "thread/mle_tlvs.hpp" #include "thread/mle_types.hpp" #include "thread/topology.hpp" -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE -#include "backbone_router/leader.hpp" -#endif namespace ot { @@ -616,8 +613,6 @@ public: /** * This method applies the Mesh Local Prefix. * - * @param[in] aPrefix A reference to the Mesh Local Prefix. - * */ void ApplyMeshLocalPrefix(void); @@ -658,39 +653,6 @@ public: return mRealmLocalAllThreadNodes.GetAddress(); } -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - /** - * This method returns a reference to the All Network Backbone Routers Multicast Address. - * - * @returns A reference to the All Network Backbone Routers Multicast Address. - * - */ - const Ip6::Address &GetAllNetworkBackboneRoutersAddress(void) const - { - return mAllNetworkBackboneRouters.GetAddress(); - } - - /** - * This method returns a reference to the All Domain Backbone Routers Multicast Address. - * - * @returns A reference to the All Domain Backbone Routers Multicast Address. - * - */ - const Ip6::Address &GetAllDomainBackboneRoutersAddress(void) const - { - return mAllDomainBackboneRouters.GetAddress(); - } - - /** - * This method updates the subscription of All Domain Backbone Routers Multicast Address. - * - * @param[in] aState The Domain Prefix state or state change. - * - */ - void UpdateAllDomainBackboneRouters(BackboneRouter::Leader::DomainPrefixState aState); - -#endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - /** * This method gets the parent when operating in End Device mode. * @@ -1870,12 +1832,6 @@ private: Ip6::NetifUnicastAddress mServiceAlocs[kMaxServiceAlocs]; #endif -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - Ip6::NetifUnicastAddress mBackboneRouterPrimaryAloc; - Ip6::NetifMulticastAddress mAllNetworkBackboneRouters; - Ip6::NetifMulticastAddress mAllDomainBackboneRouters; -#endif - otMleCounters mCounters; Ip6::NetifUnicastAddress mLinkLocal64;