1 /*
2  *  Copyright (c) 2016, 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 the local Thread Network Data.
32  */
33 
34 #include "network_data_local.hpp"
35 
36 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
37 
38 #include "common/code_utils.hpp"
39 #include "common/debug.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/log.hpp"
42 #include "instance/instance.hpp"
43 #include "mac/mac_types.hpp"
44 #include "thread/mle_types.hpp"
45 #include "thread/thread_netif.hpp"
46 
47 namespace ot {
48 namespace NetworkData {
49 
50 RegisterLogModule("NetworkData");
51 
52 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
53 
AddOnMeshPrefix(const OnMeshPrefixConfig & aConfig)54 Error Local::AddOnMeshPrefix(const OnMeshPrefixConfig &aConfig)
55 {
56     Error error = kErrorInvalidArgs;
57 
58     VerifyOrExit(aConfig.IsValid(GetInstance()));
59 
60     error =
61         AddPrefix(aConfig.GetPrefix(), NetworkDataTlv::kTypeBorderRouter, aConfig.ConvertToTlvFlags(), aConfig.mStable);
62 
63 exit:
64     return error;
65 }
66 
ContainsOnMeshPrefix(const Ip6::Prefix & aPrefix) const67 bool Local::ContainsOnMeshPrefix(const Ip6::Prefix &aPrefix) const
68 {
69     const PrefixTlv *tlv;
70     bool             contains = false;
71 
72     VerifyOrExit((tlv = FindPrefix(aPrefix)) != nullptr);
73     VerifyOrExit(tlv->FindSubTlv(NetworkDataTlv::kTypeBorderRouter) != nullptr);
74 
75     contains = true;
76 
77 exit:
78     return contains;
79 }
80 
AddHasRoutePrefix(const ExternalRouteConfig & aConfig)81 Error Local::AddHasRoutePrefix(const ExternalRouteConfig &aConfig)
82 {
83     Error error = kErrorInvalidArgs;
84 
85     VerifyOrExit(aConfig.IsValid(GetInstance()));
86 
87     error = AddPrefix(aConfig.GetPrefix(), NetworkDataTlv::kTypeHasRoute, aConfig.ConvertToTlvFlags(), aConfig.mStable);
88 
89 exit:
90     return error;
91 }
92 
AddPrefix(const Ip6::Prefix & aPrefix,NetworkDataTlv::Type aSubTlvType,uint16_t aFlags,bool aStable)93 Error Local::AddPrefix(const Ip6::Prefix &aPrefix, NetworkDataTlv::Type aSubTlvType, uint16_t aFlags, bool aStable)
94 {
95     Error      error = kErrorNone;
96     uint8_t    subTlvLength;
97     PrefixTlv *prefixTlv;
98 
99     IgnoreError(RemovePrefix(aPrefix));
100 
101     subTlvLength = (aSubTlvType == NetworkDataTlv::kTypeBorderRouter)
102                        ? sizeof(BorderRouterTlv) + sizeof(BorderRouterEntry)
103                        : sizeof(HasRouteTlv) + sizeof(HasRouteEntry);
104 
105     prefixTlv = As<PrefixTlv>(AppendTlv(sizeof(PrefixTlv) + aPrefix.GetBytesSize() + subTlvLength));
106     VerifyOrExit(prefixTlv != nullptr, error = kErrorNoBufs);
107 
108     prefixTlv->Init(0, aPrefix);
109     prefixTlv->SetSubTlvsLength(subTlvLength);
110 
111     if (aSubTlvType == NetworkDataTlv::kTypeBorderRouter)
112     {
113         BorderRouterTlv *brTlv = As<BorderRouterTlv>(prefixTlv->GetSubTlvs());
114         brTlv->Init();
115         brTlv->SetLength(brTlv->GetLength() + sizeof(BorderRouterEntry));
116         brTlv->GetEntry(0)->Init();
117         brTlv->GetEntry(0)->SetFlags(aFlags);
118     }
119     else // aSubTlvType is NetworkDataTlv::kTypeHasRoute
120     {
121         HasRouteTlv *hasRouteTlv = As<HasRouteTlv>(prefixTlv->GetSubTlvs());
122         hasRouteTlv->Init();
123         hasRouteTlv->SetLength(hasRouteTlv->GetLength() + sizeof(HasRouteEntry));
124         hasRouteTlv->GetEntry(0)->Init();
125         hasRouteTlv->GetEntry(0)->SetFlags(static_cast<uint8_t>(aFlags));
126     }
127 
128     if (aStable)
129     {
130         prefixTlv->SetStable();
131         prefixTlv->GetSubTlvs()->SetStable();
132     }
133 
134     DumpDebg("AddPrefix", GetBytes(), GetLength());
135 
136 exit:
137 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL
138     if (error == kErrorNoBufs)
139     {
140         Get<Notifier>().SignalNetworkDataFull();
141     }
142 #endif
143 
144     return error;
145 }
146 
RemovePrefix(const Ip6::Prefix & aPrefix)147 Error Local::RemovePrefix(const Ip6::Prefix &aPrefix)
148 {
149     Error      error = kErrorNone;
150     PrefixTlv *tlv;
151 
152     VerifyOrExit((tlv = FindPrefix(aPrefix)) != nullptr, error = kErrorNotFound);
153     RemoveTlv(tlv);
154 
155 exit:
156     DumpDebg("RmvPrefix", GetBytes(), GetLength());
157     return error;
158 }
159 
UpdateRloc(PrefixTlv & aPrefixTlv)160 void Local::UpdateRloc(PrefixTlv &aPrefixTlv)
161 {
162     uint16_t rloc16 = Get<Mle::MleRouter>().GetRloc16();
163 
164     for (NetworkDataTlv *cur = aPrefixTlv.GetSubTlvs(); cur < aPrefixTlv.GetNext(); cur = cur->GetNext())
165     {
166         switch (cur->GetType())
167         {
168         case NetworkDataTlv::kTypeHasRoute:
169             As<HasRouteTlv>(cur)->GetEntry(0)->SetRloc(rloc16);
170             break;
171 
172         case NetworkDataTlv::kTypeBorderRouter:
173             As<BorderRouterTlv>(cur)->GetEntry(0)->SetRloc(rloc16);
174             break;
175 
176         default:
177             OT_ASSERT(false);
178         }
179     }
180 }
181 
182 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
183 
184 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
AddService(uint32_t aEnterpriseNumber,const ServiceData & aServiceData,bool aServerStable,const ServerData & aServerData)185 Error Local::AddService(uint32_t           aEnterpriseNumber,
186                         const ServiceData &aServiceData,
187                         bool               aServerStable,
188                         const ServerData  &aServerData)
189 {
190     Error       error = kErrorNone;
191     ServiceTlv *serviceTlv;
192     ServerTlv  *serverTlv;
193     uint16_t    serviceTlvSize = ServiceTlv::CalculateSize(aEnterpriseNumber, aServiceData.GetLength()) +
194                               sizeof(ServerTlv) + aServerData.GetLength();
195 
196     IgnoreError(RemoveService(aEnterpriseNumber, aServiceData));
197 
198     VerifyOrExit(serviceTlvSize <= kMaxSize, error = kErrorNoBufs);
199 
200     serviceTlv = As<ServiceTlv>(AppendTlv(serviceTlvSize));
201     VerifyOrExit(serviceTlv != nullptr, error = kErrorNoBufs);
202 
203     serviceTlv->Init(/* aServiceId */ 0, aEnterpriseNumber, aServiceData);
204     serviceTlv->SetSubTlvsLength(sizeof(ServerTlv) + aServerData.GetLength());
205 
206     serverTlv = As<ServerTlv>(serviceTlv->GetSubTlvs());
207     serverTlv->Init(Get<Mle::MleRouter>().GetRloc16(), aServerData);
208 
209     // According to Thread spec 1.1.1, section 5.18.6 Service TLV:
210     // "The Stable flag is set if any of the included sub-TLVs have their Stable flag set."
211     // The meaning also seems to be 'if and only if'.
212     if (aServerStable)
213     {
214         serviceTlv->SetStable();
215         serverTlv->SetStable();
216     }
217 
218     DumpDebg("AddService", GetBytes(), GetLength());
219 
220 exit:
221 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL
222     if (error == kErrorNoBufs)
223     {
224         Get<Notifier>().SignalNetworkDataFull();
225     }
226 #endif
227 
228     return error;
229 }
230 
RemoveService(uint32_t aEnterpriseNumber,const ServiceData & aServiceData)231 Error Local::RemoveService(uint32_t aEnterpriseNumber, const ServiceData &aServiceData)
232 {
233     Error       error = kErrorNone;
234     ServiceTlv *tlv;
235 
236     VerifyOrExit((tlv = FindService(aEnterpriseNumber, aServiceData, kServiceExactMatch)) != nullptr,
237                  error = kErrorNotFound);
238     RemoveTlv(tlv);
239 
240 exit:
241     DumpDebg("RmvService", GetBytes(), GetLength());
242     return error;
243 }
244 
UpdateRloc(ServiceTlv & aService)245 void Local::UpdateRloc(ServiceTlv &aService)
246 {
247     uint16_t rloc16 = Get<Mle::MleRouter>().GetRloc16();
248 
249     for (NetworkDataTlv *cur = aService.GetSubTlvs(); cur < aService.GetNext(); cur = cur->GetNext())
250     {
251         switch (cur->GetType())
252         {
253         case NetworkDataTlv::kTypeServer:
254             As<ServerTlv>(cur)->SetServer16(rloc16);
255             break;
256 
257         default:
258             OT_ASSERT(false);
259         }
260     }
261 }
262 
263 #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
264 
UpdateRloc(void)265 void Local::UpdateRloc(void)
266 {
267     for (NetworkDataTlv *cur = GetTlvsStart(); cur < GetTlvsEnd(); cur = cur->GetNext())
268     {
269         switch (cur->GetType())
270         {
271 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
272         case NetworkDataTlv::kTypePrefix:
273             UpdateRloc(*As<PrefixTlv>(cur));
274             break;
275 #endif
276 
277 #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
278 
279         case NetworkDataTlv::kTypeService:
280             UpdateRloc(*As<ServiceTlv>(cur));
281             break;
282 #endif
283 
284         default:
285             OT_ASSERT(false);
286         }
287     }
288 }
289 
290 } // namespace NetworkData
291 } // namespace ot
292 
293 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
294