diff --git a/src/core/border_router/routing_manager.cpp b/src/core/border_router/routing_manager.cpp index 0e7b0527a..fa7941ad9 100644 --- a/src/core/border_router/routing_manager.cpp +++ b/src/core/border_router/routing_manager.cpp @@ -4055,12 +4055,7 @@ void RoutingManager::Nat64PrefixManager::Stop(void) { LogInfo("Stopping Nat64PrefixManager"); - if (mPublishedPrefix.IsValidNat64()) - { - IgnoreError(Get().UnpublishPrefix(mPublishedPrefix)); - } - - mPublishedPrefix.Clear(); + Unpublish(); mInfraIfPrefix.Clear(); mTimer.Stop(); @@ -4129,17 +4124,13 @@ void RoutingManager::Nat64PrefixManager::Evaluate(void) ((error == kErrorNotFound) || (netdataPrefixConfig.mPreference < preference) || (netdataPrefixConfig.GetPrefix() == mPublishedPrefix) || (netdataPrefixConfig.GetPrefix() == mInfraIfPrefix)); - if (mPublishedPrefix.IsValidNat64() && (!shouldPublish || (prefix != mPublishedPrefix))) + if (shouldPublish) { - IgnoreError(Get().UnpublishPrefix(mPublishedPrefix)); - mPublishedPrefix.Clear(); + Publish(prefix, preference); } - - if (shouldPublish && ((prefix != mPublishedPrefix) || (preference != mPublishedPreference))) + else { - mPublishedPrefix = prefix; - mPublishedPreference = preference; - Publish(); + Unpublish(); } #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE @@ -4162,10 +4153,25 @@ exit: return; } -void RoutingManager::Nat64PrefixManager::Publish(void) +void RoutingManager::Nat64PrefixManager::Publish(const Ip6::Prefix &aPrefix, RoutePreference aPreference) { + // Publishes the given prefix and preference in the Network Data + // if they differ from the currently published ones. This method + // updates `mPublishedPrefix` and `mPublishedPreference` to track + // the new values. + // + // If there is any change, this method ensures that the previous + // prefix is unpublished before the new one is published. + NetworkData::ExternalRouteConfig routeConfig; + VerifyOrExit((aPrefix != mPublishedPrefix) || (aPreference != mPublishedPreference)); + + Unpublish(); + + mPublishedPrefix = aPrefix; + mPublishedPreference = aPreference; + routeConfig.Clear(); routeConfig.SetPrefix(mPublishedPrefix); routeConfig.mPreference = mPublishedPreference; @@ -4174,6 +4180,23 @@ void RoutingManager::Nat64PrefixManager::Publish(void) SuccessOrAssert( Get().PublishExternalRoute(routeConfig, NetworkData::Publisher::kFromRoutingManager)); + +exit: + return; +} + +void RoutingManager::Nat64PrefixManager::Unpublish(void) +{ + // Unpublishes the previously published prefix (if any) and clears + // the `mPublishedPrefix`. + + VerifyOrExit(mPublishedPrefix.IsValidNat64()); + + IgnoreError(Get().UnpublishPrefix(mPublishedPrefix)); + mPublishedPrefix.Clear(); + +exit: + return; } void RoutingManager::Nat64PrefixManager::HandleTimer(void) diff --git a/src/core/border_router/routing_manager.hpp b/src/core/border_router/routing_manager.hpp index 1a06cc27b..7b15d8bbf 100644 --- a/src/core/border_router/routing_manager.hpp +++ b/src/core/border_router/routing_manager.hpp @@ -1556,12 +1556,12 @@ private: private: void Discover(void); - void Publish(void); + void Publish(const Ip6::Prefix &aPrefix, RoutePreference aPreference); + void Unpublish(void); using Nat64Timer = TimerMilliIn; - bool mEnabled; - + bool mEnabled; Ip6::Prefix mInfraIfPrefix; // The latest NAT64 prefix discovered on the infrastructure interface. Ip6::Prefix mLocalPrefix; // The local prefix (from BR ULA prefix). Ip6::Prefix mPublishedPrefix; // The prefix to publish in Net Data (empty or local or from infra-if).