diff --git a/modules/systemd-resolved/README.md b/modules/systemd-resolved/README.md index f034ce7cc..15682083b 100644 --- a/modules/systemd-resolved/README.md +++ b/modules/systemd-resolved/README.md @@ -49,6 +49,13 @@ 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 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 + 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..d3f06e6a7 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard.go @@ -90,6 +90,11 @@ type Guard struct { pending *Displacement 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 } // NewGuard is the machine's. @@ -330,13 +335,47 @@ 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. +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, g.keptFrom); 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") + } + 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 { + 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..48a8c4915 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go @@ -249,3 +249,53 @@ 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 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) +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) { + t.Helper() + f.defaults, f.changed = 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) + } + } + step("Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false") + 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") + meshAnswers = true + step("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..de7542149 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver.go @@ -16,6 +16,7 @@ import ( "context" "errors" "fmt" + "net" "net/netip" "os" "os/exec" @@ -85,11 +86,35 @@ 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 } // 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} + 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 + +// 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) + 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`). @@ -376,3 +401,35 @@ 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 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) { + var changed []string + for _, l := range routes.Links { + if !links[l.Link] || (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..3de6f78ec 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,43 @@ 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 AnswerWithin. (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() + defer func(was time.Duration) { AnswerWithin = was }(AnswerWithin) + AnswerWithin = 100 * time.Millisecond + r := aMachine(t, &fakeResolved{}) + 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 > AnswerWithin+200*time.Millisecond { + t.Errorf("a silent resolver was waited on %v", took) + } +}