core: Wireguard point-to-site handshake broken due to route not added in routing table

Describe the bug

When routing traffic through OPNsense connecting from LAN side and sending all requests upstream to a VPN provider, there is no connectivity either through IP ping requests or DNS resolution. After checking the Wireguard diagnostics at /ui/wireguard/diagnostics, there is no handshake data being received.

I have had the same point to site setup since the 23 series and it has been working fine through 24.1. Around 24.1.2 I started to notice issues with the Wireguard handshake not being completed. I tried to update to 24.1.3 and 24.1.4 and ended up reverting to 24.1.2 which I believe is where the issue started. I can’t recall, but I don’t believe I was able to revert to 24.1. After fiddling with DNS and the release in 24.1.2 I was able to get some connectivity and stayed on it for awhile while attempting to update to subsequent versions. Yesterday/Today I have updated to 24.1.5 and the hotfix releases, currently on 24.1.5_3.

It seems that there is a route 128.0.0.0/1 on the wireguard interface (e.g. wg0) that is added to the routing table on reboot or restart of the Wireguard service. Deleting this route via /ui/diagnostics/interface/routes or via shell route del -net 128.0.0.0/1 allows the handshake to be completed and traffic to flow to the VPN provider.

To Reproduce

  1. Set up OPNsense router as the client.
  2. Configure the peer as the site to route all traffic to via 0.0.0.0/0.
  3. Enable Wireguard.
  4. Visit /ui/wireguard/diagnostics in the UI and notice the handshake tries to send, but no response is received.
  5. (Optional) Try to access internet. Ssh to router and try to ping VPN endpoint or another public website. No IP connectivity and no DNS resolution.
  6. Visit /ui/diagnostics/interface/routes in the UI and delete route 128.0.0.0/1.
  7. Visit /ui/wireguard/diagnostics and notice that the handshake has now completed with internet connectivity restored.

Expected behavior

Starting or restarting the Wireguard service via the UI (or enabled at boot) with a properly configured point to site setup should complete the handshake and allow routing through the VPN. From my past use and understanding of Wireguard, the default route for all traffic should be 0.0.0.0/0 or as seen in route table of OPNsense 0.0.0.0/1 if one is trying to route all traffic out of the interface. This is true when starting the Wireguard service on OPNsense. However, I don’t believe the route 128.0.0.0/1 should be set as I haven’t seen that before and seems to be the cause of connection issues.

Relevant log files

No visible behavior in log files other than the Wireguard diagnostic screen not showing a completed handshake. The firewall log shows the request received and the outbound NAT rule applied on first connection.

Additional context

I am not an expert on routing, but the upstream urls are all above the 128.0.0.0/1 route as in 193.65.*.* and the like. I am not sure if relevant but am not able to test with IP addresses below 128.0.0.0/1. Not sure if that affects matching behavior as it is more specific that the default route and both routes are bound to the Wireguard interface (e.g. wg0).

Environment

OPNsense 24.1.5_3 (amd64)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 55 (24 by maintainers)

Commits related to this issue

Most upvoted comments

So maybe the last thing here is doing the file write before restart hook. Unfortunately I have to take a flight so I cannot propose another patch for now. Maybe you can try to edit the file. Since the other things don’t influence they can probably be safely used as previously released.

well specifically I’m wondering if this helps: https://github.com/opnsense/changelog/blob/727f3153899c978ea84682ea5db08e3769f58809/community/24.1/24.1.9#L12

As mentioned just prior, I can test this out a bit later and report back.

@fichtner It means that OPNsense tries to do the handshake through the Wireguard interface instead of the WAN interface. Because there is no existing handshake, the Wireguard connection is not valid and you get stuck in a catch-22 where you need a handshake to perform the handshake.

With Wireguard active, if you look at the routing table, you’d probably find that the default route (0.0.0.0/0) is set to the Wireguard interface. That means that if the router receives a packet whose destination IP is not defined in the routing table, it would route the packet to the Wireguard interface. Now, when OPNsense tries to perform a handshake to a remote server, it would use the default route unless the destination IP address is in the routing table (which is what Mitigation 2 does).

