diff --git a/modules/systemd-resolved/README.md b/modules/systemd-resolved/README.md index f034ce7cc..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 @@ -49,6 +50,15 @@ One long-running process, as root: `resolver-tools guard`. still raised, naming the writer, for a write nothing on the machine declared to handle. - **Only the mesh's resolvers answer every name.** Every five seconds, a link that a network manager gave servers of its own is told it is not a default route for names. Its own domains stay its own. +- **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 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 daeb2fb3f..0e39cd0d4 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard.go @@ -90,6 +90,10 @@ type Guard struct { pending *Displacement history []Displacement wake chan struct{} + + // 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. @@ -330,13 +334,39 @@ func (g *Guard) Run(ctx context.Context) { case <-g.wake: g.Tick() case <-routes.C: - 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, ", ")) - } + g.KeepRoutes(ctx) } } } +// 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. 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 { + return + } + if !g.Resolver.TheMeshAnswers(ctx, routes.Mesh.Servers) { + if !g.silent { + 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); err == nil && len(changed) > 0 { + g.Log("%s answers every name again, while the mesh's resolvers are silent", strings.Join(changed, ", ")) + } + return + } + if g.silent { + g.silent = false + g.Log("one of the mesh's resolvers answers again") + } + 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, ", ")) + } +} + // Request is one call over the socket: a verb and its arguments, one JSON line. type Request struct { Verb string `json:"verb"` diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go index bee912697..b82e0d4ef 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go @@ -249,3 +249,59 @@ func TestTheVerbsOnTheMachine(t *testing.T) { t.Errorf("the mesh's domain over the socket: %+v", r) } } + +// 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" + ctx := context.Background() + step := func(g *Guard, domains, defaults string, want ...string) { + t.Helper() + f.domain, f.defaults, f.changed = domains, defaults, nil + g.KeepRoutes(ctx) + var got []string + for _, c := range f.changed { + got = append(got, strings.Join(c, " ")) + } + if strings.Join(got, "; ") != strings.Join(want, "; ") { + t.Errorf("changed %q, want %q", got, want) + } + } + // 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(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(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. +// (1981/AC2) +func TestANetworkJoinedWhileTheMeshIsSilentKeepsItsDefaultRoute(t *testing.T) { + g, _, f := aGuardedMachine(t) + meshAnswers := false + g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers } + f.dns = "Global: 10.42.0.1\nLink 2 (wlan0): 192.168.1.1\n" + f.domain = "Global: ~.\n" + f.defaults = "Link 2 (wlan0): yes\n" + g.KeepRoutes(context.Background()) + if len(f.changed) != 0 { + t.Errorf("changed %v while the mesh was silent", f.changed) + } + meshAnswers = true + g.KeepRoutes(context.Background()) + if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl default-route wlan0 false" { + t.Errorf("changed %v once the mesh answered", f.changed) + } +} diff --git a/modules/systemd-resolved/cmd/resolver-tools/resolver.go b/modules/systemd-resolved/cmd/resolver-tools/resolver.go index 75442bd5e..e77cc62e2 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver.go @@ -16,12 +16,14 @@ import ( "context" "errors" "fmt" + "net" "net/netip" "os" "os/exec" "path/filepath" "regexp" "sort" + "strconv" "strings" "time" ) @@ -85,11 +87,58 @@ type Resolver struct { NetDir string // SuffixFile holds the mesh's own domain, which is never routed elsewhere. 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 { - return &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 +} + +// 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, r.answerWithin()) + defer cancel() + ask := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, network, _ string) (net.Conn, error) { + var d net.Dialer + if _, _, err := net.SplitHostPort(server); err == nil { + return d.DialContext(ctx, network, server) + } + return d.DialContext(ctx, network, net.JoinHostPort(server, "53")) + }} + _, err := ask.LookupHost(ctx, "node-resolver-asks."+r.suffix()) + var dnsErr *net.DNSError + return err == nil || (errors.As(err, &dnsErr) && dnsErr.IsNotFound) } // SuffixPath is the file the mesh renders the mesh's own domain into (the manifest's fact `suffix`). @@ -103,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. @@ -132,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 { @@ -163,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 @@ -376,3 +439,37 @@ func (r *Resolver) OnlyTheMeshIsADefaultRoute(ctx context.Context) ([]string, er } return changed, nil } + +// TheMeshAnswers says whether one of the mesh's resolvers answers now, asking them all at once. Behind a +// network's sign-in page none does: the private network is out of reach until the person signs in. +func (r *Resolver) TheMeshAnswers(ctx context.Context, servers []string) bool { + answered := make(chan bool, len(servers)) + for _, s := range servers { + go func() { answered <- r.Answers(ctx, s) }() + } + for range servers { + if <-answered { + return true + } + } + return false +} + +// 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 l.routed || l.DefaultRoute == nil || *l.DefaultRoute { + continue + } + if _, err := r.Change(ctx, "resolvectl", "default-route", l.Link, "true"); err != nil { + return changed, err + } + changed = append(changed, l.Link) + } + return changed, nil +} diff --git a/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go b/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go index 95dcafb1a..79a29f933 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go @@ -2,10 +2,12 @@ package main import ( "context" + "net" "os" "path/filepath" "strings" "testing" + "time" ) // fakeResolved answers resolvectl as systemd-resolved would, and records what was asked of it. @@ -170,3 +172,52 @@ func TestChangesAreEscalatedOnlyWhenNotRoot(t *testing.T) { t.Errorf("as the account: %s %v", p, a) } } + +// 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 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 { + t.Fatal(err) + } + defer conn.Close() + go func() { + buf := make([]byte, 512) + for { + n, from, err := conn.ReadFrom(buf) + if err != nil { + return + } + reply := append([]byte{}, buf[:n]...) + reply[2], reply[3] = 0x81, 0x83 // a reply, recursion available, the name not found + conn.WriteTo(reply, from) + } + }() + silent, err := net.ListenPacket("udp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer silent.Close() + 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") + } + start := time.Now() + if r.asks(context.Background(), silent.LocalAddr().String()) { + t.Error("a silent resolver answered") + } + 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 c62dbd7b4..1e43e9f5b 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", "class": "core", "upgrade": { "policy": "record", @@ -82,6 +82,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", @@ -110,5 +118,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 + } } }