systemd-resolved: the guard applies the uplink's names every round (issue 960)
Each round it finds the link carrying the default route (the lowest-metric kernel default route that is neither one route routed nor the private network's interface), gives it the names from uplink-names's file plus its own search and routing domains, and takes them off the link they were on before when that differs. What it applied is kept in uplink-names.applied, read at its own start so a restart never infers which link to take them off, and a resolved refusal is journalled once per change, not every round. The guard's own names are left out of whether a link is judged `route` routed, so the fallback still gives a link holding only them its default route back while the mesh's resolvers are silent (ADR 0688 stands). Builds: design 08 sign-in-page §7, §8
This commit is contained in:
@@ -94,6 +94,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
|
||||
|
||||
// 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,
|
||||
// never inferring which. Only the uplink names' round reads and writes it.
|
||||
applied UplinkApplied
|
||||
}
|
||||
|
||||
// NewGuard is the machine's.
|
||||
@@ -335,6 +340,7 @@ func (g *Guard) Run(ctx context.Context) {
|
||||
g.Tick()
|
||||
case <-routes.C:
|
||||
g.KeepRoutes(ctx)
|
||||
g.KeepUplinkNames(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -367,6 +373,108 @@ func (g *Guard) KeepRoutes(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// rawDomainsOf is one link's domains as resolved holds them now, `~` kept, so its routing domains and its
|
||||
// plain search domains are told apart.
|
||||
func (g *Guard) rawDomainsOf(ctx context.Context, link string) ([]string, error) {
|
||||
out, err := g.Resolver.Read(ctx, "resolvectl", "domain")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, links := perScope(out)
|
||||
return links[link], nil
|
||||
}
|
||||
|
||||
// setLinkUplinkNames sets one link's domains to its own, apart from the names the guard applied there
|
||||
// last, plus `~<name>` per name now given (design 08 sign-in-page §7): every search domain and routing
|
||||
// domain the link already has, apart from the guard's own, stays. An empty names takes them off.
|
||||
func (g *Guard) setLinkUplinkNames(ctx context.Context, link string, names []string) error {
|
||||
raw, err := g.rawDomainsOf(ctx, link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
marked := map[string]bool{}
|
||||
for _, n := range g.applied.Names {
|
||||
marked["~"+n] = true
|
||||
}
|
||||
base := withoutMarked(raw, marked)
|
||||
full := append([]string{}, base...)
|
||||
for _, n := range names {
|
||||
full = append(full, "~"+n)
|
||||
}
|
||||
_, err = g.Resolver.Change(ctx, "resolvectl", append([]string{"domain", link}, full...)...)
|
||||
return err
|
||||
}
|
||||
|
||||
// KeepUplinkNames gives the names in the file to the link carrying the default route, every round (design
|
||||
// 08 sign-in-page §7, §8): it never sets a link's servers, its default route for names, its addresses or
|
||||
// its routes. A round in which they are already in place changes nothing.
|
||||
func (g *Guard) KeepUplinkNames(ctx context.Context) {
|
||||
given := ReadUplinkNames(g.Resolver.UplinkNamesFile)
|
||||
routes, err := g.Resolver.Routes(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
link, err := g.Resolver.DefaultRouteLink(ctx, routes)
|
||||
if err != nil {
|
||||
g.Log("the kernel's default routes: %v", err)
|
||||
return
|
||||
}
|
||||
if link == g.applied.Link && sameNames(given, g.applied.Names) && g.applied.Refused == "" {
|
||||
return
|
||||
}
|
||||
if g.applied.Link != "" && g.applied.Link != link {
|
||||
if err := g.setLinkUplinkNames(ctx, g.applied.Link, nil); err != nil {
|
||||
g.Log("taking the uplink's names off %s failed: %v", g.applied.Link, err)
|
||||
}
|
||||
}
|
||||
refused := ""
|
||||
if link != "" {
|
||||
if err := g.setLinkUplinkNames(ctx, link, given); err != nil {
|
||||
refused = err.Error()
|
||||
}
|
||||
}
|
||||
if link != g.applied.Link {
|
||||
g.Log("the uplink's names are now answered over %q, were %q", link, g.applied.Link)
|
||||
} else if !sameNames(given, g.applied.Names) {
|
||||
g.Log("the uplink's names change on %s", link)
|
||||
}
|
||||
if refused != g.applied.Refused {
|
||||
if refused != "" {
|
||||
g.Log("resolved refuses the uplink's names: %s", refused)
|
||||
} else {
|
||||
g.Log("resolved accepts the uplink's names again")
|
||||
}
|
||||
}
|
||||
g.applied = UplinkApplied{Names: given, Link: link, At: g.Now(), Refused: refused}
|
||||
g.writeApplied()
|
||||
}
|
||||
|
||||
// writeApplied writes what the guard applied last, whole (beside, renamed over), design 08 sign-in-page §8.
|
||||
func (g *Guard) writeApplied() {
|
||||
raw, err := json.MarshalIndent(g.applied, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tmp := g.Resolver.UplinkNamesAppliedFile + ".tmp"
|
||||
if os.WriteFile(tmp, raw, 0o644) == nil {
|
||||
_ = os.Rename(tmp, g.Resolver.UplinkNamesAppliedFile)
|
||||
}
|
||||
}
|
||||
|
||||
// Request is one call over the socket: a verb and its arguments, one JSON line.
|
||||
type Request struct {
|
||||
Verb string `json:"verb"`
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -20,6 +22,13 @@ const vpnFile = "# Dynamic resolv.conf(5) file for glibc resolver(3) generated b
|
||||
|
||||
// aGuardedMachine is a guard over a temporary /etc and /run, with a clock the test moves.
|
||||
func aGuardedMachine(t *testing.T) (*Guard, *time.Time, *fakeResolved) {
|
||||
t.Helper()
|
||||
g, now, f, _ := aGuardedMachineWithLinks(t, "tun0")
|
||||
return g, now, f
|
||||
}
|
||||
|
||||
// aGuardedMachineWithLinks is the same, over the links named, and answers the resolver too.
|
||||
func aGuardedMachineWithLinks(t *testing.T, links ...string) (*Guard, *time.Time, *fakeResolved, *Resolver) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
for _, d := range []string{"etc/node-resolver", "run"} {
|
||||
@@ -31,10 +40,11 @@ func aGuardedMachine(t *testing.T) (*Guard, *time.Time, *fakeResolved) {
|
||||
write(t, filepath.Join(dir, "etc/resolv.conf"), meshFile)
|
||||
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, links...)
|
||||
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: aMachine(t, f, "tun0"), Log: t.Logf, wake: make(chan struct{}, 1)}
|
||||
return g, &now, f
|
||||
Resolver: r, Log: t.Logf, wake: make(chan struct{}, 1)}
|
||||
return g, &now, f, r
|
||||
}
|
||||
|
||||
func write(t *testing.T, path, content string) {
|
||||
@@ -286,6 +296,119 @@ func TestTheNetworksServersAnswerWhileTheMeshsResolversAreSilent(t *testing.T) {
|
||||
step(g, routed, "Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false")
|
||||
}
|
||||
|
||||
// The uplink's names are answered over the link carrying the default route, and move with the route: the
|
||||
// old link loses them and keeps its own search domain (design 08 sign-in-page §7, §9; R-4, R-5).
|
||||
func TestUplinkNamesMoveWithTheDefaultRoute(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0", "wlan1")
|
||||
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
||||
f.domain = "Link 2 (wlan0): lan\n"
|
||||
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
||||
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.KeepUplinkNames(context.Background())
|
||||
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~check.example" {
|
||||
t.Fatalf("wlan0 was given %v", f.changed)
|
||||
}
|
||||
if g.applied.Link != "wlan0" || strings.Join(g.applied.Names, ",") != "check.example" {
|
||||
t.Fatalf("applied is %+v", g.applied)
|
||||
}
|
||||
applied := contentOf(t, r.UplinkNamesAppliedFile)
|
||||
if !strings.Contains(applied, "wlan0") || !strings.Contains(applied, "check.example") {
|
||||
t.Errorf("uplink-names.applied does not say so: %s", applied)
|
||||
}
|
||||
// The route moves to wlan1: wlan0, as resolved now holds it, keeps "lan" and the uplink's name.
|
||||
f.domain = "Link 2 (wlan0): lan ~check.example\nLink 3 (wlan1):\n"
|
||||
f.ip = "default via 10.1.0.1 dev wlan1 metric 50\n"
|
||||
f.changed = nil
|
||||
g.KeepUplinkNames(context.Background())
|
||||
if len(f.changed) != 2 {
|
||||
t.Fatalf("moving the route changed %v", f.changed)
|
||||
}
|
||||
if strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan" {
|
||||
t.Errorf("wlan0 lost the uplink's name but not its own: %v", f.changed[0])
|
||||
}
|
||||
if strings.Join(f.changed[1], " ") != "resolvectl domain wlan1 ~check.example" {
|
||||
t.Errorf("wlan1 was not given the uplink's name: %v", f.changed[1])
|
||||
}
|
||||
if g.applied.Link != "wlan1" {
|
||||
t.Errorf("applied is now %+v", g.applied)
|
||||
}
|
||||
}
|
||||
|
||||
// A guard restarted with an applied file takes the uplink's names off the link it names, never inferring
|
||||
// which (design 08 sign-in-page §8, §9; R-6).
|
||||
func TestARestartedGuardTakesTheUplinkNamesOffTheLinkItNames(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0", "wlan1")
|
||||
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
||||
f.domain = "Link 2 (wlan0): lan ~check.example\nLink 3 (wlan1):\n"
|
||||
f.ip = "default via 10.1.0.1 dev wlan1 metric 50\n" // the route has already moved to wlan1
|
||||
applied := UplinkApplied{Names: []string{"check.example"}, Link: "wlan0", At: time.Now()}
|
||||
raw, _ := json.Marshal(applied)
|
||||
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.applied = ReadUplinkApplied(r.UplinkNamesAppliedFile) // what the guard does at its start
|
||||
g.KeepUplinkNames(context.Background())
|
||||
if len(f.changed) != 2 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan" ||
|
||||
strings.Join(f.changed[1], " ") != "resolvectl domain wlan1 ~check.example" {
|
||||
t.Errorf("the restarted guard did %v", f.changed)
|
||||
}
|
||||
}
|
||||
|
||||
// A link holding only the uplink's names is still given its default route back while the mesh's resolvers
|
||||
// are silent: the guard's own names are left out of whether it is judged `route` routed (design 08
|
||||
// sign-in-page §7, §9; DMC-2; issue 1981's AC1; R-7).
|
||||
func TestALinkOfOnlyUplinkNamesStillGetsItsDefaultRouteBack(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "tun0")
|
||||
g.Resolver.Answers = func(context.Context, string) bool { return false } // the mesh is silent
|
||||
f.dns = "Global: 10.42.0.1\nLink 7 (tun0): 10.9.0.1\n"
|
||||
f.domain = "Global: ~.\nLink 7 (tun0): ~check.example\n" // only the uplink's name, no search domain
|
||||
f.defaults = "Link 7 (tun0): no\n"
|
||||
applied := UplinkApplied{Names: []string{"check.example"}, Link: "tun0", At: time.Now()}
|
||||
raw, _ := json.Marshal(applied)
|
||||
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.KeepRoutes(context.Background())
|
||||
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl default-route tun0 true" {
|
||||
t.Errorf("the fallback did %v, want tun0 given its default route back", f.changed)
|
||||
}
|
||||
}
|
||||
|
||||
// A refusal by resolved is journalled once per change, never every round (design 08 sign-in-page §8; R-8).
|
||||
func TestARefusalByResolvedIsJournalledOncePerChange(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0")
|
||||
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
||||
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
||||
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
refuse := true
|
||||
g.Resolver.Change = func(ctx context.Context, name string, args ...string) (string, error) {
|
||||
if refuse && len(args) > 0 && args[0] == "domain" {
|
||||
return "", errors.New("refused: not a domain mesh owns")
|
||||
}
|
||||
return f.change(ctx, name, args...)
|
||||
}
|
||||
var said []string
|
||||
g.Log = func(format string, a ...any) { said = append(said, fmt.Sprintf(format, a...)) }
|
||||
g.KeepUplinkNames(context.Background())
|
||||
g.KeepUplinkNames(context.Background()) // the file and the link are unchanged: no new refusal to say
|
||||
count := 0
|
||||
for _, s := range said {
|
||||
if strings.Contains(s, "refuses") {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("the refusal was journalled %d times, want once: %v", count, said)
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
@@ -47,6 +47,9 @@ func guard() error {
|
||||
return err
|
||||
}
|
||||
g.writeHistory()
|
||||
// Read what it applied last, so a restart takes the uplink's names off the link it names, never
|
||||
// inferring which (design 08 sign-in-page §8).
|
||||
g.applied = ReadUplinkApplied(g.Resolver.UplinkNamesAppliedFile)
|
||||
go g.Run(ctx)
|
||||
return g.Serve(ctx, Socket)
|
||||
}
|
||||
|
||||
@@ -258,8 +258,20 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) {
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
// The guard's own uplink names (design 08 sign-in-page §7, DMC-2) are left out of whether a link is
|
||||
// judged `route` routed: a link holding only those names is still the fallback's to give a default
|
||||
// route back while the mesh's resolvers are silent, never one the fallback takes for a VPN's.
|
||||
applied := ReadUplinkApplied(r.UplinkNamesAppliedFile)
|
||||
marked := map[string]bool{}
|
||||
for _, n := range applied.Names {
|
||||
marked["~"+n] = true
|
||||
}
|
||||
for _, name := range names {
|
||||
s := Scope{Link: name, Servers: lServers[name], Domains: orEmpty(unrouted(lDomains[name])), routed: onlyRouting(lDomains[name])}
|
||||
judge := lDomains[name]
|
||||
if name == applied.Link && len(marked) > 0 {
|
||||
judge = withoutMarked(judge, marked)
|
||||
}
|
||||
s := Scope{Link: name, Servers: lServers[name], Domains: orEmpty(unrouted(lDomains[name])), routed: onlyRouting(judge)}
|
||||
if d, ok := lDefault[name]; ok && len(d) > 0 {
|
||||
yes := d[0] == "yes"
|
||||
s.DefaultRoute = &yes
|
||||
@@ -269,6 +281,17 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// 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))
|
||||
for _, d := range domains {
|
||||
if !marked[d] {
|
||||
out = append(out, d)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func orEmpty(s []string) []string {
|
||||
if s == nil {
|
||||
return []string{}
|
||||
|
||||
Reference in New Issue
Block a user