[Dnsmasq-discuss] [PATCH] Reply BADVERS to EDNS queries with a VERSION we don't implement.
Denis Khalizov
denis.khalizov at gmail.com
Wed Nov 4 00:40:45 UTC 2026
RFC 6891 6.1.3: "If a responder does not implement the VERSION level
of the request, then it MUST respond with RCODE=BADVERS." dnsmasq
reads the UDP size and DO bit from the OPT RR of a query but skips
the VERSION byte, so a query with VERSION 1 or higher is answered
from local data or the cache as if it were VERSION 0, and the client
gets NOERROR. When the query is forwarded, add_pseudoheader() keeps
the client's VERSION, so the answer then depends on the upstream
server: with an upstream that implements 6.1.3, the same query gets
BADVERS while the name is not in the cache and NOERROR once some
other query has put it there.
Read the VERSION in receive_query() and tcp_request(), and when it
is not zero, reply with the question only, RCODE 0 in the header and
an OPT RR of our own with VERSION 0 and extended RCODE 1, i.e.
BADVERS (16). This happens after the existing checks which drop or
refuse a query, and before auth zones, local data, the cache and
forwarding. Log it like the other locally generated error replies.
Signed-off-by: Denis Khalizov <denis.khalizov at gmail.com>
---
Hi Simon,
I found this with an RFC conformance test suite I'm writing for a
DNS forwarder of my own: dnsmasq answers NOERROR to queries with
EDNS VERSION 1, 2 or 255 instead of BADVERS.
Reproducer on loopback (any recent dig):
dnsmasq -d --no-resolv --no-hosts -p 5300 \
--address=/local.example/192.0.2.1 --server=<some upstream>
dig @127.0.0.1 -p 5300 +noednsnegotiation +edns=1 www.local.example A
dig @127.0.0.1 -p 5300 +noednsnegotiation +tcp +edns=1 www.local.example A
Before: status NOERROR with the A record, for local, cached and
forwarded names. If the upstream implements BADVERS, a forwarded
name gets BADVERS but the same name gets NOERROR once it is cached.
After: status BADVERS, no answer, the question echoed, and an OPT
with VERSION 0, over UDP and TCP, for local, cached and forwarded
names. Queries without EDNS or with VERSION 0 are unchanged, and the
DO bit is echoed. The check comes after the existing interface,
local-service and allowlist checks and before auth zones, local
data, the cache and forwarding.
Built with the default -Wall -W, no new warnings; also compiled with
-DNO_AUTH and with -DHAVE_CONNTRACK -DHAVE_DNSSEC.
Thanks,
Denis
src/cache.c | 2 ++
src/dns-protocol.h | 1 +
src/forward.c | 44 ++++++++++++++++++++++++++++++++++++++++----
3 files changed, 43 insertions(+), 4 deletions(-)
diff --git a/src/cache.c b/src/cache.c
index 6bcb1b2..37c91df 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -2412,6 +2412,8 @@ void log_query(unsigned int flags, char *name, union all_addr *addr, char *arg,
dest = "FORMERR";
else if (rcode == NOTIMP)
dest = "not implemented";
+ else if (rcode == BADVERS)
+ dest = "BADVERS";
else
sprintf(daemon->addrbuff, "%u", rcode);
diff --git a/src/dns-protocol.h b/src/dns-protocol.h
index 43dddcf..ba84c12 100644
--- a/src/dns-protocol.h
+++ b/src/dns-protocol.h
@@ -59,6 +59,7 @@
#define NXDOMAIN 3 /* non existent domain */
#define NOTIMP 4 /* not implemented */
#define REFUSED 5 /* query refused */
+#define BADVERS 16 /* extended RCODE, RFC 6891 */
#define QUERY 0 /* opcode */
diff --git a/src/forward.c b/src/forward.c
index dae9a4f..8e90854 100644
--- a/src/forward.c
+++ b/src/forward.c
@@ -1596,6 +1596,32 @@ static size_t answer_disallowed(struct dns_header *header, size_t qlen, u32 mark
}
#endif
+/* RFC 6891 6.1.3: reply BADVERS to a query with an EDNS VERSION we don't
+ implement. The reply carries the question and an OPT RR of our own,
+ VERSION 0, with the upper bits of the extended RCODE set. */
+static size_t answer_badvers(struct dns_header *header, size_t qlen, size_t limit, char *name)
+{
+ unsigned char *p;
+ union all_addr a;
+
+ a.log.rcode = BADVERS;
+ a.log.ede = EDE_UNSET;
+ log_query(F_CONFIG | F_RCODE, name, &a, NULL, 0);
+
+ if (!(p = skip_questions(header, qlen)))
+ return 0;
+
+ setup_reply(header, F_NOERR, EDE_UNSET);
+ qlen = add_pseudoheader(header, p - (unsigned char *)header, limit, 0, NULL, 0, 0, 0);
+
+ if (!find_pseudoheader(header, qlen, NULL, &p, NULL, NULL))
+ return 0;
+
+ p[2] = BADVERS >> 4; /* extended RCODE */
+
+ return qlen;
+}
+
void receive_query(struct listener *listen, time_t now)
{
struct dns_header *header = (struct dns_header *)daemon->packet;
@@ -1609,7 +1635,7 @@ void receive_query(struct listener *listen, time_t now)
int if_index = 0, auth_dns = 0, do_bit = 0;
unsigned int fwd_flags = 0;
int stale = 0, filtered = 0, ede = EDE_UNSET, do_forward = 0;
- int metric, fd;
+ int metric, fd, edns_version = 0;
struct blockdata *saved_question = NULL;
#ifdef HAVE_CONNTRACK
unsigned int mark = 0;
@@ -1902,7 +1928,8 @@ void receive_query(struct listener *listen, time_t now)
fwd_flags |= FREC_HAS_PHEADER;
GETSHORT(udp_size, pheader);
- pheader += 2; /* ext_rcode */
+ pheader++; /* ext_rcode */
+ edns_version = *pheader++;
GETSHORT(flags, pheader);
if (flags & 0x8000)
@@ -1947,6 +1974,12 @@ void receive_query(struct listener *listen, time_t now)
metric = METRIC_DNS_LOCAL_ANSWERED;
}
#endif
+ else if (edns_version != 0)
+ {
+ m = answer_badvers(header, (size_t)n, udp_size,
+ OPCODE(header) == QUERY ? daemon->namebuff : NULL);
+ metric = METRIC_DNS_LOCAL_ANSWERED;
+ }
#ifdef HAVE_AUTH
else if (auth_dns)
{
@@ -2463,7 +2496,7 @@ void tcp_request(int confd, time_t now, struct iovec *bigbuff,
while (1)
{
- int cacheable = 1, stale = 0, ede = EDE_UNSET;
+ int cacheable = 1, stale = 0, ede = EDE_UNSET, edns_version = 0;
size_t m = 0;
unsigned int flags = 0;
#ifdef HAVE_AUTH
@@ -2529,7 +2562,8 @@ void tcp_request(int confd, time_t now, struct iovec *bigbuff,
unsigned short ede_flags;
have_pseudoheader = 1;
- pheader += 4; /* udp_size, ext_rcode */
+ pheader += 3; /* udp_size, ext_rcode */
+ edns_version = *pheader++;
GETSHORT(ede_flags, pheader);
if (ede_flags & 0x8000)
@@ -2591,6 +2625,8 @@ void tcp_request(int confd, time_t now, struct iovec *bigbuff,
m = answer_disallowed(out_header, size, (u32)mark, daemon->namebuff);
}
#endif
+ else if (edns_version != 0)
+ m = answer_badvers(out_header, size, 65536, daemon->namebuff);
#ifdef HAVE_AUTH
else if (auth_dns)
m = answer_auth(out_header, ((char *) out_header) + 65536, (size_t)size, now, &peer_addr, local_auth);
--
2.56.0
More information about the Dnsmasq-discuss
mailing list