e07df0
From bd79638f1c2c8f765e316c30b15dfb3e5d31e1e4 Mon Sep 17 00:00:00 2001
e07df0
From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= <wpk@isc.org>
e07df0
Date: Thu, 3 Jan 2019 14:58:05 +0100
e07df0
Subject: [PATCH] If possible don't use forwarders when priming the resolver.
e07df0
e07df0
If we try to fetch a record from cache and need to look into
e07df0
hints database we assume that the resolver is not primed and
e07df0
start dns_resolver_prime(). Priming query is supposed to return
e07df0
NSes for "." in ANSWER section and glue records for them in
e07df0
ADDITIONAL section, so that we can fill that info in 'regular'
e07df0
cache and not use hints db anymore.
e07df0
However, if we're using a forwarder the priming query goes through
e07df0
it, and if it's configured to return minimal answers we won't get
e07df0
the addresses of root servers in ADDITIONAL section. Since the
e07df0
only records for root servers we have are in hints database we'll
e07df0
try to prime the resolver with every single query.
e07df0
e07df0
This patch adds a DNS_FETCHOPT_NOFORWARD flag which avoids using
e07df0
forwarders if possible (that is if we have forward-first policy).
e07df0
Using this flag on priming fetch fixes the problem as we get the
e07df0
proper glue. With forward-only policy the problem is non-existent,
e07df0
as we'll never ask for root server addresses because we'll never
e07df0
have a need to query them.
e07df0
e07df0
Also added a test to confirm priming queries are not forwarded.
e07df0
e07df0
(cherry picked from commit b49310ac06ac87733dc2867828e61370a84b2a9a)
e07df0
(cherry picked from commit f8963ad70e222edad0c1e64f855f7fb41fb13c3c)
e07df0
(cherry picked from commit aa9866c390a21d6984aa75cdb84d7bc77e114c2f)
e07df0
---
e07df0
 bin/tests/system/forward/ns4/named.conf.in |  3 ++
e07df0
 bin/tests/system/forward/ns7/named.conf.in | 28 +++++++++++++++++
e07df0
 bin/tests/system/forward/ns7/root.db       | 28 +++++++++++++++++
e07df0
 bin/tests/system/forward/setup.sh          |  1 +
e07df0
 bin/tests/system/forward/tests.sh          | 12 ++++++++
e07df0
 lib/dns/include/dns/resolver.h             | 36 ++++++++++++----------
e07df0
 lib/dns/resolver.c                         | 17 ++++++++--
e07df0
 7 files changed, 106 insertions(+), 19 deletions(-)
e07df0
 create mode 100644 bin/tests/system/forward/ns7/named.conf.in
e07df0
 create mode 100644 bin/tests/system/forward/ns7/root.db
e07df0
e07df0
diff --git a/bin/tests/system/forward/ns4/named.conf.in b/bin/tests/system/forward/ns4/named.conf.in
e07df0
index 480530b0f2..643e1271b5 100644
e07df0
--- a/bin/tests/system/forward/ns4/named.conf.in
e07df0
+++ b/bin/tests/system/forward/ns4/named.conf.in
e07df0
@@ -17,6 +17,9 @@ options {
e07df0
 	pid-file "named.pid";
e07df0
 	listen-on { 10.53.0.4; };
e07df0
 	listen-on-v6 { none; };
e07df0
+	recursion yes;
e07df0
+	dnssec-validation yes;
e07df0
+	minimal-responses yes;
e07df0
 };
e07df0
 
