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" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /**
29 * @file
30 * This file implements infrastructure network interface.
31 */
32
33 #include "infra_if.hpp"
34 #include "common/num_utils.hpp"
35
36 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
37
38 #include "border_router/routing_manager.hpp"
39 #include "common/as_core_type.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/logging.hpp"
42 #include "instance/instance.hpp"
43 #include "net/icmp6.hpp"
44
45 namespace ot {
46 namespace BorderRouter {
47
48 RegisterLogModule("InfraIf");
49
InfraIf(Instance & aInstance)50 InfraIf::InfraIf(Instance &aInstance)
51 : InstanceLocator(aInstance)
52 , mInitialized(false)
53 , mIsRunning(false)
54 , mIfIndex(0)
55 {
56 }
57
Init(uint32_t aIfIndex)58 Error InfraIf::Init(uint32_t aIfIndex)
59 {
60 Error error = kErrorNone;
61
62 VerifyOrExit(!mInitialized, error = kErrorInvalidState);
63
64 mIfIndex = aIfIndex;
65 mInitialized = true;
66
67 LogInfo("Init %s", ToString().AsCString());
68
69 exit:
70 return error;
71 }
72
Deinit(void)73 void InfraIf::Deinit(void)
74 {
75 mInitialized = false;
76 mIsRunning = false;
77 mIfIndex = 0;
78
79 LogInfo("Deinit");
80 }
81
HasAddress(const Ip6::Address & aAddress) const82 bool InfraIf::HasAddress(const Ip6::Address &aAddress) const
83 {
84 OT_ASSERT(mInitialized);
85
86 return otPlatInfraIfHasAddress(mIfIndex, &aAddress);
87 }
88
Send(const Icmp6Packet & aPacket,const Ip6::Address & aDestination) const89 Error InfraIf::Send(const Icmp6Packet &aPacket, const Ip6::Address &aDestination) const
90 {
91 OT_ASSERT(mInitialized);
92
93 return otPlatInfraIfSendIcmp6Nd(mIfIndex, &aDestination, aPacket.GetBytes(), aPacket.GetLength());
94 }
95
HandledReceived(uint32_t aIfIndex,const Ip6::Address & aSource,const Icmp6Packet & aPacket)96 void InfraIf::HandledReceived(uint32_t aIfIndex, const Ip6::Address &aSource, const Icmp6Packet &aPacket)
97 {
98 Error error = kErrorNone;
99
100 VerifyOrExit(mInitialized && mIsRunning, error = kErrorInvalidState);
101 VerifyOrExit(aIfIndex == mIfIndex, error = kErrorDrop);
102 VerifyOrExit(aPacket.GetBytes() != nullptr, error = kErrorInvalidArgs);
103 VerifyOrExit(aPacket.GetLength() >= sizeof(Ip6::Icmp::Header), error = kErrorParse);
104
105 Get<RoutingManager>().HandleReceived(aPacket, aSource);
106
107 exit:
108 if (error != kErrorNone)
109 {
110 LogDebg("Dropped ICMPv6 message: %s", ErrorToString(error));
111 }
112 }
113
DiscoverNat64Prefix(void) const114 Error InfraIf::DiscoverNat64Prefix(void) const
115 {
116 OT_ASSERT(mInitialized);
117
118 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
119 return otPlatInfraIfDiscoverNat64Prefix(mIfIndex);
120 #else
121 return kErrorNotImplemented;
122 #endif
123 }
124
DiscoverNat64PrefixDone(uint32_t aIfIndex,const Ip6::Prefix & aPrefix)125 void InfraIf::DiscoverNat64PrefixDone(uint32_t aIfIndex, const Ip6::Prefix &aPrefix)
126 {
127 Error error = kErrorNone;
128
129 OT_UNUSED_VARIABLE(aPrefix);
130
131 VerifyOrExit(mInitialized && mIsRunning, error = kErrorInvalidState);
132 VerifyOrExit(aIfIndex == mIfIndex, error = kErrorInvalidArgs);
133
134 #if OPENTHREAD_CONFIG_NAT64_BORDER_ROUTING_ENABLE
135 Get<RoutingManager>().HandleDiscoverNat64PrefixDone(aPrefix);
136 #endif
137
138 exit:
139 if (error != kErrorNone)
140 {
141 LogDebg("Failed to handle discovered NAT64 synthetic addresses: %s", ErrorToString(error));
142 }
143 }
144
HandleStateChanged(uint32_t aIfIndex,bool aIsRunning)145 Error InfraIf::HandleStateChanged(uint32_t aIfIndex, bool aIsRunning)
146 {
147 Error error = kErrorNone;
148
149 VerifyOrExit(mInitialized, error = kErrorInvalidState);
150 VerifyOrExit(aIfIndex == mIfIndex, error = kErrorInvalidArgs);
151
152 VerifyOrExit(aIsRunning != mIsRunning);
153 LogInfo("State changed: %sRUNNING -> %sRUNNING", mIsRunning ? "" : "NOT ", aIsRunning ? "" : "NOT ");
154
155 mIsRunning = aIsRunning;
156
157 Get<RoutingManager>().HandleInfraIfStateChanged();
158
159 #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE
160 Get<Srp::AdvertisingProxy>().HandleInfraIfStateChanged();
161 #endif
162
163 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE && OPENTHREAD_CONFIG_DNSSD_DISCOVERY_PROXY_ENABLE
164 Get<Dns::ServiceDiscovery::Server>().HandleInfraIfStateChanged();
165 #endif
166
167 #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE && OPENTHREAD_CONFIG_MULTICAST_DNS_AUTO_ENABLE_ON_INFRA_IF
168 Get<Dns::Multicast::Core>().HandleInfraIfStateChanged();
169 #endif
170
171 exit:
172 return error;
173 }
174
ToString(void) const175 InfraIf::InfoString InfraIf::ToString(void) const
176 {
177 InfoString string;
178
179 string.Append("infra netif %lu", ToUlong(mIfIndex));
180 return string;
181 }
182
183 //---------------------------------------------------------------------------------------------------------------------
184
otPlatInfraIfRecvIcmp6Nd(otInstance * aInstance,uint32_t aInfraIfIndex,const otIp6Address * aSrcAddress,const uint8_t * aBuffer,uint16_t aBufferLength)185 extern "C" void otPlatInfraIfRecvIcmp6Nd(otInstance *aInstance,
186 uint32_t aInfraIfIndex,
187 const otIp6Address *aSrcAddress,
188 const uint8_t *aBuffer,
189 uint16_t aBufferLength)
190 {
191 InfraIf::Icmp6Packet packet;
192
193 packet.Init(aBuffer, aBufferLength);
194 AsCoreType(aInstance).Get<InfraIf>().HandledReceived(aInfraIfIndex, AsCoreType(aSrcAddress), packet);
195 }
196
otPlatInfraIfStateChanged(otInstance * aInstance,uint32_t aInfraIfIndex,bool aIsRunning)197 extern "C" otError otPlatInfraIfStateChanged(otInstance *aInstance, uint32_t aInfraIfIndex, bool aIsRunning)
198 {
199 return AsCoreType(aInstance).Get<InfraIf>().HandleStateChanged(aInfraIfIndex, aIsRunning);
200 }
201
otPlatInfraIfDiscoverNat64PrefixDone(otInstance * aInstance,uint32_t aInfraIfIndex,const otIp6Prefix * aIp6Prefix)202 extern "C" void otPlatInfraIfDiscoverNat64PrefixDone(otInstance *aInstance,
203 uint32_t aInfraIfIndex,
204 const otIp6Prefix *aIp6Prefix)
205 {
206 AsCoreType(aInstance).Get<InfraIf>().DiscoverNat64PrefixDone(aInfraIfIndex, AsCoreType(aIp6Prefix));
207 }
208
209 } // namespace BorderRouter
210 } // namespace ot
211
212 //---------------------------------------------------------------------------------------------------------------------
213
214 #if OPENTHREAD_CONFIG_BORDER_ROUTING_MOCK_PLAT_APIS_ENABLE
otPlatInfraIfHasAddress(uint32_t,const otIp6Address *)215 OT_TOOL_WEAK bool otPlatInfraIfHasAddress(uint32_t, const otIp6Address *) { return false; }
216
otPlatInfraIfSendIcmp6Nd(uint32_t,const otIp6Address *,const uint8_t *,uint16_t)217 OT_TOOL_WEAK otError otPlatInfraIfSendIcmp6Nd(uint32_t, const otIp6Address *, const uint8_t *, uint16_t)
218 {
219 return OT_ERROR_FAILED;
220 }
221
otPlatInfraIfDiscoverNat64Prefix(uint32_t)222 OT_TOOL_WEAK otError otPlatInfraIfDiscoverNat64Prefix(uint32_t) { return OT_ERROR_FAILED; }
223 #endif
224
225 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
226