dhcp: Fix on-demand fine timer on more dhcp clients

2.1.3-esp: 705dd71d dhcp:init fine timer when dhcp start
2.1.3-esp: 86df9f44 lwip/dhcp: Fixed ondemand fine timers bug, that allowed only one dhcp client to run at a time without issue.
2.1.3-esp: d5e56d06 dhcp: Fix dhcp_fine_tmr() not to use netif after free
2.1.3-esp: 8dad8d3e Fix: Added check to ensure safe restart of dhcp fine timer
This commit is contained in:
xueyunfei
2024-09-11 08:06:33 +02:00
committed by David Cermak
parent b1ceb9d082
commit 15e29ca9d4
3 changed files with 53 additions and 33 deletions
+43 -30
View File
@@ -86,16 +86,15 @@
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
#include <stdbool.h>
static bool is_tmr_start = false;
#define ESP_LWIP_DHCP_FINE_TIMER_START_ONCE() if (!is_tmr_start) { \
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_timeout_cb, NULL); \
is_tmr_start = true; }
#define ESP_LWIP_DHCP_FINE_CLOSE() if (is_tmr_start) { \
sys_untimeout(dhcp_fine_timeout_cb, NULL); \
is_tmr_start = false; }
#define ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp) if(!dhcp->fine_timer_enabled) { \
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_timeout_cb, (void *)netif); \
dhcp->fine_timer_enabled = true;}
#define ESP_LWIP_DHCP_FINE_CLOSE(netif, dhcp) if(dhcp->fine_timer_enabled) { \
sys_untimeout(dhcp_fine_timeout_cb, (void *)netif); \
dhcp->fine_timer_enabled = false;}
#else
#define ESP_LWIP_DHCP_FINE_TIMER_START_ONCE()
#define ESP_LWIP_DHCP_FINE_CLOSE()
#define ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp)
#define ESP_LWIP_DHCP_FINE_CLOSE(netif, dhcp)
#endif /* ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */
#ifdef LWIP_HOOK_FILENAME
@@ -326,8 +325,7 @@ dhcp_dec_pcb_refcount(void)
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
void dhcp_fine_timeout_cb(void *arg)
{
LWIP_UNUSED_ARG(arg);
dhcp_fine_tmr();
dhcp_fine_tmr((struct netif *)arg);
}
#endif
@@ -391,7 +389,7 @@ dhcp_conflict_callback(struct netif *netif, acd_callback_enum_t state)
msecs = 10 * 1000;
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp);
break;
case ACD_DECLINE:
@@ -524,7 +522,7 @@ dhcp_select(struct netif *netif)
msecs = DHCP_REQUEST_BACKOFF_SEQUENCE(DHCP_STATE_REQUESTING, dhcp->tries);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp);
return result;
}
@@ -570,18 +568,28 @@ dhcp_coarse_tmr(void)
* A DHCP server is expected to respond within a short period of time.
* This timer checks whether an outstanding DHCP request is timed out.
*/
void
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
dhcp_fine_tmr(struct netif *netif)
#else
dhcp_fine_tmr(void)
#endif
{
struct netif *netif;
struct dhcp *dhcp;
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
bool tmr_restart = false;
#endif /* ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */
if (netif == NULL) {
return;
}
#else
struct netif *netif;
/* loop through netif's */
NETIF_FOREACH(netif) {
struct dhcp *dhcp = netif_dhcp_data(netif);
#endif /* ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */
dhcp = netif_dhcp_data(netif);
/* only act on DHCP configured interfaces */
if (dhcp != NULL) {
/* timer is active (non zero), and is about to trigger now */
@@ -601,15 +609,19 @@ dhcp_fine_tmr(void)
#endif
}
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
if (tmr_restart) {
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_timeout_cb, NULL);
} else {
sys_untimeout(dhcp_fine_timeout_cb, NULL);
is_tmr_start = false;
}
if (tmr_restart) {
if (dhcp->fine_timer_enabled == true) {
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_timeout_cb, (void *)netif);
}
} else {
sys_untimeout(dhcp_fine_timeout_cb, (void *)netif);
dhcp->fine_timer_enabled = false;
}
#endif
}
#if !ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
}
#endif
}
/**
@@ -930,7 +942,7 @@ dhcp_start(struct netif *netif)
return result;
}
#endif
ESP_LWIP_DHCP_FINE_CLOSE(netif, dhcp);
/* (re)start the DHCP negotiation */
result = dhcp_discover(netif);
if (result != ERR_OK) {
@@ -1131,7 +1143,7 @@ dhcp_discover(struct netif *netif)
msecs = DHCP_REQUEST_BACKOFF_SEQUENCE(DHCP_STATE_SELECTING, dhcp->tries);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp);
return result;
}
@@ -1204,7 +1216,7 @@ dhcp_bind(struct netif *netif)
/* netif is now bound to DHCP leased address - set this before assigning the address
to ensure the callback can use dhcp_supplied_address() */
dhcp_set_state(dhcp, DHCP_STATE_BOUND);
ESP_LWIP_DHCP_FINE_CLOSE();
ESP_LWIP_DHCP_FINE_CLOSE(netif, dhcp);
netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
/* interface is used by routing now that an address is set */
@@ -1264,7 +1276,7 @@ dhcp_renew(struct netif *netif)
msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp);
return result;
}
@@ -1319,7 +1331,7 @@ dhcp_rebind(struct netif *netif)
msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp);
return result;
}
@@ -1377,7 +1389,7 @@ dhcp_reboot(struct netif *netif)
msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE(netif, dhcp);
return result;
}
@@ -1451,6 +1463,7 @@ dhcp_release_and_stop(struct netif *netif)
acd_remove(netif, &dhcp->acd);
#endif
ESP_LWIP_DHCP_FINE_CLOSE(netif, dhcp);
if (dhcp->pcb_allocated != 0) {
dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
dhcp->pcb_allocated = 0;
+6 -1
View File
@@ -93,6 +93,9 @@ struct dhcp
/** see DHCP_FLAG_* */
u8_t flags;
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
u8_t fine_timer_enabled;
#endif
dhcp_timeout_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
dhcp_timeout_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
dhcp_timeout_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
@@ -135,8 +138,10 @@ u8_t dhcp_supplied_address(const struct netif *netif);
/* to be called every minute */
void dhcp_coarse_tmr(void);
/* to be called every half second */
#if !ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
void dhcp_fine_tmr(void);
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
#else
void dhcp_fine_tmr(struct netif *netif);
void dhcp_fine_timeout_cb(void *arg);
#endif
+4 -2
View File
@@ -158,9 +158,11 @@ static void tick_lwip(void)
#endif
if (tick % 5 == 0) {
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
sys_untimeout(dhcp_fine_timeout_cb, NULL);
#endif
sys_untimeout(dhcp_fine_timeout_cb, (void *)&net_test);
dhcp_fine_tmr(&net_test);
#else
dhcp_fine_tmr();
#endif
}
if (tick % (DHCP_COARSE_TIMER_SECS * 10) == 0) {
dhcp_coarse_tmr();