• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016, 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 IPv6 API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #include <openthread/ip6.h>
37 
38 #include "common/as_core_type.hpp"
39 #include "common/locator_getters.hpp"
40 #include "utils/slaac_address.hpp"
41 
42 using namespace ot;
43 
otIp6SetEnabled(otInstance * aInstance,bool aEnabled)44 otError otIp6SetEnabled(otInstance *aInstance, bool aEnabled)
45 {
46     Error     error    = kErrorNone;
47     Instance &instance = AsCoreType(aInstance);
48 
49 #if OPENTHREAD_CONFIG_LINK_RAW_ENABLE
50     VerifyOrExit(!instance.Get<Mac::LinkRaw>().IsEnabled(), error = kErrorInvalidState);
51 #endif
52 
53     if (aEnabled)
54     {
55         instance.Get<ThreadNetif>().Up();
56     }
57     else
58     {
59         instance.Get<ThreadNetif>().Down();
60     }
61 
62 #if OPENTHREAD_CONFIG_LINK_RAW_ENABLE
63 exit:
64 #endif
65     return error;
66 }
67 
otIp6IsEnabled(otInstance * aInstance)68 bool otIp6IsEnabled(otInstance *aInstance)
69 {
70     return AsCoreType(aInstance).Get<ThreadNetif>().IsUp();
71 }
72 
otIp6GetUnicastAddresses(otInstance * aInstance)73 const otNetifAddress *otIp6GetUnicastAddresses(otInstance *aInstance)
74 {
75     return AsCoreType(aInstance).Get<ThreadNetif>().GetUnicastAddresses().GetHead();
76 }
77 
otIp6AddUnicastAddress(otInstance * aInstance,const otNetifAddress * aAddress)78 otError otIp6AddUnicastAddress(otInstance *aInstance, const otNetifAddress *aAddress)
79 {
80     return AsCoreType(aInstance).Get<ThreadNetif>().AddExternalUnicastAddress(AsCoreType(aAddress));
81 }
82 
otIp6RemoveUnicastAddress(otInstance * aInstance,const otIp6Address * aAddress)83 otError otIp6RemoveUnicastAddress(otInstance *aInstance, const otIp6Address *aAddress)
84 {
85     return AsCoreType(aInstance).Get<ThreadNetif>().RemoveExternalUnicastAddress(AsCoreType(aAddress));
86 }
87 
otIp6GetMulticastAddresses(otInstance * aInstance)88 const otNetifMulticastAddress *otIp6GetMulticastAddresses(otInstance *aInstance)
89 {
90     return AsCoreType(aInstance).Get<ThreadNetif>().GetMulticastAddresses().GetHead();
91 }
92 
otIp6SubscribeMulticastAddress(otInstance * aInstance,const otIp6Address * aAddress)93 otError otIp6SubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress)
94 {
95     return AsCoreType(aInstance).Get<ThreadNetif>().SubscribeExternalMulticast(AsCoreType(aAddress));
96 }
97 
otIp6UnsubscribeMulticastAddress(otInstance * aInstance,const otIp6Address * aAddress)98 otError otIp6UnsubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aAddress)
99 {
100     return AsCoreType(aInstance).Get<ThreadNetif>().UnsubscribeExternalMulticast(AsCoreType(aAddress));
101 }
102 
otIp6IsMulticastPromiscuousEnabled(otInstance * aInstance)103 bool otIp6IsMulticastPromiscuousEnabled(otInstance *aInstance)
104 {
105     return AsCoreType(aInstance).Get<ThreadNetif>().IsMulticastPromiscuousEnabled();
106 }
107 
otIp6SetMulticastPromiscuousEnabled(otInstance * aInstance,bool aEnabled)108 void otIp6SetMulticastPromiscuousEnabled(otInstance *aInstance, bool aEnabled)
109 {
110     AsCoreType(aInstance).Get<ThreadNetif>().SetMulticastPromiscuous(aEnabled);
111 }
112 
otIp6SetReceiveCallback(otInstance * aInstance,otIp6ReceiveCallback aCallback,void * aCallbackContext)113 void otIp6SetReceiveCallback(otInstance *aInstance, otIp6ReceiveCallback aCallback, void *aCallbackContext)
114 {
115     AsCoreType(aInstance).Get<Ip6::Ip6>().SetReceiveDatagramCallback(aCallback, aCallbackContext);
116 }
117 
otIp6SetAddressCallback(otInstance * aInstance,otIp6AddressCallback aCallback,void * aCallbackContext)118 void otIp6SetAddressCallback(otInstance *aInstance, otIp6AddressCallback aCallback, void *aCallbackContext)
119 {
120     AsCoreType(aInstance).Get<ThreadNetif>().SetAddressCallback(aCallback, aCallbackContext);
121 }
122 
otIp6IsReceiveFilterEnabled(otInstance * aInstance)123 bool otIp6IsReceiveFilterEnabled(otInstance *aInstance)
124 {
125     return AsCoreType(aInstance).Get<Ip6::Ip6>().IsReceiveIp6FilterEnabled();
126 }
127 
otIp6SetReceiveFilterEnabled(otInstance * aInstance,bool aEnabled)128 void otIp6SetReceiveFilterEnabled(otInstance *aInstance, bool aEnabled)
129 {
130     AsCoreType(aInstance).Get<Ip6::Ip6>().SetReceiveIp6FilterEnabled(aEnabled);
131 }
132 
otIp6Send(otInstance * aInstance,otMessage * aMessage)133 otError otIp6Send(otInstance *aInstance, otMessage *aMessage)
134 {
135     return AsCoreType(aInstance).Get<Ip6::Ip6>().SendRaw(AsCoreType(aMessage),
136                                                          !OPENTHREAD_CONFIG_IP6_ALLOW_LOOP_BACK_HOST_DATAGRAMS);
137 }
138 
otIp6NewMessage(otInstance * aInstance,const otMessageSettings * aSettings)139 otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
140 {
141     return AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(0, Message::Settings::From(aSettings));
142 }
143 
otIp6NewMessageFromBuffer(otInstance * aInstance,const uint8_t * aData,uint16_t aDataLength,const otMessageSettings * aSettings)144 otMessage *otIp6NewMessageFromBuffer(otInstance *             aInstance,
145                                      const uint8_t *          aData,
146                                      uint16_t                 aDataLength,
147                                      const otMessageSettings *aSettings)
148 {
149     return (aSettings != nullptr)
150                ? AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(aData, aDataLength, AsCoreType(aSettings))
151                : AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(aData, aDataLength);
152 }
153 
otIp6AddUnsecurePort(otInstance * aInstance,uint16_t aPort)154 otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort)
155 {
156     return AsCoreType(aInstance).Get<Ip6::Filter>().AddUnsecurePort(aPort);
157 }
158 
otIp6RemoveUnsecurePort(otInstance * aInstance,uint16_t aPort)159 otError otIp6RemoveUnsecurePort(otInstance *aInstance, uint16_t aPort)
160 {
161     return AsCoreType(aInstance).Get<Ip6::Filter>().RemoveUnsecurePort(aPort);
162 }
163 
otIp6RemoveAllUnsecurePorts(otInstance * aInstance)164 void otIp6RemoveAllUnsecurePorts(otInstance *aInstance)
165 {
166     AsCoreType(aInstance).Get<Ip6::Filter>().RemoveAllUnsecurePorts();
167 }
168 
otIp6GetUnsecurePorts(otInstance * aInstance,uint8_t * aNumEntries)169 const uint16_t *otIp6GetUnsecurePorts(otInstance *aInstance, uint8_t *aNumEntries)
170 {
171     return AsCoreType(aInstance).Get<Ip6::Filter>().GetUnsecurePorts(*aNumEntries);
172 }
173 
otIp6IsAddressEqual(const otIp6Address * aFirst,const otIp6Address * aSecond)174 bool otIp6IsAddressEqual(const otIp6Address *aFirst, const otIp6Address *aSecond)
175 {
176     return AsCoreType(aFirst) == AsCoreType(aSecond);
177 }
178 
otIp6ArePrefixesEqual(const otIp6Prefix * aFirst,const otIp6Prefix * aSecond)179 bool otIp6ArePrefixesEqual(const otIp6Prefix *aFirst, const otIp6Prefix *aSecond)
180 {
181     return AsCoreType(aFirst) == AsCoreType(aSecond);
182 }
183 
otIp6AddressFromString(const char * aString,otIp6Address * aAddress)184 otError otIp6AddressFromString(const char *aString, otIp6Address *aAddress)
185 {
186     return AsCoreType(aAddress).FromString(aString);
187 }
188 
otIp6AddressToString(const otIp6Address * aAddress,char * aBuffer,uint16_t aSize)189 void otIp6AddressToString(const otIp6Address *aAddress, char *aBuffer, uint16_t aSize)
190 {
191     AsCoreType(aAddress).ToString(aBuffer, aSize);
192 }
193 
otIp6SockAddrToString(const otSockAddr * aSockAddr,char * aBuffer,uint16_t aSize)194 void otIp6SockAddrToString(const otSockAddr *aSockAddr, char *aBuffer, uint16_t aSize)
195 {
196     AsCoreType(aSockAddr).ToString(aBuffer, aSize);
197 }
198 
otIp6PrefixToString(const otIp6Prefix * aPrefix,char * aBuffer,uint16_t aSize)199 void otIp6PrefixToString(const otIp6Prefix *aPrefix, char *aBuffer, uint16_t aSize)
200 {
201     AsCoreType(aPrefix).ToString(aBuffer, aSize);
202 }
203 
otIp6PrefixMatch(const otIp6Address * aFirst,const otIp6Address * aSecond)204 uint8_t otIp6PrefixMatch(const otIp6Address *aFirst, const otIp6Address *aSecond)
205 {
206     OT_ASSERT(aFirst != nullptr && aSecond != nullptr);
207 
208     return AsCoreType(aFirst).PrefixMatch(AsCoreType(aSecond));
209 }
210 
otIp6IsAddressUnspecified(const otIp6Address * aAddress)211 bool otIp6IsAddressUnspecified(const otIp6Address *aAddress)
212 {
213     return AsCoreType(aAddress).IsUnspecified();
214 }
215 
otIp6SelectSourceAddress(otInstance * aInstance,otMessageInfo * aMessageInfo)216 otError otIp6SelectSourceAddress(otInstance *aInstance, otMessageInfo *aMessageInfo)
217 {
218     Error                             error = kErrorNone;
219     const Ip6::Netif::UnicastAddress *netifAddr;
220 
221     netifAddr = AsCoreType(aInstance).Get<Ip6::Ip6>().SelectSourceAddress(AsCoreType(aMessageInfo));
222     VerifyOrExit(netifAddr != nullptr, error = kErrorNotFound);
223     aMessageInfo->mSockAddr = netifAddr->GetAddress();
224 
225 exit:
226     return error;
227 }
228 
229 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
otIp6RegisterMulticastListeners(otInstance * aInstance,const otIp6Address * aAddresses,uint8_t aAddressNum,const uint32_t * aTimeout,otIp6RegisterMulticastListenersCallback aCallback,void * aContext)230 otError otIp6RegisterMulticastListeners(otInstance *                            aInstance,
231                                         const otIp6Address *                    aAddresses,
232                                         uint8_t                                 aAddressNum,
233                                         const uint32_t *                        aTimeout,
234                                         otIp6RegisterMulticastListenersCallback aCallback,
235                                         void *                                  aContext)
236 {
237     return AsCoreType(aInstance).Get<MlrManager>().RegisterMulticastListeners(aAddresses, aAddressNum, aTimeout,
238                                                                               aCallback, aContext);
239 }
240 #endif
241 
242 #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
243 
otIp6IsSlaacEnabled(otInstance * aInstance)244 bool otIp6IsSlaacEnabled(otInstance *aInstance)
245 {
246     return AsCoreType(aInstance).Get<Utils::Slaac>().IsEnabled();
247 }
248 
otIp6SetSlaacEnabled(otInstance * aInstance,bool aEnabled)249 void otIp6SetSlaacEnabled(otInstance *aInstance, bool aEnabled)
250 {
251     Instance &instance = AsCoreType(aInstance);
252 
253     if (aEnabled)
254     {
255         instance.Get<Utils::Slaac>().Enable();
256     }
257     else
258     {
259         instance.Get<Utils::Slaac>().Disable();
260     }
261 }
262 
otIp6SetSlaacPrefixFilter(otInstance * aInstance,otIp6SlaacPrefixFilter aFilter)263 void otIp6SetSlaacPrefixFilter(otInstance *aInstance, otIp6SlaacPrefixFilter aFilter)
264 {
265     AsCoreType(aInstance).Get<Utils::Slaac>().SetFilter(aFilter);
266 }
267 
268 #endif // OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
269 
270 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
271 
otIp6SetMeshLocalIid(otInstance * aInstance,const otIp6InterfaceIdentifier * aIid)272 otError otIp6SetMeshLocalIid(otInstance *aInstance, const otIp6InterfaceIdentifier *aIid)
273 {
274     return AsCoreType(aInstance).Get<Mle::MleRouter>().SetMeshLocalIid(AsCoreType(aIid));
275 }
276 
277 #endif
278 
otIp6ProtoToString(uint8_t aIpProto)279 const char *otIp6ProtoToString(uint8_t aIpProto)
280 {
281     return Ip6::Ip6::IpProtoToString(aIpProto);
282 }
283