Skip to content

Commit 53fef80

Browse files
committed
Merge branch 'feat/esp_netif_dns_switch_v5.2' into 'release/v5.2'
[LWIP]: Update submodule to bced058f (multi DNS + PPP/mbedTLS) (v5.2) See merge request espressif/esp-idf!32799
2 parents a9da6b3 + 4435526 commit 53fef80

File tree

14 files changed

+267
-52
lines changed

14 files changed

+267
-52
lines changed

components/esp_netif/Kconfig

+12-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,16 @@ menu "ESP NETIF Adapter"
7676
default n
7777
help
7878
Enable LwIP IEEE 802.1D bridge support in ESP-NETIF. Note that "Number of clients store data in netif"
79-
(LWIP_NUM_NETIF_CLIENT_DATA) option needs to be properly configured to be LwIP bridge avaiable!
79+
(LWIP_NUM_NETIF_CLIENT_DATA) option needs to be properly configured to be LwIP bridge available!
80+
81+
config ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
82+
bool "Enable DNS server per interface"
83+
default n
84+
select LWIP_DNS_SETSERVER_WITH_NETIF
85+
help
86+
Enable this option to use the DNS server which belongs to the selected default network interface.
87+
This feature collects DNS server and netif information from LWIP core modules.
88+
Whenever a new default netif is selected, global DNS servers in LWIP are updated with the netif
89+
related servers.
90+
8091
endmenu

components/esp_netif/include/esp_netif.h

+10
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,11 @@ esp_err_t esp_netif_dhcps_get_clients_by_mac(esp_netif_t *esp_netif, int num, es
715715
* and is designed to be set via this API.
716716
* If DHCP client is disabled, all DNS server types can be set via this API only.
717717
*
718+
* Note that LWIP stores DNS server information globally, not per interface, so the first parameter is unused
719+
* in the default LWIP configuration.
720+
* If CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=1 this API sets internal DNS server information per
721+
* netif. It's also possible to set the global DNS server info by supplying esp_netif=NULL
722+
*
718723
* If DHCP server is enabled, the Main DNS Server setting is used by the DHCP server to provide a DNS Server option
719724
* to DHCP clients (Wi-Fi stations).
720725
* - The default Main DNS server is typically the IP of the DHCP server itself.
@@ -740,6 +745,11 @@ esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty
740745
* This may be result of a previous call to esp_netif_set_dns_info(). If the interface's DHCP client is enabled,
741746
* the Main or Backup DNS Server may be set by the current DHCP lease.
742747
*
748+
* Note that LWIP stores DNS server information globally, not per interface, so the first parameter is unused
749+
* in the default LWIP configuration.
750+
* If CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=1 this API returns internally saved DNS server information per
751+
* netif. It's also possible to ask for the global DNS server info by supplying esp_netif=NULL
752+
*
743753
* @param[in] esp_netif Handle to esp-netif instance
744754
* @param[in] type Type of DNS Server to get: ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP, ESP_NETIF_DNS_FALLBACK
745755
* @param[out] dns DNS Server result is written here on success

components/esp_netif/lwip/esp_netif_lwip.c

+71-36
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ do {
113113
action; \
114114
} while(0)
115115

116-
//
117-
// Internal types
118-
//
119-
typedef enum esp_netif_action {
120-
ESP_NETIF_UNDEF,
121-
ESP_NETIF_STARTED,
122-
ESP_NETIF_STOPPED,
123-
ESP_NETIF_SET_DEFAULT,
124-
} esp_netif_action_t;
125-
126116
//
127117
// Internal variables for this module
128118
//
@@ -306,6 +296,11 @@ static void esp_netif_set_default_netif_internal(esp_netif_t *esp_netif)
306296
} else {
307297
netif_set_default(esp_netif->lwip_netif);
308298
}
299+
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
300+
for (int i = 0; i < DNS_MAX_SERVERS; ++i) {
301+
dns_setserver(i, &esp_netif->dns[i]);
302+
}
303+
#endif
309304
}
310305

