Skip to content

Commit b832dee

Browse files
Fix LWIP failure after 256 Ethernet disconnects (#3212)
Thanks to Yohine. He identified via email a leak of DHCP state that would cause LWIP to panic() after 256 disconnects. Properly clean up DHCP state on link ::end (shutdown).
1 parent b87270c commit b832dee

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

libraries/lwIP_Ethernet/src/LwipIntfDev.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ bool LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) {
350350
lwip_init();
351351
__startEthernetContext();
352352

353+
memset(&_netif, 0, sizeof(_netif));
354+
353355
if (RawDev::needsSPI()) {
354356
_spiUnit.begin();
355357
// Set SPI clocks/etc. per request, doesn't seem to be direct way other than a fake transaction
@@ -390,8 +392,7 @@ bool LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) {
390392

391393
_netif.hostname = wifi_station_hostname;
392394

393-
if (!netif_add(&_netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gw), this,
394-
netif_init_s, ethernet_input)) {
395+
if (!netif_add(&_netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gw), this, netif_init_s, ethernet_input)) {
395396
return false;
396397
}
397398

@@ -410,15 +411,10 @@ bool LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) {
410411

411412
// Start a new DHCP request
412413
_netif.flags |= NETIF_FLAG_UP;
413-
switch (dhcp_start(&_netif)) {
414-
case ERR_OK:
415-
break;
416-
417-
case ERR_IF:
418-
netif_remove(&_netif);
419-
return false;
420-
421-
default:
414+
if (dhcp_start(&_netif) != ERR_OK) {
415+
if (_intrPin < 0) {
416+
__removeEthernetPacketHandler(_phID);
417+
}
422418
netif_remove(&_netif);
423419
return false;
424420
}
@@ -465,6 +461,9 @@ extern std::function<void(struct netif *)> _removeNetifCB;
465461
template<class RawDev>
466462
void LwipIntfDev<RawDev>::end() {
467463
if (_started) {
464+
if (_isDHCP) {
465+
dhcp_stop(&_netif);
466+
}
468467
if (_intrPin < 0) {
469468
__removeEthernetPacketHandler(_phID);
470469
} else {

0 commit comments

Comments
 (0)