Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free wireguard_device after netif is removed #45

Merged
merged 1 commit into from
Feb 7, 2024

Conversation

jefftharris
Copy link
Contributor

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.

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.
@trombik
Copy link
Owner

trombik commented Feb 7, 2024

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

@trombik trombik merged commit 5f9d5a0 into trombik:main Feb 7, 2024
@jefftharris jefftharris mentioned this pull request Sep 1, 2024
@CatoLynx
Copy link

CatoLynx commented Sep 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants