943bc6
From d55a57427ee696dec51149950478394e43019607 Mon Sep 17 00:00:00 2001
10d019
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
10d019
Date: Thu, 7 Nov 2019 14:31:03 +0100
10d019
Subject: [PATCH] Implement serve-stale in 9.11
10d019
MIME-Version: 1.0
10d019
Content-Type: text/plain; charset=UTF-8
10d019
Content-Transfer-Encoding: 8bit
10d019
10d019
Squashed commit of the following:
10d019
10d019
commit 32f47f36e545223b2a4757588d7bd4af8c5f5760
10d019
Author: Petr Menšík <pemensik@redhat.com>
10d019
Date:   Tue Sep 3 18:45:54 2019 +0200
10d019
10d019
    convert serve_stale to db_test
10d019
10d019
    Manual checkout from commit e8f61dd315c5d1c88915bb79361182241e42e47a.
10d019
    Use test modified for cmocka, including serve-stale tests.
10d019
10d019
commit 071eb1fb0786f6d614955813d99c3caabff33383
10d019
Author: Michał Kępień <michal@isc.org>
10d019
Date:   Fri Apr 27 09:13:26 2018 +0200
10d019
10d019
    Detect recursion loops during query processing
10d019
10d019
    Interrupt query processing when query_recurse() attempts to ask the same
10d019
    name servers for the same QNAME/QTYPE tuple for two times in a row as
10d019
    this indicates that query processing may be stuck for an indeterminate
10d019
    period of time, e.g. due to interactions between features able to
10d019
    restart query_lookup().
10d019
10d019
    (cherry picked from commit 46bb4dd124ed031d4c219d1e37a3c6322092e30c)
10d019
10d019
commit c12090bc361c7fa4522ace73899e778e44e9b295
10d019
Author: Petr Menšík <pemensik@redhat.com>
10d019
Date:   Mon Sep 2 11:12:32 2019 +0200
10d019
10d019
    Fix test name used in whole test-suite
10d019
10d019
    Correct name is serve-stale
10d019
10d019
commit ff4d826f295d268a248ca06941d65c903e1b405c
10d019
Author: Petr Menšík <pemensik@redhat.com>
10d019
Date:   Fri Aug 30 17:43:28 2019 +0200
10d019
10d019
    Clean files in more generic rules
10d019
10d019
commit 8d81ed15eda9a2a11e1433d1fdddacfc772708b6
10d019
Author: Petr Menšík <pemensik@redhat.com>
10d019
Date:   Thu Aug 29 21:27:57 2019 +0200
10d019
10d019
    [rt46602] Pass port numbers to tests via environment variables
10d019
10d019
    Manually applied commit f5d8f079008b648d2e343543e66dd728054c6101
10d019
10d019
commit 94fafa477891576286def8c4041ad127734af2d1
10d019
Author: Tony Finch <dot@dotat.at>
10d019
Date:   Tue Apr 10 16:17:57 2018 +0100
10d019
10d019
    Move serve-stale logging to its own category, so that its verbosity can be curtailed.
10d019
10d019
    (cherry picked from commit 4b442c309dfb2c8880b19af4133047655bb734df)
10d019
10d019
commit e0c884bee98c3d2533dfaa667f58c6a80d8a3a00
10d019
Author: Michał Kępień <michal@isc.org>
10d019
Date:   Fri Apr 27 09:13:26 2018 +0200
10d019
10d019
    Prevent check_stale_header() from leaking rdataset headers
10d019
10d019
    check_stale_header() fails to update the pointer to the previous header
10d019
    while processing rdataset headers eligible for serve-stale, thus
10d019
    enabling rdataset headers to be leaked (i.e. disassociated from a node
10d019
    and left on the relevant TTL heap) while iterating through a node.  This
10d019
    can lead to several different assertion failures.  Add the missing
10d019
    pointer update.
10d019
10d019
    (cherry picked from commit 391fac1fc8d2e470287b5cc4344b3adb90c6f54a)
10d019
10d019
commit d724cc1d80ee8d46113eaf82549d49636739b67c
10d019
Author: Matthijs Mekking <matthijs@isc.org>
10d019
Date:   Thu Jan 24 10:24:44 2019 +0100
10d019
10d019
    Print in dump-file stale ttl
10d019
10d019
    This change makes rndc dumpdb correctly print the "; stale" line.
10d019
    It also provides extra information on how long this data may still
10d019
    be served to clients (in other words how long the stale RRset may
10d019
    still be used).
10d019
10d019
    (cherry picked from commit 924ebc605db798e2a383ee5eaaebad739e7c789c)
10d019
10d019
commit 625da4bd4590ac6108bb30eddd23ceffb245ae49
10d019
Author: Michał Kępień <michal@isc.org>
10d019
Date:   Mon Oct 22 15:26:45 2018 +0200
10d019
10d019
    Check serve-stale behavior with a cold cache
10d019
10d019
    Ensure that serve-stale works as expected when returning stale answers
10d019
    is enabled, the authoritative server does not respond, and there is no
10d019
    cached answer available.
10d019
10d019
    (cherry picked from commit 27cfe83a388147edfa0451b28c06c746912ea684)
10d019
10d019
commit d67ae10461c409fdafdbbe64f857db2552b71059
10d019
Author: Michał Kępień <michal@isc.org>
10d019
Date:   Mon Oct 22 15:26:45 2018 +0200
10d019
10d019
    Check TTL of stale answers
10d019
10d019
    Make sure that stale answers returned when the serve-stale feature is
10d019
    enabled have a TTL matching the value of the stale-answer-ttl setting.
10d019
10d019
    (cherry picked from commit 893ab37ce78c658215bd3a019f25afe795b37d5a)
10d019
10d019
commit 50459107805e68e4a63a8e497bf58ef3ce013ddb
10d019
Author: Michał Kępień <michal@isc.org>
10d019
Date:   Mon Jul 9 14:35:12 2018 +0200
10d019
10d019
    Do not use Net::DNS::Nameserver in the "serve-stale" system test
10d019
10d019
    Net::DNS versions older than 0.67 respond to queries sent to a
10d019
    Net::DNS::Nameserver even if its ReplyHandler returns undef.  This makes
10d019
    the "serve-stale" system test fail as it takes advantage of the newer
10d019
    behavior.  Since the latest Net::DNS version available with stock
10d019
    RHEL/CentOS 6 packages is 0.65 and we officially support that operating
10d019
    system, bin/tests/system/serve-stale/ans2/ans.pl should behave
10d019
    consistently for various Net::DNS versions.  Ensure that by reworking it
10d019
    so that it does not use Net::DNS::Nameserver.
10d019
10d019
    (cherry picked from commit c4209418a50c09142375f7edadca731c526f3d3a)
10d019
10d019
commit 4b5befc714bb386bd245b1c14ce3bce5ae6fb5fa
10d019
Author: Petr Menšík <pemensik@redhat.com>
10d019
Date:   Tue Jun 5 21:38:29 2018 +0200
10d019
10d019
    Fix server-stale requirement, skip without Time::HiRes
10d019
10d019
    (cherry picked from commit 7a0c7bf9c8e6a724e52635eed213ad25b9504e66)
10d019
10d019
commit 5ce51a3a7e5ef3087c4d022e3fca42fb2fd0c996
10d019
Author: Ondřej Surý <ondrej@sury.org>
10d019
Date:   Wed Oct 18 13:01:14 2017 +0200
10d019
10d019
    [rt46602] Update server-stale test to run on port passed from run.sh script
10d019
10d019
    (cherry picked from commit f83ebd34b9555a5a834c58146035173bcbd01dda)
10d019
10d019
commit 3954a9bf3437f6fab050294a7f2f954a23d161ec
10d019
Author: Ondřej Surý <ondrej@sury.org>
10d019
Date:   Wed Oct 18 14:18:59 2017 +0200
10d019
10d019
    [rt46602] Add serve-stale working files to .gitignore
10d019
10d019
    (cherry picked from commit cba162e70e7fac43435a606106841a69ce468526)
10d019
10d019
commit 112aa21f5fa875494820e4d1eb70e41e10e1aae7
10d019
Author: Mark Andrews <marka@isc.org>
10d019
Date:   Thu Oct 12 15:33:47 2017 +1100
10d019
10d019
    test for Net::DNS::Nameserver
10d019
10d019
    (cherry picked from commit 5b60d0608ac2852753180b762d1917163f9dc315)
10d019
10d019
commit 9d610e46af8a636f44914cee4cf8b2016054db1e
10d019
Author: Mark Andrews <marka@isc.org>
10d019
Date:   Thu Oct 12 15:19:45 2017 +1100
10d019
10d019
    add Net::DNS prerequiste test
10d019
10d019
    (cherry picked from commit fa644181f51559da3e3913acd72dbc3f6d916e71)
10d019
10d019
commit e4ea7ba88d9a9a0c79579400c68a5dabe03e8572
10d019
Author: Mark Andrews <marka@isc.org>
10d019
Date:   Wed Sep 6 19:26:10 2017 +1000
10d019
10d019
    add quotes arount $send_response
10d019
10d019
    (cherry picked from commit 023ab19634b287543169e9b7b5259f3126cd60ff)
10d019
10d019
commit 0af0c5d33c2de34da164571288b650282c6be10a
10d019
Author: Mark Andrews <marka@isc.org>
10d019
Date:   Thu Nov 23 16:11:49 2017 +1100
10d019
10d019
    initalise serve_stale_ttl
10d019
10d019
    (cherry picked from commit 2f4e0e5a81278f59037bf06ae99ff52245cd57e9)
10d019
10d019
commit fbadd90ee81863d617c4c319d5f0079b877fe102
10d019
Author: Evan Hunt <each@isc.org>
10d019
Date:   Thu Sep 14 11:48:21 2017 -0700
10d019
10d019
    [master] add thanks to APNIC and add missing note for serve-stale
