From 737964a71da3bd3a5ba709558cd5186718ce023b Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Sun, 29 Nov 2026 22:29:13 -0800 Subject: [PATCH] ipn/ipnlocal: update LAN routes on link change with any exit node linkChange only reconfigured the router after a network change if LAN access was allowed, but the router config depends on the local interfaces whenever an exit node is in use. Fixes #22739 Signed-off-by: Nick Khyl --- ipn/ipnlocal/local.go | 6 ++- ipn/ipnlocal/state_test.go | 88 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index aa5acabc2..200bf4d32 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1265,8 +1265,10 @@ func (b *LocalBackend) linkChange(delta *netmon.ChangeDelta) { } var needReconfig bool - // If the network changed and we're using an exit node and allowing LAN access, we may need to reconfigure. - if delta.RebindLikelyRequired && prefs.ExitNodeID() != "" && prefs.ExitNodeAllowLANAccess() { + // If the network changed and we're using an exit node, we may need to reconfigure. + // The LAN routes are either excluded from the tunnel or explicitly routed into it, + // depending on whether LAN access is allowed. + if delta.RebindLikelyRequired && prefs.ExitNodeID() != "" { b.logf("linkChange: in state %v; updating LAN routes", b.state) needReconfig = true } diff --git a/ipn/ipnlocal/state_test.go b/ipn/ipnlocal/state_test.go index f15769331..a572043d9 100644 --- a/ipn/ipnlocal/state_test.go +++ b/ipn/ipnlocal/state_test.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "math/rand/v2" + "net" "net/netip" "slices" "strings" @@ -20,6 +21,7 @@ import ( "github.com/gaissmai/bart" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "go4.org/netipx" "tailscale.com/control/controlclient" "tailscale.com/envknob" @@ -1857,6 +1859,92 @@ func TestPeerConfigUpdatedOnPeerRouteDelta(t *testing.T) { } } +// TestExitNodeLANRoutesOnLinkChange tests that the LAN routes in the router +// config follow the device from one network to another while an exit node is +// in use, whether or not LAN access is allowed. +func TestExitNodeLANRoutesOnLinkChange(t *testing.T) { + oldLAN := netip.MustParsePrefix("192.168.1.0/24") + newLAN := netip.MustParsePrefix("192.168.2.0/24") + + // setNetwork makes pfx the address of the device's only network interface, + // and returns the resulting interface state. + setNetwork := func(pfx netip.Prefix) *netmon.State { + iface := netmon.Interface{ + Interface: &net.Interface{Index: 2, MTU: 1500, Name: "eth0", Flags: net.FlagUp | net.FlagRunning}, + AltAddrs: []net.Addr{netipx.PrefixIPNet(pfx)}, + } + netmon.RegisterInterfaceGetter(func() ([]netmon.Interface, error) { + return []netmon.Interface{iface}, nil + }) + return &netmon.State{ + Interface: map[string]netmon.Interface{"eth0": iface}, + InterfaceIPs: map[string][]netip.Prefix{"eth0": {pfx}}, + DefaultRouteInterface: "eth0", + HaveV4: true, + } + } + t.Cleanup(func() { netmon.RegisterInterfaceGetter(nil) }) + + exitNode := makeExitNode(2, withName("exit-node"), withAddresses(netip.MustParsePrefix("100.64.1.2/32"))) + nm := buildNetmapWithPeers( + makePeer(1, withName("node-1"), withAddresses(netip.MustParsePrefix("100.64.1.1/32"))), + exitNode, + ) + + for _, allowLANAccess := range []bool{false, true} { + t.Run(fmt.Sprintf("AllowLANAccess=%v", allowLANAccess), func(t *testing.T) { + // lanRoutes returns the routes that carry LAN traffic: + // around the tunnel if LAN access is allowed, and into it otherwise. + lanRoutes := func(cfg *router.Config) []netip.Prefix { + if allowLANAccess { + return cfg.LocalRoutes + } + return cfg.Routes + } + + oldState := setNetwork(netip.PrefixFrom(oldLAN.Addr().Next(), oldLAN.Bits())) + + lb, engine, cc := newLocalBackendWithMockEngineAndControl(t, false) + mustDo(t)(lb.Start(ipn.Options{})) + mustDo2(t)(lb.EditPrefs(&ipn.MaskedPrefs{ + Prefs: ipn.Prefs{ + WantRunning: true, + ExitNodeID: exitNode.StableID(), + ExitNodeAllowLANAccess: allowLANAccess, + }, + WantRunningSet: true, + ExitNodeIDSet: true, + ExitNodeAllowLANAccessSet: true, + })) + cc().authenticated(nm) + waitForGoroutinesToStop(lb) + + if routes := lanRoutes(engine.RouterConfig()); !slices.Contains(routes, oldLAN) { + t.Fatalf("routes before network change = %v; want %v included", routes, oldLAN) + } + + newState := setNetwork(netip.PrefixFrom(newLAN.Addr().Next(), newLAN.Bits())) + delta, err := netmon.NewChangeDelta(oldState, newState, 0, true) + if err != nil { + t.Fatal(err) + } + if !delta.RebindLikelyRequired { + t.Fatal("test setup bug: network change is not a major one") + } + lb.linkChange(delta) + waitForGoroutinesToStop(lb) + + routes := lanRoutes(engine.RouterConfig()) + if !slices.Contains(routes, newLAN) { + t.Errorf("routes after network change = %v; want %v included", routes, newLAN) + } + if slices.Contains(routes, oldLAN) { + t.Errorf("routes after network change = %v; want %v removed", routes, oldLAN) + } + }) + } +} + // TestSendPreservesAuthURL tests that wgengine updates arriving in the middle of // processing an auth URL doesn't result in the auth URL being cleared. func TestSendPreservesAuthURL(t *testing.T) {