1 /*
2 * Copyright (c) 2022, 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 APIs for handling IPv4 (NAT64) messages
32 */
33
34 #include "openthread-core-config.h"
35
36 #include "instance/instance.hpp"
37
38 using namespace ot;
39
40 // Note: We support the following scenarios:
41 // - Using OpenThread's routing manager, while using external NAT64 translator (like tayga).
42 // - Using OpenThread's NAT64 translator, while using external routing manager.
43 // So OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE translator and OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE are two
44 // separate build flags and they are not depending on each other.
45
46 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
otNat64SetIp4Cidr(otInstance * aInstance,const otIp4Cidr * aCidr)47 otError otNat64SetIp4Cidr(otInstance *aInstance, const otIp4Cidr *aCidr)
48 {
49 return AsCoreType(aInstance).Get<Nat64::Translator>().SetIp4Cidr(AsCoreType(aCidr));
50 }
51
otNat64ClearIp4Cidr(otInstance * aInstance)52 void otNat64ClearIp4Cidr(otInstance *aInstance) { AsCoreType(aInstance).Get<Nat64::Translator>().ClearIp4Cidr(); }
53
otIp4NewMessage(otInstance * aInstance,const otMessageSettings * aSettings)54 otMessage *otIp4NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
55 {
56 return AsCoreType(aInstance).Get<Nat64::Translator>().NewIp4Message(Message::Settings::From(aSettings));
57 }
58
otNat64Send(otInstance * aInstance,otMessage * aMessage)59 otError otNat64Send(otInstance *aInstance, otMessage *aMessage)
60 {
61 return AsCoreType(aInstance).Get<Nat64::Translator>().SendMessage(AsCoreType(aMessage));
62 }
63
otNat64SetReceiveIp4Callback(otInstance * aInstance,otNat64ReceiveIp4Callback aCallback,void * aContext)64 void otNat64SetReceiveIp4Callback(otInstance *aInstance, otNat64ReceiveIp4Callback aCallback, void *aContext)
65 {
66 AsCoreType(aInstance).Get<Ip6::Ip6>().SetNat64ReceiveIp4Callback(aCallback, aContext);
67 }
68
otNat64InitAddressMappingIterator(otInstance * aInstance,otNat64AddressMappingIterator * aIterator)69 void otNat64InitAddressMappingIterator(otInstance *aInstance, otNat64AddressMappingIterator *aIterator)
70 {
71 AssertPointerIsNotNull(aIterator);
72
73 AsCoreType(aInstance).Get<Nat64::Translator>().InitAddressMappingIterator(*aIterator);
74 }
75
otNat64GetNextAddressMapping(otInstance * aInstance,otNat64AddressMappingIterator * aIterator,otNat64AddressMapping * aMapping)76 otError otNat64GetNextAddressMapping(otInstance *aInstance,
77 otNat64AddressMappingIterator *aIterator,
78 otNat64AddressMapping *aMapping)
79 {
80 AssertPointerIsNotNull(aIterator);
81 AssertPointerIsNotNull(aMapping);
82
83 return AsCoreType(aInstance).Get<Nat64::Translator>().GetNextAddressMapping(*aIterator, *aMapping);
84 }
85
otNat64GetCounters(otInstance * aInstance,otNat64ProtocolCounters * aCounters)86 void otNat64GetCounters(otInstance *aInstance, otNat64ProtocolCounters *aCounters)
87 {
88 AsCoreType(aInstance).Get<Nat64::Translator>().GetCounters(AsCoreType(aCounters));
89 }
90
otNat64GetErrorCounters(otInstance * aInstance,otNat64ErrorCounters * aCounters)91 void otNat64GetErrorCounters(otInstance *aInstance, otNat64ErrorCounters *aCounters)
92 {
93 AsCoreType(aInstance).Get<Nat64::Translator>().GetErrorCounters(AsCoreType(aCounters));
94 }
95
otNat64GetCidr(otInstance * aInstance,otIp4Cidr * aCidr)96 otError otNat64GetCidr(otInstance *aInstance, otIp4Cidr *aCidr)
97 {
98 return AsCoreType(aInstance).Get<Nat64::Translator>().GetIp4Cidr(AsCoreType(aCidr));
99 }
100
otNat64GetTranslatorState(otInstance * aInstance)101 otNat64State otNat64GetTranslatorState(otInstance *aInstance)
102 {
103 return MapEnum(AsCoreType(aInstance).Get<Nat64::Translator>().GetState());
104 }
105 #endif // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
106
107 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otNat64GetPrefixManagerState(otInstance * aInstance)108 otNat64State otNat64GetPrefixManagerState(otInstance *aInstance)
109 {
110 return MapEnum(AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNat64PrefixManagerState());
111 }
112 #endif
113
114 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE || OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otNat64SetEnabled(otInstance * aInstance,bool aEnabled)115 void otNat64SetEnabled(otInstance *aInstance, bool aEnabled)
116 {
117 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
118 AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetNat64PrefixManagerEnabled(aEnabled);
119 #endif
120 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
121 AsCoreType(aInstance).Get<Nat64::Translator>().SetEnabled(aEnabled);
122 #endif
123 }
124 #endif // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE || OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
125
otIp4IsAddressEqual(const otIp4Address * aFirst,const otIp4Address * aSecond)126 bool otIp4IsAddressEqual(const otIp4Address *aFirst, const otIp4Address *aSecond)
127 {
128 return AsCoreType(aFirst) == AsCoreType(aSecond);
129 }
130
otIp4ExtractFromIp6Address(uint8_t aPrefixLength,const otIp6Address * aIp6Address,otIp4Address * aIp4Address)131 void otIp4ExtractFromIp6Address(uint8_t aPrefixLength, const otIp6Address *aIp6Address, otIp4Address *aIp4Address)
132 {
133 AsCoreType(aIp4Address).ExtractFromIp6Address(aPrefixLength, AsCoreType(aIp6Address));
134 }
135
otIp4FromIp4MappedIp6Address(const otIp6Address * aIp6Address,otIp4Address * aIp4Address)136 otError otIp4FromIp4MappedIp6Address(const otIp6Address *aIp6Address, otIp4Address *aIp4Address)
137 {
138 return AsCoreType(aIp4Address).ExtractFromIp4MappedIp6Address(AsCoreType(aIp6Address));
139 }
140
otIp4ToIp4MappedIp6Address(const otIp4Address * aIp4Address,otIp6Address * aIp6Address)141 void otIp4ToIp4MappedIp6Address(const otIp4Address *aIp4Address, otIp6Address *aIp6Address)
142 {
143 AsCoreType(aIp6Address).SetToIp4Mapped(AsCoreType(aIp4Address));
144 }
145
otIp4AddressFromString(const char * aString,otIp4Address * aAddress)146 otError otIp4AddressFromString(const char *aString, otIp4Address *aAddress)
147 {
148 AssertPointerIsNotNull(aString);
149 return AsCoreType(aAddress).FromString(aString);
150 }
151
otNat64SynthesizeIp6Address(otInstance * aInstance,const otIp4Address * aIp4Address,otIp6Address * aIp6Address)152 otError otNat64SynthesizeIp6Address(otInstance *aInstance, const otIp4Address *aIp4Address, otIp6Address *aIp6Address)
153 {
154 otError err = OT_ERROR_NONE;
155 NetworkData::ExternalRouteConfig nat64Prefix;
156
157 VerifyOrExit(AsCoreType(aInstance).Get<NetworkData::Leader>().GetPreferredNat64Prefix(nat64Prefix) == OT_ERROR_NONE,
158 err = OT_ERROR_INVALID_STATE);
159 AsCoreType(aIp6Address).SynthesizeFromIp4Address(nat64Prefix.GetPrefix(), AsCoreType(aIp4Address));
160
161 exit:
162 return err;
163 }
164
otIp4AddressToString(const otIp4Address * aAddress,char * aBuffer,uint16_t aSize)165 void otIp4AddressToString(const otIp4Address *aAddress, char *aBuffer, uint16_t aSize)
166 {
167 AssertPointerIsNotNull(aBuffer);
168
169 AsCoreType(aAddress).ToString(aBuffer, aSize);
170 }
171
otIp4CidrFromString(const char * aString,otIp4Cidr * aCidr)172 otError otIp4CidrFromString(const char *aString, otIp4Cidr *aCidr) { return AsCoreType(aCidr).FromString(aString); }
173
otIp4CidrToString(const otIp4Cidr * aCidr,char * aBuffer,uint16_t aSize)174 void otIp4CidrToString(const otIp4Cidr *aCidr, char *aBuffer, uint16_t aSize)
175 {
176 AssertPointerIsNotNull(aBuffer);
177
178 AsCoreType(aCidr).ToString(aBuffer, aSize);
179 }
180