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 "common/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 #endif
108 
otBorderRoutingGetFavoredOmrPrefix(otInstance * aInstance,otIp6Prefix * aPrefix,otRoutePreference * aPreference)109 otError otBorderRoutingGetFavoredOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix, otRoutePreference *aPreference)
110 {
111     otError                                       error;
112     BorderRouter::RoutingManager::RoutePreference preference;
113 
114     AssertPointerIsNotNull(aPreference);
115 
116     SuccessOrExit(error = AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredOmrPrefix(
117                       AsCoreType(aPrefix), preference));
118     *aPreference = static_cast<otRoutePreference>(preference);
119 
120 exit:
121     return error;
122 }
123 
otBorderRoutingGetOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)124 otError otBorderRoutingGetOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
125 {
126     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetOnLinkPrefix(AsCoreType(aPrefix));
127 }
128 
otBorderRoutingGetFavoredOnLinkPrefix(otInstance * aInstance,otIp6Prefix * aPrefix)129 otError otBorderRoutingGetFavoredOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix)
130 {
131     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredOnLinkPrefix(AsCoreType(aPrefix));
132 }
133 
134 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otBorderRoutingGetNat64Prefix(otInstance * aInstance,otIp6Prefix * aPrefix)135 otError otBorderRoutingGetNat64Prefix(otInstance *aInstance, otIp6Prefix *aPrefix)
136 {
137     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNat64Prefix(AsCoreType(aPrefix));
138 }
139 
otBorderRoutingGetFavoredNat64Prefix(otInstance * aInstance,otIp6Prefix * aPrefix,otRoutePreference * aPreference)140 otError otBorderRoutingGetFavoredNat64Prefix(otInstance        *aInstance,
141                                              otIp6Prefix       *aPrefix,
142                                              otRoutePreference *aPreference)
143 {
144     otError                                       error;
145     BorderRouter::RoutingManager::RoutePreference preference;
146 
147     AssertPointerIsNotNull(aPreference);
148 
149     SuccessOrExit(error = AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetFavoredNat64Prefix(
150                       AsCoreType(aPrefix), preference));
151     *aPreference = static_cast<otRoutePreference>(preference);
152 
153 exit:
154     return error;
155 }
156 #endif
157 
otBorderRoutingPrefixTableInitIterator(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator)158 void otBorderRoutingPrefixTableInitIterator(otInstance *aInstance, otBorderRoutingPrefixTableIterator *aIterator)
159 {
160     AssertPointerIsNotNull(aIterator);
161 
162     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().InitPrefixTableIterator(*aIterator);
163 }
164 
otBorderRoutingGetNextPrefixTableEntry(otInstance * aInstance,otBorderRoutingPrefixTableIterator * aIterator,otBorderRoutingPrefixTableEntry * aEntry)165 otError otBorderRoutingGetNextPrefixTableEntry(otInstance                         *aInstance,
166                                                otBorderRoutingPrefixTableIterator *aIterator,
167                                                otBorderRoutingPrefixTableEntry    *aEntry)
168 {
169     AssertPointerIsNotNull(aIterator);
170     AssertPointerIsNotNull(aEntry);
171 
172     return AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNextPrefixTableEntry(*aIterator, *aEntry);
173 }
174 
175 #if OPENTHREAD_CONFIG_BORDER_ROUTING_DHCP6_PD_ENABLE
otBorderRoutingDhcp6PdSetEnabled(otInstance * aInstance,bool aEnabled)176 void otBorderRoutingDhcp6PdSetEnabled(otInstance *aInstance, bool aEnabled)
177 {
178     AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetDhcp6PdEnabled(aEnabled);
179 }
180 
otBorderRoutingDhcp6PdGetState(otInstance * aInstance)181 otBorderRoutingDhcp6PdState otBorderRoutingDhcp6PdGetState(otInstance *aInstance)
182 {
183     return static_cast<otBorderRoutingDhcp6PdState>(
184         AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetDhcp6PdState());
185 }
186 #endif
187 
188 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
189