// The node-resolver seat's verbs, done by systemd-resolved (novox/hq ADR 0247): what is routed where, // route a set of domains to a set of servers over one link, and take a link's route away. // // **resolved holds the routes, not this code.** A link's servers and routing domains are resolved's own // per-link state, set through resolvectl and forgotten by resolved when the link goes. So nothing here // keeps a table that could disagree with what resolved does: `routes` reads resolved, and the two // transports that serve these verbs — the mesh, through the node's runtime as the operator account, and // the machine, through the guard's socket as root — run the same code against the same daemon. // // **It knows nothing of any VPN.** A link, domains and servers. What a VPN client pushed is its own // module's to read and hand over. package main import ( "bytes" "context" "encoding/json" "errors" "fmt" "net" "net/netip" "os" "os/exec" "path/filepath" "regexp" "sort" "strconv" "strings" "time" ) // Runner runs one command — as root when it changes something — and answers what it printed. type Runner func(ctx context.Context, name string, args ...string) (string, error) // escalated is the command as it is run: as given when this process is root (the guard), else through // sudo without a prompt (the runtime's account), as the hosts file's and the packet filter's verbs do. func escalated(uid int, name string, args []string) (string, []string) { if uid == 0 { return name, args } return "sudo", append([]string{"-n", name}, args...) } // execRunner runs a command that changes resolved's state, escalated. func execRunner(ctx context.Context, name string, args ...string) (string, error) { return run(ctx, true, name, args...) } // readRunner runs a command that only reads, as whoever this process is. func readRunner(ctx context.Context, name string, args ...string) (string, error) { return run(ctx, false, name, args...) } func run(ctx context.Context, escalate bool, name string, args ...string) (string, error) { ctx, cancel := context.WithTimeout(ctx, 15*time.Second) defer cancel() program, argv := name, args if escalate { program, argv = escalated(os.Getuid(), name, args) } var stdout, stderr bytes.Buffer cmd := exec.CommandContext(ctx, program, argv...) cmd.Stdout, cmd.Stderr = &stdout, &stderr err := cmd.Run() if err == nil { return stdout.String(), nil } said := strings.TrimSpace(stdout.String() + stderr.String()) if program == "sudo" { if errors.Is(err, exec.ErrNotFound) { return "", fmt.Errorf("%s needs root, and sudo is not installed here for the runtime's account to escalate with", name) } if regexp.MustCompile(`(?m)^sudo:`).MatchString(said) { return "", fmt.Errorf("%s needs root and the runtime's account may not run it without a prompt: %s", name, said) } } if said != "" { return "", fmt.Errorf("%s: %s", name, said) } return "", fmt.Errorf("%s failed: %v", name, err) } // Resolver is systemd-resolved on this machine, as the seat's verbs see it. type Resolver struct { // Change runs what changes resolved (escalated); Read what only reads it. Change, Read Runner // NetDir is where the machine's links are listed (/sys/class/net). 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 // PrivateInterfaceFile holds the private network's own interface name, rendered beside SuffixFile // (design 08 sign-in-page §7, DQ-1): never the private network's interface is the uplink's link. PrivateInterfaceFile string // UplinkNamesFile is where the uplink's holder's names are kept, one a line (design 08 sign-in-page §6). UplinkNamesFile string // UplinkNamesAppliedFile is what the guard last gave the link carrying the default route (§8). UplinkNamesAppliedFile 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. Now func() time.Time } // ThisResolver is the machine's. 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} r.Answers = r.asks r.WriteUplinkNames = r.writeUplinkNamesFile 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`). const SuffixPath = "/etc/node-resolver/suffix" // PrivateInterfacePath is the file the mesh renders the private network's own interface name into, // beside SuffixPath (the manifest's fact `private-interface`; design 08 sign-in-page §7, DQ-1). const PrivateInterfacePath = "/etc/node-resolver/private-interface" // UplinkNamesPath is where `uplink-names` keeps the host names, one a line, whole (design 08 sign-in-page // §6): root's, mode 0644, written through `sudo -n install` then `sudo -n mv -f`. const UplinkNamesPath = "/var/lib/node-resolver/uplink-names" // UplinkNamesAppliedPath is what the guard last gave the link carrying the default route (§8): the list, // the link, when, and resolved's refusal or empty. const UplinkNamesAppliedPath = "/var/lib/node-resolver/uplink-names.applied" // privateInterface is the private network's own interface name, as the mesh rendered it; empty before it // has (DQ-1). func (r *Resolver) privateInterface() string { raw, _ := os.ReadFile(r.PrivateInterfaceFile) return strings.TrimSpace(string(raw)) } // Scope is one place resolved sends names: the machine's global servers (the mesh's resolvers), or a link. type Scope struct { Link string `json:"link,omitempty"` Servers []string `json:"servers"` // Domains are the routing domains, without resolved's `~`: every name under one goes to these servers. 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. type Routes struct { // Mesh is the global scope: the mesh's resolvers, which answer every name nothing routes elsewhere. Mesh Scope `json:"mesh"` // Links is every link given servers of its own. Links []Scope `json:"links"` } var linkLine = regexp.MustCompile(`^Link\s+\d+\s+\(([^)]+)\):\s*(.*)$`) // perScope reads one resolvectl listing (`dns`, `domain`, `default-route`) into the global line and one // line per link. func perScope(out string) (global []string, links map[string][]string) { links = map[string][]string{} for _, line := range strings.Split(out, "\n") { line = strings.TrimSpace(line) if rest, ok := strings.CutPrefix(line, "Global:"); ok { global = strings.Fields(rest) continue } if m := linkLine.FindStringSubmatch(line); m != nil { links[m[1]] = strings.Fields(m[2]) } } 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 { out = append(out, strings.TrimPrefix(d, "~")) } return out } // Routes reads what resolved sends where. It changes nothing and needs no root. func (r *Resolver) Routes(ctx context.Context) (*Routes, error) { dns, err := r.Read(ctx, "resolvectl", "dns") if err != nil { return nil, fmt.Errorf("systemd-resolved does not answer: %w", err) } domain, err := r.Read(ctx, "resolvectl", "domain") if err != nil { return nil, fmt.Errorf("systemd-resolved does not answer: %w", err) } defaults, _ := r.Read(ctx, "resolvectl", "default-route") gServers, lServers := perScope(dns) gDomains, lDomains := perScope(domain) _, lDefault := perScope(defaults) out := &Routes{Mesh: Scope{Servers: orEmpty(gServers), Domains: orEmpty(unrouted(gDomains))}, Links: []Scope{}} names := make([]string, 0, len(lServers)) for name, servers := range lServers { if len(servers) > 0 { names = append(names, name) } } sort.Strings(names) for _, name := range names { 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 } out.Links = append(out.Links, s) } return out, nil } func orEmpty(s []string) []string { if s == nil { return []string{} } return s } // Routed is what a route did. type Routed struct { Link string `json:"link"` Servers []string `json:"servers"` Domains []string `json:"domains"` Said string `json:"said"` } var ( linkName = regexp.MustCompile(`^[A-Za-z0-9_.:@-]{1,15}$`) domainName = regexp.MustCompile(`^([a-z0-9_]([a-z0-9_-]{0,61}[a-z0-9_])?\.)*[a-z0-9_]([a-z0-9_-]{0,61}[a-z0-9_])?$`) separators = regexp.MustCompile(`[\s,]+`) ) // Split reads a list given as one string, separated by spaces or commas, or as a list. func Split(v any) []string { var raw []string switch v := v.(type) { case string: raw = separators.Split(v, -1) case []any: for _, x := range v { if s, ok := x.(string); ok { raw = append(raw, separators.Split(s, -1)...) } } case []string: for _, s := range v { raw = append(raw, separators.Split(s, -1)...) } } out := []string{} for _, s := range raw { if s = strings.TrimSpace(s); s != "" { out = append(out, s) } } return out } // suffix is the mesh's own domain, as the mesh rendered it; "internal" when it has not been yet. func (r *Resolver) suffix() string { raw, err := os.ReadFile(r.SuffixFile) if s := strings.Trim(strings.TrimSpace(string(raw)), "."); err == nil && s != "" { return strings.ToLower(s) } return "internal" } // checkLink refuses a link that is not one, loopback, and one that is not on this machine now. func (r *Resolver) checkLink(link string) error { if !linkName.MatchString(link) { return fmt.Errorf("%q is not a link's name", link) } if link == "lo" { return errors.New("loopback carries no servers of its own") } if _, err := os.Stat(filepath.Join(r.NetDir, link)); err != nil { return fmt.Errorf("there is no link %q on this machine now", link) } return nil } // Domains reads the domains to route: lower-cased, without resolved's `~` or a final dot, each once. The // root and the mesh's own domain are refused: routing either away would send the mesh's names, or every // name, to servers that are not the mesh's (ADR 0223, ADR 0247). func (r *Resolver) Domains(given []string) ([]string, error) { suffix := r.suffix() seen := map[string]bool{} out := []string{} for _, d := range given { d = strings.ToLower(strings.TrimSuffix(strings.TrimPrefix(d, "~"), ".")) if d == "" { return nil, errors.New("the root domain is every name: only the mesh's resolvers answer every name") } if !domainName.MatchString(d) || len(d) > 253 { return nil, fmt.Errorf("%q is not a domain", d) } if d == suffix || strings.HasSuffix(d, "."+suffix) { return nil, fmt.Errorf("%q is the mesh's own domain: the mesh's names are answered by the mesh's resolvers alone", d) } if !seen[d] { seen[d] = true out = append(out, d) } } if len(out) == 0 { return nil, errors.New("no domain given: a link's servers answer only the domains routed to them") } if len(out) > 64 { return nil, fmt.Errorf("%d domains; at most 64", len(out)) } return out, nil } // Servers reads the servers: addresses, each once, at most eight. func Servers(given []string) ([]string, error) { seen := map[string]bool{} out := []string{} for _, s := range given { a, err := netip.ParseAddr(s) if err != nil { return nil, fmt.Errorf("%q is not an address", s) } if a.IsUnspecified() || a.IsMulticast() { return nil, fmt.Errorf("%s cannot answer names", s) } if !seen[a.String()] { seen[a.String()] = true out = append(out, a.String()) } } if len(out) == 0 { return nil, errors.New("no server given") } if len(out) > 8 { return nil, fmt.Errorf("%d servers; at most 8", len(out)) } return out, nil } // hostNameLabel matches one label of a host name: letters, digits and hyphens. var hostNameLabel = regexp.MustCompile(`^[a-z0-9-]+$`) // HostNames checks the names given to `uplink-names` (design 08 sign-in-page §2): 0 to 8 host names, each // 1 to 253 bytes of letters, digits, hyphens and dots, lower-cased. Refused, naming the name and the // reason: a name that is not a host name (a wildcard, a trailing dot, a label over 63 bytes), the root, // the mesh's own domain or a name under it (ADR 0247 §12's rule for `route`, for the same reason), or more // than 8 names. func (r *Resolver) HostNames(given []string) ([]string, error) { suffix := r.suffix() out := make([]string, 0, len(given)) for _, n := range given { n = strings.ToLower(strings.TrimSpace(n)) if n == "" || n == "." { return nil, errors.New("the root is not a host name: only the mesh's resolvers answer every name") } if strings.Contains(n, "*") { return nil, fmt.Errorf("%q is not a host name: a wildcard", n) } if strings.HasSuffix(n, ".") { return nil, fmt.Errorf("%q is not a host name: a trailing dot", n) } if len(n) > 253 { return nil, fmt.Errorf("%q is not a host name: over 253 bytes", n) } for _, label := range strings.Split(n, ".") { if len(label) > 63 { return nil, fmt.Errorf("%q is not a host name: a label over 63 bytes", n) } if !hostNameLabel.MatchString(label) { return nil, fmt.Errorf("%q is not a host name", n) } } if n == suffix || strings.HasSuffix(n, "."+suffix) { return nil, fmt.Errorf("%q is the mesh's own domain: the mesh's names are answered by the mesh's resolvers alone", n) } out = append(out, n) } if len(out) > 8 { return nil, fmt.Errorf("%d names; at most 8", len(out)) } return out, nil } // writeUplinkNamesFile writes UplinkNamesFile whole (design 08 sign-in-page §6): `sudo -n install -m 0644 // -o root -g root /dev/stdin .new` with the lines on its input, then `sudo -n mv -f .new // `, so a reader never sees it half-written. As root (the guard never calls this; only the verb, // over the bus, does), both run unescalated. func (r *Resolver) writeUplinkNamesFile(ctx context.Context, content []byte) error { ctx, cancel := context.WithTimeout(ctx, 15*time.Second) defer cancel() newPath := r.UplinkNamesFile + ".new" program, args := escalated(os.Getuid(), "install", []string{"-m", "0644", "-o", "root", "-g", "root", "/dev/stdin", newPath}) cmd := exec.CommandContext(ctx, program, args...) cmd.Stdin = bytes.NewReader(content) var stderr bytes.Buffer cmd.Stderr = &stderr if err := cmd.Run(); err != nil { if said := strings.TrimSpace(stderr.String()); said != "" { return fmt.Errorf("install: %s", said) } return fmt.Errorf("install failed: %w", err) } _, err := r.Change(ctx, "mv", "-f", newPath, r.UplinkNamesFile) return err } // UplinkNames is `uplink-names`'s answer (design 08 sign-in-page §2): the names now given, the link they // are answered over now (empty when no link carries a default route), and when the list was set. type UplinkNames struct { Names []string `json:"names"` Link string `json:"link"` SetAt time.Time `json:"set_at"` } // SetUplinkNames writes the host names the uplink's holder gives for its network's own check, through // UplinkNamesFile (§6), and answers them with the link carrying the default route now (§2, DQ-2): empty // when there is no such link. func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*UplinkNames, error) { names, err := r.HostNames(given) if err != nil { return nil, err } content := []byte(strings.Join(names, "\n")) if len(names) > 0 { content = append(content, '\n') } if err := r.WriteUplinkNames(ctx, content); err != nil { return nil, err } link := "" if routes, rerr := r.Routes(ctx); rerr == nil { link, _ = r.DefaultRouteLink(ctx, routes) } return &UplinkNames{Names: names, Link: link, SetAt: r.Now()}, nil } // ReadUplinkNames reads the host names `uplink-names` last wrote, one a line; empty before it has. func ReadUplinkNames(path string) []string { raw, err := os.ReadFile(path) if err != nil { return []string{} } out := []string{} for _, line := range strings.Split(string(raw), "\n") { if line = strings.TrimSpace(line); line != "" { out = append(out, line) } } return out } // UplinkApplied is what the guard last gave the link carrying the default route (design 08 sign-in-page // §8): the list, the link, when, and resolved's refusal or empty. type UplinkApplied struct { Names []string `json:"names"` Link string `json:"link"` At time.Time `json:"at"` Refused string `json:"refused,omitempty"` } // ReadUplinkApplied reads what the guard applied last; the zero value before its first round, or after a // restart with nothing written yet. func ReadUplinkApplied(path string) UplinkApplied { raw, err := os.ReadFile(path) if err != nil { return UplinkApplied{} } var a UplinkApplied if json.Unmarshal(raw, &a) != nil { return UplinkApplied{} } return a } // kernelDefaultRoute is one of the kernel's default routes: its link and metric (the lowest wins). type kernelDefaultRoute struct { link string metric int } // kernelDefaultRoutes reads the kernel's default routes for one family ("-4" or "-6"); a route the kernel // names no metric for gets 0, its own highest priority. func (r *Resolver) kernelDefaultRoutes(ctx context.Context, family string) ([]kernelDefaultRoute, error) { out, err := r.Read(ctx, "ip", family, "route", "show", "default") if err != nil { return nil, fmt.Errorf("the kernel's default routes: %w", err) } var routes []kernelDefaultRoute for _, line := range strings.Split(out, "\n") { fields := strings.Fields(line) if len(fields) == 0 || fields[0] != "default" { continue } var kr kernelDefaultRoute for i := 0; i+1 < len(fields); i++ { switch fields[i] { case "dev": kr.link = fields[i+1] case "metric": if n, err := strconv.Atoi(fields[i+1]); err == nil { kr.metric = n } } } if kr.link != "" { routes = append(routes, kr) } } return routes, nil } // DefaultRouteLink is the link that carries the default route now (design 08 sign-in-page §7, §9; DQ-1, // DQ-2): among the kernel's IPv4 default routes, or the IPv6 ones when there is no IPv4 one, the one with // the lowest metric whose link is neither one `route` routed (a VPN's, read from resolved) nor the private // network's own interface; empty when there is no such link. func (r *Resolver) DefaultRouteLink(ctx context.Context, routes *Routes) (string, error) { private := r.privateInterface() routed := map[string]bool{} for _, l := range routes.Links { routed[l.Link] = l.routed } candidates, err := r.kernelDefaultRoutes(ctx, "-4") if err != nil { return "", err } if len(candidates) == 0 { if candidates, err = r.kernelDefaultRoutes(ctx, "-6"); err != nil { return "", err } } best, bestMetric := "", 0 for _, c := range candidates { if c.link == private || routed[c.link] { continue } if best == "" || c.metric < bestMetric { best, bestMetric = c.link, c.metric } } return best, nil } // Route sends these domains, and every name under them, to these servers over this link — and only them. // Whatever the link was given before is replaced. resolved forgets it when the link goes. func (r *Resolver) Route(ctx context.Context, link string, domains, servers []string) (*Routed, error) { if err := r.checkLink(link); err != nil { return nil, err } ds, err := r.Domains(domains) if err != nil { return nil, err } ss, err := Servers(servers) if err != nil { return nil, err } routing := make([]string, len(ds)) for i, d := range ds { routing[i] = "~" + d } // The link is never a default route: names no domain routes go to the mesh's resolvers, so set // first, before the servers, that no question but these domains' ever reaches it. steps := [][]string{ {"default-route", link, "false"}, append([]string{"domain", link}, routing...), append([]string{"dns", link}, ss...), } for _, s := range steps { if _, err := r.Change(ctx, "resolvectl", s...); err != nil { return nil, err } } return &Routed{Link: link, Servers: ss, Domains: ds, Said: fmt.Sprintf("%d domains go to %d servers over %s; every other name to the mesh's resolvers", len(ds), len(ss), link)}, nil } // Unrouted is what taking a route away did. type Unrouted struct { Link string `json:"link"` Said string `json:"said"` } // Unroute takes one link's route away. A link that has gone has nothing to take away. func (r *Resolver) Unroute(ctx context.Context, link string) (*Unrouted, error) { if !linkName.MatchString(link) || link == "lo" { return nil, fmt.Errorf("%q is not a link's name", link) } if _, err := os.Stat(filepath.Join(r.NetDir, link)); err != nil { return &Unrouted{Link: link, Said: link + " is not on this machine; resolved forgot its route with it"}, nil } if _, err := r.Change(ctx, "resolvectl", "revert", link); err != nil { return nil, err } return &Unrouted{Link: link, Said: link + "'s domains go to the mesh's resolvers again"}, nil } // OnlyTheMeshIsADefaultRoute keeps every link that has servers of its own from answering names nothing // routes to it: a network manager telling resolved a network's servers makes them a default route, and // then resolved asks them every name beside the mesh's resolvers. Their routing domains are kept — a // link's own domains still go to it. Answers the links it changed. func (r *Resolver) OnlyTheMeshIsADefaultRoute(ctx context.Context) ([]string, error) { routes, err := r.Routes(ctx) if err != nil { return nil, err } var changed []string for _, l := range routes.Links { if l.DefaultRoute == nil || !*l.DefaultRoute { continue } if _, err := r.Change(ctx, "resolvectl", "default-route", l.Link, "false"); err != nil { return changed, err } changed = append(changed, l.Link) } 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 }