Compare commits

...
Author SHA1 Message Date
J. Nick Koston bb736033c5 [ethernet] Do not restart DHCP while the PHY has no link
The 15 s connect timeout restarted DHCP even with no link. That achieved
nothing, since the running DHCP client resumes on link up, and on ESP32 each
restart emptied lwIP's DNS servers (espressif/esp-idf#19082), breaking name
lookups on a device that runs on its Wi-Fi fallback. The timeout now only
logs while the link is down.
2026-11-16 11:43:12 -06:00
J. Nick Koston 5a0094aece [network] Restore the default interface's DNS servers when lwIP loses them
Starting DHCP on a non-default interface clears lwIP's global DNS table, and
once the default netif is pinned with esp_netif_set_default_netif() ESP-IDF
no longer copies it back (espressif/esp-idf#19082). The route check now also
compares the global table with the default netif's own servers and re-asserts
the default when they differ.

Fixes #19805
2026-11-16 11:36:37 -06:00
3 changed files with 33 additions and 5 deletions

No files matched your search

@@ -358,6 +358,8 @@ class EthernetComponent final : public Component {
// to be called at runtime after enable_on_boot:false without re-allocating, and
// ensures setup() skips the heavy init when enable_on_boot_ is false.
bool ethernet_initialized_{false};
// PHY link state from ETHERNET_EVENT_CONNECTED/DISCONNECTED; the connect timeout retries only with a link.
bool link_up_{false};
#endif
#if LWIP_IPV6
uint8_t ipv6_count_{0};
@@ -160,8 +160,15 @@ void EthernetComponent::loop() {
this->connect_trigger_.trigger();
#endif
} else if (now - this->connect_begin_ > 15000) {
ESP_LOGW(TAG, "Connecting failed; reconnecting");
this->start_connect_();
if (this->link_up_) {
ESP_LOGW(TAG, "Connecting failed; reconnecting");
this->start_connect_();
} else {
// Restarting DHCP without a link only empties lwIP's DNS servers (espressif/esp-idf#19082);
// the DHCP client already running resumes when the link comes up.
ESP_LOGW(TAG, "Connecting failed: no link");
this->connect_begin_ = now;
}
}
break;
case EthernetComponentState::CONNECTED:
@@ -765,10 +772,12 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
event_name = "ETH stopped";
global_eth_component->started_ = false;
global_eth_component->connected_ = false;
global_eth_component->link_up_ = false;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
break;
case ETHERNET_EVENT_CONNECTED:
event_name = "ETH connected";
global_eth_component->link_up_ = true;
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
@@ -785,6 +794,7 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
case ETHERNET_EVENT_DISCONNECTED:
event_name = "ETH disconnected";
global_eth_component->connected_ = false;
global_eth_component->link_up_ = false;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
break;
default:
@@ -12,7 +12,9 @@
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esp_netif_net_stack.h"
#include "lwip/dns.h"
#include "lwip/netif.h"
#include "ip_address.h"
#ifdef USE_ETHERNET
#include "esphome/components/ethernet/ethernet_component.h"
#endif
@@ -72,6 +74,18 @@ static esp_netif_t *connected_ethernet_netif() {
return nullptr;
}
// True when lwIP's global DNS table holds the netif's own servers. Call with the lwIP core lock held.
static bool dns_is_netifs(esp_netif_t *netif) {
for (auto type : {ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP}) {
esp_netif_dns_info_t own{};
if (esp_netif_get_dns_info(netif, type, &own) != ESP_OK)
return true;
if (IPAddress(&own.ip) != IPAddress(dns_getserver(type)))
return false;
}
return true;
}
void NetworkComponent::loop() {
// Pin the default route to the first connected interface in the user's priority
// order; ESP-IDF's own route_prio selection would always favor WiFi.
@@ -114,12 +128,14 @@ void NetworkComponent::loop() {
if (now - this->last_route_check_ < ROUTE_CHECK_INTERVAL_MS)
return;
this->last_route_check_ = now;
bool route_is_ours;
bool still_ours;
{
LwIPLock lock;
route_is_ours = static_cast<void *>(netif_default) == esp_netif_get_netif_impl(best);
// DHCP starting on another netif empties lwIP's DNS table, and the manual default
// stops ESP-IDF from refilling it (espressif/esp-idf#19082); re-asserting copies it back.
still_ours = static_cast<void *>(netif_default) == esp_netif_get_netif_impl(best) && dns_is_netifs(best);
}
if (route_is_ours)
if (still_ours)
return;
}
esp_err_t err = esp_netif_set_default_netif(best);