10d019
10d019
commit deb8adaa59955970b9d2f2fe58060a3cbf08312b
10d019
Author: Mark Andrews <marka@isc.org>
10d019
Date:   Wed Sep 6 12:16:10 2017 +1000
10d019
10d019
    silence 'staleanswersok' may be used uninitialized in this function warning. [RT #14147
10d019
10d019
commit 0e2d03823768dc545015e6ce309777210f4a9f85
10d019
Author: Petr Menšík <pemensik@redhat.com>
10d019
Date:   Thu Aug 29 19:57:58 2019 +0200
10d019
10d019
    More fixes to merge
10d019
10d019
commit 360e25ffe7623ea0a2eec49395001f4940967776
10d019
Author: Mark Andrews <marka@isc.org>
10d019
Date:   Wed Sep 6 09:58:29 2017 +1000
10d019
10d019
    4700.   [func]          Serving of stale answers is now supported. This
10d019
                            allows named to provide stale cached answers when
10d019
                            the authoritative server is under attack.
10d019
                            See max-stale-ttl, stale-answer-enable,
10d019
                            stale-answer-ttl. [RT #44790]
10d019
10d019
Signed-off-by: Petr Menšík <pemensik@redhat.com>
10d019
---
10d019
 bin/named/config.c                            |   9 +-
10d019
 bin/named/control.c                           |   2 +
10d019
 bin/named/include/named/control.h             |   1 +
10d019
 bin/named/include/named/log.h                 |   1 +
10d019
 bin/named/include/named/query.h               |  15 +
10d019
 bin/named/include/named/server.h              |  13 +-
10d019
 bin/named/log.c                               |   1 +
10d019
 bin/named/query.c                             | 164 +++++-
10d019
 bin/named/server.c                            | 177 +++++-
10d019
 bin/named/statschannel.c                      |   6 +
10d019
 bin/rndc/rndc.c                               |   2 +
10d019
 bin/rndc/rndc.docbook                         |  19 +
10d019
 bin/tests/system/chain/prereq.sh              |   7 +
10d019
 bin/tests/system/conf.sh.in                   |   2 +-
10d019
 bin/tests/system/dyndb/driver/db.c            |   2 +
10d019
 bin/tests/system/serve-stale/.gitignore       |  11 +
10d019
 bin/tests/system/serve-stale/ans2/ans.pl.in   | 178 ++++++
10d019
 bin/tests/system/serve-stale/clean.sh         |  15 +
10d019
 .../system/serve-stale/ns1/named1.conf.in     |  35 ++
10d019
 .../system/serve-stale/ns1/named2.conf.in     |  35 ++
10d019
 bin/tests/system/serve-stale/ns1/root.db      |   5 +
10d019
 .../system/serve-stale/ns3/named.conf.in      |  35 ++
10d019
 bin/tests/system/serve-stale/prereq.sh        |  38 ++
10d019
 bin/tests/system/serve-stale/setup.sh         |  13 +
10d019
 bin/tests/system/serve-stale/tests.sh         | 536 ++++++++++++++++++
943bc6
 doc/arm/Bv9ARM-book.xml                       |  77 ++-
10d019
 doc/arm/logging-categories.xml                |  11 +
10d019
 doc/arm/notes-rh-changes.xml                  |  14 +-
10d019
 doc/misc/options                              |  10 +
10d019
 lib/bind9/check.c                             |  78 ++-
10d019
 lib/dns/cache.c                               |  38 +-
10d019
 lib/dns/db.c                                  |  22 +
10d019
 lib/dns/ecdb.c                                |   4 +-
10d019
 lib/dns/include/dns/cache.h                   |  21 +
10d019
 lib/dns/include/dns/db.h                      |  35 ++
10d019
 lib/dns/include/dns/rdataset.h                |  11 +
10d019
 lib/dns/include/dns/resolver.h                |  43 +-
10d019
 lib/dns/include/dns/types.h                   |   6 +
10d019
 lib/dns/include/dns/view.h                    |   3 +
10d019
 lib/dns/master.c                              |  14 +-
10d019
 lib/dns/masterdump.c                          |  23 +
10d019
 lib/dns/rbtdb.c                               | 207 ++++++-
230545
 lib/dns/resolver.c                            |  79 ++-
10d019
 lib/dns/sdb.c                                 |   4 +-
10d019
 lib/dns/sdlz.c                                |   4 +-
10d019
 lib/dns/tests/db_test.c                       | 198 ++++++-
10d019
 lib/dns/view.c                                |   3 +
10d019
 lib/isccfg/namedconf.c                        |   5 +
943bc6
 48 files changed, 2126 insertions(+), 106 deletions(-)
10d019
 create mode 100644 bin/tests/system/serve-stale/.gitignore
10d019
 create mode 100644 bin/tests/system/serve-stale/ans2/ans.pl.in
10d019
 create mode 100644 bin/tests/system/serve-stale/clean.sh
10d019
 create mode 100644 bin/tests/system/serve-stale/ns1/named1.conf.in
10d019
 create mode 100644 bin/tests/system/serve-stale/ns1/named2.conf.in
10d019
 create mode 100644 bin/tests/system/serve-stale/ns1/root.db
10d019
 create mode 100644 bin/tests/system/serve-stale/ns3/named.conf.in
10d019
 create mode 100644 bin/tests/system/serve-stale/prereq.sh
10d019
 create mode 100644 bin/tests/system/serve-stale/setup.sh
10d019
 create mode 100755 bin/tests/system/serve-stale/tests.sh
10d019
10d019
diff --git a/bin/named/config.c b/bin/named/config.c
943bc6
index 9e071bb..d2cd3bc 100644
10d019
--- a/bin/named/config.c
10d019
+++ b/bin/named/config.c
10d019
@@ -182,13 +182,14 @@ options {\n\
10d019
 #ifdef HAVE_LMDB
10d019
 "	lmdb-mapsize 32M;\n"
10d019
 #endif
10d019
-"	max-acache-size 16M;\n\
10d019
-	max-cache-size 90%;\n\
10d019
+"	max-cache-size 90%;\n\
10d019
+	max-acache-size 16M;\n\
10d019
 	max-cache-ttl 604800; /* 1 week */\n\
10d019
 	max-clients-per-query 100;\n\
10d019
 	max-ncache-ttl 10800; /* 3 hours */\n\
10d019
 	max-recursion-depth 7;\n\
943bc6
 	max-recursion-queries 100;\n\
10d019
+	max-stale-ttl 604800; /* 1 week */\n\
10d019
 	message-compression yes;\n\
10d019
 #	min-roots <obsolete>;\n\
10d019
 	minimal-any false;\n\
10d019
@@ -203,10 +204,14 @@ options {\n\
10d019
 	request-expire true;\n\
10d019
 	request-ixfr true;\n\
10d019
 	require-server-cookie no;\n\
10d019
+	resolver-nonbackoff-tries 3;\n\
10d019
+	resolver-retry-interval 800; /* in milliseconds */\n\
10d019
 #	rfc2308-type1 <obsolete>;\n\
10d019
 	root-key-sentinel yes;\n\
10d019
 	servfail-ttl 1;\n\
10d019
 #	sortlist <none>\n\
10d019
+	stale-answer-enable false;\n\
10d019
+	stale-answer-ttl 1; /* 1 second */\n\
10d019
 #	topology <none>\n\
10d019
 	transfer-format many-answers;\n\
10d019
 	v6-bias 50;\n\
10d019
diff --git a/bin/named/control.c b/bin/named/control.c
943bc6
index 23620b4..0756c73 100644
10d019
--- a/bin/named/control.c
10d019
+++ b/bin/named/control.c
10d019
@@ -282,6 +282,8 @@ ns_control_docommand(isccc_sexpr_t *message, bool readonly,
10d019
 		result = ns_server_validation(ns_g_server, lex, text);
10d019
 	} else if (command_compare(command, NS_COMMAND_ZONESTATUS)) {
10d019
 		result = ns_server_zonestatus(ns_g_server, lex, text);
10d019
+	} else if (command_compare(command, NS_COMMAND_SERVESTALE)) {
10d019
+		result = ns_server_servestale(ns_g_server, lex, text);
10d019
 	} else {
10d019
 		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
10d019
 			      NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
10d019
diff --git a/bin/named/include/named/control.h b/bin/named/include/named/control.h
943bc6
index 56bad8d..37403f1 100644
10d019
--- a/bin/named/include/named/control.h
10d019
+++ b/bin/named/include/named/control.h
943bc6
@@ -67,6 +67,7 @@
10d019
 #define NS_COMMAND_MKEYS	"managed-keys"
10d019
 #define NS_COMMAND_DNSTAPREOPEN	"dnstap-reopen"
10d019
 #define NS_COMMAND_DNSTAP	"dnstap"
10d019
+#define NS_COMMAND_SERVESTALE	"serve-stale"
10d019
 
10d019
 isc_result_t
10d019
 ns_controls_create(ns_server_t *server, ns_controls_t **ctrlsp);
10d019
diff --git a/bin/named/include/named/log.h b/bin/named/include/named/log.h
943bc6
index 76e3a51..0d1d985 100644
10d019
--- a/bin/named/include/named/log.h
10d019
+++ b/bin/named/include/named/log.h
943bc6
@@ -30,6 +30,7 @@
10d019
 #define NS_LOGCATEGORY_UPDATE_SECURITY	(&ns_g_categories[6])
10d019
 #define NS_LOGCATEGORY_QUERY_ERRORS	(&ns_g_categories[7])
10d019
 #define NS_LOGCATEGORY_TAT		(&ns_g_categories[8])
10d019
+#define NS_LOGCATEGORY_SERVE_STALE	(&ns_g_categories[9])
10d019
 
10d019
 /*
10d019
  * Backwards compatibility.
10d019
diff --git a/bin/named/include/named/query.h b/bin/named/include/named/query.h
943bc6
index ef1b172..53c052b 100644
10d019
--- a/bin/named/include/named/query.h
10d019
+++ b/bin/named/include/named/query.h
10d019
@@ -35,6 +35,18 @@ typedef struct ns_dbversion {
10d019
 	ISC_LINK(struct ns_dbversion)	link;
10d019
 } ns_dbversion_t;
10d019
 
10d019
+/*%
10d019
+ * nameserver recursion parameters, to uniquely identify a recursion
10d019
+ * query; this is used to detect a recursion loop
10d019
+ */
10d019
+typedef struct ns_query_recparam {
10d019
+	dns_rdatatype_t			qtype;
10d019
+	dns_name_t *			qname;
10d019
+	dns_fixedname_t			fqname;
10d019
+	dns_name_t *			qdomain;
10d019
+	dns_fixedname_t			fqdomain;
10d019
+} ns_query_recparam_t;
10d019
+
10d019
 /*% nameserver query structure */
10d019
 struct ns_query {
10d019
 	unsigned int			attributes;
10d019
@@ -63,6 +75,7 @@ struct ns_query {
10d019
 	unsigned int			dns64_aaaaoklen;
10d019
 	unsigned int			dns64_options;
10d019
 	unsigned int			dns64_ttl;
10d019
+
10d019
 	struct {
10d019
 		dns_db_t *      	db;
10d019
 		dns_zone_t *      	zone;
10d019
@@ -76,6 +89,8 @@ struct ns_query {
10d019
 		bool		authoritative;
10d019
 		bool		is_zone;
10d019
 	} redirect;
10d019
+
10d019
+	ns_query_recparam_t		recparam;
10d019
 	dns_keytag_t root_key_sentinel_keyid;
10d019
 	bool root_key_sentinel_is_ta;
10d019
 	bool root_key_sentinel_not_ta;
10d019
diff --git a/bin/named/include/named/server.h b/bin/named/include/named/server.h
943bc6
index 0ba2627..08a02dc 100644
10d019
--- a/bin/named/include/named/server.h
10d019
+++ b/bin/named/include/named/server.h
943bc6
@@ -227,7 +227,10 @@ enum {
10d019
 
230545
 	dns_nsstatscounter_reclimitdropped = 58,
10d019
 
230545
-	dns_nsstatscounter_max = 59
230545
+	dns_nsstatscounter_trystale = 59,
230545
+	dns_nsstatscounter_usedstale = 60,
10d019
+
230545
+	dns_nsstatscounter_max = 61
10d019
 };
10d019
 
10d019
 /*%
943bc6
@@ -766,4 +769,12 @@ ns_server_mkeys(ns_server_t *server, isc_lex_t *lex, isc_buffer_t **text);
10d019
 isc_result_t
10d019
 ns_server_dnstap(ns_server_t *server, isc_lex_t *lex, isc_buffer_t **text);
10d019
 
10d019
+
10d019
+/*%
10d019
+ * Control whether stale answers are served or not when configured in
10d019
+ * named.conf.
10d019
+ */
10d019
+isc_result_t
10d019
+ns_server_servestale(ns_server_t *server, isc_lex_t *lex,
10d019
+		     isc_buffer_t **text);
10d019
 #endif /* NAMED_SERVER_H */
10d019
diff --git a/bin/named/log.c b/bin/named/log.c
943bc6
index acfa766..ea6f114 100644
10d019
--- a/bin/named/log.c
10d019
+++ b/bin/named/log.c
10d019
@@ -38,6 +38,7 @@ static isc_logcategory_t categories[] = {
10d019
 	{ "update-security",		0 },
10d019
 	{ "query-errors",		0 },
10d019
 	{ "trust-anchor-telemetry",	0 },
10d019
+	{ "serve-stale",                0 },
10d019
 	{ NULL, 			0 }
10d019
 };
10d019
 
10d019
diff --git a/bin/named/query.c b/bin/named/query.c
943bc6
index b14f081..a95f5ad 100644
10d019
--- a/bin/named/query.c
10d019
+++ b/bin/named/query.c
943bc6
@@ -149,10 +149,14 @@ last_cmpxchg(isc_stdtime_t *x, isc_stdtime_t *e, isc_stdtime_t r) {
10d019
 #define REDIRECT(c)		(((c)->query.attributes & \
10d019
 				  NS_QUERYATTR_REDIRECT) != 0)
10d019
 
10d019
-/*% No QNAME Proof? */
10d019
+/*% Does the rdataset 'r' have an attached 'No QNAME Proof'? */
10d019
 #define NOQNAME(r)		(((r)->attributes & \
10d019
 				  DNS_RDATASETATTR_NOQNAME) != 0)
10d019
 
10d019
+/*% Does the rdataset 'r' contain a stale answer? */
10d019
+#define STALE(r)		(((r)->attributes & \
10d019
+				  DNS_RDATASETATTR_STALE) != 0)
10d019
+
10d019
 #ifdef WANT_QUERYTRACE
10d019
 static inline void
10d019
 client_trace(ns_client_t *client, int level, const char *message) {
943bc6
@@ -241,6 +245,10 @@ static bool
10d019
 rpz_ck_dnssec(ns_client_t *client, isc_result_t qresult,
10d019
 	      dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
10d019
 
10d019
+static void
10d019
+recparam_update(ns_query_recparam_t *param, dns_rdatatype_t qtype,
10d019
+		const dns_name_t *qname, const dns_name_t *qdomain);
10d019
+
10d019
 /*%
10d019
  * Increment query statistics counters.
10d019
  */
943bc6
@@ -494,6 +502,7 @@ query_reset(ns_client_t *client, bool everything) {
10d019
 	client->query.isreferral = false;
10d019
 	client->query.dns64_options = 0;
10d019
 	client->query.dns64_ttl = UINT32_MAX;
10d019
+	recparam_update(&client->query.recparam, 0, NULL, NULL);
10d019
 	client->query.root_key_sentinel_keyid = 0;
10d019
 	client->query.root_key_sentinel_is_ta = false;
10d019
 	client->query.root_key_sentinel_not_ta = false;
943bc6
@@ -4305,6 +4314,54 @@ log_quota(ns_client_t *client, isc_stdtime_t *last, isc_stdtime_t now,
943bc6
 	}
10d019
 }
10d019
 
10d019
+/*%
10d019
+ * Check whether the recursion parameters in 'param' match the current query's
10d019
+ * recursion parameters provided in 'qtype', 'qname', and 'qdomain'.
10d019
+ */
10d019
+static bool
10d019
+recparam_match(const ns_query_recparam_t *param, dns_rdatatype_t qtype,
10d019
+	       const dns_name_t *qname, const dns_name_t *qdomain)
10d019
+{
10d019
+	REQUIRE(param != NULL);
10d019
+
10d019
+	return (param->qtype == qtype &&
10d019
+	        param->qname != NULL && qname != NULL &&
10d019
+	        param->qdomain != NULL && qdomain != NULL &&
10d019
+	        dns_name_equal(param->qname, qname) &&
10d019
+	        dns_name_equal(param->qdomain, qdomain));
10d019
+}
10d019
+
10d019
+/*%
10d019
+ * Update 'param' with current query's recursion parameters provided in
10d019
+ * 'qtype', 'qname', and 'qdomain'.
10d019
+ */
10d019
+static void
10d019
+recparam_update(ns_query_recparam_t *param, dns_rdatatype_t qtype,
10d019
+		const dns_name_t *qname, const dns_name_t *qdomain)
10d019
+{
10d019
+	isc_result_t result;
10d019
+
10d019
+	REQUIRE(param != NULL);
10d019
+
10d019
+	param->qtype = qtype;
10d019
+
10d019
+	if (qname == NULL) {
10d019
+		param->qname = NULL;
10d019
+	} else {
10d019
+		param->qname = dns_fixedname_initname(&param->fqname);
10d019
+		result = dns_name_copy(qname, param->qname, NULL);
10d019
+		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10d019
+	}
10d019
+
10d019
+	if (qdomain == NULL) {
10d019
+		param->qdomain = NULL;
10d019
+	} else {
10d019
+		param->qdomain = dns_fixedname_initname(&param->fqdomain);
10d019
+		result = dns_name_copy(qdomain, param->qdomain, NULL);
10d019
+		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10d019
+	}
10d019
+}
10d019
+
10d019
 static isc_result_t
10d019
 query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qname,
10d019
 	      dns_name_t *qdomain, dns_rdataset_t *nameservers,
943bc6
@@ -4314,6 +4371,19 @@ query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qname,
10d019
 	dns_rdataset_t *rdataset, *sigrdataset;
10d019
 	isc_sockaddr_t *peeraddr;
10d019
 
10d019
+	/*
10d019
+	 * Check recursion parameters from the previous query to see if they
10d019
+	 * match.  If not, update recursion parameters and proceed.
10d019
+	 */
10d019
+	if (recparam_match(&client->query.recparam, qtype, qname, qdomain)) {
10d019
+		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
10d019
+			      NS_LOGMODULE_QUERY, ISC_LOG_INFO,
10d019
+			      "recursion loop detected");
10d019
+		return (ISC_R_FAILURE);
10d019
+	}
10d019
+
10d019
+	recparam_update(&client->query.recparam, qtype, qname, qdomain);
10d019
+
10d019
 	if (!resuming)
10d019
 		inc_stats(client, dns_nsstatscounter_recursion);
10d019
 
943bc6
@@ -6821,6 +6891,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 	int line = -1;
10d019
 	bool dns64_exclude, dns64, rpz;
10d019
 	bool nxrewrite = false;
10d019
+	bool want_stale = false;
10d019
 	bool redirected = false;
10d019
 	dns_clientinfomethods_t cm;
10d019
 	dns_clientinfo_t ci;
943bc6
@@ -7130,6 +7201,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 		type = qtype;
10d019
 
10d019
  restart:
10d019
+	// query_start
10d019
 	CTRACE(ISC_LOG_DEBUG(3), "query_find: restart");
10d019
 	want_restart = false;
10d019
 	authoritative = false;
943bc6
@@ -7274,6 +7346,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 	}
10d019
 
10d019
  db_find:
10d019
+	// query_lookup
10d019
 	CTRACE(ISC_LOG_DEBUG(3), "query_find: db_find");
10d019
 	/*
10d019
 	 * We'll need some resources...
943bc6
@@ -7331,6 +7404,35 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 	if (!is_zone)
10d019
 		dns_cache_updatestats(client->view->cache, result);
10d019
 
10d019
+	if (want_stale) {
10d019
+		char namebuf[DNS_NAME_FORMATSIZE];
10d019
+		bool success;
10d019
+
10d019
+		client->query.dboptions &= ~DNS_DBFIND_STALEOK;
10d019
+		want_stale = false;
10d019
+
10d019
+		if (dns_rdataset_isassociated(rdataset) &&
10d019
+		    dns_rdataset_count(rdataset) > 0 &&
10d019
+		    STALE(rdataset)) {
10d019
+			rdataset->ttl = client->view->staleanswerttl;
10d019
+			success = true;
10d019
+		} else {
10d019
+			success = false;
10d019
+		}
10d019
+
10d019
+		dns_name_format(client->query.qname,
10d019
+				namebuf, sizeof(namebuf));
10d019
+		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_SERVE_STALE,
10d019
+			      NS_LOGMODULE_QUERY, ISC_LOG_INFO,
10d019
+			      "%s resolver failure, stale answer %s",
10d019
+			      namebuf, success ? "used" : "unavailable");
10d019
+
10d019
+		if (!success) {
10d019
+			QUERY_ERROR(DNS_R_SERVFAIL);
10d019
+			goto cleanup;
10d019
+		}
10d019
+	}
10d019
+
10d019
  resume:
10d019
 	CTRACE(ISC_LOG_DEBUG(3), "query_find: resume");
10d019
 
943bc6
@@ -7676,6 +7778,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 		 * The cache doesn't even have the root NS.  Get them from
10d019
 		 * the hints DB.
10d019
 		 */
10d019
+		// query_notfound
10d019
 		INSIST(!is_zone);
10d019
 		if (db != NULL)
10d019
 			dns_db_detach(&db);
943bc6
@@ -7738,12 +7841,14 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 		 */
10d019
 		/* FALLTHROUGH */
10d019
 	case DNS_R_DELEGATION:
10d019
+		// query_delegation
10d019
 		authoritative = false;
10d019
 		if (is_zone) {
10d019
 			/*
10d019
 			 * Look to see if we are authoritative for the
10d019
 			 * child zone if the query type is DS.
10d019
 			 */
10d019
+			// query_zone_delegation
10d019
 			if (!RECURSIONOK(client) &&
10d019
 			    (options & DNS_GETDB_NOEXACT) != 0 &&
10d019
 			    qtype == dns_rdatatype_ds) {
943bc6
@@ -8130,6 +8235,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 						       false, true);
10d019
 			}
10d019
 		}
10d019
+		// query_nxdomain
10d019
 		if (dns_rdataset_isassociated(rdataset)) {
10d019
 			/*
10d019
 			 * If we've got a NSEC record, we need to save the
943bc6
@@ -8450,7 +8556,8 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 		/*
10d019
 		 * If we have a zero ttl from the cache refetch it.
10d019
 		 */
10d019
-		if (!is_zone && !resuming && rdataset->ttl == 0 &&
10d019
+		// query_cname
10d019
+		if (!is_zone && !resuming && !STALE(rdataset) && rdataset->ttl == 0 &&
10d019
 		    RECURSIONOK(client))
10d019
 		{
10d019
 			if (dns_rdataset_isassociated(rdataset))
943bc6
@@ -8676,7 +8783,11 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 			 "query_find: unexpected error after resuming: %s",
10d019
 			 isc_result_totext(result));
10d019
 		CTRACE(ISC_LOG_ERROR, errmsg);
10d019
-		QUERY_ERROR(DNS_R_SERVFAIL);
10d019
+		if (resuming) {
10d019
+			want_stale = true;
10d019
+		} else {
10d019
+			QUERY_ERROR(DNS_R_SERVFAIL);
10d019
+		}
10d019
 		goto cleanup;
10d019
 	}
10d019
 
943bc6
@@ -8932,7 +9043,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 		/*
10d019
 		 * If we have a zero ttl from the cache refetch it.
10d019
 		 */
10d019
-		if (!is_zone && !resuming && rdataset->ttl == 0 &&
10d019
+		if (!is_zone && !resuming && !STALE(rdataset) && rdataset->ttl == 0 &&
10d019
 		    RECURSIONOK(client))
10d019
 		{
10d019
 			if (dns_rdataset_isassociated(rdataset))
943bc6
@@ -8943,6 +9054,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 			if (node != NULL)
10d019
 				dns_db_detachnode(db, &node);
10d019
 
10d019
+			// query_respond
10d019
 			INSIST(!REDIRECT(client));
10d019
 			result = query_recurse(client, qtype,
10d019
 					       client->query.qname,
943bc6
@@ -9223,6 +9335,7 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 				       dns_fixedname_name(&wildcardname),
10d019
 				       true, false);
10d019
  cleanup:
10d019
+	// query_done
10d019
 	CTRACE(ISC_LOG_DEBUG(3), "query_find: cleanup");
10d019
 	/*
10d019
 	 * General cleanup.
943bc6
@@ -9279,6 +9392,49 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
10d019
 		goto restart;
10d019
 	}
10d019
 
10d019
+	if (want_stale) {
10d019
+		dns_ttl_t stale_ttl = 0;
10d019
+		isc_result_t result;
10d019
+		bool staleanswersok = false;
10d019
+
10d019
+		/*
10d019
+		 * Stale answers only make sense if stale_ttl > 0 but
10d019
+		 * we want rndc to be able to control returning stale
10d019
+		 * answers if they are configured.
10d019
+		 */
10d019
+		dns_db_attach(client->view->cachedb, &db);
10d019
+		result = dns_db_getservestalettl(db, &stale_ttl);
10d019
+		if (result == ISC_R_SUCCESS && stale_ttl > 0)  {
10d019
+			switch (client->view->staleanswersok) {
10d019
+			case dns_stale_answer_yes:
10d019
+				staleanswersok = true;
10d019
+				break;
10d019
+			case dns_stale_answer_conf:
10d019
+				staleanswersok =
10d019
+					client->view->staleanswersenable;
10d019
+				break;
10d019
+			case dns_stale_answer_no:
10d019
+				staleanswersok = false;
10d019
+				break;
10d019
+			}
10d019
+		} else {
10d019
+			staleanswersok = false;
10d019
+		}
10d019
+
10d019
+		if (staleanswersok) {
10d019
+			client->query.dboptions |= DNS_DBFIND_STALEOK;
10d019
+			inc_stats(client, dns_nsstatscounter_trystale);
10d019
+			if (client->query.fetch != NULL)
10d019
+				dns_resolver_destroyfetch(
10d019
+						   &client->query.fetch);
10d019
+			goto db_find;
10d019
+		}
10d019
+		dns_db_detach(&db);
10d019
+		want_stale = false;
10d019
+		QUERY_ERROR(DNS_R_SERVFAIL);
10d019
+		goto cleanup;
10d019
+	}
10d019
+
10d019
 	if (eresult != ISC_R_SUCCESS &&
10d019
 	    (!PARTIALANSWER(client) || WANTRECURSION(client)
10d019
 	     || eresult == DNS_R_DROP)) {
10d019
diff --git a/bin/named/server.c b/bin/named/server.c
943bc6
index 2bdf690..3a5ba91 100644
10d019
--- a/bin/named/server.c
10d019
+++ b/bin/named/server.c
230545
@@ -1720,7 +1720,8 @@ static bool
10d019
 cache_sharable(dns_view_t *originview, dns_view_t *view,
10d019
 	       bool new_zero_no_soattl,
10d019
 	       unsigned int new_cleaning_interval,
10d019
-	       uint64_t new_max_cache_size)
10d019
+	       uint64_t new_max_cache_size,
10d019
+	       uint32_t new_stale_ttl)
10d019
 {
10d019
 	/*
10d019
 	 * If the cache cannot even reused for the same view, it cannot be
230545
@@ -1735,6 +1736,7 @@ cache_sharable(dns_view_t *originview, dns_view_t *view,
10d019
 	 */
10d019
 	if (dns_cache_getcleaninginterval(originview->cache) !=
10d019
 	    new_cleaning_interval ||
10d019
+	    dns_cache_getservestalettl(originview->cache) != new_stale_ttl ||
10d019
 	    dns_cache_getcachesize(originview->cache) != new_max_cache_size) {
10d019
 		return (false);
10d019
 	}
230545
@@ -3290,6 +3292,7 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
10d019
 	size_t max_acache_size;
10d019
 	size_t max_adb_size;
10d019
 	uint32_t lame_ttl, fail_ttl;
10d019
+	uint32_t max_stale_ttl;
10d019
 	dns_tsig_keyring_t *ring = NULL;
10d019
 	dns_view_t *pview = NULL;	/* Production view */
10d019
 	isc_mem_t *cmctx = NULL, *hmctx = NULL;
230545
@@ -3318,6 +3321,7 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
10d019
 	bool old_rpz_ok = false;
10d019
 	isc_dscp_t dscp4 = -1, dscp6 = -1;
10d019
 	dns_dyndbctx_t *dctx = NULL;
10d019
+	unsigned int resolver_param;
10d019
 
10d019
 	REQUIRE(DNS_VIEW_VALID(view));
10d019
 
230545
@@ -3732,6 +3736,24 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
10d019
 	if (view->maxncachettl > 7 * 24 * 3600)
10d019
 		view->maxncachettl = 7 * 24 * 3600;
10d019
 
10d019
+	obj = NULL;
10d019
+	result = ns_config_get(maps, "max-stale-ttl", &obj);
10d019
+	INSIST(result == ISC_R_SUCCESS);
10d019
+	max_stale_ttl = cfg_obj_asuint32(obj);
10d019
+
10d019
+	obj = NULL;
10d019
+	result = ns_config_get(maps, "stale-answer-enable", &obj);
10d019
+	INSIST(result == ISC_R_SUCCESS);
10d019
+	view->staleanswersenable = cfg_obj_asboolean(obj);
10d019
+
10d019
+	result = dns_viewlist_find(&ns_g_server->viewlist, view->name,
10d019
+				   view->rdclass, &pview);
10d019
+	if (result == ISC_R_SUCCESS) {
10d019
+		view->staleanswersok = pview->staleanswersok;
10d019
+		dns_view_detach(&pview);
10d019
+	} else
10d019
+		view->staleanswersok = dns_stale_answer_conf;
10d019
+
10d019
 	/*
10d019
 	 * Configure the view's cache.
10d019
 	 *
230545
@@ -3765,7 +3787,8 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
10d019
 	nsc = cachelist_find(cachelist, cachename, view->rdclass);
10d019
 	if (nsc != NULL) {
10d019
 		if (!cache_sharable(nsc->primaryview, view, zero_no_soattl,
10d019
-				    cleaning_interval, max_cache_size)) {
10d019
+				    cleaning_interval, max_cache_size,
10d019
+				    max_stale_ttl)) {
10d019
 			isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
10d019
 				      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
10d019
 				      "views %s and %s can't share the cache "
230545
@@ -3864,9 +3887,15 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
10d019
 
10d019
 	dns_cache_setcleaninginterval(cache, cleaning_interval);
10d019
 	dns_cache_setcachesize(cache, max_cache_size);
10d019
+	dns_cache_setservestalettl(cache, max_stale_ttl);
10d019
 
10d019
 	dns_cache_detach(&cache);
10d019
 
10d019
+	obj = NULL;
10d019
+	result = ns_config_get(maps, "stale-answer-ttl", &obj);
10d019
+	INSIST(result == ISC_R_SUCCESS);
10d019
+	view->staleanswerttl = ISC_MAX(cfg_obj_asuint32(obj), 1);
10d019
+
10d019
 	/*
10d019
 	 * Resolver.
10d019
 	 *
230545
@@ -4055,6 +4084,21 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
10d019
 		maxbits = 4096;
10d019
 	view->maxbits = maxbits;
10d019
 
10d019
+	/*
10d019
+	 * Set resolver retry parameters.
10d019
+	 */
10d019
+	obj = NULL;
10d019
+	CHECK(ns_config_get(maps, "resolver-retry-interval", &obj));
10d019
+	resolver_param = cfg_obj_asuint32(obj);
10d019
+	if (resolver_param > 0)
10d019
+		dns_resolver_setretryinterval(view->resolver, resolver_param);
10d019
+
10d019
+	obj = NULL;
10d019
+	CHECK(ns_config_get(maps, "resolver-nonbackoff-tries", &obj));
10d019
+	resolver_param = cfg_obj_asuint32(obj);
10d019
+	if (resolver_param > 0)
10d019
+		dns_resolver_setnonbackofftries(view->resolver, resolver_param);
10d019
+
10d019
 	/*
10d019
 	 * Set supported DNSSEC algorithms.
10d019
 	 */
943bc6
@@ -14559,3 +14603,132 @@ ns_server_dnstap(ns_server_t *server, isc_lex_t *lex, isc_buffer_t **text) {
10d019
 	return (ISC_R_NOTIMPLEMENTED);
10d019
 #endif
10d019
 }
10d019
+
10d019
+isc_result_t
10d019
+ns_server_servestale(ns_server_t *server, isc_lex_t *lex,
10d019
+		     isc_buffer_t **text)
10d019
+{
10d019
+	char *ptr, *classtxt, *viewtxt = NULL;
10d019
+	char msg[128];
10d019
+	dns_rdataclass_t rdclass = dns_rdataclass_in;
10d019
+	dns_view_t *view;
10d019
+	bool found = false;
10d019
+	dns_stale_answer_t staleanswersok = dns_stale_answer_conf;
10d019
+	bool wantstatus = false;
10d019
+	isc_result_t result = ISC_R_SUCCESS;
10d019
+
10d019
+	/* Skip the command name. */
10d019
+	ptr = next_token(lex, text);
10d019
+	if (ptr == NULL)
10d019
+		return (ISC_R_UNEXPECTEDEND);
10d019
+
10d019
+	ptr = next_token(lex, NULL);
10d019
+	if (ptr == NULL)
10d019
+		return (ISC_R_UNEXPECTEDEND);
10d019
+
10d019
+	if (strcasecmp(ptr, "on") == 0 || strcasecmp(ptr, "yes") == 0) {
10d019
+		staleanswersok = dns_stale_answer_yes;
10d019
+	} else if (strcasecmp(ptr, "off") == 0 || strcasecmp(ptr, "no") == 0) {
10d019
+		staleanswersok = dns_stale_answer_no;
10d019
+	} else if (strcasecmp(ptr, "reset") == 0) {
10d019
+		staleanswersok = dns_stale_answer_conf;
10d019
+	} else if (strcasecmp(ptr, "status") == 0) {
10d019
+		wantstatus = true;
10d019
+	} else
10d019
+		return (DNS_R_SYNTAX);
10d019
+
10d019
+	/* Look for the optional class name. */
10d019
+	classtxt = next_token(lex, text);
10d019
+	if (classtxt != NULL) {
10d019
+		/* Look for the optional view name. */
10d019
+		viewtxt = next_token(lex, text);
10d019
+	}
10d019
+
10d019
+	if (classtxt != NULL) {
10d019
+		isc_textregion_t r;
10d019
+
10d019
+		r.base = classtxt;
10d019
+		r.length = strlen(classtxt);
10d019
+		result = dns_rdataclass_fromtext(&rdclass, &r);
10d019
+		if (result != ISC_R_SUCCESS) {
10d019
+			if (viewtxt == NULL) {
10d019
+				viewtxt = classtxt;
10d019
+				classtxt = NULL;
10d019
+				result = ISC_R_SUCCESS;
10d019
+			} else {
10d019
+				snprintf(msg, sizeof(msg),
10d019
+					 "unknown class '%s'", classtxt);
10d019
+				(void) putstr(text, msg);
10d019
+				goto cleanup;
10d019
+			}
10d019
+		}
10d019
+	}
10d019
+
10d019
+	result = isc_task_beginexclusive(server->task);
10d019
+	RUNTIME_CHECK(result == ISC_R_SUCCESS);
10d019
+
10d019
+	for (view = ISC_LIST_HEAD(server->viewlist);
10d019
+	     view != NULL;
10d019
+	     view = ISC_LIST_NEXT(view, link))
10d019
+	{
10d019
+		dns_ttl_t stale_ttl = 0;
10d019
+		dns_db_t *db = NULL;
10d019
+
10d019
+		if (classtxt != NULL && rdclass != view->rdclass)
10d019
+			continue;
10d019
+
10d019
+		if (viewtxt != NULL && strcmp(view->name, viewtxt) != 0)
10d019
+			continue;
10d019
+
10d019
+		if (!wantstatus) {
10d019
+			view->staleanswersok = staleanswersok;
10d019
+			found = true;
10d019
+			continue;
10d019
+		}
10d019
+
10d019
+		db = NULL;
10d019
+		dns_db_attach(view->cachedb, &db);
10d019
+		(void)dns_db_getservestalettl(db, &stale_ttl);
10d019
+		dns_db_detach(&db);
10d019
+		if (found)
10d019
+			CHECK(putstr(text, "\n"));
10d019
+		CHECK(putstr(text, view->name));
10d019
+		CHECK(putstr(text, ": "));
10d019
+		switch (view->staleanswersok) {
10d019
+		case dns_stale_answer_yes:
10d019
+			if (stale_ttl > 0)
10d019
+				CHECK(putstr(text, "on (rndc)"));
10d019
+			else
10d019
+				CHECK(putstr(text, "off (not-cached)"));
10d019
+			break;
10d019
+		case dns_stale_answer_no:
10d019
+			CHECK(putstr(text, "off (rndc)"));
10d019
+			break;
10d019
+		case dns_stale_answer_conf:
10d019
+			if (view->staleanswersenable && stale_ttl > 0)
10d019
+				CHECK(putstr(text, "on"));
10d019
+			else if (view->staleanswersenable)
10d019
+				CHECK(putstr(text, "off (not-cached)"));
10d019
+			else
10d019
+				CHECK(putstr(text, "off"));
10d019
+			break;
10d019
+		}
10d019
+		if (stale_ttl > 0) {
10d019
+			snprintf(msg, sizeof(msg),
10d019
+				 " (stale-answer-ttl=%u max-stale-ttl=%u)",
10d019
+				 view->staleanswerttl, stale_ttl);
10d019
+			CHECK(putstr(text, msg));
10d019
+		}
10d019
+		found = true;
10d019
+	}
10d019
+	isc_task_endexclusive(ns_g_server->task);
10d019
+
10d019
+	if (!found)
10d019
+		result = ISC_R_NOTFOUND;
10d019
+
10d019
+cleanup:
10d019
+	if (isc_buffer_usedlength(*text) > 0)
10d019
+		(void) putnull(text);
10d019
+
10d019
+	return (result);
10d019
+}
10d019
diff --git a/bin/named/statschannel.c b/bin/named/statschannel.c
943bc6
index 12ab048..4938c03 100644
10d019
--- a/bin/named/statschannel.c
10d019
+++ b/bin/named/statschannel.c
230545
@@ -300,6 +300,12 @@ init_desc(void) {
230545
 	SET_NSSTATDESC(reclimitdropped,
230545
 		       "queries dropped due to recursive client limit",
230545
 		       "RecLimitDropped");
10d019
+	SET_NSSTATDESC(trystale,
10d019
+		       "attempts to use stale cache data after lookup failure",
10d019
+		       "QryTryStale");
10d019
+	SET_NSSTATDESC(usedstale,
10d019
+		       "successful uses of stale cache data after lookup failure",
10d019
+		       "QryUsedStale");
10d019
 	INSIST(i == dns_nsstatscounter_max);
10d019
 
10d019
 	/* Initialize resolver statistics */
10d019
diff --git a/bin/rndc/rndc.c b/bin/rndc/rndc.c
943bc6
index 0acfe3a..2c21c1d 100644
10d019
--- a/bin/rndc/rndc.c
10d019
+++ b/bin/rndc/rndc.c
10d019
@@ -160,6 +160,8 @@ command is one of the following:\n\
10d019
   scan		Scan available network interfaces for changes.\n\
10d019
   secroots [view ...]\n\
10d019
 		Write security roots to the secroots file.\n\
10d019
+  serve-stale	( yes | no | reset ) [class [view]]\n\
10d019
+		Control whether stale answers are returned\n\
10d019
   showzone zone [class [view]]\n\
10d019
 		Print a zone's configuration.\n\
10d019
   sign zone [class [view]]\n\
10d019
diff --git a/bin/rndc/rndc.docbook b/bin/rndc/rndc.docbook
943bc6
index 159ded9..12a7208 100644
10d019
--- a/bin/rndc/rndc.docbook
10d019
+++ b/bin/rndc/rndc.docbook
230545
@@ -689,6 +689,25 @@
10d019
 	</listitem>
10d019
       </varlistentry>
10d019
 
10d019
+      <varlistentry>
10d019
+	<term><userinput>serve-stale ( on | off | reset | status) <optional><replaceable>class</replaceable> <optional><replaceable>view</replaceable></optional></optional></userinput></term>
10d019
+	<listitem>
10d019
+	  <para>
10d019
+	    Enable, disable, or reset the serving of stale answers
10d019
+	    as configured in named.conf. Serving of stale answers
10d019
+	    will remain disabled across <filename>named.conf</filename>
10d019
+	    reloads if disabled via rndc until it is reset via rndc.
10d019
+	  </para>
10d019
+	  <para>
10d019
+	    Status will report whether serving of stale answers is
10d019
+	    currently enabled, disabled or not configured for a
10d019
+	    view.  If serving of stale records is configured then
10d019
+	    the values of stale-answer-ttl and max-stale-ttl are
10d019
+	    reported.
10d019
+	  </para>
10d019
+	</listitem>
10d019
+      </varlistentry>
10d019
+
10d019
       <varlistentry>
10d019
 	<term><userinput>secroots <optional>-</optional> <optional><replaceable>view ...</replaceable></optional></userinput></term>
10d019
 	<listitem>
10d019
diff --git a/bin/tests/system/chain/prereq.sh b/bin/tests/system/chain/prereq.sh
943bc6
index 23bedcd..43385de 100644
10d019
--- a/bin/tests/system/chain/prereq.sh
10d019
+++ b/bin/tests/system/chain/prereq.sh
10d019
@@ -48,3 +48,10 @@ else
10d019
     echo_i "This test requires the Net::DNS::Nameserver library." >&2
10d019
     exit 1
10d019
 fi
10d019
+if $PERL -e 'use Net::DNS::Nameserver;' 2>/dev/null
10d019
+then
10d019
+	:
10d019
+else
10d019
+    echo "I:This test requires the Net::DNS::Nameserver library." >&2
10d019
+    exit 1
10d019
+fi
10d019
diff --git a/bin/tests/system/conf.sh.in b/bin/tests/system/conf.sh.in
943bc6
index f6412f6..26c8901 100644
10d019
--- a/bin/tests/system/conf.sh.in
10d019
+++ b/bin/tests/system/conf.sh.in
230545
@@ -128,7 +128,7 @@ PARALLELDIRS="dnssec rpzrecurse \
10d019
 	reclimit redirect resolver rndc rootkeysentinel rpz \
10d019
 	rrchecker rrl rrsetorder rsabigexponent runtime \
10d019
 	sfcache smartsign sortlist \
10d019
-	spf staticstub statistics statschannel stub \
10d019
+	spf serve-stale staticstub statistics statschannel stub \
10d019
 	tcp tsig tsiggss \
10d019
 	unknown upforwd verify views wildcard \
10d019
 	xfer xferquota zero zonechecks"
10d019
diff --git a/bin/tests/system/dyndb/driver/db.c b/bin/tests/system/dyndb/driver/db.c
10d019
index 02aa6ab..a77c7de 100644
10d019
--- a/bin/tests/system/dyndb/driver/db.c
10d019
+++ b/bin/tests/system/dyndb/driver/db.c
10d019
@@ -629,6 +629,8 @@ static dns_dbmethods_t sampledb_methods = {
10d019
 	hashsize,
10d019
 	NULL,
10d019
 	NULL,
10d019
+	NULL,
10d019
+	NULL,
10d019
 };
10d019
 
10d019
 /* Auxiliary driver functions. */
10d019
diff --git a/bin/tests/system/serve-stale/.gitignore b/bin/tests/system/serve-stale/.gitignore
10d019
new file mode 100644
10d019
index 0000000..2272eef
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/.gitignore
10d019
@@ -0,0 +1,11 @@
10d019
+/ans2/ans.pid
10d019
+/ans2/ans.pl
10d019
+/dig.out*
10d019
+/ns1/named.conf
10d019
+/ns3/named.conf
10d019
+/ns3/root.bk
10d019
+/rndc.out*
10d019
+named.lock
10d019
+named.pid
10d019
+named.port
10d019
+named.run
10d019
diff --git a/bin/tests/system/serve-stale/ans2/ans.pl.in b/bin/tests/system/serve-stale/ans2/ans.pl.in
10d019
new file mode 100644
10d019
index 0000000..2b39eca
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/ans2/ans.pl.in
10d019
@@ -0,0 +1,178 @@
10d019
+#!/usr/bin/env perl
10d019
+#
10d019
+# Copyright (C) 2014-2016  Internet Systems Consortium, Inc. ("ISC")
10d019
+#
10d019
+# This Source Code Form is subject to the terms of the Mozilla Public
10d019
+# License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+
10d019
+use strict;
10d019
+use warnings;
10d019
+
10d019
+use IO::File;
10d019
+use IO::Socket;
10d019
+use Getopt::Long;
10d019
+use Net::DNS;
10d019
+use Time::HiRes qw(usleep nanosleep);
10d019
+
10d019
+my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
10d019
+print $pidf "$$\n" or die "cannot write pid file: $!";
10d019
+$pidf->close or die "cannot close pid file: $!";
10d019
+sub rmpid { unlink "ans.pid"; exit 1; };
10d019
+
10d019
+$SIG{INT} = \&rmpid;
10d019
+$SIG{TERM} = \&rmpid;
10d019
+
10d019
+my $send_response = 1;
10d019
+
10d019
+my $localaddr = "10.53.0.2";
10d019
+my $localport = @PORT@;
10d019
+my $udpsock = IO::Socket::INET->new(LocalAddr => "$localaddr",
10d019
+   LocalPort => $localport, Proto => "udp", Reuse => 1) or die "$!";
10d019
+
10d019
+#
10d019
+# Delegation
10d019
+#
10d019
+my $SOA = "example 300 IN SOA . . 0 0 0 0 300";
10d019
+my $NS = "example 300 IN NS ns.example";
10d019
+my $A = "ns.example 300 IN A $localaddr";
10d019
+#
10d019
+# Records to be TTL stretched
10d019
+#
10d019
+my $TXT = "data.example 1 IN TXT \"A text record with a 1 second ttl\"";
10d019
+my $negSOA = "example 1 IN SOA . . 0 0 0 0 300";
10d019
+
10d019
+sub reply_handler {
10d019
+    my ($qname, $qclass, $qtype) = @_;
10d019
+    my ($rcode, @ans, @auth, @add);
10d019
+
10d019
+    print ("request: $qname/$qtype\n");
10d019
+    STDOUT->flush();
10d019
+
10d019
+    # Control whether we send a response or not.
10d019
+    # We always respond to control commands.
10d019
+    if ($qname eq "enable" ) {
10d019
+	if ($qtype eq "TXT") {
10d019
+	    $send_response = 1;
10d019
+            my $rr = new Net::DNS::RR("$qname 0 $qclass TXT \"$send_response\"");
10d019
+            push @ans, $rr;
10d019
+	}
10d019
+	$rcode = "NOERROR";
10d019
+        return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
10d019
+    } elsif ($qname eq "disable" ) {
10d019
+	if ($qtype eq "TXT") {
10d019
+	    $send_response = 0;
10d019
+            my $rr = new Net::DNS::RR("$qname 0 $qclass TXT \"$send_response\"");
10d019
+            push @ans, $rr;
10d019
+	}
10d019
+	$rcode = "NOERROR";
10d019
+        return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
10d019
+    }
10d019
+
10d019
+    # If we are not responding to queries we are done.
10d019
+    return if (!$send_response);
10d019
+
10d019
+    # Construct the response and send it.
10d019
+    if ($qname eq "ns.example" ) {
10d019
+	if ($qtype eq "A") {
10d019
+	    my $rr = new Net::DNS::RR($A);
10d019
+	    push @ans, $rr;
10d019
+	} else {
10d019
+	    my $rr = new Net::DNS::RR($SOA);
10d019
+	    push @auth, $rr;
10d019
+	}
10d019
+	$rcode = "NOERROR";
10d019
+    } elsif ($qname eq "example") {
10d019
+	if ($qtype eq "NS") {
10d019
+	    my $rr = new Net::DNS::RR($NS);
10d019
+	    push @auth, $rr;
10d019
+	    $rr = new Net::DNS::RR($A);
10d019
+	    push @add, $rr;
10d019
+	} elsif ($qtype eq "SOA") {
10d019
+	    my $rr = new Net::DNS::RR($SOA);
10d019
+	    push @ans, $rr;
10d019
+	} else {
10d019
+	    my $rr = new Net::DNS::RR($SOA);
10d019
+	    push @auth, $rr;
10d019
+	}
10d019
+	$rcode = "NOERROR";
10d019
+    } elsif ($qname eq "nodata.example") {
10d019
+	my $rr = new Net::DNS::RR($negSOA);
10d019
+	push @auth, $rr;
10d019
+	$rcode = "NOERROR";
10d019
+    } elsif ($qname eq "data.example") {
10d019
+	if ($qtype eq "TXT") {
10d019
+	    my $rr = new Net::DNS::RR($TXT);
10d019
+	    push @ans, $rr;
10d019
+	} else {
10d019
+	    my $rr = new Net::DNS::RR($negSOA);
10d019
+	    push @auth, $rr;
10d019
+	}
10d019
+	$rcode = "NOERROR";
10d019
+    } elsif ($qname eq "nxdomain.example") {
10d019
+	my $rr = new Net::DNS::RR($negSOA);
10d019
+	push @auth, $rr;
10d019
+	$rcode = "NXDOMAIN";
10d019
+    } else {
10d019
+        my $rr = new Net::DNS::RR($SOA);
10d019
+	push @auth, $rr;
10d019
+	$rcode = "NXDOMAIN";
10d019
+    }
10d019
+
10d019
+    # mark the answer as authoritive (by setting the 'aa' flag
10d019
+    return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
10d019
+}
10d019
+
10d019
+GetOptions(
10d019
+    'port=i' => \$localport,
10d019
+);
10d019
+
10d019
+my $rin;
10d019
+my $rout;
10d019
+
10d019
+for (;;) {
10d019
+	$rin = '';
10d019
+	vec($rin, fileno($udpsock), 1) = 1;
10d019
+
10d019
+	select($rout = $rin, undef, undef, undef);
10d019
+
10d019
+	if (vec($rout, fileno($udpsock), 1)) {
10d019
+		my ($buf, $request, $err);
10d019
+		$udpsock->recv($buf, 512);
10d019
+
10d019
+		if ($Net::DNS::VERSION > 0.68) {
10d019
+			$request = new Net::DNS::Packet(\$buf, 0);
10d019
+			$@ and die $@;
10d019
+		} else {
10d019
+			my $err;
10d019
+			($request, $err) = new Net::DNS::Packet(\$buf, 0);
10d019
+			$err and die $err;
10d019
+		}
10d019
+
10d019
+		my @questions = $request->question;
10d019
+		my $qname = $questions[0]->qname;
10d019
+		my $qclass = $questions[0]->qclass;
10d019
+		my $qtype = $questions[0]->qtype;
10d019
+		my $id = $request->header->id;
10d019
+
10d019
+		my ($rcode, $ans, $auth, $add, $headermask) = reply_handler($qname, $qclass, $qtype);
10d019
+
10d019
+		if (!defined($rcode)) {
10d019
+			print "  Silently ignoring query\n";
10d019
+			next;
10d019
+		}
10d019
+
10d019
+		my $reply = Net::DNS::Packet->new();
10d019
+		$reply->header->qr(1);
10d019
+		$reply->header->aa(1) if $headermask->{'aa'};
10d019
+		$reply->header->id($id);
10d019
+		$reply->header->rcode($rcode);
10d019
+		$reply->push("question",   @questions);
10d019
+		$reply->push("answer",     @$ans)  if $ans;
10d019
+		$reply->push("authority",  @$auth) if $auth;
10d019
+		$reply->push("additional", @$add)  if $add;
10d019
+
10d019
+		my $num_chars = $udpsock->send($reply->data);
10d019
+		print "  Sent $num_chars bytes via UDP\n";
10d019
+	}
10d019
+}
10d019
diff --git a/bin/tests/system/serve-stale/clean.sh b/bin/tests/system/serve-stale/clean.sh
10d019
new file mode 100644
10d019
index 0000000..2397326
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/clean.sh
10d019
@@ -0,0 +1,15 @@
10d019
+# Copyright (C) 2017  Internet Systems Consortium, Inc. ("ISC")
10d019
+#
10d019
+# This Source Code Form is subject to the terms of the Mozilla Public
10d019
+# License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+
10d019
+rm -f test.output
10d019
+rm -f dig.out.test*
10d019
+rm -f ans2/ans.pl
10d019
+rm -f ns3/root.bk
10d019
+rm -f rndc.out.test*
10d019
+rm -f ns*/named.memstats
10d019
+rm -f ns*/managed-keys.bind
10d019
+rm -f ns*/named.conf
10d019
+rm -f ns*/named.run
10d019
diff --git a/bin/tests/system/serve-stale/ns1/named1.conf.in b/bin/tests/system/serve-stale/ns1/named1.conf.in
10d019
new file mode 100644
10d019
index 0000000..8a75a10
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/ns1/named1.conf.in
10d019
@@ -0,0 +1,35 @@
10d019
+/*
10d019
+ * Copyright (C) 2017  Internet Systems Consortium, Inc. ("ISC")
10d019
+ *
10d019
+ * This Source Code Form is subject to the terms of the Mozilla Public
10d019
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+ */
10d019
+
10d019
+key rndc_key {
10d019
+        secret "1234abcd8765";
10d019
+        algorithm hmac-sha256;
10d019
+};
10d019
+
10d019
+controls {
10d019
+	inet 10.53.0.1 port @CONTROLPORT@ allow { any; } keys { rndc_key; };
10d019
+};
10d019
+
10d019
+options {
10d019
+	query-source address 10.53.0.1;
10d019
+	notify-source 10.53.0.1;
10d019
+	transfer-source 10.53.0.1;
10d019
+	port @PORT@;
10d019
+	pid-file "named.pid";
10d019
+	listen-on { 10.53.0.1; };
10d019
+	listen-on-v6 { none; };
10d019
+	recursion yes;
10d019
+	max-stale-ttl 3600;
10d019
+	stale-answer-ttl 1;
10d019
+	stale-answer-enable yes;
10d019
+};
10d019
+
10d019
+zone "." {
10d019
+	type master;
10d019
+	file "root.db";
10d019
+};
10d019
diff --git a/bin/tests/system/serve-stale/ns1/named2.conf.in b/bin/tests/system/serve-stale/ns1/named2.conf.in
10d019
new file mode 100644
10d019
index 0000000..072e6ec
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/ns1/named2.conf.in
10d019
@@ -0,0 +1,35 @@
10d019
+/*
10d019
+ * Copyright (C) 2017  Internet Systems Consortium, Inc. ("ISC")
10d019
+ *
10d019
+ * This Source Code Form is subject to the terms of the Mozilla Public
10d019
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+ */
10d019
+
10d019
+key rndc_key {
10d019
+        secret "1234abcd8765";
10d019
+        algorithm hmac-sha256;
10d019
+};
10d019
+
10d019
+controls {
10d019
+	inet 10.53.0.1 port @CONTROLPORT@ allow { any; } keys { rndc_key; };
10d019
+};
10d019
+
10d019
+options {
10d019
+	query-source address 10.53.0.1;
10d019
+	notify-source 10.53.0.1;
10d019
+	transfer-source 10.53.0.1;
10d019
+	port @PORT@;
10d019
+	pid-file "named.pid";
10d019
+	listen-on { 10.53.0.1; };
10d019
+	listen-on-v6 { none; };
10d019
+	recursion yes;
10d019
+	max-stale-ttl 7200;
10d019
+	stale-answer-ttl 2;
10d019
+	stale-answer-enable yes;
10d019
+};
10d019
+
10d019
+zone "." {
10d019
+	type master;
10d019
+	file "root.db";
10d019
+};
10d019
diff --git a/bin/tests/system/serve-stale/ns1/root.db b/bin/tests/system/serve-stale/ns1/root.db
10d019
new file mode 100644
10d019
index 0000000..eb9ad3e
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/ns1/root.db
10d019
@@ -0,0 +1,5 @@
10d019
+.		300	SOA	. . 0 0 0 0 0
10d019
+.		300	NS	ns.nil.
10d019
+ns.nil.		300	A	10.53.0.1
10d019
+example.	300	NS	ns.example.
10d019
+ns.example.	300	A	10.53.0.2
10d019
diff --git a/bin/tests/system/serve-stale/ns3/named.conf.in b/bin/tests/system/serve-stale/ns3/named.conf.in
10d019
new file mode 100644
10d019
index 0000000..24a3293
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/ns3/named.conf.in
10d019
@@ -0,0 +1,35 @@
10d019
+/*
10d019
+ * Copyright (C) 2017  Internet Systems Consortium, Inc. ("ISC")
10d019
+ *
10d019
+ * This Source Code Form is subject to the terms of the Mozilla Public
10d019
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+ */
10d019
+
10d019
+key rndc_key {
10d019
+        secret "1234abcd8765";
10d019
+        algorithm hmac-sha256;
10d019
+};
10d019
+
10d019
+controls {
10d019
+	inet 10.53.0.3 port @CONTROLPORT@ allow { any; } keys { rndc_key; };
10d019
+};
10d019
+
10d019
+options {
10d019
+	query-source address 10.53.0.3;
10d019
+	notify-source 10.53.0.3;
10d019
+	transfer-source 10.53.0.3;
10d019
+	port @PORT@;
10d019
+	pid-file "named.pid";
10d019
+	listen-on { 10.53.0.3; };
10d019
+	listen-on-v6 { none; };
10d019
+	recursion yes;
10d019
+	// max-stale-ttl 3600;
10d019
+	// stale-answer-ttl 3;
10d019
+};
10d019
+
10d019
+zone "." {
10d019
+	type slave;
10d019
+	masters { 10.53.0.1; };
10d019
+	file "root.bk";
10d019
+};
10d019
diff --git a/bin/tests/system/serve-stale/prereq.sh b/bin/tests/system/serve-stale/prereq.sh
10d019
new file mode 100644
10d019
index 0000000..a3bbef8
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/prereq.sh
10d019
@@ -0,0 +1,38 @@
10d019
+#!/bin/sh
10d019
+#
10d019
+# Copyright (C) 2011, 2012, 2014, 2016  Internet Systems Consortium, Inc. ("ISC")
10d019
+#
10d019
+# This Source Code Form is subject to the terms of the Mozilla Public
10d019
+# License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+
10d019
+SYSTEMTESTTOP=..
10d019
+. $SYSTEMTESTTOP/conf.sh
10d019
+
10d019
+if $PERL -e 'use Net::DNS;' 2>/dev/null
10d019
+then
10d019
+    if $PERL -e 'use Net::DNS; die if ($Net::DNS::VERSION >= 0.69 && $Net::DNS::VERSION <= 0.74);' 2>/dev/null
10d019
+    then
10d019
+        :
10d019
+    else
10d019
+        echo "I:Net::DNS versions 0.69 to 0.74 have bugs that cause this test to fail: please update." >&2
10d019
+        exit 1
10d019
+    fi
10d019
+else
10d019
+    echo "I:This test requires the Net::DNS library." >&2
10d019
+    exit 1
10d019
+fi
10d019
+if $PERL -e 'use Net::DNS::Nameserver;' 2>/dev/null
10d019
+then
10d019
+	:
10d019
+else
10d019
+	echo "I:This test requires the Net::DNS::Nameserver library." >&2
10d019
+	exit 1
10d019
+fi
10d019
+if $PERL -e 'use Time::HiRes;' 2>/dev/null
10d019
+then
10d019
+	:
10d019
+else
10d019
+	echo "I:This test requires the Time::HiRes library." >&2
10d019
+	exit 1
10d019
+fi
10d019
diff --git a/bin/tests/system/serve-stale/setup.sh b/bin/tests/system/serve-stale/setup.sh
10d019
new file mode 100644
10d019
index 0000000..690f43c
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/setup.sh
10d019
@@ -0,0 +1,13 @@
10d019
+#!/bin/sh
10d019
+# Copyright (C) 2017  Internet Systems Consortium, Inc. ("ISC")
10d019
+#
10d019
+# This Source Code Form is subject to the terms of the Mozilla Public
10d019
+# License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+
10d019
+SYSTEMTESTTOP=..
10d019
+. $SYSTEMTESTTOP/conf.sh
10d019
+
10d019
+copy_setports ns1/named1.conf.in ns1/named.conf
10d019
+copy_setports ans2/ans.pl.in ans2/ans.pl
10d019
+copy_setports ns3/named.conf.in ns3/named.conf
10d019
diff --git a/bin/tests/system/serve-stale/tests.sh b/bin/tests/system/serve-stale/tests.sh
10d019
new file mode 100755
10d019
index 0000000..201c996
10d019
--- /dev/null
10d019
+++ b/bin/tests/system/serve-stale/tests.sh
10d019
@@ -0,0 +1,536 @@
10d019
+#!/bin/sh
10d019
+#
10d019
+# Copyright (C) 2000, 2001, 2004, 2007, 2009-2016  Internet Systems Consortium, Inc. ("ISC")
10d019
+#
10d019
+# This Source Code Form is subject to the terms of the Mozilla Public
10d019
+# License, v. 2.0. If a copy of the MPL was not distributed with this
10d019
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
10d019
+
10d019
+SYSTEMTESTTOP=..
10d019
+. $SYSTEMTESTTOP/conf.sh
10d019
+
10d019
+while getopts "p:c:" flag; do
10d019
+    case "$flag" in
10d019
+	p) port=$OPTARG ;;
10d019
+	c) controlport=$OPTARG ;;
10d019
+	*) exit 1 ;;
10d019
+    esac
10d019
+done
10d019
+
10d019
+RNDCCMD="$RNDC -c $SYSTEMTESTTOP/common/rndc.conf -p ${CONTROLPORT} -s"
10d019
+
10d019
+echo "RNDCCMD: ${RNDCCMD}"
10d019
+
10d019
+status=0
10d019
+n=0
10d019
+
10d019
+#echo "I:check ans.pl server ($n)"
10d019
+#$DIG -p ${PORT} @10.53.0.2 example NS
10d019
+#$DIG -p ${PORT} @10.53.0.2 example SOA
10d019
+#$DIG -p ${PORT} @10.53.0.2 ns.example A
10d019
+#$DIG -p ${PORT} @10.53.0.2 ns.example AAAA
10d019
+#$DIG -p ${PORT} @10.53.0.2 txt enable
10d019
+#$DIG -p ${PORT} @10.53.0.2 txt disable
10d019
+#$DIG -p ${PORT} @10.53.0.2 ns.example AAAA
10d019
+#$DIG -p ${PORT} @10.53.0.2 txt enable
10d019
+#$DIG -p ${PORT} @10.53.0.2 ns.example AAAA
10d019
+##$DIG -p ${PORT} @10.53.0.2 data.example TXT
10d019
+#$DIG -p ${PORT} @10.53.0.2 nodata.example TXT
10d019
+#$DIG -p ${PORT} @10.53.0.2 nxdomain.example TXT
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:prime cache data.example ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:prime cache nodata.example ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:prime cache nxdomain.example ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:disable responses from authoritative server ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.2 txt disable  > dig.out.test$n
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "TXT.\"0\"" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+sleep 1
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: on (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale data.example ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nodata.example ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nxdomain.example ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale off' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale off || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: off (rndc) (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale data.example (serve-stale off) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nodata.example (serve-stale off) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nxdomain.example (serve-stale off) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale on' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale on || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: on (rndc) (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale data.example (serve-stale on) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nodata.example (serve-stale on) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nxdomain.example (serve-stale on) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale no' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale no || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: off (rndc) (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale data.example (serve-stale no) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nodata.example (serve-stale no) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nxdomain.example (serve-stale no) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale yes' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale yes || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: on (rndc) (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale data.example (serve-stale yes) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nodata.example (serve-stale yes) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nxdomain.example (serve-stale yes) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale off' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale off || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale reset' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale reset || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: on (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale data.example (serve-stale reset) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nodata.example (serve-stale reset) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check stale nxdomain.example (serve-stale reset) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.1 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc serve-stale off' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale off || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: off (rndc) (stale-answer-ttl=1 max-stale-ttl=3600)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:updating ns1/named.conf ($n)"
10d019
+ret=0
10d019
+sed -e "s/@PORT@/${PORT}/g;s/@CONTROLPORT@/${CONTROLPORT}/g" < ns1/named2.conf.in > ns1/named.conf
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:running 'rndc reload' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 reload > rndc.out.test$n 2>&1 || ret=1
10d019
+grep "server reload successful" rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: off (rndc) (stale-answer-ttl=2 max-stale-ttl=7200)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale > rndc.out.test$n 2>&1 && ret=1
10d019
+grep "unexpected end of input" rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale unknown' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 serve-stale unknown > rndc.out.test$n 2>&1 && ret=1
10d019
+grep "syntax error" rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo_i "flush cache, re-enable serve-stale and query again ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.1 flushtree example > rndc.out.test$n.1 2>&1 || ret=1
10d019
+$RNDCCMD 10.53.0.1 serve-stale on > rndc.out.test$n.2 2>&1 || ret=1
10d019
+$DIG -p ${PORT} @10.53.0.1 data.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo_i "failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.2 txt enable  > dig.out.test$n
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "TXT.\"1\"" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:prime cache data.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:prime cache nodata.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:prime cache nxdomain.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:disable responses from authoritative server ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.2 txt disable  > dig.out.test$n
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "TXT.\"0\"" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+sleep 1
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.3 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: off (stale-answer-ttl=1 max-stale-ttl=604800)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check fail of data.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 data.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check fail of nodata.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 nodata.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check fail of nxdomain.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: SERVFAIL" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale on' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.3 serve-stale on > rndc.out.test$n 2>&1 || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check 'rndc serve-stale status' ($n)"
10d019
+ret=0
10d019
+$RNDCCMD 10.53.0.3 serve-stale status > rndc.out.test$n 2>&1 || ret=1
10d019
+grep '_default: on (rndc) (stale-answer-ttl=1 max-stale-ttl=604800)' rndc.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check data.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 data.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 1," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check nodata.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 nodata.example TXT > dig.out.test$n
10d019
+grep "status: NOERROR" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+n=`expr $n + 1`
10d019
+echo "I:check nxdomain.example (max-stale-ttl default) ($n)"
10d019
+ret=0
10d019
+$DIG -p ${PORT} @10.53.0.3 nxdomain.example TXT > dig.out.test$n
10d019
+grep "status: NXDOMAIN" dig.out.test$n > /dev/null || ret=1
10d019
+grep "ANSWER: 0," dig.out.test$n > /dev/null || ret=1
10d019
+grep "example.*1.*IN" dig.out.test$n > /dev/null || ret=1
10d019
+if [ $ret != 0 ]; then echo "I:failed"; fi
10d019
+status=`expr $status + $ret`
10d019
+
10d019
+echo "I:exit status: $status"
10d019
+[ $status -eq 0 ] || exit 1
10d019
diff --git a/doc/arm/Bv9ARM-book.xml b/doc/arm/Bv9ARM-book.xml
943bc6
index 99c8680..5fbabfe 100644
10d019
--- a/doc/arm/Bv9ARM-book.xml
10d019
+++ b/doc/arm/Bv9ARM-book.xml
943bc6
@@ -4336,6 +4336,9 @@ badresp:1,adberr:0,findfail:0,valfail:0]
10d019
 	  statement in the <filename>named.conf</filename> file:
10d019
 	</para>
10d019
 	<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="options.grammar.xml"/>
10d019
+  [ <command>max-stale-ttl</command> <replaceable>number</replaceable> ; ]
10d019
+  [ <command>stale-answer-enable</command> <replaceable>yes_or_no</replaceable> ; ]
10d019
+  [ <command>stale-answer-ttl</command> <replaceable>number</replaceable> ; ]
10d019
       </section>
10d019
 
10d019
       <section xml:id="options"><info><title><command>options</command> Statement Definition and
943bc6
@@ -4429,6 +4432,7 @@ badresp:1,adberr:0,findfail:0,valfail:0]
10d019
 		  <command>dnssec-validation</command>,
10d019
 		  <command>max-cache-ttl</command>,
10d019
 		  <command>max-ncache-ttl</command>,
10d019
+		  <command>max-stale-ttl</command>,
10d019
 		  <command>max-cache-size</command>, and
10d019
 		  <command>zero-no-soa-ttl</command>.
10d019
 		</para>
943bc6
@@ -5438,7 +5442,6 @@ options {
10d019
 	    </listitem>
10d019
 	  </varlistentry>
10d019
 
10d019
-
10d019
 	  <varlistentry>
10d019
 	    <term><command>max-zone-ttl</command></term>
10d019
 	    <listitem>
943bc6
@@ -5474,6 +5477,21 @@ options {
10d019
 	    </listitem>
10d019
 	  </varlistentry>
10d019
 
10d019
+	  <varlistentry>
10d019
+	    <term><command>stale-answer-ttl</command></term>
10d019
+	    <listitem>
10d019
+	      <para>
10d019
+		Specifies the TTL to be returned on stale answers.
10d019
+		The default is 1 second. The minimal allowed is
10d019
+		also 1 second; a value of 0 will be updated silently
10d019
+		to 1 second.  For stale answers to be returned
10d019
+		<option>max-stale-ttl</option> must be set to a
10d019
+		non zero value and they must not have been disabled
10d019
+		by <command>rndc</command>.
10d019
+	      </para>
10d019
+	    </listitem>
10d019
+	  </varlistentry>
10d019
+
10d019
 	  <varlistentry>
10d019
 	    <term><command>serial-update-method</command></term>
10d019
 	    <listitem>
943bc6
@@ -6227,6 +6245,22 @@ options {
10d019
 	      </listitem>
10d019
 	    </varlistentry>
10d019
 
10d019
+	    <varlistentry>
10d019
+	      <term><command>serve-stale-enable</command></term>
10d019
+	      <listitem>
10d019
+		<para>
10d019
+		  Enable the returning of stale answers when the
10d019
+		  nameservers for the zone are not answering.  This
10d019
+		  is off by default but can be enabled/disabled via
10d019
+		  <command>rndc server-stale on</command> and
10d019
+		  <command>rndc server-stale off</command> which
10d019
+		  override the named.conf setting.  <command>rndc
10d019
+		  server-stale reset</command> will restore control
10d019
+		  via named.conf.
10d019
+		</para>
10d019
+	      </listitem>
10d019
+	    </varlistentry>
10d019
+
10d019
 	    <varlistentry>
10d019
 	      <term><command>nocookie-udp-size</command></term>
10d019
 	      <listitem>
943bc6
@@ -7449,13 +7483,19 @@ options {
10d019
 	      <term><command>resolver-query-timeout</command></term>
10d019
 	      <listitem>
10d019
 		<para>
943bc6
-		  This is the amount of time in seconds that the
943bc6
-		  resolver spends attempting to resolve a recursive
943bc6
-		  query before failing.  The default and minimum
10d019
-		  is <literal>10</literal> and the maximum is
10d019
-		  <literal>30</literal>.  Setting it to
943bc6
-		  <literal>0</literal> results in the default
943bc6
-		  being used.
943bc6
+		  The amount of time in milliseconds that the resolver
943bc6
+ 		  will spend attempting to resolve a recursive
943bc6
+ 		  query before failing.  The default and minimum
10d019
+		  is <literal>10000</literal> and the maximum is
10d019
+		  <literal>30000</literal>.  Setting it to
943bc6
+ 		  <literal>0</literal> will result in the default
943bc6
+ 		  being used.
943bc6
+ 		</para>
10d019
+		<para>
10d019
+		  This value was originally specified in seconds.
10d019
+		  Values less than or equal to 300 will be be treated
10d019
+		  as seconds and converted to milliseconds before
10d019
+		  applying the above limits.
943bc6
 		</para>
10d019
 	      </listitem>
10d019
 	    </varlistentry>
943bc6
@@ -9016,6 +9056,27 @@ avoid-v6-udp-ports { 40000; range 50000 60000; };
10d019
 	      </listitem>
10d019
 	    </varlistentry>
10d019
 
10d019
+	    <varlistentry>
10d019
+	      <term><command>max-stale-ttl</command></term>
10d019
+	      <listitem>
10d019
+		<para>
10d019
+		  Sets the maximum time for which the server will
10d019
+		  retain records past their normal expiry to
10d019
+		  return them as stale records when the servers
10d019
+		  for those records are not reachable.  The default
10d019
+		  is to not retain the record.
10d019
+		</para>
10d019
+		<para>
10d019
+		  <command>rndc serve-stale</command> can be used
10d019
+		  to disable and re-enable the serving of stale
10d019
+		  records at runtime.  Reloading or reconfiguring
10d019
+		  <command>named</command> will not re-enable serving
10d019
+		  of stale records if they have been disabled via
10d019
+		  <command>rndc</command>.
10d019
+		</para>
10d019
+	      </listitem>
10d019
+	    </varlistentry>
10d019
+
10d019
 	    <varlistentry>
10d019
 	      <term><command>min-roots</command></term>
10d019
 	      <listitem>
10d019
diff --git a/doc/arm/logging-categories.xml b/doc/arm/logging-categories.xml
943bc6
index 56d05e8..098342b 100644
10d019
--- a/doc/arm/logging-categories.xml
10d019
+++ b/doc/arm/logging-categories.xml
10d019
@@ -311,6 +311,17 @@
10d019
 	  </para>
10d019
 	</entry>
10d019
       </row>
10d019
+      <row rowsep="0">
10d019
+	<entry colname="1">
10d019
+	  <para><command>serve-stale</command></para>
10d019
+	</entry>
10d019
+	<entry colname="2">
10d019
+	  <para>
10d019
+	    Whether or not a stale answer is used
10d019
+	    following a resolver failure.
10d019
+	  </para>
10d019
+	</entry>
10d019
+      </row>
10d019
       <row rowsep="0">
10d019
 	<entry colname="1">
10d019
 	  <para><command>spill</command></para>
10d019
diff --git a/doc/arm/notes-rh-changes.xml b/doc/arm/notes-rh-changes.xml
230545
index 89a4961..80b7dee 100644
10d019
--- a/doc/arm/notes-rh-changes.xml
10d019
+++ b/doc/arm/notes-rh-changes.xml
230545
@@ -12,6 +12,9 @@
10d019
 <section xml:id="relnotes_rh_changes"><info><title>Red Hat Specific Changes</title></info>
10d019
   <itemizedlist>
10d019
      <listitem>
10d019
+      <para>
10d019
+        This version includes some features not present in releases by ISC.
10d019
+      </para>
10d019
       <para>
10d019
         By default, BIND now uses the random number generation functions
10d019
         in the cryptographic library (i.e., OpenSSL or a PKCS#11
230545
@@ -36,7 +39,16 @@
10d019
         case <filename>/dev/random</filename> will be the default
10d019
         entropy source.  [RT #31459] [RT #46047]
10d019
       </para>
10d019
-    </listitem>
10d019
+      <para>
10d019
+        When acting as a recursive resolver, <command>named</command>
10d019
+        can now continue returning answers whose TTLs have expired
10d019
+        when the authoritative server is under attack and unable to
10d019
+        respond. This is controlled by the
10d019
+        <command>stale-answer-enable</command>,
10d019
+        <command>stale-answer-ttl</command> and
10d019
+        <command>max-stale-ttl</command> options. [RT #44790]
10d019
+      </para>
10d019
+     </listitem>
10d019
   </itemizedlist>
10d019
 </section>
10d019
 
10d019
diff --git a/doc/misc/options b/doc/misc/options
10d019
index e11beed..fde93c7 100644
10d019
--- a/doc/misc/options
10d019
+++ b/doc/misc/options
10d019
@@ -225,6 +225,7 @@ options {
10d019
         max-refresh-time <integer>;
10d019
         max-retry-time <integer>;
10d019
         max-rsa-exponent-size <integer>;
10d019
+        max-stale-ttl <ttlval>;
10d019
         max-transfer-idle-in <integer>;
10d019
         max-transfer-idle-out <integer>;
10d019
         max-transfer-time-in <integer>;
10d019
@@ -298,7 +299,9 @@ options {
10d019
         request-sit <boolean>; // obsolete
10d019
         require-server-cookie <boolean>;
10d019
         reserved-sockets <integer>;
10d019
+        resolver-nonbackoff-tries <integer>;
10d019
         resolver-query-timeout <integer>;
10d019
+        resolver-retry-interval <integer>;
10d019
         response-policy { zone <string> [ log <boolean> ] [ max-policy-ttl
10d019
             <integer> ] [ policy ( cname | disabled | drop | given | no-op
10d019
             | nodata | nxdomain | passthru | tcp-only <quoted_string> ) ] [
10d019
@@ -328,6 +331,8 @@ options {
10d019
         sit-secret <string>; // obsolete
10d019
         sortlist { <address_match_element>; ... };
10d019
         stacksize ( default | unlimited | <sizeval> );
10d019
+        stale-answer-enable <boolean>;
10d019
+        stale-answer-ttl <ttlval>;
10d019
         startup-notify-rate <integer>;
10d019
         statistics-file <quoted_string>;
10d019
         statistics-interval <integer>; // not yet implemented
10d019
@@ -539,6 +544,7 @@ view <string> [ <class> ] {
10d019
         max-recursion-queries <integer>;
10d019
         max-refresh-time <integer>;
10d019
         max-retry-time <integer>;
10d019
+        max-stale-ttl <ttlval>;
10d019
         max-transfer-idle-in <integer>;
10d019
         max-transfer-idle-out <integer>;
10d019
         max-transfer-time-in <integer>;
10d019
@@ -600,7 +606,9 @@ view <string> [ <class> ] {
10d019
         request-nsid <boolean>;
10d019
         request-sit <boolean>; // obsolete
10d019
         require-server-cookie <boolean>;
10d019
+        resolver-nonbackoff-tries <integer>;
10d019
         resolver-query-timeout <integer>;
10d019
+        resolver-retry-interval <integer>;
10d019
         response-policy { zone <string> [ log <boolean> ] [ max-policy-ttl
10d019
             <integer> ] [ policy ( cname | disabled | drop | given | no-op
10d019
             | nodata | nxdomain | passthru | tcp-only <quoted_string> ) ] [
10d019
@@ -655,6 +663,8 @@ view <string> [ <class> ] {
10d019
         sig-signing-type <integer>;
10d019
         sig-validity-interval <integer> [ <integer> ];
10d019
         sortlist { <address_match_element>; ... };
10d019
+        stale-answer-enable <boolean>;
10d019
+        stale-answer-ttl <ttlval>;
10d019
         suppress-initial-notify <boolean>; // not yet implemented
10d019
         topology { <address_match_element>; ... }; // not implemented
10d019
         transfer-format ( many-answers | one-answer );
10d019
diff --git a/lib/bind9/check.c b/lib/bind9/check.c
943bc6
index bf769fe..6c57fa4 100644
10d019
--- a/lib/bind9/check.c
10d019
+++ b/lib/bind9/check.c
10d019
@@ -99,7 +99,8 @@ check_orderent(const cfg_obj_t *ent, isc_log_t *logctx) {
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "rrset-order: invalid class '%s'",
10d019
 				    r.base);
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_FAILURE;
10d019
 		}
10d019
 	}
10d019
 
10d019
@@ -112,7 +113,8 @@ check_orderent(const cfg_obj_t *ent, isc_log_t *logctx) {
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "rrset-order: invalid type '%s'",
10d019
 				    r.base);
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_FAILURE;
10d019
 		}
10d019
 	}
10d019
 
10d019
@@ -126,7 +128,8 @@ check_orderent(const cfg_obj_t *ent, isc_log_t *logctx) {
10d019
 		if (tresult != ISC_R_SUCCESS) {
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "rrset-order: invalid name '%s'", str);
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_FAILURE;
10d019
 		}
10d019
 	}
10d019
 
10d019
@@ -135,14 +138,16 @@ check_orderent(const cfg_obj_t *ent, isc_log_t *logctx) {
10d019
 	    strcasecmp("order", cfg_obj_asstring(obj)) != 0) {
10d019
 		cfg_obj_log(ent, logctx, ISC_LOG_ERROR,
10d019
 			    "rrset-order: keyword 'order' missing");
10d019
-		result = ISC_R_FAILURE;
10d019
+		if (result == ISC_R_SUCCESS)
10d019
+			result = ISC_R_FAILURE;
10d019
 	}
10d019
 
10d019
 	obj = cfg_tuple_get(ent, "ordering");
10d019
 	if (!cfg_obj_isstring(obj)) {
10d019
 	    cfg_obj_log(ent, logctx, ISC_LOG_ERROR,
10d019
 			"rrset-order: missing ordering");
10d019
-		result = ISC_R_FAILURE;
10d019
+		if (result == ISC_R_SUCCESS)
10d019
+			result = ISC_R_FAILURE;
10d019
 	} else if (strcasecmp(cfg_obj_asstring(obj), "fixed") == 0) {
10d019
 #if !DNS_RDATASET_FIXED
10d019
 		cfg_obj_log(obj, logctx, ISC_LOG_WARNING,
10d019
@@ -154,7 +159,8 @@ check_orderent(const cfg_obj_t *ent, isc_log_t *logctx) {
10d019
 		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 			    "rrset-order: invalid order '%s'",
10d019
 			    cfg_obj_asstring(obj));
10d019
-		result = ISC_R_FAILURE;
10d019
+		if (result == ISC_R_SUCCESS)
10d019
+			result = ISC_R_FAILURE;
10d019
 	}
10d019
 	return (result);
10d019
 }
10d019
@@ -174,7 +180,7 @@ check_order(const cfg_obj_t *options, isc_log_t *logctx) {
10d019
 	     element = cfg_list_next(element))
10d019
 	{
10d019
 		tresult = check_orderent(cfg_listelt_value(element), logctx);
10d019
-		if (tresult != ISC_R_SUCCESS)
10d019
+		if (result == ISC_R_SUCCESS && tresult != ISC_R_SUCCESS)
10d019
 			result = tresult;
10d019
 	}
10d019
 	return (result);
10d019
@@ -204,7 +210,8 @@ check_dual_stack(const cfg_obj_t *options, isc_log_t *logctx) {
10d019
 		if (val > UINT16_MAX) {
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "port '%u' out of range", val);
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		}
10d019
 	}
10d019
 	obj = cfg_tuple_get(alternates, "addresses");
10d019
@@ -224,7 +231,8 @@ check_dual_stack(const cfg_obj_t *options, isc_log_t *logctx) {
10d019
 		if (tresult != ISC_R_SUCCESS) {
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "bad name '%s'", str);
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = tresult;
10d019
 		}
10d019
 		obj = cfg_tuple_get(value, "port");
10d019
 		if (cfg_obj_isuint32(obj)) {
10d019
@@ -232,7 +240,8 @@ check_dual_stack(const cfg_obj_t *options, isc_log_t *logctx) {
10d019
 			if (val > UINT16_MAX) {
10d019
 				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 					    "port '%u' out of range", val);
10d019
-				result = ISC_R_FAILURE;
10d019
+				if (result == ISC_R_SUCCESS)
10d019
+					result = ISC_R_RANGE;
10d019
 			}
10d019
 		}
10d019
 	}
230545
@@ -1271,7 +1280,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "auto-dnssec may only be activated at the "
10d019
 				    "zone level");
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_FAILURE;
10d019
 		}
10d019
 	}
10d019
 
230545
@@ -1291,7 +1301,7 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 		{
10d019
 			obj = cfg_listelt_value(element);
10d019
 			tresult = mustbesecure(obj, symtab, logctx, mctx);
10d019
-			if (tresult != ISC_R_SUCCESS)
10d019
+			if (result == ISC_R_SUCCESS && tresult != ISC_R_SUCCESS)
10d019
 				result = tresult;
10d019
 		}
10d019
 		if (symtab != NULL)
230545
@@ -1310,7 +1320,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 					    "%s: invalid name '%s'",
10d019
 					    server_contact[i], str);
10d019
-				result = ISC_R_FAILURE;
10d019
+				if (result == ISC_R_SUCCESS)
10d019
+					result = ISC_R_FAILURE;
10d019
 			}
10d019
 		}
10d019
 	}
230545
@@ -1330,7 +1341,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "disable-empty-zone: invalid name '%s'",
10d019
 				    str);
10d019
-			result = ISC_R_FAILURE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_FAILURE;
10d019
 		}
10d019
 	}
10d019
 
230545
@@ -1344,11 +1356,12 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 	    strlen(cfg_obj_asstring(obj)) > 1024U) {
10d019
 		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 			    "'server-id' too big (>1024 bytes)");
10d019
-		result = ISC_R_FAILURE;
10d019
+		if (result == ISC_R_SUCCESS)
10d019
+			result = ISC_R_FAILURE;
10d019
 	}
10d019
 
10d019
 	tresult = check_dscp(options, logctx);
10d019
-	if (tresult != ISC_R_SUCCESS)
10d019
+	if (result == ISC_R_SUCCESS && tresult != ISC_R_SUCCESS)
10d019
 		result = tresult;
10d019
 
10d019
 	obj = NULL;
230545
@@ -1358,11 +1371,13 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 		if (lifetime > 604800) {	/* 7 days */
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "'nta-lifetime' cannot exceed one week");
10d019
-			result = ISC_R_RANGE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		} else if (lifetime == 0) {
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "'nta-lifetime' may not be zero");
10d019
-			result = ISC_R_RANGE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		}
10d019
 	}
