mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 16:46:52 +01:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f59f895a4 | ||
|
|
df1c4d9364 | ||
|
|
36579bae03 | ||
|
|
73d26dd87d | ||
|
|
94372f4f51 | ||
|
|
42280f04ce | ||
|
|
11c1917387 | ||
|
|
5ba7aff4b3 | ||
|
|
8a2de9b1b5 | ||
|
|
a8c52f5c13 | ||
|
|
13af8dd6f3 |
No files matched your search
@@ -13,11 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
||||||
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
||||||
the handshake starts, so a pending hostinfo never exists without one.
|
the handshake starts, so a pending hostinfo never exists without one.
|
||||||
- A firewall rule with `group: []`, `groups:` with no value, or a `groups` entry that isn't a string, such as
|
|
||||||
`groups: [1, 2]`, no longer crashes Nebula on startup or reload. A null `group` or `groups` means none was
|
|
||||||
provided. An unquoted int or bool group, such as `group: 7`, is still read as a string but logs a warning to
|
|
||||||
quote it, since YAML has already turned a value like `007` into `7`. A float, a date, an empty array, a null
|
|
||||||
entry, a nested array, or a map is rejected with an error.
|
|
||||||
|
|
||||||
## [1.11.1] - 2026-08-21
|
## [1.11.1] - 2026-08-21
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package nebula
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -11,6 +13,7 @@ import (
|
|||||||
ct "github.com/slackhq/nebula/cert_test"
|
ct "github.com/slackhq/nebula/cert_test"
|
||||||
"github.com/slackhq/nebula/handshake"
|
"github.com/slackhq/nebula/handshake"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/overlay"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -147,6 +150,122 @@ func TestSendNoMetricsCloseTunnelKeepsRebindEpoch(t *testing.T) {
|
|||||||
assert.True(t, hostinfo.markOut(1), "a CloseTunnel send must not consume the rebind epoch")
|
assert.True(t, hostinfo.markOut(1), "a CloseTunnel send must not consume the rebind epoch")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// capturingConn is a udp.Conn that records every WriteTo.
|
||||||
|
type capturingConn struct {
|
||||||
|
udp.NoopConn
|
||||||
|
writes [][]byte
|
||||||
|
addrs []netip.AddrPort
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *capturingConn) WriteTo(b []byte, addr netip.AddrPort) error {
|
||||||
|
c.writes = append(c.writes, append([]byte(nil), b...))
|
||||||
|
c.addrs = append(c.addrs, addr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSendNoMetricsViaRelay sends through a relay from the mtu sized buffer the cached packet flush uses. Payloads up
|
||||||
|
// to overlay.MaxMTU go out intact, anything bigger is dropped rather than outgrowing the buffer, which once panicked.
|
||||||
|
func TestSendNoMetricsViaRelay(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
n int
|
||||||
|
sent bool
|
||||||
|
}{
|
||||||
|
{0, true},
|
||||||
|
{1300, true},
|
||||||
|
{overlay.MaxMTU, true},
|
||||||
|
{overlay.MaxMTU + 1, false}, // too big for the relay's tag
|
||||||
|
{mtu - 47, false}, // too big for the inner tag, this panicked
|
||||||
|
{9000, false}, // a full packet at tun.mtu 9000
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(strconv.Itoa(tt.n), func(t *testing.T) {
|
||||||
|
// One tunnel to the target, carried end to end, and one to the relay that wraps it
|
||||||
|
targetInit, targetResp := runTestHandshake(t)
|
||||||
|
relayInit, relayResp := runTestHandshake(t)
|
||||||
|
ci, err := newConnectionStateFromResult(targetInit)
|
||||||
|
require.NoError(t, err)
|
||||||
|
relayCI, err := newConnectionStateFromResult(relayInit)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
targetAddr := netip.MustParseAddr("10.0.0.2")
|
||||||
|
relayAddr := netip.MustParseAddr("10.0.0.3")
|
||||||
|
relayRemote := netip.MustParseAddrPort("192.0.2.1:4242")
|
||||||
|
|
||||||
|
relay := &Relay{Type: TerminalType, State: Established, LocalIndex: 5, RemoteIndex: 6, PeerAddr: targetAddr}
|
||||||
|
relayHI := &HostInfo{
|
||||||
|
vpnAddrs: []netip.Addr{relayAddr},
|
||||||
|
ConnectionState: relayCI,
|
||||||
|
relayState: RelayState{
|
||||||
|
relayForByAddr: map[netip.Addr]*Relay{targetAddr: relay},
|
||||||
|
relayForByIdx: map[uint32]*Relay{relay.LocalIndex: relay},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
relayHI.remote.Store(&relayRemote)
|
||||||
|
|
||||||
|
// No remote of its own, so the send has to go via the relay
|
||||||
|
hostinfo := &HostInfo{
|
||||||
|
vpnAddrs: []netip.Addr{targetAddr},
|
||||||
|
ConnectionState: ci,
|
||||||
|
remoteIndexId: 7,
|
||||||
|
relayState: RelayState{
|
||||||
|
relays: []netip.Addr{relayAddr},
|
||||||
|
relayForByAddr: map[netip.Addr]*Relay{},
|
||||||
|
relayForByIdx: map[uint32]*Relay{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
l := test.NewLogger()
|
||||||
|
hm := newHostMap(l)
|
||||||
|
hm.Hosts[relayAddr] = relayHI
|
||||||
|
conn := &capturingConn{}
|
||||||
|
f := &Interface{
|
||||||
|
l: l,
|
||||||
|
hostMap: hm,
|
||||||
|
messageMetrics: &MessageMetrics{txExhausted: metrics.NewCounter()},
|
||||||
|
writers: []udp.Conn{conn},
|
||||||
|
}
|
||||||
|
f.connectionManager = &connectionManager{intf: f, relayUsed: map[uint32]struct{}{}, relayUsedLock: &sync.RWMutex{}}
|
||||||
|
|
||||||
|
payload := make([]byte, tt.n)
|
||||||
|
for i := range payload {
|
||||||
|
payload[i] = byte(i)
|
||||||
|
}
|
||||||
|
counter := ci.messageCounter.Load()
|
||||||
|
f.sendNoMetrics(header.Message, 0, ci, hostinfo, netip.AddrPort{}, payload, make([]byte, 12), make([]byte, mtu), 0)
|
||||||
|
|
||||||
|
if !tt.sent {
|
||||||
|
assert.Empty(t, conn.writes)
|
||||||
|
assert.Equal(t, counter, ci.messageCounter.Load(), "a dropped packet must not spend a counter")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
require.Len(t, conn.writes, 1)
|
||||||
|
assert.Equal(t, relayRemote, conn.addrs[0])
|
||||||
|
pkt := conn.writes[0]
|
||||||
|
|
||||||
|
// The relay authenticates the whole packet under its own header
|
||||||
|
outer := &header.H{}
|
||||||
|
require.NoError(t, outer.Parse(pkt))
|
||||||
|
assert.Equal(t, header.MessageRelay, outer.Subtype)
|
||||||
|
assert.Equal(t, relay.RemoteIndex, outer.RemoteIndex)
|
||||||
|
relayPeer, err := newConnectionStateFromResult(relayResp)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, relayPeer.VerifyRelay(l, outer.MessageCounter, pkt, make([]byte, 12)))
|
||||||
|
|
||||||
|
// Inside it, the target decrypts the end to end packet back to the payload
|
||||||
|
inner := pkt[header.Len : len(pkt)-relayPeer.dKey.Overhead()]
|
||||||
|
h := &header.H{}
|
||||||
|
require.NoError(t, h.Parse(inner))
|
||||||
|
assert.Equal(t, header.Message, h.Type)
|
||||||
|
assert.Equal(t, hostinfo.remoteIndexId, h.RemoteIndex)
|
||||||
|
targetPeer, err := newConnectionStateFromResult(targetResp)
|
||||||
|
require.NoError(t, err)
|
||||||
|
got, err := targetPeer.Decrypt(l, h.MessageCounter, inner, make([]byte, 12))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, payload, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewConnectionStateFromResult(t *testing.T) {
|
func TestNewConnectionStateFromResult(t *testing.T) {
|
||||||
initR, respR := runTestHandshake(t)
|
initR, respR := runTestHandshake(t)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
|
||||||
|
package cpupick
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Avoid removes cpus from the affinity of every thread of the process, for use at startup. New threads inherit
|
||||||
|
// their parent's mask, so it repeats until a pass finds nothing left to change; a thread that exits meanwhile is
|
||||||
|
// skipped. Every mask is checked before any is changed. With apply false it only validates, for configuration tests.
|
||||||
|
// This affects the entire process, including other users of an embedded Nebula. Not wired up yet: tun.avoid_cpus
|
||||||
|
// only steers Default for now.
|
||||||
|
func Avoid(cpus []int, apply bool) error {
|
||||||
|
const capacity = int(unsafe.Sizeof(unix.CPUSet{})) * 8
|
||||||
|
for _, cpu := range cpus {
|
||||||
|
if cpu < 0 || cpu >= capacity {
|
||||||
|
return fmt.Errorf("CPU %d is out of range", cpu)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(cpus) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
masks, err := avoidingMasks(cpus)
|
||||||
|
if err != nil || !apply || len(masks) == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for tid, mask := range masks {
|
||||||
|
if err := unix.SchedSetaffinity(tid, &mask); err != nil && !errors.Is(err, unix.ESRCH) {
|
||||||
|
return fmt.Errorf("set thread %d affinity: %w", tid, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// avoidingMasks returns the mask without cpus of every thread whose mask has any of them.
|
||||||
|
func avoidingMasks(cpus []int) (map[int]unix.CPUSet, error) {
|
||||||
|
entries, err := os.ReadDir("/proc/self/task")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
masks := make(map[int]unix.CPUSet, len(entries))
|
||||||
|
for _, entry := range entries {
|
||||||
|
tid, err := strconv.Atoi(entry.Name())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var mask unix.CPUSet
|
||||||
|
err = unix.SchedGetaffinity(tid, &mask)
|
||||||
|
if errors.Is(err, unix.ESRCH) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read thread %d affinity: %w", tid, err)
|
||||||
|
}
|
||||||
|
next := mask
|
||||||
|
for _, cpu := range cpus {
|
||||||
|
next.Clear(cpu)
|
||||||
|
}
|
||||||
|
if next.Count() == 0 {
|
||||||
|
return nil, fmt.Errorf("avoid_cpus leaves thread %d with no allowed CPU", tid)
|
||||||
|
}
|
||||||
|
if next != mask {
|
||||||
|
masks[tid] = next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return masks, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
|
||||||
|
package cpupick
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/util"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestAvoid changes the affinity of the whole process, so it runs in a child, leaving the test runner untouched.
|
||||||
|
func TestAvoid(t *testing.T) {
|
||||||
|
if os.Getenv("NEBULA_CPUPICK_AVOID_TEST") != "1" {
|
||||||
|
exe, err := os.Executable()
|
||||||
|
require.NoError(t, err)
|
||||||
|
cmd := exec.Command(exe, "-test.run=^TestAvoid$", "-test.v")
|
||||||
|
cmd.Env = append(os.Environ(), "NEBULA_CPUPICK_AVOID_TEST=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, "%s", out)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
allowed, err := util.AllowedCPUs()
|
||||||
|
require.NoError(t, err)
|
||||||
|
if len(allowed) < 2 {
|
||||||
|
t.Skip("requires at least two allowed CPUs")
|
||||||
|
}
|
||||||
|
cpu := allowed[0]
|
||||||
|
release := make(chan struct{})
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
// lockThreads occupies 8 OS threads until release, so the runtime has to start more
|
||||||
|
lockThreads := func() {
|
||||||
|
var ready sync.WaitGroup
|
||||||
|
for range 8 {
|
||||||
|
ready.Add(1)
|
||||||
|
wg.Go(func() {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
ready.Done()
|
||||||
|
<-release
|
||||||
|
})
|
||||||
|
}
|
||||||
|
ready.Wait()
|
||||||
|
}
|
||||||
|
lockThreads()
|
||||||
|
|
||||||
|
require.NoError(t, Avoid([]int{cpu}, false))
|
||||||
|
after, err := util.AllowedCPUs()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, allowed, after, "configuration check must not mutate affinity")
|
||||||
|
require.Error(t, Avoid(allowed, true), "must reject an empty mask before applying")
|
||||||
|
require.NoError(t, Avoid([]int{cpu, cpu}, true))
|
||||||
|
requireAvoided(t, cpu, allowed)
|
||||||
|
// Existing threads stay locked while new ones inherit the restriction
|
||||||
|
lockThreads()
|
||||||
|
requireAvoided(t, cpu, allowed)
|
||||||
|
close(release)
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// requireAvoided checks no thread may run on cpu, and every thread still may on some CPU of allowed only.
|
||||||
|
func requireAvoided(t *testing.T, cpu int, allowed []int) {
|
||||||
|
t.Helper()
|
||||||
|
entries, err := os.ReadDir("/proc/self/task")
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, entry := range entries {
|
||||||
|
tid, err := strconv.Atoi(entry.Name())
|
||||||
|
require.NoError(t, err)
|
||||||
|
var mask unix.CPUSet
|
||||||
|
err = unix.SchedGetaffinity(tid, &mask)
|
||||||
|
if err == unix.ESRCH {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, mask.IsSet(cpu), "thread %d", tid)
|
||||||
|
require.Positive(t, mask.Count())
|
||||||
|
for id := range len(mask) * 64 {
|
||||||
|
if mask.IsSet(id) {
|
||||||
|
require.Contains(t, allowed, id, "must not widen the inherited mask")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAvoidRange checks the bounds of unix.CPUSet, whose words are 32 bits wide on 32-bit platforms.
|
||||||
|
func TestAvoidRange(t *testing.T) {
|
||||||
|
capacity := int(unsafe.Sizeof(unix.CPUSet{})) * 8
|
||||||
|
require.NoError(t, Avoid([]int{capacity - 1}, false))
|
||||||
|
require.EqualError(t, Avoid([]int{capacity}, false), fmt.Sprintf("CPU %d is out of range", capacity))
|
||||||
|
require.EqualError(t, Avoid([]int{-1}, false), "CPU -1 is out of range")
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
//go:build !linux || android || e2e_testing
|
||||||
|
|
||||||
|
package cpupick
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// Avoid is unsupported outside Linux: there is no thread affinity API to restrict.
|
||||||
|
func Avoid(_ []int, _ bool) error { return errors.ErrUnsupported }
|
||||||
+17
-6
@@ -16,10 +16,15 @@
|
|||||||
// across distinct physical cores when the topology permits, CPU 0's physical
|
// across distinct physical cores when the topology permits, CPU 0's physical
|
||||||
// core demoted to last resort, and the order rotated by a stable per-instance
|
// core demoted to last resort, and the order rotated by a stable per-instance
|
||||||
// key so co-located instances spread instead of stacking.
|
// key so co-located instances spread instead of stacking.
|
||||||
|
//
|
||||||
|
// CPUs the operator asked to keep readers off (tun.avoid_cpus, typically the
|
||||||
|
// underlay NIC's receive IRQ CPUs) are removed from the allowed set before
|
||||||
|
// any of that, so no rule can bring one back.
|
||||||
package cpupick
|
package cpupick
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
)
|
)
|
||||||
@@ -57,12 +62,17 @@ func flatTopology(cpus []int) topology {
|
|||||||
// Default computes the pin order for `routines` tun readers. key is any
|
// Default computes the pin order for `routines` tun readers. key is any
|
||||||
// stable per-instance value; the bound UDP port is ideal — distinct across
|
// stable per-instance value; the bound UDP port is ideal — distinct across
|
||||||
// co-located instances, stable across restarts so benchmark runs stay
|
// co-located instances, stable across restarts so benchmark runs stay
|
||||||
// comparable. Returns nil when there is nothing useful to say (no affinity
|
// comparable. avoid lists CPUs never to return, matched literally. Returns
|
||||||
// support on this platform, lookup failure); callers keep their existing
|
// nil when there is nothing useful to say (no affinity support on this
|
||||||
// fallback spread.
|
// platform, lookup failure, avoid covering every allowed CPU); callers keep
|
||||||
func Default(routines int, key uint64, l *slog.Logger) []int {
|
// their existing fallback spread.
|
||||||
|
func Default(routines int, key uint64, avoid []int, l *slog.Logger) []int {
|
||||||
allowed, err := util.AllowedCPUs()
|
allowed, err := util.AllowedCPUs()
|
||||||
if err != nil || len(allowed) == 0 {
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
allowed = slices.DeleteFunc(allowed, func(c int) bool { return slices.Contains(avoid, c) })
|
||||||
|
if len(allowed) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
perf, signal := perfCPUs(allowed)
|
perf, signal := perfCPUs(allowed)
|
||||||
@@ -77,7 +87,8 @@ func Default(routines int, key uint64, l *slog.Logger) []int {
|
|||||||
if l != nil {
|
if l != nil {
|
||||||
l.Info("chose default pin CPUs for tun readers",
|
l.Info("chose default pin CPUs for tun readers",
|
||||||
"cpus", cpus[:min(routines, len(cpus))],
|
"cpus", cpus[:min(routines, len(cpus))],
|
||||||
"perfSignal", signal)
|
"perfSignal", signal,
|
||||||
|
"avoided", avoid)
|
||||||
}
|
}
|
||||||
return cpus
|
return cpus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
|
||||||
|
package cpupick
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/util"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDefaultSkipsAvoided(t *testing.T) {
|
||||||
|
allowed, err := util.AllowedCPUs()
|
||||||
|
require.NoError(t, err)
|
||||||
|
if len(allowed) < 2 {
|
||||||
|
t.Skip("requires at least two allowed CPUs")
|
||||||
|
}
|
||||||
|
avoid := allowed[:len(allowed)/2]
|
||||||
|
for key := range uint64(16) {
|
||||||
|
got := Default(len(allowed), key, avoid, nil)
|
||||||
|
require.NotEmpty(t, got)
|
||||||
|
for _, cpu := range got {
|
||||||
|
assert.NotContains(t, avoid, cpu, "key %d", key)
|
||||||
|
assert.Contains(t, allowed, cpu, "key %d", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Nil(t, Default(2, 0, allowed, nil), "avoiding everything leaves nothing to suggest")
|
||||||
|
|
||||||
|
// Without avoid every allowed CPU stays in play
|
||||||
|
got := Default(len(allowed), 0, nil, nil)
|
||||||
|
slices.Sort(got)
|
||||||
|
assert.Equal(t, allowed, got)
|
||||||
|
}
|
||||||
+15
-5
@@ -135,12 +135,12 @@ listen:
|
|||||||
# superpacket, so the receive scratch is batch * 64KiB per listening socket (~4MiB per routine at the
|
# superpacket, so the receive scratch is batch * 64KiB per listening socket (~4MiB per routine at the
|
||||||
# default of 64). Lower this to trade peak per-syscall throughput for memory on constrained hosts.
|
# default of 64). Lower this to trade peak per-syscall throughput for memory on constrained hosts.
|
||||||
#batch: 64
|
#batch: 64
|
||||||
# Configure socket buffers for the udp side (outside), leave unset to use the system defaults. Values will be doubled by the kernel
|
# Linux only. Configure socket buffers for the udp side (outside), leave unset to use the system defaults. Values will be doubled by the kernel
|
||||||
# Default is net.core.rmem_default and net.core.wmem_default (/proc/sys/net/core/rmem_default and /proc/sys/net/core/rmem_default)
|
# Default is net.core.rmem_default and net.core.wmem_default (/proc/sys/net/core/rmem_default and /proc/sys/net/core/rmem_default)
|
||||||
# Maximum is limited by memory in the system, SO_RCVBUFFORCE and SO_SNDBUFFORCE is used to avoid having to raise the system wide
|
# Maximum is limited by memory in the system, SO_RCVBUFFORCE and SO_SNDBUFFORCE is used to avoid having to raise the system wide
|
||||||
# max, net.core.rmem_max and net.core.wmem_max
|
# max, net.core.rmem_max and net.core.wmem_max
|
||||||
#read_buffer: 10485760
|
read_buffer: 3412512
|
||||||
#write_buffer: 10485760
|
write_buffer: 3412512
|
||||||
|
|
||||||
# On Windows only
|
# On Windows only
|
||||||
# When true, Nebula installs a WFP (Windows Filtering Platform) PERMIT filter scoped to UDP at the listener port.
|
# When true, Nebula installs a WFP (Windows Filtering Platform) PERMIT filter scoped to UDP at the listener port.
|
||||||
@@ -262,9 +262,10 @@ tun:
|
|||||||
drop_local_broadcast: false
|
drop_local_broadcast: false
|
||||||
# Toggles forwarding of multicast packets
|
# Toggles forwarding of multicast packets
|
||||||
drop_multicast: false
|
drop_multicast: false
|
||||||
# Sets the transmit queue length, if you notice lots of transmit drops on the tun it may help to raise this number. Default is 500
|
# Sets the transmit queue length, if you notice lots of transmit drops on the tun it may help to raise this number. Default is 5000
|
||||||
tx_queue: 500
|
tx_queue: 5000
|
||||||
# Default MTU for every packet, safe setting is (and the default) 1300 for internet based traffic
|
# Default MTU for every packet, safe setting is (and the default) 1300 for internet based traffic
|
||||||
|
# Anything above 8937, here or on a route below, is capped to 8937 with a warning
|
||||||
mtu: 1300
|
mtu: 1300
|
||||||
|
|
||||||
# the use_offloads setting controls if Nebula will attempt to enable GSO and GRO for the tun device. Linux only, not reloadable.
|
# the use_offloads setting controls if Nebula will attempt to enable GSO and GRO for the tun device. Linux only, not reloadable.
|
||||||
@@ -294,6 +295,15 @@ tun:
|
|||||||
# - 2
|
# - 2
|
||||||
# - 4
|
# - 4
|
||||||
|
|
||||||
|
# Linux only. avoid_cpus lists CPU IDs the tun readers are never pinned to, exactly as listed (SMT siblings are not
|
||||||
|
# added for you). The default pin selection skips them, and a cpu_affinity entry naming one is rejected like any
|
||||||
|
# other invalid entry. Other threads are not restricted, and the setting does nothing with pin_threads: false. An
|
||||||
|
# invalid list, or one covering every allowed CPU, is ignored with a warning. Not reloadable. To keep Nebula off the
|
||||||
|
# underlay NIC's receive IRQ CPUs, find them in /proc/interrupts and /proc/irq/<IRQ>/effective_affinity_list, add
|
||||||
|
# their SMT siblings from /sys/devices/system/cpu/cpu<N>/topology/thread_siblings_list if you want those kept clear
|
||||||
|
# too, and revisit the list when IRQ placement changes.
|
||||||
|
#avoid_cpus: []
|
||||||
|
|
||||||
# Route based MTU overrides, you have known vpn ip paths that can support larger MTUs you can increase/decrease them here
|
# Route based MTU overrides, you have known vpn ip paths that can support larger MTUs you can increase/decrease them here
|
||||||
routes:
|
routes:
|
||||||
#- mtu: 8800
|
#- mtu: 8800
|
||||||
|
|||||||
+17
-65
@@ -967,54 +967,33 @@ func convertRule(l *slog.Logger, p any, table string, i int) (rule, error) {
|
|||||||
r.CAName = toString("ca_name", m)
|
r.CAName = toString("ca_name", m)
|
||||||
r.CASha = toString("ca_sha", m)
|
r.CASha = toString("ca_sha", m)
|
||||||
|
|
||||||
// A null value, `group:` with nothing after it, is the same as no group
|
// Make sure group isn't an array
|
||||||
var singleGroup string
|
if v, ok := m["group"].([]any); ok {
|
||||||
if g, ok := m["group"]; ok && g != nil {
|
if len(v) > 1 {
|
||||||
// Make sure group isn't an array
|
return r, errors.New("group should contain a single value, an array with more than one entry was provided")
|
||||||
v, isArray := g.([]any)
|
|
||||||
if isArray {
|
|
||||||
if len(v) > 1 {
|
|
||||||
return r, errors.New("group should contain a single value, an array with more than one entry was provided")
|
|
||||||
}
|
|
||||||
if len(v) == 0 {
|
|
||||||
return r, errors.New("group should contain a single value, an empty array was provided")
|
|
||||||
}
|
|
||||||
g = v[0]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
l.Warn("group was an array with a single value, converting to simple value",
|
||||||
singleGroup, err = groupName(l, g, table, i)
|
"table", table,
|
||||||
if err != nil {
|
"rule", i,
|
||||||
return r, fmt.Errorf("group should contain a single value, %w", err)
|
)
|
||||||
}
|
m["group"] = v[0]
|
||||||
|
|
||||||
if isArray {
|
|
||||||
l.Warn("group was an array with a single value, converting to simple value",
|
|
||||||
"table", table,
|
|
||||||
"rule", i,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A null value, `groups:` with nothing after it, is the same as no groups
|
singleGroup := toString("group", m)
|
||||||
if rg, ok := m["groups"]; ok && rg != nil {
|
|
||||||
|
if rg, ok := m["groups"]; ok {
|
||||||
switch reflect.TypeOf(rg).Kind() {
|
switch reflect.TypeOf(rg).Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
v := reflect.ValueOf(rg)
|
v := reflect.ValueOf(rg)
|
||||||
r.Groups = make([]string, v.Len())
|
r.Groups = make([]string, v.Len())
|
||||||
for gi := 0; gi < v.Len(); gi++ {
|
for i := 0; i < v.Len(); i++ {
|
||||||
name, err := groupName(l, v.Index(gi).Interface(), table, i)
|
r.Groups[i] = v.Index(i).Interface().(string)
|
||||||
if err != nil {
|
|
||||||
return r, fmt.Errorf("groups entry #%d should contain a single value, %w", gi, err)
|
|
||||||
}
|
|
||||||
r.Groups[gi] = name
|
|
||||||
}
|
}
|
||||||
|
case reflect.String:
|
||||||
|
r.Groups = []string{rg.(string)}
|
||||||
default:
|
default:
|
||||||
name, err := groupName(l, rg, table, i)
|
r.Groups = []string{fmt.Sprintf("%v", rg)}
|
||||||
if err != nil {
|
|
||||||
return r, fmt.Errorf("groups should contain an array or a single value, %w", err)
|
|
||||||
}
|
|
||||||
r.Groups = []string{name}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1030,33 +1009,6 @@ func convertRule(l *slog.Logger, p any, table string, i int) (rule, error) {
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// groupName reads a group name from config. YAML has already parsed an unquoted value by the time we see it, so `007`
|
|
||||||
// arrives as 7, `1.10` as 1.1, and a date as a time.Time, and the text that was written is gone. Strings are used as
|
|
||||||
// is. Ints and bools are converted so `group: 7` keeps working, with a warning to quote them. Anything else would
|
|
||||||
// become a name that may not match what was written, or matches nothing like "<nil>" or "map[a:b]", so it is rejected.
|
|
||||||
func groupName(l *slog.Logger, v any, table string, i int) (string, error) {
|
|
||||||
if v == nil {
|
|
||||||
return "", errors.New("a null was provided")
|
|
||||||
}
|
|
||||||
switch reflect.TypeOf(v).Kind() {
|
|
||||||
case reflect.String:
|
|
||||||
return reflect.ValueOf(v).String(), nil
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
||||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Bool:
|
|
||||||
l.Warn("group was not a string and should be quoted to match as written",
|
|
||||||
"table", table,
|
|
||||||
"rule", i,
|
|
||||||
"group", v,
|
|
||||||
)
|
|
||||||
return fmt.Sprintf("%v", v), nil
|
|
||||||
case reflect.Slice, reflect.Array:
|
|
||||||
return "", errors.New("an array was provided")
|
|
||||||
case reflect.Map:
|
|
||||||
return "", errors.New("a map was provided")
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("%v was read as a %T, quote it to use it as a group name", v, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// sanity returns an error if the rule would be evaluated in a way that would short-circuit a configured check on a wildcard value
|
// sanity returns an error if the rule would be evaluated in a way that would short-circuit a configured check on a wildcard value
|
||||||
// rules are evaluated as "port AND proto AND (ca_sha OR ca_name) AND (host OR group OR groups OR cidr) AND local_cidr"
|
// rules are evaluated as "port AND proto AND (ca_sha OR ca_name) AND (host OR group OR groups OR cidr) AND local_cidr"
|
||||||
func (r *rule) sanity() error {
|
func (r *rule) sanity() error {
|
||||||
|
|||||||
@@ -1439,56 +1439,6 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|||||||
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
||||||
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall)
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall)
|
||||||
|
|
||||||
// Test group and groups values that used to crash or become a group that matches nothing: an empty array,
|
|
||||||
// a null, an array, or a map is rejected
|
|
||||||
badGroups := []struct {
|
|
||||||
key string
|
|
||||||
value any
|
|
||||||
err string
|
|
||||||
}{
|
|
||||||
{"group", []any{}, "group should contain a single value, an empty array was provided"},
|
|
||||||
{"group", []any{nil}, "group should contain a single value, a null was provided"},
|
|
||||||
{"group", []any{[]any{"a"}}, "group should contain a single value, an array was provided"},
|
|
||||||
{"group", map[string]any{"a": "b"}, "group should contain a single value, a map was provided"},
|
|
||||||
{"groups", []any{nil}, "groups entry #0 should contain a single value, a null was provided"},
|
|
||||||
{"groups", []any{"a", []any{"b"}}, "groups entry #1 should contain a single value, an array was provided"},
|
|
||||||
{"groups", []any{map[string]any{"a": "b"}}, "groups entry #0 should contain a single value, a map was provided"},
|
|
||||||
{"groups", map[string]any{"a": "b"}, "groups should contain an array or a single value, a map was provided"},
|
|
||||||
{"group", 1.1, "group should contain a single value, 1.1 was read as a float64, quote it to use it as a group name"},
|
|
||||||
{"group", []any{1.1}, "group should contain a single value, 1.1 was read as a float64, quote it to use it as a group name"},
|
|
||||||
{"groups", []any{"a", 1.1}, "groups entry #1 should contain a single value, 1.1 was read as a float64, quote it to use it as a group name"},
|
|
||||||
{"groups", time.Date(2001, 12, 14, 0, 0, 0, 0, time.UTC), "groups should contain an array or a single value, 2001-12-14 00:00:00 +0000 UTC was read as a time.Time, quote it to use it as a group name"},
|
|
||||||
}
|
|
||||||
for _, tc := range badGroups {
|
|
||||||
conf = config.NewC(test.NewLogger())
|
|
||||||
mf = &mockFirewall{}
|
|
||||||
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", tc.key: tc.value}}}
|
|
||||||
require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; "+tc.err, "%s: %v", tc.key, tc.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test int and bool groups entries are read as strings
|
|
||||||
conf = config.NewC(test.NewLogger())
|
|
||||||
mf = &mockFirewall{}
|
|
||||||
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []any{1, true}}}}
|
|
||||||
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
||||||
assert.Equal(t, []string{"1", "true"}, mf.lastCall.groups)
|
|
||||||
|
|
||||||
conf = config.NewC(test.NewLogger())
|
|
||||||
mf = &mockFirewall{}
|
|
||||||
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": 1}}}
|
|
||||||
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
||||||
assert.Equal(t, []string{"1"}, mf.lastCall.groups)
|
|
||||||
|
|
||||||
// Test a null group or groups is the same as not providing it
|
|
||||||
for _, key := range []string{"group", "groups"} {
|
|
||||||
conf = config.NewC(test.NewLogger())
|
|
||||||
mf = &mockFirewall{}
|
|
||||||
conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", key: nil, "host": "a"}}}
|
|
||||||
require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
||||||
assert.Empty(t, mf.lastCall.groups)
|
|
||||||
assert.Equal(t, "a", mf.lastCall.host)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Add error
|
// Test Add error
|
||||||
conf = config.NewC(test.NewLogger())
|
conf = config.NewC(test.NewLogger())
|
||||||
mf = &mockFirewall{}
|
mf = &mockFirewall{}
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ type m = map[string]any
|
|||||||
const (
|
const (
|
||||||
Version uint8 = 1
|
Version uint8 = 1
|
||||||
Len = 16
|
Len = 16
|
||||||
|
|
||||||
|
// MaxOverhead is the most a payload grows on its way to the underlay.
|
||||||
|
// A relayed packet has two headers and two AEAD tags, one for the inner tunnel and another for the relay tunnel.
|
||||||
|
// Every supported cipher (today) has a 16 byte tag.
|
||||||
|
MaxOverhead = 2 * (Len + 16)
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageType uint8
|
type MessageType uint8
|
||||||
|
|||||||
@@ -199,8 +199,8 @@ func (f *Interface) sendInsideMessage(hostinfo *HostInfo, pkt tio.Packet, nb []b
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = tio.SegmentSuperpacket(pkt, func(seg []byte) error {
|
err = tio.SegmentSuperpacket(pkt, func(seg []byte) error {
|
||||||
//relay header + header + plaintext + AEAD tag (16 bytes for both AES-GCM and ChaCha20-Poly1305) + relay tag
|
//relay header + header + plaintext + AEAD tag + relay tag
|
||||||
scratch := sendBatch.Reserve(header.Len + header.Len + len(seg) + 16 + 16)
|
scratch := sendBatch.Reserve(len(seg) + header.MaxOverhead)
|
||||||
|
|
||||||
innerPacket := f.sendInsideEncrypt(hostinfo, ci, seg, scratch[header.Len:], nb)
|
innerPacket := f.sendInsideEncrypt(hostinfo, ci, seg, scratch[header.Len:], nb)
|
||||||
if innerPacket == nil {
|
if innerPacket == nil {
|
||||||
@@ -529,16 +529,26 @@ func (f *Interface) sendNoMetrics(t header.MessageType, st header.MessageSubType
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
useRelay := !remote.IsValid() && !hostinfo.GetRemote().IsValid()
|
useRelay := !remote.IsValid() && !hostinfo.GetRemote().IsValid()
|
||||||
fullOut := out
|
|
||||||
|
|
||||||
|
// The encrypt appends to out in place and a relayed packet is then wrapped in place,
|
||||||
|
// so out has to fit the whole packet up front or the encrypt reallocates out from under SendVia.
|
||||||
|
// We cap the MTU of tun devices and routes we create ourselves, so only a host that set a bigger MTU than we allow gets here.
|
||||||
|
need := header.Len + len(p) + ci.eKey.Overhead()
|
||||||
if useRelay {
|
if useRelay {
|
||||||
if len(out) < header.Len {
|
// The relay tunnel runs the same cipher, and adds its own header and tag
|
||||||
// out always has a capacity of mtu, but not always a length greater than the header.Len.
|
need += header.Len + ci.eKey.Overhead()
|
||||||
// Grow it to make sure the next operation works.
|
}
|
||||||
out = out[:header.Len]
|
if cap(out) < need {
|
||||||
|
if l, ok := logging.RateLimited(f.l, f.txOversizeLimiter); ok {
|
||||||
|
hostinfo.logger(l).Error("Dropping outbound packet, out buffer not large enough", "outCap", cap(out), "payloadLen", len(p), "relayed", useRelay)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fullOut := out
|
||||||
|
if useRelay {
|
||||||
// Save a header's worth of data at the front of the 'out' buffer.
|
// Save a header's worth of data at the front of the 'out' buffer.
|
||||||
out = out[header.Len:]
|
out = out[header.Len:header.Len]
|
||||||
}
|
}
|
||||||
|
|
||||||
if noiseutil.EncryptLockNeeded {
|
if noiseutil.EncryptLockNeeded {
|
||||||
|
|||||||
+15
-1
@@ -60,6 +60,9 @@ type InterfaceConfig struct {
|
|||||||
// shorter lists than `routines` cycle. Empty list keeps the default
|
// shorter lists than `routines` cycle. Empty list keeps the default
|
||||||
// pin-to-(i % NumCPU) behavior. Only consulted when PinThreads is true.
|
// pin-to-(i % NumCPU) behavior. Only consulted when PinThreads is true.
|
||||||
CpuAffinity []int
|
CpuAffinity []int
|
||||||
|
// AvoidCPUs (tun.avoid_cpus) lists CPUs the default pin spread skips when
|
||||||
|
// CpuAffinity is empty. Only consulted when PinThreads is true.
|
||||||
|
AvoidCPUs []int
|
||||||
// PinThreads controls whether each TUN reader OS thread is pinned to a
|
// PinThreads controls whether each TUN reader OS thread is pinned to a
|
||||||
// single CPU (via tun.pin_threads, default true). Pinning keeps each
|
// single CPU (via tun.pin_threads, default true). Pinning keeps each
|
||||||
// goroutine's sendmmsg on one XPS-selected NIC TX ring so per-flow
|
// goroutine's sendmmsg on one XPS-selected NIC TX ring so per-flow
|
||||||
@@ -95,6 +98,8 @@ type Interface struct {
|
|||||||
// Empty falls back to the default pin-to-(allowed CPU) behavior.
|
// Empty falls back to the default pin-to-(allowed CPU) behavior.
|
||||||
// Only consulted when pinThreads is true.
|
// Only consulted when pinThreads is true.
|
||||||
cpuAffinity []int
|
cpuAffinity []int
|
||||||
|
// avoidCPUs are removed from that fallback spread, unless they would empty it.
|
||||||
|
avoidCPUs []int
|
||||||
// pinThreads controls whether listenIn pins each TUN reader OS thread to
|
// pinThreads controls whether listenIn pins each TUN reader OS thread to
|
||||||
// a CPU at all (tun.pin_threads, default true). When false, threads are
|
// a CPU at all (tun.pin_threads, default true). When false, threads are
|
||||||
// left free to migrate as on stock nebula.
|
// left free to migrate as on stock nebula.
|
||||||
@@ -127,6 +132,7 @@ type Interface struct {
|
|||||||
|
|
||||||
udpTxErrLimiter *ratelimit.Limiter // udp direct transmit error log limiter
|
udpTxErrLimiter *ratelimit.Limiter // udp direct transmit error log limiter
|
||||||
udpRelayTxErrLimiter *ratelimit.Limiter // udp relay transmit error log limiter
|
udpRelayTxErrLimiter *ratelimit.Limiter // udp relay transmit error log limiter
|
||||||
|
txOversizeLimiter *ratelimit.Limiter // oversized outbound packet drop log limiter
|
||||||
|
|
||||||
// fatalErr holds the first unexpected reader error that caused shutdown.
|
// fatalErr holds the first unexpected reader error that caused shutdown.
|
||||||
// nil means "no fatal error" (yet)
|
// nil means "no fatal error" (yet)
|
||||||
@@ -210,6 +216,10 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
txOversizeLimiter, err := ratelimit.New(1, time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if c.routines <= 1 {
|
if c.routines <= 1 {
|
||||||
c.PinThreads = false //pinning is not useful unless there's more than one tun reader
|
c.PinThreads = false //pinning is not useful unless there's more than one tun reader
|
||||||
@@ -242,9 +252,11 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
connectionManager: c.connectionManager,
|
connectionManager: c.connectionManager,
|
||||||
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
||||||
cpuAffinity: c.CpuAffinity,
|
cpuAffinity: c.CpuAffinity,
|
||||||
|
avoidCPUs: c.AvoidCPUs,
|
||||||
pinThreads: c.PinThreads,
|
pinThreads: c.PinThreads,
|
||||||
udpTxErrLimiter: udpTxErrLimiter,
|
udpTxErrLimiter: udpTxErrLimiter,
|
||||||
udpRelayTxErrLimiter: udpRelayTxErrLimiter,
|
udpRelayTxErrLimiter: udpRelayTxErrLimiter,
|
||||||
|
txOversizeLimiter: txOversizeLimiter,
|
||||||
|
|
||||||
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
metricHandshakes: metrics.GetOrRegisterHistogram("handshakes", nil, metrics.NewExpDecaySample(1028, 0.015)),
|
||||||
metricTxDropped: metrics.GetOrRegisterCounter("udp.tx.dropped", nil),
|
metricTxDropped: metrics.GetOrRegisterCounter("udp.tx.dropped", nil),
|
||||||
@@ -431,12 +443,14 @@ func (f *Interface) pinThisThread(i int) {
|
|||||||
var cpu int
|
var cpu int
|
||||||
if n := len(f.cpuAffinity); n > 0 {
|
if n := len(f.cpuAffinity); n > 0 {
|
||||||
// Explicit tun.cpu_affinity list wins; parseCpuAffinity already
|
// Explicit tun.cpu_affinity list wins; parseCpuAffinity already
|
||||||
// validated the entries against the allowed CPU set.
|
// validated the entries against the allowed CPU set and tun.avoid_cpus.
|
||||||
cpu = f.cpuAffinity[i%n]
|
cpu = f.cpuAffinity[i%n]
|
||||||
} else if allowed, err := util.AllowedCPUs(); err == nil && len(allowed) > 0 {
|
} else if allowed, err := util.AllowedCPUs(); err == nil && len(allowed) > 0 {
|
||||||
// Default: spread queues across the CPUs we're actually allowed to
|
// Default: spread queues across the CPUs we're actually allowed to
|
||||||
// run on. Under a cpuset/taskset mask these aren't 0..NumCPU-1, so
|
// run on. Under a cpuset/taskset mask these aren't 0..NumCPU-1, so
|
||||||
// i % NumCPU would pick unrunnable IDs and every pin would fail.
|
// i % NumCPU would pick unrunnable IDs and every pin would fail.
|
||||||
|
// parseAvoidCPUs already dropped an avoid list that covers them all.
|
||||||
|
allowed = slices.DeleteFunc(allowed, func(c int) bool { return slices.Contains(f.avoidCPUs, c) })
|
||||||
cpu = allowed[i%len(allowed)]
|
cpu = allowed[i%len(allowed)]
|
||||||
} else {
|
} else {
|
||||||
cpu = i % runtime.NumCPU()
|
cpu = i % runtime.NumCPU()
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/slackhq/nebula/util"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -118,3 +119,22 @@ func TestReloadFirewall_NoChange(t *testing.T) {
|
|||||||
|
|
||||||
assert.Same(t, fw, f.firewall, "firewall should not have been replaced")
|
assert.Same(t, fw, f.firewall, "firewall should not have been replaced")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestPinThisThreadFallbackSkipsAvoided covers the single reader, which never goes through cpupick: its fallback
|
||||||
|
// spread over the allowed set must still skip tun.avoid_cpus.
|
||||||
|
func TestPinThisThreadFallbackSkipsAvoided(t *testing.T) {
|
||||||
|
allowed, err := util.AllowedCPUs()
|
||||||
|
require.NoError(t, err)
|
||||||
|
if len(allowed) < 2 {
|
||||||
|
t.Skip("requires at least two allowed CPUs")
|
||||||
|
}
|
||||||
|
f := &Interface{l: test.NewLogger(), pinThreads: true, avoidCPUs: []int{allowed[0]}}
|
||||||
|
got := make(chan []int)
|
||||||
|
go func() {
|
||||||
|
// The pin leaves this goroutine locked to its thread, so the runtime discards the thread when it returns.
|
||||||
|
f.pinThisThread(0)
|
||||||
|
cpus, _ := util.AllowedCPUs()
|
||||||
|
got <- cpus
|
||||||
|
}()
|
||||||
|
assert.Equal(t, []int{allowed[1]}, <-got)
|
||||||
|
}
|
||||||
@@ -243,11 +243,19 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
}
|
}
|
||||||
|
|
||||||
pinThreads := c.GetBool("tun.pin_threads", true)
|
pinThreads := c.GetBool("tun.pin_threads", true)
|
||||||
cpuAffinity := parseCpuAffinity(c, l, routines)
|
avoidCPUs := parseAvoidCPUs(c, l)
|
||||||
|
if len(avoidCPUs) > 0 && !pinThreads {
|
||||||
|
l.Warn("tun.avoid_cpus has no effect with tun.pin_threads disabled")
|
||||||
|
}
|
||||||
|
// Keeping every thread of the process off avoidCPUs, not just the pinned readers, is parked for now:
|
||||||
|
//if err := cpupick.Avoid(avoidCPUs, !configTest); err != nil && !errors.Is(err, errors.ErrUnsupported) {
|
||||||
|
// return nil, fmt.Errorf("tun.avoid_cpus: %w", err)
|
||||||
|
//}
|
||||||
|
cpuAffinity := parseCpuAffinity(c, l, routines, avoidCPUs)
|
||||||
if pinThreads && routines > 1 && len(cpuAffinity) == 0 && !configTest {
|
if pinThreads && routines > 1 && len(cpuAffinity) == 0 && !configTest {
|
||||||
// The operator didn't choose pin CPUs, so pick a default set that
|
// The operator didn't choose pin CPUs, so pick a default set that
|
||||||
// prefers performance cores and doesn't stack co-located instances
|
// prefers performance cores, skips tun.avoid_cpus and doesn't stack
|
||||||
// onto allowed[0].
|
// co-located instances onto allowed[0].
|
||||||
|
|
||||||
// key is used to seed the spreading of routines->cores.
|
// key is used to seed the spreading of routines->cores.
|
||||||
// use PID if you want to ensure many different Nebulas in VMs or containers land on different cores
|
// use PID if you want to ensure many different Nebulas in VMs or containers land on different cores
|
||||||
@@ -270,7 +278,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
l.Warn("tun.pin_threads_key is invalid, using PID")
|
l.Warn("tun.pin_threads_key is invalid, using PID")
|
||||||
}
|
}
|
||||||
|
|
||||||
cpuAffinity = cpupick.Default(routines, key, l)
|
cpuAffinity = cpupick.Default(routines, key, avoidCPUs, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
ifConfig := &InterfaceConfig{
|
ifConfig := &InterfaceConfig{
|
||||||
@@ -295,6 +303,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
punchy: punchy,
|
punchy: punchy,
|
||||||
ConntrackCacheTimeout: conntrackCacheTimeout,
|
ConntrackCacheTimeout: conntrackCacheTimeout,
|
||||||
CpuAffinity: cpuAffinity,
|
CpuAffinity: cpuAffinity,
|
||||||
|
AvoidCPUs: avoidCPUs,
|
||||||
PinThreads: pinThreads,
|
PinThreads: pinThreads,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
@@ -363,8 +372,10 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
// mask (util.AllowedCPUs) rather than 0..NumCPU-1: under a cgroup cpuset or
|
// mask (util.AllowedCPUs) rather than 0..NumCPU-1: under a cgroup cpuset or
|
||||||
// taskset the runnable IDs are frequently not that contiguous range, and
|
// taskset the runnable IDs are frequently not that contiguous range, and
|
||||||
// pinning to an unrunnable ID always fails. If the allowed set can't be
|
// pinning to an unrunnable ID always fails. If the allowed set can't be
|
||||||
// determined we fall back to a plain non-negative check.
|
// determined we fall back to a plain non-negative check. An entry listed in
|
||||||
func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
// avoid (tun.avoid_cpus) contradicts the operator's own config and is treated
|
||||||
|
// the same as any other invalid entry.
|
||||||
|
func parseCpuAffinity(c *config.C, l *slog.Logger, routines int, avoid []int) []int {
|
||||||
raw := c.Get("tun.cpu_affinity")
|
raw := c.Get("tun.cpu_affinity")
|
||||||
if raw == nil {
|
if raw == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -407,6 +418,11 @@ func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
|||||||
"index", i, "cpu", cpu, "allowed", allowed)
|
"index", i, "cpu", cpu, "allowed", allowed)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if slices.Contains(avoid, cpu) {
|
||||||
|
l.Warn("tun.cpu_affinity entry is listed in tun.avoid_cpus; ignoring affinity",
|
||||||
|
"index", i, "cpu", cpu)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
cpus = append(cpus, cpu)
|
cpus = append(cpus, cpu)
|
||||||
}
|
}
|
||||||
if len(cpus) != routines {
|
if len(cpus) != routines {
|
||||||
@@ -416,6 +432,36 @@ func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
|||||||
return cpus
|
return cpus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseAvoidCPUs reads tun.avoid_cpus, a list of CPU IDs the tun readers must not be pinned to. IDs outside the
|
||||||
|
// allowed set are accepted, since one config may serve differently shaped hosts. Like tun.cpu_affinity, an invalid
|
||||||
|
// list is a warning and is ignored entirely, as is one covering every allowed CPU, which would leave nowhere to pin.
|
||||||
|
func parseAvoidCPUs(c *config.C, l *slog.Logger) []int {
|
||||||
|
raw := c.Get("tun.avoid_cpus")
|
||||||
|
if raw == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
values, ok := raw.([]any)
|
||||||
|
if !ok {
|
||||||
|
l.Warn("tun.avoid_cpus must be a list of integers; ignoring", "value", raw)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cpus := make([]int, 0, len(values))
|
||||||
|
for i, value := range values {
|
||||||
|
cpu, ok := value.(int)
|
||||||
|
if !ok || cpu < 0 {
|
||||||
|
l.Warn("tun.avoid_cpus entry not a non-negative integer; ignoring avoid_cpus", "index", i, "value", value)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cpus = append(cpus, cpu)
|
||||||
|
}
|
||||||
|
if allowed, err := util.AllowedCPUs(); err == nil && len(allowed) > 0 &&
|
||||||
|
!slices.ContainsFunc(allowed, func(cpu int) bool { return !slices.Contains(cpus, cpu) }) {
|
||||||
|
l.Warn("tun.avoid_cpus covers every allowed CPU; ignoring avoid_cpus", "cpus", cpus, "allowed", allowed)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return cpus
|
||||||
|
}
|
||||||
|
|
||||||
func moduleVersion() string {
|
func moduleVersion() string {
|
||||||
info, ok := debug.ReadBuildInfo()
|
info, ok := debug.ReadBuildInfo()
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
+36
-5
@@ -32,7 +32,7 @@ func TestParseCpuAffinity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// unset -> nil (listenIn falls back to spreading across the allowed set)
|
// unset -> nil (listenIn falls back to spreading across the allowed set)
|
||||||
assert.Nil(t, parseCpuAffinity(newConfig(nil), l, 1))
|
assert.Nil(t, parseCpuAffinity(newConfig(nil), l, 1, nil))
|
||||||
|
|
||||||
// Pick a CPU we're actually allowed to run on so a valid list survives
|
// Pick a CPU we're actually allowed to run on so a valid list survives
|
||||||
// validation regardless of the host's affinity mask.
|
// validation regardless of the host's affinity mask.
|
||||||
@@ -43,19 +43,50 @@ func TestParseCpuAffinity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// valid list -> parsed through unchanged
|
// valid list -> parsed through unchanged
|
||||||
assert.Equal(t, []int{validCPU, validCPU}, parseCpuAffinity(newConfig([]any{validCPU, validCPU}), l, 2))
|
assert.Equal(t, []int{validCPU, validCPU}, parseCpuAffinity(newConfig([]any{validCPU, validCPU}), l, 2, nil))
|
||||||
|
|
||||||
// a negative entry is out of range on every platform -> disables the override
|
// a negative entry is out of range on every platform -> disables the override
|
||||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, -1}), l, 2))
|
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, -1}), l, 2, nil))
|
||||||
|
|
||||||
// a non-integer entry -> disables the override
|
// a non-integer entry -> disables the override
|
||||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, "not-a-cpu"}), l, 2))
|
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, "not-a-cpu"}), l, 2, nil))
|
||||||
|
|
||||||
// a CPU id outside the allowed set -> disables the override. Only assertable
|
// a CPU id outside the allowed set -> disables the override. Only assertable
|
||||||
// where we can enumerate the allowed set (e.g. linux); 1<<20 is far beyond
|
// where we can enumerate the allowed set (e.g. linux); 1<<20 is far beyond
|
||||||
// any representable CPU id so it can never be in the mask.
|
// any representable CPU id so it can never be in the mask.
|
||||||
if len(allowed) > 0 {
|
if len(allowed) > 0 {
|
||||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1))
|
assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1, nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// an entry also listed in tun.avoid_cpus -> disables the override
|
||||||
|
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU}), l, 1, []int{validCPU}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseAvoidCPUs(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
for _, tc := range []struct {
|
||||||
|
value any
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{nil, nil}, {[]any{}, []int{}}, {[]any{1, 1 << 20}, []int{1, 1 << 20}},
|
||||||
|
{"auto", nil}, {"1,2", nil}, {[]any{-1}, nil}, {[]any{1.5}, nil}, {[]any{1, "2"}, nil},
|
||||||
|
} {
|
||||||
|
c := config.NewC(l)
|
||||||
|
if tc.value != nil {
|
||||||
|
c.Settings["tun"] = map[string]any{"avoid_cpus": tc.value}
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.want, parseAvoidCPUs(c, l), "%v", tc.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// avoiding every allowed CPU leaves nowhere to pin; only checkable where the allowed set is known
|
||||||
|
if allowed, _ := util.AllowedCPUs(); len(allowed) > 0 {
|
||||||
|
all := make([]any, len(allowed))
|
||||||
|
for i, cpu := range allowed {
|
||||||
|
all[i] = cpu
|
||||||
|
}
|
||||||
|
c := config.NewC(l)
|
||||||
|
c.Settings["tun"] = map[string]any{"avoid_cpus": all}
|
||||||
|
assert.Nil(t, parseAvoidCPUs(c, l))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -160,9 +160,7 @@ func (f *Interface) readOutsidePackets(via ViaSender, packet []byte, rxc *rxCont
|
|||||||
case header.TestReply:
|
case header.TestReply:
|
||||||
// No-op, useful for the Roaming and connectionManager side-effects above
|
// No-op, useful for the Roaming and connectionManager side-effects above
|
||||||
case header.TestRequest:
|
case header.TestRequest:
|
||||||
const maxCipherOverhead = 16 //todo we use this too often, needs a real importable const
|
if header.MaxOverhead+len(out) > len(rxc.scratch) {
|
||||||
const maxOverhead = header.Len + header.Len + maxCipherOverhead + maxCipherOverhead
|
|
||||||
if maxOverhead+len(out) > len(rxc.scratch) {
|
|
||||||
// A reply that cannot fit in scratch is dropped no matter the log level.
|
// A reply that cannot fit in scratch is dropped no matter the log level.
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
hostinfo.logger(f.l).Debug("dropping oversized test request", "payloadLen", len(out), "from", via)
|
hostinfo.logger(f.l).Debug("dropping oversized test request", "payloadLen", len(out), "from", via)
|
||||||
|
|||||||
+28
-4
@@ -50,7 +50,13 @@ func (r Route) String() string {
|
|||||||
|
|
||||||
func makeRouteTree(l *slog.Logger, routes []Route, allowMTU bool) (*bart.Table[routing.Gateways], error) {
|
func makeRouteTree(l *slog.Logger, routes []Route, allowMTU bool) (*bart.Table[routing.Gateways], error) {
|
||||||
routeTree := new(bart.Table[routing.Gateways])
|
routeTree := new(bart.Table[routing.Gateways])
|
||||||
|
seen := make(map[netip.Prefix]bool, len(routes))
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
|
if seen[r.Cidr] {
|
||||||
|
l.Warn("route is listed more than once, only the last entry's via is used", "route", r)
|
||||||
|
}
|
||||||
|
seen[r.Cidr] = true
|
||||||
|
|
||||||
if !allowMTU && r.MTU > 0 {
|
if !allowMTU && r.MTU > 0 {
|
||||||
l.Warn("route MTU is not supported on this platform",
|
l.Warn("route MTU is not supported on this platform",
|
||||||
"goos", runtime.GOOS,
|
"goos", runtime.GOOS,
|
||||||
@@ -98,7 +104,11 @@ func parseRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) {
|
|||||||
|
|
||||||
mtu, ok := rMtu.(int)
|
mtu, ok := rMtu.(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
mtu, err = strconv.Atoi(rMtu.(string))
|
s, ok := rMtu.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not an integer: %v", i+1, rMtu)
|
||||||
|
}
|
||||||
|
mtu, err = strconv.Atoi(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not an integer: %v", i+1, err)
|
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not an integer: %v", i+1, err)
|
||||||
}
|
}
|
||||||
@@ -174,7 +184,11 @@ func parseUnsafeRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) {
|
|||||||
if rMtu, ok := m["mtu"]; ok {
|
if rMtu, ok := m["mtu"]; ok {
|
||||||
mtu, ok = rMtu.(int)
|
mtu, ok = rMtu.(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
mtu, err = strconv.Atoi(rMtu.(string))
|
s, ok := rMtu.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is not an integer: %v", i+1, rMtu)
|
||||||
|
}
|
||||||
|
mtu, err = strconv.Atoi(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is not an integer: %v", i+1, err)
|
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is not an integer: %v", i+1, err)
|
||||||
}
|
}
|
||||||
@@ -192,10 +206,15 @@ func parseUnsafeRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) {
|
|||||||
|
|
||||||
metric, ok := rMetric.(int)
|
metric, ok := rMetric.(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
_, err = strconv.ParseInt(rMetric.(string), 10, 32)
|
s, ok := rMetric.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not an integer: %v", i+1, rMetric)
|
||||||
|
}
|
||||||
|
n, err := strconv.ParseInt(s, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not an integer: %v", i+1, err)
|
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not an integer: %v", i+1, err)
|
||||||
}
|
}
|
||||||
|
metric = int(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
if metric < 0 || metric > math.MaxInt32 {
|
if metric < 0 || metric > math.MaxInt32 {
|
||||||
@@ -248,10 +267,15 @@ func parseUnsafeRoutes(c *config.C, networks []netip.Prefix) ([]Route, error) {
|
|||||||
|
|
||||||
gatewayWeight, ok := rGatewayWeight.(int)
|
gatewayWeight, ok := rGatewayWeight.(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
_, err = strconv.ParseInt(rGatewayWeight.(string), 10, 32)
|
s, ok := rGatewayWeight.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("entry .weight in tun.unsafe_routes[%v].via[%v] is not an integer", i+1, ig+1)
|
||||||
|
}
|
||||||
|
w, err := strconv.ParseInt(s, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("entry .weight in tun.unsafe_routes[%v].via[%v] is not an integer", i+1, ig+1)
|
return nil, fmt.Errorf("entry .weight in tun.unsafe_routes[%v].via[%v] is not an integer", i+1, ig+1)
|
||||||
}
|
}
|
||||||
|
gatewayWeight = int(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gatewayWeight < 1 || gatewayWeight > math.MaxInt32 {
|
if gatewayWeight < 1 || gatewayWeight > math.MaxInt32 {
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package overlay
|
package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -402,3 +406,115 @@ func Test_makeMultipathUnsafeRouteTree(t *testing.T) {
|
|||||||
routing.CalculateBucketsForGateways(expectedGateways)
|
routing.CalculateBucketsForGateways(expectedGateways)
|
||||||
assert.ElementsMatch(t, expectedGateways, r)
|
assert.ElementsMatch(t, expectedGateways, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_getMTU(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
c := config.NewC(l)
|
||||||
|
|
||||||
|
// unset
|
||||||
|
assert.Equal(t, DefaultMTU, getMTU(l, c))
|
||||||
|
|
||||||
|
// the largest a relayed packet can carry in one underlay buffer
|
||||||
|
require.Equal(t, udp.MTU, MaxMTU+header.MaxOverhead)
|
||||||
|
c.Settings["tun"] = map[string]any{"mtu": MaxMTU}
|
||||||
|
assert.Equal(t, MaxMTU, getMTU(l, c))
|
||||||
|
|
||||||
|
// too big is capped
|
||||||
|
c.Settings["tun"] = map[string]any{"mtu": 9000}
|
||||||
|
assert.Equal(t, MaxMTU, getMTU(l, c))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_getAllRoutesFromConfigCapsMTU(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
c := config.NewC(l)
|
||||||
|
c.Settings["tun"] = map[string]any{
|
||||||
|
"routes": []any{
|
||||||
|
map[string]any{"mtu": "9000", "route": "10.0.0.0/29"},
|
||||||
|
map[string]any{"mtu": "1300", "route": "10.0.0.8/29"},
|
||||||
|
},
|
||||||
|
"unsafe_routes": []any{
|
||||||
|
map[string]any{"via": "127.0.0.1", "mtu": "9000", "route": "1.0.0.0/29"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
changed, routes, err := getAllRoutesFromConfig(l, c, []netip.Prefix{netip.MustParsePrefix("10.0.0.0/24")}, true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, changed)
|
||||||
|
|
||||||
|
mtus := map[string]int{}
|
||||||
|
for _, r := range routes {
|
||||||
|
mtus[r.Cidr.String()] = r.MTU
|
||||||
|
}
|
||||||
|
assert.Equal(t, map[string]int{"10.0.0.0/29": MaxMTU, "10.0.0.8/29": 1300, "1.0.0.0/29": MaxMTU}, mtus)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The route tree keeps one entry per CIDR, so a CIDR listed twice only routes by the last one. Say so instead of
|
||||||
|
// letting the earlier via, or on Windows the earlier metric, quietly lose
|
||||||
|
func Test_makeRouteTree_WarnsOnDuplicate(t *testing.T) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
l := test.NewLoggerWithOutput(buf)
|
||||||
|
via := func(s string) routing.Gateways {
|
||||||
|
return routing.Gateways{routing.NewGateway(netip.MustParseAddr(s), 1)}
|
||||||
|
}
|
||||||
|
cidr := netip.MustParsePrefix("10.251.1.0/24")
|
||||||
|
|
||||||
|
_, err := makeRouteTree(l, []Route{
|
||||||
|
{Cidr: cidr, Via: via("10.250.0.2"), Install: true, Metric: 100},
|
||||||
|
{Cidr: netip.MustParsePrefix("10.251.2.0/24"), Via: via("10.250.0.2"), Install: true},
|
||||||
|
}, false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, buf.String())
|
||||||
|
|
||||||
|
tree, err := makeRouteTree(l, []Route{
|
||||||
|
{Cidr: cidr, Via: via("10.250.0.2"), Install: true, Metric: 100},
|
||||||
|
{Cidr: cidr, Via: via("10.250.0.3"), Install: true, Metric: 200},
|
||||||
|
}, false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, strings.Count(buf.String(), "route is listed more than once, only the last entry's via is used"))
|
||||||
|
gw, ok := tree.Lookup(netip.MustParseAddr("10.251.1.1"))
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, netip.MustParseAddr("10.250.0.3"), gw[0].Addr(), "the last entry wins")
|
||||||
|
}
|
||||||
|
|
||||||
|
// A quoted metric or weight is a number written as a string, it means that number. Anything else is an error, not a
|
||||||
|
// panic
|
||||||
|
func Test_parseUnsafeRoutes_QuotedNumbers(t *testing.T) {
|
||||||
|
c := config.NewC(test.NewLogger())
|
||||||
|
n := netip.MustParsePrefix("10.0.0.1/24")
|
||||||
|
route := func(metric, weight any) {
|
||||||
|
c.Settings["tun"] = map[string]any{"unsafe_routes": []any{map[string]any{
|
||||||
|
"route": "1.0.0.0/8",
|
||||||
|
"metric": metric,
|
||||||
|
"via": []any{map[string]any{"gateway": "10.0.0.2", "weight": weight}},
|
||||||
|
}}}
|
||||||
|
}
|
||||||
|
|
||||||
|
route("100", "5")
|
||||||
|
routes, err := parseUnsafeRoutes(c, []netip.Prefix{n})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, routes, 1)
|
||||||
|
assert.Equal(t, 100, routes[0].Metric)
|
||||||
|
assert.Equal(t, "{addr: 10.0.0.2, weight: 5}", routes[0].Via[0].String())
|
||||||
|
|
||||||
|
route(1.5, 1)
|
||||||
|
_, err = parseUnsafeRoutes(c, []netip.Prefix{n})
|
||||||
|
require.EqualError(t, err, "entry 1.metric in tun.unsafe_routes is not an integer: 1.5")
|
||||||
|
|
||||||
|
route(1, true)
|
||||||
|
_, err = parseUnsafeRoutes(c, []netip.Prefix{n})
|
||||||
|
require.EqualError(t, err, "entry .weight in tun.unsafe_routes[1].via[1] is not an integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
// An mtu that is neither a number nor a quoted one is an error, not a panic
|
||||||
|
func Test_parseRoutes_MTUNotANumber(t *testing.T) {
|
||||||
|
c := config.NewC(test.NewLogger())
|
||||||
|
n := netip.MustParsePrefix("10.0.0.1/24")
|
||||||
|
|
||||||
|
c.Settings["tun"] = map[string]any{"routes": []any{map[string]any{"route": "1.0.0.0/8", "mtu": 1500.5}}}
|
||||||
|
_, err := parseRoutes(c, []netip.Prefix{n})
|
||||||
|
require.EqualError(t, err, "entry 1.mtu in tun.routes is not an integer: 1500.5")
|
||||||
|
|
||||||
|
c.Settings["tun"] = map[string]any{"unsafe_routes": []any{map[string]any{"route": "1.0.0.0/8", "via": "10.0.0.2", "mtu": 1500.5}}}
|
||||||
|
_, err = parseUnsafeRoutes(c, []netip.Prefix{n})
|
||||||
|
require.EqualError(t, err, "entry 1.mtu in tun.unsafe_routes is not an integer: 1500.5")
|
||||||
|
}
|
||||||
+24
-2
@@ -7,11 +7,27 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
"github.com/slackhq/nebula/util"
|
"github.com/slackhq/nebula/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultMTU = 1300
|
const DefaultMTU = 1300
|
||||||
|
|
||||||
|
// MaxMTU is the largest tun.mtu, or route mtu, the underlay can carry.
|
||||||
|
// Every underlay buffer is udp.MTU bytes, and a relayed packet adds header.MaxOverhead to its plaintext.
|
||||||
|
const MaxMTU = udp.MTU - header.MaxOverhead
|
||||||
|
|
||||||
|
// getMTU reads tun.mtu, capping a value too big for the underlay to carry.
|
||||||
|
func getMTU(l *slog.Logger, c *config.C) int {
|
||||||
|
mtu := c.GetInt("tun.mtu", DefaultMTU)
|
||||||
|
if mtu > MaxMTU {
|
||||||
|
l.Warn("tun.mtu is too big for the underlay to carry, capping it", "mtu", mtu, "maxMTU", MaxMTU)
|
||||||
|
return MaxMTU
|
||||||
|
}
|
||||||
|
return mtu
|
||||||
|
}
|
||||||
|
|
||||||
type NameError struct {
|
type NameError struct {
|
||||||
Name string
|
Name string
|
||||||
Underlying error
|
Underlying error
|
||||||
@@ -27,7 +43,7 @@ type DeviceFactory func(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix,
|
|||||||
func NewDeviceFromConfig(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) {
|
func NewDeviceFromConfig(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) {
|
||||||
switch {
|
switch {
|
||||||
case c.GetBool("tun.disabled", false):
|
case c.GetBool("tun.disabled", false):
|
||||||
tun := newDisabledTun(vpnNetworks, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l)
|
tun := newDisabledTun(vpnNetworks, c.GetInt("tun.tx_queue", 5000), c.GetBool("stats.message_metrics", false), l)
|
||||||
return tun, nil
|
return tun, nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -41,7 +57,7 @@ func NewFdDeviceFromConfig(fd *int) DeviceFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllRoutesFromConfig(c *config.C, vpnNetworks []netip.Prefix, initial bool) (bool, []Route, error) {
|
func getAllRoutesFromConfig(l *slog.Logger, c *config.C, vpnNetworks []netip.Prefix, initial bool) (bool, []Route, error) {
|
||||||
if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
|
if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
|
||||||
return false, nil, nil
|
return false, nil, nil
|
||||||
}
|
}
|
||||||
@@ -57,6 +73,12 @@ func getAllRoutesFromConfig(c *config.C, vpnNetworks []netip.Prefix, initial boo
|
|||||||
}
|
}
|
||||||
|
|
||||||
routes = append(routes, unsafeRoutes...)
|
routes = append(routes, unsafeRoutes...)
|
||||||
|
for i, r := range routes {
|
||||||
|
if r.MTU > MaxMTU {
|
||||||
|
l.Warn("route mtu is too big for the underlay to carry, capping it", "route", r.Cidr, "mtu", r.MTU, "maxMTU", MaxMTU)
|
||||||
|
routes[i].MTU = MaxMTU
|
||||||
|
}
|
||||||
|
}
|
||||||
return true, routes, nil
|
return true, routes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (t *tun) Activate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+101
-17
@@ -6,8 +6,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"math/bits"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -28,7 +30,7 @@ type tun struct {
|
|||||||
DefaultMTU int
|
DefaultMTU int
|
||||||
Routes atomic.Pointer[[]Route]
|
Routes atomic.Pointer[[]Route]
|
||||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||||
linkAddr *netroute.LinkAddr
|
linkAddr atomic.Pointer[netroute.LinkAddr]
|
||||||
// hostOwned means the fd arrived from the OS, which has already configured addressing, mtu
|
// hostOwned means the fd arrived from the OS, which has already configured addressing, mtu
|
||||||
// and routes for it. NEPacketTunnelProvider on darwin does this.
|
// and routes for it. NEPacketTunnelProvider on darwin does this.
|
||||||
hostOwned bool
|
hostOwned bool
|
||||||
@@ -126,7 +128,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
f: os.NewFile(uintptr(fd), ""),
|
f: os.NewFile(uintptr(fd), ""),
|
||||||
Device: name,
|
Device: name,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
DefaultMTU: c.GetInt("tun.mtu", DefaultMTU),
|
DefaultMTU: getMTU(l, c),
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +169,7 @@ func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip
|
|||||||
f: file,
|
f: file,
|
||||||
Device: utunNameFromFd(deviceFd),
|
Device: utunNameFromFd(deviceFd),
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
DefaultMTU: c.GetInt("tun.mtu", DefaultMTU),
|
DefaultMTU: getMTU(l, c),
|
||||||
hostOwned: true,
|
hostOwned: true,
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
@@ -243,7 +245,7 @@ func (t *tun) Activate() error {
|
|||||||
if linkAddr == nil {
|
if linkAddr == nil {
|
||||||
return fmt.Errorf("unable to discover link_addr for tun interface")
|
return fmt.Errorf("unable to discover link_addr for tun interface")
|
||||||
}
|
}
|
||||||
t.linkAddr = linkAddr
|
t.linkAddr.Store(linkAddr)
|
||||||
|
|
||||||
for _, network := range t.vpnNetworks {
|
for _, network := range t.vpnNetworks {
|
||||||
if network.Addr().Is4() {
|
if network.Addr().Is4() {
|
||||||
@@ -269,6 +271,7 @@ func (t *tun) Activate() error {
|
|||||||
return t.addRoutes(false)
|
return t.addRoutes(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// activate4 assigns our vpn address as a /32 and routes the vpn network to the tun explicitly.
|
||||||
func (t *tun) activate4(network netip.Prefix) error {
|
func (t *tun) activate4(network netip.Prefix) error {
|
||||||
s, err := unix.Socket(
|
s, err := unix.Socket(
|
||||||
unix.AF_INET,
|
unix.AF_INET,
|
||||||
@@ -295,22 +298,32 @@ func (t *tun) activate4(network netip.Prefix) error {
|
|||||||
MaskAddr: unix.RawSockaddrInet4{
|
MaskAddr: unix.RawSockaddrInet4{
|
||||||
Len: unix.SizeofSockaddrInet4,
|
Len: unix.SizeofSockaddrInet4,
|
||||||
Family: unix.AF_INET,
|
Family: unix.AF_INET,
|
||||||
Addr: prefixToMask(network).As4(),
|
Addr: [4]byte{255, 255, 255, 255}, //always make the address a /32
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On Darwin, VPNs don't edit the routing table directly.
|
||||||
|
// A network extension hands its routes to the system, and configd installs them.
|
||||||
|
// configd replaces any route already holding the same prefix, including ours.
|
||||||
|
// As an example, Tailscale with an exit node treats any interface prefix wider than one address as a LAN.
|
||||||
|
// With LAN access off, it routes each LAN into Tailscale's utun.
|
||||||
|
// With LAN access on, it excludes each LAN from its tunnel.
|
||||||
|
// configd routes an excluded prefix like its own routes would, ignoring ours, so it goes out the default route.
|
||||||
|
// Either way our route is replaced, and traffic for the vpn network no longer reaches nebula.
|
||||||
|
// A single address isn't a LAN to Tailscale, so our route is left alone.
|
||||||
|
// We route the vpn network to the tun explicitly instead.
|
||||||
|
|
||||||
if err := ioctl(uintptr(s), unix.SIOCAIFADDR, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
if err := ioctl(uintptr(s), unix.SIOCAIFADDR, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
||||||
return fmt.Errorf("failed to set tun v4 address: %s", err)
|
return fmt.Errorf("failed to set tun v4 address: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = addRoute(network, t.linkAddr)
|
if err := addRoute(network, t.linkAddr.Load()); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("failed to add route for %v: %w", network.Masked(), err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// activate6 is activate4 for IPv6
|
||||||
func (t *tun) activate6(network netip.Prefix) error {
|
func (t *tun) activate6(network netip.Prefix) error {
|
||||||
s, err := unix.Socket(
|
s, err := unix.Socket(
|
||||||
unix.AF_INET6,
|
unix.AF_INET6,
|
||||||
@@ -332,25 +345,29 @@ func (t *tun) activate6(network netip.Prefix) error {
|
|||||||
PrefixMask: unix.RawSockaddrInet6{
|
PrefixMask: unix.RawSockaddrInet6{
|
||||||
Len: unix.SizeofSockaddrInet6,
|
Len: unix.SizeofSockaddrInet6,
|
||||||
Family: unix.AF_INET6,
|
Family: unix.AF_INET6,
|
||||||
Addr: prefixToMask(network).As16(),
|
// Always set the netmask to be a /128. See activate4 for why we do this.
|
||||||
|
Addr: [16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
|
||||||
},
|
},
|
||||||
Lifetime: addrLifetime{
|
Lifetime: addrLifetime{
|
||||||
// never expires
|
// never expires
|
||||||
Vltime: 0xffffffff,
|
Vltime: 0xffffffff,
|
||||||
Pltime: 0xffffffff,
|
Pltime: 0xffffffff,
|
||||||
},
|
},
|
||||||
Flags: _IN6_IFF_NODAD,
|
Flags: _IN6_IFF_NODAD, // No DAD, a utun has no neighbors.
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ioctl(uintptr(s), _SIOCAIFADDR_IN6, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
if err := ioctl(uintptr(s), _SIOCAIFADDR_IN6, uintptr(unsafe.Pointer(&ifr))); err != nil {
|
||||||
return fmt.Errorf("failed to set tun address: %s", err)
|
return fmt.Errorf("failed to set tun address: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := addRoute(network, t.linkAddr.Load()); err != nil {
|
||||||
|
return fmt.Errorf("failed to add route for %v: %w", network.Masked(), err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -394,6 +411,56 @@ func (t *tun) RoutesFor(ip netip.Addr) routing.Gateways {
|
|||||||
return routing.Gateways{}
|
return routing.Gateways{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fetchRoutes() ([]netroute.Message, error) {
|
||||||
|
rib, err := netroute.FetchRIB(unix.AF_UNSPEC, unix.NET_RT_DUMP, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return netroute.ParseRIB(unix.NET_RT_DUMP, rib)
|
||||||
|
}
|
||||||
|
|
||||||
|
// routeInterfaceIn returns the index of the interface prefix is routed through, or 0 if msgs has no route for exactly
|
||||||
|
// that prefix. Routes scoped to an interface are skipped, they don't stop us adding ours.
|
||||||
|
func routeInterfaceIn(msgs []netroute.Message, prefix netip.Prefix) int {
|
||||||
|
for _, msg := range msgs {
|
||||||
|
m, ok := msg.(*netroute.RouteMessage)
|
||||||
|
if !ok || len(m.Addrs) <= unix.RTAX_NETMASK || m.Flags&unix.RTF_IFSCOPE != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p, ok := routePrefix(m.Addrs[unix.RTAX_DST], m.Addrs[unix.RTAX_NETMASK]); ok && p == prefix.Masked() {
|
||||||
|
return m.Index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// routePrefix returns the prefix a route covers. Host routes have no netmask.
|
||||||
|
func routePrefix(dst, mask netroute.Addr) (netip.Prefix, bool) {
|
||||||
|
switch d := dst.(type) {
|
||||||
|
case *netroute.Inet4Addr:
|
||||||
|
bits := 32
|
||||||
|
if m, ok := mask.(*netroute.Inet4Addr); ok {
|
||||||
|
bits = maskBits(m.IP[:])
|
||||||
|
}
|
||||||
|
return netip.PrefixFrom(netip.AddrFrom4(d.IP), bits), true
|
||||||
|
case *netroute.Inet6Addr:
|
||||||
|
bits := 128
|
||||||
|
if m, ok := mask.(*netroute.Inet6Addr); ok {
|
||||||
|
bits = maskBits(m.IP[:])
|
||||||
|
}
|
||||||
|
return netip.PrefixFrom(netip.AddrFrom16(d.IP), bits), true
|
||||||
|
}
|
||||||
|
return netip.Prefix{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func maskBits(mask []byte) int {
|
||||||
|
n := 0
|
||||||
|
for _, b := range mask {
|
||||||
|
n += bits.OnesCount8(b)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// Get the LinkAddr for the interface of the given name
|
// Get the LinkAddr for the interface of the given name
|
||||||
// Is there an easier way to fetch this when we create the interface?
|
// Is there an easier way to fetch this when we create the interface?
|
||||||
// Maybe SIOCGIFINDEX? but this doesn't appear to exist in the darwin headers.
|
// Maybe SIOCGIFINDEX? but this doesn't appear to exist in the darwin headers.
|
||||||
@@ -428,7 +495,14 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Activate hasn't run yet, it will install the routes
|
||||||
|
linkAddr := t.linkAddr.Load()
|
||||||
|
if linkAddr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
|
systemRoutes := sync.OnceValues(fetchRoutes)
|
||||||
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if len(r.Via) == 0 || !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
@@ -436,10 +510,16 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := addRoute(r.Cidr, t.linkAddr)
|
err := addRoute(r.Cidr, linkAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, unix.EEXIST) {
|
if errors.Is(err, unix.EEXIST) {
|
||||||
t.l.Warn("unable to add unsafe_route, identical route already exists", "route", r.Cidr)
|
// Every route we added already exists on a reload, only warn about someone else's
|
||||||
|
msgs, err := systemRoutes()
|
||||||
|
if err != nil {
|
||||||
|
t.l.Warn("unable to add unsafe_route, a route already exists but its interface could not be determined", "route", r.Cidr, "error", err)
|
||||||
|
} else if routeInterfaceIn(msgs, r.Cidr) != linkAddr.Index {
|
||||||
|
t.l.Warn("unable to add unsafe_route, the destination is already routed through another interface", "route", r.Cidr)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
if logErrors {
|
if logErrors {
|
||||||
@@ -450,6 +530,8 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.l.Info("Added route", "route", r)
|
t.l.Info("Added route", "route", r)
|
||||||
|
// A cidr listed twice would otherwise find our own route missing from an older fetch
|
||||||
|
systemRoutes = sync.OnceValues(fetchRoutes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,16 +539,18 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
func (t *tun) removeRoutes(routes []Route) error {
|
||||||
if t.hostOwned {
|
linkAddr := t.linkAddr.Load()
|
||||||
|
if t.hostOwned || linkAddr == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
|
// addRoutes never installs these
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := delRoute(r.Cidr, t.linkAddr)
|
err := delRoute(r.Cidr, linkAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.l.Error("Failed to remove route", "error", err, "route", r)
|
t.l.Error("Failed to remove route", "error", err, "route", r)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
//go:build darwin && !ios && !e2e_testing
|
||||||
|
|
||||||
|
package overlay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/routing"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
netroute "golang.org/x/net/route"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Every mac routes loopback through lo0
|
||||||
|
func TestRouteInterfaceIn(t *testing.T) {
|
||||||
|
msgs, err := fetchRoutes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
routeInterface := func(s string) int { return routeInterfaceIn(msgs, netip.MustParsePrefix(s)) }
|
||||||
|
|
||||||
|
lo0, err := net.InterfaceByName("lo0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, lo0.Index, routeInterface("127.0.0.0/8"))
|
||||||
|
assert.Equal(t, lo0.Index, routeInterface("127.0.0.1/32"), "a host route has no netmask")
|
||||||
|
assert.Equal(t, lo0.Index, routeInterface("::1/128"))
|
||||||
|
assert.Equal(t, 0, routeInterface("127.0.0.0/16"), "only an exact prefix counts")
|
||||||
|
|
||||||
|
// Most macs also list the default route scoped to their other interfaces
|
||||||
|
out, err := exec.Command("route", "-n", "get", "default").Output()
|
||||||
|
require.NoError(t, err)
|
||||||
|
var name string
|
||||||
|
for _, line := range strings.Split(string(out), "\n") {
|
||||||
|
if v, ok := strings.CutPrefix(strings.TrimSpace(line), "interface: "); ok {
|
||||||
|
name = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if name == "" {
|
||||||
|
t.Skip("no default route")
|
||||||
|
}
|
||||||
|
def, err := net.InterfaceByName(name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, def.Index, routeInterface("0.0.0.0/0"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouteInterfaceIn_SkipsScopedCopies(t *testing.T) {
|
||||||
|
route := func(index, flags int) netroute.Message {
|
||||||
|
addrs := make([]netroute.Addr, unix.RTAX_MAX)
|
||||||
|
addrs[unix.RTAX_DST] = &netroute.Inet4Addr{IP: [4]byte{192, 168, 1, 0}}
|
||||||
|
addrs[unix.RTAX_NETMASK] = &netroute.Inet4Addr{IP: [4]byte{255, 255, 255, 0}}
|
||||||
|
return &netroute.RouteMessage{Index: index, Flags: flags, Addrs: addrs}
|
||||||
|
}
|
||||||
|
p := netip.MustParsePrefix("192.168.1.0/24")
|
||||||
|
assert.Equal(t, 7, routeInterfaceIn([]netroute.Message{route(4, unix.RTF_UP|unix.RTF_IFSCOPE), route(7, unix.RTF_UP)}, p))
|
||||||
|
assert.Equal(t, 0, routeInterfaceIn([]netroute.Message{route(4, unix.RTF_UP|unix.RTF_IFSCOPE)}, p))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A reload can happen before Activate
|
||||||
|
func TestTunRoutesBeforeActivate(t *testing.T) {
|
||||||
|
tn := &tun{l: test.NewLogger()}
|
||||||
|
routes := []Route{{
|
||||||
|
Cidr: netip.MustParsePrefix("10.253.1.0/24"),
|
||||||
|
Via: routing.Gateways{routing.NewGateway(netip.MustParseAddr("10.250.0.2"), 1)},
|
||||||
|
Install: true,
|
||||||
|
}}
|
||||||
|
tn.Routes.Store(&routes)
|
||||||
|
|
||||||
|
assert.NoError(t, tn.addRoutes(true))
|
||||||
|
assert.NoError(t, tn.removeRoutes(routes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A tun.routes entry with no via is never installed, so removing it must not delete a route for the same cidr that
|
||||||
|
// belongs to someone else
|
||||||
|
func TestTunReloadLeavesRoutesItNeverInstalled(t *testing.T) {
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
t.Skip("creating a utun and adding routes needs root")
|
||||||
|
}
|
||||||
|
lo0, err := net.InterfaceByName("lo0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
cidr := "10.250.0.128/25"
|
||||||
|
require.NoError(t, exec.Command("route", "-q", "-n", "add", "-net", cidr, "-interface", "lo0").Run())
|
||||||
|
t.Cleanup(func() { _ = exec.Command("route", "-q", "-n", "delete", "-net", cidr).Run() })
|
||||||
|
|
||||||
|
l := test.NewLogger()
|
||||||
|
c := config.NewC(l)
|
||||||
|
require.NoError(t, c.LoadString("tun:\n routes:\n - route: "+cidr+"\n mtu: 1300\n"))
|
||||||
|
tn, err := newTun(c, l, []netip.Prefix{netip.MustParsePrefix("10.250.0.1/24")}, false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { _ = tn.Close() })
|
||||||
|
require.NoError(t, tn.Activate())
|
||||||
|
|
||||||
|
require.NoError(t, c.ReloadConfigString("tun: {}\n"))
|
||||||
|
msgs, err := fetchRoutes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, lo0.Index, routeInterfaceIn(msgs, netip.MustParsePrefix(cidr)))
|
||||||
|
}
|
||||||
@@ -370,7 +370,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
t := &tun{
|
t := &tun{
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: getMTU(l, c),
|
||||||
l: l,
|
l: l,
|
||||||
fd: fd,
|
fd: fd,
|
||||||
shutdownR: shutdownR,
|
shutdownR: shutdownR,
|
||||||
@@ -510,7 +510,7 @@ func (t *tun) setMTU() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -589,7 +589,8 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
func (t *tun) removeRoutes(routes []Route) error {
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
|
// addRoutes never installs these
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -68,7 +68,7 @@ func (t *tun) Activate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ func newTunGeneric(c *config.C, l *slog.Logger, fd int, vnetHdr bool, offloadFla
|
|||||||
vnetHdr: vnetHdr,
|
vnetHdr: vnetHdr,
|
||||||
offloadFlags: offloadFlags,
|
offloadFlags: offloadFlags,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
TXQueueLen: c.GetInt("tun.tx_queue", 500),
|
TXQueueLen: c.GetInt("tun.tx_queue", 5000),
|
||||||
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
useSystemRoutes: c.GetBool("tun.use_system_route_table", false),
|
||||||
useSystemRoutesBufferSize: c.GetInt("tun.use_system_route_table_buffer_size", 0),
|
useSystemRoutesBufferSize: c.GetInt("tun.use_system_route_table_buffer_size", 0),
|
||||||
routesFromSystem: map[netip.Prefix]routing.Gateways{},
|
routesFromSystem: map[netip.Prefix]routing.Gateways{},
|
||||||
@@ -244,7 +244,7 @@ func newTunGeneric(c *config.C, l *slog.Logger, fd int, vnetHdr bool, offloadFla
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
routeChange, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
routeChange, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -256,7 +256,7 @@ func (t *tun) reload(c *config.C, initial bool) error {
|
|||||||
// Only the mtu changed. The routes are the same, parse them again so the ones without their own mtu pick up the
|
// Only the mtu changed. The routes are the same, parse them again so the ones without their own mtu pick up the
|
||||||
// new default. Without this an mtu change would remove them all
|
// new default. Without this an mtu change would remove them all
|
||||||
if !routeChange {
|
if !routeChange {
|
||||||
_, routes, err = getAllRoutesFromConfig(c, t.vpnNetworks, true)
|
_, routes, err = getAllRoutesFromConfig(t.l, c, t.vpnNetworks, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -276,7 +276,7 @@ func (t *tun) reload(c *config.C, initial bool) error {
|
|||||||
|
|
||||||
oldDefaultMTU := t.DefaultMTU
|
oldDefaultMTU := t.DefaultMTU
|
||||||
oldMaxMTU := t.MaxMTU
|
oldMaxMTU := t.MaxMTU
|
||||||
newDefaultMTU := c.GetInt("tun.mtu", DefaultMTU)
|
newDefaultMTU := getMTU(t.l, c)
|
||||||
newMaxMTU := newDefaultMTU
|
newMaxMTU := newDefaultMTU
|
||||||
for i, r := range routes {
|
for i, r := range routes {
|
||||||
if r.MTU == 0 {
|
if r.MTU == 0 {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
fd: fd,
|
fd: fd,
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: getMTU(l, c),
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +340,7 @@ func (t *tun) doIoctlByName(ctl uintptr, value uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -420,7 +420,8 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
func (t *tun) removeRoutes(routes []Route) error {
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
|
// addRoutes never installs these
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
fd: fd,
|
fd: fd,
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: getMTU(l, c),
|
||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,7 +319,7 @@ func (t *tun) doIoctlByName(ctl uintptr, value uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) reload(c *config.C, initial bool) error {
|
func (t *tun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -399,7 +399,8 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) error {
|
func (t *tun) removeRoutes(routes []Route) error {
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
|
// addRoutes never installs these
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ type TestTun struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*TestTun, error) {
|
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*TestTun, error) {
|
||||||
_, routes, err := getAllRoutesFromConfig(c, vpnNetworks, true)
|
_, routes, err := getAllRoutesFromConfig(l, c, vpnNetworks, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+77
-6
@@ -4,6 +4,7 @@ package overlay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -75,7 +76,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*w
|
|||||||
t := &winTun{
|
t := &winTun{
|
||||||
Device: deviceName,
|
Device: deviceName,
|
||||||
vpnNetworks: vpnNetworks,
|
vpnNetworks: vpnNetworks,
|
||||||
MTU: c.GetInt("tun.mtu", DefaultMTU),
|
MTU: getMTU(l, c),
|
||||||
guid: *guid,
|
guid: *guid,
|
||||||
networkCategory: cat,
|
networkCategory: cat,
|
||||||
setCategory: setCat,
|
setCategory: setCat,
|
||||||
@@ -118,7 +119,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*w
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *winTun) reload(c *config.C, initial bool) error {
|
func (t *winTun) reload(c *config.C, initial bool) error {
|
||||||
change, routes, err := getAllRoutesFromConfig(c, t.vpnNetworks, initial)
|
change, routes, err := getAllRoutesFromConfig(t.l, c, t.vpnNetworks, initial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -138,7 +139,7 @@ func (t *winTun) reload(c *config.C, initial bool) error {
|
|||||||
|
|
||||||
if !initial {
|
if !initial {
|
||||||
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
// Remove first, if the system removes a wanted route hopefully it will be re-added next
|
||||||
err := t.removeRoutes(findRemovedRoutes(routes, *oldRoutes))
|
err := t.removeRoutes(findRemovedSystemRoutes(routes, *oldRoutes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
util.LogWithContextIfNeeded("Failed to remove routes", err, t.l)
|
||||||
}
|
}
|
||||||
@@ -187,17 +188,27 @@ func (t *winTun) addRoutes(logErrors bool) error {
|
|||||||
foundDefault4 := false
|
foundDefault4 := false
|
||||||
carriesV6 := slices.ContainsFunc(t.vpnNetworks, func(p netip.Prefix) bool { return p.Addr().Is6() })
|
carriesV6 := slices.ContainsFunc(t.vpnNetworks, func(p netip.Prefix) bool { return p.Addr().Is6() })
|
||||||
|
|
||||||
for _, r := range routes {
|
// A CIDR listed twice routes by its last entry, install only that one or each reload would flip between them. The
|
||||||
|
// same rule as systemRoutes
|
||||||
|
last := make(map[netip.Prefix]int, len(routes))
|
||||||
|
for i, r := range routes {
|
||||||
|
last[r.Cidr] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, r := range routes {
|
||||||
if len(r.Via) == 0 || !r.Install {
|
if len(r.Via) == 0 || !r.Install {
|
||||||
// We don't allow route MTUs so only install routes with a via
|
// We don't allow route MTUs so only install routes with a via
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if last[r.Cidr] != i {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// A v6 unsafe_route is legal under a v4-only cert; uninstalled ones put nothing on the adapter.
|
// A v6 unsafe_route is legal under a v4-only cert; uninstalled ones put nothing on the adapter.
|
||||||
carriesV6 = carriesV6 || r.Cidr.Addr().Is6()
|
carriesV6 = carriesV6 || r.Cidr.Addr().Is6()
|
||||||
|
|
||||||
// Add our unsafe route as an on-link route to the nebula tun device.
|
// Add our unsafe route as an on-link route to the nebula tun device.
|
||||||
err := luid.AddRoute(r.Cidr, unspecifiedNextHop(r.Cidr), uint32(r.Metric))
|
result, err := addRoute(luid, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
if logErrors {
|
if logErrors {
|
||||||
@@ -207,7 +218,12 @@ func (t *winTun) addRoutes(logErrors bool) error {
|
|||||||
return retErr
|
return retErr
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.l.Info("Added route", "route", r)
|
switch result {
|
||||||
|
case routeAdded:
|
||||||
|
t.l.Info("Added route", "route", r)
|
||||||
|
case routeMetricUpdated:
|
||||||
|
t.l.Info("Updated route metric", "route", r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !foundDefault4 {
|
if !foundDefault4 {
|
||||||
@@ -319,6 +335,61 @@ func (t *winTun) Close() error {
|
|||||||
return t.tun.Close()
|
return t.tun.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// systemRoutes maps each CIDR to the route that puts it in the system table. A CIDR's last entry decides, including
|
||||||
|
// one that says not to install it
|
||||||
|
func systemRoutes(routes []Route) map[netip.Prefix]Route {
|
||||||
|
out := make(map[netip.Prefix]Route, len(routes))
|
||||||
|
for _, r := range routes {
|
||||||
|
if len(r.Via) > 0 && r.Install {
|
||||||
|
out[r.Cidr] = r
|
||||||
|
} else {
|
||||||
|
delete(out, r.Cidr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// findRemovedSystemRoutes is the routes to delete from the system table. Windows deletes by destination and next hop,
|
||||||
|
// so only a CIDR nothing installs anymore goes, a changed metric is set on the route in place by addRoute
|
||||||
|
func findRemovedSystemRoutes(newRoutes, oldRoutes []Route) []Route {
|
||||||
|
keep := systemRoutes(newRoutes)
|
||||||
|
var removed []Route
|
||||||
|
for _, r := range systemRoutes(oldRoutes) {
|
||||||
|
if _, ok := keep[r.Cidr]; !ok {
|
||||||
|
removed = append(removed, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed
|
||||||
|
}
|
||||||
|
|
||||||
|
type routeResult int
|
||||||
|
|
||||||
|
const (
|
||||||
|
routeAdded routeResult = iota
|
||||||
|
routeMetricUpdated
|
||||||
|
routeAlreadyInstalled
|
||||||
|
)
|
||||||
|
|
||||||
|
// addRoute installs r on the interface. A reload re-adds every route to catch any the system dropped, and Windows
|
||||||
|
// refuses one it has with the same destination and next hop whatever its metric. One that is there with another
|
||||||
|
// metric, ours from before a metric change or someone else's, is brought to ours in place
|
||||||
|
func addRoute(luid winipcfg.LUID, r Route) (routeResult, error) {
|
||||||
|
nextHop := unspecifiedNextHop(r.Cidr)
|
||||||
|
err := luid.AddRoute(r.Cidr, nextHop, uint32(r.Metric))
|
||||||
|
if !errors.Is(err, windows.ERROR_OBJECT_ALREADY_EXISTS) {
|
||||||
|
return routeAdded, err
|
||||||
|
}
|
||||||
|
row, err := luid.Route(r.Cidr, nextHop)
|
||||||
|
if err != nil {
|
||||||
|
return routeAlreadyInstalled, err
|
||||||
|
}
|
||||||
|
if row.Metric != uint32(r.Metric) {
|
||||||
|
row.Metric = uint32(r.Metric)
|
||||||
|
return routeMetricUpdated, row.Set()
|
||||||
|
}
|
||||||
|
return routeAlreadyInstalled, nil
|
||||||
|
}
|
||||||
|
|
||||||
func unspecifiedNextHop(p netip.Prefix) netip.Addr {
|
func unspecifiedNextHop(p netip.Prefix) netip.Addr {
|
||||||
if p.Addr().Is4() {
|
if p.Addr().Is4() {
|
||||||
return netip.IPv4Unspecified()
|
return netip.IPv4Unspecified()
|
||||||
|
|||||||
@@ -0,0 +1,168 @@
|
|||||||
|
//go:build windows && !e2e_testing
|
||||||
|
|
||||||
|
package overlay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
||||||
|
)
|
||||||
|
|
||||||
|
type routeTest struct {
|
||||||
|
c *config.C
|
||||||
|
tun *winTun
|
||||||
|
luid winipcfg.LUID
|
||||||
|
log *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
// newRouteTest makes a wintun device of its own, before installing tun.unsafe_routes from cfg
|
||||||
|
func newRouteTest(t *testing.T, cfg string) *routeTest {
|
||||||
|
t.Helper()
|
||||||
|
if !windows.GetCurrentProcessToken().IsElevated() {
|
||||||
|
t.Skip("creating a wintun device needs an elevated process")
|
||||||
|
}
|
||||||
|
if err := checkWinTunExists(); err != nil {
|
||||||
|
t.Skipf("wintun.dll is not where nebula looks for it: %v", err)
|
||||||
|
}
|
||||||
|
rt := &routeTest{log: &bytes.Buffer{}}
|
||||||
|
l := test.NewLoggerWithOutput(rt.log)
|
||||||
|
rt.c = config.NewC(l)
|
||||||
|
require.NoError(t, rt.c.LoadString(cfg))
|
||||||
|
var err error
|
||||||
|
rt.tun, err = newTun(rt.c, l, []netip.Prefix{netip.MustParsePrefix("10.250.0.1/24")}, false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { _ = rt.tun.Close() })
|
||||||
|
rt.luid = winipcfg.LUID(rt.tun.tun.LUID())
|
||||||
|
return rt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rt *routeTest) metric(t *testing.T, cidr string) uint32 {
|
||||||
|
t.Helper()
|
||||||
|
p := netip.MustParsePrefix(cidr)
|
||||||
|
row, err := rt.luid.Route(p, unspecifiedNextHop(p))
|
||||||
|
require.NoError(t, err)
|
||||||
|
return row.Metric
|
||||||
|
}
|
||||||
|
|
||||||
|
func unsafeRoutes(routes ...string) string {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("tun:\n dev: nebroutetest\n unsafe_routes:\n")
|
||||||
|
// cidr[@metric][!], the ! for install: false
|
||||||
|
for _, r := range routes {
|
||||||
|
r, noInstall := strings.CutSuffix(r, "!")
|
||||||
|
cidr, metric, _ := strings.Cut(r, "@")
|
||||||
|
fmt.Fprintf(&b, " - route: %s\n via: 10.250.0.2\n", cidr)
|
||||||
|
if metric != "" {
|
||||||
|
fmt.Fprintf(&b, " metric: %s\n", metric)
|
||||||
|
}
|
||||||
|
if noInstall {
|
||||||
|
b.WriteString(" install: false\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// A reload that adds a route re-adds the ones already installed, Windows refusing those as already there isn't a failure
|
||||||
|
func TestWinTun_ReloadKeepsInstalledRoutes(t *testing.T) {
|
||||||
|
forFamilies(t, func(t *testing.T, a, b string) {
|
||||||
|
rt := newRouteTest(t, unsafeRoutes(a))
|
||||||
|
require.NoError(t, rt.tun.Activate())
|
||||||
|
|
||||||
|
require.NoError(t, rt.c.ReloadConfigString(unsafeRoutes(a, b)))
|
||||||
|
assert.NotContains(t, rt.log.String(), "Failed to add route")
|
||||||
|
rt.metric(t, a)
|
||||||
|
rt.metric(t, b)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Windows calls a route the same by destination and next hop, so a changed metric is set on the installed route in
|
||||||
|
// place rather than deleted and added, which would leave a moment with no route
|
||||||
|
func TestWinTun_ReloadMetricChangeApplies(t *testing.T) {
|
||||||
|
forFamilies(t, func(t *testing.T, a, b string) {
|
||||||
|
rt := newRouteTest(t, unsafeRoutes(a+"@100"))
|
||||||
|
require.NoError(t, rt.tun.Activate())
|
||||||
|
require.Equal(t, uint32(100), rt.metric(t, a))
|
||||||
|
|
||||||
|
require.NoError(t, rt.c.ReloadConfigString(unsafeRoutes(a+"@200")))
|
||||||
|
assert.NotContains(t, rt.log.String(), "Failed to add route")
|
||||||
|
assert.NotContains(t, rt.log.String(), "Removed route")
|
||||||
|
assert.Equal(t, uint32(200), rt.metric(t, a))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// A route someone else put on the device with another metric is brought to ours, at startup too
|
||||||
|
func TestWinTun_ForeignRouteGetsOurMetric(t *testing.T) {
|
||||||
|
forFamilies(t, func(t *testing.T, a, b string) {
|
||||||
|
rt := newRouteTest(t, unsafeRoutes(a+"@100"))
|
||||||
|
p := netip.MustParsePrefix(a)
|
||||||
|
require.NoError(t, rt.luid.AddRoute(p, unspecifiedNextHop(p), 50))
|
||||||
|
|
||||||
|
require.NoError(t, rt.tun.Activate())
|
||||||
|
assert.Equal(t, uint32(100), rt.metric(t, a))
|
||||||
|
assert.Contains(t, rt.log.String(), "level=INFO msg=\"Updated route metric\"")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// A CIDR listed twice is installed once, by its last entry, and reloads leave it there instead of flipping between the
|
||||||
|
// two metrics
|
||||||
|
func TestWinTun_DuplicateCidrInstallsTheLastEntry(t *testing.T) {
|
||||||
|
forFamilies(t, func(t *testing.T, a, b string) {
|
||||||
|
rt := newRouteTest(t, unsafeRoutes(a+"@100", a+"@200"))
|
||||||
|
require.NoError(t, rt.tun.Activate())
|
||||||
|
assert.Equal(t, uint32(200), rt.metric(t, a))
|
||||||
|
|
||||||
|
require.NoError(t, rt.c.ReloadConfigString(unsafeRoutes(a+"@100", a+"@200", b)))
|
||||||
|
assert.Equal(t, uint32(200), rt.metric(t, a))
|
||||||
|
assert.NotContains(t, rt.log.String(), "Updated route metric")
|
||||||
|
assert.NotContains(t, rt.log.String(), "Failed to add route")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dropping the duplicate that wasn't installed leaves the installed route alone. Windows deletes by destination and
|
||||||
|
// next hop, deleting the dropped one would take the live route with it
|
||||||
|
func TestWinTun_DroppingAnUninstalledDuplicateKeepsTheRoute(t *testing.T) {
|
||||||
|
forFamilies(t, func(t *testing.T, a, b string) {
|
||||||
|
rt := newRouteTest(t, unsafeRoutes(a+"@100", a+"@200"))
|
||||||
|
require.NoError(t, rt.tun.Activate())
|
||||||
|
|
||||||
|
require.NoError(t, rt.c.ReloadConfigString(unsafeRoutes(a+"@200")))
|
||||||
|
assert.NotContains(t, rt.log.String(), "Removed route")
|
||||||
|
assert.Equal(t, uint32(200), rt.metric(t, a))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// A reload ends where a fresh start with the same config would, a CIDR whose last entry says not to install it is
|
||||||
|
// removed
|
||||||
|
func TestWinTun_ReloadMatchesAFreshStart(t *testing.T) {
|
||||||
|
forFamilies(t, func(t *testing.T, a, b string) {
|
||||||
|
cidr := netip.MustParsePrefix(a)
|
||||||
|
routes := unsafeRoutes(a+"@100", a+"@100!")
|
||||||
|
|
||||||
|
fresh := newRouteTest(t, routes)
|
||||||
|
require.NoError(t, fresh.tun.Activate())
|
||||||
|
_, err := fresh.luid.Route(cidr, unspecifiedNextHop(cidr))
|
||||||
|
require.Error(t, err, "a fresh start installed it")
|
||||||
|
require.NoError(t, fresh.tun.Close())
|
||||||
|
|
||||||
|
rt := newRouteTest(t, unsafeRoutes(a+"@100"))
|
||||||
|
require.NoError(t, rt.tun.Activate())
|
||||||
|
require.NoError(t, rt.c.ReloadConfigString(routes))
|
||||||
|
_, err = rt.luid.Route(cidr, unspecifiedNextHop(cidr))
|
||||||
|
assert.Error(t, err, "the reload left it installed")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// forFamilies runs f with two unsafe_routes CIDRs of each address family, the tun's own network stays IPv4
|
||||||
|
func forFamilies(t *testing.T, f func(t *testing.T, a, b string)) {
|
||||||
|
t.Run("ipv4", func(t *testing.T) { f(t, "10.251.1.0/24", "10.251.2.0/24") })
|
||||||
|
t.Run("ipv6", func(t *testing.T) { f(t, "fd00:251:1::/64", "fd00:251:2::/64") })
|
||||||
|
}
|
||||||
@@ -60,6 +60,20 @@ type sshDeviceInfoFlags struct {
|
|||||||
func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
||||||
c.RegisterReloadCallback(func(c *config.C) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
if c.GetBool("sshd.enabled", false) {
|
if c.GetBool("sshd.enabled", false) {
|
||||||
|
// Restarting the server drops every open session, the one that asked for this reload included. Only restart
|
||||||
|
// when it isn't running or its config changed. The host key file can change under the same config, new
|
||||||
|
// handshakes use the new key without a restart
|
||||||
|
if ssh.Running() && !c.HasChanged("sshd") {
|
||||||
|
key, err := loadSSHHostKey(c)
|
||||||
|
if err == nil {
|
||||||
|
err = ssh.SetHostKey(key)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Failed to reconfigure the sshd", "error", err)
|
||||||
|
ssh.Stop()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
sshRun, err := configSSH(l, ssh, c)
|
sshRun, err := configSSH(l, ssh, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Error("Failed to reconfigure the sshd", "error", err)
|
l.Error("Failed to reconfigure the sshd", "error", err)
|
||||||
@@ -74,6 +88,22 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadSSHHostKey returns sshd.host_key, read from its file when it names one.
|
||||||
|
func loadSSHHostKey(c *config.C) ([]byte, error) {
|
||||||
|
hostKeyPathOrKey := c.GetString("sshd.host_key", "")
|
||||||
|
if hostKeyPathOrKey == "" {
|
||||||
|
return nil, fmt.Errorf("sshd.host_key must be provided")
|
||||||
|
}
|
||||||
|
if strings.Contains(hostKeyPathOrKey, "-----BEGIN") {
|
||||||
|
return []byte(hostKeyPathOrKey), nil
|
||||||
|
}
|
||||||
|
b, err := os.ReadFile(hostKeyPathOrKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while loading sshd.host_key file: %s", err)
|
||||||
|
}
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
// configSSH reads the ssh info out of the passed-in Config and
|
// configSSH reads the ssh info out of the passed-in Config and
|
||||||
// updates the passed-in SSHServer. On success, it returns a function
|
// updates the passed-in SSHServer. On success, it returns a function
|
||||||
// that callers may invoke to run the configured ssh server. On
|
// that callers may invoke to run the configured ssh server. On
|
||||||
@@ -92,19 +122,9 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error)
|
|||||||
return nil, fmt.Errorf("sshd.listen can not use port 22")
|
return nil, fmt.Errorf("sshd.listen can not use port 22")
|
||||||
}
|
}
|
||||||
|
|
||||||
hostKeyPathOrKey := c.GetString("sshd.host_key", "")
|
hostKeyBytes, err := loadSSHHostKey(c)
|
||||||
if hostKeyPathOrKey == "" {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("sshd.host_key must be provided")
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
var hostKeyBytes []byte
|
|
||||||
if strings.Contains(hostKeyPathOrKey, "-----BEGIN") {
|
|
||||||
hostKeyBytes = []byte(hostKeyPathOrKey)
|
|
||||||
} else {
|
|
||||||
hostKeyBytes, err = os.ReadFile(hostKeyPathOrKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error while loading sshd.host_key file: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ssh.SetHostKey(hostKeyBytes)
|
err = ssh.SetHostKey(hostKeyBytes)
|
||||||
|
|||||||
+48
-13
@@ -8,14 +8,18 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/armon/go-radix"
|
"github.com/armon/go-radix"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SSHServer struct {
|
type SSHServer struct {
|
||||||
config *ssh.ServerConfig
|
// config is replaced whole when the host key changes, ssh.ServerConfig.AddHostKey only replaces a key of the same
|
||||||
l *slog.Logger
|
// algorithm and appends any other
|
||||||
|
config atomic.Pointer[ssh.ServerConfig]
|
||||||
|
authenticate func(ssh.ConnMetadata, ssh.PublicKey) (*ssh.Permissions, error)
|
||||||
|
l *slog.Logger
|
||||||
|
|
||||||
certChecker *ssh.CertChecker
|
certChecker *ssh.CertChecker
|
||||||
|
|
||||||
@@ -28,7 +32,12 @@ type SSHServer struct {
|
|||||||
// List of available commands
|
// List of available commands
|
||||||
helpCommand *Command
|
helpCommand *Command
|
||||||
commands *radix.Tree
|
commands *radix.Tree
|
||||||
listener net.Listener
|
listener atomic.Pointer[net.Listener]
|
||||||
|
|
||||||
|
// runs counts the Runs that are listening. A reload's Run can start before the one it replaced has returned
|
||||||
|
runs atomic.Int32
|
||||||
|
// addr is where the newest Run listens, nil when none is
|
||||||
|
addr atomic.Pointer[net.Addr]
|
||||||
|
|
||||||
// ctx parents per-Run contexts. Cancelling it (e.g. via Control.Stop) tears the server down even
|
// ctx parents per-Run contexts. Cancelling it (e.g. via Control.Stop) tears the server down even
|
||||||
// across reloads, since each Run derives a fresh child rather than reusing this one directly.
|
// across reloads, since each Run derives a fresh child rather than reusing this one directly.
|
||||||
@@ -85,10 +94,8 @@ func NewSSHServer(ctx context.Context, l *slog.Logger) (*SSHServer, error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
s.config = &ssh.ServerConfig{
|
s.authenticate = cc.Authenticate
|
||||||
PublicKeyCallback: cc.Authenticate,
|
s.config.Store(s.newConfig())
|
||||||
ServerVersion: fmt.Sprintf("SSH-2.0-Nebula???"),
|
|
||||||
}
|
|
||||||
|
|
||||||
s.RegisterCommand(&Command{
|
s.RegisterCommand(&Command{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
@@ -107,10 +114,32 @@ func (s *SSHServer) SetHostKey(hostPrivateKey []byte) error {
|
|||||||
return fmt.Errorf("failed to parse private key: %s", err)
|
return fmt.Errorf("failed to parse private key: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.config.AddHostKey(private)
|
config := s.newConfig()
|
||||||
|
config.AddHostKey(private)
|
||||||
|
s.config.Store(config)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SSHServer) newConfig() *ssh.ServerConfig {
|
||||||
|
return &ssh.ServerConfig{
|
||||||
|
PublicKeyCallback: s.authenticate,
|
||||||
|
ServerVersion: fmt.Sprintf("SSH-2.0-Nebula???"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Addr is the address the newest Run listens on, nil when none is.
|
||||||
|
func (s *SSHServer) Addr() net.Addr {
|
||||||
|
if a := s.addr.Load(); a != nil {
|
||||||
|
return *a
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Running is true while a Run is listening.
|
||||||
|
func (s *SSHServer) Running() bool {
|
||||||
|
return s.runs.Load() > 0
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SSHServer) ClearTrustedCAs() {
|
func (s *SSHServer) ClearTrustedCAs() {
|
||||||
s.authLock.Lock()
|
s.authLock.Lock()
|
||||||
s.trustedCAs = []ssh.PublicKey{}
|
s.trustedCAs = []ssh.PublicKey{}
|
||||||
@@ -181,10 +210,16 @@ func (s *SSHServer) Run(addr string) error {
|
|||||||
// this run owns. They start equal but a fast reload may overwrite s.listener with the next run's
|
// this run owns. They start equal but a fast reload may overwrite s.listener with the next run's
|
||||||
// listener before this run's watcher fires, so each run must close its own listener via the local
|
// listener before this run's watcher fires, so each run must close its own listener via the local
|
||||||
// reference.
|
// reference.
|
||||||
s.listener = listener
|
s.listener.Store(&listener)
|
||||||
|
s.runs.Add(1)
|
||||||
|
bound := listener.Addr()
|
||||||
|
s.addr.Store(&bound)
|
||||||
|
|
||||||
runCtx, cancel := context.WithCancel(s.ctx)
|
runCtx, cancel := context.WithCancel(s.ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
// Deferred after cancel so they run first, a run on its way out no longer counts as listening
|
||||||
|
defer s.runs.Add(-1)
|
||||||
|
defer s.addr.CompareAndSwap(&bound, nil)
|
||||||
|
|
||||||
// Close the listener when this run's context is cancelled. That can come from the parent
|
// Close the listener when this run's context is cancelled. That can come from the parent
|
||||||
// (Control.Stop), from Run returning normally (defer cancel above), or transitively when a sibling
|
// (Control.Stop), from Run returning normally (defer cancel above), or transitively when a sibling
|
||||||
@@ -197,7 +232,7 @@ func (s *SSHServer) Run(addr string) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.l.Info("SSH server is listening", "sshListener", addr)
|
s.l.Info("SSH server is listening", "sshListener", bound)
|
||||||
|
|
||||||
// Run loops until there is an error
|
// Run loops until there is an error
|
||||||
s.run(runCtx, listener)
|
s.run(runCtx, listener)
|
||||||
@@ -226,7 +261,7 @@ func (s *SSHServer) run(ctx context.Context, listener net.Listener) {
|
|||||||
<-sessionContext.Done()
|
<-sessionContext.Done()
|
||||||
c.Close()
|
c.Close()
|
||||||
}()
|
}()
|
||||||
conn, chans, reqs, err := ssh.NewServerConn(c, s.config)
|
conn, chans, reqs, err := ssh.NewServerConn(c, s.config.Load())
|
||||||
fp := ""
|
fp := ""
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
fp = conn.Permissions.Extensions["fp"]
|
fp = conn.Permissions.Extensions["fp"]
|
||||||
@@ -264,8 +299,8 @@ func (s *SSHServer) run(ctx context.Context, listener net.Listener) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SSHServer) Stop() {
|
func (s *SSHServer) Stop() {
|
||||||
if s.listener != nil {
|
if l := s.listener.Load(); l != nil {
|
||||||
if err := s.listener.Close(); err != nil {
|
if err := (*l).Close(); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||||
s.l.Warn("Failed to close the sshd listener", "error", err)
|
s.l.Warn("Failed to close the sshd listener", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,196 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/sshd"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
type sshdReloadHarness struct {
|
||||||
|
c *config.C
|
||||||
|
server *sshd.SSHServer
|
||||||
|
keyFile string
|
||||||
|
signer ssh.Signer
|
||||||
|
extra string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSSHDReloadHarness(t *testing.T) *sshdReloadHarness {
|
||||||
|
t.Helper()
|
||||||
|
l := test.NewLogger()
|
||||||
|
h := &sshdReloadHarness{keyFile: filepath.Join(t.TempDir(), "host_key")}
|
||||||
|
h.writeHostKey(t, newEd25519Key(t))
|
||||||
|
|
||||||
|
_, userKey, err := ed25519.GenerateKey(rand.Reader)
|
||||||
|
require.NoError(t, err)
|
||||||
|
h.signer, err = ssh.NewSignerFromKey(userKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
h.c = config.NewC(l)
|
||||||
|
require.NoError(t, h.c.LoadString(h.config()))
|
||||||
|
h.server, err = sshd.NewSSHServer(t.Context(), l)
|
||||||
|
require.NoError(t, err)
|
||||||
|
wireSSHReload(l, h.server, h.c)
|
||||||
|
run, err := configSSH(l, h.server, h.c)
|
||||||
|
require.NoError(t, err)
|
||||||
|
go run()
|
||||||
|
t.Cleanup(h.server.Stop)
|
||||||
|
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func newEd25519Key(t *testing.T) crypto.Signer {
|
||||||
|
_, key, err := ed25519.GenerateKey(rand.Reader)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeHostKey replaces the host key file and returns the public key a client should now see
|
||||||
|
func (h *sshdReloadHarness) writeHostKey(t *testing.T, key crypto.Signer) ssh.PublicKey {
|
||||||
|
t.Helper()
|
||||||
|
block, err := ssh.MarshalPrivateKey(key, "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, os.WriteFile(h.keyFile, pem.EncodeToMemory(block), 0o600))
|
||||||
|
pub, err := ssh.NewPublicKey(key.Public())
|
||||||
|
require.NoError(t, err)
|
||||||
|
return pub
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *sshdReloadHarness) config() string {
|
||||||
|
return fmt.Sprintf("sshd:\n enabled: true\n listen: 127.0.0.1:0\n host_key: %s\n authorized_users:\n - user: test\n keys: [%q]\n%s",
|
||||||
|
h.keyFile, string(ssh.MarshalAuthorizedKey(h.signer.PublicKey())), h.extra)
|
||||||
|
}
|
||||||
|
|
||||||
|
// dial connects to wherever the server listens now, a restart on :0 moves it. hostKeyAlgorithms limits which host keys
|
||||||
|
// the client accepts, none means any
|
||||||
|
func (h *sshdReloadHarness) dial(hostKeyAlgorithms ...string) (*ssh.Client, ssh.PublicKey, error) {
|
||||||
|
addr := h.server.Addr()
|
||||||
|
if addr == nil {
|
||||||
|
return nil, nil, fmt.Errorf("not listening")
|
||||||
|
}
|
||||||
|
var hostKey ssh.PublicKey
|
||||||
|
c, err := ssh.Dial("tcp", addr.String(), &ssh.ClientConfig{
|
||||||
|
User: "test",
|
||||||
|
Auth: []ssh.AuthMethod{ssh.PublicKeys(h.signer)},
|
||||||
|
HostKeyCallback: func(_ string, _ net.Addr, key ssh.PublicKey) error {
|
||||||
|
hostKey = key
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
HostKeyAlgorithms: hostKeyAlgorithms,
|
||||||
|
Timeout: time.Second,
|
||||||
|
})
|
||||||
|
return c, hostKey, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *sshdReloadHarness) connect(t *testing.T) (*ssh.Client, ssh.PublicKey) {
|
||||||
|
t.Helper()
|
||||||
|
var client *ssh.Client
|
||||||
|
var hostKey ssh.PublicKey
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
var err error
|
||||||
|
client, hostKey, err = h.dial()
|
||||||
|
return err == nil
|
||||||
|
}, 5*time.Second, 20*time.Millisecond)
|
||||||
|
t.Cleanup(func() { _ = client.Close() })
|
||||||
|
return client, hostKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// dropped is true when the server closed the client's connection within wait
|
||||||
|
func dropped(client *ssh.Client, wait time.Duration) bool {
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
_ = client.Wait()
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return true
|
||||||
|
case <-time.After(wait):
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restarting sshd drops every open session, the one that asked for the reload included. A reload that touches nothing
|
||||||
|
// sshd reads leaves it alone
|
||||||
|
func TestSSHDReload_UnchangedKeepsSessions(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()+"punchy:\n punch: true\n"))
|
||||||
|
assert.False(t, dropped(client, 500*time.Millisecond), "an unchanged sshd was restarted")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSSHDReload_ChangedConfigRestarts(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
|
h.extra = " trusted_cas: []\n"
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()))
|
||||||
|
assert.True(t, dropped(client, 5*time.Second), "a changed sshd config was not applied")
|
||||||
|
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
||||||
|
h.connect(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The config can stay the same while the host key file it names changes. New connections get the new key, open
|
||||||
|
// sessions are left alone
|
||||||
|
func TestSSHDReload_ChangedHostKeyFile(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
|
want := h.writeHostKey(t, newEd25519Key(t))
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()))
|
||||||
|
_, got := h.connect(t)
|
||||||
|
assert.Equal(t, want.Marshal(), got.Marshal(), "a new host key was not applied")
|
||||||
|
assert.False(t, dropped(client, 500*time.Millisecond), "a new host key restarted sshd")
|
||||||
|
}
|
||||||
|
|
||||||
|
// A host key of another algorithm replaces the old one rather than being offered next to it
|
||||||
|
func TestSSHDReload_HostKeyAlgorithmChange(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
h.connect(t)
|
||||||
|
|
||||||
|
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
|
require.NoError(t, err)
|
||||||
|
want := h.writeHostKey(t, rsaKey)
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()))
|
||||||
|
|
||||||
|
_, got := h.connect(t)
|
||||||
|
assert.Equal(t, want.Marshal(), got.Marshal())
|
||||||
|
_, _, err = h.dial(ssh.KeyAlgoED25519)
|
||||||
|
assert.Error(t, err, "the old ed25519 host key is still offered")
|
||||||
|
}
|
||||||
|
|
||||||
|
// A server that isn't running, say its port was busy, is tried again on every reload as before
|
||||||
|
func TestSSHDReload_NotRunningRestarts(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
h.server.Stop()
|
||||||
|
require.Eventually(t, func() bool { return !h.server.Running() }, 5*time.Second, 10*time.Millisecond)
|
||||||
|
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()))
|
||||||
|
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
||||||
|
h.connect(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A host key that no longer loads stops sshd, the same as a config that doesn't
|
||||||
|
func TestSSHDReload_BadHostKeyFileStops(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
h.connect(t)
|
||||||
|
|
||||||
|
require.NoError(t, os.WriteFile(h.keyFile, []byte("not a key"), 0o600))
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()))
|
||||||
|
require.Eventually(t, func() bool { return !h.server.Running() }, 5*time.Second, 10*time.Millisecond)
|
||||||
|
}
|
||||||
@@ -30,7 +30,7 @@ func procyield(cycles uint32)
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
packetsPerRing = 1024
|
packetsPerRing = 1024
|
||||||
// Caps tun.mtu at MTU-32 direct, MTU-64 relayed, unenforced anywhere else. 17.6MB page locked per socket.
|
// Fits a relayed packet at overlay.MaxMTU, which tun.mtu and route mtus are capped to. 17.6MB page locked per socket.
|
||||||
bytesPerPacket = MTU
|
bytesPerPacket = MTU
|
||||||
receiveSpins = 15
|
receiveSpins = 15
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user