mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-04 23:52:20 -08:00
wgengine/router: accept loopback traffic from Service VIPs (#22702)
In TUN mode the VIPs of a Tailscale Service have to be on a local interface, so a connection from the node to its own Service runs over lo with a VIP as both source and destination. ts-input accepts loopback traffic only from the node's own addresses, so over IPv4 these packets hit the CGNAT anti-spoofing DROP, and the node cannot reach its own Service. IPv6 has no such drop and works. Have the Linux router keep an "-i lo -s <vip> -j ACCEPT" rule for each address in router.Config.ServiceAddrs, like it does for LocalAddrs, re-adding them when the netfilter mode changes. The rule matches only packets that enter on lo, so it has no effect unless the VIP is a local address. Fixes #22701 Change-Id: I94563d2b3b989f29a737de1e377ab59f9e25fc2c Signed-off-by: Roman Nazarkin <[email protected]> Signed-off-by: Brendan Creane <[email protected]> Co-authored-by: Roman Nazarkin <[email protected]>
This commit is contained in:
1 parent
f53a1c7ba4
commit
f8accbe3c4
3 files changed
+119
-3
No files matched your search
@@ -102,6 +102,7 @@ type linuxRouter struct {
|
||||
mu sync.Mutex
|
||||
addrs map[netip.Prefix]bool
|
||||
lastScanAddrs set.Set[netip.Prefix] // desired addrs at the last successful orphan scan; nil until the first scan
|
||||
serviceAddrs map[netip.Prefix]bool // service addresses with a loopback rule
|
||||
routes map[netip.Prefix]bool
|
||||
localRoutes map[netip.Prefix]bool
|
||||
snatSubnetRoutes bool
|
||||
@@ -486,6 +487,12 @@ func (r *linuxRouter) Set(cfg *router.Config) error {
|
||||
}
|
||||
r.addrs = newAddrs
|
||||
|
||||
newServiceAddrs, err := cidrDiff("serviceAddr", r.serviceAddrs, cfg.ServiceAddrs, r.addServiceAddr, r.delServiceAddr, r.logf)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
r.serviceAddrs = newServiceAddrs
|
||||
|
||||
// r.addrs only tracks what this instance configured, so it misses
|
||||
// Tailscale addresses a previous instance left on a persistent tailscale0.
|
||||
// After the reconcile above (so our own addresses and their loopback rules
|
||||
@@ -906,6 +913,11 @@ func (r *linuxRouter) setNetfilterModeLocked(mode preftype.NetfilterMode) error
|
||||
return fmt.Errorf("error adding loopback rule: %w", err)
|
||||
}
|
||||
}
|
||||
for cidr := range r.serviceAddrs {
|
||||
if err := r.addLoopbackRule(cidr.Addr()); err != nil {
|
||||
return fmt.Errorf("error adding service loopback rule: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Re-add the CGNAT rules if we had any set.
|
||||
// This does not call [linuxRouter.setCGNATDropModeLocked] because that
|
||||
@@ -1191,6 +1203,19 @@ func (r *linuxRouter) addLoopbackRule(addr netip.Addr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// addServiceAddr permits loopback traffic from the address of a
|
||||
// Tailscale Service this node hosts. Unlike [linuxRouter.addAddress],
|
||||
// it does not assign the address: in TUN mode the operator puts it on a
|
||||
// local interface, and in the default mode netstack terminates it.
|
||||
func (r *linuxRouter) addServiceAddr(addr netip.Prefix) error {
|
||||
return r.addLoopbackRule(addr.Addr())
|
||||
}
|
||||
|
||||
// delServiceAddr removes the rule added by [linuxRouter.addServiceAddr].
|
||||
func (r *linuxRouter) delServiceAddr(addr netip.Prefix) error {
|
||||
return r.delLoopbackRule(addr.Addr())
|
||||
}
|
||||
|
||||
// delLoopbackRule removes the firewall rule permitting loopback
|
||||
// traffic to a Tailscale IP.
|
||||
func (r *linuxRouter) delLoopbackRule(addr netip.Addr) error {
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"tailscale.com/tsconst"
|
||||
"tailscale.com/tstest"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/types/preftype"
|
||||
"tailscale.com/util/eventbus"
|
||||
"tailscale.com/util/eventbus/eventbustest"
|
||||
"tailscale.com/util/linuxfw"
|
||||
@@ -38,6 +39,89 @@ import (
|
||||
|
||||
type Config = router.Config
|
||||
|
||||
// TestRouterServiceAddrs tests that the router keeps exactly one loopback
|
||||
// accept rule for each address of the Services the node hosts. It checks
|
||||
// that across netfilter mode changes, Service changes, and both in the
|
||||
// same Set.
|
||||
func TestRouterServiceAddrs(t *testing.T) {
|
||||
lr, fake := newTestLinuxRouter(t)
|
||||
|
||||
// Two dual-stack Services, A and B.
|
||||
const (
|
||||
a4 = "100.101.102.105"
|
||||
a6 = "fd7a:115c:a1e0::5"
|
||||
b4 = "100.101.102.106"
|
||||
b6 = "fd7a:115c:a1e0::6"
|
||||
)
|
||||
svcA := []string{a4, a6}
|
||||
svcB := []string{b4, b6}
|
||||
both := slices.Concat(svcA, svcB)
|
||||
|
||||
config := func(mode preftype.NetfilterMode, vips []string) *Config {
|
||||
var addrs []netip.Prefix
|
||||
for _, vip := range vips {
|
||||
ip := netip.MustParseAddr(vip)
|
||||
addrs = append(addrs, netip.PrefixFrom(ip, ip.BitLen()))
|
||||
}
|
||||
return &Config{
|
||||
LocalAddrs: mustCIDRs("100.101.102.104/10"),
|
||||
Routes: addrs,
|
||||
ServiceAddrs: addrs,
|
||||
NetfilterMode: mode,
|
||||
}
|
||||
}
|
||||
loopbackRule := func(vip string) string {
|
||||
family := "v4"
|
||||
if netip.MustParseAddr(vip).Is6() {
|
||||
family = "v6"
|
||||
}
|
||||
return family + "/filter/ts-input -i lo -s " + vip + " -j ACCEPT"
|
||||
}
|
||||
|
||||
steps := []struct {
|
||||
name string
|
||||
mode preftype.NetfilterMode
|
||||
vips []string // Service addresses in the config
|
||||
want []string // Service addresses that must have a rule
|
||||
}{
|
||||
{"both services", netfilterOn, both, both},
|
||||
{"netfilter off", netfilterOff, both, nil},
|
||||
{"netfilter back on", netfilterOn, both, both},
|
||||
{"nodivert", netfilterNoDivert, both, both},
|
||||
{"back to on", netfilterOn, both, both},
|
||||
{"remove B", netfilterOn, svcA, svcA},
|
||||
{"add B back", netfilterOn, both, both},
|
||||
{"off with both", netfilterOff, both, nil},
|
||||
{"on while removing B", netfilterOn, svcA, svcA},
|
||||
{"off with A", netfilterOff, svcA, nil},
|
||||
{"on while adding B", netfilterOn, both, both},
|
||||
{"services gone", netfilterOn, nil, nil},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if err := lr.Set(config(step.mode, step.vips)); err != nil {
|
||||
t.Fatalf("%s: Set: %v", step.name, err)
|
||||
}
|
||||
got := fake.String()
|
||||
lines := strings.Split(got, "\n")
|
||||
for _, vip := range both {
|
||||
want := 0
|
||||
if slices.Contains(step.want, vip) {
|
||||
want = 1
|
||||
}
|
||||
rule := loopbackRule(vip)
|
||||
n := 0
|
||||
for _, line := range lines {
|
||||
if line == rule {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n != want {
|
||||
t.Errorf("%s: %d copies of rule %q; want %d\n%s", step.name, n, rule, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterStates(t *testing.T) {
|
||||
basic := `
|
||||
ip rule add -4 pref 5210 fwmark 0x80000/0xff0000 table main
|
||||
|
||||
@@ -133,9 +133,16 @@ type Config struct {
|
||||
|
||||
// ServiceAddrs are the addresses of the Tailscale Services (VIP
|
||||
// services) this node hosts. The router does not assign them to the
|
||||
// tunnel interface. Only Linux uses them: an operator serving a
|
||||
// Service in TUN mode may assign them to the tunnel interface, and
|
||||
// the router must not remove them as stale addresses.
|
||||
// tunnel interface. Only Linux uses them, in two ways.
|
||||
//
|
||||
// An operator serving a Service in TUN mode has to assign its
|
||||
// addresses to a local interface, which may be the tunnel interface.
|
||||
// The router must not remove them as stale addresses.
|
||||
//
|
||||
// A connection from this node to its own TUN-mode Service runs over
|
||||
// lo with a Service address as both source and destination.
|
||||
// Netfilter accepts loopback traffic from these addresses, as it
|
||||
// does for LocalAddrs.
|
||||
ServiceAddrs []netip.Prefix
|
||||
|
||||
// SNATSubnetRoutes enables SNAT for traffic to local subnets.
|
||||
|
||||
Reference in new issue
Block a user