Skip to content

Commit

Permalink
Move all of tun into overlay (slackhq#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrownus authored Nov 11, 2021
1 parent 88ce0ed commit e07524a
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 186 deletions.
7 changes: 4 additions & 3 deletions connection_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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"
Expand Down Expand Up @@ -38,7 +39,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: &Tun{},
inside: &overlay.Tun{},
outside: &udp.Conn{},
certState: cs,
firewall: &Firewall{},
Expand Down Expand Up @@ -107,7 +108,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: &Tun{},
inside: &overlay.Tun{},
outside: &udp.Conn{},
certState: cs,
firewall: &Firewall{},
Expand Down Expand Up @@ -216,7 +217,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: &Tun{},
inside: &overlay.Tun{},
outside: &udp.Conn{},
certState: cs,
firewall: &Firewall{},
Expand Down
7 changes: 4 additions & 3 deletions control_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/gopacket/layers"
"github.com/slackhq/nebula/header"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/overlay"
"github.com/slackhq/nebula/udp"
)

Expand Down Expand Up @@ -64,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.(*Tun).Get(block)
return c.f.inside.(*overlay.Tun).Get(block)
}

// GetFromUDP will pull a udp packet off the udp side of nebula
Expand All @@ -77,7 +78,7 @@ func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
}

func (c *Control) GetTunTxChan() <-chan []byte {
return c.f.inside.(*Tun).txPackets
return c.f.inside.(*overlay.Tun).TxPackets
}

// InjectUDPPacket will inject a packet into the udp side of nebula
Expand Down Expand Up @@ -114,7 +115,7 @@ func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16
panic(err)
}

c.f.inside.(*Tun).Send(buffer.Bytes())
c.f.inside.(*overlay.Tun).Send(buffer.Bytes())
}

func (c *Control) GetUDPAddr() string {
Expand Down
7 changes: 4 additions & 3 deletions hostmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/slackhq/nebula/cidr"
"github.com/slackhq/nebula/header"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/overlay"
"github.com/slackhq/nebula/udp"
)

Expand Down Expand Up @@ -408,10 +409,10 @@ func (hm *HostMap) Punchy(ctx context.Context, conn *udp.Conn) {
}
}

