diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-02-22 02:08:52 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-02-22 15:28:04 +0100 |
commit | d9e89d18d219f8af09cddf7b002729ed6eaeadb6 (patch) | |
tree | aa2709b096fb89d921b5ac15406b8e68c66326b5 /tunnel | |
parent | build: port to arm64 (diff) | |
download | wireguard-windows-d9e89d18d219f8af09cddf7b002729ed6eaeadb6.tar.xz wireguard-windows-d9e89d18d219f8af09cddf7b002729ed6eaeadb6.zip |
tunnel: new bind object handling
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tunnel')
-rw-r--r-- | tunnel/defaultroutemonitor.go | 16 | ||||
-rw-r--r-- | tunnel/interfacewatcher.go | 10 | ||||
-rw-r--r-- | tunnel/service.go | 6 |
3 files changed, 15 insertions, 17 deletions
diff --git a/tunnel/defaultroutemonitor.go b/tunnel/defaultroutemonitor.go index ed1fd201..aa0db675 100644 --- a/tunnel/defaultroutemonitor.go +++ b/tunnel/defaultroutemonitor.go @@ -11,13 +11,13 @@ import ( "time" "golang.org/x/sys/windows" + "golang.zx2c4.com/wireguard/conn" - "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" ) -func bindSocketRoute(family winipcfg.AddressFamily, device *device.Device, ourLUID winipcfg.LUID, lastLUID *winipcfg.LUID, lastIndex *uint32, blackholeWhenLoop bool) error { +func bindSocketRoute(family winipcfg.AddressFamily, binder conn.BindSocketToInterface, ourLUID winipcfg.LUID, lastLUID *winipcfg.LUID, lastIndex *uint32, blackholeWhenLoop bool) error { r, err := winipcfg.GetIPForwardTable2(family) if err != nil { return err @@ -51,21 +51,17 @@ func bindSocketRoute(family winipcfg.AddressFamily, device *device.Device, ourLU *lastLUID = luid *lastIndex = index blackhole := blackholeWhenLoop && index == 0 - bind, _ := device.Bind().(conn.BindSocketToInterface) - if bind == nil { - return nil - } if family == windows.AF_INET { log.Printf("Binding v4 socket to interface %d (blackhole=%v)", index, blackhole) - return bind.BindSocketToInterface4(index, blackhole) + return binder.BindSocketToInterface4(index, blackhole) } else if family == windows.AF_INET6 { log.Printf("Binding v6 socket to interface %d (blackhole=%v)", index, blackhole) - return bind.BindSocketToInterface6(index, blackhole) + return binder.BindSocketToInterface6(index, blackhole) } return nil } -func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, autoMTU bool, blackholeWhenLoop bool, tun *tun.NativeTun) ([]winipcfg.ChangeCallback, error) { +func monitorDefaultRoutes(family winipcfg.AddressFamily, binder conn.BindSocketToInterface, autoMTU bool, blackholeWhenLoop bool, tun *tun.NativeTun) ([]winipcfg.ChangeCallback, error) { var minMTU uint32 if family == windows.AF_INET { minMTU = 576 @@ -77,7 +73,7 @@ func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, lastIndex := ^uint32(0) lastMTU := uint32(0) doIt := func() error { - err := bindSocketRoute(family, device, ourLUID, &lastLUID, &lastIndex, blackholeWhenLoop) + err := bindSocketRoute(family, binder, ourLUID, &lastLUID, &lastIndex, blackholeWhenLoop) if err != nil { return err } diff --git a/tunnel/interfacewatcher.go b/tunnel/interfacewatcher.go index 80406874..e12e5929 100644 --- a/tunnel/interfacewatcher.go +++ b/tunnel/interfacewatcher.go @@ -11,7 +11,7 @@ import ( "golang.org/x/sys/windows" - "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/windows/conf" @@ -31,7 +31,7 @@ type interfaceWatcherEvent struct { type interfaceWatcher struct { errors chan interfaceWatcherError - device *device.Device + binder conn.BindSocketToInterface conf *conf.Config tun *tun.NativeTun @@ -101,7 +101,7 @@ func (iw *interfaceWatcher) setup(family winipcfg.AddressFamily) { var err error log.Printf("Monitoring default %s routes", ipversion) - *changeCallbacks, err = monitorDefaultRoutes(family, iw.device, iw.conf.Interface.MTU == 0, hasDefaultRoute(family, iw.conf.Peers), iw.tun) + *changeCallbacks, err = monitorDefaultRoutes(family, iw.binder, iw.conf.Interface.MTU == 0, hasDefaultRoute(family, iw.conf.Peers), iw.tun) if err != nil { iw.errors <- interfaceWatcherError{services.ErrorBindSocketsToDefaultRoutes, err} return @@ -142,11 +142,11 @@ func watchInterface() (*interfaceWatcher, error) { return iw, nil } -func (iw *interfaceWatcher) Configure(device *device.Device, conf *conf.Config, tun *tun.NativeTun) { +func (iw *interfaceWatcher) Configure(binder conn.BindSocketToInterface, conf *conf.Config, tun *tun.NativeTun) { iw.setupMutex.Lock() defer iw.setupMutex.Unlock() - iw.device, iw.conf, iw.tun = device, conf, tun + iw.binder, iw.conf, iw.tun = binder, conf, tun for _, event := range iw.storedEvents { if event.luid == winipcfg.LUID(iw.tun.LUID()) { iw.setup(event.family) diff --git a/tunnel/service.go b/tunnel/service.go index 36825665..63cd243f 100644 --- a/tunnel/service.go +++ b/tunnel/service.go @@ -17,6 +17,7 @@ import ( "golang.org/x/sys/windows" "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/mgr" + "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" @@ -195,7 +196,8 @@ func (service *tunnelService) Execute(args []string, r <-chan svc.ChangeRequest, } log.Println("Creating interface instance") - dev = device.NewDevice(wintun, &device.Logger{log.Printf, log.Printf}) + bind := conn.NewDefaultBind() + dev = device.NewDevice(wintun, bind, &device.Logger{log.Printf, log.Printf}) log.Println("Setting interface configuration") uapi, err = ipc.UAPIListen(config.Name) @@ -212,7 +214,7 @@ func (service *tunnelService) Execute(args []string, r <-chan svc.ChangeRequest, log.Println("Bringing peers up") dev.Up() - watcher.Configure(dev, config, nativeTun) + watcher.Configure(bind.(conn.BindSocketToInterface), config, nativeTun) log.Println("Listening for UAPI requests") go func() { |