mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-01 14:12:32 -08:00
ipn/ipnlocal: keep IPv4 and IPv6 Service VIPs in extraDNS
nodeBackend rebuilt extraDNS from DNS.ExtraRecords as a map[string]netip.Addr, so a Service's A and AAAA records under the same name overwrote each other and only the last one (the AAAA, i.e. the IPv6 VIP) survived. resolveMagicDNS then returned only the IPv6 VIP for UserDial dials of the Service name, with no IPv4 fallback, and a tcp4 dial of the name failed outright. Keep every address per name (map[string][]netip.Addr) and select among them with the same addrForNetwork helper used for device names, which prefers IPv4 for unqualified networks and honors tcp4/tcp6. This makes outbound HTTP proxy, SOCKS5, and 'tailscale nc' dials of Service names reach the IPv4 VIP in IPv4-only deployments (e.g. Kubernetes operator ingress ProxyGroups). Fixes #22261 Signed-off-by: evan188199-tech <[email protected]>
This commit is contained in:
1 parent
737964a71d
commit
54a4fecaf4
3 files changed
+153
-24
No files matched your search
@@ -147,8 +147,10 @@ type nodeBackend struct {
|
|||||||
|
|
||||||
// extraDNS stores DNS.ExtraRecords A/AAAA entries from the netmap
|
// extraDNS stores DNS.ExtraRecords A/AAAA entries from the netmap
|
||||||
// (typically service VIPs pushed by control), keyed by canonicalized
|
// (typically service VIPs pushed by control), keyed by canonicalized
|
||||||
// hostname (lowercase, no trailing dot).
|
// hostname (lowercase, no trailing dot). A name can have both an A
|
||||||
extraDNS map[string]netip.Addr
|
// 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
|
// userProfiles is the live set of user profiles, updated incrementally
|
||||||
// by mergeUserProfiles as deltas arrive. It parallels the peers map:
|
// 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
|
nb.extraDNS = nil
|
||||||
for _, rec := range nm.DNS.ExtraRecords {
|
for _, rec := range nm.DNS.ExtraRecords {
|
||||||
if rec.Type != "" {
|
if rec.Type != "" {
|
||||||
@@ -866,7 +871,8 @@ func (nb *nodeBackend) updateNodeByNameLocked() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
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
|
return nid, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtraDNSByName returns the IP for a DNS.ExtraRecords entry (e.g. service VIPs).
|
// ExtraDNSByName returns the IP addresses for a DNS.ExtraRecords entry
|
||||||
func (nb *nodeBackend) ExtraDNSByName(hostname string) (_ netip.Addr, ok bool) {
|
// (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()
|
nb.mu.Lock()
|
||||||
defer nb.mu.Unlock()
|
defer nb.mu.Unlock()
|
||||||
ip, ok := nb.extraDNS[hostname]
|
addrs, ok := nb.extraDNS[hostname]
|
||||||
return ip, ok
|
return addrs, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nb *nodeBackend) updatePeersLocked() (res netmapResult) {
|
func (nb *nodeBackend) updatePeersLocked() (res netmapResult) {
|
||||||
|
|||||||
+17
-16
@@ -69,8 +69,8 @@ func (b *LocalBackend) resolveMagicDNS(hostname, network string) (_ netip.Addr,
|
|||||||
}
|
}
|
||||||
return netip.Addr{}, false
|
return netip.Addr{}, false
|
||||||
}
|
}
|
||||||
if ip, ok := nb.ExtraDNSByName(hostname); ok && addrFamilyMatch(ip, network) {
|
if addrs, ok := nb.ExtraDNSByName(hostname); ok {
|
||||||
return ip, true
|
return addrForNetwork(addrs, network)
|
||||||
}
|
}
|
||||||
return netip.Addr{}, false
|
return netip.Addr{}, false
|
||||||
}
|
}
|
||||||
@@ -116,11 +116,24 @@ func nodeAddrForNetwork(n tailcfg.NodeView, network string) (_ netip.Addr, ok bo
|
|||||||
if addrs.Len() == 0 {
|
if addrs.Len() == 0 {
|
||||||
return netip.Addr{}, false
|
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")
|
want4 := strings.HasSuffix(network, "4")
|
||||||
want6 := strings.HasSuffix(network, "6")
|
want6 := strings.HasSuffix(network, "6")
|
||||||
var v6 netip.Addr
|
var v6 netip.Addr
|
||||||
for _, pfx := range addrs.All() {
|
for _, ip := range addrs {
|
||||||
ip := pfx.Addr()
|
|
||||||
if want4 && ip.Is4() {
|
if want4 && ip.Is4() {
|
||||||
return ip, true
|
return ip, true
|
||||||
}
|
}
|
||||||
@@ -142,18 +155,6 @@ func nodeAddrForNetwork(n tailcfg.NodeView, network string) (_ netip.Addr, ok bo
|
|||||||
return netip.Addr{}, false
|
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.
|
// PeerForIP returns which peer is responsible for a given IP address.
|
||||||
// Despite the name, it can also return the self node (with IsSelf set).
|
// 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
|
// It handles both Tailscale IPs (returning the owning peer or self) and
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
package ipnlocal
|
package ipnlocal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"testing"
|
"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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user