diff --git a/src/core/thread/tmf.cpp b/src/core/thread/tmf.cpp index d9547d669..24a4ae9a8 100644 --- a/src/core/thread/tmf.cpp +++ b/src/core/thread/tmf.cpp @@ -89,26 +89,32 @@ Error Agent::Filter(const Message &aMessage, const Ip6::MessageInfo &aMessageInf { OT_UNUSED_VARIABLE(aMessage); - return static_cast(aContext)->IsTmfMessage(aMessageInfo) ? kErrorNone : kErrorNotTmf; + return static_cast(aContext)->IsTmfMessage(aMessageInfo.GetPeerAddr(), aMessageInfo.GetSockAddr(), + aMessageInfo.GetSockPort()) + ? kErrorNone + : kErrorNotTmf; } -bool Agent::IsTmfMessage(const Ip6::MessageInfo &aMessageInfo) const +bool Agent::IsTmfMessage(const Ip6::Address &aSourceAddress, const Ip6::Address &aDestAddress, uint16_t aDestPort) const { - bool rval = true; + bool isTmf = false; + + VerifyOrExit(aDestPort == kUdpPort); + + if (aSourceAddress.IsLinkLocal()) + { + isTmf = aDestAddress.IsLinkLocal() || aDestAddress.IsLinkLocalMulticast(); + ExitNow(); + } + + VerifyOrExit(Get().IsMeshLocalAddress(aSourceAddress)); + VerifyOrExit(Get().IsMeshLocalAddress(aDestAddress) || aDestAddress.IsLinkLocalMulticast() || + aDestAddress.IsRealmLocalMulticast()); + + isTmf = true; - // 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. Or - // 2. Both the destination and the source are Link-Local Addresses. - VerifyOrExit( - ((Get().IsMeshLocalAddress(aMessageInfo.GetSockAddr()) || - aMessageInfo.GetSockAddr().IsLinkLocalMulticast() || aMessageInfo.GetSockAddr().IsRealmLocalMulticast()) && - Get().IsMeshLocalAddress(aMessageInfo.GetPeerAddr())) || - ((aMessageInfo.GetSockAddr().IsLinkLocal() || aMessageInfo.GetSockAddr().IsLinkLocalMulticast()) && - aMessageInfo.GetPeerAddr().IsLinkLocal()), - rval = false); exit: - return rval; + return isTmf; } } // namespace Tmf diff --git a/src/core/thread/tmf.hpp b/src/core/thread/tmf.hpp index dc4e286e8..39e0185cd 100644 --- a/src/core/thread/tmf.hpp +++ b/src/core/thread/tmf.hpp @@ -153,13 +153,23 @@ public: Error Start(void); /** - * This method returns whether Thread Management Framework Addressing Rules are met. + * This method indicates whether or not a message meets TMF addressing rules. * - * @retval TRUE if Thread Management Framework Addressing Rules are met. - * @retval FALSE if Thread Management Framework Addressing Rules are not met. + * A TMF message MUST comply with following rules: + * + * - The destination port is `Tmf::kUdpPort`. + * - Both source and destination addresses are Link-Local, or + * - Source is Mesh Local and then destination is Mesh Local or Link-Local Multicast or Realm-Local Multicast. + * + * @param[in] aSourceAddress Source IPv6 address. + * @param[in] aDestAddress Destination IPv6 address. + * @param[in] aDestPort Destination port number. + * + * @retval TRUE if TMF addressing rules are met. + * @retval FALSE if TMF addressing rules are not met. * */ - bool IsTmfMessage(const Ip6::MessageInfo &aMessageInfo) const; + bool IsTmfMessage(const Ip6::Address &aSourceAddress, const Ip6::Address &aDestAddress, uint16_t aDestPort) const; private: static Error Filter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext);