Instance inventory: read procfs where netlink is refused (CARLOS)
On CARLOS the instance units allow only AF_INET, AF_INET6 and AF_UNIX
sockets, so net.InterfaceAddrs failed ("netlinkrib: address family not
supported by protocol"), the inventory was never ready and lookups were
never offered. The scan now falls back to /proc/self/net/fib_trie and
if_inet6, as the platform reads its boxes' own addresses; /proc/self/net
because ProcSubset=pid hides /proc/net. A list that can't be read whole
fails the scan and leaves the inventory not ready, with no card.
The scan keeps only addresses websource.PublicAddress accepts, as
Research's site inventory does: the guard refuses every other address
before it asks the inventory, so private, tailnet and bridge addresses
no longer need prefixes listed or confirmed. No new copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+445
−44
docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md+21 −0internal/equipo/chat/instance_addrs.go+182 −0internal/equipo/chat/instance_inventory.go+13 −30internal/equipo/chat/instance_inventory_test.go+226 −13internal/equipo/chat/store.go+3 −1
diff --git a/docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md b/docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md| index 672ce649..b7ec9fd2 100644 |
| --- a/docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md |
| +++ b/docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md |
| @@ -8241,3 +8241,24 @@ R1: the tap carries what was shown (web form, native call, `wire.RunNow.offer`: |
| - release pinning the prediction read at the tap whatever was shown -> FAIL TestRunNowRefusesAChangedOffer (a tap showing Codex was accepted); restored -> ok. |
| - the job card not moving when the plan behind carry_on changes -> FAIL TestCarryOnPinsItsConnection (carry on after the plan changed was not refused); restored -> ok. |
| - runner ignoring the pin -> FAIL TestSchedulePinnedReleaseRunsOnlyThere (ran on claude-a) and TestSchedulePinnedReleaseNeverBillsMetered (ran on TensorX); restored -> ok. |
| + |
| +## Lookups: the instance inventory under CARLOS (fix-lookup-inventory, 2026-11-29) |
| + |
| +equipo-staging (2301e3c1, on CARLOS) logged `instance inventory err="route ip+net: netlinkrib: address family not supported by protocol"` at every scan, so the inventory was never ready and lookups were never offered. CARLOS's instance units set `RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX` (platform `internal/host/units/carlos-instance@.service` and every other instance template), which refuses the netlink socket `net.InterfaceAddrs` opens. The platform hit the same on its own units in v0.25.0 and reads procfs instead (`internal/instancectl/localaddrs.go`). |
| + |
| +The scan (`internal/equipo/chat/instance_addrs.go`) now tries netlink and, when it is refused, reads `/proc/self/net/fib_trie` (IPv4 LOCAL routes) and `/proc/self/net/if_inet6`. `/proc/self/net`, not `/proc/net`: the units mount `/proc` with `ProcSubset=pid` and `ProtectProc=invisible`, which hide the `/proc/net` link but leave the process's own directory (checked with `unshare -Urpm` and `mount -t proc -o subset=pid,hidepid=invisible`: `/proc/net` absent, `/proc/self/net/fib_trie` and `if_inet6` readable). An instance shares the box's network namespace, so these are the box's addresses. A list that can't be read whole is an error, never a shorter list: both sources failing, a fib_trie with no LOCAL route, a LOCAL route for a whole range other than loopback's, or a bad if_inet6 line each fail the scan, which leaves the inventory not ready and raises no card. |
| + |
| +The scan keeps the addresses a lookup could otherwise reach, those `websource.PublicAddress` accepts, as Research's site inventory does (`internal/runner/thin/research.go`). The guard refuses every other address before it asks the inventory, in the DNS check and again in the dial's Control hook, so listing them only asked an owner to list and confirm prefixes for a box's VPC, tailnet and bridge addresses that nothing dials, and a new bridge asked again. No reachable address changed: Inside differs only for addresses the guard has already refused. The existing tests moved from documentation ranges (refused by PublicAddress) to public ones. No new copy: the card is unchanged. |
| + |
| +Tests: `TestInventoryWithoutNetlink` (netlink refused with staging's error, procfs fixtures: the card shows the prefixes and the public addresses, Confirm makes it ready), `TestInventoryUnlistedStaysClosed`, `TestInventoryProcfsShapes` (local range, no local route, bad if_inet6, no IPv6), `TestInventoryIgnoresAddressesNeverReached`. Red first: `TestInventoryWithoutNetlink` failed with staging's own error. Guards observed failing, then restored: |
| + |
| +- a local range accepted -> FAIL TestInventoryProcfsShapes/local_range; restored -> ok. |
| +- the no-LOCAL-route check removed -> FAIL TestInventoryProcfsShapes/no_local_route; restored -> ok. |
| +- the netlink cause dropped from the error -> FAIL TestInventoryUnlistedStaysClosed; restored -> ok. |
| +- non-public addresses kept (IsGlobalUnicast) -> FAIL TestInventoryIgnoresAddressesNeverReached; restored -> ok. |
| +- a bad if_inet6 read as a shorter list -> FAIL TestInventoryProcfsShapes/bad_if_inet6; restored -> ok. |
| +- no procfs fallback -> FAIL TestInventoryWithoutNetlink; restored -> ok. |
| + |
| +`CGO_ENABLED=0 go test ./internal/equipo/chat ./internal/websource/... ./internal/plan3test -count=1`: ok. |
| + |
| +Operator, after release: `EQUIPO_INSTANCE_PREFIXES` must cover every public address the box holds (the card lists them); private ones no longer need listing. |
diff --git a/internal/equipo/chat/instance_addrs.go b/internal/equipo/chat/instance_addrs.go| new file mode 100644 |
| index 00000000..7a2367c9 |
| --- /dev/null |
| +++ b/internal/equipo/chat/instance_addrs.go |
| @@ -0,0 +1,182 @@ |
| +package chat |
| + |
| +import ( |
| + "bufio" |
| + "bytes" |
| + "encoding/hex" |
| + "errors" |
| + "fmt" |
| + "io/fs" |
| + "net" |
| + "net/netip" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strconv" |
| + "strings" |
| + |
| + "amadan.net/oficina/equipo/internal/websource" |
| +) |
| + |
| +// The instance's own addresses, for its inventory (instance_inventory.go). |
| +// |
| +// net.InterfaceAddrs asks the kernel over a netlink socket. CARLOS's |
| +// instance units allow only AF_INET, AF_INET6 and AF_UNIX sockets |
| +// (RestrictAddressFamilies=), so there it fails ("route ip+net: |
| +// netlinkrib: address family not supported by protocol") and the |
| +// inventory was never ready. The same addresses are then read from |
| +// procfs, as CARLOS reads its boxes' own (platform |
| +// internal/instancectl/localaddrs.go): IPv4 from fib_trie's LOCAL routes, |
| +// IPv6 from if_inet6. The directory is /proc/self/net, not /proc/net: |
| +// the units mount /proc with ProcSubset=pid, which hides the /proc/net |
| +// link and leaves the process's own directory. An instance shares the |
| +// box's network namespace, so these are the box's addresses. |
| +// |
| +// A list that can't be read whole is an error, never a shorter list. |
| + |
| +// procSelfNet is where the procfs sources are read. |
| +const procSelfNet = "/proc/self/net" |
| + |
| +// instanceAddrs is what the inventory is about of the instance's |
| +// interface addresses: those a lookup could otherwise reach. Every other |
| +// address websource.PublicAddress refuses before the inventory is asked |
| +// (as Research's site inventory, internal/runner/thin/research.go), so |
| +// listing it would only ask an owner to confirm prefixes for addresses |
| +// nothing dials. Canonical, once each, sorted. |
| +func (s *Store) instanceAddrs() ([]netip.Addr, error) { |
| + all, err := s.interfaceAddrs() |
| + if err != nil { |
| + return nil, err |
| + } |
| + var out []netip.Addr |
| + for _, a := range all { |
| + if a = a.Unmap(); websource.PublicAddress(a) && !slices.Contains(out, a) { |
| + out = append(out, a) |
| + } |
| + } |
| + slices.SortFunc(out, func(a, b netip.Addr) int { return a.Compare(b) }) |
| + return out, nil |
| +} |
| + |
| +// interfaceAddrs lists every address on the instance's interfaces: |
| +// netlink, or procfs where netlink is refused. |
| +func (s *Store) interfaceAddrs() ([]netip.Addr, error) { |
| + list := s.ifaceAddrs |
| + if list == nil { |
| + list = net.InterfaceAddrs |
| + } |
| + raw, nlErr := list() |
| + if nlErr == nil { |
| + var out []netip.Addr |
| + for _, a := range raw { |
| + n, ok := a.(*net.IPNet) |
| + if !ok { |
| + continue |
| + } |
| + if addr, ok := netip.AddrFromSlice(n.IP); ok { |
| + out = append(out, addr) |
| + } |
| + } |
| + return out, nil |
| + } |
| + dir := s.procNet |
| + if dir == "" { |
| + dir = procSelfNet |
| + } |
| + out, err := procNetAddrs(dir) |
| + if err != nil { |
| + return nil, errors.Join(nlErr, err) |
| + } |
| + return out, nil |
| +} |
| + |
| +// procNetAddrs reads dir's fib_trie and if_inet6. No if_inet6 is a |
| +// kernel without IPv6, so no IPv6 address. |
| +func procNetAddrs(dir string) ([]netip.Addr, error) { |
| + b, err := os.ReadFile(filepath.Join(dir, "fib_trie")) |
| + if err != nil { |
| + return nil, err |
| + } |
| + out, err := parseFibTrie(b) |
| + if err != nil { |
| + return nil, fmt.Errorf("fib_trie: %w", err) |
| + } |
| + b, err = os.ReadFile(filepath.Join(dir, "if_inet6")) |
| + switch { |
| + case errors.Is(err, fs.ErrNotExist): |
| + return out, nil |
| + case err != nil: |
| + return nil, err |
| + } |
| + v6, err := parseIfInet6(b) |
| + if err != nil { |
| + return nil, fmt.Errorf("if_inet6: %w", err) |
| + } |
| + return append(out, v6...), nil |
| +} |
| + |
| +// parseFibTrie reads fib_trie: a leaf line "|-- <addr>" is followed by |
| +// one line per route at it, "/<len> <scope> <type>". A LOCAL route is a |
| +// destination the box answers for itself; one for a single address is an |
| +// interface address. A LOCAL route for a range (the box answering for |
| +// every address in it) is refused unless it is loopback's: the inventory |
| +// holds addresses, and a range left out would be reachable. A file with |
| +// no LOCAL route at all is refused too: every kernel has 127.0.0.1, so it |
| +// isn't the file this parser knows. |
| +func parseFibTrie(b []byte) ([]netip.Addr, error) { |
| + var out []netip.Addr |
| + var leaf netip.Addr |
| + local := false |
| + sc := bufio.NewScanner(bytes.NewReader(b)) |
| + for sc.Scan() { |
| + line := strings.TrimSpace(sc.Text()) |
| + if a, ok := strings.CutPrefix(line, "|-- "); ok { |
| + leaf, _ = netip.ParseAddr(strings.TrimSpace(a)) |
| + continue |
| + } |
| + f := strings.Fields(line) |
| + if len(f) < 3 || !strings.HasPrefix(f[0], "/") || f[len(f)-1] != "LOCAL" || !leaf.IsValid() { |
| + continue |
| + } |
| + bits, err := strconv.Atoi(f[0][1:]) |
| + if err != nil { |
| + return nil, fmt.Errorf("%q is not a route", line) |
| + } |
| + local = true |
| + p, err := leaf.Prefix(bits) |
| + switch { |
| + case err != nil: |
| + return nil, fmt.Errorf("%q is not a route", line) |
| + case p.IsSingleIP(): |
| + out = append(out, leaf) |
| + case !p.Addr().IsLoopback(): |
| + return nil, fmt.Errorf("a local route for %s", p) |
| + } |
| + } |
| + if err := sc.Err(); err != nil { |
| + return nil, err |
| + } |
| + if !local { |
| + return nil, errors.New("no local route") |
| + } |
| + return out, nil |
| +} |
| + |
| +// parseIfInet6 reads if_inet6: each line starts with an address as 32 |
| +// hex digits. |
| +func parseIfInet6(b []byte) ([]netip.Addr, error) { |
| + var out []netip.Addr |
| + sc := bufio.NewScanner(bytes.NewReader(b)) |
| + for sc.Scan() { |
| + f := strings.Fields(sc.Text()) |
| + if len(f) == 0 { |
| + continue |
| + } |
| + raw, err := hex.DecodeString(f[0]) |
| + if err != nil || len(raw) != 16 { |
| + return nil, fmt.Errorf("%q is not an address", f[0]) |
| + } |
| + out = append(out, netip.AddrFrom16([16]byte(raw))) |
| + } |
| + return out, sc.Err() |
| +} |
diff --git a/internal/equipo/chat/instance_inventory.go b/internal/equipo/chat/instance_inventory.go| index 892023b1..afe2db9d 100644 |
| --- a/internal/equipo/chat/instance_inventory.go |
| +++ b/internal/equipo/chat/instance_inventory.go |
| @@ -4,7 +4,6 @@ import ( |
| "context" |
| "encoding/json" |
| "errors" |
| - "net" |
| "net/netip" |
| "slices" |
| "strings" |
| @@ -20,11 +19,13 @@ import ( |
| // The instance inventory (spec 2026-11-26-assistant-abilities §4.3; MCP |
| // plan Task 7): the network the instance itself is on, which no request |
| // to the public web may reach. The operator lists its prefixes |
| -// (EQUIPO_INSTANCE_PREFIXES); the instance scans its own interfaces at |
| -// start and hourly; an Equipo owner confirms. Until every listed prefix |
| -// is confirmed and every address seen is inside one, the inventory isn't |
| -// ready, and nothing that needs it (lookups, services) runs; each change |
| -// asks every owner once, on an instance_inventory card. |
| +// (EQUIPO_INSTANCE_PREFIXES); the instance scans its own interfaces' |
| +// public addresses at start and hourly (instance_addrs.go: netlink, or |
| +// procfs where it is refused, as on CARLOS); an Equipo owner confirms. |
| +// Until every listed prefix is confirmed and every address seen is inside |
| +// one, the inventory isn't ready, and nothing that needs it (lookups, |
| +// services) runs; each change asks every owner once, on an |
| +// instance_inventory card. |
| // instancePrefixRecord is a prefix an owner confirmed: who, when, on |
| // which card. Never removed. |
| @@ -199,35 +200,17 @@ func inventoryRows(tx *gorm.DB) ([]netip.Prefix, []netip.Addr, error) { |
| return prefixes, addrs, nil |
| } |
| -// ScanInstanceAddresses records the global unicast addresses of the |
| -// instance's interfaces (at start and hourly) and, when the inventory |
| -// isn't ready with prefixes listed, asks every owner to confirm it: once |
| -// per change in what the card shows. |
| +// ScanInstanceAddresses records the public addresses of the instance's |
| +// interfaces (instanceAddrs: netlink, or procfs where it is refused, at |
| +// start and hourly) and, when the inventory isn't ready with prefixes |
| +// listed, asks every owner to confirm it: once per change in what the |
| +// card shows. Addresses that can't be listed leave it not ready. |
| func (s *Store) ScanInstanceAddresses(ctx context.Context) error { |
| - list := s.ifaceAddrs |
| - if list == nil { |
| - list = net.InterfaceAddrs |
| - } |
| - raw, err := list() |
| + seen, err := s.instanceAddrs() |
| if err != nil { |
| s.instance.scanned.Store(false) |
| return err |
| } |
| - var seen []netip.Addr |
| - for _, a := range raw { |
| - n, ok := a.(*net.IPNet) |
| - if !ok { |
| - continue |
| - } |
| - addr, ok := netip.AddrFromSlice(n.IP) |
| - if !ok { |
| - continue |
| - } |
| - if addr = addr.Unmap(); addr.IsGlobalUnicast() && !slices.Contains(seen, addr) { |
| - seen = append(seen, addr) |
| - } |
| - } |
| - slices.SortFunc(seen, func(a, b netip.Addr) int { return a.Compare(b) }) |
| listed := s.listedPrefixes() |
| var snap *inventorySnap |
| err = s.tx(ctx, func(t *txn) error { |
diff --git a/internal/equipo/chat/instance_inventory_test.go b/internal/equipo/chat/instance_inventory_test.go| index 2079f074..ba66e8ec 100644 |
| --- a/internal/equipo/chat/instance_inventory_test.go |
| +++ b/internal/equipo/chat/instance_inventory_test.go |
| @@ -3,8 +3,14 @@ package chat |
| import ( |
| "context" |
| "errors" |
| + "io/fs" |
| "net" |
| "net/netip" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "syscall" |
| "testing" |
| "amadan.net/oficina/equipo/internal/websource/guard" |
| @@ -47,7 +53,7 @@ func openInventoryCards(t *testing.T, h *abilitiesHarness) map[string]cardRecord |
| // is asked. |
| func TestInventoryNotReadyWithoutPrefixes(t *testing.T) { |
| h := newAbilitiesHarness(t) |
| - scanning(h, "203.0.113.5") |
| + scanning(h, "192.0.43.5") |
| scan(t, h) |
| if err := h.s.InstanceInventory().Ready(context.Background()); !errors.Is(err, guard.ErrInventoryUnconfirmed) { |
| t.Errorf("ready with no prefixes: %v", err) |
| @@ -63,10 +69,10 @@ func TestInventoryNotReadyWithoutPrefixes(t *testing.T) { |
| func TestInventoryCardAndConfirm(t *testing.T) { |
| h := newAbilitiesHarness(t) |
| ctx := context.Background() |
| - if err := h.s.SetInstancePrefixes("203.0.113.0/24"); err != nil { |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil { |
| t.Fatal(err) |
| } |
| - scanning(h, "203.0.113.5") |
| + scanning(h, "192.0.43.5") |
| scan(t, h) |
| if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) { |
| t.Fatalf("ready before Confirm: %v", err) |
| @@ -88,7 +94,7 @@ func TestInventoryCardAndConfirm(t *testing.T) { |
| }); err != nil { |
| t.Fatal(err) |
| } |
| - if shown.Inventory == nil || len(shown.Inventory.Addresses) != 2 || shown.Inventory.Addresses[0] != "203.0.113.0/24" || shown.Inventory.Addresses[1] != "203.0.113.5" { |
| + if shown.Inventory == nil || len(shown.Inventory.Addresses) != 2 || shown.Inventory.Addresses[0] != "192.0.43.0/24" || shown.Inventory.Addresses[1] != "192.0.43.5" { |
| t.Errorf("the card shows %+v", shown.Inventory) |
| } |
| if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil { |
| @@ -108,16 +114,16 @@ func TestInventoryCardAndConfirm(t *testing.T) { |
| func TestInventoryDriftStopsRequests(t *testing.T) { |
| h := newAbilitiesHarness(t) |
| ctx := context.Background() |
| - if err := h.s.SetInstancePrefixes("203.0.113.0/24"); err != nil { |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil { |
| t.Fatal(err) |
| } |
| - scanning(h, "203.0.113.5") |
| + scanning(h, "192.0.43.5") |
| scan(t, h) |
| c := openInventoryCards(t, h)["owner@example.com"] |
| if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil { |
| t.Fatal(err) |
| } |
| - scanning(h, "203.0.113.5", "198.51.100.7") |
| + scanning(h, "192.0.43.5", "192.0.32.7") |
| scan(t, h) |
| if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) { |
| t.Errorf("ready with an address outside: %v", err) |
| @@ -132,13 +138,13 @@ func TestInventoryDriftStopsRequests(t *testing.T) { |
| // it has seen. |
| func TestInventoryInside(t *testing.T) { |
| h := newAbilitiesHarness(t) |
| - if err := h.s.SetInstancePrefixes("203.0.113.0/24"); err != nil { |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil { |
| t.Fatal(err) |
| } |
| - scanning(h, "198.51.100.7") |
| + scanning(h, "192.0.32.7") |
| scan(t, h) |
| inv := h.s.InstanceInventory() |
| - for addr, want := range map[string]bool{"203.0.113.9": true, "198.51.100.7": true, "93.184.216.34": false} { |
| + for addr, want := range map[string]bool{"192.0.43.9": true, "192.0.32.7": true, "93.184.216.34": false} { |
| if got := inv.Inside(netip.MustParseAddr(addr)); got != want { |
| t.Errorf("Inside(%s) = %v", addr, got) |
| } |
| @@ -154,21 +160,22 @@ func TestInventoryInside(t *testing.T) { |
| func TestInventoryNeedsItsOwnScan(t *testing.T) { |
| h := newAbilitiesHarness(t) |
| ctx := context.Background() |
| - if err := h.s.SetInstancePrefixes("203.0.113.0/24"); err != nil { |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil { |
| t.Fatal(err) |
| } |
| - if err := h.s.db.G.Create(&instancePrefixRecord{Prefix: "203.0.113.0/24", ConfirmedBy: "owner@example.com"}).Error; err != nil { |
| + if err := h.s.db.G.Create(&instancePrefixRecord{Prefix: "192.0.43.0/24", ConfirmedBy: "owner@example.com"}).Error; err != nil { |
| t.Fatal(err) |
| } |
| if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) { |
| t.Errorf("ready before any scan: %v", err) |
| } |
| - scanning(h, "203.0.113.5") |
| + scanning(h, "192.0.43.5") |
| scan(t, h) |
| if err := h.s.InstanceInventory().Ready(ctx); err != nil { |
| t.Fatalf("not ready after a scan: %v", err) |
| } |
| h.s.ifaceAddrs = func() ([]net.Addr, error) { return nil, errors.New("no interfaces") } |
| + procNet(t, h, nil, nil) |
| if err := h.s.ScanInstanceAddresses(ctx); err == nil { |
| t.Fatal("the failing scan succeeded") |
| } |
| @@ -176,3 +183,209 @@ func TestInventoryNeedsItsOwnScan(t *testing.T) { |
| t.Errorf("ready after a failed scan: %v", err) |
| } |
| } |
| + |
| +// netlinkRefused has the store's scans fail to list interfaces as they |
| +// do on CARLOS, whose instance units allow only AF_INET, AF_INET6 and |
| +// AF_UNIX sockets: equipo-staging's log, "route ip+net: netlinkrib: |
| +// address family not supported by protocol". |
| +func netlinkRefused(h *abilitiesHarness) { |
| + h.s.ifaceAddrs = func() ([]net.Addr, error) { |
| + return nil, &net.OpError{Op: "route", Net: "ip+net", Err: os.NewSyscallError("netlinkrib", syscall.EAFNOSUPPORT)} |
| + } |
| +} |
| + |
| +// procNet points the store's procfs reads at a directory holding these |
| +// fib_trie and if_inet6 files; a nil one is absent. |
| +func procNet(t *testing.T, h *abilitiesHarness, fibTrie, ifInet6 []string) { |
| + t.Helper() |
| + dir := t.TempDir() |
| + for name, lines := range map[string][]string{"fib_trie": fibTrie, "if_inet6": ifInet6} { |
| + if lines == nil { |
| + continue |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, name), []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + h.s.procNet = dir |
| +} |
| + |
| +// A box's /proc/net/fib_trie: loopback, a private address on its VPC and |
| +// a public one, in the main and local tables. |
| +var boxFibTrie = []string{ |
| + "Main:", |
| + " +-- 0.0.0.0/0 3 0 5", |
| + " |-- 0.0.0.0", |
| + " /0 universe UNICAST", |
| + " +-- 127.0.0.0/8 2 0 2", |
| + " +-- 127.0.0.0/31 1 0 0", |
| + " |-- 127.0.0.0", |
| + " /8 host LOCAL", |
| + " |-- 127.0.0.1", |
| + " /32 host LOCAL", |
| + " |-- 127.255.255.255", |
| + " /32 link BROADCAST", |
| + " +-- 172.31.0.0/16 2 0 2", |
| + " |-- 172.31.0.0", |
| + " /16 link UNICAST", |
| + " |-- 172.31.5.9", |
| + " /32 host LOCAL", |
| + " |-- 172.31.255.255", |
| + " /32 link BROADCAST", |
| + " |-- 192.0.43.5", |
| + " /32 host LOCAL", |
| + "Local:", |
| + " +-- 0.0.0.0/0 3 0 5", |
| + " |-- 127.0.0.1", |
| + " /32 host LOCAL", |
| + " |-- 172.31.5.9", |
| + " /32 host LOCAL", |
| + " |-- 192.0.43.5", |
| + " /32 host LOCAL", |
| +} |
| + |
| +// Its /proc/net/if_inet6: loopback, a public address and its link-local |
| +// one, and a tailnet address. |
| +var boxIfInet6 = []string{ |
| + "00000000000000000000000000000001 01 80 10 80 lo", |
| + "2a0104f8000000000000000000000005 02 40 00 80 eth0", |
| + "fe80000000000000020000fffe000005 02 40 20 80 eth0", |
| + "fd7a115ca1e00000000000007537b43a 06 80 00 80 tailscale0", |
| +} |
| + |
| +// Without netlink (CARLOS), the scan reads the same addresses from the |
| +// process's own /proc/self/net: the card shows them, an owner's Confirm |
| +// makes the inventory ready, and Inside covers them. |
| +func TestInventoryWithoutNetlink(t *testing.T) { |
| + h := newAbilitiesHarness(t) |
| + ctx := context.Background() |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24,2a01:4f8::/64"); err != nil { |
| + t.Fatal(err) |
| + } |
| + netlinkRefused(h) |
| + procNet(t, h, boxFibTrie, boxIfInet6) |
| + scan(t, h) |
| + c, ok := openInventoryCards(t, h)["owner@example.com"] |
| + if !ok { |
| + t.Fatal("no card") |
| + } |
| + var shown wire.Card |
| + if err := h.s.tx(ctx, func(tx *txn) error { |
| + var err error |
| + shown, err = tx.cardView(c) |
| + return err |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := []string{"192.0.43.0/24", "2a01:4f8::/64", "192.0.43.5", "2a01:4f8::5"}; shown.Inventory == nil || !slices.Equal(shown.Inventory.Addresses, want) { |
| + t.Errorf("the card shows %+v, want %v", shown.Inventory, want) |
| + } |
| + if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := h.s.InstanceInventory().Ready(ctx); err != nil { |
| + t.Errorf("not ready after Confirm: %v", err) |
| + } |
| + if !h.s.InstanceInventory().Inside(netip.MustParseAddr("2a01:4f8::5")) { |
| + t.Error("the address seen isn't inside") |
| + } |
| +} |
| + |
| +// With neither netlink nor procfs to list them, the instance's addresses |
| +// are unknown, never none: the scan fails, the inventory isn't ready, |
| +// even with every prefix confirmed, and nobody is asked to confirm a |
| +// list that would leave addresses out. |
| +func TestInventoryUnlistedStaysClosed(t *testing.T) { |
| + h := newAbilitiesHarness(t) |
| + ctx := context.Background() |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := h.s.db.G.Create(&instancePrefixRecord{Prefix: "192.0.43.0/24", ConfirmedBy: "owner@example.com"}).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + netlinkRefused(h) |
| + procNet(t, h, nil, boxIfInet6) |
| + err := h.s.ScanInstanceAddresses(ctx) |
| + if err == nil { |
| + t.Fatal("the scan succeeded with nothing to read") |
| + } |
| + if !errors.Is(err, syscall.EAFNOSUPPORT) || !errors.Is(err, fs.ErrNotExist) { |
| + t.Errorf("the error leaves out a cause: %v", err) |
| + } |
| + if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) { |
| + t.Errorf("ready with the addresses unknown: %v", err) |
| + } |
| + if n := len(openInventoryCards(t, h)); n != 0 { |
| + t.Errorf("%d cards", n) |
| + } |
| +} |
| + |
| +// procfs that can't be trusted to list every address is an unknown list |
| +// too: a local route for a whole public range (the box answers for every |
| +// address in it, which the inventory can't hold as addresses), or a |
| +// fib_trie with no local route at all (every kernel has 127.0.0.1, so it |
| +// isn't the file the parser knows). No if_inet6 is a kernel without IPv6. |
| +func TestInventoryProcfsShapes(t *testing.T) { |
| + cases := []struct { |
| + name string |
| + fibTrie []string |
| + ifInet6 []string |
| + want []string |
| + }{ |
| + {"local range", append(slices.Clone(boxFibTrie), " |-- 192.0.44.0", " /24 host LOCAL"), boxIfInet6, nil}, |
| + {"no local route", []string{"Main:", " +-- 0.0.0.0/0 3 0 5", " |-- 0.0.0.0", " /0 universe UNICAST"}, boxIfInet6, nil}, |
| + {"bad if_inet6", boxFibTrie, []string{"not an address 01 80 10 80 lo"}, nil}, |
| + {"no IPv6", boxFibTrie, nil, []string{"192.0.43.5"}}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + h := newAbilitiesHarness(t) |
| + netlinkRefused(h) |
| + procNet(t, h, tc.fibTrie, tc.ifInet6) |
| + err := h.s.ScanInstanceAddresses(context.Background()) |
| + if tc.want == nil { |
| + if err == nil { |
| + t.Fatal("the scan succeeded") |
| + } |
| + return |
| + } |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var got []string |
| + if err := h.s.db.G.Model(&instanceAddressRecord{}).Order("address").Pluck("address", &got).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + if !slices.Equal(got, tc.want) { |
| + t.Errorf("seen %v, want %v", got, tc.want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The inventory holds the addresses a lookup could otherwise reach: |
| +// websource.PublicAddress refuses loopback, private, CGNAT and tailnet, |
| +// link-local and metadata addresses before the inventory is asked, so an |
| +// interface holding one needs no prefix listed for it and asks nothing. |
| +func TestInventoryIgnoresAddressesNeverReached(t *testing.T) { |
| + h := newAbilitiesHarness(t) |
| + ctx := context.Background() |
| + if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil { |
| + t.Fatal(err) |
| + } |
| + scanning(h, "10.0.0.5", "172.31.5.9", "100.100.1.1", "169.254.169.254", "fd7a:115c:a1e0::1", "192.0.43.5") |
| + scan(t, h) |
| + c := openInventoryCards(t, h)["owner@example.com"] |
| + if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := h.s.InstanceInventory().Ready(ctx); err != nil { |
| + t.Errorf("not ready with only unreachable addresses outside the prefixes: %v", err) |
| + } |
| + scanning(h, "10.0.0.5", "10.9.9.9", "192.0.43.5") |
| + scan(t, h) |
| + if n := len(openInventoryCards(t, h)); n != 0 { |
| + t.Errorf("a private address asked again: %d cards", n) |
| + } |
| +} |
diff --git a/internal/equipo/chat/store.go b/internal/equipo/chat/store.go| index b3cf66f5..027dab05 100644 |
| --- a/internal/equipo/chat/store.go |
| +++ b/internal/equipo/chat/store.go |
| @@ -117,9 +117,11 @@ type Store struct { |
| // X-Forwarded-For, when a test sets it (SetForwardedHops). |
| hops atomic.Pointer[int] |
| // instance is the instance inventory's listed prefixes and latest |
| - // scan; ifaceAddrs replaces net.InterfaceAddrs in tests. |
| + // scan; ifaceAddrs replaces net.InterfaceAddrs, and procNet the |
| + // directory /proc/self/net, in tests. |
| instance instanceNet |
| ifaceAddrs func() ([]net.Addr, error) |
| + procNet string |
| // live is the lookups' live channel to each runner (lookup_live.go); |
| // lookups their configuration, pace and deny lists (web_provider.go). |
| live lookupLive |