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.
This commit is contained in:
@@ -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, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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=<n>`.
|
||||
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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user