| /* | |
| * | |
| * Copyright (c) 2020-2021 Project CHIP Authors | |
| * Copyright (c) 2018 Google LLC. | |
| * Copyright (c) 2013-2018 Nest Labs, Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include <inet/UDPEndPointImplOpenThread.h> | |
| #include <lib/support/CodeUtils.h> | |
| #include <lib/support/SafeInt.h> | |
| #include <lib/support/logging/CHIPLogging.h> | |
| #include <platform/OpenThread/OpenThreadUtils.h> | |
| #include <system/SystemPacketBuffer.h> | |
| #include <openthread/error.h> | |
| #include <openthread/udp.h> | |
| #if INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| #include <inet/IPv6Datagram.h> | |
| #include <openthread/icmp6.h> | |
| #include <openthread/message.h> | |
| #include <algorithm> | |
| #include <cstring> | |
| #endif // INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| namespace chip { | |
| namespace Inet { | |
| using DeviceLayer::Internal::MapOpenThreadError; | |
| otInstance * globalOtInstance; | |
| namespace { | |
| // We want to reserve space for an IPPacketInfo in our buffer, but it needs to | |
| // be 4-byte aligned. We ensure the alignment by masking off the low bits of | |
| // the pointer that we get by doing `Start() - sizeof(IPPacketInfo)`. That | |
| // might move it backward by up to kPacketInfoAlignmentBytes, so we need to make | |
| // sure we allocate enough reserved space that this will still be within our | |
| // buffer. | |
| constexpr size_t kPacketInfoAlignmentBytes = sizeof(uint32_t) - 1; | |
| constexpr size_t kPacketInfoReservedSize = sizeof(IPPacketInfo) + kPacketInfoAlignmentBytes; | |
| #if INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| // RFC 4443 section 3.1; openthread/icmp6.h does not define it. | |
| constexpr uint8_t kIcmp6CodeDstUnreachPort = 4; | |
| otIcmp6Handler sIcmp6Handler; | |
| // Registered once: this assumes no platform re-initializes OpenThread at runtime; if one does, reports stop. | |
| otInstance * sIcmp6HandlerInstance = nullptr; | |
| // sPending and sBoundEndPoints are guarded by the OpenThread lock: HandleIcmp6Receive runs with it held, and all | |
| // other access takes LockOpenThread(). | |
| // One undelivered event: a later one for the same endpoint replaces it, and one for another endpoint is dropped. | |
| struct PendingUnreachable | |
| { | |
| UDPEndPointImplOT * endPoint; | |
| IPPacketInfo info; | |
| uint8_t quoted[UDPEndPoint::kMaxQuotedPayloadLen]; | |
| size_t quotedLen; | |
| bool valid; | |
| }; | |
| PendingUnreachable sPending = {}; | |
| UDPEndPointImplOT * sBoundEndPoints = nullptr; | |
| #endif // INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| } // namespace | |
| #if INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| void UDPEndPointImplOT::HandleIcmp6Receive(void * aContext, otMessage * aMsg, const otMessageInfo * aInfo, | |
| const otIcmp6Header * aIcmpHeader) | |
| { | |
| if (aIcmpHeader->mType != OT_ICMP6_TYPE_DST_UNREACH || aIcmpHeader->mCode != kIcmp6CodeDstUnreachPort) | |
| { | |
| return; | |
| } | |
| uint8_t inner[IPv6Datagram::kUDPHeadersLen + kMaxQuotedPayloadLen]; | |
| uint16_t bytes = otMessageRead(aMsg, otMessageGetOffset(aMsg), inner, sizeof(inner)); | |
| IPPacketInfo info; | |
| info.Clear(); | |
| ByteSpan quoted; | |
| if (IPv6Datagram::ParseUDPHeaders(ByteSpan(inner, bytes), info, quoted) != CHIP_NO_ERROR) | |
| { | |
| return; | |
| } | |
| // RFC 4443 section 3.1: port-unreachable comes from the destination host. | |
| if (!(IPAddress::FromOtAddr(aInfo->mPeerAddr) == info.DestAddress)) | |
| { | |
| return; | |
| } | |
| UDPEndPointImplOT * ep = nullptr; | |
| for (UDPEndPointImplOT * candidate = sBoundEndPoints; candidate != nullptr; candidate = candidate->mNextBoundEndPoint) | |
| { | |
| if (candidate->GetBoundPort() == info.SrcPort) | |
| { | |
| ep = candidate; | |
| break; | |
| } | |
| } | |
| if (ep == nullptr || ep->mState == State::kClosed || ep->OnPortUnreachable == nullptr) | |
| { | |
| return; | |
| } | |
| const bool dispatchPending = sPending.valid; | |
| if (dispatchPending && sPending.endPoint != ep) | |
| { | |
| return; | |
| } | |
| sPending.endPoint = ep; | |
| sPending.info = info; | |
| sPending.quotedLen = std::min(quoted.size(), sizeof(sPending.quoted)); | |
| memcpy(sPending.quoted, quoted.data(), sPending.quotedLen); | |
| sPending.valid = true; | |
| if (dispatchPending) | |
| { | |
| return; | |
| } | |
| ep->Ref(); | |
| CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep] { | |
| ep->LockOpenThread(); | |
| PendingUnreachable pending = sPending; | |
| if (sPending.endPoint == ep) | |
| { | |
| sPending.valid = false; | |
| } | |
| ep->UnlockOpenThread(); | |
| if (pending.valid && pending.endPoint == ep && ep->mState != State::kClosed && ep->OnPortUnreachable != nullptr) | |
| { | |
| ep->OnPortUnreachable(ep, pending.info, ByteSpan(pending.quoted, pending.quotedLen)); | |
| } | |
| ep->Unref(); | |
| }); | |
| if (err != CHIP_NO_ERROR) | |
| { | |
| sPending.valid = false; | |
| ep->Unref(); | |
| } | |
| } | |
| #endif // INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| void UDPEndPointImplOT::handleUdpReceive(void * aContext, otMessage * aMessage, const otMessageInfo * aMessageInfo) | |
| { | |
| UDPEndPointImplOT * ep = static_cast<UDPEndPointImplOT *>(aContext); | |
| uint16_t msgLen = otMessageGetLength(aMessage); | |
| System::PacketBufferHandle payload; | |
| #if CHIP_DETAIL_LOGGING | |
| static uint16_t msgReceivedCount = 0; | |
| char sourceStr[Inet::IPAddress::kMaxStringLength]; | |
| char destStr[Inet::IPAddress::kMaxStringLength]; | |
| #endif | |
| if (ep->mState == State::kClosed) | |
| return; | |
| if (msgLen > System::PacketBuffer::kMaxSizeWithoutReserve) | |
| { | |
| ChipLogError(Inet, "UDP message too long, discarding. Size received %d", msgLen); | |
| return; | |
| } | |
| payload = System::PacketBufferHandle::New(msgLen, kPacketInfoReservedSize); | |
| if (payload.IsNull()) | |
| { | |
| ChipLogError(Inet, "Failed to allocate a System buffer of size %d for UDP Message reception.", msgLen); | |
| return; | |
| } | |
| IPPacketInfo * pktInfo = GetPacketInfo(payload); | |
| if (pktInfo == nullptr) | |
| { | |
| ChipLogError(Inet, "Failed to pre-allocate reserved space for an IPPacketInfo for UDP Message reception."); | |
| return; | |
| } | |
| pktInfo->SrcAddress = IPAddress::FromOtAddr(aMessageInfo->mPeerAddr); | |
| pktInfo->DestAddress = IPAddress::FromOtAddr(aMessageInfo->mSockAddr); | |
| pktInfo->SrcPort = aMessageInfo->mPeerPort; | |
| pktInfo->DestPort = aMessageInfo->mSockPort; | |
| #if CHIP_DETAIL_LOGGING | |
| pktInfo->SrcAddress.ToString(sourceStr, Inet::IPAddress::kMaxStringLength); | |
| pktInfo->DestAddress.ToString(destStr, Inet::IPAddress::kMaxStringLength); | |
| ChipLogDetail(Inet, | |
| "UDP Message Received packet nb : %d SrcAddr : %s[%d] DestAddr " | |
| ": %s[%d] Payload Length %d", | |
| ++msgReceivedCount, sourceStr, pktInfo->SrcPort, destStr, pktInfo->DestPort, msgLen); | |
| #endif | |
| if (otMessageRead(aMessage, 0, payload->Start(), msgLen) != msgLen) | |
| { | |
| ChipLogError(Inet, "Failed to copy OpenThread buffer into System Packet buffer"); | |
| return; | |
| } | |
| payload->SetDataLength(static_cast<uint16_t>(msgLen)); | |
| // TODO: add thread-safe reference counting for UDP endpoints | |
| auto * buf = std::move(payload).UnsafeRelease(); | |
| ep->Ref(); | |
| CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep, buf] { | |
| ep->HandleDataReceived(System::PacketBufferHandle::Adopt(buf)); | |
| ep->Unref(); | |
| }); | |
| if (err != CHIP_NO_ERROR) | |
| { | |
| ep->Unref(); | |
| // Make sure we properly clean up buf and ep, since our lambda will not | |
| // run. | |
| payload = System::PacketBufferHandle::Adopt(buf); | |
| } | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::IPv6Bind(otUdpSocket & socket, const IPAddress & address, uint16_t port, | |
| [[maybe_unused]] InterfaceId interface) | |
| { | |
| otError err = OT_ERROR_NONE; | |
| otSockAddr listenSockAddr; | |
| memset(&socket, 0, sizeof(socket)); | |
| memset(&listenSockAddr, 0, sizeof(listenSockAddr)); | |
| listenSockAddr.mPort = port; | |
| listenSockAddr.mAddress = address.ToIPv6(); | |
| LockOpenThread(); | |
| err = otUdpOpen(mOTInstance, &socket, handleUdpReceive, this); | |
| VerifyOrExit(err == OT_ERROR_NONE, ); | |
| #if OPENTHREAD_API_VERSION >= 465 | |
| err = otUdpBind(mOTInstance, &socket, &listenSockAddr, OT_NETIF_THREAD_INTERNAL); | |
| #else | |
| err = otUdpBind(mOTInstance, &socket, &listenSockAddr, OT_NETIF_THREAD); | |
| #endif | |
| if (err != OT_ERROR_NONE) | |
| { | |
| auto closeErr = otUdpClose(mOTInstance, &socket); | |
| if (closeErr != OT_ERROR_NONE) | |
| { | |
| ChipLogError(Inet, "Failed to close socket: %s", chip::ErrorStr(MapOpenThreadError(closeErr))); | |
| } | |
| } | |
| if (err == OT_ERROR_NONE) | |
| { | |
| mBoundPort = socket.mSockName.mPort; | |
| } | |
| #if INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| if (err == OT_ERROR_NONE) | |
| { | |
| if (sIcmp6HandlerInstance == nullptr) | |
| { | |
| sIcmp6Handler.mReceiveCallback = HandleIcmp6Receive; | |
| sIcmp6Handler.mContext = nullptr; | |
| otError icmpErr = otIcmp6RegisterHandler(mOTInstance, &sIcmp6Handler); | |
| if (icmpErr == OT_ERROR_NONE || icmpErr == OT_ERROR_ALREADY) | |
| { | |
| sIcmp6HandlerInstance = mOTInstance; | |
| } | |
| else | |
| { | |
| ChipLogError(Inet, "otIcmp6RegisterHandler failed; port-unreachable detection unavailable"); | |
| } | |
| } | |
| else if (sIcmp6HandlerInstance != mOTInstance) | |
| { | |
| ChipLogError(Inet, "Port-unreachable detection is limited to the first OpenThread instance"); | |
| } | |
| if (!mIsBoundEndPointLinked) | |
| { | |
| mNextBoundEndPoint = sBoundEndPoints; | |
| sBoundEndPoints = this; | |
| mIsBoundEndPointLinked = true; | |
| } | |
| } | |
| #endif // INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| exit: | |
| UnlockOpenThread(); | |
| return MapOpenThreadError(err); | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::BindImpl(IPAddressType addressType, const IPAddress & addr, uint16_t port, InterfaceId interface) | |
| { | |
| if (addressType != IPAddressType::kIPv6) | |
| { | |
| return INET_ERROR_WRONG_ADDRESS_TYPE; | |
| } | |
| ReturnErrorOnFailure(IPv6Bind(mSocket, addr, port, interface)); | |
| mBoundIntfId = interface; | |
| return CHIP_NO_ERROR; | |
| } | |
| InterfaceId UDPEndPointImplOT::GetBoundInterface() const | |
| { | |
| return mBoundIntfId; | |
| } | |
| uint16_t UDPEndPointImplOT::GetBoundPort() const | |
| { | |
| return mBoundPort; | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::ListenImpl() | |
| { | |
| // Nothing to do. Callback was set upon Binding call. | |
| return CHIP_NO_ERROR; | |
| } | |
| void UDPEndPointImplOT::HandleDataReceived(System::PacketBufferHandle && msg) | |
| { | |
| if ((mState == State::kListening) && (OnMessageReceived != nullptr)) | |
| { | |
| const IPPacketInfo * pktInfo = GetPacketInfo(msg); | |
| if (pktInfo != nullptr) | |
| { | |
| const IPPacketInfo pktInfoCopy = *pktInfo; // copy the address info so that the app can free the | |
| // PacketBuffer without affecting access to address info. | |
| OnMessageReceived(this, std::move(msg), &pktInfoCopy); | |
| } | |
| else | |
| { | |
| if (OnReceiveError != nullptr) | |
| { | |
| OnReceiveError(this, CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG, nullptr); | |
| } | |
| } | |
| } | |
| } | |
| void UDPEndPointImplOT::SetNativeParams(void * params) | |
| { | |
| if (params == nullptr) | |
| { | |
| ChipLogError(Inet, "FATAL!! No native parameters provided!!!!!"); | |
| VerifyOrDie(false); | |
| } | |
| OpenThreadEndpointInitParam * initParams = static_cast<OpenThreadEndpointInitParam *>(params); | |
| mOTInstance = initParams->openThreadInstancePtr; | |
| globalOtInstance = mOTInstance; | |
| lockOpenThread = initParams->lockCb; | |
| unlockOpenThread = initParams->unlockCb; | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) | |
| { | |
| (void) aIPVersion; | |
| (void) aLoopback; | |
| // TODO | |
| return CHIP_ERROR_NOT_IMPLEMENTED; | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) | |
| { | |
| (void) addressType; | |
| (void) interfaceId; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::SendMsgImpl(const IPPacketInfo * aPktInfo, System::PacketBufferHandle && msg) | |
| { | |
| otError error = OT_ERROR_NONE; | |
| otMessage * message; | |
| otMessageInfo messageInfo; | |
| otMessageSettings settings = {}; | |
| // For now the entire message must fit within a single buffer. | |
| VerifyOrReturnError(!msg->HasChainedBuffer() && msg->DataLength() <= UINT16_MAX, CHIP_ERROR_MESSAGE_TOO_LONG); | |
| memset(&messageInfo, 0, sizeof(messageInfo)); | |
| messageInfo.mSockAddr = aPktInfo->SrcAddress.ToIPv6(); | |
| messageInfo.mPeerAddr = aPktInfo->DestAddress.ToIPv6(); | |
| messageInfo.mPeerPort = aPktInfo->DestPort; | |
| LockOpenThread(); | |
| settings.mPriority = OT_MESSAGE_PRIORITY_NORMAL; | |
| settings.mLinkSecurityEnabled = otThreadGetDeviceRole(mOTInstance) != OT_DEVICE_ROLE_DISABLED; | |
| message = otUdpNewMessage(mOTInstance, &settings); | |
| VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); | |
| error = otMessageAppend(message, msg->Start(), static_cast<uint16_t>(msg->DataLength())); | |
| if (error == OT_ERROR_NONE) | |
| { | |
| error = otUdpSend(mOTInstance, &mSocket, message, &messageInfo); | |
| } | |
| exit: | |
| if (error != OT_ERROR_NONE && message != NULL) | |
| { | |
| otMessageFree(message); | |
| } | |
| UnlockOpenThread(); | |
| return MapOpenThreadError(error); | |
| } | |
| void UDPEndPointImplOT::CloseImpl() | |
| { | |
| LockOpenThread(); | |
| if (otUdpIsOpen(mOTInstance, &mSocket)) | |
| { | |
| auto err = otUdpClose(mOTInstance, &mSocket); | |
| if (err != OT_ERROR_NONE) | |
| { | |
| ChipLogError(Inet, "Failed to close socket: %s", chip::ErrorStr(MapOpenThreadError(err))); | |
| } | |
| } | |
| #if INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| if (sPending.valid && sPending.endPoint == this) | |
| { | |
| sPending.valid = false; | |
| } | |
| if (mIsBoundEndPointLinked) | |
| { | |
| for (UDPEndPointImplOT ** link = &sBoundEndPoints; *link != nullptr; link = &(*link)->mNextBoundEndPoint) | |
| { | |
| if (*link == this) | |
| { | |
| *link = mNextBoundEndPoint; | |
| break; | |
| } | |
| } | |
| mNextBoundEndPoint = nullptr; | |
| mIsBoundEndPointLinked = false; | |
| } | |
| #endif // INET_CONFIG_ENABLE_UDP_PORT_UNREACHABLE | |
| UnlockOpenThread(); | |
| } | |
| CHIP_ERROR UDPEndPointImplOT::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) | |
| { | |
| const otIp6Address otAddress = aAddress.ToIPv6(); | |
| otError err; | |
| LockOpenThread(); | |
| if (join) | |
| { | |
| err = otIp6SubscribeMulticastAddress(mOTInstance, &otAddress); | |
| } | |
| else | |
| { | |
| err = otIp6UnsubscribeMulticastAddress(mOTInstance, &otAddress); | |
| } | |
| UnlockOpenThread(); | |
| return MapOpenThreadError(err); | |
| } | |
| IPPacketInfo * UDPEndPointImplOT::GetPacketInfo(const System::PacketBufferHandle & aBuffer) | |
| { | |
| if (!aBuffer->EnsureReservedSize(kPacketInfoReservedSize)) | |
| { | |
| return nullptr; | |
| } | |
| uintptr_t lStart = (uintptr_t) aBuffer->Start(); | |
| uintptr_t lPacketInfoStart = lStart - sizeof(IPPacketInfo); | |
| // Align to a 4-byte boundary | |
| return reinterpret_cast<IPPacketInfo *>(lPacketInfoStart & ~kPacketInfoAlignmentBytes); | |
| } | |
| } // namespace Inet | |
| } // namespace chip |