Skip to content

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?

7 points12 comments
Open on Reddit
About this post
Posted
Archived
Score
7 · 90% up
Kind
Text
ID
p8oopi

Author

13
posts
61
comments
3
media
View profile

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.)

2 points

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.

1 points

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 with systemd-networkd? You could also try setting static routes for the wg server's public IP to always go through the default connection.

1 points

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.

1 points
unknown author3 hidden

are you using keepalive?

I let systemd - networkd handle wireguard directly and wireguard survives hiccups for me

1 points

are you using keepalive?

No, unless that can be configured server-side (which I don't have access to).

1 points
unknown author1 hidden

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.

2 points

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.

1 points

How was this resolved?

1 points

Worked around it with a timer that checks for connectivity and restarts the service.

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.