Skip to content

Commit

Permalink
Remove WriteRaw, cidrTree -> routeTree to better describe its purpose…
Browse files Browse the repository at this point in the history
…, remove redundancy from field names (#582)
  • Loading branch information
nbrownus authored Nov 12, 2021
1 parent 467e605 commit 78d0d46
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 204 deletions.
2 changes: 1 addition & 1 deletion control_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 2 additions & 3 deletions overlay/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions overlay/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"math"
"net"
"runtime"
"strconv"

"github.com/slackhq/nebula/cidr"
"github.com/slackhq/nebula/config"
)

Expand All @@ -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

Expand Down
17 changes: 0 additions & 17 deletions overlay/tun.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}
33 changes: 5 additions & 28 deletions overlay/tun_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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"
}

Expand Down
27 changes: 11 additions & 16 deletions overlay/tun_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
9 changes: 2 additions & 7 deletions overlay/tun_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
}
Expand Down
47 changes: 21 additions & 26 deletions overlay/tun_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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))
Expand All @@ -101,27 +101,22 @@ 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)
}

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")
}
Loading

0 comments on commit 78d0d46

Please sign in to comment.