e07df0
 zone "." {
e07df0
diff --git a/bin/tests/system/forward/ns7/named.conf.in b/bin/tests/system/forward/ns7/named.conf.in
e07df0
new file mode 100644
e07df0
index 0000000000..d9f5e8a9db
e07df0
--- /dev/null
e07df0
+++ b/bin/tests/system/forward/ns7/named.conf.in
e07df0
@@ -0,0 +1,28 @@
e07df0
+/*
e07df0
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
e07df0
+ *
e07df0
+ * This Source Code Form is subject to the terms of the Mozilla Public
e07df0
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
e07df0
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
e07df0
+ *
e07df0
+ * See the COPYRIGHT file distributed with this work for additional
e07df0
+ * information regarding copyright ownership.
e07df0
+ */
e07df0
+
e07df0
+options {
e07df0
+	query-source address 10.53.0.7;
e07df0
+	notify-source 10.53.0.7;
e07df0
+	transfer-source 10.53.0.7;
e07df0
+	port @PORT@;
e07df0
+	pid-file "named.pid";
e07df0
+	listen-on { 10.53.0.7; };
e07df0
+	listen-on-v6 { none; };
e07df0
+	forwarders { 10.53.0.4; };
e07df0
+	forward first;
e07df0
+	dnssec-validation yes;
e07df0
+};
e07df0
+
e07df0
+zone "." {
e07df0
+	type hint;
e07df0
+	file "root.db";
e07df0
+};
e07df0
diff --git a/bin/tests/system/forward/ns7/root.db b/bin/tests/system/forward/ns7/root.db
e07df0
new file mode 100644
e07df0
index 0000000000..7346810ba6
e07df0
--- /dev/null
e07df0
+++ b/bin/tests/system/forward/ns7/root.db
e07df0
@@ -0,0 +1,28 @@
e07df0
+; Copyright (C) Internet Systems Consortium, Inc. ("ISC")
e07df0
+;
e07df0
+; This Source Code Form is subject to the terms of the Mozilla Public
e07df0
+; License, v. 2.0. If a copy of the MPL was not distributed with this
e07df0
+; file, You can obtain one at http://mozilla.org/MPL/2.0/.
e07df0
+;
e07df0
+; See the COPYRIGHT file distributed with this work for additional
e07df0
+; information regarding copyright ownership.
e07df0
+
e07df0
+$TTL 300
e07df0
+. 			IN SOA	gson.nominum.com. a.root.servers.nil. (
e07df0
+				2000042100   	; serial
e07df0
+				600         	; refresh
e07df0
+				600         	; retry
e07df0
+				1200    	; expire
e07df0
+				600       	; minimum
e07df0
+				)
e07df0
+.			NS	a.root-servers.nil.
e07df0
+a.root-servers.nil.	A	10.53.0.1
e07df0
+
e07df0
+example1		NS	ns.example1
e07df0
+ns.example1		A	10.53.0.1
e07df0
+
e07df0
+example2		NS	ns.example2
e07df0
+ns.example2		A	10.53.0.1
e07df0
+
e07df0
+example3		NS	ns.example3
e07df0
+ns.example3		A	10.53.0.1
e07df0
diff --git a/bin/tests/system/forward/setup.sh b/bin/tests/system/forward/setup.sh
e07df0
index c63aeb10d2..d64579e590 100644
e07df0
--- a/bin/tests/system/forward/setup.sh
e07df0
+++ b/bin/tests/system/forward/setup.sh
e07df0
@@ -18,3 +18,4 @@ copy_setports ns2/named.conf.in ns2/named.conf
e07df0
 copy_setports ns3/named.conf.in ns3/named.conf
e07df0
 copy_setports ns4/named.conf.in ns4/named.conf
e07df0
 copy_setports ns5/named.conf.in ns5/named.conf
e07df0
+copy_setports ns7/named.conf.in ns7/named.conf
e07df0
diff --git a/bin/tests/system/forward/tests.sh b/bin/tests/system/forward/tests.sh
e07df0
index f23cde1751..8c6496037d 100644
e07df0
--- a/bin/tests/system/forward/tests.sh
e07df0
+++ b/bin/tests/system/forward/tests.sh
e07df0
@@ -131,5 +131,17 @@ $CHECKCONF ula-notinherited.conf | grep "forward first;" >/dev/null && ret=1
e07df0
 if [ $ret != 0 ]; then echo_i "failed"; fi
e07df0
 status=`expr $status + $ret`
e07df0
 
e07df0
+echo_i "checking that priming queries are not forwarded"
e07df0
+ret=0
e07df0
+$DIG $DIGOPTS +noadd +noauth txt.example1. txt @10.53.0.7 > dig.out.f7 || ret=1
e07df0
+sent=`sed -n '/sending packet to 10.53.0.1/,/^$/p' ns7/named.run | grep ";.*IN.*NS" | wc -l`
e07df0
+[ $sent -eq 1 ] || ret=1
e07df0
+sent=`grep "10.53.0.7#.* (.): query '\./NS/IN' approved" ns4/named.run | wc -l`
e07df0
+[ $sent -eq 0 ] || ret=1
e07df0
+sent=`grep "10.53.0.7#.* (.): query '\./NS/IN' approved" ns1/named.run | wc -l`
e07df0
+[ $sent -eq 1 ] || ret=1
e07df0
+if [ $ret != 0 ]; then echo_i "failed"; fi
e07df0
+status=`expr $status + $ret`
e07df0
+
e07df0
 echo_i "exit status: $status"
e07df0
 [ $status -eq 0 ] || exit 1
e07df0
diff --git a/lib/dns/include/dns/resolver.h b/lib/dns/include/dns/resolver.h
e07df0
index f2f8714d16..e1a10295f3 100644
e07df0
--- a/lib/dns/include/dns/resolver.h
e07df0
+++ b/lib/dns/include/dns/resolver.h
e07df0
@@ -88,23 +88,25 @@ typedef enum {
e07df0
 /*
e07df0
  * Options that modify how a 'fetch' is done.
e07df0
  */
e07df0
-#define DNS_FETCHOPT_TCP		0x0001	     /*%< Use TCP. */
e07df0
-#define DNS_FETCHOPT_UNSHARED		0x0002	     /*%< See below. */
e07df0
-#define DNS_FETCHOPT_RECURSIVE		0x0004	     /*%< Set RD? */
e07df0
-#define DNS_FETCHOPT_NOEDNS0		0x0008	     /*%< Do not use EDNS. */
e07df0
-#define DNS_FETCHOPT_FORWARDONLY	0x0010	     /*%< Only use forwarders. */
e07df0
-#define DNS_FETCHOPT_NOVALIDATE		0x0020	     /*%< Disable validation. */
e07df0
-#define DNS_FETCHOPT_EDNS512		0x0040	     /*%< Advertise a 512 byte
e07df0
-							  UDP buffer. */
e07df0
-#define DNS_FETCHOPT_WANTNSID		0x0080	     /*%< Request NSID */
e07df0
-#define DNS_FETCHOPT_PREFETCH		0x0100	     /*%< Do prefetch */
e07df0
-#define DNS_FETCHOPT_NOCDFLAG		0x0200	     /*%< Don't set CD flag. */
e07df0
-#define DNS_FETCHOPT_NONTA		0x0400	     /*%< Ignore NTA table. */
e07df0
-/* RESERVED ECS				0x0000 */
e07df0
-/* RESERVED ECS				0x1000 */
e07df0
-/* RESERVED ECS				0x2000 */
e07df0
-/* RESERVED TCPCLIENT			0x4000 */
e07df0
-#define DNS_FETCHOPT_NOCACHED		0x8000	     /*%< Force cache update. */
e07df0
+#define DNS_FETCHOPT_TCP		0x00001	     /*%< Use TCP. */
e07df0
+#define DNS_FETCHOPT_UNSHARED		0x00002	     /*%< See below. */
e07df0
+#define DNS_FETCHOPT_RECURSIVE		0x00004	     /*%< Set RD? */
e07df0
+#define DNS_FETCHOPT_NOEDNS0		0x00008	     /*%< Do not use EDNS. */
e07df0
+#define DNS_FETCHOPT_FORWARDONLY	0x00010	     /*%< Only use forwarders. */
e07df0
+#define DNS_FETCHOPT_NOVALIDATE		0x00020	     /*%< Disable validation. */
e07df0
+#define DNS_FETCHOPT_EDNS512		0x00040	     /*%< Advertise a 512 byte
e07df0
+					0		  UDP buffer. */
e07df0
+#define DNS_FETCHOPT_WANTNSID		0x00080	     /*%< Request NSID */
e07df0
+#define DNS_FETCHOPT_PREFETCH		0x00100	     /*%< Do prefetch */
e07df0
+#define DNS_FETCHOPT_NOCDFLAG		0x00200	     /*%< Don't set CD flag. */
e07df0
+#define DNS_FETCHOPT_NONTA		0x00400	     /*%< Ignore NTA table. */
e07df0
+/* RESERVED ECS				0x00000 */
e07df0
+/* RESERVED ECS				0x01000 */
e07df0
+/* RESERVED ECS				0x02000 */
e07df0
+/* RESERVED TCPCLIENT			0x04000 */
e07df0
+#define DNS_FETCHOPT_NOCACHED		0x08000	     /*%< Force cache update. */
e07df0
+#define DNS_FETCHOPT_NOFORWARD		0x80000 /*%< Do not use forwarders
e07df0
+							if possible. */
e07df0
 
e07df0
 /* Reserved in use by adb.c		0x00400000 */
e07df0
 #define	DNS_FETCHOPT_EDNSVERSIONSET	0x00800000
e07df0
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
e07df0
index 301ad52fe2..f701a09be9 100644
e07df0
--- a/lib/dns/resolver.c
e07df0
+++ b/lib/dns/resolver.c
e07df0
@@ -3261,6 +3261,18 @@ fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
e07df0
 	INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
e07df0
 	INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
e07df0
 
e07df0
+	/*
e07df0
+	 * If we have DNS_FETCHOPT_NOFORWARD set and forwarding policy
e07df0
+	 * allows us to not forward - skip forwarders and go straight
e07df0
+	 * to NSes. This is currently used to make sure that priming query
e07df0
+	 * gets root servers' IP addresses in ADDITIONAL section.
e07df0
+	 */
e07df0
+	if ((fctx->options & DNS_FETCHOPT_NOFORWARD) != 0 &&
e07df0
+	    (fctx->fwdpolicy != dns_fwdpolicy_only))
e07df0
+	{
e07df0
+		goto normal_nses;
e07df0
+	}
e07df0
+
e07df0
 	/*
e07df0
 	 * If this fctx has forwarders, use them; otherwise use any
e07df0
 	 * selective forwarders specified in the view; otherwise use the
e07df0
@@ -3346,7 +3358,7 @@ fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
e07df0
 	/*
e07df0
 	 * Normal nameservers.
e07df0
 	 */
e07df0
-
e07df0
+ normal_nses:
e07df0
 	stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
e07df0
 	if (fctx->restarts == 1) {
e07df0
 		/*
e07df0
@@ -9194,7 +9206,8 @@ dns_resolver_prime(dns_resolver_t *res) {
e07df0
 		LOCK(&res->primelock);
e07df0
 		result = dns_resolver_createfetch(res, dns_rootname,
e07df0
 						  dns_rdatatype_ns,
e07df0
-						  NULL, NULL, NULL, 0,
e07df0
+						  NULL, NULL, NULL,
e07df0
+						  DNS_FETCHOPT_NOFORWARD,
e07df0
 						  res->buckets[0].task,
e07df0
 						  prime_done,
e07df0
 						  res, rdataset, NULL,
e07df0
-- 
e07df0
2.21.1
e07df0