aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tunnel/defaultroutemonitor.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-09-28 19:20:49 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-10-01 13:59:42 +0200
commitbba001018f0a8f2fb1c88a6b97adb23690a6512b (patch)
tree9e9d8ad1787e72f9398fdf3b01f003d743178d2e /tunnel/defaultroutemonitor.go
parentelevate: use fallback shellexecute when not EV-signed (diff)
downloadwireguard-windows-bba001018f0a8f2fb1c88a6b97adb23690a6512b.tar.xz
wireguard-windows-bba001018f0a8f2fb1c88a6b97adb23690a6512b.zip
tunnel: windows does not always add/remove routes with up/down interface
On Linux, we're used to routes being added after an interface is up, and routes being removed as a consequence of an interface going down. On Windows, this isn't always the case, at least not from the perspective of the route notifiers. In order to work around this and make a multi-interface model coherent, we search for a new default route not only whenever the routing table changes but also whenever any interface link parameters change, such as up/down. The practical consequence is that now WireGuard connects properly when wifi is disconnected and then reconnected. Reported-by: Nenad Kozul <me@nenadkozul.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tunnel/defaultroutemonitor.go')
-rw-r--r--tunnel/defaultroutemonitor.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/tunnel/defaultroutemonitor.go b/tunnel/defaultroutemonitor.go
index c1722c45..f9c63e56 100644
--- a/tunnel/defaultroutemonitor.go
+++ b/tunnel/defaultroutemonitor.go
@@ -7,6 +7,7 @@ package tunnel
import (
"log"
+ "sync"
"golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/device"
@@ -29,7 +30,6 @@ func bindSocketRoute(family winipcfg.AddressFamily, device *device.Device, ourLU
}
ifrow, err := r[i].InterfaceLUID.Interface()
if err != nil || ifrow.OperStatus != winipcfg.IfOperStatusUp {
- log.Printf("Found default route for interface %d, but not up, so skipping", r[i].InterfaceIndex)
continue
}
if r[i].Metric < lowestMetric {
@@ -53,7 +53,7 @@ func bindSocketRoute(family winipcfg.AddressFamily, device *device.Device, ourLU
return nil
}
-func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, autoMTU bool, tun *tun.NativeTun) (*winipcfg.RouteChangeCallback, error) {
+func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, autoMTU bool, tun *tun.NativeTun) ([]winipcfg.ChangeCallback, error) {
var minMTU uint32
if family == windows.AF_INET {
minMTU = 576
@@ -64,7 +64,10 @@ func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device,
lastLUID := winipcfg.LUID(0)
lastIndex := uint32(0)
lastMTU := uint32(0)
+ mutex := sync.Mutex{}
doIt := func() error {
+ mutex.Lock()
+ defer mutex.Unlock()
err := bindSocketRoute(family, device, ourLUID, &lastLUID, &lastIndex)
if err != nil {
return err
@@ -104,13 +107,22 @@ func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device,
if err != nil {
return nil, err
}
- cb, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
+ cbr, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
if route != nil && route.DestinationPrefix.PrefixLength == 0 {
- _ = doIt()
+ doIt()
}
})
if err != nil {
return nil, err
}
- return cb, nil
+ cbi, err := winipcfg.RegisterInterfaceChangeCallback(func(notificationType winipcfg.MibNotificationType, iface *winipcfg.MibIPInterfaceRow) {
+ if notificationType == winipcfg.MibParameterNotification {
+ doIt()
+ }
+ })
+ if err != nil {
+ cbr.Unregister()
+ return nil, err
+ }
+ return []winipcfg.ChangeCallback{cbr, cbi}, nil
}