Restarting wireguard after a network cycle
Hi,
I've been using wireguard on a machine with a wireless network connection, and I noticed the VPN connection no longer works whenever the wireless connection is lost and later reconnected (which happens due to occasional power outages, the machine is on a battery but the internet box is not).
I'm using systemd-networkd, wpa_supplicant and wg-quick on that machine. I noticed the wg-quick@ service unit has
After=network-online.target nss-lookup.target
Wants=network-online.target nss-lookup.target
but from what I understand that only handles the network coming up on boot.
I was browsing the systemd manpages but I'm not sure how to set up what I want. I figured I would probably need to add a dependency to systemd-networkd-wait-online.service in an override for wg-quick@.service in order for the latter to be stopped when network goes down, but I'm not sure whether to use Requires= or BindsTo= (and whether to also add After= or not), and I don't think that would automatically start wg-quick@ again when network is back up.
Anyone has some insight on how to achieve that?
About this post
- Posted
- Archived
- Score
- 7 · 90% up
- Kind
- Text
- ID
- p8oopi
14 comments
Honestly I'd like to find out what stops it from working, first...
Is it incoming or outgoing connections through the VPN that break? I have to assume it's the former (due to the internet box losing firewall state and/or getting a different address), as Wireguard usually handles the latter reliably...
In general, Wireguard is connectionless, so as long as it's able to send packets it should just continue working as if nothing happened, the peer just learns the new endpoint automatically, there's nothing like a TCP connection that needs to be closed and reestablished. (in some cases, PersistentKeepalive is likely to help.)
From what I can tell both directions were failing. I could not connect to the machine using the VPN address and a few logs on the machine were showing outgoing connection issues (all traffic is routed through the wg interface on that machine).
The internet box did not change addresses, I have a fixed ipv4 address.
Unfortunately I could not really investigate while the problem was present because I restarted wg-quick pretty quickly since the machine hosts services that are "critical" for me (most notably e-mail). Investigating logs revealed that the wireless connection had gone down for a few hours, and after it was back any outgoing connection was failing.
I'll try to set up something to reproduce the problem I guess.
I've noticed that if I have a full tunnel up and the base connection breaks, I won't even get an IP (if using DHCP) when the base connection is up again. I use NetworkManager though, but seems like a similar problem.
I solved this by making a dispatcher script that does
wg-quick down... && wg-quick up...each time either ethernet or wifi is connected. Maybe something similar could be done withsystemd-networkd? You could also try setting static routes for the wg server's public IP to always go through the default connection.Does the computer itself have a static IP address, or is it just behind a NAT router that has one?
If it's behind NAT, do you have the tunnel UDP port "port-forwarded"?
If doing that is not an option, you'll need to use PersistentKeepalive to reestablish the NAT state whenever the internet box reboots.
are you using keepalive?
I let systemd - networkd handle wireguard directly and wireguard survives hiccups for me
No, unless that can be configured server-side (which I don't have access to).
There is no server in wireguard, only peers, so you can set it up either or both sides. I set mine to 15 seconds.
If you terminate wireguard and restart it from scratch, all connections using wireguard will be terminated along with it; wireguard is supposed to survive disconnects as long as they're short term, so depending connections won't timeout.
Keepalive is important esp. in home use behind router behind NAT. If you don't send UDP packet out regularly, the router will eventually forget which local client to send incoming packets to. If the local client disconnects it will even forget instantly. So you will be unable to receive data until you send some again first to refresh the routers connection tracking.
Do you connect via a dyndns address? Wireguard resolves the IP only at startup which could be problematic if your router obtains a new public IP after a restart.
I don't
How was this resolved?
Worked around it with a timer that checks for connectivity and restarts the service.