1 /*
2 * Copyright (c) 2016-2017, 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 OpenThread Border Router API.
32 */
33
34 #include "openthread-core-config.h"
35
36 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
37
38 #include <openthread/border_router.h>
39
40 #include "border_router/routing_manager.hpp"
41 #include "common/debug.hpp"
42 #include "common/instance.hpp"
43
44 using namespace ot;
45
46 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
otBorderRoutingInit(otInstance * aInstance,uint32_t aInfraIfIndex,bool aInfraIfIsRunning)47 otError otBorderRoutingInit(otInstance *aInstance, uint32_t aInfraIfIndex, bool aInfraIfIsRunning)
48 {
49 Instance &instance = *static_cast<Instance *>(aInstance);
50
51 return instance.Get<BorderRouter::RoutingManager>().Init(aInfraIfIndex, aInfraIfIsRunning);
52 }
53
otBorderRoutingSetEnabled(otInstance * aInstance,bool aEnabled)54 otError otBorderRoutingSetEnabled(otInstance *aInstance, bool aEnabled)
55 {
56 Instance &instance = *static_cast<Instance *>(aInstance);
57
58 return instance.Get<BorderRouter::RoutingManager>().SetEnabled(aEnabled);
59 }
60
otBorderRoutingGetOmrPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)61 otError otBorderRoutingGetOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
62 {
63 Instance &instance = *static_cast<Instance *>(aInstance);
64
65 return instance.Get<BorderRouter::RoutingManager>().GetOmrPrefix(static_cast<Ip6::Prefix &>(*aPrefix));
66 }
67
otBorderRoutingGetOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)68 otError otBorderRoutingGetOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
69 {
70 Instance &instance = *static_cast<Instance *>(aInstance);
71
72 return instance.Get<BorderRouter::RoutingManager>().GetOnLinkPrefix(static_cast<Ip6::Prefix &>(*aPrefix));
73 }
74
75 #endif
76
otBorderRouterGetNetData(otInstance * aInstance,bool aStable,uint8_t * aData,uint8_t * aDataLength)77 otError otBorderRouterGetNetData(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength)
78 {
79 Instance &instance = *static_cast<Instance *>(aInstance);
80
81 OT_ASSERT(aData != nullptr && aDataLength != nullptr);
82
83 return instance.Get<NetworkData::Local>().GetNetworkData(aStable, aData, *aDataLength);
84 }
85
otBorderRouterAddOnMeshPrefix(otInstance * aInstance,const otBorderRouterConfig * aConfig)86 otError otBorderRouterAddOnMeshPrefix(otInstance *aInstance, const otBorderRouterConfig *aConfig)
87 {
88 Error error = kErrorNone;
89 Instance & instance = *static_cast<Instance *>(aInstance);
90 const NetworkData::OnMeshPrefixConfig *config = static_cast<const NetworkData::OnMeshPrefixConfig *>(aConfig);
91
92 OT_ASSERT(aConfig != nullptr);
93
94 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
95 if (aConfig->mDp)
96 {
97 error = instance.Get<BackboneRouter::Local>().SetDomainPrefix(*config);
98 }
99 else
100 #endif
101 {
102 error = instance.Get<NetworkData::Local>().AddOnMeshPrefix(*config);
103 }
104
105 return error;
106 }
107
otBorderRouterRemoveOnMeshPrefix(otInstance * aInstance,const otIp6Prefix * aPrefix)108 otError otBorderRouterRemoveOnMeshPrefix(otInstance *aInstance, const otIp6Prefix *aPrefix)
109 {
110 Error error = kErrorNone;
111 Instance & instance = *static_cast<Instance *>(aInstance);
112 const Ip6::Prefix *prefix = static_cast<const Ip6::Prefix *>(aPrefix);
113
114 OT_ASSERT(aPrefix != nullptr);
115
116 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
117 error = instance.Get<BackboneRouter::Local>().RemoveDomainPrefix(*prefix);
118
119 if (error == kErrorNotFound)
120 #endif
121 {
122 error = instance.Get<NetworkData::Local>().RemoveOnMeshPrefix(*prefix);
123 }
124
125 return error;
126 }
127
otBorderRouterGetNextOnMeshPrefix(otInstance * aInstance,otNetworkDataIterator * aIterator,otBorderRouterConfig * aConfig)128 otError otBorderRouterGetNextOnMeshPrefix(otInstance * aInstance,
129 otNetworkDataIterator *aIterator,
130 otBorderRouterConfig * aConfig)
131 {
132 Instance & instance = *static_cast<Instance *>(aInstance);
133 NetworkData::OnMeshPrefixConfig *config = static_cast<NetworkData::OnMeshPrefixConfig *>(aConfig);
134
135 OT_ASSERT(aIterator != nullptr && aConfig != nullptr);
136
137 return instance.Get<NetworkData::Local>().GetNextOnMeshPrefix(*aIterator, *config);
138 }
139
otBorderRouterAddRoute(otInstance * aInstance,const otExternalRouteConfig * aConfig)140 otError otBorderRouterAddRoute(otInstance *aInstance, const otExternalRouteConfig *aConfig)
141 {
142 Instance &instance = *static_cast<Instance *>(aInstance);
143
144 OT_ASSERT(aConfig != nullptr);
145
146 return instance.Get<NetworkData::Local>().AddHasRoutePrefix(
147 *static_cast<const NetworkData::ExternalRouteConfig *>(aConfig));
148 }
149
otBorderRouterRemoveRoute(otInstance * aInstance,const otIp6Prefix * aPrefix)150 otError otBorderRouterRemoveRoute(otInstance *aInstance, const otIp6Prefix *aPrefix)
151 {
152 Instance &instance = *static_cast<Instance *>(aInstance);
153
154 OT_ASSERT(aPrefix != nullptr);
155
156 return instance.Get<NetworkData::Local>().RemoveHasRoutePrefix(*static_cast<const Ip6::Prefix *>(aPrefix));
157 }
158
otBorderRouterGetNextRoute(otInstance * aInstance,otNetworkDataIterator * aIterator,otExternalRouteConfig * aConfig)159 otError otBorderRouterGetNextRoute(otInstance * aInstance,
160 otNetworkDataIterator *aIterator,
161 otExternalRouteConfig *aConfig)
162 {
163 Instance &instance = *static_cast<Instance *>(aInstance);
164
165 OT_ASSERT(aIterator != nullptr && aConfig != nullptr);
166
167 return instance.Get<NetworkData::Local>().GetNextExternalRoute(
168 *aIterator, *static_cast<NetworkData::ExternalRouteConfig *>(aConfig));
169 }
170
otBorderRouterRegister(otInstance * aInstance)171 otError otBorderRouterRegister(otInstance *aInstance)
172 {
173 Instance &instance = *static_cast<Instance *>(aInstance);
174
175 instance.Get<NetworkData::Notifier>().HandleServerDataUpdated();
176
177 return kErrorNone;
178 }
179
180 #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE
181