10d019
 
230545
@@ -1373,7 +1388,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 		if (recheck > 604800) {		/* 7 days */
10d019
 			cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 				    "'nta-recheck' cannot exceed one week");
10d019
-			result = ISC_R_RANGE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		}
10d019
 
10d019
 		if (recheck > lifetime)
230545
@@ -1391,7 +1407,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 	if (strcasecmp(ccalg, "aes") == 0) {
10d019
 		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 			    "cookie-algorithm: '%s' not supported", ccalg);
10d019
-		result = ISC_R_NOTIMPLEMENTED;
10d019
+		if (result == ISC_R_SUCCESS)
10d019
+			result = ISC_R_NOTIMPLEMENTED;
10d019
 	}
10d019
 #endif
10d019
 
230545
@@ -1480,7 +1497,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 				cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
 					    "%s out of range (%u < %u)",
10d019
 					    fstrm[i].name, value, fstrm[i].min);
10d019
-			result = ISC_R_RANGE;
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		}
10d019
 
10d019
 		if (strcmp(fstrm[i].name, "fstrm-set-input-queue-size") == 0) {
230545
@@ -1494,7 +1512,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 					    "%s '%u' not a power-of-2",
10d019
 					    fstrm[i].name,
10d019
 					    cfg_obj_asuint32(obj));
