net/wireguard: 'vpn' configure plugin hook missing
Important notices
Before you add a new report, we ask you kindly to acknowledge the following:
- [ x] I have read the contributing guide lines at https://github.com/opnsense/core/blob/master/CONTRIBUTING.md
- [ x] I am convinced that my issue is new after having checked both open and closed issues at https://github.com/opnsense/core/issues?q=is%3Aissue
Describe the bug
Since 23.7 opnsense randomly fails to init wireguard interfaces at boot if DNS lookup fails. I used a host name as Endpoint Address which needs a DNS lookup. It seems that the PPPoE session hasn't started yet, thus DNS query fails, causing the whole wireguard interfaces the Endpoint is attached to being unavailable.
Expected behavior
wireguard interface is brought up even if DNS is used as endpoint address
Describe alternatives you considered
Switching to static IP adress solves the issue for me.
Relevant log files
See attachement fail.log
I know why this happens...spotted it this week doing a review for upcoming wireguard changes.
Wasn't the dnsreresolve action not intended for these kind of issues? https://github.com/opnsense/plugins/blob/dc06d5837f38e80308ec1abb5bceb65422504020/net/wireguard/src/opnsense/service/conf/actions.d/actions_wireguard.conf#L25-L30
ref: https://github.com/WireGuard/wireguard-tools/tree/master/contrib/reresolve-dns
I think this was meant for cron job use to unbreak.
what I mean is adding the vpn hook like OpenVPN and IPsec do on core which is called by newwanip which is called by PPPoE eventually.
To clarify this is pre os-wireguard 2.0. The problem always existed. Flaky DNS during boot is a main driver for this issue. Most people don't even realise that until it happens.
@AdSchellevis looks like you are right... does this work for you then? https://github.com/opnsense/plugins/commit/d6df03a56
Same problem with wireguard tunnel, here. Worked reliably in previous versions. Cron job "Renew DNS for WireGuard on stale connection" does not resolve the issue for me!
/usr/local/opnsense/scripts/Wireguard/wg-service-control.php: The command '/usr/bin/wg setconf 'wg1' '/usr/local/etc/wireguard/wg1.conf'' returned exit code '1', the output was 'Name does not resolve: dyndnsexample.com:51993' Configuration parsing error'`
„Name does not resolve“ is a pretty basic support issue, not a technical problem. Nothing we do here will solve it.
Sorry for opening this up, again. Problem persists with OPNsense 23.7.4
I know that "Name does not resolve" is a basic error, but the endpoint domain name is correct and CAN be resolved. The problem with this is, that the DNS does not resolve upon opnsense startup, but does resolve later on.
- Opnsense boots
- During wireguard startup, DNS resolve for the endpoint fails. Wireguard does not load configuration at all. (with failure above)
- The "/usr/local/opnsense/scripts/Wireguard/reresolve-dns.py" (wireguard renew) action does not fix the issue, since it only sets peer and endpoint, but not pre-shared key, allowed ips etc. -> Wireguard handshake keeps failing
Manual intervention is needed: restarting wireguard service via gui -> DNS now resolves -> tunnel is successfully established.
Possible solution: Reassign config file prior to setting peer in reresolve-dns.py for example with command "wg setconf wg1 /usr/local/etc/wireguard/wg1.conf"
I’m not sure it makes too much sense. It might not be a wireguard issue. It’s trying to restart all the time on dynamic changes. Maybe the root issue is the WAN configuration and DNS setup.
From my point of view, wireguard is started too early upon opnsense bootup.
From the attached excepts of various log files you can see, the following sequence of events:
- LAN interface (igc0) is up
- Wireguard instance wg1 is started and fails due to DNS problem (WAN is not up, yet!)
- WAN interface (igc3) is up
- "Renew DNS for Wireguard" is called, at that time, wg1 config is not loaded, command does not establish tunnel
It seems that the statup sequence does not account for the possibility, WAN is not attached to the first available interface. (It will probably also not account for temporary DNS server failures)
Best regards, Walter
2023-09-16T15:20:38 Notice configd.py [2fd65f11-34be-4916-a523-2ccfceedbd30] Linkup starting igc0
2023-09-16T15:20:38 Notice configd.py [07508d5e-ecbf-4daa-8063-e48ef44be722] configure wireguard instances
2023-09-16T15:20:41 Notice wireguard Wireguard interface WKhomeWireguard (wg1) started
2023-09-16T15:20:41 Error wireguard /usr/local/opnsense/scripts/Wireguard/wg-service-control.php: The command '/usr/bin/wg setconf 'wg1' '/usr/local/etc/wireguard/wg1.conf'' returned exit code '1', the output was 'Name does not resolve: xxxx.mooo.com:51234' Configuration parsing error'
2023-09-16T15:20:46 Notice configd.py [3e84c15b-ae35-487c-9e00-88baaef5b98e] Linkup starting igc3
2023-09-16T15:20:44 Notice configd.py [ebca1ea6-4ce8-4154-a9ee-72e416e8c7ba] Renew DNS for WireGuard`
Linkup starting igc3 is suspicious. The only reason for this is probably netmap being used. I’d rather like to see the system log.
It’s trying to restart all the time on dynamic changes.
Unfortunately it doesn't. If the config is not loaded during initial wg startup, the "wireguard renew" action fails.
System log pulled from /var/logs: see syslog.log General log copied from GUI: see general.log
Thank you for your efforts!
It’s trying to restart all the time on dynamic changes.
It is probably not necessary to review the logs (see above). You're assuming that your plugin hook restarts wireguard on any type of change, but it doesn't. If the config is not loaded during initial wg startup, the "wireguard renew" action fails.
Here is what happens from wireguard side of view:
This is what "wg show all" displays after initial startup with failed DNS request. Interface seems to exist, but with no configuration at all.
interface: wg1
listening port: 25027
This is what happens after your "renew" (what you consider a restart) action (/usr/bin/wg set wg1 peer XXXX endpoint xxx.mooo.com:12345) with successful DNS resolution. Please note: Allowed ips and preshared key are empty/missing.
`interface: wg1 listening port: 25027
peer: XXXX endpoint: 1xx.1xx.2xx.2:12345 allowed ips: (none)`
This is what a successfully established tunnel looks like:
`interface: wg1 public key: XXX private key: (hidden) listening port: 3826
peer: XXX preshared key: (hidden) endpoint: 1xx.1xx.2xx.2:12345 allowed ips: 192.168.26.0/24, 100.102.0.0/15, 161.156.128.32/28, 188.144.0.0/15, 192.168.50.0/24, 192.168.3.0/24, 10.11.0.2/32, 192.168.2.0/24 latest handshake: 19 seconds ago transfer: 527.65 KiB received, 614.14 KiB sent`
Reassigning the config file in reresolve-dns.py (wireguard renew) seems necessary ("wg setconf wg1 /usr/local/etc/wireguard/wg1.conf").
Another (in my oppinion rather bad fix) would be: start wg with dummy static endpoint ip (so that it loads config), then assign correct endpoint IP oder DNS name later (using your renew action).
The reresolve dns script is only intended to resolve an address again as per upstream example.
We might consider calling configure on interface event, but it likely needs some additional checks in the existing script (maybe required for future CARP changes anyway)
The root cause however might be something different as DNS should be available at that point in time and the link event looks rather suspicious (in which case forcing a resolve for a functional tunnel should be all it needs)
I think this can/should be adressed. The problem pops up every now and then, like:
https://forum.opnsense.org/index.php?topic=51381.15 https://forum.opnsense.org/index.php?topic=52240.0
I think the problem is that the script in its current form only sets the peer, but does not create an interface that has not been created on boot.
The logic should cover three cases instead of only two:
- Handshake is current -> do nothing
- Handshake is older than 135 seconds -> set peer again (reresolve DNS)
- NEW: Handshake was never done (i.e. interface does not even exist) -> create interface with peer like on boot, but this time with (hopefully) working DNS.
The basic question is: are these wireguard instances assigned as interfaces or not?
I can test the workflow but I'm doubting the narrative: how can the DNS lookup fail and not produce an interface which is needed to set a peer in the first place?
I think the observed problem often occurs when DNS does not work right after reboot, so the initial WG interface setup fails. In that case the script only tries to set a peer afterwards, but it will always fail, even if the DNS (or the referenced peer) came up in the meantime.
If for some reason the DNS (or the peer) never comes up, then certainly the script cannot not fix that condition and will go on trying forever.
It’s easy to emulate the failure: just boot with a non-resolvable endpoint. The wg interface was there after reboot.
Switch endpoint to resolvable in config.xml and try to re-resolve. I can’t test that without a peer setup.
It actually not that easy for me to try, because I only have one instance, but many peers and also, those peers try to contact me vice-versa.
But that made me think again and I was unclear about one thing: It is not the "interface" missing, because that should exist even if there was no (active) peer. However, the script now only updates the Publickey and the Endpoint, but misses the AllowedIPs, which have no default.
Thus, the third, now uncovered case is that there never was an existing peer including all neccessary settings. In that case, the peer line would be missing in handshakes[] ("None"), and both adding and replacing a peer can easily be done via "wg addconf" (or, maybe "wg syncconf"). AFAIK, "addconf" does not create orphans when it sees existing peers.
So, IMO, the second part from line 58 should become:
if 'Endpoint' in this_peer and 'PublicKey' in this_peer:
peer_key = "%(ifname)s-%(PublicKey)s" % this_peer
last_handshake = handshakes.get(peer_key, None)
# Fall 1: Handshake ist aktuell (< 135 Sek) -> NIX machen
if last_handshake is not None and last_handshake <= 135:
pass
# ZUSAMMENGEFASST (Fall 2 & Fall 3):
# Handshake ist zu alt (> 135) ODER Peer fehlt komplett (None)
else:
# addconf repariert verwaiste Boot-Peers (inkl. AllowedIPs)
# UND aktualisiert IP-Wechsel im laufenden Betrieb,
# OHNE gesunde Nachbar-Peers auf dem Interface zu trennen.
subprocess.run(
[
'/usr/bin/wg',
'addconf',
ifname,
filename
],
capture_output=True,
text=True
)
this_peer = {}
I changed that code on my OpnSense, kept the peer OpnSense from reverse-initiating the connection, such that only my side was to to that and then used "wg show all latest-handshakes" to find the interface and public key for that specific peer.
Then, I tore the peer down via "wg set wg1 peer M4BndVLerI+LgAeQDzzMhRTYDUHw4tne9V7+Lh1AfUE= remove"
Afterwards, "wg show all latest-handshakes" was missing that peer and the IPs on the remote side could no longer be pinged.
Then, I ran "configctl wireguard renew", which made the peer return in "wg show all latest-handshakes" and the ping worked again.
So, this should work and also cover the case that a peer does not exist at all.
So basically what you are saying is that "syncconf" never sets anything when it runs into a DNS error for the endpoint. That would be a very simple but effective design flaw?
Cheers, Franco