diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard.go b/modules/systemd-resolved/cmd/resolver-tools/guard.go index 5d244d9d7..7fb736a67 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard.go @@ -46,6 +46,9 @@ const ( Socket = RunDir + "/verbs.sock" History = RunDir + "/history.json" Displaced = RunDir + "/displaced" + // Fallback is `routes`'s state for ADR 0688's fallback (design 08 sign-in-page §8, S-N5, R2-2): + // whether the mesh's resolvers are silent, and since when. + Fallback = RunDir + "/fallback.json" ) // Timing. @@ -94,6 +97,11 @@ type Guard 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 + // silentSince is when silent last changed, for Fallback's `since` (design 08 sign-in-page §8, S-N5). + silentSince time.Time + // fallbackWritten is whether Fallback has been written since this guard started; its first round + // writes it once more regardless of a turn, since it goes at every boot (§8). + fallbackWritten bool // applied is what the guard last gave the link carrying the default route (design 08 sign-in-page §8): // read at start from UplinkNamesAppliedFile, so a restart takes the names off the link it names, @@ -355,24 +363,69 @@ func (g *Guard) KeepRoutes(ctx context.Context) { return } if !g.Resolver.TheMeshAnswers(ctx, routes.Mesh.Servers) { - if !g.silent { + if turn := !g.silent; turn || !g.fallbackWritten { + if turn { + g.silentSince = g.Now() + } g.silent = true - g.Log("none of the mesh's resolvers answers; a network's own servers answer every name until one does") + g.writeFallback() + if turn { + 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 { + if turn := g.silent; turn || !g.fallbackWritten { + if turn { + g.silentSince = g.Now() + } g.silent = false - g.Log("one of the mesh's resolvers answers again") + g.writeFallback() + if turn { + 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, ", ")) } } +// Fallback is `routes`'s state for ADR 0688's fallback (design 08 sign-in-page §8). +type FallbackState struct { + Silent bool `json:"silent"` + Since time.Time `json:"since"` +} + +// writeFallback writes Resolver.FallbackFile whole, at each of the fallback's two turns, and once more at +// the guard's first round, since it goes at every boot (design 08 sign-in-page §8). +func (g *Guard) writeFallback() { + g.fallbackWritten = true + raw, err := json.MarshalIndent(FallbackState{Silent: g.silent, Since: g.silentSince}, "", " ") + if err != nil { + return + } + tmp := g.Resolver.FallbackFile + ".tmp" + if os.WriteFile(tmp, raw, 0o644) == nil { + _ = os.Rename(tmp, g.Resolver.FallbackFile) + } +} + +// ReadFallback reads what the guard last said of ADR 0688's fallback; the zero value before it has run. +func ReadFallback(path string) FallbackState { + raw, err := os.ReadFile(path) + if err != nil { + return FallbackState{} + } + var s FallbackState + if json.Unmarshal(raw, &s) != nil { + return FallbackState{} + } + return s +} + // sameNames is whether two name lists are the same, in the same order (the file holds them one a line, in // the order given). func sameNames(a, b []string) bool { @@ -499,7 +552,8 @@ func (g *Guard) Answer(ctx context.Context, req Request) Reply { case "routes": var routes *Routes if routes, err = g.Resolver.Routes(ctx); err == nil { - result = map[string]any{"mesh": routes.Mesh, "links": routes.Links} + result = map[string]any{"mesh": routes.Mesh, "links": routes.Links, + "uplink_names": g.Resolver.UplinkNamesAnswer(routes), "mesh_resolvers": g.Resolver.MeshResolversAnswer()} } case "route": var routed *Routed diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go index ca6a0ec94..2acfa29aa 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go @@ -41,6 +41,7 @@ func aGuardedMachineWithLinks(t *testing.T, links ...string) (*Guard, *time.Time now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC) f := &fakeResolved{} r := aMachine(t, f, links...) + r.FallbackFile = filepath.Join(dir, "run", "fallback.json") g := &Guard{Path: filepath.Join(dir, "etc/resolv.conf"), KeptPath: filepath.Join(dir, "etc/node-resolver/resolv.conf"), Dir: filepath.Join(dir, "run"), Hold: Hold, Now: func() time.Time { return now }, Resolver: r, Log: t.Logf, wake: make(chan struct{}, 1)} @@ -409,6 +410,73 @@ func TestARefusalByResolvedIsJournalledOncePerChange(t *testing.T) { } } +// The guard writes Resolver.FallbackFile at each of ADR 0688's two turns, and once more at its own first +// round, since it goes at every boot (design 08 sign-in-page §8; S-N5, R2-2; R-9). +func TestTheGuardWritesTheFallbackFileAtEachTurnAndItsFirstRound(t *testing.T) { + g, now, f := aGuardedMachine(t) + meshAnswers := true + g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers } + f.dns = "Global: 10.42.0.1\n" + f.domain = "Global: ~.\n" + // The first round, the mesh answering, writes the file although nothing changed. + g.KeepRoutes(context.Background()) + first := ReadFallback(g.Resolver.FallbackFile) + if first.Silent { + t.Fatalf("the first round says %+v, want not silent", first) + } + // A second round with nothing changed does not move `since`. + *now = now.Add(time.Second) + g.KeepRoutes(context.Background()) + if again := ReadFallback(g.Resolver.FallbackFile); !again.Since.Equal(first.Since) { + t.Errorf("an unchanged round moved since: %v, was %v", again.Since, first.Since) + } + // The mesh goes silent: the turn is written with the new since. + *now = now.Add(time.Second) + meshAnswers = false + g.KeepRoutes(context.Background()) + silentState := ReadFallback(g.Resolver.FallbackFile) + if !silentState.Silent || !silentState.Since.Equal(*now) { + t.Errorf("the silent turn is %+v, want silent since %v", silentState, *now) + } + // It answers again: the other turn. + *now = now.Add(time.Second) + meshAnswers = true + g.KeepRoutes(context.Background()) + answering := ReadFallback(g.Resolver.FallbackFile) + if answering.Silent || !answering.Since.Equal(*now) { + t.Errorf("the answering turn is %+v, want not silent since %v", answering, *now) + } +} + +// routes answers uplink_names with the applied list's names resolved holds on that link now, resolved's +// refusal, and mesh_resolvers from the fallback file (design 08 sign-in-page §2, §9; R-9). +func TestRoutesAnswersUplinkNamesAndMeshResolvers(t *testing.T) { + f := &fakeResolved{dns: "Link 2 (wlan0): 192.168.1.1\n", domain: "Link 2 (wlan0): lan ~check.example\n"} + r := aMachine(t, f, "wlan0") + applied := UplinkApplied{Names: []string{"check.example", "not-held.example"}, Link: "wlan0", Refused: "refused once"} + raw, _ := json.Marshal(applied) + if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil { + t.Fatal(err) + } + since := time.Date(2026, 11, 24, 10, 0, 0, 0, time.UTC) + fb, _ := json.Marshal(FallbackState{Silent: true, Since: since}) + if err := os.WriteFile(r.FallbackFile, fb, 0o644); err != nil { + t.Fatal(err) + } + routes, err := r.Routes(context.Background()) + if err != nil { + t.Fatal(err) + } + un := r.UplinkNamesAnswer(routes) + if names, _ := un["names"].([]string); strings.Join(names, ",") != "check.example" || un["link"] != "wlan0" || un["refused"] != "refused once" { + t.Errorf("uplink_names is %+v", un) + } + mr := r.MeshResolversAnswer() + if mr["silent"] != true || !mr["since"].(time.Time).Equal(since) { + t.Errorf("mesh_resolvers is %+v", mr) + } +} + // 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) { diff --git a/modules/systemd-resolved/cmd/resolver-tools/main.go b/modules/systemd-resolved/cmd/resolver-tools/main.go index b48a37a50..440625dea 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/main.go +++ b/modules/systemd-resolved/cmd/resolver-tools/main.go @@ -81,7 +81,8 @@ func tools(r *Resolver, history string) []stdio.Tool { if err != nil { return nil, err } - return map[string]any{"mesh": routes.Mesh, "links": routes.Links, "outside_writes": ReadHistory(history)}, nil + return map[string]any{"mesh": routes.Mesh, "links": routes.Links, "outside_writes": ReadHistory(history), + "uplink_names": r.UplinkNamesAnswer(routes), "mesh_resolvers": r.MeshResolversAnswer()}, nil }), verb("route", "Send these domains, and every name under them, to these servers over this link, and only "+ "them: the link never answers other names, and the mesh's own domain is refused. Replaces whatever the "+ diff --git a/modules/systemd-resolved/cmd/resolver-tools/resolver.go b/modules/systemd-resolved/cmd/resolver-tools/resolver.go index 13c0fb00c..2d6b8c851 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver.go @@ -99,6 +99,9 @@ type Resolver struct { UplinkNamesFile string // UplinkNamesAppliedFile is what the guard last gave the link carrying the default route (§8). UplinkNamesAppliedFile string + // FallbackFile is `routes`'s state for ADR 0688's fallback (§8): whether the mesh's resolvers are + // silent, and since when. + FallbackFile string // WriteUplinkNames writes UplinkNamesFile whole, as root (§6); stood in for in tests. WriteUplinkNames func(ctx context.Context, content []byte) error // Now is the clock the uplink-names verb stamps its answer with; stood in for in tests. @@ -109,7 +112,8 @@ type Resolver struct { func ThisResolver() *Resolver { r := &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath, AnswerWithinFile: AnswerWithinPath, PrivateInterfaceFile: PrivateInterfacePath, - UplinkNamesFile: UplinkNamesPath, UplinkNamesAppliedFile: UplinkNamesAppliedPath, Now: time.Now} + UplinkNamesFile: UplinkNamesPath, UplinkNamesAppliedFile: UplinkNamesAppliedPath, FallbackFile: Fallback, + Now: time.Now} r.Answers = r.asks r.WriteUplinkNames = r.writeUplinkNamesFile return r @@ -281,6 +285,36 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) { return out, nil } +// UplinkNamesAnswer is `routes`'s `uplink_names` field (design 08 sign-in-page §2, §9; DQ-3): the applied +// list's names that resolved holds on that link now, never the file's, the link, and resolved's refusal or +// empty. +func (r *Resolver) UplinkNamesAnswer(routes *Routes) map[string]any { + applied := ReadUplinkApplied(r.UplinkNamesAppliedFile) + held := []string{} + for _, l := range routes.Links { + if l.Link != applied.Link { + continue + } + has := map[string]bool{} + for _, d := range l.Domains { + has[d] = true + } + for _, n := range applied.Names { + if has[n] { + held = append(held, n) + } + } + } + return map[string]any{"names": held, "link": applied.Link, "refused": applied.Refused} +} + +// MeshResolversAnswer is `routes`'s `mesh_resolvers` field (design 08 sign-in-page §9; S-N5): whether the +// mesh's resolvers are silent now, by ADR 0688's guard, and since when. +func (r *Resolver) MeshResolversAnswer() map[string]any { + s := ReadFallback(r.FallbackFile) + return map[string]any{"silent": s.Silent, "since": s.Since} +} + // withoutMarked is domains without those marked (design 08 sign-in-page DMC-2). func withoutMarked(domains []string, marked map[string]bool) []string { out := make([]string, 0, len(domains)) diff --git a/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go b/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go index acb722f93..caceb0cd8 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/resolver_test.go @@ -59,6 +59,7 @@ func aMachine(t *testing.T, f *fakeResolved, links ...string) *Resolver { PrivateInterfaceFile: filepath.Join(dir, "private-interface"), UplinkNamesFile: filepath.Join(dir, "uplink-names"), UplinkNamesAppliedFile: filepath.Join(dir, "uplink-names.applied"), + FallbackFile: filepath.Join(dir, "fallback.json"), Now: func() time.Time { return time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) }, WriteUplinkNames: func(_ context.Context, content []byte) error { return os.WriteFile(filepath.Join(dir, "uplink-names"), content, 0o644)