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
otIp4NewMessage(otInstance * aInstance,const otMessageSettings * aSettings)61 otMessage *otIp4NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
62 {
63 return AsCoreType(aInstance).Get<Nat64::Translator>().NewIp4Message(Message::Settings::From(aSettings));
64 }
65
otNat64Send(otInstance * aInstance,otMessage * aMessage)66 otError otNat64Send(otInstance *aInstance, otMessage *aMessage)
67 {
68 return AsCoreType(aInstance).Get<Nat64::Translator>().SendMessage(AsCoreType(aMessage));
69 }
70
otNat64SetReceiveIp4Callback(otInstance * aInstance,otNat64ReceiveIp4Callback aCallback,void * aContext)71 void otNat64SetReceiveIp4Callback(otInstance *aInstance, otNat64ReceiveIp4Callback aCallback, void *aContext)
72 {
73 AsCoreType(aInstance).Get<Ip6::Ip6>().SetNat64ReceiveIp4DatagramCallback(aCallback, aContext);
74 }
75
otNat64InitAddressMappingIterator(otInstance * aInstance,otNat64AddressMappingIterator * aIterator)76 void otNat64InitAddressMappingIterator(otInstance *aInstance, otNat64AddressMappingIterator *aIterator)
77 {
78 AssertPointerIsNotNull(aIterator);
79
80 AsCoreType(aInstance).Get<Nat64::Translator>().InitAddressMappingIterator(*aIterator);
81 }
82
otNat64GetNextAddressMapping(otInstance * aInstance,otNat64AddressMappingIterator * aIterator,otNat64AddressMapping * aMapping)83 otError otNat64GetNextAddressMapping(otInstance *aInstance,
84 otNat64AddressMappingIterator *aIterator,
85 otNat64AddressMapping *aMapping)
86 {
87 AssertPointerIsNotNull(aIterator);
88 AssertPointerIsNotNull(aMapping);
89
90 return AsCoreType(aInstance).Get<Nat64::Translator>().GetNextAddressMapping(*aIterator, *aMapping);
91 }
92
otNat64GetCounters(otInstance * aInstance,otNat64ProtocolCounters * aCounters)93 void otNat64GetCounters(otInstance *aInstance, otNat64ProtocolCounters *aCounters)
94 {
95 AsCoreType(aInstance).Get<Nat64::Translator>().GetCounters(AsCoreType(aCounters));
96 }
97
otNat64GetErrorCounters(otInstance * aInstance,otNat64ErrorCounters * aCounters)98 void otNat64GetErrorCounters(otInstance *aInstance, otNat64ErrorCounters *aCounters)
99 {
100 AsCoreType(aInstance).Get<Nat64::Translator>().GetErrorCounters(AsCoreType(aCounters));
101 }
102
otNat64GetCidr(otInstance * aInstance,otIp4Cidr * aCidr)103 otError otNat64GetCidr(otInstance *aInstance, otIp4Cidr *aCidr)
104 {
105 return AsCoreType(aInstance).Get<Nat64::Translator>().GetIp4Cidr(AsCoreType(aCidr));
106 }
107
otNat64GetTranslatorState(otInstance * aInstance)108 otNat64State otNat64GetTranslatorState(otInstance *aInstance)
109 {
110 return MapEnum(AsCoreType(aInstance).Get<Nat64::Translator>().GetState());
111 }
112 #endif // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
113
114 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otNat64GetPrefixManagerState(otInstance * aInstance)115 otNat64State otNat64GetPrefixManagerState(otInstance *aInstance)
116 {
117 return MapEnum(AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().GetNat64PrefixManagerState());
118 }
119 #endif
120
121 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE || OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
otNat64SetEnabled(otInstance * aInstance,bool aEnabled)122 void otNat64SetEnabled(otInstance *aInstance, bool aEnabled)
123 {
124 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
125 AsCoreType(aInstance).Get<BorderRouter::RoutingManager>().SetNat64PrefixManagerEnabled(aEnabled);
126 #endif
127 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
128 AsCoreType(aInstance).Get<Nat64::Translator>().SetEnabled(aEnabled);
129 #endif
130 }
131 #endif // OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE || OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
132
otIp4IsAddressEqual(const otIp4Address * aFirst,const otIp4Address * aSecond)133 bool otIp4IsAddressEqual(const otIp4Address *aFirst, const otIp4Address *aSecond)
134 {
135 return AsCoreType(aFirst) == AsCoreType(aSecond);
136 }
137
otIp4ExtractFromIp6Address(uint8_t aPrefixLength,const otIp6Address * aIp6Address,otIp4Address * aIp4Address)138 void otIp4ExtractFromIp6Address(uint8_t aPrefixLength, const otIp6Address *aIp6Address, otIp4Address *aIp4Address)
139 {
140 AsCoreType(aIp4Address).ExtractFromIp6Address(aPrefixLength, AsCoreType(aIp6Address));
141 }
142
otIp4AddressFromString(const char * aString,otIp4Address * aAddress)143 otError otIp4AddressFromString(const char *aString, otIp4Address *aAddress)
144 {
145 AssertPointerIsNotNull(aString);
146 return AsCoreType(aAddress).FromString(aString);
147 }
148
otNat64SynthesizeIp6Address(otInstance * aInstance,const otIp4Address * aIp4Address,otIp6Address * aIp6Address)149 otError otNat64SynthesizeIp6Address(otInstance *aInstance, const otIp4Address *aIp4Address, otIp6Address *aIp6Address)
150 {
151 otError err = OT_ERROR_NONE;
152 NetworkData::ExternalRouteConfig nat64Prefix;
153
154 VerifyOrExit(AsCoreType(aInstance).Get<NetworkData::Leader>().GetPreferredNat64Prefix(nat64Prefix) == OT_ERROR_NONE,
155 err = OT_ERROR_INVALID_STATE);
156 AsCoreType(aIp6Address).SynthesizeFromIp4Address(nat64Prefix.GetPrefix(), AsCoreType(aIp4Address));
157
158 exit:
159 return err;
160 }
161
otIp4AddressToString(const otIp4Address * aAddress,char * aBuffer,uint16_t aSize)162 void otIp4AddressToString(const otIp4Address *aAddress, char *aBuffer, uint16_t aSize)
163 {
164 AssertPointerIsNotNull(aBuffer);
165
166 AsCoreType(aAddress).ToString(aBuffer, aSize);
167 }
168
otIp4CidrFromString(const char * aString,otIp4Cidr * aCidr)169 otError otIp4CidrFromString(const char *aString, otIp4Cidr *aCidr) { return AsCoreType(aCidr).FromString(aString); }
170
otIp4CidrToString(const otIp4Cidr * aCidr,char * aBuffer,uint16_t aSize)171 void otIp4CidrToString(const otIp4Cidr *aCidr, char *aBuffer, uint16_t aSize)
172 {
173 AssertPointerIsNotNull(aBuffer);
174
175 AsCoreType(aCidr).ToString(aBuffer, aSize);
176 }
177