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 implements Thread Management Framework (TMF) functionalities.
32 */
33
34 #include "thread/tmf.hpp"
35
36 #include "common/locator_getters.hpp"
37
38 namespace ot {
39 namespace Tmf {
40
41 //----------------------------------------------------------------------------------------------------------------------
42 // MessageInfo
43
SetSockAddrToRloc(void)44 void MessageInfo::SetSockAddrToRloc(void)
45 {
46 SetSockAddr(Get<Mle::MleRouter>().GetMeshLocal16());
47 }
48
SetSockAddrToRlocPeerAddrToLeaderAloc(void)49 Error MessageInfo::SetSockAddrToRlocPeerAddrToLeaderAloc(void)
50 {
51 SetSockAddrToRloc();
52 return Get<Mle::MleRouter>().GetLeaderAloc(GetPeerAddr());
53 }
54
SetSockAddrToRlocPeerAddrToLeaderRloc(void)55 Error MessageInfo::SetSockAddrToRlocPeerAddrToLeaderRloc(void)
56 {
57 SetSockAddrToRloc();
58 return Get<Mle::MleRouter>().GetLeaderAddress(GetPeerAddr());
59 }
60
SetSockAddrToRlocPeerAddrToRealmLocalAllRoutersMulticast(void)61 void MessageInfo::SetSockAddrToRlocPeerAddrToRealmLocalAllRoutersMulticast(void)
62 {
63 SetSockAddrToRloc();
64 GetPeerAddr().SetToRealmLocalAllRoutersMulticast();
65 }
66
SetSockAddrToRlocPeerAddrTo(uint16_t aRloc16)67 void MessageInfo::SetSockAddrToRlocPeerAddrTo(uint16_t aRloc16)
68 {
69 SetSockAddrToRloc();
70 SetPeerAddr(Get<Mle::MleRouter>().GetMeshLocal16());
71 GetPeerAddr().GetIid().SetLocator(aRloc16);
72 }
73
SetSockAddrToRlocPeerAddrTo(const Ip6::Address & aPeerAddress)74 void MessageInfo::SetSockAddrToRlocPeerAddrTo(const Ip6::Address &aPeerAddress)
75 {
76 SetSockAddrToRloc();
77 SetPeerAddr(aPeerAddress);
78 }
79
80 //----------------------------------------------------------------------------------------------------------------------
81 // Agent
82
Start(void)83 Error Agent::Start(void)
84 {
85 return Coap::Start(kUdpPort, OT_NETIF_THREAD);
86 }
87
Filter(const Message & aMessage,const Ip6::MessageInfo & aMessageInfo,void * aContext)88 Error Agent::Filter(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext)
89 {
90 OT_UNUSED_VARIABLE(aMessage);
91
92 return static_cast<Agent *>(aContext)->IsTmfMessage(aMessageInfo.GetPeerAddr(), aMessageInfo.GetSockAddr(),
93 aMessageInfo.GetSockPort())
94 ? kErrorNone
95 : kErrorNotTmf;
96 }
97
IsTmfMessage(const Ip6::Address & aSourceAddress,const Ip6::Address & aDestAddress,uint16_t aDestPort) const98 bool Agent::IsTmfMessage(const Ip6::Address &aSourceAddress, const Ip6::Address &aDestAddress, uint16_t aDestPort) const
99 {
100 bool isTmf = false;
101
102 VerifyOrExit(aDestPort == kUdpPort);
103
104 if (aSourceAddress.IsLinkLocal())
105 {
106 isTmf = aDestAddress.IsLinkLocal() || aDestAddress.IsLinkLocalMulticast();
107 ExitNow();
108 }
109
110 VerifyOrExit(Get<Mle::Mle>().IsMeshLocalAddress(aSourceAddress));
111 VerifyOrExit(Get<Mle::Mle>().IsMeshLocalAddress(aDestAddress) || aDestAddress.IsLinkLocalMulticast() ||
112 aDestAddress.IsRealmLocalMulticast());
113
114 isTmf = true;
115
116 exit:
117 return isTmf;
118 }
119
120 } // namespace Tmf
121 } // namespace ot
122