Skip to content

Commit 250e6b9

Browse files
authored
Merge pull request #738 from rancher-sandbox/dns-fix
Hostresolver: Treat AAAA queries as A queries when IPv6 is disabled
2 parents 5f6dae6 + c2f8bf9 commit 250e6b9

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

pkg/hostagent/dns/dns.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
9191
Class: q.Qclass,
9292
Ttl: 5,
9393
}
94-
switch q.Qtype {
94+
qtype := q.Qtype
95+
switch qtype {
9596
case dns.TypeAAAA:
9697
if !h.IPv6 {
97-
handled = true
98-
break
98+
// A "correct" answer would be to set `handled = true` and return a NODATA response.
99+
// Unfortunately some older resolvers use a slow random source to set the transaction id.
100+
// This creates a problem on M1 computers, which are too fast for that implementation:
101+
// Both the A and AAAA queries might end up with the same id. Returning NODATA for AAAA
102+
// is faster, so would arrive first, and be treated as the response to the A query.
103+
// To avoid this, we will treat an AAAA query as an A query when IPv6 has been disabled.
104+
// This way it is either a valid response for an A query, or the A records will be discarded
105+
// by a genuine AAAA query, resulting in the desired NODATA response.
106+
qtype = dns.TypeA
99107
}
100108
fallthrough
101109
case dns.TypeCNAME, dns.TypeA:
@@ -129,7 +137,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
129137
reply.Answer = append(reply.Answer, a)
130138
handled = true
131139
}
132-
if q.Qtype == dns.TypeCNAME {
140+
if qtype == dns.TypeCNAME {
133141
break
134142
}
135143
hdr.Name = cname
@@ -144,13 +152,13 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
144152
for _, ip := range addrs {
145153
var a dns.RR
146154
ipv6 := ip.To4() == nil
147-
if q.Qtype == dns.TypeA && !ipv6 {
155+
if qtype == dns.TypeA && !ipv6 {
148156
hdr.Rrtype = dns.TypeA
149157
a = &dns.A{
150158
Hdr: hdr,
151159
A: ip.To4(),
152160
}
153-
} else if q.Qtype == dns.TypeAAAA && ipv6 {
161+
} else if qtype == dns.TypeAAAA && ipv6 {
154162
hdr.Rrtype = dns.TypeAAAA
155163
a = &dns.AAAA{
156164
Hdr: hdr,

0 commit comments

Comments
 (0)