From f96cf1892a26a1fe98970b6ef4d6c6aeb5f6f58f Mon Sep 17 00:00:00 2001 From: Aleksandar Pesic Date: Mon, 22 Feb 2021 15:50:45 +0100 Subject: [PATCH] Create ifacewatcher --- wgengine/ifacewatcher_other.go | 25 +++++++++++++++++++++++++ wgengine/ifacewatcher_windows.go | 28 ++++++++++++++++++++++++++++ wgengine/userspace.go | 9 +++++++++ 3 files changed, 62 insertions(+) create mode 100644 wgengine/ifacewatcher_other.go create mode 100644 wgengine/ifacewatcher_windows.go diff --git a/wgengine/ifacewatcher_other.go b/wgengine/ifacewatcher_other.go new file mode 100644 index 000000000..0c57d65e1 --- /dev/null +++ b/wgengine/ifacewatcher_other.go @@ -0,0 +1,25 @@ +// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//+build !windows + +package wgengine + +import ( + "tailscale.com/types/logger" +) + +// Dummy implementation that does nothing. +type ifaceWatcher struct { +} + +func initWatcher(logf logger.Logf) (*ifaceWatcher, error) { + return &ifaceWatcher{}, nil +} + +func (iw *ifaceWatcher) setTun(ifc interface{}) { +} + +func (iw *ifaceWatcher) Destroy() { +} diff --git a/wgengine/ifacewatcher_windows.go b/wgengine/ifacewatcher_windows.go new file mode 100644 index 000000000..f69434966 --- /dev/null +++ b/wgengine/ifacewatcher_windows.go @@ -0,0 +1,28 @@ +// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package wgengine + +import ( + "github.com/tailscale/wireguard-go/tun" + "tailscale.com/types/logger" +) + +type ifaceWatcher struct { + logf logger.Logf + iface *tun.NativeTun +} + +func initWatcher(logf logger.Logf) (*ifaceWatcher, error) { + return &ifaceWatcher{logf: logger.WithPrefix(logf, "ifaceWatcher: ")}, nil +} + +func (iw *ifaceWatcher) setTun(ifc interface{}) { + iw.iface = ifc.(*tun.NativeTun) + iw.logf("setTun LUID=%v", iw.iface.LUID()) +} + +func (iw *ifaceWatcher) Destroy() { + iw.logf("Destroy") +} diff --git a/wgengine/userspace.go b/wgengine/userspace.go index 352a490cd..3e0ef50ae 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -179,14 +179,22 @@ func NewUserspaceEngine(logf logger.Logf, tunName string, listenPort uint16) (En logf("Starting userspace wireguard engine with tun device %q", tunName) + iw, err := initWatcher(logf) + if err != nil { + return nil, err + } + tun, err := tun.CreateTUN(tunName, minimalMTU) if err != nil { + iw.Destroy() diagnoseTUNFailure(tunName, logf) logf("CreateTUN: %v", err) return nil, err } logf("CreateTUN ok.") + iw.setTun(tun) + conf := EngineConfig{ Logf: logf, TUN: tun, @@ -196,6 +204,7 @@ func NewUserspaceEngine(logf logger.Logf, tunName string, listenPort uint16) (En e, err := NewUserspaceEngineAdvanced(conf) if err != nil { + iw.Destroy() tun.Close() return nil, err }