311306
/**
@@ -316,7 +311,7 @@ static void esp_netif_set_default_netif_internal(esp_netif_t *esp_netif)
316311
static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
317312
{
318313
esp_netif_t *esp_netif = msg->esp_netif;
319-
esp_netif_action_t action = (esp_netif_action_t)msg->data;
314+
esp_netif_route_prio_action_t action = (esp_netif_route_prio_action_t)msg->data;
320315

321316
ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
322317

@@ -336,6 +331,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
336331
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
337332
break;
338333
case ESP_NETIF_STARTED:
334+
case ESP_NETIF_GOT_IP:
339335
{
340336
// check if previously default interface hasn't been destroyed in the meantime
341337
s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
@@ -351,6 +347,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
351347

352348
default:
353349
case ESP_NETIF_STOPPED:
350+
case ESP_NETIF_LOST_IP:
354351
{
355352
s_last_default_esp_netif = NULL;
356353
esp_netif_t *netif = esp_netif_next_unsafe(NULL);
@@ -382,7 +379,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
382379
* @param esp_netif current interface which just updated state
383380
* @param action updating action (on-off)
384381
*/
385-
static esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_action_t action)
382+
esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_route_prio_action_t action)
386383
{
387384
return esp_netif_lwip_ipc_call(esp_netif_update_default_netif_lwip, esp_netif, (void*)action);
388385
}
@@ -503,6 +500,24 @@ void* esp_netif_get_netif_impl(esp_netif_t *esp_netif)
503500
return NULL;
504501
}
505502

503+
#if CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
504+
static void store_dnsserver_info(struct netif* netif, u8_t numdns, const ip_addr_t *dnsserver)
505+
{
506+
if (netif == NULL) {
507+
return;
508+
}
509+
esp_netif_t *esp_netif = lwip_get_esp_netif(netif);
510+
if (esp_netif == NULL || !esp_netif_is_netif_listed(esp_netif)) {
511+
return;
512+
}
513+
if (!ip_addr_isany(dnsserver)) {
514+
ip_addr_copy(esp_netif->dns[numdns], *dnsserver);
515+
} else {
516+
ip_addr_copy(esp_netif->dns[numdns], *IP_ADDR_ANY);
517+
}
518+
}
519+
#endif
520+
506521
static void tcpip_init_done(void *arg)
507522
{
508523
sys_sem_t *init_sem = arg;
@@ -546,6 +561,12 @@ esp_err_t esp_netif_init(void)
546561
sys_sem_wait(&init_sem);
547562
sys_sem_free(&init_sem);
548563
ESP_LOGD(TAG, "LwIP stack has been initialized");
564+
#if CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
565+
if (dns_setserver_callback(store_dnsserver_info) != ERR_OK) {
566+
ESP_LOGE(TAG, "Feiled to configure DNS set server callback");
567+
return ESP_FAIL;
568+
}
569+
#endif
549570
}
550571

551572
#if !LWIP_TCPIP_CORE_LOCKING
@@ -1072,11 +1093,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg)
10721093
ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
10731094
if (ESP_NETIF_IS_POINT2POINT_TYPE(esp_netif, PPP_LWIP_NETIF)) {
10741095
#if CONFIG_PPP_SUPPORT
1075-
esp_err_t ret = esp_netif_start_ppp(esp_netif);
1076-
if (ret == ESP_OK) {
1077-
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
1078-
}
1079-
return ret;
1096+
return esp_netif_start_ppp(esp_netif);
10801097
#endif
10811098
}
10821099

@@ -1153,8 +1170,10 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg)
11531170
LOG_NETIF_DISABLED_AND_DO("IPv4's DHCP Client", return ESP_ERR_NOT_SUPPORTED);
11541171
#endif
11551172
}
1156-
1157-
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
1173+
// For netifs with (active) DHCP client: we update the default netif after getting a valid IP
1174+
if (!((esp_netif->flags & ESP_NETIF_DHCP_CLIENT) && esp_netif->dhcpc_status != ESP_NETIF_DHCP_STOPPED)) {
1175+
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
1176+
}
11581177

