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/counter.h>
26 #include <isc/platform.h>
27 #include <isc/print.h>
28 #include <isc/string.h>
29 #include <isc/random.h>
31 #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 unsigned int options;
176 unsigned int attributes;
178 unsigned int connects;
179 unsigned char data[512];
182 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
183 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
185 #define RESQUERY_ATTR_CANCELED 0x02
187 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
188 #define RESQUERY_CANCELED(q) (((q)->attributes & \
189 RESQUERY_ATTR_CANCELED) != 0)
190 #define RESQUERY_SENDING(q) ((q)->sends > 0)
193 fetchstate_init = 0, /*%< Start event has not run yet. */
195 fetchstate_done /*%< FETCHDONE events posted. */
199 badns_unreachable = 0,
207 dns_resolver_t * res;
209 dns_rdatatype_t type;
210 unsigned int options;
211 unsigned int bucketnum;
215 /*% Locked by appropriate bucket lock. */
217 isc_boolean_t want_shutdown;
218 isc_boolean_t cloned;
219 isc_boolean_t spilled;
220 unsigned int references;
221 isc_event_t control_event;
222 ISC_LINK(struct fetchctx) link;
223 ISC_LIST(dns_fetchevent_t) events;
224 /*% Locked by task event serialization. */
226 dns_rdataset_t nameservers;
227 unsigned int attributes;
230 isc_interval_t interval;
231 dns_message_t * qmessage;
232 dns_message_t * rmessage;
233 ISC_LIST(resquery_t) queries;
234 dns_adbfindlist_t finds;
235 dns_adbfind_t * find;
236 dns_adbfindlist_t altfinds;
237 dns_adbfind_t * altfind;
238 dns_adbaddrinfolist_t forwaddrs;
239 dns_adbaddrinfolist_t altaddrs;
240 isc_sockaddrlist_t forwarders;
241 dns_fwdpolicy_t fwdpolicy;
242 isc_sockaddrlist_t bad;
243 isc_sockaddrlist_t edns;
244 isc_sockaddrlist_t edns512;
245 isc_sockaddrlist_t bad_edns;
246 dns_validator_t * validator;
247 ISC_LIST(dns_validator_t) validators;
250 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 referrals;
311 unsigned int lamecount;
313 unsigned int badresp;
315 unsigned int findfail;
316 unsigned int valfail;
317 isc_boolean_t timeout;
318 dns_adbaddrinfo_t *addrinfo;
319 isc_sockaddr_t *client;
323 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
324 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
326 #define FCTX_ATTR_HAVEANSWER 0x0001
327 #define FCTX_ATTR_GLUING 0x0002
328 #define FCTX_ATTR_ADDRWAIT 0x0004
329 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
330 #define FCTX_ATTR_WANTCACHE 0x0010
331 #define FCTX_ATTR_WANTNCACHE 0x0020
332 #define FCTX_ATTR_NEEDEDNS0 0x0040
333 #define FCTX_ATTR_TRIEDFIND 0x0080
334 #define FCTX_ATTR_TRIEDALT 0x0100
336 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
338 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
340 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
342 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
344 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
345 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
346 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
347 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
348 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
351 dns_adbaddrinfo_t * addrinfo;
357 fetchctx_t * private;
360 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
361 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
363 typedef struct fctxbucket {
366 ISC_LIST(fetchctx_t) fctxs;
367 isc_boolean_t exiting;
371 typedef struct alternate {
372 isc_boolean_t isaddress;
380 ISC_LINK(struct alternate) link;
383 typedef struct dns_badcache dns_badcache_t;
384 struct dns_badcache {
385 dns_badcache_t * next;
386 dns_rdatatype_t type;
388 unsigned int hashval;
391 #define DNS_BADCACHE_SIZE 1021
392 #define DNS_BADCACHE_TTL(fctx) \
393 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
395 struct dns_resolver {
401 isc_mutex_t primelock;
402 dns_rdataclass_t rdclass;
403 isc_socketmgr_t * socketmgr;
404 isc_timermgr_t * timermgr;
405 isc_taskmgr_t * taskmgr;
407 isc_boolean_t frozen;
408 unsigned int options;
409 dns_dispatchmgr_t * dispatchmgr;
410 dns_dispatch_t * dispatchv4;
411 isc_boolean_t exclusivev4;
412 dns_dispatch_t * dispatchv6;
413 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;
434 unsigned int maxqueries;
436 /* Locked by lock. */
437 unsigned int references;
438 isc_boolean_t exiting;
439 isc_eventlist_t whenshutdown;
440 unsigned int activebuckets;
441 isc_boolean_t priming;
442 unsigned int spillat; /* clients-per-query */
443 unsigned int nextdisp;
446 dns_badcache_t ** badcache;
447 unsigned int badcount;
448 unsigned int badhash;
449 unsigned int badsweep;
451 /* Locked by primelock. */
452 dns_fetch_t * primefetch;
453 /* Locked by nlock. */
457 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
458 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
461 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
462 * which we also use as an addrinfo flag.
464 #define FCTX_ADDRINFO_MARK 0x0001
465 #define FCTX_ADDRINFO_FORWARDER 0x1000
466 #define FCTX_ADDRINFO_TRIED 0x2000
467 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
469 #define ISFORWARDER(a) (((a)->flags & \
470 FCTX_ADDRINFO_FORWARDER) != 0)
471 #define TRIED(a) (((a)->flags & \
472 FCTX_ADDRINFO_TRIED) != 0)
474 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
475 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
477 static void destroy(dns_resolver_t *res);
478 static void empty_bucket(dns_resolver_t *res);
479 static isc_result_t resquery_send(resquery_t *query);
480 static void resquery_response(isc_task_t *task, isc_event_t *event);
481 static void resquery_connected(isc_task_t *task, isc_event_t *event);
482 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
483 isc_boolean_t badcache);
484 static void fctx_destroy(fetchctx_t *fctx);
485 static isc_boolean_t fctx_unlink(fetchctx_t *fctx);
486 static isc_result_t ncache_adderesult(dns_message_t *message,
487 dns_db_t *cache, dns_dbnode_t *node,
488 dns_rdatatype_t covers,
489 isc_stdtime_t now, dns_ttl_t maxttl,
490 isc_boolean_t optout,
491 isc_boolean_t secure,
492 dns_rdataset_t *ardataset,
493 isc_result_t *eresultp);
494 static void validated(isc_task_t *task, isc_event_t *event);
495 static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked);
496 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
497 isc_result_t reason, badnstype_t badtype);
498 static inline isc_result_t findnoqname(fetchctx_t *fctx, dns_name_t *name,
499 dns_rdatatype_t type,
500 dns_name_t **noqname);
503 * Increment resolver-related statistics counters.
506 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
507 if (res->view->resstats != NULL)
508 isc_stats_increment(res->view->resstats, counter);
512 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
513 dns_rdatatype_t type, dns_rdataset_t *rdataset,
514 dns_rdataset_t *sigrdataset, unsigned int valoptions,
517 dns_validator_t *validator = NULL;
518 dns_valarg_t *valarg;
521 valarg = isc_mem_get(fctx->mctx, sizeof(*valarg));
523 return (ISC_R_NOMEMORY);
526 valarg->addrinfo = addrinfo;
528 if (!ISC_LIST_EMPTY(fctx->validators))
529 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
531 result = dns_validator_create(fctx->res->view, name, type, rdataset,
532 sigrdataset, fctx->rmessage,
533 valoptions, task, validated, valarg,
535 if (result == ISC_R_SUCCESS) {
536 inc_stats(fctx->res, dns_resstatscounter_val);
537 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
538 INSIST(fctx->validator == NULL);
539 fctx->validator = validator;
541 ISC_LIST_APPEND(fctx->validators, validator, link);
543 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
548 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
549 dns_namereln_t namereln;
550 dns_rdata_rrsig_t rrsig;
551 dns_rdata_t rdata = DNS_RDATA_INIT;
556 for (result = dns_rdataset_first(rdataset);
557 result == ISC_R_SUCCESS;
558 result = dns_rdataset_next(rdataset)) {
559 dns_rdataset_current(rdataset, &rdata);
560 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
561 RUNTIME_CHECK(result == ISC_R_SUCCESS);
562 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
564 if (namereln == dns_namereln_subdomain)
566 dns_rdata_reset(&rdata);
572 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
574 dns_name_t *domain = &fctx->domain;
575 dns_rdataset_t *rdataset;
576 dns_rdatatype_t type;
578 isc_boolean_t keep_auth = ISC_FALSE;
580 if (message->rcode == dns_rcode_nxdomain)
584 * A DS RRset can appear anywhere in a zone, even for a delegation-only
585 * zone. So a response to an explicit query for this type should be
586 * excluded from delegation-only fixup.
588 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
589 * response to a query for these types can never violate the
590 * delegation-only assumption: if the query name is below a
591 * zone cut, the response should normally be a referral, which should
592 * be accepted; if the query name is below a zone cut but the server
593 * happens to have authority for the zone of the query name, the
594 * response is a (non-referral) answer. But this does not violate
595 * delegation-only because the query name must be in a different zone
596 * due to the "apex-only" nature of these types. Note that if the
597 * remote server happens to have authority for a child zone of a
598 * delegation-only zone, we may still incorrectly "fix" the response
599 * with NXDOMAIN for queries for other types. Unfortunately it's
600 * generally impossible to differentiate this case from violation of
601 * the delegation-only assumption. Once the resolver learns the
602 * correct zone cut, possibly via a separate query for an "apex-only"
603 * type, queries for other types will be resolved correctly.
605 * A query for type ANY will be accepted if it hits an exceptional
606 * type above in the answer section as it should be from a child
609 * Also accept answers with RRSIG records from the child zone.
610 * Direct queries for RRSIG records should not be answered from
614 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
615 (fctx->type == dns_rdatatype_ns ||
616 fctx->type == dns_rdatatype_ds ||
617 fctx->type == dns_rdatatype_soa ||
618 fctx->type == dns_rdatatype_any ||
619 fctx->type == dns_rdatatype_rrsig ||
620 fctx->type == dns_rdatatype_dnskey)) {
621 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
622 while (result == ISC_R_SUCCESS) {
624 dns_message_currentname(message, DNS_SECTION_ANSWER,
626 for (rdataset = ISC_LIST_HEAD(name->list);
628 rdataset = ISC_LIST_NEXT(rdataset, link)) {
629 if (!dns_name_equal(name, &fctx->name))
631 type = rdataset->type;
635 if (type == dns_rdatatype_rrsig &&
636 rrsig_fromchildzone(fctx, rdataset))
639 * Direct query for apex records or DS.
641 if (fctx->type == type &&
642 (type == dns_rdatatype_ds ||
643 type == dns_rdatatype_ns ||
644 type == dns_rdatatype_soa ||
645 type == dns_rdatatype_dnskey))
648 * Indirect query for apex records or DS.
650 if (fctx->type == dns_rdatatype_any &&
651 (type == dns_rdatatype_ns ||
652 type == dns_rdatatype_ds ||
653 type == dns_rdatatype_soa ||
654 type == dns_rdatatype_dnskey))
657 result = dns_message_nextname(message,
663 * A NODATA response to a DS query?
665 if (fctx->type == dns_rdatatype_ds &&
666 message->counts[DNS_SECTION_ANSWER] == 0)
669 /* Look for referral or indication of answer from child zone? */
670 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
673 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
674 while (result == ISC_R_SUCCESS) {
676 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
677 for (rdataset = ISC_LIST_HEAD(name->list);
679 rdataset = ISC_LIST_NEXT(rdataset, link)) {
680 type = rdataset->type;
681 if (type == dns_rdatatype_soa &&
682 dns_name_equal(name, domain))
683 keep_auth = ISC_TRUE;
685 if (type != dns_rdatatype_ns &&
686 type != dns_rdatatype_soa &&
687 type != dns_rdatatype_rrsig)
690 if (type == dns_rdatatype_rrsig) {
691 if (rrsig_fromchildzone(fctx, rdataset))
697 /* NS or SOA records. */
698 if (dns_name_equal(name, domain)) {
700 * If a query for ANY causes a negative
701 * response, we can be sure that this is
702 * an empty node. For other type of queries
703 * we cannot differentiate an empty node
704 * from a node that just doesn't have that
705 * type of record. We only accept the former
708 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
709 fctx->type == dns_rdatatype_any)
711 } else if (dns_name_issubdomain(name, domain)) {
712 /* Referral or answer from child zone. */
716 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
720 message->rcode = dns_rcode_nxdomain;
721 message->counts[DNS_SECTION_ANSWER] = 0;
723 message->counts[DNS_SECTION_AUTHORITY] = 0;
724 message->counts[DNS_SECTION_ADDITIONAL] = 0;
728 static inline isc_result_t
729 fctx_starttimer(fetchctx_t *fctx) {
731 * Start the lifetime timer for fctx.
733 * This is also used for stopping the idle timer; in that
734 * case we must purge events already posted to ensure that
735 * no further idle events are delivered.
737 return (isc_timer_reset(fctx->timer, isc_timertype_once,
738 &fctx->expires, NULL, ISC_TRUE));
742 fctx_stoptimer(fetchctx_t *fctx) {
746 * We don't return a result if resetting the timer to inactive fails
747 * since there's nothing to be done about it. Resetting to inactive
748 * should never fail anyway, since the code as currently written
749 * cannot fail in that case.
751 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
752 NULL, NULL, ISC_TRUE);
753 if (result != ISC_R_SUCCESS) {
754 UNEXPECTED_ERROR(__FILE__, __LINE__,
755 "isc_timer_reset(): %s",
756 isc_result_totext(result));
761 static inline isc_result_t
762 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
764 * Start the idle timer for fctx. The lifetime timer continues
767 return (isc_timer_reset(fctx->timer, isc_timertype_once,
768 &fctx->expires, interval, ISC_FALSE));
772 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
773 * we use fctx_stopidletimer for readability in the code below.
775 #define fctx_stopidletimer fctx_starttimer
779 resquery_destroy(resquery_t **queryp) {
782 REQUIRE(queryp != NULL);
784 REQUIRE(!ISC_LINK_LINKED(query, link));
786 INSIST(query->tcpsocket == NULL);
788 query->fctx->nqueries--;
789 if (SHUTTINGDOWN(query->fctx)) {
790 dns_resolver_t *res = query->fctx->res;
791 if (maybe_destroy(query->fctx, ISC_FALSE))
795 isc_mem_put(query->mctx, query, sizeof(*query));
800 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
801 isc_time_t *finish, isc_boolean_t no_response)
805 unsigned int rtt, rttms;
808 dns_adbaddrinfo_t *addrinfo;
809 isc_socket_t *socket;
814 FCTXTRACE("cancelquery");
816 REQUIRE(!RESQUERY_CANCELED(query));
818 query->attributes |= RESQUERY_ATTR_CANCELED;
821 * Should we update the RTT?
823 if (finish != NULL || no_response) {
824 if (finish != NULL) {
826 * We have both the start and finish times for this
827 * packet, so we can compute a real RTT.
829 rtt = (unsigned int)isc_time_microdiff(finish,
831 factor = DNS_ADB_RTTADJDEFAULT;
834 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
836 dns_resstatscounter_queryrtt0);
837 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
839 dns_resstatscounter_queryrtt1);
840 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
842 dns_resstatscounter_queryrtt2);
843 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
845 dns_resstatscounter_queryrtt3);
846 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
848 dns_resstatscounter_queryrtt4);
851 dns_resstatscounter_queryrtt5);
855 * We don't have an RTT for this query. Maybe the
856 * packet was lost, or maybe this server is very
857 * slow. We don't know. Increase the RTT.
860 rtt = query->addrinfo->srtt + 200000;
861 if (rtt > MAX_SINGLE_QUERY_TIMEOUT_US)
862 rtt = MAX_SINGLE_QUERY_TIMEOUT_US;
864 * Replace the current RTT with our value.
866 factor = DNS_ADB_RTTADJREPLACE;
868 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
871 /* Remember that the server has been tried. */
872 if (!TRIED(query->addrinfo)) {
873 dns_adb_changeflags(fctx->adb, query->addrinfo,
874 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
878 * Age RTTs of servers not tried.
880 factor = DNS_ADB_RTTADJAGE;
882 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
884 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
885 if (UNMARKED(addrinfo))
886 dns_adb_adjustsrtt(fctx->adb, addrinfo,
889 if (finish != NULL && TRIEDFIND(fctx))
890 for (find = ISC_LIST_HEAD(fctx->finds);
892 find = ISC_LIST_NEXT(find, publink))
893 for (addrinfo = ISC_LIST_HEAD(find->list);
895 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
896 if (UNMARKED(addrinfo))
897 dns_adb_adjustsrtt(fctx->adb, addrinfo,
900 if (finish != NULL && TRIEDALT(fctx)) {
901 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
903 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
904 if (UNMARKED(addrinfo))
905 dns_adb_adjustsrtt(fctx->adb, addrinfo,
907 for (find = ISC_LIST_HEAD(fctx->altfinds);
909 find = ISC_LIST_NEXT(find, publink))
910 for (addrinfo = ISC_LIST_HEAD(find->list);
912 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
913 if (UNMARKED(addrinfo))
914 dns_adb_adjustsrtt(fctx->adb, addrinfo,
919 * Check for any outstanding socket events. If they exist, cancel
920 * them and let the event handlers finish the cleanup. The resolver
921 * only needs to worry about managing the connect and send events;
922 * the dispatcher manages the recv events.
924 if (RESQUERY_CONNECTING(query)) {
926 * Cancel the connect.
928 if (query->tcpsocket != NULL) {
929 isc_socket_cancel(query->tcpsocket, NULL,
930 ISC_SOCKCANCEL_CONNECT);
931 } else if (query->dispentry != NULL) {
932 INSIST(query->exclusivesocket);
933 socket = dns_dispatch_getentrysocket(query->dispentry);
935 isc_socket_cancel(socket, NULL,
936 ISC_SOCKCANCEL_CONNECT);
938 } else if (RESQUERY_SENDING(query)) {
940 * Cancel the pending send.
942 if (query->exclusivesocket && query->dispentry != NULL)
943 socket = dns_dispatch_getentrysocket(query->dispentry);
945 socket = dns_dispatch_getsocket(query->dispatch);
947 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
950 if (query->dispentry != NULL)
951 dns_dispatch_removeresponse(&query->dispentry, deventp);
953 ISC_LIST_UNLINK(fctx->queries, query, link);
955 if (query->tsig != NULL)
956 isc_buffer_free(&query->tsig);
958 if (query->tsigkey != NULL)
959 dns_tsigkey_detach(&query->tsigkey);
961 if (query->dispatch != NULL)
962 dns_dispatch_detach(&query->dispatch);
964 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
966 * It's safe to destroy the query now.
968 resquery_destroy(&query);
972 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
973 resquery_t *query, *next_query;
975 FCTXTRACE("cancelqueries");
977 for (query = ISC_LIST_HEAD(fctx->queries);
979 query = next_query) {
980 next_query = ISC_LIST_NEXT(query, link);
981 fctx_cancelquery(&query, NULL, NULL, no_response);
986 fctx_cleanupfinds(fetchctx_t *fctx) {
987 dns_adbfind_t *find, *next_find;
989 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
991 for (find = ISC_LIST_HEAD(fctx->finds);
994 next_find = ISC_LIST_NEXT(find, publink);
995 ISC_LIST_UNLINK(fctx->finds, find, publink);
996 dns_adb_destroyfind(&find);
1002 fctx_cleanupaltfinds(fetchctx_t *fctx) {
1003 dns_adbfind_t *find, *next_find;
1005 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1007 for (find = ISC_LIST_HEAD(fctx->altfinds);
1010 next_find = ISC_LIST_NEXT(find, publink);
1011 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
1012 dns_adb_destroyfind(&find);
1014 fctx->altfind = NULL;
1018 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
1019 dns_adbaddrinfo_t *addr, *next_addr;
1021 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1023 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
1026 next_addr = ISC_LIST_NEXT(addr, publink);
1027 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
1028 dns_adb_freeaddrinfo(fctx->adb, &addr);
1033 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
1034 dns_adbaddrinfo_t *addr, *next_addr;
1036 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1038 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
1041 next_addr = ISC_LIST_NEXT(addr, publink);
1042 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1043 dns_adb_freeaddrinfo(fctx->adb, &addr);
1048 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1049 FCTXTRACE("stopeverything");
1050 fctx_cancelqueries(fctx, no_response);
1051 fctx_cleanupfinds(fctx);
1052 fctx_cleanupaltfinds(fctx);
1053 fctx_cleanupforwaddrs(fctx);
1054 fctx_cleanupaltaddrs(fctx);
1055 fctx_stoptimer(fctx);
1059 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1060 dns_fetchevent_t *event, *next_event;
1062 unsigned int count = 0;
1064 isc_boolean_t logit = ISC_FALSE;
1066 unsigned int old_spillat;
1067 unsigned int new_spillat = 0; /* initialized to silence
1068 compiler warnings */
1071 * Caller must be holding the appropriate bucket lock.
1073 REQUIRE(fctx->state == fetchstate_done);
1075 FCTXTRACE("sendevents");
1078 * Keep some record of fetch result for logging later (if required).
1080 fctx->result = result;
1081 fctx->exitline = line;
1083 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1085 for (event = ISC_LIST_HEAD(fctx->events);
1087 event = next_event) {
1088 next_event = ISC_LIST_NEXT(event, ev_link);
1089 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1090 task = event->ev_sender;
1091 event->ev_sender = fctx;
1092 event->vresult = fctx->vresult;
1093 if (!HAVE_ANSWER(fctx))
1094 event->result = result;
1096 INSIST(result != ISC_R_SUCCESS ||
1097 dns_rdataset_isassociated(event->rdataset) ||
1098 fctx->type == dns_rdatatype_any ||
1099 fctx->type == dns_rdatatype_rrsig ||
1100 fctx->type == dns_rdatatype_sig);
1103 * Negative results must be indicated in event->result.
1105 if (dns_rdataset_isassociated(event->rdataset) &&
1106 NEGATIVE(event->rdataset)) {
1107 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1108 event->result == DNS_R_NCACHENXRRSET);
1111 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1115 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1117 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1118 LOCK(&fctx->res->lock);
1119 if (count == fctx->res->spillat && !fctx->res->exiting) {
1120 old_spillat = fctx->res->spillat;
1121 fctx->res->spillat += 5;
1122 if (fctx->res->spillat > fctx->res->spillatmax &&
1123 fctx->res->spillatmax != 0)
1124 fctx->res->spillat = fctx->res->spillatmax;
1125 new_spillat = fctx->res->spillat;
1126 if (new_spillat != old_spillat) {
1129 isc_interval_set(&i, 20 * 60, 0);
1130 result = isc_timer_reset(fctx->res->spillattimer,
1131 isc_timertype_ticker, NULL,
1133 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1135 UNLOCK(&fctx->res->lock);
1137 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1138 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1139 "clients-per-query increased to %u",
1145 log_edns(fetchctx_t *fctx) {
1146 char domainbuf[DNS_NAME_FORMATSIZE];
1148 if (fctx->reason == NULL)
1152 * We do not know if fctx->domain is the actual domain the record
1153 * lives in or a parent domain so we have a '?' after it.
1155 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1156 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1157 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1158 "success resolving '%s' (in '%s'?) after %s",
1159 fctx->info, domainbuf, fctx->reason);
1161 fctx->reason = NULL;
1165 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1166 dns_resolver_t *res;
1167 isc_boolean_t no_response;
1175 if (result == ISC_R_SUCCESS) {
1177 * Log any deferred EDNS timeout messages.
1180 no_response = ISC_TRUE;
1182 no_response = ISC_FALSE;
1184 fctx->reason = NULL;
1185 fctx_stopeverything(fctx, no_response);
1187 LOCK(&res->buckets[fctx->bucketnum].lock);
1189 fctx->state = fetchstate_done;
1190 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1191 fctx_sendevents(fctx, result, line);
1193 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1197 process_sendevent(resquery_t *query, isc_event_t *event) {
1198 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1199 isc_boolean_t retry = ISC_FALSE;
1200 isc_result_t result;
1205 if (RESQUERY_CANCELED(query)) {
1206 if (query->sends == 0 && query->connects == 0) {
1208 * This query was canceled while the
1209 * isc_socket_sendto/connect() was in progress.
1211 if (query->tcpsocket != NULL)
1212 isc_socket_detach(&query->tcpsocket);
1213 resquery_destroy(&query);
1216 switch (sevent->result) {
1220 case ISC_R_HOSTUNREACH:
1221 case ISC_R_NETUNREACH:
1223 case ISC_R_ADDRNOTAVAIL:
1224 case ISC_R_CONNREFUSED:
1227 * No route to remote.
1229 add_bad(fctx, query->addrinfo, sevent->result,
1231 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1236 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
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);
1437 dns_dispatch_getlocaladdress(res->dispatchv4,
1442 dns_dispatch_getlocaladdress(res->dispatchv6,
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(res->dispatchv4,
1501 query->exclusivesocket = res->exclusivev4;
1504 dns_dispatch_attach(res->dispatchv6,
1506 query->exclusivesocket = res->exclusivev6;
1509 result = ISC_R_NOTIMPLEMENTED;
1514 * We should always have a valid dispatcher here. If we
1515 * don't support a protocol family, then its dispatcher
1516 * will be NULL, but we shouldn't be finding addresses for
1517 * protocol types we don't support, so the dispatcher
1518 * we found should never be NULL.
1520 INSIST(query->dispatch != NULL);
1523 query->dispentry = NULL;
1526 query->tsigkey = NULL;
1527 ISC_LINK_INIT(query, link);
1528 query->magic = QUERY_MAGIC;
1530 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1532 * Connect to the remote server.
1534 * XXXRTH Should we attach to the socket?
1536 result = isc_socket_connect(query->tcpsocket,
1537 &addrinfo->sockaddr, task,
1538 resquery_connected, query);
1539 if (result != ISC_R_SUCCESS)
1540 goto cleanup_socket;
1542 QTRACE("connecting via TCP");
1544 result = resquery_send(query);
1545 if (result != ISC_R_SUCCESS)
1546 goto cleanup_dispatch;
1551 ISC_LIST_APPEND(fctx->queries, query, link);
1552 query->fctx->nqueries++;
1553 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1554 inc_stats(res, dns_resstatscounter_queryv4);
1556 inc_stats(res, dns_resstatscounter_queryv6);
1557 if (res->view->resquerystats != NULL)
1558 dns_rdatatypestats_increment(res->view->resquerystats,
1561 return (ISC_R_SUCCESS);
1564 isc_socket_detach(&query->tcpsocket);
1567 if (query->dispatch != NULL)
1568 dns_dispatch_detach(&query->dispatch);
1571 if (query->connects == 0) {
1573 isc_mem_put(fctx->mctx, query, sizeof(*query));
1577 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1582 static isc_boolean_t
1583 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1586 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1588 sa = ISC_LIST_NEXT(sa, link)) {
1589 if (isc_sockaddr_equal(sa, address))
1597 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1600 if (bad_edns(fctx, address))
1603 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1608 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1611 static isc_boolean_t
1612 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1615 for (sa = ISC_LIST_HEAD(fctx->edns);
1617 sa = ISC_LIST_NEXT(sa, link)) {
1618 if (isc_sockaddr_equal(sa, address))
1626 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1629 if (triededns(fctx, address))
1632 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1637 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1640 static isc_boolean_t
1641 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1644 for (sa = ISC_LIST_HEAD(fctx->edns512);
1646 sa = ISC_LIST_NEXT(sa, link)) {
1647 if (isc_sockaddr_equal(sa, address))
1655 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1658 if (triededns512(fctx, address))
1661 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1666 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1670 resquery_send(resquery_t *query) {
1672 isc_result_t result;
1673 dns_name_t *qname = NULL;
1674 dns_rdataset_t *qrdataset = NULL;
1676 dns_resolver_t *res;
1678 isc_socket_t *socket;
1679 isc_buffer_t tcpbuffer;
1680 isc_sockaddr_t *address;
1681 isc_buffer_t *buffer;
1682 isc_netaddr_t ipaddr;
1683 dns_tsigkey_t *tsigkey = NULL;
1684 dns_peer_t *peer = NULL;
1685 isc_boolean_t useedns;
1686 dns_compress_t cctx;
1687 isc_boolean_t cleanup_cctx = ISC_FALSE;
1688 isc_boolean_t secure_domain;
1689 isc_boolean_t connecting = ISC_FALSE;
1690 dns_ednsopt_t ednsopts[EDNSOPTS];
1691 unsigned ednsopt = 0;
1697 task = res->buckets[fctx->bucketnum].task;
1700 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1702 * Reserve space for the TCP message length.
1704 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1705 isc_buffer_init(&query->buffer, query->data + 2,
1706 sizeof(query->data) - 2);
1707 buffer = &tcpbuffer;
1709 isc_buffer_init(&query->buffer, query->data,
1710 sizeof(query->data));
1711 buffer = &query->buffer;
1714 result = dns_message_gettempname(fctx->qmessage, &qname);
1715 if (result != ISC_R_SUCCESS)
1717 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1718 if (result != ISC_R_SUCCESS)
1722 * Get a query id from the dispatch.
1724 result = dns_dispatch_addresponse2(query->dispatch,
1725 &query->addrinfo->sockaddr,
1732 if (result != ISC_R_SUCCESS)
1735 fctx->qmessage->opcode = dns_opcode_query;
1740 dns_name_init(qname, NULL);
1741 dns_name_clone(&fctx->name, qname);
1742 dns_rdataset_init(qrdataset);
1743 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1744 ISC_LIST_APPEND(qname->list, qrdataset, link);
1745 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1750 * Set RD if the client has requested that we do a recursive query,
1751 * or if we're sending to a forwarder.
1753 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1754 ISFORWARDER(query->addrinfo))
1755 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1758 * Set CD if the client says don't validate or the question is
1759 * under a secure entry point.
1761 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1762 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1763 } else if (res->view->enablevalidation) {
1764 result = dns_view_issecuredomain(res->view, &fctx->name,
1766 if (result != ISC_R_SUCCESS)
1767 secure_domain = ISC_FALSE;
1768 if (res->view->dlv != NULL)
1769 secure_domain = ISC_TRUE;
1771 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1775 * We don't have to set opcode because it defaults to query.
1777 fctx->qmessage->id = query->id;
1780 * Convert the question to wire format.
1782 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1783 if (result != ISC_R_SUCCESS)
1784 goto cleanup_message;
1785 cleanup_cctx = ISC_TRUE;
1787 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1789 if (result != ISC_R_SUCCESS)
1790 goto cleanup_message;
1792 result = dns_message_rendersection(fctx->qmessage,
1793 DNS_SECTION_QUESTION, 0);
1794 if (result != ISC_R_SUCCESS)
1795 goto cleanup_message;
1798 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1799 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1802 * The ADB does not know about servers with "edns no". Check this,
1803 * and then inform the ADB for future use.
1805 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1807 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1810 query->options |= DNS_FETCHOPT_NOEDNS0;
1811 dns_adb_changeflags(fctx->adb, query->addrinfo,
1812 DNS_FETCHOPT_NOEDNS0,
1813 DNS_FETCHOPT_NOEDNS0);
1816 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1817 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1818 query->options |= DNS_FETCHOPT_NOEDNS0;
1821 * Handle timeouts by reducing the UDP response size to 512 bytes
1822 * then if that doesn't work disabling EDNS (includes DO) and CD.
1824 * These timeout can be due to:
1825 * * broken nameservers that don't respond to EDNS queries.
1826 * * broken/misconfigured firewalls and NAT implementations
1827 * that don't handle IP fragmentation.
1828 * * broken/misconfigured firewalls that don't handle responses
1829 * greater than 512 bytes.
1830 * * broken/misconfigured firewalls that don't handle EDNS, DO
1832 * * packet loss / link outage.
1834 if (fctx->timeout) {
1835 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1836 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1837 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1838 query->options |= DNS_FETCHOPT_NOEDNS0;
1839 fctx->reason = "disabling EDNS";
1840 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1841 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1842 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1843 query->options |= DNS_FETCHOPT_EDNS512;
1844 fctx->reason = "reducing the advertised EDNS UDP "
1845 "packet size to 512 octets";
1847 fctx->timeout = ISC_FALSE;
1851 * Use EDNS0, unless the caller doesn't want it, or we know that
1852 * the remote server doesn't like it.
1854 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1855 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1856 unsigned int version = 0; /* Default version. */
1858 isc_uint16_t udpsize = res->udpsize;
1859 isc_boolean_t reqnsid = res->view->requestnsid;
1861 flags = query->addrinfo->flags;
1862 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1863 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1864 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1866 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1868 else if (peer != NULL)
1869 (void)dns_peer_getudpsize(peer, &udpsize);
1871 /* request NSID for current view or peer? */
1873 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1875 INSIST(ednsopt < EDNSOPTS);
1876 ednsopts[ednsopt].code = DNS_OPT_NSID;
1877 ednsopts[ednsopt].length = 0;
1878 ednsopts[ednsopt].value = NULL;
1881 result = fctx_addopt(fctx->qmessage, version,
1882 udpsize, ednsopts, ednsopt);
1883 if (reqnsid && result == ISC_R_SUCCESS) {
1884 query->options |= DNS_FETCHOPT_WANTNSID;
1885 } else if (result != ISC_R_SUCCESS) {
1887 * We couldn't add the OPT, but we'll press on.
1888 * We're not using EDNS0, so set the NOEDNS0
1891 query->options |= DNS_FETCHOPT_NOEDNS0;
1895 * We know this server doesn't like EDNS0, so we
1896 * won't use it. Set the NOEDNS0 bit since we're
1899 query->options |= DNS_FETCHOPT_NOEDNS0;
1904 * If we need EDNS0 to do this query and aren't using it, we lose.
1906 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1907 result = DNS_R_SERVFAIL;
1908 goto cleanup_message;
1911 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1912 add_triededns(fctx, &query->addrinfo->sockaddr);
1914 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1915 add_triededns512(fctx, &query->addrinfo->sockaddr);
1918 * Clear CD if EDNS is not in use.
1920 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1921 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1924 * Add TSIG record tailored to the current recipient.
1926 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1927 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1928 goto cleanup_message;
1930 if (tsigkey != NULL) {
1931 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1932 dns_tsigkey_detach(&tsigkey);
1933 if (result != ISC_R_SUCCESS)
1934 goto cleanup_message;
1937 result = dns_message_rendersection(fctx->qmessage,
1938 DNS_SECTION_ADDITIONAL, 0);
1939 if (result != ISC_R_SUCCESS)
1940 goto cleanup_message;
1942 result = dns_message_renderend(fctx->qmessage);
1943 if (result != ISC_R_SUCCESS)
1944 goto cleanup_message;
1946 dns_compress_invalidate(&cctx);
1947 cleanup_cctx = ISC_FALSE;
1949 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1950 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1952 result = dns_message_getquerytsig(fctx->qmessage,
1955 if (result != ISC_R_SUCCESS)
1956 goto cleanup_message;
1960 * If using TCP, write the length of the message at the beginning
1963 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1964 isc_buffer_usedregion(&query->buffer, &r);
1965 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1966 isc_buffer_add(&tcpbuffer, r.length);
1970 * We're now done with the query message.
1972 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1974 if (query->exclusivesocket)
1975 socket = dns_dispatch_getentrysocket(query->dispentry);
1977 socket = dns_dispatch_getsocket(query->dispatch);
1981 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1982 address = &query->addrinfo->sockaddr;
1983 if (query->exclusivesocket) {
1984 result = isc_socket_connect(socket, address, task,
1985 resquery_udpconnected,
1987 if (result != ISC_R_SUCCESS)
1988 goto cleanup_message;
1989 connecting = ISC_TRUE;
1993 isc_buffer_usedregion(buffer, &r);
1996 * XXXRTH Make sure we don't send to ourselves! We should probably
1997 * prune out these addresses when we get them from the ADB.
1999 result = isc_socket_sendto(socket, &r, task, resquery_senddone,
2000 query, address, NULL);
2001 if (result != ISC_R_SUCCESS) {
2004 * This query is still connecting.
2005 * Mark it as canceled so that it will just be
2006 * cleaned up when the connected event is received.
2007 * Keep fctx around until the event is processed.
2009 query->fctx->nqueries++;
2010 query->attributes |= RESQUERY_ATTR_CANCELED;
2012 goto cleanup_message;
2019 return (ISC_R_SUCCESS);
2023 dns_compress_invalidate(&cctx);
2025 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2028 * Stop the dispatcher from listening.
2030 dns_dispatch_removeresponse(&query->dispentry, NULL);
2034 dns_message_puttempname(fctx->qmessage, &qname);
2035 if (qrdataset != NULL)
2036 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2042 resquery_connected(isc_task_t *task, isc_event_t *event) {
2043 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2044 resquery_t *query = event->ev_arg;
2045 isc_boolean_t retry = ISC_FALSE;
2046 isc_interval_t interval;
2047 isc_result_t result;
2051 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2052 REQUIRE(VALID_QUERY(query));
2054 QTRACE("connected");
2061 * Currently we don't wait for the connect event before retrying
2062 * a query. This means that if we get really behind, we may end
2063 * up doing extra work!
2069 if (RESQUERY_CANCELED(query)) {
2071 * This query was canceled while the connect() was in
2074 isc_socket_detach(&query->tcpsocket);
2075 resquery_destroy(&query);
2077 switch (sevent->result) {
2081 * Extend the idle timer for TCP. 20 seconds
2082 * should be long enough for a TCP connection to be
2083 * established, a single DNS request to be sent,
2084 * and the response received.
2086 isc_interval_set(&interval, 20, 0);
2087 result = fctx_startidletimer(query->fctx, &interval);
2088 if (result != ISC_R_SUCCESS) {
2089 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2090 fctx_done(fctx, result, __LINE__);
2094 * We are connected. Create a dispatcher and
2098 attrs |= DNS_DISPATCHATTR_TCP;
2099 attrs |= DNS_DISPATCHATTR_PRIVATE;
2100 attrs |= DNS_DISPATCHATTR_CONNECTED;
2101 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2103 attrs |= DNS_DISPATCHATTR_IPV4;
2105 attrs |= DNS_DISPATCHATTR_IPV6;
2106 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2108 result = dns_dispatch_createtcp(query->dispatchmgr,
2110 query->fctx->res->taskmgr,
2111 4096, 2, 1, 1, 3, attrs,
2115 * Regardless of whether dns_dispatch_create()
2116 * succeeded or not, we don't need our reference
2117 * to the socket anymore.
2119 isc_socket_detach(&query->tcpsocket);
2121 if (result == ISC_R_SUCCESS)
2122 result = resquery_send(query);
2124 if (result != ISC_R_SUCCESS) {
2125 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2126 fctx_done(fctx, result, __LINE__);
2130 case ISC_R_NETUNREACH:
2131 case ISC_R_HOSTUNREACH:
2132 case ISC_R_CONNREFUSED:
2134 case ISC_R_ADDRNOTAVAIL:
2135 case ISC_R_CONNECTIONRESET:
2137 * No route to remote.
2139 isc_socket_detach(&query->tcpsocket);
2140 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2145 isc_socket_detach(&query->tcpsocket);
2146 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2151 isc_event_free(&event);
2155 * Behave as if the idle timer has expired. For TCP
2156 * connections this may not actually reflect the latest timer.
2158 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2159 result = fctx_stopidletimer(fctx);
2160 if (result != ISC_R_SUCCESS)
2161 fctx_done(fctx, result, __LINE__);
2163 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2168 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2170 dns_adbfind_t *find;
2171 dns_resolver_t *res;
2172 isc_boolean_t want_try = ISC_FALSE;
2173 isc_boolean_t want_done = ISC_FALSE;
2174 isc_boolean_t bucket_empty = ISC_FALSE;
2175 unsigned int bucketnum;
2176 isc_boolean_t destroy = ISC_FALSE;
2178 find = event->ev_sender;
2179 fctx = event->ev_arg;
2180 REQUIRE(VALID_FCTX(fctx));
2185 FCTXTRACE("finddone");
2187 bucketnum = fctx->bucketnum;
2188 LOCK(&res->buckets[bucketnum].lock);
2190 INSIST(fctx->pending > 0);
2193 if (ADDRWAIT(fctx)) {
2195 * The fetch is waiting for a name to be found.
2197 INSIST(!SHUTTINGDOWN(fctx));
2198 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2199 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES) {
2200 want_try = ISC_TRUE;
2203 if (fctx->pending == 0) {
2205 * We've got nothing else to wait for and don't
2206 * know the answer. There's nothing to do but
2209 want_done = ISC_TRUE;
2212 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2213 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2215 if (fctx->references == 0) {
2216 bucket_empty = fctx_unlink(fctx);
2220 UNLOCK(&res->buckets[bucketnum].lock);
2222 isc_event_free(&event);
2223 dns_adb_destroyfind(&find);
2226 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2228 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2237 static inline isc_boolean_t
2238 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2241 for (sa = ISC_LIST_HEAD(fctx->bad);
2243 sa = ISC_LIST_NEXT(sa, link)) {
2244 if (isc_sockaddr_equal(sa, address))
2251 static inline isc_boolean_t
2252 mark_bad(fetchctx_t *fctx) {
2253 dns_adbfind_t *curr;
2254 dns_adbaddrinfo_t *addrinfo;
2255 isc_boolean_t all_bad = ISC_TRUE;
2258 * Mark all known bad servers, so we don't try to talk to them
2263 * Mark any bad nameservers.
2265 for (curr = ISC_LIST_HEAD(fctx->finds);
2267 curr = ISC_LIST_NEXT(curr, publink)) {
2268 for (addrinfo = ISC_LIST_HEAD(curr->list);
2270 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2271 if (bad_server(fctx, &addrinfo->sockaddr))
2272 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2274 all_bad = ISC_FALSE;
2279 * Mark any bad forwarders.
2281 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2283 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2284 if (bad_server(fctx, &addrinfo->sockaddr))
2285 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2287 all_bad = ISC_FALSE;
2291 * Mark any bad alternates.
2293 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2295 curr = ISC_LIST_NEXT(curr, publink)) {
2296 for (addrinfo = ISC_LIST_HEAD(curr->list);
2298 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2299 if (bad_server(fctx, &addrinfo->sockaddr))
2300 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2302 all_bad = ISC_FALSE;
2306 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2308 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2309 if (bad_server(fctx, &addrinfo->sockaddr))
2310 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2312 all_bad = ISC_FALSE;
2319 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2320 badnstype_t badtype)
2322 char namebuf[DNS_NAME_FORMATSIZE];
2323 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2329 const char *spc = "";
2330 isc_sockaddr_t *address = &addrinfo->sockaddr;
2332 if (reason == DNS_R_LAME)
2336 case badns_unreachable:
2339 case badns_response:
2342 case badns_validation:
2343 break; /* counted as 'valfail' */
2347 if (bad_server(fctx, address)) {
2349 * We already know this server is bad.
2354 FCTXTRACE("add_bad");
2356 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
2360 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2362 if (reason == DNS_R_LAME) /* already logged */
2365 if (reason == DNS_R_UNEXPECTEDRCODE &&
2366 fctx->rmessage->rcode == dns_rcode_servfail &&
2367 ISFORWARDER(addrinfo))
2370 if (reason == DNS_R_UNEXPECTEDRCODE) {
2371 isc_buffer_init(&b, code, sizeof(code) - 1);
2372 dns_rcode_totext(fctx->rmessage->rcode, &b);
2373 code[isc_buffer_usedlength(&b)] = '\0';
2375 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2376 isc_buffer_init(&b, code, sizeof(code) - 1);
2377 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2378 code[isc_buffer_usedlength(&b)] = '\0';
2383 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2384 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2385 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2386 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2387 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2388 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2389 "error (%s%s%s) resolving '%s/%s/%s': %s",
2390 dns_result_totext(reason), spc, code,
2391 namebuf, typebuf, classbuf, addrbuf);
2395 * Sort addrinfo list by RTT.
2398 sort_adbfind(dns_adbfind_t *find) {
2399 dns_adbaddrinfo_t *best, *curr;
2400 dns_adbaddrinfolist_t sorted;
2402 /* Lame N^2 bubble sort. */
2403 ISC_LIST_INIT(sorted);
2404 while (!ISC_LIST_EMPTY(find->list)) {
2405 best = ISC_LIST_HEAD(find->list);
2406 curr = ISC_LIST_NEXT(best, publink);
2407 while (curr != NULL) {
2408 if (curr->srtt < best->srtt)
2410 curr = ISC_LIST_NEXT(curr, publink);
2412 ISC_LIST_UNLINK(find->list, best, publink);
2413 ISC_LIST_APPEND(sorted, best, publink);
2415 find->list = sorted;
2419 * Sort a list of finds by server RTT.
2422 sort_finds(dns_adbfindlist_t *findlist) {
2423 dns_adbfind_t *best, *curr;
2424 dns_adbfindlist_t sorted;
2425 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2427 /* Sort each find's addrinfo list by SRTT. */
2428 for (curr = ISC_LIST_HEAD(*findlist);
2430 curr = ISC_LIST_NEXT(curr, publink))
2433 /* Lame N^2 bubble sort. */
2434 ISC_LIST_INIT(sorted);
2435 while (!ISC_LIST_EMPTY(*findlist)) {
2436 best = ISC_LIST_HEAD(*findlist);
2437 bestaddrinfo = ISC_LIST_HEAD(best->list);
2438 INSIST(bestaddrinfo != NULL);
2439 curr = ISC_LIST_NEXT(best, publink);
2440 while (curr != NULL) {
2441 addrinfo = ISC_LIST_HEAD(curr->list);
2442 INSIST(addrinfo != NULL);
2443 if (addrinfo->srtt < bestaddrinfo->srtt) {
2445 bestaddrinfo = addrinfo;
2447 curr = ISC_LIST_NEXT(curr, publink);
2449 ISC_LIST_UNLINK(*findlist, best, publink);
2450 ISC_LIST_APPEND(sorted, best, publink);
2456 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2457 unsigned int options, unsigned int flags, isc_stdtime_t now,
2458 isc_boolean_t *need_alternate)
2460 dns_adbaddrinfo_t *ai;
2461 dns_adbfind_t *find;
2462 dns_resolver_t *res;
2463 isc_boolean_t unshared;
2464 isc_result_t result;
2467 unshared = ISC_TF((fctx->options & DNS_FETCHOPT_UNSHARED) != 0);
2469 * If this name is a subdomain of the query domain, tell
2470 * the ADB to start looking using zone/hint data. This keeps us
2471 * from getting stuck if the nameserver is beneath the zone cut
2472 * and we don't know its address (e.g. because the A record has
2475 if (dns_name_issubdomain(name, &fctx->domain))
2476 options |= DNS_ADBFIND_STARTATZONE;
2477 options |= DNS_ADBFIND_GLUEOK;
2478 options |= DNS_ADBFIND_HINTOK;
2481 * See what we know about this address.
2484 result = dns_adb_createfind2(fctx->adb,
2485 res->buckets[fctx->bucketnum].task,
2486 fctx_finddone, fctx, name,
2487 &fctx->name, fctx->type,
2490 fctx->depth + 1, fctx->qc, &find);
2491 if (result != ISC_R_SUCCESS) {
2492 if (result == DNS_R_ALIAS) {
2494 * XXXRTH Follow the CNAME/DNAME chain?
2496 dns_adb_destroyfind(&find);
2499 } else if (!ISC_LIST_EMPTY(find->list)) {
2501 * We have at least some of the addresses for the
2504 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2505 if (flags != 0 || port != 0) {
2506 for (ai = ISC_LIST_HEAD(find->list);
2508 ai = ISC_LIST_NEXT(ai, publink)) {
2511 isc_sockaddr_setport(&ai->sockaddr,
2515 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2516 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2518 ISC_LIST_APPEND(fctx->finds, find, publink);
2521 * We don't know any of the addresses for this
2524 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2526 * We're looking for them and will get an
2527 * event about it later.
2533 if (need_alternate != NULL &&
2534 !*need_alternate && unshared &&
2535 ((res->dispatchv4 == NULL &&
2536 find->result_v6 != DNS_R_NXDOMAIN) ||
2537 (res->dispatchv6 == NULL &&
2538 find->result_v4 != DNS_R_NXDOMAIN)))
2539 *need_alternate = ISC_TRUE;
2541 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2542 fctx->lamecount++; /* cached lame server */
2544 fctx->adberr++; /* unreachable server, etc. */
2547 * If we know there are no addresses for
2548 * the family we are using then try to add
2549 * an alternative server.
2551 if (need_alternate != NULL && !*need_alternate &&
2552 ((res->dispatchv4 == NULL &&
2553 find->result_v6 == DNS_R_NXRRSET) ||
2554 (res->dispatchv6 == NULL &&
2555 find->result_v4 == DNS_R_NXRRSET)))
2556 *need_alternate = ISC_TRUE;
2557 dns_adb_destroyfind(&find);
2562 static isc_boolean_t
2563 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2565 unsigned int nlabels;
2566 dns_namereln_t namereln;
2568 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2569 return (ISC_TF(namereln == dns_namereln_subdomain));
2573 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2574 dns_rdata_t rdata = DNS_RDATA_INIT;
2575 isc_result_t result;
2576 dns_resolver_t *res;
2578 unsigned int stdoptions = 0;
2580 dns_adbaddrinfo_t *ai;
2581 isc_boolean_t all_bad;
2583 isc_boolean_t need_alternate = ISC_FALSE;
2585 FCTXTRACE("getaddresses");
2588 * Don't pound on remote servers. (Failsafe!)
2591 if (fctx->restarts > 10) {
2592 FCTXTRACE("too many restarts");
2593 return (DNS_R_SERVFAIL);
2598 if (fctx->depth > res->maxdepth) {
2599 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
2600 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3),
2601 "too much NS indirection resolving '%s'",
2603 return (DNS_R_SERVFAIL);
2610 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2611 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2614 * If this fctx has forwarders, use them; otherwise use any
2615 * selective forwarders specified in the view; otherwise use the
2616 * resolver's forwarders (if any).
2618 sa = ISC_LIST_HEAD(fctx->forwarders);
2620 dns_forwarders_t *forwarders = NULL;
2621 dns_name_t *name = &fctx->name;
2623 unsigned int labels;
2624 dns_fixedname_t fixed;
2628 * DS records are found in the parent server.
2629 * Strip label to get the correct forwarder (if any).
2631 if (dns_rdatatype_atparent(fctx->type) &&
2632 dns_name_countlabels(name) > 1) {
2633 dns_name_init(&suffix, NULL);
2634 labels = dns_name_countlabels(name);
2635 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2639 dns_fixedname_init(&fixed);
2640 domain = dns_fixedname_name(&fixed);
2641 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2642 domain, &forwarders);
2643 if (result == ISC_R_SUCCESS) {
2644 sa = ISC_LIST_HEAD(forwarders->addrs);
2645 fctx->fwdpolicy = forwarders->fwdpolicy;
2646 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2647 isstrictsubdomain(domain, &fctx->domain)) {
2648 dns_name_free(&fctx->domain, fctx->mctx);
2649 dns_name_init(&fctx->domain, NULL);
2650 result = dns_name_dup(domain, fctx->mctx,
2652 if (result != ISC_R_SUCCESS)
2658 while (sa != NULL) {
2659 if ((isc_sockaddr_pf(sa) == AF_INET &&
2660 fctx->res->dispatchv4 == NULL) ||
2661 (isc_sockaddr_pf(sa) == AF_INET6 &&
2662 fctx->res->dispatchv6 == NULL)) {
2663 sa = ISC_LIST_NEXT(sa, link);
2667 result = dns_adb_findaddrinfo(fctx->adb,
2668 sa, &ai, 0); /* XXXMLG */
2669 if (result == ISC_R_SUCCESS) {
2670 dns_adbaddrinfo_t *cur;
2671 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2672 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2673 while (cur != NULL && cur->srtt < ai->srtt)
2674 cur = ISC_LIST_NEXT(cur, publink);
2676 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2679 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2681 sa = ISC_LIST_NEXT(sa, link);
2685 * If the forwarding policy is "only", we don't need the addresses
2686 * of the nameservers.
2688 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2692 * Normal nameservers.
2695 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2696 if (fctx->restarts == 1) {
2698 * To avoid sending out a flood of queries likely to
2699 * result in NXRRSET, we suppress fetches for address
2700 * families we don't have the first time through,
2701 * provided that we have addresses in some family we
2704 * We don't want to set this option all the time, since
2705 * if fctx->restarts > 1, we've clearly been having trouble
2706 * with the addresses we had, so getting more could help.
2708 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2710 if (res->dispatchv4 != NULL)
2711 stdoptions |= DNS_ADBFIND_INET;
2712 if (res->dispatchv6 != NULL)
2713 stdoptions |= DNS_ADBFIND_INET6;
2714 isc_stdtime_get(&now);
2716 INSIST(ISC_LIST_EMPTY(fctx->finds));
2717 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2719 for (result = dns_rdataset_first(&fctx->nameservers);
2720 result == ISC_R_SUCCESS;
2721 result = dns_rdataset_next(&fctx->nameservers))
2723 dns_rdataset_current(&fctx->nameservers, &rdata);
2725 * Extract the name from the NS record.
2727 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2728 if (result != ISC_R_SUCCESS)
2731 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2733 dns_rdata_reset(&rdata);
2734 dns_rdata_freestruct(&ns);
2736 if (result != ISC_R_NOMORE)
2740 * Do we need to use 6 to 4?
2742 if (need_alternate) {
2745 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2746 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2748 a = ISC_LIST_NEXT(a, link)) {
2749 if (!a->isaddress) {
2750 findname(fctx, &a->_u._n.name, a->_u._n.port,
2751 stdoptions, FCTX_ADDRINFO_FORWARDER,
2755 if (isc_sockaddr_pf(&a->_u.addr) != family)
2758 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2760 if (result == ISC_R_SUCCESS) {
2761 dns_adbaddrinfo_t *cur;
2762 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2763 cur = ISC_LIST_HEAD(fctx->altaddrs);
2764 while (cur != NULL && cur->srtt < ai->srtt)
2765 cur = ISC_LIST_NEXT(cur, publink);
2767 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2770 ISC_LIST_APPEND(fctx->altaddrs, ai,
2778 * Mark all known bad servers.
2780 all_bad = mark_bad(fctx);
2787 * We've got no addresses.
2789 if (fctx->pending > 0) {
2791 * We're fetching the addresses, but don't have any
2792 * yet. Tell the caller to wait for an answer.
2794 result = DNS_R_WAIT;
2799 * We've lost completely. We don't know any
2800 * addresses, and the ADB has told us it can't get
2803 FCTXTRACE("no addresses");
2804 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2805 result = isc_time_nowplusinterval(&expire, &i);
2807 (fctx->type == dns_rdatatype_dnskey ||
2808 fctx->type == dns_rdatatype_dlv ||
2809 fctx->type == dns_rdatatype_ds) &&
2810 result == ISC_R_SUCCESS)
2811 dns_resolver_addbadcache(fctx->res,
2813 fctx->type, &expire);
2814 result = ISC_R_FAILURE;
2818 * We've found some addresses. We might still be looking
2819 * for more addresses.
2821 sort_finds(&fctx->finds);
2822 sort_finds(&fctx->altfinds);
2823 result = ISC_R_SUCCESS;
2830 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2833 char buf[ISC_NETADDR_FORMATSIZE];
2835 isc_boolean_t aborted = ISC_FALSE;
2836 isc_boolean_t bogus;
2837 dns_acl_t *blackhole;
2838 isc_netaddr_t ipaddr;
2839 dns_peer_t *peer = NULL;
2840 dns_resolver_t *res;
2841 const char *msg = NULL;
2843 sa = &addr->sockaddr;
2846 isc_netaddr_fromsockaddr(&ipaddr, sa);
2847 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2848 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2850 if (blackhole != NULL) {
2853 if (dns_acl_match(&ipaddr, NULL, blackhole,
2855 &match, NULL) == ISC_R_SUCCESS &&
2861 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2866 addr->flags |= FCTX_ADDRINFO_MARK;
2867 msg = "ignoring blackholed / bogus server: ";
2868 } else if (isc_sockaddr_ismulticast(sa)) {
2869 addr->flags |= FCTX_ADDRINFO_MARK;
2870 msg = "ignoring multicast address: ";
2871 } else if (isc_sockaddr_isexperimental(sa)) {
2872 addr->flags |= FCTX_ADDRINFO_MARK;
2873 msg = "ignoring experimental address: ";
2874 } else if (sa->type.sa.sa_family != AF_INET6) {
2876 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2877 addr->flags |= FCTX_ADDRINFO_MARK;
2878 msg = "ignoring IPv6 mapped IPV4 address: ";
2879 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2880 addr->flags |= FCTX_ADDRINFO_MARK;
2881 msg = "ignoring IPv6 compatibility IPV4 address: ";
2885 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2888 isc_netaddr_fromsockaddr(&na, sa);
2889 isc_netaddr_format(&na, buf, sizeof(buf));
2890 FCTXTRACE2(msg, buf);
2893 static inline dns_adbaddrinfo_t *
2894 fctx_nextaddress(fetchctx_t *fctx) {
2895 dns_adbfind_t *find, *start;
2896 dns_adbaddrinfo_t *addrinfo;
2897 dns_adbaddrinfo_t *faddrinfo;
2900 * Return the next untried address, if any.
2904 * Find the first unmarked forwarder (if any).
2906 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2908 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2909 if (!UNMARKED(addrinfo))
2911 possibly_mark(fctx, addrinfo);
2912 if (UNMARKED(addrinfo)) {
2913 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2920 * No forwarders. Move to the next find.
2923 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2927 find = ISC_LIST_HEAD(fctx->finds);
2929 find = ISC_LIST_NEXT(find, publink);
2931 find = ISC_LIST_HEAD(fctx->finds);
2935 * Find the first unmarked addrinfo.
2941 for (addrinfo = ISC_LIST_HEAD(find->list);
2943 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2944 if (!UNMARKED(addrinfo))
2946 possibly_mark(fctx, addrinfo);
2947 if (UNMARKED(addrinfo)) {
2948 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2952 if (addrinfo != NULL)
2954 find = ISC_LIST_NEXT(find, publink);
2956 find = ISC_LIST_HEAD(fctx->finds);
2957 } while (find != start);
2961 if (addrinfo != NULL)
2965 * No nameservers left. Try alternates.
2968 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2970 find = fctx->altfind;
2972 find = ISC_LIST_HEAD(fctx->altfinds);
2974 find = ISC_LIST_NEXT(find, publink);
2976 find = ISC_LIST_HEAD(fctx->altfinds);
2980 * Find the first unmarked addrinfo.
2986 for (addrinfo = ISC_LIST_HEAD(find->list);
2988 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2989 if (!UNMARKED(addrinfo))
2991 possibly_mark(fctx, addrinfo);
2992 if (UNMARKED(addrinfo)) {
2993 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2997 if (addrinfo != NULL)
2999 find = ISC_LIST_NEXT(find, publink);
3001 find = ISC_LIST_HEAD(fctx->altfinds);
3002 } while (find != start);
3005 faddrinfo = addrinfo;
3008 * See if we have a better alternate server by address.
3011 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
3013 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3014 if (!UNMARKED(addrinfo))
3016 possibly_mark(fctx, addrinfo);
3017 if (UNMARKED(addrinfo) &&
3018 (faddrinfo == NULL ||
3019 addrinfo->srtt < faddrinfo->srtt)) {
3020 if (faddrinfo != NULL)
3021 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3022 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3027 if (addrinfo == NULL) {
3028 addrinfo = faddrinfo;
3029 fctx->altfind = find;
3036 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3037 isc_result_t result;
3038 dns_adbaddrinfo_t *addrinfo;
3042 REQUIRE(!ADDRWAIT(fctx));
3044 addrinfo = fctx_nextaddress(fctx);
3045 if (addrinfo == NULL) {
3047 * We have no more addresses. Start over.
3049 fctx_cancelqueries(fctx, ISC_TRUE);
3050 fctx_cleanupfinds(fctx);
3051 fctx_cleanupaltfinds(fctx);
3052 fctx_cleanupforwaddrs(fctx);
3053 fctx_cleanupaltaddrs(fctx);
3054 result = fctx_getaddresses(fctx, badcache);
3055 if (result == DNS_R_WAIT) {
3057 * Sleep waiting for addresses.
3059 FCTXTRACE("addrwait");
3060 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3062 } else if (result != ISC_R_SUCCESS) {
3064 * Something bad happened.
3066 fctx_done(fctx, result, __LINE__);
3070 addrinfo = fctx_nextaddress(fctx);
3072 * While we may have addresses from the ADB, they
3073 * might be bad ones. In this case, return SERVFAIL.
3075 if (addrinfo == NULL) {
3076 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3081 result = isc_counter_increment(fctx->qc);
3082 if (result != ISC_R_SUCCESS) {
3083 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3084 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3),
3085 "exceeded max queries resolving '%s'",
3087 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3091 result = fctx_query(fctx, addrinfo, fctx->options);
3092 if (result != ISC_R_SUCCESS)
3093 fctx_done(fctx, result, __LINE__);
3095 inc_stats(fctx->res, dns_resstatscounter_retry);
3098 static isc_boolean_t
3099 fctx_unlink(fetchctx_t *fctx) {
3100 dns_resolver_t *res;
3101 unsigned int bucketnum;
3104 * Caller must be holding the bucket lock.
3107 REQUIRE(VALID_FCTX(fctx));
3108 REQUIRE(fctx->state == fetchstate_done ||
3109 fctx->state == fetchstate_init);
3110 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3111 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3112 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3113 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3114 REQUIRE(fctx->pending == 0);
3115 REQUIRE(fctx->references == 0);
3116 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3118 FCTXTRACE("unlink");
3121 bucketnum = fctx->bucketnum;
3123 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3127 UNLOCK(&res->nlock);
3129 if (res->buckets[bucketnum].exiting &&
3130 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3137 fctx_destroy(fetchctx_t *fctx) {
3138 isc_sockaddr_t *sa, *next_sa;
3140 REQUIRE(VALID_FCTX(fctx));
3141 REQUIRE(fctx->state == fetchstate_done ||
3142 fctx->state == fetchstate_init);
3143 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3144 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3145 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3146 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3147 REQUIRE(fctx->pending == 0);
3148 REQUIRE(fctx->references == 0);
3149 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3150 REQUIRE(!ISC_LINK_LINKED(fctx, link));
3152 FCTXTRACE("destroy");
3157 for (sa = ISC_LIST_HEAD(fctx->bad);
3160 next_sa = ISC_LIST_NEXT(sa, link);
3161 ISC_LIST_UNLINK(fctx->bad, sa, link);
3162 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3165 for (sa = ISC_LIST_HEAD(fctx->edns);
3168 next_sa = ISC_LIST_NEXT(sa, link);
3169 ISC_LIST_UNLINK(fctx->edns, sa, link);
3170 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3173 for (sa = ISC_LIST_HEAD(fctx->edns512);
3176 next_sa = ISC_LIST_NEXT(sa, link);
3177 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3178 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3181 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3184 next_sa = ISC_LIST_NEXT(sa, link);
3185 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3186 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3189 isc_counter_detach(&fctx->qc);
3190 isc_timer_detach(&fctx->timer);
3191 dns_message_destroy(&fctx->rmessage);
3192 dns_message_destroy(&fctx->qmessage);
3193 if (dns_name_countlabels(&fctx->domain) > 0)
3194 dns_name_free(&fctx->domain, fctx->mctx);
3195 if (dns_rdataset_isassociated(&fctx->nameservers))
3196 dns_rdataset_disassociate(&fctx->nameservers);
3197 dns_name_free(&fctx->name, fctx->mctx);
3198 dns_db_detach(&fctx->cache);
3199 dns_adb_detach(&fctx->adb);
3200 isc_mem_free(fctx->mctx, fctx->info);
3201 isc_mem_putanddetach(&fctx->mctx, fctx, sizeof(*fctx));
3205 * Fetch event handlers.
3209 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3210 fetchctx_t *fctx = event->ev_arg;
3211 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3214 REQUIRE(VALID_FCTX(fctx));
3218 FCTXTRACE("timeout");
3220 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3222 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3223 fctx->reason = NULL;
3224 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3226 isc_result_t result;
3229 fctx->timeout = ISC_TRUE;
3231 * We could cancel the running queries here, or we could let
3232 * them keep going. Since we normally use separate sockets for
3233 * different queries, we adopt the former approach to reduce
3234 * the number of open sockets: cancel the oldest query if it
3235 * expired after the query had started (this is usually the
3236 * case but is not always so, depending on the task schedule
3239 query = ISC_LIST_HEAD(fctx->queries);
3240 if (query != NULL &&
3241 isc_time_compare(&tevent->due, &query->start) >= 0) {
3242 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3244 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3246 * Our timer has triggered. Reestablish the fctx lifetime
3249 result = fctx_starttimer(fctx);
3250 if (result != ISC_R_SUCCESS)
3251 fctx_done(fctx, result, __LINE__);
3256 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3259 isc_event_free(&event);
3263 fctx_shutdown(fetchctx_t *fctx) {
3264 isc_event_t *cevent;
3267 * Start the shutdown process for fctx, if it isn't already underway.
3270 FCTXTRACE("shutdown");
3273 * The caller must be holding the appropriate bucket lock.
3276 if (fctx->want_shutdown)
3279 fctx->want_shutdown = ISC_TRUE;
3282 * Unless we're still initializing (in which case the
3283 * control event is still outstanding), we need to post
3284 * the control event to tell the fetch we want it to
3287 if (fctx->state != fetchstate_init) {
3288 cevent = &fctx->control_event;
3289 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3295 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3296 fetchctx_t *fctx = event->ev_arg;
3297 isc_boolean_t bucket_empty = ISC_FALSE;
3298 dns_resolver_t *res;
3299 unsigned int bucketnum;
3300 dns_validator_t *validator;
3301 isc_boolean_t destroy = ISC_FALSE;
3303 REQUIRE(VALID_FCTX(fctx));
3308 bucketnum = fctx->bucketnum;
3310 FCTXTRACE("doshutdown");
3313 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3315 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3318 * Cancel all pending validators. Note that this must be done
3319 * without the bucket lock held, since that could cause deadlock.
3321 validator = ISC_LIST_HEAD(fctx->validators);
3322 while (validator != NULL) {
3323 dns_validator_cancel(validator);
3324 validator = ISC_LIST_NEXT(validator, link);
3327 if (fctx->nsfetch != NULL)
3328 dns_resolver_cancelfetch(fctx->nsfetch);
3331 * Shut down anything that is still running on behalf of this
3332 * fetch. To avoid deadlock with the ADB, we must do this
3333 * before we lock the bucket lock.
3335 fctx_stopeverything(fctx, ISC_FALSE);
3337 LOCK(&res->buckets[bucketnum].lock);
3339 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3341 INSIST(fctx->state == fetchstate_active ||
3342 fctx->state == fetchstate_done);
3343 INSIST(fctx->want_shutdown);
3345 if (fctx->state != fetchstate_done) {
3346 fctx->state = fetchstate_done;
3347 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3350 if (fctx->references == 0 && fctx->pending == 0 &&
3351 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3352 bucket_empty = fctx_unlink(fctx);
3356 UNLOCK(&res->buckets[bucketnum].lock);
3366 fctx_start(isc_task_t *task, isc_event_t *event) {
3367 fetchctx_t *fctx = event->ev_arg;
3368 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3369 dns_resolver_t *res;
3370 unsigned int bucketnum;
3371 isc_boolean_t destroy = ISC_FALSE;
3373 REQUIRE(VALID_FCTX(fctx));
3378 bucketnum = fctx->bucketnum;
3382 LOCK(&res->buckets[bucketnum].lock);
3384 INSIST(fctx->state == fetchstate_init);
3385 if (fctx->want_shutdown) {
3387 * We haven't started this fctx yet, and we've been requested
3390 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3391 fctx->state = fetchstate_done;
3392 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3394 * Since we haven't started, we INSIST that we have no
3395 * pending ADB finds and no pending validations.
3397 INSIST(fctx->pending == 0);
3398 INSIST(fctx->nqueries == 0);
3399 INSIST(ISC_LIST_EMPTY(fctx->validators));
3400 if (fctx->references == 0) {
3402 * It's now safe to destroy this fctx.
3404 bucket_empty = fctx_unlink(fctx);
3410 * Normal fctx startup.
3412 fctx->state = fetchstate_active;
3414 * Reset the control event for later use in shutting down
3417 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3418 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3422 UNLOCK(&res->buckets[bucketnum].lock);
3425 isc_result_t result;
3430 * All is well. Start working on the fetch.
3432 result = fctx_starttimer(fctx);
3433 if (result != ISC_R_SUCCESS)
3434 fctx_done(fctx, result, __LINE__);
3436 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3437 } else if (destroy) {
3445 * Fetch Creation, Joining, and Cancelation.
3448 static inline isc_result_t
3449 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3450 dns_messageid_t id, isc_taskaction_t action, void *arg,
3451 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3455 dns_fetchevent_t *event;
3460 * We store the task we're going to send this event to in the
3461 * sender field. We'll make the fetch the sender when we actually
3465 isc_task_attach(task, &clone);
3466 event = (dns_fetchevent_t *)
3467 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3468 action, arg, sizeof(*event));
3469 if (event == NULL) {
3470 isc_task_detach(&clone);
3471 return (ISC_R_NOMEMORY);
3473 event->result = DNS_R_SERVFAIL;
3474 event->qtype = fctx->type;
3477 event->rdataset = rdataset;
3478 event->sigrdataset = sigrdataset;
3479 event->fetch = fetch;
3480 event->client = client;
3482 dns_fixedname_init(&event->foundname);
3485 * Make sure that we can store the sigrdataset in the
3486 * first event if it is needed by any of the events.
3488 if (event->sigrdataset != NULL)
3489 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3491 ISC_LIST_APPEND(fctx->events, event, ev_link);
3493 fctx->client = client;
3495 fetch->magic = DNS_FETCH_MAGIC;
3496 fetch->private = fctx;
3498 return (ISC_R_SUCCESS);
3502 log_ns_ttl(fetchctx_t *fctx, const char *where) {
3503 char namebuf[DNS_NAME_FORMATSIZE];
3504 char domainbuf[DNS_NAME_FORMATSIZE];
3506 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3507 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3508 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3509 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
3510 "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u",
3511 fctx, where, namebuf, domainbuf,
3512 fctx->ns_ttl_ok, fctx->ns_ttl);
3516 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3517 dns_name_t *domain, dns_rdataset_t *nameservers,
3518 unsigned int options, unsigned int bucketnum, unsigned int depth,
3519 isc_counter_t *qc, fetchctx_t **fctxp)
3522 isc_result_t result;
3523 isc_result_t iresult;
3524 isc_interval_t interval;
3525 dns_fixedname_t fixed;
3526 unsigned int findoptions = 0;
3527 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3528 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3533 * Caller must be holding the lock for bucket number 'bucketnum'.
3535 REQUIRE(fctxp != NULL && *fctxp == NULL);
3537 mctx = res->buckets[bucketnum].mctx;
3538 fctx = isc_mem_get(mctx, sizeof(*fctx));
3540 return (ISC_R_NOMEMORY);
3544 isc_counter_attach(qc, &fctx->qc);
3546 result = isc_counter_create(res->mctx,
3547 res->maxqueries, &fctx->qc);
3548 if (result != ISC_R_SUCCESS)
3553 * Make fctx->info point to a copy of a formatted string
3556 dns_name_format(name, buf, sizeof(buf));
3557 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3558 strcat(buf, "/"); /* checked */
3559 strcat(buf, typebuf); /* checked */
3560 fctx->info = isc_mem_strdup(mctx, buf);
3561 if (fctx->info == NULL) {
3562 result = ISC_R_NOMEMORY;
3563 goto cleanup_counter;
3565 FCTXTRACE("create");
3566 dns_name_init(&fctx->name, NULL);
3567 result = dns_name_dup(name, mctx, &fctx->name);
3568 if (result != ISC_R_SUCCESS)
3570 dns_name_init(&fctx->domain, NULL);
3571 dns_rdataset_init(&fctx->nameservers);
3574 fctx->options = options;
3576 * Note! We do not attach to the task. We are relying on the
3577 * resolver to ensure that this task doesn't go away while we are
3581 fctx->references = 0;
3582 fctx->bucketnum = bucketnum;
3583 fctx->state = fetchstate_init;
3584 fctx->want_shutdown = ISC_FALSE;
3585 fctx->cloned = ISC_FALSE;
3586 fctx->depth = depth;
3587 ISC_LIST_INIT(fctx->queries);
3588 ISC_LIST_INIT(fctx->finds);
3589 ISC_LIST_INIT(fctx->altfinds);
3590 ISC_LIST_INIT(fctx->forwaddrs);
3591 ISC_LIST_INIT(fctx->altaddrs);
3592 ISC_LIST_INIT(fctx->forwarders);
3593 fctx->fwdpolicy = dns_fwdpolicy_none;
3594 ISC_LIST_INIT(fctx->bad);
3595 ISC_LIST_INIT(fctx->edns);
3596 ISC_LIST_INIT(fctx->edns512);
3597 ISC_LIST_INIT(fctx->bad_edns);
3598 ISC_LIST_INIT(fctx->validators);
3599 fctx->validator = NULL;
3601 fctx->altfind = NULL;
3604 fctx->querysent = 0;
3605 fctx->referrals = 0;
3606 TIME_NOW(&fctx->start);
3608 fctx->lamecount = 0;
3614 fctx->result = ISC_R_FAILURE;
3615 fctx->vresult = ISC_R_SUCCESS;
3616 fctx->exitline = -1; /* sentinel */
3617 fctx->logged = ISC_FALSE;
3618 fctx->attributes = 0;
3619 fctx->spilled = ISC_FALSE;
3621 fctx->reason = NULL;
3623 fctx->rand_bits = 0;
3624 fctx->timeout = ISC_FALSE;
3625 fctx->addrinfo = NULL;
3626 fctx->client = NULL;
3628 fctx->ns_ttl_ok = ISC_FALSE;
3630 dns_name_init(&fctx->nsname, NULL);
3631 fctx->nsfetch = NULL;
3632 dns_rdataset_init(&fctx->nsrrset);
3634 if (domain == NULL) {
3635 dns_forwarders_t *forwarders = NULL;
3636 unsigned int labels;
3637 dns_name_t *fwdname = name;
3640 * DS records are found in the parent server.
3641 * Strip label to get the correct forwarder (if any).
3643 if (dns_rdatatype_atparent(fctx->type) &&
3644 dns_name_countlabels(name) > 1) {
3645 dns_name_init(&suffix, NULL);
3646 labels = dns_name_countlabels(name);
3647 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3650 dns_fixedname_init(&fixed);
3651 domain = dns_fixedname_name(&fixed);
3652 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3653 domain, &forwarders);
3654 if (result == ISC_R_SUCCESS)
3655 fctx->fwdpolicy = forwarders->fwdpolicy;
3657 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3659 * The caller didn't supply a query domain and
3660 * nameservers, and we're not in forward-only mode,
3661 * so find the best nameservers to use.
3663 if (dns_rdatatype_atparent(fctx->type))
3664 findoptions |= DNS_DBFIND_NOEXACT;
3665 result = dns_view_findzonecut(res->view, fwdname,
3666 domain, 0, findoptions,
3670 if (result != ISC_R_SUCCESS)
3673 result = dns_name_dup(domain, mctx, &fctx->domain);
3674 if (result != ISC_R_SUCCESS) {
3675 dns_rdataset_disassociate(&fctx->nameservers);
3678 fctx->ns_ttl = fctx->nameservers.ttl;
3679 fctx->ns_ttl_ok = ISC_TRUE;
3682 * We're in forward-only mode. Set the query domain.
3684 result = dns_name_dup(domain, mctx, &fctx->domain);
3685 if (result != ISC_R_SUCCESS)
3689 result = dns_name_dup(domain, mctx, &fctx->domain);
3690 if (result != ISC_R_SUCCESS)
3692 dns_rdataset_clone(nameservers, &fctx->nameservers);
3693 fctx->ns_ttl = fctx->nameservers.ttl;
3694 fctx->ns_ttl_ok = ISC_TRUE;
3697 log_ns_ttl(fctx, "fctx_create");
3699 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3701 fctx->qmessage = NULL;
3702 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
3705 if (result != ISC_R_SUCCESS)
3706 goto cleanup_domain;
3708 fctx->rmessage = NULL;
3709 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
3712 if (result != ISC_R_SUCCESS)
3713 goto cleanup_qmessage;
3716 * Compute an expiration time for the entire fetch.
3718 isc_interval_set(&interval, res->query_timeout, 0);
3719 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3720 if (iresult != ISC_R_SUCCESS) {
3721 UNEXPECTED_ERROR(__FILE__, __LINE__,
3722 "isc_time_nowplusinterval: %s",
3723 isc_result_totext(iresult));
3724 result = ISC_R_UNEXPECTED;
3725 goto cleanup_rmessage;
3729 * Default retry interval initialization. We set the interval now
3730 * mostly so it won't be uninitialized. It will be set to the
3731 * correct value before a query is issued.
3733 isc_interval_set(&fctx->interval, 2, 0);
3736 * Create an inactive timer. It will be made active when the fetch
3737 * is actually started.
3740 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3742 res->buckets[bucketnum].task, fctx_timeout,
3743 fctx, &fctx->timer);
3744 if (iresult != ISC_R_SUCCESS) {
3745 UNEXPECTED_ERROR(__FILE__, __LINE__,
3746 "isc_timer_create: %s",
3747 isc_result_totext(iresult));
3748 result = ISC_R_UNEXPECTED;
3749 goto cleanup_rmessage;
3753 * Attach to the view's cache and adb.
3756 dns_db_attach(res->view->cachedb, &fctx->cache);
3758 dns_adb_attach(res->view->adb, &fctx->adb);
3760 isc_mem_attach(mctx, &fctx->mctx);
3762 ISC_LIST_INIT(fctx->events);
3763 ISC_LINK_INIT(fctx, link);
3764 fctx->magic = FCTX_MAGIC;
3766 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3770 UNLOCK(&res->nlock);
3774 return (ISC_R_SUCCESS);
3777 dns_message_destroy(&fctx->rmessage);
3780 dns_message_destroy(&fctx->qmessage);
3783 if (dns_name_countlabels(&fctx->domain) > 0)
3784 dns_name_free(&fctx->domain, mctx);
3785 if (dns_rdataset_isassociated(&fctx->nameservers))
3786 dns_rdataset_disassociate(&fctx->nameservers);
3789 dns_name_free(&fctx->name, mctx);
3792 isc_mem_free(mctx, fctx->info);
3795 isc_counter_detach(&fctx->qc);
3798 isc_mem_put(mctx, fctx, sizeof(*fctx));
3806 static inline isc_boolean_t
3807 is_lame(fetchctx_t *fctx) {
3808 dns_message_t *message = fctx->rmessage;
3810 dns_rdataset_t *rdataset;
3811 isc_result_t result;
3813 if (message->rcode != dns_rcode_noerror &&
3814 message->rcode != dns_rcode_nxdomain)
3817 if (message->counts[DNS_SECTION_ANSWER] != 0)
3820 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3823 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3824 while (result == ISC_R_SUCCESS) {
3826 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3827 for (rdataset = ISC_LIST_HEAD(name->list);
3829 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3830 dns_namereln_t namereln;
3832 unsigned int labels;
3833 if (rdataset->type != dns_rdatatype_ns)
3835 namereln = dns_name_fullcompare(name, &fctx->domain,
3837 if (namereln == dns_namereln_equal &&
3838 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3840 if (namereln == dns_namereln_subdomain)
3844 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3851 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3852 char namebuf[DNS_NAME_FORMATSIZE];
3853 char domainbuf[DNS_NAME_FORMATSIZE];
3854 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3856 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3857 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3858 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3859 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3860 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3861 "lame server resolving '%s' (in '%s'?): %s",
3862 namebuf, domainbuf, addrbuf);
3866 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3867 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3868 char clbuf[ISC_SOCKADDR_FORMATSIZE];
3869 const char *clmsg = "";
3873 va_start(args, format);
3874 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3877 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3879 if (fctx->client != NULL) {
3880 clmsg = " for client ";
3881 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3886 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3887 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3888 "DNS format error from %s resolving %s%s%s: %s",
3889 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3892 static inline isc_result_t
3893 same_question(fetchctx_t *fctx) {
3894 isc_result_t result;
3895 dns_message_t *message = fctx->rmessage;
3897 dns_rdataset_t *rdataset;
3900 * Caller must be holding the fctx lock.
3904 * XXXRTH Currently we support only one question.
3906 if (message->counts[DNS_SECTION_QUESTION] != 1) {
3907 log_formerr(fctx, "too many questions");
3908 return (DNS_R_FORMERR);
3911 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3912 if (result != ISC_R_SUCCESS)
3915 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3916 rdataset = ISC_LIST_HEAD(name->list);
3917 INSIST(rdataset != NULL);
3918 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3920 if (fctx->type != rdataset->type ||
3921 fctx->res->rdclass != rdataset->rdclass ||
3922 !dns_name_equal(&fctx->name, name)) {
3923 char namebuf[DNS_NAME_FORMATSIZE];
3924 char class[DNS_RDATACLASS_FORMATSIZE];
3925 char type[DNS_RDATATYPE_FORMATSIZE];
3927 dns_name_format(name, namebuf, sizeof(namebuf));
3928 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3929 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3930 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3931 namebuf, class, type);
3932 return (DNS_R_FORMERR);
3935 return (ISC_R_SUCCESS);
3939 clone_results(fetchctx_t *fctx) {
3940 dns_fetchevent_t *event, *hevent;
3941 isc_result_t result;
3942 dns_name_t *name, *hname;
3944 FCTXTRACE("clone_results");
3947 * Set up any other events to have the same data as the first
3950 * Caller must be holding the appropriate lock.
3953 fctx->cloned = ISC_TRUE;
3954 hevent = ISC_LIST_HEAD(fctx->events);
3957 hname = dns_fixedname_name(&hevent->foundname);
3958 for (event = ISC_LIST_NEXT(hevent, ev_link);
3960 event = ISC_LIST_NEXT(event, ev_link)) {
3961 name = dns_fixedname_name(&event->foundname);
3962 result = dns_name_copy(hname, name, NULL);
3963 if (result != ISC_R_SUCCESS)
3964 event->result = result;
3966 event->result = hevent->result;
3967 dns_db_attach(hevent->db, &event->db);
3968 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3969 INSIST(hevent->rdataset != NULL);
3970 INSIST(event->rdataset != NULL);
3971 if (dns_rdataset_isassociated(hevent->rdataset))
3972 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3973 INSIST(! (hevent->sigrdataset == NULL &&
3974 event->sigrdataset != NULL));
3975 if (hevent->sigrdataset != NULL &&
3976 dns_rdataset_isassociated(hevent->sigrdataset) &&
3977 event->sigrdataset != NULL)
3978 dns_rdataset_clone(hevent->sigrdataset,
3979 event->sigrdataset);
3983 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3984 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3985 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3986 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3987 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3988 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3989 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3993 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3994 * no references and is no longer waiting for any events).
3997 * '*fctx' is shutting down.
4000 * true if the resolver is exiting and this is the last fctx in the bucket.
4002 static isc_boolean_t
4003 maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) {
4004 unsigned int bucketnum;
4005 isc_boolean_t bucket_empty = ISC_FALSE;
4006 dns_resolver_t *res = fctx->res;
4007 dns_validator_t *validator, *next_validator;
4008 isc_boolean_t destroy = ISC_FALSE;
4010 REQUIRE(SHUTTINGDOWN(fctx));
4012 bucketnum = fctx->bucketnum;
4014 LOCK(&res->buckets[bucketnum].lock);
4015 if (fctx->pending != 0 || fctx->nqueries != 0)
4018 for (validator = ISC_LIST_HEAD(fctx->validators);
4019 validator != NULL; validator = next_validator) {
4020 next_validator = ISC_LIST_NEXT(validator, link);
4021 dns_validator_cancel(validator);
4024 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators)) {
4025 bucket_empty = fctx_unlink(fctx);
4030 UNLOCK(&res->buckets[bucketnum].lock);
4033 return (bucket_empty);
4037 * The validator has finished.
4040 validated(isc_task_t *task, isc_event_t *event) {
4041 dns_adbaddrinfo_t *addrinfo;
4042 dns_dbnode_t *node = NULL;
4043 dns_dbnode_t *nsnode = NULL;
4044 dns_fetchevent_t *hevent;
4046 dns_rdataset_t *ardataset = NULL;
4047 dns_rdataset_t *asigrdataset = NULL;
4048 dns_rdataset_t *rdataset;
4049 dns_rdataset_t *sigrdataset;
4050 dns_resolver_t *res;
4051 dns_valarg_t *valarg;
4052 dns_validatorevent_t *vevent;
4054 isc_boolean_t chaining;
4055 isc_boolean_t negative;
4056 isc_boolean_t sentresponse;
4057 isc_result_t eresult = ISC_R_SUCCESS;
4058 isc_result_t result = ISC_R_SUCCESS;
4062 UNUSED(task); /* for now */
4064 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
4065 valarg = event->ev_arg;
4066 fctx = valarg->fctx;
4068 addrinfo = valarg->addrinfo;
4069 REQUIRE(VALID_FCTX(fctx));
4070 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
4072 vevent = (dns_validatorevent_t *)event;
4073 fctx->vresult = vevent->result;
4075 FCTXTRACE("received validation completion event");
4077 LOCK(&res->buckets[fctx->bucketnum].lock);
4079 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
4080 fctx->validator = NULL;
4083 * Destroy the validator early so that we can
4084 * destroy the fctx if necessary.
4086 dns_validator_destroy(&vevent->validator);
4087 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
4089 negative = ISC_TF(vevent->rdataset == NULL);
4091 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
4094 * If shutting down, ignore the results. Check to see if we're
4095 * done waiting for validator completions and ADB pending events; if
4096 * so, destroy the fctx.
4098 if (SHUTTINGDOWN(fctx) && !sentresponse) {
4099 isc_uint32_t bucketnum = fctx->bucketnum;
4100 isc_boolean_t bucket_empty;
4101 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4102 UNLOCK(&res->buckets[bucketnum].lock);
4108 isc_stdtime_get(&now);
4111 * If chaining, we need to make sure that the right result code is
4112 * returned, and that the rdatasets are bound.
4114 if (vevent->result == ISC_R_SUCCESS &&
4116 vevent->rdataset != NULL &&
4117 CHAINING(vevent->rdataset))
4119 if (vevent->rdataset->type == dns_rdatatype_cname)
4120 eresult = DNS_R_CNAME;
4122 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4123 eresult = DNS_R_DNAME;
4125 chaining = ISC_TRUE;
4127 chaining = ISC_FALSE;
4130 * Either we're not shutting down, or we are shutting down but want
4131 * to cache the result anyway (if this was a validation started by
4132 * a query with cd set)
4135 hevent = ISC_LIST_HEAD(fctx->events);
4136 if (hevent != NULL) {
4137 if (!negative && !chaining &&
4138 (fctx->type == dns_rdatatype_any ||
4139 fctx->type == dns_rdatatype_rrsig ||
4140 fctx->type == dns_rdatatype_sig)) {
4142 * Don't bind rdatasets; the caller
4143 * will iterate the node.
4146 ardataset = hevent->rdataset;
4147 asigrdataset = hevent->sigrdataset;
4151 if (vevent->result != ISC_R_SUCCESS) {
4152 FCTXTRACE("validation failed");
4153 inc_stats(res, dns_resstatscounter_valfail);
4155 fctx->vresult = vevent->result;
4156 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4157 result = ISC_R_NOTFOUND;
4158 if (vevent->rdataset != NULL)
4159 result = dns_db_findnode(fctx->cache,
4162 if (result == ISC_R_SUCCESS)
4163 (void)dns_db_deleterdataset(fctx->cache, node,
4166 if (result == ISC_R_SUCCESS &&
4167 vevent->sigrdataset != NULL)
4168 (void)dns_db_deleterdataset(fctx->cache, node,
4170 dns_rdatatype_rrsig,
4172 if (result == ISC_R_SUCCESS)
4173 dns_db_detachnode(fctx->cache, &node);
4175 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4177 * Cache the data as pending for later validation.
4179 result = ISC_R_NOTFOUND;
4180 if (vevent->rdataset != NULL)
4181 result = dns_db_findnode(fctx->cache,
4184 if (result == ISC_R_SUCCESS) {
4185 (void)dns_db_addrdataset(fctx->cache, node,
4187 vevent->rdataset, 0,
4190 if (result == ISC_R_SUCCESS &&
4191 vevent->sigrdataset != NULL)
4192 (void)dns_db_addrdataset(fctx->cache, node,
4194 vevent->sigrdataset,
4196 if (result == ISC_R_SUCCESS)
4197 dns_db_detachnode(fctx->cache, &node);
4199 result = fctx->vresult;
4200 add_bad(fctx, addrinfo, result, badns_validation);
4201 isc_event_free(&event);
4202 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4203 INSIST(fctx->validator == NULL);
4204 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4205 if (fctx->validator != NULL)
4206 dns_validator_send(fctx->validator);
4207 else if (sentresponse)
4208 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4209 else if (result == DNS_R_BROKENCHAIN) {
4210 isc_result_t tresult;
4214 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4215 tresult = isc_time_nowplusinterval(&expire, &i);
4217 (fctx->type == dns_rdatatype_dnskey ||
4218 fctx->type == dns_rdatatype_dlv ||
4219 fctx->type == dns_rdatatype_ds) &&
4220 tresult == ISC_R_SUCCESS)
4221 dns_resolver_addbadcache(res, &fctx->name,
4222 fctx->type, &expire);
4223 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4225 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4231 dns_rdatatype_t covers;
4232 FCTXTRACE("nonexistence validation OK");
4234 inc_stats(res, dns_resstatscounter_valnegsuccess);
4236 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4237 covers = dns_rdatatype_any;
4239 covers = fctx->type;
4241 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4243 if (result != ISC_R_SUCCESS)
4244 goto noanswer_response;
4247 * If we are asking for a SOA record set the cache time
4248 * to zero to facilitate locating the containing zone of
4251 ttl = res->view->maxncachettl;
4252 if (fctx->type == dns_rdatatype_soa &&
4253 covers == dns_rdatatype_any && res->zero_no_soa_ttl)
4256 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4257 covers, now, ttl, vevent->optout,
4258 vevent->secure, ardataset, &eresult);
4259 if (result != ISC_R_SUCCESS)
4260 goto noanswer_response;
4261 goto answer_response;
4263 inc_stats(res, dns_resstatscounter_valsuccess);
4265 FCTXTRACE("validation OK");
4267 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4268 result = dns_rdataset_addnoqname(vevent->rdataset,
4269 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4270 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4271 INSIST(vevent->sigrdataset != NULL);
4272 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4273 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4274 result = dns_rdataset_addclosest(vevent->rdataset,
4275 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4276 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4278 } else if (vevent->rdataset->trust == dns_trust_answer &&
4279 vevent->rdataset->type != dns_rdatatype_rrsig)
4281 isc_result_t tresult;
4282 dns_name_t *noqname = NULL;
4283 tresult = findnoqname(fctx, vevent->name,
4284 vevent->rdataset->type, &noqname);
4285 if (tresult == ISC_R_SUCCESS && noqname != NULL) {
4286 tresult = dns_rdataset_addnoqname(vevent->rdataset,
4288 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4293 * The data was already cached as pending data.
4294 * Re-cache it as secure and bind the cached
4295 * rdatasets to the first event on the fetch
4298 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4299 if (result != ISC_R_SUCCESS)
4300 goto noanswer_response;
4302 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4303 vevent->rdataset, 0, ardataset);
4304 if (result != ISC_R_SUCCESS &&
4305 result != DNS_R_UNCHANGED)
4306 goto noanswer_response;
4307 if (ardataset != NULL && NEGATIVE(ardataset)) {
4308 if (NXDOMAIN(ardataset))
4309 eresult = DNS_R_NCACHENXDOMAIN;
4311 eresult = DNS_R_NCACHENXRRSET;
4312 } else if (vevent->sigrdataset != NULL) {
4313 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4314 vevent->sigrdataset, 0,
4316 if (result != ISC_R_SUCCESS &&
4317 result != DNS_R_UNCHANGED)
4318 goto noanswer_response;
4322 isc_boolean_t bucket_empty = ISC_FALSE;
4324 * If we only deferred the destroy because we wanted to cache
4325 * the data, destroy now.
4327 dns_db_detachnode(fctx->cache, &node);
4328 if (SHUTTINGDOWN(fctx))
4329 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4330 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4336 if (!ISC_LIST_EMPTY(fctx->validators)) {
4338 INSIST(fctx->type == dns_rdatatype_any ||
4339 fctx->type == dns_rdatatype_rrsig ||
4340 fctx->type == dns_rdatatype_sig);
4342 * Don't send a response yet - we have
4343 * more rdatasets that still need to
4346 dns_db_detachnode(fctx->cache, &node);
4347 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4348 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4354 * Cache any NS/NSEC records that happened to be validated.
4356 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4357 while (result == ISC_R_SUCCESS) {
4359 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4361 for (rdataset = ISC_LIST_HEAD(name->list);
4363 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4364 if ((rdataset->type != dns_rdatatype_ns &&
4365 rdataset->type != dns_rdatatype_nsec) ||
4366 rdataset->trust != dns_trust_secure)
4368 for (sigrdataset = ISC_LIST_HEAD(name->list);
4369 sigrdataset != NULL;
4370 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4371 if (sigrdataset->type != dns_rdatatype_rrsig ||
4372 sigrdataset->covers != rdataset->type)
4376 if (sigrdataset == NULL ||
4377 sigrdataset->trust != dns_trust_secure)
4379 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4381 if (result != ISC_R_SUCCESS)
4384 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4385 now, rdataset, 0, NULL);
4386 if (result == ISC_R_SUCCESS)
4387 result = dns_db_addrdataset(fctx->cache, nsnode,
4391 dns_db_detachnode(fctx->cache, &nsnode);
4392 if (result != ISC_R_SUCCESS)
4395 result = dns_message_nextname(fctx->rmessage,
4396 DNS_SECTION_AUTHORITY);
4399 result = ISC_R_SUCCESS;
4402 * Respond with an answer, positive or negative,
4403 * as opposed to an error. 'node' must be non-NULL.
4406 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4408 if (hevent != NULL) {
4410 * Negative results must be indicated in event->result.
4412 if (dns_rdataset_isassociated(hevent->rdataset) &&
4413 NEGATIVE(hevent->rdataset)) {
4414 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4415 eresult == DNS_R_NCACHENXRRSET);
4417 hevent->result = eresult;
4418 RUNTIME_CHECK(dns_name_copy(vevent->name,
4419 dns_fixedname_name(&hevent->foundname), NULL)
4421 dns_db_attach(fctx->cache, &hevent->db);
4422 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4423 clone_results(fctx);
4428 dns_db_detachnode(fctx->cache, &node);
4430 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4431 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4434 INSIST(node == NULL);
4435 isc_event_free(&event);
4439 fctx_log(void *arg, int level, const char *fmt, ...) {
4442 fetchctx_t *fctx = arg;
4444 va_start(args, fmt);
4445 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
4448 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4449 DNS_LOGMODULE_RESOLVER, level,
4450 "fctx %p(%s): %s", fctx, fctx->info, msgbuf);
4453 static inline isc_result_t
4454 findnoqname(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
4455 dns_name_t **noqnamep)
4457 dns_rdataset_t *nrdataset, *next, *sigrdataset;
4458 dns_rdata_rrsig_t rrsig;
4459 isc_result_t result;
4460 unsigned int labels;
4461 dns_section_t section;
4462 dns_name_t *zonename;
4463 dns_fixedname_t fzonename;
4464 dns_name_t *closest;
4465 dns_fixedname_t fclosest;
4466 dns_name_t *nearest;
4467 dns_fixedname_t fnearest;
4468 dns_rdatatype_t found = dns_rdatatype_none;
4469 dns_name_t *noqname = NULL;
4471 FCTXTRACE("findnoqname");
4473 REQUIRE(noqnamep != NULL && *noqnamep == NULL);
4476 * Find the SIG for this rdataset, if we have it.
4478 for (sigrdataset = ISC_LIST_HEAD(name->list);
4479 sigrdataset != NULL;
4480 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4481 if (sigrdataset->type == dns_rdatatype_rrsig &&
4482 sigrdataset->covers == type)
4486 if (sigrdataset == NULL)
4487 return (ISC_R_NOTFOUND);
4489 labels = dns_name_countlabels(name);
4491 for (result = dns_rdataset_first(sigrdataset);
4492 result == ISC_R_SUCCESS;
4493 result = dns_rdataset_next(sigrdataset)) {
4494 dns_rdata_t rdata = DNS_RDATA_INIT;
4495 dns_rdataset_current(sigrdataset, &rdata);
4496 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
4497 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4498 /* Wildcard has rrsig.labels < labels - 1. */
4499 if (rrsig.labels + 1U >= labels)
4504 if (result == ISC_R_NOMORE)
4505 return (ISC_R_NOTFOUND);
4506 if (result != ISC_R_SUCCESS)
4509 dns_fixedname_init(&fzonename);
4510 zonename = dns_fixedname_name(&fzonename);
4511 dns_fixedname_init(&fclosest);
4512 closest = dns_fixedname_name(&fclosest);
4513 dns_fixedname_init(&fnearest);
4514 nearest = dns_fixedname_name(&fnearest);
4516 #define NXND(x) ((x) == ISC_R_SUCCESS)
4518 section = DNS_SECTION_AUTHORITY;
4519 for (result = dns_message_firstname(fctx->rmessage, section);
4520 result == ISC_R_SUCCESS;
4521 result = dns_message_nextname(fctx->rmessage, section)) {
4522 dns_name_t *nsec = NULL;
4523 dns_message_currentname(fctx->rmessage, section, &nsec);
4524 for (nrdataset = ISC_LIST_HEAD(nsec->list);
4525 nrdataset != NULL; nrdataset = next) {
4526 isc_boolean_t data = ISC_FALSE, exists = ISC_FALSE;
4527 isc_boolean_t optout = ISC_FALSE, unknown = ISC_FALSE;
4528 isc_boolean_t setclosest = ISC_FALSE;
4529 isc_boolean_t setnearest = ISC_FALSE;
4531 next = ISC_LIST_NEXT(nrdataset, link);
4532 if (nrdataset->type != dns_rdatatype_nsec &&
4533 nrdataset->type != dns_rdatatype_nsec3)
4536 if (nrdataset->type == dns_rdatatype_nsec &&
4537 NXND(dns_nsec_noexistnodata(type, name, nsec,
4539 &data, NULL, fctx_log,
4544 found = dns_rdatatype_nsec;
4548 if (nrdataset->type == dns_rdatatype_nsec3 &&
4549 NXND(dns_nsec3_noexistnodata(type, name, nsec,
4550 nrdataset, zonename,
4558 if (!exists && setnearest) {
4560 found = dns_rdatatype_nsec3;
4565 if (result == ISC_R_NOMORE)
4566 result = ISC_R_SUCCESS;
4567 if (noqname != NULL) {
4568 for (sigrdataset = ISC_LIST_HEAD(noqname->list);
4569 sigrdataset != NULL;
4570 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4571 if (sigrdataset->type == dns_rdatatype_rrsig &&
4572 sigrdataset->covers == found)
4575 if (sigrdataset != NULL)
4576 *noqnamep = noqname;
4581 static inline isc_result_t
4582 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4585 dns_rdataset_t *rdataset, *sigrdataset;
4586 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4587 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4588 dns_dbnode_t *node, **anodep;
4591 dns_resolver_t *res;
4592 isc_boolean_t need_validation, secure_domain, have_answer;
4593 isc_result_t result, eresult;
4594 dns_fetchevent_t *event;
4595 unsigned int options;
4598 unsigned int valoptions = 0;
4601 * The appropriate bucket lock must be held.
4605 need_validation = ISC_FALSE;
4606 POST(need_validation);
4607 secure_domain = ISC_FALSE;
4608 have_answer = ISC_FALSE;
4609 eresult = ISC_R_SUCCESS;
4610 task = res->buckets[fctx->bucketnum].task;
4613 * Is DNSSEC validation required for this name?
4615 if (res->view->enablevalidation) {
4616 result = dns_view_issecuredomain(res->view, name,
4618 if (result != ISC_R_SUCCESS)
4621 if (!secure_domain && res->view->dlv != NULL) {
4622 valoptions = DNS_VALIDATOR_DLV;
4623 secure_domain = ISC_TRUE;
4627 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4628 need_validation = ISC_FALSE;
4630 need_validation = secure_domain;
4636 asigrdataset = NULL;
4638 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4640 have_answer = ISC_TRUE;
4641 event = ISC_LIST_HEAD(fctx->events);
4642 if (event != NULL) {
4644 aname = dns_fixedname_name(&event->foundname);
4645 result = dns_name_copy(name, aname, NULL);
4646 if (result != ISC_R_SUCCESS)
4648 anodep = &event->node;
4650 * If this is an ANY, SIG or RRSIG query, we're not
4651 * going to return any rdatasets, unless we encountered
4652 * a CNAME or DNAME as "the answer". In this case,
4653 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4654 * and we must set up the rdatasets.
4656 if ((fctx->type != dns_rdatatype_any &&
4657 fctx->type != dns_rdatatype_rrsig &&
4658 fctx->type != dns_rdatatype_sig) ||
4659 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4660 ardataset = event->rdataset;
4661 asigrdataset = event->sigrdataset;
4667 * Find or create the cache node.
4670 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4671 if (result != ISC_R_SUCCESS)
4675 * Cache or validate each cacheable rdataset.
4677 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4678 for (rdataset = ISC_LIST_HEAD(name->list);
4680 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4681 if (!CACHE(rdataset))
4683 if (CHECKNAMES(rdataset)) {
4684 char namebuf[DNS_NAME_FORMATSIZE];
4685 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4686 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4688 dns_name_format(name, namebuf, sizeof(namebuf));
4689 dns_rdatatype_format(rdataset->type, typebuf,
4691 dns_rdataclass_format(rdataset->rdclass, classbuf,
4693 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4694 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4695 "check-names %s %s/%s/%s",
4696 fail ? "failure" : "warning",
4697 namebuf, typebuf, classbuf);
4699 if (ANSWER(rdataset)) {
4700 dns_db_detachnode(fctx->cache, &node);
4701 return (DNS_R_BADNAME);
4708 * Enforce the configure maximum cache TTL.
4710 if (rdataset->ttl > res->view->maxcachettl)
4711 rdataset->ttl = res->view->maxcachettl;
4714 * Find the SIG for this rdataset, if we have it.
4716 for (sigrdataset = ISC_LIST_HEAD(name->list);
4717 sigrdataset != NULL;
4718 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4719 if (sigrdataset->type == dns_rdatatype_rrsig &&
4720 sigrdataset->covers == rdataset->type)
4725 * If this RRset is in a secure domain, is in bailiwick,
4726 * and is not glue, attempt DNSSEC validation. (We do not
4727 * attempt to validate glue or out-of-bailiwick data--even
4728 * though there might be some performance benefit to doing
4729 * so--because it makes it simpler and safer to ensure that
4730 * records from a secure domain are only cached if validated
4731 * within the context of a query to the domain that owns
4734 if (secure_domain && rdataset->trust != dns_trust_glue &&
4735 !EXTERNAL(rdataset)) {
4739 * RRSIGs are validated as part of validating the
4742 if (rdataset->type == dns_rdatatype_rrsig)
4745 if (sigrdataset == NULL) {
4746 if (!ANSWER(rdataset) && need_validation) {
4748 * Ignore non-answer rdatasets that
4749 * are missing signatures.
4756 * Normalize the rdataset and sigrdataset TTLs.
4758 if (sigrdataset != NULL) {
4759 rdataset->ttl = ISC_MIN(rdataset->ttl,
4761 sigrdataset->ttl = rdataset->ttl;
4765 * Cache this rdataset/sigrdataset pair as
4766 * pending data. Track whether it was additional
4769 if (rdataset->trust == dns_trust_additional)
4770 trust = dns_trust_pending_additional;
4772 trust = dns_trust_pending_answer;
4774 rdataset->trust = trust;
4775 if (sigrdataset != NULL)
4776 sigrdataset->trust = trust;
4777 if (!need_validation || !ANSWER(rdataset)) {
4778 if (ANSWER(rdataset) &&
4779 rdataset->type != dns_rdatatype_rrsig) {
4780 isc_result_t tresult;
4781 dns_name_t *noqname = NULL;
4782 tresult = findnoqname(fctx, name,
4785 if (tresult == ISC_R_SUCCESS &&
4788 dns_rdataset_addnoqname(
4790 RUNTIME_CHECK(tresult ==
4794 addedrdataset = ardataset;
4795 result = dns_db_addrdataset(fctx->cache, node,
4796 NULL, now, rdataset,
4798 if (result == DNS_R_UNCHANGED) {
4799 result = ISC_R_SUCCESS;
4800 if (!need_validation &&
4801 ardataset != NULL &&
4802 NEGATIVE(ardataset)) {
4804 * The answer in the cache is
4805 * better than the answer we
4806 * found, and is a negative
4807 * cache entry, so we must set
4808 * eresult appropriately.
4810 if (NXDOMAIN(ardataset))
4812 DNS_R_NCACHENXDOMAIN;
4815 DNS_R_NCACHENXRRSET;
4817 * We have a negative response
4818 * from the cache so don't
4819 * attempt to add the RRSIG
4825 if (result != ISC_R_SUCCESS)
4827 if (sigrdataset != NULL) {
4828 addedrdataset = asigrdataset;
4829 result = dns_db_addrdataset(fctx->cache,
4833 if (result == DNS_R_UNCHANGED)
4834 result = ISC_R_SUCCESS;
4835 if (result != ISC_R_SUCCESS)
4837 } else if (!ANSWER(rdataset))
4841 if (ANSWER(rdataset) && need_validation) {
4842 if (fctx->type != dns_rdatatype_any &&
4843 fctx->type != dns_rdatatype_rrsig &&
4844 fctx->type != dns_rdatatype_sig) {
4846 * This is The Answer. We will
4847 * validate it, but first we cache
4848 * the rest of the response - it may
4849 * contain useful keys.
4851 INSIST(valrdataset == NULL &&
4852 valsigrdataset == NULL);
4853 valrdataset = rdataset;
4854 valsigrdataset = sigrdataset;
4857 * This is one of (potentially)
4858 * multiple answers to an ANY
4859 * or SIG query. To keep things
4860 * simple, we just start the
4861 * validator right away rather
4862 * than caching first and
4863 * having to remember which
4864 * rdatasets needed validation.
4866 result = valcreate(fctx, addrinfo,
4867 name, rdataset->type,
4872 * Defer any further validations.
4873 * This prevents multiple validators
4874 * from manipulating fctx->rmessage
4877 valoptions |= DNS_VALIDATOR_DEFER;
4879 } else 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 } else if (!EXTERNAL(rdataset)) {
4890 * It's OK to cache this rdataset now.
4892 if (ANSWER(rdataset))
4893 addedrdataset = ardataset;
4894 else if (ANSWERSIG(rdataset))
4895 addedrdataset = asigrdataset;
4897 addedrdataset = NULL;
4898 if (CHAINING(rdataset)) {
4899 if (rdataset->type == dns_rdatatype_cname)
4900 eresult = DNS_R_CNAME;
4902 INSIST(rdataset->type ==
4903 dns_rdatatype_dname);
4904 eresult = DNS_R_DNAME;
4907 if (rdataset->trust == dns_trust_glue &&
4908 (rdataset->type == dns_rdatatype_ns ||
4909 (rdataset->type == dns_rdatatype_rrsig &&
4910 rdataset->covers == dns_rdatatype_ns))) {
4912 * If the trust level is 'dns_trust_glue'
4913 * then we are adding data from a referral
4914 * we got while executing the search algorithm.
4915 * New referral data always takes precedence
4916 * over the existing cache contents.
4918 options = DNS_DBADD_FORCE;
4922 if (ANSWER(rdataset) &&
4923 rdataset->type != dns_rdatatype_rrsig) {
4924 isc_result_t tresult;
4925 dns_name_t *noqname = NULL;
4926 tresult = findnoqname(fctx, name,
4927 rdataset->type, &noqname);
4928 if (tresult == ISC_R_SUCCESS &&
4930 tresult = dns_rdataset_addnoqname(
4932 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4937 * Now we can add the rdataset.
4939 result = dns_db_addrdataset(fctx->cache,
4945 if (result == DNS_R_UNCHANGED) {
4946 if (ANSWER(rdataset) &&
4947 ardataset != NULL &&
4948 NEGATIVE(ardataset)) {
4950 * The answer in the cache is better
4951 * than the answer we found, and is
4952 * a negative cache entry, so we
4953 * must set eresult appropriately.
4955 if (NXDOMAIN(ardataset))
4956 eresult = DNS_R_NCACHENXDOMAIN;
4958 eresult = DNS_R_NCACHENXRRSET;
4960 result = ISC_R_SUCCESS;
4961 } else if (result != ISC_R_SUCCESS)
4966 if (valrdataset != NULL)
4967 result = valcreate(fctx, addrinfo, name, fctx->type,
4968 valrdataset, valsigrdataset, valoptions,
4971 if (result == ISC_R_SUCCESS && have_answer) {
4972 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4973 if (event != NULL) {
4975 * Negative results must be indicated in event->result.
4977 if (dns_rdataset_isassociated(event->rdataset) &&
4978 NEGATIVE(event->rdataset)) {
4979 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4980 eresult == DNS_R_NCACHENXRRSET);
4982 event->result = eresult;
4983 dns_db_attach(fctx->cache, adbp);
4984 dns_db_transfernode(fctx->cache, &node, anodep);
4985 clone_results(fctx);
4990 dns_db_detachnode(fctx->cache, &node);
4995 static inline isc_result_t
4996 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4998 isc_result_t result;
4999 dns_section_t section;
5002 FCTXTRACE("cache_message");
5004 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
5006 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
5008 for (section = DNS_SECTION_ANSWER;
5009 section <= DNS_SECTION_ADDITIONAL;
5011 result = dns_message_firstname(fctx->rmessage, section);
5012 while (result == ISC_R_SUCCESS) {
5014 dns_message_currentname(fctx->rmessage, section,
5016 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
5017 result = cache_name(fctx, name, addrinfo, now);
5018 if (result != ISC_R_SUCCESS)
5021 result = dns_message_nextname(fctx->rmessage, section);
5023 if (result != ISC_R_NOMORE)
5026 if (result == ISC_R_NOMORE)
5027 result = ISC_R_SUCCESS;
5029 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
5035 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
5038 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
5039 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
5040 isc_boolean_t optout, isc_boolean_t secure,
5041 dns_rdataset_t *ardataset, isc_result_t *eresultp)
5043 isc_result_t result;
5044 dns_rdataset_t rdataset;
5046 if (ardataset == NULL) {
5047 dns_rdataset_init(&rdataset);
5048 ardataset = &rdataset;
5051 result = dns_ncache_addoptout(message, cache, node, covers,
5052 now, maxttl, optout, ardataset);
5054 result = dns_ncache_add(message, cache, node, covers, now,
5056 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
5058 * If the cache now contains a negative entry and we
5059 * care about whether it is DNS_R_NCACHENXDOMAIN or
5060 * DNS_R_NCACHENXRRSET then extract it.
5062 if (NEGATIVE(ardataset)) {
5064 * The cache data is a negative cache entry.
5066 if (NXDOMAIN(ardataset))
5067 *eresultp = DNS_R_NCACHENXDOMAIN;
5069 *eresultp = DNS_R_NCACHENXRRSET;
5072 * Either we don't care about the nature of the
5073 * cache rdataset (because no fetch is interested
5074 * in the outcome), or the cache rdataset is not
5075 * a negative cache entry. Whichever case it is,
5076 * we can return success.
5078 * XXXRTH There's a CNAME/DNAME problem here.
5080 *eresultp = ISC_R_SUCCESS;
5082 result = ISC_R_SUCCESS;
5084 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
5085 dns_rdataset_disassociate(ardataset);
5090 static inline isc_result_t
5091 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
5092 dns_rdatatype_t covers, isc_stdtime_t now)
5094 isc_result_t result, eresult;
5096 dns_resolver_t *res;
5098 dns_dbnode_t *node, **anodep;
5099 dns_rdataset_t *ardataset;
5100 isc_boolean_t need_validation, secure_domain;
5102 dns_fetchevent_t *event;
5104 unsigned int valoptions = 0;
5106 FCTXTRACE("ncache_message");
5108 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
5111 need_validation = ISC_FALSE;
5112 POST(need_validation);
5113 secure_domain = ISC_FALSE;
5114 eresult = ISC_R_SUCCESS;
5119 * XXXMPA remove when we follow cnames and adjust the setting
5120 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
5122 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
5125 * Is DNSSEC validation required for this name?
5127 if (fctx->res->view->enablevalidation) {
5128 result = dns_view_issecuredomain(res->view, name,
5130 if (result != ISC_R_SUCCESS)
5133 if (!secure_domain && res->view->dlv != NULL) {
5134 valoptions = DNS_VALIDATOR_DLV;
5135 secure_domain = ISC_TRUE;
5139 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
5140 need_validation = ISC_FALSE;
5142 need_validation = secure_domain;
5144 if (secure_domain) {
5146 * Mark all rdatasets as pending.
5148 dns_rdataset_t *trdataset;
5151 result = dns_message_firstname(fctx->rmessage,
5152 DNS_SECTION_AUTHORITY);
5153 while (result == ISC_R_SUCCESS) {
5155 dns_message_currentname(fctx->rmessage,
5156 DNS_SECTION_AUTHORITY,
5158 for (trdataset = ISC_LIST_HEAD(tname->list);
5160 trdataset = ISC_LIST_NEXT(trdataset, link))
5161 trdataset->trust = dns_trust_pending_answer;
5162 result = dns_message_nextname(fctx->rmessage,
5163 DNS_SECTION_AUTHORITY);
5165 if (result != ISC_R_NOMORE)
5170 if (need_validation) {
5172 * Do negative response validation.
5174 result = valcreate(fctx, addrinfo, name, fctx->type,
5175 NULL, NULL, valoptions,
5176 res->buckets[fctx->bucketnum].task);
5178 * If validation is necessary, return now. Otherwise continue
5179 * to process the message, letting the validation complete
5180 * in its own good time.
5185 LOCK(&res->buckets[fctx->bucketnum].lock);
5191 if (!HAVE_ANSWER(fctx)) {
5192 event = ISC_LIST_HEAD(fctx->events);
5193 if (event != NULL) {
5195 aname = dns_fixedname_name(&event->foundname);
5196 result = dns_name_copy(name, aname, NULL);
5197 if (result != ISC_R_SUCCESS)
5199 anodep = &event->node;
5200 ardataset = event->rdataset;
5205 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
5206 if (result != ISC_R_SUCCESS)
5210 * If we are asking for a SOA record set the cache time
5211 * to zero to facilitate locating the containing zone of
5214 ttl = fctx->res->view->maxncachettl;
5215 if (fctx->type == dns_rdatatype_soa &&
5216 covers == dns_rdatatype_any &&
5217 fctx->res->zero_no_soa_ttl)
5220 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
5221 covers, now, ttl, ISC_FALSE,
5222 ISC_FALSE, ardataset, &eresult);
5223 if (result != ISC_R_SUCCESS)
5226 if (!HAVE_ANSWER(fctx)) {
5227 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
5228 if (event != NULL) {
5229 event->result = eresult;
5230 dns_db_attach(fctx->cache, adbp);
5231 dns_db_transfernode(fctx->cache, &node, anodep);
5232 clone_results(fctx);
5237 UNLOCK(&res->buckets[fctx->bucketnum].lock);
5240 dns_db_detachnode(fctx->cache, &node);
5246 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
5247 isc_boolean_t external, isc_boolean_t gluing)
5249 name->attributes |= DNS_NAMEATTR_CACHE;
5251 rdataset->trust = dns_trust_glue;
5253 * Glue with 0 TTL causes problems. We force the TTL to
5254 * 1 second to prevent this.
5256 if (rdataset->ttl == 0)
5259 rdataset->trust = dns_trust_additional;
5261 * Avoid infinite loops by only marking new rdatasets.
5263 if (!CACHE(rdataset)) {
5264 name->attributes |= DNS_NAMEATTR_CHASE;
5265 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
5267 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
5269 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
5273 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
5274 dns_section_t section)
5276 fetchctx_t *fctx = arg;
5277 isc_result_t result;
5279 dns_rdataset_t *rdataset;
5280 isc_boolean_t external;
5281 dns_rdatatype_t rtype;
5282 isc_boolean_t gluing;
5284 REQUIRE(VALID_FCTX(fctx));
5286 #if CHECK_FOR_GLUE_IN_ANSWER
5287 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
5288 return (ISC_R_SUCCESS);
5297 result = dns_message_findname(fctx->rmessage, section, addname,
5298 dns_rdatatype_any, 0, &name, NULL);
5299 if (result == ISC_R_SUCCESS) {
5300 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5301 if (type == dns_rdatatype_a) {
5302 for (rdataset = ISC_LIST_HEAD(name->list);
5304 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5305 if (rdataset->type == dns_rdatatype_rrsig)
5306 rtype = rdataset->covers;
5308 rtype = rdataset->type;
5309 if (rtype == dns_rdatatype_a ||
5310 rtype == dns_rdatatype_aaaa)
5311 mark_related(name, rdataset, external,
5315 result = dns_message_findtype(name, type, 0,
5317 if (result == ISC_R_SUCCESS) {
5318 mark_related(name, rdataset, external, gluing);
5320 * Do we have its SIG too?
5323 result = dns_message_findtype(name,
5324 dns_rdatatype_rrsig,
5326 if (result == ISC_R_SUCCESS)
5327 mark_related(name, rdataset, external,
5333 return (ISC_R_SUCCESS);
5337 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5338 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5341 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5342 #define CHECK_FOR_GLUE_IN_ANSWER 0
5344 #if CHECK_FOR_GLUE_IN_ANSWER
5346 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5347 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5352 chase_additional(fetchctx_t *fctx) {
5353 isc_boolean_t rescan;
5354 dns_section_t section = DNS_SECTION_ADDITIONAL;
5355 isc_result_t result;
5360 for (result = dns_message_firstname(fctx->rmessage, section);
5361 result == ISC_R_SUCCESS;
5362 result = dns_message_nextname(fctx->rmessage, section)) {
5363 dns_name_t *name = NULL;
5364 dns_rdataset_t *rdataset;
5365 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5367 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5369 name->attributes &= ~DNS_NAMEATTR_CHASE;
5370 for (rdataset = ISC_LIST_HEAD(name->list);
5372 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5373 if (CHASE(rdataset)) {
5374 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5375 (void)dns_rdataset_additionaldata(rdataset,
5386 static inline isc_result_t
5387 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5388 isc_result_t result;
5389 dns_rdata_t rdata = DNS_RDATA_INIT;
5390 dns_rdata_cname_t cname;
5392 result = dns_rdataset_first(rdataset);
5393 if (result != ISC_R_SUCCESS)
5395 dns_rdataset_current(rdataset, &rdata);
5396 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5397 if (result != ISC_R_SUCCESS)
5399 dns_name_init(tname, NULL);
5400 dns_name_clone(&cname.cname, tname);
5401 dns_rdata_freestruct(&cname);
5403 return (ISC_R_SUCCESS);
5406 static inline isc_result_t
5407 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5408 dns_name_t *oname, dns_fixedname_t *fixeddname)
5410 isc_result_t result;
5411 dns_rdata_t rdata = DNS_RDATA_INIT;
5412 unsigned int nlabels;
5414 dns_namereln_t namereln;
5415 dns_rdata_dname_t dname;
5416 dns_fixedname_t prefix;
5419 * Get the target name of the DNAME.
5421 result = dns_rdataset_first(rdataset);
5422 if (result != ISC_R_SUCCESS)
5424 dns_rdataset_current(rdataset, &rdata);
5425 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5426 if (result != ISC_R_SUCCESS)
5430 * Get the prefix of qname.
5432 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5433 if (namereln != dns_namereln_subdomain) {
5434 char qbuf[DNS_NAME_FORMATSIZE];
5435 char obuf[DNS_NAME_FORMATSIZE];
5437 dns_rdata_freestruct(&dname);
5438 dns_name_format(qname, qbuf, sizeof(qbuf));
5439 dns_name_format(oname, obuf, sizeof(obuf));
5440 log_formerr(fctx, "unrelated DNAME in answer: "
5441 "%s is not in %s", qbuf, obuf);
5442 return (DNS_R_FORMERR);
5444 dns_fixedname_init(&prefix);
5445 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5446 dns_fixedname_init(fixeddname);
5447 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5449 dns_fixedname_name(fixeddname), NULL);
5450 dns_rdata_freestruct(&dname);
5454 static isc_boolean_t
5455 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5456 dns_rdataset_t *rdataset)
5458 isc_result_t result;
5459 dns_rdata_t rdata = DNS_RDATA_INIT;
5461 struct in6_addr in6a;
5462 isc_netaddr_t netaddr;
5463 char addrbuf[ISC_NETADDR_FORMATSIZE];
5464 char namebuf[DNS_NAME_FORMATSIZE];
5469 /* By default, we allow any addresses. */
5470 if (view->denyansweracl == NULL)
5474 * If the owner name matches one in the exclusion list, either exactly
5475 * or partially, allow it.
5477 if (view->answeracl_exclude != NULL) {
5478 dns_rbtnode_t *node = NULL;
5480 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5481 &node, NULL, 0, NULL, NULL);
5483 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5488 * Otherwise, search the filter list for a match for each address
5489 * record. If a match is found, the address should be filtered,
5490 * so should the entire answer.
5492 for (result = dns_rdataset_first(rdataset);
5493 result == ISC_R_SUCCESS;
5494 result = dns_rdataset_next(rdataset)) {
5495 dns_rdata_reset(&rdata);
5496 dns_rdataset_current(rdataset, &rdata);
5497 if (rdataset->type == dns_rdatatype_a) {
5498 INSIST(rdata.length == sizeof(ina.s_addr));
5499 memmove(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5500 isc_netaddr_fromin(&netaddr, &ina);
5502 INSIST(rdata.length == sizeof(in6a.s6_addr));
5503 memmove(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5504 isc_netaddr_fromin6(&netaddr, &in6a);
5507 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5508 &view->aclenv, &match, NULL);
5510 if (result == ISC_R_SUCCESS && match > 0) {
5511 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5512 dns_name_format(name, namebuf, sizeof(namebuf));
5513 dns_rdatatype_format(rdataset->type, typebuf,
5515 dns_rdataclass_format(rdataset->rdclass, classbuf,
5517 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5518 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5519 "answer address %s denied for %s/%s/%s",
5520 addrbuf, namebuf, typebuf, classbuf);
5528 static isc_boolean_t
5529 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5530 dns_rdatatype_t type, dns_name_t *tname,
5533 isc_result_t result;
5534 dns_rbtnode_t *node = NULL;
5535 char qnamebuf[DNS_NAME_FORMATSIZE];
5536 char tnamebuf[DNS_NAME_FORMATSIZE];
5540 /* By default, we allow any target name. */
5541 if (view->denyanswernames == NULL)
5545 * If the owner name matches one in the exclusion list, either exactly
5546 * or partially, allow it.
5548 if (view->answernames_exclude != NULL) {
5549 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5550 &node, NULL, 0, NULL, NULL);
5551 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5556 * If the target name is a subdomain of the search domain, allow it.
5558 if (dns_name_issubdomain(tname, domain))
5562 * Otherwise, apply filters.
5564 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5565 NULL, 0, NULL, NULL);
5566 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5567 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5568 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5569 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5570 dns_rdataclass_format(view->rdclass, classbuf,
5572 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5573 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5574 "%s target %s denied for %s/%s",
5575 typebuf, tnamebuf, qnamebuf, classbuf);
5583 trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) {
5584 char ns_namebuf[DNS_NAME_FORMATSIZE];
5585 char namebuf[DNS_NAME_FORMATSIZE];
5586 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5588 if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) {
5589 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf));
5590 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
5591 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf));
5593 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5594 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
5595 "fctx %p: trimming ttl of %s/NS for %s/%s: "
5596 "%u -> %u", fctx, ns_namebuf, namebuf, tbuf,
5597 rdataset->ttl, fctx->ns_ttl);
5598 rdataset->ttl = fctx->ns_ttl;
5603 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5604 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5605 * section for the NS RRset if the query type is NS; if it has
5606 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5607 * section for A and AAAA queries.
5609 #define LOOK_FOR_NS_IN_ANSWER 0x1
5610 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5613 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5614 unsigned int look_in_options)
5616 isc_result_t result;
5617 dns_message_t *message;
5618 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name, *save_name;
5619 dns_rdataset_t *rdataset, *ns_rdataset;
5620 isc_boolean_t aa, negative_response;
5621 dns_rdatatype_t type, save_type;
5622 dns_section_t section;
5624 FCTXTRACE("noanswer_response");
5626 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5627 INSIST(fctx->type == dns_rdatatype_ns);
5628 section = DNS_SECTION_ANSWER;
5630 section = DNS_SECTION_AUTHORITY;
5632 message = fctx->rmessage;
5637 if (oqname == NULL) {
5639 * We have a normal, non-chained negative response or
5642 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5646 qname = &fctx->name;
5649 * We're being invoked by answer_response() after it has
5650 * followed a CNAME/DNAME chain.
5655 * If the current qname is not a subdomain of the query
5656 * domain, there's no point in looking at the authority
5657 * section without doing DNSSEC validation.
5659 * Until we do that validation, we'll just return success
5662 if (!dns_name_issubdomain(qname, &fctx->domain))
5663 return (ISC_R_SUCCESS);
5667 * We have to figure out if this is a negative response, or a
5672 * Sometimes we can tell if its a negative response by looking at
5673 * the message header.
5675 negative_response = ISC_FALSE;
5676 if (message->rcode == dns_rcode_nxdomain ||
5677 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5678 message->counts[DNS_SECTION_AUTHORITY] == 0))
5679 negative_response = ISC_TRUE;
5682 * Process the authority section.
5689 save_type = dns_rdatatype_none;
5690 result = dns_message_firstname(message, section);
5691 while (result == ISC_R_SUCCESS) {
5693 dns_message_currentname(message, section, &name);
5694 if (dns_name_issubdomain(name, &fctx->domain)) {
5696 * Look for NS/SOA RRsets first.
5698 for (rdataset = ISC_LIST_HEAD(name->list);
5700 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5701 type = rdataset->type;
5702 if (type == dns_rdatatype_rrsig)
5703 type = rdataset->covers;
5704 if (((type == dns_rdatatype_ns ||
5705 type == dns_rdatatype_soa) &&
5706 !dns_name_issubdomain(qname, name))) {
5707 char qbuf[DNS_NAME_FORMATSIZE];
5708 char nbuf[DNS_NAME_FORMATSIZE];
5709 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5710 dns_rdatatype_format(type, tbuf,
5712 dns_name_format(name, nbuf,
5714 dns_name_format(qname, qbuf,
5717 "unrelated %s %s in "
5718 "%s authority section",
5720 return (DNS_R_FORMERR);
5722 if (type == dns_rdatatype_ns) {
5726 * Only one set of NS RRs is allowed.
5728 if (rdataset->type ==
5730 if (ns_name != NULL &&
5737 return (DNS_R_FORMERR);
5740 ns_rdataset = rdataset;
5744 rdataset->attributes |=
5745 DNS_RDATASETATTR_CACHE;
5746 rdataset->trust = dns_trust_glue;
5748 if (type == dns_rdatatype_soa) {
5750 * SOA, or RRSIG SOA.
5752 * Only one SOA is allowed.
5754 if (rdataset->type ==
5755 dns_rdatatype_soa) {
5756 if (soa_name != NULL &&
5763 return (DNS_R_FORMERR);
5768 DNS_NAMEATTR_NCACHE;
5769 rdataset->attributes |=
5770 DNS_RDATASETATTR_NCACHE;
5773 dns_trust_authauthority;
5774 else if (ISFORWARDER(fctx->addrinfo))
5779 dns_trust_additional;
5783 result = dns_message_nextname(message, section);
5784 if (result == ISC_R_NOMORE)
5786 else if (result != ISC_R_SUCCESS)
5790 log_ns_ttl(fctx, "noanswer_response");
5792 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
5793 !dns_name_equal(ns_name, dns_rootname))
5794 trim_ns_ttl(fctx, ns_name, ns_rdataset);
5797 * A negative response has a SOA record (Type 2)
5798 * and a optional NS RRset (Type 1) or it has neither
5799 * a SOA or a NS RRset (Type 3, handled above) or
5800 * rcode is NXDOMAIN (handled above) in which case
5801 * the NS RRset is allowed (Type 4).
5803 if (soa_name != NULL)
5804 negative_response = ISC_TRUE;
5806 result = dns_message_firstname(message, section);
5807 while (result == ISC_R_SUCCESS) {
5809 dns_message_currentname(message, section, &name);
5810 if (dns_name_issubdomain(name, &fctx->domain)) {
5811 for (rdataset = ISC_LIST_HEAD(name->list);
5813 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5814 type = rdataset->type;
5815 if (type == dns_rdatatype_rrsig)
5816 type = rdataset->covers;
5817 if (type == dns_rdatatype_nsec ||
5818 type == dns_rdatatype_nsec3) {
5820 * NSEC or RRSIG NSEC.
5822 if (negative_response) {
5824 DNS_NAMEATTR_NCACHE;
5825 rdataset->attributes |=
5826 DNS_RDATASETATTR_NCACHE;
5827 } else if (type == dns_rdatatype_nsec) {
5830 rdataset->attributes |=
5831 DNS_RDATASETATTR_CACHE;
5835 dns_trust_authauthority;
5836 else if (ISFORWARDER(fctx->addrinfo))
5841 dns_trust_additional;
5843 * No additional data needs to be
5846 } else if (type == dns_rdatatype_ds) {
5850 * These should only be here if
5851 * this is a referral, and there
5852 * should only be one DS RRset.
5854 if (ns_name == NULL) {
5858 return (DNS_R_FORMERR);
5860 if (rdataset->type ==
5862 if (ds_name != NULL &&
5869 return (DNS_R_FORMERR);
5875 rdataset->attributes |=
5876 DNS_RDATASETATTR_CACHE;
5879 dns_trust_authauthority;
5880 else if (ISFORWARDER(fctx->addrinfo))
5885 dns_trust_additional;
5890 save_type = ISC_LIST_HEAD(name->list)->type;
5892 result = dns_message_nextname(message, section);
5893 if (result == ISC_R_NOMORE)
5895 else if (result != ISC_R_SUCCESS)
5900 * Trigger lookups for DNS nameservers.
5902 if (negative_response && message->rcode == dns_rcode_noerror &&
5903 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5904 dns_name_equal(soa_name, qname) &&
5905 !dns_name_equal(qname, dns_rootname))
5906 return (DNS_R_CHASEDSSERVERS);
5909 * Did we find anything?
5911 if (!negative_response && ns_name == NULL) {
5915 if (oqname != NULL) {
5917 * We've already got a partial CNAME/DNAME chain,
5918 * and haven't found else anything useful here, but
5919 * no error has occurred since we have an answer.
5921 return (ISC_R_SUCCESS);
5924 * The responder is insane.
5926 if (save_name == NULL) {
5927 log_formerr(fctx, "invalid response");
5928 return (DNS_R_FORMERR);
5930 if (!dns_name_issubdomain(save_name, &fctx->domain)) {
5931 char nbuf[DNS_NAME_FORMATSIZE];
5932 char dbuf[DNS_NAME_FORMATSIZE];
5933 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5935 dns_rdatatype_format(save_type, tbuf,
5937 dns_name_format(save_name, nbuf, sizeof(nbuf));
5938 dns_name_format(&fctx->domain, dbuf,
5941 log_formerr(fctx, "Name %s (%s) not subdomain"
5942 " of zone %s -- invalid response",
5945 log_formerr(fctx, "invalid response");
5947 return (DNS_R_FORMERR);
5952 * If we found both NS and SOA, they should be the same name.
5954 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5955 log_formerr(fctx, "NS/SOA mismatch");
5956 return (DNS_R_FORMERR);
5960 * Do we have a referral? (We only want to follow a referral if
5961 * we're not following a chain.)
5963 if (!negative_response && ns_name != NULL && oqname == NULL) {
5965 * We already know ns_name is a subdomain of fctx->domain.
5966 * If ns_name is equal to fctx->domain, we're not making
5967 * progress. We return DNS_R_FORMERR so that we'll keep
5968 * trying other servers.
5970 if (dns_name_equal(ns_name, &fctx->domain)) {
5971 log_formerr(fctx, "non-improving referral");
5972 return (DNS_R_FORMERR);
5976 * If the referral name is not a parent of the query
5977 * name, consider the responder insane.
5979 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5981 log_formerr(fctx, "referral to non-parent");
5982 FCTXTRACE("referral to non-parent");
5983 return (DNS_R_FORMERR);
5987 * Mark any additional data related to this rdataset.
5988 * It's important that we do this before we change the
5991 INSIST(ns_rdataset != NULL);
5992 fctx->attributes |= FCTX_ATTR_GLUING;
5993 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5995 #if CHECK_FOR_GLUE_IN_ANSWER
5997 * Look in the answer section for "glue" that is incorrectly
5998 * returned as a answer. This is needed if the server also
5999 * minimizes the response size by not adding records to the
6000 * additional section that are in the answer section or if
6001 * the record gets dropped due to message size constraints.
6003 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
6004 (fctx->type == dns_rdatatype_aaaa ||
6005 fctx->type == dns_rdatatype_a))
6006 (void)dns_rdataset_additionaldata(ns_rdataset,
6007 check_answer, fctx);
6009 fctx->attributes &= ~FCTX_ATTR_GLUING;
6011 * NS rdatasets with 0 TTL cause problems.
6012 * dns_view_findzonecut() will not find them when we
6013 * try to follow the referral, and we'll SERVFAIL
6014 * because the best nameservers are now above QDOMAIN.
6015 * We force the TTL to 1 second to prevent this.
6017 if (ns_rdataset->ttl == 0)
6018 ns_rdataset->ttl = 1;
6020 * Set the current query domain to the referral name.
6022 * XXXRTH We should check if we're in forward-only mode, and
6023 * if so we should bail out.
6025 INSIST(dns_name_countlabels(&fctx->domain) > 0);
6026 dns_name_free(&fctx->domain, fctx->mctx);
6027 if (dns_rdataset_isassociated(&fctx->nameservers))
6028 dns_rdataset_disassociate(&fctx->nameservers);
6029 dns_name_init(&fctx->domain, NULL);
6030 result = dns_name_dup(ns_name, fctx->mctx, &fctx->domain);
6031 if (result != ISC_R_SUCCESS)
6033 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6034 fctx->ns_ttl_ok = ISC_FALSE;
6035 log_ns_ttl(fctx, "DELEGATION");
6036 return (DNS_R_DELEGATION);
6040 * Since we're not doing a referral, we don't want to cache any
6041 * NS RRs we may have found.
6043 if (ns_name != NULL)
6044 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
6046 if (negative_response && oqname == NULL)
6047 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
6049 return (ISC_R_SUCCESS);
6053 answer_response(fetchctx_t *fctx) {
6054 isc_result_t result;
6055 dns_message_t *message;
6056 dns_name_t *name, *qname, tname, *ns_name;
6057 dns_rdataset_t *rdataset, *ns_rdataset;
6058 isc_boolean_t done, external, chaining, aa, found, want_chaining;
6059 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
6061 dns_rdatatype_t type;
6062 dns_fixedname_t dname, fqname;
6065 FCTXTRACE("answer_response");
6067 message = fctx->rmessage;
6070 * Examine the answer section, marking those rdatasets which are
6071 * part of the answer and should be cached.
6075 found_cname = ISC_FALSE;
6076 found_type = ISC_FALSE;
6077 chaining = ISC_FALSE;
6078 have_answer = ISC_FALSE;
6079 want_chaining = ISC_FALSE;
6080 POST(want_chaining);
6081 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
6085 qname = &fctx->name;
6087 view = fctx->res->view;
6088 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
6089 while (!done && result == ISC_R_SUCCESS) {
6091 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
6092 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6093 if (dns_name_equal(name, qname)) {
6094 wanted_chaining = ISC_FALSE;
6095 for (rdataset = ISC_LIST_HEAD(name->list);
6097 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6099 want_chaining = ISC_FALSE;
6101 if (rdataset->type == dns_rdatatype_nsec3) {
6103 * NSEC3 records are not allowed to
6104 * appear in the answer section.
6106 log_formerr(fctx, "NSEC3 in answer");
6107 return (DNS_R_FORMERR);
6111 * Apply filters, if given, on answers to reject
6112 * a malicious attempt of rebinding.
6114 if ((rdataset->type == dns_rdatatype_a ||
6115 rdataset->type == dns_rdatatype_aaaa) &&
6116 !is_answeraddress_allowed(view, name,
6118 return (DNS_R_SERVFAIL);
6121 if (rdataset->type == type && !found_cname) {
6123 * We've found an ordinary answer.
6126 found_type = ISC_TRUE;
6128 aflag = DNS_RDATASETATTR_ANSWER;
6129 } else if (type == dns_rdatatype_any) {
6131 * We've found an answer matching
6132 * an ANY query. There may be
6136 aflag = DNS_RDATASETATTR_ANSWER;
6137 } else if (rdataset->type == dns_rdatatype_rrsig
6138 && rdataset->covers == type
6141 * We've found a signature that
6142 * covers the type we're looking for.
6145 found_type = ISC_TRUE;
6146 aflag = DNS_RDATASETATTR_ANSWERSIG;
6147 } else if (rdataset->type ==
6151 * We're looking for something else,
6152 * but we found a CNAME.
6154 * Getting a CNAME response for some
6155 * query types is an error, see
6156 * RFC 4035, Section 2.5.
6158 if (type == dns_rdatatype_rrsig ||
6159 type == dns_rdatatype_key ||
6160 type == dns_rdatatype_nsec) {
6161 char buf[DNS_RDATATYPE_FORMATSIZE];
6162 dns_rdatatype_format(fctx->type,
6167 return (DNS_R_FORMERR);
6170 found_cname = ISC_TRUE;
6171 want_chaining = ISC_TRUE;
6172 aflag = DNS_RDATASETATTR_ANSWER;
6173 result = cname_target(rdataset,
6175 if (result != ISC_R_SUCCESS)
6177 /* Apply filters on the target name. */
6178 if (!is_answertarget_allowed(view,
6183 return (DNS_R_SERVFAIL);
6185 } else if (rdataset->type == dns_rdatatype_rrsig
6186 && rdataset->covers ==
6190 * We're looking for something else,
6191 * but we found a SIG CNAME.
6194 found_cname = ISC_TRUE;
6195 aflag = DNS_RDATASETATTR_ANSWERSIG;
6200 * We've found an answer to our
6205 rdataset->attributes |=
6206 DNS_RDATASETATTR_CACHE;
6207 rdataset->trust = dns_trust_answer;
6210 * This data is "the" answer
6211 * to our question only if
6212 * we're not chaining (i.e.
6213 * if we haven't followed
6214 * a CNAME or DNAME).
6218 DNS_RDATASETATTR_ANSWER)
6219 have_answer = ISC_TRUE;
6221 DNS_NAMEATTR_ANSWER;
6222 rdataset->attributes |= aflag;
6225 dns_trust_authanswer;
6226 } else if (external) {
6228 * This data is outside of
6229 * our query domain, and
6230 * may not be cached.
6232 rdataset->attributes |=
6233 DNS_RDATASETATTR_EXTERNAL;
6237 * Mark any additional data related
6240 (void)dns_rdataset_additionaldata(
6248 if (want_chaining) {
6249 wanted_chaining = ISC_TRUE;
6251 DNS_NAMEATTR_CHAINING;
6252 rdataset->attributes |=
6253 DNS_RDATASETATTR_CHAINING;
6258 * We could add an "else" clause here and
6259 * log that we're ignoring this rdataset.
6263 * If wanted_chaining is true, we've done
6264 * some chaining as the result of processing
6265 * this node, and thus we need to set
6268 * We don't set chaining inside of the
6269 * rdataset loop because doing that would
6270 * cause us to ignore the signatures of
6273 if (wanted_chaining)
6274 chaining = ISC_TRUE;
6277 * Look for a DNAME (or its SIG). Anything else is
6280 wanted_chaining = ISC_FALSE;
6281 for (rdataset = ISC_LIST_HEAD(name->list);
6283 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6284 isc_boolean_t found_dname = ISC_FALSE;
6285 dns_name_t *dname_name;
6289 if (rdataset->type == dns_rdatatype_dname) {
6291 * We're looking for something else,
6292 * but we found a DNAME.
6294 * If we're not chaining, then the
6295 * DNAME should not be external.
6297 if (!chaining && external) {
6300 return (DNS_R_FORMERR);
6303 want_chaining = ISC_TRUE;
6304 POST(want_chaining);
6305 aflag = DNS_RDATASETATTR_ANSWER;
6306 result = dname_target(fctx, rdataset,
6309 if (result == ISC_R_NOSPACE) {
6311 * We can't construct the
6312 * DNAME target. Do not
6315 want_chaining = ISC_FALSE;
6316 POST(want_chaining);
6317 } else if (result != ISC_R_SUCCESS)
6320 found_dname = ISC_TRUE;
6322 dname_name = dns_fixedname_name(&dname);
6323 if (!is_answertarget_allowed(view,
6328 return (DNS_R_SERVFAIL);
6330 } else if (rdataset->type == dns_rdatatype_rrsig
6331 && rdataset->covers ==
6332 dns_rdatatype_dname) {
6334 * We've found a signature that
6338 aflag = DNS_RDATASETATTR_ANSWERSIG;
6343 * We've found an answer to our
6348 rdataset->attributes |=
6349 DNS_RDATASETATTR_CACHE;
6350 rdataset->trust = dns_trust_answer;
6353 * This data is "the" answer
6354 * to our question only if
6355 * we're not chaining.
6359 DNS_RDATASETATTR_ANSWER)
6360 have_answer = ISC_TRUE;
6362 DNS_NAMEATTR_ANSWER;
6363 rdataset->attributes |= aflag;
6366 dns_trust_authanswer;
6367 } else if (external) {
6368 rdataset->attributes |=
6369 DNS_RDATASETATTR_EXTERNAL;
6377 * Copy the dname into the
6380 * Although we check for
6381 * failure of the copy
6382 * operation, in practice it
6383 * should never fail since
6384 * we already know that the
6385 * result fits in a fixedname.
6387 dns_fixedname_init(&fqname);
6388 result = dns_name_copy(
6389 dns_fixedname_name(&dname),
6390 dns_fixedname_name(&fqname),
6392 if (result != ISC_R_SUCCESS)
6394 wanted_chaining = ISC_TRUE;
6396 DNS_NAMEATTR_CHAINING;
6397 rdataset->attributes |=
6398 DNS_RDATASETATTR_CHAINING;
6399 qname = dns_fixedname_name(
6404 if (wanted_chaining)
6405 chaining = ISC_TRUE;
6407 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6409 if (result == ISC_R_NOMORE)
6410 result = ISC_R_SUCCESS;
6411 if (result != ISC_R_SUCCESS)
6415 * We should have found an answer.
6418 log_formerr(fctx, "reply has no answer");
6419 return (DNS_R_FORMERR);
6423 * This response is now potentially cacheable.
6425 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6428 * Did chaining end before we got the final answer?
6432 * Yes. This may be a negative reply, so hand off
6433 * authority section processing to the noanswer code.
6434 * If it isn't a noanswer response, no harm will be
6437 return (noanswer_response(fctx, qname, 0));
6441 * We didn't end with an incomplete chain, so the rcode should be
6444 if (message->rcode != dns_rcode_noerror) {
6445 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6447 return (DNS_R_FORMERR);
6451 * Examine the authority section (if there is one).
6453 * We expect there to be only one owner name for all the rdatasets
6454 * in this section, and we expect that it is not external.
6459 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6460 while (!done && result == ISC_R_SUCCESS) {
6462 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6463 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6466 * We expect to find NS or SIG NS rdatasets, and
6469 for (rdataset = ISC_LIST_HEAD(name->list);
6471 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6472 if (rdataset->type == dns_rdatatype_ns ||
6473 (rdataset->type == dns_rdatatype_rrsig &&
6474 rdataset->covers == dns_rdatatype_ns)) {
6477 rdataset->attributes |=
6478 DNS_RDATASETATTR_CACHE;
6479 if (aa && !chaining)
6481 dns_trust_authauthority;
6484 dns_trust_additional;
6486 if (rdataset->type == dns_rdatatype_ns) {
6488 ns_rdataset = rdataset;
6491 * Mark any additional data related
6494 (void)dns_rdataset_additionaldata(
6502 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6504 if (result == ISC_R_NOMORE)
6505 result = ISC_R_SUCCESS;
6507 log_ns_ttl(fctx, "answer_response");
6509 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6510 !dns_name_equal(ns_name, dns_rootname))
6511 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6516 static isc_boolean_t
6517 fctx_decreference(fetchctx_t *fctx) {
6518 isc_boolean_t bucket_empty = ISC_FALSE;
6520 INSIST(fctx->references > 0);
6522 if (fctx->references == 0) {
6524 * No one cares about the result of this fetch anymore.
6526 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6527 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6529 * This fctx is already shutdown; we were just
6530 * waiting for the last reference to go away.
6532 bucket_empty = fctx_unlink(fctx);
6536 * Initiate shutdown.
6538 fctx_shutdown(fctx);
6541 return (bucket_empty);
6545 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6546 dns_fetchevent_t *fevent;
6547 dns_resolver_t *res;
6549 isc_result_t result;
6550 isc_boolean_t bucket_empty;
6551 isc_boolean_t locked = ISC_FALSE;
6552 unsigned int bucketnum;
6553 dns_rdataset_t nameservers;
6554 dns_fixedname_t fixed;
6557 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6558 fevent = (dns_fetchevent_t *)event;
6559 fctx = event->ev_arg;
6560 REQUIRE(VALID_FCTX(fctx));
6564 FCTXTRACE("resume_dslookup");
6566 if (fevent->node != NULL)
6567 dns_db_detachnode(fevent->db, &fevent->node);
6568 if (fevent->db != NULL)
6569 dns_db_detach(&fevent->db);
6571 dns_rdataset_init(&nameservers);
6573 bucketnum = fctx->bucketnum;
6574 if (fevent->result == ISC_R_CANCELED) {
6575 dns_resolver_destroyfetch(&fctx->nsfetch);
6576 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6577 } else if (fevent->result == ISC_R_SUCCESS) {
6579 FCTXTRACE("resuming DS lookup");
6581 dns_resolver_destroyfetch(&fctx->nsfetch);
6582 if (dns_rdataset_isassociated(&fctx->nameservers))
6583 dns_rdataset_disassociate(&fctx->nameservers);
6584 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6585 fctx->ns_ttl = fctx->nameservers.ttl;
6586 fctx->ns_ttl_ok = ISC_TRUE;
6587 log_ns_ttl(fctx, "resume_dslookup");
6588 dns_name_free(&fctx->domain, fctx->mctx);
6589 dns_name_init(&fctx->domain, NULL);
6590 result = dns_name_dup(&fctx->nsname, fctx->mctx, &fctx->domain);
6591 if (result != ISC_R_SUCCESS) {
6592 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6598 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6601 dns_rdataset_t *nsrdataset = NULL;
6604 * Retrieve state from fctx->nsfetch before we destroy it.
6606 dns_fixedname_init(&fixed);
6607 domain = dns_fixedname_name(&fixed);
6608 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6609 if (dns_name_equal(&fctx->nsname, domain)) {
6610 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6611 dns_resolver_destroyfetch(&fctx->nsfetch);
6614 if (dns_rdataset_isassociated(
6615 &fctx->nsfetch->private->nameservers)) {
6617 &fctx->nsfetch->private->nameservers,
6619 nsrdataset = &nameservers;
6622 dns_resolver_destroyfetch(&fctx->nsfetch);
6623 n = dns_name_countlabels(&fctx->nsname);
6624 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6627 if (dns_rdataset_isassociated(fevent->rdataset))
6628 dns_rdataset_disassociate(fevent->rdataset);
6629 FCTXTRACE("continuing to look for parent's NS records");
6630 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6631 dns_rdatatype_ns, domain,
6632 nsrdataset, NULL, 0, task,
6633 resume_dslookup, fctx,
6634 &fctx->nsrrset, NULL,
6636 if (result != ISC_R_SUCCESS)
6637 fctx_done(fctx, result, __LINE__);
6639 LOCK(&res->buckets[bucketnum].lock);
6646 if (dns_rdataset_isassociated(&nameservers))
6647 dns_rdataset_disassociate(&nameservers);
6648 if (dns_rdataset_isassociated(fevent->rdataset))
6649 dns_rdataset_disassociate(fevent->rdataset);
6650 INSIST(fevent->sigrdataset == NULL);
6651 isc_event_free(&event);
6653 LOCK(&res->buckets[bucketnum].lock);
6654 bucket_empty = fctx_decreference(fctx);
6655 UNLOCK(&res->buckets[bucketnum].lock);
6661 checknamessection(dns_message_t *message, dns_section_t section) {
6662 isc_result_t result;
6664 dns_rdata_t rdata = DNS_RDATA_INIT;
6665 dns_rdataset_t *rdataset;
6667 for (result = dns_message_firstname(message, section);
6668 result == ISC_R_SUCCESS;
6669 result = dns_message_nextname(message, section))
6672 dns_message_currentname(message, section, &name);
6673 for (rdataset = ISC_LIST_HEAD(name->list);
6675 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6676 for (result = dns_rdataset_first(rdataset);
6677 result == ISC_R_SUCCESS;
6678 result = dns_rdataset_next(rdataset)) {
6679 dns_rdataset_current(rdataset, &rdata);
6680 if (!dns_rdata_checkowner(name, rdata.rdclass,
6683 !dns_rdata_checknames(&rdata, name, NULL))
6685 rdataset->attributes |=
6686 DNS_RDATASETATTR_CHECKNAMES;
6688 dns_rdata_reset(&rdata);
6695 checknames(dns_message_t *message) {
6697 checknamessection(message, DNS_SECTION_ANSWER);
6698 checknamessection(message, DNS_SECTION_AUTHORITY);
6699 checknamessection(message, DNS_SECTION_ADDITIONAL);
6703 * Log server NSID at log level 'level'
6706 log_nsid(isc_buffer_t *opt, size_t nsid_len, resquery_t *query,
6707 int level, isc_mem_t *mctx)
6709 static const char hex[17] = "0123456789abcdef";
6710 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6711 isc_uint16_t buflen, i;
6712 unsigned char *p, *buf, *nsid;
6714 /* Allocate buffer for storing hex version of the NSID */
6715 buflen = (isc_uint16_t)nsid_len * 2 + 1;
6716 buf = isc_mem_get(mctx, buflen);
6720 /* Convert to hex */
6722 nsid = isc_buffer_current(opt);
6723 for (i = 0; i < nsid_len; i++) {
6724 *p++ = hex[(nsid[0] >> 4) & 0xf];
6725 *p++ = hex[nsid[0] & 0xf];
6730 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6732 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6733 DNS_LOGMODULE_RESOLVER, level,
6734 "received NSID '%s' from %s", buf, addrbuf);
6737 isc_mem_put(mctx, buf, buflen);
6742 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6743 isc_buffer_t buffer;
6746 isc_result_t result;
6748 if (! isc_log_wouldlog(dns_lctx, level))
6752 * Note that these are multiline debug messages. We want a newline
6753 * to appear in the log after each message.
6757 buf = isc_mem_get(mctx, len);
6760 isc_buffer_init(&buffer, buf, len);
6761 result = dns_message_totext(message, &dns_master_style_debug,
6763 if (result == ISC_R_NOSPACE) {
6764 isc_mem_put(mctx, buf, len);
6766 } else if (result == ISC_R_SUCCESS)
6767 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6768 DNS_LOGMODULE_RESOLVER, level,
6769 "received packet:\n%.*s",
6770 (int)isc_buffer_usedlength(&buffer),
6772 } while (result == ISC_R_NOSPACE);
6775 isc_mem_put(mctx, buf, len);
6778 static isc_boolean_t
6779 iscname(fetchctx_t *fctx) {
6780 isc_result_t result;
6782 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6783 &fctx->name, dns_rdatatype_cname, 0,
6785 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6788 static isc_boolean_t
6789 betterreferral(fetchctx_t *fctx) {
6790 isc_result_t result;
6792 dns_rdataset_t *rdataset;
6793 dns_message_t *message = fctx->rmessage;
6795 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6796 result == ISC_R_SUCCESS;
6797 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6799 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6800 if (!isstrictsubdomain(name, &fctx->domain))
6802 for (rdataset = ISC_LIST_HEAD(name->list);
6804 rdataset = ISC_LIST_NEXT(rdataset, link))
6805 if (rdataset->type == dns_rdatatype_ns)
6812 process_opt(resquery_t *query, dns_rdataset_t *opt) {
6814 isc_buffer_t optbuf;
6815 isc_result_t result;
6816 isc_uint16_t optcode;
6817 isc_uint16_t optlen;
6819 result = dns_rdataset_first(opt);
6820 if (result == ISC_R_SUCCESS) {
6821 dns_rdata_init(&rdata);
6822 dns_rdataset_current(opt, &rdata);
6823 isc_buffer_init(&optbuf, rdata.data, rdata.length);
6824 isc_buffer_add(&optbuf, rdata.length);
6825 while (isc_buffer_remaininglength(&optbuf) >= 4) {
6826 optcode = isc_buffer_getuint16(&optbuf);
6827 optlen = isc_buffer_getuint16(&optbuf);
6828 INSIST(optlen <= isc_buffer_remaininglength(&optbuf));
6831 if (query->options & DNS_FETCHOPT_WANTNSID)
6832 log_nsid(&optbuf, optlen, query,
6834 query->fctx->res->mctx);
6835 isc_buffer_forward(&optbuf, optlen);
6838 isc_buffer_forward(&optbuf, optlen);
6842 INSIST(isc_buffer_remaininglength(&optbuf) == 0U);
6847 resquery_response(isc_task_t *task, isc_event_t *event) {
6848 isc_result_t result = ISC_R_SUCCESS;
6849 resquery_t *query = event->ev_arg;
6850 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6851 isc_boolean_t keep_trying, get_nameservers, resend;
6852 isc_boolean_t truncated;
6853 dns_message_t *message;
6854 dns_rdataset_t *opt;
6857 dns_fixedname_t foundname;
6859 isc_time_t tnow, *finish;
6860 dns_adbaddrinfo_t *addrinfo;
6861 unsigned int options;
6862 unsigned int findoptions;
6863 isc_result_t broken_server;
6864 badnstype_t broken_type = badns_response;
6865 isc_boolean_t no_response;
6867 REQUIRE(VALID_QUERY(query));
6869 options = query->options;
6870 REQUIRE(VALID_FCTX(fctx));
6871 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6875 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6876 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6878 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6880 (void)isc_timer_touch(fctx->timer);
6882 keep_trying = ISC_FALSE;
6883 broken_server = ISC_R_SUCCESS;
6884 get_nameservers = ISC_FALSE;
6886 truncated = ISC_FALSE;
6888 no_response = ISC_FALSE;
6890 if (fctx->res->exiting) {
6891 result = ISC_R_SHUTTINGDOWN;
6896 fctx->timeout = ISC_FALSE;
6897 fctx->addrinfo = query->addrinfo;
6900 * XXXRTH We should really get the current time just once. We
6901 * need a routine to convert from an isc_time_t to an
6906 isc_stdtime_get(&now);
6909 * Did the dispatcher have a problem?
6911 if (devent->result != ISC_R_SUCCESS) {
6912 if (devent->result == ISC_R_EOF &&
6913 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6915 * The problem might be that they
6916 * don't understand EDNS0. Turn it
6917 * off and try again.
6919 options |= DNS_FETCHOPT_NOEDNS0;
6921 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6924 * There's no hope for this query.
6926 keep_trying = ISC_TRUE;
6929 * If this is a network error on an exclusive query
6930 * socket, mark the server as bad so that we won't try
6931 * it for this fetch again. Also adjust finish and
6932 * no_response so that we penalize this address in SRTT
6935 if (query->exclusivesocket &&
6936 (devent->result == ISC_R_HOSTUNREACH ||
6937 devent->result == ISC_R_NETUNREACH ||
6938 devent->result == ISC_R_CONNREFUSED ||
6939 devent->result == ISC_R_CANCELED)) {
6940 broken_server = devent->result;
6941 broken_type = badns_unreachable;
6943 no_response = ISC_TRUE;
6949 message = fctx->rmessage;
6951 if (query->tsig != NULL) {
6952 result = dns_message_setquerytsig(message, query->tsig);
6953 if (result != ISC_R_SUCCESS)
6957 if (query->tsigkey) {
6958 result = dns_message_settsigkey(message, query->tsigkey);
6959 if (result != ISC_R_SUCCESS)
6963 result = dns_message_parse(message, &devent->buffer, 0);
6964 if (result != ISC_R_SUCCESS) {
6966 case ISC_R_UNEXPECTEDEND:
6967 if (!message->question_ok ||
6968 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6969 (options & DNS_FETCHOPT_TCP) != 0) {
6971 * Either the message ended prematurely,
6972 * and/or wasn't marked as being truncated,
6973 * and/or this is a response to a query we
6974 * sent over TCP. In all of these cases,
6975 * something is wrong with the remote
6976 * server and we don't want to retry using
6979 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6982 * The problem might be that they
6983 * don't understand EDNS0. Turn it
6984 * off and try again.
6986 options |= DNS_FETCHOPT_NOEDNS0;
6989 &query->addrinfo->sockaddr);
6990 inc_stats(fctx->res,
6991 dns_resstatscounter_edns0fail);
6993 broken_server = result;
6994 keep_trying = ISC_TRUE;
6999 * We defer retrying via TCP for a bit so we can
7000 * check out this message further.
7002 truncated = ISC_TRUE;
7005 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7007 * The problem might be that they
7008 * don't understand EDNS0. Turn it
7009 * off and try again.
7011 options |= DNS_FETCHOPT_NOEDNS0;
7013 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7014 inc_stats(fctx->res,
7015 dns_resstatscounter_edns0fail);
7017 broken_server = DNS_R_UNEXPECTEDRCODE;
7018 keep_trying = ISC_TRUE;
7023 * Something bad has happened.
7031 * Log the incoming packet.
7033 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
7036 * Process receive opt record.
7038 opt = dns_message_getopt(message);
7040 process_opt(query, opt);
7043 * If the message is signed, check the signature. If not, this
7044 * returns success anyway.
7046 result = dns_message_checksig(message, fctx->res->view);
7047 if (result != ISC_R_SUCCESS)
7051 * The dispatcher should ensure we only get responses with QR set.
7053 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
7055 * INSIST() that the message comes from the place we sent it to,
7056 * since the dispatch code should ensure this.
7058 * INSIST() that the message id is correct (this should also be
7059 * ensured by the dispatch code).
7063 * We have an affirmative response to the query and we have
7064 * previously got a response from this server which indicated
7065 * EDNS may not be supported so we can now cache the lack of
7069 (message->rcode == dns_rcode_noerror ||
7070 message->rcode == dns_rcode_nxdomain ||
7071 message->rcode == dns_rcode_refused ||
7072 message->rcode == dns_rcode_yxdomain) &&
7073 bad_edns(fctx, &query->addrinfo->sockaddr)) {
7074 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7075 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7077 dns_adb_changeflags(fctx->adb, query->addrinfo,
7078 DNS_FETCHOPT_NOEDNS0,
7079 DNS_FETCHOPT_NOEDNS0);
7083 * Deal with truncated responses by retrying using TCP.
7085 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
7086 truncated = ISC_TRUE;
7089 inc_stats(fctx->res, dns_resstatscounter_truncated);
7090 if ((options & DNS_FETCHOPT_TCP) != 0) {
7091 broken_server = DNS_R_TRUNCATEDTCP;
7092 keep_trying = ISC_TRUE;
7094 options |= DNS_FETCHOPT_TCP;
7101 * Is it a query response?
7103 if (message->opcode != dns_opcode_query) {
7105 broken_server = DNS_R_UNEXPECTEDOPCODE;
7106 keep_trying = ISC_TRUE;
7111 * Update statistics about erroneous responses.
7113 if (message->rcode != dns_rcode_noerror) {
7114 switch (message->rcode) {
7115 case dns_rcode_nxdomain:
7116 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
7118 case dns_rcode_servfail:
7119 inc_stats(fctx->res, dns_resstatscounter_servfail);
7121 case dns_rcode_formerr:
7122 inc_stats(fctx->res, dns_resstatscounter_formerr);
7125 inc_stats(fctx->res, dns_resstatscounter_othererror);
7131 * Is the remote server broken, or does it dislike us?
7133 if (message->rcode != dns_rcode_noerror &&
7134 message->rcode != dns_rcode_nxdomain) {
7135 if (((message->rcode == dns_rcode_formerr ||
7136 message->rcode == dns_rcode_notimp) ||
7137 (message->rcode == dns_rcode_servfail &&
7138 dns_message_getopt(message) == NULL)) &&
7139 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7141 * It's very likely they don't like EDNS0.
7142 * If the response code is SERVFAIL, also check if the
7143 * response contains an OPT RR and don't cache the
7144 * failure since it can be returned for various other
7147 * XXXRTH We should check if the question
7148 * we're asking requires EDNS0, and
7149 * if so, we should bail out.
7151 options |= DNS_FETCHOPT_NOEDNS0;
7154 * Remember that they may not like EDNS0.
7156 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7157 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
7158 } else if (message->rcode == dns_rcode_formerr) {
7159 if (ISFORWARDER(query->addrinfo)) {
7161 * This forwarder doesn't understand us,
7162 * but other forwarders might. Keep trying.
7164 broken_server = DNS_R_REMOTEFORMERR;
7165 keep_trying = ISC_TRUE;
7168 * The server doesn't understand us. Since
7169 * all servers for a zone need similar
7170 * capabilities, we assume that we will get
7171 * FORMERR from all servers, and thus we
7172 * cannot make any more progress with this
7175 log_formerr(fctx, "server sent FORMERR");
7176 result = DNS_R_FORMERR;
7178 } else if (message->rcode == dns_rcode_yxdomain) {
7180 * DNAME mapping failed because the new name
7181 * was too long. There's no chance of success
7184 result = DNS_R_YXDOMAIN;
7185 } else if (message->rcode == dns_rcode_badvers) {
7186 unsigned int flags, mask;
7187 unsigned int version;
7190 INSIST(opt != NULL);
7191 version = (opt->ttl >> 16) & 0xff;
7192 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
7193 DNS_FETCHOPT_EDNSVERSIONSET;
7194 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
7195 DNS_FETCHOPT_EDNSVERSIONSET;
7198 dns_adb_changeflags(fctx->adb, query->addrinfo,
7202 broken_server = DNS_R_BADVERS;
7203 keep_trying = ISC_TRUE;
7210 broken_server = DNS_R_UNEXPECTEDRCODE;
7211 INSIST(broken_server != ISC_R_SUCCESS);
7212 keep_trying = ISC_TRUE;
7218 * Is the question the same as the one we asked?
7220 result = same_question(fctx);
7221 if (result != ISC_R_SUCCESS) {
7223 if (result == DNS_R_FORMERR)
7224 keep_trying = ISC_TRUE;
7229 * Is the server lame?
7231 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
7233 inc_stats(fctx->res, dns_resstatscounter_lame);
7234 log_lame(fctx, query->addrinfo);
7235 result = dns_adb_marklame(fctx->adb, query->addrinfo,
7236 &fctx->name, fctx->type,
7237 now + fctx->res->lame_ttl);
7238 if (result != ISC_R_SUCCESS)
7239 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7240 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
7241 "could not mark server as lame: %s",
7242 isc_result_totext(result));
7243 broken_server = DNS_R_LAME;
7244 keep_trying = ISC_TRUE;
7249 * Enforce delegations only zones like NET and COM.
7251 if (!ISFORWARDER(query->addrinfo) &&
7252 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
7253 !dns_name_equal(&fctx->domain, &fctx->name) &&
7254 fix_mustbedelegationornxdomain(message, fctx)) {
7255 char namebuf[DNS_NAME_FORMATSIZE];
7256 char domainbuf[DNS_NAME_FORMATSIZE];
7257 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7261 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
7262 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7263 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
7264 dns_rdataclass_format(fctx->res->rdclass, classbuf,
7266 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7269 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
7270 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7271 "enforced delegation-only for '%s' (%s/%s/%s) "
7273 domainbuf, namebuf, typebuf, classbuf, addrbuf);
7276 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
7277 checknames(message);
7282 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
7285 * Did we get any answers?
7287 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
7288 (message->rcode == dns_rcode_noerror ||
7289 message->rcode == dns_rcode_nxdomain)) {
7292 * We've got answers. If it has an authoritative answer or an
7293 * answer from a forwarder, we're done.
7295 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
7296 ISFORWARDER(query->addrinfo))
7297 result = answer_response(fctx);
7298 else if (iscname(fctx) &&
7299 fctx->type != dns_rdatatype_any &&
7300 fctx->type != dns_rdatatype_cname) {
7302 * A BIND8 server could return a non-authoritative
7303 * answer when a CNAME is followed. We should treat
7304 * it as a valid answer.
7306 result = answer_response(fctx);
7307 } else if (fctx->type != dns_rdatatype_ns &&
7308 !betterreferral(fctx)) {
7310 * Lame response !!!.
7312 result = answer_response(fctx);
7314 if (fctx->type == dns_rdatatype_ns) {
7316 * A BIND 8 server could incorrectly return a
7317 * non-authoritative answer to an NS query
7318 * instead of a referral. Since this answer
7319 * lacks the SIGs necessary to do DNSSEC
7320 * validation, we must invoke the following
7321 * special kludge to treat it as a referral.
7323 result = noanswer_response(fctx, NULL,
7324 LOOK_FOR_NS_IN_ANSWER);
7327 * Some other servers may still somehow include
7328 * an answer when it should return a referral
7329 * with an empty answer. Check to see if we can
7330 * treat this as a referral by ignoring the
7331 * answer. Further more, there may be an
7332 * implementation that moves A/AAAA glue records
7333 * to the answer section for that type of
7334 * delegation when the query is for that glue
7335 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
7336 * such a corner case.
7338 result = noanswer_response(fctx, NULL,
7339 LOOK_FOR_GLUE_IN_ANSWER);
7341 if (result != DNS_R_DELEGATION) {
7343 * At this point, AA is not set, the response
7344 * is not a referral, and the server is not a
7345 * forwarder. It is technically lame and it's
7346 * easier to treat it as such than to figure out
7347 * some more elaborate course of action.
7349 broken_server = DNS_R_LAME;
7350 keep_trying = ISC_TRUE;
7353 goto force_referral;
7355 if (result != ISC_R_SUCCESS) {
7356 if (result == DNS_R_FORMERR)
7357 keep_trying = ISC_TRUE;
7360 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
7361 message->rcode == dns_rcode_noerror ||
7362 message->rcode == dns_rcode_nxdomain) {
7364 * NXDOMAIN, NXRDATASET, or referral.
7366 result = noanswer_response(fctx, NULL, 0);
7369 case DNS_R_CHASEDSSERVERS:
7371 case DNS_R_DELEGATION:
7374 * We don't have the answer, but we know a better
7377 get_nameservers = ISC_TRUE;
7378 keep_trying = ISC_TRUE;
7380 * We have a new set of name servers, and it
7381 * has not experienced any restarts yet.
7386 * Update local statistics counters collected for each
7390 fctx->querysent = 0;
7391 fctx->lamecount = 0;
7396 result = ISC_R_SUCCESS;
7400 * Something has gone wrong.
7402 if (result == DNS_R_FORMERR)
7403 keep_trying = ISC_TRUE;
7408 * The server is insane.
7411 broken_server = DNS_R_UNEXPECTEDRCODE;
7412 keep_trying = ISC_TRUE;
7417 * Follow additional section data chains.
7419 chase_additional(fctx);
7422 * Cache the cacheable parts of the message. This may also cause
7423 * work to be queued to the DNSSEC validator.
7425 if (WANTCACHE(fctx)) {
7426 result = cache_message(fctx, query->addrinfo, now);
7427 if (result != ISC_R_SUCCESS)
7432 * Ncache the negatively cacheable parts of the message. This may
7433 * also cause work to be queued to the DNSSEC validator.
7435 if (WANTNCACHE(fctx)) {
7436 dns_rdatatype_t covers;
7437 if (message->rcode == dns_rcode_nxdomain)
7438 covers = dns_rdatatype_any;
7440 covers = fctx->type;
7443 * Cache any negative cache entries in the message.
7445 result = ncache_message(fctx, query->addrinfo, covers, now);
7450 * Remember the query's addrinfo, in case we need to mark the
7453 addrinfo = query->addrinfo;
7458 * XXXRTH Don't cancel the query if waiting for validation?
7460 fctx_cancelquery(&query, &devent, finish, no_response);
7463 if (result == DNS_R_FORMERR)
7464 broken_server = DNS_R_FORMERR;
7465 if (broken_server != ISC_R_SUCCESS) {
7467 * Add this server to the list of bad servers for
7470 add_bad(fctx, addrinfo, broken_server, broken_type);
7473 if (get_nameservers) {
7475 dns_fixedname_init(&foundname);
7476 fname = dns_fixedname_name(&foundname);
7477 if (result != ISC_R_SUCCESS) {
7478 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7482 if (dns_rdatatype_atparent(fctx->type))
7483 findoptions |= DNS_DBFIND_NOEXACT;
7484 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7487 name = &fctx->domain;
7488 result = dns_view_findzonecut(fctx->res->view,
7494 if (result != ISC_R_SUCCESS) {
7495 FCTXTRACE("couldn't find a zonecut");
7496 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7499 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7501 * The best nameservers are now above our
7504 FCTXTRACE("nameservers now above QDOMAIN");
7505 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7508 dns_name_free(&fctx->domain, fctx->mctx);
7509 dns_name_init(&fctx->domain, NULL);
7510 result = dns_name_dup(fname, fctx->mctx, &fctx->domain);
7511 if (result != ISC_R_SUCCESS) {
7512 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7515 fctx->ns_ttl = fctx->nameservers.ttl;
7516 fctx->ns_ttl_ok = ISC_TRUE;
7517 fctx_cancelqueries(fctx, ISC_TRUE);
7518 fctx_cleanupfinds(fctx);
7519 fctx_cleanupaltfinds(fctx);
7520 fctx_cleanupforwaddrs(fctx);
7521 fctx_cleanupaltaddrs(fctx);
7526 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7527 } else if (resend) {
7529 * Resend (probably with changed options).
7531 FCTXTRACE("resend");
7532 inc_stats(fctx->res, dns_resstatscounter_retry);
7533 result = fctx_query(fctx, addrinfo, options);
7534 if (result != ISC_R_SUCCESS)
7535 fctx_done(fctx, result, __LINE__);
7536 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7538 * All has gone well so far, but we are waiting for the
7539 * DNSSEC validator to validate the answer.
7541 FCTXTRACE("wait for validator");
7542 fctx_cancelqueries(fctx, ISC_TRUE);
7544 * We must not retransmit while the validator is working;
7545 * it has references to the current rmessage.
7547 result = fctx_stopidletimer(fctx);
7548 if (result != ISC_R_SUCCESS)
7549 fctx_done(fctx, result, __LINE__);
7550 } else if (result == DNS_R_CHASEDSSERVERS) {
7552 add_bad(fctx, addrinfo, result, broken_type);
7553 fctx_cancelqueries(fctx, ISC_TRUE);
7554 fctx_cleanupfinds(fctx);
7555 fctx_cleanupforwaddrs(fctx);
7557 n = dns_name_countlabels(&fctx->name);
7558 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7560 FCTXTRACE("suspending DS lookup to find parent's NS records");
7562 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7564 NULL, NULL, NULL, 0, task,
7565 resume_dslookup, fctx,
7566 &fctx->nsrrset, NULL,
7568 if (result != ISC_R_SUCCESS)
7569 fctx_done(fctx, result, __LINE__);
7571 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7573 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7574 result = fctx_stopidletimer(fctx);
7575 if (result != ISC_R_SUCCESS)
7576 fctx_done(fctx, result, __LINE__);
7582 fctx_done(fctx, result, __LINE__);
7588 *** Resolver Methods
7591 destroy_badcache(dns_resolver_t *res) {
7592 dns_badcache_t *bad, *next;
7595 if (res->badcache != NULL) {
7596 for (i = 0; i < res->badhash; i++)
7597 for (bad = res->badcache[i]; bad != NULL;
7600 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7604 isc_mem_put(res->mctx, res->badcache,
7605 sizeof(*res->badcache) * res->badhash);
7606 res->badcache = NULL;
7608 INSIST(res->badcount == 0);
7613 destroy(dns_resolver_t *res) {
7617 REQUIRE(res->references == 0);
7618 REQUIRE(!res->priming);
7619 REQUIRE(res->primefetch == NULL);
7623 INSIST(res->nfctx == 0);
7625 DESTROYLOCK(&res->primelock);
7626 DESTROYLOCK(&res->nlock);
7627 DESTROYLOCK(&res->lock);
7628 for (i = 0; i < res->nbuckets; i++) {
7629 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7630 isc_task_shutdown(res->buckets[i].task);
7631 isc_task_detach(&res->buckets[i].task);
7632 DESTROYLOCK(&res->buckets[i].lock);
7633 isc_mem_detach(&res->buckets[i].mctx);
7635 isc_mem_put(res->mctx, res->buckets,
7636 res->nbuckets * sizeof(fctxbucket_t));
7637 if (res->dispatchv4 != NULL)
7638 dns_dispatch_detach(&res->dispatchv4);
7639 if (res->dispatchv6 != NULL)
7640 dns_dispatch_detach(&res->dispatchv6);
7641 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7642 ISC_LIST_UNLINK(res->alternates, a, link);
7644 dns_name_free(&a->_u._n.name, res->mctx);
7645 isc_mem_put(res->mctx, a, sizeof(*a));
7647 dns_resolver_reset_algorithms(res);
7648 destroy_badcache(res);
7649 dns_resolver_resetmustbesecure(res);
7651 isc_rwlock_destroy(&res->alglock);
7654 isc_rwlock_destroy(&res->mbslock);
7656 isc_timer_detach(&res->spillattimer);
7658 isc_mem_put(res->mctx, res, sizeof(*res));
7662 send_shutdown_events(dns_resolver_t *res) {
7663 isc_event_t *event, *next_event;
7667 * Caller must be holding the resolver lock.
7670 for (event = ISC_LIST_HEAD(res->whenshutdown);
7672 event = next_event) {
7673 next_event = ISC_LIST_NEXT(event, ev_link);
7674 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7675 etask = event->ev_sender;
7676 event->ev_sender = res;
7677 isc_task_sendanddetach(&etask, &event);
7682 empty_bucket(dns_resolver_t *res) {
7683 RTRACE("empty_bucket");
7687 INSIST(res->activebuckets > 0);
7688 res->activebuckets--;
7689 if (res->activebuckets == 0)
7690 send_shutdown_events(res);
7696 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7697 dns_resolver_t *res = event->ev_arg;
7698 isc_result_t result;
7700 isc_boolean_t logit = ISC_FALSE;
7702 REQUIRE(VALID_RESOLVER(res));
7707 INSIST(!res->exiting);
7708 if (res->spillat > res->spillatmin) {
7712 if (res->spillat <= res->spillatmin) {
7713 result = isc_timer_reset(res->spillattimer,
7714 isc_timertype_inactive, NULL,
7716 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7718 count = res->spillat;
7721 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7722 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7723 "clients-per-query decreased to %u", count);
7725 isc_event_free(&event);
7729 dns_resolver_create(dns_view_t *view,
7730 isc_taskmgr_t *taskmgr, unsigned int ntasks,
7731 isc_socketmgr_t *socketmgr,
7732 isc_timermgr_t *timermgr,
7733 unsigned int options,
7734 dns_dispatchmgr_t *dispatchmgr,
7735 dns_dispatch_t *dispatchv4,
7736 dns_dispatch_t *dispatchv6,
7737 dns_resolver_t **resp)
7739 dns_resolver_t *res;
7740 isc_result_t result = ISC_R_SUCCESS;
7741 unsigned int i, buckets_created = 0;
7742 isc_task_t *task = NULL;
7747 * Create a resolver.
7750 REQUIRE(DNS_VIEW_VALID(view));
7751 REQUIRE(ntasks > 0);
7752 REQUIRE(resp != NULL && *resp == NULL);
7753 REQUIRE(dispatchmgr != NULL);
7754 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7756 res = isc_mem_get(view->mctx, sizeof(*res));
7758 return (ISC_R_NOMEMORY);
7760 res->mctx = view->mctx;
7761 res->rdclass = view->rdclass;
7762 res->socketmgr = socketmgr;
7763 res->timermgr = timermgr;
7764 res->taskmgr = taskmgr;
7765 res->dispatchmgr = dispatchmgr;
7767 res->options = options;
7769 ISC_LIST_INIT(res->alternates);
7770 res->udpsize = RECV_BUFFER_SIZE;
7771 res->algorithms = NULL;
7772 res->badcache = NULL;
7776 res->mustbesecure = NULL;
7777 res->spillatmin = res->spillat = 10;
7778 res->spillatmax = 100;
7779 res->spillattimer = NULL;
7780 res->zero_no_soa_ttl = ISC_FALSE;
7781 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7783 res->nextdisp = 0; /* meaningless at this point, but init it */
7784 res->maxdepth = DEFAULT_RECURSION_DEPTH;
7785 res->maxqueries = DEFAULT_MAX_QUERIES;
7786 res->nbuckets = ntasks;
7787 res->activebuckets = ntasks;
7788 res->buckets = isc_mem_get(view->mctx,
7789 ntasks * sizeof(fctxbucket_t));
7790 if (res->buckets == NULL) {
7791 result = ISC_R_NOMEMORY;
7794 for (i = 0; i < ntasks; i++) {
7795 result = isc_mutex_init(&res->buckets[i].lock);
7796 if (result != ISC_R_SUCCESS)
7797 goto cleanup_buckets;
7798 res->buckets[i].task = NULL;
7799 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7800 if (result != ISC_R_SUCCESS) {
7801 DESTROYLOCK(&res->buckets[i].lock);
7802 goto cleanup_buckets;
7804 res->buckets[i].mctx = NULL;
7805 snprintf(name, sizeof(name), "res%u", i);
7806 #ifdef ISC_PLATFORM_USETHREADS
7808 * Use a separate memory context for each bucket to reduce
7809 * contention among multiple threads. Do this only when
7810 * enabling threads because it will be require more memory.
7812 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7813 if (result != ISC_R_SUCCESS) {
7814 isc_task_detach(&res->buckets[i].task);
7815 DESTROYLOCK(&res->buckets[i].lock);
7816 goto cleanup_buckets;
7818 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7820 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7822 isc_task_setname(res->buckets[i].task, name, res);
7823 ISC_LIST_INIT(res->buckets[i].fctxs);
7824 res->buckets[i].exiting = ISC_FALSE;
7828 res->dispatchv4 = NULL;
7829 if (dispatchv4 != NULL) {
7830 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
7831 dispattr = dns_dispatch_getattributes(dispatchv4);
7833 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7836 res->dispatchv6 = NULL;
7837 if (dispatchv6 != NULL) {
7838 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
7839 dispattr = dns_dispatch_getattributes(dispatchv6);
7841 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7844 res->references = 1;
7845 res->exiting = ISC_FALSE;
7846 res->frozen = ISC_FALSE;
7847 ISC_LIST_INIT(res->whenshutdown);
7848 res->priming = ISC_FALSE;
7849 res->primefetch = NULL;
7852 result = isc_mutex_init(&res->lock);
7853 if (result != ISC_R_SUCCESS)
7854 goto cleanup_dispatches;
7856 result = isc_mutex_init(&res->nlock);
7857 if (result != ISC_R_SUCCESS)
7860 result = isc_mutex_init(&res->primelock);
7861 if (result != ISC_R_SUCCESS)
7865 result = isc_task_create(taskmgr, 0, &task);
7866 if (result != ISC_R_SUCCESS)
7867 goto cleanup_primelock;
7869 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7870 task, spillattimer_countdown, res,
7871 &res->spillattimer);
7872 isc_task_detach(&task);
7873 if (result != ISC_R_SUCCESS)
7874 goto cleanup_primelock;
7877 result = isc_rwlock_init(&res->alglock, 0, 0);
7878 if (result != ISC_R_SUCCESS)
7879 goto cleanup_spillattimer;
7882 result = isc_rwlock_init(&res->mbslock, 0, 0);
7883 if (result != ISC_R_SUCCESS)
7884 goto cleanup_alglock;
7887 res->magic = RES_MAGIC;
7891 return (ISC_R_SUCCESS);
7896 isc_rwlock_destroy(&res->alglock);
7899 #if USE_ALGLOCK || USE_MBSLOCK
7900 cleanup_spillattimer:
7901 isc_timer_detach(&res->spillattimer);
7905 DESTROYLOCK(&res->primelock);
7908 DESTROYLOCK(&res->nlock);
7911 DESTROYLOCK(&res->lock);
7914 if (res->dispatchv6 != NULL)
7915 dns_dispatch_detach(&res->dispatchv6);
7916 if (res->dispatchv4 != NULL)
7917 dns_dispatch_detach(&res->dispatchv4);
7920 for (i = 0; i < buckets_created; i++) {
7921 isc_mem_detach(&res->buckets[i].mctx);
7922 DESTROYLOCK(&res->buckets[i].lock);
7923 isc_task_shutdown(res->buckets[i].task);
7924 isc_task_detach(&res->buckets[i].task);
7926 isc_mem_put(view->mctx, res->buckets,
7927 res->nbuckets * sizeof(fctxbucket_t));
7930 isc_mem_put(view->mctx, res, sizeof(*res));
7937 prime_done(isc_task_t *task, isc_event_t *event) {
7938 dns_resolver_t *res;
7939 dns_fetchevent_t *fevent;
7941 dns_db_t *db = NULL;
7943 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7944 fevent = (dns_fetchevent_t *)event;
7945 res = event->ev_arg;
7946 REQUIRE(VALID_RESOLVER(res));
7952 INSIST(res->priming);
7953 res->priming = ISC_FALSE;
7954 LOCK(&res->primelock);
7955 fetch = res->primefetch;
7956 res->primefetch = NULL;
7957 UNLOCK(&res->primelock);
7961 if (fevent->result == ISC_R_SUCCESS &&
7962 res->view->cache != NULL && res->view->hints != NULL) {
7963 dns_cache_attachdb(res->view->cache, &db);
7964 dns_root_checkhints(res->view, res->view->hints, db);
7968 if (fevent->node != NULL)
7969 dns_db_detachnode(fevent->db, &fevent->node);
7970 if (fevent->db != NULL)
7971 dns_db_detach(&fevent->db);
7972 if (dns_rdataset_isassociated(fevent->rdataset))
7973 dns_rdataset_disassociate(fevent->rdataset);
7974 INSIST(fevent->sigrdataset == NULL);
7976 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7978 isc_event_free(&event);
7979 dns_resolver_destroyfetch(&fetch);
7983 dns_resolver_prime(dns_resolver_t *res) {
7984 isc_boolean_t want_priming = ISC_FALSE;
7985 dns_rdataset_t *rdataset;
7986 isc_result_t result;
7988 REQUIRE(VALID_RESOLVER(res));
7989 REQUIRE(res->frozen);
7991 RTRACE("dns_resolver_prime");
7995 if (!res->exiting && !res->priming) {
7996 INSIST(res->primefetch == NULL);
7997 res->priming = ISC_TRUE;
7998 want_priming = ISC_TRUE;
8005 * To avoid any possible recursive locking problems, we
8006 * start the priming fetch like any other fetch, and holding
8007 * no resolver locks. No one else will try to start it
8008 * because we're the ones who set res->priming to true.
8009 * Any other callers of dns_resolver_prime() while we're
8010 * running will see that res->priming is already true and
8014 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
8015 if (rdataset == NULL) {
8017 INSIST(res->priming);
8018 INSIST(res->primefetch == NULL);
8019 res->priming = ISC_FALSE;
8023 dns_rdataset_init(rdataset);
8024 LOCK(&res->primelock);
8025 result = dns_resolver_createfetch(res, dns_rootname,
8027 NULL, NULL, NULL, 0,
8028 res->buckets[0].task,
8030 res, rdataset, NULL,
8032 UNLOCK(&res->primelock);
8033 if (result != ISC_R_SUCCESS) {
8035 INSIST(res->priming);
8036 res->priming = ISC_FALSE;
8044 dns_resolver_freeze(dns_resolver_t *res) {
8049 REQUIRE(VALID_RESOLVER(res));
8051 res->frozen = ISC_TRUE;
8055 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
8056 REQUIRE(VALID_RESOLVER(source));
8057 REQUIRE(targetp != NULL && *targetp == NULL);
8059 RRTRACE(source, "attach");
8060 LOCK(&source->lock);
8061 REQUIRE(!source->exiting);
8063 INSIST(source->references > 0);
8064 source->references++;
8065 INSIST(source->references != 0);
8066 UNLOCK(&source->lock);
8072 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
8073 isc_event_t **eventp)
8078 REQUIRE(VALID_RESOLVER(res));
8079 REQUIRE(eventp != NULL);
8086 if (res->exiting && res->activebuckets == 0) {
8088 * We're already shutdown. Send the event.
8090 event->ev_sender = res;
8091 isc_task_send(task, &event);
8094 isc_task_attach(task, &clone);
8095 event->ev_sender = clone;
8096 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
8103 dns_resolver_shutdown(dns_resolver_t *res) {
8107 isc_result_t result;
8109 REQUIRE(VALID_RESOLVER(res));
8115 if (!res->exiting) {
8117 res->exiting = ISC_TRUE;
8119 for (i = 0; i < res->nbuckets; i++) {
8120 LOCK(&res->buckets[i].lock);
8121 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
8123 fctx = ISC_LIST_NEXT(fctx, link))
8124 fctx_shutdown(fctx);
8125 if (res->dispatchv4 != NULL && !res->exclusivev4) {
8126 sock = dns_dispatch_getsocket(res->dispatchv4);
8127 isc_socket_cancel(sock, res->buckets[i].task,
8128 ISC_SOCKCANCEL_ALL);
8130 if (res->dispatchv6 != NULL && !res->exclusivev6) {
8131 sock = dns_dispatch_getsocket(res->dispatchv6);
8132 isc_socket_cancel(sock, res->buckets[i].task,
8133 ISC_SOCKCANCEL_ALL);
8135 res->buckets[i].exiting = ISC_TRUE;
8136 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
8137 INSIST(res->activebuckets > 0);
8138 res->activebuckets--;
8140 UNLOCK(&res->buckets[i].lock);
8142 if (res->activebuckets == 0)
8143 send_shutdown_events(res);
8144 result = isc_timer_reset(res->spillattimer,
8145 isc_timertype_inactive, NULL,
8147 RUNTIME_CHECK(result == ISC_R_SUCCESS);
8154 dns_resolver_detach(dns_resolver_t **resp) {
8155 dns_resolver_t *res;
8156 isc_boolean_t need_destroy = ISC_FALSE;
8158 REQUIRE(resp != NULL);
8160 REQUIRE(VALID_RESOLVER(res));
8166 INSIST(res->references > 0);
8168 if (res->references == 0) {
8169 INSIST(res->exiting && res->activebuckets == 0);
8170 need_destroy = ISC_TRUE;
8181 static inline isc_boolean_t
8182 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
8183 unsigned int options)
8186 * Don't match fetch contexts that are shutting down.
8188 if (fctx->cloned || fctx->state == fetchstate_done ||
8189 ISC_LIST_EMPTY(fctx->events))
8192 if (fctx->type != type || fctx->options != options)
8194 return (dns_name_equal(&fctx->name, name));
8198 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
8199 char namebuf[DNS_NAME_FORMATSIZE];
8200 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8201 int level = ISC_LOG_DEBUG(1);
8203 if (! isc_log_wouldlog(dns_lctx, level))
8206 dns_name_format(name, namebuf, sizeof(namebuf));
8207 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
8209 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
8210 DNS_LOGMODULE_RESOLVER, level,
8211 "createfetch: %s %s", namebuf, typebuf);
8215 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
8216 dns_rdatatype_t type,
8217 dns_name_t *domain, dns_rdataset_t *nameservers,
8218 dns_forwarders_t *forwarders,
8219 unsigned int options, isc_task_t *task,
8220 isc_taskaction_t action, void *arg,
8221 dns_rdataset_t *rdataset,
8222 dns_rdataset_t *sigrdataset,
8223 dns_fetch_t **fetchp)
8225 return (dns_resolver_createfetch3(res, name, type, domain,
8226 nameservers, forwarders, NULL, 0,
8227 options, 0, NULL, task, action, arg,
8228 rdataset, sigrdataset, fetchp));
8232 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
8233 dns_rdatatype_t type,
8234 dns_name_t *domain, dns_rdataset_t *nameservers,
8235 dns_forwarders_t *forwarders,
8236 isc_sockaddr_t *client, dns_messageid_t id,
8237 unsigned int options, isc_task_t *task,
8238 isc_taskaction_t action, void *arg,
8239 dns_rdataset_t *rdataset,
8240 dns_rdataset_t *sigrdataset,
8241 dns_fetch_t **fetchp)
8243 return (dns_resolver_createfetch3(res, name, type, domain,
8244 nameservers, forwarders, client, id,
8245 options, 0, NULL, task, action, arg,
8246 rdataset, sigrdataset, fetchp));
8250 dns_resolver_createfetch3(dns_resolver_t *res, dns_name_t *name,
8251 dns_rdatatype_t type,
8252 dns_name_t *domain, dns_rdataset_t *nameservers,
8253 dns_forwarders_t *forwarders,
8254 isc_sockaddr_t *client, dns_messageid_t id,
8255 unsigned int options, unsigned int depth,
8256 isc_counter_t *qc, isc_task_t *task,
8257 isc_taskaction_t action, void *arg,
8258 dns_rdataset_t *rdataset,
8259 dns_rdataset_t *sigrdataset,
8260 dns_fetch_t **fetchp)
8263 fetchctx_t *fctx = NULL;
8264 isc_result_t result = ISC_R_SUCCESS;
8265 unsigned int bucketnum;
8266 isc_boolean_t new_fctx = ISC_FALSE;
8268 unsigned int count = 0;
8269 unsigned int spillat;
8270 unsigned int spillatmin;
8271 isc_boolean_t destroy = ISC_FALSE;
8275 REQUIRE(VALID_RESOLVER(res));
8276 REQUIRE(res->frozen);
8277 /* XXXRTH Check for meta type */
8278 if (domain != NULL) {
8279 REQUIRE(DNS_RDATASET_VALID(nameservers));
8280 REQUIRE(nameservers->type == dns_rdatatype_ns);
8282 REQUIRE(nameservers == NULL);
8283 REQUIRE(forwarders == NULL);
8284 REQUIRE(!dns_rdataset_isassociated(rdataset));
8285 REQUIRE(sigrdataset == NULL ||
8286 !dns_rdataset_isassociated(sigrdataset));
8287 REQUIRE(fetchp != NULL && *fetchp == NULL);
8289 log_fetch(name, type);
8292 * XXXRTH use a mempool?
8294 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
8296 return (ISC_R_NOMEMORY);
8298 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
8301 spillat = res->spillat;
8302 spillatmin = res->spillatmin;
8304 LOCK(&res->buckets[bucketnum].lock);
8306 if (res->buckets[bucketnum].exiting) {
8307 result = ISC_R_SHUTTINGDOWN;
8311 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
8312 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
8314 fctx = ISC_LIST_NEXT(fctx, link)) {
8315 if (fctx_match(fctx, name, type, options))
8321 * Is this a duplicate?
8323 if (fctx != NULL && client != NULL) {
8324 dns_fetchevent_t *fevent;
8325 for (fevent = ISC_LIST_HEAD(fctx->events);
8327 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
8328 if (fevent->client != NULL && fevent->id == id &&
8329 isc_sockaddr_equal(fevent->client, client)) {
8330 result = DNS_R_DUPLICATE;
8336 if (count >= spillatmin && spillatmin != 0) {
8337 INSIST(fctx != NULL);
8338 if (count >= spillat)
8339 fctx->spilled = ISC_TRUE;
8340 if (fctx->spilled) {
8341 result = DNS_R_DROP;
8347 result = fctx_create(res, name, type, domain, nameservers,
8348 options, bucketnum, depth, qc, &fctx);
8349 if (result != ISC_R_SUCCESS)
8351 new_fctx = ISC_TRUE;
8352 } else if (fctx->depth > depth)
8353 fctx->depth = depth;
8355 result = fctx_join(fctx, task, client, id, action, arg,
8356 rdataset, sigrdataset, fetch);
8358 if (result == ISC_R_SUCCESS) {
8362 event = &fctx->control_event;
8363 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
8364 DNS_EVENT_FETCHCONTROL,
8365 fctx_start, fctx, NULL,
8367 isc_task_send(res->buckets[bucketnum].task, &event);
8370 * We don't care about the result of fctx_unlink()
8371 * since we know we're not exiting.
8373 (void)fctx_unlink(fctx);
8379 UNLOCK(&res->buckets[bucketnum].lock);
8384 if (result == ISC_R_SUCCESS) {
8388 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8394 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
8396 dns_resolver_t *res;
8397 dns_fetchevent_t *event, *next_event;
8400 REQUIRE(DNS_FETCH_VALID(fetch));
8401 fctx = fetch->private;
8402 REQUIRE(VALID_FCTX(fctx));
8405 FTRACE("cancelfetch");
8407 LOCK(&res->buckets[fctx->bucketnum].lock);
8410 * Find the completion event for this fetch (as opposed
8411 * to those for other fetches that have joined the same
8412 * fctx) and send it with result = ISC_R_CANCELED.
8415 if (fctx->state != fetchstate_done) {
8416 for (event = ISC_LIST_HEAD(fctx->events);
8418 event = next_event) {
8419 next_event = ISC_LIST_NEXT(event, ev_link);
8420 if (event->fetch == fetch) {
8421 ISC_LIST_UNLINK(fctx->events, event, ev_link);
8426 if (event != NULL) {
8427 etask = event->ev_sender;
8428 event->ev_sender = fctx;
8429 event->result = ISC_R_CANCELED;
8430 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8433 * The fctx continues running even if no fetches remain;
8434 * the answer is still cached.
8437 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8441 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8443 dns_resolver_t *res;
8444 dns_fetchevent_t *event, *next_event;
8446 unsigned int bucketnum;
8447 isc_boolean_t bucket_empty;
8449 REQUIRE(fetchp != NULL);
8451 REQUIRE(DNS_FETCH_VALID(fetch));
8452 fctx = fetch->private;
8453 REQUIRE(VALID_FCTX(fctx));
8456 FTRACE("destroyfetch");
8458 bucketnum = fctx->bucketnum;
8459 LOCK(&res->buckets[bucketnum].lock);
8462 * Sanity check: the caller should have gotten its event before
8463 * trying to destroy the fetch.
8466 if (fctx->state != fetchstate_done) {
8467 for (event = ISC_LIST_HEAD(fctx->events);
8469 event = next_event) {
8470 next_event = ISC_LIST_NEXT(event, ev_link);
8471 RUNTIME_CHECK(event->fetch != fetch);
8475 bucket_empty = fctx_decreference(fctx);
8477 UNLOCK(&res->buckets[bucketnum].lock);
8479 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8487 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8488 isc_logcategory_t *category, isc_logmodule_t *module,
8489 int level, isc_boolean_t duplicateok)
8492 dns_resolver_t *res;
8493 char domainbuf[DNS_NAME_FORMATSIZE];
8495 REQUIRE(DNS_FETCH_VALID(fetch));
8496 fctx = fetch->private;
8497 REQUIRE(VALID_FCTX(fctx));
8500 LOCK(&res->buckets[fctx->bucketnum].lock);
8502 INSIST(fctx->exitline >= 0);
8503 if (!fctx->logged || duplicateok) {
8504 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8505 isc_log_write(lctx, category, module, level,
8506 "fetch completed at %s:%d for %s in "
8507 "%" ISC_PRINT_QUADFORMAT "u."
8508 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8509 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8510 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8511 "adberr:%u,findfail:%u,valfail:%u]",
8512 __FILE__, fctx->exitline, fctx->info,
8513 fctx->duration / US_PER_SEC,
8514 fctx->duration % US_PER_SEC,
8515 isc_result_totext(fctx->result),
8516 isc_result_totext(fctx->vresult), domainbuf,
8517 fctx->referrals, fctx->restarts,
8518 fctx->querysent, fctx->timeouts, fctx->lamecount,
8519 fctx->neterr, fctx->badresp, fctx->adberr,
8520 fctx->findfail, fctx->valfail);
8521 fctx->logged = ISC_TRUE;
8524 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8528 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8529 REQUIRE(VALID_RESOLVER(resolver));
8530 return (resolver->dispatchmgr);
8534 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8535 REQUIRE(VALID_RESOLVER(resolver));
8536 return (resolver->dispatchv4);
8540 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8541 REQUIRE(VALID_RESOLVER(resolver));
8542 return (resolver->dispatchv6);
8546 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8547 REQUIRE(VALID_RESOLVER(resolver));
8548 return (resolver->socketmgr);
8552 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8553 REQUIRE(VALID_RESOLVER(resolver));
8554 return (resolver->taskmgr);
8558 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8559 REQUIRE(VALID_RESOLVER(resolver));
8560 return (resolver->lame_ttl);
8564 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8565 REQUIRE(VALID_RESOLVER(resolver));
8566 resolver->lame_ttl = lame_ttl;
8570 dns_resolver_nrunning(dns_resolver_t *resolver) {
8572 LOCK(&resolver->nlock);
8573 n = resolver->nfctx;
8574 UNLOCK(&resolver->nlock);
8579 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8580 dns_name_t *name, in_port_t port) {
8582 isc_result_t result;
8584 REQUIRE(VALID_RESOLVER(resolver));
8585 REQUIRE(!resolver->frozen);
8586 REQUIRE((alt == NULL) ^ (name == NULL));
8588 a = isc_mem_get(resolver->mctx, sizeof(*a));
8590 return (ISC_R_NOMEMORY);
8592 a->isaddress = ISC_TRUE;
8595 a->isaddress = ISC_FALSE;
8596 a->_u._n.port = port;
8597 dns_name_init(&a->_u._n.name, NULL);
8598 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8599 if (result != ISC_R_SUCCESS) {
8600 isc_mem_put(resolver->mctx, a, sizeof(*a));
8604 ISC_LINK_INIT(a, link);
8605 ISC_LIST_APPEND(resolver->alternates, a, link);
8607 return (ISC_R_SUCCESS);
8611 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8612 REQUIRE(VALID_RESOLVER(resolver));
8613 resolver->udpsize = udpsize;
8617 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8618 REQUIRE(VALID_RESOLVER(resolver));
8619 return (resolver->udpsize);
8623 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8625 dns_badcache_t *bad, *prev, *next;
8627 REQUIRE(VALID_RESOLVER(resolver));
8629 LOCK(&resolver->lock);
8630 if (resolver->badcache == NULL)
8635 isc_result_t result;
8636 result = isc_time_now(&now);
8637 if (result != ISC_R_SUCCESS)
8638 isc_time_settoepoch(&now);
8639 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8641 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8644 n = isc_time_compare(&bad->expire, &now);
8645 if (n < 0 || dns_name_equal(name, &bad->name)) {
8647 resolver->badcache[i] = bad->next;
8649 prev->next = bad->next;
8650 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8652 resolver->badcount--;
8657 destroy_badcache(resolver);
8660 UNLOCK(&resolver->lock);
8665 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8666 unsigned int newsize;
8667 dns_badcache_t **new, *bad, *next;
8671 newsize = resolver->badhash * 2 + 1;
8673 newsize = (resolver->badhash - 1) / 2;
8675 new = isc_mem_get(resolver->mctx,
8676 sizeof(*resolver->badcache) * newsize);
8679 memset(new, 0, sizeof(*resolver->badcache) * newsize);
8680 for (i = 0; i < resolver->badhash; i++) {
8681 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8683 if (isc_time_compare(&bad->expire, now) < 0) {
8684 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8686 resolver->badcount--;
8688 bad->next = new[bad->hashval % newsize];
8689 new[bad->hashval % newsize] = bad;
8693 isc_mem_put(resolver->mctx, resolver->badcache,
8694 sizeof(*resolver->badcache) * resolver->badhash);
8695 resolver->badhash = newsize;
8696 resolver->badcache = new;
8700 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8701 dns_rdatatype_t type, isc_time_t *expire)
8704 isc_result_t result = ISC_R_SUCCESS;
8705 unsigned int i, hashval;
8706 dns_badcache_t *bad, *prev, *next;
8708 REQUIRE(VALID_RESOLVER(resolver));
8710 LOCK(&resolver->lock);
8711 if (resolver->badcache == NULL) {
8712 resolver->badcache = isc_mem_get(resolver->mctx,
8713 sizeof(*resolver->badcache) *
8715 if (resolver->badcache == NULL)
8717 resolver->badhash = DNS_BADCACHE_SIZE;
8718 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8722 result = isc_time_now(&now);
8723 if (result != ISC_R_SUCCESS)
8724 isc_time_settoepoch(&now);
8725 hashval = dns_name_hash(name, ISC_FALSE);
8726 i = hashval % resolver->badhash;
8728 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8730 if (bad->type == type && dns_name_equal(name, &bad->name))
8732 if (isc_time_compare(&bad->expire, &now) < 0) {
8734 resolver->badcache[i] = bad->next;
8736 prev->next = bad->next;
8737 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8739 resolver->badcount--;
8744 isc_buffer_t buffer;
8745 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8749 bad->hashval = hashval;
8750 bad->expire = *expire;
8751 isc_buffer_init(&buffer, bad + 1, name->length);
8752 dns_name_init(&bad->name, NULL);
8753 dns_name_copy(name, &bad->name, &buffer);
8754 bad->next = resolver->badcache[i];
8755 resolver->badcache[i] = bad;
8756 resolver->badcount++;
8757 if (resolver->badcount > resolver->badhash * 8)
8758 resizehash(resolver, &now, ISC_TRUE);
8759 if (resolver->badcount < resolver->badhash * 2 &&
8760 resolver->badhash > DNS_BADCACHE_SIZE)
8761 resizehash(resolver, &now, ISC_FALSE);
8763 bad->expire = *expire;
8765 UNLOCK(&resolver->lock);
8769 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8770 dns_rdatatype_t type, isc_time_t *now)
8772 dns_badcache_t *bad, *prev, *next;
8773 isc_boolean_t answer = ISC_FALSE;
8776 REQUIRE(VALID_RESOLVER(resolver));
8778 LOCK(&resolver->lock);
8779 if (resolver->badcache == NULL)
8782 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8784 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8787 * Search the hash list. Clean out expired records as we go.
8789 if (isc_time_compare(&bad->expire, now) < 0) {
8791 prev->next = bad->next;
8793 resolver->badcache[i] = bad->next;
8794 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8796 resolver->badcount--;
8799 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8807 * Slow sweep to clean out stale records.
8809 i = resolver->badsweep++ % resolver->badhash;
8810 bad = resolver->badcache[i];
8811 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8812 resolver->badcache[i] = bad->next;
8813 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8815 resolver->badcount--;
8819 UNLOCK(&resolver->lock);
8824 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8825 char namebuf[DNS_NAME_FORMATSIZE];
8826 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8827 dns_badcache_t *bad, *next, *prev;
8832 LOCK(&resolver->lock);
8833 fprintf(fp, ";\n; Bad cache\n;\n");
8835 if (resolver->badcache == NULL)
8839 for (i = 0; i < resolver->badhash; i++) {
8841 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8843 if (isc_time_compare(&bad->expire, &now) < 0) {
8845 prev->next = bad->next;
8847 resolver->badcache[i] = bad->next;
8848 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8850 resolver->badcount--;
8854 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8855 dns_rdatatype_format(bad->type, typebuf,
8857 t = isc_time_microdiff(&bad->expire, &now);
8859 fprintf(fp, "; %s/%s [ttl "
8860 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8861 namebuf, typebuf, t);
8866 UNLOCK(&resolver->lock);
8870 free_algorithm(void *node, void *arg) {
8871 unsigned char *algorithms = node;
8872 isc_mem_t *mctx = arg;
8874 isc_mem_put(mctx, algorithms, *algorithms);
8878 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8880 REQUIRE(VALID_RESOLVER(resolver));
8883 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8885 if (resolver->algorithms != NULL)
8886 dns_rbt_destroy(&resolver->algorithms);
8888 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8893 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8896 unsigned int len, mask;
8898 unsigned char *algorithms;
8899 isc_result_t result;
8900 dns_rbtnode_t *node = NULL;
8902 REQUIRE(VALID_RESOLVER(resolver));
8904 return (ISC_R_RANGE);
8907 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8909 if (resolver->algorithms == NULL) {
8910 result = dns_rbt_create(resolver->mctx, free_algorithm,
8911 resolver->mctx, &resolver->algorithms);
8912 if (result != ISC_R_SUCCESS)
8917 mask = 1 << (alg%8);
8919 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8921 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8922 algorithms = node->data;
8923 if (algorithms == NULL || len > *algorithms) {
8924 new = isc_mem_get(resolver->mctx, len);
8926 result = ISC_R_NOMEMORY;
8929 memset(new, 0, len);
8930 if (algorithms != NULL)
8931 memmove(new, algorithms, *algorithms);
8935 if (algorithms != NULL)
8936 isc_mem_put(resolver->mctx, algorithms,
8939 algorithms[len-1] |= mask;
8941 result = ISC_R_SUCCESS;
8944 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8950 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8953 unsigned int len, mask;
8954 unsigned char *algorithms;
8956 isc_result_t result;
8957 isc_boolean_t found = ISC_FALSE;
8959 REQUIRE(VALID_RESOLVER(resolver));
8962 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8964 if (resolver->algorithms == NULL)
8966 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8967 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8969 mask = 1 << (alg%8);
8971 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8976 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8980 return (dst_algorithm_supported(alg));
8984 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8987 return (dns_ds_digest_supported(digest));
8991 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8993 REQUIRE(VALID_RESOLVER(resolver));
8996 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8998 if (resolver->mustbesecure != NULL)
8999 dns_rbt_destroy(&resolver->mustbesecure);
9001 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
9005 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
9008 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
9009 isc_boolean_t value)
9011 isc_result_t result;
9013 REQUIRE(VALID_RESOLVER(resolver));
9016 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
9018 if (resolver->mustbesecure == NULL) {
9019 result = dns_rbt_create(resolver->mctx, NULL, NULL,
9020 &resolver->mustbesecure);
9021 if (result != ISC_R_SUCCESS)
9024 result = dns_rbt_addname(resolver->mustbesecure, name,
9025 value ? &yes : &no);
9028 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
9034 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
9036 isc_boolean_t value = ISC_FALSE;
9037 isc_result_t result;
9039 REQUIRE(VALID_RESOLVER(resolver));
9042 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
9044 if (resolver->mustbesecure == NULL)
9046 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
9047 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
9048 value = *(isc_boolean_t*)data;
9051 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
9057 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
9058 isc_uint32_t *min, isc_uint32_t *max)
9060 REQUIRE(VALID_RESOLVER(resolver));
9062 LOCK(&resolver->lock);
9064 *cur = resolver->spillat;
9066 *min = resolver->spillatmin;
9068 *max = resolver->spillatmax;
9069 UNLOCK(&resolver->lock);
9073 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
9076 REQUIRE(VALID_RESOLVER(resolver));
9078 LOCK(&resolver->lock);
9079 resolver->spillatmin = resolver->spillat = min;
9080 resolver->spillatmax = max;
9081 UNLOCK(&resolver->lock);
9085 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
9086 REQUIRE(VALID_RESOLVER(resolver));
9088 return (resolver->zero_no_soa_ttl);
9092 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
9093 REQUIRE(VALID_RESOLVER(resolver));
9095 resolver->zero_no_soa_ttl = state;
9099 dns_resolver_getoptions(dns_resolver_t *resolver) {
9100 REQUIRE(VALID_RESOLVER(resolver));
9102 return (resolver->options);
9106 dns_resolver_gettimeout(dns_resolver_t *resolver) {
9107 REQUIRE(VALID_RESOLVER(resolver));
9109 return (resolver->query_timeout);
9113 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
9114 REQUIRE(VALID_RESOLVER(resolver));
9117 seconds = DEFAULT_QUERY_TIMEOUT;
9118 if (seconds > MAXIMUM_QUERY_TIMEOUT)
9119 seconds = MAXIMUM_QUERY_TIMEOUT;
9120 if (seconds < MINIMUM_QUERY_TIMEOUT)
9121 seconds = MINIMUM_QUERY_TIMEOUT;
9123 resolver->query_timeout = seconds;
9127 dns_resolver_setmaxdepth(dns_resolver_t *resolver, unsigned int maxdepth) {
9128 REQUIRE(VALID_RESOLVER(resolver));
9129 resolver->maxdepth = maxdepth;
9133 dns_resolver_getmaxdepth(dns_resolver_t *resolver) {
9134 REQUIRE(VALID_RESOLVER(resolver));
9135 return (resolver->maxdepth);
9139 dns_resolver_setmaxqueries(dns_resolver_t *resolver, unsigned int queries) {
9140 REQUIRE(VALID_RESOLVER(resolver));
9141 resolver->maxqueries = queries;
9145 dns_resolver_getmaxqueries(dns_resolver_t *resolver) {
9146 REQUIRE(VALID_RESOLVER(resolver));
9147 return (resolver->maxqueries);