From d65ad241a20ddc6c357342bfb0b2780c2f4efef9 Mon Sep 17 00:00:00 2001 From: LiPeng Date: Mon, 25 Jul 2022 19:00:58 +0800 Subject: [PATCH] napt: Fix ip_portmap_add() to keep only one port mapping --- src/core/ipv4/dhcp.c | 3 --- src/core/ipv4/ip4_napt.c | 25 +++++++++++++++++++++---- test/unit/core/test_ip4_route.c | 1 - 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index 40286612..e05ce8f6 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -390,9 +390,6 @@ dhcp_check(struct netif *netif) static void dhcp_handle_offer(struct netif *netif, struct dhcp_msg *msg_in) { -#if ESP_DHCP && !ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER - u8_t n; -#endif /* ESP_DHCP && !ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER */ struct dhcp *dhcp = netif_dhcp_data(netif); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n", diff --git a/src/core/ipv4/ip4_napt.c b/src/core/ipv4/ip4_napt.c index a883aa49..b51c2f91 100644 --- a/src/core/ipv4/ip4_napt.c +++ b/src/core/ipv4/ip4_napt.c @@ -176,17 +176,17 @@ static void ip_napt_deinit(void) { napt_list = NO_IDX; -ip_napt_max = 0; + ip_napt_max = 0; #if IP_NAPT_PORTMAP ip_portmap_max = 0; #endif mem_free(ip_napt_table); -ip_napt_table = NULL; + ip_napt_table = NULL; #if IP_NAPT_PORTMAP mem_free(ip_portmap_table); ip_portmap_table = NULL; #endif - sys_untimeout(ip_napt_tmr, NULL); + sys_untimeout(ip_napt_tmr, NULL); } /** @@ -196,7 +196,11 @@ ip_napt_table = NULL; * @param max_portmap max number of enties in the NAPT table (use IP_PORTMAP_MAX if in doubt) */ static void +#if IP_NAPT_PORTMAP ip_napt_init(uint16_t max_nat, uint8_t max_portmap) +#else +ip_napt_init(uint16_t max_nat) +#endif { u16_t i; @@ -210,7 +214,7 @@ ip_napt_init(uint16_t max_nat, uint8_t max_portmap) ip_napt_table = (struct ip_napt_entry *) mem_calloc(max_nat, sizeof(*ip_napt_table)); #if IP_NAPT_PORTMAP - ip_portmap_table = (struct ip_portmap_entry *) mem_calloc(max_portmap, sizeof(*ip_portmap_table)); + ip_portmap_table = (struct ip_portmap_entry *) mem_calloc(max_portmap, sizeof(*ip_portmap_table)); assert(ip_portmap_table != NULL && ip_napt_table != NULL); #else assert(ip_napt_table != NULL); @@ -238,7 +242,11 @@ ip_napt_enable(u32_t addr, int enable) } } if (napt_in_any_netif) { +#if IP_NAPT_PORTMAP ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX); +#else + ip_napt_init(IP_NAPT_MAX); +#endif } else { ip_napt_deinit(); } @@ -252,7 +260,11 @@ ip_napt_enable_no(u8_t number, int enable) if (netif->num == number) { netif->napt = !!enable; if (enable) +#if IP_NAPT_PORTMAP ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX); +#else + ip_napt_init(IP_NAPT_MAX); +#endif else ip_napt_deinit(); break; @@ -272,7 +284,11 @@ ip_napt_enable_netif(struct netif *netif, int enable) if (!netif->napt && enable) { /* Enable napt */ netif->napt = 1; +#if IP_NAPT_PORTMAP ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX); +#else + ip_napt_init(IP_NAPT_MAX); +#endif } else if (netif->napt && !enable) { /* Disable napt */ @@ -611,6 +627,7 @@ ip_portmap_add(u8_t proto, u32_t maddr, u16_t mport, u32_t daddr, u16_t dport) if (p->valid && p->proto == proto && p->mport == mport) { p->dport = dport; p->daddr = daddr; + p->maddr = maddr; } else if (!p->valid) { p->maddr = maddr; p->daddr = daddr; diff --git a/test/unit/core/test_ip4_route.c b/test/unit/core/test_ip4_route.c index 1951f6ff..9034ccf6 100644 --- a/test/unit/core/test_ip4_route.c +++ b/test/unit/core/test_ip4_route.c @@ -474,7 +474,6 @@ END_TEST START_TEST(test_ip4_route_netif_max_napt) { #define TCP_PORT 2222 - int i; packet_type_t packet_type = PACKET_PBUF_RAM; ip4_addr_t addr, src_addr, sta_addr; ip4_addr_t netmask;