11591178
return ESP_OK;
11601179
}
@@ -1303,7 +1322,7 @@ static void esp_netif_internal_dhcpc_cb(struct netif *netif)
13031322
if (memcmp(ip_info, ip_info_old, sizeof(esp_netif_ip_info_t))) {
13041323
evt.ip_changed = true;
13051324
}
1306-
1325+
esp_netif_update_default_netif(esp_netif, ESP_NETIF_GOT_IP);
13071326
memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t));
13081327
memcpy(ip_info_old, ip_info, sizeof(esp_netif_ip_info_t));
13091328
ESP_LOGD(TAG, "if%p ip changed=%d", esp_netif, evt.ip_changed);
@@ -1344,7 +1363,7 @@ static void esp_netif_ip_lost_timer(void *arg)
13441363
.esp_netif = esp_netif,
13451364
};
13461365
int ret;
1347-
1366+
esp_netif_update_default_netif(esp_netif, ESP_NETIF_LOST_IP);
13481367
ESP_LOGD(TAG, "if%p ip lost tmr: raise ip lost event", esp_netif);
13491368
memset(esp_netif->ip_info_old, 0, sizeof(esp_netif_ip_info_t));
13501369
if (esp_netif->lost_ip_event) {
@@ -1663,7 +1682,10 @@ static esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg)
16631682
netif_set_up(lwip_netif);
16641683
netif_set_link_up(lwip_netif);
16651684

1666-
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
1685+
// For netifs with (active) DHCP client: we update the default netif after getting a valid IP
1686+
if (!((esp_netif->flags & ESP_NETIF_DHCP_CLIENT) && esp_netif->dhcpc_status != ESP_NETIF_DHCP_STOPPED)) {
1687+
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
1688+
}
16671689

16681690
return ESP_OK;
16691691
}
@@ -1842,7 +1864,7 @@ static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg)
18421864
if (memcmp(ip_info, esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t))) {
18431865
evt.ip_changed = true;
18441866
}
1845-
1867+
esp_netif_update_default_netif(esp_netif, ESP_NETIF_GOT_IP);
18461868
memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t));
18471869
memcpy(esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t));
18481870
ret = esp_event_post(IP_EVENT, evt_id, &evt, sizeof(evt), 0);
@@ -1903,7 +1925,7 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
19031925

19041926
ip_addr_t lwip_ip = {};
19051927
ESPIP_TO_IP(&dns->ip, &lwip_ip);
1906-
if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
1928+
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
19071929
#if ESP_DHCPS
19081930
// if DHCP server configured to set DNS in dhcps API
19091931
if (type != ESP_NETIF_DNS_MAIN) {
@@ -1916,17 +1938,29 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
19161938
LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED);
19171939
#endif
19181940
} else {
1941+
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
1942+
if (esp_netif) {
1943+
store_dnsserver_info(esp_netif->lwip_netif, type, &lwip_ip);
1944+
}
1945+
if (esp_netif == s_last_default_esp_netif || // if this is the default one -> need to update global DNS servers
1946+
esp_netif == NULL) { // if the netif ptr is set to NULL -> we explicitly require the update
1947+
dns_setserver(type, &lwip_ip);
1948+
}
1949+
#else
19191950
dns_setserver(type, &lwip_ip);
1951+
#endif
19201952
}
19211953

19221954
return ESP_OK;
19231955
}
19241956

