From b694b00951eeeb4986387de1179d55dd38643a87 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Tue, 18 Jun 2019 11:49:05 -0700 Subject: [PATCH] [tlvs] allow longer length to support forward compatibility (#3919) --- src/core/meshcop/commissioner.cpp | 10 +- src/core/meshcop/joiner.cpp | 3 +- src/core/meshcop/leader.cpp | 5 +- src/core/meshcop/meshcop_tlvs.hpp | 153 ++++++++++++-------- src/core/thread/mle.cpp | 11 +- src/core/thread/mle_tlvs.hpp | 70 +++++---- src/core/thread/network_diagnostic.cpp | 6 - src/core/thread/network_diagnostic_tlvs.hpp | 69 ++------- src/core/thread/thread_tlvs.hpp | 14 +- 9 files changed, 170 insertions(+), 171 deletions(-) diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 180ad4751..4c423ac86 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -224,9 +224,9 @@ otError Commissioner::SendCommissionerSet(void) } // set bloom filter - memcpy(dataset.mSteeringData.m8, steeringData.GetValue(), steeringData.GetLength()); - dataset.mSteeringData.mLength = steeringData.GetLength(); - dataset.mIsSteeringDataSet = true; + dataset.mSteeringData.mLength = steeringData.GetSteeringDataLength(); + memcpy(dataset.mSteeringData.m8, steeringData.GetValue(), dataset.mSteeringData.mLength); + dataset.mIsSteeringDataSet = true; SuccessOrExit(error = SendMgmtCommissionerSetRequest(dataset, NULL, 0)); @@ -946,9 +946,9 @@ void Commissioner::HandleJoinerFinalize(Coap::Message &aMessage, const Ip6::Mess if (Tlv::GetTlv(aMessage, Tlv::kProvisioningUrl, sizeof(provisioningUrl), provisioningUrl) == OT_ERROR_NONE) { - if (provisioningUrl.GetLength() != mProvisioningUrl.GetLength() || + if (provisioningUrl.GetProvisioningUrlLength() != mProvisioningUrl.GetProvisioningUrlLength() || memcmp(provisioningUrl.GetProvisioningUrl(), mProvisioningUrl.GetProvisioningUrl(), - provisioningUrl.GetLength()) != 0) + provisioningUrl.GetProvisioningUrlLength()) != 0) { state = StateTlv::kReject; } diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 708c538cf..17ba8d780 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -547,7 +547,6 @@ void Joiner::HandleJoinerEntrust(Coap::Message &aMessage, const Ip6::MessageInfo VerifyOrExit(extendedPanId.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kNetworkName, sizeof(networkName), networkName)); - VerifyOrExit(networkName.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp)); VerifyOrExit(activeTimestamp.IsValid(), error = OT_ERROR_PARSE); @@ -560,7 +559,7 @@ void Joiner::HandleJoinerEntrust(Coap::Message &aMessage, const Ip6::MessageInfo Get().SetMeshLocalPrefix(meshLocalPrefix.GetMeshLocalPrefix()); Get().SetExtendedPanId(extendedPanId.GetExtendedPanId()); - Get().SetNetworkName(networkName.GetNetworkName(), networkName.GetLength()); + Get().SetNetworkName(networkName.GetNetworkName(), networkName.GetNetworkNameLength()); otLogInfoMeshCoP("Joiner successful!"); diff --git a/src/core/meshcop/leader.cpp b/src/core/meshcop/leader.cpp index 293515737..fd5ba0e98 100644 --- a/src/core/meshcop/leader.cpp +++ b/src/core/meshcop/leader.cpp @@ -82,13 +82,12 @@ void Leader::HandlePetition(Coap::Message &aMessage, const Ip6::MessageInfo &aMe VerifyOrExit(Get().IsRoutingLocator(aMessageInfo.GetPeerAddr())); SuccessOrExit(Tlv::GetTlv(aMessage, Tlv::kCommissionerId, sizeof(commissionerId), commissionerId)); - VerifyOrExit(commissionerId.IsValid()); if (mTimer.IsRunning()) { - VerifyOrExit((commissionerId.GetLength() == mCommissionerId.GetLength()) && + VerifyOrExit((commissionerId.GetCommissionerIdLength() == mCommissionerId.GetCommissionerIdLength()) && (!strncmp(commissionerId.GetCommissionerId(), mCommissionerId.GetCommissionerId(), - commissionerId.GetLength()))); + commissionerId.GetCommissionerIdLength()))); ResignCommissioner(); } diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index 03c5a2550..9be06b1eb 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -306,7 +306,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the PAN ID value. @@ -353,7 +353,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Extended PAN ID value. @@ -400,7 +400,18 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return true; } + + /** + * This method returns the Network Name length. + * + * @returns The Network Name length. + * + */ + uint8_t GetNetworkNameLength(void) const + { + return GetLength() <= sizeof(mNetworkName) ? GetLength() : sizeof(mNetworkName); + } /** * This method returns the Network Name value. @@ -452,7 +463,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the PSKc value. @@ -499,7 +510,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Network Master Key value. @@ -546,7 +557,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Network Key Sequence value. @@ -593,7 +604,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Mesh Local Prefix value. @@ -641,19 +652,30 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return ((GetLength() != 0) && (GetLength() <= sizeof(*this) - sizeof(Tlv))); } + bool IsValid(void) const { return GetLength() > 0; } + + /** + * This method returns the Steering Data length. + * + * @returns The Steering Data length. + * + */ + uint8_t GetSteeringDataLength(void) const + { + return GetLength() <= sizeof(mSteeringData) ? GetLength() : sizeof(mSteeringData); + } /** * This method sets all bits in the Bloom Filter to zero. * */ - void Clear(void) { memset(mSteeringData, 0, GetLength()); } + void Clear(void) { memset(mSteeringData, 0, GetSteeringDataLength()); } /** * Ths method sets all bits in the Bloom Filter to one. * */ - void Set(void) { memset(mSteeringData, 0xff, GetLength()); } + void Set(void) { memset(mSteeringData, 0xff, GetSteeringDataLength()); } /** * Ths method indicates whether or not the SteeringData allows all Joiners. @@ -666,7 +688,7 @@ public: { bool rval = true; - for (uint8_t i = 0; i < GetLength(); i++) + for (uint8_t i = 0; i < GetSteeringDataLength(); i++) { if (mSteeringData[i] != 0xff) { @@ -684,7 +706,7 @@ public: * @returns The number of bits in the Bloom Filter. * */ - uint8_t GetNumBits(void) const { return GetLength() * 8; } + uint8_t GetNumBits(void) const { return GetSteeringDataLength() * 8; } /** * This method indicates whether or not bit @p aBit is set. @@ -695,7 +717,10 @@ public: * @retval FALSE If bit @p aBit is not set. * */ - bool GetBit(uint8_t aBit) const { return (mSteeringData[GetLength() - 1 - (aBit / 8)] & (1 << (aBit % 8))) != 0; } + bool GetBit(uint8_t aBit) const + { + return (mSteeringData[GetSteeringDataLength() - 1 - (aBit / 8)] & (1 << (aBit % 8))) != 0; + } /** * This method clears bit @p aBit. @@ -703,7 +728,7 @@ public: * @param[in] aBit The bit offset. * */ - void ClearBit(uint8_t aBit) { mSteeringData[GetLength() - 1 - (aBit / 8)] &= ~(1 << (aBit % 8)); } + void ClearBit(uint8_t aBit) { mSteeringData[GetSteeringDataLength() - 1 - (aBit / 8)] &= ~(1 << (aBit % 8)); } /** * This method sets bit @p aBit. @@ -711,7 +736,7 @@ public: * @param[in] aBit The bit offset. * */ - void SetBit(uint8_t aBit) { mSteeringData[GetLength() - 1 - (aBit / 8)] |= 1 << (aBit % 8); } + void SetBit(uint8_t aBit) { mSteeringData[GetSteeringDataLength() - 1 - (aBit / 8)] |= 1 << (aBit % 8); } /** * Ths method indicates whether or not the SteeringData is all zeros. @@ -759,7 +784,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Border Agent Locator value. @@ -800,13 +825,15 @@ public: } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method returns the Commissioner ID length. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns The Commissioner ID length. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + uint8_t GetCommissionerIdLength(void) const + { + return GetLength() <= sizeof(mCommissionerId) ? GetLength() : sizeof(mCommissionerId); + } /** * This method returns the Commissioner ID value. @@ -863,7 +890,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Commissioner Session ID value. @@ -910,7 +937,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Rotation Time value. @@ -984,7 +1011,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } } OT_TOOL_PACKED_END; /** @@ -1012,7 +1039,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the UDP Port value. @@ -1059,7 +1086,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * State values. @@ -1117,7 +1144,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the UDP Port value. @@ -1164,7 +1191,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns a pointer to the Joiner IID. @@ -1211,7 +1238,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Joiner Router Locator value. @@ -1258,7 +1285,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns a pointer to the Joiner Router KEK. @@ -1306,7 +1333,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } } OT_TOOL_PACKED_END; /** @@ -1334,7 +1361,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Delay Timer value. @@ -1657,7 +1684,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Count value. @@ -1704,7 +1731,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Period value. @@ -1751,7 +1778,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Scan Duration value. @@ -1824,14 +1851,16 @@ public: SetLength(0); } - /** - * This method indicates whether or not the TLV appears to be well-formed. + /* + * This method returns the Provisioning URL length. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns The Provisioning URL length. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + uint8_t GetProvisioningUrlLength(void) const + { + return GetLength() <= sizeof(mProvisioningUrl) ? GetLength() : sizeof(mProvisioningUrl); + } /** * This method returns the Provisioning URL value. @@ -1882,13 +1911,15 @@ public: } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method returns the Vendor Name length. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns The Vendor Name length. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + uint8_t GetVendorNameLength(void) const + { + return GetLength() <= sizeof(mVendorName) ? GetLength() : sizeof(mVendorName); + } /** * This method returns the Vendor Name value. @@ -1944,13 +1975,15 @@ public: } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method returns the Vendor Model length. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns The Vendor Model length. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + uint8_t GetVendorModelLength(void) const + { + return GetLength() <= sizeof(mVendorModel) ? GetLength() : sizeof(mVendorModel); + } /** * This method returns the Vendor Model value. @@ -2006,13 +2039,15 @@ public: } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method returns the Vendor SW Version length. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns The Vendor SW Version length. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + uint8_t GetVendorSwVersionLength(void) const + { + return GetLength() <= sizeof(mVendorSwVersion) ? GetLength() : sizeof(mVendorSwVersion); + } /** * This method returns the Vendor SW Version value. @@ -2068,13 +2103,15 @@ public: } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method returns the Vendor Data length. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns The Vendor Data length. * */ - bool IsValid(void) const { return GetLength() <= sizeof(*this) - sizeof(Tlv); } + uint8_t GetVendorDataLength(void) const + { + return GetLength() <= sizeof(mVendorData) ? GetLength() : sizeof(mVendorData); + } /** * This method returns the Vendor Data value. @@ -2146,7 +2183,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Stack Vendor OUI value. @@ -2417,7 +2454,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Version value. @@ -2504,7 +2541,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Version value. diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index ca3ff80fa..348e27986 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3349,8 +3349,8 @@ otError Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInf // Challenge SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kChallenge, sizeof(challenge), challenge)); VerifyOrExit(challenge.IsValid(), error = OT_ERROR_PARSE); - memcpy(mChildIdRequest.mChallenge, challenge.GetChallenge(), challenge.GetLength()); - mChildIdRequest.mChallengeLength = challenge.GetLength(); + mChildIdRequest.mChallengeLength = challenge.GetChallengeLength(); + memcpy(mChildIdRequest.mChallenge, challenge.GetChallenge(), mChildIdRequest.mChallengeLength); mParentCandidate.SetExtAddress(extAddress); mParentCandidate.SetRloc16(sourceAddress.GetRloc16()); @@ -3828,9 +3828,8 @@ otError Mle::HandleDiscoveryResponse(const Message &aMessage, const Ip6::Message case MeshCoP::Tlv::kNetworkName: aMessage.Read(offset, sizeof(networkName), &networkName); - VerifyOrExit(networkName.IsValid(), error = OT_ERROR_PARSE); - memcpy(&result.mNetworkName, networkName.GetNetworkName(), networkName.GetLength()); - result.mNetworkName.m8[networkName.GetLength()] = '\0'; + memcpy(&result.mNetworkName, networkName.GetNetworkName(), networkName.GetNetworkNameLength()); + result.mNetworkName.m8[networkName.GetNetworkNameLength()] = '\0'; break; case MeshCoP::Tlv::kSteeringData: @@ -3844,7 +3843,7 @@ otError Mle::HandleDiscoveryResponse(const Message &aMessage, const Ip6::Message } didCheckSteeringData = true; - result.mSteeringData.mLength = steeringData.GetLength(); + result.mSteeringData.mLength = steeringData.GetSteeringDataLength(); memcpy(result.mSteeringData.m8, steeringData.GetValue(), result.mSteeringData.mLength); break; diff --git a/src/core/thread/mle_tlvs.hpp b/src/core/thread/mle_tlvs.hpp index 4e6ddb157..d914a5c80 100644 --- a/src/core/thread/mle_tlvs.hpp +++ b/src/core/thread/mle_tlvs.hpp @@ -196,7 +196,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the RLOC16 value. @@ -243,7 +243,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } enum { @@ -298,7 +298,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Timeout value. @@ -330,6 +330,7 @@ class ChallengeTlv : public Tlv public: enum { + kMinSize = 4, ///< Minimum size in bytes (Thread Specification). kMaxSize = 8, ///< Maximum size in bytes (Thread Specification). }; @@ -350,7 +351,18 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() >= 4 && GetLength() <= 8; } + bool IsValid(void) const { return GetLength() >= kMinSize; } + + /** + * This method returns the Challenge length. + * + * @returns The Challenge length. + * + */ + uint8_t GetChallengeLength(void) const + { + return GetLength() <= sizeof(mChallenge) ? GetLength() : sizeof(mChallenge); + } /** * This method returns a pointer to the Challenge value. @@ -366,7 +378,7 @@ public: * @param[in] aChallenge A pointer to the Challenge value. * */ - void SetChallenge(const uint8_t *aChallenge) { memcpy(mChallenge, aChallenge, GetLength()); } + void SetChallenge(const uint8_t *aChallenge) { memcpy(mChallenge, aChallenge, GetChallengeLength()); } private: uint8_t mChallenge[kMaxSize]; @@ -382,6 +394,7 @@ class ResponseTlv : public Tlv public: enum { + kMinSize = 4, ///< Minimum size in bytes (Thread Specification). kMaxSize = 8, ///< Maximum size in bytes (Thread Specification). }; @@ -398,11 +411,14 @@ public: /** * This method indicates whether or not the TLV appears to be well-formed. * + * OpenThread only generates Challenge values with 8-byte length. As a result, a Response value lengths must also + * have 8-byte length. + * * @retval TRUE If the TLV appears to be well-formed. * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns a pointer to the Response value. @@ -449,7 +465,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Frame Counter value. @@ -498,11 +514,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const - { - return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask) && - GetLength() <= sizeof(*this) - sizeof(Tlv); - } + bool IsValid(void) const { return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask); } /** * This method returns the Router ID Sequence value. @@ -674,11 +686,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const - { - return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask) && - GetLength() <= sizeof(*this) - sizeof(Tlv); - } + bool IsValid(void) const { return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask); } /** * This method returns the Router ID Sequence value. @@ -888,7 +896,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Frame Counter value. @@ -935,7 +943,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the RLOC16 value. @@ -982,7 +990,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Partition ID value. @@ -1207,7 +1215,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } enum { @@ -1518,7 +1526,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Link Margin value. @@ -1565,7 +1573,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * Status values. @@ -1620,7 +1628,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Version value. @@ -1762,7 +1770,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the Channel Page value. @@ -1826,7 +1834,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the PAN ID value. @@ -1874,7 +1882,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } } OT_TOOL_PACKED_END; /** @@ -1902,7 +1910,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the time sync period. @@ -1966,7 +1974,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } /** * This method returns the XTAL accuracy. @@ -2015,7 +2023,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } } OT_TOOL_PACKED_END; /** @@ -2044,7 +2052,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(Tlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } } OT_TOOL_PACKED_END; /** diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index eae830086..23e106b01 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -481,8 +481,6 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Message &aMessage, const VerifyOrExit(networkDiagnosticTlv.GetType() == NetworkDiagnosticTlv::kTypeList, error = OT_ERROR_PARSE); - VerifyOrExit((static_cast(&networkDiagnosticTlv)->IsValid()), error = OT_ERROR_PARSE); - // DIAG_GET.qry may be sent as a confirmable message. if (aMessage.GetType() == OT_COAP_TYPE_CONFIRMABLE) { @@ -553,8 +551,6 @@ void NetworkDiagnostic::HandleDiagnosticGetRequest(Coap::Message &aMessage, cons VerifyOrExit(networkDiagnosticTlv.GetType() == NetworkDiagnosticTlv::kTypeList, error = OT_ERROR_PARSE); - VerifyOrExit((static_cast(&networkDiagnosticTlv)->IsValid()), error = OT_ERROR_PARSE); - VerifyOrExit((message = Get().NewMessage()) != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aMessage)); @@ -647,8 +643,6 @@ void NetworkDiagnostic::HandleDiagnosticReset(Coap::Message &aMessage, const Ip6 VerifyOrExit(networkDiagnosticTlv.GetType() == NetworkDiagnosticTlv::kTypeList); - VerifyOrExit((static_cast(&networkDiagnosticTlv)->IsValid())); - offset = aMessage.GetOffset() + sizeof(NetworkDiagnosticTlv); for (uint8_t i = 0; i < networkDiagnosticTlv.GetLength(); i++) diff --git a/src/core/thread/network_diagnostic_tlvs.hpp b/src/core/thread/network_diagnostic_tlvs.hpp index c98afbede..79723ef66 100644 --- a/src/core/thread/network_diagnostic_tlvs.hpp +++ b/src/core/thread/network_diagnostic_tlvs.hpp @@ -179,7 +179,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns a pointer to the Extended MAC Address. @@ -226,7 +226,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the RLOC16 value. @@ -273,7 +273,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } enum { @@ -328,7 +328,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the Timeout value. @@ -377,9 +377,9 @@ public: */ bool IsValid(void) const { - return (GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv) || - GetLength() == - sizeof(*this) - sizeof(NetworkDiagnosticTlv) - sizeof(mSedBufferSize) - sizeof(mSedDatagramCount)); + return ((GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv)) || + (GetLength() == + sizeof(*this) - sizeof(NetworkDiagnosticTlv) - sizeof(mSedBufferSize) - sizeof(mSedDatagramCount))); } /** @@ -572,11 +572,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const - { - return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask) && - GetLength() <= sizeof(*this) - sizeof(NetworkDiagnosticTlv); - } + bool IsValid(void) const { return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask); } /** * This method returns the Router ID Sequence value. @@ -746,7 +742,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the Partition ID value. @@ -891,11 +887,6 @@ OT_TOOL_PACKED_BEGIN class Ip6AddressListTlv : public NetworkDiagnosticTlv { public: - enum - { - kMaxSize = 8, ///< Maximum size in bytes (Thread Specification). - }; - /** * This method initializes the TLV. * @@ -906,15 +897,6 @@ public: SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); } - /** - * This method indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() <= sizeof(Ip6::Address) * kMaxSize; } - /** * This method returns a pointer to the IPv6 address entry. * @@ -955,7 +937,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the IfInUnknownProtos counter. @@ -1146,7 +1128,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the Status value. @@ -1193,7 +1175,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the Status value. @@ -1351,7 +1333,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return (GetLength() % sizeof(ChildTableEntry)) == 0; } /** * This method returns the number of Child Table entries. @@ -1394,14 +1376,11 @@ public: } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method returns a pointer to the list of Channel Pages. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @returns A pointer to the list of Channel Pages. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } - uint8_t *GetChannelPages(void) { return mChannelPages; } private: @@ -1425,22 +1404,6 @@ public: SetType(kTypeList); SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); } - - /** - * This method indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() <= OT_NETWORK_DIAGNOSTIC_TYPELIST_MAX_ENTRIES; } - - /** - * This method returns a pointer to the Challenge value. - * - * @returns A pointer to the Challenge value. - * - */ } OT_TOOL_PACKED_END; /** @@ -1468,7 +1431,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } /** * This method returns the Timeout value. diff --git a/src/core/thread/thread_tlvs.hpp b/src/core/thread/thread_tlvs.hpp index 642406fee..a4c795550 100644 --- a/src/core/thread/thread_tlvs.hpp +++ b/src/core/thread/thread_tlvs.hpp @@ -138,7 +138,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * This method returns a reference to the Target EID. @@ -185,7 +185,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * This method returns a reference to the Extended MAC Address. @@ -232,7 +232,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * This method returns the RLOC16 value. @@ -279,7 +279,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * This method returns a pointer to the ML-EID IID. @@ -338,7 +338,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * Status values. @@ -398,7 +398,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * This method returns the Last Transaction Time value. @@ -444,7 +444,7 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(ThreadTlv); } + bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(ThreadTlv); } /** * This method returns the ID Sequence value.