Feel free to correct me if I’ve got some things wrong. I’m experiencing the same issue and this is what my intuition tells me is happening.

Who Is Affected This issue is specific for users of Wireguard who are intending to send all traffic through a VPN tunnel from their LAN to an upstream provider. They have configured Allowed IPs to 0.0.0.0/0 or ::/0 on the Edit Peer configuration screen. Below IPv4 is used as the example for simplicity.

The Issue The Wireguard handshake is not completed due to a routing issue. The client cannot connect to the upstream VPN provider due to the order in which the interface is configured. Specifically this commit as part of the 24.1.3 release moved the interface configuration (e.g. ifconfig wg0 up) below the block for adding routes: https://github.com/opnsense/core/commit/dbe52eeaa9c17ec56a22ff6cefcf6b94615bd8b4. This had the unintended effect of trying to configure a tunnel over a tunnel that does not exist yet.

Configuration

Description of the setup:

  1. OPNsense router acts as the Wireguard client
  2. OPNsense outbound NATs all requests through the Wireguard interface to a VPN provider
  3. The router sits on the network edge with WAN connected to internet and LAN as your home network
  4. All devices in your LAN use the OPNsense router as a gateway
  5. The OPNsense router sees that traffic is not specific to the LAN or for the router, so it routes the traffic outside of the LAN

Wireguard Instance Setup:

  1. Tunnel Address configured with <tunnel-ip>/32
  2. Disable Routes is unchecked

Wireguard Peer Setup:

  1. Allowed IPs is 0.0.0.0/0
  2. Endpoint Address non-cidr single IP 1.2.3.4
  3. Endpoint Port 51820
  4. Keepalive Internal 25

Firewall Outbound NAT rules:

  1. Hybrid Outbound NAT rule generation is checked
  2. Firewall outbound NAT rule interface Wireguard (Group)
  3. TCP/IP Version IPv4
  4. Protocol any
  5. Source address LAN net
  6. Source port any
  7. Destination address/port any/any
  8. Translation/target Interface address

Description of the Bug

In the wg_start() function of opnsense/scripts/Wireguard/wg-service-control.php it first checks if we have a Wireguard interface up. Assuming a cold start (or restart), we do not. So the first task is to create the interface and add it to the wireguard group:

if (!does_interface_exist($server->interface)) {
    mwexecf('/sbin/ifconfig wg create name %s', [$server->interface]);
    mwexecf('/sbin/ifconfig %s group wireguard', [$server->interface]);
    $reload = true;
}

Second, it adds the Wireguard configuration from the Web UI to the interface by syncing the conf file:

mwexecf('/usr/bin/wg syncconf %s %s', [$server->interface, $server->cnfFilename]);

Third, it adds the tunnel IP address (tunnel address) and MTU (if set) to the Wireguard interface:

foreach (array_filter(explode(',', (string)$server->tunneladdress)) as $alias) {
    $proto = strpos($alias, ':') === false ? "inet" : "inet6";
    mwexecf('/sbin/ifconfig %s %s %s alias', [$server->interface, $proto, $alias]);
}
if (!empty((string)$server->mtu)) {
    mwexecf('/sbin/ifconfig %s mtu %s', [$server->interface, $server->mtu]);
}

[Bug Introduced] Before commit https://github.com/opnsense/core/commit/dbe52eeaa9c17ec56a22ff6cefcf6b94615bd8b4#diff-42cee99e3d444702e61cfd3b37d6accd73586da661add38c2ad5a04d16c6952cL85 it would bring up the Wireguard interface with:

mwexecf('/sbin/ifconfig %s %s', [$server->interface, $ifcfgflag]);

This immediately made a handshake to the the VPN server. This is no longer the case.


