mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-06 23:36:34 +09:00
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 <bcreane@gmail.com>
This commit is contained in:
3 files changed
+76
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user