Skip to content

Commit

Permalink
napt: Fix ip_portmap_add() to keep only one port mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
udoudou authored and espressif-abhikroy committed Sep 22, 2023
1 parent 7033e26 commit d65ad24
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/core/ipv4/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 21 additions & 4 deletions src/core/ipv4/ip4_napt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion test/unit/core/test_ip4_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d65ad24

Please sign in to comment.