Skip to content

Commit dd33295

Browse files
authored
[mac] add RxFrame::IsSecuredWith() helper method (openthread#13064)
This commit introduces a new helper method, `RxFrame::IsSecuredWith()`, which allows callers to cleanly verify if a received MAC frame has security enabled and uses a specific set of allowed Key ID Modes. This eliminates redundant logic in `ThreadLinkInfo::SetFrom()`, where the code previously had to manually check `GetSecurityEnabled()`, extract the Key ID Mode, and validate it against `kKeyIdMode0` or `kKeyIdMode1`. Mac::ProcessCsl()` is updated to use this new method to cleanly enforce that CSL IE processing only occurs on frames secured with Key ID Mode 1 Crucially, this commit also updates `DataPollHandler::HandleDataPoll()` to use this new helper. Previously, it only checked if the frame was secured (`GetSecurityEnabled()`), which would accept frames using any Key ID Mode (including mode 2 with fixed/known keys). By restricting the data poll handling to only accept Key ID Mode 1, we ensure that data polls are only processed if they are secured with a valid Thread network key.
1 parent e6134cb commit dd33295

5 files changed

Lines changed: 58 additions & 25 deletions

File tree

src/core/mac/data_poll_handler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ void DataPollHandler::HandleDataPoll(Mac::RxFrame &aFrame)
8989
Child *child;
9090
uint16_t indirectMsgCount;
9191

92-
VerifyOrExit(aFrame.GetSecurityEnabled());
92+
VerifyOrExit(aFrame.IsSecuredWith(Mac::RxFrame::kAllowKeyIdMode1));
93+
9394
VerifyOrExit(!Get<Mle::Mle>().IsDetached());
9495

9596
SuccessOrExit(aFrame.GetSrcAddr(macSource));

src/core/mac/mac.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,12 +2491,8 @@ void Mac::ProcessCsl(const RxFrame &aFrame, const Address &aSrcAddr)
24912491
CslNeighbor *neighbor = nullptr;
24922492
const CslIe *csl;
24932493

2494-
uint8_t keyIdMode;
2495-
2496-
VerifyOrExit(aFrame.IsVersion2015() && aFrame.GetSecurityEnabled());
2497-
2498-
IgnoreError(aFrame.GetKeyIdMode(keyIdMode));
2499-
VerifyOrExit(keyIdMode == Frame::kKeyIdMode1);
2494+
VerifyOrExit(aFrame.IsVersion2015());
2495+
VerifyOrExit(aFrame.IsSecuredWith(RxFrame::kAllowKeyIdMode1));
25002496

25012497
csl = aFrame.GetCslIe();
25022498
VerifyOrExit(csl != nullptr);

src/core/mac/mac_frame.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,32 @@ Error TxFrame::GenerateWakeupFrame(PanId aPanId, const WakeupRequest &aWakeupReq
15611561
}
15621562
#endif // OPENTHREAD_CONFIG_WAKEUP_COORDINATOR_ENABLE
15631563

1564+
bool RxFrame::IsSecuredWith(KeyIdModeFlags aFlags) const
1565+
{
1566+
bool isSecure = false;
1567+
uint8_t keyIdMode;
1568+
1569+
VerifyOrExit(GetSecurityEnabled());
1570+
SuccessOrExit(GetKeyIdMode(keyIdMode));
1571+
1572+
switch (keyIdMode)
1573+
{
1574+
case kKeyIdMode0:
1575+
VerifyOrExit(aFlags & kAllowKeyIdMode0);
1576+
break;
1577+
case kKeyIdMode1:
1578+
VerifyOrExit(aFlags & kAllowKeyIdMode1);
1579+
break;
1580+
default:
1581+
ExitNow();
1582+
}
1583+
1584+
isSecure = true;
1585+
1586+
exit:
1587+
return isSecure;
1588+
}
1589+
15641590
Error RxFrame::ProcessReceiveAesCcm(const ExtAddress &aExtAddress, const KeyMaterial &aMacKey)
15651591
{
15661592
#if OPENTHREAD_FTD || OPENTHREAD_MTD

src/core/mac/mac_frame.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,30 @@ class RxFrame : public Frame
933933
public:
934934
friend class TxFrame;
935935

936+
/**
937+
* Defines flags to indicate allowed Key ID Modes, used in `IsSecuredWith()`.
938+
*/
939+
enum KeyIdModeFlag : uint8_t
940+
{
941+
kAllowKeyIdMode0 = (1 << 0), ///< Allow Key ID Mode 0.
942+
kAllowKeyIdMode1 = (1 << 1), ///< Allow Key ID Mode 1.
943+
};
944+
945+
/**
946+
* Represents a set of `KeyIdModeFlag`s.
947+
*/
948+
typedef uint8_t KeyIdModeFlags;
949+
950+
/**
951+
* Indicates whether the frame is secured with a given set of allowed Key ID Modes.
952+
*
953+
* @param[in] aFlags A bitmask of `KeyIdModeFlags` specifying the allowed modes.
954+
*
955+
* @retval TRUE The frame has security enabled and uses one of the allowed Key ID Modes.
956+
* @retval FALSE The frame does not have security enabled, or its Key ID Mode is not allowed.
957+
*/
958+
bool IsSecuredWith(KeyIdModeFlags aFlags) const;
959+
936960
/**
937961
* Returns the RSSI in dBm used for reception.
938962
*

src/core/thread/thread_link_info.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,10 @@ void ThreadLinkInfo::SetFrom(const Mac::RxFrame &aFrame)
5555
mIsDstPanIdBroadcast = (dstPanId == Mac::kPanIdBroadcast);
5656
}
5757

58-
if (aFrame.GetSecurityEnabled())
59-
{
60-
uint8_t keyIdMode;
61-
62-
// MAC Frame Security was already validated at the MAC
63-
// layer. As a result, `GetKeyIdMode()` will never return
64-
// failure here.
65-
IgnoreError(aFrame.GetKeyIdMode(keyIdMode));
66-
67-
mLinkSecurity = (keyIdMode == Mac::Frame::kKeyIdMode0) || (keyIdMode == Mac::Frame::kKeyIdMode1);
68-
}
69-
else
70-
{
71-
mLinkSecurity = false;
72-
}
73-
mChannel = aFrame.GetChannel();
74-
mRss = aFrame.GetRssi();
75-
mLqi = aFrame.GetLqi();
58+
mLinkSecurity = aFrame.IsSecuredWith(Mac::RxFrame::kAllowKeyIdMode0 | Mac::RxFrame::kAllowKeyIdMode1);
59+
mChannel = aFrame.GetChannel();
60+
mRss = aFrame.GetRssi();
61+
mLqi = aFrame.GetLqi();
7662
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
7763
if (aFrame.GetTimeIe() != nullptr)
7864
{

0 commit comments

Comments
 (0)