From eab730a52435f5fc9e06bd34aaf9a2a74287405d Mon Sep 17 00:00:00 2001 From: Brendan Creane Date: Fri, 4 Dec 2026 23:27:10 +0000 Subject: [PATCH] ipn/ipnext,ipn/ipnlocal: add ExtraWireGuardAllowedIPsApplied hook An extension that returns prefixes from the ExtraWireGuardAllowedIPs hook had no way to learn when WireGuard started using them. authReconfig now calls the new ExtraWireGuardAllowedIPsApplied hook after it has committed the prefixes to the route manager, synced the changed peers into the WireGuard device, and refreshed the data plane's peer routes. Updates #22918 Signed-off-by: Brendan Creane --- ipn/ipnext/ipnext.go | 8 +++++ ipn/ipnlocal/local.go | 3 ++ ipn/ipnlocal/local_test.go | 65 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/ipn/ipnext/ipnext.go b/ipn/ipnext/ipnext.go index 967b57fc7..d5f83c932 100644 --- a/ipn/ipnext/ipnext.go +++ b/ipn/ipnext/ipnext.go @@ -502,6 +502,14 @@ type Hooks struct { // WireGuard APIs. ExtraWireGuardAllowedIPs feature.Hook[func(peers iter.Seq[tailcfg.NodeID]) map[tailcfg.NodeID][]netip.Prefix] + // ExtraWireGuardAllowedIPsApplied is called after the prefixes returned + // by the preceding [Hooks.ExtraWireGuardAllowedIPs] call are in effect. + // From then on WireGuard sends packets for those prefixes to their + // peers and accepts packets from them. + // + // The hook is called with LocalBackend's mutex locked. + ExtraWireGuardAllowedIPsApplied feature.Hook[func()] + // ExtraRouterConfigRoutes returns a view of prefixes to append to [router.Config.Routes]. // // Routes goes through the WireGuard engine which makes efforts to avoid diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 6eb4698ec..1765333ad 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -6628,6 +6628,9 @@ func (b *LocalBackend) authReconfigLocked() { // commit, and before the Reconfig below, so packets flowing under // the new config never see a stale peer table. b.setDataPlanePeerRoutes() + if f, ok := b.extHost.hooks.ExtraWireGuardAllowedIPsApplied.GetOk(); ok { + f() + } err := b.e.Reconfig(cfg, rcfg, dcfg) if err == wgengine.ErrNoChanges { diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 005931334..f62149121 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "iter" "log" "maps" "math" @@ -11528,3 +11529,67 @@ func TestExtensionStateHooksWhileBlocked(t *testing.T) { t.Errorf("network configuration events = %v, want [false]", configured) } } + +// TestAuthReconfigExtraWireGuardAllowedIPsApplied tests that authReconfig +// calls the ExtraWireGuardAllowedIPsApplied hook after the prefixes from the +// ExtraWireGuardAllowedIPs hook are in the outbound route table. +func TestAuthReconfigExtraWireGuardAllowedIPsApplied(t *testing.T) { + b := newTestLocalBackend(t) + + peer := (&tailcfg.Node{ + ID: 2, + Key: key.NewNode().Public(), + HomeDERP: 1, + Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.2/32")}, + AllowedIPs: []netip.Prefix{netip.MustParsePrefix("100.64.0.2/32")}, + }).View() + transit := netip.MustParsePrefix("169.254.0.1/32") + + var extrasCalls, appliedCalls int + b.extHost.Hooks().ExtraWireGuardAllowedIPs.Set(func(peers iter.Seq[tailcfg.NodeID]) map[tailcfg.NodeID][]netip.Prefix { + extrasCalls++ + return map[tailcfg.NodeID][]netip.Prefix{peer.ID(): {transit}} + }) + b.extHost.Hooks().ExtraWireGuardAllowedIPsApplied.Set(func() { + appliedCalls++ + if extrasCalls != appliedCalls { + t.Errorf("applied hook call %d followed %d extras hook calls, want the same count", appliedCalls, extrasCalls) + } + pr, ok := b.currentNode().routeMgr.Outbound().Lookup(transit.Addr()) + if !ok || pr.Key != peer.Key() { + t.Errorf("at the applied hook, outbound lookup of %v = %v, %v; want peer %v", transit.Addr(), pr, ok, peer.Key()) + } + }) + + nk := key.NewNode() + b.mu.Lock() + err := b.pm.SetPrefs((&ipn.Prefs{ + ControlURL: "https://localhost:1/", + WantRunning: true, + Persist: &persist.Persist{PrivateNodeKey: nk}, + }).View(), ipn.NetworkProfile{}) + b.mu.Unlock() + if err != nil { + t.Fatal(err) + } + b.SetControlClientStatus(b.cc, controlclient.Status{ + NetMap: &netmap.NetworkMap{ + NodeKey: nk.Public(), + SelfNode: (&tailcfg.Node{ + ID: 1, + Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32")}, + }).View(), + Peers: []tailcfg.NodeView{peer}, + }, + LoggedIn: true, + }) + + b.mu.Lock() + defer b.mu.Unlock() + before := appliedCalls + b.authReconfigLocked() + + if appliedCalls != before+1 { + t.Errorf("authReconfig called the applied hook %d times, want 1", appliedCalls-before) + } +}