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  * @brief
32  *  This file defines the OpenThread Network Data API.
33  */
34 
35 #ifndef OPENTHREAD_NETDATA_H_
36 #define OPENTHREAD_NETDATA_H_
37 
38 #include <openthread/commissioner.h>
39 #include <openthread/ip6.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46  * @addtogroup api-thread-general
47  *
48  * @{
49  *
50  */
51 
52 #define OT_NETWORK_DATA_ITERATOR_INIT 0 ///< Value to initialize `otNetworkDataIterator`.
53 
54 typedef uint32_t otNetworkDataIterator; ///< Used to iterate through Network Data information.
55 
56 /**
57  * Represents a Border Router configuration.
58  */
59 typedef struct otBorderRouterConfig
60 {
61     otIp6Prefix mPrefix;           ///< The IPv6 prefix.
62     signed int  mPreference : 2;   ///< A 2-bit signed int preference (`OT_ROUTE_PREFERENCE_*` values).
63     bool        mPreferred : 1;    ///< Whether prefix is preferred.
64     bool        mSlaac : 1;        ///< Whether prefix can be used for address auto-configuration (SLAAC).
65     bool        mDhcp : 1;         ///< Whether border router is DHCPv6 Agent.
66     bool        mConfigure : 1;    ///< Whether DHCPv6 Agent supplying other config data.
67     bool        mDefaultRoute : 1; ///< Whether border router is a default router for prefix.
68     bool        mOnMesh : 1;       ///< Whether this prefix is considered on-mesh.
69     bool        mStable : 1;       ///< Whether this configuration is considered Stable Network Data.
70     bool        mNdDns : 1;        ///< Whether this border router can supply DNS information via ND.
71     bool        mDp : 1;           ///< Whether prefix is a Thread Domain Prefix (added since Thread 1.2).
72     uint16_t    mRloc16;           ///< The border router's RLOC16 (value ignored on config add).
73 } otBorderRouterConfig;
74 
75 /**
76  * Represents 6LoWPAN Context ID information associated with a prefix in Network Data.
77  *
78  */
79 typedef struct otLowpanContextInfo
80 {
81     uint8_t     mContextId;    ///< The 6LoWPAN Context ID.
82     bool        mCompressFlag; ///< The compress flag.
83     otIp6Prefix mPrefix;       ///< The associated IPv6 prefix.
84 } otLowpanContextInfo;
85 
86 /**
87  * Represents an External Route configuration.
88  *
89  */
90 typedef struct otExternalRouteConfig
91 {
92     otIp6Prefix mPrefix;                  ///< The IPv6 prefix.
93     uint16_t    mRloc16;                  ///< The border router's RLOC16 (value ignored on config add).
94     signed int  mPreference : 2;          ///< A 2-bit signed int preference (`OT_ROUTE_PREFERENCE_*` values).
95     bool        mNat64 : 1;               ///< Whether this is a NAT64 prefix.
96     bool        mStable : 1;              ///< Whether this configuration is considered Stable Network Data.
97     bool        mNextHopIsThisDevice : 1; ///< Whether the next hop is this device (value ignored on config add).
98     bool        mAdvPio : 1;              ///< Whether or not BR is advertising a ULA prefix in PIO (AP flag).
99 } otExternalRouteConfig;
100 
101 /**
102  * Defines valid values for `mPreference` in `otExternalRouteConfig` and `otBorderRouterConfig`.
103  *
104  */
105 typedef enum otRoutePreference
106 {
107     OT_ROUTE_PREFERENCE_LOW  = -1, ///< Low route preference.
108     OT_ROUTE_PREFERENCE_MED  = 0,  ///< Medium route preference.
109     OT_ROUTE_PREFERENCE_HIGH = 1,  ///< High route preference.
110 } otRoutePreference;
111 
112 #define OT_SERVICE_DATA_MAX_SIZE 252 ///< Max size of Service Data in bytes.
113 #define OT_SERVER_DATA_MAX_SIZE 248  ///< Max size of Server Data in bytes. Theoretical limit, practically much lower.
114 
115 /**
116  * Represents a Server configuration.
117  *
118  */
119 typedef struct otServerConfig
120 {
121     bool     mStable : 1;                          ///< Whether this config is considered Stable Network Data.
122     uint8_t  mServerDataLength;                    ///< Length of server data.
123     uint8_t  mServerData[OT_SERVER_DATA_MAX_SIZE]; ///< Server data bytes.
124     uint16_t mRloc16;                              ///< The Server RLOC16.
125 } otServerConfig;
126 
127 /**
128  * Represents a Service configuration.
129  *
130  */
131 typedef struct otServiceConfig
132 {
133     uint8_t        mServiceId;                             ///< Service ID (when iterating over the  Network Data).
134     uint32_t       mEnterpriseNumber;                      ///< IANA Enterprise Number.
135     uint8_t        mServiceDataLength;                     ///< Length of service data.
136     uint8_t        mServiceData[OT_SERVICE_DATA_MAX_SIZE]; ///< Service data bytes.
137     otServerConfig mServerConfig;                          ///< The Server configuration.
138 } otServiceConfig;
139 
140 /**
141  * Provide full or stable copy of the Partition's Thread Network Data.
142  *
143  * @param[in]      aInstance    A pointer to an OpenThread instance.
144  * @param[in]      aStable      TRUE when copying the stable version, FALSE when copying the full version.
145  * @param[out]     aData        A pointer to the data buffer.
146  * @param[in,out]  aDataLength  On entry, size of the data buffer pointed to by @p aData.
147  *                              On exit, number of copied bytes.
148  *
149  * @retval OT_ERROR_NONE    Successfully copied the Thread Network Data into @p aData and updated @p aDataLength.
150  * @retval OT_ERROR_NO_BUFS Not enough space in @p aData to fully copy the Thread Network Data.
151  *
152  */
153 otError otNetDataGet(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength);
154 
155 /**
156  * Get the current length (number of bytes) of Partition's Thread Network Data.
157  *
158  * @param[in] aInstance    A pointer to an OpenThread instance.
159  *
160  * @return The length of the Network Data.
161  *
162  */
163 uint8_t otNetDataGetLength(otInstance *aInstance);
164 
165 /**
166  * Get the maximum observed length of the Thread Network Data since OT stack initialization or since the last call to
167  * `otNetDataResetMaxLength()`.
168  *
169  * @param[in] aInstance    A pointer to an OpenThread instance.
170  *
171  * @return The maximum length of the Network Data (high water mark for Network Data length).
172  *
173  */
174 uint8_t otNetDataGetMaxLength(otInstance *aInstance);
175 
176 /**
177  * Reset the tracked maximum length of the Thread Network Data.
178  *
179  * @param[in] aInstance    A pointer to an OpenThread instance.
180  *
181  * @sa otNetDataGetMaxLength
182  *
183  */
184 void otNetDataResetMaxLength(otInstance *aInstance);
185 
186 /**
187  * Get the next On Mesh Prefix in the partition's Network Data.
188  *
189  * @param[in]      aInstance  A pointer to an OpenThread instance.
190  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first on-mesh entry
191                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
192  * @param[out]     aConfig    A pointer to where the On Mesh Prefix information will be placed.
193  *
194  * @retval OT_ERROR_NONE       Successfully found the next On Mesh prefix.
195  * @retval OT_ERROR_NOT_FOUND  No subsequent On Mesh prefix exists in the Thread Network Data.
196  *
197  */
198 otError otNetDataGetNextOnMeshPrefix(otInstance            *aInstance,
199                                      otNetworkDataIterator *aIterator,
200                                      otBorderRouterConfig  *aConfig);
201 
202 /**
203  * Get the next external route in the partition's Network Data.
204  *
205  * @param[in]      aInstance  A pointer to an OpenThread instance.
206  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first external route entry
207                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
208  * @param[out]     aConfig    A pointer to where the External Route information will be placed.
209  *
210  * @retval OT_ERROR_NONE       Successfully found the next External Route.
211  * @retval OT_ERROR_NOT_FOUND  No subsequent external route entry exists in the Thread Network Data.
212  *
213  */
214 otError otNetDataGetNextRoute(otInstance *aInstance, otNetworkDataIterator *aIterator, otExternalRouteConfig *aConfig);
215 
216 /**
217  * Get the next service in the partition's Network Data.
218  *
219  * @param[in]      aInstance  A pointer to an OpenThread instance.
220  * @param[in,out]  aIterator  A pointer to the Network Data iterator context. To get the first service entry
221                               it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
222  * @param[out]     aConfig    A pointer to where the service information will be placed.
223  *
224  * @retval OT_ERROR_NONE       Successfully found the next service.
225  * @retval OT_ERROR_NOT_FOUND  No subsequent service exists in the partition's Network Data.
226  *
227  */
228 otError otNetDataGetNextService(otInstance *aInstance, otNetworkDataIterator *aIterator, otServiceConfig *aConfig);
229 
230 /**
231  * Get the next 6LoWPAN Context ID info in the partition's Network Data.
232  *
233  * @param[in]      aInstance     A pointer to an OpenThread instance.
234  * @param[in,out]  aIterator     A pointer to the Network Data iterator. To get the first service entry
235                                  it should be set to OT_NETWORK_DATA_ITERATOR_INIT.
236  * @param[out]     aContextInfo  A pointer to where the retrieved 6LoWPAN Context ID information will be placed.
237  *
238  * @retval OT_ERROR_NONE       Successfully found the next 6LoWPAN Context ID info.
239  * @retval OT_ERROR_NOT_FOUND  No subsequent 6LoWPAN Context info exists in the partition's Network Data.
240  *
241  */
242 otError otNetDataGetNextLowpanContextInfo(otInstance            *aInstance,
243                                           otNetworkDataIterator *aIterator,
244                                           otLowpanContextInfo   *aContextInfo);
245 
246 /**
247  * Gets the Commissioning Dataset from the partition's Network Data.
248  *
249  * @param[in]  aInstance   A pointer to the OpenThread instance.
250  * @param[out] aDataset    A pointer to a `otCommissioningDataset` to populate.
251  *
252  */
253 void otNetDataGetCommissioningDataset(otInstance *aInstance, otCommissioningDataset *aDataset);
254 
255 /**
256  * Get the Network Data Version.
257  *
258  * @param[in]  aInstance A pointer to an OpenThread instance.
259  *
260  * @returns The Network Data Version.
261  *
262  */
263 uint8_t otNetDataGetVersion(otInstance *aInstance);
264 
265 /**
266  * Get the Stable Network Data Version.
267  *
268  * @param[in]  aInstance A pointer to an OpenThread instance.
269  *
270  * @returns The Stable Network Data Version.
271  *
272  */
273 uint8_t otNetDataGetStableVersion(otInstance *aInstance);
274 
275 /**
276  * Check if the steering data includes a Joiner.
277  *
278  * @param[in]  aInstance          A pointer to an OpenThread instance.
279  * @param[in]  aEui64             A pointer to the Joiner's IEEE EUI-64.
280  *
281  * @retval OT_ERROR_NONE          @p aEui64 is included in the steering data.
282  * @retval OT_ERROR_INVALID_STATE No steering data present.
283  * @retval OT_ERROR_NOT_FOUND     @p aEui64 is not included in the steering data.
284  *
285  */
286 otError otNetDataSteeringDataCheckJoiner(otInstance *aInstance, const otExtAddress *aEui64);
287 
288 // Forward declaration
289 struct otJoinerDiscerner;
290 
291 /**
292  * Check if the steering data includes a Joiner with a given discerner value.
293  *
294  * @param[in]  aInstance          A pointer to an OpenThread instance.
295  * @param[in]  aDiscerner         A pointer to the Joiner Discerner.
296  *
297  * @retval OT_ERROR_NONE          @p aDiscerner is included in the steering data.
298  * @retval OT_ERROR_INVALID_STATE No steering data present.
299  * @retval OT_ERROR_NOT_FOUND     @p aDiscerner is not included in the steering data.
300  *
301  */
302 otError otNetDataSteeringDataCheckJoinerWithDiscerner(otInstance                     *aInstance,
303                                                       const struct otJoinerDiscerner *aDiscerner);
304 
305 /**
306  * Check whether a given Prefix can act as a valid OMR prefix and also the Leader's Network Data contains this prefix.
307  *
308  * @param[in]  aInstance  A pointer to an OpenThread instance.
309  * @param[in]  aPrefix    A pointer to the IPv6 prefix.
310  *
311  * @returns  Whether @p aPrefix is a valid OMR prefix and Leader's Network Data contains the OMR prefix @p aPrefix.
312  *
313  * @note This API is only available when `OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE` is used.
314  *
315  */
316 bool otNetDataContainsOmrPrefix(otInstance *aInstance, const otIp6Prefix *aPrefix);
317 
318 /**
319  * @}
320  *
321  */
322 
323 #ifdef __cplusplus
324 } // extern "C"
325 #endif
326 
327 #endif // OPENTHREAD_NETDATA_H_
328