I am running wireguard client on Ubuntu 22.04 from a Lenovo laptop using a WiFi connection, to connect to my home network. The configuration leads all traffic through my home network and it all works well. I recently changed the configuration slightly to include some custom routes to go directly to select sites - e.g. facebook.com (157.240.0.0/16) - rather than through my home network. To this end I did the following:
1. Added the following line to /etc/iproute2/rt_tables to create new custom routing table
>200 custom
2. I added "PostUp and "PreDown" directives to my wireguad configuration file:
PostUp = ip route add 157.240.0.0/16 via `ip route | grep default | cut -d ' ' -f 3,3` table custom
PreDown = ip route delete 157.240.0.0/16 via `ip route | grep default | cut -d ' ' -f 3,3` table custom
Now this all worked fine and the routing tables looked as expected:
ip rule show
0: from all lookup local
32763: from all lookup custom
32764: from all lookup main suppress_prefixlength 0
32765: not from all fwmark 0xca6c lookup 51820
32766: from all lookup main
32767: from all lookup default
ip route show table custom
157.240.0.0/16 via 192.168.1.254 dev wlp1s0
ip route show table 51820
default dev wghome scope link
However, if I close the lid on the laptop, it has been configured to go into suspend mode to save power. But when I open the lid and log in again, the wireguard interface comes up allright but the custom route table is now empty.
And when I try to do a "wg-quick down wg0" it will fail to close down the wg0 interface because the predown directive fails due to the missing route it wants to delete. And I have to do some manual shutdowns using ip link statements etc. and the custom DNS configuration that directs to my home router remains unchanged, causing name resolution to fail.
I have changed the NetworkManager default configuration to prevent it from handling wireguard interfaces since it cannot handle PreUp and PreDown directives and I have also turned off NetworkManager powersave for the wifi interface. But it has no effects on the problem.
Now it is not the greatest problem in the world since I can simply live without the custom rule and let all traffic go through my home network, but that will sometimes add some long roundtrip times due to the longer network path, depending on where I am presently located in the world. So if anyone have an idea I would be happy.
I suspect I need to have some Up/Down logic tied to the wghome interface going up and down during suspend or maybe the wifi interface (or both) ?