| 1 |
package overlay |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log/slog" |
| 8 |
"net/netip" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/rcrowley/go-metrics" |
| 12 |
"github.com/slackhq/nebula/iputil" |
| 13 |
"github.com/slackhq/nebula/routing" |
| 14 |
) |
| 15 |
|
| 16 |
type disabledTun struct { |
| 17 |
read chan []byte |
| 18 |
vpnNetworks []netip.Prefix |
| 19 |
|
| 20 |
// Track these metrics since we don't have the tun device to do it for us |
| 21 |
tx metrics.Counter |
| 22 |
rx metrics.Counter |
| 23 |
l *slog.Logger |
| 24 |
} |
| 25 |
|
| 26 |
func newDisabledTun(vpnNetworks []netip.Prefix, queueLen int, metricsEnabled bool, l *slog.Logger) *disabledTun { |
| 27 |
tun := &disabledTun{ |
| 28 |
vpnNetworks: vpnNetworks, |
| 29 |
read: make(chan []byte, queueLen), |
| 30 |
l: l, |
| 31 |
} |
| 32 |
|
| 33 |
if metricsEnabled { |
| 34 |
tun.tx = metrics.GetOrRegisterCounter("messages.tx.message", nil) |
| 35 |
tun.rx = metrics.GetOrRegisterCounter("messages.rx.message", nil) |
| 36 |
} else { |
| 37 |
tun.tx = &metrics.NilCounter{} |
| 38 |
tun.rx = &metrics.NilCounter{} |
| 39 |
} |
| 40 |
|
| 41 |
return tun |
| 42 |
} |
| 43 |
|
| 44 |
func (*disabledTun) Activate() error { |
| 45 |
return nil |
| 46 |
} |
| 47 |
|
| 48 |
func (*disabledTun) RoutesFor(addr netip.Addr) routing.Gateways { |
| 49 |
return routing.Gateways{} |
| 50 |
} |
| 51 |
|
| 52 |
func (t *disabledTun) Networks() []netip.Prefix { |
| 53 |
return t.vpnNetworks |
| 54 |
} |
| 55 |
|
| 56 |
func (*disabledTun) Name() string { |
| 57 |
return "disabled" |
| 58 |
} |
| 59 |
|
| 60 |
func (t *disabledTun) Read(b []byte) (int, error) { |
| 61 |
r, ok := <-t.read |
| 62 |
if !ok { |
| 63 |
return 0, io.EOF |
| 64 |
} |
| 65 |
|
| 66 |
if len(r) > len(b) { |
| 67 |
return 0, fmt.Errorf("packet larger than mtu: %d > %d bytes", len(r), len(b)) |
| 68 |
} |
| 69 |
|
| 70 |
t.tx.Inc(1) |
| 71 |
if t.l.Enabled(context.Background(), slog.LevelDebug) { |
| 72 |
t.l.Debug("Write payload", "raw", prettyPacket(r)) |
| 73 |
} |
| 74 |
|
| 75 |
return copy(b, r), nil |
| 76 |
} |
| 77 |
|
| 78 |
func (t *disabledTun) handleICMPEchoRequest(b []byte) bool { |
| 79 |
out := make([]byte, len(b)) |
| 80 |
out = iputil.CreateICMPEchoResponse(b, out) |
| 81 |
if out == nil { |
| 82 |
return false |
| 83 |
} |
| 84 |
|
| 85 |
// attempt to write it, but don't block |
| 86 |
select { |
| 87 |
case t.read <- out: |
| 88 |
default: |
| 89 |
t.l.Debug("tun_disabled: dropped ICMP Echo Reply response") |
| 90 |
} |
| 91 |
|
| 92 |
return true |
| 93 |
} |
| 94 |
|
| 95 |
func (t *disabledTun) Write(b []byte) (int, error) { |
| 96 |
t.rx.Inc(1) |
| 97 |
|
| 98 |
// Check for ICMP Echo Request before spending time doing the full parsing |
| 99 |
if t.handleICMPEchoRequest(b) { |
| 100 |
if t.l.Enabled(context.Background(), slog.LevelDebug) { |
| 101 |
t.l.Debug("Disabled tun responded to ICMP Echo Request", "raw", prettyPacket(b)) |
| 102 |
} |
| 103 |
} else if t.l.Enabled(context.Background(), slog.LevelDebug) { |
| 104 |
t.l.Debug("Disabled tun received unexpected payload", "raw", prettyPacket(b)) |
| 105 |
} |
| 106 |
return len(b), nil |
| 107 |
} |
| 108 |
|
| 109 |
func (t *disabledTun) SupportsMultiqueue() bool { |
| 110 |
return true |
| 111 |
} |
| 112 |
|
| 113 |
func (t *disabledTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { |
| 114 |
return t, nil |
| 115 |
} |
| 116 |
|
| 117 |
func (t *disabledTun) Close() error { |
| 118 |
if t.read != nil { |
| 119 |
close(t.read) |
| 120 |
t.read = nil |
| 121 |
} |
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
type prettyPacket []byte |
| 126 |
|
| 127 |
func (p prettyPacket) String() string { |
| 128 |
var s strings.Builder |
| 129 |
|
| 130 |
for i, b := range p { |
| 131 |
if i > 0 && i%8 == 0 { |
| 132 |
s.WriteString(" ") |
| 133 |
} |
| 134 |
s.WriteString(fmt.Sprintf("%02x ", b)) |
| 135 |
} |
| 136 |
|
| 137 |
return s.String() |
| 138 |
} |