From 78d0d46baea578c9d6e02c2f0f137068bc269839 Mon Sep 17 00:00:00 2001 From: Nate Brown Date: Fri, 12 Nov 2021 12:47:09 -0600 Subject: [PATCH] Remove WriteRaw, cidrTree -> routeTree to better describe its purpose, remove redundancy from field names (#582) --- control_tester.go | 2 +- interface.go | 2 +- overlay/device.go | 5 ++-- overlay/route.go | 16 ++++++++++++ overlay/tun.go | 17 ------------- overlay/tun_android.go | 33 ++++-------------------- overlay/tun_darwin.go | 27 ++++++++------------ overlay/tun_disabled.go | 9 ++----- overlay/tun_freebsd.go | 47 ++++++++++++++++------------------- overlay/tun_ios.go | 16 ++++-------- overlay/tun_linux.go | 47 ++++++++++++++++------------------- overlay/tun_tester.go | 36 ++++++++++++--------------- overlay/tun_water_windows.go | 39 +++++++++++++---------------- overlay/tun_wintun_windows.go | 37 ++++++++++++--------------- test/tun.go | 8 ++---- 15 files changed, 137 insertions(+), 204 deletions(-) diff --git a/control_tester.go b/control_tester.go index a02eadc..00691de 100644 --- a/control_tester.go +++ b/control_tester.go @@ -92,7 +92,7 @@ func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16 Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, - SrcIP: c.f.inside.CidrNet().IP, + SrcIP: c.f.inside.Cidr().IP, DstIP: toIp, } diff --git a/interface.go b/interface.go index 610578d..5d099db 100644 --- a/interface.go +++ b/interface.go @@ -147,7 +147,7 @@ func (f *Interface) activate() { f.l.WithError(err).Error("Failed to get udp listen address") } - f.l.WithField("interface", f.inside.DeviceName()).WithField("network", f.inside.CidrNet().String()). + f.l.WithField("interface", f.inside.Name()).WithField("network", f.inside.Cidr().String()). WithField("build", f.version).WithField("udpAddr", addr). Info("Nebula interface is active") diff --git a/overlay/device.go b/overlay/device.go index f20ef4a..3f3f2eb 100644 --- a/overlay/device.go +++ b/overlay/device.go @@ -10,9 +10,8 @@ import ( type Device interface { io.ReadWriteCloser Activate() error - CidrNet() *net.IPNet - DeviceName() string - WriteRaw([]byte) error + Cidr() *net.IPNet + Name() string RouteFor(iputil.VpnIp) iputil.VpnIp NewMultiQueueReader() (io.ReadWriteCloser, error) } diff --git a/overlay/route.go b/overlay/route.go index c6a3ad4..5f0033c 100644 --- a/overlay/route.go +++ b/overlay/route.go @@ -4,8 +4,10 @@ import ( "fmt" "math" "net" + "runtime" "strconv" + "github.com/slackhq/nebula/cidr" "github.com/slackhq/nebula/config" ) @@ -16,6 +18,20 @@ type Route struct { Via *net.IP } +func makeRouteTree(routes []Route, allowMTU bool) (*cidr.Tree4, error) { + routeTree := cidr.NewTree4() + for _, r := range routes { + if !allowMTU && r.MTU > 0 { + return nil, fmt.Errorf("route MTU is not supported in %s", runtime.GOOS) + } + + if r.Via != nil { + routeTree.AddCIDR(r.Cidr, r.Via) + } + } + return routeTree, nil +} + func parseRoutes(c *config.C, network *net.IPNet) ([]Route, error) { var err error diff --git a/overlay/tun.go b/overlay/tun.go index abe99f7..3da50b8 100644 --- a/overlay/tun.go +++ b/overlay/tun.go @@ -1,12 +1,9 @@ package overlay import ( - "fmt" "net" - "runtime" "github.com/sirupsen/logrus" - "github.com/slackhq/nebula/cidr" "github.com/slackhq/nebula/config" "github.com/slackhq/nebula/util" ) @@ -52,17 +49,3 @@ func NewDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, fd * ) } } - -func makeCidrTree(routes []Route, allowMTU bool) (*cidr.Tree4, error) { - cidrTree := cidr.NewTree4() - for _, r := range routes { - if !allowMTU && r.MTU > 0 { - return nil, fmt.Errorf("route MTU is not supported in %s", runtime.GOOS) - } - - if r.Via != nil { - cidrTree.AddCIDR(r.Cidr, r.Via) - } - } - return cidrTree, nil -} diff --git a/overlay/tun_android.go b/overlay/tun_android.go index b541b9a..697dffa 100644 --- a/overlay/tun_android.go +++ b/overlay/tun_android.go @@ -12,13 +12,12 @@ import ( "github.com/sirupsen/logrus" "github.com/slackhq/nebula/iputil" - "golang.org/x/sys/unix" ) type tun struct { io.ReadWriteCloser fd int - Cidr *net.IPNet + cidr *net.IPNet l *logrus.Logger } @@ -32,7 +31,7 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes return &tun{ ReadWriteCloser: file, fd: int(file.Fd()), - Cidr: cidr, + cidr: cidr, l: l, }, nil } @@ -45,37 +44,15 @@ func (t *tun) RouteFor(iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *tun) WriteRaw(b []byte) error { - var nn int - for { - max := len(b) - n, err := unix.Write(t.fd, b[nn:max]) - if n > 0 { - nn += n - } - if nn == len(b) { - return err - } - - if err != nil { - return err - } - - if n == 0 { - return io.ErrUnexpectedEOF - } - } -} - func (t tun) Activate() error { return nil } -func (t *tun) CidrNet() *net.IPNet { - return t.Cidr +func (t *tun) Cidr() *net.IPNet { + return t.cidr } -func (t *tun) DeviceName() string { +func (t *tun) Name() string { return "android" } diff --git a/overlay/tun_darwin.go b/overlay/tun_darwin.go index 987e03d..e812c33 100644 --- a/overlay/tun_darwin.go +++ b/overlay/tun_darwin.go @@ -21,10 +21,10 @@ import ( type tun struct { io.ReadWriteCloser Device string - Cidr *net.IPNet + cidr *net.IPNet DefaultMTU int Routes []Route - cidrTree *cidr.Tree4 + routeTree *cidr.Tree4 l *logrus.Logger // cache out buffer since we need to prepend 4 bytes for tun metadata @@ -77,7 +77,7 @@ type ifreqMTU struct { } func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, routes []Route, _ int, _ bool) (*tun, error) { - cidrTree, err := makeCidrTree(routes, false) + routeTree, err := makeRouteTree(routes, false) if err != nil { return nil, err } @@ -152,10 +152,10 @@ func newTun(l *logrus.Logger, name string, cidr *net.IPNet, defaultMTU int, rout tun := &tun{ ReadWriteCloser: file, Device: name, - Cidr: cidr, + cidr: cidr, DefaultMTU: defaultMTU, Routes: routes, - cidrTree: cidrTree, + routeTree: routeTree, l: l, } @@ -185,8 +185,8 @@ func (t *tun) Activate() error { var addr, mask [4]byte - copy(addr[:], t.Cidr.IP.To4()) - copy(mask[:], t.Cidr.Mask) + copy(addr[:], t.cidr.IP.To4()) + copy(mask[:], t.cidr.Mask) s, err := unix.Socket( unix.AF_INET, @@ -303,7 +303,7 @@ func (t *tun) Activate() error { } func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { - r := t.cidrTree.MostSpecificContains(ip) + r := t.routeTree.MostSpecificContains(ip) if r != nil { return r.(iputil.VpnIp) } @@ -403,19 +403,14 @@ func (t *tun) Write(from []byte) (int, error) { return n - 4, err } -func (t *tun) CidrNet() *net.IPNet { - return t.Cidr +func (t *tun) Cidr() *net.IPNet { + return t.cidr } -func (t *tun) DeviceName() string { +func (t *tun) Name() string { return t.Device } -func (t *tun) WriteRaw(b []byte) error { - _, err := t.Write(b) - return err -} - func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for darwin") } diff --git a/overlay/tun_disabled.go b/overlay/tun_disabled.go index 17e11e6..b7f7273 100644 --- a/overlay/tun_disabled.go +++ b/overlay/tun_disabled.go @@ -48,11 +48,11 @@ func (*disabledTun) RouteFor(iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *disabledTun) CidrNet() *net.IPNet { +func (t *disabledTun) Cidr() *net.IPNet { return t.cidr } -func (*disabledTun) DeviceName() string { +func (*disabledTun) Name() string { return "disabled" } @@ -128,11 +128,6 @@ func (t *disabledTun) Write(b []byte) (int, error) { return len(b), nil } -func (t *disabledTun) WriteRaw(b []byte) error { - _, err := t.Write(b) - return err -} - func (t *disabledTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return t, nil } diff --git a/overlay/tun_freebsd.go b/overlay/tun_freebsd.go index affa870..709a243 100644 --- a/overlay/tun_freebsd.go +++ b/overlay/tun_freebsd.go @@ -21,12 +21,12 @@ import ( var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`) type tun struct { - Device string - Cidr *net.IPNet - MTU int - Routes []Route - cidrTree *cidr.Tree4 - l *logrus.Logger + Device string + cidr *net.IPNet + MTU int + Routes []Route + routeTree *cidr.Tree4 + l *logrus.Logger io.ReadWriteCloser } @@ -43,7 +43,7 @@ func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ int } func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, routes []Route, _ int, _ bool) (*tun, error) { - cidrTree, err := makeCidrTree(routes, false) + routeTree, err := makeRouteTree(routes, false) if err != nil { return nil, err } @@ -55,12 +55,12 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int return nil, fmt.Errorf("tun.dev must match `tun[0-9]+`") } return &tun{ - Device: deviceName, - Cidr: cidr, - MTU: defaultMTU, - Routes: routes, - cidrTree: cidrTree, - l: l, + Device: deviceName, + cidr: cidr, + MTU: defaultMTU, + Routes: routes, + routeTree: routeTree, + l: l, }, nil } @@ -72,12 +72,12 @@ func (t *tun) Activate() error { } // TODO use syscalls instead of exec.Command - 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 { + 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) } - 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 { + 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) } t.l.Debug("command: ifconfig", t.Device, "mtu", strconv.Itoa(t.MTU)) @@ -101,7 +101,7 @@ func (t *tun) Activate() error { } func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { - r := t.cidrTree.MostSpecificContains(ip) + r := t.routeTree.MostSpecificContains(ip) if r != nil { return r.(iputil.VpnIp) } @@ -109,19 +109,14 @@ func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *tun) CidrNet() *net.IPNet { - return t.Cidr +func (t *tun) Cidr() *net.IPNet { + return t.cidr } -func (t *tun) DeviceName() string { +func (t *tun) Name() string { return t.Device } -func (t *tun) WriteRaw(b []byte) error { - _, err := t.Write(b) - return err -} - 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 61f69c9..3f30b55 100644 --- a/overlay/tun_ios.go +++ b/overlay/tun_ios.go @@ -19,7 +19,7 @@ import ( type tun struct { io.ReadWriteCloser - Cidr *net.IPNet + cidr *net.IPNet } func newTun(_ *logrus.Logger, _ string, _ *net.IPNet, _ int, _ []Route, _ int, _ bool) (*tun, error) { @@ -33,8 +33,7 @@ func newTunFromFd(_ *logrus.Logger, deviceFd int, cidr *net.IPNet, _ int, routes file := os.NewFile(uintptr(deviceFd), "/dev/tun") return &tun{ - Cidr: cidr, - Device: "iOS", + cidr: cidr, ReadWriteCloser: &tunReadCloser{f: file}, }, nil } @@ -47,11 +46,6 @@ func (t *tun) RouteFor(iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *tun) WriteRaw(b []byte) error { - _, err := t.Write(b) - return err -} - // The following is hoisted up from water, we do this so we can inject our own fd on iOS type tunReadCloser struct { f io.ReadWriteCloser @@ -110,11 +104,11 @@ func (tr *tunReadCloser) Close() error { return tr.f.Close() } -func (t *tun) CidrNet() *net.IPNet { - return t.Cidr +func (t *tun) Cidr() *net.IPNet { + return t.cidr } -func (t *tun) DeviceName() string { +func (t *tun) Name() string { return "iOS" } diff --git a/overlay/tun_linux.go b/overlay/tun_linux.go index 87aac32..15b8b27 100644 --- a/overlay/tun_linux.go +++ b/overlay/tun_linux.go @@ -22,12 +22,12 @@ type tun struct { io.ReadWriteCloser fd int Device string - Cidr *net.IPNet + cidr *net.IPNet MaxMTU int DefaultMTU int TXQueueLen int Routes []Route - cidrTree *cidr.Tree4 + routeTree *cidr.Tree4 l *logrus.Logger } @@ -64,7 +64,7 @@ type ifreqQLEN struct { } func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU int, routes []Route, txQueueLen int) (*tun, error) { - cidrTree, err := makeCidrTree(routes, true) + routeTree, err := makeRouteTree(routes, true) if err != nil { return nil, err } @@ -75,11 +75,11 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU in ReadWriteCloser: file, fd: int(file.Fd()), Device: "tun0", - Cidr: cidr, + cidr: cidr, DefaultMTU: defaultMTU, TXQueueLen: txQueueLen, Routes: routes, - cidrTree: cidrTree, + routeTree: routeTree, l: l, }, nil } @@ -110,7 +110,7 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int } } - cidrTree, err := makeCidrTree(routes, true) + routeTree, err := makeRouteTree(routes, true) if err != nil { return nil, err } @@ -119,12 +119,12 @@ func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int ReadWriteCloser: file, fd: int(file.Fd()), Device: name, - Cidr: cidr, + cidr: cidr, MaxMTU: maxMTU, DefaultMTU: defaultMTU, TXQueueLen: txQueueLen, Routes: routes, - cidrTree: cidrTree, + routeTree: routeTree, l: l, }, nil } @@ -148,7 +148,7 @@ func (t *tun) NewMultiQueueReader() (io.ReadWriteCloser, error) { } func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { - r := t.cidrTree.MostSpecificContains(ip) + r := t.routeTree.MostSpecificContains(ip) if r != nil { return r.(iputil.VpnIp) } @@ -156,32 +156,29 @@ func (t *tun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *tun) WriteRaw(b []byte) error { +func (t *tun) Write(b []byte) (int, error) { var nn int + max := len(b) + for { - max := len(b) n, err := unix.Write(t.fd, b[nn:max]) if n > 0 { nn += n } if nn == len(b) { - return err + return nn, err } if err != nil { - return err + return nn, err } if n == 0 { - return io.ErrUnexpectedEOF + return nn, io.ErrUnexpectedEOF } } } -func (t *tun) Write(b []byte) (int, error) { - return len(b), t.WriteRaw(b) -} - func (t tun) deviceBytes() (o [16]byte) { for i, c := range t.Device { o[i] = byte(c) @@ -194,8 +191,8 @@ func (t tun) Activate() error { var addr, mask [4]byte - copy(addr[:], t.Cidr.IP.To4()) - copy(mask[:], t.Cidr.Mask) + copy(addr[:], t.cidr.IP.To4()) + copy(mask[:], t.cidr.Mask) s, err := unix.Socket( unix.AF_INET, @@ -259,14 +256,14 @@ func (t tun) Activate() error { } // Default route - dr := &net.IPNet{IP: t.Cidr.IP.Mask(t.Cidr.Mask), Mask: t.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: t.DefaultMTU, AdvMSS: t.advMSS(Route{}), Scope: unix.RT_SCOPE_LINK, - Src: t.Cidr.IP, + Src: t.cidr.IP, Protocol: unix.RTPROT_KERNEL, Table: unix.RT_TABLE_MAIN, Type: unix.RTN_UNICAST, @@ -305,11 +302,11 @@ func (t tun) Activate() error { return nil } -func (t *tun) CidrNet() *net.IPNet { - return t.Cidr +func (t *tun) Cidr() *net.IPNet { + return t.cidr } -func (t *tun) DeviceName() string { +func (t *tun) Name() string { return t.Device } diff --git a/overlay/tun_tester.go b/overlay/tun_tester.go index 2638b97..25d2648 100644 --- a/overlay/tun_tester.go +++ b/overlay/tun_tester.go @@ -14,27 +14,27 @@ import ( ) type TestTun struct { - Device string - Cidr *net.IPNet - Routes []Route - cidrTree *cidr.Tree4 - l *logrus.Logger + Device string + cidr *net.IPNet + Routes []Route + routeTree *cidr.Tree4 + l *logrus.Logger rxPackets chan []byte // Packets to receive into nebula TxPackets chan []byte // Packets transmitted outside by nebula } func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, _ int, routes []Route, _ int, _ bool) (*TestTun, error) { - cidrTree, err := makeCidrTree(routes, false) + routeTree, err := makeRouteTree(routes, false) if err != nil { return nil, err } return &TestTun{ Device: deviceName, - Cidr: cidr, + cidr: cidr, Routes: routes, - cidrTree: cidrTree, + routeTree: routeTree, l: l, rxPackets: make(chan []byte, 1), TxPackets: make(chan []byte, 1), @@ -74,7 +74,7 @@ func (t *TestTun) Get(block bool) []byte { //********************************************************************************************************************// func (t *TestTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { - r := t.cidrTree.MostSpecificContains(ip) + r := t.routeTree.MostSpecificContains(ip) if r != nil { return r.(iputil.VpnIp) } @@ -86,16 +86,19 @@ func (t *TestTun) Activate() error { return nil } -func (t *TestTun) CidrNet() *net.IPNet { - return t.Cidr +func (t *TestTun) Cidr() *net.IPNet { + return t.cidr } -func (t *TestTun) DeviceName() string { +func (t *TestTun) Name() string { return t.Device } func (t *TestTun) Write(b []byte) (n int, err error) { - return len(b), t.WriteRaw(b) + packet := make([]byte, len(b), len(b)) + copy(packet, b) + t.TxPackets <- packet + return len(b), nil } func (t *TestTun) Close() error { @@ -103,13 +106,6 @@ func (t *TestTun) Close() error { return nil } -func (t *TestTun) WriteRaw(b []byte) error { - packet := make([]byte, len(b), len(b)) - copy(packet, b) - t.TxPackets <- packet - return nil -} - func (t *TestTun) Read(b []byte) (int, error) { p := <-t.rxPackets copy(b, p) diff --git a/overlay/tun_water_windows.go b/overlay/tun_water_windows.go index b219fef..10a6dc0 100644 --- a/overlay/tun_water_windows.go +++ b/overlay/tun_water_windows.go @@ -13,27 +13,27 @@ import ( ) type waterTun struct { - Device string - Cidr *net.IPNet - MTU int - Routes []Route - cidrTree *cidr.Tree4 + Device string + cidr *net.IPNet + MTU int + Routes []Route + routeTree *cidr.Tree4 *water.Interface } func newWaterTun(cidr *net.IPNet, defaultMTU int, routes []Route) (*waterTun, error) { - cidrTree, err := makeCidrTree(routes, false) + routeTree, err := makeRouteTree(routes, false) if err != nil { return nil, err } // NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate() return &waterTun{ - Cidr: cidr, - MTU: defaultMTU, - Routes: routes, - cidrTree: cidrTree, + cidr: cidr, + MTU: defaultMTU, + Routes: routes, + routeTree: routeTree, }, nil } @@ -43,7 +43,7 @@ func (t *waterTun) Activate() error { DeviceType: water.TUN, PlatformSpecificParams: water.PlatformSpecificParams{ ComponentID: "tap0901", - Network: t.Cidr.String(), + Network: t.cidr.String(), }, }) if err != nil { @@ -57,8 +57,8 @@ func (t *waterTun) Activate() error { `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address", fmt.Sprintf("name=%s", t.Device), "source=static", - fmt.Sprintf("addr=%s", t.Cidr.IP), - fmt.Sprintf("mask=%s", net.IP(t.Cidr.Mask)), + fmt.Sprintf("addr=%s", t.cidr.IP), + fmt.Sprintf("mask=%s", net.IP(t.cidr.Mask)), "gateway=none", ).Run() if err != nil { @@ -96,7 +96,7 @@ func (t *waterTun) Activate() error { } func (t *waterTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { - r := t.cidrTree.MostSpecificContains(ip) + r := t.routeTree.MostSpecificContains(ip) if r != nil { return r.(iputil.VpnIp) } @@ -104,19 +104,14 @@ func (t *waterTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *waterTun) CidrNet() *net.IPNet { - return t.Cidr +func (t *waterTun) Cidr() *net.IPNet { + return t.cidr } -func (t *waterTun) DeviceName() string { +func (t *waterTun) Name() string { return t.Device } -func (t *waterTun) WriteRaw(b []byte) error { - _, err := t.Write(b) - return err -} - func (t *waterTun) Close() error { if t.Interface == nil { return nil diff --git a/overlay/tun_wintun_windows.go b/overlay/tun_wintun_windows.go index 37f30e5..745f554 100644 --- a/overlay/tun_wintun_windows.go +++ b/overlay/tun_wintun_windows.go @@ -17,11 +17,11 @@ import ( const tunGUIDLabel = "Fixed Nebula Windows GUID v1" type winTun struct { - Device string - Cidr *net.IPNet - MTU int - Routes []Route - cidrTree *cidr.Tree4 + Device string + cidr *net.IPNet + MTU int + Routes []Route + routeTree *cidr.Tree4 tun *wintun.NativeTun } @@ -56,17 +56,17 @@ func newWinTun(deviceName string, cidr *net.IPNet, defaultMTU int, routes []Rout return nil, fmt.Errorf("create TUN device failed: %w", err) } - cidrTree, err := makeCidrTree(routes, false) + routeTree, err := makeRouteTree(routes, false) if err != nil { return nil, err } return &winTun{ - Device: deviceName, - Cidr: cidr, - MTU: defaultMTU, - Routes: routes, - cidrTree: cidrTree, + Device: deviceName, + cidr: cidr, + MTU: defaultMTU, + Routes: routes, + routeTree: routeTree, tun: tunDevice.(*wintun.NativeTun), }, nil @@ -75,7 +75,7 @@ func newWinTun(deviceName string, cidr *net.IPNet, defaultMTU int, routes []Rout func (t *winTun) Activate() error { luid := winipcfg.LUID(t.tun.LUID()) - if err := luid.SetIPAddresses([]net.IPNet{*t.Cidr}); err != nil { + if err := luid.SetIPAddresses([]net.IPNet{*t.cidr}); err != nil { return fmt.Errorf("failed to set address: %w", err) } @@ -125,7 +125,7 @@ func (t *winTun) Activate() error { } func (t *winTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { - r := t.cidrTree.MostSpecificContains(ip) + r := t.routeTree.MostSpecificContains(ip) if r != nil { return r.(iputil.VpnIp) } @@ -133,11 +133,11 @@ func (t *winTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp { return 0 } -func (t *winTun) CidrNet() *net.IPNet { - return t.Cidr +func (t *winTun) Cidr() *net.IPNet { + return t.cidr } -func (t *winTun) DeviceName() string { +func (t *winTun) Name() string { return t.Device } @@ -149,11 +149,6 @@ func (t *winTun) Write(b []byte) (int, error) { return t.tun.Write(b, 0) } -func (t *winTun) WriteRaw(b []byte) error { - _, err := t.Write(b) - return err -} - func (t *winTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { return nil, fmt.Errorf("TODO: multiqueue not implemented for windows") } diff --git a/test/tun.go b/test/tun.go index bf6a487..86656c9 100644 --- a/test/tun.go +++ b/test/tun.go @@ -18,11 +18,11 @@ func (NoopTun) Activate() error { return nil } -func (NoopTun) CidrNet() *net.IPNet { +func (NoopTun) Cidr() *net.IPNet { return nil } -func (NoopTun) DeviceName() string { +func (NoopTun) Name() string { return "noop" } @@ -34,10 +34,6 @@ 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") }