From 19f70f5f56d2bcb743cad4bb4df943f223dd967f Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 31 May 2019 15:04:52 +0200 Subject: tunnel: make tcp/ip waiting explicit Signed-off-by: Jason A. Donenfeld --- tunnel/defaultroutemonitor.go | 23 +++++------------------ tunnel/ifaceconfig.go | 19 +++++++++++++++++++ tunnel/service.go | 3 +++ 3 files changed, 27 insertions(+), 18 deletions(-) (limited to 'tunnel') diff --git a/tunnel/defaultroutemonitor.go b/tunnel/defaultroutemonitor.go index f14048cd..e1692bc9 100644 --- a/tunnel/defaultroutemonitor.go +++ b/tunnel/defaultroutemonitor.go @@ -7,7 +7,6 @@ package tunnel import ( "log" - "time" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/device" @@ -54,18 +53,6 @@ func bindSocketRoute(family winipcfg.AddressFamily, device *device.Device, ourLU return nil } -func getIPInterfaceRetry(luid winipcfg.LUID, family winipcfg.AddressFamily, retry bool, maxRetries int) (ipi *winipcfg.MibIPInterfaceRow, err error) { - for i := 0; i < maxRetries; i++ { - ipi, err = luid.IPInterface(family) - if retry && i != maxRetries-1 && err == windows.ERROR_NOT_FOUND { - time.Sleep(time.Millisecond * 50) - continue - } - break - } - return -} - func monitorDefaultRoutes(device *device.Device, autoMTU bool, tun *tun.NativeTun) (*winipcfg.RouteChangeCallback, error) { ourLUID := winipcfg.LUID(tun.LUID()) lastLUID4 := winipcfg.LUID(0) @@ -73,7 +60,7 @@ func monitorDefaultRoutes(device *device.Device, autoMTU bool, tun *tun.NativeTu lastIndex4 := uint32(0) lastIndex6 := uint32(0) lastMTU := uint32(0) - doIt := func(retry bool) error { + doIt := func() error { err := bindSocketRoute(windows.AF_INET, device, ourLUID, &lastLUID4, &lastIndex4) if err != nil { return err @@ -105,7 +92,7 @@ func monitorDefaultRoutes(device *device.Device, autoMTU bool, tun *tun.NativeTu } } if mtu > 0 && lastMTU != mtu { - iface, err := getIPInterfaceRetry(ourLUID, windows.AF_INET, retry, 100) + iface, err := ourLUID.IPInterface(windows.AF_INET) if err != nil { return err } @@ -118,7 +105,7 @@ func monitorDefaultRoutes(device *device.Device, autoMTU bool, tun *tun.NativeTu return err } tun.ForceMTU(int(iface.NLMTU)) //TODO: it sort of breaks the model with v6 mtu and v4 mtu being different. Just set v4 one for now. - iface, err = getIPInterfaceRetry(ourLUID, windows.AF_INET6, retry, 3) + iface, err = ourLUID.IPInterface(windows.AF_INET6) if err == nil { // People seem to like to disable IPv6, so we make this non-fatal. iface.NLMTU = mtu - 80 if iface.NLMTU < 1280 { @@ -133,13 +120,13 @@ func monitorDefaultRoutes(device *device.Device, autoMTU bool, tun *tun.NativeTu } return nil } - err := doIt(true) + err := doIt() if err != nil { return nil, err } cb, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) { if route != nil && route.DestinationPrefix.PrefixLength == 0 { - _ = doIt(false) + _ = doIt() } }) if err != nil { diff --git a/tunnel/ifaceconfig.go b/tunnel/ifaceconfig.go index 1ad076fa..3954441b 100644 --- a/tunnel/ifaceconfig.go +++ b/tunnel/ifaceconfig.go @@ -10,6 +10,7 @@ import ( "log" "net" "sort" + "time" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/tun" @@ -221,3 +222,21 @@ func enableFirewall(conf *conf.Config, tun *tun.NativeTun) error { } return firewall.EnableFirewall(tun.LUID(), conf.Interface.DNS, restrictAll) } + +func waitForFamilies(tun *tun.NativeTun) { + //TODO: This whole thing is a disgusting hack that shouldn't be neccessary. + + f := func(luid winipcfg.LUID, family winipcfg.AddressFamily, maxRetries int) { + for i := 0; i < maxRetries; i++ { + _, err := luid.IPInterface(family) + if i != maxRetries-1 && err == windows.ERROR_NOT_FOUND { + time.Sleep(time.Millisecond * 50) + continue + } + break + } + } + luid := winipcfg.LUID(tun.LUID()) + f(luid, windows.AF_INET, 100) + f(luid, windows.AF_INET6, 3) +} \ No newline at end of file diff --git a/tunnel/service.go b/tunnel/service.go index 99bb3497..70e2c8a1 100644 --- a/tunnel/service.go +++ b/tunnel/service.go @@ -183,6 +183,9 @@ func (service *Service) Execute(args []string, r <-chan svc.ChangeRequest, chang log.Println("Bringing peers up") dev.Up() + log.Println("Waiting for TCP/IP to attach to interface") + waitForFamilies(nativeTun) //TODO: move this sort of thing into tun/wintun/CreateInterface + log.Println("Monitoring default routes") routeChangeCallback, err = monitorDefaultRoutes(dev, conf.Interface.MTU == 0, nativeTun) if err != nil { -- cgit v1.2.3-59-g8ed1b