From 7fd6d895670957284c457adeeb20524a834be80f Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 09:50:08 +0100 Subject: [PATCH] systemd-resolved: find the links to give back from resolved, never a routed one; answer-within is a config value (issue 1981 review) The guard kept the links it had turned off in memory, so a VPN link turned off before it was routed got its default route back, and a guard restarted behind a sign-in page gave nothing back. It now reads them from resolved each round and skips a link whose domains are all routing domains. How long a mesh resolver is given to answer is the config value answer-within. --- modules/systemd-resolved/README.md | 9 ++- .../cmd/resolver-tools/guard.go | 23 +++---- .../cmd/resolver-tools/guard_test.go | 26 ++++---- .../cmd/resolver-tools/resolver.go | 60 +++++++++++++++---- .../cmd/resolver-tools/resolver_test.go | 17 ++++-- modules/systemd-resolved/files/answer-within | 4 ++ modules/systemd-resolved/module.json | 19 +++++- 7 files changed, 114 insertions(+), 44 deletions(-) create mode 100644 modules/systemd-resolved/files/answer-within diff --git a/modules/systemd-resolved/README.md b/modules/systemd-resolved/README.md index 15682083b..17c752a8f 100644 --- a/modules/systemd-resolved/README.md +++ b/modules/systemd-resolved/README.md @@ -19,6 +19,7 @@ itself (NetworkManager's VPNs, WireGuard set up by networkd, Tailscale) needs no | `/etc/node-resolver/resolv.conf` | the same file, kept beside it for the guard to compare with and put back | | `/etc/node-resolver/suffix` | the mesh's own domain, which a route may never take | | `/etc/systemd/resolved.conf.d/50-mesh.conf` | every mesh resolver as the default route for names (`DNS=`, `Domains=~.`), no public fallback, the stub on loopback and on the private address, no cache, no LLMNR or mDNS | +| `/etc/node-resolver/answer-within` | the config value `answer-within`: how long the guard gives each of the mesh's resolvers to answer before it takes them for silent (issue 1981) | **On the private address, so containers reach it.** A container copies its machine's resolver file and cannot reach the machine's loopback, so the file names the address the machine has on the private @@ -52,9 +53,11 @@ One long-running process, as root: `resolver-tools guard`. - **While the mesh's resolvers are silent, a network's own servers answer.** Behind a network's sign-in page (a guest or hotel network) the private network is out of reach until the person signs in, so no name, the page's included, would resolve. Every five seconds the guard first asks each of the mesh's - resolvers a name under the mesh's domain, straight over the network, each within a second; while none - answers, the links it kept from being a default route are one again, and a network joined meanwhile - keeps its default route. Once one answers, they lose it within five seconds. The guard says both turns on + resolvers a name under the mesh's domain, straight over the network, each within the config value + `answer-within` (seconds, default 1); while none answers, every link a network manager gave servers is a + default route again, read from resolved each round (so it holds after the guard restarts), and a network + joined meanwhile keeps its default route. A link `route` routed (a VPN's) never is. Once one answers, + they lose it within five seconds. The guard says both turns on its journal, and the node's network check still says the mesh's names do not resolve. - **What it says elsewhere** — the `routes` verb's `outside_writes`, from `/run/node-resolver/history.json` — is when, the writer's name and what became of each write. It never includes a server or a domain. diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard.go b/modules/systemd-resolved/cmd/resolver-tools/guard.go index d3f06e6a7..0e39cd0d4 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard.go @@ -91,10 +91,9 @@ type Guard struct { history []Displacement wake chan struct{} - // keptFrom is the links the guard kept from answering every name; silent is whether none of the - // mesh's resolvers answered at the last look. Only the routes' loop reads and writes them. - keptFrom map[string]bool - silent bool + // silent is whether none of the mesh's resolvers answered at the last look, so each turn is said once. + // Only the routes' loop reads and writes it. + silent bool } // NewGuard is the machine's. @@ -342,7 +341,8 @@ func (g *Guard) Run(ctx context.Context) { // KeepRoutes keeps every link's own servers from being a default route while one of the mesh's resolvers // answers. While none does, as behind a network's sign-in page, the links it kept from it answer every name -// again, so the page can be found; once one answers, they are kept from it once more. +// again, so the page can be found; once one answers, they are kept from it once more. Which links is read +// from resolved each round; the guard keeps no list of its own. func (g *Guard) KeepRoutes(ctx context.Context) { routes, err := g.Resolver.Routes(ctx) if err != nil { @@ -353,7 +353,7 @@ func (g *Guard) KeepRoutes(ctx context.Context) { g.silent = true g.Log("none of the mesh's resolvers answers; a network's own servers answer every name until one does") } - if changed, err := g.Resolver.ADefaultRouteAgain(ctx, routes, g.keptFrom); err == nil && len(changed) > 0 { + if changed, err := g.Resolver.ADefaultRouteAgain(ctx, routes); err == nil && len(changed) > 0 { g.Log("%s answers every name again, while the mesh's resolvers are silent", strings.Join(changed, ", ")) } return @@ -362,16 +362,7 @@ func (g *Guard) KeepRoutes(ctx context.Context) { g.silent = false g.Log("one of the mesh's resolvers answers again") } - changed, err := g.Resolver.OnlyTheMeshIsADefaultRoute(ctx) - if len(changed) > 0 { - if g.keptFrom == nil { - g.keptFrom = map[string]bool{} - } - for _, l := range changed { - g.keptFrom[l] = true - } - } - if err == nil && len(changed) > 0 { + if changed, err := g.Resolver.OnlyTheMeshIsADefaultRoute(ctx); err == nil && len(changed) > 0 { g.Log("%s had servers answering every name; now only their own domains", strings.Join(changed, ", ")) } } diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go index 48a8c4915..b82e0d4ef 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go @@ -250,19 +250,19 @@ func TestTheVerbsOnTheMachine(t *testing.T) { } } -// Behind a network's sign-in page the mesh's resolvers are out of reach, so the links the guard kept from -// answering every name answer them again until one of the mesh's resolvers answers; then they are kept -// from it once more. A routed link the guard never changed is left alone. (1981/AC1) +// Behind a network's sign-in page the mesh's resolvers are out of reach, so a link a network manager gave +// servers answers every name again until one of the mesh's resolvers answers; then it is kept from it once +// more. A link `route` routed keeps its default route off, even one the guard turned off before it was +// routed, and a guard started afresh behind the page finds the link from resolved alone. (1981/AC1) func TestTheNetworksServersAnswerWhileTheMeshsResolversAreSilent(t *testing.T) { g, _, f := aGuardedMachine(t) meshAnswers := true g.Resolver.Answers = func(_ context.Context, server string) bool { return meshAnswers && server == "10.42.0.1" } f.dns = "Global: 10.42.0.1 10.42.0.2\nLink 2 (wlan0): 192.168.1.1\nLink 7 (tun0): 10.9.0.1\n" - f.domain = "Global: ~.\nLink 2 (wlan0): lan\nLink 7 (tun0): ~corp.example\n" ctx := context.Background() - step := func(defaults string, want ...string) { + step := func(g *Guard, domains, defaults string, want ...string) { t.Helper() - f.defaults, f.changed = defaults, nil + f.domain, f.defaults, f.changed = domains, defaults, nil g.KeepRoutes(ctx) var got []string for _, c := range f.changed { @@ -272,12 +272,18 @@ func TestTheNetworksServersAnswerWhileTheMeshsResolversAreSilent(t *testing.T) { t.Errorf("changed %q, want %q", got, want) } } - step("Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false") + // tun0 comes up with servers and a default route, is turned off, and is then routed. + step(g, "Global: ~.\nLink 2 (wlan0): lan\nLink 7 (tun0):\n", "Link 2 (wlan0): yes\nLink 7 (tun0): yes\n", + "resolvectl default-route tun0 false", "resolvectl default-route wlan0 false") + routed := "Global: ~.\nLink 2 (wlan0): lan\nLink 7 (tun0): ~corp.example\n" meshAnswers = false - step("Link 2 (wlan0): no\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 true") - step("Link 2 (wlan0): yes\nLink 7 (tun0): no\n") + step(g, routed, "Link 2 (wlan0): no\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 true") + step(g, routed, "Link 2 (wlan0): yes\nLink 7 (tun0): no\n") + afresh, _, _ := aGuardedMachine(t) + afresh.Resolver = g.Resolver + step(afresh, routed, "Link 2 (wlan0): no\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 true") meshAnswers = true - step("Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false") + step(g, routed, "Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false") } // A network joined while the mesh's resolvers are silent keeps its default route, and loses it once one answers. diff --git a/modules/systemd-resolved/cmd/resolver-tools/resolver.go b/modules/systemd-resolved/cmd/resolver-tools/resolver.go index de7542149..e77cc62e2 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver.go @@ -23,6 +23,7 @@ import ( "path/filepath" "regexp" "sort" + "strconv" "strings" "time" ) @@ -88,22 +89,45 @@ type Resolver struct { SuffixFile string // Answers says whether one of the mesh's resolvers answers a name now. Answers func(ctx context.Context, server string) bool + // AnswerWithinFile holds how long one of them is given to answer (the config value answer-within). + AnswerWithinFile string } // ThisResolver is the machine's. func ThisResolver() *Resolver { - r := &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath} + r := &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath, + AnswerWithinFile: AnswerWithinPath} r.Answers = r.asks return r } -// AnswerWithin is how long one of the mesh's resolvers is given to answer the guard's question. -var AnswerWithin = time.Second +// AnswerWithinPath is the file the mesh renders the config value answer-within into, as `seconds=`. +const AnswerWithinPath = "/etc/node-resolver/answer-within" + +// DefaultAnswerWithin is the config value's default, used until the mesh has rendered the file. +const DefaultAnswerWithin = time.Second + +// answerWithin is how long one of the mesh's resolvers is given to answer the guard's question, read each +// round, so a changed config value holds from the next round on. +func (r *Resolver) answerWithin() time.Duration { + raw, err := os.ReadFile(r.AnswerWithinFile) + if err != nil { + return DefaultAnswerWithin + } + for _, line := range strings.Split(string(raw), "\n") { + if v, ok := strings.CutPrefix(strings.TrimSpace(line), "seconds="); ok { + if n, err := strconv.ParseFloat(v, 64); err == nil && n > 0 { + return time.Duration(n * float64(time.Second)) + } + } + } + return DefaultAnswerWithin +} // asks one of the mesh's resolvers a name under the mesh's own domain, straight over the network and not // through resolved: any answer, a name not found included, is an answer; only silence is not. func (r *Resolver) asks(ctx context.Context, server string) bool { - ctx, cancel := context.WithTimeout(ctx, AnswerWithin) + ctx, cancel := context.WithTimeout(ctx, r.answerWithin()) defer cancel() ask := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, network, _ string) (net.Conn, error) { var d net.Dialer @@ -128,6 +152,9 @@ type Scope struct { Domains []string `json:"domains"` // DefaultRoute is whether names no domain routes may also go here. Only the mesh's resolvers are. DefaultRoute *bool `json:"default_route,omitempty"` + + // routed is a link given its domains by `route`: every one a routing domain, as Route writes them. + routed bool } // Routes is what resolved sends where. @@ -157,6 +184,17 @@ func perScope(out string) (global []string, links map[string][]string) { return global, links } +// onlyRouting is whether a link's domains are all routing domains, as `route` gives them; a network manager +// gives a network's search domains, without the `~`. +func onlyRouting(domains []string) bool { + for _, d := range domains { + if !strings.HasPrefix(d, "~") { + return false + } + } + return len(domains) > 0 +} + func unrouted(domains []string) []string { out := make([]string, 0, len(domains)) for _, d := range domains { @@ -188,7 +226,7 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) { } sort.Strings(names) for _, name := range names { - s := Scope{Link: name, Servers: lServers[name], Domains: orEmpty(unrouted(lDomains[name]))} + s := Scope{Link: name, Servers: lServers[name], Domains: orEmpty(unrouted(lDomains[name])), routed: onlyRouting(lDomains[name])} if d, ok := lDefault[name]; ok && len(d) > 0 { yes := d[0] == "yes" s.DefaultRoute = &yes @@ -417,13 +455,15 @@ func (r *Resolver) TheMeshAnswers(ctx context.Context, servers []string) bool { return false } -// ADefaultRouteAgain lets the links named answer every name again, while the mesh's resolvers are silent: -// a network's own servers are then the only ones that can find its sign-in page. A link that went, or -// already answers every name, is left alone. Answers the links it changed. -func (r *Resolver) ADefaultRouteAgain(ctx context.Context, routes *Routes, links map[string]bool) ([]string, error) { +// ADefaultRouteAgain lets every link a network manager gave servers answer every name again, while the +// mesh's resolvers are silent: a network's own servers are then the only ones that can find its sign-in +// page. Read from what resolved holds, not from what the guard did, so it holds after the guard restarts. +// A link `route` routed (a VPN's) is never one, and one that already answers every name is left alone. +// Answers the links it changed. +func (r *Resolver) ADefaultRouteAgain(ctx context.Context, routes *Routes) ([]string, error) { var changed []string for _, l := range routes.Links { - if !links[l.Link] || (l.DefaultRoute != nil && *l.DefaultRoute) { + if l.routed || l.DefaultRoute == nil || *l.DefaultRoute { continue } if _, err := r.Change(ctx, "resolvectl", "default-route", l.Link, "true"); err != nil { diff --git a/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go b/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go index 3de6f78ec..79a29f933 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go @@ -174,7 +174,8 @@ func TestChangesAreEscalatedOnlyWhenNotRoot(t *testing.T) { } // One of the mesh's resolvers answers when it says anything, a name not found included; one that is silent -// does not, and the guard does not wait on it past AnswerWithin. (1981/AC3) +// does not, and the guard does not wait on it past the config value answer-within, read from the file the +// mesh renders, or a second before it has. (1981/AC3) func TestAResolverAnswersUnlessItIsSilent(t *testing.T) { conn, err := net.ListenPacket("udp", "127.0.0.1:0") if err != nil { @@ -198,9 +199,17 @@ func TestAResolverAnswersUnlessItIsSilent(t *testing.T) { t.Fatal(err) } defer silent.Close() - defer func(was time.Duration) { AnswerWithin = was }(AnswerWithin) - AnswerWithin = 100 * time.Millisecond r := aMachine(t, &fakeResolved{}) + r.AnswerWithinFile = filepath.Join(t.TempDir(), "answer-within") + if got := r.answerWithin(); got != DefaultAnswerWithin { + t.Errorf("before the file is rendered, a resolver is given %v", got) + } + if err := os.WriteFile(r.AnswerWithinFile, []byte("# written by the mesh\nseconds=0.1\n"), 0o644); err != nil { + t.Fatal(err) + } + if got := r.answerWithin(); got != 100*time.Millisecond { + t.Errorf("with answer-within 0.1, a resolver is given %v", got) + } if !r.asks(context.Background(), conn.LocalAddr().String()) { t.Error("a resolver that said the name is not found did not answer") } @@ -208,7 +217,7 @@ func TestAResolverAnswersUnlessItIsSilent(t *testing.T) { if r.asks(context.Background(), silent.LocalAddr().String()) { t.Error("a silent resolver answered") } - if took := time.Since(start); took > AnswerWithin+200*time.Millisecond { + if took := time.Since(start); took > 300*time.Millisecond { t.Errorf("a silent resolver was waited on %v", took) } } diff --git a/modules/systemd-resolved/files/answer-within b/modules/systemd-resolved/files/answer-within new file mode 100644 index 000000000..f20d1687f --- /dev/null +++ b/modules/systemd-resolved/files/answer-within @@ -0,0 +1,4 @@ +# How long, in seconds, the guard gives one of the mesh's resolvers to answer, from the config value +# answer-within, written by the mesh (module systemd-resolved, novox/hq issue 1981). Replaced on every +# push; change the config value, never this file. +seconds=${config:answer-within} diff --git a/modules/systemd-resolved/module.json b/modules/systemd-resolved/module.json index 270010bb6..93e3e2775 100644 --- a/modules/systemd-resolved/module.json +++ b/modules/systemd-resolved/module.json @@ -1,6 +1,6 @@ { "module": "systemd-resolved", - "version": "0.1", + "version": "0.2", "upgrade": { "policy": "record", "why": "the machine's names: a build that breaks this resolver stops every name resolving on a machine a person works on, the bus's included, and then neither the gate's rollback nor a push reaches it (hq ADR 0236, ADR 0247)" @@ -81,6 +81,14 @@ "kind": "unit" } }, + { + "id": "answer-within", + "type": "file", + "path": "/etc/node-resolver/answer-within", + "mode": "0644", + "trusted": false, + "template": "files/answer-within" + }, { "id": "guard", "type": "process", @@ -109,5 +117,14 @@ ] } ] + }, + "config": { + "answer-within": { + "type": "number", + "description": "How long, in seconds, the guard gives one of the mesh's resolvers to answer its question each round (novox/hq issue 1981): while none answers within it, a network's own servers answer every name, so a sign-in page can be found. 1 suits a tunnel's usual round trip; a slower link (tethered, abroad) needs more, or the guard takes a slow mesh for a silent one.", + "minimum": 0.2, + "maximum": 5, + "default": 1 + } } }