From bcb613b7307e27e6b23e83416b3c8b78d32c7d7f Mon Sep 17 00:00:00 2001 From: rongli Date: Wed, 24 May 2017 23:34:03 +0800 Subject: [PATCH] Update TMF rules to reflect updates in SPEC-719 (#1807) * Update TMF rules to reflect updates in SPEC-719 * update for comments * rebase and resolve conflicts * fix typo * Limit allowable TMF multicast address to be link local or realm local. * update involved files to fit in 120 columns * update for comments --- src/core/coap/coap.cpp | 3 ++- src/core/coap/coap.hpp | 10 +++++++--- src/core/thread/mle.cpp | 8 +++++++- src/core/thread/mle.hpp | 15 +++++++++++++-- src/core/thread/thread_netif.cpp | 22 +++++++++++----------- src/core/thread/thread_netif.hpp | 2 +- 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index d9b404139..eadeec077 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -58,6 +58,7 @@ Coap::Coap(ThreadNetif &aNetif): mSocket(aNetif.GetIp6().mUdp), mRetransmissionTimer(aNetif.GetIp6().mTimerScheduler, &Coap::HandleRetransmissionTimer, this), mResources(NULL), + mContext(NULL), mInterceptor(NULL), mResponsesQueue(aNetif), mDefaultHandler(NULL), @@ -640,7 +641,7 @@ void Coap::ProcessReceivedRequest(Header &aHeader, Message &aMessage, const Ip6: if (mInterceptor != NULL) { - SuccessOrExit(error = mInterceptor(aMessage, aMessageInfo)); + SuccessOrExit(error = mInterceptor(aMessage, aMessageInfo, mContext)); } aMessage.MoveOffset(aHeader.GetLength()); diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 3e0956d34..5abae1f7c 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -427,6 +427,7 @@ public: * * @param[in] aMessage A reference to the message. @ @param[in] aMessageInfo A reference to the message info associated with @p aMessage. + * @param[in] aContext A pointer to arbitrary context information. * * @retval OT_ERROR_NONE Server should continue processing this message, other * return values indicates the server should stop processing @@ -434,7 +435,7 @@ public: * @retval OT_ERROR_NOT_TMF The message is not a TMF message. * */ - typedef otError(* Interceptor)(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo); + typedef otError(* Interceptor)(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext); /** * This constructor initializes the object. @@ -617,10 +618,12 @@ public: * This method sets interceptor to be called before processing a CoAP packet. * * @param[in] aInterceptor A pointer to the interceptor. + * @param[in] aContext A pointer to arbitrary context information. * */ - void SetInterceptor(Interceptor aInterpreter) { - mInterceptor = aInterpreter; + void SetInterceptor(Interceptor aInterceptor, void *aContext) { + mInterceptor = aInterceptor; + mContext = aContext; } /** @@ -692,6 +695,7 @@ private: Resource *mResources; + void *mContext; Interceptor mInterceptor; ResponsesQueue mResponsesQueue; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 929c28796..edf638461 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3233,7 +3233,13 @@ bool Mle::IsRoutingLocator(const Ip6::Address &aAddress) const bool Mle::IsAnycastLocator(const Ip6::Address &aAddress) const { - return memcmp(&mMeshLocal16, &aAddress, kRlocPrefixLength) == 0 && aAddress.mFields.m8[14] == Ip6::Address::kAloc16Mask; + return memcmp(&mMeshLocal16, &aAddress, kRlocPrefixLength) == 0 && + aAddress.mFields.m8[14] == Ip6::Address::kAloc16Mask; +} + +bool Mle::IsMeshLocalAddress(const Ip6::Address &aAddress) const +{ + return aAddress.PrefixMatch(GetMeshLocal16()) >= Ip6::Address::kMeshLocalPrefixLength; } Router *Mle::GetParent(void) diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 2bde7bc8b..32b4ab6b8 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -539,7 +539,8 @@ public: * @param[in] aScanChannels A bit vector indicating which channels to scan. * @param[in] aPanId The PAN ID filter (set to Broadcast PAN to disable filter). * @param[in] aJoiner Value of the Joiner Flag in the Discovery Request TLV. - * @param[in] aEnableEui64Filtering Enable filtering out MLE discovery responses that don't match our factory assigned EUI64. + * @param[in] aEnableEui64Filtering Enable filtering out MLE discovery responses that don't match our factory + * assigned EUI64. * @param[in] aHandler A pointer to a function that is called on receiving an MLE Discovery Response. * @param[in] aContext A pointer to arbitrary context information. * @@ -702,6 +703,15 @@ public: */ bool IsAnycastLocator(const Ip6::Address &aAddress) const; + /** + * This method indicates whether or not an IPv6 address is a Mesh Local Address. + * + * @retval TRUE If @p aAddress is a Mesh Local Address. + * @retval FALSE If @p aAddress is not a Mesh Local Address. + * + */ + bool IsMeshLocalAddress(const Ip6::Address &aAddress) const; + /** * This method returns the MLE Timeout value. * @@ -1097,7 +1107,8 @@ protected: * This method appends a Active Timestamp TLV to a message. * * @param[in] aMessage A reference to the message. - * @param[in] aCouldUseLocal True to use local Active Timestamp when network Active Timestamp is not available, False not. + * @param[in] aCouldUseLocal True to use local Active Timestamp when network Active Timestamp is not available, + * False not. * * @retval OT_ERROR_NONE Successfully appended the Active Timestamp TLV. * @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Active Timestamp TLV. diff --git a/src/core/thread/thread_netif.cpp b/src/core/thread/thread_netif.cpp index e84eb974b..e698d2d44 100644 --- a/src/core/thread/thread_netif.cpp +++ b/src/core/thread/thread_netif.cpp @@ -116,7 +116,7 @@ ThreadNetif::ThreadNetif(Ip6::Ip6 &aIp6): { mKeyManager.SetMasterKey(kThreadMasterKey); - mCoap.SetInterceptor(&ThreadNetif::TmfFilter); + mCoap.SetInterceptor(&ThreadNetif::TmfFilter, this); } otError ThreadNetif::Up(void) @@ -182,19 +182,19 @@ exit: return error; } -otError ThreadNetif::TmfFilter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo) +otError ThreadNetif::TmfFilter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext) { otError error = OT_ERROR_NONE; - // A TMF message must comply at least one of the following rules: - // 1. The IPv6 source address is RLOC or ALOC. - // 2. The IPv6 destination address is RLOC or ALOC. - // 3. The IPv6 destination address is Link-Local address. - VerifyOrExit(aMessageInfo.GetPeerAddr().IsRoutingLocator() || - aMessageInfo.GetPeerAddr().IsAnycastRoutingLocator() || - aMessageInfo.GetSockAddr().IsRoutingLocator() || - aMessageInfo.GetSockAddr().IsAnycastRoutingLocator() || - aMessageInfo.GetSockAddr().IsLinkLocal(), + // A TMF message must comply with following rules: + // 1. The destination is a Mesh Local Address or a Link-Local Multicast Address or a Realm-Local Multicast Address, + // and the source is a Mesh Local Address. + // 2. Both the destination and the source are Link-Local Addresses. + VerifyOrExit(((static_cast(aContext)->mMleRouter.IsMeshLocalAddress(aMessageInfo.GetSockAddr()) || + aMessageInfo.GetSockAddr().IsLinkLocalMulticast() || + aMessageInfo.GetSockAddr().IsRealmLocalMulticast()) && + static_cast(aContext)->mMleRouter.IsMeshLocalAddress(aMessageInfo.GetPeerAddr())) || + (aMessageInfo.GetSockAddr().IsLinkLocal() && aMessageInfo.GetPeerAddr().IsLinkLocal()), error = OT_ERROR_NOT_TMF); exit: (void)aMessage; diff --git a/src/core/thread/thread_netif.hpp b/src/core/thread/thread_netif.hpp index 7e6e3aef0..b29e4d8fb 100644 --- a/src/core/thread/thread_netif.hpp +++ b/src/core/thread/thread_netif.hpp @@ -407,7 +407,7 @@ public: otInstance *GetInstance(void); private: - static otError TmfFilter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo); + static otError TmfFilter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext); Coap::Coap mCoap; #if OPENTHREAD_ENABLE_DHCP6_CLIENT