From b446a002dbcc18bf82e96893f6cc7d97b19c5e0f Mon Sep 17 00:00:00 2001 From: wangchen Date: Tue, 29 Aug 2023 18:02:32 +0800 Subject: [PATCH] net:add customizable default max & min port add customizable default max & min port Signed-off-by: wangchen --- net/Kconfig | 12 ++++++++++++ net/nat/ipv4_nat_entry.c | 17 ++++------------- net/tcp/tcp_conn.c | 10 +++++----- net/udp/udp_conn.c | 10 +++++----- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/net/Kconfig b/net/Kconfig index 7c8cf4a3d7..bb7d7c8c1b 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -57,6 +57,18 @@ config NET_PROMISCUOUS Force the Ethernet driver to operate in promiscuous mode (if supported by the Ethernet driver). +config NET_DEFAULT_MIN_PORT + int "Net Default Min Port" + default 4096 + ---help--- + Default Network min port + +config NET_DEFAULT_MAX_PORT + int "Net Default Max Port" + default 32000 + ---help--- + Default Network max port + menu "Driver buffer configuration" config NET_ETH_PKTSIZE diff --git a/net/nat/ipv4_nat_entry.c b/net/nat/ipv4_nat_entry.c index 822c8df7e3..4deccad416 100644 --- a/net/nat/ipv4_nat_entry.c +++ b/net/nat/ipv4_nat_entry.c @@ -40,15 +40,6 @@ #if defined(CONFIG_NET_NAT) && defined(CONFIG_NET_IPv4) -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* TODO: Why we limit to 32000 in net stack? */ - -#define NAT_PORT_REASSIGN_MAX 32000 -#define NAT_PORT_REASSIGN_MIN 4096 - /**************************************************************************** * Private Data ****************************************************************************/ @@ -119,9 +110,9 @@ static uint16_t ipv4_nat_select_port_without_stack( uint16_t hport = NTOHS(portno); while (ipv4_nat_port_inuse(protocol, ip, portno)) { - if (++hport >= NAT_PORT_REASSIGN_MAX) + if (++hport >= CONFIG_NET_DEFAULT_MAX_PORT) { - hport = NAT_PORT_REASSIGN_MIN; + hport = CONFIG_NET_DEFAULT_MIN_PORT; } portno = HTONS(hport); @@ -209,9 +200,9 @@ static uint16_t ipv4_nat_select_port(FAR struct net_driver_s *dev, while (icmp_findconn(dev, id) || ipv4_nat_port_inuse(IP_PROTO_ICMP, dev->d_ipaddr, id)) { - if (++hid >= NAT_PORT_REASSIGN_MAX) + if (++hid >= CONFIG_NET_DEFAULT_MAX_PORT) { - hid = NAT_PORT_REASSIGN_MIN; + hid = CONFIG_NET_DEFAULT_MIN_PORT; } id = HTONS(hid); diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c index 90e1b753b6..c540170201 100644 --- a/net/tcp/tcp_conn.c +++ b/net/tcp/tcp_conn.c @@ -585,11 +585,11 @@ int tcp_selectport(uint8_t domain, { net_getrandom(&g_last_tcp_port, sizeof(uint16_t)); - g_last_tcp_port = g_last_tcp_port % 32000; + g_last_tcp_port = g_last_tcp_port % CONFIG_NET_DEFAULT_MAX_PORT; - if (g_last_tcp_port < 4096) + if (g_last_tcp_port < CONFIG_NET_DEFAULT_MIN_PORT) { - g_last_tcp_port += 4096; + g_last_tcp_port += CONFIG_NET_DEFAULT_MIN_PORT; } } @@ -608,9 +608,9 @@ int tcp_selectport(uint8_t domain, * is within range. */ - if (++g_last_tcp_port >= 32000) + if (++g_last_tcp_port >= CONFIG_NET_DEFAULT_MAX_PORT) { - g_last_tcp_port = 4096; + g_last_tcp_port = CONFIG_NET_DEFAULT_MIN_PORT; } portno = HTONS(g_last_tcp_port); diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c index 34b9bd00b1..d940e03368 100644 --- a/net/udp/udp_conn.c +++ b/net/udp/udp_conn.c @@ -540,11 +540,11 @@ uint16_t udp_select_port(uint8_t domain, FAR union ip_binding_u *u) if (g_last_udp_port == 0) { - g_last_udp_port = clock_systime_ticks() % 32000; + g_last_udp_port = clock_systime_ticks() % CONFIG_NET_DEFAULT_MAX_PORT; - if (g_last_udp_port < 4096) + if (g_last_udp_port < CONFIG_NET_DEFAULT_MIN_PORT) { - g_last_udp_port += 4096; + g_last_udp_port += CONFIG_NET_DEFAULT_MIN_PORT; } } @@ -562,9 +562,9 @@ uint16_t udp_select_port(uint8_t domain, FAR union ip_binding_u *u) /* Make sure that the port number is within range */ - if (g_last_udp_port >= 32000) + if (g_last_udp_port >= CONFIG_NET_DEFAULT_MAX_PORT) { - g_last_udp_port = 4096; + g_last_udp_port = CONFIG_NET_DEFAULT_MIN_PORT; } } while (udp_find_conn(domain, u, HTONS(g_last_udp_port), 0) != NULL