1 /* 2 * Copyright (c) 2020, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for TMF functionality. 32 */ 33 34 #ifndef OT_CORE_THREAD_TMF_HPP_ 35 #define OT_CORE_THREAD_TMF_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include "coap/coap.hpp" 40 #include "common/locator.hpp" 41 42 namespace ot { 43 namespace Tmf { 44 45 constexpr uint16_t kUdpPort = 61631; ///< TMF UDP Port 46 47 typedef Coap::Message Message; ///< A TMF message. 48 49 /** 50 * This class represents message information for a TMF message. 51 * 52 * This is sub-class of `Ip6::MessageInfo` intended for use when sending TMF messages. 53 * 54 */ 55 class MessageInfo : public InstanceLocator, public Ip6::MessageInfo 56 { 57 public: 58 /** 59 * This constructor initializes the `MessageInfo`. 60 * 61 * The peer port is set to `Tmf::kUdpPort` and all other properties are cleared (set to zero). 62 * 63 * @param[in] aInstance The OpenThread instance. 64 * 65 */ MessageInfo(Instance & aInstance)66 explicit MessageInfo(Instance &aInstance) 67 : InstanceLocator(aInstance) 68 { 69 SetPeerPort(kUdpPort); 70 } 71 72 /** 73 * This method sets the local socket port to TMF port. 74 * 75 */ SetSockPortToTmf(void)76 void SetSockPortToTmf(void) { SetSockPort(kUdpPort); } 77 78 /** 79 * This method sets the local socket address to mesh-local RLOC address. 80 * 81 */ 82 void SetSockAddrToRloc(void); 83 84 /** 85 * This method sets the local socket address to RLOC address and the peer socket address to leader ALOC. 86 * 87 * @retval kErrorNone Successfully set the addresses. 88 * @retval kErrorDetached Cannot set leader ALOC since device is currently detached. 89 * 90 */ 91 Error SetSockAddrToRlocPeerAddrToLeaderAloc(void); 92 93 /** 94 * This method sets the local socket address to RLOC address and the peer socket address to leader RLOC. 95 * 96 * @retval kErrorNone Successfully set the addresses. 97 * @retval kErrorDetached Cannot set leader RLOC since device is currently detached. 98 * 99 */ 100 Error SetSockAddrToRlocPeerAddrToLeaderRloc(void); 101 102 /** 103 * This method sets the local socket address to RLOC address and the peer socket address to realm-local all 104 * routers multicast address. 105 * 106 */ 107 void SetSockAddrToRlocPeerAddrToRealmLocalAllRoutersMulticast(void); 108 109 /** 110 * This method sets the local socket address to RLOC address and the peer socket address to a router RLOC based on 111 * a given RLOC16. 112 * 113 * @param[in] aRloc16 The RLOC16 to use for peer address. 114 * 115 */ 116 void SetSockAddrToRlocPeerAddrTo(uint16_t aRloc16); 117 118 /** 119 * This method sets the local socket address to RLOC address and the peer socket address to a given address. 120 * 121 * @param[in] aPeerAddress The peer address. 122 * 123 */ 124 void SetSockAddrToRlocPeerAddrTo(const Ip6::Address &aPeerAddress); 125 }; 126 127 /** 128 * This class implements functionality of the Thread TMF agent. 129 * 130 */ 131 class Agent : public Coap::Coap 132 { 133 public: 134 /** 135 * This constructor initializes the object. 136 * 137 * @param[in] aInstance A reference to the OpenThread instance. 138 * 139 */ Agent(Instance & aInstance)140 explicit Agent(Instance &aInstance) 141 : Coap::Coap(aInstance) 142 { 143 SetInterceptor(&Filter, this); 144 } 145 146 /** 147 * This method starts the TMF agent. 148 * 149 * @retval kErrorNone Successfully started the CoAP service. 150 * @retval kErrorFailed Failed to start the TMF agent. 151 * 152 */ 153 Error Start(void); 154 155 /** 156 * This method indicates whether or not a message meets TMF addressing rules. 157 * 158 * A TMF message MUST comply with following rules: 159 * 160 * - The destination port is `Tmf::kUdpPort`. 161 * - Both source and destination addresses are Link-Local, or 162 * - Source is Mesh Local and then destination is Mesh Local or Link-Local Multicast or Realm-Local Multicast. 163 * 164 * @param[in] aSourceAddress Source IPv6 address. 165 * @param[in] aDestAddress Destination IPv6 address. 166 * @param[in] aDestPort Destination port number. 167 * 168 * @retval TRUE if TMF addressing rules are met. 169 * @retval FALSE if TMF addressing rules are not met. 170 * 171 */ 172 bool IsTmfMessage(const Ip6::Address &aSourceAddress, const Ip6::Address &aDestAddress, uint16_t aDestPort) const; 173 174 private: 175 static Error Filter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext); 176 }; 177 178 } // namespace Tmf 179 } // namespace ot 180 181 #endif // OT_CORE_THREAD_TMF_HPP_ 182