Fourth, wg_start() checks if Disable Routes is set for the Wireguard client (our router). In our case it is not set. So it sets up routes in the routing table according to Allowed IPs of the Wireguard peer. Within this block of code it checks if we intend to send all requests over the VPN:

if (str_ends_with(trim($address), '/0')) {
    if ($ipproto == 'inet') {
	array_push($routes_to_add[$ipproto], '0.0.0.0/1', '128.0.0.0/1');
    } else {
	array_push($routes_to_add[$ipproto], '::/1', '8000::/1');
    }
} elseif {

If so, it splits the full IP routing space into halves so as to not mess with the default route of the routing table. This essentially makes sure all requests go to the VPN. It then adds those routes to the interface:

mwexecf('/sbin/route -q -n add -%s %s -interface %s', [$ipproto, $route, $server->interface]);

At this point we haven’t brought up the Wireguard interface so no handshake has been attempted.

Finally, after the routing table has been configured for our Wireguard interface, the wg interface is brought up:

if ($reload) {
  interfaces_restart_by_device(false, [(string)$server->interface]);
}

mwexecf('/sbin/ifconfig %s %s', [$server->interface, $ifcfgflag]);

At this point, the Wireguard interface attempts to make a handshake to the VPN server, the configured endpoint address of the peer. The endpoint address falls into the previously configured routes of 0.0.0.0/1 and 128.0.0.0/1 to be routed over the Wireguard interface. However, how can we make a handshake over a secure tunnel that we haven’t set up yet to secure that tunnel? We can’t. This is why the handshake is not completing and one will see Send traffic on the VPN status page of the web UI, but no Received traffic. It is attempting to send, but it cannot do so.

Mitigations

Mitigation 1: Revert to the old behavior

Apply this patch when ssh’ed into the OPNsense box:

diff --git a/src/opnsense/scripts/Wireguard/wg-service-control.php b/src/opnsense/scripts/Wireguard/wg-service-control.php
index 7f801b9b4..5ac8c4c75 100755
--- a/src/opnsense/scripts/Wireguard/wg-service-control.php
+++ b/src/opnsense/scripts/Wireguard/wg-service-control.php
@@ -81,6 +81,8 @@ function wg_start($server, $fhandle, $ifcfgflag = 'up', $reload = false)
         mwexecf('/sbin/ifconfig %s mtu %s', [$server->interface, $server->mtu]);
     }

+    mwexecf('/sbin/ifconfig %s %s', [$server->interface, $ifcfgflag]);
+
     if (empty((string)$server->disableroutes)) {
         /**
          * Add routes for all configured peers, wg-quick seems to parse 'wg show wgX allowed-ips' for this,
@@ -138,8 +140,6 @@ function wg_start($server, $fhandle, $ifcfgflag = 'up', $reload = false)
         interfaces_restart_by_device(false, [(string)$server->interface]);
     }

-    mwexecf('/sbin/ifconfig %s %s', [$server->interface, $ifcfgflag]);
-
     // flush checksum to ease change detection
     fseek($fhandle, 0);
     ftruncate($fhandle, 0);

To do so, first copy the patch to your router, then:

# As root

cd /usr/local
patch -p2 < /path/to/patch

This will revert to the previous behavior before this issue was introduced.

[1] However, do note that there may be a small time when the Wireguard interface comes up, yet routes will not be configured so there is a small chance that traffic can leak out of the tunnel before this happens. This is unconfirmed and speculated, but may be a possibility and part of your threat model so it warranted a mention.

Mitigation 2: Add an explicit route to your VPN server endpoint

Add an explicit route for your endpoint VPN server to be routed outside of the tunnel and through the WAN interface. This is recommended by the Wireguard Docs. You can set a route explicitly over ssh as noted in my previous comment on this issue. Or you can navigate to System->Routes->Configuration in the Web UI and add a static route there:

Network Address: <endpoint ip>/32
Gateway: WAN
Description: VPN Handshake Route

After completing one of the mitigations, you can run the latest OPNsense release and will be able to get a full handshake upon starting the Wireguard service.