[mac-frame] handle exception case for payload of mac cmd (#5499)

In current Mac::Frame implementation, when the frame is a Mac command,
the CommandId is always not included as its private payload, which
means we won't encrypt the CommandId.

In most cases the Private Payload(things we need to encrypt) shall be
set to the MAC Payload field(For Mac cmd, this field includes
CommandId, shown in Figure 7-17) and the Open Payload shall be
empty. The only 2 exception cases are listed in Table 9-1.

So for Mac command with version >= 2, its CommandId should be included
in Private Payload and should be encrypted when necessary. For Mac
command with version < 2, CommandId should be in included in Open
Payload and shouldn't be encrypted.

This commit fixes this problem and adds unit tests for this.
This commit is contained in:
Li Cao
2020-09-08 18:24:17 -07:00
committed by GitHub
parent 3ffc503c32
commit e9d578da51
3 changed files with 49 additions and 7 deletions
-1
View File
@@ -163,7 +163,6 @@ do_unit()
if [[ ${THREAD_VERSION} == "1.2" ]]; then
do_unit_version "1.2-bbr"
do_unit_version "1.1"
fi
}
+3 -4
View File
@@ -582,10 +582,9 @@ otError Frame::GetCommandId(uint8_t &aCommandId) const
{
otError error = OT_ERROR_NONE;
uint8_t index = FindPayloadIndex();
VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE);
aCommandId = mPsdu[index - 1];
aCommandId = mPsdu[IsVersion2015() ? index : (index - 1)];
exit:
return error;
@@ -598,7 +597,7 @@ otError Frame::SetCommandId(uint8_t aCommandId)
VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE);
mPsdu[index - 1] = aCommandId;
mPsdu[IsVersion2015() ? index : (index - 1)] = aCommandId;
exit:
return error;
@@ -848,7 +847,7 @@ uint8_t Frame::FindPayloadIndex(void) const
}
#endif // OPENTHREAD_CONFIG_MAC_HEADER_IE_SUPPORT
if ((GetFrameControlField() & kFcfFrameTypeMask) == kFcfFrameMacCmd)
if (!IsVersion2015() && (GetFrameControlField() & kFcfFrameTypeMask) == kFcfFrameMacCmd)
{
index += kCommandIdSize;
}
+46 -2
View File
@@ -410,13 +410,19 @@ void TestMacChannelMask(void)
void TestMacFrameApi(void)
{
uint8_t ack_psdu1[] = {0x02, 0x10, 0x5e, 0xd2, 0x9b};
uint8_t ack_psdu1[] = {0x02, 0x10, 0x5e, 0xd2, 0x9b};
uint8_t mac_cmd_psdu1[] = {0x6b, 0xdc, 0x85, 0xce, 0xfa, 0x47, 0x36, 0x07, 0xd9, 0x74, 0x45, 0x8d,
0xb2, 0x6e, 0x81, 0x25, 0xc9, 0xdb, 0xac, 0x2b, 0x0a, 0x0d, 0x00, 0x00,
0x00, 0x00, 0x01, 0x04, 0xaf, 0x14, 0xce, 0xaa, 0x5a, 0xe5};
Mac::Frame frame;
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
uint8_t data_psdu1[] = {0x29, 0xee, 0x53, 0xce, 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x6e, 0x16, 0x05,
uint8_t data_psdu1[] = {0x29, 0xee, 0x53, 0xce, 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x6e, 0x16, 0x05,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x6e, 0x16, 0x0d, 0x01, 0x00, 0x00, 0x00, 0x01};
uint8_t mac_cmd_psdu2[] = {0x6b, 0xaa, 0x8d, 0xce, 0xfa, 0x00, 0x68, 0x01, 0x68, 0x0d,
0x08, 0x00, 0x00, 0x00, 0x01, 0x04, 0x0d, 0xed, 0x0b, 0x35,
0x0c, 0x80, 0x3f, 0x04, 0x4b, 0x88, 0x89, 0xd6, 0x59, 0xe1};
otError error;
uint8_t scf; // SecurityControlField
#endif
@@ -470,6 +476,44 @@ void TestMacFrameApi(void)
"Mac::Frame::GetSecurityControlField failed\n");
VerifyOrQuit(scf == 0xff, "Mac::Frame::SetSecurityControlField value failed\n");
#endif // OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2
// IEEE 802.15.4-2006 Mac Command
// Sequence Number: 133
// Command Identifier: Data Request (0x04)
uint8_t commandId;
frame.mPsdu = mac_cmd_psdu1;
frame.mLength = sizeof(mac_cmd_psdu1);
VerifyOrQuit(frame.GetSequence() == 133, "Mac::Frame::GetSequence failed\n");
VerifyOrQuit(frame.GetVersion() == Mac::Frame::kFcfFrameVersion2006, "Mac::Frame::GetVersion failed\n");
VerifyOrQuit(frame.GetType() == Mac::Frame::kFcfFrameMacCmd, "Mac::Frame::GetType failed\n");
VerifyOrQuit(frame.GetCommandId(commandId) == OT_ERROR_NONE, "Mac::Frame::GetCommandId failed\n");
VerifyOrQuit(commandId == Mac::Frame::kMacCmdDataRequest, "Mac::Frame::GetCommandId value not correct\n");
VerifyOrQuit(frame.SetCommandId(Mac::Frame::kMacCmdBeaconRequest) == OT_ERROR_NONE,
"Mac::Frame::SetCommandId failed\n");
VerifyOrQuit(frame.GetCommandId(commandId) == OT_ERROR_NONE, "Mac::Frame::GetCommandId failed\n");
VerifyOrQuit(commandId == Mac::Frame::kMacCmdBeaconRequest, "Mac::Frame::SetCommandId value not correct\n");
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
// IEEE 802.15.4-2015 Mac Command
// Sequence Number: 141
// Header IEs
// CSL IE
// Header Termination 2 IE (Payload follows)
// Command Identifier: Data Request (0x04)
frame.mPsdu = mac_cmd_psdu2;
frame.mLength = sizeof(mac_cmd_psdu2);
VerifyOrQuit(frame.GetSequence() == 141, "Mac::Frame::GetSequence failed\n");
VerifyOrQuit(frame.IsVersion2015() == true, "Mac::Frame::IsVersion2015 failed\n");
VerifyOrQuit(frame.GetType() == Mac::Frame::kFcfFrameMacCmd, "Mac::Frame::GetVersion failed\n");
VerifyOrQuit(frame.GetCommandId(commandId) == OT_ERROR_NONE, "Mac::Frame::GetCommandId failed\n");
VerifyOrQuit(commandId == Mac::Frame::kMacCmdDataRequest, "Mac::Frame::GetCommandId value not correct\n");
printf("commandId:%d\n", commandId);
VerifyOrQuit(frame.SetCommandId(Mac::Frame::kMacCmdOrphanNotification) == OT_ERROR_NONE,
"Mac::Frame::SetCommandId failed\n");
VerifyOrQuit(frame.GetCommandId(commandId) == OT_ERROR_NONE, "Mac::Frame::GetCommandId failed\n");
VerifyOrQuit(commandId == Mac::Frame::kMacCmdOrphanNotification, "Mac::Frame::SetCommandId value not correct\n");
#endif
}
void TestMacFrameAckGeneration(void)