From 7af2865862a7bac9435151fdf7cf270a19e04d41 Mon Sep 17 00:00:00 2001 From: TD-er Date: Sun, 14 Apr 2024 18:15:00 +0200 Subject: [PATCH] [IPv6] Add option to enable/disable IPv6 --- src/src/DataStructs/EthernetEventData.cpp | 16 ++-------- .../DataStructs/FactoryDefault_WiFi_NVS.cpp | 2 ++ src/src/DataStructs/FactoryDefault_WiFi_NVS.h | 3 +- src/src/DataStructs/NodeStruct.cpp | 9 ++++-- src/src/DataStructs/SettingsStruct.h | 5 ++- src/src/DataStructs/WiFiEventData.cpp | 13 ++------ src/src/ESPEasyCore/ESPEasyEth.cpp | 4 ++- src/src/ESPEasyCore/ESPEasyWifi.cpp | 4 ++- src/src/Helpers/StringProvider.cpp | 8 +++++ src/src/Helpers/StringProvider.h | 3 ++ src/src/WebServer/AdvancedConfigPage.cpp | 8 +++++ src/src/WebServer/JSON.cpp | 5 +++ src/src/WebServer/RootPage.cpp | 32 ++++++++++++------- src/src/WebServer/SysInfoPage.cpp | 12 +++++-- 14 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/src/DataStructs/EthernetEventData.cpp b/src/src/DataStructs/EthernetEventData.cpp index 62acc93bf0..008320e9ea 100644 --- a/src/src/DataStructs/EthernetEventData.cpp +++ b/src/src/DataStructs/EthernetEventData.cpp @@ -3,6 +3,7 @@ #if FEATURE_ETHERNET #include "../ESPEasyCore/ESPEasy_Log.h" +#include "../Globals/Settings.h" #include "../Helpers/Networking.h" #include @@ -189,20 +190,9 @@ void EthernetEventData_t::markConnected() { #endif #if FEATURE_USE_IPV6 - ETH.enableIPv6(true); - /* - // workaround for the race condition in LWIP, see https://github.com/espressif/arduino-esp32/pull/9016#discussion_r1451774885 - { - uint32_t i = 5; // try 5 times only - while (esp_netif_create_ip6_linklocal(ETH.netif()) != ESP_OK) { - delay(1); - if (i-- == 0) { -// addLog(LOG_LEVEL_ERROR, ">>>> HELP"); - break; - } - } + if (Settings.EnableIPv6()) { + ETH.enableIPv6(true); } - */ #endif } diff --git a/src/src/DataStructs/FactoryDefault_WiFi_NVS.cpp b/src/src/DataStructs/FactoryDefault_WiFi_NVS.cpp index 6b40da5175..b97559841d 100644 --- a/src/src/DataStructs/FactoryDefault_WiFi_NVS.cpp +++ b/src/src/DataStructs/FactoryDefault_WiFi_NVS.cpp @@ -28,6 +28,7 @@ void FactoryDefault_WiFi_NVS::fromSettings() { bits.WaitWiFiConnect = Settings.WaitWiFiConnect(); bits.SDK_WiFi_autoreconnect = Settings.SDK_WiFi_autoreconnect(); bits.HiddenSSID_SlowConnectPerBSSID = Settings.HiddenSSID_SlowConnectPerBSSID(); + bits.EnableIPv6 = Settings.EnableIPv6(); } void FactoryDefault_WiFi_NVS::applyToSettings() const { @@ -43,6 +44,7 @@ void FactoryDefault_WiFi_NVS::applyToSettings() const { Settings.WaitWiFiConnect(bits.WaitWiFiConnect); Settings.SDK_WiFi_autoreconnect(bits.SDK_WiFi_autoreconnect); Settings.HiddenSSID_SlowConnectPerBSSID(bits.HiddenSSID_SlowConnectPerBSSID); + Settings.EnableIPv6(bits.EnableIPv6); } struct FactoryDefault_WiFi_NVS_securityPrefs { diff --git a/src/src/DataStructs/FactoryDefault_WiFi_NVS.h b/src/src/DataStructs/FactoryDefault_WiFi_NVS.h index f184abdd7b..59a8d3b92c 100644 --- a/src/src/DataStructs/FactoryDefault_WiFi_NVS.h +++ b/src/src/DataStructs/FactoryDefault_WiFi_NVS.h @@ -40,8 +40,9 @@ class FactoryDefault_WiFi_NVS { uint64_t WaitWiFiConnect : 1; uint64_t SDK_WiFi_autoreconnect : 1; uint64_t HiddenSSID_SlowConnectPerBSSID : 1; + uint64_t EnableIPv6 : 1; - uint64_t unused : 52; + uint64_t unused : 51; } bits; uint64_t data{}; diff --git a/src/src/DataStructs/NodeStruct.cpp b/src/src/DataStructs/NodeStruct.cpp index 0bc543662c..9cd1278987 100644 --- a/src/src/DataStructs/NodeStruct.cpp +++ b/src/src/DataStructs/NodeStruct.cpp @@ -62,7 +62,9 @@ bool NodeStruct::validate(const IPAddress& remoteIP) { #if FEATURE_USE_IPV6 // Check if we're in the same global subnet - if (hasIPv6_mac_based_link_global && remoteIP.type() == IPv6) { + if (Settings.EnableIPv6() && + hasIPv6_mac_based_link_global && + remoteIP.type() == IPv6) { const IPAddress this_global = NetworkGlobalIP6(); // Check first 64 bit to see if we're in the same global scope for (int i = 0; i < 8 && hasIPv6_mac_based_link_global; ++i) { @@ -151,7 +153,7 @@ IPAddress NodeStruct::IP() const { #if FEATURE_USE_IPV6 IPAddress NodeStruct::IPv6_link_local(bool stripZone) const { - if (hasIPv6_mac_based_link_local) { + if (Settings.EnableIPv6() && hasIPv6_mac_based_link_local) { // Base IPv6 on MAC address IPAddress ipv6; if (IPv6_link_local_from_MAC(sta_mac, ipv6)) { @@ -166,7 +168,7 @@ IPAddress NodeStruct::IPv6_link_local(bool stripZone) const IPAddress NodeStruct::IPv6_global() const { - if (hasIPv6_mac_based_link_global) { + if (Settings.EnableIPv6() && hasIPv6_mac_based_link_global) { // Base IPv6 on MAC address IPAddress ipv6; if (IPv6_global_from_MAC(sta_mac, ipv6)) { @@ -177,6 +179,7 @@ IPAddress NodeStruct::IPv6_global() const } bool NodeStruct::hasIPv6() const { + if (!Settings.EnableIPv6()) return false; return hasIPv6_mac_based_link_local || hasIPv6_mac_based_link_global; } diff --git a/src/src/DataStructs/SettingsStruct.h b/src/src/DataStructs/SettingsStruct.h index 1fc8af9090..dd0b0a8c4f 100644 --- a/src/src/DataStructs/SettingsStruct.h +++ b/src/src/DataStructs/SettingsStruct.h @@ -188,6 +188,9 @@ class SettingsStruct_tmpl bool HiddenSSID_SlowConnectPerBSSID() const { return !VariousBits_2.HiddenSSID_SlowConnectPerBSSID; } void HiddenSSID_SlowConnectPerBSSID(bool value) { VariousBits_2.HiddenSSID_SlowConnectPerBSSID = !value; } + bool EnableIPv6() const { return !VariousBits_2.EnableIPv6; } + void EnableIPv6(bool value) { VariousBits_2.EnableIPv6 = !value; } + // Use Espressif's auto reconnect. bool SDK_WiFi_autoreconnect() const { return VariousBits_2.SDK_WiFi_autoreconnect; } void SDK_WiFi_autoreconnect(bool value) { VariousBits_2.SDK_WiFi_autoreconnect = value; } @@ -519,7 +522,7 @@ class SettingsStruct_tmpl uint32_t SDK_WiFi_autoreconnect : 1; // Bit 01 uint32_t DisableRulesCodeCompletion : 1; // Bit 02 uint32_t HiddenSSID_SlowConnectPerBSSID : 1; // Bit 03 // inverted - uint32_t unused_04 : 1; // Bit 04 + uint32_t EnableIPv6 : 1; // Bit 04 // inverted uint32_t unused_05 : 1; // Bit 05 uint32_t unused_06 : 1; // Bit 06 uint32_t unused_07 : 1; // Bit 07 diff --git a/src/src/DataStructs/WiFiEventData.cpp b/src/src/DataStructs/WiFiEventData.cpp index f84cdb8f4c..48906d5dc6 100644 --- a/src/src/DataStructs/WiFiEventData.cpp +++ b/src/src/DataStructs/WiFiEventData.cpp @@ -3,6 +3,7 @@ #include "../ESPEasyCore/ESPEasy_Log.h" #include "../Globals/RTC.h" +#include "../Globals/Settings.h" #include "../Globals/WiFi_AP_Candidates.h" #include "../Helpers/ESPEasy_Storage.h" @@ -236,16 +237,8 @@ void WiFiEventData_t::markConnected(const String& ssid, const uint8_t bssid[6], } } #if FEATURE_USE_IPV6 - WiFi.enableIPv6(true); - // workaround for the race condition in LWIP, see https://github.com/espressif/arduino-esp32/pull/9016#discussion_r1451774885 - { - uint32_t i = 5; // try 5 times only - while (esp_netif_create_ip6_linklocal(get_esp_interface_netif(ESP_IF_WIFI_STA)) != ESP_OK) { - delay(1); - if (i-- == 0) { - break; - } - } + if (Settings.EnableIPv6()) { + WiFi.enableIPv6(true); } #endif } diff --git a/src/src/ESPEasyCore/ESPEasyEth.cpp b/src/src/ESPEasyCore/ESPEasyEth.cpp index ab0da4168c..5add9a198c 100644 --- a/src/src/ESPEasyCore/ESPEasyEth.cpp +++ b/src/src/ESPEasyCore/ESPEasyEth.cpp @@ -169,7 +169,9 @@ bool ETHConnectRelaxed() { (eth_clock_mode_t)Settings.ETH_Clock_Mode); #else #if FEATURE_USE_IPV6 - ETH.enableIPv6(true); + if (Settings.EnableIPv6()) { + ETH.enableIPv6(true); + } #endif if (isSPI_EthernetType(Settings.ETH_Phy_Type)) { diff --git a/src/src/ESPEasyCore/ESPEasyWifi.cpp b/src/src/ESPEasyCore/ESPEasyWifi.cpp index 46f88667d0..a1a1340d2c 100644 --- a/src/src/ESPEasyCore/ESPEasyWifi.cpp +++ b/src/src/ESPEasyCore/ESPEasyWifi.cpp @@ -494,7 +494,9 @@ void AttemptWiFiConnect() { const String key = WiFi_AP_CandidatesList::get_key(candidate.index); #if FEATURE_USE_IPV6 - WiFi.enableIPv6(true); + if (Settings.EnableIPv6()) { + WiFi.enableIPv6(true); + } #endif if ((Settings.HiddenSSID_SlowConnectPerBSSID() || !candidate.bits.isHidden) diff --git a/src/src/Helpers/StringProvider.cpp b/src/src/Helpers/StringProvider.cpp index 5b869388cd..7924a30c41 100644 --- a/src/src/Helpers/StringProvider.cpp +++ b/src/src/Helpers/StringProvider.cpp @@ -194,6 +194,10 @@ const __FlashStringHelper * getLabel(LabelType::Enum label) { case LabelType::CONNECT_HIDDEN_SSID: return F("Include Hidden SSID"); case LabelType::HIDDEN_SSID_SLOW_CONNECT: return F("Hidden SSID Slow Connect"); case LabelType::SDK_WIFI_AUTORECONNECT: return F("Enable SDK WiFi Auto Reconnect"); +#if FEATURE_USE_IPV6 + case LabelType::ENABLE_IPV6: return F("Enable IPv6"); +#endif + case LabelType::BUILD_DESC: return F("Build"); case LabelType::GIT_BUILD: return F("Git Build"); @@ -509,6 +513,10 @@ String getValue(LabelType::Enum label) { case LabelType::CONNECT_HIDDEN_SSID: return jsonBool(Settings.IncludeHiddenSSID()); case LabelType::HIDDEN_SSID_SLOW_CONNECT: return jsonBool(Settings.HiddenSSID_SlowConnectPerBSSID()); case LabelType::SDK_WIFI_AUTORECONNECT: return jsonBool(Settings.SDK_WiFi_autoreconnect()); +#if FEATURE_USE_IPV6 + case LabelType::ENABLE_IPV6: return jsonBool(Settings.EnableIPv6()); +#endif + case LabelType::BUILD_DESC: return getSystemBuildString(); case LabelType::GIT_BUILD: diff --git a/src/src/Helpers/StringProvider.h b/src/src/Helpers/StringProvider.h index 0a487af2b7..da8d33187b 100644 --- a/src/src/Helpers/StringProvider.h +++ b/src/src/Helpers/StringProvider.h @@ -142,6 +142,9 @@ struct LabelType { HIDDEN_SSID_SLOW_CONNECT, CONNECT_HIDDEN_SSID, SDK_WIFI_AUTORECONNECT, +#if FEATURE_USE_IPV6 + ENABLE_IPV6, +#endif BUILD_DESC, GIT_BUILD, diff --git a/src/src/WebServer/AdvancedConfigPage.cpp b/src/src/WebServer/AdvancedConfigPage.cpp index 8de04c71c4..f34ec61904 100644 --- a/src/src/WebServer/AdvancedConfigPage.cpp +++ b/src/src/WebServer/AdvancedConfigPage.cpp @@ -134,6 +134,11 @@ void handle_advanced() { Settings.WaitWiFiConnect(isFormItemChecked(LabelType::WAIT_WIFI_CONNECT)); Settings.HiddenSSID_SlowConnectPerBSSID(isFormItemChecked(LabelType::HIDDEN_SSID_SLOW_CONNECT)); Settings.SDK_WiFi_autoreconnect(isFormItemChecked(LabelType::SDK_WIFI_AUTORECONNECT)); +#if FEATURE_USE_IPV6 + Settings.EnableIPv6(isFormItemChecked(LabelType::ENABLE_IPV6)); + addFormNote(F("Toggling IPv6 requires reboot")); +#endif + #ifndef BUILD_NO_RAM_TRACKER @@ -387,6 +392,9 @@ void handle_advanced() { addFormCheckBox(LabelType::WAIT_WIFI_CONNECT, Settings.WaitWiFiConnect()); addFormCheckBox(LabelType::SDK_WIFI_AUTORECONNECT, Settings.SDK_WiFi_autoreconnect()); addFormCheckBox(LabelType::HIDDEN_SSID_SLOW_CONNECT, Settings.HiddenSSID_SlowConnectPerBSSID()); +#if FEATURE_USE_IPV6 + addFormCheckBox(LabelType::ENABLE_IPV6, Settings.EnableIPv6()); +#endif diff --git a/src/src/WebServer/JSON.cpp b/src/src/WebServer/JSON.cpp index 000400aaef..631e20d9f6 100644 --- a/src/src/WebServer/JSON.cpp +++ b/src/src/WebServer/JSON.cpp @@ -251,6 +251,7 @@ void handle_json() #if FEATURE_USE_IPV6 LabelType::IP6_LOCAL, LabelType::IP6_GLOBAL, + LabelType::ENABLE_IPV6, #endif LabelType::IP_SUBNET, LabelType::GATEWAY, @@ -288,6 +289,10 @@ void handle_json() LabelType::WIFI_USE_LAST_CONN_FROM_RTC, LabelType::WIFI_RSSI, + LabelType::WAIT_WIFI_CONNECT, + LabelType::HIDDEN_SSID_SLOW_CONNECT, + LabelType::CONNECT_HIDDEN_SSID, + LabelType::SDK_WIFI_AUTORECONNECT, LabelType::MAX_LABEL }; diff --git a/src/src/WebServer/RootPage.cpp b/src/src/WebServer/RootPage.cpp index f7c8b2f15a..e7065ef6d9 100644 --- a/src/src/WebServer/RootPage.cpp +++ b/src/src/WebServer/RootPage.cpp @@ -197,8 +197,10 @@ void handle_root() { { addRowLabelValue(LabelType::IP_ADDRESS); #if FEATURE_USE_IPV6 - addRowLabelValue(LabelType::IP6_LOCAL); - // Do not show global IPv6 on the root page + if (Settings.EnableIPv6()) { + addRowLabelValue(LabelType::IP6_LOCAL); + // Do not show global IPv6 on the root page + } #endif addRowLabel(LabelType::WIFI_RSSI); addHtml(strformat( @@ -213,8 +215,10 @@ void handle_root() { addRowLabelValue(LabelType::ETH_SPEED_STATE); addRowLabelValue(LabelType::ETH_IP_ADDRESS); #if FEATURE_USE_IPV6 - addRowLabelValue(LabelType::ETH_IP6_LOCAL); - // Do not show global IPv6 on the root page + if (Settings.EnableIPv6()) { + addRowLabelValue(LabelType::ETH_IP6_LOCAL); + // Do not show global IPv6 on the root page + } #endif } # endif // if FEATURE_ETHERNET @@ -337,8 +341,10 @@ void handle_root() { if (it->second.ip[0] != 0 #if FEATURE_USE_IPV6 - || it->second.hasIPv6_mac_based_link_local - || it->second.hasIPv6_mac_based_link_global + || (Settings.EnableIPv6() && + (it->second.hasIPv6_mac_based_link_local || + it->second.hasIPv6_mac_based_link_global) + ) #endif ) { @@ -347,12 +353,14 @@ void handle_root() { #if FEATURE_USE_IPV6 bool isIPv6 = false; - if (it->second.hasIPv6_mac_based_link_local) { - ip = it->second.IPv6_link_local(true); - isIPv6 = true; - } else if (it->second.hasIPv6_mac_based_link_global) { - ip = it->second.IPv6_global(); - isIPv6 = true; + if (Settings.EnableIPv6()) { + if (it->second.hasIPv6_mac_based_link_local) { + ip = it->second.IPv6_link_local(true); + isIPv6 = true; + } else if (it->second.hasIPv6_mac_based_link_global) { + ip = it->second.IPv6_global(); + isIPv6 = true; + } } if (it->second.hasIPv4 && it->second.hasIPv6()) { // Add 2 buttons for IPv4 and IPv6 address diff --git a/src/src/WebServer/SysInfoPage.cpp b/src/src/WebServer/SysInfoPage.cpp index e8ba02427e..c660d44c39 100644 --- a/src/src/WebServer/SysInfoPage.cpp +++ b/src/src/WebServer/SysInfoPage.cpp @@ -121,8 +121,10 @@ void handle_sysinfo_json() { json_prop(F("dhcp"), useStaticIP() ? getLabel(LabelType::IP_CONFIG_STATIC) : getLabel(LabelType::IP_CONFIG_DYNAMIC)); json_prop(F("ip"), getValue(LabelType::IP_ADDRESS)); #if FEATURE_USE_IPV6 - json_prop(F("ip6_local"), getValue(LabelType::IP6_LOCAL)); - json_prop(F("ip6_global"), getValue(LabelType::IP6_GLOBAL)); + if (Settings.EnableIPv6()) { + json_prop(F("ip6_local"), getValue(LabelType::IP6_LOCAL)); + json_prop(F("ip6_global"), getValue(LabelType::IP6_GLOBAL)); + } #endif json_prop(F("subnet"), getValue(LabelType::IP_SUBNET)); @@ -153,7 +155,8 @@ void handle_sysinfo_json() { json_prop(F("ethstate"), getValue(LabelType::ETH_STATE)); json_prop(F("ethspeedstate"), getValue(LabelType::ETH_SPEED_STATE)); #if FEATURE_USE_IPV6 - json_prop(F("ethipv6local"), getValue(LabelType::ETH_IP6_LOCAL)); + if (Settings.EnableIPv6()) + json_prop(F("ethipv6local"), getValue(LabelType::ETH_IP6_LOCAL)); #endif json_close(); # endif // if FEATURE_ETHERNET @@ -542,6 +545,9 @@ void handle_sysinfo_WiFiSettings() { LabelType::HIDDEN_SSID_SLOW_CONNECT, LabelType::CONNECT_HIDDEN_SSID, LabelType::SDK_WIFI_AUTORECONNECT, +#if FEATURE_USE_IPV6 + LabelType::ENABLE_IPV6, +#endif LabelType::MAX_LABEL };