diff --git a/ipn/ipnlocal/node_backend.go b/ipn/ipnlocal/node_backend.go index f7a2537c3..940a49cbb 100644 --- a/ipn/ipnlocal/node_backend.go +++ b/ipn/ipnlocal/node_backend.go @@ -147,8 +147,10 @@ type nodeBackend struct { // extraDNS stores DNS.ExtraRecords A/AAAA entries from the netmap // (typically service VIPs pushed by control), keyed by canonicalized - // hostname (lowercase, no trailing dot). - extraDNS map[string]netip.Addr + // hostname (lowercase, no trailing dot). A name can have both an A + // and an AAAA record (e.g. a Service's IPv4 and IPv6 VIPs), so the + // value keeps all addresses in record order. + extraDNS map[string][]netip.Addr // userProfiles is the live set of user profiles, updated incrementally // by mergeUserProfiles as deltas arrive. It parallels the peers map: @@ -856,7 +858,10 @@ func (nb *nodeBackend) updateNodeByNameLocked() { } }) - // Rebuild extraDNS from DNS.ExtraRecords (service VIPs, etc). + // Rebuild extraDNS from DNS.ExtraRecords (service VIPs, etc), + // keeping every address per name: a Service has both an A and an + // AAAA record under the same name, and dropping either would break + // dials of the other address family (issue #22261). nb.extraDNS = nil for _, rec := range nm.DNS.ExtraRecords { if rec.Type != "" { @@ -866,7 +871,8 @@ func (nb *nodeBackend) updateNodeByNameLocked() { if err != nil { continue } - mak.Set(&nb.extraDNS, strings.ToLower(strings.TrimSuffix(rec.Name, ".")), ip) + name := strings.ToLower(strings.TrimSuffix(rec.Name, ".")) + mak.Set(&nb.extraDNS, name, append(nb.extraDNS[name], ip)) } } @@ -880,12 +886,14 @@ func (nb *nodeBackend) NodeByName(hostname string) (_ tailcfg.NodeID, ok bool) { return nid, ok } -// ExtraDNSByName returns the IP for a DNS.ExtraRecords entry (e.g. service VIPs). -func (nb *nodeBackend) ExtraDNSByName(hostname string) (_ netip.Addr, ok bool) { +// ExtraDNSByName returns the IP addresses for a DNS.ExtraRecords entry +// (e.g. service VIPs). A name with both A and AAAA records returns +// both, in record order. +func (nb *nodeBackend) ExtraDNSByName(hostname string) (_ []netip.Addr, ok bool) { nb.mu.Lock() defer nb.mu.Unlock() - ip, ok := nb.extraDNS[hostname] - return ip, ok + addrs, ok := nb.extraDNS[hostname] + return addrs, ok } func (nb *nodeBackend) updatePeersLocked() (res netmapResult) { diff --git a/ipn/ipnlocal/resolve.go b/ipn/ipnlocal/resolve.go index e9241f7c6..460bed33a 100644 --- a/ipn/ipnlocal/resolve.go +++ b/ipn/ipnlocal/resolve.go @@ -69,8 +69,8 @@ func (b *LocalBackend) resolveMagicDNS(hostname, network string) (_ netip.Addr, } return netip.Addr{}, false } - if ip, ok := nb.ExtraDNSByName(hostname); ok && addrFamilyMatch(ip, network) { - return ip, true + if addrs, ok := nb.ExtraDNSByName(hostname); ok { + return addrForNetwork(addrs, network) } return netip.Addr{}, false } @@ -116,11 +116,24 @@ func nodeAddrForNetwork(n tailcfg.NodeView, network string) (_ netip.Addr, ok bo if addrs.Len() == 0 { return netip.Addr{}, false } + ips := make([]netip.Addr, 0, addrs.Len()) + for _, pfx := range addrs.All() { + ips = append(ips, pfx.Addr()) + } + return addrForNetwork(ips, network) +} + +// addrForNetwork returns the best address from addrs for the given +// network ("tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"). For +// unqualified networks ("tcp", "udp"), it prefers IPv4. It is used for +// both node addresses (see [nodeAddrForNetwork]) and DNS.ExtraRecords +// entries, so Service names resolve with the same address-family +// preferences as device names. +func addrForNetwork(addrs []netip.Addr, network string) (_ netip.Addr, ok bool) { want4 := strings.HasSuffix(network, "4") want6 := strings.HasSuffix(network, "6") var v6 netip.Addr - for _, pfx := range addrs.All() { - ip := pfx.Addr() + for _, ip := range addrs { if want4 && ip.Is4() { return ip, true } @@ -142,18 +155,6 @@ func nodeAddrForNetwork(n tailcfg.NodeView, network string) (_ netip.Addr, ok bo return netip.Addr{}, false } -// addrFamilyMatch reports whether ip is compatible with the requested -// network address family. -func addrFamilyMatch(ip netip.Addr, network string) bool { - if strings.HasSuffix(network, "4") { - return ip.Is4() - } - if strings.HasSuffix(network, "6") { - return ip.Is6() - } - return true -} - // PeerForIP returns which peer is responsible for a given IP address. // Despite the name, it can also return the self node (with IsSelf set). // It handles both Tailscale IPs (returning the owning peer or self) and diff --git a/ipn/ipnlocal/resolve_test.go b/ipn/ipnlocal/resolve_test.go index c00290de4..000e622cc 100644 --- a/ipn/ipnlocal/resolve_test.go +++ b/ipn/ipnlocal/resolve_test.go @@ -4,6 +4,8 @@ package ipnlocal import ( + "context" + "net" "net/netip" "testing" @@ -87,3 +89,121 @@ func TestResolveMagicDNS(t *testing.T) { }) } } + +// newDualStackExtraDNSBackend returns a test backend whose netmap has +// DNS.ExtraRecords for a dual-stack Service name (A record first, AAAA +// second, as control sends them; see issue #22261) and a v6-only +// Service name. +func newDualStackExtraDNSBackend(t *testing.T) *LocalBackend { + t.Helper() + b := newTestLocalBackend(t) + + nm := &netmap.NetworkMap{ + SelfNode: (&tailcfg.Node{ + ID: 1, + Name: "self.tail-scale.ts.net.", + Key: makeNodeKeyFromID(1), + DiscoKey: makeDiscoKeyFromID(1), + Addresses: []netip.Prefix{ + netip.MustParsePrefix("100.64.0.1/32"), + netip.MustParsePrefix("fd7a:115c:a1e0::1/128"), + }, + }).View(), + DNS: tailcfg.DNSConfig{ + ExtraRecords: []tailcfg.DNSRecord{ + {Name: "svc-dual.tail-scale.ts.net.", Value: "100.11.22.33"}, + {Name: "svc-dual.tail-scale.ts.net.", Value: "fd7a:115c:a1e0::33"}, + {Name: "svc-v6.tail-scale.ts.net.", Value: "fd7a:115c:a1e0::44"}, + }, + }, + } + nm.Domain = "tail-scale.ts.net" + b.currentNode().SetNetMap(nm) + return b +} + +// TestResolveMagicDNSExtraRecordsBothFamilies verifies that a Service +// name with both an A and an AAAA DNS.ExtraRecord keeps the IPv4 +// address: the AAAA record used to overwrite the A record in +// nodeBackend.extraDNS, so UserDial always got the IPv6 VIP and a +// tcp4 dial of the name failed (issue #22261). +func TestResolveMagicDNSExtraRecordsBothFamilies(t *testing.T) { + b := newDualStackExtraDNSBackend(t) + + // Both the IPv4 and IPv6 VIPs must be retained for the name. + addrs, ok := b.currentNode().ExtraDNSByName("svc-dual.tail-scale.ts.net") + if !ok || len(addrs) != 2 || + addrs[0] != netip.MustParseAddr("100.11.22.33") || + addrs[1] != netip.MustParseAddr("fd7a:115c:a1e0::33") { + t.Fatalf("ExtraDNSByName(svc-dual) = %v, %v; want [100.11.22.33 fd7a:115c:a1e0::33]", addrs, ok) + } + + tests := []struct { + name string + host string + network string + wantIP string + wantOK bool + }{ + // Like device names, an unqualified network prefers IPv4. + {name: "dual_tcp_prefers_ipv4", host: "svc-dual.tail-scale.ts.net", network: "tcp", wantIP: "100.11.22.33", wantOK: true}, + {name: "dual_tcp4", host: "svc-dual.tail-scale.ts.net", network: "tcp4", wantIP: "100.11.22.33", wantOK: true}, + {name: "dual_tcp6", host: "svc-dual.tail-scale.ts.net", network: "tcp6", wantIP: "fd7a:115c:a1e0::33", wantOK: true}, + // A v6-only Service name still resolves, and still misses for tcp4. + {name: "v6_only_tcp", host: "svc-v6.tail-scale.ts.net", network: "tcp", wantIP: "fd7a:115c:a1e0::44", wantOK: true}, + {name: "v6_only_tcp4_miss", host: "svc-v6.tail-scale.ts.net", network: "tcp4", wantOK: false}, + {name: "v6_only_tcp6", host: "svc-v6.tail-scale.ts.net", network: "tcp6", wantIP: "fd7a:115c:a1e0::44", wantOK: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ip, ok := b.resolveMagicDNS(tt.host, tt.network) + if ok != tt.wantOK { + t.Fatalf("resolveMagicDNS(%q, %q): ok=%v, want %v", tt.host, tt.network, ok, tt.wantOK) + } + if ok && ip.String() != tt.wantIP { + t.Fatalf("resolveMagicDNS(%q, %q): ip=%v, want %v", tt.host, tt.network, ip, tt.wantIP) + } + }) + } +} + +// TestUserDialServiceNamePrefersIPv4 verifies that UserDial of a +// dual-stack Service name dials the IPv4 VIP (and that a tcp4 dial +// succeeds), matching device-name behavior (issue #22261). +func TestUserDialServiceNamePrefersIPv4(t *testing.T) { + b := newDualStackExtraDNSBackend(t) + + got := make(chan netip.AddrPort, 1) + b.dialer.UseNetstackForIP = func(netip.Addr) bool { return true } + b.dialer.NetstackDialTCP = func(ctx context.Context, ipp netip.AddrPort) (net.Conn, error) { + select { + case got <- ipp: + default: + } + c, s := net.Pipe() + s.Close() + return c, nil + } + b.dialer.NetstackDialUDP = func(context.Context, netip.AddrPort) (net.Conn, error) { + t.Error("NetstackDialUDP should not be called for a TCP dial") + return nil, nil + } + + wantV4 := netip.MustParseAddrPort("100.11.22.33:50051") + for _, network := range []string{"tcp", "tcp4"} { + c, err := b.dialer.UserDial(context.Background(), network, "svc-dual.tail-scale.ts.net:50051") + if err != nil { + t.Fatalf("UserDial(%q): %v", network, err) + } + c.Close() + select { + case ipp := <-got: + if ipp != wantV4 { + t.Errorf("UserDial(%q) dialed %v; want %v", network, ipp, wantV4) + } + default: + t.Fatalf("UserDial(%q) did not dial", network) + } + } +}