1 /*
2  *  Copyright (c) 2019, 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 Primary Backbone Router service management in the Thread Network.
32  */
33 
34 #include "bbr_leader.hpp"
35 
36 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
37 
38 #include "common/instance.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/logging.hpp"
41 
42 namespace ot {
43 namespace BackboneRouter {
44 
Leader(Instance & aInstance)45 Leader::Leader(Instance &aInstance)
46     : InstanceLocator(aInstance)
47 {
48     Reset();
49 }
50 
Reset(void)51 void Leader::Reset(void)
52 {
53     // Invalid server short address indicates no available Backbone Router service in the Thread Network.
54     mConfig.mServer16 = Mac::kShortAddrInvalid;
55 
56     // Domain Prefix Length 0 indicates no available Domain Prefix in the Thread network.
57     mDomainPrefix.SetLength(0);
58 }
59 
GetConfig(BackboneRouterConfig & aConfig) const60 Error Leader::GetConfig(BackboneRouterConfig &aConfig) const
61 {
62     Error error = kErrorNone;
63 
64     VerifyOrExit(HasPrimary(), error = kErrorNotFound);
65 
66     aConfig = mConfig;
67 
68 exit:
69     return error;
70 }
71 
GetServiceId(uint8_t & aServiceId) const72 Error Leader::GetServiceId(uint8_t &aServiceId) const
73 {
74     Error error = kErrorNone;
75 
76     VerifyOrExit(HasPrimary(), error = kErrorNotFound);
77     error = Get<NetworkData::Service::Manager>().GetServiceId<NetworkData::Service::BackboneRouter>(
78         /* aServerStable */ true, aServiceId);
79 
80 exit:
81     return error;
82 }
83 
84 #if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_BBR == 1)
LogBackboneRouterPrimary(State aState,const BackboneRouterConfig & aConfig) const85 void Leader::LogBackboneRouterPrimary(State aState, const BackboneRouterConfig &aConfig) const
86 {
87     OT_UNUSED_VARIABLE(aConfig);
88 
89     otLogInfoBbr("PBBR state: %s", StateToString(aState));
90 
91     if (aState != kStateRemoved && aState != kStateNone)
92     {
93         otLogInfoBbr("Rloc16: 0x%4X, seqno: %d, delay: %d, timeout %d", aConfig.mServer16, aConfig.mSequenceNumber,
94                      aConfig.mReregistrationDelay, aConfig.mMlrTimeout);
95     }
96 }
97 
LogDomainPrefix(DomainPrefixState aState,const Ip6::Prefix & aPrefix) const98 void Leader::LogDomainPrefix(DomainPrefixState aState, const Ip6::Prefix &aPrefix) const
99 {
100     otLogInfoBbr("Domain Prefix: %s, state: %s", aPrefix.ToString().AsCString(), DomainPrefixStateToString(aState));
101 }
102 
StateToString(State aState)103 const char *Leader::StateToString(State aState)
104 {
105     const char *logString = "Unknown";
106 
107     switch (aState)
108     {
109     case kStateNone:
110         logString = "None";
111         break;
112 
113     case kStateAdded:
114         logString = "Added";
115         break;
116 
117     case kStateRemoved:
118         logString = "Removed";
119         break;
120 
121     case kStateToTriggerRereg:
122         logString = "Rereg triggered";
123         break;
124 
125     case kStateRefreshed:
126         logString = "Refreshed";
127         break;
128 
129     case kStateUnchanged:
130         logString = "Unchanged";
131         break;
132 
133     default:
134         break;
135     }
136 
137     return logString;
138 }
139 
DomainPrefixStateToString(DomainPrefixState aState)140 const char *Leader::DomainPrefixStateToString(DomainPrefixState aState)
141 {
142     const char *logString = "Unknown";
143 
144     switch (aState)
145     {
146     case kDomainPrefixNone:
147         logString = "None";
148         break;
149 
150     case kDomainPrefixAdded:
151         logString = "Added";
152         break;
153 
154     case kDomainPrefixRemoved:
155         logString = "Removed";
156         break;
157 
158     case kDomainPrefixRefreshed:
159         logString = "Refreshed";
160         break;
161 
162     case kDomainPrefixUnchanged:
163         logString = "Unchanged";
164         break;
165     }
166 
167     return logString;
168 }
169 #endif
170 
Update(void)171 void Leader::Update(void)
172 {
173     UpdateBackboneRouterPrimary();
174     UpdateDomainPrefixConfig();
175 }
176 
UpdateBackboneRouterPrimary(void)177 void Leader::UpdateBackboneRouterPrimary(void)
178 {
179     BackboneRouterConfig config;
180     State                state;
181     uint32_t             origMlrTimeout;
182 
183     Get<NetworkData::Service::Manager>().GetBackboneRouterPrimary(config);
184 
185     if (config.mServer16 != mConfig.mServer16)
186     {
187         if (config.mServer16 == Mac::kShortAddrInvalid)
188         {
189             state = kStateRemoved;
190         }
191         else if (mConfig.mServer16 == Mac::kShortAddrInvalid)
192         {
193             state = kStateAdded;
194         }
195         else
196         {
197             // Short Address of PBBR changes.
198             state = kStateToTriggerRereg;
199         }
200     }
201     else if (config.mServer16 == Mac::kShortAddrInvalid)
202     {
203         // If no Primary all the time.
204         state = kStateNone;
205     }
206     else if (config.mSequenceNumber != mConfig.mSequenceNumber)
207     {
208         state = kStateToTriggerRereg;
209     }
210     else if (config.mReregistrationDelay != mConfig.mReregistrationDelay || config.mMlrTimeout != mConfig.mMlrTimeout)
211     {
212         state = kStateRefreshed;
213     }
214     else
215     {
216         state = kStateUnchanged;
217     }
218 
219     // Restrain the range of MLR timeout to be always valid
220     if (config.mServer16 != Mac::kShortAddrInvalid)
221     {
222         origMlrTimeout     = config.mMlrTimeout;
223         config.mMlrTimeout = config.mMlrTimeout < static_cast<uint32_t>(Mle::kMlrTimeoutMin)
224                                  ? static_cast<uint32_t>(Mle::kMlrTimeoutMin)
225                                  : config.mMlrTimeout;
226         config.mMlrTimeout = config.mMlrTimeout > static_cast<uint32_t>(Mle::kMlrTimeoutMax)
227                                  ? static_cast<uint32_t>(Mle::kMlrTimeoutMax)
228                                  : config.mMlrTimeout;
229 
230         if (config.mMlrTimeout != origMlrTimeout)
231         {
232             otLogNoteBbr("Leader MLR Timeout is normalized from %u to %u", origMlrTimeout, config.mMlrTimeout);
233         }
234     }
235 
236     mConfig = config;
237     LogBackboneRouterPrimary(state, mConfig);
238 
239 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
240     Get<BackboneRouter::Local>().HandleBackboneRouterPrimaryUpdate(state, mConfig);
241 #endif
242 
243 #if OPENTHREAD_CONFIG_MLR_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE)
244     Get<MlrManager>().HandleBackboneRouterPrimaryUpdate(state, mConfig);
245 #endif
246 
247 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
248     Get<DuaManager>().HandleBackboneRouterPrimaryUpdate(state, mConfig);
249 #endif
250 }
251 
UpdateDomainPrefixConfig(void)252 void Leader::UpdateDomainPrefixConfig(void)
253 {
254     NetworkData::Iterator           iterator = NetworkData::kIteratorInit;
255     NetworkData::OnMeshPrefixConfig config;
256     DomainPrefixState               state;
257     bool                            found = false;
258 
259     while (Get<NetworkData::Leader>().GetNextOnMeshPrefix(iterator, config) == kErrorNone)
260     {
261         if (config.mDp)
262         {
263             found = true;
264             break;
265         }
266     }
267 
268     if (!found)
269     {
270         if (mDomainPrefix.GetLength() != 0)
271         {
272             // Domain Prefix does not exist any more.
273             mDomainPrefix.SetLength(0);
274             state = kDomainPrefixRemoved;
275         }
276         else
277         {
278             state = kDomainPrefixNone;
279         }
280     }
281     else if (config.GetPrefix() == mDomainPrefix)
282     {
283         state = kDomainPrefixUnchanged;
284     }
285     else
286     {
287         if (mDomainPrefix.mLength == 0)
288         {
289             state = kDomainPrefixAdded;
290         }
291         else
292         {
293             state = kDomainPrefixRefreshed;
294         }
295 
296         mDomainPrefix = config.GetPrefix();
297     }
298 
299     LogDomainPrefix(state, mDomainPrefix);
300 
301 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
302     Get<Local>().HandleDomainPrefixUpdate(state);
303 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
304     Get<NdProxyTable>().HandleDomainPrefixUpdate(state);
305 #endif
306 #endif
307 
308 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
309     Get<DuaManager>().HandleDomainPrefixUpdate(state);
310 #endif
311 }
312 
IsDomainUnicast(const Ip6::Address & aAddress) const313 bool Leader::IsDomainUnicast(const Ip6::Address &aAddress) const
314 {
315     return HasDomainPrefix() && aAddress.MatchesPrefix(mDomainPrefix);
316 }
317 
318 } // namespace BackboneRouter
319 } // namespace ot
320 
321 #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
322