Skip to content

Commit

Permalink
[udp] remove extra ShouldUsePlatformUdp() checks (openthread#10962)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abtink authored Nov 26, 2024
1 parent d43cb0d commit d347dd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/core/net/ip6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,7 @@ Error Ip6::PassToHost(OwnedPtr<Message> &aMessagePtr,
Udp::Header udp;

IgnoreError(aMessagePtr->Read(aMessagePtr->GetOffset(), udp));
VerifyOrExit(Get<Udp>().ShouldUsePlatformUdp(udp.GetDestinationPort()) &&
!Get<Udp>().IsPortInUse(udp.GetDestinationPort()),
error = kErrorNoRoute);
VerifyOrExit(!Get<Udp>().IsPortInUse(udp.GetDestinationPort()), error = kErrorNoRoute);
break;
}

Expand Down
28 changes: 16 additions & 12 deletions src/core/net/udp6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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<MeshCoP::BorderAgent>().GetUdpProxyPort()
VerifyOrExit(aPort != Get<MeshCoP::BorderAgent>().GetUdpProxyPort());
#endif
#if OPENTHREAD_FTD
&& aPort != Get<MeshCoP::JoinerRouter>().GetJoinerUdpPort()
VerifyOrExit(aPort != Get<MeshCoP::JoinerRouter>().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)
Expand Down
11 changes: 1 addition & 10 deletions src/core/net/udp6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,16 +634,6 @@ class Udp : public InstanceLocator, private NonCopyable
*/
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
Expand All @@ -657,6 +647,7 @@ class Udp : public InstanceLocator, private NonCopyable
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

Expand Down

0 comments on commit d347dd5

Please sign in to comment.