10d019
-				result = ISC_R_RANGE;
10d019
+				if (result == ISC_R_SUCCESS)
10d019
+					result = ISC_R_RANGE;
10d019
 			}
10d019
 		}
10d019
 	}
230545
@@ -1512,7 +1531,8 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 				    "%" PRId64 "' "
10d019
 				    "is too small",
10d019
 				    mapsize);
10d019
-			return (ISC_R_RANGE);
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		} else if (mapsize > (1ULL << 40)) { /* 1 terabyte */
10d019
 			cfg_obj_log(obj, logctx,
10d019
 				    ISC_LOG_ERROR,
230545
@@ -1520,10 +1540,20 @@ check_options(const cfg_obj_t *options, isc_log_t *logctx, isc_mem_t *mctx,
10d019
 				    "%" PRId64 "' "
10d019
 				    "is too large",
10d019
 				    mapsize);
10d019
-			return (ISC_R_RANGE);
10d019
+			if (result == ISC_R_SUCCESS)
10d019
+				result = ISC_R_RANGE;
10d019
 		}
10d019
 	}
10d019
 
10d019
+	obj = NULL;
10d019
+	(void)cfg_map_get(options, "resolver-nonbackoff-tries", &obj);
10d019
+	if (obj != NULL && cfg_obj_asuint32(obj) == 0U) {
10d019
+		cfg_obj_log(obj, logctx, ISC_LOG_ERROR,
10d019
+			    "'resolver-nonbackoff-tries' must be >= 1");
10d019
+		if (result == ISC_R_SUCCESS)
10d019
+			result = ISC_R_RANGE;
10d019
+	}
10d019
+
10d019
 	return (result);
