Skip to content

Commit

Permalink
[mle] update kAloc16ServiceEnd and add explicit ALOC16 range check (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
abtink authored Feb 27, 2025
1 parent aaa929f commit a67454f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/core/common/num_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 3 additions & 1 deletion src/core/thread/mle_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/core/thread/network_data_leader_ftd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_serial_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a67454f

Please sign in to comment.