From 32c6f960009f16341d1f8b6c20b29f127fff0b84 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Fri, 19 Dec 2014 16:04:48 +0100 Subject: [PATCH] prevent dhcp from starting when netif link is down (only when LWIP_DHCP_CHECK_LINK_UP==1, which is disabled by default for compatibility reasons) --- CHANGELOG | 5 +++++ src/core/dhcp.c | 25 +++++++++++++++++++------ src/include/lwip/dhcp.h | 4 ++-- src/include/lwip/opt.h | 10 ++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d1257cd2..4d6776ae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -143,6 +143,11 @@ HISTORY ++ Bugfixes: + 2014-12-19: Simon Goldschmidt + * opt.h, dhcp.h/.c: prevent dhcp from starting when netif link is down (only + when LWIP_DHCP_CHECK_LINK_UP==1, which is disabled by default for + compatibility reasons) + 2014-12-17: Simon Goldschmidt * tcp_out.c: fixed bug #43840 Checksum error for TCP_CHECKSUM_ON_COPY==1 for no-copy data with odd length diff --git a/src/core/dhcp.c b/src/core/dhcp.c index 6a734d7b..49fd632d 100644 --- a/src/core/dhcp.c +++ b/src/core/dhcp.c @@ -627,14 +627,11 @@ err_t dhcp_start(struct netif *netif) { struct dhcp *dhcp; - err_t result = ERR_OK; + err_t result; LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;); dhcp = netif->dhcp; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); - /* Remove the flag that says this netif is handled by DHCP, - it is set when we succeeded starting. */ - netif->flags &= ~NETIF_FLAG_DHCP; /* check hwtype of the netif */ if ((netif->flags & NETIF_FLAG_ETHARP) == 0) { @@ -648,6 +645,10 @@ dhcp_start(struct netif *netif) return ERR_MEM; } + /* Remove the flag that says this netif is handled by DHCP, + it is set when we succeeded starting. */ + netif->flags &= ~NETIF_FLAG_DHCP; + /* no DHCP client attached yet? */ if (dhcp == NULL) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n")); @@ -668,7 +669,7 @@ dhcp_start(struct netif *netif) LWIP_ASSERT("pbuf p_out wasn't freed", dhcp->p_out == NULL); LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL ); } - + /* clear data structure */ memset(dhcp, 0, sizeof(struct dhcp)); /* dhcp_set_state(&dhcp, DHCP_OFF); */ @@ -685,6 +686,16 @@ dhcp_start(struct netif *netif) /* set up the recv callback and argument */ udp_recv(dhcp->pcb, dhcp_recv, netif); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n")); + +#if LWIP_DHCP_CHECK_LINK_UP + if (!netif_is_link_up(netif)) { + /* set state INIT and wait for dhcp_network_changed() to call dhcp_discover() */ + dhcp_set_state(dhcp, DHCP_INIT); + netif->flags |= NETIF_FLAG_DHCP; + return ERR_OK; + } +#endif /* LWIP_DHCP_CHECK_LINK_UP */ + /* (re)start the DHCP negotiation */ result = dhcp_discover(netif); if (result != ERR_OK) { @@ -779,7 +790,9 @@ dhcp_network_changed(struct netif *netif) /* stay off */ break; default: - dhcp->tries = 0; + /* INIT/REQUESTING/CHECKING/BACKING_OFF restart with new 'rid' because the + state changes, SELECTING: continue with current 'rid' as we stay in the + same state */ #if LWIP_DHCP_AUTOIP_COOP if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) { autoip_stop(netif); diff --git a/src/include/lwip/dhcp.h b/src/include/lwip/dhcp.h index c8eede1c..9e4cff3f 100644 --- a/src/include/lwip/dhcp.h +++ b/src/include/lwip/dhcp.h @@ -161,9 +161,9 @@ void dhcp_fine_tmr(void); #define DHCP_REBINDING 4 #define DHCP_RENEWING 5 #define DHCP_SELECTING 6 -#define DHCP_INFORMING 7 +/* not yet implemented #define DHCP_INFORMING 7*/ #define DHCP_CHECKING 8 -#define DHCP_PERMANENT 9 +/* not yet implemented #define DHCP_PERMANENT 9*/ #define DHCP_BOUND 10 /** not yet implemented #define DHCP_RELEASING 11 */ #define DHCP_BACKING_OFF 12 diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index 3eec7d63..26d4a800 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -751,6 +751,16 @@ #define DHCP_DOES_ARP_CHECK ((LWIP_DHCP) && (LWIP_ARP)) #endif +/** + * LWIP_DHCP_CHECK_LINK_UP==1: dhcp_start() only really starts if the netif has + * NETIF_FLAG_LINK_UP set in its flags. As this is only an optimization and + * netif drivers might not set this flag, the default is off. If enabled, + * netif_set_link_up() must be called to continue dhcp starting. + */ +#ifndef LWIP_DHCP_CHECK_LINK_UP +#define LWIP_DHCP_CHECK_LINK_UP 0 +#endif + /** * LWIP_DHCP_BOOTP_FILE==1: Store offered_si_addr and boot_file_name. */