From d347dd51e7e98b8528a189056c8851520c16f9f7 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 26 Nov 2024 11:18:17 -0800 Subject: [PATCH] [udp] remove extra `ShouldUsePlatformUdp()` checks (#10962) This commit removes the `ShouldUsePlatformUdp()` call in `Ip6::PassToHost()` and `Udp::HandleMessage()`. `ShouldUsePlatformUdp(port)` checks whether the port is NOT one of the port numbers used by the OT stack, such as the MLE port, TMF port, border agent port, or joiner port. This check is already covered by `Udp::IsPortInUse()`, which checks if there is a UDP socket bound to the given port. Applying such a filter in `Udp::HandleMessage()` seems to have been added by mistake, as it blocks the use of UDP receivers, which should be able to receive and process on any port, not just the ports where we have a socket bound. --- src/core/net/ip6.cpp | 4 +--- src/core/net/udp6.cpp | 28 ++++++++++++++++------------ src/core/net/udp6.hpp | 11 +---------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 8ccb3c7ee..ca3cd124c 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -972,9 +972,7 @@ Error Ip6::PassToHost(OwnedPtr &aMessagePtr, Udp::Header udp; IgnoreError(aMessagePtr->Read(aMessagePtr->GetOffset(), udp)); - VerifyOrExit(Get().ShouldUsePlatformUdp(udp.GetDestinationPort()) && - !Get().IsPortInUse(udp.GetDestinationPort()), - error = kErrorNoRoute); + VerifyOrExit(!Get().IsPortInUse(udp.GetDestinationPort()), error = kErrorNoRoute); break; } diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index 15cf4e9c2..bbd91fe66 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -412,10 +412,6 @@ Error Udp::HandleMessage(Message &aMessage, MessageInfo &aMessageInfo) aMessageInfo.mPeerPort = udpHeader.GetSourcePort(); aMessageInfo.mSockPort = udpHeader.GetDestinationPort(); -#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE - VerifyOrExit(!ShouldUsePlatformUdp(aMessageInfo.mSockPort) || IsPortInUse(aMessageInfo.mSockPort)); -#endif - for (Receiver &receiver : mReceivers) { VerifyOrExit(!receiver.HandleMessage(aMessage, aMessageInfo)); @@ -458,28 +454,36 @@ bool Udp::IsPortInUse(uint16_t aPort) const return found; } +#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + bool Udp::ShouldUsePlatformUdp(uint16_t aPort) const { - return (aPort != Mle::kUdpPort && aPort != Tmf::kUdpPort + bool shouldUse = false; + + VerifyOrExit(aPort != Mle::kUdpPort); + VerifyOrExit(aPort != Tmf::kUdpPort); #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE && !OPENTHREAD_CONFIG_DNSSD_SERVER_BIND_UNSPECIFIED_NETIF - && aPort != Dns::ServiceDiscovery::Server::kPort + VerifyOrExit(aPort != Dns::ServiceDiscovery::Server::kPort); #endif #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE - && aPort != Get().GetUdpProxyPort() + VerifyOrExit(aPort != Get().GetUdpProxyPort()); #endif #if OPENTHREAD_FTD - && aPort != Get().GetJoinerUdpPort() + VerifyOrExit(aPort != Get().GetJoinerUdpPort()); #endif #if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE - && aPort != Dhcp6::kDhcpServerPort + VerifyOrExit(aPort != Dhcp6::kDhcpServerPort); #endif #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE - && aPort != Dhcp6::kDhcpClientPort + VerifyOrExit(aPort != Dhcp6::kDhcpClientPort); #endif - ); + + shouldUse = true; + +exit: + return shouldUse; } -#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE bool Udp::ShouldUsePlatformUdp(const Udp::SocketHandle &aSocket) const { return (ShouldUsePlatformUdp(aSocket.mSockName.mPort) diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index 5225ee184..f8650699d 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -634,16 +634,6 @@ public: */ bool IsPortInUse(uint16_t aPort) const; - /** - * Returns whether a udp port belongs to the platform or the stack. - * - * @param[in] aPort The udp port - * - * @retval True when the port belongs to the platform. - * @retval False when the port belongs to the stack. - */ - bool ShouldUsePlatformUdp(uint16_t aPort) const; - private: static constexpr uint16_t kDynamicPortMin = 49152; // Service Name and Transport Protocol Port Number Registry static constexpr uint16_t kDynamicPortMax = 65535; // Service Name and Transport Protocol Port Number Registry @@ -657,6 +647,7 @@ private: void AddSocket(SocketHandle &aSocket); void RemoveSocket(SocketHandle &aSocket); #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + bool ShouldUsePlatformUdp(uint16_t aPort) const; bool ShouldUsePlatformUdp(const SocketHandle &aSocket) const; #endif