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 Dataset Updater.
32  *
33  */
34 
35 #include "dataset_updater.hpp"
36 
37 #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
38 
39 #include "common/code_utils.hpp"
40 #include "common/instance.hpp"
41 #include "common/locator_getters.hpp"
42 #include "common/log.hpp"
43 #include "common/random.hpp"
44 #include "meshcop/timestamp.hpp"
45 
46 namespace ot {
47 namespace MeshCoP {
48 
DatasetUpdater(Instance & aInstance)49 DatasetUpdater::DatasetUpdater(Instance &aInstance)
50     : InstanceLocator(aInstance)
51     , mTimer(aInstance)
52     , mDataset(nullptr)
53 {
54 }
55 
RequestUpdate(const Dataset::Info & aDataset,UpdaterCallback aCallback,void * aContext)56 Error DatasetUpdater::RequestUpdate(const Dataset::Info &aDataset, UpdaterCallback aCallback, void *aContext)
57 {
58     Error    error   = kErrorNone;
59     Message *message = nullptr;
60 
61     VerifyOrExit(!Get<Mle::Mle>().IsDisabled(), error = kErrorInvalidState);
62     VerifyOrExit(mDataset == nullptr, error = kErrorBusy);
63 
64     VerifyOrExit(!aDataset.IsActiveTimestampPresent() && !aDataset.IsPendingTimestampPresent(),
65                  error = kErrorInvalidArgs);
66 
67     message = Get<MessagePool>().Allocate(Message::kTypeOther);
68     VerifyOrExit(message != nullptr, error = kErrorNoBufs);
69 
70     SuccessOrExit(error = message->Append(aDataset));
71 
72     mCallback.Set(aCallback, aContext);
73     mDataset = message;
74 
75     mTimer.Start(1);
76 
77 exit:
78     FreeMessageOnError(message, error);
79     return error;
80 }
81 
CancelUpdate(void)82 void DatasetUpdater::CancelUpdate(void)
83 {
84     VerifyOrExit(mDataset != nullptr);
85 
86     FreeMessage(mDataset);
87     mDataset = nullptr;
88     mTimer.Stop();
89 
90 exit:
91     return;
92 }
93 
HandleTimer(void)94 void DatasetUpdater::HandleTimer(void) { PreparePendingDataset(); }
95 
PreparePendingDataset(void)96 void DatasetUpdater::PreparePendingDataset(void)
97 {
98     Dataset       dataset;
99     Dataset::Info requestedDataset;
100     Error         error;
101 
102     VerifyOrExit(!Get<Mle::Mle>().IsDisabled(), error = kErrorInvalidState);
103 
104     IgnoreError(mDataset->Read(0, requestedDataset));
105 
106     error = Get<ActiveDatasetManager>().Read(dataset);
107 
108     if (error != kErrorNone)
109     {
110         // If there is no valid Active Dataset but MLE is not disabled,
111         // set the timer to try again after the retry interval. This
112         // handles the situation where a dataset update request comes
113         // right after the network is formed but before the active
114         // dataset is created.
115 
116         mTimer.Start(kRetryInterval);
117         ExitNow(error = kErrorNone);
118     }
119 
120     IgnoreError(dataset.SetFrom(requestedDataset));
121 
122     if (!requestedDataset.IsDelayPresent())
123     {
124         uint32_t delay = kDefaultDelay;
125 
126         SuccessOrExit(error = dataset.SetTlv(Tlv::kDelayTimer, delay));
127     }
128 
129     {
130         Timestamp timestamp;
131 
132         if (Get<PendingDatasetManager>().GetTimestamp() != nullptr)
133         {
134             timestamp = *Get<PendingDatasetManager>().GetTimestamp();
135         }
136 
137         timestamp.AdvanceRandomTicks();
138         dataset.SetTimestamp(Dataset::kPending, timestamp);
139     }
140 
141     {
142         ActiveTimestampTlv *tlv = dataset.GetTlv<ActiveTimestampTlv>();
143 
144         tlv->GetTimestamp().AdvanceRandomTicks();
145     }
146 
147     SuccessOrExit(error = Get<PendingDatasetManager>().Save(dataset));
148 
149 exit:
150     if (error != kErrorNone)
151     {
152         Finish(error);
153     }
154 }
155 
Finish(Error aError)156 void DatasetUpdater::Finish(Error aError)
157 {
158     OT_ASSERT(mDataset != nullptr);
159 
160     FreeMessage(mDataset);
161     mDataset = nullptr;
162 
163     mCallback.InvokeIfSet(aError);
164 }
165 
HandleNotifierEvents(Events aEvents)166 void DatasetUpdater::HandleNotifierEvents(Events aEvents)
167 {
168     Dataset::Info requestedDataset;
169     Dataset::Info dataset;
170 
171     VerifyOrExit(mDataset != nullptr);
172 
173     VerifyOrExit(aEvents.ContainsAny(kEventActiveDatasetChanged | kEventPendingDatasetChanged));
174 
175     IgnoreError(mDataset->Read(0, requestedDataset));
176 
177     if (aEvents.Contains(kEventActiveDatasetChanged) && Get<ActiveDatasetManager>().Read(dataset) == kErrorNone)
178     {
179         if (requestedDataset.IsSubsetOf(dataset))
180         {
181             Finish(kErrorNone);
182         }
183         else
184         {
185             Timestamp requestedDatasetTimestamp;
186             Timestamp activeDatasetTimestamp;
187 
188             requestedDataset.GetActiveTimestamp(requestedDatasetTimestamp);
189             dataset.GetActiveTimestamp(activeDatasetTimestamp);
190             if (Timestamp::Compare(requestedDatasetTimestamp, activeDatasetTimestamp) <= 0)
191             {
192                 Finish(kErrorAlready);
193             }
194         }
195     }
196 
197     if (aEvents.Contains(kEventPendingDatasetChanged) && Get<PendingDatasetManager>().Read(dataset) == kErrorNone)
198     {
199         if (!requestedDataset.IsSubsetOf(dataset))
200         {
201             Finish(kErrorAlready);
202         }
203     }
204 
205 exit:
206     return;
207 }
208 
209 } // namespace MeshCoP
210 } // namespace ot
211 
212 #endif // #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
213