10d019
 }
10d019
 
10d019
diff --git a/lib/dns/cache.c b/lib/dns/cache.c
943bc6
index 2965a4f..617737a 100644
10d019
--- a/lib/dns/cache.c
10d019
+++ b/lib/dns/cache.c
10d019
@@ -138,6 +138,7 @@ struct dns_cache {
10d019
 	int			db_argc;
10d019
 	char			**db_argv;
10d019
 	size_t			size;
10d019
+	dns_ttl_t		serve_stale_ttl;
10d019
 	isc_stats_t		*stats;
10d019
 
10d019
 	/* Locked by 'filelock'. */
10d019
@@ -167,9 +168,13 @@ overmem_cleaning_action(isc_task_t *task, isc_event_t *event);
10d019
 
10d019
 static inline isc_result_t
10d019
 cache_create_db(dns_cache_t *cache, dns_db_t **db) {
10d019
-	return (dns_db_create(cache->mctx, cache->db_type, dns_rootname,
10d019
-			      dns_dbtype_cache, cache->rdclass,
10d019
-			      cache->db_argc, cache->db_argv, db));
10d019
+	isc_result_t result;
10d019
+	result = dns_db_create(cache->mctx, cache->db_type, dns_rootname,
10d019
+			       dns_dbtype_cache, cache->rdclass,
10d019
+			       cache->db_argc, cache->db_argv, db);
10d019
+	if (result == ISC_R_SUCCESS)
10d019
+		dns_db_setservestalettl(*db, cache->serve_stale_ttl);
10d019
+	return (result);
10d019
 }
10d019
 
10d019
 isc_result_t
10d019
@@ -238,6 +243,7 @@ dns_cache_create3(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr,
10d019
 	cache->references = 1;
10d019
 	cache->live_tasks = 0;
10d019
 	cache->rdclass = rdclass;
10d019
+	cache->serve_stale_ttl = 0;
10d019
 
10d019
 	cache->stats = NULL;
10d019
 	result = isc_stats_create(cmctx, &cache->stats,
10d019
@@ -1092,6 +1098,32 @@ dns_cache_getcachesize(dns_cache_t *cache) {
10d019
 	return (size);
10d019
 }
10d019
 
10d019
+void
10d019
+dns_cache_setservestalettl(dns_cache_t *cache, dns_ttl_t ttl) {
10d019
+	REQUIRE(VALID_CACHE(cache));
10d019
+
10d019
+	LOCK(&cache->lock);
10d019
+	cache->serve_stale_ttl = ttl;
10d019
+	UNLOCK(&cache->lock);
10d019
+
10d019
+	(void)dns_db_setservestalettl(cache->db, ttl);
10d019
+}
10d019
+
10d019
+dns_ttl_t
10d019
+dns_cache_getservestalettl(dns_cache_t *cache) {
10d019
+	dns_ttl_t ttl;
10d019
+	isc_result_t result;
10d019
+
10d019
+	REQUIRE(VALID_CACHE(cache));
10d019
+
10d019
+	/*
10d019
+	 * Could get it straight from the dns_cache_t, but use db
10d019
+	 * to confirm the value that the db is really using.
10d019
+	 */
10d019
+	result = dns_db_getservestalettl(cache->db, &ttl);
10d019
+	return result == ISC_R_SUCCESS ? ttl : 0;
10d019
+}
10d019
+
10d019
 /*
10d019
  * The cleaner task is shutting down; do the necessary cleanup.
10d019
  */
10d019
diff --git a/lib/dns/db.c b/lib/dns/db.c
943bc6
index a28a566..c581646 100644
10d019
--- a/lib/dns/db.c
10d019
+++ b/lib/dns/db.c
10d019
@@ -1130,3 +1130,25 @@ dns_db_nodefullname(dns_db_t *db, dns_dbnode_t *node, dns_name_t *name) {
10d019
 		return (ISC_R_NOTIMPLEMENTED);
10d019
 	return ((db->methods->nodefullname)(db, node, name));
10d019
 }
10d019
+
10d019
+isc_result_t
10d019
+dns_db_setservestalettl(dns_db_t *db, dns_ttl_t ttl)
10d019
+{
10d019
+	REQUIRE(DNS_DB_VALID(db));
10d019
+	REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
10d019
+
10d019
+	if (db->methods->setservestalettl != NULL)
10d019
+		return ((db->methods->setservestalettl)(db, ttl));
10d019
+	return (ISC_R_NOTIMPLEMENTED);
10d019
+}
10d019
+
10d019
+isc_result_t
10d019
+dns_db_getservestalettl(dns_db_t *db, dns_ttl_t *ttl)
10d019
+{
10d019
+	REQUIRE(DNS_DB_VALID(db));
10d019
+	REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
10d019
+
10d019
+	if (db->methods->getservestalettl != NULL)
10d019
+		return ((db->methods->getservestalettl)(db, ttl));
10d019
+	return (ISC_R_NOTIMPLEMENTED);
10d019
+}
10d019
diff --git a/lib/dns/ecdb.c b/lib/dns/ecdb.c
943bc6
index fc94ccf..76d0417 100644
10d019
--- a/lib/dns/ecdb.c
10d019
+++ b/lib/dns/ecdb.c
10d019
@@ -588,7 +588,9 @@ static dns_dbmethods_t ecdb_methods = {
10d019
 	NULL,			/* setcachestats */
10d019
 	NULL,			/* hashsize */
10d019
 	NULL,			/* nodefullname */
10d019
-	NULL			/* getsize */
10d019
+	NULL,			/* getsize */
10d019
+	NULL,			/* setservestalettl */
10d019
+	NULL			/* getservestalettl */
10d019
 };
10d019
 
10d019
 static isc_result_t
10d019
diff --git a/lib/dns/include/dns/cache.h b/lib/dns/include/dns/cache.h
943bc6
index ab4b0b5..e158014 100644
10d019
--- a/lib/dns/include/dns/cache.h
10d019
+++ b/lib/dns/include/dns/cache.h
10d019
@@ -260,6 +260,27 @@ dns_cache_getcachesize(dns_cache_t *cache);
10d019
  * Get the maximum cache size.
10d019
  */
10d019
 
10d019
+void
10d019
+dns_cache_setservestalettl(dns_cache_t *cache, dns_ttl_t ttl);
10d019
+/*%<
10d019
+ * Sets the maximum length of time that cached answers may be retained
10d019
+ * past their normal TTL.  Default value for the library is 0, disabling
10d019
+ * the use of stale data.
10d019
+ *
10d019
+ * Requires:
10d019
+ *\li	'cache' to be valid.
10d019
+ */
10d019
+
10d019
+dns_ttl_t
10d019
+dns_cache_getservestalettl(dns_cache_t *cache);
10d019
+/*%<
10d019
+ * Gets the maximum length of time that cached answers may be kept past
10d019
+ * normal expiry.
10d019
+ *
10d019
+ * Requires:
10d019
+ *\li	'cache' to be valid.
10d019
+ */
10d019
+
10d019
 isc_result_t
10d019
 dns_cache_flush(dns_cache_t *cache);
10d019
 /*%<
10d019
diff --git a/lib/dns/include/dns/db.h b/lib/dns/include/dns/db.h
943bc6
index 96f3a8f..452770f 100644
10d019
--- a/lib/dns/include/dns/db.h
10d019
+++ b/lib/dns/include/dns/db.h
230545
@@ -195,6 +195,8 @@ typedef struct dns_dbmethods {
10d019
 					dns_name_t *name);
10d019
 	isc_result_t	(*getsize)(dns_db_t *db, dns_dbversion_t *version,
10d019
 				   uint64_t *records, uint64_t *bytes);
10d019
+	isc_result_t	(*setservestalettl)(dns_db_t *db, dns_ttl_t ttl);
10d019
+	isc_result_t	(*getservestalettl)(dns_db_t *db, dns_ttl_t *ttl);
10d019
 } dns_dbmethods_t;
10d019
 
10d019
 typedef isc_result_t
230545
@@ -253,6 +255,7 @@ struct dns_dbonupdatelistener {
10d019
 #define DNS_DBFIND_FORCENSEC3		0x0080
10d019
 #define DNS_DBFIND_ADDITIONALOK		0x0100
10d019
 #define DNS_DBFIND_NOZONECUT		0x0200
10d019
+#define DNS_DBFIND_STALEOK		0x0400
10d019
 /*@}*/
10d019
 
10d019
 /*@{*/
230545
@@ -1683,6 +1686,38 @@ dns_db_nodefullname(dns_db_t *db, dns_dbnode_t *node, dns_name_t *name);
10d019
  * \li	'db' is a valid database
10d019
  * \li	'node' and 'name' are not NULL
10d019
  */
10d019
+
10d019
+isc_result_t
10d019
+dns_db_setservestalettl(dns_db_t *db, dns_ttl_t ttl);
10d019
+/*%<
10d019
+ * Sets the maximum length of time that cached answers may be retained
10d019
+ * past their normal TTL. Default value for the library is 0, disabling
10d019
+ * the use of stale data.
10d019
+ *
10d019
+ * Requires:
10d019
+ * \li	'db' is a valid cache database.
10d019
+ * \li	'ttl' is the number of seconds to retain data past its normal expiry.
10d019
+ *
10d019
+ * Returns:
10d019
+ * \li	#ISC_R_SUCCESS
10d019
+ * \li	#ISC_R_NOTIMPLEMENTED - Not supported by this DB implementation.
10d019
+ */
10d019
+
10d019
+isc_result_t
10d019
+dns_db_getservestalettl(dns_db_t *db, dns_ttl_t *ttl);
10d019
+/*%<
10d019
+ * Gets maximum length of time that cached answers may be kept past
10d019
+ * normal TTL expiration.
10d019
+ *
10d019
+ * Requires:
10d019
+ * \li	'db' is a valid cache database.
10d019
+ * \li	'ttl' is the number of seconds to retain data past its normal expiry.
10d019
+ *
10d019
+ * Returns:
10d019
+ * \li	#ISC_R_SUCCESS
10d019
+ * \li	#ISC_R_NOTIMPLEMENTED - Not supported by this DB implementation.
10d019
+ */
10d019
+
10d019
 ISC_LANG_ENDDECLS
10d019
 
10d019
 #endif /* DNS_DB_H */
10d019
diff --git a/lib/dns/include/dns/rdataset.h b/lib/dns/include/dns/rdataset.h
943bc6
index ed9119a..710e97c 100644
10d019
--- a/lib/dns/include/dns/rdataset.h
10d019
+++ b/lib/dns/include/dns/rdataset.h
10d019
@@ -128,6 +128,7 @@ struct dns_rdataset {
10d019
 	unsigned int			magic;		/* XXX ? */
10d019
 	dns_rdatasetmethods_t *		methods;
10d019
 	ISC_LINK(dns_rdataset_t)	link;
10d019
+
10d019
 	/*
10d019
 	 * XXX do we need these, or should they be retrieved by methods?
10d019
 	 * Leaning towards the latter, since they are not frequently required
10d019
@@ -136,12 +137,19 @@ struct dns_rdataset {
10d019
 	dns_rdataclass_t		rdclass;
10d019
 	dns_rdatatype_t			type;
10d019
 	dns_ttl_t			ttl;
10d019
+	/*
10d019
+	 * Stale ttl is used to see how long this RRset can still be used
10d019
+	 * to serve to clients, after the TTL has expired.
10d019
+	 */
10d019
+	dns_ttl_t			stale_ttl;
10d019
 	dns_trust_t			trust;
10d019
 	dns_rdatatype_t			covers;
10d019
+
10d019
 	/*
10d019
 	 * attributes
10d019
 	 */
10d019
 	unsigned int			attributes;
10d019
+
10d019
 	/*%
10d019
 	 * the counter provides the starting point in the "cyclic" order.
10d019
 	 * The value UINT32_MAX has a special meaning of "picking up a
10d019
@@ -149,11 +157,13 @@ struct dns_rdataset {
10d019
 	 * increment the counter.
10d019
 	 */
10d019
 	uint32_t			count;
10d019
+
10d019
 	/*
10d019
 	 * This RRSIG RRset should be re-generated around this time.
10d019
 	 * Only valid if DNS_RDATASETATTR_RESIGN is set in attributes.
10d019
 	 */
10d019
 	isc_stdtime_t			resign;
10d019
+
10d019
 	/*@{*/
10d019
 	/*%
10d019
 	 * These are for use by the rdataset implementation, and MUST NOT
10d019
@@ -206,6 +216,7 @@ struct dns_rdataset {
10d019
 #define DNS_RDATASETATTR_OPTOUT		0x00100000	/*%< OPTOUT proof */
10d019
 #define DNS_RDATASETATTR_NEGATIVE	0x00200000
10d019
 #define DNS_RDATASETATTR_PREFETCH	0x00400000
10d019
+#define DNS_RDATASETATTR_STALE		0x01000000
10d019
 
10d019
 /*%
10d019
  * _OMITDNSSEC:
10d019
diff --git a/lib/dns/include/dns/resolver.h b/lib/dns/include/dns/resolver.h
943bc6
index 7b3c047..bd7d225 100644
10d019
--- a/lib/dns/include/dns/resolver.h
10d019
+++ b/lib/dns/include/dns/resolver.h
10d019
@@ -547,9 +547,12 @@ dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name);
10d019
 
10d019
 
10d019
 void
10d019
-dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds);
10d019
+dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int timeout);
10d019
 /*%<
10d019
- * Set the length of time the resolver will work on a query, in seconds.
10d019
+ * Set the length of time the resolver will work on a query, in milliseconds.
10d019
+ *
10d019
+ * 'timeout' was originally defined in seconds, and later redefined to be in
10d019
+ * milliseconds.  Values less than or equal to 300 are treated as seconds.
10d019
  *
10d019
  * If timeout is 0, the default timeout will be applied.
10d019
  *
10d019
@@ -560,7 +563,8 @@ dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds);
10d019
 unsigned int
10d019
 dns_resolver_gettimeout(dns_resolver_t *resolver);
10d019
 /*%<
10d019
- * Get the current length of time the resolver will work on a query, in seconds.
10d019
+ * Get the current length of time the resolver will work on a query,
10d019
+ * in milliseconds.
10d019
  *
10d019
  * Requires:
10d019
  * \li  resolver to be valid.
10d019
@@ -582,6 +586,39 @@ dns_resolver_getzeronosoattl(dns_resolver_t *resolver);
10d019
 void
10d019
 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, bool state);
10d019
 
10d019
+unsigned int
10d019
+dns_resolver_getretryinterval(dns_resolver_t *resolver);
10d019
+
10d019
+void
10d019
+dns_resolver_setretryinterval(dns_resolver_t *resolver, unsigned int interval);
10d019
+/*%<
10d019
+ * Sets the amount of time, in millseconds, that is waited for a reply
10d019
+ * to a server before another server is tried.  Interacts with the
10d019
+ * value of dns_resolver_getnonbackofftries() by trying that number of times
10d019
+ * at this interval, before doing exponential backoff and doubling the interval
10d019
+ * on each subsequent try, to a maximum of 10 seconds.  Defaults to 800 ms;
10d019
+ * silently capped at 2000 ms.
10d019
+ *
10d019
+ * Requires:
10d019
+ * \li	resolver to be valid.
10d019
+ * \li  interval > 0.
10d019
+ */
10d019
+
10d019
+unsigned int
10d019
+dns_resolver_getnonbackofftries(dns_resolver_t *resolver);
10d019
+
10d019
+void
10d019
+dns_resolver_setnonbackofftries(dns_resolver_t *resolver, unsigned int tries);
10d019
+/*%<
10d019
+ * Sets the number of failures of getting a reply from remote servers for
10d019
+ * a query before backing off by doubling the retry interval for each
10d019
+ * subsequent request sent.  Defaults to 3.
10d019
+ *
10d019
+ * Requires:
10d019
+ * \li	resolver to be valid.
10d019
+ * \li  tries > 0.
10d019
+ */
10d019
+
10d019
 unsigned int
10d019
 dns_resolver_getoptions(dns_resolver_t *resolver);
10d019
 
10d019
diff --git a/lib/dns/include/dns/types.h b/lib/dns/include/dns/types.h
943bc6
index 2468e3c..934a641 100644
10d019
--- a/lib/dns/include/dns/types.h
10d019
+++ b/lib/dns/include/dns/types.h
943bc6
@@ -390,6 +390,12 @@ typedef struct {
943bc6
 	size_t      count;
943bc6
 } dns_indent_t;
10d019
 
10d019
+typedef enum {
10d019
+	dns_stale_answer_no,
10d019
+	dns_stale_answer_yes,
10d019
+	dns_stale_answer_conf
10d019
+} dns_stale_answer_t;
10d019
+
10d019
 /*
10d019
  * Functions.
10d019
  */
10d019
diff --git a/lib/dns/include/dns/view.h b/lib/dns/include/dns/view.h
943bc6
index 53f1db1..96148c7 100644
10d019
--- a/lib/dns/include/dns/view.h
10d019
+++ b/lib/dns/include/dns/view.h
10d019
@@ -229,6 +229,9 @@ struct dns_view {
10d019
 	dns_dtenv_t			*dtenv;		/* Dnstap environment */
10d019
 	dns_dtmsgtype_t			dttypes;	/* Dnstap message types
10d019
 							   to log */
10d019
+	dns_ttl_t			staleanswerttl;
10d019
+	dns_stale_answer_t		staleanswersok;		/* rndc setting */
10d019
+	bool				staleanswersenable;	/* named.conf setting */
10d019
 };
10d019
 
10d019
 #define DNS_VIEW_MAGIC			ISC_MAGIC('V','i','e','w')
10d019
diff --git a/lib/dns/master.c b/lib/dns/master.c
943bc6
index 7d26b81..36999b5 100644
10d019
--- a/lib/dns/master.c
10d019
+++ b/lib/dns/master.c
10d019
@@ -1948,12 +1948,18 @@ load_text(dns_loadctx_t *lctx) {
10d019
 
10d019
 		if ((lctx->options & DNS_MASTER_AGETTL) != 0) {
10d019
 			/*
10d019
-			 * Adjust the TTL for $DATE.  If the RR has already
10d019
-			 * expired, ignore it.
10d019
+			 * Adjust the TTL for $DATE. If the RR has
10d019
+			 * already expired, set its TTL to 0. This
10d019
+			 * should be okay even if the TTL stretching
10d019
+			 * feature is not in effect, because it will
10d019
+			 * just be quickly expired by the cache, and the
10d019
+			 * way this was written before the patch it
10d019
+			 * could potentially add 0 TTLs anyway.
10d019
 			 */
10d019
 			if (lctx->ttl < ttl_offset)
10d019
-				continue;
10d019
-			lctx->ttl -= ttl_offset;
10d019
+				lctx->ttl = 0;
10d019
+			else
10d019
+				lctx->ttl -= ttl_offset;
10d019
 		}
10d019
 
10d019
 		/*
10d019
diff --git a/lib/dns/masterdump.c b/lib/dns/masterdump.c
943bc6
index fa839a0..91b3cab 100644
10d019
--- a/lib/dns/masterdump.c
10d019
+++ b/lib/dns/masterdump.c
10d019
@@ -81,6 +81,9 @@ struct dns_master_style {
10d019
  */
10d019
 #define DNS_TOTEXT_LINEBREAK_MAXLEN 100
10d019
 
10d019
+/*% Does the rdataset 'r' contain a stale answer? */
10d019
+#define STALE(r) (((r)->attributes & DNS_RDATASETATTR_STALE) != 0)
10d019
+
10d019
 /*%
10d019
  * Context structure for a masterfile dump in progress.
10d019
  */
10d019
@@ -94,6 +97,7 @@ typedef struct dns_totext_ctx {
10d019
 	dns_fixedname_t		origin_fixname;
10d019
 	uint32_t 		current_ttl;
10d019
 	bool 			current_ttl_valid;
10d019
+	dns_ttl_t		serve_stale_ttl;
10d019
 } dns_totext_ctx_t;
10d019
 
10d019
 LIBDNS_EXTERNAL_DATA const dns_master_style_t
10d019
@@ -382,6 +386,7 @@ totext_ctx_init(const dns_master_style_t *style, dns_totext_ctx_t *ctx) {
10d019
 	ctx->neworigin = NULL;
10d019
 	ctx->current_ttl = 0;
10d019
 	ctx->current_ttl_valid = false;
10d019
+	ctx->serve_stale_ttl = 0;
10d019
 
10d019
 	return (ISC_R_SUCCESS);
10d019
 }
10d019
@@ -1028,6 +1033,11 @@ dump_rdatasets_text(isc_mem_t *mctx, dns_name_t *name,
10d019
 		    (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
10d019
 			/* Omit negative cache entries */
10d019
 		} else {
10d019
+			if (STALE(rds)) {
10d019
+				fprintf(f, "; stale (for %u more seconds)\n",
10d019
+					(rds->stale_ttl -
10d019
+					 ctx->serve_stale_ttl));
10d019
+			}
10d019
 			isc_result_t result =
10d019
 				dump_rdataset(mctx, name, rds, ctx,
10d019
 					       buffer, f);
10d019
@@ -1496,6 +1506,16 @@ dumpctx_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
10d019
 	dns_db_attach(db, &dctx->db);
10d019
 
10d019
 	dctx->do_date = dns_db_iscache(dctx->db);
10d019
+	if (dctx->do_date) {
10d019
+		/*
10d019
+		 * Adjust the date backwards by the serve-stale TTL, if any.
10d019
+		 * This is so the TTL will be loaded correctly when next
10d019
+		 * started.
10d019
+		 */
10d019
+		(void)dns_db_getservestalettl(dctx->db,
10d019
+					      &dctx->tctx.serve_stale_ttl);
10d019
+		dctx->now -= dctx->tctx.serve_stale_ttl;
10d019
+	}
10d019
 
10d019
 	if (dctx->format == dns_masterformat_text &&
10d019
 	    (dctx->tctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) {
10d019
@@ -1555,6 +1575,9 @@ writeheader(dns_dumpctx_t *dctx) {
10d019
 		 * it in the zone case.
10d019
 		 */
10d019
 		if (dctx->do_date) {
10d019
+			fprintf(dctx->f,
10d019
+				"; using a %d second stale ttl\n",
10d019
+				dctx->tctx.serve_stale_ttl);
10d019
 			result = dns_time32_totext(dctx->now, &buffer);
10d019
 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
10d019
 			isc_buffer_usedregion(&buffer, &r);
10d019
diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c
943bc6
index 3a60bcf..8ea4d47 100644
10d019
--- a/lib/dns/rbtdb.c
10d019
+++ b/lib/dns/rbtdb.c
943bc6
@@ -511,6 +511,7 @@ typedef ISC_LIST(rdatasetheader_t)      rdatasetheaderlist_t;
10d019
 typedef ISC_LIST(dns_rbtnode_t)         rbtnodelist_t;
10d019
 
10d019
 #define RDATASET_ATTR_NONEXISTENT       0x0001
10d019
+/*%< May be potentially served as stale data. */
10d019
 #define RDATASET_ATTR_STALE             0x0002
10d019
 #define RDATASET_ATTR_IGNORE            0x0004
10d019
 #define RDATASET_ATTR_RETAIN            0x0008
943bc6
@@ -523,6 +524,8 @@ typedef ISC_LIST(dns_rbtnode_t)         rbtnodelist_t;
10d019
 #define RDATASET_ATTR_CASESET           0x0400
10d019
 #define RDATASET_ATTR_ZEROTTL           0x0800
10d019
 #define RDATASET_ATTR_CASEFULLYLOWER    0x1000
10d019
+/*%< Ancient - awaiting cleanup. */
10d019
+#define RDATASET_ATTR_ANCIENT           0x2000
10d019
 
10d019
 typedef struct acache_cbarg {
10d019
 	dns_rdatasetadditional_t        type;
943bc6
@@ -573,6 +576,8 @@ struct acachectl {
10d019
 	(((header)->attributes & RDATASET_ATTR_ZEROTTL) != 0)
10d019
 #define CASEFULLYLOWER(header) \
10d019
 	(((header)->attributes & RDATASET_ATTR_CASEFULLYLOWER) != 0)
10d019
+#define ANCIENT(header) \
10d019
+	(((header)->attributes & RDATASET_ATTR_ANCIENT) != 0)
10d019
 
10d019
 
10d019
 #define ACTIVE(header, now) \
943bc6
@@ -632,6 +637,12 @@ typedef enum {
10d019
 	expire_flush
10d019
 } expire_t;
10d019
 
10d019
+typedef enum {
10d019
+	rdataset_ttl_fresh,
10d019
+	rdataset_ttl_stale,
10d019
+	rdataset_ttl_ancient
10d019
+} rdataset_ttl_t;
10d019
+
10d019
 typedef struct rbtdb_version {
10d019
 	/* Not locked */
10d019
 	rbtdb_serial_t                  serial;
943bc6
@@ -699,6 +710,12 @@ struct dns_rbtdb {
10d019
 	dns_dbnode_t                    *soanode;
10d019
 	dns_dbnode_t                    *nsnode;
10d019
 
10d019
+	/*
10d019
+	 * Maximum length of time to keep using a stale answer past its
10d019
+	 * normal TTL expiry.
10d019
+	*/
10d019
+	dns_ttl_t			serve_stale_ttl;
10d019
+
10d019
 	/*
10d019
 	 * This is a linked list used to implement the LRU cache.  There will
10d019
 	 * be node_lock_count linked lists here.  Nodes in bucket 1 will be
943bc6
@@ -742,6 +759,8 @@ struct dns_rbtdb {
10d019
 #define RBTDB_ATTR_LOADED               0x01
10d019
 #define RBTDB_ATTR_LOADING              0x02
10d019
 
10d019
+#define KEEPSTALE(rbtdb) ((rbtdb)->serve_stale_ttl > 0)
10d019
+
10d019
 /*%
10d019
  * Search Context
10d019
  */
943bc6
@@ -1816,15 +1835,15 @@ rollback_node(dns_rbtnode_t *node, rbtdb_serial_t serial) {
10d019
 }
10d019
 
10d019
 static inline void
10d019
-mark_stale_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header) {
10d019
+mark_header_ancient(dns_rbtdb_t *rbtdb, rdatasetheader_t *header) {
10d019
 
10d019
 	/*
10d019
-	 * If we are already stale there is nothing to do.
10d019
+	 * If we are already ancient there is nothing to do.
10d019
 	 */
10d019
-	if ((header->attributes & RDATASET_ATTR_STALE) != 0)
10d019
+	if (ANCIENT(header))
10d019
 		return;
10d019
 
10d019
-	header->attributes |= RDATASET_ATTR_STALE;
10d019
+	header->attributes |= RDATASET_ATTR_ANCIENT;
10d019
 	header->node->dirty = 1;
10d019
 
10d019
 	/*
943bc6
@@ -1865,8 +1884,8 @@ clean_cache_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
10d019
 		/*
10d019
 		 * If current is nonexistent or stale, we can clean it up.
10d019
 		 */
10d019
-		if ((current->attributes &
10d019
-		     (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0) {
10d019
+		if (NONEXISTENT(current) || ANCIENT(current) ||
10d019
+		    (STALE(current) && ! KEEPSTALE(rbtdb))) {
10d019
 			if (top_prev != NULL)
10d019
 				top_prev->next = current->next;
10d019
 			else
943bc6
@@ -2111,6 +2130,80 @@ delete_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
10d019
 	}
10d019
 }
10d019
 
10d019
+#if 0
10d019
+static void
10d019
+clean_now_or_later(dns_rbtnode_t *node, dns_rbtdb_t *rbtdb,
10d019
+		   rdatasetheader_t *header, rdatasetheader_t **header_prevp)
10d019
+{
10d019
+	if (dns_rbtnode_refcurrent(node) == 0) {
10d019
+		isc_mem_t *mctx;
10d019
+
10d019
+		/*
10d019
+		 * header->down can be non-NULL if the refcount has just
10d019
+		 * decremented to 0 but decrement_reference() has not performed
10d019
+		 * clean_cache_node(), in which case we need to purge the stale
10d019
+		 * headers first.
10d019
+		 */
10d019
+		mctx = rbtdb->common.mctx;
10d019
+		clean_stale_headers(rbtdb, mctx, header);
10d019
+		if (*header_prevp != NULL)
10d019
+			(*header_prevp)->next = header->next;
10d019
+		else
10d019
+			node->data = header->next;
10d019
+		free_rdataset(rbtdb, mctx, header);
10d019
+	} else {
10d019
+		header->attributes |= RDATASET_ATTR_STALE |
10d019
+		                      RDATASET_ATTR_ANCIENT;
10d019
+		node->dirty = 1;
10d019
+		*header_prevp = header;
10d019
+	}
10d019
+}
10d019
+
10d019
+static rdataset_ttl_t
10d019
+check_ttl(dns_rbtnode_t *node, rbtdb_search_t *search,
10d019
+	  rdatasetheader_t *header, rdatasetheader_t **header_prevp,
10d019
+	  nodelock_t *lock, isc_rwlocktype_t *locktype)
10d019
+{
10d019
+	dns_rbtdb_t *rbtdb = search->rbtdb;
10d019
+
10d019
+	if (header->rdh_ttl > search->now)
10d019
+		return rdataset_ttl_fresh;
10d019
+
10d019
+	/*
10d019
+	 * This rdataset is stale, but perhaps still usable.
10d019
+	 */
10d019
+	if (KEEPSTALE(rbtdb) &&
10d019
+	    header->rdh_ttl + rbtdb->serve_stale_ttl > search->now) {
10d019
+		header->attributes |= RDATASET_ATTR_STALE;
10d019
+		/* Doesn't set dirty because it doesn't need removal. */
10d019
+		return rdataset_ttl_stale;
10d019
+	}
10d019
+
10d019
+	/*
10d019
+	 * This rdataset is so stale it is no longer usable, even with
10d019
+	 * KEEPSTALE.  If no one else is using the node, we can clean it up
10d019
+	 * right now, otherwise we mark it as ancient, and the node as dirty,
10d019
+	 * so it will get cleaned up later.
10d019
+	 */
10d019
+	if ((header->rdh_ttl <= search->now - RBTDB_VIRTUAL) &&
10d019
+	    (*locktype == isc_rwlocktype_write ||
10d019
+	     NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
10d019
+		/*
10d019
+		 * We update the node's status only when we can get write
10d019
+		 * access; otherwise, we leave others to this work.  Periodical
10d019
+		 * cleaning will eventually take the job as the last resort.
10d019
+		 * We won't downgrade the lock, since other rdatasets are
10d019
+		 * probably stale, too.
10d019
+		 */
10d019
+		*locktype = isc_rwlocktype_write;
10d019
+		clean_now_or_later(node, rbtdb, header, header_prevp);
10d019
+	} else
10d019
+		*header_prevp = header;
10d019
+
10d019
+	return rdataset_ttl_ancient;
10d019
+}
10d019
+#endif
10d019
+
10d019
 /*
10d019
  * Caller must be holding the node lock.
10d019
  */
943bc6
@@ -3343,6 +3436,12 @@ bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, rdatasetheader_t *header,
10d019
 		rdataset->attributes |= DNS_RDATASETATTR_OPTOUT;
10d019
 	if (PREFETCH(header))
10d019
 		rdataset->attributes |= DNS_RDATASETATTR_PREFETCH;
10d019
+	if (STALE(header)) {
10d019
+		rdataset->attributes |= DNS_RDATASETATTR_STALE;
10d019
+		rdataset->stale_ttl =
10d019
+			(rbtdb->serve_stale_ttl + header->rdh_ttl) - now;
10d019
+		rdataset->ttl = 0;
10d019
+	}
10d019
 	rdataset->private1 = rbtdb;
10d019
 	rdataset->private2 = node;
10d019
 	raw = (unsigned char *)header + sizeof(*header);
943bc6
@@ -4698,6 +4797,19 @@ check_stale_header(dns_rbtnode_t *node, rdatasetheader_t *header,
10d019
 #endif
10d019
 
10d019
 	if (!ACTIVE(header, search->now)) {
10d019
+		dns_ttl_t stale = header->rdh_ttl +
10d019
+				  search->rbtdb->serve_stale_ttl;
10d019
+		/*
10d019
+		 * If this data is in the stale window keep it and if
10d019
+		 * DNS_DBFIND_STALEOK is not set we tell the caller to
10d019
+		 * skip this record.
10d019
+		 */
10d019
+		if (KEEPSTALE(search->rbtdb) && stale > search->now) {
10d019
+			header->attributes |= RDATASET_ATTR_STALE;
10d019
+			*header_prev = header;
10d019
+			return ((search->options & DNS_DBFIND_STALEOK) == 0);
10d019
+		}
10d019
+
10d019
 		/*
10d019
 		 * This rdataset is stale.  If no one else is using the
10d019
 		 * node, we can clean it up right now, otherwise we mark
943bc6
@@ -4737,7 +4849,7 @@ check_stale_header(dns_rbtnode_t *node, rdatasetheader_t *header,
10d019
 					node->data = header->next;
10d019
 				free_rdataset(search->rbtdb, mctx, header);
10d019
 			} else {
10d019
-				mark_stale_header(search->rbtdb, header);
10d019
+				mark_header_ancient(search->rbtdb, header);
10d019
 				*header_prev = header;
10d019
 			}
10d019
 		} else
943bc6
@@ -5178,7 +5290,7 @@ cache_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
10d019
 				       &locktype, lock, &search,
10d019
 				       &header_prev)) {
10d019
 			/* Do nothing. */
10d019
-		} else if (EXISTS(header) && (!STALE(header))) {
10d019
+		} else if (EXISTS(header) && !ANCIENT(header)) {
10d019
 			/*
10d019
 			 * We now know that there is at least one active
10d019
 			 * non-stale rdataset at this node.
943bc6
@@ -5661,7 +5773,7 @@ expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
10d019
 			 * refcurrent(rbtnode) must be non-zero.  This is so
10d019
 			 * because 'node' is an argument to the function.
10d019
 			 */
10d019
-			mark_stale_header(rbtdb, header);
10d019
+			mark_header_ancient(rbtdb, header);
10d019
 			if (log)
10d019
 				isc_log_write(dns_lctx, category, module,
10d019
 					      level, "overmem cache: stale %s",
943bc6
@@ -5669,7 +5781,7 @@ expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
10d019
 		} else if (force_expire) {
10d019
 			if (! RETAIN(header)) {
10d019
 				set_ttl(rbtdb, header, 0);
10d019
-				mark_stale_header(rbtdb, header);
10d019
+				mark_header_ancient(rbtdb, header);
10d019
 			} else if (log) {
10d019
 				isc_log_write(dns_lctx, category, module,
10d019
 					      level, "overmem cache: "
943bc6
@@ -5928,9 +6040,9 @@ cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
10d019
 				 * non-zero.  This is so because 'node' is an
10d019
 				 * argument to the function.
10d019
 				 */
10d019
-				mark_stale_header(rbtdb, header);
10d019
+				mark_header_ancient(rbtdb, header);
10d019
 			}
10d019
-		} else if (EXISTS(header) && (!STALE(header))) {
10d019
+		} else if (EXISTS(header) && !ANCIENT(header)) {
10d019
 			if (header->type == matchtype)
10d019
 				found = header;
10d019
 			else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
943bc6
@@ -6232,7 +6344,7 @@ add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
10d019
 				     topheader = topheader->next)
10d019
 				{
10d019
 					set_ttl(rbtdb, topheader, 0);
10d019
-					mark_stale_header(rbtdb, topheader);
10d019
+					mark_header_ancient(rbtdb, topheader);
10d019
 				}
10d019
 				goto find_header;
10d019
 			}
943bc6
@@ -6293,7 +6405,7 @@ add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
10d019
 				 * ncache entry.
10d019
 				 */
10d019
 				set_ttl(rbtdb, topheader, 0);
10d019
-				mark_stale_header(rbtdb, topheader);
10d019
+				mark_header_ancient(rbtdb, topheader);
10d019
 				topheader = NULL;
10d019
 				goto find_header;
10d019
 			}
943bc6
@@ -6331,8 +6443,11 @@ add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
10d019
 		}
10d019
 
10d019
 		/*
10d019
-		 * Trying to add an rdataset with lower trust to a cache DB
10d019
-		 * has no effect, provided that the cache data isn't stale.
10d019
+		 * Trying to add an rdataset with lower trust to a cache
10d019
+		 * DB has no effect, provided that the cache data isn't
10d019
+		 * stale. If the cache data is stale, new lower trust
10d019
+		 * data will supersede it below. Unclear what the best
10d019
+		 * policy is here.
10d019
 		 */
10d019
 		if (rbtversion == NULL && trust < header->trust &&
10d019
 		    (ACTIVE(header, now) || header_nx)) {
943bc6
@@ -6362,6 +6477,10 @@ add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
10d019
 
10d019
 			if ((options & DNS_DBADD_EXACT) != 0)
10d019
 				flags |= DNS_RDATASLAB_EXACT;
10d019
+			/*
10d019
+			 * TTL use here is irrelevant to the cache;
10d019
+			 * merge is only done with zonedbs.
10d019
+			 */
10d019
 			if ((options & DNS_DBADD_EXACTTTL) != 0 &&
10d019
 			     newheader->rdh_ttl != header->rdh_ttl)
10d019
 					result = DNS_R_NOTEXACT;
943bc6
@@ -6405,11 +6524,12 @@ add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
10d019
 			}
10d019
 		}
10d019
 		/*
10d019
-		 * Don't replace existing NS, A and AAAA RRsets
10d019
-		 * in the cache if they are already exist.  This
10d019
-		 * prevents named being locked to old servers.
10d019
-		 * Don't lower trust of existing record if the
10d019
-		 * update is forced.
10d019
+		 * Don't replace existing NS, A and AAAA RRsets in the
10d019
+		 * cache if they are already exist. This prevents named
10d019
+		 * being locked to old servers. Don't lower trust of
10d019
+		 * existing record if the update is forced. Nothing
10d019
+		 * special to be done w.r.t stale data; it gets replaced
10d019
+		 * normally further down.
10d019
 		 */
10d019
 		if (IS_CACHE(rbtdb) && ACTIVE(header, now) &&
10d019
 		    header->type == dns_rdatatype_ns &&
943bc6
@@ -6582,10 +6702,10 @@ add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
10d019
 				changed->dirty = true;
10d019
 			if (rbtversion == NULL) {
10d019
 				set_ttl(rbtdb, header, 0);
10d019
-				mark_stale_header(rbtdb, header);
10d019
+				mark_header_ancient(rbtdb, header);
10d019
 				if (sigheader != NULL) {
10d019
 					set_ttl(rbtdb, sigheader, 0);
10d019
-					mark_stale_header(rbtdb, sigheader);
10d019
+					mark_header_ancient(rbtdb, sigheader);
10d019
 				}
10d019
 			}
10d019
 			if (rbtversion != NULL && !header_nx) {
943bc6
@@ -8436,6 +8556,30 @@ nodefullname(dns_db_t *db, dns_dbnode_t *node, dns_name_t *name) {
10d019
 	return (result);
10d019
 }
10d019
 
10d019
+static isc_result_t
10d019
+setservestalettl(dns_db_t *db, dns_ttl_t ttl) {
10d019
+	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
10d019
+
10d019
+	REQUIRE(VALID_RBTDB(rbtdb));
10d019
+	REQUIRE(IS_CACHE(rbtdb));
10d019
+
10d019
+	/* currently no bounds checking.  0 means disable. */
10d019
+	rbtdb->serve_stale_ttl = ttl;
10d019
+	return ISC_R_SUCCESS;
10d019
+}
10d019
+
10d019
+static isc_result_t
10d019
+getservestalettl(dns_db_t *db, dns_ttl_t *ttl) {
10d019
+	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
10d019
+
10d019
+	REQUIRE(VALID_RBTDB(rbtdb));
10d019
+	REQUIRE(IS_CACHE(rbtdb));
10d019
+
10d019
+	*ttl = rbtdb->serve_stale_ttl;
10d019
+	return ISC_R_SUCCESS;
10d019
+}
10d019
+
10d019
+
10d019
 static dns_dbmethods_t zone_methods = {
10d019
 	attach,
10d019
 	detach,
943bc6
@@ -8481,7 +8625,9 @@ static dns_dbmethods_t zone_methods = {
10d019
 	NULL,
10d019
 	hashsize,
10d019
 	nodefullname,
10d019
-	getsize
10d019
+	getsize,
10d019
+	NULL,
10d019
+	NULL
10d019
 };
10d019
 
10d019
 static dns_dbmethods_t cache_methods = {
943bc6
@@ -8529,7 +8675,9 @@ static dns_dbmethods_t cache_methods = {
10d019
 	setcachestats,
10d019
 	hashsize,
10d019
 	nodefullname,
10d019
-	NULL
10d019
+	NULL,
10d019
+	setservestalettl,
10d019
+	getservestalettl
10d019
 };
10d019
 
10d019
 isc_result_t
943bc6
@@ -8800,7 +8948,7 @@ dns_rbtdb_create
10d019
 	rbtdb->rpzs = NULL;
10d019
 	rbtdb->load_rpzs = NULL;
10d019
 	rbtdb->rpz_num = DNS_RPZ_INVALID_NUM;
10d019
-
10d019
+	rbtdb->serve_stale_ttl = 0;
10d019
 	/*
10d019
 	 * Version Initialization.
10d019
 	 */
943bc6
@@ -9218,7 +9366,8 @@ rdatasetiter_first(dns_rdatasetiter_t *iterator) {
10d019
 				 * rdatasets to work.
10d019
 				 */
10d019
 				if (NONEXISTENT(header) ||
10d019
-				    (now != 0 && now > header->rdh_ttl))
10d019
+				    (now != 0 && now > header->rdh_ttl
10d019
+						     + rbtdb->serve_stale_ttl))
10d019
 					header = NULL;
10d019
 				break;
10d019
 			} else
943bc6
@@ -10427,7 +10576,7 @@ static inline bool
10d019
 need_headerupdate(rdatasetheader_t *header, isc_stdtime_t now) {
10d019
 	if ((header->attributes &
10d019
 	     (RDATASET_ATTR_NONEXISTENT |
10d019
-	      RDATASET_ATTR_STALE |
10d019
+	      RDATASET_ATTR_ANCIENT |
10d019
 	      RDATASET_ATTR_ZEROTTL)) != 0)
10d019
 		return (false);
10d019
 
943bc6
@@ -10533,7 +10682,7 @@ expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
10d019
 	      bool tree_locked, expire_t reason)
10d019
 {
10d019
 	set_ttl(rbtdb, header, 0);
10d019
-	mark_stale_header(rbtdb, header);
10d019
+	mark_header_ancient(rbtdb, header);
10d019
 
10d019
 	/*
10d019
 	 * Caller must hold the node (write) lock.
10d019
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
943bc6
index 49ec49c..2de70a6 100644
10d019
--- a/lib/dns/resolver.c
10d019
+++ b/lib/dns/resolver.c
10d019
@@ -141,16 +141,17 @@
10d019
 #endif /* WANT_QUERYTRACE */
10d019
 
10d019
 #define US_PER_SEC 1000000U
10d019
+#define US_PER_MSEC 1000U
10d019
 /*
10d019
  * The maximum time we will wait for a single query.
10d019
  */
10d019
-#define MAX_SINGLE_QUERY_TIMEOUT 9U
10d019
-#define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_SEC)
10d019
+#define MAX_SINGLE_QUERY_TIMEOUT 9000U
10d019
+#define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_MSEC)
10d019
 
10d019
 /*
10d019
  * We need to allow a individual query time to complete / timeout.
10d019
  */
10d019
-#define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1U)
10d019
+#define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1000U)
10d019
 
10d019
 /* The default time in seconds for the whole query to live. */
10d019
 #ifndef DEFAULT_QUERY_TIMEOUT
10d019
@@ -159,7 +160,7 @@
10d019
 
10d019
 /* The maximum time in seconds for the whole query to live. */
10d019
 #ifndef MAXIMUM_QUERY_TIMEOUT
10d019
-#define MAXIMUM_QUERY_TIMEOUT 30
10d019
+#define MAXIMUM_QUERY_TIMEOUT 30000
10d019
 #endif
10d019
 
10d019
 /* The default maximum number of recursions to follow before giving up. */
943bc6
@@ -529,6 +530,11 @@ struct dns_resolver {
230545
 	dns_fetch_t *			primefetch;
230545
 	/* Locked by nlock. */
230545
 	unsigned int			nfctx;
230545
+
230545
+	/* Unlocked. Additions for serve-stale feature. */
10d019
+	unsigned int			retryinterval; /* in milliseconds */
10d019
+	unsigned int			nonbackofftries;
10d019
+
230545
 };
230545
 
230545
 #define RES_MAGIC			ISC_MAGIC('R', 'e', 's', '!')
943bc6
@@ -1650,14 +1656,12 @@ fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
10d019
 	unsigned int seconds;
10d019
 	unsigned int us;
10d019
 
10d019
+	us = fctx->res->retryinterval * 1000;
10d019
 	/*
10d019
-	 * We retry every .8 seconds the first two times through the address
10d019
-	 * list, and then we do exponential back-off.
10d019
+	 * Exponential backoff after the first few tries.
10d019
 	 */
10d019
-	if (fctx->restarts < 3)
10d019
-		us = 800000;
10d019
-	else
10d019
-		us = (800000 << (fctx->restarts - 2));
10d019
+	if (fctx->restarts >= fctx->res->nonbackofftries)
10d019
+		us <<= (fctx->restarts - fctx->res->nonbackofftries - 1);
10d019
 
10d019
 	/*
10d019
 	 * Add a fudge factor to the expected rtt based on the current
943bc6
@@ -4542,7 +4546,8 @@ fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
10d019
 	/*
10d019
 	 * Compute an expiration time for the entire fetch.
10d019
 	 */
10d019
-	isc_interval_set(&interval, res->query_timeout, 0);
10d019
+	isc_interval_set(&interval, res->query_timeout / 1000,
10d019
+			 res->query_timeout % 1000 * 1000000);
10d019
 	iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
10d019
 	if (iresult != ISC_R_SUCCESS) {
10d019
 		UNEXPECTED_ERROR(__FILE__, __LINE__,
943bc6
@@ -9105,6 +9110,8 @@ dns_resolver_create(dns_view_t *view,
10d019
 	res->spillattimer = NULL;
10d019
 	res->zspill = 0;
10d019
 	res->zero_no_soa_ttl = false;
10d019
+	res->retryinterval = 30000;
10d019
+	res->nonbackofftries = 3;
10d019
 	res->query_timeout = DEFAULT_QUERY_TIMEOUT;
10d019
 	res->maxdepth = DEFAULT_RECURSION_DEPTH;
10d019
 	res->maxqueries = DEFAULT_MAX_QUERIES;
943bc6
@@ -10439,17 +10446,20 @@ dns_resolver_gettimeout(dns_resolver_t *resolver) {
10d019
 }
10d019
 
10d019
 void
10d019
-dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
10d019
+dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int timeout) {
10d019
 	REQUIRE(VALID_RESOLVER(resolver));
10d019
 
10d019
-	if (seconds == 0)
10d019
-		seconds = DEFAULT_QUERY_TIMEOUT;
10d019
-	if (seconds > MAXIMUM_QUERY_TIMEOUT)
10d019
-		seconds = MAXIMUM_QUERY_TIMEOUT;
10d019
-	if (seconds < MINIMUM_QUERY_TIMEOUT)
10d019
-		seconds =  MINIMUM_QUERY_TIMEOUT;
10d019
+	if (timeout <= 300)
10d019
+		timeout *= 1000;
10d019
+
10d019
+	if (timeout == 0)
10d019
+		timeout = DEFAULT_QUERY_TIMEOUT;
10d019
+	if (timeout > MAXIMUM_QUERY_TIMEOUT)
10d019
+		timeout = MAXIMUM_QUERY_TIMEOUT;
10d019
+	if (timeout < MINIMUM_QUERY_TIMEOUT)
10d019
+		timeout =  MINIMUM_QUERY_TIMEOUT;
10d019
 
10d019
-	resolver->query_timeout = seconds;
10d019
+	resolver->query_timeout = timeout;
10d019
 }
10d019
 
10d019
 void
943bc6
@@ -10546,3 +10556,34 @@ dns_resolver_getquotaresponse(dns_resolver_t *resolver, dns_quotatype_t which)
10d019
 
10d019
 	return (resolver->quotaresp[which]);
10d019
 }
10d019
+
10d019
+unsigned int
10d019
+dns_resolver_getretryinterval(dns_resolver_t *resolver) {
10d019
+	REQUIRE(VALID_RESOLVER(resolver));
10d019
+
10d019
+	return (resolver->retryinterval);
10d019
+}
10d019
+
10d019
+void
10d019
+dns_resolver_setretryinterval(dns_resolver_t *resolver, unsigned int interval)
10d019
+{
10d019
+	REQUIRE(VALID_RESOLVER(resolver));
10d019
+	REQUIRE(interval > 0);
10d019
+
10d019
+	resolver->retryinterval = ISC_MIN(interval, 2000);
10d019
+}
10d019
+
10d019
+unsigned int
10d019
+dns_resolver_getnonbackofftries(dns_resolver_t *resolver) {
10d019
+	REQUIRE(VALID_RESOLVER(resolver));
10d019
+
10d019
+	return (resolver->nonbackofftries);
10d019
+}
10d019
+
10d019
+void
10d019
+dns_resolver_setnonbackofftries(dns_resolver_t *resolver, unsigned int tries) {
10d019
+	REQUIRE(VALID_RESOLVER(resolver));
10d019
+	REQUIRE(tries > 0);
10d019
+
10d019
+	resolver->nonbackofftries = tries;
10d019
+}
10d019
diff --git a/lib/dns/sdb.c b/lib/dns/sdb.c
943bc6
index 477bb74..09cf932 100644
10d019
--- a/lib/dns/sdb.c
10d019
+++ b/lib/dns/sdb.c
943bc6
@@ -1370,7 +1370,9 @@ static dns_dbmethods_t sdb_methods = {
10d019
 	NULL,			/* setcachestats */
10d019
 	NULL,			/* hashsize */
10d019
 	NULL,			/* nodefullname */
10d019
-	NULL			/* getsize */
10d019
+	NULL,			/* getsize */
10d019
+	NULL,			/* setservestalettl */
10d019
+	NULL			/* getservestalettl */
10d019
 };
10d019
 
10d019
 static isc_result_t
10d019
diff --git a/lib/dns/sdlz.c b/lib/dns/sdlz.c
943bc6
index 037d74a..9218fed 100644
10d019
--- a/lib/dns/sdlz.c
10d019
+++ b/lib/dns/sdlz.c
10d019
@@ -1336,7 +1336,9 @@ static dns_dbmethods_t sdlzdb_methods = {
10d019
 	NULL,			/* setcachestats */
10d019
 	NULL,			/* hashsize */
10d019
 	NULL,			/* nodefullname */
10d019
-	NULL			/* getsize */
10d019
+	NULL,			/* getsize */
10d019
+	NULL,			/* setservestalettl */
10d019
+	NULL			/* getservestalettl */
10d019
 };
10d019
 
10d019
 /*
10d019
diff --git a/lib/dns/tests/db_test.c b/lib/dns/tests/db_test.c
943bc6
index bc1cc3f..60fdb81 100644
10d019
--- a/lib/dns/tests/db_test.c
10d019
+++ b/lib/dns/tests/db_test.c
10d019
@@ -28,8 +28,9 @@
10d019
 
10d019
 #include <dns/db.h>
10d019
 #include <dns/dbiterator.h>
10d019
-#include <dns/name.h>
10d019
 #include <dns/journal.h>
10d019
+#include <dns/name.h>
10d019
+#include <dns/rdatalist.h>
10d019
 
10d019
 #include "dnstest.h"
10d019
 
10d019
@@ -76,7 +77,7 @@ getoriginnode_test(void **state) {
10d019
 	assert_int_equal(result, ISC_R_SUCCESS);
10d019
 
10d019
 	result = dns_db_create(mymctx, "rbt", dns_rootname, dns_dbtype_zone,
10d019
-			    dns_rdataclass_in, 0, NULL, &db);
10d019
+			       dns_rdataclass_in, 0, NULL, &db);
10d019
 	assert_int_equal(result, ISC_R_SUCCESS);
10d019
 
10d019
 	result = dns_db_getoriginnode(db, &node);
10d019
@@ -91,6 +92,197 @@ getoriginnode_test(void **state) {
10d019
 	isc_mem_detach(&mymctx);
10d019
 }
10d019
 
10d019
+/* test getservestalettl and setservestalettl */
10d019
+static void
10d019
+getsetservestalettl_test(void **state) {
10d019
+	dns_db_t *db = NULL;
10d019
+	isc_mem_t *mymctx = NULL;
10d019
+	isc_result_t result;
10d019
+	dns_ttl_t ttl;
10d019
+
10d019
+	UNUSED(state);
10d019
+
10d019
+	result = isc_mem_create(0, 0, &mymctx);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+	result = dns_db_create(mymctx, "rbt", dns_rootname, dns_dbtype_cache,
10d019
+			       dns_rdataclass_in, 0, NULL, &db);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+	ttl = 5000;
10d019
+	result = dns_db_getservestalettl(db, &ttl);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+	assert_int_equal(ttl, 0);
10d019
+
10d019
+	ttl = 6 * 3600;
10d019
+	result = dns_db_setservestalettl(db, ttl);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+	ttl = 5000;
10d019
+	result = dns_db_getservestalettl(db, &ttl);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+	assert_int_equal(ttl, 6 * 3600);
10d019
+
10d019
+	dns_db_detach(&db);
10d019
+	isc_mem_detach(&mymctx);
10d019
+}
10d019
+
10d019
+/* check DNS_DBFIND_STALEOK works */
10d019
+static void
10d019
+dns_dbfind_staleok_test(void **state) {
10d019
+	dns_db_t *db = NULL;
10d019
+	dns_dbnode_t *node = NULL;
10d019
+	dns_fixedname_t example_fixed;
10d019
+	dns_fixedname_t found_fixed;
10d019
+	dns_name_t *example;
10d019
+	dns_name_t *found;
10d019
+	dns_rdatalist_t rdatalist;
10d019
+	dns_rdataset_t rdataset;
10d019
+	int count;
10d019
+	int pass;
10d019
+	isc_mem_t *mymctx = NULL;
10d019
+	isc_result_t result;
10d019
+	unsigned char data[] = { 0x0a, 0x00, 0x00, 0x01 };
10d019
+
10d019
+	UNUSED(state);
10d019
+
10d019
+	result = isc_mem_create(0, 0, &mymctx);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+	result = dns_db_create(mymctx, "rbt", dns_rootname, dns_dbtype_cache,
10d019
+			       dns_rdataclass_in, 0, NULL, &db);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+	example = dns_fixedname_initname(&example_fixed);
10d019
+	found = dns_fixedname_initname(&found_fixed);
10d019
+
10d019
+	result = dns_name_fromstring(example, "example", 0, NULL);
10d019
+	assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+	/*
10d019
+	 * Pass 0: default; no stale processing permitted.
10d019
+	 * Pass 1: stale processing for 1 second.
10d019
+	 * Pass 2: stale turned off after being on.
10d019
+	 */
10d019
+	for (pass = 0; pass < 3; pass++) {
10d019
+		dns_rdata_t rdata = DNS_RDATA_INIT;
10d019
+
10d019
+		/* 10.0.0.1 */
10d019
+		rdata.data = data;
10d019
+		rdata.length = 4;
10d019
+		rdata.rdclass = dns_rdataclass_in;
10d019
+		rdata.type = dns_rdatatype_a;
10d019
+
10d019
+		dns_rdatalist_init(&rdatalist);
10d019
+		rdatalist.ttl = 2;
10d019
+		rdatalist.type = dns_rdatatype_a;
10d019
+		rdatalist.rdclass = dns_rdataclass_in;
10d019
+		ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
10d019
+
10d019
+		switch (pass) {
10d019
+		case 0:
10d019
+			/* default: stale processing off */
10d019
+			break;
10d019
+		case 1:
10d019
+			/* turn on stale processing */
10d019
+			result = dns_db_setservestalettl(db, 1);
10d019
+			assert_int_equal(result, ISC_R_SUCCESS);
10d019
+			break;
10d019
+		case 2:
10d019
+			/* turn off stale processing */
10d019
+			result = dns_db_setservestalettl(db, 0);
10d019
+			assert_int_equal(result, ISC_R_SUCCESS);
10d019
+			break;
10d019
+		}
10d019
+
10d019
+		dns_rdataset_init(&rdataset);
10d019
+		result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
10d019
+		assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+		result = dns_db_findnode(db, example, true, &node);
10d019
+		assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+		result = dns_db_addrdataset(db, node, NULL, 0, &rdataset, 0,
10d019
+					    NULL);
10d019
+		assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+		dns_db_detachnode(db, &node);
10d019
+		dns_rdataset_disassociate(&rdataset);
10d019
+
10d019
+		result = dns_db_find(db, example, NULL, dns_rdatatype_a,
10d019
+				     0, 0, &node, found, &rdataset, NULL);
10d019
+		assert_int_equal(result, ISC_R_SUCCESS);
10d019
+
10d019
+		/*
10d019
+		 * May loop for up to 2 seconds performing non stale lookups.
10d019
+		 */
10d019
+		count = 0;
10d019
+		do {
10d019
+			count++;
10d019
+			assert_in_range(count, 0, 20); /* loop sanity */
10d019
+			assert_int_equal(rdataset.attributes &
10d019
+				     DNS_RDATASETATTR_STALE, 0);
10d019
+			assert_true(rdataset.ttl > 0);
10d019
+			dns_db_detachnode(db, &node);
10d019
+			dns_rdataset_disassociate(&rdataset);
10d019
+
10d019
+			usleep(100000);	/* 100 ms */
10d019
+
10d019
+			result = dns_db_find(db, example, NULL,
10d019
+					     dns_rdatatype_a, 0, 0,
10d019
+					     &node, found, &rdataset, NULL);
10d019
+		} while (result == ISC_R_SUCCESS);
10d019
+
10d019
+		assert_int_equal(result, ISC_R_NOTFOUND);
10d019
+
10d019
+		/*
10d019
+		 * Check whether we can get stale data.
10d019
+		 */
10d019
+		result = dns_db_find(db, example, NULL, dns_rdatatype_a,
10d019
+				     DNS_DBFIND_STALEOK, 0,
10d019
+				     &node, found, &rdataset, NULL);
10d019
+		switch (pass) {
10d019
+		case 0:
10d019
+			assert_int_equal(result, ISC_R_NOTFOUND);
10d019
+			break;
10d019
+		case 1:
10d019
+			/*
10d019
+			 * Should loop for 1 second with stale lookups then
10d019
+			 * stop.
10d019
+			 */
10d019
+			count = 0;
10d019
+			do {
10d019
+				count++;
10d019
+				assert_in_range(count, 0, 49); /* loop sanity */
10d019
+				assert_int_equal(result, ISC_R_SUCCESS);
10d019
+				assert_int_equal(rdataset.ttl, 0);
10d019
+				assert_int_equal(rdataset.attributes &
10d019
+					     DNS_RDATASETATTR_STALE,
10d019
+					     DNS_RDATASETATTR_STALE);
10d019
+				dns_db_detachnode(db, &node);
10d019
+				dns_rdataset_disassociate(&rdataset);
10d019
+
10d019
+				usleep(100000);	/* 100 ms */
10d019
+
10d019
+				result = dns_db_find(db, example, NULL,
10d019
+						     dns_rdatatype_a,
10d019
+						     DNS_DBFIND_STALEOK,
10d019
+						     0, &node, found,
10d019
+						     &rdataset, NULL);
10d019
+			} while (result == ISC_R_SUCCESS);
10d019
+			assert_in_range(count, 1, 10);
10d019
+			assert_int_equal(result, ISC_R_NOTFOUND);
10d019
+			break;
10d019
+		case 2:
10d019
+			assert_int_equal(result, ISC_R_NOTFOUND);
10d019
+			break;
10d019
+		}
10d019
+	}
10d019
+
10d019
+	dns_db_detach(&db);
10d019
+	isc_mem_detach(&mymctx);
10d019
+}
10d019
+
10d019
 /* database class */
10d019
 static void
10d019
 class_test(void **state) {
10d019
@@ -213,6 +405,8 @@ int
10d019
 main(void) {
10d019
 	const struct CMUnitTest tests[] = {
10d019
 		cmocka_unit_test(getoriginnode_test),
10d019
+		cmocka_unit_test(getsetservestalettl_test),
10d019
+		cmocka_unit_test(dns_dbfind_staleok_test),
10d019
 		cmocka_unit_test_setup_teardown(class_test,
10d019
 						_setup, _teardown),
10d019
 		cmocka_unit_test_setup_teardown(dbtype_test,
10d019
diff --git a/lib/dns/view.c b/lib/dns/view.c
943bc6
index a7ba613..a644c5f 100644
10d019
--- a/lib/dns/view.c
10d019
+++ b/lib/dns/view.c
10d019
@@ -229,6 +229,9 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
10d019
 	view->flush = false;
10d019
 	view->dlv = NULL;
10d019
 	view->maxudp = 0;
10d019
+	view->staleanswerttl = 1;
10d019
+	view->staleanswersok = dns_stale_answer_conf;
10d019
+	view->staleanswersenable = false;
10d019
 	view->nocookieudp = 0;
10d019
 	view->maxbits = 0;
10d019
 	view->v4_aaaa = dns_aaaa_ok;
10d019
diff --git a/lib/isccfg/namedconf.c b/lib/isccfg/namedconf.c
943bc6
index 212194e..b562f95 100644
10d019
--- a/lib/isccfg/namedconf.c
10d019
+++ b/lib/isccfg/namedconf.c
10d019
@@ -1778,6 +1778,7 @@ view_clauses[] = {
10d019
 	{ "max-ncache-ttl", &cfg_type_uint32, 0 },
10d019
 	{ "max-recursion-depth", &cfg_type_uint32, 0 },
10d019
 	{ "max-recursion-queries", &cfg_type_uint32, 0 },
10d019
+	{ "max-stale-ttl", &cfg_type_ttlval, 0 },
10d019
 	{ "max-udp-size", &cfg_type_uint32, 0 },
10d019
 	{ "message-compression", &cfg_type_boolean, 0 },
10d019
 	{ "min-roots", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
10d019
@@ -1806,7 +1807,9 @@ view_clauses[] = {
10d019
 	{ "request-nsid", &cfg_type_boolean, 0 },
10d019
 	{ "request-sit", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
10d019
 	{ "require-server-cookie", &cfg_type_boolean, 0 },
10d019
+	{ "resolver-nonbackoff-tries", &cfg_type_uint32, 0 },
10d019
 	{ "resolver-query-timeout", &cfg_type_uint32, 0 },
10d019
+	{ "resolver-retry-interval", &cfg_type_uint32, 0 },
10d019
 	{ "response-policy", &cfg_type_rpz, 0 },
10d019
 	{ "rfc2308-type1", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
10d019
 	{ "root-delegation-only",  &cfg_type_optional_exclude, 0 },
10d019
@@ -1815,6 +1818,8 @@ view_clauses[] = {
10d019
 	{ "send-cookie", &cfg_type_boolean, 0 },
10d019
 	{ "servfail-ttl", &cfg_type_ttlval, 0 },
10d019
 	{ "sortlist", &cfg_type_bracketed_aml, 0 },
10d019
+	{ "stale-answer-enable", &cfg_type_boolean, 0 },
10d019
+	{ "stale-answer-ttl", &cfg_type_ttlval, 0 },
10d019
 	{ "suppress-initial-notify", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
10d019
 	{ "topology", &cfg_type_bracketed_aml, CFG_CLAUSEFLAG_NOTIMP },
10d019
 	{ "transfer-format", &cfg_type_transferformat, 0 },
10d019
-- 
943bc6
2.26.2
10d019