• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "coap/coap_secure.hpp"
41 #include "common/locator.hpp"
42 
43 namespace ot {
44 namespace Tmf {
45 
46 /**
47  * Declares a TMF handler (a full template specialization of `HandleTmf<Uri>` method) in a given `Type`.
48  *
49  * The class `Type` MUST declare a template method of the following format:
50  *
51  *  template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
52  *
53  * @param[in] Type      The `Type` in which the TMF handler is declared.
54  * @param[in] kUri      The `Uri` which is handled.
55  */
56 #define DeclareTmfHandler(Type, kUri) \
57     template <> void Type::HandleTmf<kUri>(Coap::Message & aMessage, const Ip6::MessageInfo &aMessageInfo)
58 
59 constexpr uint16_t kUdpPort = 61631; ///< TMF UDP Port
60 
61 typedef Coap::Message Message; ///< A TMF message.
62 
63 /**
64  * Represents message information for a TMF message.
65  *
66  * This is sub-class of `Ip6::MessageInfo` intended for use when sending TMF messages.
67  */
68 class MessageInfo : public InstanceLocator, public Ip6::MessageInfo
69 {
70 public:
71     /**
72      * Initializes the `MessageInfo`.
73      *
74      * The peer port is set to `Tmf::kUdpPort` and all other properties are cleared (set to zero).
75      *
76      * @param[in] aInstance    The OpenThread instance.
77      */
MessageInfo(Instance & aInstance)78     explicit MessageInfo(Instance &aInstance)
79         : InstanceLocator(aInstance)
80     {
81         SetPeerPort(kUdpPort);
82     }
83 
84     /**
85      * Sets the local socket port to TMF port.
86      */
SetSockPortToTmf(void)87     void SetSockPortToTmf(void) { SetSockPort(kUdpPort); }
88 
89     /**
90      * Sets the local socket address to mesh-local RLOC address.
91      */
92     void SetSockAddrToRloc(void);
93 
94     /**
95      * Sets the local socket address to RLOC address and the peer socket address to leader ALOC.
96      */
97     void SetSockAddrToRlocPeerAddrToLeaderAloc(void);
98 
99     /**
100      * Sets the local socket address to RLOC address and the peer socket address to leader RLOC.
101      */
102     void SetSockAddrToRlocPeerAddrToLeaderRloc(void);
103 
104     /**
105      * Sets the local socket address to RLOC address and the peer socket address to realm-local all
106      * routers multicast address.
107      */
108     void SetSockAddrToRlocPeerAddrToRealmLocalAllRoutersMulticast(void);
109 
110     /**
111      * Sets the local socket address to RLOC address and the peer socket address to a router RLOC based on
112      * a given RLOC16.
113      *
114      * @param[in] aRloc16     The RLOC16 to use for peer address.
115      */
116     void SetSockAddrToRlocPeerAddrTo(uint16_t aRloc16);
117 
118     /**
119      * 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     void SetSockAddrToRlocPeerAddrTo(const Ip6::Address &aPeerAddress);
124 };
125 
126 /**
127  * Implements functionality of the Thread TMF agent.
128  */
129 class Agent : public Coap::Coap
130 {
131 public:
132     /**
133      * Initializes the object.
134      *
135      * @param[in] aInstance      A reference to the OpenThread instance.
136      */
137     explicit Agent(Instance &aInstance);
138 
139     /**
140      * Starts the TMF agent.
141      *
142      * @retval kErrorNone    Successfully started the CoAP service.
143      * @retval kErrorFailed  Failed to start the TMF agent.
144      */
145     Error Start(void);
146 
147     /**
148      * Indicates whether or not a message meets TMF addressing rules.
149      *
150      * A TMF message MUST comply with following rules:
151      *
152      * - The destination port is `Tmf::kUdpPort`.
153      * - Both source and destination addresses are Link-Local, or
154      * - Source is Mesh Local and then destination is Mesh Local or Link-Local Multicast or Realm-Local Multicast.
155      *
156      * @param[in] aSourceAddress   Source IPv6 address.
157      * @param[in] aDestAddress     Destination IPv6 address.
158      * @param[in] aDestPort        Destination port number.
159      *
160      * @retval TRUE   if TMF addressing rules are met.
161      * @retval FALSE  if TMF addressing rules are not met.
162      */
163     bool IsTmfMessage(const Ip6::Address &aSourceAddress, const Ip6::Address &aDestAddress, uint16_t aDestPort) const;
164 
165     /**
166      * Converts a TMF message priority to IPv6 header DSCP value.
167      *
168      * @param[in] aPriority  The message priority to convert.
169      *
170      * @returns The DSCP value corresponding to @p aPriority.
171      */
172     static uint8_t PriorityToDscp(Message::Priority aPriority);
173 
174     /**
175      * Converts a IPv6 header DSCP value to message priority for TMF message.
176      *
177      * @param[in] aDscp      The IPv6 header DSCP value in a TMF message.
178      *
179      * @returns The message priority corresponding to the @p aDscp.
180      */
181     static Message::Priority DscpToPriority(uint8_t aDscp);
182 
183 private:
184     template <Uri kUri> void HandleTmf(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
185 
186     static bool HandleResource(CoapBase               &aCoapBase,
187                                const char             *aUriPath,
188                                Message                &aMessage,
189                                const Ip6::MessageInfo &aMessageInfo);
190     bool        HandleResource(const char *aUriPath, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
191 
192     static Error Filter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext);
193 };
194 
195 #if OPENTHREAD_CONFIG_SECURE_TRANSPORT_ENABLE
196 
197 /**
198  * Implements functionality of the secure TMF agent.
199  */
200 class SecureAgent : public Coap::Dtls::Transport, public Coap::SecureSession
201 {
202 public:
203     /**
204      * Initializes the object.
205      *
206      * @param[in] aInstance      A reference to the OpenThread instance.
207      */
208     explicit SecureAgent(Instance &aInstance);
209 
210 private:
211     static MeshCoP::SecureSession *HandleDtlsAccept(void *aContext, const Ip6::MessageInfo &aMessageInfo);
212     Coap::SecureSession           *HandleDtlsAccept(void);
213 
214 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
215     static bool HandleResource(CoapBase               &aCoapBase,
216                                const char             *aUriPath,
217                                Message                &aMessage,
218                                const Ip6::MessageInfo &aMessageInfo);
219     bool        HandleResource(const char *aUriPath, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
220 #endif
221 };
222 
223 #endif
224 
225 } // namespace Tmf
226 } // namespace ot
227 
228 #endif //  OT_CORE_THREAD_TMF_HPP_
229