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