mirror of
https://github.com/slackhq/nebula.git
synced 2026-08-15 13:27:04 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc04261d0b | |||
| 57e1a9b6af |
@@ -242,6 +242,10 @@ tun:
|
||||
# When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root)
|
||||
disabled: false
|
||||
# Name of the device. If not set, a default will be chosen by the OS.
|
||||
# For Linux: a single `%d` anywhere in the name is treated as a template and replaced with the
|
||||
# lowest number that yields an unused device name (e.g. `nebula%d` becomes `nebula0`, then `nebula1`, and so on, `neb%dprod` becomes `neb0prod`).
|
||||
# Only on Linux: `nebula%d` is the default if tun.dev is unset.
|
||||
# The name, both before and after %d substitution, must be shorter than the kernel limit of 16 characters.
|
||||
# For macOS: if set, must be in the form `utun[0-9]+`.
|
||||
# For NetBSD: Required to be set, must be in the form `tun[0-9]+`
|
||||
dev: nebula1
|
||||
|
||||
+35
-4
@@ -5,6 +5,7 @@ package overlay
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -250,6 +251,17 @@ func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip
|
||||
}
|
||||
|
||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue bool) (*tun, error) {
|
||||
// Validate the device name up front so a bad tun.dev fails fast, before we
|
||||
// open /dev/net/tun or leak a file descriptor. A single %d in the name is
|
||||
// substituted by the kernel during TUNSETIFF (dev_alloc_name) with the
|
||||
// lowest number that yields an unused device name. Resolving the template
|
||||
// in the kernel keeps the pick-a-name/create-the-device pair atomic, so
|
||||
// concurrent callers can never race each other to the same name.
|
||||
tunName := c.GetString("tun.dev", "nebula%d")
|
||||
if err := validateTunName(tunName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
// If /dev/net/tun doesn't exist, try to create it (will happen in docker)
|
||||
@@ -277,12 +289,11 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue
|
||||
if multiqueue {
|
||||
req.Flags |= unix.IFF_MULTI_QUEUE
|
||||
}
|
||||
nameStr := c.GetString("tun.dev", "")
|
||||
copy(req.Name[:], nameStr)
|
||||
copy(req.Name[:], tunName)
|
||||
if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
|
||||
_ = unix.Close(fd)
|
||||
return nil, &NameError{
|
||||
Name: nameStr,
|
||||
Name: tunName,
|
||||
Underlying: err,
|
||||
}
|
||||
}
|
||||
@@ -298,6 +309,27 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, multiqueue
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func validateTunName(tunName string) error {
|
||||
if !strings.Contains(tunName, "%d") {
|
||||
if len(tunName) >= unix.IFNAMSIZ {
|
||||
return fmt.Errorf("tun.dev %q is not shorter than the maximum device name length of %d", tunName, unix.IFNAMSIZ)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if strings.Count(tunName, "%d") > 1 {
|
||||
return fmt.Errorf("tun.dev template %q may only contain a single %%d", tunName)
|
||||
}
|
||||
if tunName == "%d" {
|
||||
return errors.New("please don't name your tun device '%d'")
|
||||
}
|
||||
// The kernel substitutes the %d itself and requires the template, like a
|
||||
// literal name, to be NUL-terminated within IFNAMSIZ bytes.
|
||||
if len(tunName) >= unix.IFNAMSIZ {
|
||||
return fmt.Errorf("tun.dev template %q is not shorter than the maximum device name length of %d", tunName, unix.IFNAMSIZ)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// newTunGeneric does all the stuff common to different tun initialization paths. It will close your files on error.
|
||||
func newTunGeneric(c *config.C, l *slog.Logger, fd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
||||
tfd, err := newTunFd(fd)
|
||||
@@ -768,7 +800,6 @@ func (t *tun) isGatewayInVpnNetworks(gwAddr netip.Addr) bool {
|
||||
|
||||
func (t *tun) getGatewaysFromRoute(r *netlink.Route) routing.Gateways {
|
||||
var gateways routing.Gateways
|
||||
|
||||
link, err := netlink.LinkByName(t.Device)
|
||||
if err != nil {
|
||||
t.l.Error("Ignoring route update: failed to get link by name", "deviceName", t.Device)
|
||||
|
||||
@@ -3,7 +3,12 @@
|
||||
|
||||
package overlay
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var runAdvMSSTests = []struct {
|
||||
name string
|
||||
@@ -32,3 +37,39 @@ func TestTunAdvMSS(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTunName(t *testing.T) {
|
||||
// A device name must be shorter than IFNAMSIZ (i.e. IFNAMSIZ-1 chars max).
|
||||
maxLenName := strings.Repeat("a", unix.IFNAMSIZ-1)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tmpl string
|
||||
wantErr bool
|
||||
}{
|
||||
{"short literal name is fine", "nebula1", false},
|
||||
{"literal name at the max length is fine", maxLenName, false},
|
||||
{"literal name at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ), true},
|
||||
{"trailing template is fine", "nebula%d", false},
|
||||
{"mid-string template is fine", "neb%dprod", false},
|
||||
{"leading template is fine", "%dnebula", false},
|
||||
{"template at the max length is fine", strings.Repeat("a", unix.IFNAMSIZ-3) + "%d", false},
|
||||
{"template at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ-2) + "%d", true},
|
||||
{"bare %d is rejected", "%d", true},
|
||||
{"multiple %d is rejected", "neb%d%dprod", true},
|
||||
{"over-long template is rejected", strings.Repeat("a", unix.IFNAMSIZ-1) + "%d", true},
|
||||
{"over-long mid-string template is rejected", "neb%d" + strings.Repeat("a", unix.IFNAMSIZ-3), true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateTunName(tt.tmpl)
|
||||
if tt.wantErr && err == nil {
|
||||
t.Fatalf("expected an error for %q, got none", tt.tmpl)
|
||||
}
|
||||
if !tt.wantErr && err != nil {
|
||||
t.Fatalf("unexpected error for %q: %v", tt.tmpl, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user