From 2f1f0d602f4191e3f01554fd8dc229489884a4ae Mon Sep 17 00:00:00 2001 From: Nate Brown Date: Fri, 12 Nov 2021 10:47:36 -0600 Subject: [PATCH] Cleanup most of the remaining nits (#578) --- connection_manager_test.go | 7 +-- control_tester.go | 6 +- overlay/tun_android.go | 27 ++++----- overlay/tun_darwin.go | 52 +++++++--------- overlay/tun_freebsd.go | 54 ++++++++--------- overlay/tun_ios.go | 67 +++++++++++---------- overlay/tun_linux.go | 108 ++++++++++++++-------------------- overlay/tun_linux_test.go | 14 ++--- overlay/tun_tester.go | 48 +++++++-------- overlay/tun_water_windows.go | 52 ++++++++-------- overlay/tun_windows.go | 34 ++++------- overlay/tun_wintun_windows.go | 54 ++++++++--------- test/tun.go | 47 +++++++++++++++ 13 files changed, 285 insertions(+), 285 deletions(-) create mode 100644 test/tun.go diff --git a/connection_manager_test.go b/connection_manager_test.go index 603a236..80a0178 100644 --- a/connection_manager_test.go +++ b/connection_manager_test.go @@ -11,7 +11,6 @@ import ( "github.com/flynn/noise" "github.com/slackhq/nebula/cert" "github.com/slackhq/nebula/iputil" - "github.com/slackhq/nebula/overlay" "github.com/slackhq/nebula/test" "github.com/slackhq/nebula/udp" "github.com/stretchr/testify/assert" @@ -39,7 +38,7 @@ func Test_NewConnectionManagerTest(t *testing.T) { lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false) ifce := &Interface{ hostMap: hostMap, - inside: &overlay.Tun{}, + inside: &test.NoopTun{}, outside: &udp.Conn{}, certState: cs, firewall: &Firewall{}, @@ -108,7 +107,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) { lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false) ifce := &Interface{ hostMap: hostMap, - inside: &overlay.Tun{}, + inside: &test.NoopTun{}, outside: &udp.Conn{}, certState: cs, firewall: &Firewall{}, @@ -217,7 +216,7 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) { lh := NewLightHouse(l, false, &net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPMask{0, 0, 0, 0}}, []iputil.VpnIp{}, 1000, 0, &udp.Conn{}, false, 1, false) ifce := &Interface{ hostMap: hostMap, - inside: &overlay.Tun{}, + inside: &test.NoopTun{}, outside: &udp.Conn{}, certState: cs, firewall: &Firewall{}, diff --git a/control_tester.go b/control_tester.go index c9a9161..a02eadc 100644 --- a/control_tester.go +++ b/control_tester.go @@ -65,7 +65,7 @@ func (c *Control) InjectLightHouseAddr(vpnIp net.IP, toAddr *net.UDPAddr) { // GetFromTun will pull a packet off the tun side of nebula func (c *Control) GetFromTun(block bool) []byte { - return c.f.inside.(*overlay.Tun).Get(block) + return c.f.inside.(*overlay.TestTun).Get(block) } // GetFromUDP will pull a udp packet off the udp side of nebula @@ -78,7 +78,7 @@ func (c *Control) GetUDPTxChan() <-chan *udp.Packet { } func (c *Control) GetTunTxChan() <-chan []byte { - return c.f.inside.(*overlay.Tun).TxPackets + return c.f.inside.(*overlay.TestTun).TxPackets } // InjectUDPPacket will inject a packet into the udp side of nebula @@ -115,7 +115,7 @@ func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16 panic(err) } - c.f.inside.(*overlay.Tun).Send(buffer.Bytes()) + c.f.inside.(*overlay.TestTun).Send(buffer.Bytes()) } func (c *Control) GetUDPAddr() string { diff --git a/overlay/tun_android.go b/overlay/tun_android.go index d94a0a8..ff37487 100644 --- a/overlay/tun_android.go +++ b/overlay/tun_android.go @@ -13,7 +13,7 @@ import ( "golang.org/x/sys/unix" ) -type Tun struct { +type tun struct { io.ReadWriteCloser fd int Device string @@ -26,10 +26,10 @@ type Tun struct { l *logrus.Logger } -func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) { +func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (*tun, error) { file := os.NewFile(uintptr(deviceFd), "/dev/net/tun") - ifce = &Tun{ + return &tun{ ReadWriteCloser: file, fd: int(file.Fd()), Device: "android", @@ -39,19 +39,18 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU in Routes: routes, UnsafeRoutes: unsafeRoutes, l: l, - } - return + }, nil } -func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) { +func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int, _ bool) (*tun, error) { return nil, fmt.Errorf("newTun not supported in Android") } -func (c *Tun) WriteRaw(b []byte) error { +func (t *tun) WriteRaw(b []byte) error { var nn int for { max := len(b) - n, err := unix.Write(c.fd, b[nn:max]) + n, err := unix.Write(t.fd, b[nn:max]) if n > 0 { nn += n } @@ -69,18 +68,18 @@ func (c *Tun) WriteRaw(b []byte) error { } } -func (c Tun) Activate() error { +func (t tun) Activate() error { return nil } -func (c *Tun) CidrNet() *net.IPNet { - return c.Cidr +func (t *tun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *Tun) DeviceName() string { - return c.Device +func (t *tun) DeviceName() string { + return t.Device } -func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for android") } diff --git a/overlay/tun_darwin.go b/overlay/tun_darwin.go index 526b302..51b8048 100644 --- a/overlay/tun_darwin.go +++ b/overlay/tun_darwin.go @@ -16,7 +16,7 @@ import ( "golang.org/x/sys/unix" ) -type Tun struct { +type tun struct { io.ReadWriteCloser Device string Cidr *net.IPNet @@ -74,13 +74,7 @@ type ifreqMTU struct { pad [8]byte } -type ifreqQLEN struct { - Name [16]byte - Value int32 - pad [8]byte -} - -func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) { +func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, _ bool) (*tun, error) { if len(routes) > 0 { return nil, fmt.Errorf("route MTU not supported in Darwin") } @@ -106,7 +100,7 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout ctlName [96]byte }{} - copy(ctlInfo.ctlName[:], []byte(utunControlName)) + copy(ctlInfo.ctlName[:], utunControlName) err = ioctl(uintptr(fd), uintptr(_CTLIOCGINFO), uintptr(unsafe.Pointer(ctlInfo))) if err != nil { @@ -125,7 +119,7 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout unix.SYS_CONNECT, uintptr(fd), uintptr(unsafe.Pointer(&sc)), - uintptr(sockaddrCtlSize), + sockaddrCtlSize, ) if errno != 0 { return nil, fmt.Errorf("SYS_CONNECT: %v", errno) @@ -152,7 +146,7 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout file := os.NewFile(uintptr(fd), "") - tun := &Tun{ + tun := &tun{ ReadWriteCloser: file, Device: name, Cidr: cidr, @@ -165,25 +159,25 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout return tun, nil } -func (t *Tun) deviceBytes() (o [16]byte) { +func (t *tun) deviceBytes() (o [16]byte) { for i, c := range t.Device { o[i] = byte(c) } return } -func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) { +func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*tun, error) { return nil, fmt.Errorf("newTunFromFd not supported in Darwin") } -func (c *Tun) Close() error { - if c.ReadWriteCloser != nil { - return c.ReadWriteCloser.Close() +func (t *tun) Close() error { + if t.ReadWriteCloser != nil { + return t.ReadWriteCloser.Close() } return nil } -func (t *Tun) Activate() error { +func (t *tun) Activate() error { devName := t.deviceBytes() var addr, mask [4]byte @@ -231,7 +225,7 @@ func (t *Tun) Activate() error { // Set the MTU on the device ifm := ifreqMTU{Name: devName, MTU: int32(t.DefaultMTU)} if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil { - return fmt.Errorf("Failed to set tun mtu: %v", err) + return fmt.Errorf("failed to set tun mtu: %v", err) } /* @@ -353,9 +347,7 @@ func addRoute(sock int, addr, mask *netroute.Inet4Addr, link *netroute.LinkAddr) return nil } -var _ io.ReadWriteCloser = (*Tun)(nil) - -func (t *Tun) Read(to []byte) (int, error) { +func (t *tun) Read(to []byte) (int, error) { buf := make([]byte, len(to)+4) @@ -366,7 +358,7 @@ func (t *Tun) Read(to []byte) (int, error) { } // Write is only valid for single threaded use -func (t *Tun) Write(from []byte) (int, error) { +func (t *tun) Write(from []byte) (int, error) { buf := t.out if cap(buf) < len(from)+4 { buf = make([]byte, len(from)+4) @@ -385,7 +377,7 @@ func (t *Tun) Write(from []byte) (int, error) { } else if ipVer == 6 { buf[3] = syscall.AF_INET6 } else { - return 0, fmt.Errorf("Unable to determine IP version from packet") + return 0, fmt.Errorf("unable to determine IP version from packet") } copy(buf[4:], from) @@ -394,19 +386,19 @@ func (t *Tun) Write(from []byte) (int, error) { return n - 4, err } -func (c *Tun) CidrNet() *net.IPNet { - return c.Cidr +func (t *tun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *Tun) DeviceName() string { - return c.Device +func (t *tun) DeviceName() string { + return t.Device } -func (c *Tun) WriteRaw(b []byte) error { - _, err := c.Write(b) +func (t *tun) WriteRaw(b []byte) error { + _, err := t.Write(b) return err } -func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin") } diff --git a/overlay/tun_freebsd.go b/overlay/tun_freebsd.go index d5b5b3e..7e717a0 100644 --- a/overlay/tun_freebsd.go +++ b/overlay/tun_freebsd.go @@ -18,7 +18,7 @@ import ( var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`) -type Tun struct { +type tun struct { Device string Cidr *net.IPNet MTU int @@ -28,20 +28,20 @@ type Tun struct { io.ReadWriteCloser } -func (c *Tun) Close() error { - if c.ReadWriteCloser != nil { - return c.ReadWriteCloser.Close() +func (t *tun) Close() error { + if t.ReadWriteCloser != nil { + return t.ReadWriteCloser.Close() } return nil } -func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) { +func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*tun, error) { return nil, fmt.Errorf("newTunFromFd not supported in FreeBSD") } -func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) { +func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, _ int, _ bool) (*tun, error) { if len(routes) > 0 { - return nil, fmt.Errorf("Route MTU not supported in FreeBSD") + return nil, fmt.Errorf("route MTU not supported in FreeBSD") } if strings.HasPrefix(deviceName, "/dev/") { deviceName = strings.TrimPrefix(deviceName, "/dev/") @@ -49,7 +49,7 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int if !deviceNameRE.MatchString(deviceName) { return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`") } - return &Tun{ + return &tun{ Device: deviceName, Cidr: cidr, MTU: defaultMTU, @@ -58,30 +58,30 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int }, nil } -func (c *Tun) Activate() error { +func (t *tun) Activate() error { var err error - c.ReadWriteCloser, err = os.OpenFile("/dev/"+c.Device, os.O_RDWR, 0) + t.ReadWriteCloser, err = os.OpenFile("/dev/"+t.Device, os.O_RDWR, 0) if err != nil { - return fmt.Errorf("Activate failed: %v", err) + return fmt.Errorf("activate failed: %v", err) } // TODO use syscalls instead of exec.Command - c.l.Debug("command: ifconfig", c.Device, c.Cidr.String(), c.Cidr.IP.String()) - if err = exec.Command("/sbin/ifconfig", c.Device, c.Cidr.String(), c.Cidr.IP.String()).Run(); err != nil { + t.l.Debug("command: ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String()) + if err = exec.Command("/sbin/ifconfig", t.Device, t.Cidr.String(), t.Cidr.IP.String()).Run(); err != nil { return fmt.Errorf("failed to run 'ifconfig': %s", err) } - c.l.Debug("command: route", "-n", "add", "-net", c.Cidr.String(), "-interface", c.Device) - if err = exec.Command("/sbin/route", "-n", "add", "-net", c.Cidr.String(), "-interface", c.Device).Run(); err != nil { + t.l.Debug("command: route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device) + if err = exec.Command("/sbin/route", "-n", "add", "-net", t.Cidr.String(), "-interface", t.Device).Run(); err != nil { return fmt.Errorf("failed to run 'route add': %s", err) } - c.l.Debug("command: ifconfig", c.Device, "mtu", strconv.Itoa(c.MTU)) - if err = exec.Command("/sbin/ifconfig", c.Device, "mtu", strconv.Itoa(c.MTU)).Run(); err != nil { + t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)) + if err = exec.Command("/sbin/ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)).Run(); err != nil { return fmt.Errorf("failed to run 'ifconfig': %s", err) } // Unsafe path routes - for _, r := range c.UnsafeRoutes { - c.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", c.Device) - if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", c.Device).Run(); err != nil { + for _, r := range t.UnsafeRoutes { + t.l.Debug("command: route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device) + if err = exec.Command("/sbin/route", "-n", "add", "-net", r.Cidr.String(), "-interface", t.Device).Run(); err != nil { return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.Cidr.String(), err) } } @@ -89,19 +89,19 @@ func (c *Tun) Activate() error { return nil } -func (c *Tun) CidrNet() *net.IPNet { - return c.Cidr +func (t *tun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *Tun) DeviceName() string { - return c.Device +func (t *tun) DeviceName() string { + return t.Device } -func (c *Tun) WriteRaw(b []byte) error { - _, err := c.Write(b) +func (t *tun) WriteRaw(b []byte) error { + _, err := t.Write(b) return err } -func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for freebsd") } diff --git a/overlay/tun_ios.go b/overlay/tun_ios.go index 4dd448d..8bc8d3c 100644 --- a/overlay/tun_ios.go +++ b/overlay/tun_ios.go @@ -15,36 +15,35 @@ import ( "github.com/sirupsen/logrus" ) -type Tun struct { +type tun struct { io.ReadWriteCloser Device string Cidr *net.IPNet } -func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) { +func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int, _ bool) (*tun, error) { return nil, fmt.Errorf("newTun not supported in iOS") } -func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) { +func newTunFromFd(_ *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes []Route, _ []Route, _ int) (*tun, error) { if len(routes) > 0 { return nil, fmt.Errorf("route MTU not supported in Darwin") } file := os.NewFile(uintptr(deviceFd), "/dev/tun") - ifce = &Tun{ + return &tun{ Cidr: cidr, Device: "iOS", ReadWriteCloser: &tunReadCloser{f: file}, - } - return + }, nil } -func (c *Tun) Activate() error { +func (t *tun) Activate() error { return nil } -func (c *Tun) WriteRaw(b []byte) error { - _, err := c.Write(b) +func (t *tun) WriteRaw(b []byte) error { + _, err := t.Write(b) return err } @@ -59,62 +58,62 @@ type tunReadCloser struct { wBuf []byte } -func (t *tunReadCloser) Read(to []byte) (int, error) { - t.rMu.Lock() - defer t.rMu.Unlock() +func (tr *tunReadCloser) Read(to []byte) (int, error) { + tr.rMu.Lock() + defer tr.rMu.Unlock() - if cap(t.rBuf) < len(to)+4 { - t.rBuf = make([]byte, len(to)+4) + if cap(tr.rBuf) < len(to)+4 { + tr.rBuf = make([]byte, len(to)+4) } - t.rBuf = t.rBuf[:len(to)+4] + tr.rBuf = tr.rBuf[:len(to)+4] - n, err := t.f.Read(t.rBuf) - copy(to, t.rBuf[4:]) + n, err := tr.f.Read(tr.rBuf) + copy(to, tr.rBuf[4:]) return n - 4, err } -func (t *tunReadCloser) Write(from []byte) (int, error) { +func (tr *tunReadCloser) Write(from []byte) (int, error) { if len(from) == 0 { return 0, syscall.EIO } - t.wMu.Lock() - defer t.wMu.Unlock() + tr.wMu.Lock() + defer tr.wMu.Unlock() - if cap(t.wBuf) < len(from)+4 { - t.wBuf = make([]byte, len(from)+4) + if cap(tr.wBuf) < len(from)+4 { + tr.wBuf = make([]byte, len(from)+4) } - t.wBuf = t.wBuf[:len(from)+4] + tr.wBuf = tr.wBuf[:len(from)+4] // Determine the IP Family for the NULL L2 Header ipVer := from[0] >> 4 if ipVer == 4 { - t.wBuf[3] = syscall.AF_INET + tr.wBuf[3] = syscall.AF_INET } else if ipVer == 6 { - t.wBuf[3] = syscall.AF_INET6 + tr.wBuf[3] = syscall.AF_INET6 } else { return 0, errors.New("unable to determine IP version from packet") } - copy(t.wBuf[4:], from) + copy(tr.wBuf[4:], from) - n, err := t.f.Write(t.wBuf) + n, err := tr.f.Write(tr.wBuf) return n - 4, err } -func (t *tunReadCloser) Close() error { - return t.f.Close() +func (tr *tunReadCloser) Close() error { + return tr.f.Close() } -func (c *Tun) CidrNet() *net.IPNet { - return c.Cidr +func (t *tun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *Tun) DeviceName() string { - return c.Device +func (t *tun) DeviceName() string { + return t.Device } -func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for ios") } diff --git a/overlay/tun_linux.go b/overlay/tun_linux.go index 1403381..f5c29b3 100644 --- a/overlay/tun_linux.go +++ b/overlay/tun_linux.go @@ -16,7 +16,7 @@ import ( "golang.org/x/sys/unix" ) -type Tun struct { +type tun struct { io.ReadWriteCloser fd int Device string @@ -43,26 +43,6 @@ func ioctl(a1, a2, a3 uintptr) error { return nil } -/* -func ipv4(addr string) (o [4]byte, err error) { - ip := net.ParseIP(addr).To4() - if ip == nil { - err = fmt.Errorf("failed to parse addr %s", addr) - return - } - for i, b := range ip { - o[i] = b - } - return -} -*/ - -const ( - cIFF_TUN = 0x0001 - cIFF_NO_PI = 0x1000 - cIFF_MULTI_QUEUE = 0x0100 -) - type ifreqAddr struct { Name [16]byte Addr unix.RawSockaddrInet4 @@ -81,11 +61,11 @@ type ifreqQLEN struct { pad [8]byte } -func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) { +func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (*tun, error) { file := os.NewFile(uintptr(deviceFd), "/dev/net/tun") - ifce = &Tun{ + return &tun{ ReadWriteCloser: file, fd: int(file.Fd()), Device: "tun0", @@ -95,20 +75,19 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU in Routes: routes, UnsafeRoutes: unsafeRoutes, l: l, - } - return + }, nil } -func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) { +func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (*tun, error) { fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0) if err != nil { return nil, err } var req ifReq - req.Flags = uint16(cIFF_TUN | cIFF_NO_PI) + req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI) if multiqueue { - req.Flags |= cIFF_MULTI_QUEUE + req.Flags |= unix.IFF_MULTI_QUEUE } copy(req.Name[:], deviceName) if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil { @@ -125,7 +104,7 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int } } - ifce = &Tun{ + return &tun{ ReadWriteCloser: file, fd: int(file.Fd()), Device: name, @@ -136,19 +115,18 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int Routes: routes, UnsafeRoutes: unsafeRoutes, l: l, - } - return + }, nil } -func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0) if err != nil { return nil, err } var req ifReq - req.Flags = uint16(cIFF_TUN | cIFF_NO_PI | cIFF_MULTI_QUEUE) - copy(req.Name[:], c.Device) + req.Flags = uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE) + copy(req.Name[:], t.Device) if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil { return nil, err } @@ -158,11 +136,11 @@ func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return file, nil } -func (c *Tun) WriteRaw(b []byte) error { +func (t *tun) WriteRaw(b []byte) error { var nn int for { max := len(b) - n, err := unix.Write(c.fd, b[nn:max]) + n, err := unix.Write(t.fd, b[nn:max]) if n > 0 { nn += n } @@ -180,24 +158,24 @@ func (c *Tun) WriteRaw(b []byte) error { } } -func (c *Tun) Write(b []byte) (int, error) { - return len(b), c.WriteRaw(b) +func (t *tun) Write(b []byte) (int, error) { + return len(b), t.WriteRaw(b) } -func (c Tun) deviceBytes() (o [16]byte) { - for i, c := range c.Device { +func (t tun) deviceBytes() (o [16]byte) { + for i, c := range t.Device { o[i] = byte(c) } return } -func (c Tun) Activate() error { - devName := c.deviceBytes() +func (t tun) Activate() error { + devName := t.deviceBytes() var addr, mask [4]byte - copy(addr[:], c.Cidr.IP.To4()) - copy(mask[:], c.Cidr.Mask) + copy(addr[:], t.Cidr.IP.To4()) + copy(mask[:], t.Cidr.Mask) s, err := unix.Socket( unix.AF_INET, @@ -235,17 +213,17 @@ func (c Tun) Activate() error { } // Set the MTU on the device - ifm := ifreqMTU{Name: devName, MTU: int32(c.MaxMTU)} + ifm := ifreqMTU{Name: devName, MTU: int32(t.MaxMTU)} if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil { // This is currently a non fatal condition because the route table must have the MTU set appropriately as well - c.l.WithError(err).Error("Failed to set tun mtu") + t.l.WithError(err).Error("Failed to set tun mtu") } // Set the transmit queue length - ifrq := ifreqQLEN{Name: devName, Value: int32(c.TXQueueLen)} + ifrq := ifreqQLEN{Name: devName, Value: int32(t.TXQueueLen)} if err = ioctl(fd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil { // If we can't set the queue length nebula will still work but it may lead to packet loss - c.l.WithError(err).Error("Failed to set tun tx queue length") + t.l.WithError(err).Error("Failed to set tun tx queue length") } // Bring up the interface @@ -255,36 +233,36 @@ func (c Tun) Activate() error { } // Set the routes - link, err := netlink.LinkByName(c.Device) + link, err := netlink.LinkByName(t.Device) if err != nil { return fmt.Errorf("failed to get tun device link: %s", err) } // Default route - dr := &net.IPNet{IP: c.Cidr.IP.Mask(c.Cidr.Mask), Mask: c.Cidr.Mask} + dr := &net.IPNet{IP: t.Cidr.IP.Mask(t.Cidr.Mask), Mask: t.Cidr.Mask} nr := netlink.Route{ LinkIndex: link.Attrs().Index, Dst: dr, - MTU: c.DefaultMTU, - AdvMSS: c.advMSS(Route{}), + MTU: t.DefaultMTU, + AdvMSS: t.advMSS(Route{}), Scope: unix.RT_SCOPE_LINK, - Src: c.Cidr.IP, + Src: t.Cidr.IP, Protocol: unix.RTPROT_KERNEL, Table: unix.RT_TABLE_MAIN, Type: unix.RTN_UNICAST, } err = netlink.RouteReplace(&nr) if err != nil { - return fmt.Errorf("failed to set mtu %v on the default route %v; %v", c.DefaultMTU, dr, err) + return fmt.Errorf("failed to set mtu %v on the default route %v; %v", t.DefaultMTU, dr, err) } // Path routes - for _, r := range c.Routes { + for _, r := range t.Routes { nr := netlink.Route{ LinkIndex: link.Attrs().Index, Dst: r.Cidr, MTU: r.MTU, - AdvMSS: c.advMSS(r), + AdvMSS: t.advMSS(r), Scope: unix.RT_SCOPE_LINK, } @@ -295,13 +273,13 @@ func (c Tun) Activate() error { } // Unsafe path routes - for _, r := range c.UnsafeRoutes { + for _, r := range t.UnsafeRoutes { nr := netlink.Route{ LinkIndex: link.Attrs().Index, Dst: r.Cidr, MTU: r.MTU, Priority: r.Metric, - AdvMSS: c.advMSS(r), + AdvMSS: t.advMSS(r), Scope: unix.RT_SCOPE_LINK, } @@ -320,22 +298,22 @@ func (c Tun) Activate() error { return nil } -func (c *Tun) CidrNet() *net.IPNet { - return c.Cidr +func (t *tun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *Tun) DeviceName() string { - return c.Device +func (t *tun) DeviceName() string { + return t.Device } -func (c Tun) advMSS(r Route) int { +func (t tun) advMSS(r Route) int { mtu := r.MTU if r.MTU == 0 { - mtu = c.DefaultMTU + mtu = t.DefaultMTU } // We only need to set advmss if the route MTU does not match the device MTU - if mtu != c.MaxMTU { + if mtu != t.MaxMTU { return mtu - 40 } return 0 diff --git a/overlay/tun_linux_test.go b/overlay/tun_linux_test.go index 1951a0e..6c2043d 100644 --- a/overlay/tun_linux_test.go +++ b/overlay/tun_linux_test.go @@ -7,19 +7,19 @@ import "testing" var runAdvMSSTests = []struct { name string - tun Tun + tun tun r Route expected int }{ // Standard case, default MTU is the device max MTU - {"default", Tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0}, - {"default-min", Tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0}, - {"default-low", Tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160}, + {"default", tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0}, + {"default-min", tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0}, + {"default-low", tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160}, // Case where we have a route MTU set higher than the default - {"route", Tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400}, - {"route-min", Tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400}, - {"route-high", Tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0}, + {"route", tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400}, + {"route-min", tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400}, + {"route-high", tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0}, } func TestTunAdvMSS(t *testing.T) { diff --git a/overlay/tun_tester.go b/overlay/tun_tester.go index 4d333b0..014d256 100644 --- a/overlay/tun_tester.go +++ b/overlay/tun_tester.go @@ -11,7 +11,7 @@ import ( "github.com/sirupsen/logrus" ) -type Tun struct { +type TestTun struct { Device string Cidr *net.IPNet MTU int @@ -22,8 +22,8 @@ type Tun struct { TxPackets chan []byte // Packets transmitted outside by nebula } -func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, _ []Route, unsafeRoutes []Route, _ int, _ bool) (ifce *Tun, err error) { - return &Tun{ +func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, _ []Route, unsafeRoutes []Route, _ int, _ bool) (*TestTun, error) { + return &TestTun{ Device: deviceName, Cidr: cidr, MTU: defaultMTU, @@ -34,28 +34,28 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int }, nil } -func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (ifce *Tun, err error) { +func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (*TestTun, error) { return nil, fmt.Errorf("newTunFromFd not supported") } // Send will place a byte array onto the receive queue for nebula to consume // These are unencrypted ip layer frames destined for another nebula node. // packets should exit the udp side, capture them with udpConn.Get -func (c *Tun) Send(packet []byte) { - c.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet") - c.rxPackets <- packet +func (t *TestTun) Send(packet []byte) { + t.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet") + t.rxPackets <- packet } // Get will pull an unencrypted ip layer frame from the transmit queue // nebula meant to send this message to some application on the local system // packets were ingested from the udp side, you can send them with udpConn.Send -func (c *Tun) Get(block bool) []byte { +func (t *TestTun) Get(block bool) []byte { if block { - return <-c.TxPackets + return <-t.TxPackets } select { - case p := <-c.TxPackets: + case p := <-t.TxPackets: return p default: return nil @@ -66,40 +66,40 @@ func (c *Tun) Get(block bool) []byte { // Below this is boilerplate implementation to make nebula actually work //********************************************************************************************************************// -func (c *Tun) Activate() error { +func (t *TestTun) Activate() error { return nil } -func (c *Tun) CidrNet() *net.IPNet { - return c.Cidr +func (t *TestTun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *Tun) DeviceName() string { - return c.Device +func (t *TestTun) DeviceName() string { + return t.Device } -func (c *Tun) Write(b []byte) (n int, err error) { - return len(b), c.WriteRaw(b) +func (t *TestTun) Write(b []byte) (n int, err error) { + return len(b), t.WriteRaw(b) } -func (c *Tun) Close() error { - close(c.rxPackets) +func (t *TestTun) Close() error { + close(t.rxPackets) return nil } -func (c *Tun) WriteRaw(b []byte) error { +func (t *TestTun) WriteRaw(b []byte) error { packet := make([]byte, len(b), len(b)) copy(packet, b) - c.TxPackets <- packet + t.TxPackets <- packet return nil } -func (c *Tun) Read(b []byte) (int, error) { - p := <-c.rxPackets +func (t *TestTun) Read(b []byte) (int, error) { + p := <-t.rxPackets copy(b, p) return len(p), nil } -func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *TestTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented") } diff --git a/overlay/tun_water_windows.go b/overlay/tun_water_windows.go index 99a82c0..d3835a0 100644 --- a/overlay/tun_water_windows.go +++ b/overlay/tun_water_windows.go @@ -10,7 +10,7 @@ import ( "github.com/songgao/water" ) -type WindowsWaterTun struct { +type waterTun struct { Device string Cidr *net.IPNet MTU int @@ -19,37 +19,37 @@ type WindowsWaterTun struct { *water.Interface } -func newWindowsWaterTun(deviceName string, cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route, txQueueLen int) (ifce *WindowsWaterTun, err error) { +func newWaterTun(cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route) (*waterTun, error) { // NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate() - return &WindowsWaterTun{ + return &waterTun{ Cidr: cidr, MTU: defaultMTU, UnsafeRoutes: unsafeRoutes, }, nil } -func (c *WindowsWaterTun) Activate() error { +func (t *waterTun) Activate() error { var err error - c.Interface, err = water.New(water.Config{ + t.Interface, err = water.New(water.Config{ DeviceType: water.TUN, PlatformSpecificParams: water.PlatformSpecificParams{ ComponentID: "tap0901", - Network: c.Cidr.String(), + Network: t.Cidr.String(), }, }) if err != nil { - return fmt.Errorf("Activate failed: %v", err) + return fmt.Errorf("activate failed: %v", err) } - c.Device = c.Interface.Name() + t.Device = t.Interface.Name() // TODO use syscalls instead of exec.Command err = exec.Command( `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address", - fmt.Sprintf("name=%s", c.Device), + fmt.Sprintf("name=%s", t.Device), "source=static", - fmt.Sprintf("addr=%s", c.Cidr.IP), - fmt.Sprintf("mask=%s", net.IP(c.Cidr.Mask)), + fmt.Sprintf("addr=%s", t.Cidr.IP), + fmt.Sprintf("mask=%s", net.IP(t.Cidr.Mask)), "gateway=none", ).Run() if err != nil { @@ -57,19 +57,19 @@ func (c *WindowsWaterTun) Activate() error { } err = exec.Command( `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "interface", - c.Device, - fmt.Sprintf("mtu=%d", c.MTU), + t.Device, + fmt.Sprintf("mtu=%d", t.MTU), ).Run() if err != nil { return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err) } - iface, err := net.InterfaceByName(c.Device) + iface, err := net.InterfaceByName(t.Device) if err != nil { - return fmt.Errorf("failed to find interface named %s: %v", c.Device, err) + return fmt.Errorf("failed to find interface named %s: %v", t.Device, err) } - for _, r := range c.UnsafeRoutes { + for _, r := range t.UnsafeRoutes { err = exec.Command( "C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(iface.Index), "METRIC", strconv.Itoa(r.Metric), ).Run() @@ -81,27 +81,27 @@ func (c *WindowsWaterTun) Activate() error { return nil } -func (c *WindowsWaterTun) CidrNet() *net.IPNet { - return c.Cidr +func (t *waterTun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *WindowsWaterTun) DeviceName() string { - return c.Device +func (t *waterTun) DeviceName() string { + return t.Device } -func (c *WindowsWaterTun) WriteRaw(b []byte) error { - _, err := c.Write(b) +func (t *waterTun) WriteRaw(b []byte) error { + _, err := t.Write(b) return err } -func (c *WindowsWaterTun) Close() error { - if c.Interface == nil { +func (t *waterTun) Close() error { + if t.Interface == nil { return nil } - return c.Interface.Close() + return t.Interface.Close() } -func (t *WindowsWaterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *waterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for windows") } diff --git a/overlay/tun_windows.go b/overlay/tun_windows.go index 0351f01..57d28c2 100644 --- a/overlay/tun_windows.go +++ b/overlay/tun_windows.go @@ -5,7 +5,6 @@ package overlay import ( "fmt" - "io" "net" "os" "path/filepath" @@ -15,41 +14,34 @@ import ( "github.com/sirupsen/logrus" ) -type Tun struct { - Device -} - -func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int) (ifce *Tun, err error) { +func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ []Route, _ int) (Device, error) { return nil, fmt.Errorf("newTunFromFd not supported in Windows") } -func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, txQueueLen int, multiqueue bool) (ifce *Tun, err error) { +func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, unsafeRoutes []Route, _ int, _ bool) (Device, error) { if len(routes) > 0 { return nil, fmt.Errorf("route MTU not supported in Windows") } useWintun := true - if err = checkWinTunExists(); err != nil { + if err := checkWinTunExists(); err != nil { l.WithError(err).Warn("Check Wintun driver failed, fallback to wintap driver") useWintun = false } - var inside Device if useWintun { - inside, err = newWinTun(deviceName, cidr, defaultMTU, unsafeRoutes, txQueueLen) + device, err := newWinTun(deviceName, cidr, defaultMTU, unsafeRoutes) if err != nil { - return nil, fmt.Errorf("Create Wintun interface failed, %w", err) - } - } else { - inside, err = newWindowsWaterTun(deviceName, cidr, defaultMTU, unsafeRoutes, txQueueLen) - if err != nil { - return nil, fmt.Errorf("Create wintap driver failed, %w", err) + return nil, fmt.Errorf("create Wintun interface failed, %w", err) } + return device, nil } - return &Tun{ - Device: inside, - }, nil + device, err := newWaterTun(cidr, defaultMTU, unsafeRoutes) + if err != nil { + return nil, fmt.Errorf("create wintap driver failed, %w", err) + } + return device, nil } func checkWinTunExists() error { @@ -68,7 +60,3 @@ func checkWinTunExists() error { _, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll")) return err } - -func (t *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { - return nil, fmt.Errorf("TODO: multiqueue not implemented for windows") -} diff --git a/overlay/tun_wintun_windows.go b/overlay/tun_wintun_windows.go index 67fdfe2..970e739 100644 --- a/overlay/tun_wintun_windows.go +++ b/overlay/tun_wintun_windows.go @@ -14,7 +14,7 @@ import ( const tunGUIDLabel = "Fixed Nebula Windows GUID v1" -type WinTun struct { +type winTun struct { Device string Cidr *net.IPNet MTU int @@ -42,40 +42,38 @@ func generateGUIDByDeviceName(name string) (*windows.GUID, error) { return (*windows.GUID)(unsafe.Pointer(&sum[0])), nil } -func newWinTun(deviceName string, cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route, txQueueLen int) (ifce *WinTun, err error) { +func newWinTun(deviceName string, cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route) (*winTun, error) { guid, err := generateGUIDByDeviceName(deviceName) if err != nil { - return nil, fmt.Errorf("Generate GUID failed: %w", err) + return nil, fmt.Errorf("generate GUID failed: %w", err) } tunDevice, err := wintun.CreateTUNWithRequestedGUID(deviceName, guid, defaultMTU) if err != nil { - return nil, fmt.Errorf("Create TUN device failed: %w", err) + return nil, fmt.Errorf("create TUN device failed: %w", err) } - ifce = &WinTun{ + return &winTun{ Device: deviceName, Cidr: cidr, MTU: defaultMTU, UnsafeRoutes: unsafeRoutes, tun: tunDevice.(*wintun.NativeTun), - } - - return ifce, nil + }, nil } -func (c *WinTun) Activate() error { - luid := winipcfg.LUID(c.tun.LUID()) +func (t *winTun) Activate() error { + luid := winipcfg.LUID(t.tun.LUID()) - if err := luid.SetIPAddresses([]net.IPNet{*c.Cidr}); err != nil { + if err := luid.SetIPAddresses([]net.IPNet{*t.Cidr}); err != nil { return fmt.Errorf("failed to set address: %w", err) } foundDefault4 := false - routes := make([]*winipcfg.RouteData, 0, len(c.UnsafeRoutes)+1) + routes := make([]*winipcfg.RouteData, 0, len(t.UnsafeRoutes)+1) - for _, r := range c.UnsafeRoutes { + for _, r := range t.UnsafeRoutes { if !foundDefault4 { if cidr, bits := r.Cidr.Mask.Size(); cidr == 0 && bits != 0 { foundDefault4 = true @@ -99,7 +97,7 @@ func (c *WinTun) Activate() error { return fmt.Errorf("failed to get ip interface: %w", err) } - ipif.NLMTU = uint32(c.MTU) + ipif.NLMTU = uint32(t.MTU) if foundDefault4 { ipif.UseAutomaticMetric = false ipif.Metric = 0 @@ -112,35 +110,35 @@ func (c *WinTun) Activate() error { return nil } -func (c *WinTun) CidrNet() *net.IPNet { - return c.Cidr +func (t *winTun) CidrNet() *net.IPNet { + return t.Cidr } -func (c *WinTun) DeviceName() string { - return c.Device +func (t *winTun) DeviceName() string { + return t.Device } -func (c *WinTun) Read(b []byte) (int, error) { - return c.tun.Read(b, 0) +func (t *winTun) Read(b []byte) (int, error) { + return t.tun.Read(b, 0) } -func (c *WinTun) Write(b []byte) (int, error) { - return c.tun.Write(b, 0) +func (t *winTun) Write(b []byte) (int, error) { + return t.tun.Write(b, 0) } -func (c *WinTun) WriteRaw(b []byte) error { - _, err := c.Write(b) +func (t *winTun) WriteRaw(b []byte) error { + _, err := t.Write(b) return err } -func (c *WinTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { +func (t *winTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for windows") } -func (c *WinTun) Close() error { +func (t *winTun) Close() error { // It seems that the Windows networking stack doesn't like it when we destroy interfaces that have active routes, // so to be certain, just remove everything before destroying. - luid := winipcfg.LUID(c.tun.LUID()) + luid := winipcfg.LUID(t.tun.LUID()) _ = luid.FlushRoutes(windows.AF_INET) _ = luid.FlushIPAddresses(windows.AF_INET) /* We don't support IPV6 yet @@ -149,5 +147,5 @@ func (c *WinTun) Close() error { */ _ = luid.FlushDNS(windows.AF_INET) - return c.tun.Close() + return t.tun.Close() } diff --git a/test/tun.go b/test/tun.go new file mode 100644 index 0000000..bf6a487 --- /dev/null +++ b/test/tun.go @@ -0,0 +1,47 @@ +package test + +import ( + "errors" + "io" + "net" + + "github.com/slackhq/nebula/iputil" +) + +type NoopTun struct{} + +func (NoopTun) RouteFor(iputil.VpnIp) iputil.VpnIp { + return 0 +} + +func (NoopTun) Activate() error { + return nil +} + +func (NoopTun) CidrNet() *net.IPNet { + return nil +} + +func (NoopTun) DeviceName() string { + return "noop" +} + +func (NoopTun) Read([]byte) (int, error) { + return 0, nil +} + +func (NoopTun) Write([]byte) (int, error) { + return 0, nil +} + +func (NoopTun) WriteRaw([]byte) error { + return nil +} + +func (NoopTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { + return nil, errors.New("unsupported") +} + +func (NoopTun) Close() error { + return nil +}