From 8467a3d5223c274b65a71080f6aad94dc182a9bb Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Wed, 21 Apr 2021 13:32:07 +0800 Subject: [PATCH] [border-agent] use ephemeral port (#6450) This commit changes border agent service to use ephemeral port by default. This change makes it easier to support multiple Thread interfaces on a single host. --- include/openthread/border_agent.h | 12 +++++++++++- include/openthread/instance.h | 2 +- src/cli/README.md | 11 +++++++++++ src/cli/cli.cpp | 14 ++++++++++++++ src/cli/cli.hpp | 6 ++++++ src/core/api/border_agent_api.cpp | 7 +++++++ src/core/coap/coap_secure.hpp | 8 ++++++++ src/core/config/border_router.h | 10 ++++++++++ src/core/config/srp_server.h | 2 +- src/core/meshcop/border_agent.cpp | 9 +++++++++ src/core/meshcop/border_agent.hpp | 8 ++++++++ src/core/meshcop/dtls.cpp | 5 +++++ src/core/meshcop/dtls.hpp | 8 ++++++++ src/core/meshcop/meshcop.hpp | 2 +- src/core/net/ip6_filter.cpp | 2 +- src/core/thread/mle_router.cpp | 3 ++- tests/scripts/expect/cli-misc.exp | 3 +++ .../thread-cert/border_router/test_dnssd_server.py | 9 ++++++--- tests/scripts/thread-cert/config.py | 2 -- 19 files changed, 112 insertions(+), 11 deletions(-) diff --git a/include/openthread/border_agent.h b/include/openthread/border_agent.h index 1d0f3cb04..0b267a867 100644 --- a/include/openthread/border_agent.h +++ b/include/openthread/border_agent.h @@ -65,13 +65,23 @@ typedef enum otBorderAgentState /** * This function gets the state of Thread Border Agent role. * - * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aInstance A pointer to an OpenThread instance. * * @returns State of the Border Agent. * */ otBorderAgentState otBorderAgentGetState(otInstance *aInstance); +/** + * This function gets the UDP port of Thread Border Agent service. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns UDP port of the Border Agent. + * + */ +uint16_t otBorderAgentGetUdpPort(otInstance *aInstance); + /** * @} * diff --git a/include/openthread/instance.h b/include/openthread/instance.h index a45f5ecc0..46d8b4fbc 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (102) +#define OPENTHREAD_API_VERSION (103) /** * @addtogroup api-instance diff --git a/src/cli/README.md b/src/cli/README.md index 8543edf38..522a44b07 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -21,6 +21,7 @@ Done ## OpenThread Command List +- [ba](#ba-port) - [bbr](#bbr) - [br](#br) - [bufferinfo](#bufferinfo) @@ -316,6 +317,16 @@ Set jitter (in seconds) for Backbone Router registration for Thread 1.2 FTD. Done ``` +### ba port + +Print border agent service port. + +```bash +> ba port +49152 +Done +``` + ### br Enbale/disable the Border Routing functionality. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index fb61a812f..ccb1d27c6 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -285,6 +285,20 @@ otError Interpreter::ProcessHelp(uint8_t aArgsLength, char *aArgs[]) return OT_ERROR_NONE; } +#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE +otError Interpreter::ProcessBorderAgent(uint8_t aArgsLength, char *aArgs[]) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(aArgsLength == 1 && strcmp(aArgs[0], "port") == 0, error = OT_ERROR_INVALID_COMMAND); + + OutputLine("%hu", otBorderAgentGetUdpPort(mInstance)); + +exit: + return error; +} +#endif + #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE otError Interpreter::ProcessBorderRouting(uint8_t aArgsLength, char *aArgs[]) { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index bab02caf0..561469790 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -332,6 +332,9 @@ private: otError ProcessCcaThreshold(uint8_t aArgsLength, char *aArgs[]); otError ProcessBufferInfo(uint8_t aArgsLength, char *aArgs[]); otError ProcessChannel(uint8_t aArgsLength, char *aArgs[]); +#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE + otError ProcessBorderAgent(uint8_t aArgsLength, char *aArgs[]); +#endif #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE otError ProcessBorderRouting(uint8_t aArgsLength, char *aArgs[]); #endif @@ -638,6 +641,9 @@ private: void HandleDiscoveryRequest(const otThreadDiscoveryRequestInfo &aInfo); static constexpr Command sCommands[] = { +#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE + {"ba", &Interpreter::ProcessBorderAgent}, +#endif #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) {"bbr", &Interpreter::ProcessBackboneRouter}, #endif diff --git a/src/core/api/border_agent_api.cpp b/src/core/api/border_agent_api.cpp index d84fa0282..96c4f9237 100644 --- a/src/core/api/border_agent_api.cpp +++ b/src/core/api/border_agent_api.cpp @@ -49,4 +49,11 @@ otBorderAgentState otBorderAgentGetState(otInstance *aInstance) return static_cast(instance.Get().GetState()); } +uint16_t otBorderAgentGetUdpPort(otInstance *aInstance) +{ + Instance &instance = *static_cast(aInstance); + + return instance.Get().GetUdpPort(); +} + #endif // OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE diff --git a/src/core/coap/coap_secure.hpp b/src/core/coap/coap_secure.hpp index c4e37653a..8b712e504 100644 --- a/src/core/coap/coap_secure.hpp +++ b/src/core/coap/coap_secure.hpp @@ -155,6 +155,14 @@ public: */ MeshCoP::Dtls &GetDtls(void) { return mDtls; } + /** + * This method gets the UDP port of this agent. + * + * @returns UDP port number. + * + */ + uint16_t GetUdpPort(void) const { return mDtls.GetUdpPort(); } + /** * This method sets the PSK. * diff --git a/src/core/config/border_router.h b/src/core/config/border_router.h index bdddb0e34..d42548f3f 100644 --- a/src/core/config/border_router.h +++ b/src/core/config/border_router.h @@ -65,4 +65,14 @@ #define OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE 0 #endif +/** + * @def OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT + * + * Specifies the Border Agent UDP port, and use 0 for ephemeral port. + * + */ +#ifndef OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT +#define OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT 0 +#endif + #endif // CONFIG_BORDER_ROUTER_H_ diff --git a/src/core/config/srp_server.h b/src/core/config/srp_server.h index c0d9e30ef..42fe08d84 100644 --- a/src/core/config/srp_server.h +++ b/src/core/config/srp_server.h @@ -48,7 +48,7 @@ /** * @def OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT * - * Specifies the SRP Server UDP port. + * Specifies the SRP Server UDP port, and use 0 for ephemeral port. * */ #ifndef OPENTHREAD_CONFIG_SRP_SERVER_UDP_PORT diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 42ce6625d..c9561d8d5 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -48,6 +48,10 @@ namespace ot { namespace MeshCoP { +namespace { +constexpr uint16_t kBorderAgentUdpPort = OPENTHREAD_CONFIG_BORDER_AGENT_UDP_PORT; ///< UDP port of border agent service. +} + void BorderAgent::ForwardContext::Init(Instance & aInstance, const Coap::Message &aMessage, bool aPetition, @@ -570,6 +574,11 @@ void BorderAgent::HandleConnected(bool aConnected) } } +uint16_t BorderAgent::GetUdpPort(void) const +{ + return Get().GetUdpPort(); +} + Error BorderAgent::Start(void) { Error error; diff --git a/src/core/meshcop/border_agent.hpp b/src/core/meshcop/border_agent.hpp index 2409e4754..3b2031ee1 100644 --- a/src/core/meshcop/border_agent.hpp +++ b/src/core/meshcop/border_agent.hpp @@ -74,6 +74,14 @@ public: */ explicit BorderAgent(Instance &aInstance); + /** + * This method gets the UDP port of this service. + * + * @returns UDP port number. + * + */ + uint16_t GetUdpPort(void) const; + /** * This method starts the Border Agent service. * diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index f145a3eb3..f198082af 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -208,6 +208,11 @@ exit: return; } +uint16_t Dtls::GetUdpPort(void) const +{ + return mSocket.GetSockName().GetPort(); +} + Error Dtls::Bind(uint16_t aPort) { Error error; diff --git a/src/core/meshcop/dtls.hpp b/src/core/meshcop/dtls.hpp index 0e507d003..a032c96b6 100644 --- a/src/core/meshcop/dtls.hpp +++ b/src/core/meshcop/dtls.hpp @@ -135,6 +135,14 @@ public: */ Error Bind(uint16_t aPort); + /** + * This method gets the UDP port of this session. + * + * @returns UDP port number. + * + */ + uint16_t GetUdpPort(void) const; + /** * This method binds this DTLS with a transport callback. * diff --git a/src/core/meshcop/meshcop.hpp b/src/core/meshcop/meshcop.hpp index f5e8d3a25..8562d3424 100644 --- a/src/core/meshcop/meshcop.hpp +++ b/src/core/meshcop/meshcop.hpp @@ -59,7 +59,7 @@ namespace MeshCoP { enum { - kBorderAgentUdpPort = 49191, ///< UDP port of border agent service. + kNativeCommissionerUdpPort = 49191, ///< UDP port of native commissioner service. }; /** diff --git a/src/core/net/ip6_filter.cpp b/src/core/net/ip6_filter.cpp index 56603e3e3..892d29879 100644 --- a/src/core/net/ip6_filter.cpp +++ b/src/core/net/ip6_filter.cpp @@ -88,7 +88,7 @@ bool Filter::Accept(Message &aMessage) const } // Allow native commissioner traffic - if (mAllowNativeCommissioner && dstport == MeshCoP::kBorderAgentUdpPort) + if (mAllowNativeCommissioner && dstport == MeshCoP::kNativeCommissionerUdpPort) { ExitNow(rval = true); } diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 250e165e8..feefdccc4 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -2909,7 +2909,8 @@ Error MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, const M if (Get().GetSecurityPolicy().mNativeCommissioningEnabled) { - SuccessOrExit(error = Tlv::Append(*message, MeshCoP::kBorderAgentUdpPort)); + SuccessOrExit(error = + Tlv::Append(*message, MeshCoP::kNativeCommissionerUdpPort)); discoveryResponse.SetNativeCommissioner(true); } diff --git a/tests/scripts/expect/cli-misc.exp b/tests/scripts/expect/cli-misc.exp index 98651e8b5..e0e6eb3c2 100755 --- a/tests/scripts/expect/cli-misc.exp +++ b/tests/scripts/expect/cli-misc.exp @@ -167,4 +167,7 @@ expect_line "Done" send "diag start\n" expect ": InvalidState" +send "ba port\n" +expect "Done" + dispose_all diff --git a/tests/scripts/thread-cert/border_router/test_dnssd_server.py b/tests/scripts/thread-cert/border_router/test_dnssd_server.py index 3ea314887..2932cc6b7 100644 --- a/tests/scripts/thread-cert/border_router/test_dnssd_server.py +++ b/tests/scripts/thread-cert/border_router/test_dnssd_server.py @@ -232,13 +232,16 @@ class TestDnssdServerOnBr(thread_cert.TestCase): dp_instance_name = f'{network_name}._meshcop._udp.default.service.arpa.' dp_hostname = lambda x: x.endswith('.default.service.arpa.') + def check_border_agent_port(port): + return 0 < port <= 65535 + dig_result = self.nodes[DIGGER].dns_dig(server_addr, dp_service_name, 'PTR') self._assert_dig_result_matches( dig_result, { 'QUESTION': [(dp_service_name, 'IN', 'PTR'),], 'ANSWER': [(dp_service_name, 'IN', 'PTR', dp_instance_name),], 'ADDITIONAL': [ - (dp_instance_name, 'IN', 'SRV', 0, 0, config.BORDER_AGENT_UDP_PORT, dp_hostname), + (dp_instance_name, 'IN', 'SRV', 0, 0, check_border_agent_port, dp_hostname), (dp_instance_name, 'IN', 'TXT', lambda txt: (isinstance(txt, dict) and txt.get( 'nn') == network_name and 'xp' in txt and 'tv' in txt and 'dd' in txt)), ], @@ -258,7 +261,7 @@ class TestDnssdServerOnBr(thread_cert.TestCase): self._assert_dig_result_matches( dig_result, { 'QUESTION': [(dp_instance_name, 'IN', 'SRV'),], - 'ANSWER': [(dp_instance_name, 'IN', 'SRV', 0, 0, config.BORDER_AGENT_UDP_PORT, dp_hostname),], + 'ANSWER': [(dp_instance_name, 'IN', 'SRV', 0, 0, check_border_agent_port, dp_hostname),], 'ADDITIONAL': [(dp_instance_name, 'IN', 'TXT', lambda txt: (isinstance(txt, dict) and txt.get( 'nn') == network_name and 'xp' in txt and 'tv' in txt and 'dd' in txt)),], }) @@ -269,7 +272,7 @@ class TestDnssdServerOnBr(thread_cert.TestCase): 'QUESTION': [(dp_instance_name, 'IN', 'TXT'),], 'ANSWER': [(dp_instance_name, 'IN', 'TXT', lambda txt: (isinstance(txt, dict) and txt.get( 'nn') == network_name and 'xp' in txt and 'tv' in txt and 'dd' in txt)),], - 'ADDITIONAL': [(dp_instance_name, 'IN', 'SRV', 0, 0, config.BORDER_AGENT_UDP_PORT, dp_hostname),], + 'ADDITIONAL': [(dp_instance_name, 'IN', 'SRV', 0, 0, check_border_agent_port, dp_hostname),], }) if dp_ip6_address is not None: diff --git a/tests/scripts/thread-cert/config.py b/tests/scripts/thread-cert/config.py index 718f97d71..3b50c1598 100644 --- a/tests/scripts/thread-cert/config.py +++ b/tests/scripts/thread-cert/config.py @@ -144,8 +144,6 @@ LEADER_NOTIFY_SED_BY_CHILD_UPDATE_REQUEST = True THREAD_VERSION_1_1 = 2 THREAD_VERSION_1_2 = 3 -BORDER_AGENT_UDP_PORT = 49191 - def create_default_network_data_prefix_sub_tlvs_factories(): return {