From a67454f8cce198f08d2c76b14e50fbdfee4f5b69 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 26 Feb 2025 16:16:24 -0800 Subject: [PATCH] [mle] update `kAloc16ServiceEnd` and add explicit ALOC16 range check (#11300) This commit updates `kAloc16ServiceEnd` to `0xfc1f`, aligning it with recent changes in the Thread specification. The range `0xfc10-0xfc1f` is now used for service ALOC16 corresponding to the 16 service IDs, and the range `0xfc20-0xfc2f` is reserved for future use. It also updates the `NetworkData::Leader::AnycastLookup()` to explicitly check a given `aAloc16` against the defined ALOC16 ranges. This replaces the previous model, which assumed that ALOC16 ranges followed each other sequentially. While this assumption held true previously, the introduction of the reserved range disrupts this pattern. The explicit range check ensures correct behavior regardless of future changes, making it a safer and more robust approach. --- src/core/common/num_utils.hpp | 19 +++++++++++++++++++ src/core/thread/mle_types.hpp | 4 +++- src/core/thread/network_data_leader_ftd.cpp | 8 ++++---- tests/unit/test_serial_number.cpp | 6 ++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/core/common/num_utils.hpp b/src/core/common/num_utils.hpp index 726cba712..ab5753eb9 100644 --- a/src/core/common/num_utils.hpp +++ b/src/core/common/num_utils.hpp @@ -148,6 +148,25 @@ template int8_t ClampToInt8(IntType aValue) static_cast(NumericLimits::kMax))); } +/** + * This template function checks whether a given value is in a given closed range [min, max]. + * + * Uses `operator<=` to compare the values. The behavior is undefined if the value of @p aMin is greater than @p aMax. + * + * @tparam Type The value type. + * + * @param[in] aValue The value to check + * @param[in] aMin The minimum value. + * @param[in] aMax The maximum value. + * + * @retval TRUE If @p aValue is within `[aMin, aMax]` (inclusive). + * @retval FALSE If @p aValue is not within `[aMin, aMax]` (inclusive). + */ +template Type IsValueInRange(Type aValue, Type aMin, Type aMax) +{ + return (aMin <= aValue) && (aValue <= aMax); +} + /** * This template function performs a three-way comparison between two values. * diff --git a/src/core/thread/mle_types.hpp b/src/core/thread/mle_types.hpp index c679c1e8f..619affb02 100644 --- a/src/core/thread/mle_types.hpp +++ b/src/core/thread/mle_types.hpp @@ -159,7 +159,9 @@ constexpr uint16_t kAloc16Leader = 0xfc00; constexpr uint16_t kAloc16DhcpAgentStart = 0xfc01; constexpr uint16_t kAloc16DhcpAgentEnd = 0xfc0f; constexpr uint16_t kAloc16ServiceStart = 0xfc10; -constexpr uint16_t kAloc16ServiceEnd = 0xfc2f; +constexpr uint16_t kAloc16ServiceEnd = 0xfc1f; +constexpr uint16_t kAloc16ReservedStart = 0xfc20; +constexpr uint16_t kAloc16ReservedEnd = 0xfc2f; constexpr uint16_t kAloc16CommissionerStart = 0xfc30; constexpr uint16_t kAloc16CommissionerEnd = 0xfc37; constexpr uint16_t kAloc16BackboneRouterPrimary = 0xfc38; diff --git a/src/core/thread/network_data_leader_ftd.cpp b/src/core/thread/network_data_leader_ftd.cpp index 22b2b7dda..f3419472e 100644 --- a/src/core/thread/network_data_leader_ftd.cpp +++ b/src/core/thread/network_data_leader_ftd.cpp @@ -109,17 +109,17 @@ Error Leader::AnycastLookup(uint16_t aAloc16, uint16_t &aRloc16) const { aRloc16 = Get().GetLeaderRloc16(); } - else if (aAloc16 <= Mle::kAloc16DhcpAgentEnd) + else if (IsValueInRange(aAloc16, Mle::kAloc16DhcpAgentStart, Mle::kAloc16DhcpAgentEnd)) { uint8_t contextId = static_cast(aAloc16 - Mle::kAloc16DhcpAgentStart + 1); error = LookupRouteForAgentAloc(contextId, IsEntryForDhcp6Agent, aRloc16); } - else if (aAloc16 <= Mle::kAloc16ServiceEnd) + else if (IsValueInRange(aAloc16, Mle::kAloc16ServiceStart, Mle::kAloc16ServiceEnd)) { error = LookupRouteForServiceAloc(aAloc16, aRloc16); } - else if (aAloc16 <= Mle::kAloc16CommissionerEnd) + else if (IsValueInRange(aAloc16, Mle::kAloc16CommissionerStart, Mle::kAloc16CommissionerEnd)) { error = FindBorderAgentRloc(aRloc16); } @@ -130,7 +130,7 @@ Error Leader::AnycastLookup(uint16_t aAloc16, uint16_t &aRloc16) const aRloc16 = Get().GetServer16(); } #endif - else if ((aAloc16 >= Mle::kAloc16NeighborDiscoveryAgentStart) && (aAloc16 <= Mle::kAloc16NeighborDiscoveryAgentEnd)) + else if (IsValueInRange(aAloc16, Mle::kAloc16NeighborDiscoveryAgentStart, Mle::kAloc16NeighborDiscoveryAgentEnd)) { uint8_t contextId = static_cast(aAloc16 - Mle::kAloc16NeighborDiscoveryAgentStart + 1); diff --git a/tests/unit/test_serial_number.cpp b/tests/unit/test_serial_number.cpp index cc80e7b2f..8e388ddb8 100644 --- a/tests/unit/test_serial_number.cpp +++ b/tests/unit/test_serial_number.cpp @@ -110,6 +110,12 @@ void TestNumUtils(void) u32 = 0xfff0000; VerifyOrQuit(ClampToUint16(u32) == 0xffff); + VerifyOrQuit(IsValueInRange(5, 5, 10)); + VerifyOrQuit(IsValueInRange(7, 5, 10)); + VerifyOrQuit(IsValueInRange(10, 5, 10)); + VerifyOrQuit(!IsValueInRange(4, 5, 10)); + VerifyOrQuit(!IsValueInRange(11, 5, 10)); + VerifyOrQuit(ThreeWayCompare(2, 2) == 0); VerifyOrQuit(ThreeWayCompare(2, 1) > 0); VerifyOrQuit(ThreeWayCompare(1, 2) < 0);