Free wireguard_device after netif is removed #45
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.