1 /*
2  *  Copyright (c) 2021-22, 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 Routing Manager API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
37 
38 #include <openthread/border_routing.h>
39 #include <openthread/platform/border_routing.h>
40 
41 #include "border_router/routing_manager.hpp"
42 #include "instance/instance.hpp"
43 
44 using namespace ot;
45 
otBorderRoutingInit(otInstance * aInstance,uint32_t aInfraIfIndex,bool aInfraIfIsRunning)46 otError otBorderRoutingInit(otInstance *aInstance, uint32_t aInfraIfIndex, bool aInfraIfIsRunning)
47 {
48     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().Init(aInfraIfIndex, aInfraIfIsRunning);
49 }
50 
otBorderRoutingSetEnabled(otInstance * aInstance,bool aEnabled)51 otError otBorderRoutingSetEnabled(otInstance *aInstance, bool aEnabled)
52 {
53     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetEnabled(aEnabled);
54 }
55 
otBorderRoutingGetState(otInstance * aInstance)56 otBorderRoutingState otBorderRoutingGetState(otInstance *aInstance)
57 {
58     return MapEnum(AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetState());
59 }
60 
otBorderRoutingGetRouteInfoOptionPreference(otInstance * aInstance)61 otRoutePreference otBorderRoutingGetRouteInfoOptionPreference(otInstance *aInstance)
62 {
63     return static_cast<otRoutePreference>(
64         AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetRouteInfoOptionPreference());
65 }
66 
otBorderRoutingSetRouteInfoOptionPreference(otInstance * aInstance,otRoutePreference aPreference)67 void otBorderRoutingSetRouteInfoOptionPreference(otInstance *aInstance, otRoutePreference aPreference)
68 {
69     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetRouteInfoOptionPreference(
70         static_cast<NetworkData::RoutePreference>(aPreference));
71 }
72 
otBorderRoutingClearRouteInfoOptionPreference(otInstance * aInstance)73 void otBorderRoutingClearRouteInfoOptionPreference(otInstance *aInstance)
74 {
75     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().ClearRouteInfoOptionPreference();
76 }
77 
otBorderRoutingSetExtraRouterAdvertOptions(otInstance * aInstance,const uint8_t * aOptions,uint16_t aLength)78 otError otBorderRoutingSetExtraRouterAdvertOptions(otInstance *aInstance, const uint8_t *aOptions, uint16_t aLength)
79 {
80     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetExtraRouterAdvertOptions(aOptions, aLength);
81 }
82 
otBorderRoutingGetRoutePreference(otInstance * aInstance)83 otRoutePreference otBorderRoutingGetRoutePreference(otInstance *aInstance)
84 {
85     return static_cast<otRoutePreference>(
86         AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetRoutePreference());
87 }
88 
otBorderRoutingSetRoutePreference(otInstance * aInstance,otRoutePreference aPreference)89 void otBorderRoutingSetRoutePreference(otInstance *aInstance, otRoutePreference aPreference)
90 {
91     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetRoutePreference(
92         static_cast<NetworkData::RoutePreference>(aPreference));
93 }
94 
otBorderRoutingClearRoutePreference(otInstance * aInstance)95 void otBorderRoutingClearRoutePreference(otInstance *aInstance)
96 {
97     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().ClearRoutePreference();
98 }
99 
otBorderRoutingGetOmrPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)100 otError otBorderRoutingGetOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
101 {
102     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetOmrPrefix(AsCoreType(aPrefix));
103 }
104 
105 #if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
otBorderRoutingGetPdOmrPrefix(otInstance * aInstance,otBorderRoutingPrefixTableEntry * aPrefixInfo)106 otError otBorderRoutingGetPdOmrPrefix(otInstance *aInstance, otBorderRoutingPrefixTableEntry *aPrefixInfo)
107 {
108     AssertPointerIsNotNull(aPrefixInfo);
109 
110     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetPdOmrPrefix(*aPrefixInfo);
111 }
112 
otBorderRoutingGetPdProcessedRaInfo(otInstance * aInstance,otPdProcessedRaInfo * aPdProcessedRaInfo)113 otError otBorderRoutingGetPdProcessedRaInfo(otInstance *aInstance, otPdProcessedRaInfo *aPdProcessedRaInfo)
114 {
115     AssertPointerIsNotNull(aPdProcessedRaInfo);
116 
117     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetPdProcessedRaInfo(*aPdProcessedRaInfo);
118 }
119 #endif
120 
otBorderRoutingGetFavoredOmrPrefix(otInstance * aInstance,otIp6Prefix * aPrefix,otRoutePreference * aPreference)121 otError otBorderRoutingGetFavoredOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix, otRoutePreference *aPreference)
122 {
123     otError                                       error;
124     BorderRouter::RoutingManager::RoutePreference preference;
125 
126     AssertPointerIsNotNull(aPreference);
127 
128     SuccessOrExit(error = AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredOmrPrefix(
129                       AsCoreType(aPrefix), preference));
130     *aPreference = static_cast<otRoutePreference>(preference);
131 
132 exit:
133     return error;
134 }
135 
otBorderRoutingGetOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)136 otError otBorderRoutingGetOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
137 {
138     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetOnLinkPrefix(AsCoreType(aPrefix));
139 }
140 
otBorderRoutingGetFavoredOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)141 otError otBorderRoutingGetFavoredOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
142 {
143     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredOnLinkPrefix(AsCoreType(aPrefix));
144 }
145 
146 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otBorderRoutingGetNat64Prefix(otInstance * aInstance,otIp6Prefix * aPrefix)147 otError otBorderRoutingGetNat64Prefix(otInstance *aInstance, otIp6Prefix *aPrefix)
148 {
149     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNat64Prefix(AsCoreType(aPrefix));
150 }
151 
otBorderRoutingGetFavoredNat64Prefix(otInstance * aInstance,otIp6Prefix * aPrefix,otRoutePreference * aPreference)152 otError otBorderRoutingGetFavoredNat64Prefix(otInstance        *aInstance,
153                                              otIp6Prefix       *aPrefix,
154                                              otRoutePreference *aPreference)
155 {
156     otError                                       error;
157     BorderRouter::RoutingManager::RoutePreference preference;
158 
159     AssertPointerIsNotNull(aPreference);
160 
161     SuccessOrExit(error = AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredNat64Prefix(
162                       AsCoreType(aPrefix), preference));
163     *aPreference = static_cast<otRoutePreference>(preference);
164 
165 exit:
166     return error;
167 }
168 #endif
169 
otBorderRoutingPrefixTableInitIterator(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator)170 void otBorderRoutingPrefixTableInitIterator(otInstance *aInstance, otBorderRoutingPrefixTableIterator *aIterator)
171 {
172     AssertPointerIsNotNull(aIterator);
173 
174     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().InitPrefixTableIterator(*aIterator);
175 }
176 
otBorderRoutingGetNextPrefixTableEntry(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator,otBorderRoutingPrefixTableEntry * aEntry)177 otError otBorderRoutingGetNextPrefixTableEntry(otInstance                         *aInstance,
178                                                otBorderRoutingPrefixTableIterator *aIterator,
179                                                otBorderRoutingPrefixTableEntry    *aEntry)
180 {
181     AssertPointerIsNotNull(aIterator);
182     AssertPointerIsNotNull(aEntry);
183 
184     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNextPrefixTableEntry(*aIterator, *aEntry);
185 }
186 
otBorderRoutingGetNextRouterEntry(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator,otBorderRoutingRouterEntry * aEntry)187 otError otBorderRoutingGetNextRouterEntry(otInstance                         *aInstance,
188                                           otBorderRoutingPrefixTableIterator *aIterator,
189                                           otBorderRoutingRouterEntry         *aEntry)
190 {
191     AssertPointerIsNotNull(aIterator);
192     AssertPointerIsNotNull(aEntry);
193 
194     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNextRouterEntry(*aIterator, *aEntry);
195 }
196 
197 #if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
otBorderRoutingDhcp6PdSetEnabled(otInstance * aInstance,bool aEnabled)198 void otBorderRoutingDhcp6PdSetEnabled(otInstance *aInstance, bool aEnabled)
199 {
200     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetDhcp6PdEnabled(aEnabled);
201 }
202 
otBorderRoutingDhcp6PdGetState(otInstance * aInstance)203 otBorderRoutingDhcp6PdState otBorderRoutingDhcp6PdGetState(otInstance *aInstance)
204 {
205     return static_cast<otBorderRoutingDhcp6PdState>(
206         AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetDhcp6PdState());
207 }
208 
otBorderRoutingDhcp6PdSetRequestCallback(otInstance * aInstance,otBorderRoutingRequestDhcp6PdCallback aCallback,void * aContext)209 void otBorderRoutingDhcp6PdSetRequestCallback(otInstance                           *aInstance,
210                                               otBorderRoutingRequestDhcp6PdCallback aCallback,
211                                               void                                 *aContext)
212 {
213     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetRequestDhcp6PdCallback(aCallback, aContext);
214 }
215 
216 #endif
217 
218 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
219