trombik / esp_wireguard

WireGuard Implementation for ESP-IDF
Other
193 stars 33 forks source link

Crash When remote server goes down #53

Open eroom1966 opened 3 weeks ago

eroom1966 commented 3 weeks ago

I have a strange scenario. I am connected to a server/endpoint and all is working as expected. The server/endpoint then goes down, and I stop receiving traffic.

I then call esp_wireguardif_peer_is_up() to see if it is still available, but this causes a crash The crash is due to the following assert failing

esp_wireguardif_peer_is_up() -> wireguardif_peer_is_up() wireguardif_peer_is_up() -> wireguardif_lookup_peer()

This assert fails due to netif being NULL

static err_t wireguardif_lookup_peer(struct netif *netif, u8_t peer_index, struct wireguard_peer **out) {
    LWIP_ASSERT("netif != NULL", (netif != NULL));

I put a simple fix into this function, to check that ctx->netif is non NULL

esp_err_t esp_wireguardif_peer_is_up(wireguard_ctx_t *ctx)
{
...
    if (!ctx->netif) {
        err = ESP_ERR_INVALID_ARG;
        goto fail;
    }

I don't understand enough about this code, but this solves my problem, what I do not understand is why is ctx->netif==NULL ? All I do to get to this state, is to take down the remote wireguard after connection has been established

Thx Lee

eroom1966 commented 3 weeks ago

Hmm, I think I see my problem. When I see that the remote end has gone away, I call esp_wireguard_disconnect(), this results in setting ctx->netif to NULL a subsequent call to esp_wireguardif_peer_is_up, then causes the crash.

Ultimately what I want to do is install a new certificate when it is no longer valid, I cannot figure out the sequence of calls. Effectively I have this

setup wg_config call esp_wireguard_init() call esp_wireguard_connect(&ctx); check esp_wireguardif_peer_is_up(&ctx)

// all is now running as expected

at some point later, the certificate becomes invalid, and I need to install a new certificate and force a re-connect.

What calls should I make ?