func (hm *HostMap) addUnsafeRoutes(routes *[]route) {
func (hm *HostMap) addUnsafeRoutes(routes *[]overlay.Route) {
for _, r := range *routes {
hm.l.WithField("route", r.route).WithField("via", r.via).Warn("Adding UNSAFE Route")
hm.unsafeRoutes.AddCIDR(r.route, iputil.Ip2VpnIp(*r.via))
hm.l.WithField("cidr", r.Cidr).WithField("via", r.Via).Warn("Adding UNSAFE Route")
hm.unsafeRoutes.AddCIDR(r.Cidr, iputil.Ip2VpnIp(*r.Via))
}
}

Expand Down
31 changes: 3 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg

// TODO: make sure mask is 4 bytes
tunCidr := cs.certificate.Details.Ips[0]
routes, err := parseRoutes(c, tunCidr)
routes, err := overlay.ParseRoutes(c, tunCidr)
if err != nil {
return nil, util.NewContextualError("Could not parse tun.routes", nil, err)
}
unsafeRoutes, err := parseUnsafeRoutes(c, tunCidr)
unsafeRoutes, err := overlay.ParseUnsafeRoutes(c, tunCidr)
if err != nil {
return nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
}
Expand Down Expand Up @@ -142,32 +142,7 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg
if !configTest {
c.CatchHUP(ctx)

switch {
case c.GetBool("tun.disabled", false):
tun = newDisabledTun(tunCidr, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l)
case tunFd != nil:
tun, err = newTunFromFd(
l,
*tunFd,
tunCidr,
c.GetInt("tun.mtu", DEFAULT_MTU),
routes,
unsafeRoutes,
c.GetInt("tun.tx_queue", 500),
)
default:
tun, err = newTun(
l,
c.GetString("tun.dev", ""),
tunCidr,
c.GetInt("tun.mtu", DEFAULT_MTU),
routes,
unsafeRoutes,
c.GetInt("tun.tx_queue", 500),
routines > 1,
)
}

tun, err = overlay.NewDeviceFromConfig(c, l, tunCidr, routes, unsafeRoutes, tunFd, routines)
if err != nil {
return nil, util.NewContextualError("Failed to get a tun/tap device", nil, err)
}
Expand Down
56 changes: 27 additions & 29 deletions tun_common.go → overlay/route.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nebula
package overlay

import (
"fmt"
Expand All @@ -9,21 +9,19 @@ import (
"github.com/slackhq/nebula/config"
)

const DEFAULT_MTU = 1300

type route struct {
mtu int
metric int
route *net.IPNet
via *net.IP
type Route struct {
MTU int
Metric int
Cidr *net.IPNet
Via *net.IP
}

func parseRoutes(c *config.C, network *net.IPNet) ([]route, error) {
func ParseRoutes(c *config.C, network *net.IPNet) ([]Route, error) {
var err error

r := c.Get("tun.routes")
if r == nil {
return []route{}, nil
return []Route{}, nil
}

rawRoutes, ok := r.([]interface{})
Expand All @@ -32,10 +30,10 @@ func parseRoutes(c *config.C, network *net.IPNet) ([]route, error) {
}

if len(rawRoutes) < 1 {
return []route{}, nil
return []Route{}, nil
}

routes := make([]route, len(rawRoutes))
routes := make([]Route, len(rawRoutes))
for i, r := range rawRoutes {
m, ok := r.(map[interface{}]interface{})
if !ok {
Expand Down Expand Up @@ -64,20 +62,20 @@ func parseRoutes(c *config.C, network *net.IPNet) ([]route, error) {
return nil, fmt.Errorf("entry %v.route in tun.routes is not present", i+1)
}

r := route{
mtu: mtu,
r := Route{
MTU: mtu,
}

_, r.route, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
_, r.Cidr, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
if err != nil {
return nil, fmt.Errorf("entry %v.route in tun.routes failed to parse: %v", i+1, err)
}

if !ipWithin(network, r.route) {
if !ipWithin(network, r.Cidr) {
return nil, fmt.Errorf(
"entry %v.route in tun.routes is not contained within the network attached to the certificate; route: %v, network: %v",
i+1,
r.route.String(),
r.Cidr.String(),
network.String(),
)
}
Expand All @@ -88,12 +86,12 @@ func parseRoutes(c *config.C, network *net.IPNet) ([]route, error) {
return routes, nil
}

func parseUnsafeRoutes(c *config.C, network *net.IPNet) ([]route, error) {
func ParseUnsafeRoutes(c *config.C, network *net.IPNet) ([]Route, error) {
var err error

r := c.Get("tun.unsafe_routes")
if r == nil {
return []route{}, nil
return []Route{}, nil
}

rawRoutes, ok := r.([]interface{})
Expand All @@ -102,10 +100,10 @@ func parseUnsafeRoutes(c *config.C, network *net.IPNet) ([]route, error) {
}

if len(rawRoutes) < 1 {
return []route{}, nil
return []Route{}, nil
}

routes := make([]route, len(rawRoutes))
routes := make([]Route, len(rawRoutes))
for i, r := range rawRoutes {
m, ok := r.(map[interface{}]interface{})
if !ok {
Expand All @@ -114,7 +112,7 @@ func parseUnsafeRoutes(c *config.C, network *net.IPNet) ([]route, error) {

rMtu, ok := m["mtu"]
if !ok {
rMtu = c.GetInt("tun.mtu", DEFAULT_MTU)
rMtu = c.GetInt("tun.mtu", DefaultMTU)
}

mtu, ok := rMtu.(int)
Expand Down Expand Up @@ -166,22 +164,22 @@ func parseUnsafeRoutes(c *config.C, network *net.IPNet) ([]route, error) {
return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes is not present", i+1)
}

r := route{
via: &nVia,
mtu: mtu,
metric: metric,
r := Route{
Via: &nVia,
MTU: mtu,
Metric: metric,
}

_, r.route, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
_, r.Cidr, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
if err != nil {
return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes failed to parse: %v", i+1, err)
}

if ipWithin(network, r.route) {
if ipWithin(network, r.Cidr) {
return nil, fmt.Errorf(
"entry %v.route in tun.unsafe_routes is contained within the network attached to the certificate; route: %v, network: %v",
i+1,
r.route.String(),
r.Cidr.String(),
network.String(),
)
}
Expand Down
41 changes: 41 additions & 0 deletions overlay/tun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package overlay

import (
"net"

"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/config"
)

const DefaultMTU = 1300

func NewDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routes, unsafeRoutes []Route, fd *int, routines int) (Device, error) {
switch {
case c.GetBool("tun.disabled", false):
tun := newDisabledTun(tunCidr, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l)
return tun, nil

case fd != nil:
return newTunFromFd(
l,
*fd,
tunCidr,
c.GetInt("tun.mtu", DefaultMTU),
routes,
unsafeRoutes,
c.GetInt("tun.tx_queue", 500),
)

default:
return newTun(
l,
c.GetString("tun.dev", ""),
tunCidr,
c.GetInt("tun.mtu", DefaultMTU),
routes,
unsafeRoutes,
c.GetInt("tun.tx_queue", 500),
routines > 1,
)
}
}
10 changes: 5 additions & 5 deletions tun_android.go → overlay/tun_android.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !e2e_testing
// +build !e2e_testing

package nebula
package overlay

import (
"fmt"
Expand All @@ -21,12 +21,12 @@ type Tun struct {
MaxMTU int
DefaultMTU int
TXQueueLen int
Routes []route
UnsafeRoutes []route
Routes []Route
UnsafeRoutes []Route
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) (ifce *Tun, err error) {
file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")

ifce = &Tun{
Expand All @@ -43,7 +43,7 @@ func newTunFromFd(l *logrus.Logger, deviceFd int, cidr *net.IPNet, defaultMTU in
return
}

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) (ifce *Tun, err error) {
return nil, fmt.Errorf("newTun not supported in Android")
}

Expand Down
Loading

0 comments on commit e07524a

Please sign in to comment.