Skip to content

netif_add and netif_set_addr crash with non-zero IP on ESP32-C3 #59

@SensorsIot

Description

@SensorsIot

When using esp_wireguard on ESP32-C3, the device crashes with "Load access fault" when allowed_ip is set to a non-zero IP address (e.g., 10.0.0.2).

Environment

  • ESP-IDF: 5.x (via PlatformIO)
  • esp_wireguard version: 0.9.0
  • Target: ESP32-C3

Steps to reproduce

wireguard_config_t wg_config = {0};
wg_config.private_key = "...";
wg_config.public_key = "...";
wg_config.allowed_ip = "10.0.0.2";      // Non-zero IP causes crash
wg_config.allowed_ip_mask = "255.255.255.0";
wg_config.endpoint = "vpn.example.com";
wg_config.port = 51820;

esp_wireguard_init(&wg_config, &ctx);
esp_wireguard_connect(&ctx);  // Crashes here

Expected behavior

WireGuard interface created with IP 10.0.0.2

Actual behavior

Guru Meditation Error: Core 0 panic'ed (Load access fault). Exception was unhandled.

The crash occurs inside netif_add() in esp_wireguard_netif_create(). Using netif_set_addr() after creating the netif with 0.0.0.0 also crashes.

Workaround

In esp_wireguard.c, modify esp_wireguard_netif_create() to create the netif with 0.0.0.0 and then set the real IP via direct struct assignment:

// Create netif with zero IP to avoid crash
ip_addr_t zero_ip = IPADDR4_INIT_BYTES(0, 0, 0, 0);
ip_addr_t zero_mask = IPADDR4_INIT_BYTES(0, 0, 0, 0);
wg_netif = netif_add(&wg_netif_struct, ip_2_ip4(&zero_ip), ip_2_ip4(&zero_mask), 
                     ip_2_ip4(&gateway), &wg, &wireguardif_init, &ip_input);

// Set real IP via direct assignment (netif_set_addr also crashes)
ip4_addr_copy(wg_netif->ip_addr.u_addr.ip4, *ip_2_ip4(&ip_addr));
ip4_addr_copy(wg_netif->netmask.u_addr.ip4, *ip_2_ip4(&netmask));
ip4_addr_copy(wg_netif->gw.u_addr.ip4, *ip_2_ip4(&gateway));

This workaround allows the WireGuard tunnel to function correctly. The tunnel connects, handshake completes, and traffic flows through the VPN.

Notes

  • Using allowed_ip = "0.0.0.0" does not crash, but the interface has no IP and routing fails
  • The crash address (MEPC) is in ROM, suggesting a low-level memory access issue
  • This may be specific to ESP32-C3 or certain ESP-IDF/lwIP versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions