trombik / esp_wireguard

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

Free wireguard_device after netif is removed #45

Closed jefftharris closed 7 months ago

jefftharris commented 1 year ago

As a fix for #38 , the wireguard_device stored as the netif state pointer must remain valid until after the netif is removed. During removal, packets may be sent to disconnect TCP sockets and such requiring a valid device for the wireguardif_output function.

Move the device free from the wireguardif_shutdown function into a new wireguardif_fini function which is called after the netif_remove. Add a device check in wireguardif_output as a precaution.

Make an explicit call to clear the IPv4 address of the Wireguard interface when disconnecting. The call mimics the behavior in netif_remove however it is performed before the Wireguard peers are shutdown. As a result, the TCP reset packets can be sent.

Reproduced on an ESP32 by creating an open TCP connection over the Wireguard VPN and then causing the VPN to disconnect. With the changes, the TCP connection is closed, and the crash does not occur.

trombik commented 7 months ago

Thanks for the fix. Unfortunately, I have been unable to confirm the fix due to changes in my private life.

CatoLynx commented 2 weeks ago

I had the same issue and tested this fix (after coming up with pretty much the same fix myself), but unfortunately, it still crashes. While it previously (without the fix) crashed with a proper backtrace, with the fix (your version as well as my version), it crashes with an InstructionFetchError.

So there seems to be something more to this... I have also tried out various orderings of the function calls in esp_wireguard_disconnect, to no avail.