2 * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
25 #include <isc/platform.h>
26 #include <isc/print.h>
27 #include <isc/string.h>
28 #include <isc/random.h>
29 #include <isc/socket.h>
30 #include <isc/stats.h>
32 #include <isc/timer.h>
37 #include <dns/cache.h>
39 #include <dns/dispatch.h>
41 #include <dns/events.h>
42 #include <dns/forward.h>
43 #include <dns/keytable.h>
45 #include <dns/message.h>
46 #include <dns/ncache.h>
48 #include <dns/nsec3.h>
49 #include <dns/opcode.h>
52 #include <dns/rcode.h>
53 #include <dns/rdata.h>
54 #include <dns/rdataclass.h>
55 #include <dns/rdatalist.h>
56 #include <dns/rdataset.h>
57 #include <dns/rdatastruct.h>
58 #include <dns/rdatatype.h>
59 #include <dns/resolver.h>
60 #include <dns/result.h>
61 #include <dns/rootns.h>
62 #include <dns/stats.h>
64 #include <dns/validator.h>
66 #define DNS_RESOLVER_TRACE
67 #ifdef DNS_RESOLVER_TRACE
68 #define RTRACE(m) isc_log_write(dns_lctx, \
69 DNS_LOGCATEGORY_RESOLVER, \
70 DNS_LOGMODULE_RESOLVER, \
72 "res %p: %s", res, (m))
73 #define RRTRACE(r, m) isc_log_write(dns_lctx, \
74 DNS_LOGCATEGORY_RESOLVER, \
75 DNS_LOGMODULE_RESOLVER, \
77 "res %p: %s", (r), (m))
78 #define FCTXTRACE(m) isc_log_write(dns_lctx, \
79 DNS_LOGCATEGORY_RESOLVER, \
80 DNS_LOGMODULE_RESOLVER, \
82 "fctx %p(%s): %s", fctx, fctx->info, (m))
83 #define FCTXTRACE2(m1, m2) \
84 isc_log_write(dns_lctx, \
85 DNS_LOGCATEGORY_RESOLVER, \
86 DNS_LOGMODULE_RESOLVER, \
88 "fctx %p(%s): %s %s", \
89 fctx, fctx->info, (m1), (m2))
90 #define FTRACE(m) isc_log_write(dns_lctx, \
91 DNS_LOGCATEGORY_RESOLVER, \
92 DNS_LOGMODULE_RESOLVER, \
94 "fetch %p (fctx %p(%s)): %s", \
95 fetch, fetch->private, \
96 fetch->private->info, (m))
97 #define QTRACE(m) isc_log_write(dns_lctx, \
98 DNS_LOGCATEGORY_RESOLVER, \
99 DNS_LOGMODULE_RESOLVER, \
101 "resquery %p (fctx %p(%s)): %s", \
102 query, query->fctx, \
103 query->fctx->info, (m))
106 #define RRTRACE(r, m)
108 #define FCTXTRACE2(m1, m2)
113 #define US_PER_SEC 1000000U
115 * The maximum time we will wait for a single query.
117 #define MAX_SINGLE_QUERY_TIMEOUT 9U
118 #define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_SEC)
121 * We need to allow a individual query time to complete / timeout.
123 #define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1U)
125 /* The default time in seconds for the whole query to live. */
126 #ifndef DEFAULT_QUERY_TIMEOUT
127 #define DEFAULT_QUERY_TIMEOUT MINIMUM_QUERY_TIMEOUT
130 #ifndef MAXIMUM_QUERY_TIMEOUT
131 #define MAXIMUM_QUERY_TIMEOUT 30 /* The maximum time in seconds for the whole query to live. */
134 /* The default maximum number of recursions to follow before giving up. */
135 #ifndef DEFAULT_RECURSION_DEPTH
136 #define DEFAULT_RECURSION_DEPTH 7
139 /* The default maximum number of iterative queries to allow before giving up. */
140 #ifndef DEFAULT_MAX_QUERIES
141 #define DEFAULT_MAX_QUERIES 50
145 * Maximum EDNS0 input packet size.
147 #define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */
151 * This defines the maximum number of timeouts we will permit before we
152 * disable EDNS0 on the query.
154 #define MAX_EDNS0_TIMEOUTS 3
156 typedef struct fetchctx fetchctx_t;
158 typedef struct query {
159 /* Locked by task event serialization. */
163 dns_dispatchmgr_t * dispatchmgr;
164 dns_dispatch_t * dispatch;
165 isc_boolean_t exclusivesocket;
166 dns_adbaddrinfo_t * addrinfo;
167 isc_socket_t * tcpsocket;
170 dns_dispentry_t * dispentry;
171 ISC_LINK(struct query) link;
174 dns_tsigkey_t *tsigkey;
175 isc_socketevent_t sendevent;
176 unsigned int options;
177 unsigned int attributes;
179 unsigned int connects;
180 unsigned char data[512];
183 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
184 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
186 #define RESQUERY_ATTR_CANCELED 0x02
188 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
189 #define RESQUERY_CANCELED(q) (((q)->attributes & \
190 RESQUERY_ATTR_CANCELED) != 0)
191 #define RESQUERY_SENDING(q) ((q)->sends > 0)
194 fetchstate_init = 0, /*%< Start event has not run yet. */
196 fetchstate_done /*%< FETCHDONE events posted. */
200 badns_unreachable = 0,
208 dns_resolver_t * res;
210 dns_rdatatype_t type;
211 unsigned int options;
212 unsigned int bucketnum;
216 /*% Locked by appropriate bucket lock. */
218 isc_boolean_t want_shutdown;
219 isc_boolean_t cloned;
220 isc_boolean_t spilled;
221 unsigned int references;
222 isc_event_t control_event;
223 ISC_LINK(struct fetchctx) link;
224 ISC_LIST(dns_fetchevent_t) events;
225 /*% Locked by task event serialization. */
227 dns_rdataset_t nameservers;
228 unsigned int attributes;
231 isc_interval_t interval;
232 dns_message_t * qmessage;
233 dns_message_t * rmessage;
234 ISC_LIST(resquery_t) queries;
235 dns_adbfindlist_t finds;
236 dns_adbfind_t * find;
237 dns_adbfindlist_t altfinds;
238 dns_adbfind_t * altfind;
239 dns_adbaddrinfolist_t forwaddrs;
240 dns_adbaddrinfolist_t altaddrs;
241 isc_sockaddrlist_t forwarders;
242 dns_fwdpolicy_t fwdpolicy;
243 isc_sockaddrlist_t bad;
244 isc_sockaddrlist_t edns;
245 isc_sockaddrlist_t edns512;
246 isc_sockaddrlist_t bad_edns;
247 dns_validator_t *validator;
248 ISC_LIST(dns_validator_t) validators;
251 isc_boolean_t ns_ttl_ok;
255 * The number of events we're waiting for.
257 unsigned int pending;
260 * The number of times we've "restarted" the current
261 * nameserver set. This acts as a failsafe to prevent
262 * us from pounding constantly on a particular set of
263 * servers that, for whatever reason, are not giving
264 * us useful responses, but are responding in such a
265 * way that they are not marked "bad".
267 unsigned int restarts;
270 * The number of timeouts that have occurred since we
271 * last successfully received a response packet. This
272 * is used for EDNS0 black hole detection.
274 unsigned int timeouts;
277 * Look aside state for DS lookups.
280 dns_fetch_t * nsfetch;
281 dns_rdataset_t nsrrset;
284 * Number of queries that reference this context.
286 unsigned int nqueries;
289 * The reason to print when logging a successful
290 * response to a query.
295 * Random numbers to use for mixing up server addresses.
297 isc_uint32_t rand_buf;
298 isc_uint32_t rand_bits;
301 * Fetch-local statistics for detailed logging.
303 isc_result_t result; /*%< fetch result */
304 isc_result_t vresult; /*%< validation result */
307 isc_uint64_t duration;
308 isc_boolean_t logged;
309 unsigned int querysent;
310 unsigned int totalqueries;
311 unsigned int referrals;
312 unsigned int lamecount;
314 unsigned int badresp;
316 unsigned int findfail;
317 unsigned int valfail;
318 isc_boolean_t timeout;
319 dns_adbaddrinfo_t *addrinfo;
320 isc_sockaddr_t *client;
324 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
325 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
327 #define FCTX_ATTR_HAVEANSWER 0x0001
328 #define FCTX_ATTR_GLUING 0x0002
329 #define FCTX_ATTR_ADDRWAIT 0x0004
330 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
331 #define FCTX_ATTR_WANTCACHE 0x0010
332 #define FCTX_ATTR_WANTNCACHE 0x0020
333 #define FCTX_ATTR_NEEDEDNS0 0x0040
334 #define FCTX_ATTR_TRIEDFIND 0x0080
335 #define FCTX_ATTR_TRIEDALT 0x0100
337 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
339 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
341 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
343 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
345 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
346 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
347 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
348 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
349 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
352 dns_adbaddrinfo_t * addrinfo;
358 fetchctx_t * private;
361 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
362 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
364 typedef struct fctxbucket {
367 ISC_LIST(fetchctx_t) fctxs;
368 isc_boolean_t exiting;
372 typedef struct alternate {
373 isc_boolean_t isaddress;
381 ISC_LINK(struct alternate) link;
384 typedef struct dns_badcache dns_badcache_t;
385 struct dns_badcache {
386 dns_badcache_t * next;
387 dns_rdatatype_t type;
389 unsigned int hashval;
392 #define DNS_BADCACHE_SIZE 1021
393 #define DNS_BADCACHE_TTL(fctx) \
394 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
396 struct dns_resolver {
402 isc_mutex_t primelock;
403 dns_rdataclass_t rdclass;
404 isc_socketmgr_t * socketmgr;
405 isc_timermgr_t * timermgr;
406 isc_taskmgr_t * taskmgr;
408 isc_boolean_t frozen;
409 unsigned int options;
410 dns_dispatchmgr_t * dispatchmgr;
411 dns_dispatchset_t * dispatches4;
412 isc_boolean_t exclusivev4;
413 dns_dispatchset_t * dispatches6;
414 isc_boolean_t exclusivev6;
415 unsigned int nbuckets;
416 fctxbucket_t * buckets;
417 isc_uint32_t lame_ttl;
418 ISC_LIST(alternate_t) alternates;
419 isc_uint16_t udpsize;
421 isc_rwlock_t alglock;
423 dns_rbt_t * algorithms;
425 isc_rwlock_t mbslock;
427 dns_rbt_t * mustbesecure;
428 unsigned int spillatmax;
429 unsigned int spillatmin;
430 isc_timer_t * spillattimer;
431 isc_boolean_t zero_no_soa_ttl;
432 unsigned int query_timeout;
433 unsigned int maxdepth;
435 /* Locked by lock. */
436 unsigned int references;
437 isc_boolean_t exiting;
438 isc_eventlist_t whenshutdown;
439 unsigned int activebuckets;
440 isc_boolean_t priming;
441 unsigned int spillat; /* clients-per-query */
444 dns_badcache_t ** badcache;
445 unsigned int badcount;
446 unsigned int badhash;
447 unsigned int badsweep;
449 /* Locked by primelock. */
450 dns_fetch_t * primefetch;
451 /* Locked by nlock. */
455 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
456 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
459 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
460 * which we also use as an addrinfo flag.
462 #define FCTX_ADDRINFO_MARK 0x0001
463 #define FCTX_ADDRINFO_FORWARDER 0x1000
464 #define FCTX_ADDRINFO_TRIED 0x2000
465 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
467 #define ISFORWARDER(a) (((a)->flags & \
468 FCTX_ADDRINFO_FORWARDER) != 0)
469 #define TRIED(a) (((a)->flags & \
470 FCTX_ADDRINFO_TRIED) != 0)
472 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
473 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
475 static void destroy(dns_resolver_t *res);
476 static void empty_bucket(dns_resolver_t *res);
477 static isc_result_t resquery_send(resquery_t *query);
478 static void resquery_response(isc_task_t *task, isc_event_t *event);
479 static void resquery_connected(isc_task_t *task, isc_event_t *event);
480 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
481 isc_boolean_t badcache);
482 static void fctx_destroy(fetchctx_t *fctx);
483 static isc_boolean_t fctx_unlink(fetchctx_t *fctx);
484 static isc_result_t ncache_adderesult(dns_message_t *message,
485 dns_db_t *cache, dns_dbnode_t *node,
486 dns_rdatatype_t covers,
487 isc_stdtime_t now, dns_ttl_t maxttl,
488 isc_boolean_t optout,
489 isc_boolean_t secure,
490 dns_rdataset_t *ardataset,
491 isc_result_t *eresultp);
492 static void validated(isc_task_t *task, isc_event_t *event);
493 static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked);
494 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
495 isc_result_t reason, badnstype_t badtype);
496 static inline isc_result_t findnoqname(fetchctx_t *fctx, dns_name_t *name,
497 dns_rdatatype_t type,
498 dns_name_t **noqname);
501 * Increment resolver-related statistics counters.
504 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
505 if (res->view->resstats != NULL)
506 isc_stats_increment(res->view->resstats, counter);
510 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
511 dns_rdatatype_t type, dns_rdataset_t *rdataset,
512 dns_rdataset_t *sigrdataset, unsigned int valoptions,
515 dns_validator_t *validator = NULL;
516 dns_valarg_t *valarg;
519 valarg = isc_mem_get(fctx->mctx, sizeof(*valarg));
521 return (ISC_R_NOMEMORY);
524 valarg->addrinfo = addrinfo;
526 if (!ISC_LIST_EMPTY(fctx->validators))
527 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
529 result = dns_validator_create(fctx->res->view, name, type, rdataset,
530 sigrdataset, fctx->rmessage,
531 valoptions, task, validated, valarg,
533 if (result == ISC_R_SUCCESS) {
534 inc_stats(fctx->res, dns_resstatscounter_val);
535 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
536 INSIST(fctx->validator == NULL);
537 fctx->validator = validator;
539 ISC_LIST_APPEND(fctx->validators, validator, link);
541 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
546 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
547 dns_namereln_t namereln;
548 dns_rdata_rrsig_t rrsig;
549 dns_rdata_t rdata = DNS_RDATA_INIT;
554 for (result = dns_rdataset_first(rdataset);
555 result == ISC_R_SUCCESS;
556 result = dns_rdataset_next(rdataset)) {
557 dns_rdataset_current(rdataset, &rdata);
558 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
559 RUNTIME_CHECK(result == ISC_R_SUCCESS);
560 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
562 if (namereln == dns_namereln_subdomain)
564 dns_rdata_reset(&rdata);
570 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
572 dns_name_t *domain = &fctx->domain;
573 dns_rdataset_t *rdataset;
574 dns_rdatatype_t type;
576 isc_boolean_t keep_auth = ISC_FALSE;
578 if (message->rcode == dns_rcode_nxdomain)
582 * A DS RRset can appear anywhere in a zone, even for a delegation-only
583 * zone. So a response to an explicit query for this type should be
584 * excluded from delegation-only fixup.
586 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
587 * response to a query for these types can never violate the
588 * delegation-only assumption: if the query name is below a
589 * zone cut, the response should normally be a referral, which should
590 * be accepted; if the query name is below a zone cut but the server
591 * happens to have authority for the zone of the query name, the
592 * response is a (non-referral) answer. But this does not violate
593 * delegation-only because the query name must be in a different zone
594 * due to the "apex-only" nature of these types. Note that if the
595 * remote server happens to have authority for a child zone of a
596 * delegation-only zone, we may still incorrectly "fix" the response
597 * with NXDOMAIN for queries for other types. Unfortunately it's
598 * generally impossible to differentiate this case from violation of
599 * the delegation-only assumption. Once the resolver learns the
600 * correct zone cut, possibly via a separate query for an "apex-only"
601 * type, queries for other types will be resolved correctly.
603 * A query for type ANY will be accepted if it hits an exceptional
604 * type above in the answer section as it should be from a child
607 * Also accept answers with RRSIG records from the child zone.
608 * Direct queries for RRSIG records should not be answered from
612 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
613 (fctx->type == dns_rdatatype_ns ||
614 fctx->type == dns_rdatatype_ds ||
615 fctx->type == dns_rdatatype_soa ||
616 fctx->type == dns_rdatatype_any ||
617 fctx->type == dns_rdatatype_rrsig ||
618 fctx->type == dns_rdatatype_dnskey)) {
619 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
620 while (result == ISC_R_SUCCESS) {
622 dns_message_currentname(message, DNS_SECTION_ANSWER,
624 for (rdataset = ISC_LIST_HEAD(name->list);
626 rdataset = ISC_LIST_NEXT(rdataset, link)) {
627 if (!dns_name_equal(name, &fctx->name))
629 type = rdataset->type;
633 if (type == dns_rdatatype_rrsig &&
634 rrsig_fromchildzone(fctx, rdataset))
637 * Direct query for apex records or DS.
639 if (fctx->type == type &&
640 (type == dns_rdatatype_ds ||
641 type == dns_rdatatype_ns ||
642 type == dns_rdatatype_soa ||
643 type == dns_rdatatype_dnskey))
646 * Indirect query for apex records or DS.
648 if (fctx->type == dns_rdatatype_any &&
649 (type == dns_rdatatype_ns ||
650 type == dns_rdatatype_ds ||
651 type == dns_rdatatype_soa ||
652 type == dns_rdatatype_dnskey))
655 result = dns_message_nextname(message,
661 * A NODATA response to a DS query?
663 if (fctx->type == dns_rdatatype_ds &&
664 message->counts[DNS_SECTION_ANSWER] == 0)
667 /* Look for referral or indication of answer from child zone? */
668 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
671 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
672 while (result == ISC_R_SUCCESS) {
674 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
675 for (rdataset = ISC_LIST_HEAD(name->list);
677 rdataset = ISC_LIST_NEXT(rdataset, link)) {
678 type = rdataset->type;
679 if (type == dns_rdatatype_soa &&
680 dns_name_equal(name, domain))
681 keep_auth = ISC_TRUE;
683 if (type != dns_rdatatype_ns &&
684 type != dns_rdatatype_soa &&
685 type != dns_rdatatype_rrsig)
688 if (type == dns_rdatatype_rrsig) {
689 if (rrsig_fromchildzone(fctx, rdataset))
695 /* NS or SOA records. */
696 if (dns_name_equal(name, domain)) {
698 * If a query for ANY causes a negative
699 * response, we can be sure that this is
700 * an empty node. For other type of queries
701 * we cannot differentiate an empty node
702 * from a node that just doesn't have that
703 * type of record. We only accept the former
706 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
707 fctx->type == dns_rdatatype_any)
709 } else if (dns_name_issubdomain(name, domain)) {
710 /* Referral or answer from child zone. */
714 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
718 message->rcode = dns_rcode_nxdomain;
719 message->counts[DNS_SECTION_ANSWER] = 0;
721 message->counts[DNS_SECTION_AUTHORITY] = 0;
722 message->counts[DNS_SECTION_ADDITIONAL] = 0;
726 static inline isc_result_t
727 fctx_starttimer(fetchctx_t *fctx) {
729 * Start the lifetime timer for fctx.
731 * This is also used for stopping the idle timer; in that
732 * case we must purge events already posted to ensure that
733 * no further idle events are delivered.
735 return (isc_timer_reset(fctx->timer, isc_timertype_once,
736 &fctx->expires, NULL, ISC_TRUE));
740 fctx_stoptimer(fetchctx_t *fctx) {
744 * We don't return a result if resetting the timer to inactive fails
745 * since there's nothing to be done about it. Resetting to inactive
746 * should never fail anyway, since the code as currently written
747 * cannot fail in that case.
749 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
750 NULL, NULL, ISC_TRUE);
751 if (result != ISC_R_SUCCESS) {
752 UNEXPECTED_ERROR(__FILE__, __LINE__,
753 "isc_timer_reset(): %s",
754 isc_result_totext(result));
759 static inline isc_result_t
760 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
762 * Start the idle timer for fctx. The lifetime timer continues
765 return (isc_timer_reset(fctx->timer, isc_timertype_once,
766 &fctx->expires, interval, ISC_FALSE));
770 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
771 * we use fctx_stopidletimer for readability in the code below.
773 #define fctx_stopidletimer fctx_starttimer
777 resquery_destroy(resquery_t **queryp) {
780 REQUIRE(queryp != NULL);
782 REQUIRE(!ISC_LINK_LINKED(query, link));
784 INSIST(query->tcpsocket == NULL);
786 query->fctx->nqueries--;
787 if (SHUTTINGDOWN(query->fctx)) {
788 dns_resolver_t *res = query->fctx->res;
789 if (maybe_destroy(query->fctx, ISC_FALSE))
793 isc_mem_put(query->mctx, query, sizeof(*query));
798 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
799 isc_time_t *finish, isc_boolean_t no_response)
803 unsigned int rtt, rttms;
806 dns_adbaddrinfo_t *addrinfo;
807 isc_socket_t *socket;
812 FCTXTRACE("cancelquery");
814 REQUIRE(!RESQUERY_CANCELED(query));
816 query->attributes |= RESQUERY_ATTR_CANCELED;
819 * Should we update the RTT?
821 if (finish != NULL || no_response) {
822 if (finish != NULL) {
824 * We have both the start and finish times for this
825 * packet, so we can compute a real RTT.
827 rtt = (unsigned int)isc_time_microdiff(finish,
829 factor = DNS_ADB_RTTADJDEFAULT;
832 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
834 dns_resstatscounter_queryrtt0);
835 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
837 dns_resstatscounter_queryrtt1);
838 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
840 dns_resstatscounter_queryrtt2);
841 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
843 dns_resstatscounter_queryrtt3);
844 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
846 dns_resstatscounter_queryrtt4);
849 dns_resstatscounter_queryrtt5);
853 * We don't have an RTT for this query. Maybe the
854 * packet was lost, or maybe this server is very
855 * slow. We don't know. Increase the RTT.
858 rtt = query->addrinfo->srtt + 200000;
859 if (rtt > MAX_SINGLE_QUERY_TIMEOUT_US)
860 rtt = MAX_SINGLE_QUERY_TIMEOUT_US;
862 * Replace the current RTT with our value.
864 factor = DNS_ADB_RTTADJREPLACE;
866 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
869 /* Remember that the server has been tried. */
870 if (!TRIED(query->addrinfo)) {
871 dns_adb_changeflags(fctx->adb, query->addrinfo,
872 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
876 * Age RTTs of servers not tried.
878 factor = DNS_ADB_RTTADJAGE;
880 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
882 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
883 if (UNMARKED(addrinfo))
884 dns_adb_adjustsrtt(fctx->adb, addrinfo,
887 if (finish != NULL && TRIEDFIND(fctx))
888 for (find = ISC_LIST_HEAD(fctx->finds);
890 find = ISC_LIST_NEXT(find, publink))
891 for (addrinfo = ISC_LIST_HEAD(find->list);
893 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
894 if (UNMARKED(addrinfo))
895 dns_adb_adjustsrtt(fctx->adb, addrinfo,
898 if (finish != NULL && TRIEDALT(fctx)) {
899 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
901 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
902 if (UNMARKED(addrinfo))
903 dns_adb_adjustsrtt(fctx->adb, addrinfo,
905 for (find = ISC_LIST_HEAD(fctx->altfinds);
907 find = ISC_LIST_NEXT(find, publink))
908 for (addrinfo = ISC_LIST_HEAD(find->list);
910 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
911 if (UNMARKED(addrinfo))
912 dns_adb_adjustsrtt(fctx->adb, addrinfo,
917 * Check for any outstanding socket events. If they exist, cancel
918 * them and let the event handlers finish the cleanup. The resolver
919 * only needs to worry about managing the connect and send events;
920 * the dispatcher manages the recv events.
922 if (RESQUERY_CONNECTING(query)) {
924 * Cancel the connect.
926 if (query->tcpsocket != NULL) {
927 isc_socket_cancel(query->tcpsocket, NULL,
928 ISC_SOCKCANCEL_CONNECT);
929 } else if (query->dispentry != NULL) {
930 INSIST(query->exclusivesocket);
931 socket = dns_dispatch_getentrysocket(query->dispentry);
933 isc_socket_cancel(socket, NULL,
934 ISC_SOCKCANCEL_CONNECT);
936 } else if (RESQUERY_SENDING(query)) {
938 * Cancel the pending send.
940 if (query->exclusivesocket && query->dispentry != NULL)
941 socket = dns_dispatch_getentrysocket(query->dispentry);
943 socket = dns_dispatch_getsocket(query->dispatch);
945 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
948 if (query->dispentry != NULL)
949 dns_dispatch_removeresponse(&query->dispentry, deventp);
951 ISC_LIST_UNLINK(fctx->queries, query, link);
953 if (query->tsig != NULL)
954 isc_buffer_free(&query->tsig);
956 if (query->tsigkey != NULL)
957 dns_tsigkey_detach(&query->tsigkey);
959 if (query->dispatch != NULL)
960 dns_dispatch_detach(&query->dispatch);
962 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
964 * It's safe to destroy the query now.
966 resquery_destroy(&query);
970 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
971 resquery_t *query, *next_query;
973 FCTXTRACE("cancelqueries");
975 for (query = ISC_LIST_HEAD(fctx->queries);
977 query = next_query) {
978 next_query = ISC_LIST_NEXT(query, link);
979 fctx_cancelquery(&query, NULL, NULL, no_response);
984 fctx_cleanupfinds(fetchctx_t *fctx) {
985 dns_adbfind_t *find, *next_find;
987 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
989 for (find = ISC_LIST_HEAD(fctx->finds);
992 next_find = ISC_LIST_NEXT(find, publink);
993 ISC_LIST_UNLINK(fctx->finds, find, publink);
994 dns_adb_destroyfind(&find);
1000 fctx_cleanupaltfinds(fetchctx_t *fctx) {
1001 dns_adbfind_t *find, *next_find;
1003 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1005 for (find = ISC_LIST_HEAD(fctx->altfinds);
1008 next_find = ISC_LIST_NEXT(find, publink);
1009 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
1010 dns_adb_destroyfind(&find);
1012 fctx->altfind = NULL;
1016 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
1017 dns_adbaddrinfo_t *addr, *next_addr;
1019 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1021 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
1024 next_addr = ISC_LIST_NEXT(addr, publink);
1025 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
1026 dns_adb_freeaddrinfo(fctx->adb, &addr);
1031 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
1032 dns_adbaddrinfo_t *addr, *next_addr;
1034 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1036 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
1039 next_addr = ISC_LIST_NEXT(addr, publink);
1040 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1041 dns_adb_freeaddrinfo(fctx->adb, &addr);
1046 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1047 FCTXTRACE("stopeverything");
1048 fctx_cancelqueries(fctx, no_response);
1049 fctx_cleanupfinds(fctx);
1050 fctx_cleanupaltfinds(fctx);
1051 fctx_cleanupforwaddrs(fctx);
1052 fctx_cleanupaltaddrs(fctx);
1053 fctx_stoptimer(fctx);
1057 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1058 dns_fetchevent_t *event, *next_event;
1060 unsigned int count = 0;
1062 isc_boolean_t logit = ISC_FALSE;
1064 unsigned int old_spillat;
1065 unsigned int new_spillat = 0; /* initialized to silence
1066 compiler warnings */
1069 * Caller must be holding the appropriate bucket lock.
1071 REQUIRE(fctx->state == fetchstate_done);
1073 FCTXTRACE("sendevents");
1076 * Keep some record of fetch result for logging later (if required).
1078 fctx->result = result;
1079 fctx->exitline = line;
1081 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1083 for (event = ISC_LIST_HEAD(fctx->events);
1085 event = next_event) {
1086 next_event = ISC_LIST_NEXT(event, ev_link);
1087 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1088 task = event->ev_sender;
1089 event->ev_sender = fctx;
1090 event->vresult = fctx->vresult;
1091 if (!HAVE_ANSWER(fctx))
1092 event->result = result;
1094 INSIST(result != ISC_R_SUCCESS ||
1095 dns_rdataset_isassociated(event->rdataset) ||
1096 fctx->type == dns_rdatatype_any ||
1097 fctx->type == dns_rdatatype_rrsig ||
1098 fctx->type == dns_rdatatype_sig);
1101 * Negative results must be indicated in event->result.
1103 if (dns_rdataset_isassociated(event->rdataset) &&
1104 NEGATIVE(event->rdataset)) {
1105 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1106 event->result == DNS_R_NCACHENXRRSET);
1109 event->qtotal = fctx->totalqueries;
1110 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1114 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1116 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1117 LOCK(&fctx->res->lock);
1118 if (count == fctx->res->spillat && !fctx->res->exiting) {
1119 old_spillat = fctx->res->spillat;
1120 fctx->res->spillat += 5;
1121 if (fctx->res->spillat > fctx->res->spillatmax &&
1122 fctx->res->spillatmax != 0)
1123 fctx->res->spillat = fctx->res->spillatmax;
1124 new_spillat = fctx->res->spillat;
1125 if (new_spillat != old_spillat) {
1128 isc_interval_set(&i, 20 * 60, 0);
1129 result = isc_timer_reset(fctx->res->spillattimer,
1130 isc_timertype_ticker, NULL,
1132 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1134 UNLOCK(&fctx->res->lock);
1136 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1137 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1138 "clients-per-query increased to %u",
1144 log_edns(fetchctx_t *fctx) {
1145 char domainbuf[DNS_NAME_FORMATSIZE];
1147 if (fctx->reason == NULL)
1151 * We do not know if fctx->domain is the actual domain the record
1152 * lives in or a parent domain so we have a '?' after it.
1154 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1155 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1156 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1157 "success resolving '%s' (in '%s'?) after %s",
1158 fctx->info, domainbuf, fctx->reason);
1160 fctx->reason = NULL;
1164 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1165 dns_resolver_t *res;
1166 isc_boolean_t no_response;
1174 if (result == ISC_R_SUCCESS) {
1176 * Log any deferred EDNS timeout messages.
1179 no_response = ISC_TRUE;
1181 no_response = ISC_FALSE;
1183 fctx->reason = NULL;
1184 fctx_stopeverything(fctx, no_response);
1186 LOCK(&res->buckets[fctx->bucketnum].lock);
1188 fctx->state = fetchstate_done;
1189 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1190 fctx_sendevents(fctx, result, line);
1192 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1196 process_sendevent(resquery_t *query, isc_event_t *event) {
1197 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1198 isc_boolean_t retry = ISC_FALSE;
1199 isc_result_t result;
1204 if (RESQUERY_CANCELED(query)) {
1205 if (query->sends == 0 && query->connects == 0) {
1207 * This query was canceled while the
1208 * isc_socket_sendto/connect() was in progress.
1210 if (query->tcpsocket != NULL)
1211 isc_socket_detach(&query->tcpsocket);
1212 resquery_destroy(&query);
1215 switch (sevent->result) {
1219 case ISC_R_HOSTUNREACH:
1220 case ISC_R_NETUNREACH:
1222 case ISC_R_ADDRNOTAVAIL:
1223 case ISC_R_CONNREFUSED:
1226 * No route to remote.
1228 add_bad(fctx, query->addrinfo, sevent->result,
1230 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1235 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1240 if (event->ev_type == ISC_SOCKEVENT_CONNECT)
1241 isc_event_free(&event);
1245 * Behave as if the idle timer has expired. For TCP
1246 * this may not actually reflect the latest timer.
1248 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1249 result = fctx_stopidletimer(fctx);
1250 if (result != ISC_R_SUCCESS)
1251 fctx_done(fctx, result, __LINE__);
1253 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1258 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1259 resquery_t *query = event->ev_arg;
1261 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1263 QTRACE("udpconnected");
1267 INSIST(RESQUERY_CONNECTING(query));
1271 process_sendevent(query, event);
1275 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1276 resquery_t *query = event->ev_arg;
1278 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1285 * Currently we don't wait for the senddone event before retrying
1286 * a query. This means that if we get really behind, we may end
1287 * up doing extra work!
1292 INSIST(RESQUERY_SENDING(query));
1296 process_sendevent(query, event);
1299 static inline isc_result_t
1300 fctx_addopt(dns_message_t *message, unsigned int version,
1301 isc_uint16_t udpsize, dns_ednsopt_t *ednsopts, size_t count)
1303 dns_rdataset_t *rdataset = NULL;
1304 isc_result_t result;
1306 result = dns_message_buildopt(message, &rdataset, version, udpsize,
1307 DNS_MESSAGEEXTFLAG_DO, ednsopts, count);
1308 if (result != ISC_R_SUCCESS)
1310 return (dns_message_setopt(message, rdataset));
1314 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1315 unsigned int seconds;
1319 * We retry every .8 seconds the first two times through the address
1320 * list, and then we do exponential back-off.
1322 if (fctx->restarts < 3)
1325 us = (800000 << (fctx->restarts - 2));
1328 * Add a fudge factor to the expected rtt based on the current
1333 else if (rtt < 100000)
1339 * Always wait for at least the expected rtt.
1345 * But don't ever wait for more than 10 seconds.
1347 if (us > MAX_SINGLE_QUERY_TIMEOUT_US)
1348 us = MAX_SINGLE_QUERY_TIMEOUT_US;
1350 seconds = us / US_PER_SEC;
1351 us -= seconds * US_PER_SEC;
1352 isc_interval_set(&fctx->interval, seconds, us * 1000);
1356 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1357 unsigned int options)
1359 dns_resolver_t *res;
1361 isc_result_t result;
1363 isc_sockaddr_t addr;
1364 isc_boolean_t have_addr = ISC_FALSE;
1370 task = res->buckets[fctx->bucketnum].task;
1372 srtt = addrinfo->srtt;
1375 * A forwarder needs to make multiple queries. Give it at least
1376 * a second to do these in.
1378 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1381 fctx_setretryinterval(fctx, srtt);
1382 result = fctx_startidletimer(fctx, &fctx->interval);
1383 if (result != ISC_R_SUCCESS)
1386 INSIST(ISC_LIST_EMPTY(fctx->validators));
1388 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1390 query = isc_mem_get(fctx->mctx, sizeof(*query));
1391 if (query == NULL) {
1392 result = ISC_R_NOMEMORY;
1393 goto stop_idle_timer;
1395 query->mctx = fctx->mctx;
1396 query->options = options;
1397 query->attributes = 0;
1399 query->connects = 0;
1401 * Note that the caller MUST guarantee that 'addrinfo' will remain
1402 * valid until this query is canceled.
1404 query->addrinfo = addrinfo;
1405 TIME_NOW(&query->start);
1408 * If this is a TCP query, then we need to make a socket and
1409 * a dispatch for it here. Otherwise we use the resolver's
1412 query->dispatchmgr = res->dispatchmgr;
1413 query->dispatch = NULL;
1414 query->exclusivesocket = ISC_FALSE;
1415 query->tcpsocket = NULL;
1416 if (res->view->peers != NULL) {
1417 dns_peer_t *peer = NULL;
1418 isc_netaddr_t dstip;
1419 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1420 result = dns_peerlist_peerbyaddr(res->view->peers,
1422 if (result == ISC_R_SUCCESS) {
1423 result = dns_peer_getquerysource(peer, &addr);
1424 if (result == ISC_R_SUCCESS)
1425 have_addr = ISC_TRUE;
1429 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1432 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1436 result = dns_dispatch_getlocaladdress(
1437 res->dispatches4->dispatches[0],
1441 result = dns_dispatch_getlocaladdress(
1442 res->dispatches6->dispatches[0],
1446 result = ISC_R_NOTIMPLEMENTED;
1449 if (result != ISC_R_SUCCESS)
1452 isc_sockaddr_setport(&addr, 0);
1454 result = isc_socket_create(res->socketmgr, pf,
1457 if (result != ISC_R_SUCCESS)
1460 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1461 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1462 if (result != ISC_R_SUCCESS)
1463 goto cleanup_socket;
1467 * A dispatch will be created once the connect succeeds.
1471 unsigned int attrs, attrmask;
1472 attrs = DNS_DISPATCHATTR_UDP;
1473 switch (isc_sockaddr_pf(&addr)) {
1475 attrs |= DNS_DISPATCHATTR_IPV4;
1478 attrs |= DNS_DISPATCHATTR_IPV6;
1481 result = ISC_R_NOTIMPLEMENTED;
1484 attrmask = DNS_DISPATCHATTR_UDP;
1485 attrmask |= DNS_DISPATCHATTR_TCP;
1486 attrmask |= DNS_DISPATCHATTR_IPV4;
1487 attrmask |= DNS_DISPATCHATTR_IPV6;
1488 result = dns_dispatch_getudp(res->dispatchmgr,
1490 res->taskmgr, &addr,
1491 4096, 1000, 32768, 16411,
1492 16433, attrs, attrmask,
1494 if (result != ISC_R_SUCCESS)
1497 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1499 dns_dispatch_attach(
1500 dns_resolver_dispatchv4(res),
1502 query->exclusivesocket = res->exclusivev4;
1505 dns_dispatch_attach(
1506 dns_resolver_dispatchv6(res),
1508 query->exclusivesocket = res->exclusivev6;
1511 result = ISC_R_NOTIMPLEMENTED;
1516 * We should always have a valid dispatcher here. If we
1517 * don't support a protocol family, then its dispatcher
1518 * will be NULL, but we shouldn't be finding addresses for
1519 * protocol types we don't support, so the dispatcher
1520 * we found should never be NULL.
1522 INSIST(query->dispatch != NULL);
1525 query->dispentry = NULL;
1528 query->tsigkey = NULL;
1529 ISC_LINK_INIT(query, link);
1530 query->magic = QUERY_MAGIC;
1532 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1534 * Connect to the remote server.
1536 * XXXRTH Should we attach to the socket?
1538 result = isc_socket_connect(query->tcpsocket,
1539 &addrinfo->sockaddr, task,
1540 resquery_connected, query);
1541 if (result != ISC_R_SUCCESS)
1542 goto cleanup_socket;
1544 QTRACE("connecting via TCP");
1546 result = resquery_send(query);
1547 if (result != ISC_R_SUCCESS)
1548 goto cleanup_dispatch;
1552 fctx->totalqueries++;
1554 ISC_LIST_APPEND(fctx->queries, query, link);
1555 query->fctx->nqueries++;
1556 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1557 inc_stats(res, dns_resstatscounter_queryv4);
1559 inc_stats(res, dns_resstatscounter_queryv6);
1560 if (res->view->resquerystats != NULL)
1561 dns_rdatatypestats_increment(res->view->resquerystats,
1564 return (ISC_R_SUCCESS);
1567 isc_socket_detach(&query->tcpsocket);
1570 if (query->dispatch != NULL)
1571 dns_dispatch_detach(&query->dispatch);
1574 if (query->connects == 0) {
1576 isc_mem_put(fctx->mctx, query, sizeof(*query));
1580 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1585 static isc_boolean_t
1586 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1589 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1591 sa = ISC_LIST_NEXT(sa, link)) {
1592 if (isc_sockaddr_equal(sa, address))
1600 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1603 if (bad_edns(fctx, address))
1606 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1611 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1614 static isc_boolean_t
1615 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1618 for (sa = ISC_LIST_HEAD(fctx->edns);
1620 sa = ISC_LIST_NEXT(sa, link)) {
1621 if (isc_sockaddr_equal(sa, address))
1629 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1632 if (triededns(fctx, address))
1635 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1640 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1643 static isc_boolean_t
1644 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1647 for (sa = ISC_LIST_HEAD(fctx->edns512);
1649 sa = ISC_LIST_NEXT(sa, link)) {
1650 if (isc_sockaddr_equal(sa, address))
1658 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1661 if (triededns512(fctx, address))
1664 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1669 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1673 resquery_send(resquery_t *query) {
1675 isc_result_t result;
1676 dns_name_t *qname = NULL;
1677 dns_rdataset_t *qrdataset = NULL;
1679 dns_resolver_t *res;
1681 isc_socket_t *socket;
1682 isc_buffer_t tcpbuffer;
1683 isc_sockaddr_t *address;
1684 isc_buffer_t *buffer;
1685 isc_netaddr_t ipaddr;
1686 dns_tsigkey_t *tsigkey = NULL;
1687 dns_peer_t *peer = NULL;
1688 isc_boolean_t useedns;
1689 dns_compress_t cctx;
1690 isc_boolean_t cleanup_cctx = ISC_FALSE;
1691 isc_boolean_t secure_domain;
1692 isc_boolean_t connecting = ISC_FALSE;
1693 dns_ednsopt_t ednsopts[EDNSOPTS];
1694 unsigned ednsopt = 0;
1700 task = res->buckets[fctx->bucketnum].task;
1703 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1705 * Reserve space for the TCP message length.
1707 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1708 isc_buffer_init(&query->buffer, query->data + 2,
1709 sizeof(query->data) - 2);
1710 buffer = &tcpbuffer;
1712 isc_buffer_init(&query->buffer, query->data,
1713 sizeof(query->data));
1714 buffer = &query->buffer;
1717 result = dns_message_gettempname(fctx->qmessage, &qname);
1718 if (result != ISC_R_SUCCESS)
1720 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1721 if (result != ISC_R_SUCCESS)
1725 * Get a query id from the dispatch.
1727 result = dns_dispatch_addresponse2(query->dispatch,
1728 &query->addrinfo->sockaddr,
1735 if (result != ISC_R_SUCCESS)
1738 fctx->qmessage->opcode = dns_opcode_query;
1743 dns_name_init(qname, NULL);
1744 dns_name_clone(&fctx->name, qname);
1745 dns_rdataset_init(qrdataset);
1746 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1747 ISC_LIST_APPEND(qname->list, qrdataset, link);
1748 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1753 * Set RD if the client has requested that we do a recursive query,
1754 * or if we're sending to a forwarder.
1756 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1757 ISFORWARDER(query->addrinfo))
1758 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1761 * Set CD if the client says don't validate or the question is
1762 * under a secure entry point.
1764 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1765 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1766 } else if (res->view->enablevalidation) {
1767 result = dns_view_issecuredomain(res->view, &fctx->name,
1769 if (result != ISC_R_SUCCESS)
1770 secure_domain = ISC_FALSE;
1771 if (res->view->dlv != NULL)
1772 secure_domain = ISC_TRUE;
1774 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1778 * We don't have to set opcode because it defaults to query.
1780 fctx->qmessage->id = query->id;
1783 * Convert the question to wire format.
1785 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1786 if (result != ISC_R_SUCCESS)
1787 goto cleanup_message;
1788 cleanup_cctx = ISC_TRUE;
1790 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1792 if (result != ISC_R_SUCCESS)
1793 goto cleanup_message;
1795 result = dns_message_rendersection(fctx->qmessage,
1796 DNS_SECTION_QUESTION, 0);
1797 if (result != ISC_R_SUCCESS)
1798 goto cleanup_message;
1801 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1802 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1805 * The ADB does not know about servers with "edns no". Check this,
1806 * and then inform the ADB for future use.
1808 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1810 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1813 query->options |= DNS_FETCHOPT_NOEDNS0;
1814 dns_adb_changeflags(fctx->adb, query->addrinfo,
1815 DNS_FETCHOPT_NOEDNS0,
1816 DNS_FETCHOPT_NOEDNS0);
1819 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1820 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1821 query->options |= DNS_FETCHOPT_NOEDNS0;
1824 * Handle timeouts by reducing the UDP response size to 512 bytes
1825 * then if that doesn't work disabling EDNS (includes DO) and CD.
1827 * These timeout can be due to:
1828 * * broken nameservers that don't respond to EDNS queries.
1829 * * broken/misconfigured firewalls and NAT implementations
1830 * that don't handle IP fragmentation.
1831 * * broken/misconfigured firewalls that don't handle responses
1832 * greater than 512 bytes.
1833 * * broken/misconfigured firewalls that don't handle EDNS, DO
1835 * * packet loss / link outage.
1837 if (fctx->timeout) {
1838 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1839 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1840 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1841 query->options |= DNS_FETCHOPT_NOEDNS0;
1842 fctx->reason = "disabling EDNS";
1843 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1844 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1845 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1846 query->options |= DNS_FETCHOPT_EDNS512;
1847 fctx->reason = "reducing the advertised EDNS UDP "
1848 "packet size to 512 octets";
1850 fctx->timeout = ISC_FALSE;
1854 * Use EDNS0, unless the caller doesn't want it, or we know that
1855 * the remote server doesn't like it.
1857 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1858 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1859 unsigned int version = 0; /* Default version. */
1861 isc_uint16_t udpsize = res->udpsize;
1862 isc_boolean_t reqnsid = res->view->requestnsid;
1864 flags = query->addrinfo->flags;
1865 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1866 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1867 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1869 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1871 else if (peer != NULL)
1872 (void)dns_peer_getudpsize(peer, &udpsize);
1874 /* request NSID for current view or peer? */
1876 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1878 INSIST(ednsopt < EDNSOPTS);
1879 ednsopts[ednsopt].code = DNS_OPT_NSID;
1880 ednsopts[ednsopt].length = 0;
1881 ednsopts[ednsopt].value = NULL;
1884 result = fctx_addopt(fctx->qmessage, version,
1885 udpsize, ednsopts, ednsopt);
1886 if (reqnsid && result == ISC_R_SUCCESS) {
1887 query->options |= DNS_FETCHOPT_WANTNSID;
1888 } else if (result != ISC_R_SUCCESS) {
1890 * We couldn't add the OPT, but we'll press on.
1891 * We're not using EDNS0, so set the NOEDNS0
1894 query->options |= DNS_FETCHOPT_NOEDNS0;
1898 * We know this server doesn't like EDNS0, so we
1899 * won't use it. Set the NOEDNS0 bit since we're
1902 query->options |= DNS_FETCHOPT_NOEDNS0;
1907 * If we need EDNS0 to do this query and aren't using it, we lose.
1909 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1910 result = DNS_R_SERVFAIL;
1911 goto cleanup_message;
1914 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1915 add_triededns(fctx, &query->addrinfo->sockaddr);
1917 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1918 add_triededns512(fctx, &query->addrinfo->sockaddr);
1921 * Clear CD if EDNS is not in use.
1923 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1924 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1927 * Add TSIG record tailored to the current recipient.
1929 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1930 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1931 goto cleanup_message;
1933 if (tsigkey != NULL) {
1934 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1935 dns_tsigkey_detach(&tsigkey);
1936 if (result != ISC_R_SUCCESS)
1937 goto cleanup_message;
1940 result = dns_message_rendersection(fctx->qmessage,
1941 DNS_SECTION_ADDITIONAL, 0);
1942 if (result != ISC_R_SUCCESS)
1943 goto cleanup_message;
1945 result = dns_message_renderend(fctx->qmessage);
1946 if (result != ISC_R_SUCCESS)
1947 goto cleanup_message;
1949 dns_compress_invalidate(&cctx);
1950 cleanup_cctx = ISC_FALSE;
1952 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1953 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1955 result = dns_message_getquerytsig(fctx->qmessage,
1958 if (result != ISC_R_SUCCESS)
1959 goto cleanup_message;
1963 * If using TCP, write the length of the message at the beginning
1966 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1967 isc_buffer_usedregion(&query->buffer, &r);
1968 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1969 isc_buffer_add(&tcpbuffer, r.length);
1973 * We're now done with the query message.
1975 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1977 if (query->exclusivesocket)
1978 socket = dns_dispatch_getentrysocket(query->dispentry);
1980 socket = dns_dispatch_getsocket(query->dispatch);
1984 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1985 address = &query->addrinfo->sockaddr;
1986 if (query->exclusivesocket) {
1987 result = isc_socket_connect(socket, address, task,
1988 resquery_udpconnected,
1990 if (result != ISC_R_SUCCESS)
1991 goto cleanup_message;
1992 connecting = ISC_TRUE;
1996 isc_buffer_usedregion(buffer, &r);
1999 * XXXRTH Make sure we don't send to ourselves! We should probably
2000 * prune out these addresses when we get them from the ADB.
2002 ISC_EVENT_INIT(&query->sendevent, sizeof(query->sendevent), 0, NULL,
2003 ISC_SOCKEVENT_SENDDONE, resquery_senddone, query,
2005 result = isc_socket_sendto2(socket, &r, task, address, NULL,
2006 &query->sendevent, 0);
2007 if (result != ISC_R_SUCCESS) {
2010 * This query is still connecting.
2011 * Mark it as canceled so that it will just be
2012 * cleaned up when the connected event is received.
2013 * Keep fctx around until the event is processed.
2015 query->fctx->nqueries++;
2016 query->attributes |= RESQUERY_ATTR_CANCELED;
2018 goto cleanup_message;
2025 return (ISC_R_SUCCESS);
2029 dns_compress_invalidate(&cctx);
2031 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2034 * Stop the dispatcher from listening.
2036 dns_dispatch_removeresponse(&query->dispentry, NULL);
2040 dns_message_puttempname(fctx->qmessage, &qname);
2041 if (qrdataset != NULL)
2042 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2048 resquery_connected(isc_task_t *task, isc_event_t *event) {
2049 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2050 resquery_t *query = event->ev_arg;
2051 isc_boolean_t retry = ISC_FALSE;
2052 isc_interval_t interval;
2053 isc_result_t result;
2057 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2058 REQUIRE(VALID_QUERY(query));
2060 QTRACE("connected");
2067 * Currently we don't wait for the connect event before retrying
2068 * a query. This means that if we get really behind, we may end
2069 * up doing extra work!
2075 if (RESQUERY_CANCELED(query)) {
2077 * This query was canceled while the connect() was in
2080 isc_socket_detach(&query->tcpsocket);
2081 resquery_destroy(&query);
2083 switch (sevent->result) {
2087 * Extend the idle timer for TCP. 20 seconds
2088 * should be long enough for a TCP connection to be
2089 * established, a single DNS request to be sent,
2090 * and the response received.
2092 isc_interval_set(&interval, 20, 0);
2093 result = fctx_startidletimer(query->fctx, &interval);
2094 if (result != ISC_R_SUCCESS) {
2095 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2096 fctx_done(fctx, result, __LINE__);
2100 * We are connected. Create a dispatcher and
2104 attrs |= DNS_DISPATCHATTR_TCP;
2105 attrs |= DNS_DISPATCHATTR_PRIVATE;
2106 attrs |= DNS_DISPATCHATTR_CONNECTED;
2107 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2109 attrs |= DNS_DISPATCHATTR_IPV4;
2111 attrs |= DNS_DISPATCHATTR_IPV6;
2112 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2114 result = dns_dispatch_createtcp(query->dispatchmgr,
2116 query->fctx->res->taskmgr,
2117 4096, 2, 1, 1, 3, attrs,
2121 * Regardless of whether dns_dispatch_create()
2122 * succeeded or not, we don't need our reference
2123 * to the socket anymore.
2125 isc_socket_detach(&query->tcpsocket);
2127 if (result == ISC_R_SUCCESS)
2128 result = resquery_send(query);
2130 if (result != ISC_R_SUCCESS) {
2131 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2132 fctx_done(fctx, result, __LINE__);
2136 case ISC_R_NETUNREACH:
2137 case ISC_R_HOSTUNREACH:
2138 case ISC_R_CONNREFUSED:
2140 case ISC_R_ADDRNOTAVAIL:
2141 case ISC_R_CONNECTIONRESET:
2143 * No route to remote.
2145 isc_socket_detach(&query->tcpsocket);
2146 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2151 isc_socket_detach(&query->tcpsocket);
2152 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2157 isc_event_free(&event);
2161 * Behave as if the idle timer has expired. For TCP
2162 * connections this may not actually reflect the latest timer.
2164 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2165 result = fctx_stopidletimer(fctx);
2166 if (result != ISC_R_SUCCESS)
2167 fctx_done(fctx, result, __LINE__);
2169 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2174 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2176 dns_adbfind_t *find;
2177 dns_resolver_t *res;
2178 isc_boolean_t want_try = ISC_FALSE;
2179 isc_boolean_t want_done = ISC_FALSE;
2180 isc_boolean_t bucket_empty = ISC_FALSE;
2181 unsigned int bucketnum;
2182 isc_boolean_t destroy = ISC_FALSE;
2184 find = event->ev_sender;
2185 fctx = event->ev_arg;
2186 REQUIRE(VALID_FCTX(fctx));
2191 FCTXTRACE("finddone");
2193 bucketnum = fctx->bucketnum;
2194 LOCK(&res->buckets[bucketnum].lock);
2196 INSIST(fctx->pending > 0);
2199 if (ADDRWAIT(fctx)) {
2201 * The fetch is waiting for a name to be found.
2203 INSIST(!SHUTTINGDOWN(fctx));
2204 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2205 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES) {
2206 want_try = ISC_TRUE;
2207 fctx->totalqueries += find->qtotal;
2210 if (fctx->pending == 0) {
2212 * We've got nothing else to wait for and don't
2213 * know the answer. There's nothing to do but
2216 want_done = ISC_TRUE;
2219 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2220 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2222 if (fctx->references == 0) {
2223 bucket_empty = fctx_unlink(fctx);
2227 UNLOCK(&res->buckets[bucketnum].lock);
2229 isc_event_free(&event);
2230 dns_adb_destroyfind(&find);
2233 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2235 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2244 static inline isc_boolean_t
2245 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2248 for (sa = ISC_LIST_HEAD(fctx->bad);
2250 sa = ISC_LIST_NEXT(sa, link)) {
2251 if (isc_sockaddr_equal(sa, address))
2258 static inline isc_boolean_t
2259 mark_bad(fetchctx_t *fctx) {
2260 dns_adbfind_t *curr;
2261 dns_adbaddrinfo_t *addrinfo;
2262 isc_boolean_t all_bad = ISC_TRUE;
2265 * Mark all known bad servers, so we don't try to talk to them
2270 * Mark any bad nameservers.
2272 for (curr = ISC_LIST_HEAD(fctx->finds);
2274 curr = ISC_LIST_NEXT(curr, publink)) {
2275 for (addrinfo = ISC_LIST_HEAD(curr->list);
2277 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2278 if (bad_server(fctx, &addrinfo->sockaddr))
2279 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2281 all_bad = ISC_FALSE;
2286 * Mark any bad forwarders.
2288 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2290 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2291 if (bad_server(fctx, &addrinfo->sockaddr))
2292 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2294 all_bad = ISC_FALSE;
2298 * Mark any bad alternates.
2300 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2302 curr = ISC_LIST_NEXT(curr, publink)) {
2303 for (addrinfo = ISC_LIST_HEAD(curr->list);
2305 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2306 if (bad_server(fctx, &addrinfo->sockaddr))
2307 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2309 all_bad = ISC_FALSE;
2313 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2315 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2316 if (bad_server(fctx, &addrinfo->sockaddr))
2317 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2319 all_bad = ISC_FALSE;
2326 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2327 badnstype_t badtype)
2329 char namebuf[DNS_NAME_FORMATSIZE];
2330 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2336 const char *spc = "";
2337 isc_sockaddr_t *address = &addrinfo->sockaddr;
2339 if (reason == DNS_R_LAME)
2343 case badns_unreachable:
2346 case badns_response:
2349 case badns_validation:
2350 break; /* counted as 'valfail' */
2354 if (bad_server(fctx, address)) {
2356 * We already know this server is bad.
2361 FCTXTRACE("add_bad");
2363 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
2367 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2369 if (reason == DNS_R_LAME) /* already logged */
2372 if (reason == DNS_R_UNEXPECTEDRCODE &&
2373 fctx->rmessage->rcode == dns_rcode_servfail &&
2374 ISFORWARDER(addrinfo))
2377 if (reason == DNS_R_UNEXPECTEDRCODE) {
2378 isc_buffer_init(&b, code, sizeof(code) - 1);
2379 dns_rcode_totext(fctx->rmessage->rcode, &b);
2380 code[isc_buffer_usedlength(&b)] = '\0';
2382 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2383 isc_buffer_init(&b, code, sizeof(code) - 1);
2384 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2385 code[isc_buffer_usedlength(&b)] = '\0';
2390 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2391 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2392 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2393 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2394 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2395 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2396 "error (%s%s%s) resolving '%s/%s/%s': %s",
2397 dns_result_totext(reason), spc, code,
2398 namebuf, typebuf, classbuf, addrbuf);
2402 * Sort addrinfo list by RTT.
2405 sort_adbfind(dns_adbfind_t *find) {
2406 dns_adbaddrinfo_t *best, *curr;
2407 dns_adbaddrinfolist_t sorted;
2409 /* Lame N^2 bubble sort. */
2410 ISC_LIST_INIT(sorted);
2411 while (!ISC_LIST_EMPTY(find->list)) {
2412 best = ISC_LIST_HEAD(find->list);
2413 curr = ISC_LIST_NEXT(best, publink);
2414 while (curr != NULL) {
2415 if (curr->srtt < best->srtt)
2417 curr = ISC_LIST_NEXT(curr, publink);
2419 ISC_LIST_UNLINK(find->list, best, publink);
2420 ISC_LIST_APPEND(sorted, best, publink);
2422 find->list = sorted;
2426 * Sort a list of finds by server RTT.
2429 sort_finds(dns_adbfindlist_t *findlist) {
2430 dns_adbfind_t *best, *curr;
2431 dns_adbfindlist_t sorted;
2432 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2434 /* Sort each find's addrinfo list by SRTT. */
2435 for (curr = ISC_LIST_HEAD(*findlist);
2437 curr = ISC_LIST_NEXT(curr, publink))
2440 /* Lame N^2 bubble sort. */
2441 ISC_LIST_INIT(sorted);
2442 while (!ISC_LIST_EMPTY(*findlist)) {
2443 best = ISC_LIST_HEAD(*findlist);
2444 bestaddrinfo = ISC_LIST_HEAD(best->list);
2445 INSIST(bestaddrinfo != NULL);
2446 curr = ISC_LIST_NEXT(best, publink);
2447 while (curr != NULL) {
2448 addrinfo = ISC_LIST_HEAD(curr->list);
2449 INSIST(addrinfo != NULL);
2450 if (addrinfo->srtt < bestaddrinfo->srtt) {
2452 bestaddrinfo = addrinfo;
2454 curr = ISC_LIST_NEXT(curr, publink);
2456 ISC_LIST_UNLINK(*findlist, best, publink);
2457 ISC_LIST_APPEND(sorted, best, publink);
2463 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2464 unsigned int options, unsigned int flags, isc_stdtime_t now,
2465 isc_boolean_t *need_alternate)
2467 dns_adbaddrinfo_t *ai;
2468 dns_adbfind_t *find;
2469 dns_resolver_t *res;
2470 isc_boolean_t unshared;
2471 isc_result_t result;
2474 unshared = ISC_TF((fctx->options & DNS_FETCHOPT_UNSHARED) != 0);
2476 * If this name is a subdomain of the query domain, tell
2477 * the ADB to start looking using zone/hint data. This keeps us
2478 * from getting stuck if the nameserver is beneath the zone cut
2479 * and we don't know its address (e.g. because the A record has
2482 if (dns_name_issubdomain(name, &fctx->domain))
2483 options |= DNS_ADBFIND_STARTATZONE;
2484 options |= DNS_ADBFIND_GLUEOK;
2485 options |= DNS_ADBFIND_HINTOK;
2488 * See what we know about this address.
2491 result = dns_adb_createfind2(fctx->adb,
2492 res->buckets[fctx->bucketnum].task,
2493 fctx_finddone, fctx, name,
2494 &fctx->name, fctx->type,
2497 fctx->depth + 1, &find);
2498 if (result != ISC_R_SUCCESS) {
2499 if (result == DNS_R_ALIAS) {
2501 * XXXRTH Follow the CNAME/DNAME chain?
2503 dns_adb_destroyfind(&find);
2506 } else if (!ISC_LIST_EMPTY(find->list)) {
2508 * We have at least some of the addresses for the
2511 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2512 if (flags != 0 || port != 0) {
2513 for (ai = ISC_LIST_HEAD(find->list);
2515 ai = ISC_LIST_NEXT(ai, publink)) {
2518 isc_sockaddr_setport(&ai->sockaddr,
2522 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2523 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2525 ISC_LIST_APPEND(fctx->finds, find, publink);
2528 * We don't know any of the addresses for this
2531 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2533 * We're looking for them and will get an
2534 * event about it later.
2540 if (need_alternate != NULL &&
2541 !*need_alternate && unshared &&
2542 ((res->dispatches4 == NULL &&
2543 find->result_v6 != DNS_R_NXDOMAIN) ||
2544 (res->dispatches6 == NULL &&
2545 find->result_v4 != DNS_R_NXDOMAIN)))
2546 *need_alternate = ISC_TRUE;
2548 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2549 fctx->lamecount++; /* cached lame server */
2551 fctx->adberr++; /* unreachable server, etc. */
2554 * If we know there are no addresses for
2555 * the family we are using then try to add
2556 * an alternative server.
2558 if (need_alternate != NULL && !*need_alternate &&
2559 ((res->dispatches4 == NULL &&
2560 find->result_v6 == DNS_R_NXRRSET) ||
2561 (res->dispatches6 == NULL &&
2562 find->result_v4 == DNS_R_NXRRSET)))
2563 *need_alternate = ISC_TRUE;
2564 dns_adb_destroyfind(&find);
2569 static isc_boolean_t
2570 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2572 unsigned int nlabels;
2573 dns_namereln_t namereln;
2575 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2576 return (ISC_TF(namereln == dns_namereln_subdomain));
2580 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2581 dns_rdata_t rdata = DNS_RDATA_INIT;
2582 isc_result_t result;
2583 dns_resolver_t *res;
2585 unsigned int stdoptions = 0;
2587 dns_adbaddrinfo_t *ai;
2588 isc_boolean_t all_bad;
2590 isc_boolean_t need_alternate = ISC_FALSE;
2592 FCTXTRACE("getaddresses");
2595 * Don't pound on remote servers. (Failsafe!)
2598 if (fctx->restarts > 10) {
2599 FCTXTRACE("too many restarts");
2600 return (DNS_R_SERVFAIL);
2605 if (fctx->depth > res->maxdepth) {
2606 FCTXTRACE("too much NS indirection");
2607 return (DNS_R_SERVFAIL);
2614 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2615 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2618 * If this fctx has forwarders, use them; otherwise use any
2619 * selective forwarders specified in the view; otherwise use the
2620 * resolver's forwarders (if any).
2622 sa = ISC_LIST_HEAD(fctx->forwarders);
2624 dns_forwarders_t *forwarders = NULL;
2625 dns_name_t *name = &fctx->name;
2627 unsigned int labels;
2628 dns_fixedname_t fixed;
2632 * DS records are found in the parent server.
2633 * Strip label to get the correct forwarder (if any).
2635 if (dns_rdatatype_atparent(fctx->type) &&
2636 dns_name_countlabels(name) > 1) {
2637 dns_name_init(&suffix, NULL);
2638 labels = dns_name_countlabels(name);
2639 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2643 dns_fixedname_init(&fixed);
2644 domain = dns_fixedname_name(&fixed);
2645 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2646 domain, &forwarders);
2647 if (result == ISC_R_SUCCESS) {
2648 sa = ISC_LIST_HEAD(forwarders->addrs);
2649 fctx->fwdpolicy = forwarders->fwdpolicy;
2650 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2651 isstrictsubdomain(domain, &fctx->domain)) {
2652 dns_name_free(&fctx->domain, fctx->mctx);
2653 dns_name_init(&fctx->domain, NULL);
2654 result = dns_name_dup(domain, fctx->mctx,
2656 if (result != ISC_R_SUCCESS)
2662 while (sa != NULL) {
2663 if ((isc_sockaddr_pf(sa) == AF_INET &&
2664 fctx->res->dispatches4 == NULL) ||
2665 (isc_sockaddr_pf(sa) == AF_INET6 &&
2666 fctx->res->dispatches6 == NULL)) {
2667 sa = ISC_LIST_NEXT(sa, link);
2671 result = dns_adb_findaddrinfo(fctx->adb,
2672 sa, &ai, 0); /* XXXMLG */
2673 if (result == ISC_R_SUCCESS) {
2674 dns_adbaddrinfo_t *cur;
2675 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2676 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2677 while (cur != NULL && cur->srtt < ai->srtt)
2678 cur = ISC_LIST_NEXT(cur, publink);
2680 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2683 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2685 sa = ISC_LIST_NEXT(sa, link);
2689 * If the forwarding policy is "only", we don't need the addresses
2690 * of the nameservers.
2692 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2696 * Normal nameservers.
2699 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2700 if (fctx->restarts == 1) {
2702 * To avoid sending out a flood of queries likely to
2703 * result in NXRRSET, we suppress fetches for address
2704 * families we don't have the first time through,
2705 * provided that we have addresses in some family we
2708 * We don't want to set this option all the time, since
2709 * if fctx->restarts > 1, we've clearly been having trouble
2710 * with the addresses we had, so getting more could help.
2712 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2714 if (res->dispatches4 != NULL)
2715 stdoptions |= DNS_ADBFIND_INET;
2716 if (res->dispatches6 != NULL)
2717 stdoptions |= DNS_ADBFIND_INET6;
2718 isc_stdtime_get(&now);
2720 INSIST(ISC_LIST_EMPTY(fctx->finds));
2721 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2723 for (result = dns_rdataset_first(&fctx->nameservers);
2724 result == ISC_R_SUCCESS;
2725 result = dns_rdataset_next(&fctx->nameservers))
2727 dns_rdataset_current(&fctx->nameservers, &rdata);
2729 * Extract the name from the NS record.
2731 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2732 if (result != ISC_R_SUCCESS)
2735 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2737 dns_rdata_reset(&rdata);
2738 dns_rdata_freestruct(&ns);
2740 if (result != ISC_R_NOMORE)
2744 * Do we need to use 6 to 4?
2746 if (need_alternate) {
2749 family = (res->dispatches6 != NULL) ? AF_INET6 : AF_INET;
2750 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2752 a = ISC_LIST_NEXT(a, link)) {
2753 if (!a->isaddress) {
2754 findname(fctx, &a->_u._n.name, a->_u._n.port,
2755 stdoptions, FCTX_ADDRINFO_FORWARDER,
2759 if (isc_sockaddr_pf(&a->_u.addr) != family)
2762 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2764 if (result == ISC_R_SUCCESS) {
2765 dns_adbaddrinfo_t *cur;
2766 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2767 cur = ISC_LIST_HEAD(fctx->altaddrs);
2768 while (cur != NULL && cur->srtt < ai->srtt)
2769 cur = ISC_LIST_NEXT(cur, publink);
2771 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2774 ISC_LIST_APPEND(fctx->altaddrs, ai,
2782 * Mark all known bad servers.
2784 all_bad = mark_bad(fctx);
2791 * We've got no addresses.
2793 if (fctx->pending > 0) {
2795 * We're fetching the addresses, but don't have any
2796 * yet. Tell the caller to wait for an answer.
2798 result = DNS_R_WAIT;
2803 * We've lost completely. We don't know any
2804 * addresses, and the ADB has told us it can't get
2807 FCTXTRACE("no addresses");
2808 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2809 result = isc_time_nowplusinterval(&expire, &i);
2811 (fctx->type == dns_rdatatype_dnskey ||
2812 fctx->type == dns_rdatatype_dlv ||
2813 fctx->type == dns_rdatatype_ds) &&
2814 result == ISC_R_SUCCESS)
2815 dns_resolver_addbadcache(fctx->res,
2817 fctx->type, &expire);
2818 result = ISC_R_FAILURE;
2822 * We've found some addresses. We might still be looking
2823 * for more addresses.
2825 sort_finds(&fctx->finds);
2826 sort_finds(&fctx->altfinds);
2827 result = ISC_R_SUCCESS;
2834 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2837 char buf[ISC_NETADDR_FORMATSIZE];
2839 isc_boolean_t aborted = ISC_FALSE;
2840 isc_boolean_t bogus;
2841 dns_acl_t *blackhole;
2842 isc_netaddr_t ipaddr;
2843 dns_peer_t *peer = NULL;
2844 dns_resolver_t *res;
2845 const char *msg = NULL;
2847 sa = &addr->sockaddr;
2850 isc_netaddr_fromsockaddr(&ipaddr, sa);
2851 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2852 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2854 if (blackhole != NULL) {
2857 if (dns_acl_match(&ipaddr, NULL, blackhole,
2859 &match, NULL) == ISC_R_SUCCESS &&
2865 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2870 addr->flags |= FCTX_ADDRINFO_MARK;
2871 msg = "ignoring blackholed / bogus server: ";
2872 } else if (isc_sockaddr_ismulticast(sa)) {
2873 addr->flags |= FCTX_ADDRINFO_MARK;
2874 msg = "ignoring multicast address: ";
2875 } else if (isc_sockaddr_isexperimental(sa)) {
2876 addr->flags |= FCTX_ADDRINFO_MARK;
2877 msg = "ignoring experimental address: ";
2878 } else if (sa->type.sa.sa_family != AF_INET6) {
2880 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2881 addr->flags |= FCTX_ADDRINFO_MARK;
2882 msg = "ignoring IPv6 mapped IPV4 address: ";
2883 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2884 addr->flags |= FCTX_ADDRINFO_MARK;
2885 msg = "ignoring IPv6 compatibility IPV4 address: ";
2889 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2892 isc_netaddr_fromsockaddr(&na, sa);
2893 isc_netaddr_format(&na, buf, sizeof(buf));
2894 FCTXTRACE2(msg, buf);
2897 static inline dns_adbaddrinfo_t *
2898 fctx_nextaddress(fetchctx_t *fctx) {
2899 dns_adbfind_t *find, *start;
2900 dns_adbaddrinfo_t *addrinfo;
2901 dns_adbaddrinfo_t *faddrinfo;
2904 * Return the next untried address, if any.
2908 * Find the first unmarked forwarder (if any).
2910 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2912 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2913 if (!UNMARKED(addrinfo))
2915 possibly_mark(fctx, addrinfo);
2916 if (UNMARKED(addrinfo)) {
2917 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2924 * No forwarders. Move to the next find.
2927 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2931 find = ISC_LIST_HEAD(fctx->finds);
2933 find = ISC_LIST_NEXT(find, publink);
2935 find = ISC_LIST_HEAD(fctx->finds);
2939 * Find the first unmarked addrinfo.
2945 for (addrinfo = ISC_LIST_HEAD(find->list);
2947 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2948 if (!UNMARKED(addrinfo))
2950 possibly_mark(fctx, addrinfo);
2951 if (UNMARKED(addrinfo)) {
2952 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2956 if (addrinfo != NULL)
2958 find = ISC_LIST_NEXT(find, publink);
2960 find = ISC_LIST_HEAD(fctx->finds);
2961 } while (find != start);
2965 if (addrinfo != NULL)
2969 * No nameservers left. Try alternates.
2972 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2974 find = fctx->altfind;
2976 find = ISC_LIST_HEAD(fctx->altfinds);
2978 find = ISC_LIST_NEXT(find, publink);
2980 find = ISC_LIST_HEAD(fctx->altfinds);
2984 * Find the first unmarked addrinfo.
2990 for (addrinfo = ISC_LIST_HEAD(find->list);
2992 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2993 if (!UNMARKED(addrinfo))
2995 possibly_mark(fctx, addrinfo);
2996 if (UNMARKED(addrinfo)) {
2997 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3001 if (addrinfo != NULL)
3003 find = ISC_LIST_NEXT(find, publink);
3005 find = ISC_LIST_HEAD(fctx->altfinds);
3006 } while (find != start);
3009 faddrinfo = addrinfo;
3012 * See if we have a better alternate server by address.
3015 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
3017 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3018 if (!UNMARKED(addrinfo))
3020 possibly_mark(fctx, addrinfo);
3021 if (UNMARKED(addrinfo) &&
3022 (faddrinfo == NULL ||
3023 addrinfo->srtt < faddrinfo->srtt)) {
3024 if (faddrinfo != NULL)
3025 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3026 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3031 if (addrinfo == NULL) {
3032 addrinfo = faddrinfo;
3033 fctx->altfind = find;
3040 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3041 isc_result_t result;
3042 dns_adbaddrinfo_t *addrinfo;
3046 REQUIRE(!ADDRWAIT(fctx));
3048 if (fctx->totalqueries > DEFAULT_MAX_QUERIES)
3049 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3051 addrinfo = fctx_nextaddress(fctx);
3052 if (addrinfo == NULL) {
3054 * We have no more addresses. Start over.
3056 fctx_cancelqueries(fctx, ISC_TRUE);
3057 fctx_cleanupfinds(fctx);
3058 fctx_cleanupaltfinds(fctx);
3059 fctx_cleanupforwaddrs(fctx);
3060 fctx_cleanupaltaddrs(fctx);
3061 result = fctx_getaddresses(fctx, badcache);
3062 if (result == DNS_R_WAIT) {
3064 * Sleep waiting for addresses.
3066 FCTXTRACE("addrwait");
3067 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3069 } else if (result != ISC_R_SUCCESS) {
3071 * Something bad happened.
3073 fctx_done(fctx, result, __LINE__);
3077 addrinfo = fctx_nextaddress(fctx);
3079 * While we may have addresses from the ADB, they
3080 * might be bad ones. In this case, return SERVFAIL.
3082 if (addrinfo == NULL) {
3083 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3088 result = fctx_query(fctx, addrinfo, fctx->options);
3089 if (result != ISC_R_SUCCESS)
3090 fctx_done(fctx, result, __LINE__);
3092 inc_stats(fctx->res, dns_resstatscounter_retry);
3095 static isc_boolean_t
3096 fctx_unlink(fetchctx_t *fctx) {
3097 dns_resolver_t *res;
3098 unsigned int bucketnum;
3101 * Caller must be holding the bucket lock.
3104 REQUIRE(VALID_FCTX(fctx));
3105 REQUIRE(fctx->state == fetchstate_done ||
3106 fctx->state == fetchstate_init);
3107 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3108 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3109 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3110 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3111 REQUIRE(fctx->pending == 0);
3112 REQUIRE(fctx->references == 0);
3113 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3115 FCTXTRACE("unlink");
3118 bucketnum = fctx->bucketnum;
3120 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3124 UNLOCK(&res->nlock);
3126 if (res->buckets[bucketnum].exiting &&
3127 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3134 fctx_destroy(fetchctx_t *fctx) {
3135 isc_sockaddr_t *sa, *next_sa;
3137 REQUIRE(VALID_FCTX(fctx));
3138 REQUIRE(fctx->state == fetchstate_done ||
3139 fctx->state == fetchstate_init);
3140 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3141 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3142 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3143 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3144 REQUIRE(fctx->pending == 0);
3145 REQUIRE(fctx->references == 0);
3146 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3147 REQUIRE(!ISC_LINK_LINKED(fctx, link));
3149 FCTXTRACE("destroy");
3154 for (sa = ISC_LIST_HEAD(fctx->bad);
3157 next_sa = ISC_LIST_NEXT(sa, link);
3158 ISC_LIST_UNLINK(fctx->bad, sa, link);
3159 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3162 for (sa = ISC_LIST_HEAD(fctx->edns);
3165 next_sa = ISC_LIST_NEXT(sa, link);
3166 ISC_LIST_UNLINK(fctx->edns, sa, link);
3167 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3170 for (sa = ISC_LIST_HEAD(fctx->edns512);
3173 next_sa = ISC_LIST_NEXT(sa, link);
3174 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3175 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3178 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3181 next_sa = ISC_LIST_NEXT(sa, link);
3182 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3183 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3186 isc_timer_detach(&fctx->timer);
3187 dns_message_destroy(&fctx->rmessage);
3188 dns_message_destroy(&fctx->qmessage);
3189 if (dns_name_countlabels(&fctx->domain) > 0)
3190 dns_name_free(&fctx->domain, fctx->mctx);
3191 if (dns_rdataset_isassociated(&fctx->nameservers))
3192 dns_rdataset_disassociate(&fctx->nameservers);
3193 dns_name_free(&fctx->name, fctx->mctx);
3194 dns_db_detach(&fctx->cache);
3195 dns_adb_detach(&fctx->adb);
3196 isc_mem_free(fctx->mctx, fctx->info);
3197 isc_mem_putanddetach(&fctx->mctx, fctx, sizeof(*fctx));
3201 * Fetch event handlers.
3205 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3206 fetchctx_t *fctx = event->ev_arg;
3207 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3210 REQUIRE(VALID_FCTX(fctx));
3214 FCTXTRACE("timeout");
3216 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3218 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3219 fctx->reason = NULL;
3220 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3222 isc_result_t result;
3225 fctx->timeout = ISC_TRUE;
3227 * We could cancel the running queries here, or we could let
3228 * them keep going. Since we normally use separate sockets for
3229 * different queries, we adopt the former approach to reduce
3230 * the number of open sockets: cancel the oldest query if it
3231 * expired after the query had started (this is usually the
3232 * case but is not always so, depending on the task schedule
3235 query = ISC_LIST_HEAD(fctx->queries);
3236 if (query != NULL &&
3237 isc_time_compare(&tevent->due, &query->start) >= 0) {
3238 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3240 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3242 * Our timer has triggered. Reestablish the fctx lifetime
3245 result = fctx_starttimer(fctx);
3246 if (result != ISC_R_SUCCESS)
3247 fctx_done(fctx, result, __LINE__);
3252 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3255 isc_event_free(&event);
3259 fctx_shutdown(fetchctx_t *fctx) {
3260 isc_event_t *cevent;
3263 * Start the shutdown process for fctx, if it isn't already underway.
3266 FCTXTRACE("shutdown");
3269 * The caller must be holding the appropriate bucket lock.
3272 if (fctx->want_shutdown)
3275 fctx->want_shutdown = ISC_TRUE;
3278 * Unless we're still initializing (in which case the
3279 * control event is still outstanding), we need to post
3280 * the control event to tell the fetch we want it to
3283 if (fctx->state != fetchstate_init) {
3284 cevent = &fctx->control_event;
3285 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3291 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3292 fetchctx_t *fctx = event->ev_arg;
3293 isc_boolean_t bucket_empty = ISC_FALSE;
3294 dns_resolver_t *res;
3295 unsigned int bucketnum;
3296 dns_validator_t *validator;
3297 isc_boolean_t destroy = ISC_FALSE;
3299 REQUIRE(VALID_FCTX(fctx));
3304 bucketnum = fctx->bucketnum;
3306 FCTXTRACE("doshutdown");
3309 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3311 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3314 * Cancel all pending validators. Note that this must be done
3315 * without the bucket lock held, since that could cause deadlock.
3317 validator = ISC_LIST_HEAD(fctx->validators);
3318 while (validator != NULL) {
3319 dns_validator_cancel(validator);
3320 validator = ISC_LIST_NEXT(validator, link);
3323 if (fctx->nsfetch != NULL)
3324 dns_resolver_cancelfetch(fctx->nsfetch);
3327 * Shut down anything that is still running on behalf of this
3328 * fetch. To avoid deadlock with the ADB, we must do this
3329 * before we lock the bucket lock.
3331 fctx_stopeverything(fctx, ISC_FALSE);
3333 LOCK(&res->buckets[bucketnum].lock);
3335 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3337 INSIST(fctx->state == fetchstate_active ||
3338 fctx->state == fetchstate_done);
3339 INSIST(fctx->want_shutdown);
3341 if (fctx->state != fetchstate_done) {
3342 fctx->state = fetchstate_done;
3343 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3346 if (fctx->references == 0 && fctx->pending == 0 &&
3347 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3348 bucket_empty = fctx_unlink(fctx);
3352 UNLOCK(&res->buckets[bucketnum].lock);
3362 fctx_start(isc_task_t *task, isc_event_t *event) {
3363 fetchctx_t *fctx = event->ev_arg;
3364 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3365 dns_resolver_t *res;
3366 unsigned int bucketnum;
3367 isc_boolean_t destroy = ISC_FALSE;
3369 REQUIRE(VALID_FCTX(fctx));
3374 bucketnum = fctx->bucketnum;
3378 LOCK(&res->buckets[bucketnum].lock);
3380 INSIST(fctx->state == fetchstate_init);
3381 if (fctx->want_shutdown) {
3383 * We haven't started this fctx yet, and we've been requested
3386 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3387 fctx->state = fetchstate_done;
3388 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3390 * Since we haven't started, we INSIST that we have no
3391 * pending ADB finds and no pending validations.
3393 INSIST(fctx->pending == 0);
3394 INSIST(fctx->nqueries == 0);
3395 INSIST(ISC_LIST_EMPTY(fctx->validators));
3396 if (fctx->references == 0) {
3398 * It's now safe to destroy this fctx.
3400 bucket_empty = fctx_unlink(fctx);
3406 * Normal fctx startup.
3408 fctx->state = fetchstate_active;
3409 fctx->totalqueries = 0;
3411 * Reset the control event for later use in shutting down
3414 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3415 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3419 UNLOCK(&res->buckets[bucketnum].lock);
3422 isc_result_t result;
3427 * All is well. Start working on the fetch.
3429 result = fctx_starttimer(fctx);
3430 if (result != ISC_R_SUCCESS)
3431 fctx_done(fctx, result, __LINE__);
3433 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3434 } else if (destroy) {
3442 * Fetch Creation, Joining, and Cancelation.
3445 static inline isc_result_t
3446 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3447 dns_messageid_t id, isc_taskaction_t action, void *arg,
3448 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3452 dns_fetchevent_t *event;
3457 * We store the task we're going to send this event to in the
3458 * sender field. We'll make the fetch the sender when we actually
3462 isc_task_attach(task, &clone);
3463 event = (dns_fetchevent_t *)
3464 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3465 action, arg, sizeof(*event));
3466 if (event == NULL) {
3467 isc_task_detach(&clone);
3468 return (ISC_R_NOMEMORY);
3470 event->result = DNS_R_SERVFAIL;
3471 event->qtype = fctx->type;
3474 event->rdataset = rdataset;
3475 event->sigrdataset = sigrdataset;
3476 event->fetch = fetch;
3477 event->client = client;
3480 dns_fixedname_init(&event->foundname);
3483 * Make sure that we can store the sigrdataset in the
3484 * first event if it is needed by any of the events.
3486 if (event->sigrdataset != NULL)
3487 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3489 ISC_LIST_APPEND(fctx->events, event, ev_link);
3491 fctx->client = client;
3493 fetch->magic = DNS_FETCH_MAGIC;
3494 fetch->private = fctx;
3496 return (ISC_R_SUCCESS);
3500 log_ns_ttl(fetchctx_t *fctx, const char *where) {
3501 char namebuf[DNS_NAME_FORMATSIZE];
3502 char domainbuf[DNS_NAME_FORMATSIZE];
3504 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3505 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3506 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3507 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
3508 "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u",
3509 fctx, where, namebuf, domainbuf,
3510 fctx->ns_ttl_ok, fctx->ns_ttl);
3514 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3515 dns_name_t *domain, dns_rdataset_t *nameservers,
3516 unsigned int options, unsigned int bucketnum, unsigned int depth,
3520 isc_result_t result;
3521 isc_result_t iresult;
3522 isc_interval_t interval;
3523 dns_fixedname_t fixed;
3524 unsigned int findoptions = 0;
3525 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3526 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3531 * Caller must be holding the lock for bucket number 'bucketnum'.
3533 REQUIRE(fctxp != NULL && *fctxp == NULL);
3535 mctx = res->buckets[bucketnum].mctx;
3536 fctx = isc_mem_get(mctx, sizeof(*fctx));
3538 return (ISC_R_NOMEMORY);
3539 dns_name_format(name, buf, sizeof(buf));
3540 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3541 strcat(buf, "/"); /* checked */
3542 strcat(buf, typebuf); /* checked */
3543 fctx->info = isc_mem_strdup(mctx, buf);
3544 if (fctx->info == NULL) {
3545 result = ISC_R_NOMEMORY;
3548 FCTXTRACE("create");
3549 dns_name_init(&fctx->name, NULL);
3550 result = dns_name_dup(name, mctx, &fctx->name);
3551 if (result != ISC_R_SUCCESS)
3553 dns_name_init(&fctx->domain, NULL);
3554 dns_rdataset_init(&fctx->nameservers);
3557 fctx->options = options;
3559 * Note! We do not attach to the task. We are relying on the
3560 * resolver to ensure that this task doesn't go away while we are
3564 fctx->references = 0;
3565 fctx->bucketnum = bucketnum;
3566 fctx->state = fetchstate_init;
3567 fctx->want_shutdown = ISC_FALSE;
3568 fctx->cloned = ISC_FALSE;
3569 fctx->depth = depth;
3570 ISC_LIST_INIT(fctx->queries);
3571 ISC_LIST_INIT(fctx->finds);
3572 ISC_LIST_INIT(fctx->altfinds);
3573 ISC_LIST_INIT(fctx->forwaddrs);
3574 ISC_LIST_INIT(fctx->altaddrs);
3575 ISC_LIST_INIT(fctx->forwarders);
3576 fctx->fwdpolicy = dns_fwdpolicy_none;
3577 ISC_LIST_INIT(fctx->bad);
3578 ISC_LIST_INIT(fctx->edns);
3579 ISC_LIST_INIT(fctx->edns512);
3580 ISC_LIST_INIT(fctx->bad_edns);
3581 ISC_LIST_INIT(fctx->validators);
3582 fctx->validator = NULL;
3584 fctx->altfind = NULL;
3587 fctx->querysent = 0;
3588 fctx->totalqueries = 0;
3589 fctx->referrals = 0;
3590 TIME_NOW(&fctx->start);
3592 fctx->lamecount = 0;
3598 fctx->result = ISC_R_FAILURE;
3599 fctx->vresult = ISC_R_SUCCESS;
3600 fctx->exitline = -1; /* sentinel */
3601 fctx->logged = ISC_FALSE;
3602 fctx->attributes = 0;
3603 fctx->spilled = ISC_FALSE;
3605 fctx->reason = NULL;
3607 fctx->rand_bits = 0;
3608 fctx->timeout = ISC_FALSE;
3609 fctx->addrinfo = NULL;
3610 fctx->client = NULL;
3612 fctx->ns_ttl_ok = ISC_FALSE;
3614 dns_name_init(&fctx->nsname, NULL);
3615 fctx->nsfetch = NULL;
3616 dns_rdataset_init(&fctx->nsrrset);
3618 if (domain == NULL) {
3619 dns_forwarders_t *forwarders = NULL;
3620 unsigned int labels;
3621 dns_name_t *fwdname = name;
3624 * DS records are found in the parent server.
3625 * Strip label to get the correct forwarder (if any).
3627 if (dns_rdatatype_atparent(fctx->type) &&
3628 dns_name_countlabels(name) > 1) {
3629 dns_name_init(&suffix, NULL);
3630 labels = dns_name_countlabels(name);
3631 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3634 dns_fixedname_init(&fixed);
3635 domain = dns_fixedname_name(&fixed);
3636 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3637 domain, &forwarders);
3638 if (result == ISC_R_SUCCESS)
3639 fctx->fwdpolicy = forwarders->fwdpolicy;
3641 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3643 * The caller didn't supply a query domain and
3644 * nameservers, and we're not in forward-only mode,
3645 * so find the best nameservers to use.
3647 if (dns_rdatatype_atparent(fctx->type))
3648 findoptions |= DNS_DBFIND_NOEXACT;
3649 result = dns_view_findzonecut(res->view, fwdname,
3650 domain, 0, findoptions,
3654 if (result != ISC_R_SUCCESS)
3657 result = dns_name_dup(domain, mctx, &fctx->domain);
3658 if (result != ISC_R_SUCCESS) {
3659 dns_rdataset_disassociate(&fctx->nameservers);
3662 fctx->ns_ttl = fctx->nameservers.ttl;
3663 fctx->ns_ttl_ok = ISC_TRUE;
3666 * We're in forward-only mode. Set the query domain.
3668 result = dns_name_dup(domain, mctx, &fctx->domain);
3669 if (result != ISC_R_SUCCESS)
3673 result = dns_name_dup(domain, mctx, &fctx->domain);
3674 if (result != ISC_R_SUCCESS)
3676 dns_rdataset_clone(nameservers, &fctx->nameservers);
3677 fctx->ns_ttl = fctx->nameservers.ttl;
3678 fctx->ns_ttl_ok = ISC_TRUE;
3681 log_ns_ttl(fctx, "fctx_create");
3683 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3685 fctx->qmessage = NULL;
3686 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
3689 if (result != ISC_R_SUCCESS)
3690 goto cleanup_domain;
3692 fctx->rmessage = NULL;
3693 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
3696 if (result != ISC_R_SUCCESS)
3697 goto cleanup_qmessage;
3700 * Compute an expiration time for the entire fetch.
3702 isc_interval_set(&interval, res->query_timeout, 0);
3703 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3704 if (iresult != ISC_R_SUCCESS) {
3705 UNEXPECTED_ERROR(__FILE__, __LINE__,
3706 "isc_time_nowplusinterval: %s",
3707 isc_result_totext(iresult));
3708 result = ISC_R_UNEXPECTED;
3709 goto cleanup_rmessage;
3713 * Default retry interval initialization. We set the interval now
3714 * mostly so it won't be uninitialized. It will be set to the
3715 * correct value before a query is issued.
3717 isc_interval_set(&fctx->interval, 2, 0);
3720 * Create an inactive timer. It will be made active when the fetch
3721 * is actually started.
3724 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3726 res->buckets[bucketnum].task, fctx_timeout,
3727 fctx, &fctx->timer);
3728 if (iresult != ISC_R_SUCCESS) {
3729 UNEXPECTED_ERROR(__FILE__, __LINE__,
3730 "isc_timer_create: %s",
3731 isc_result_totext(iresult));
3732 result = ISC_R_UNEXPECTED;
3733 goto cleanup_rmessage;
3737 * Attach to the view's cache and adb.
3740 dns_db_attach(res->view->cachedb, &fctx->cache);
3742 dns_adb_attach(res->view->adb, &fctx->adb);
3744 isc_mem_attach(mctx, &fctx->mctx);
3746 ISC_LIST_INIT(fctx->events);
3747 ISC_LINK_INIT(fctx, link);
3748 fctx->magic = FCTX_MAGIC;
3750 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3754 UNLOCK(&res->nlock);
3758 return (ISC_R_SUCCESS);
3761 dns_message_destroy(&fctx->rmessage);
3764 dns_message_destroy(&fctx->qmessage);
3767 if (dns_name_countlabels(&fctx->domain) > 0)
3768 dns_name_free(&fctx->domain, mctx);
3769 if (dns_rdataset_isassociated(&fctx->nameservers))
3770 dns_rdataset_disassociate(&fctx->nameservers);
3773 dns_name_free(&fctx->name, mctx);
3776 isc_mem_free(mctx, fctx->info);
3779 isc_mem_put(mctx, fctx, sizeof(*fctx));
3787 static inline isc_boolean_t
3788 is_lame(fetchctx_t *fctx) {
3789 dns_message_t *message = fctx->rmessage;
3791 dns_rdataset_t *rdataset;
3792 isc_result_t result;
3794 if (message->rcode != dns_rcode_noerror &&
3795 message->rcode != dns_rcode_nxdomain)
3798 if (message->counts[DNS_SECTION_ANSWER] != 0)
3801 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3804 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3805 while (result == ISC_R_SUCCESS) {
3807 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3808 for (rdataset = ISC_LIST_HEAD(name->list);
3810 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3811 dns_namereln_t namereln;
3813 unsigned int labels;
3814 if (rdataset->type != dns_rdatatype_ns)
3816 namereln = dns_name_fullcompare(name, &fctx->domain,
3818 if (namereln == dns_namereln_equal &&
3819 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3821 if (namereln == dns_namereln_subdomain)
3825 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3832 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3833 char namebuf[DNS_NAME_FORMATSIZE];
3834 char domainbuf[DNS_NAME_FORMATSIZE];
3835 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3837 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3838 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3839 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3840 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3841 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3842 "lame server resolving '%s' (in '%s'?): %s",
3843 namebuf, domainbuf, addrbuf);
3847 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3848 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3849 char clbuf[ISC_SOCKADDR_FORMATSIZE];
3850 const char *clmsg = "";
3854 va_start(args, format);
3855 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3858 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3860 if (fctx->client != NULL) {
3861 clmsg = " for client ";
3862 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3867 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3868 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3869 "DNS format error from %s resolving %s%s%s: %s",
3870 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3873 static inline isc_result_t
3874 same_question(fetchctx_t *fctx) {
3875 isc_result_t result;
3876 dns_message_t *message = fctx->rmessage;
3878 dns_rdataset_t *rdataset;
3881 * Caller must be holding the fctx lock.
3885 * XXXRTH Currently we support only one question.
3887 if (message->counts[DNS_SECTION_QUESTION] != 1) {
3888 log_formerr(fctx, "too many questions");
3889 return (DNS_R_FORMERR);
3892 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3893 if (result != ISC_R_SUCCESS)
3896 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3897 rdataset = ISC_LIST_HEAD(name->list);
3898 INSIST(rdataset != NULL);
3899 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3901 if (fctx->type != rdataset->type ||
3902 fctx->res->rdclass != rdataset->rdclass ||
3903 !dns_name_equal(&fctx->name, name)) {
3904 char namebuf[DNS_NAME_FORMATSIZE];
3905 char class[DNS_RDATACLASS_FORMATSIZE];
3906 char type[DNS_RDATATYPE_FORMATSIZE];
3908 dns_name_format(name, namebuf, sizeof(namebuf));
3909 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3910 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3911 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3912 namebuf, class, type);
3913 return (DNS_R_FORMERR);
3916 return (ISC_R_SUCCESS);
3920 clone_results(fetchctx_t *fctx) {
3921 dns_fetchevent_t *event, *hevent;
3922 isc_result_t result;
3923 dns_name_t *name, *hname;
3925 FCTXTRACE("clone_results");
3928 * Set up any other events to have the same data as the first
3931 * Caller must be holding the appropriate lock.
3934 fctx->cloned = ISC_TRUE;
3935 hevent = ISC_LIST_HEAD(fctx->events);
3938 hname = dns_fixedname_name(&hevent->foundname);
3939 for (event = ISC_LIST_NEXT(hevent, ev_link);
3941 event = ISC_LIST_NEXT(event, ev_link)) {
3942 name = dns_fixedname_name(&event->foundname);
3943 result = dns_name_copy(hname, name, NULL);
3944 if (result != ISC_R_SUCCESS)
3945 event->result = result;
3947 event->result = hevent->result;
3948 dns_db_attach(hevent->db, &event->db);
3949 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3950 INSIST(hevent->rdataset != NULL);
3951 INSIST(event->rdataset != NULL);
3952 if (dns_rdataset_isassociated(hevent->rdataset))
3953 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3954 INSIST(! (hevent->sigrdataset == NULL &&
3955 event->sigrdataset != NULL));
3956 if (hevent->sigrdataset != NULL &&
3957 dns_rdataset_isassociated(hevent->sigrdataset) &&
3958 event->sigrdataset != NULL)
3959 dns_rdataset_clone(hevent->sigrdataset,
3960 event->sigrdataset);
3964 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3965 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3966 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3967 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3968 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3969 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3970 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3974 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3975 * no references and is no longer waiting for any events).
3978 * '*fctx' is shutting down.
3981 * true if the resolver is exiting and this is the last fctx in the bucket.
3983 static isc_boolean_t
3984 maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) {
3985 unsigned int bucketnum;
3986 isc_boolean_t bucket_empty = ISC_FALSE;
3987 dns_resolver_t *res = fctx->res;
3988 dns_validator_t *validator, *next_validator;
3989 isc_boolean_t destroy = ISC_FALSE;
3991 REQUIRE(SHUTTINGDOWN(fctx));
3993 bucketnum = fctx->bucketnum;
3995 LOCK(&res->buckets[bucketnum].lock);
3996 if (fctx->pending != 0 || fctx->nqueries != 0)
3999 for (validator = ISC_LIST_HEAD(fctx->validators);
4000 validator != NULL; validator = next_validator) {
4001 next_validator = ISC_LIST_NEXT(validator, link);
4002 dns_validator_cancel(validator);
4005 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators)) {
4006 bucket_empty = fctx_unlink(fctx);
4011 UNLOCK(&res->buckets[bucketnum].lock);
4014 return (bucket_empty);
4018 * The validator has finished.
4021 validated(isc_task_t *task, isc_event_t *event) {
4022 dns_adbaddrinfo_t *addrinfo;
4023 dns_dbnode_t *node = NULL;
4024 dns_dbnode_t *nsnode = NULL;
4025 dns_fetchevent_t *hevent;
4027 dns_rdataset_t *ardataset = NULL;
4028 dns_rdataset_t *asigrdataset = NULL;
4029 dns_rdataset_t *rdataset;
4030 dns_rdataset_t *sigrdataset;
4031 dns_resolver_t *res;
4032 dns_valarg_t *valarg;
4033 dns_validatorevent_t *vevent;
4035 isc_boolean_t chaining;
4036 isc_boolean_t negative;
4037 isc_boolean_t sentresponse;
4038 isc_result_t eresult = ISC_R_SUCCESS;
4039 isc_result_t result = ISC_R_SUCCESS;
4043 UNUSED(task); /* for now */
4045 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
4046 valarg = event->ev_arg;
4047 fctx = valarg->fctx;
4049 addrinfo = valarg->addrinfo;
4050 REQUIRE(VALID_FCTX(fctx));
4051 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
4053 vevent = (dns_validatorevent_t *)event;
4054 fctx->vresult = vevent->result;
4056 FCTXTRACE("received validation completion event");
4058 LOCK(&res->buckets[fctx->bucketnum].lock);
4060 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
4061 fctx->validator = NULL;
4064 * Destroy the validator early so that we can
4065 * destroy the fctx if necessary.
4067 dns_validator_destroy(&vevent->validator);
4068 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
4070 negative = ISC_TF(vevent->rdataset == NULL);
4072 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
4075 * If shutting down, ignore the results. Check to see if we're
4076 * done waiting for validator completions and ADB pending events; if
4077 * so, destroy the fctx.
4079 if (SHUTTINGDOWN(fctx) && !sentresponse) {
4080 isc_uint32_t bucketnum = fctx->bucketnum;
4081 isc_boolean_t bucket_empty;
4082 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4083 UNLOCK(&res->buckets[bucketnum].lock);
4089 isc_stdtime_get(&now);
4092 * If chaining, we need to make sure that the right result code is
4093 * returned, and that the rdatasets are bound.
4095 if (vevent->result == ISC_R_SUCCESS &&
4097 vevent->rdataset != NULL &&
4098 CHAINING(vevent->rdataset))
4100 if (vevent->rdataset->type == dns_rdatatype_cname)
4101 eresult = DNS_R_CNAME;
4103 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4104 eresult = DNS_R_DNAME;
4106 chaining = ISC_TRUE;
4108 chaining = ISC_FALSE;
4111 * Either we're not shutting down, or we are shutting down but want
4112 * to cache the result anyway (if this was a validation started by
4113 * a query with cd set)
4116 hevent = ISC_LIST_HEAD(fctx->events);
4117 if (hevent != NULL) {
4118 if (!negative && !chaining &&
4119 (fctx->type == dns_rdatatype_any ||
4120 fctx->type == dns_rdatatype_rrsig ||
4121 fctx->type == dns_rdatatype_sig)) {
4123 * Don't bind rdatasets; the caller
4124 * will iterate the node.
4127 ardataset = hevent->rdataset;
4128 asigrdataset = hevent->sigrdataset;
4132 if (vevent->result != ISC_R_SUCCESS) {
4133 FCTXTRACE("validation failed");
4134 inc_stats(res, dns_resstatscounter_valfail);
4136 fctx->vresult = vevent->result;
4137 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4138 result = ISC_R_NOTFOUND;
4139 if (vevent->rdataset != NULL)
4140 result = dns_db_findnode(fctx->cache,
4143 if (result == ISC_R_SUCCESS)
4144 (void)dns_db_deleterdataset(fctx->cache, node,
4147 if (result == ISC_R_SUCCESS &&
4148 vevent->sigrdataset != NULL)
4149 (void)dns_db_deleterdataset(fctx->cache, node,
4151 dns_rdatatype_rrsig,
4153 if (result == ISC_R_SUCCESS)
4154 dns_db_detachnode(fctx->cache, &node);
4156 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4158 * Cache the data as pending for later validation.
4160 result = ISC_R_NOTFOUND;
4161 if (vevent->rdataset != NULL)
4162 result = dns_db_findnode(fctx->cache,
4165 if (result == ISC_R_SUCCESS) {
4166 (void)dns_db_addrdataset(fctx->cache, node,
4168 vevent->rdataset, 0,
4171 if (result == ISC_R_SUCCESS &&
4172 vevent->sigrdataset != NULL)
4173 (void)dns_db_addrdataset(fctx->cache, node,
4175 vevent->sigrdataset,
4177 if (result == ISC_R_SUCCESS)
4178 dns_db_detachnode(fctx->cache, &node);
4180 result = fctx->vresult;
4181 add_bad(fctx, addrinfo, result, badns_validation);
4182 isc_event_free(&event);
4183 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4184 INSIST(fctx->validator == NULL);
4185 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4186 if (fctx->validator != NULL)
4187 dns_validator_send(fctx->validator);
4188 else if (sentresponse)
4189 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4190 else if (result == DNS_R_BROKENCHAIN) {
4191 isc_result_t tresult;
4195 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4196 tresult = isc_time_nowplusinterval(&expire, &i);
4198 (fctx->type == dns_rdatatype_dnskey ||
4199 fctx->type == dns_rdatatype_dlv ||
4200 fctx->type == dns_rdatatype_ds) &&
4201 tresult == ISC_R_SUCCESS)
4202 dns_resolver_addbadcache(res, &fctx->name,
4203 fctx->type, &expire);
4204 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4206 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4212 dns_rdatatype_t covers;
4213 FCTXTRACE("nonexistence validation OK");
4215 inc_stats(res, dns_resstatscounter_valnegsuccess);
4217 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4218 covers = dns_rdatatype_any;
4220 covers = fctx->type;
4222 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4224 if (result != ISC_R_SUCCESS)
4225 goto noanswer_response;
4228 * If we are asking for a SOA record set the cache time
4229 * to zero to facilitate locating the containing zone of
4232 ttl = res->view->maxncachettl;
4233 if (fctx->type == dns_rdatatype_soa &&
4234 covers == dns_rdatatype_any && res->zero_no_soa_ttl)
4237 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4238 covers, now, ttl, vevent->optout,
4239 vevent->secure, ardataset, &eresult);
4240 if (result != ISC_R_SUCCESS)
4241 goto noanswer_response;
4242 goto answer_response;
4244 inc_stats(res, dns_resstatscounter_valsuccess);
4246 FCTXTRACE("validation OK");
4248 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4249 result = dns_rdataset_addnoqname(vevent->rdataset,
4250 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4251 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4252 INSIST(vevent->sigrdataset != NULL);
4253 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4254 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4255 result = dns_rdataset_addclosest(vevent->rdataset,
4256 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4257 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4259 } else if (vevent->rdataset->trust == dns_trust_answer &&
4260 vevent->rdataset->type != dns_rdatatype_rrsig)
4262 isc_result_t tresult;
4263 dns_name_t *noqname = NULL;
4264 tresult = findnoqname(fctx, vevent->name,
4265 vevent->rdataset->type, &noqname);
4266 if (tresult == ISC_R_SUCCESS && noqname != NULL) {
4267 tresult = dns_rdataset_addnoqname(vevent->rdataset,
4269 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4274 * The data was already cached as pending data.
4275 * Re-cache it as secure and bind the cached
4276 * rdatasets to the first event on the fetch
4279 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4280 if (result != ISC_R_SUCCESS)
4281 goto noanswer_response;
4283 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4284 vevent->rdataset, 0, ardataset);
4285 if (result != ISC_R_SUCCESS &&
4286 result != DNS_R_UNCHANGED)
4287 goto noanswer_response;
4288 if (ardataset != NULL && NEGATIVE(ardataset)) {
4289 if (NXDOMAIN(ardataset))
4290 eresult = DNS_R_NCACHENXDOMAIN;
4292 eresult = DNS_R_NCACHENXRRSET;
4293 } else if (vevent->sigrdataset != NULL) {
4294 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4295 vevent->sigrdataset, 0,
4297 if (result != ISC_R_SUCCESS &&
4298 result != DNS_R_UNCHANGED)
4299 goto noanswer_response;
4303 isc_boolean_t bucket_empty = ISC_FALSE;
4305 * If we only deferred the destroy because we wanted to cache
4306 * the data, destroy now.
4308 dns_db_detachnode(fctx->cache, &node);
4309 if (SHUTTINGDOWN(fctx))
4310 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4311 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4317 if (!ISC_LIST_EMPTY(fctx->validators)) {
4319 INSIST(fctx->type == dns_rdatatype_any ||
4320 fctx->type == dns_rdatatype_rrsig ||
4321 fctx->type == dns_rdatatype_sig);
4323 * Don't send a response yet - we have
4324 * more rdatasets that still need to
4327 dns_db_detachnode(fctx->cache, &node);
4328 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4329 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4335 * Cache any NS/NSEC records that happened to be validated.
4337 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4338 while (result == ISC_R_SUCCESS) {
4340 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4342 for (rdataset = ISC_LIST_HEAD(name->list);
4344 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4345 if ((rdataset->type != dns_rdatatype_ns &&
4346 rdataset->type != dns_rdatatype_nsec) ||
4347 rdataset->trust != dns_trust_secure)
4349 for (sigrdataset = ISC_LIST_HEAD(name->list);
4350 sigrdataset != NULL;
4351 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4352 if (sigrdataset->type != dns_rdatatype_rrsig ||
4353 sigrdataset->covers != rdataset->type)
4357 if (sigrdataset == NULL ||
4358 sigrdataset->trust != dns_trust_secure)
4360 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4362 if (result != ISC_R_SUCCESS)
4365 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4366 now, rdataset, 0, NULL);
4367 if (result == ISC_R_SUCCESS)
4368 result = dns_db_addrdataset(fctx->cache, nsnode,
4372 dns_db_detachnode(fctx->cache, &nsnode);
4373 if (result != ISC_R_SUCCESS)
4376 result = dns_message_nextname(fctx->rmessage,
4377 DNS_SECTION_AUTHORITY);
4380 result = ISC_R_SUCCESS;
4383 * Respond with an answer, positive or negative,
4384 * as opposed to an error. 'node' must be non-NULL.
4387 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4389 if (hevent != NULL) {
4391 * Negative results must be indicated in event->result.
4393 if (dns_rdataset_isassociated(hevent->rdataset) &&
4394 NEGATIVE(hevent->rdataset)) {
4395 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4396 eresult == DNS_R_NCACHENXRRSET);
4398 hevent->result = eresult;
4399 RUNTIME_CHECK(dns_name_copy(vevent->name,
4400 dns_fixedname_name(&hevent->foundname), NULL)
4402 dns_db_attach(fctx->cache, &hevent->db);
4403 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4404 clone_results(fctx);
4409 dns_db_detachnode(fctx->cache, &node);
4411 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4412 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4415 INSIST(node == NULL);
4416 isc_event_free(&event);
4420 fctx_log(void *arg, int level, const char *fmt, ...) {
4423 fetchctx_t *fctx = arg;
4425 va_start(args, fmt);
4426 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
4429 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4430 DNS_LOGMODULE_RESOLVER, level,
4431 "fctx %p(%s): %s", fctx, fctx->info, msgbuf);
4434 static inline isc_result_t
4435 findnoqname(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
4436 dns_name_t **noqnamep)
4438 dns_rdataset_t *nrdataset, *next, *sigrdataset;
4439 dns_rdata_rrsig_t rrsig;
4440 isc_result_t result;
4441 unsigned int labels;
4442 dns_section_t section;
4443 dns_name_t *zonename;
4444 dns_fixedname_t fzonename;
4445 dns_name_t *closest;
4446 dns_fixedname_t fclosest;
4447 dns_name_t *nearest;
4448 dns_fixedname_t fnearest;
4449 dns_rdatatype_t found = dns_rdatatype_none;
4450 dns_name_t *noqname = NULL;
4452 FCTXTRACE("findnoqname");
4454 REQUIRE(noqnamep != NULL && *noqnamep == NULL);
4457 * Find the SIG for this rdataset, if we have it.
4459 for (sigrdataset = ISC_LIST_HEAD(name->list);
4460 sigrdataset != NULL;
4461 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4462 if (sigrdataset->type == dns_rdatatype_rrsig &&
4463 sigrdataset->covers == type)
4467 if (sigrdataset == NULL)
4468 return (ISC_R_NOTFOUND);
4470 labels = dns_name_countlabels(name);
4472 for (result = dns_rdataset_first(sigrdataset);
4473 result == ISC_R_SUCCESS;
4474 result = dns_rdataset_next(sigrdataset)) {
4475 dns_rdata_t rdata = DNS_RDATA_INIT;
4476 dns_rdataset_current(sigrdataset, &rdata);
4477 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
4478 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4479 /* Wildcard has rrsig.labels < labels - 1. */
4480 if (rrsig.labels + 1U >= labels)
4485 if (result == ISC_R_NOMORE)
4486 return (ISC_R_NOTFOUND);
4487 if (result != ISC_R_SUCCESS)
4490 dns_fixedname_init(&fzonename);
4491 zonename = dns_fixedname_name(&fzonename);
4492 dns_fixedname_init(&fclosest);
4493 closest = dns_fixedname_name(&fclosest);
4494 dns_fixedname_init(&fnearest);
4495 nearest = dns_fixedname_name(&fnearest);
4497 #define NXND(x) ((x) == ISC_R_SUCCESS)
4499 section = DNS_SECTION_AUTHORITY;
4500 for (result = dns_message_firstname(fctx->rmessage, section);
4501 result == ISC_R_SUCCESS;
4502 result = dns_message_nextname(fctx->rmessage, section)) {
4503 dns_name_t *nsec = NULL;
4504 dns_message_currentname(fctx->rmessage, section, &nsec);
4505 for (nrdataset = ISC_LIST_HEAD(nsec->list);
4506 nrdataset != NULL; nrdataset = next) {
4507 isc_boolean_t data = ISC_FALSE, exists = ISC_FALSE;
4508 isc_boolean_t optout = ISC_FALSE, unknown = ISC_FALSE;
4509 isc_boolean_t setclosest = ISC_FALSE;
4510 isc_boolean_t setnearest = ISC_FALSE;
4512 next = ISC_LIST_NEXT(nrdataset, link);
4513 if (nrdataset->type != dns_rdatatype_nsec &&
4514 nrdataset->type != dns_rdatatype_nsec3)
4517 if (nrdataset->type == dns_rdatatype_nsec &&
4518 NXND(dns_nsec_noexistnodata(type, name, nsec,
4520 &data, NULL, fctx_log,
4525 found = dns_rdatatype_nsec;
4529 if (nrdataset->type == dns_rdatatype_nsec3 &&
4530 NXND(dns_nsec3_noexistnodata(type, name, nsec,
4531 nrdataset, zonename,
4539 if (!exists && setnearest) {
4541 found = dns_rdatatype_nsec3;
4546 if (result == ISC_R_NOMORE)
4547 result = ISC_R_SUCCESS;
4548 if (noqname != NULL) {
4549 for (sigrdataset = ISC_LIST_HEAD(noqname->list);
4550 sigrdataset != NULL;
4551 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4552 if (sigrdataset->type == dns_rdatatype_rrsig &&
4553 sigrdataset->covers == found)
4556 if (sigrdataset != NULL)
4557 *noqnamep = noqname;
4562 static inline isc_result_t
4563 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4566 dns_rdataset_t *rdataset, *sigrdataset;
4567 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4568 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4569 dns_dbnode_t *node, **anodep;
4572 dns_resolver_t *res;
4573 isc_boolean_t need_validation, secure_domain, have_answer;
4574 isc_result_t result, eresult;
4575 dns_fetchevent_t *event;
4576 unsigned int options;
4579 unsigned int valoptions = 0;
4582 * The appropriate bucket lock must be held.
4586 need_validation = ISC_FALSE;
4587 POST(need_validation);
4588 secure_domain = ISC_FALSE;
4589 have_answer = ISC_FALSE;
4590 eresult = ISC_R_SUCCESS;
4591 task = res->buckets[fctx->bucketnum].task;
4594 * Is DNSSEC validation required for this name?
4596 if (res->view->enablevalidation) {
4597 result = dns_view_issecuredomain(res->view, name,
4599 if (result != ISC_R_SUCCESS)
4602 if (!secure_domain && res->view->dlv != NULL) {
4603 valoptions = DNS_VALIDATOR_DLV;
4604 secure_domain = ISC_TRUE;
4608 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4609 need_validation = ISC_FALSE;
4611 need_validation = secure_domain;
4617 asigrdataset = NULL;
4619 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4621 have_answer = ISC_TRUE;
4622 event = ISC_LIST_HEAD(fctx->events);
4623 if (event != NULL) {
4625 aname = dns_fixedname_name(&event->foundname);
4626 result = dns_name_copy(name, aname, NULL);
4627 if (result != ISC_R_SUCCESS)
4629 anodep = &event->node;
4631 * If this is an ANY, SIG or RRSIG query, we're not
4632 * going to return any rdatasets, unless we encountered
4633 * a CNAME or DNAME as "the answer". In this case,
4634 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4635 * and we must set up the rdatasets.
4637 if ((fctx->type != dns_rdatatype_any &&
4638 fctx->type != dns_rdatatype_rrsig &&
4639 fctx->type != dns_rdatatype_sig) ||
4640 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4641 ardataset = event->rdataset;
4642 asigrdataset = event->sigrdataset;
4648 * Find or create the cache node.
4651 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4652 if (result != ISC_R_SUCCESS)
4656 * Cache or validate each cacheable rdataset.
4658 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4659 for (rdataset = ISC_LIST_HEAD(name->list);
4661 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4662 if (!CACHE(rdataset))
4664 if (CHECKNAMES(rdataset)) {
4665 char namebuf[DNS_NAME_FORMATSIZE];
4666 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4667 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4669 dns_name_format(name, namebuf, sizeof(namebuf));
4670 dns_rdatatype_format(rdataset->type, typebuf,
4672 dns_rdataclass_format(rdataset->rdclass, classbuf,
4674 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4675 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4676 "check-names %s %s/%s/%s",
4677 fail ? "failure" : "warning",
4678 namebuf, typebuf, classbuf);
4680 if (ANSWER(rdataset)) {
4681 dns_db_detachnode(fctx->cache, &node);
4682 return (DNS_R_BADNAME);
4689 * Enforce the configure maximum cache TTL.
4691 if (rdataset->ttl > res->view->maxcachettl)
4692 rdataset->ttl = res->view->maxcachettl;
4695 * Find the SIG for this rdataset, if we have it.
4697 for (sigrdataset = ISC_LIST_HEAD(name->list);
4698 sigrdataset != NULL;
4699 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4700 if (sigrdataset->type == dns_rdatatype_rrsig &&
4701 sigrdataset->covers == rdataset->type)
4706 * If this RRset is in a secure domain, is in bailiwick,
4707 * and is not glue, attempt DNSSEC validation. (We do not
4708 * attempt to validate glue or out-of-bailiwick data--even
4709 * though there might be some performance benefit to doing
4710 * so--because it makes it simpler and safer to ensure that
4711 * records from a secure domain are only cached if validated
4712 * within the context of a query to the domain that owns
4715 if (secure_domain && rdataset->trust != dns_trust_glue &&
4716 !EXTERNAL(rdataset)) {
4720 * RRSIGs are validated as part of validating the
4723 if (rdataset->type == dns_rdatatype_rrsig)
4726 if (sigrdataset == NULL) {
4727 if (!ANSWER(rdataset) && need_validation) {
4729 * Ignore non-answer rdatasets that
4730 * are missing signatures.
4737 * Normalize the rdataset and sigrdataset TTLs.
4739 if (sigrdataset != NULL) {
4740 rdataset->ttl = ISC_MIN(rdataset->ttl,
4742 sigrdataset->ttl = rdataset->ttl;
4746 * Cache this rdataset/sigrdataset pair as
4747 * pending data. Track whether it was additional
4750 if (rdataset->trust == dns_trust_additional)
4751 trust = dns_trust_pending_additional;
4753 trust = dns_trust_pending_answer;
4755 rdataset->trust = trust;
4756 if (sigrdataset != NULL)
4757 sigrdataset->trust = trust;
4758 if (!need_validation || !ANSWER(rdataset)) {
4759 if (ANSWER(rdataset) &&
4760 rdataset->type != dns_rdatatype_rrsig) {
4761 isc_result_t tresult;
4762 dns_name_t *noqname = NULL;
4763 tresult = findnoqname(fctx, name,
4766 if (tresult == ISC_R_SUCCESS &&
4769 dns_rdataset_addnoqname(
4771 RUNTIME_CHECK(tresult ==
4775 addedrdataset = ardataset;
4776 result = dns_db_addrdataset(fctx->cache, node,
4777 NULL, now, rdataset,
4779 if (result == DNS_R_UNCHANGED) {
4780 result = ISC_R_SUCCESS;
4781 if (!need_validation &&
4782 ardataset != NULL &&
4783 NEGATIVE(ardataset)) {
4785 * The answer in the cache is
4786 * better than the answer we
4787 * found, and is a negative
4788 * cache entry, so we must set
4789 * eresult appropriately.
4791 if (NXDOMAIN(ardataset))
4793 DNS_R_NCACHENXDOMAIN;
4796 DNS_R_NCACHENXRRSET;
4798 * We have a negative response
4799 * from the cache so don't
4800 * attempt to add the RRSIG
4806 if (result != ISC_R_SUCCESS)
4808 if (sigrdataset != NULL) {
4809 addedrdataset = asigrdataset;
4810 result = dns_db_addrdataset(fctx->cache,
4814 if (result == DNS_R_UNCHANGED)
4815 result = ISC_R_SUCCESS;
4816 if (result != ISC_R_SUCCESS)
4818 } else if (!ANSWER(rdataset))
4822 if (ANSWER(rdataset) && need_validation) {
4823 if (fctx->type != dns_rdatatype_any &&
4824 fctx->type != dns_rdatatype_rrsig &&
4825 fctx->type != dns_rdatatype_sig) {
4827 * This is The Answer. We will
4828 * validate it, but first we cache
4829 * the rest of the response - it may
4830 * contain useful keys.
4832 INSIST(valrdataset == NULL &&
4833 valsigrdataset == NULL);
4834 valrdataset = rdataset;
4835 valsigrdataset = sigrdataset;
4838 * This is one of (potentially)
4839 * multiple answers to an ANY
4840 * or SIG query. To keep things
4841 * simple, we just start the
4842 * validator right away rather
4843 * than caching first and
4844 * having to remember which
4845 * rdatasets needed validation.
4847 result = valcreate(fctx, addrinfo,
4848 name, rdataset->type,
4853 * Defer any further validations.
4854 * This prevents multiple validators
4855 * from manipulating fctx->rmessage
4858 valoptions |= DNS_VALIDATOR_DEFER;
4860 } else if (CHAINING(rdataset)) {
4861 if (rdataset->type == dns_rdatatype_cname)
4862 eresult = DNS_R_CNAME;
4864 INSIST(rdataset->type ==
4865 dns_rdatatype_dname);
4866 eresult = DNS_R_DNAME;
4869 } else if (!EXTERNAL(rdataset)) {
4871 * It's OK to cache this rdataset now.
4873 if (ANSWER(rdataset))
4874 addedrdataset = ardataset;
4875 else if (ANSWERSIG(rdataset))
4876 addedrdataset = asigrdataset;
4878 addedrdataset = NULL;
4879 if (CHAINING(rdataset)) {
4880 if (rdataset->type == dns_rdatatype_cname)
4881 eresult = DNS_R_CNAME;
4883 INSIST(rdataset->type ==
4884 dns_rdatatype_dname);
4885 eresult = DNS_R_DNAME;
4888 if (rdataset->trust == dns_trust_glue &&
4889 (rdataset->type == dns_rdatatype_ns ||
4890 (rdataset->type == dns_rdatatype_rrsig &&
4891 rdataset->covers == dns_rdatatype_ns))) {
4893 * If the trust level is 'dns_trust_glue'
4894 * then we are adding data from a referral
4895 * we got while executing the search algorithm.
4896 * New referral data always takes precedence
4897 * over the existing cache contents.
4899 options = DNS_DBADD_FORCE;
4903 if (ANSWER(rdataset) &&
4904 rdataset->type != dns_rdatatype_rrsig) {
4905 isc_result_t tresult;
4906 dns_name_t *noqname = NULL;
4907 tresult = findnoqname(fctx, name,
4908 rdataset->type, &noqname);
4909 if (tresult == ISC_R_SUCCESS &&
4911 tresult = dns_rdataset_addnoqname(
4913 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4918 * Now we can add the rdataset.
4920 result = dns_db_addrdataset(fctx->cache,
4926 if (result == DNS_R_UNCHANGED) {
4927 if (ANSWER(rdataset) &&
4928 ardataset != NULL &&
4929 NEGATIVE(ardataset)) {
4931 * The answer in the cache is better
4932 * than the answer we found, and is
4933 * a negative cache entry, so we
4934 * must set eresult appropriately.
4936 if (NXDOMAIN(ardataset))
4937 eresult = DNS_R_NCACHENXDOMAIN;
4939 eresult = DNS_R_NCACHENXRRSET;
4941 result = ISC_R_SUCCESS;
4942 } else if (result != ISC_R_SUCCESS)
4947 if (valrdataset != NULL)
4948 result = valcreate(fctx, addrinfo, name, fctx->type,
4949 valrdataset, valsigrdataset, valoptions,
4952 if (result == ISC_R_SUCCESS && have_answer) {
4953 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4954 if (event != NULL) {
4956 * Negative results must be indicated in event->result.
4958 if (dns_rdataset_isassociated(event->rdataset) &&
4959 NEGATIVE(event->rdataset)) {
4960 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4961 eresult == DNS_R_NCACHENXRRSET);
4963 event->result = eresult;
4964 dns_db_attach(fctx->cache, adbp);
4965 dns_db_transfernode(fctx->cache, &node, anodep);
4966 clone_results(fctx);
4971 dns_db_detachnode(fctx->cache, &node);
4976 static inline isc_result_t
4977 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4979 isc_result_t result;
4980 dns_section_t section;
4983 FCTXTRACE("cache_message");
4985 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4987 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4989 for (section = DNS_SECTION_ANSWER;
4990 section <= DNS_SECTION_ADDITIONAL;
4992 result = dns_message_firstname(fctx->rmessage, section);
4993 while (result == ISC_R_SUCCESS) {
4995 dns_message_currentname(fctx->rmessage, section,
4997 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4998 result = cache_name(fctx, name, addrinfo, now);
4999 if (result != ISC_R_SUCCESS)
5002 result = dns_message_nextname(fctx->rmessage, section);
5004 if (result != ISC_R_NOMORE)
5007 if (result == ISC_R_NOMORE)
5008 result = ISC_R_SUCCESS;
5010 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
5016 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
5019 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
5020 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
5021 isc_boolean_t optout, isc_boolean_t secure,
5022 dns_rdataset_t *ardataset, isc_result_t *eresultp)
5024 isc_result_t result;
5025 dns_rdataset_t rdataset;
5027 if (ardataset == NULL) {
5028 dns_rdataset_init(&rdataset);
5029 ardataset = &rdataset;
5032 result = dns_ncache_addoptout(message, cache, node, covers,
5033 now, maxttl, optout, ardataset);
5035 result = dns_ncache_add(message, cache, node, covers, now,
5037 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
5039 * If the cache now contains a negative entry and we
5040 * care about whether it is DNS_R_NCACHENXDOMAIN or
5041 * DNS_R_NCACHENXRRSET then extract it.
5043 if (NEGATIVE(ardataset)) {
5045 * The cache data is a negative cache entry.
5047 if (NXDOMAIN(ardataset))
5048 *eresultp = DNS_R_NCACHENXDOMAIN;
5050 *eresultp = DNS_R_NCACHENXRRSET;
5053 * Either we don't care about the nature of the
5054 * cache rdataset (because no fetch is interested
5055 * in the outcome), or the cache rdataset is not
5056 * a negative cache entry. Whichever case it is,
5057 * we can return success.
5059 * XXXRTH There's a CNAME/DNAME problem here.
5061 *eresultp = ISC_R_SUCCESS;
5063 result = ISC_R_SUCCESS;
5065 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
5066 dns_rdataset_disassociate(ardataset);
5071 static inline isc_result_t
5072 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
5073 dns_rdatatype_t covers, isc_stdtime_t now)
5075 isc_result_t result, eresult;
5077 dns_resolver_t *res;
5079 dns_dbnode_t *node, **anodep;
5080 dns_rdataset_t *ardataset;
5081 isc_boolean_t need_validation, secure_domain;
5083 dns_fetchevent_t *event;
5085 unsigned int valoptions = 0;
5087 FCTXTRACE("ncache_message");
5089 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
5092 need_validation = ISC_FALSE;
5093 POST(need_validation);
5094 secure_domain = ISC_FALSE;
5095 eresult = ISC_R_SUCCESS;
5100 * XXXMPA remove when we follow cnames and adjust the setting
5101 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
5103 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
5106 * Is DNSSEC validation required for this name?
5108 if (fctx->res->view->enablevalidation) {
5109 result = dns_view_issecuredomain(res->view, name,
5111 if (result != ISC_R_SUCCESS)
5114 if (!secure_domain && res->view->dlv != NULL) {
5115 valoptions = DNS_VALIDATOR_DLV;
5116 secure_domain = ISC_TRUE;
5120 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
5121 need_validation = ISC_FALSE;
5123 need_validation = secure_domain;
5125 if (secure_domain) {
5127 * Mark all rdatasets as pending.
5129 dns_rdataset_t *trdataset;
5132 result = dns_message_firstname(fctx->rmessage,
5133 DNS_SECTION_AUTHORITY);
5134 while (result == ISC_R_SUCCESS) {
5136 dns_message_currentname(fctx->rmessage,
5137 DNS_SECTION_AUTHORITY,
5139 for (trdataset = ISC_LIST_HEAD(tname->list);
5141 trdataset = ISC_LIST_NEXT(trdataset, link))
5142 trdataset->trust = dns_trust_pending_answer;
5143 result = dns_message_nextname(fctx->rmessage,
5144 DNS_SECTION_AUTHORITY);
5146 if (result != ISC_R_NOMORE)
5151 if (need_validation) {
5153 * Do negative response validation.
5155 result = valcreate(fctx, addrinfo, name, fctx->type,
5156 NULL, NULL, valoptions,
5157 res->buckets[fctx->bucketnum].task);
5159 * If validation is necessary, return now. Otherwise continue
5160 * to process the message, letting the validation complete
5161 * in its own good time.
5166 LOCK(&res->buckets[fctx->bucketnum].lock);
5172 if (!HAVE_ANSWER(fctx)) {
5173 event = ISC_LIST_HEAD(fctx->events);
5174 if (event != NULL) {
5176 aname = dns_fixedname_name(&event->foundname);
5177 result = dns_name_copy(name, aname, NULL);
5178 if (result != ISC_R_SUCCESS)
5180 anodep = &event->node;
5181 ardataset = event->rdataset;
5186 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
5187 if (result != ISC_R_SUCCESS)
5191 * If we are asking for a SOA record set the cache time
5192 * to zero to facilitate locating the containing zone of
5195 ttl = fctx->res->view->maxncachettl;
5196 if (fctx->type == dns_rdatatype_soa &&
5197 covers == dns_rdatatype_any &&
5198 fctx->res->zero_no_soa_ttl)
5201 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
5202 covers, now, ttl, ISC_FALSE,
5203 ISC_FALSE, ardataset, &eresult);
5204 if (result != ISC_R_SUCCESS)
5207 if (!HAVE_ANSWER(fctx)) {
5208 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
5209 if (event != NULL) {
5210 event->result = eresult;
5211 dns_db_attach(fctx->cache, adbp);
5212 dns_db_transfernode(fctx->cache, &node, anodep);
5213 clone_results(fctx);
5218 UNLOCK(&res->buckets[fctx->bucketnum].lock);
5221 dns_db_detachnode(fctx->cache, &node);
5227 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
5228 isc_boolean_t external, isc_boolean_t gluing)
5230 name->attributes |= DNS_NAMEATTR_CACHE;
5232 rdataset->trust = dns_trust_glue;
5234 * Glue with 0 TTL causes problems. We force the TTL to
5235 * 1 second to prevent this.
5237 if (rdataset->ttl == 0)
5240 rdataset->trust = dns_trust_additional;
5242 * Avoid infinite loops by only marking new rdatasets.
5244 if (!CACHE(rdataset)) {
5245 name->attributes |= DNS_NAMEATTR_CHASE;
5246 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
5248 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
5250 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
5254 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
5255 dns_section_t section)
5257 fetchctx_t *fctx = arg;
5258 isc_result_t result;
5260 dns_rdataset_t *rdataset;
5261 isc_boolean_t external;
5262 dns_rdatatype_t rtype;
5263 isc_boolean_t gluing;
5265 REQUIRE(VALID_FCTX(fctx));
5267 #if CHECK_FOR_GLUE_IN_ANSWER
5268 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
5269 return (ISC_R_SUCCESS);
5278 result = dns_message_findname(fctx->rmessage, section, addname,
5279 dns_rdatatype_any, 0, &name, NULL);
5280 if (result == ISC_R_SUCCESS) {
5281 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5282 if (type == dns_rdatatype_a) {
5283 for (rdataset = ISC_LIST_HEAD(name->list);
5285 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5286 if (rdataset->type == dns_rdatatype_rrsig)
5287 rtype = rdataset->covers;
5289 rtype = rdataset->type;
5290 if (rtype == dns_rdatatype_a ||
5291 rtype == dns_rdatatype_aaaa)
5292 mark_related(name, rdataset, external,
5296 result = dns_message_findtype(name, type, 0,
5298 if (result == ISC_R_SUCCESS) {
5299 mark_related(name, rdataset, external, gluing);
5301 * Do we have its SIG too?
5304 result = dns_message_findtype(name,
5305 dns_rdatatype_rrsig,
5307 if (result == ISC_R_SUCCESS)
5308 mark_related(name, rdataset, external,
5314 return (ISC_R_SUCCESS);
5318 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5319 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5322 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5323 #define CHECK_FOR_GLUE_IN_ANSWER 0
5325 #if CHECK_FOR_GLUE_IN_ANSWER
5327 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5328 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5333 chase_additional(fetchctx_t *fctx) {
5334 isc_boolean_t rescan;
5335 dns_section_t section = DNS_SECTION_ADDITIONAL;
5336 isc_result_t result;
5341 for (result = dns_message_firstname(fctx->rmessage, section);
5342 result == ISC_R_SUCCESS;
5343 result = dns_message_nextname(fctx->rmessage, section)) {
5344 dns_name_t *name = NULL;
5345 dns_rdataset_t *rdataset;
5346 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5348 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5350 name->attributes &= ~DNS_NAMEATTR_CHASE;
5351 for (rdataset = ISC_LIST_HEAD(name->list);
5353 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5354 if (CHASE(rdataset)) {
5355 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5356 (void)dns_rdataset_additionaldata(rdataset,
5367 static inline isc_result_t
5368 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5369 isc_result_t result;
5370 dns_rdata_t rdata = DNS_RDATA_INIT;
5371 dns_rdata_cname_t cname;
5373 result = dns_rdataset_first(rdataset);
5374 if (result != ISC_R_SUCCESS)
5376 dns_rdataset_current(rdataset, &rdata);
5377 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5378 if (result != ISC_R_SUCCESS)
5380 dns_name_init(tname, NULL);
5381 dns_name_clone(&cname.cname, tname);
5382 dns_rdata_freestruct(&cname);
5384 return (ISC_R_SUCCESS);
5387 static inline isc_result_t
5388 dname_target(dns_rdataset_t *rdataset, dns_name_t *qname,
5389 unsigned int nlabels, dns_fixedname_t *fixeddname)
5391 isc_result_t result;
5392 dns_rdata_t rdata = DNS_RDATA_INIT;
5393 dns_rdata_dname_t dname;
5394 dns_fixedname_t prefix;
5397 * Get the target name of the DNAME.
5399 result = dns_rdataset_first(rdataset);
5400 if (result != ISC_R_SUCCESS)
5402 dns_rdataset_current(rdataset, &rdata);
5403 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5404 if (result != ISC_R_SUCCESS)
5407 dns_fixedname_init(&prefix);
5408 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5409 dns_fixedname_init(fixeddname);
5410 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5412 dns_fixedname_name(fixeddname), NULL);
5413 dns_rdata_freestruct(&dname);
5417 static isc_boolean_t
5418 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5419 dns_rdataset_t *rdataset)
5421 isc_result_t result;
5422 dns_rdata_t rdata = DNS_RDATA_INIT;
5424 struct in6_addr in6a;
5425 isc_netaddr_t netaddr;
5426 char addrbuf[ISC_NETADDR_FORMATSIZE];
5427 char namebuf[DNS_NAME_FORMATSIZE];
5432 /* By default, we allow any addresses. */
5433 if (view->denyansweracl == NULL)
5437 * If the owner name matches one in the exclusion list, either exactly
5438 * or partially, allow it.
5440 if (view->answeracl_exclude != NULL) {
5441 dns_rbtnode_t *node = NULL;
5443 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5444 &node, NULL, 0, NULL, NULL);
5446 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5451 * Otherwise, search the filter list for a match for each address
5452 * record. If a match is found, the address should be filtered,
5453 * so should the entire answer.
5455 for (result = dns_rdataset_first(rdataset);
5456 result == ISC_R_SUCCESS;
5457 result = dns_rdataset_next(rdataset)) {
5458 dns_rdata_reset(&rdata);
5459 dns_rdataset_current(rdataset, &rdata);
5460 if (rdataset->type == dns_rdatatype_a) {
5461 INSIST(rdata.length == sizeof(ina.s_addr));
5462 memmove(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5463 isc_netaddr_fromin(&netaddr, &ina);
5465 INSIST(rdata.length == sizeof(in6a.s6_addr));
5466 memmove(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5467 isc_netaddr_fromin6(&netaddr, &in6a);
5470 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5471 &view->aclenv, &match, NULL);
5473 if (result == ISC_R_SUCCESS && match > 0) {
5474 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5475 dns_name_format(name, namebuf, sizeof(namebuf));
5476 dns_rdatatype_format(rdataset->type, typebuf,
5478 dns_rdataclass_format(rdataset->rdclass, classbuf,
5480 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5481 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5482 "answer address %s denied for %s/%s/%s",
5483 addrbuf, namebuf, typebuf, classbuf);
5491 static isc_boolean_t
5492 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5493 dns_rdatatype_t type, dns_name_t *tname,
5496 isc_result_t result;
5497 dns_rbtnode_t *node = NULL;
5498 char qnamebuf[DNS_NAME_FORMATSIZE];
5499 char tnamebuf[DNS_NAME_FORMATSIZE];
5503 /* By default, we allow any target name. */
5504 if (view->denyanswernames == NULL)
5508 * If the owner name matches one in the exclusion list, either exactly
5509 * or partially, allow it.
5511 if (view->answernames_exclude != NULL) {
5512 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5513 &node, NULL, 0, NULL, NULL);
5514 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5519 * If the target name is a subdomain of the search domain, allow it.
5521 if (dns_name_issubdomain(tname, domain))
5525 * Otherwise, apply filters.
5527 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5528 NULL, 0, NULL, NULL);
5529 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5530 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5531 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5532 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5533 dns_rdataclass_format(view->rdclass, classbuf,
5535 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5536 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5537 "%s target %s denied for %s/%s",
5538 typebuf, tnamebuf, qnamebuf, classbuf);
5546 trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) {
5547 char ns_namebuf[DNS_NAME_FORMATSIZE];
5548 char namebuf[DNS_NAME_FORMATSIZE];
5549 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5551 if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) {
5552 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf));
5553 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
5554 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf));
5556 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5557 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
5558 "fctx %p: trimming ttl of %s/NS for %s/%s: "
5559 "%u -> %u", fctx, ns_namebuf, namebuf, tbuf,
5560 rdataset->ttl, fctx->ns_ttl);
5561 rdataset->ttl = fctx->ns_ttl;
5566 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5567 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5568 * section for the NS RRset if the query type is NS; if it has
5569 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5570 * section for A and AAAA queries.
5572 #define LOOK_FOR_NS_IN_ANSWER 0x1
5573 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5576 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5577 unsigned int look_in_options)
5579 isc_result_t result;
5580 dns_message_t *message;
5581 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name, *save_name;
5582 dns_rdataset_t *rdataset, *ns_rdataset;
5583 isc_boolean_t aa, negative_response;
5584 dns_rdatatype_t type, save_type;
5585 dns_section_t section;
5587 FCTXTRACE("noanswer_response");
5589 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5590 INSIST(fctx->type == dns_rdatatype_ns);
5591 section = DNS_SECTION_ANSWER;
5593 section = DNS_SECTION_AUTHORITY;
5595 message = fctx->rmessage;
5600 if (oqname == NULL) {
5602 * We have a normal, non-chained negative response or
5605 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5609 qname = &fctx->name;
5612 * We're being invoked by answer_response() after it has
5613 * followed a CNAME/DNAME chain.
5618 * If the current qname is not a subdomain of the query
5619 * domain, there's no point in looking at the authority
5620 * section without doing DNSSEC validation.
5622 * Until we do that validation, we'll just return success
5625 if (!dns_name_issubdomain(qname, &fctx->domain))
5626 return (ISC_R_SUCCESS);
5630 * We have to figure out if this is a negative response, or a
5635 * Sometimes we can tell if its a negative response by looking at
5636 * the message header.
5638 negative_response = ISC_FALSE;
5639 if (message->rcode == dns_rcode_nxdomain ||
5640 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5641 message->counts[DNS_SECTION_AUTHORITY] == 0))
5642 negative_response = ISC_TRUE;
5645 * Process the authority section.
5652 save_type = dns_rdatatype_none;
5653 result = dns_message_firstname(message, section);
5654 while (result == ISC_R_SUCCESS) {
5656 dns_message_currentname(message, section, &name);
5657 if (dns_name_issubdomain(name, &fctx->domain)) {
5659 * Look for NS/SOA RRsets first.
5661 for (rdataset = ISC_LIST_HEAD(name->list);
5663 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5664 type = rdataset->type;
5665 if (type == dns_rdatatype_rrsig)
5666 type = rdataset->covers;
5667 if (((type == dns_rdatatype_ns ||
5668 type == dns_rdatatype_soa) &&
5669 !dns_name_issubdomain(qname, name))) {
5670 char qbuf[DNS_NAME_FORMATSIZE];
5671 char nbuf[DNS_NAME_FORMATSIZE];
5672 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5673 dns_rdatatype_format(fctx->type, tbuf,
5675 dns_name_format(name, nbuf,
5677 dns_name_format(qname, qbuf,
5680 "unrelated %s %s in "
5681 "%s authority section",
5683 return (DNS_R_FORMERR);
5685 if (type == dns_rdatatype_ns) {
5689 * Only one set of NS RRs is allowed.
5691 if (rdataset->type ==
5693 if (ns_name != NULL &&
5700 return (DNS_R_FORMERR);
5703 ns_rdataset = rdataset;
5707 rdataset->attributes |=
5708 DNS_RDATASETATTR_CACHE;
5709 rdataset->trust = dns_trust_glue;
5711 if (type == dns_rdatatype_soa) {
5713 * SOA, or RRSIG SOA.
5715 * Only one SOA is allowed.
5717 if (rdataset->type ==
5718 dns_rdatatype_soa) {
5719 if (soa_name != NULL &&
5726 return (DNS_R_FORMERR);
5731 DNS_NAMEATTR_NCACHE;
5732 rdataset->attributes |=
5733 DNS_RDATASETATTR_NCACHE;
5736 dns_trust_authauthority;
5737 else if (ISFORWARDER(fctx->addrinfo))
5742 dns_trust_additional;
5746 result = dns_message_nextname(message, section);
5747 if (result == ISC_R_NOMORE)
5749 else if (result != ISC_R_SUCCESS)
5753 log_ns_ttl(fctx, "noanswer_response");
5755 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
5756 !dns_name_equal(ns_name, dns_rootname))
5757 trim_ns_ttl(fctx, ns_name, ns_rdataset);
5760 * A negative response has a SOA record (Type 2)
5761 * and a optional NS RRset (Type 1) or it has neither
5762 * a SOA or a NS RRset (Type 3, handled above) or
5763 * rcode is NXDOMAIN (handled above) in which case
5764 * the NS RRset is allowed (Type 4).
5766 if (soa_name != NULL)
5767 negative_response = ISC_TRUE;
5769 result = dns_message_firstname(message, section);
5770 while (result == ISC_R_SUCCESS) {
5772 dns_message_currentname(message, section, &name);
5773 if (dns_name_issubdomain(name, &fctx->domain)) {
5774 for (rdataset = ISC_LIST_HEAD(name->list);
5776 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5777 type = rdataset->type;
5778 if (type == dns_rdatatype_rrsig)
5779 type = rdataset->covers;
5780 if (type == dns_rdatatype_nsec ||
5781 type == dns_rdatatype_nsec3) {
5783 * NSEC or RRSIG NSEC.
5785 if (negative_response) {
5787 DNS_NAMEATTR_NCACHE;
5788 rdataset->attributes |=
5789 DNS_RDATASETATTR_NCACHE;
5790 } else if (type == dns_rdatatype_nsec) {
5793 rdataset->attributes |=
5794 DNS_RDATASETATTR_CACHE;
5798 dns_trust_authauthority;
5799 else if (ISFORWARDER(fctx->addrinfo))
5804 dns_trust_additional;
5806 * No additional data needs to be
5809 } else if (type == dns_rdatatype_ds) {
5813 * These should only be here if
5814 * this is a referral, and there
5815 * should only be one DS RRset.
5817 if (ns_name == NULL) {
5821 return (DNS_R_FORMERR);
5823 if (rdataset->type ==
5825 if (ds_name != NULL &&
5832 return (DNS_R_FORMERR);
5838 rdataset->attributes |=
5839 DNS_RDATASETATTR_CACHE;
5842 dns_trust_authauthority;
5843 else if (ISFORWARDER(fctx->addrinfo))
5848 dns_trust_additional;
5853 save_type = ISC_LIST_HEAD(name->list)->type;
5855 result = dns_message_nextname(message, section);
5856 if (result == ISC_R_NOMORE)
5858 else if (result != ISC_R_SUCCESS)
5863 * Trigger lookups for DNS nameservers.
5865 if (negative_response && message->rcode == dns_rcode_noerror &&
5866 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5867 dns_name_equal(soa_name, qname) &&
5868 !dns_name_equal(qname, dns_rootname))
5869 return (DNS_R_CHASEDSSERVERS);
5872 * Did we find anything?
5874 if (!negative_response && ns_name == NULL) {
5878 if (oqname != NULL) {
5880 * We've already got a partial CNAME/DNAME chain,
5881 * and haven't found else anything useful here, but
5882 * no error has occurred since we have an answer.
5884 return (ISC_R_SUCCESS);
5887 * The responder is insane.
5889 if (save_name == NULL) {
5890 log_formerr(fctx, "invalid response");
5891 return (DNS_R_FORMERR);
5893 if (!dns_name_issubdomain(save_name, &fctx->domain)) {
5894 char nbuf[DNS_NAME_FORMATSIZE];
5895 char dbuf[DNS_NAME_FORMATSIZE];
5896 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5898 dns_rdatatype_format(save_type, tbuf,
5900 dns_name_format(save_name, nbuf, sizeof(nbuf));
5901 dns_name_format(&fctx->domain, dbuf,
5904 log_formerr(fctx, "Name %s (%s) not subdomain"
5905 " of zone %s -- invalid response",
5908 log_formerr(fctx, "invalid response");
5910 return (DNS_R_FORMERR);
5915 * If we found both NS and SOA, they should be the same name.
5917 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5918 log_formerr(fctx, "NS/SOA mismatch");
5919 return (DNS_R_FORMERR);
5923 * Do we have a referral? (We only want to follow a referral if
5924 * we're not following a chain.)
5926 if (!negative_response && ns_name != NULL && oqname == NULL) {
5928 * We already know ns_name is a subdomain of fctx->domain.
5929 * If ns_name is equal to fctx->domain, we're not making
5930 * progress. We return DNS_R_FORMERR so that we'll keep
5931 * trying other servers.
5933 if (dns_name_equal(ns_name, &fctx->domain)) {
5934 log_formerr(fctx, "non-improving referral");
5935 return (DNS_R_FORMERR);
5939 * If the referral name is not a parent of the query
5940 * name, consider the responder insane.
5942 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5944 log_formerr(fctx, "referral to non-parent");
5945 FCTXTRACE("referral to non-parent");
5946 return (DNS_R_FORMERR);
5950 * Mark any additional data related to this rdataset.
5951 * It's important that we do this before we change the
5954 INSIST(ns_rdataset != NULL);
5955 fctx->attributes |= FCTX_ATTR_GLUING;
5956 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5958 #if CHECK_FOR_GLUE_IN_ANSWER
5960 * Look in the answer section for "glue" that is incorrectly
5961 * returned as a answer. This is needed if the server also
5962 * minimizes the response size by not adding records to the
5963 * additional section that are in the answer section or if
5964 * the record gets dropped due to message size constraints.
5966 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
5967 (fctx->type == dns_rdatatype_aaaa ||
5968 fctx->type == dns_rdatatype_a))
5969 (void)dns_rdataset_additionaldata(ns_rdataset,
5970 check_answer, fctx);
5972 fctx->attributes &= ~FCTX_ATTR_GLUING;
5974 * NS rdatasets with 0 TTL cause problems.
5975 * dns_view_findzonecut() will not find them when we
5976 * try to follow the referral, and we'll SERVFAIL
5977 * because the best nameservers are now above QDOMAIN.
5978 * We force the TTL to 1 second to prevent this.
5980 if (ns_rdataset->ttl == 0)
5981 ns_rdataset->ttl = 1;
5983 * Set the current query domain to the referral name.
5985 * XXXRTH We should check if we're in forward-only mode, and
5986 * if so we should bail out.
5988 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5989 dns_name_free(&fctx->domain, fctx->mctx);
5990 if (dns_rdataset_isassociated(&fctx->nameservers))
5991 dns_rdataset_disassociate(&fctx->nameservers);
5992 dns_name_init(&fctx->domain, NULL);
5993 result = dns_name_dup(ns_name, fctx->mctx, &fctx->domain);
5994 if (result != ISC_R_SUCCESS)
5996 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5997 fctx->ns_ttl_ok = ISC_FALSE;
5998 log_ns_ttl(fctx, "DELEGATION");
5999 return (DNS_R_DELEGATION);
6003 * Since we're not doing a referral, we don't want to cache any
6004 * NS RRs we may have found.
6006 if (ns_name != NULL)
6007 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
6009 if (negative_response && oqname == NULL)
6010 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
6012 return (ISC_R_SUCCESS);
6016 answer_response(fetchctx_t *fctx) {
6017 isc_result_t result;
6018 dns_message_t *message;
6019 dns_name_t *name, *dname = NULL, *qname, tname, *ns_name;
6020 dns_rdataset_t *rdataset, *ns_rdataset;
6021 isc_boolean_t done, external, chaining, aa, found, want_chaining;
6022 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
6024 dns_rdatatype_t type;
6025 dns_fixedname_t fdname, fqname;
6028 FCTXTRACE("answer_response");
6030 message = fctx->rmessage;
6033 * Examine the answer section, marking those rdatasets which are
6034 * part of the answer and should be cached.
6038 found_cname = ISC_FALSE;
6039 found_type = ISC_FALSE;
6040 chaining = ISC_FALSE;
6041 have_answer = ISC_FALSE;
6042 want_chaining = ISC_FALSE;
6043 POST(want_chaining);
6044 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
6048 qname = &fctx->name;
6050 view = fctx->res->view;
6051 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
6052 while (!done && result == ISC_R_SUCCESS) {
6053 dns_namereln_t namereln;
6055 unsigned int nlabels;
6058 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
6059 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6060 namereln = dns_name_fullcompare(qname, name, &order, &nlabels);
6061 if (namereln == dns_namereln_equal) {
6062 wanted_chaining = ISC_FALSE;
6063 for (rdataset = ISC_LIST_HEAD(name->list);
6065 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6067 want_chaining = ISC_FALSE;
6069 if (rdataset->type == dns_rdatatype_nsec3) {
6071 * NSEC3 records are not allowed to
6072 * appear in the answer section.
6074 log_formerr(fctx, "NSEC3 in answer");
6075 return (DNS_R_FORMERR);
6079 * Apply filters, if given, on answers to reject
6080 * a malicious attempt of rebinding.
6082 if ((rdataset->type == dns_rdatatype_a ||
6083 rdataset->type == dns_rdatatype_aaaa) &&
6084 !is_answeraddress_allowed(view, name,
6086 return (DNS_R_SERVFAIL);
6089 if (rdataset->type == type && !found_cname) {
6091 * We've found an ordinary answer.
6094 found_type = ISC_TRUE;
6096 aflag = DNS_RDATASETATTR_ANSWER;
6097 } else if (type == dns_rdatatype_any) {
6099 * We've found an answer matching
6100 * an ANY query. There may be
6104 aflag = DNS_RDATASETATTR_ANSWER;
6105 } else if (rdataset->type == dns_rdatatype_rrsig
6106 && rdataset->covers == type
6109 * We've found a signature that
6110 * covers the type we're looking for.
6113 found_type = ISC_TRUE;
6114 aflag = DNS_RDATASETATTR_ANSWERSIG;
6115 } else if (rdataset->type ==
6119 * We're looking for something else,
6120 * but we found a CNAME.
6122 * Getting a CNAME response for some
6123 * query types is an error, see
6124 * RFC 4035, Section 2.5.
6126 if (type == dns_rdatatype_rrsig ||
6127 type == dns_rdatatype_key ||
6128 type == dns_rdatatype_nsec) {
6129 char buf[DNS_RDATATYPE_FORMATSIZE];
6130 dns_rdatatype_format(fctx->type,
6135 return (DNS_R_FORMERR);
6138 found_cname = ISC_TRUE;
6139 want_chaining = ISC_TRUE;
6140 aflag = DNS_RDATASETATTR_ANSWER;
6141 result = cname_target(rdataset,
6143 if (result != ISC_R_SUCCESS)
6145 /* Apply filters on the target name. */
6146 if (!is_answertarget_allowed(view,
6151 return (DNS_R_SERVFAIL);
6153 } else if (rdataset->type == dns_rdatatype_rrsig
6154 && rdataset->covers ==
6158 * We're looking for something else,
6159 * but we found a SIG CNAME.
6162 found_cname = ISC_TRUE;
6163 aflag = DNS_RDATASETATTR_ANSWERSIG;
6168 * We've found an answer to our
6173 rdataset->attributes |=
6174 DNS_RDATASETATTR_CACHE;
6175 rdataset->trust = dns_trust_answer;
6178 * This data is "the" answer
6179 * to our question only if
6180 * we're not chaining (i.e.
6181 * if we haven't followed
6182 * a CNAME or DNAME).
6186 DNS_RDATASETATTR_ANSWER) {
6187 have_answer = ISC_TRUE;
6189 DNS_NAMEATTR_ANSWER;
6191 rdataset->attributes |= aflag;
6194 dns_trust_authanswer;
6195 } else if (external) {
6197 * This data is outside of
6198 * our query domain, and
6199 * may not be cached.
6201 rdataset->attributes |=
6202 DNS_RDATASETATTR_EXTERNAL;
6206 * Mark any additional data related
6209 (void)dns_rdataset_additionaldata(
6217 if (want_chaining) {
6218 wanted_chaining = ISC_TRUE;
6220 DNS_NAMEATTR_CHAINING;
6221 rdataset->attributes |=
6222 DNS_RDATASETATTR_CHAINING;
6227 * We could add an "else" clause here and
6228 * log that we're ignoring this rdataset.
6232 * If wanted_chaining is true, we've done
6233 * some chaining as the result of processing
6234 * this node, and thus we need to set
6237 * We don't set chaining inside of the
6238 * rdataset loop because doing that would
6239 * cause us to ignore the signatures of
6242 if (wanted_chaining)
6243 chaining = ISC_TRUE;
6245 dns_rdataset_t *dnameset = NULL;
6248 * Look for a DNAME (or its SIG). Anything else is
6251 wanted_chaining = ISC_FALSE;
6252 for (rdataset = ISC_LIST_HEAD(name->list);
6254 rdataset = ISC_LIST_NEXT(rdataset, link))
6257 * Only pass DNAME or RRSIG(DNAME).
6259 if (rdataset->type != dns_rdatatype_dname &&
6260 (rdataset->type != dns_rdatatype_rrsig ||
6261 rdataset->covers != dns_rdatatype_dname))
6265 * If we're not chaining, then the DNAME and
6266 * its signature should not be external.
6268 if (!chaining && external) {
6269 char qbuf[DNS_NAME_FORMATSIZE];
6270 char obuf[DNS_NAME_FORMATSIZE];
6272 dns_name_format(name, qbuf,
6274 dns_name_format(&fctx->domain, obuf,
6276 log_formerr(fctx, "external DNAME or "
6277 "RRSIG covering DNAME "
6279 "not in %s", qbuf, obuf);
6280 return (DNS_R_FORMERR);
6283 if (namereln != dns_namereln_subdomain) {
6284 char qbuf[DNS_NAME_FORMATSIZE];
6285 char obuf[DNS_NAME_FORMATSIZE];
6287 dns_name_format(qname, qbuf,
6289 dns_name_format(name, obuf,
6291 log_formerr(fctx, "unrelated DNAME "
6293 "not in %s", qbuf, obuf);
6294 return (DNS_R_FORMERR);
6298 if (rdataset->type == dns_rdatatype_dname) {
6299 want_chaining = ISC_TRUE;
6300 POST(want_chaining);
6301 aflag = DNS_RDATASETATTR_ANSWER;
6302 result = dname_target(rdataset, qname,
6304 if (result == ISC_R_NOSPACE) {
6306 * We can't construct the
6307 * DNAME target. Do not
6310 want_chaining = ISC_FALSE;
6311 POST(want_chaining);
6312 } else if (result != ISC_R_SUCCESS)
6315 dnameset = rdataset;
6317 dname = dns_fixedname_name(&fdname);
6318 if (!is_answertarget_allowed(view,
6319 qname, rdataset->type,
6320 dname, &fctx->domain)) {
6321 return (DNS_R_SERVFAIL);
6325 * We've found a signature that
6328 aflag = DNS_RDATASETATTR_ANSWERSIG;
6332 * We've found an answer to our
6335 name->attributes |= DNS_NAMEATTR_CACHE;
6336 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
6337 rdataset->trust = dns_trust_answer;
6340 * This data is "the" answer to
6341 * our question only if we're
6345 if (aflag == DNS_RDATASETATTR_ANSWER) {
6346 have_answer = ISC_TRUE;
6348 DNS_NAMEATTR_ANSWER;
6350 rdataset->attributes |= aflag;
6353 dns_trust_authanswer;
6354 } else if (external) {
6355 rdataset->attributes |=
6356 DNS_RDATASETATTR_EXTERNAL;
6363 if (dnameset != NULL) {
6365 * Copy the dname into the qname fixed name.
6367 * Although we check for failure of the copy
6368 * operation, in practice it should never fail
6369 * since we already know that the result fits
6372 dns_fixedname_init(&fqname);
6373 qname = dns_fixedname_name(&fqname);
6374 result = dns_name_copy(dname, qname, NULL);
6375 if (result != ISC_R_SUCCESS)
6377 wanted_chaining = ISC_TRUE;
6378 name->attributes |= DNS_NAMEATTR_CHAINING;
6379 dnameset->attributes |=
6380 DNS_RDATASETATTR_CHAINING;
6382 if (wanted_chaining)
6383 chaining = ISC_TRUE;
6385 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6387 if (result == ISC_R_NOMORE)
6388 result = ISC_R_SUCCESS;
6389 if (result != ISC_R_SUCCESS)
6393 * We should have found an answer.
6396 log_formerr(fctx, "reply has no answer");
6397 return (DNS_R_FORMERR);
6401 * This response is now potentially cacheable.
6403 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6406 * Did chaining end before we got the final answer?
6410 * Yes. This may be a negative reply, so hand off
6411 * authority section processing to the noanswer code.
6412 * If it isn't a noanswer response, no harm will be
6415 return (noanswer_response(fctx, qname, 0));
6419 * We didn't end with an incomplete chain, so the rcode should be
6422 if (message->rcode != dns_rcode_noerror) {
6423 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6425 return (DNS_R_FORMERR);
6429 * Examine the authority section (if there is one).
6431 * We expect there to be only one owner name for all the rdatasets
6432 * in this section, and we expect that it is not external.
6437 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6438 while (!done && result == ISC_R_SUCCESS) {
6440 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6441 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6444 * We expect to find NS or SIG NS rdatasets, and
6447 for (rdataset = ISC_LIST_HEAD(name->list);
6449 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6450 if (rdataset->type == dns_rdatatype_ns ||
6451 (rdataset->type == dns_rdatatype_rrsig &&
6452 rdataset->covers == dns_rdatatype_ns)) {
6455 rdataset->attributes |=
6456 DNS_RDATASETATTR_CACHE;
6457 if (aa && !chaining)
6459 dns_trust_authauthority;
6462 dns_trust_additional;
6464 if (rdataset->type == dns_rdatatype_ns) {
6466 ns_rdataset = rdataset;
6469 * Mark any additional data related
6472 (void)dns_rdataset_additionaldata(
6480 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6482 if (result == ISC_R_NOMORE)
6483 result = ISC_R_SUCCESS;
6485 log_ns_ttl(fctx, "answer_response");
6487 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6488 !dns_name_equal(ns_name, dns_rootname))
6489 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6494 static isc_boolean_t
6495 fctx_decreference(fetchctx_t *fctx) {
6496 isc_boolean_t bucket_empty = ISC_FALSE;
6498 INSIST(fctx->references > 0);
6500 if (fctx->references == 0) {
6502 * No one cares about the result of this fetch anymore.
6504 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6505 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6507 * This fctx is already shutdown; we were just
6508 * waiting for the last reference to go away.
6510 bucket_empty = fctx_unlink(fctx);
6514 * Initiate shutdown.
6516 fctx_shutdown(fctx);
6519 return (bucket_empty);
6523 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6524 dns_fetchevent_t *fevent;
6525 dns_resolver_t *res;
6527 isc_result_t result;
6528 isc_boolean_t bucket_empty;
6529 isc_boolean_t locked = ISC_FALSE;
6530 unsigned int bucketnum;
6531 dns_rdataset_t nameservers;
6532 dns_fixedname_t fixed;
6535 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6536 fevent = (dns_fetchevent_t *)event;
6537 fctx = event->ev_arg;
6538 REQUIRE(VALID_FCTX(fctx));
6542 FCTXTRACE("resume_dslookup");
6544 if (fevent->node != NULL)
6545 dns_db_detachnode(fevent->db, &fevent->node);
6546 if (fevent->db != NULL)
6547 dns_db_detach(&fevent->db);
6549 dns_rdataset_init(&nameservers);
6551 bucketnum = fctx->bucketnum;
6552 if (fevent->result == ISC_R_CANCELED) {
6553 dns_resolver_destroyfetch(&fctx->nsfetch);
6554 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6555 } else if (fevent->result == ISC_R_SUCCESS) {
6557 FCTXTRACE("resuming DS lookup");
6559 dns_resolver_destroyfetch(&fctx->nsfetch);
6560 if (dns_rdataset_isassociated(&fctx->nameservers))
6561 dns_rdataset_disassociate(&fctx->nameservers);
6562 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6563 fctx->ns_ttl = fctx->nameservers.ttl;
6564 fctx->ns_ttl_ok = ISC_TRUE;
6565 log_ns_ttl(fctx, "resume_dslookup");
6566 dns_name_free(&fctx->domain, fctx->mctx);
6567 dns_name_init(&fctx->domain, NULL);
6568 result = dns_name_dup(&fctx->nsname, fctx->mctx, &fctx->domain);
6569 if (result != ISC_R_SUCCESS) {
6570 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6576 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6579 dns_rdataset_t *nsrdataset = NULL;
6582 * Retrieve state from fctx->nsfetch before we destroy it.
6584 dns_fixedname_init(&fixed);
6585 domain = dns_fixedname_name(&fixed);
6586 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6587 if (dns_name_equal(&fctx->nsname, domain)) {
6588 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6589 dns_resolver_destroyfetch(&fctx->nsfetch);
6592 if (dns_rdataset_isassociated(
6593 &fctx->nsfetch->private->nameservers)) {
6595 &fctx->nsfetch->private->nameservers,
6597 nsrdataset = &nameservers;
6600 dns_resolver_destroyfetch(&fctx->nsfetch);
6601 n = dns_name_countlabels(&fctx->nsname);
6602 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6605 if (dns_rdataset_isassociated(fevent->rdataset))
6606 dns_rdataset_disassociate(fevent->rdataset);
6607 FCTXTRACE("continuing to look for parent's NS records");
6608 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6609 dns_rdatatype_ns, domain,
6610 nsrdataset, NULL, 0, task,
6611 resume_dslookup, fctx,
6612 &fctx->nsrrset, NULL,
6614 if (result != ISC_R_SUCCESS)
6615 fctx_done(fctx, result, __LINE__);
6617 LOCK(&res->buckets[bucketnum].lock);
6624 if (dns_rdataset_isassociated(&nameservers))
6625 dns_rdataset_disassociate(&nameservers);
6626 if (dns_rdataset_isassociated(fevent->rdataset))
6627 dns_rdataset_disassociate(fevent->rdataset);
6628 INSIST(fevent->sigrdataset == NULL);
6629 isc_event_free(&event);
6631 LOCK(&res->buckets[bucketnum].lock);
6632 bucket_empty = fctx_decreference(fctx);
6633 UNLOCK(&res->buckets[bucketnum].lock);
6639 checknamessection(dns_message_t *message, dns_section_t section) {
6640 isc_result_t result;
6642 dns_rdata_t rdata = DNS_RDATA_INIT;
6643 dns_rdataset_t *rdataset;
6645 for (result = dns_message_firstname(message, section);
6646 result == ISC_R_SUCCESS;
6647 result = dns_message_nextname(message, section))
6650 dns_message_currentname(message, section, &name);
6651 for (rdataset = ISC_LIST_HEAD(name->list);
6653 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6654 for (result = dns_rdataset_first(rdataset);
6655 result == ISC_R_SUCCESS;
6656 result = dns_rdataset_next(rdataset)) {
6657 dns_rdataset_current(rdataset, &rdata);
6658 if (!dns_rdata_checkowner(name, rdata.rdclass,
6661 !dns_rdata_checknames(&rdata, name, NULL))
6663 rdataset->attributes |=
6664 DNS_RDATASETATTR_CHECKNAMES;
6666 dns_rdata_reset(&rdata);
6673 checknames(dns_message_t *message) {
6675 checknamessection(message, DNS_SECTION_ANSWER);
6676 checknamessection(message, DNS_SECTION_AUTHORITY);
6677 checknamessection(message, DNS_SECTION_ADDITIONAL);
6681 * Log server NSID at log level 'level'
6684 log_nsid(isc_buffer_t *opt, size_t nsid_len, resquery_t *query,
6685 int level, isc_mem_t *mctx)
6687 static const char hex[17] = "0123456789abcdef";
6688 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6689 isc_uint16_t buflen, i;
6690 unsigned char *p, *buf, *nsid;
6692 /* Allocate buffer for storing hex version of the NSID */
6693 buflen = (isc_uint16_t)nsid_len * 2 + 1;
6694 buf = isc_mem_get(mctx, buflen);
6698 /* Convert to hex */
6700 nsid = isc_buffer_current(opt);
6701 for (i = 0; i < nsid_len; i++) {
6702 *p++ = hex[(nsid[0] >> 4) & 0xf];
6703 *p++ = hex[nsid[0] & 0xf];
6708 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6710 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6711 DNS_LOGMODULE_RESOLVER, level,
6712 "received NSID '%s' from %s", buf, addrbuf);
6715 isc_mem_put(mctx, buf, buflen);
6720 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6721 isc_buffer_t buffer;
6724 isc_result_t result;
6726 if (! isc_log_wouldlog(dns_lctx, level))
6730 * Note that these are multiline debug messages. We want a newline
6731 * to appear in the log after each message.
6735 buf = isc_mem_get(mctx, len);
6738 isc_buffer_init(&buffer, buf, len);
6739 result = dns_message_totext(message, &dns_master_style_debug,
6741 if (result == ISC_R_NOSPACE) {
6742 isc_mem_put(mctx, buf, len);
6744 } else if (result == ISC_R_SUCCESS)
6745 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6746 DNS_LOGMODULE_RESOLVER, level,
6747 "received packet:\n%.*s",
6748 (int)isc_buffer_usedlength(&buffer),
6750 } while (result == ISC_R_NOSPACE);
6753 isc_mem_put(mctx, buf, len);
6756 static isc_boolean_t
6757 iscname(fetchctx_t *fctx) {
6758 isc_result_t result;
6760 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6761 &fctx->name, dns_rdatatype_cname, 0,
6763 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6766 static isc_boolean_t
6767 betterreferral(fetchctx_t *fctx) {
6768 isc_result_t result;
6770 dns_rdataset_t *rdataset;
6771 dns_message_t *message = fctx->rmessage;
6773 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6774 result == ISC_R_SUCCESS;
6775 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6777 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6778 if (!isstrictsubdomain(name, &fctx->domain))
6780 for (rdataset = ISC_LIST_HEAD(name->list);
6782 rdataset = ISC_LIST_NEXT(rdataset, link))
6783 if (rdataset->type == dns_rdatatype_ns)
6790 process_opt(resquery_t *query, dns_rdataset_t *opt) {
6792 isc_buffer_t optbuf;
6793 isc_result_t result;
6794 isc_uint16_t optcode;
6795 isc_uint16_t optlen;
6797 result = dns_rdataset_first(opt);
6798 if (result == ISC_R_SUCCESS) {
6799 dns_rdata_init(&rdata);
6800 dns_rdataset_current(opt, &rdata);
6801 isc_buffer_init(&optbuf, rdata.data, rdata.length);
6802 isc_buffer_add(&optbuf, rdata.length);
6803 while (isc_buffer_remaininglength(&optbuf) >= 4) {
6804 optcode = isc_buffer_getuint16(&optbuf);
6805 optlen = isc_buffer_getuint16(&optbuf);
6806 INSIST(optlen <= isc_buffer_remaininglength(&optbuf));
6809 if (query->options & DNS_FETCHOPT_WANTNSID)
6810 log_nsid(&optbuf, optlen, query,
6812 query->fctx->res->mctx);
6813 isc_buffer_forward(&optbuf, optlen);
6816 isc_buffer_forward(&optbuf, optlen);
6820 INSIST(isc_buffer_remaininglength(&optbuf) == 0U);
6825 resquery_response(isc_task_t *task, isc_event_t *event) {
6826 isc_result_t result = ISC_R_SUCCESS;
6827 resquery_t *query = event->ev_arg;
6828 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6829 isc_boolean_t keep_trying, get_nameservers, resend;
6830 isc_boolean_t truncated;
6831 dns_message_t *message;
6832 dns_rdataset_t *opt;
6835 dns_fixedname_t foundname;
6837 isc_time_t tnow, *finish;
6838 dns_adbaddrinfo_t *addrinfo;
6839 unsigned int options;
6840 unsigned int findoptions;
6841 isc_result_t broken_server;
6842 badnstype_t broken_type = badns_response;
6843 isc_boolean_t no_response;
6845 REQUIRE(VALID_QUERY(query));
6847 options = query->options;
6848 REQUIRE(VALID_FCTX(fctx));
6849 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6853 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6854 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6856 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6858 (void)isc_timer_touch(fctx->timer);
6860 keep_trying = ISC_FALSE;
6861 broken_server = ISC_R_SUCCESS;
6862 get_nameservers = ISC_FALSE;
6864 truncated = ISC_FALSE;
6866 no_response = ISC_FALSE;
6868 if (fctx->res->exiting) {
6869 result = ISC_R_SHUTTINGDOWN;
6874 fctx->timeout = ISC_FALSE;
6875 fctx->addrinfo = query->addrinfo;
6878 * XXXRTH We should really get the current time just once. We
6879 * need a routine to convert from an isc_time_t to an
6884 isc_stdtime_get(&now);
6887 * Did the dispatcher have a problem?
6889 if (devent->result != ISC_R_SUCCESS) {
6890 if (devent->result == ISC_R_EOF &&
6891 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6893 * The problem might be that they
6894 * don't understand EDNS0. Turn it
6895 * off and try again.
6897 options |= DNS_FETCHOPT_NOEDNS0;
6899 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6902 * There's no hope for this query.
6904 keep_trying = ISC_TRUE;
6907 * If this is a network error on an exclusive query
6908 * socket, mark the server as bad so that we won't try
6909 * it for this fetch again. Also adjust finish and
6910 * no_response so that we penalize this address in SRTT
6913 if (query->exclusivesocket &&
6914 (devent->result == ISC_R_HOSTUNREACH ||
6915 devent->result == ISC_R_NETUNREACH ||
6916 devent->result == ISC_R_CONNREFUSED ||
6917 devent->result == ISC_R_CANCELED)) {
6918 broken_server = devent->result;
6919 broken_type = badns_unreachable;
6921 no_response = ISC_TRUE;
6927 message = fctx->rmessage;
6929 if (query->tsig != NULL) {
6930 result = dns_message_setquerytsig(message, query->tsig);
6931 if (result != ISC_R_SUCCESS)
6935 if (query->tsigkey) {
6936 result = dns_message_settsigkey(message, query->tsigkey);
6937 if (result != ISC_R_SUCCESS)
6941 dns_message_setclass(message, fctx->res->rdclass);
6943 result = dns_message_parse(message, &devent->buffer, 0);
6944 if (result != ISC_R_SUCCESS) {
6946 case ISC_R_UNEXPECTEDEND:
6947 if (!message->question_ok ||
6948 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6949 (options & DNS_FETCHOPT_TCP) != 0) {
6951 * Either the message ended prematurely,
6952 * and/or wasn't marked as being truncated,
6953 * and/or this is a response to a query we
6954 * sent over TCP. In all of these cases,
6955 * something is wrong with the remote
6956 * server and we don't want to retry using
6959 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6962 * The problem might be that they
6963 * don't understand EDNS0. Turn it
6964 * off and try again.
6966 options |= DNS_FETCHOPT_NOEDNS0;
6969 &query->addrinfo->sockaddr);
6970 inc_stats(fctx->res,
6971 dns_resstatscounter_edns0fail);
6973 broken_server = result;
6974 keep_trying = ISC_TRUE;
6979 * We defer retrying via TCP for a bit so we can
6980 * check out this message further.
6982 truncated = ISC_TRUE;
6985 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6987 * The problem might be that they
6988 * don't understand EDNS0. Turn it
6989 * off and try again.
6991 options |= DNS_FETCHOPT_NOEDNS0;
6993 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6994 inc_stats(fctx->res,
6995 dns_resstatscounter_edns0fail);
6997 broken_server = DNS_R_UNEXPECTEDRCODE;
6998 keep_trying = ISC_TRUE;
7003 * Something bad has happened.
7011 * Log the incoming packet.
7013 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
7015 if (message->rdclass != fctx->res->rdclass) {
7017 FCTXTRACE("bad class");
7022 * Process receive opt record.
7024 opt = dns_message_getopt(message);
7026 process_opt(query, opt);
7029 * If the message is signed, check the signature. If not, this
7030 * returns success anyway.
7032 result = dns_message_checksig(message, fctx->res->view);
7033 if (result != ISC_R_SUCCESS)
7037 * The dispatcher should ensure we only get responses with QR set.
7039 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
7041 * INSIST() that the message comes from the place we sent it to,
7042 * since the dispatch code should ensure this.
7044 * INSIST() that the message id is correct (this should also be
7045 * ensured by the dispatch code).
7049 * We have an affirmative response to the query and we have
7050 * previously got a response from this server which indicated
7051 * EDNS may not be supported so we can now cache the lack of
7055 (message->rcode == dns_rcode_noerror ||
7056 message->rcode == dns_rcode_nxdomain ||
7057 message->rcode == dns_rcode_refused ||
7058 message->rcode == dns_rcode_yxdomain) &&
7059 bad_edns(fctx, &query->addrinfo->sockaddr)) {
7060 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7061 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7063 dns_adb_changeflags(fctx->adb, query->addrinfo,
7064 DNS_FETCHOPT_NOEDNS0,
7065 DNS_FETCHOPT_NOEDNS0);
7069 * Deal with truncated responses by retrying using TCP.
7071 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
7072 truncated = ISC_TRUE;
7075 inc_stats(fctx->res, dns_resstatscounter_truncated);
7076 if ((options & DNS_FETCHOPT_TCP) != 0) {
7077 broken_server = DNS_R_TRUNCATEDTCP;
7078 keep_trying = ISC_TRUE;
7080 options |= DNS_FETCHOPT_TCP;
7087 * Is it a query response?
7089 if (message->opcode != dns_opcode_query) {
7091 broken_server = DNS_R_UNEXPECTEDOPCODE;
7092 keep_trying = ISC_TRUE;
7097 * Update statistics about erroneous responses.
7099 if (message->rcode != dns_rcode_noerror) {
7100 switch (message->rcode) {
7101 case dns_rcode_nxdomain:
7102 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
7104 case dns_rcode_servfail:
7105 inc_stats(fctx->res, dns_resstatscounter_servfail);
7107 case dns_rcode_formerr:
7108 inc_stats(fctx->res, dns_resstatscounter_formerr);
7111 inc_stats(fctx->res, dns_resstatscounter_othererror);
7117 * Is the remote server broken, or does it dislike us?
7119 if (message->rcode != dns_rcode_noerror &&
7120 message->rcode != dns_rcode_nxdomain) {
7121 if (((message->rcode == dns_rcode_formerr ||
7122 message->rcode == dns_rcode_notimp) ||
7123 (message->rcode == dns_rcode_servfail &&
7124 dns_message_getopt(message) == NULL)) &&
7125 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7127 * It's very likely they don't like EDNS0.
7128 * If the response code is SERVFAIL, also check if the
7129 * response contains an OPT RR and don't cache the
7130 * failure since it can be returned for various other
7133 * XXXRTH We should check if the question
7134 * we're asking requires EDNS0, and
7135 * if so, we should bail out.
7137 options |= DNS_FETCHOPT_NOEDNS0;
7140 * Remember that they may not like EDNS0.
7142 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7143 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
7144 } else if (message->rcode == dns_rcode_formerr) {
7145 if (ISFORWARDER(query->addrinfo)) {
7147 * This forwarder doesn't understand us,
7148 * but other forwarders might. Keep trying.
7150 broken_server = DNS_R_REMOTEFORMERR;
7151 keep_trying = ISC_TRUE;
7154 * The server doesn't understand us. Since
7155 * all servers for a zone need similar
7156 * capabilities, we assume that we will get
7157 * FORMERR from all servers, and thus we
7158 * cannot make any more progress with this
7161 log_formerr(fctx, "server sent FORMERR");
7162 result = DNS_R_FORMERR;
7164 } else if (message->rcode == dns_rcode_yxdomain) {
7166 * DNAME mapping failed because the new name
7167 * was too long. There's no chance of success
7170 result = DNS_R_YXDOMAIN;
7171 } else if (message->rcode == dns_rcode_badvers) {
7172 unsigned int flags, mask;
7173 unsigned int version;
7176 INSIST(opt != NULL);
7177 version = (opt->ttl >> 16) & 0xff;
7178 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
7179 DNS_FETCHOPT_EDNSVERSIONSET;
7180 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
7181 DNS_FETCHOPT_EDNSVERSIONSET;
7184 dns_adb_changeflags(fctx->adb, query->addrinfo,
7188 broken_server = DNS_R_BADVERS;
7189 keep_trying = ISC_TRUE;
7196 broken_server = DNS_R_UNEXPECTEDRCODE;
7197 INSIST(broken_server != ISC_R_SUCCESS);
7198 keep_trying = ISC_TRUE;
7204 * Is the question the same as the one we asked?
7206 result = same_question(fctx);
7207 if (result != ISC_R_SUCCESS) {
7209 if (result == DNS_R_FORMERR)
7210 keep_trying = ISC_TRUE;
7215 * Is the server lame?
7217 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
7219 inc_stats(fctx->res, dns_resstatscounter_lame);
7220 log_lame(fctx, query->addrinfo);
7221 result = dns_adb_marklame(fctx->adb, query->addrinfo,
7222 &fctx->name, fctx->type,
7223 now + fctx->res->lame_ttl);
7224 if (result != ISC_R_SUCCESS)
7225 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7226 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
7227 "could not mark server as lame: %s",
7228 isc_result_totext(result));
7229 broken_server = DNS_R_LAME;
7230 keep_trying = ISC_TRUE;
7235 * Enforce delegations only zones like NET and COM.
7237 if (!ISFORWARDER(query->addrinfo) &&
7238 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
7239 !dns_name_equal(&fctx->domain, &fctx->name) &&
7240 fix_mustbedelegationornxdomain(message, fctx)) {
7241 char namebuf[DNS_NAME_FORMATSIZE];
7242 char domainbuf[DNS_NAME_FORMATSIZE];
7243 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7247 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
7248 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7249 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
7250 dns_rdataclass_format(fctx->res->rdclass, classbuf,
7252 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7255 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
7256 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7257 "enforced delegation-only for '%s' (%s/%s/%s) "
7259 domainbuf, namebuf, typebuf, classbuf, addrbuf);
7262 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
7263 checknames(message);
7268 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
7271 * Did we get any answers?
7273 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
7274 (message->rcode == dns_rcode_noerror ||
7275 message->rcode == dns_rcode_nxdomain)) {
7278 * We've got answers. If it has an authoritative answer or an
7279 * answer from a forwarder, we're done.
7281 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
7282 ISFORWARDER(query->addrinfo))
7283 result = answer_response(fctx);
7284 else if (iscname(fctx) &&
7285 fctx->type != dns_rdatatype_any &&
7286 fctx->type != dns_rdatatype_cname) {
7288 * A BIND8 server could return a non-authoritative
7289 * answer when a CNAME is followed. We should treat
7290 * it as a valid answer.
7292 result = answer_response(fctx);
7293 } else if (fctx->type != dns_rdatatype_ns &&
7294 !betterreferral(fctx)) {
7296 * Lame response !!!.
7298 result = answer_response(fctx);
7300 if (fctx->type == dns_rdatatype_ns) {
7302 * A BIND 8 server could incorrectly return a
7303 * non-authoritative answer to an NS query
7304 * instead of a referral. Since this answer
7305 * lacks the SIGs necessary to do DNSSEC
7306 * validation, we must invoke the following
7307 * special kludge to treat it as a referral.
7309 result = noanswer_response(fctx, NULL,
7310 LOOK_FOR_NS_IN_ANSWER);
7313 * Some other servers may still somehow include
7314 * an answer when it should return a referral
7315 * with an empty answer. Check to see if we can
7316 * treat this as a referral by ignoring the
7317 * answer. Further more, there may be an
7318 * implementation that moves A/AAAA glue records
7319 * to the answer section for that type of
7320 * delegation when the query is for that glue
7321 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
7322 * such a corner case.
7324 result = noanswer_response(fctx, NULL,
7325 LOOK_FOR_GLUE_IN_ANSWER);
7327 if (result != DNS_R_DELEGATION) {
7329 * At this point, AA is not set, the response
7330 * is not a referral, and the server is not a
7331 * forwarder. It is technically lame and it's
7332 * easier to treat it as such than to figure out
7333 * some more elaborate course of action.
7335 broken_server = DNS_R_LAME;
7336 keep_trying = ISC_TRUE;
7339 goto force_referral;
7341 if (result != ISC_R_SUCCESS) {
7342 if (result == DNS_R_FORMERR)
7343 keep_trying = ISC_TRUE;
7346 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
7347 message->rcode == dns_rcode_noerror ||
7348 message->rcode == dns_rcode_nxdomain) {
7350 * NXDOMAIN, NXRDATASET, or referral.
7352 result = noanswer_response(fctx, NULL, 0);
7355 case DNS_R_CHASEDSSERVERS:
7357 case DNS_R_DELEGATION:
7360 * We don't have the answer, but we know a better
7363 get_nameservers = ISC_TRUE;
7364 keep_trying = ISC_TRUE;
7366 * We have a new set of name servers, and it
7367 * has not experienced any restarts yet.
7372 * Update local statistics counters collected for each
7376 fctx->querysent = 0;
7377 fctx->lamecount = 0;
7382 result = ISC_R_SUCCESS;
7386 * Something has gone wrong.
7388 if (result == DNS_R_FORMERR)
7389 keep_trying = ISC_TRUE;
7394 * The server is insane.
7397 broken_server = DNS_R_UNEXPECTEDRCODE;
7398 keep_trying = ISC_TRUE;
7403 * Follow additional section data chains.
7405 chase_additional(fctx);
7408 * Cache the cacheable parts of the message. This may also cause
7409 * work to be queued to the DNSSEC validator.
7411 if (WANTCACHE(fctx)) {
7412 result = cache_message(fctx, query->addrinfo, now);
7413 if (result != ISC_R_SUCCESS)
7418 * Ncache the negatively cacheable parts of the message. This may
7419 * also cause work to be queued to the DNSSEC validator.
7421 if (WANTNCACHE(fctx)) {
7422 dns_rdatatype_t covers;
7423 if (message->rcode == dns_rcode_nxdomain)
7424 covers = dns_rdatatype_any;
7426 covers = fctx->type;
7429 * Cache any negative cache entries in the message.
7431 result = ncache_message(fctx, query->addrinfo, covers, now);
7436 * Remember the query's addrinfo, in case we need to mark the
7439 addrinfo = query->addrinfo;
7444 * XXXRTH Don't cancel the query if waiting for validation?
7446 fctx_cancelquery(&query, &devent, finish, no_response);
7449 if (result == DNS_R_FORMERR)
7450 broken_server = DNS_R_FORMERR;
7451 if (broken_server != ISC_R_SUCCESS) {
7453 * Add this server to the list of bad servers for
7456 add_bad(fctx, addrinfo, broken_server, broken_type);
7459 if (get_nameservers) {
7461 dns_fixedname_init(&foundname);
7462 fname = dns_fixedname_name(&foundname);
7463 if (result != ISC_R_SUCCESS) {
7464 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7468 if (dns_rdatatype_atparent(fctx->type))
7469 findoptions |= DNS_DBFIND_NOEXACT;
7470 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7473 name = &fctx->domain;
7474 result = dns_view_findzonecut(fctx->res->view,
7480 if (result != ISC_R_SUCCESS) {
7481 FCTXTRACE("couldn't find a zonecut");
7482 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7485 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7487 * The best nameservers are now above our
7490 FCTXTRACE("nameservers now above QDOMAIN");
7491 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7494 dns_name_free(&fctx->domain, fctx->mctx);
7495 dns_name_init(&fctx->domain, NULL);
7496 result = dns_name_dup(fname, fctx->mctx, &fctx->domain);
7497 if (result != ISC_R_SUCCESS) {
7498 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7501 fctx->ns_ttl = fctx->nameservers.ttl;
7502 fctx->ns_ttl_ok = ISC_TRUE;
7503 fctx_cancelqueries(fctx, ISC_TRUE);
7504 fctx_cleanupfinds(fctx);
7505 fctx_cleanupaltfinds(fctx);
7506 fctx_cleanupforwaddrs(fctx);
7507 fctx_cleanupaltaddrs(fctx);
7512 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7513 } else if (resend) {
7515 * Resend (probably with changed options).
7517 FCTXTRACE("resend");
7518 inc_stats(fctx->res, dns_resstatscounter_retry);
7519 result = fctx_query(fctx, addrinfo, options);
7520 if (result != ISC_R_SUCCESS)
7521 fctx_done(fctx, result, __LINE__);
7522 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7524 * All has gone well so far, but we are waiting for the
7525 * DNSSEC validator to validate the answer.
7527 FCTXTRACE("wait for validator");
7528 fctx_cancelqueries(fctx, ISC_TRUE);
7530 * We must not retransmit while the validator is working;
7531 * it has references to the current rmessage.
7533 result = fctx_stopidletimer(fctx);
7534 if (result != ISC_R_SUCCESS)
7535 fctx_done(fctx, result, __LINE__);
7536 } else if (result == DNS_R_CHASEDSSERVERS) {
7538 add_bad(fctx, addrinfo, result, broken_type);
7539 fctx_cancelqueries(fctx, ISC_TRUE);
7540 fctx_cleanupfinds(fctx);
7541 fctx_cleanupforwaddrs(fctx);
7543 n = dns_name_countlabels(&fctx->name);
7544 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7546 FCTXTRACE("suspending DS lookup to find parent's NS records");
7548 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7550 NULL, NULL, NULL, 0, task,
7551 resume_dslookup, fctx,
7552 &fctx->nsrrset, NULL,
7554 if (result != ISC_R_SUCCESS)
7555 fctx_done(fctx, result, __LINE__);
7557 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7559 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7560 result = fctx_stopidletimer(fctx);
7561 if (result != ISC_R_SUCCESS)
7562 fctx_done(fctx, result, __LINE__);
7568 fctx_done(fctx, result, __LINE__);
7574 *** Resolver Methods
7577 destroy_badcache(dns_resolver_t *res) {
7578 dns_badcache_t *bad, *next;
7581 if (res->badcache != NULL) {
7582 for (i = 0; i < res->badhash; i++)
7583 for (bad = res->badcache[i]; bad != NULL;
7586 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7590 isc_mem_put(res->mctx, res->badcache,
7591 sizeof(*res->badcache) * res->badhash);
7592 res->badcache = NULL;
7594 INSIST(res->badcount == 0);
7599 destroy(dns_resolver_t *res) {
7603 REQUIRE(res->references == 0);
7604 REQUIRE(!res->priming);
7605 REQUIRE(res->primefetch == NULL);
7609 INSIST(res->nfctx == 0);
7611 DESTROYLOCK(&res->primelock);
7612 DESTROYLOCK(&res->nlock);
7613 DESTROYLOCK(&res->lock);
7614 for (i = 0; i < res->nbuckets; i++) {
7615 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7616 isc_task_shutdown(res->buckets[i].task);
7617 isc_task_detach(&res->buckets[i].task);
7618 DESTROYLOCK(&res->buckets[i].lock);
7619 isc_mem_detach(&res->buckets[i].mctx);
7621 isc_mem_put(res->mctx, res->buckets,
7622 res->nbuckets * sizeof(fctxbucket_t));
7623 if (res->dispatches4 != NULL)
7624 dns_dispatchset_destroy(&res->dispatches4);
7625 if (res->dispatches6 != NULL)
7626 dns_dispatchset_destroy(&res->dispatches6);
7627 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7628 ISC_LIST_UNLINK(res->alternates, a, link);
7630 dns_name_free(&a->_u._n.name, res->mctx);
7631 isc_mem_put(res->mctx, a, sizeof(*a));
7633 dns_resolver_reset_algorithms(res);
7634 destroy_badcache(res);
7635 dns_resolver_resetmustbesecure(res);
7637 isc_rwlock_destroy(&res->alglock);
7640 isc_rwlock_destroy(&res->mbslock);
7642 isc_timer_detach(&res->spillattimer);
7644 isc_mem_put(res->mctx, res, sizeof(*res));
7648 send_shutdown_events(dns_resolver_t *res) {
7649 isc_event_t *event, *next_event;
7653 * Caller must be holding the resolver lock.
7656 for (event = ISC_LIST_HEAD(res->whenshutdown);
7658 event = next_event) {
7659 next_event = ISC_LIST_NEXT(event, ev_link);
7660 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7661 etask = event->ev_sender;
7662 event->ev_sender = res;
7663 isc_task_sendanddetach(&etask, &event);
7668 empty_bucket(dns_resolver_t *res) {
7669 RTRACE("empty_bucket");
7673 INSIST(res->activebuckets > 0);
7674 res->activebuckets--;
7675 if (res->activebuckets == 0)
7676 send_shutdown_events(res);
7682 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7683 dns_resolver_t *res = event->ev_arg;
7684 isc_result_t result;
7686 isc_boolean_t logit = ISC_FALSE;
7688 REQUIRE(VALID_RESOLVER(res));
7693 INSIST(!res->exiting);
7694 if (res->spillat > res->spillatmin) {
7698 if (res->spillat <= res->spillatmin) {
7699 result = isc_timer_reset(res->spillattimer,
7700 isc_timertype_inactive, NULL,
7702 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7704 count = res->spillat;
7707 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7708 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7709 "clients-per-query decreased to %u", count);
7711 isc_event_free(&event);
7715 dns_resolver_create(dns_view_t *view,
7716 isc_taskmgr_t *taskmgr,
7717 unsigned int ntasks, unsigned int ndisp,
7718 isc_socketmgr_t *socketmgr,
7719 isc_timermgr_t *timermgr,
7720 unsigned int options,
7721 dns_dispatchmgr_t *dispatchmgr,
7722 dns_dispatch_t *dispatchv4,
7723 dns_dispatch_t *dispatchv6,
7724 dns_resolver_t **resp)
7726 dns_resolver_t *res;
7727 isc_result_t result = ISC_R_SUCCESS;
7728 unsigned int i, buckets_created = 0;
7729 isc_task_t *task = NULL;
7734 * Create a resolver.
7737 REQUIRE(DNS_VIEW_VALID(view));
7738 REQUIRE(ntasks > 0);
7740 REQUIRE(resp != NULL && *resp == NULL);
7741 REQUIRE(dispatchmgr != NULL);
7742 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7744 res = isc_mem_get(view->mctx, sizeof(*res));
7746 return (ISC_R_NOMEMORY);
7748 res->mctx = view->mctx;
7749 res->rdclass = view->rdclass;
7750 res->socketmgr = socketmgr;
7751 res->timermgr = timermgr;
7752 res->taskmgr = taskmgr;
7753 res->dispatchmgr = dispatchmgr;
7755 res->options = options;
7757 ISC_LIST_INIT(res->alternates);
7758 res->udpsize = RECV_BUFFER_SIZE;
7759 res->algorithms = NULL;
7760 res->badcache = NULL;
7764 res->mustbesecure = NULL;
7765 res->spillatmin = res->spillat = 10;
7766 res->spillatmax = 100;
7767 res->spillattimer = NULL;
7768 res->zero_no_soa_ttl = ISC_FALSE;
7769 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7770 res->maxdepth = DEFAULT_RECURSION_DEPTH;
7771 res->nbuckets = ntasks;
7772 res->activebuckets = ntasks;
7773 res->buckets = isc_mem_get(view->mctx,
7774 ntasks * sizeof(fctxbucket_t));
7775 if (res->buckets == NULL) {
7776 result = ISC_R_NOMEMORY;
7779 for (i = 0; i < ntasks; i++) {
7780 result = isc_mutex_init(&res->buckets[i].lock);
7781 if (result != ISC_R_SUCCESS)
7782 goto cleanup_buckets;
7783 res->buckets[i].task = NULL;
7784 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7785 if (result != ISC_R_SUCCESS) {
7786 DESTROYLOCK(&res->buckets[i].lock);
7787 goto cleanup_buckets;
7789 res->buckets[i].mctx = NULL;
7790 snprintf(name, sizeof(name), "res%u", i);
7791 #ifdef ISC_PLATFORM_USETHREADS
7793 * Use a separate memory context for each bucket to reduce
7794 * contention among multiple threads. Do this only when
7795 * enabling threads because it will be require more memory.
7797 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7798 if (result != ISC_R_SUCCESS) {
7799 isc_task_detach(&res->buckets[i].task);
7800 DESTROYLOCK(&res->buckets[i].lock);
7801 goto cleanup_buckets;
7803 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7805 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7807 isc_task_setname(res->buckets[i].task, name, res);
7808 ISC_LIST_INIT(res->buckets[i].fctxs);
7809 res->buckets[i].exiting = ISC_FALSE;
7813 res->dispatches4 = NULL;
7814 if (dispatchv4 != NULL) {
7815 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
7816 dispatchv4, &res->dispatches4, ndisp);
7817 dispattr = dns_dispatch_getattributes(dispatchv4);
7819 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7822 res->dispatches6 = NULL;
7823 if (dispatchv6 != NULL) {
7824 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
7825 dispatchv6, &res->dispatches6, ndisp);
7826 dispattr = dns_dispatch_getattributes(dispatchv6);
7828 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7831 res->references = 1;
7832 res->exiting = ISC_FALSE;
7833 res->frozen = ISC_FALSE;
7834 ISC_LIST_INIT(res->whenshutdown);
7835 res->priming = ISC_FALSE;
7836 res->primefetch = NULL;
7839 result = isc_mutex_init(&res->lock);
7840 if (result != ISC_R_SUCCESS)
7841 goto cleanup_dispatches;
7843 result = isc_mutex_init(&res->nlock);
7844 if (result != ISC_R_SUCCESS)
7847 result = isc_mutex_init(&res->primelock);
7848 if (result != ISC_R_SUCCESS)
7852 result = isc_task_create(taskmgr, 0, &task);
7853 if (result != ISC_R_SUCCESS)
7854 goto cleanup_primelock;
7856 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7857 task, spillattimer_countdown, res,
7858 &res->spillattimer);
7859 isc_task_detach(&task);
7860 if (result != ISC_R_SUCCESS)
7861 goto cleanup_primelock;
7864 result = isc_rwlock_init(&res->alglock, 0, 0);
7865 if (result != ISC_R_SUCCESS)
7866 goto cleanup_spillattimer;
7869 result = isc_rwlock_init(&res->mbslock, 0, 0);
7870 if (result != ISC_R_SUCCESS)
7871 goto cleanup_alglock;
7874 res->magic = RES_MAGIC;
7878 return (ISC_R_SUCCESS);
7883 isc_rwlock_destroy(&res->alglock);
7886 #if USE_ALGLOCK || USE_MBSLOCK
7887 cleanup_spillattimer:
7888 isc_timer_detach(&res->spillattimer);
7892 DESTROYLOCK(&res->primelock);
7895 DESTROYLOCK(&res->nlock);
7898 DESTROYLOCK(&res->lock);
7901 if (res->dispatches6 != NULL)
7902 dns_dispatchset_destroy(&res->dispatches6);
7903 if (res->dispatches4 != NULL)
7904 dns_dispatchset_destroy(&res->dispatches4);
7907 for (i = 0; i < buckets_created; i++) {
7908 isc_mem_detach(&res->buckets[i].mctx);
7909 DESTROYLOCK(&res->buckets[i].lock);
7910 isc_task_shutdown(res->buckets[i].task);
7911 isc_task_detach(&res->buckets[i].task);
7913 isc_mem_put(view->mctx, res->buckets,
7914 res->nbuckets * sizeof(fctxbucket_t));
7917 isc_mem_put(view->mctx, res, sizeof(*res));
7924 prime_done(isc_task_t *task, isc_event_t *event) {
7925 dns_resolver_t *res;
7926 dns_fetchevent_t *fevent;
7928 dns_db_t *db = NULL;
7930 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7931 fevent = (dns_fetchevent_t *)event;
7932 res = event->ev_arg;
7933 REQUIRE(VALID_RESOLVER(res));
7939 INSIST(res->priming);
7940 res->priming = ISC_FALSE;
7941 LOCK(&res->primelock);
7942 fetch = res->primefetch;
7943 res->primefetch = NULL;
7944 UNLOCK(&res->primelock);
7948 if (fevent->result == ISC_R_SUCCESS &&
7949 res->view->cache != NULL && res->view->hints != NULL) {
7950 dns_cache_attachdb(res->view->cache, &db);
7951 dns_root_checkhints(res->view, res->view->hints, db);
7955 if (fevent->node != NULL)
7956 dns_db_detachnode(fevent->db, &fevent->node);
7957 if (fevent->db != NULL)
7958 dns_db_detach(&fevent->db);
7959 if (dns_rdataset_isassociated(fevent->rdataset))
7960 dns_rdataset_disassociate(fevent->rdataset);
7961 INSIST(fevent->sigrdataset == NULL);
7963 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7965 isc_event_free(&event);
7966 dns_resolver_destroyfetch(&fetch);
7970 dns_resolver_prime(dns_resolver_t *res) {
7971 isc_boolean_t want_priming = ISC_FALSE;
7972 dns_rdataset_t *rdataset;
7973 isc_result_t result;
7975 REQUIRE(VALID_RESOLVER(res));
7976 REQUIRE(res->frozen);
7978 RTRACE("dns_resolver_prime");
7982 if (!res->exiting && !res->priming) {
7983 INSIST(res->primefetch == NULL);
7984 res->priming = ISC_TRUE;
7985 want_priming = ISC_TRUE;
7992 * To avoid any possible recursive locking problems, we
7993 * start the priming fetch like any other fetch, and holding
7994 * no resolver locks. No one else will try to start it
7995 * because we're the ones who set res->priming to true.
7996 * Any other callers of dns_resolver_prime() while we're
7997 * running will see that res->priming is already true and
8001 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
8002 if (rdataset == NULL) {
8004 INSIST(res->priming);
8005 INSIST(res->primefetch == NULL);
8006 res->priming = ISC_FALSE;
8010 dns_rdataset_init(rdataset);
8011 LOCK(&res->primelock);
8012 result = dns_resolver_createfetch(res, dns_rootname,
8014 NULL, NULL, NULL, 0,
8015 res->buckets[0].task,
8017 res, rdataset, NULL,
8019 UNLOCK(&res->primelock);
8020 if (result != ISC_R_SUCCESS) {
8022 INSIST(res->priming);
8023 res->priming = ISC_FALSE;
8031 dns_resolver_freeze(dns_resolver_t *res) {
8036 REQUIRE(VALID_RESOLVER(res));
8038 res->frozen = ISC_TRUE;
8042 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
8043 REQUIRE(VALID_RESOLVER(source));
8044 REQUIRE(targetp != NULL && *targetp == NULL);
8046 RRTRACE(source, "attach");
8047 LOCK(&source->lock);
8048 REQUIRE(!source->exiting);
8050 INSIST(source->references > 0);
8051 source->references++;
8052 INSIST(source->references != 0);
8053 UNLOCK(&source->lock);
8059 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
8060 isc_event_t **eventp)
8065 REQUIRE(VALID_RESOLVER(res));
8066 REQUIRE(eventp != NULL);
8073 if (res->exiting && res->activebuckets == 0) {
8075 * We're already shutdown. Send the event.
8077 event->ev_sender = res;
8078 isc_task_send(task, &event);
8081 isc_task_attach(task, &clone);
8082 event->ev_sender = clone;
8083 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
8090 dns_resolver_shutdown(dns_resolver_t *res) {
8093 isc_result_t result;
8095 REQUIRE(VALID_RESOLVER(res));
8101 if (!res->exiting) {
8103 res->exiting = ISC_TRUE;
8105 for (i = 0; i < res->nbuckets; i++) {
8106 LOCK(&res->buckets[i].lock);
8107 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
8109 fctx = ISC_LIST_NEXT(fctx, link))
8110 fctx_shutdown(fctx);
8111 if (res->dispatches4 != NULL && !res->exclusivev4) {
8112 dns_dispatchset_cancelall(res->dispatches4,
8113 res->buckets[i].task);
8115 if (res->dispatches6 != NULL && !res->exclusivev6) {
8116 dns_dispatchset_cancelall(res->dispatches6,
8117 res->buckets[i].task);
8119 res->buckets[i].exiting = ISC_TRUE;
8120 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
8121 INSIST(res->activebuckets > 0);
8122 res->activebuckets--;
8124 UNLOCK(&res->buckets[i].lock);
8126 if (res->activebuckets == 0)
8127 send_shutdown_events(res);
8128 result = isc_timer_reset(res->spillattimer,
8129 isc_timertype_inactive, NULL,
8131 RUNTIME_CHECK(result == ISC_R_SUCCESS);
8138 dns_resolver_detach(dns_resolver_t **resp) {
8139 dns_resolver_t *res;
8140 isc_boolean_t need_destroy = ISC_FALSE;
8142 REQUIRE(resp != NULL);
8144 REQUIRE(VALID_RESOLVER(res));
8150 INSIST(res->references > 0);
8152 if (res->references == 0) {
8153 INSIST(res->exiting && res->activebuckets == 0);
8154 need_destroy = ISC_TRUE;
8165 static inline isc_boolean_t
8166 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
8167 unsigned int options)
8170 * Don't match fetch contexts that are shutting down.
8172 if (fctx->cloned || fctx->state == fetchstate_done ||
8173 ISC_LIST_EMPTY(fctx->events))
8176 if (fctx->type != type || fctx->options != options)
8178 return (dns_name_equal(&fctx->name, name));
8182 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
8183 char namebuf[DNS_NAME_FORMATSIZE];
8184 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8185 int level = ISC_LOG_DEBUG(1);
8187 if (! isc_log_wouldlog(dns_lctx, level))
8190 dns_name_format(name, namebuf, sizeof(namebuf));
8191 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
8193 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
8194 DNS_LOGMODULE_RESOLVER, level,
8195 "createfetch: %s %s", namebuf, typebuf);
8199 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
8200 dns_rdatatype_t type,
8201 dns_name_t *domain, dns_rdataset_t *nameservers,
8202 dns_forwarders_t *forwarders,
8203 unsigned int options, isc_task_t *task,
8204 isc_taskaction_t action, void *arg,
8205 dns_rdataset_t *rdataset,
8206 dns_rdataset_t *sigrdataset,
8207 dns_fetch_t **fetchp)
8209 return (dns_resolver_createfetch3(res, name, type, domain,
8210 nameservers, forwarders, NULL, 0,
8211 options, 0, task, action, arg,
8212 rdataset, sigrdataset, fetchp));
8216 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
8217 dns_rdatatype_t type,
8218 dns_name_t *domain, dns_rdataset_t *nameservers,
8219 dns_forwarders_t *forwarders,
8220 isc_sockaddr_t *client, dns_messageid_t id,
8221 unsigned int options, isc_task_t *task,
8222 isc_taskaction_t action, void *arg,
8223 dns_rdataset_t *rdataset,
8224 dns_rdataset_t *sigrdataset,
8225 dns_fetch_t **fetchp)
8227 return (dns_resolver_createfetch3(res, name, type, domain,
8228 nameservers, forwarders, client, id,
8229 options, 0, task, action, arg,
8230 rdataset, sigrdataset, fetchp));
8234 dns_resolver_createfetch3(dns_resolver_t *res, dns_name_t *name,
8235 dns_rdatatype_t type,
8236 dns_name_t *domain, dns_rdataset_t *nameservers,
8237 dns_forwarders_t *forwarders,
8238 isc_sockaddr_t *client, dns_messageid_t id,
8239 unsigned int options, unsigned int depth,
8241 isc_taskaction_t action, void *arg,
8242 dns_rdataset_t *rdataset,
8243 dns_rdataset_t *sigrdataset,
8244 dns_fetch_t **fetchp)
8247 fetchctx_t *fctx = NULL;
8248 isc_result_t result = ISC_R_SUCCESS;
8249 unsigned int bucketnum;
8250 isc_boolean_t new_fctx = ISC_FALSE;
8252 unsigned int count = 0;
8253 unsigned int spillat;
8254 unsigned int spillatmin;
8255 isc_boolean_t destroy = ISC_FALSE;
8259 REQUIRE(VALID_RESOLVER(res));
8260 REQUIRE(res->frozen);
8261 /* XXXRTH Check for meta type */
8262 if (domain != NULL) {
8263 REQUIRE(DNS_RDATASET_VALID(nameservers));
8264 REQUIRE(nameservers->type == dns_rdatatype_ns);
8266 REQUIRE(nameservers == NULL);
8267 REQUIRE(forwarders == NULL);
8268 REQUIRE(!dns_rdataset_isassociated(rdataset));
8269 REQUIRE(sigrdataset == NULL ||
8270 !dns_rdataset_isassociated(sigrdataset));
8271 REQUIRE(fetchp != NULL && *fetchp == NULL);
8273 log_fetch(name, type);
8276 * XXXRTH use a mempool?
8278 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
8280 return (ISC_R_NOMEMORY);
8282 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
8285 spillat = res->spillat;
8286 spillatmin = res->spillatmin;
8288 LOCK(&res->buckets[bucketnum].lock);
8290 if (res->buckets[bucketnum].exiting) {
8291 result = ISC_R_SHUTTINGDOWN;
8295 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
8296 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
8298 fctx = ISC_LIST_NEXT(fctx, link)) {
8299 if (fctx_match(fctx, name, type, options))
8305 * Is this a duplicate?
8307 if (fctx != NULL && client != NULL) {
8308 dns_fetchevent_t *fevent;
8309 for (fevent = ISC_LIST_HEAD(fctx->events);
8311 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
8312 if (fevent->client != NULL && fevent->id == id &&
8313 isc_sockaddr_equal(fevent->client, client)) {
8314 result = DNS_R_DUPLICATE;
8320 if (count >= spillatmin && spillatmin != 0) {
8321 INSIST(fctx != NULL);
8322 if (count >= spillat)
8323 fctx->spilled = ISC_TRUE;
8324 if (fctx->spilled) {
8325 result = DNS_R_DROP;
8331 result = fctx_create(res, name, type, domain, nameservers,
8332 options, bucketnum, depth, &fctx);
8333 if (result != ISC_R_SUCCESS)
8335 new_fctx = ISC_TRUE;
8336 } else if (fctx->depth > depth)
8337 fctx->depth = depth;
8339 result = fctx_join(fctx, task, client, id, action, arg,
8340 rdataset, sigrdataset, fetch);
8342 if (result == ISC_R_SUCCESS) {
8346 event = &fctx->control_event;
8347 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
8348 DNS_EVENT_FETCHCONTROL,
8349 fctx_start, fctx, NULL,
8351 isc_task_send(res->buckets[bucketnum].task, &event);
8354 * We don't care about the result of fctx_unlink()
8355 * since we know we're not exiting.
8357 (void)fctx_unlink(fctx);
8363 UNLOCK(&res->buckets[bucketnum].lock);
8368 if (result == ISC_R_SUCCESS) {
8372 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8378 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
8380 dns_resolver_t *res;
8381 dns_fetchevent_t *event, *next_event;
8384 REQUIRE(DNS_FETCH_VALID(fetch));
8385 fctx = fetch->private;
8386 REQUIRE(VALID_FCTX(fctx));
8389 FTRACE("cancelfetch");
8391 LOCK(&res->buckets[fctx->bucketnum].lock);
8394 * Find the completion event for this fetch (as opposed
8395 * to those for other fetches that have joined the same
8396 * fctx) and send it with result = ISC_R_CANCELED.
8399 if (fctx->state != fetchstate_done) {
8400 for (event = ISC_LIST_HEAD(fctx->events);
8402 event = next_event) {
8403 next_event = ISC_LIST_NEXT(event, ev_link);
8404 if (event->fetch == fetch) {
8405 ISC_LIST_UNLINK(fctx->events, event, ev_link);
8410 if (event != NULL) {
8411 etask = event->ev_sender;
8412 event->ev_sender = fctx;
8413 event->result = ISC_R_CANCELED;
8414 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8417 * The fctx continues running even if no fetches remain;
8418 * the answer is still cached.
8421 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8425 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8427 dns_resolver_t *res;
8428 dns_fetchevent_t *event, *next_event;
8430 unsigned int bucketnum;
8431 isc_boolean_t bucket_empty;
8433 REQUIRE(fetchp != NULL);
8435 REQUIRE(DNS_FETCH_VALID(fetch));
8436 fctx = fetch->private;
8437 REQUIRE(VALID_FCTX(fctx));
8440 FTRACE("destroyfetch");
8442 bucketnum = fctx->bucketnum;
8443 LOCK(&res->buckets[bucketnum].lock);
8446 * Sanity check: the caller should have gotten its event before
8447 * trying to destroy the fetch.
8450 if (fctx->state != fetchstate_done) {
8451 for (event = ISC_LIST_HEAD(fctx->events);
8453 event = next_event) {
8454 next_event = ISC_LIST_NEXT(event, ev_link);
8455 RUNTIME_CHECK(event->fetch != fetch);
8459 bucket_empty = fctx_decreference(fctx);
8461 UNLOCK(&res->buckets[bucketnum].lock);
8463 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8471 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8472 isc_logcategory_t *category, isc_logmodule_t *module,
8473 int level, isc_boolean_t duplicateok)
8476 dns_resolver_t *res;
8477 char domainbuf[DNS_NAME_FORMATSIZE];
8479 REQUIRE(DNS_FETCH_VALID(fetch));
8480 fctx = fetch->private;
8481 REQUIRE(VALID_FCTX(fctx));
8484 LOCK(&res->buckets[fctx->bucketnum].lock);
8486 INSIST(fctx->exitline >= 0);
8487 if (!fctx->logged || duplicateok) {
8488 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8489 isc_log_write(lctx, category, module, level,
8490 "fetch completed at %s:%d for %s in "
8491 "%" ISC_PRINT_QUADFORMAT "u."
8492 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8493 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8494 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8495 "adberr:%u,findfail:%u,valfail:%u]",
8496 __FILE__, fctx->exitline, fctx->info,
8497 fctx->duration / US_PER_SEC,
8498 fctx->duration % US_PER_SEC,
8499 isc_result_totext(fctx->result),
8500 isc_result_totext(fctx->vresult), domainbuf,
8501 fctx->referrals, fctx->restarts,
8502 fctx->querysent, fctx->timeouts, fctx->lamecount,
8503 fctx->neterr, fctx->badresp, fctx->adberr,
8504 fctx->findfail, fctx->valfail);
8505 fctx->logged = ISC_TRUE;
8508 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8512 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8513 REQUIRE(VALID_RESOLVER(resolver));
8514 return (resolver->dispatchmgr);
8518 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8519 REQUIRE(VALID_RESOLVER(resolver));
8520 return (dns_dispatchset_get(resolver->dispatches4));
8524 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8525 REQUIRE(VALID_RESOLVER(resolver));
8526 return (dns_dispatchset_get(resolver->dispatches6));
8530 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8531 REQUIRE(VALID_RESOLVER(resolver));
8532 return (resolver->socketmgr);
8536 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8537 REQUIRE(VALID_RESOLVER(resolver));
8538 return (resolver->taskmgr);
8542 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8543 REQUIRE(VALID_RESOLVER(resolver));
8544 return (resolver->lame_ttl);
8548 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8549 REQUIRE(VALID_RESOLVER(resolver));
8550 resolver->lame_ttl = lame_ttl;
8554 dns_resolver_nrunning(dns_resolver_t *resolver) {
8556 LOCK(&resolver->nlock);
8557 n = resolver->nfctx;
8558 UNLOCK(&resolver->nlock);
8563 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8564 dns_name_t *name, in_port_t port) {
8566 isc_result_t result;
8568 REQUIRE(VALID_RESOLVER(resolver));
8569 REQUIRE(!resolver->frozen);
8570 REQUIRE((alt == NULL) ^ (name == NULL));
8572 a = isc_mem_get(resolver->mctx, sizeof(*a));
8574 return (ISC_R_NOMEMORY);
8576 a->isaddress = ISC_TRUE;
8579 a->isaddress = ISC_FALSE;
8580 a->_u._n.port = port;
8581 dns_name_init(&a->_u._n.name, NULL);
8582 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8583 if (result != ISC_R_SUCCESS) {
8584 isc_mem_put(resolver->mctx, a, sizeof(*a));
8588 ISC_LINK_INIT(a, link);
8589 ISC_LIST_APPEND(resolver->alternates, a, link);
8591 return (ISC_R_SUCCESS);
8595 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8596 REQUIRE(VALID_RESOLVER(resolver));
8597 resolver->udpsize = udpsize;
8601 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8602 REQUIRE(VALID_RESOLVER(resolver));
8603 return (resolver->udpsize);
8607 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8609 dns_badcache_t *bad, *prev, *next;
8611 REQUIRE(VALID_RESOLVER(resolver));
8613 LOCK(&resolver->lock);
8614 if (resolver->badcache == NULL)
8619 isc_result_t result;
8620 result = isc_time_now(&now);
8621 if (result != ISC_R_SUCCESS)
8622 isc_time_settoepoch(&now);
8623 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8625 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8628 n = isc_time_compare(&bad->expire, &now);
8629 if (n < 0 || dns_name_equal(name, &bad->name)) {
8631 resolver->badcache[i] = bad->next;
8633 prev->next = bad->next;
8634 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8636 resolver->badcount--;
8641 destroy_badcache(resolver);
8644 UNLOCK(&resolver->lock);
8649 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8650 unsigned int newsize;
8651 dns_badcache_t **new, *bad, *next;
8655 newsize = resolver->badhash * 2 + 1;
8657 newsize = (resolver->badhash - 1) / 2;
8659 new = isc_mem_get(resolver->mctx,
8660 sizeof(*resolver->badcache) * newsize);
8663 memset(new, 0, sizeof(*resolver->badcache) * newsize);
8664 for (i = 0; i < resolver->badhash; i++) {
8665 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8667 if (isc_time_compare(&bad->expire, now) < 0) {
8668 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8670 resolver->badcount--;
8672 bad->next = new[bad->hashval % newsize];
8673 new[bad->hashval % newsize] = bad;
8677 isc_mem_put(resolver->mctx, resolver->badcache,
8678 sizeof(*resolver->badcache) * resolver->badhash);
8679 resolver->badhash = newsize;
8680 resolver->badcache = new;
8684 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8685 dns_rdatatype_t type, isc_time_t *expire)
8688 isc_result_t result = ISC_R_SUCCESS;
8689 unsigned int i, hashval;
8690 dns_badcache_t *bad, *prev, *next;
8692 REQUIRE(VALID_RESOLVER(resolver));
8694 LOCK(&resolver->lock);
8695 if (resolver->badcache == NULL) {
8696 resolver->badcache = isc_mem_get(resolver->mctx,
8697 sizeof(*resolver->badcache) *
8699 if (resolver->badcache == NULL)
8701 resolver->badhash = DNS_BADCACHE_SIZE;
8702 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8706 result = isc_time_now(&now);
8707 if (result != ISC_R_SUCCESS)
8708 isc_time_settoepoch(&now);
8709 hashval = dns_name_hash(name, ISC_FALSE);
8710 i = hashval % resolver->badhash;
8712 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8714 if (bad->type == type && dns_name_equal(name, &bad->name))
8716 if (isc_time_compare(&bad->expire, &now) < 0) {
8718 resolver->badcache[i] = bad->next;
8720 prev->next = bad->next;
8721 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8723 resolver->badcount--;
8728 isc_buffer_t buffer;
8729 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8733 bad->hashval = hashval;
8734 bad->expire = *expire;
8735 isc_buffer_init(&buffer, bad + 1, name->length);
8736 dns_name_init(&bad->name, NULL);
8737 dns_name_copy(name, &bad->name, &buffer);
8738 bad->next = resolver->badcache[i];
8739 resolver->badcache[i] = bad;
8740 resolver->badcount++;
8741 if (resolver->badcount > resolver->badhash * 8)
8742 resizehash(resolver, &now, ISC_TRUE);
8743 if (resolver->badcount < resolver->badhash * 2 &&
8744 resolver->badhash > DNS_BADCACHE_SIZE)
8745 resizehash(resolver, &now, ISC_FALSE);
8747 bad->expire = *expire;
8749 UNLOCK(&resolver->lock);
8753 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8754 dns_rdatatype_t type, isc_time_t *now)
8756 dns_badcache_t *bad, *prev, *next;
8757 isc_boolean_t answer = ISC_FALSE;
8760 REQUIRE(VALID_RESOLVER(resolver));
8762 LOCK(&resolver->lock);
8763 if (resolver->badcache == NULL)
8766 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8768 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8771 * Search the hash list. Clean out expired records as we go.
8773 if (isc_time_compare(&bad->expire, now) < 0) {
8775 prev->next = bad->next;
8777 resolver->badcache[i] = bad->next;
8778 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8780 resolver->badcount--;
8783 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8791 * Slow sweep to clean out stale records.
8793 i = resolver->badsweep++ % resolver->badhash;
8794 bad = resolver->badcache[i];
8795 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8796 resolver->badcache[i] = bad->next;
8797 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8799 resolver->badcount--;
8803 UNLOCK(&resolver->lock);
8808 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8809 char namebuf[DNS_NAME_FORMATSIZE];
8810 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8811 dns_badcache_t *bad, *next, *prev;
8816 LOCK(&resolver->lock);
8817 fprintf(fp, ";\n; Bad cache\n;\n");
8819 if (resolver->badcache == NULL)
8823 for (i = 0; i < resolver->badhash; i++) {
8825 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8827 if (isc_time_compare(&bad->expire, &now) < 0) {
8829 prev->next = bad->next;
8831 resolver->badcache[i] = bad->next;
8832 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8834 resolver->badcount--;
8838 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8839 dns_rdatatype_format(bad->type, typebuf,
8841 t = isc_time_microdiff(&bad->expire, &now);
8843 fprintf(fp, "; %s/%s [ttl "
8844 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8845 namebuf, typebuf, t);
8850 UNLOCK(&resolver->lock);
8854 free_algorithm(void *node, void *arg) {
8855 unsigned char *algorithms = node;
8856 isc_mem_t *mctx = arg;
8858 isc_mem_put(mctx, algorithms, *algorithms);
8862 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8864 REQUIRE(VALID_RESOLVER(resolver));
8867 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8869 if (resolver->algorithms != NULL)
8870 dns_rbt_destroy(&resolver->algorithms);
8872 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8877 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8880 unsigned int len, mask;
8882 unsigned char *algorithms;
8883 isc_result_t result;
8884 dns_rbtnode_t *node = NULL;
8886 REQUIRE(VALID_RESOLVER(resolver));
8888 return (ISC_R_RANGE);
8891 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8893 if (resolver->algorithms == NULL) {
8894 result = dns_rbt_create(resolver->mctx, free_algorithm,
8895 resolver->mctx, &resolver->algorithms);
8896 if (result != ISC_R_SUCCESS)
8901 mask = 1 << (alg%8);
8903 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8905 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8906 algorithms = node->data;
8907 if (algorithms == NULL || len > *algorithms) {
8908 new = isc_mem_get(resolver->mctx, len);
8910 result = ISC_R_NOMEMORY;
8913 memset(new, 0, len);
8914 if (algorithms != NULL)
8915 memmove(new, algorithms, *algorithms);
8919 if (algorithms != NULL)
8920 isc_mem_put(resolver->mctx, algorithms,
8923 algorithms[len-1] |= mask;
8925 result = ISC_R_SUCCESS;
8928 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8934 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8937 unsigned int len, mask;
8938 unsigned char *algorithms;
8940 isc_result_t result;
8941 isc_boolean_t found = ISC_FALSE;
8943 REQUIRE(VALID_RESOLVER(resolver));
8946 * DH is unsupported for DNSKEYs, see RFC 4034 sec. A.1.
8948 if ((alg == DST_ALG_DH) || (alg == DST_ALG_INDIRECT))
8952 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8954 if (resolver->algorithms == NULL)
8956 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8957 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8959 mask = 1 << (alg%8);
8961 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8966 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8971 return (dst_algorithm_supported(alg));
8975 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8978 return (dns_ds_digest_supported(digest));
8982 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8984 REQUIRE(VALID_RESOLVER(resolver));
8987 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8989 if (resolver->mustbesecure != NULL)
8990 dns_rbt_destroy(&resolver->mustbesecure);
8992 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8996 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8999 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
9000 isc_boolean_t value)
9002 isc_result_t result;
9004 REQUIRE(VALID_RESOLVER(resolver));
9007 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
9009 if (resolver->mustbesecure == NULL) {
9010 result = dns_rbt_create(resolver->mctx, NULL, NULL,
9011 &resolver->mustbesecure);
9012 if (result != ISC_R_SUCCESS)
9015 result = dns_rbt_addname(resolver->mustbesecure, name,
9016 value ? &yes : &no);
9019 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
9025 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
9027 isc_boolean_t value = ISC_FALSE;
9028 isc_result_t result;
9030 REQUIRE(VALID_RESOLVER(resolver));
9033 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
9035 if (resolver->mustbesecure == NULL)
9037 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
9038 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
9039 value = *(isc_boolean_t*)data;
9042 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
9048 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
9049 isc_uint32_t *min, isc_uint32_t *max)
9051 REQUIRE(VALID_RESOLVER(resolver));
9053 LOCK(&resolver->lock);
9055 *cur = resolver->spillat;
9057 *min = resolver->spillatmin;
9059 *max = resolver->spillatmax;
9060 UNLOCK(&resolver->lock);
9064 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
9067 REQUIRE(VALID_RESOLVER(resolver));
9069 LOCK(&resolver->lock);
9070 resolver->spillatmin = resolver->spillat = min;
9071 resolver->spillatmax = max;
9072 UNLOCK(&resolver->lock);
9076 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
9077 REQUIRE(VALID_RESOLVER(resolver));
9079 return (resolver->zero_no_soa_ttl);
9083 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
9084 REQUIRE(VALID_RESOLVER(resolver));
9086 resolver->zero_no_soa_ttl = state;
9090 dns_resolver_getoptions(dns_resolver_t *resolver) {
9091 REQUIRE(VALID_RESOLVER(resolver));
9093 return (resolver->options);
9097 dns_resolver_gettimeout(dns_resolver_t *resolver) {
9098 REQUIRE(VALID_RESOLVER(resolver));
9100 return (resolver->query_timeout);
9104 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
9105 REQUIRE(VALID_RESOLVER(resolver));
9108 seconds = DEFAULT_QUERY_TIMEOUT;
9109 if (seconds > MAXIMUM_QUERY_TIMEOUT)
9110 seconds = MAXIMUM_QUERY_TIMEOUT;
9111 if (seconds < MINIMUM_QUERY_TIMEOUT)
9112 seconds = MINIMUM_QUERY_TIMEOUT;
9114 resolver->query_timeout = seconds;
9118 dns_resolver_setmaxdepth(dns_resolver_t *resolver, unsigned int maxdepth) {
9119 REQUIRE(VALID_RESOLVER(resolver));
9120 resolver->maxdepth = maxdepth;
9124 dns_resolver_getmaxdepth(dns_resolver_t *resolver) {
9125 REQUIRE(VALID_RESOLVER(resolver));
9126 return (resolver->maxdepth);