From 66ad537f4e2144112cb6686c4fc4856d70bf6a3d Mon Sep 17 00:00:00 2001 From: Philipp Czerner Date: Fri, 27 Mar 2020 18:07:50 +0100 Subject: tunnel: calculate the actual route metric by summing interface and route metric I had some issues setting up WireGuard behind another VPN. Curiously, it bound the physical interface instead of the other VPN, which was the default route. According to MSDN "the actual route metric used to compute the route preference is the summation of interface metric specified in the Metric member of the MIB_IPINTERFACE_ROW structure and the route metric offset specified in this member" (documentation for MIB_IPFORWARD_ROW2), but the code did not seem to consider this. After I changed the calculation, I got the expected behaviour. Signed-off-by: Philipp Czerner Link: https://docs.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_ipforward_row2 [zx2c4: fixed up commit message, removed semicolon] Signed-off-by: Jason A. Donenfeld --- tunnel/defaultroutemonitor.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tunnel/defaultroutemonitor.go b/tunnel/defaultroutemonitor.go index c102b644..68853fd1 100644 --- a/tunnel/defaultroutemonitor.go +++ b/tunnel/defaultroutemonitor.go @@ -33,8 +33,14 @@ func bindSocketRoute(family winipcfg.AddressFamily, device *device.Device, ourLU if err != nil || ifrow.OperStatus != winipcfg.IfOperStatusUp { continue } - if r[i].Metric < lowestMetric { - lowestMetric = r[i].Metric + + iface, err := r[i].InterfaceLUID.IPInterface(family) + if err != nil { + continue + } + + if r[i].Metric + iface.Metric < lowestMetric { + lowestMetric = r[i].Metric + iface.Metric index = r[i].InterfaceIndex luid = r[i].InterfaceLUID } -- cgit v1.2.3-59-g8ed1b