diff --git a/src/core/thread/network_data_leader.cpp b/src/core/thread/network_data_leader.cpp index 1fda49a93..ca1fd2185 100644 --- a/src/core/thread/network_data_leader.cpp +++ b/src/core/thread/network_data_leader.cpp @@ -334,7 +334,8 @@ Error LeaderBase::ExternalRouteLookup(uint8_t aDomainId, const Ip6::Address &aDe for (const HasRouteEntry *entry = hasRoute->GetFirstEntry(); entry <= hasRoute->GetLastEntry(); entry = entry->GetNext()) { - if (bestRouteEntry == nullptr || CompareRouteEntries(*entry, *bestRouteEntry) > 0) + if ((bestRouteEntry == nullptr) || (prefixLength > bestMatchLength) || + CompareRouteEntries(*entry, *bestRouteEntry) > 0) { bestRouteEntry = entry; bestMatchLength = prefixLength;