How to set a static global IPV6 address using esp-idf 5.2.1

kiwironnie
Posts: 5
Joined: Mon May 20, 2024 7:55 pm

How to set a static global IPV6 address using esp-idf 5.2.1

Postby kiwironnie » Mon May 20, 2024 8:06 pm

Hi All
The latest ESP FAQ Handbook says: "If it is a global static IP, both IPv6 and IPv4 support manual configuration"
However I've not been able to discover how to successfully achieve this, particularly to establish a WiFi connection with an ESP32 board.
Rather than static the preference is actually to retrieve a global IPv6 address via dhcp6 (via an Ubuntu 22.04 dnsmasq server, served up according to the client's MAC address using the normal dhcp-host method) but it doesn't look like this is possible right now.
Help would be very much appreciated, either a suitable manual reference(s) or example code. Cheers Ron.

kiwironnie
Posts: 5
Joined: Mon May 20, 2024 7:55 pm

Re: How to set a static global IPV6 address using esp-idf 5.2.1

Postby kiwironnie » Wed May 22, 2024 3:27 am

Partly answering myself. To enable SLAAC global addresses it is necessary to set a kconfig setting using menuconfig as described here:
https://github.com/espressif/arduino-esp32/issues/6626

"On current ESP-IDF version, I can configure sample projects to receive IPv6 either via SLAAC or DCHP6 via idf.py menuconfig -> Component config ---> LWIP ---> Enable IPV6 stateless address autoconfiguration (SLAAC)"

kiwironnie
Posts: 5
Joined: Mon May 20, 2024 7:55 pm

Re: How to set a static global IPV6 address using esp-idf 5.2.1

Postby kiwironnie » Mon Jun 24, 2024 7:04 am

Responding again in case it helps other.

Eventually I did manage to find an obscure reference (not in the latest esp-idf manual) that enables a static IPV6 address to be configured. The code for the relevant wifi setup function in my code is provided here:
  1.  
  2. // Static IPv6 configuration
  3. #define STATIC_IPV6_ADDR "2001:470:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX"
  4.  
  5. void wifi_init_sta() {
  6.  
  7.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  8.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  9.    
  10.     ESP_ERROR_CHECK(esp_netif_init());
  11.     // Initialize the event loop
  12.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  13.  
  14.     sta_netif = esp_netif_create_default_wifi_sta();
  15.     esp_netif_set_hostname(sta_netif, hostname);
  16.  
  17.     esp_netif_dhcpc_stop(sta_netif);  // Disable DHCP client for IPv4
  18.  
  19.     if (!sta_netif) {
  20.         ESP_LOGE(TAG, "Failed to create default WiFi station interface");
  21.         return;
  22.     }
  23.  
  24.      ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
  25.     // Register handler for IPv6 events
  26.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &wifi_event_handler, NULL));
  27.  
  28.  
  29.     wifi_config_t wifi_config = {
  30.         .sta = {
  31.             .ssid = SSID,
  32.             .password = PASSWORD,
  33.         },
  34.     };
  35.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  36.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  37.  
  38.     ESP_ERROR_CHECK(esp_wifi_start());
  39.  
  40.     // Configure static IPV6 address
  41.  
  42.     esp_netif_ip6_info_t ip6_addr = { 0 };
  43.     ip6addr_aton(STATIC_IPV6_ADDR, (ip6_addr_t *)&ip6_addr.ip);
  44.     struct netif *lwip_netif = esp_netif_get_netif_impl(sta_netif);
  45.     ESP_ERROR_CHECK(netif_add_ip6_address(lwip_netif, (ip6_addr_t *)&ip6_addr.ip, 0));
  46.     // ESP_ERROR_CHECK(esp_netif_set_dns_info(sta_netif), ESP_NETIF_DNS_MAIN, &dns_info));
  47.  
  48. }
  49.  
Cheers
Ron

eriksl
Posts: 111
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: How to set a static global IPV6 address using esp-idf 5.2.1

Postby eriksl » Sat Jul 27, 2024 8:08 am

The way HOW you set a static ipv6 address is properly documented. What's not documented is how you should set netmask and gateway.

DHCPv6 is not supported, this also documented. Best use SLAAC then.

Who is online

Users browsing this forum: username and 44 guests