• 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 managing Domain Unicast Address feature defined in Thread 1.2.
32  */
33 
34 #ifndef DUA_MANAGER_HPP_
35 #define DUA_MANAGER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
40 
41 #if OPENTHREAD_CONFIG_DUA_ENABLE && (OPENTHREAD_CONFIG_THREAD_VERSION < OT_THREAD_VERSION_1_2)
42 #error "Thread 1.2 or higher version is required for OPENTHREAD_CONFIG_DUA_ENABLE"
43 #endif
44 
45 #if OPENTHREAD_CONFIG_DUA_ENABLE && !OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
46 #error "OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE is required for OPENTHREAD_CONFIG_DUA_ENABLE"
47 #endif
48 
49 #include "backbone_router/bbr_leader.hpp"
50 #include "coap/coap.hpp"
51 #include "coap/coap_message.hpp"
52 #include "common/locator.hpp"
53 #include "common/non_copyable.hpp"
54 #include "common/notifier.hpp"
55 #include "common/tasklet.hpp"
56 #include "common/time.hpp"
57 #include "common/time_ticker.hpp"
58 #include "common/timer.hpp"
59 #include "net/netif.hpp"
60 #include "thread/thread_tlvs.hpp"
61 #include "thread/topology.hpp"
62 
63 namespace ot {
64 
65 /**
66  * @addtogroup core-dua
67  *
68  * @brief
69  *   This module includes definitions for generating, managing, registering Domain Unicast Address.
70  *
71  * @{
72  *
73  * @defgroup core-dua Dua
74  *
75  * @}
76  *
77  */
78 
79 /**
80  * This class implements managing DUA.
81  *
82  */
83 class DuaManager : public InstanceLocator, private NonCopyable
84 {
85     friend class ot::Notifier;
86     friend class ot::TimeTicker;
87 
88 public:
89     /**
90      * This constructor initializes the object.
91      *
92      * @param[in]  aInstance     A reference to the OpenThread instance.
93      *
94      */
95     explicit DuaManager(Instance &aInstance);
96 
97     /**
98      * This method notifies Domain Prefix status.
99      *
100      * @param[in]  aState  The Domain Prefix state or state change.
101      *
102      */
103     void HandleDomainPrefixUpdate(BackboneRouter::Leader::DomainPrefixState aState);
104 
105     /**
106      * This method notifies Primary Backbone Router status.
107      *
108      * @param[in]  aState   The state or state change of Primary Backbone Router.
109      * @param[in]  aConfig  The Primary Backbone Router service.
110      *
111      */
112     void HandleBackboneRouterPrimaryUpdate(BackboneRouter::Leader::State               aState,
113                                            const BackboneRouter::BackboneRouterConfig &aConfig);
114 
115 #if OPENTHREAD_CONFIG_DUA_ENABLE
116 
117     /**
118      * This method returns a reference to the Domain Unicast Address.
119      *
120      * @returns A reference to the Domain Unicast Address.
121      *
122      */
GetDomainUnicastAddress(void) const123     const Ip6::Address &GetDomainUnicastAddress(void) const { return mDomainUnicastAddress.GetAddress(); }
124 
125     /**
126      * This method sets the Interface Identifier manually specified for the Thread Domain Unicast Address.
127      *
128      * @param[in]  aIid        A reference to the Interface Identifier to set.
129      *
130      * @retval kErrorNone          Successfully set the Interface Identifier.
131      * @retval kErrorInvalidArgs   The specified Interface Identifier is reserved.
132      *
133      */
134     Error SetFixedDuaInterfaceIdentifier(const Ip6::InterfaceIdentifier &aIid);
135 
136     /**
137      * This method clears the Interface Identifier manually specified for the Thread Domain Unicast Address.
138      *
139      */
140     void ClearFixedDuaInterfaceIdentifier(void);
141 
142     /**
143      * This method indicates whether or not there is Interface Identifier manually specified for the Thread
144      * Domain Unicast Address.
145      *
146      * @retval true  If there is Interface Identifier manually specified.
147      * @retval false If there is no Interface Identifier manually specified.
148      *
149      */
IsFixedDuaInterfaceIdentifierSet(void)150     bool IsFixedDuaInterfaceIdentifierSet(void) { return !mFixedDuaInterfaceIdentifier.IsUnspecified(); }
151 
152     /**
153      * This method gets the Interface Identifier for the Thread Domain Unicast Address if manually specified.
154      *
155      * @returns A reference to the Interface Identifier.
156      *
157      */
GetFixedDuaInterfaceIdentifier(void) const158     const Ip6::InterfaceIdentifier &GetFixedDuaInterfaceIdentifier(void) const { return mFixedDuaInterfaceIdentifier; }
159 
160     /*
161      * This method restores duplicate address detection information from non-volatile memory.
162      *
163      */
164     void Restore(void);
165 
166     /**
167      * This method notifies duplicated Domain Unicast Address.
168      *
169      */
170     void NotifyDuplicateDomainUnicastAddress(void);
171 #endif
172 
173 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE
174     void UpdateChildDomainUnicastAddress(const Child &aChild, Mle::ChildDuaState aState);
175 #endif
176 
177 private:
178     static constexpr uint8_t kNewRouterRegistrationDelay = 3; ///< Delay (in sec) to establish link for a new router.
179     static constexpr uint8_t kNewDuaRegistrationDelay    = 1; ///< Delay (in sec) for newly added DUA.
180 
181 #if OPENTHREAD_CONFIG_DUA_ENABLE
182     Error GenerateDomainUnicastAddressIid(void);
183     Error Store(void);
184 
185     void AddDomainUnicastAddress(void);
186     void RemoveDomainUnicastAddress(void);
187     void UpdateRegistrationDelay(uint8_t aDelay);
188 #endif
189 
190 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE
191     void SendAddressNotification(Ip6::Address &aAddress, ThreadStatusTlv::DuaStatus aStatus, const Child &aChild);
192 #endif
193 
194     void HandleNotifierEvents(Events aEvents);
195 
196     void HandleTimeTick(void);
197 
198     static void HandleRegistrationTask(Tasklet &aTasklet);
199 
200     void UpdateTimeTickerRegistration(void);
201 
202     static void HandleDuaResponse(void *               aContext,
203                                   otMessage *          aMessage,
204                                   const otMessageInfo *aMessageInfo,
205                                   Error                aResult);
206     void        HandleDuaResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
207 
208     static void HandleDuaNotification(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
209     void        HandleDuaNotification(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
210 
211     Error ProcessDuaResponse(Coap::Message &aMessage);
212 
213     void PerformNextRegistration(void);
214     void UpdateReregistrationDelay(void);
215     void UpdateCheckDelay(uint8_t aDelay);
216 
217     Tasklet        mRegistrationTask;
218     Coap::Resource mDuaNotification;
219     Ip6::Address   mRegisteringDua;
220     bool           mIsDuaPending : 1;
221 
222 #if OPENTHREAD_CONFIG_DUA_ENABLE
223     enum DuaState : uint8_t
224     {
225         kNotExist,    ///< DUA is not available.
226         kToRegister,  ///< DUA is to be registered.
227         kRegistering, ///< DUA is being registered.
228         kRegistered,  ///< DUA is registered.
229     };
230 
231     DuaState  mDuaState;
232     uint8_t   mDadCounter;
233     TimeMilli mLastRegistrationTime; // The time (in milliseconds) when sent last DUA.req or received DUA.rsp.
234     Ip6::InterfaceIdentifier   mFixedDuaInterfaceIdentifier;
235     Ip6::Netif::UnicastAddress mDomainUnicastAddress;
236 #endif
237 
238     union
239     {
240         struct
241         {
242             uint16_t mReregistrationDelay; // Delay (in seconds) for DUA re-registration.
243             uint8_t  mCheckDelay;          // Delay (in seconds) for checking whether or not registration is required.
244 #if OPENTHREAD_CONFIG_DUA_ENABLE
245             uint8_t mRegistrationDelay; // Delay (in seconds) for DUA registration.
246 #endif
247         } mFields;
248         uint32_t mValue; // Non-zero indicates timer should start.
249     } mDelay;
250 
251 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE
252     // TODO: (DUA) may re-evaluate the alternative option of distributing the flags into the child table:
253     //       - Child class itself have some padding - may save some RAM
254     //       - Avoid cross reference between a bit-vector and the child entry
255     ChildMask mChildDuaMask;             // Child Mask for child who registers DUA via Child Update Request.
256     ChildMask mChildDuaRegisteredMask;   // Child Mask for child's DUA that was registered by the parent on behalf.
257     uint16_t  mChildIndexDuaRegistering; // Child Index of the DUA being registered.
258 #endif
259 };
260 
261 } // namespace ot
262 
263 #endif // OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
264 #endif // DUA_MANAGER_HPP_
265