From 0fb738760fde1a14836188a690e8b14bf2aab5da Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 28 Mar 2021 19:25:01 -0700 Subject: [PATCH] wgengine: make Tun optional again, default to fake. This makes setup more explicit in prod codepaths, without requiring a bunch of arguments or helpers for tests and userspace mode. Signed-off-by: David Anderson --- cmd/tailscaled/tailscaled.go | 11 ++++------- cmd/tailscaled/tailscaled_windows.go | 3 ++- wgengine/userspace.go | 14 +++++++++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/tailscaled/tailscaled.go b/cmd/tailscaled/tailscaled.go index 924dc8b6d..931072b55 100644 --- a/cmd/tailscaled/tailscaled.go +++ b/cmd/tailscaled/tailscaled.go @@ -29,7 +29,6 @@ import ( "time" "github.com/go-multierror/multierror" - "github.com/tailscale/wireguard-go/tun" "tailscale.com/ipn/ipnserver" "tailscale.com/logpolicy" "tailscale.com/net/socks5" @@ -333,15 +332,13 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, name string) (e wgengine. LinkMonitor: linkMon, } isUserspace = name == "userspace-networking" - var dev tun.Device - if isUserspace { - dev = tstun.NewFake() - } else { - dev, err = tstun.New(logf, name) + if !isUserspace { + dev, err := tstun.New(logf, name) if err != nil { tstun.Diagnose(logf, name) return nil, false, err } + conf.Tun = dev r, err := router.New(logf, dev) if err != nil { dev.Close() @@ -349,7 +346,7 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, name string) (e wgengine. } conf.Router = r } - e, err = wgengine.NewUserspaceEngine(logf, dev, conf) + e, err = wgengine.NewUserspaceEngine(logf, conf) if err != nil { return nil, isUserspace, err } diff --git a/cmd/tailscaled/tailscaled_windows.go b/cmd/tailscaled/tailscaled_windows.go index fef215d74..b57190b7e 100644 --- a/cmd/tailscaled/tailscaled_windows.go +++ b/cmd/tailscaled/tailscaled_windows.go @@ -170,7 +170,8 @@ func startIPNServer(ctx context.Context, logid string) error { dev.Close() return nil, err } - eng, err := wgengine.NewUserspaceEngine(logf, dev, wgengine.Config{ + eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{ + Tun: dev, Router: r, ListenPort: 41641, }) diff --git a/wgengine/userspace.go b/wgengine/userspace.go index 27b87586e..f9bdd9ad3 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -133,6 +133,11 @@ func (e *userspaceEngine) GetInternals() (*tstun.Wrapper, *magicsock.Conn) { // Config is the engine configuration. type Config struct { + // Tun is the device used by the Engine to exchange packets with + // the OS. + // If nil, a fake Device that does nothing is used. + Tun tun.Device + // Router interfaces the Engine to the OS network stack. // If nil, a fake Router that does nothing is used. Router router.Router @@ -152,7 +157,7 @@ type Config struct { func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) { logf("Starting userspace wireguard engine (with fake TUN device)") - return NewUserspaceEngine(logf, tstun.NewFake(), Config{ + return NewUserspaceEngine(logf, Config{ ListenPort: listenPort, Fake: true, }) @@ -160,15 +165,18 @@ func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) // NewUserspaceEngine creates the named tun device and returns a // Tailscale Engine running on it. -func NewUserspaceEngine(logf logger.Logf, dev tun.Device, conf Config) (_ Engine, reterr error) { +func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error) { var closePool closeOnErrorPool defer closePool.closeAllIfError(&reterr) + if conf.Tun == nil { + conf.Tun = tstun.NewFake() + } if conf.Router == nil { conf.Router = router.NewFake(logf) } - tsTUNDev := tstun.Wrap(logf, dev) + tsTUNDev := tstun.Wrap(logf, conf.Tun) closePool.add(tsTUNDev) e := &userspaceEngine{