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 
otBorderRoutingGetRoutePreference(otInstance * aInstance)78 otRoutePreference otBorderRoutingGetRoutePreference(otInstance *aInstance)
79 {
80     return static_cast<otRoutePreference>(
81         AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetRoutePreference());
82 }
83 
otBorderRoutingSetRoutePreference(otInstance * aInstance,otRoutePreference aPreference)84 void otBorderRoutingSetRoutePreference(otInstance *aInstance, otRoutePreference aPreference)
85 {
86     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetRoutePreference(
87         static_cast<NetworkData::RoutePreference>(aPreference));
88 }
89 
otBorderRoutingClearRoutePreference(otInstance * aInstance)90 void otBorderRoutingClearRoutePreference(otInstance *aInstance)
91 {
92     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().ClearRoutePreference();
93 }
94 
otBorderRoutingGetOmrPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)95 otError otBorderRoutingGetOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
96 {
97     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetOmrPrefix(AsCoreType(aPrefix));
98 }
99 
100 #if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
otBorderRoutingGetPdOmrPrefix(otInstance * aInstance,otBorderRoutingPrefixTableEntry * aPrefixInfo)101 otError otBorderRoutingGetPdOmrPrefix(otInstance *aInstance, otBorderRoutingPrefixTableEntry *aPrefixInfo)
102 {
103     AssertPointerIsNotNull(aPrefixInfo);
104 
105     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetPdOmrPrefix(*aPrefixInfo);
106 }
107 
otBorderRoutingGetPdProcessedRaInfo(otInstance * aInstance,otPdProcessedRaInfo * aPdProcessedRaInfo)108 otError otBorderRoutingGetPdProcessedRaInfo(otInstance *aInstance, otPdProcessedRaInfo *aPdProcessedRaInfo)
109 {
110     AssertPointerIsNotNull(aPdProcessedRaInfo);
111 
112     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetPdProcessedRaInfo(*aPdProcessedRaInfo);
113 }
114 #endif
115 
otBorderRoutingGetFavoredOmrPrefix(otInstance * aInstance,otIp6Prefix * aPrefix,otRoutePreference * aPreference)116 otError otBorderRoutingGetFavoredOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix, otRoutePreference *aPreference)
117 {
118     otError                                       error;
119     BorderRouter::RoutingManager::RoutePreference preference;
120 
121     AssertPointerIsNotNull(aPreference);
122 
123     SuccessOrExit(error = AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredOmrPrefix(
124                       AsCoreType(aPrefix), preference));
125     *aPreference = static_cast<otRoutePreference>(preference);
126 
127 exit:
128     return error;
129 }
130 
otBorderRoutingGetOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)131 otError otBorderRoutingGetOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
132 {
133     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetOnLinkPrefix(AsCoreType(aPrefix));
134 }
135 
otBorderRoutingGetFavoredOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)136 otError otBorderRoutingGetFavoredOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
137 {
138     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredOnLinkPrefix(AsCoreType(aPrefix));
139 }
140 
141 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otBorderRoutingGetNat64Prefix(otInstance * aInstance,otIp6Prefix * aPrefix)142 otError otBorderRoutingGetNat64Prefix(otInstance *aInstance, otIp6Prefix *aPrefix)
143 {
144     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNat64Prefix(AsCoreType(aPrefix));
145 }
146 
otBorderRoutingGetFavoredNat64Prefix(otInstance * aInstance,otIp6Prefix * aPrefix,otRoutePreference * aPreference)147 otError otBorderRoutingGetFavoredNat64Prefix(otInstance        *aInstance,
148                                              otIp6Prefix       *aPrefix,
149                                              otRoutePreference *aPreference)
150 {
151     otError                                       error;
152     BorderRouter::RoutingManager::RoutePreference preference;
153 
154     AssertPointerIsNotNull(aPreference);
155 
156     SuccessOrExit(error = AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredNat64Prefix(
157                       AsCoreType(aPrefix), preference));
158     *aPreference = static_cast<otRoutePreference>(preference);
159 
160 exit:
161     return error;
162 }
163 #endif
164 
otBorderRoutingPrefixTableInitIterator(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator)165 void otBorderRoutingPrefixTableInitIterator(otInstance *aInstance, otBorderRoutingPrefixTableIterator *aIterator)
166 {
167     AssertPointerIsNotNull(aIterator);
168 
169     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().InitPrefixTableIterator(*aIterator);
170 }
171 
otBorderRoutingGetNextPrefixTableEntry(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator,otBorderRoutingPrefixTableEntry * aEntry)172 otError otBorderRoutingGetNextPrefixTableEntry(otInstance                         *aInstance,
173                                                otBorderRoutingPrefixTableIterator *aIterator,
174                                                otBorderRoutingPrefixTableEntry    *aEntry)
175 {
176     AssertPointerIsNotNull(aIterator);
177     AssertPointerIsNotNull(aEntry);
178 
179     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNextPrefixTableEntry(*aIterator, *aEntry);
180 }
181 
otBorderRoutingGetNextRouterEntry(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator,otBorderRoutingRouterEntry * aEntry)182 otError otBorderRoutingGetNextRouterEntry(otInstance                         *aInstance,
183                                           otBorderRoutingPrefixTableIterator *aIterator,
184                                           otBorderRoutingRouterEntry         *aEntry)
185 {
186     AssertPointerIsNotNull(aIterator);
187     AssertPointerIsNotNull(aEntry);
188 
189     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNextRouterEntry(*aIterator, *aEntry);
190 }
191 
192 #if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
otBorderRoutingDhcp6PdSetEnabled(otInstance * aInstance,bool aEnabled)193 void otBorderRoutingDhcp6PdSetEnabled(otInstance *aInstance, bool aEnabled)
194 {
195     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetDhcp6PdEnabled(aEnabled);
196 }
197 
otBorderRoutingDhcp6PdGetState(otInstance * aInstance)198 otBorderRoutingDhcp6PdState otBorderRoutingDhcp6PdGetState(otInstance *aInstance)
199 {
200     return static_cast<otBorderRoutingDhcp6PdState>(
201         AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetDhcp6PdState());
202 }
203 #endif
204 
205 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
206