Skip to content

Commit

Permalink
[netdata] simplify parsing of Commissioning Dataset sub-TLVs (openthr…
Browse files Browse the repository at this point in the history
…ead#9541)

This commit contains changes related to parsing of Commissioning
Dataset sub-TLVs in `NetworkData::Leader`:
- Adds `FindInCommisioningData<SubTlvType>` to search for a given
  `SubTlvType` in Commissioning Data.
- Adds `FindCommissioningSessionId()`, `FindBorderAgentRloc()`,
  and `FindJoinerUdpPort()` to parse and get get the info
  from Commissioning Dataset.
  • Loading branch information
abtink authored Oct 18, 2023
1 parent 03bfced commit 2457ba7
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 114 deletions.
7 changes: 3 additions & 4 deletions src/core/backbone_router/bbr_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,10 @@ void Manager::HandleMulticastListenerRegistration(const Coap::Message &aMessage,

if (Tlv::Find<ThreadCommissionerSessionIdTlv>(aMessage, commissionerSessionId) == kErrorNone)
{
const MeshCoP::CommissionerSessionIdTlv *commissionerSessionIdTlv = As<MeshCoP::CommissionerSessionIdTlv>(
Get<NetworkData::Leader>().GetCommissioningDataSubTlv(MeshCoP::Tlv::kCommissionerSessionId));
uint16_t localSessionId;

VerifyOrExit(commissionerSessionIdTlv != nullptr &&
commissionerSessionIdTlv->GetCommissionerSessionId() == commissionerSessionId,
VerifyOrExit((Get<NetworkData::Leader>().FindCommissioningSessionId(localSessionId) == kErrorNone) &&
(localSessionId == commissionerSessionId),
status = ThreadStatusTlv::kMlrGeneralFailure);

hasCommissionerSessionIdTlv = true;
Expand Down
21 changes: 7 additions & 14 deletions src/core/meshcop/dataset_manager_ftd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,12 @@ Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo
// check commissioner session id
if (Tlv::Find<CommissionerSessionIdTlv>(aMessage, sessionId) == kErrorNone)
{
const CommissionerSessionIdTlv *localId;
uint16_t localSessionId;

isUpdateFromCommissioner = true;

localId = As<CommissionerSessionIdTlv>(
Get<NetworkData::Leader>().GetCommissioningDataSubTlv(Tlv::kCommissionerSessionId));

VerifyOrExit(localId != nullptr && localId->GetCommissionerSessionId() == sessionId);
SuccessOrExit(Get<NetworkData::Leader>().FindCommissioningSessionId(localSessionId));
VerifyOrExit(localSessionId == sessionId);
}

// verify an MGMT_ACTIVE_SET.req from a Commissioner does not affect connectivity
Expand Down Expand Up @@ -240,16 +238,11 @@ Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo
// notify commissioner if update is from thread device
if (!isUpdateFromCommissioner)
{
const CommissionerSessionIdTlv *localSessionId;
Ip6::Address destination;

localSessionId = As<CommissionerSessionIdTlv>(
Get<NetworkData::Leader>().GetCommissioningDataSubTlv(Tlv::kCommissionerSessionId));
VerifyOrExit(localSessionId != nullptr);

SuccessOrExit(
Get<Mle::MleRouter>().GetCommissionerAloc(destination, localSessionId->GetCommissionerSessionId()));
uint16_t localSessionId;
Ip6::Address destination;

SuccessOrExit(Get<NetworkData::Leader>().FindCommissioningSessionId(localSessionId));
SuccessOrExit(Get<Mle::MleRouter>().GetCommissionerAloc(destination, localSessionId));
Get<Leader>().SendDatasetChanged(destination);
}

Expand Down
24 changes: 14 additions & 10 deletions src/core/meshcop/joiner_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void JoinerRouter::Start(void)
{
VerifyOrExit(Get<Mle::MleRouter>().IsFullThreadDevice());

if (Get<NetworkData::Leader>().IsJoiningEnabled())
if (Get<NetworkData::Leader>().IsJoiningAllowed())
{
uint16_t port = GetJoinerUdpPort();

Expand All @@ -99,20 +99,24 @@ void JoinerRouter::Start(void)
return;
}

uint16_t JoinerRouter::GetJoinerUdpPort(void)
uint16_t JoinerRouter::GetJoinerUdpPort(void) const
{
uint16_t rval = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
const JoinerUdpPortTlv *joinerUdpPort;
uint16_t port;

VerifyOrExit(!mIsJoinerPortConfigured, rval = mJoinerUdpPort);
if (mIsJoinerPortConfigured)
{
ExitNow(port = mJoinerUdpPort);
}

joinerUdpPort = As<JoinerUdpPortTlv>(Get<NetworkData::Leader>().GetCommissioningDataSubTlv(Tlv::kJoinerUdpPort));
VerifyOrExit(joinerUdpPort != nullptr);
if (Get<NetworkData::Leader>().FindJoinerUdpPort(port) == kErrorNone)
{
ExitNow();
}

rval = joinerUdpPort->GetUdpPort();
port = kDefaultJoinerUdpPort;

exit:
return rval;
return port;
}

void JoinerRouter::SetJoinerUdpPort(uint16_t aJoinerUdpPort)
Expand All @@ -137,7 +141,7 @@ void JoinerRouter::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &a

LogInfo("JoinerRouter::HandleUdpReceive");

SuccessOrExit(error = GetBorderAgentRloc(Get<ThreadNetif>(), borderAgentRloc));
SuccessOrExit(error = Get<NetworkData::Leader>().FindBorderAgentRloc(borderAgentRloc));

message = Get<Tmf::Agent>().NewPriorityNonConfirmablePostMessage(kUriRelayRx);
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
Expand Down
5 changes: 3 additions & 2 deletions src/core/meshcop/joiner_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ class JoinerRouter : public InstanceLocator, private NonCopyable
/**
* Returns the Joiner UDP Port.
*
* @returns The Joiner UDP Port number .
* @returns The Joiner UDP Port number.
*
*/
uint16_t GetJoinerUdpPort(void);
uint16_t GetJoinerUdpPort(void) const;

/**
* Sets the Joiner UDP Port.
Expand All @@ -85,6 +85,7 @@ class JoinerRouter : public InstanceLocator, private NonCopyable
void SetJoinerUdpPort(uint16_t aJoinerUdpPort);

private:
static constexpr uint16_t kDefaultJoinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
static constexpr uint32_t kJoinerEntrustTxDelay = 50; // in msec

struct JoinerEntrustMetadata
Expand Down
16 changes: 1 addition & 15 deletions src/core/meshcop/meshcop.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* Copyright (c) 2017, The OpenThread Authors.
* All rights reserved.
Expand Down Expand Up @@ -300,21 +301,6 @@ void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId)
aJoinerId.SetLocal(true);
}

Error GetBorderAgentRloc(ThreadNetif &aNetif, uint16_t &aRloc)
{
Error error = kErrorNone;
const BorderAgentLocatorTlv *borderAgentLocator;

borderAgentLocator = As<BorderAgentLocatorTlv>(
aNetif.Get<NetworkData::Leader>().GetCommissioningDataSubTlv(Tlv::kBorderAgentLocator));
VerifyOrExit(borderAgentLocator != nullptr, error = kErrorNotFound);

aRloc = borderAgentLocator->GetBorderAgentLocator();

exit:
return error;
}

#if OPENTHREAD_FTD
Error GeneratePskc(const char *aPassPhrase,
const NetworkName &aNetworkName,
Expand Down
12 changes: 0 additions & 12 deletions src/core/meshcop/meshcop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,18 +434,6 @@ Error GeneratePskc(const char *aPassPhrase,
*/
void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId);

/**
* Gets the border agent RLOC.
*
* @param[in] aNetIf A reference to the thread interface.
* @param[out] aRloc Border agent RLOC.
*
* @retval kErrorNone Successfully got the Border Agent Rloc.
* @retval kErrorNotFound Border agent is not available.
*
*/
Error GetBorderAgentRloc(ThreadNetif &aNetIf, uint16_t &aRloc);

#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN)
/**
* Emits a log message indicating an error during a MeshCoP action.
Expand Down
3 changes: 1 addition & 2 deletions src/core/meshcop/meshcop_leader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ template <> void Leader::HandleTmf<kUriLeaderKeepAlive>(Coap::Message &aMessage,

SuccessOrExit(Tlv::Find<CommissionerSessionIdTlv>(aMessage, sessionId));

borderAgentLocator =
As<BorderAgentLocatorTlv>(Get<NetworkData::Leader>().GetCommissioningDataSubTlv(Tlv::kBorderAgentLocator));
borderAgentLocator = Get<NetworkData::Leader>().FindInCommissioningData<BorderAgentLocatorTlv>();

if ((borderAgentLocator == nullptr) || (sessionId != mSessionId))
{
Expand Down
4 changes: 3 additions & 1 deletion src/core/thread/energy_scan_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ void EnergyScanServer::SendReport(void)

void EnergyScanServer::HandleNotifierEvents(Events aEvents)
{
uint16_t borderAgentRloc;

if (aEvents.Contains(kEventThreadNetdataChanged) && (mReportMessage != nullptr) &&
Get<NetworkData::Leader>().GetCommissioningData() == nullptr)
Get<NetworkData::Leader>().FindBorderAgentRloc(borderAgentRloc) != kErrorNone)
{
mReportMessage->Free();
mReportMessage = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/mesh_forwarder_ftd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ Error MeshForwarder::UpdateIp6RouteFtd(const Ip6::Header &aIp6Header, Message &a
}
else if (aloc16 <= Mle::kAloc16CommissionerEnd)
{
SuccessOrExit(error = MeshCoP::GetBorderAgentRloc(Get<ThreadNetif>(), mMeshDest));
SuccessOrExit(error = Get<NetworkData::Leader>().FindBorderAgentRloc(mMeshDest));
}

#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
Expand Down
10 changes: 5 additions & 5 deletions src/core/thread/mle_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2740,7 +2740,7 @@ void MleRouter::HandleDiscoveryRequest(RxInfo &aRxInfo)
else // if steering data is not set out of band, fall back to network data
#endif
{
VerifyOrExit(Get<NetworkData::Leader>().IsJoiningEnabled(), error = kErrorSecurity);
VerifyOrExit(Get<NetworkData::Leader>().IsJoiningAllowed(), error = kErrorSecurity);
}
}
}
Expand Down Expand Up @@ -2821,13 +2821,13 @@ Error MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, const M
else
#endif
{
const MeshCoP::Tlv *steeringData;
const MeshCoP::SteeringDataTlv *steeringDataTlv;

steeringData = Get<NetworkData::Leader>().GetCommissioningDataSubTlv(MeshCoP::Tlv::kSteeringData);
steeringDataTlv = Get<NetworkData::Leader>().FindInCommissioningData<MeshCoP::SteeringDataTlv>();

if (steeringData != nullptr)
if (steeringDataTlv != nullptr)
{
SuccessOrExit(error = steeringData->AppendTo(*message));
SuccessOrExit(error = steeringDataTlv->AppendTo(*message));
}
}

Expand Down
74 changes: 50 additions & 24 deletions src/core/thread/network_data_leader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,50 +453,76 @@ Error LeaderBase::SetCommissioningData(const void *aValue, uint8_t aValueLength)
return error;
}

const CommissioningDataTlv *LeaderBase::GetCommissioningData(void) const
const CommissioningDataTlv *LeaderBase::FindCommissioningData(void) const
{
return NetworkDataTlv::Find<CommissioningDataTlv>(GetTlvsStart(), GetTlvsEnd());
}

const MeshCoP::Tlv *LeaderBase::GetCommissioningDataSubTlv(MeshCoP::Tlv::Type aType) const
const MeshCoP::Tlv *LeaderBase::FindCommissioningDataSubTlv(uint8_t aType) const
{
const MeshCoP::Tlv *rval = nullptr;
const NetworkDataTlv *commissioningDataTlv;
const MeshCoP::Tlv *subTlv = nullptr;
const NetworkDataTlv *dataTlv = FindCommissioningData();

commissioningDataTlv = GetCommissioningData();
VerifyOrExit(commissioningDataTlv != nullptr);
VerifyOrExit(dataTlv != nullptr);
subTlv = As<MeshCoP::Tlv>(Tlv::FindTlv(dataTlv->GetValue(), dataTlv->GetLength(), aType));

rval = As<MeshCoP::Tlv>(Tlv::FindTlv(commissioningDataTlv->GetValue(), commissioningDataTlv->GetLength(), aType));
exit:
return subTlv;
}

Error LeaderBase::ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::Type aType, uint16_t &aValue) const
{
Error error = kErrorNone;
const MeshCoP::Tlv *subTlv = FindCommissioningDataSubTlv(aType);

VerifyOrExit(subTlv != nullptr, error = kErrorNotFound);
VerifyOrExit(subTlv->GetLength() >= sizeof(uint16_t), error = kErrorParse);
aValue = Encoding::BigEndian::ReadUint16(subTlv->GetValue());

exit:
return rval;
return error;
}

Error LeaderBase::FindBorderAgentRloc(uint16_t &aRloc16) const
{
return ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::kBorderAgentLocator, aRloc16);
}

Error LeaderBase::FindCommissioningSessionId(uint16_t &aSessionId) const
{
return ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::kCommissionerSessionId, aSessionId);
}

bool LeaderBase::IsJoiningEnabled(void) const
Error LeaderBase::FindJoinerUdpPort(uint16_t &aPort) const
{
const MeshCoP::Tlv *steeringData;
bool rval = false;
return ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::kJoinerUdpPort, aPort);
}

bool LeaderBase::IsJoiningAllowed(void) const
{
bool isAllowed = false;
const MeshCoP::SteeringDataTlv *steeringDataTlv;

VerifyOrExit(GetCommissioningDataSubTlv(MeshCoP::Tlv::kBorderAgentLocator) != nullptr);
VerifyOrExit(FindInCommissioningData<MeshCoP::BorderAgentLocatorTlv>() != nullptr);

steeringData = GetCommissioningDataSubTlv(MeshCoP::Tlv::kSteeringData);
VerifyOrExit(steeringData != nullptr);
steeringDataTlv = FindInCommissioningData<MeshCoP::SteeringDataTlv>();
VerifyOrExit(steeringDataTlv != nullptr);

for (int i = 0; i < steeringData->GetLength(); i++)
for (int i = 0; i < steeringDataTlv->GetLength(); i++)
{
if (steeringData->GetValue()[i] != 0)
if (steeringDataTlv->GetValue()[i] != 0)
{
ExitNow(rval = true);
ExitNow(isAllowed = true);
}
}

exit:
return rval;
return isAllowed;
}

void LeaderBase::RemoveCommissioningData(void)
{
CommissioningDataTlv *tlv = GetCommissioningData();
CommissioningDataTlv *tlv = FindCommissioningData();

VerifyOrExit(tlv != nullptr);
RemoveTlv(tlv);
Expand All @@ -507,14 +533,14 @@ void LeaderBase::RemoveCommissioningData(void)

Error LeaderBase::SteeringDataCheck(const FilterIndexes &aFilterIndexes) const
{
Error error = kErrorNone;
const MeshCoP::Tlv *steeringDataTlv;
MeshCoP::SteeringData steeringData;
Error error = kErrorNone;
const MeshCoP::SteeringDataTlv *steeringDataTlv;
MeshCoP::SteeringData steeringData;

steeringDataTlv = GetCommissioningDataSubTlv(MeshCoP::Tlv::kSteeringData);
steeringDataTlv = FindInCommissioningData<MeshCoP::SteeringDataTlv>();
VerifyOrExit(steeringDataTlv != nullptr, error = kErrorInvalidState);

As<MeshCoP::SteeringDataTlv>(steeringDataTlv)->CopyTo(steeringData);
steeringDataTlv->CopyTo(steeringData);

VerifyOrExit(steeringData.Contains(aFilterIndexes), error = kErrorNotFound);

Expand Down
Loading

0 comments on commit 2457ba7

Please sign in to comment.