19251957
esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns)
19261958
{
1959+
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
19271960
if (esp_netif == NULL) {
19281961
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
19291962
}
1963+
#endif
19301964

19311965
if (dns == NULL) {
19321966
ESP_LOGD(TAG, "set dns null dns");
@@ -1954,7 +1988,7 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
19541988

19551989
ESP_LOGD(TAG, "esp_netif_get_dns_info: esp_netif=%p type=%d", esp_netif, type);
19561990

1957-
if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
1991+
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
19581992
#if ESP_DHCPS
19591993
ip4_addr_t dns_ip;
19601994
dhcps_dns_getserver(esp_netif->dhcps, &dns_ip);
@@ -1965,7 +1999,15 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
19651999
#endif
19662000
} else {
19672001
const ip_addr_t* dns_ip = NULL;
2002+
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
2003+
if (esp_netif == NULL) { // by setting esp_netif to NULL we require the global DNS server entry
2004+
dns_ip = dns_getserver(type);
2005+
} else {
2006+
dns_ip = &esp_netif->dns[type];
2007+
}
2008+
#else
19682009
dns_ip = dns_getserver(type);
2010+
#endif
19692011
if(dns_ip != NULL) {
19702012
IP_TO_ESPIP(dns_ip, &dns->ip);
19712013
}
@@ -1976,9 +2018,11 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
19762018

19772019
esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns)
19782020
{
2021+
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
19792022
if (esp_netif == NULL) {
19802023
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
19812024
}
2025+
#endif
19822026

19832027
if (dns == NULL) {
19842028
ESP_LOGE(TAG, "%s: dns_info cannot be NULL", __func__);
@@ -2074,7 +2118,7 @@ static void esp_netif_internal_nd6_cb(struct netif *netif, uint8_t ip_index)
20742118
ESP_LOGW(TAG,"CONFIG_LWIP_ESP_MLDV6_REPORT not enabled, but esp-netif configured with ESP_NETIF_FLAG_MLDV6_REPORT");
20752119
#endif
20762120
}
2077-
2121+
esp_netif_update_default_netif(esp_netif, ESP_NETIF_GOT_IP);
20782122
memcpy(&evt.ip6_info, &ip6_info, sizeof(esp_netif_ip6_info_t));
20792123
int ret = esp_event_post(IP_EVENT, IP_EVENT_GOT_IP6, &evt, sizeof(evt), 0);
20802124
if (ESP_OK != ret) {
@@ -2516,15 +2560,6 @@ esp_err_t esp_netif_ppp_set_auth(esp_netif_t *esp_netif, esp_netif_auth_type_t a
25162560
{
25172561
set_auth_msg_t msg = { .authtype = authtype, .user = user, .passwd = passwd };
25182562
return esp_netif_lwip_ipc_call(esp_netif_ppp_set_auth_api, esp_netif, &msg);
2519-
#if PPP_AUTH_SUPPORT
2520-
lwip_peer2peer_ctx_t *ppp_ctx = (lwip_peer2peer_ctx_t *)netif->related_data;
2521-
assert(ppp_ctx->base.netif_type == PPP_LWIP_NETIF);
2522-
pppapi_set_auth(ppp_ctx->ppp, authtype, user, passwd);
2523-
return ESP_OK;
2524-
#else
2525-
ESP_LOGE(TAG, "%s failed: No authorisation enabled in menuconfig", __func__);
2526-
return ESP_ERR_ESP_NETIF_IF_NOT_READY;
2527-
#endif
25282563
}
25292564

25302565
esp_err_t esp_netif_napt_disable(esp_netif_t *esp_netif)
@@ -2613,7 +2648,7 @@ static esp_err_t esp_netif_add_ip6_address_api(esp_netif_api_msg_t *msg)
26132648
err_t err = netif_add_ip6_address(msg->esp_netif->lwip_netif, &ip6addr, &index);
26142649
ESP_RETURN_ON_FALSE(err == ERR_OK && index >= 0, ESP_ERR_ESP_NETIF_IP6_ADDR_FAILED, TAG,
26152650
"Failed to add ip6 address");
2616-
2651+
esp_netif_update_default_netif(msg->esp_netif, ESP_NETIF_GOT_IP);
26172652
netif_ip6_addr_set_state(msg->esp_netif->lwip_netif, index,
26182653
addr->preferred ? IP6_ADDR_PREFERRED : IP6_ADDR_DEPRECATED);
26192654
ip_event_got_ip6_t evt = {.esp_netif = msg->esp_netif, .ip_index = index};

components/esp_netif/lwip/esp_netif_lwip_internal.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -114,4 +114,19 @@ struct esp_netif_obj {
114114
#endif // CONFIG_ESP_NETIF_BRIDGE_EN
115115
// mldv6 timer
116116
bool mldv6_report_timer_started;
117+
118+
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
119+
ip_addr_t dns[DNS_MAX_SERVERS];
120+
#endif
117121
};
122+
123+
typedef enum esp_netif_set_default_state {
124+
ESP_NETIF_UNDEF,
125+
ESP_NETIF_STARTED,
126+
ESP_NETIF_GOT_IP,
127+
ESP_NETIF_STOPPED,
128+
ESP_NETIF_LOST_IP,
129+
ESP_NETIF_SET_DEFAULT,
130+
} esp_netif_route_prio_action_t;
131+
132+
esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_route_prio_action_t action);

components/esp_netif/lwip/esp_netif_lwip_ppp.c

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
7171
break;
7272
case PPPERR_CONNECT: /* Connection lost */
7373
ESP_LOGI(TAG, "Connection lost");
74+
esp_netif_update_default_netif(netif, ESP_NETIF_LOST_IP);
7475
err = esp_event_post(IP_EVENT, netif->lost_ip_event, &evt, sizeof(evt), 0);
7576

7677
if (ESP_OK != err) {

0 commit comments

Comments
 (0)