[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.
This commit is contained in:
Abtin Keshavarzian
2025-02-26 16:16:24 -08:00
committed by GitHub
parent aaa929f6a6
commit a67454f8cc
4 changed files with 32 additions and 5 deletions
+19
View File
@@ -148,6 +148,25 @@ template <typename IntType> int8_t ClampToInt8(IntType aValue)
static_cast<IntType>(NumericLimits<int8_t>::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 <typename Type> Type IsValueInRange(Type aValue, Type aMin, Type aMax)
{
return (aMin <= aValue) && (aValue <= aMax);
}
/**
* This template function performs a three-way comparison between two values.
*
+3 -1
View File
@@ -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;
+4 -4
View File
@@ -109,17 +109,17 @@ Error Leader::AnycastLookup(uint16_t aAloc16, uint16_t &aRloc16) const
{
aRloc16 = Get<Mle::Mle>().GetLeaderRloc16();
}
else if (aAloc16 <= Mle::kAloc16DhcpAgentEnd)
else if (IsValueInRange(aAloc16, Mle::kAloc16DhcpAgentStart, Mle::kAloc16DhcpAgentEnd))
{
uint8_t contextId = static_cast<uint8_t>(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<BackboneRouter::Leader>().GetServer16();
}
#endif
else if ((aAloc16 >= Mle::kAloc16NeighborDiscoveryAgentStart) && (aAloc16 <= Mle::kAloc16NeighborDiscoveryAgentEnd))
else if (IsValueInRange(aAloc16, Mle::kAloc16NeighborDiscoveryAgentStart, Mle::kAloc16NeighborDiscoveryAgentEnd))
{
uint8_t contextId = static_cast<uint8_t>(aAloc16 - Mle::kAloc16NeighborDiscoveryAgentStart + 1);
+6
View File
@@ -110,6 +110,12 @@ void TestNumUtils(void)
u32 = 0xfff0000;
VerifyOrQuit(ClampToUint16(u32) == 0xffff);
VerifyOrQuit(IsValueInRange<uint8_t>(5, 5, 10));
VerifyOrQuit(IsValueInRange<uint8_t>(7, 5, 10));
VerifyOrQuit(IsValueInRange<uint8_t>(10, 5, 10));
VerifyOrQuit(!IsValueInRange<uint8_t>(4, 5, 10));
VerifyOrQuit(!IsValueInRange<uint8_t>(11, 5, 10));
VerifyOrQuit(ThreeWayCompare<uint8_t>(2, 2) == 0);
VerifyOrQuit(ThreeWayCompare<uint8_t>(2, 1) > 0);
VerifyOrQuit(ThreeWayCompare<uint8_t>(1, 2) < 0);