2 * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
25 #include <isc/platform.h>
26 #include <isc/print.h>
27 #include <isc/string.h>
28 #include <isc/random.h>
29 #include <isc/socket.h>
30 #include <isc/stats.h>
32 #include <isc/timer.h>
37 #include <dns/cache.h>
39 #include <dns/dispatch.h>
41 #include <dns/events.h>
42 #include <dns/forward.h>
43 #include <dns/keytable.h>
45 #include <dns/message.h>
46 #include <dns/ncache.h>
48 #include <dns/nsec3.h>
49 #include <dns/opcode.h>
52 #include <dns/rcode.h>
53 #include <dns/rdata.h>
54 #include <dns/rdataclass.h>
55 #include <dns/rdatalist.h>
56 #include <dns/rdataset.h>
57 #include <dns/rdatastruct.h>
58 #include <dns/rdatatype.h>
59 #include <dns/resolver.h>
60 #include <dns/result.h>
61 #include <dns/rootns.h>
62 #include <dns/stats.h>
64 #include <dns/validator.h>
66 #define DNS_RESOLVER_TRACE
67 #ifdef DNS_RESOLVER_TRACE
68 #define RTRACE(m) isc_log_write(dns_lctx, \
69 DNS_LOGCATEGORY_RESOLVER, \
70 DNS_LOGMODULE_RESOLVER, \
72 "res %p: %s", res, (m))
73 #define RRTRACE(r, m) isc_log_write(dns_lctx, \
74 DNS_LOGCATEGORY_RESOLVER, \
75 DNS_LOGMODULE_RESOLVER, \
77 "res %p: %s", (r), (m))
78 #define FCTXTRACE(m) isc_log_write(dns_lctx, \
79 DNS_LOGCATEGORY_RESOLVER, \
80 DNS_LOGMODULE_RESOLVER, \
82 "fctx %p(%s): %s", fctx, fctx->info, (m))
83 #define FCTXTRACE2(m1, m2) \
84 isc_log_write(dns_lctx, \
85 DNS_LOGCATEGORY_RESOLVER, \
86 DNS_LOGMODULE_RESOLVER, \
88 "fctx %p(%s): %s %s", \
89 fctx, fctx->info, (m1), (m2))
90 #define FTRACE(m) isc_log_write(dns_lctx, \
91 DNS_LOGCATEGORY_RESOLVER, \
92 DNS_LOGMODULE_RESOLVER, \
94 "fetch %p (fctx %p(%s)): %s", \
95 fetch, fetch->private, \
96 fetch->private->info, (m))
97 #define QTRACE(m) isc_log_write(dns_lctx, \
98 DNS_LOGCATEGORY_RESOLVER, \
99 DNS_LOGMODULE_RESOLVER, \
101 "resquery %p (fctx %p(%s)): %s", \
102 query, query->fctx, \
103 query->fctx->info, (m))
106 #define RRTRACE(r, m)
108 #define FCTXTRACE2(m1, m2)
113 #define US_PER_SEC 1000000U
115 * The maximum time we will wait for a single query.
117 #define MAX_SINGLE_QUERY_TIMEOUT 9U
118 #define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_SEC)
121 * We need to allow a individual query time to complete / timeout.
123 #define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1U)
125 /* The default time in seconds for the whole query to live. */
126 #ifndef DEFAULT_QUERY_TIMEOUT
127 #define DEFAULT_QUERY_TIMEOUT MINIMUM_QUERY_TIMEOUT
130 #ifndef MAXIMUM_QUERY_TIMEOUT
131 #define MAXIMUM_QUERY_TIMEOUT 30 /* The maximum time in seconds for the whole query to live. */
135 * Maximum EDNS0 input packet size.
137 #define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */
141 * This defines the maximum number of timeouts we will permit before we
142 * disable EDNS0 on the query.
144 #define MAX_EDNS0_TIMEOUTS 3
146 typedef struct fetchctx fetchctx_t;
148 typedef struct query {
149 /* Locked by task event serialization. */
153 dns_dispatchmgr_t * dispatchmgr;
154 dns_dispatch_t * dispatch;
155 isc_boolean_t exclusivesocket;
156 dns_adbaddrinfo_t * addrinfo;
157 isc_socket_t * tcpsocket;
160 dns_dispentry_t * dispentry;
161 ISC_LINK(struct query) link;
164 dns_tsigkey_t *tsigkey;
165 isc_socketevent_t sendevent;
166 unsigned int options;
167 unsigned int attributes;
169 unsigned int connects;
170 unsigned char data[512];
173 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
174 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
176 #define RESQUERY_ATTR_CANCELED 0x02
178 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
179 #define RESQUERY_CANCELED(q) (((q)->attributes & \
180 RESQUERY_ATTR_CANCELED) != 0)
181 #define RESQUERY_SENDING(q) ((q)->sends > 0)
184 fetchstate_init = 0, /*%< Start event has not run yet. */
186 fetchstate_done /*%< FETCHDONE events posted. */
190 badns_unreachable = 0,
198 dns_resolver_t * res;
200 dns_rdatatype_t type;
201 unsigned int options;
202 unsigned int bucketnum;
206 /*% Locked by appropriate bucket lock. */
208 isc_boolean_t want_shutdown;
209 isc_boolean_t cloned;
210 isc_boolean_t spilled;
211 unsigned int references;
212 isc_event_t control_event;
213 ISC_LINK(struct fetchctx) link;
214 ISC_LIST(dns_fetchevent_t) events;
215 /*% Locked by task event serialization. */
217 dns_rdataset_t nameservers;
218 unsigned int attributes;
221 isc_interval_t interval;
222 dns_message_t * qmessage;
223 dns_message_t * rmessage;
224 ISC_LIST(resquery_t) queries;
225 dns_adbfindlist_t finds;
226 dns_adbfind_t * find;
227 dns_adbfindlist_t altfinds;
228 dns_adbfind_t * altfind;
229 dns_adbaddrinfolist_t forwaddrs;
230 dns_adbaddrinfolist_t altaddrs;
231 isc_sockaddrlist_t forwarders;
232 dns_fwdpolicy_t fwdpolicy;
233 isc_sockaddrlist_t bad;
234 isc_sockaddrlist_t edns;
235 isc_sockaddrlist_t edns512;
236 isc_sockaddrlist_t bad_edns;
237 dns_validator_t *validator;
238 ISC_LIST(dns_validator_t) validators;
241 isc_boolean_t ns_ttl_ok;
245 * The number of events we're waiting for.
247 unsigned int pending;
250 * The number of times we've "restarted" the current
251 * nameserver set. This acts as a failsafe to prevent
252 * us from pounding constantly on a particular set of
253 * servers that, for whatever reason, are not giving
254 * us useful responses, but are responding in such a
255 * way that they are not marked "bad".
257 unsigned int restarts;
260 * The number of timeouts that have occurred since we
261 * last successfully received a response packet. This
262 * is used for EDNS0 black hole detection.
264 unsigned int timeouts;
267 * Look aside state for DS lookups.
270 dns_fetch_t * nsfetch;
271 dns_rdataset_t nsrrset;
274 * Number of queries that reference this context.
276 unsigned int nqueries;
279 * The reason to print when logging a successful
280 * response to a query.
285 * Random numbers to use for mixing up server addresses.
287 isc_uint32_t rand_buf;
288 isc_uint32_t rand_bits;
291 * Fetch-local statistics for detailed logging.
293 isc_result_t result; /*%< fetch result */
294 isc_result_t vresult; /*%< validation result */
297 isc_uint64_t duration;
298 isc_boolean_t logged;
299 unsigned int querysent;
300 unsigned int referrals;
301 unsigned int lamecount;
303 unsigned int badresp;
305 unsigned int findfail;
306 unsigned int valfail;
307 isc_boolean_t timeout;
308 dns_adbaddrinfo_t *addrinfo;
309 isc_sockaddr_t *client;
312 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
313 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
315 #define FCTX_ATTR_HAVEANSWER 0x0001
316 #define FCTX_ATTR_GLUING 0x0002
317 #define FCTX_ATTR_ADDRWAIT 0x0004
318 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
319 #define FCTX_ATTR_WANTCACHE 0x0010
320 #define FCTX_ATTR_WANTNCACHE 0x0020
321 #define FCTX_ATTR_NEEDEDNS0 0x0040
322 #define FCTX_ATTR_TRIEDFIND 0x0080
323 #define FCTX_ATTR_TRIEDALT 0x0100
325 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
327 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
329 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
331 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
333 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
334 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
335 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
336 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
337 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
340 dns_adbaddrinfo_t * addrinfo;
346 fetchctx_t * private;
349 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
350 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
352 typedef struct fctxbucket {
355 ISC_LIST(fetchctx_t) fctxs;
356 isc_boolean_t exiting;
360 typedef struct alternate {
361 isc_boolean_t isaddress;
369 ISC_LINK(struct alternate) link;
372 typedef struct dns_badcache dns_badcache_t;
373 struct dns_badcache {
374 dns_badcache_t * next;
375 dns_rdatatype_t type;
377 unsigned int hashval;
380 #define DNS_BADCACHE_SIZE 1021
381 #define DNS_BADCACHE_TTL(fctx) \
382 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
384 struct dns_resolver {
390 isc_mutex_t primelock;
391 dns_rdataclass_t rdclass;
392 isc_socketmgr_t * socketmgr;
393 isc_timermgr_t * timermgr;
394 isc_taskmgr_t * taskmgr;
396 isc_boolean_t frozen;
397 unsigned int options;
398 dns_dispatchmgr_t * dispatchmgr;
399 dns_dispatchset_t * dispatches4;
400 isc_boolean_t exclusivev4;
401 dns_dispatchset_t * dispatches6;
402 isc_boolean_t exclusivev6;
403 unsigned int nbuckets;
404 fctxbucket_t * buckets;
405 isc_uint32_t lame_ttl;
406 ISC_LIST(alternate_t) alternates;
407 isc_uint16_t udpsize;
409 isc_rwlock_t alglock;
411 dns_rbt_t * algorithms;
413 isc_rwlock_t mbslock;
415 dns_rbt_t * mustbesecure;
416 unsigned int spillatmax;
417 unsigned int spillatmin;
418 isc_timer_t * spillattimer;
419 isc_boolean_t zero_no_soa_ttl;
420 unsigned int query_timeout;
422 /* Locked by lock. */
423 unsigned int references;
424 isc_boolean_t exiting;
425 isc_eventlist_t whenshutdown;
426 unsigned int activebuckets;
427 isc_boolean_t priming;
428 unsigned int spillat; /* clients-per-query */
431 dns_badcache_t ** badcache;
432 unsigned int badcount;
433 unsigned int badhash;
434 unsigned int badsweep;
436 /* Locked by primelock. */
437 dns_fetch_t * primefetch;
438 /* Locked by nlock. */
442 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
443 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
446 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
447 * which we also use as an addrinfo flag.
449 #define FCTX_ADDRINFO_MARK 0x0001
450 #define FCTX_ADDRINFO_FORWARDER 0x1000
451 #define FCTX_ADDRINFO_TRIED 0x2000
452 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
454 #define ISFORWARDER(a) (((a)->flags & \
455 FCTX_ADDRINFO_FORWARDER) != 0)
456 #define TRIED(a) (((a)->flags & \
457 FCTX_ADDRINFO_TRIED) != 0)
459 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
460 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
462 static void destroy(dns_resolver_t *res);
463 static void empty_bucket(dns_resolver_t *res);
464 static isc_result_t resquery_send(resquery_t *query);
465 static void resquery_response(isc_task_t *task, isc_event_t *event);
466 static void resquery_connected(isc_task_t *task, isc_event_t *event);
467 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
468 isc_boolean_t badcache);
469 static void fctx_destroy(fetchctx_t *fctx);
470 static isc_boolean_t fctx_unlink(fetchctx_t *fctx);
471 static isc_result_t ncache_adderesult(dns_message_t *message,
472 dns_db_t *cache, dns_dbnode_t *node,
473 dns_rdatatype_t covers,
474 isc_stdtime_t now, dns_ttl_t maxttl,
475 isc_boolean_t optout,
476 isc_boolean_t secure,
477 dns_rdataset_t *ardataset,
478 isc_result_t *eresultp);
479 static void validated(isc_task_t *task, isc_event_t *event);
480 static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked);
481 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
482 isc_result_t reason, badnstype_t badtype);
483 static inline isc_result_t findnoqname(fetchctx_t *fctx, dns_name_t *name,
484 dns_rdatatype_t type,
485 dns_name_t **noqname);
488 * Increment resolver-related statistics counters.
491 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
492 if (res->view->resstats != NULL)
493 isc_stats_increment(res->view->resstats, counter);
497 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
498 dns_rdatatype_t type, dns_rdataset_t *rdataset,
499 dns_rdataset_t *sigrdataset, unsigned int valoptions,
502 dns_validator_t *validator = NULL;
503 dns_valarg_t *valarg;
506 valarg = isc_mem_get(fctx->mctx, sizeof(*valarg));
508 return (ISC_R_NOMEMORY);
511 valarg->addrinfo = addrinfo;
513 if (!ISC_LIST_EMPTY(fctx->validators))
514 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
516 result = dns_validator_create(fctx->res->view, name, type, rdataset,
517 sigrdataset, fctx->rmessage,
518 valoptions, task, validated, valarg,
520 if (result == ISC_R_SUCCESS) {
521 inc_stats(fctx->res, dns_resstatscounter_val);
522 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
523 INSIST(fctx->validator == NULL);
524 fctx->validator = validator;
526 ISC_LIST_APPEND(fctx->validators, validator, link);
528 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
533 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
534 dns_namereln_t namereln;
535 dns_rdata_rrsig_t rrsig;
536 dns_rdata_t rdata = DNS_RDATA_INIT;
541 for (result = dns_rdataset_first(rdataset);
542 result == ISC_R_SUCCESS;
543 result = dns_rdataset_next(rdataset)) {
544 dns_rdataset_current(rdataset, &rdata);
545 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
546 RUNTIME_CHECK(result == ISC_R_SUCCESS);
547 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
549 if (namereln == dns_namereln_subdomain)
551 dns_rdata_reset(&rdata);
557 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
559 dns_name_t *domain = &fctx->domain;
560 dns_rdataset_t *rdataset;
561 dns_rdatatype_t type;
563 isc_boolean_t keep_auth = ISC_FALSE;
565 if (message->rcode == dns_rcode_nxdomain)
569 * A DS RRset can appear anywhere in a zone, even for a delegation-only
570 * zone. So a response to an explicit query for this type should be
571 * excluded from delegation-only fixup.
573 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
574 * response to a query for these types can never violate the
575 * delegation-only assumption: if the query name is below a
576 * zone cut, the response should normally be a referral, which should
577 * be accepted; if the query name is below a zone cut but the server
578 * happens to have authority for the zone of the query name, the
579 * response is a (non-referral) answer. But this does not violate
580 * delegation-only because the query name must be in a different zone
581 * due to the "apex-only" nature of these types. Note that if the
582 * remote server happens to have authority for a child zone of a
583 * delegation-only zone, we may still incorrectly "fix" the response
584 * with NXDOMAIN for queries for other types. Unfortunately it's
585 * generally impossible to differentiate this case from violation of
586 * the delegation-only assumption. Once the resolver learns the
587 * correct zone cut, possibly via a separate query for an "apex-only"
588 * type, queries for other types will be resolved correctly.
590 * A query for type ANY will be accepted if it hits an exceptional
591 * type above in the answer section as it should be from a child
594 * Also accept answers with RRSIG records from the child zone.
595 * Direct queries for RRSIG records should not be answered from
599 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
600 (fctx->type == dns_rdatatype_ns ||
601 fctx->type == dns_rdatatype_ds ||
602 fctx->type == dns_rdatatype_soa ||
603 fctx->type == dns_rdatatype_any ||
604 fctx->type == dns_rdatatype_rrsig ||
605 fctx->type == dns_rdatatype_dnskey)) {
606 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
607 while (result == ISC_R_SUCCESS) {
609 dns_message_currentname(message, DNS_SECTION_ANSWER,
611 for (rdataset = ISC_LIST_HEAD(name->list);
613 rdataset = ISC_LIST_NEXT(rdataset, link)) {
614 if (!dns_name_equal(name, &fctx->name))
616 type = rdataset->type;
620 if (type == dns_rdatatype_rrsig &&
621 rrsig_fromchildzone(fctx, rdataset))
624 * Direct query for apex records or DS.
626 if (fctx->type == type &&
627 (type == dns_rdatatype_ds ||
628 type == dns_rdatatype_ns ||
629 type == dns_rdatatype_soa ||
630 type == dns_rdatatype_dnskey))
633 * Indirect query for apex records or DS.
635 if (fctx->type == dns_rdatatype_any &&
636 (type == dns_rdatatype_ns ||
637 type == dns_rdatatype_ds ||
638 type == dns_rdatatype_soa ||
639 type == dns_rdatatype_dnskey))
642 result = dns_message_nextname(message,
648 * A NODATA response to a DS query?
650 if (fctx->type == dns_rdatatype_ds &&
651 message->counts[DNS_SECTION_ANSWER] == 0)
654 /* Look for referral or indication of answer from child zone? */
655 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
658 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
659 while (result == ISC_R_SUCCESS) {
661 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
662 for (rdataset = ISC_LIST_HEAD(name->list);
664 rdataset = ISC_LIST_NEXT(rdataset, link)) {
665 type = rdataset->type;
666 if (type == dns_rdatatype_soa &&
667 dns_name_equal(name, domain))
668 keep_auth = ISC_TRUE;
670 if (type != dns_rdatatype_ns &&
671 type != dns_rdatatype_soa &&
672 type != dns_rdatatype_rrsig)
675 if (type == dns_rdatatype_rrsig) {
676 if (rrsig_fromchildzone(fctx, rdataset))
682 /* NS or SOA records. */
683 if (dns_name_equal(name, domain)) {
685 * If a query for ANY causes a negative
686 * response, we can be sure that this is
687 * an empty node. For other type of queries
688 * we cannot differentiate an empty node
689 * from a node that just doesn't have that
690 * type of record. We only accept the former
693 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
694 fctx->type == dns_rdatatype_any)
696 } else if (dns_name_issubdomain(name, domain)) {
697 /* Referral or answer from child zone. */
701 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
705 message->rcode = dns_rcode_nxdomain;
706 message->counts[DNS_SECTION_ANSWER] = 0;
708 message->counts[DNS_SECTION_AUTHORITY] = 0;
709 message->counts[DNS_SECTION_ADDITIONAL] = 0;
713 static inline isc_result_t
714 fctx_starttimer(fetchctx_t *fctx) {
716 * Start the lifetime timer for fctx.
718 * This is also used for stopping the idle timer; in that
719 * case we must purge events already posted to ensure that
720 * no further idle events are delivered.
722 return (isc_timer_reset(fctx->timer, isc_timertype_once,
723 &fctx->expires, NULL, ISC_TRUE));
727 fctx_stoptimer(fetchctx_t *fctx) {
731 * We don't return a result if resetting the timer to inactive fails
732 * since there's nothing to be done about it. Resetting to inactive
733 * should never fail anyway, since the code as currently written
734 * cannot fail in that case.
736 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
737 NULL, NULL, ISC_TRUE);
738 if (result != ISC_R_SUCCESS) {
739 UNEXPECTED_ERROR(__FILE__, __LINE__,
740 "isc_timer_reset(): %s",
741 isc_result_totext(result));
746 static inline isc_result_t
747 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
749 * Start the idle timer for fctx. The lifetime timer continues
752 return (isc_timer_reset(fctx->timer, isc_timertype_once,
753 &fctx->expires, interval, ISC_FALSE));
757 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
758 * we use fctx_stopidletimer for readability in the code below.
760 #define fctx_stopidletimer fctx_starttimer
764 resquery_destroy(resquery_t **queryp) {
767 REQUIRE(queryp != NULL);
769 REQUIRE(!ISC_LINK_LINKED(query, link));
771 INSIST(query->tcpsocket == NULL);
773 query->fctx->nqueries--;
774 if (SHUTTINGDOWN(query->fctx)) {
775 dns_resolver_t *res = query->fctx->res;
776 if (maybe_destroy(query->fctx, ISC_FALSE))
780 isc_mem_put(query->mctx, query, sizeof(*query));
785 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
786 isc_time_t *finish, isc_boolean_t no_response)
790 unsigned int rtt, rttms;
793 dns_adbaddrinfo_t *addrinfo;
794 isc_socket_t *socket;
799 FCTXTRACE("cancelquery");
801 REQUIRE(!RESQUERY_CANCELED(query));
803 query->attributes |= RESQUERY_ATTR_CANCELED;
806 * Should we update the RTT?
808 if (finish != NULL || no_response) {
809 if (finish != NULL) {
811 * We have both the start and finish times for this
812 * packet, so we can compute a real RTT.
814 rtt = (unsigned int)isc_time_microdiff(finish,
816 factor = DNS_ADB_RTTADJDEFAULT;
819 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
821 dns_resstatscounter_queryrtt0);
822 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
824 dns_resstatscounter_queryrtt1);
825 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
827 dns_resstatscounter_queryrtt2);
828 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
830 dns_resstatscounter_queryrtt3);
831 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
833 dns_resstatscounter_queryrtt4);
836 dns_resstatscounter_queryrtt5);
840 * We don't have an RTT for this query. Maybe the
841 * packet was lost, or maybe this server is very
842 * slow. We don't know. Increase the RTT.
845 rtt = query->addrinfo->srtt + 200000;
846 if (rtt > MAX_SINGLE_QUERY_TIMEOUT_US)
847 rtt = MAX_SINGLE_QUERY_TIMEOUT_US;
849 * Replace the current RTT with our value.
851 factor = DNS_ADB_RTTADJREPLACE;
853 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
856 /* Remember that the server has been tried. */
857 if (!TRIED(query->addrinfo)) {
858 dns_adb_changeflags(fctx->adb, query->addrinfo,
859 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
863 * Age RTTs of servers not tried.
865 factor = DNS_ADB_RTTADJAGE;
867 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
869 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
870 if (UNMARKED(addrinfo))
871 dns_adb_adjustsrtt(fctx->adb, addrinfo,
874 if (finish != NULL && TRIEDFIND(fctx))
875 for (find = ISC_LIST_HEAD(fctx->finds);
877 find = ISC_LIST_NEXT(find, publink))
878 for (addrinfo = ISC_LIST_HEAD(find->list);
880 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
881 if (UNMARKED(addrinfo))
882 dns_adb_adjustsrtt(fctx->adb, addrinfo,
885 if (finish != NULL && TRIEDALT(fctx)) {
886 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
888 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
889 if (UNMARKED(addrinfo))
890 dns_adb_adjustsrtt(fctx->adb, addrinfo,
892 for (find = ISC_LIST_HEAD(fctx->altfinds);
894 find = ISC_LIST_NEXT(find, publink))
895 for (addrinfo = ISC_LIST_HEAD(find->list);
897 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
898 if (UNMARKED(addrinfo))
899 dns_adb_adjustsrtt(fctx->adb, addrinfo,
904 * Check for any outstanding socket events. If they exist, cancel
905 * them and let the event handlers finish the cleanup. The resolver
906 * only needs to worry about managing the connect and send events;
907 * the dispatcher manages the recv events.
909 if (RESQUERY_CONNECTING(query)) {
911 * Cancel the connect.
913 if (query->tcpsocket != NULL) {
914 isc_socket_cancel(query->tcpsocket, NULL,
915 ISC_SOCKCANCEL_CONNECT);
916 } else if (query->dispentry != NULL) {
917 INSIST(query->exclusivesocket);
918 socket = dns_dispatch_getentrysocket(query->dispentry);
920 isc_socket_cancel(socket, NULL,
921 ISC_SOCKCANCEL_CONNECT);
923 } else if (RESQUERY_SENDING(query)) {
925 * Cancel the pending send.
927 if (query->exclusivesocket && query->dispentry != NULL)
928 socket = dns_dispatch_getentrysocket(query->dispentry);
930 socket = dns_dispatch_getsocket(query->dispatch);
932 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
935 if (query->dispentry != NULL)
936 dns_dispatch_removeresponse(&query->dispentry, deventp);
938 ISC_LIST_UNLINK(fctx->queries, query, link);
940 if (query->tsig != NULL)
941 isc_buffer_free(&query->tsig);
943 if (query->tsigkey != NULL)
944 dns_tsigkey_detach(&query->tsigkey);
946 if (query->dispatch != NULL)
947 dns_dispatch_detach(&query->dispatch);
949 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
951 * It's safe to destroy the query now.
953 resquery_destroy(&query);
957 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
958 resquery_t *query, *next_query;
960 FCTXTRACE("cancelqueries");
962 for (query = ISC_LIST_HEAD(fctx->queries);
964 query = next_query) {
965 next_query = ISC_LIST_NEXT(query, link);
966 fctx_cancelquery(&query, NULL, NULL, no_response);
971 fctx_cleanupfinds(fetchctx_t *fctx) {
972 dns_adbfind_t *find, *next_find;
974 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
976 for (find = ISC_LIST_HEAD(fctx->finds);
979 next_find = ISC_LIST_NEXT(find, publink);
980 ISC_LIST_UNLINK(fctx->finds, find, publink);
981 dns_adb_destroyfind(&find);
987 fctx_cleanupaltfinds(fetchctx_t *fctx) {
988 dns_adbfind_t *find, *next_find;
990 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
992 for (find = ISC_LIST_HEAD(fctx->altfinds);
995 next_find = ISC_LIST_NEXT(find, publink);
996 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
997 dns_adb_destroyfind(&find);
999 fctx->altfind = NULL;
1003 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
1004 dns_adbaddrinfo_t *addr, *next_addr;
1006 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1008 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
1011 next_addr = ISC_LIST_NEXT(addr, publink);
1012 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
1013 dns_adb_freeaddrinfo(fctx->adb, &addr);
1018 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
1019 dns_adbaddrinfo_t *addr, *next_addr;
1021 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1023 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
1026 next_addr = ISC_LIST_NEXT(addr, publink);
1027 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1028 dns_adb_freeaddrinfo(fctx->adb, &addr);
1033 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1034 FCTXTRACE("stopeverything");
1035 fctx_cancelqueries(fctx, no_response);
1036 fctx_cleanupfinds(fctx);
1037 fctx_cleanupaltfinds(fctx);
1038 fctx_cleanupforwaddrs(fctx);
1039 fctx_cleanupaltaddrs(fctx);
1040 fctx_stoptimer(fctx);
1044 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1045 dns_fetchevent_t *event, *next_event;
1047 unsigned int count = 0;
1049 isc_boolean_t logit = ISC_FALSE;
1051 unsigned int old_spillat;
1052 unsigned int new_spillat = 0; /* initialized to silence
1053 compiler warnings */
1056 * Caller must be holding the appropriate bucket lock.
1058 REQUIRE(fctx->state == fetchstate_done);
1060 FCTXTRACE("sendevents");
1063 * Keep some record of fetch result for logging later (if required).
1065 fctx->result = result;
1066 fctx->exitline = line;
1068 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1070 for (event = ISC_LIST_HEAD(fctx->events);
1072 event = next_event) {
1073 next_event = ISC_LIST_NEXT(event, ev_link);
1074 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1075 task = event->ev_sender;
1076 event->ev_sender = fctx;
1077 event->vresult = fctx->vresult;
1078 if (!HAVE_ANSWER(fctx))
1079 event->result = result;
1081 INSIST(result != ISC_R_SUCCESS ||
1082 dns_rdataset_isassociated(event->rdataset) ||
1083 fctx->type == dns_rdatatype_any ||
1084 fctx->type == dns_rdatatype_rrsig ||
1085 fctx->type == dns_rdatatype_sig);
1088 * Negative results must be indicated in event->result.
1090 if (dns_rdataset_isassociated(event->rdataset) &&
1091 NEGATIVE(event->rdataset)) {
1092 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1093 event->result == DNS_R_NCACHENXRRSET);
1096 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1100 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1102 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1103 LOCK(&fctx->res->lock);
1104 if (count == fctx->res->spillat && !fctx->res->exiting) {
1105 old_spillat = fctx->res->spillat;
1106 fctx->res->spillat += 5;
1107 if (fctx->res->spillat > fctx->res->spillatmax &&
1108 fctx->res->spillatmax != 0)
1109 fctx->res->spillat = fctx->res->spillatmax;
1110 new_spillat = fctx->res->spillat;
1111 if (new_spillat != old_spillat) {
1114 isc_interval_set(&i, 20 * 60, 0);
1115 result = isc_timer_reset(fctx->res->spillattimer,
1116 isc_timertype_ticker, NULL,
1118 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1120 UNLOCK(&fctx->res->lock);
1122 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1123 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1124 "clients-per-query increased to %u",
1130 log_edns(fetchctx_t *fctx) {
1131 char domainbuf[DNS_NAME_FORMATSIZE];
1133 if (fctx->reason == NULL)
1137 * We do not know if fctx->domain is the actual domain the record
1138 * lives in or a parent domain so we have a '?' after it.
1140 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1141 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1142 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1143 "success resolving '%s' (in '%s'?) after %s",
1144 fctx->info, domainbuf, fctx->reason);
1146 fctx->reason = NULL;
1150 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1151 dns_resolver_t *res;
1152 isc_boolean_t no_response;
1160 if (result == ISC_R_SUCCESS) {
1162 * Log any deferred EDNS timeout messages.
1165 no_response = ISC_TRUE;
1167 no_response = ISC_FALSE;
1169 fctx->reason = NULL;
1170 fctx_stopeverything(fctx, no_response);
1172 LOCK(&res->buckets[fctx->bucketnum].lock);
1174 fctx->state = fetchstate_done;
1175 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1176 fctx_sendevents(fctx, result, line);
1178 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1182 process_sendevent(resquery_t *query, isc_event_t *event) {
1183 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1184 isc_boolean_t retry = ISC_FALSE;
1185 isc_result_t result;
1190 if (RESQUERY_CANCELED(query)) {
1191 if (query->sends == 0 && query->connects == 0) {
1193 * This query was canceled while the
1194 * isc_socket_sendto/connect() was in progress.
1196 if (query->tcpsocket != NULL)
1197 isc_socket_detach(&query->tcpsocket);
1198 resquery_destroy(&query);
1201 switch (sevent->result) {
1205 case ISC_R_HOSTUNREACH:
1206 case ISC_R_NETUNREACH:
1208 case ISC_R_ADDRNOTAVAIL:
1209 case ISC_R_CONNREFUSED:
1212 * No route to remote.
1214 add_bad(fctx, query->addrinfo, sevent->result,
1216 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1221 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1226 if (event->ev_type == ISC_SOCKEVENT_CONNECT)
1227 isc_event_free(&event);
1231 * Behave as if the idle timer has expired. For TCP
1232 * this may not actually reflect the latest timer.
1234 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1235 result = fctx_stopidletimer(fctx);
1236 if (result != ISC_R_SUCCESS)
1237 fctx_done(fctx, result, __LINE__);
1239 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1244 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1245 resquery_t *query = event->ev_arg;
1247 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1249 QTRACE("udpconnected");
1253 INSIST(RESQUERY_CONNECTING(query));
1257 process_sendevent(query, event);
1261 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1262 resquery_t *query = event->ev_arg;
1264 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1271 * Currently we don't wait for the senddone event before retrying
1272 * a query. This means that if we get really behind, we may end
1273 * up doing extra work!
1278 INSIST(RESQUERY_SENDING(query));
1282 process_sendevent(query, event);
1285 static inline isc_result_t
1286 fctx_addopt(dns_message_t *message, unsigned int version,
1287 isc_uint16_t udpsize, dns_ednsopt_t *ednsopts, size_t count)
1289 dns_rdataset_t *rdataset = NULL;
1290 isc_result_t result;
1292 result = dns_message_buildopt(message, &rdataset, version, udpsize,
1293 DNS_MESSAGEEXTFLAG_DO, ednsopts, count);
1294 if (result != ISC_R_SUCCESS)
1296 return (dns_message_setopt(message, rdataset));
1300 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1301 unsigned int seconds;
1305 * We retry every .8 seconds the first two times through the address
1306 * list, and then we do exponential back-off.
1308 if (fctx->restarts < 3)
1311 us = (800000 << (fctx->restarts - 2));
1314 * Add a fudge factor to the expected rtt based on the current
1319 else if (rtt < 100000)
1325 * Always wait for at least the expected rtt.
1331 * But don't ever wait for more than 10 seconds.
1333 if (us > MAX_SINGLE_QUERY_TIMEOUT_US)
1334 us = MAX_SINGLE_QUERY_TIMEOUT_US;
1336 seconds = us / US_PER_SEC;
1337 us -= seconds * US_PER_SEC;
1338 isc_interval_set(&fctx->interval, seconds, us * 1000);
1342 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1343 unsigned int options)
1345 dns_resolver_t *res;
1347 isc_result_t result;
1349 isc_sockaddr_t addr;
1350 isc_boolean_t have_addr = ISC_FALSE;
1356 task = res->buckets[fctx->bucketnum].task;
1358 srtt = addrinfo->srtt;
1361 * A forwarder needs to make multiple queries. Give it at least
1362 * a second to do these in.
1364 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1367 fctx_setretryinterval(fctx, srtt);
1368 result = fctx_startidletimer(fctx, &fctx->interval);
1369 if (result != ISC_R_SUCCESS)
1372 INSIST(ISC_LIST_EMPTY(fctx->validators));
1374 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1376 query = isc_mem_get(fctx->mctx, sizeof(*query));
1377 if (query == NULL) {
1378 result = ISC_R_NOMEMORY;
1379 goto stop_idle_timer;
1381 query->mctx = fctx->mctx;
1382 query->options = options;
1383 query->attributes = 0;
1385 query->connects = 0;
1387 * Note that the caller MUST guarantee that 'addrinfo' will remain
1388 * valid until this query is canceled.
1390 query->addrinfo = addrinfo;
1391 TIME_NOW(&query->start);
1394 * If this is a TCP query, then we need to make a socket and
1395 * a dispatch for it here. Otherwise we use the resolver's
1398 query->dispatchmgr = res->dispatchmgr;
1399 query->dispatch = NULL;
1400 query->exclusivesocket = ISC_FALSE;
1401 query->tcpsocket = NULL;
1402 if (res->view->peers != NULL) {
1403 dns_peer_t *peer = NULL;
1404 isc_netaddr_t dstip;
1405 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1406 result = dns_peerlist_peerbyaddr(res->view->peers,
1408 if (result == ISC_R_SUCCESS) {
1409 result = dns_peer_getquerysource(peer, &addr);
1410 if (result == ISC_R_SUCCESS)
1411 have_addr = ISC_TRUE;
1415 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1418 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1422 result = dns_dispatch_getlocaladdress(
1423 res->dispatches4->dispatches[0],
1427 result = dns_dispatch_getlocaladdress(
1428 res->dispatches6->dispatches[0],
1432 result = ISC_R_NOTIMPLEMENTED;
1435 if (result != ISC_R_SUCCESS)
1438 isc_sockaddr_setport(&addr, 0);
1440 result = isc_socket_create(res->socketmgr, pf,
1443 if (result != ISC_R_SUCCESS)
1446 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1447 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1448 if (result != ISC_R_SUCCESS)
1449 goto cleanup_socket;
1453 * A dispatch will be created once the connect succeeds.
1457 unsigned int attrs, attrmask;
1458 attrs = DNS_DISPATCHATTR_UDP;
1459 switch (isc_sockaddr_pf(&addr)) {
1461 attrs |= DNS_DISPATCHATTR_IPV4;
1464 attrs |= DNS_DISPATCHATTR_IPV6;
1467 result = ISC_R_NOTIMPLEMENTED;
1470 attrmask = DNS_DISPATCHATTR_UDP;
1471 attrmask |= DNS_DISPATCHATTR_TCP;
1472 attrmask |= DNS_DISPATCHATTR_IPV4;
1473 attrmask |= DNS_DISPATCHATTR_IPV6;
1474 result = dns_dispatch_getudp(res->dispatchmgr,
1476 res->taskmgr, &addr,
1477 4096, 1000, 32768, 16411,
1478 16433, attrs, attrmask,
1480 if (result != ISC_R_SUCCESS)
1483 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1485 dns_dispatch_attach(
1486 dns_resolver_dispatchv4(res),
1488 query->exclusivesocket = res->exclusivev4;
1491 dns_dispatch_attach(
1492 dns_resolver_dispatchv6(res),
1494 query->exclusivesocket = res->exclusivev6;
1497 result = ISC_R_NOTIMPLEMENTED;
1502 * We should always have a valid dispatcher here. If we
1503 * don't support a protocol family, then its dispatcher
1504 * will be NULL, but we shouldn't be finding addresses for
1505 * protocol types we don't support, so the dispatcher
1506 * we found should never be NULL.
1508 INSIST(query->dispatch != NULL);
1511 query->dispentry = NULL;
1514 query->tsigkey = NULL;
1515 ISC_LINK_INIT(query, link);
1516 query->magic = QUERY_MAGIC;
1518 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1520 * Connect to the remote server.
1522 * XXXRTH Should we attach to the socket?
1524 result = isc_socket_connect(query->tcpsocket,
1525 &addrinfo->sockaddr, task,
1526 resquery_connected, query);
1527 if (result != ISC_R_SUCCESS)
1528 goto cleanup_socket;
1530 QTRACE("connecting via TCP");
1532 result = resquery_send(query);
1533 if (result != ISC_R_SUCCESS)
1534 goto cleanup_dispatch;
1538 ISC_LIST_APPEND(fctx->queries, query, link);
1539 query->fctx->nqueries++;
1540 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1541 inc_stats(res, dns_resstatscounter_queryv4);
1543 inc_stats(res, dns_resstatscounter_queryv6);
1544 if (res->view->resquerystats != NULL)
1545 dns_rdatatypestats_increment(res->view->resquerystats,
1548 return (ISC_R_SUCCESS);
1551 isc_socket_detach(&query->tcpsocket);
1554 if (query->dispatch != NULL)
1555 dns_dispatch_detach(&query->dispatch);
1558 if (query->connects == 0) {
1560 isc_mem_put(fctx->mctx, query, sizeof(*query));
1564 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1569 static isc_boolean_t
1570 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1573 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1575 sa = ISC_LIST_NEXT(sa, link)) {
1576 if (isc_sockaddr_equal(sa, address))
1584 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1587 if (bad_edns(fctx, address))
1590 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1595 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1598 static isc_boolean_t
1599 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1602 for (sa = ISC_LIST_HEAD(fctx->edns);
1604 sa = ISC_LIST_NEXT(sa, link)) {
1605 if (isc_sockaddr_equal(sa, address))
1613 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1616 if (triededns(fctx, address))
1619 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1624 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1627 static isc_boolean_t
1628 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1631 for (sa = ISC_LIST_HEAD(fctx->edns512);
1633 sa = ISC_LIST_NEXT(sa, link)) {
1634 if (isc_sockaddr_equal(sa, address))
1642 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1645 if (triededns512(fctx, address))
1648 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1653 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1657 resquery_send(resquery_t *query) {
1659 isc_result_t result;
1660 dns_name_t *qname = NULL;
1661 dns_rdataset_t *qrdataset = NULL;
1663 dns_resolver_t *res;
1665 isc_socket_t *socket;
1666 isc_buffer_t tcpbuffer;
1667 isc_sockaddr_t *address;
1668 isc_buffer_t *buffer;
1669 isc_netaddr_t ipaddr;
1670 dns_tsigkey_t *tsigkey = NULL;
1671 dns_peer_t *peer = NULL;
1672 isc_boolean_t useedns;
1673 dns_compress_t cctx;
1674 isc_boolean_t cleanup_cctx = ISC_FALSE;
1675 isc_boolean_t secure_domain;
1676 isc_boolean_t connecting = ISC_FALSE;
1677 dns_ednsopt_t ednsopts[EDNSOPTS];
1678 unsigned ednsopt = 0;
1684 task = res->buckets[fctx->bucketnum].task;
1687 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1689 * Reserve space for the TCP message length.
1691 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1692 isc_buffer_init(&query->buffer, query->data + 2,
1693 sizeof(query->data) - 2);
1694 buffer = &tcpbuffer;
1696 isc_buffer_init(&query->buffer, query->data,
1697 sizeof(query->data));
1698 buffer = &query->buffer;
1701 result = dns_message_gettempname(fctx->qmessage, &qname);
1702 if (result != ISC_R_SUCCESS)
1704 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1705 if (result != ISC_R_SUCCESS)
1709 * Get a query id from the dispatch.
1711 result = dns_dispatch_addresponse2(query->dispatch,
1712 &query->addrinfo->sockaddr,
1719 if (result != ISC_R_SUCCESS)
1722 fctx->qmessage->opcode = dns_opcode_query;
1727 dns_name_init(qname, NULL);
1728 dns_name_clone(&fctx->name, qname);
1729 dns_rdataset_init(qrdataset);
1730 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1731 ISC_LIST_APPEND(qname->list, qrdataset, link);
1732 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1737 * Set RD if the client has requested that we do a recursive query,
1738 * or if we're sending to a forwarder.
1740 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1741 ISFORWARDER(query->addrinfo))
1742 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1745 * Set CD if the client says don't validate or the question is
1746 * under a secure entry point.
1748 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1749 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1750 } else if (res->view->enablevalidation) {
1751 result = dns_view_issecuredomain(res->view, &fctx->name,
1753 if (result != ISC_R_SUCCESS)
1754 secure_domain = ISC_FALSE;
1755 if (res->view->dlv != NULL)
1756 secure_domain = ISC_TRUE;
1758 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1762 * We don't have to set opcode because it defaults to query.
1764 fctx->qmessage->id = query->id;
1767 * Convert the question to wire format.
1769 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1770 if (result != ISC_R_SUCCESS)
1771 goto cleanup_message;
1772 cleanup_cctx = ISC_TRUE;
1774 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1776 if (result != ISC_R_SUCCESS)
1777 goto cleanup_message;
1779 result = dns_message_rendersection(fctx->qmessage,
1780 DNS_SECTION_QUESTION, 0);
1781 if (result != ISC_R_SUCCESS)
1782 goto cleanup_message;
1785 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1786 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1789 * The ADB does not know about servers with "edns no". Check this,
1790 * and then inform the ADB for future use.
1792 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1794 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1797 query->options |= DNS_FETCHOPT_NOEDNS0;
1798 dns_adb_changeflags(fctx->adb, query->addrinfo,
1799 DNS_FETCHOPT_NOEDNS0,
1800 DNS_FETCHOPT_NOEDNS0);
1803 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1804 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1805 query->options |= DNS_FETCHOPT_NOEDNS0;
1808 * Handle timeouts by reducing the UDP response size to 512 bytes
1809 * then if that doesn't work disabling EDNS (includes DO) and CD.
1811 * These timeout can be due to:
1812 * * broken nameservers that don't respond to EDNS queries.
1813 * * broken/misconfigured firewalls and NAT implementations
1814 * that don't handle IP fragmentation.
1815 * * broken/misconfigured firewalls that don't handle responses
1816 * greater than 512 bytes.
1817 * * broken/misconfigured firewalls that don't handle EDNS, DO
1819 * * packet loss / link outage.
1821 if (fctx->timeout) {
1822 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1823 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1824 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1825 query->options |= DNS_FETCHOPT_NOEDNS0;
1826 fctx->reason = "disabling EDNS";
1827 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1828 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1829 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1830 query->options |= DNS_FETCHOPT_EDNS512;
1831 fctx->reason = "reducing the advertised EDNS UDP "
1832 "packet size to 512 octets";
1834 fctx->timeout = ISC_FALSE;
1838 * Use EDNS0, unless the caller doesn't want it, or we know that
1839 * the remote server doesn't like it.
1841 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1842 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1843 unsigned int version = 0; /* Default version. */
1845 isc_uint16_t udpsize = res->udpsize;
1846 isc_boolean_t reqnsid = res->view->requestnsid;
1848 flags = query->addrinfo->flags;
1849 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1850 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1851 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1853 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1855 else if (peer != NULL)
1856 (void)dns_peer_getudpsize(peer, &udpsize);
1858 /* request NSID for current view or peer? */
1860 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1862 INSIST(ednsopt < EDNSOPTS);
1863 ednsopts[ednsopt].code = DNS_OPT_NSID;
1864 ednsopts[ednsopt].length = 0;
1865 ednsopts[ednsopt].value = NULL;
1868 result = fctx_addopt(fctx->qmessage, version,
1869 udpsize, ednsopts, ednsopt);
1870 if (reqnsid && result == ISC_R_SUCCESS) {
1871 query->options |= DNS_FETCHOPT_WANTNSID;
1872 } else if (result != ISC_R_SUCCESS) {
1874 * We couldn't add the OPT, but we'll press on.
1875 * We're not using EDNS0, so set the NOEDNS0
1878 query->options |= DNS_FETCHOPT_NOEDNS0;
1882 * We know this server doesn't like EDNS0, so we
1883 * won't use it. Set the NOEDNS0 bit since we're
1886 query->options |= DNS_FETCHOPT_NOEDNS0;
1891 * If we need EDNS0 to do this query and aren't using it, we lose.
1893 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1894 result = DNS_R_SERVFAIL;
1895 goto cleanup_message;
1898 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1899 add_triededns(fctx, &query->addrinfo->sockaddr);
1901 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1902 add_triededns512(fctx, &query->addrinfo->sockaddr);
1905 * Clear CD if EDNS is not in use.
1907 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1908 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1911 * Add TSIG record tailored to the current recipient.
1913 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1914 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1915 goto cleanup_message;
1917 if (tsigkey != NULL) {
1918 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1919 dns_tsigkey_detach(&tsigkey);
1920 if (result != ISC_R_SUCCESS)
1921 goto cleanup_message;
1924 result = dns_message_rendersection(fctx->qmessage,
1925 DNS_SECTION_ADDITIONAL, 0);
1926 if (result != ISC_R_SUCCESS)
1927 goto cleanup_message;
1929 result = dns_message_renderend(fctx->qmessage);
1930 if (result != ISC_R_SUCCESS)
1931 goto cleanup_message;
1933 dns_compress_invalidate(&cctx);
1934 cleanup_cctx = ISC_FALSE;
1936 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1937 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1939 result = dns_message_getquerytsig(fctx->qmessage,
1942 if (result != ISC_R_SUCCESS)
1943 goto cleanup_message;
1947 * If using TCP, write the length of the message at the beginning
1950 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1951 isc_buffer_usedregion(&query->buffer, &r);
1952 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1953 isc_buffer_add(&tcpbuffer, r.length);
1957 * We're now done with the query message.
1959 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1961 if (query->exclusivesocket)
1962 socket = dns_dispatch_getentrysocket(query->dispentry);
1964 socket = dns_dispatch_getsocket(query->dispatch);
1968 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1969 address = &query->addrinfo->sockaddr;
1970 if (query->exclusivesocket) {
1971 result = isc_socket_connect(socket, address, task,
1972 resquery_udpconnected,
1974 if (result != ISC_R_SUCCESS)
1975 goto cleanup_message;
1976 connecting = ISC_TRUE;
1980 isc_buffer_usedregion(buffer, &r);
1983 * XXXRTH Make sure we don't send to ourselves! We should probably
1984 * prune out these addresses when we get them from the ADB.
1986 ISC_EVENT_INIT(&query->sendevent, sizeof(query->sendevent), 0, NULL,
1987 ISC_SOCKEVENT_SENDDONE, resquery_senddone, query,
1989 result = isc_socket_sendto2(socket, &r, task, address, NULL,
1990 &query->sendevent, 0);
1991 if (result != ISC_R_SUCCESS) {
1994 * This query is still connecting.
1995 * Mark it as canceled so that it will just be
1996 * cleaned up when the connected event is received.
1997 * Keep fctx around until the event is processed.
1999 query->fctx->nqueries++;
2000 query->attributes |= RESQUERY_ATTR_CANCELED;
2002 goto cleanup_message;
2009 return (ISC_R_SUCCESS);
2013 dns_compress_invalidate(&cctx);
2015 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2018 * Stop the dispatcher from listening.
2020 dns_dispatch_removeresponse(&query->dispentry, NULL);
2024 dns_message_puttempname(fctx->qmessage, &qname);
2025 if (qrdataset != NULL)
2026 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2032 resquery_connected(isc_task_t *task, isc_event_t *event) {
2033 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2034 resquery_t *query = event->ev_arg;
2035 isc_boolean_t retry = ISC_FALSE;
2036 isc_interval_t interval;
2037 isc_result_t result;
2041 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2042 REQUIRE(VALID_QUERY(query));
2044 QTRACE("connected");
2051 * Currently we don't wait for the connect event before retrying
2052 * a query. This means that if we get really behind, we may end
2053 * up doing extra work!
2059 if (RESQUERY_CANCELED(query)) {
2061 * This query was canceled while the connect() was in
2064 isc_socket_detach(&query->tcpsocket);
2065 resquery_destroy(&query);
2067 switch (sevent->result) {
2071 * Extend the idle timer for TCP. 20 seconds
2072 * should be long enough for a TCP connection to be
2073 * established, a single DNS request to be sent,
2074 * and the response received.
2076 isc_interval_set(&interval, 20, 0);
2077 result = fctx_startidletimer(query->fctx, &interval);
2078 if (result != ISC_R_SUCCESS) {
2079 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2080 fctx_done(fctx, result, __LINE__);
2084 * We are connected. Create a dispatcher and
2088 attrs |= DNS_DISPATCHATTR_TCP;
2089 attrs |= DNS_DISPATCHATTR_PRIVATE;
2090 attrs |= DNS_DISPATCHATTR_CONNECTED;
2091 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2093 attrs |= DNS_DISPATCHATTR_IPV4;
2095 attrs |= DNS_DISPATCHATTR_IPV6;
2096 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2098 result = dns_dispatch_createtcp(query->dispatchmgr,
2100 query->fctx->res->taskmgr,
2101 4096, 2, 1, 1, 3, attrs,
2105 * Regardless of whether dns_dispatch_create()
2106 * succeeded or not, we don't need our reference
2107 * to the socket anymore.
2109 isc_socket_detach(&query->tcpsocket);
2111 if (result == ISC_R_SUCCESS)
2112 result = resquery_send(query);
2114 if (result != ISC_R_SUCCESS) {
2115 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2116 fctx_done(fctx, result, __LINE__);
2120 case ISC_R_NETUNREACH:
2121 case ISC_R_HOSTUNREACH:
2122 case ISC_R_CONNREFUSED:
2124 case ISC_R_ADDRNOTAVAIL:
2125 case ISC_R_CONNECTIONRESET:
2127 * No route to remote.
2129 isc_socket_detach(&query->tcpsocket);
2130 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2135 isc_socket_detach(&query->tcpsocket);
2136 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2141 isc_event_free(&event);
2145 * Behave as if the idle timer has expired. For TCP
2146 * connections this may not actually reflect the latest timer.
2148 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2149 result = fctx_stopidletimer(fctx);
2150 if (result != ISC_R_SUCCESS)
2151 fctx_done(fctx, result, __LINE__);
2153 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2158 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2160 dns_adbfind_t *find;
2161 dns_resolver_t *res;
2162 isc_boolean_t want_try = ISC_FALSE;
2163 isc_boolean_t want_done = ISC_FALSE;
2164 isc_boolean_t bucket_empty = ISC_FALSE;
2165 unsigned int bucketnum;
2166 isc_boolean_t destroy = ISC_FALSE;
2168 find = event->ev_sender;
2169 fctx = event->ev_arg;
2170 REQUIRE(VALID_FCTX(fctx));
2175 FCTXTRACE("finddone");
2177 bucketnum = fctx->bucketnum;
2178 LOCK(&res->buckets[bucketnum].lock);
2180 INSIST(fctx->pending > 0);
2183 if (ADDRWAIT(fctx)) {
2185 * The fetch is waiting for a name to be found.
2187 INSIST(!SHUTTINGDOWN(fctx));
2188 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2189 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2190 want_try = ISC_TRUE;
2193 if (fctx->pending == 0) {
2195 * We've got nothing else to wait for and don't
2196 * know the answer. There's nothing to do but
2199 want_done = ISC_TRUE;
2202 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2203 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2205 if (fctx->references == 0) {
2206 bucket_empty = fctx_unlink(fctx);
2210 UNLOCK(&res->buckets[bucketnum].lock);
2212 isc_event_free(&event);
2213 dns_adb_destroyfind(&find);
2216 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2218 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2227 static inline isc_boolean_t
2228 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2231 for (sa = ISC_LIST_HEAD(fctx->bad);
2233 sa = ISC_LIST_NEXT(sa, link)) {
2234 if (isc_sockaddr_equal(sa, address))
2241 static inline isc_boolean_t
2242 mark_bad(fetchctx_t *fctx) {
2243 dns_adbfind_t *curr;
2244 dns_adbaddrinfo_t *addrinfo;
2245 isc_boolean_t all_bad = ISC_TRUE;
2248 * Mark all known bad servers, so we don't try to talk to them
2253 * Mark any bad nameservers.
2255 for (curr = ISC_LIST_HEAD(fctx->finds);
2257 curr = ISC_LIST_NEXT(curr, publink)) {
2258 for (addrinfo = ISC_LIST_HEAD(curr->list);
2260 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2261 if (bad_server(fctx, &addrinfo->sockaddr))
2262 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2264 all_bad = ISC_FALSE;
2269 * Mark any bad forwarders.
2271 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2273 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2274 if (bad_server(fctx, &addrinfo->sockaddr))
2275 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2277 all_bad = ISC_FALSE;
2281 * Mark any bad alternates.
2283 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2285 curr = ISC_LIST_NEXT(curr, publink)) {
2286 for (addrinfo = ISC_LIST_HEAD(curr->list);
2288 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2289 if (bad_server(fctx, &addrinfo->sockaddr))
2290 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2292 all_bad = ISC_FALSE;
2296 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
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;
2309 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2310 badnstype_t badtype)
2312 char namebuf[DNS_NAME_FORMATSIZE];
2313 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2319 const char *spc = "";
2320 isc_sockaddr_t *address = &addrinfo->sockaddr;
2322 if (reason == DNS_R_LAME)
2326 case badns_unreachable:
2329 case badns_response:
2332 case badns_validation:
2333 break; /* counted as 'valfail' */
2337 if (bad_server(fctx, address)) {
2339 * We already know this server is bad.
2344 FCTXTRACE("add_bad");
2346 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
2350 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2352 if (reason == DNS_R_LAME) /* already logged */
2355 if (reason == DNS_R_UNEXPECTEDRCODE &&
2356 fctx->rmessage->rcode == dns_rcode_servfail &&
2357 ISFORWARDER(addrinfo))
2360 if (reason == DNS_R_UNEXPECTEDRCODE) {
2361 isc_buffer_init(&b, code, sizeof(code) - 1);
2362 dns_rcode_totext(fctx->rmessage->rcode, &b);
2363 code[isc_buffer_usedlength(&b)] = '\0';
2365 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2366 isc_buffer_init(&b, code, sizeof(code) - 1);
2367 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2368 code[isc_buffer_usedlength(&b)] = '\0';
2373 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2374 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2375 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2376 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2377 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2378 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2379 "error (%s%s%s) resolving '%s/%s/%s': %s",
2380 dns_result_totext(reason), spc, code,
2381 namebuf, typebuf, classbuf, addrbuf);
2385 * Sort addrinfo list by RTT.
2388 sort_adbfind(dns_adbfind_t *find) {
2389 dns_adbaddrinfo_t *best, *curr;
2390 dns_adbaddrinfolist_t sorted;
2392 /* Lame N^2 bubble sort. */
2393 ISC_LIST_INIT(sorted);
2394 while (!ISC_LIST_EMPTY(find->list)) {
2395 best = ISC_LIST_HEAD(find->list);
2396 curr = ISC_LIST_NEXT(best, publink);
2397 while (curr != NULL) {
2398 if (curr->srtt < best->srtt)
2400 curr = ISC_LIST_NEXT(curr, publink);
2402 ISC_LIST_UNLINK(find->list, best, publink);
2403 ISC_LIST_APPEND(sorted, best, publink);
2405 find->list = sorted;
2409 * Sort a list of finds by server RTT.
2412 sort_finds(dns_adbfindlist_t *findlist) {
2413 dns_adbfind_t *best, *curr;
2414 dns_adbfindlist_t sorted;
2415 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2417 /* Sort each find's addrinfo list by SRTT. */
2418 for (curr = ISC_LIST_HEAD(*findlist);
2420 curr = ISC_LIST_NEXT(curr, publink))
2423 /* Lame N^2 bubble sort. */
2424 ISC_LIST_INIT(sorted);
2425 while (!ISC_LIST_EMPTY(*findlist)) {
2426 best = ISC_LIST_HEAD(*findlist);
2427 bestaddrinfo = ISC_LIST_HEAD(best->list);
2428 INSIST(bestaddrinfo != NULL);
2429 curr = ISC_LIST_NEXT(best, publink);
2430 while (curr != NULL) {
2431 addrinfo = ISC_LIST_HEAD(curr->list);
2432 INSIST(addrinfo != NULL);
2433 if (addrinfo->srtt < bestaddrinfo->srtt) {
2435 bestaddrinfo = addrinfo;
2437 curr = ISC_LIST_NEXT(curr, publink);
2439 ISC_LIST_UNLINK(*findlist, best, publink);
2440 ISC_LIST_APPEND(sorted, best, publink);
2446 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2447 unsigned int options, unsigned int flags, isc_stdtime_t now,
2448 isc_boolean_t *need_alternate)
2450 dns_adbaddrinfo_t *ai;
2451 dns_adbfind_t *find;
2452 dns_resolver_t *res;
2453 isc_boolean_t unshared;
2454 isc_result_t result;
2457 unshared = ISC_TF((fctx->options & DNS_FETCHOPT_UNSHARED) != 0);
2459 * If this name is a subdomain of the query domain, tell
2460 * the ADB to start looking using zone/hint data. This keeps us
2461 * from getting stuck if the nameserver is beneath the zone cut
2462 * and we don't know its address (e.g. because the A record has
2465 if (dns_name_issubdomain(name, &fctx->domain))
2466 options |= DNS_ADBFIND_STARTATZONE;
2467 options |= DNS_ADBFIND_GLUEOK;
2468 options |= DNS_ADBFIND_HINTOK;
2471 * See what we know about this address.
2474 result = dns_adb_createfind(fctx->adb,
2475 res->buckets[fctx->bucketnum].task,
2476 fctx_finddone, fctx, name,
2477 &fctx->name, fctx->type,
2479 res->view->dstport, &find);
2480 if (result != ISC_R_SUCCESS) {
2481 if (result == DNS_R_ALIAS) {
2483 * XXXRTH Follow the CNAME/DNAME chain?
2485 dns_adb_destroyfind(&find);
2488 } else if (!ISC_LIST_EMPTY(find->list)) {
2490 * We have at least some of the addresses for the
2493 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2494 if (flags != 0 || port != 0) {
2495 for (ai = ISC_LIST_HEAD(find->list);
2497 ai = ISC_LIST_NEXT(ai, publink)) {
2500 isc_sockaddr_setport(&ai->sockaddr,
2504 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2505 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2507 ISC_LIST_APPEND(fctx->finds, find, publink);
2510 * We don't know any of the addresses for this
2513 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2515 * We're looking for them and will get an
2516 * event about it later.
2522 if (need_alternate != NULL &&
2523 !*need_alternate && unshared &&
2524 ((res->dispatches4 == NULL &&
2525 find->result_v6 != DNS_R_NXDOMAIN) ||
2526 (res->dispatches6 == NULL &&
2527 find->result_v4 != DNS_R_NXDOMAIN)))
2528 *need_alternate = ISC_TRUE;
2530 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2531 fctx->lamecount++; /* cached lame server */
2533 fctx->adberr++; /* unreachable server, etc. */
2536 * If we know there are no addresses for
2537 * the family we are using then try to add
2538 * an alternative server.
2540 if (need_alternate != NULL && !*need_alternate &&
2541 ((res->dispatches4 == NULL &&
2542 find->result_v6 == DNS_R_NXRRSET) ||
2543 (res->dispatches6 == NULL &&
2544 find->result_v4 == DNS_R_NXRRSET)))
2545 *need_alternate = ISC_TRUE;
2546 dns_adb_destroyfind(&find);
2551 static isc_boolean_t
2552 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2554 unsigned int nlabels;
2555 dns_namereln_t namereln;
2557 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2558 return (ISC_TF(namereln == dns_namereln_subdomain));
2562 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2563 dns_rdata_t rdata = DNS_RDATA_INIT;
2564 isc_result_t result;
2565 dns_resolver_t *res;
2567 unsigned int stdoptions = 0;
2569 dns_adbaddrinfo_t *ai;
2570 isc_boolean_t all_bad;
2572 isc_boolean_t need_alternate = ISC_FALSE;
2574 FCTXTRACE("getaddresses");
2577 * Don't pound on remote servers. (Failsafe!)
2580 if (fctx->restarts > 10) {
2581 FCTXTRACE("too many restarts");
2582 return (DNS_R_SERVFAIL);
2591 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2592 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2595 * If this fctx has forwarders, use them; otherwise use any
2596 * selective forwarders specified in the view; otherwise use the
2597 * resolver's forwarders (if any).
2599 sa = ISC_LIST_HEAD(fctx->forwarders);
2601 dns_forwarders_t *forwarders = NULL;
2602 dns_name_t *name = &fctx->name;
2604 unsigned int labels;
2605 dns_fixedname_t fixed;
2609 * DS records are found in the parent server.
2610 * Strip label to get the correct forwarder (if any).
2612 if (dns_rdatatype_atparent(fctx->type) &&
2613 dns_name_countlabels(name) > 1) {
2614 dns_name_init(&suffix, NULL);
2615 labels = dns_name_countlabels(name);
2616 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2620 dns_fixedname_init(&fixed);
2621 domain = dns_fixedname_name(&fixed);
2622 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2623 domain, &forwarders);
2624 if (result == ISC_R_SUCCESS) {
2625 sa = ISC_LIST_HEAD(forwarders->addrs);
2626 fctx->fwdpolicy = forwarders->fwdpolicy;
2627 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2628 isstrictsubdomain(domain, &fctx->domain)) {
2629 dns_name_free(&fctx->domain, fctx->mctx);
2630 dns_name_init(&fctx->domain, NULL);
2631 result = dns_name_dup(domain, fctx->mctx,
2633 if (result != ISC_R_SUCCESS)
2639 while (sa != NULL) {
2640 if ((isc_sockaddr_pf(sa) == AF_INET &&
2641 fctx->res->dispatches4 == NULL) ||
2642 (isc_sockaddr_pf(sa) == AF_INET6 &&
2643 fctx->res->dispatches6 == NULL)) {
2644 sa = ISC_LIST_NEXT(sa, link);
2648 result = dns_adb_findaddrinfo(fctx->adb,
2649 sa, &ai, 0); /* XXXMLG */
2650 if (result == ISC_R_SUCCESS) {
2651 dns_adbaddrinfo_t *cur;
2652 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2653 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2654 while (cur != NULL && cur->srtt < ai->srtt)
2655 cur = ISC_LIST_NEXT(cur, publink);
2657 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2660 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2662 sa = ISC_LIST_NEXT(sa, link);
2666 * If the forwarding policy is "only", we don't need the addresses
2667 * of the nameservers.
2669 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2673 * Normal nameservers.
2676 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2677 if (fctx->restarts == 1) {
2679 * To avoid sending out a flood of queries likely to
2680 * result in NXRRSET, we suppress fetches for address
2681 * families we don't have the first time through,
2682 * provided that we have addresses in some family we
2685 * We don't want to set this option all the time, since
2686 * if fctx->restarts > 1, we've clearly been having trouble
2687 * with the addresses we had, so getting more could help.
2689 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2691 if (res->dispatches4 != NULL)
2692 stdoptions |= DNS_ADBFIND_INET;
2693 if (res->dispatches6 != NULL)
2694 stdoptions |= DNS_ADBFIND_INET6;
2695 isc_stdtime_get(&now);
2697 INSIST(ISC_LIST_EMPTY(fctx->finds));
2698 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2700 for (result = dns_rdataset_first(&fctx->nameservers);
2701 result == ISC_R_SUCCESS;
2702 result = dns_rdataset_next(&fctx->nameservers))
2704 dns_rdataset_current(&fctx->nameservers, &rdata);
2706 * Extract the name from the NS record.
2708 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2709 if (result != ISC_R_SUCCESS)
2712 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2714 dns_rdata_reset(&rdata);
2715 dns_rdata_freestruct(&ns);
2717 if (result != ISC_R_NOMORE)
2721 * Do we need to use 6 to 4?
2723 if (need_alternate) {
2726 family = (res->dispatches6 != NULL) ? AF_INET6 : AF_INET;
2727 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2729 a = ISC_LIST_NEXT(a, link)) {
2730 if (!a->isaddress) {
2731 findname(fctx, &a->_u._n.name, a->_u._n.port,
2732 stdoptions, FCTX_ADDRINFO_FORWARDER,
2736 if (isc_sockaddr_pf(&a->_u.addr) != family)
2739 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2741 if (result == ISC_R_SUCCESS) {
2742 dns_adbaddrinfo_t *cur;
2743 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2744 cur = ISC_LIST_HEAD(fctx->altaddrs);
2745 while (cur != NULL && cur->srtt < ai->srtt)
2746 cur = ISC_LIST_NEXT(cur, publink);
2748 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2751 ISC_LIST_APPEND(fctx->altaddrs, ai,
2759 * Mark all known bad servers.
2761 all_bad = mark_bad(fctx);
2768 * We've got no addresses.
2770 if (fctx->pending > 0) {
2772 * We're fetching the addresses, but don't have any
2773 * yet. Tell the caller to wait for an answer.
2775 result = DNS_R_WAIT;
2780 * We've lost completely. We don't know any
2781 * addresses, and the ADB has told us it can't get
2784 FCTXTRACE("no addresses");
2785 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2786 result = isc_time_nowplusinterval(&expire, &i);
2788 (fctx->type == dns_rdatatype_dnskey ||
2789 fctx->type == dns_rdatatype_dlv ||
2790 fctx->type == dns_rdatatype_ds) &&
2791 result == ISC_R_SUCCESS)
2792 dns_resolver_addbadcache(fctx->res,
2794 fctx->type, &expire);
2795 result = ISC_R_FAILURE;
2799 * We've found some addresses. We might still be looking
2800 * for more addresses.
2802 sort_finds(&fctx->finds);
2803 sort_finds(&fctx->altfinds);
2804 result = ISC_R_SUCCESS;
2811 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2814 char buf[ISC_NETADDR_FORMATSIZE];
2816 isc_boolean_t aborted = ISC_FALSE;
2817 isc_boolean_t bogus;
2818 dns_acl_t *blackhole;
2819 isc_netaddr_t ipaddr;
2820 dns_peer_t *peer = NULL;
2821 dns_resolver_t *res;
2822 const char *msg = NULL;
2824 sa = &addr->sockaddr;
2827 isc_netaddr_fromsockaddr(&ipaddr, sa);
2828 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2829 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2831 if (blackhole != NULL) {
2834 if (dns_acl_match(&ipaddr, NULL, blackhole,
2836 &match, NULL) == ISC_R_SUCCESS &&
2842 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2847 addr->flags |= FCTX_ADDRINFO_MARK;
2848 msg = "ignoring blackholed / bogus server: ";
2849 } else if (isc_sockaddr_ismulticast(sa)) {
2850 addr->flags |= FCTX_ADDRINFO_MARK;
2851 msg = "ignoring multicast address: ";
2852 } else if (isc_sockaddr_isexperimental(sa)) {
2853 addr->flags |= FCTX_ADDRINFO_MARK;
2854 msg = "ignoring experimental address: ";
2855 } else if (sa->type.sa.sa_family != AF_INET6) {
2857 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2858 addr->flags |= FCTX_ADDRINFO_MARK;
2859 msg = "ignoring IPv6 mapped IPV4 address: ";
2860 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2861 addr->flags |= FCTX_ADDRINFO_MARK;
2862 msg = "ignoring IPv6 compatibility IPV4 address: ";
2866 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2869 isc_netaddr_fromsockaddr(&na, sa);
2870 isc_netaddr_format(&na, buf, sizeof(buf));
2871 FCTXTRACE2(msg, buf);
2874 static inline dns_adbaddrinfo_t *
2875 fctx_nextaddress(fetchctx_t *fctx) {
2876 dns_adbfind_t *find, *start;
2877 dns_adbaddrinfo_t *addrinfo;
2878 dns_adbaddrinfo_t *faddrinfo;
2881 * Return the next untried address, if any.
2885 * Find the first unmarked forwarder (if any).
2887 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2889 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2890 if (!UNMARKED(addrinfo))
2892 possibly_mark(fctx, addrinfo);
2893 if (UNMARKED(addrinfo)) {
2894 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2901 * No forwarders. Move to the next find.
2904 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2908 find = ISC_LIST_HEAD(fctx->finds);
2910 find = ISC_LIST_NEXT(find, publink);
2912 find = ISC_LIST_HEAD(fctx->finds);
2916 * Find the first unmarked addrinfo.
2922 for (addrinfo = ISC_LIST_HEAD(find->list);
2924 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2925 if (!UNMARKED(addrinfo))
2927 possibly_mark(fctx, addrinfo);
2928 if (UNMARKED(addrinfo)) {
2929 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2933 if (addrinfo != NULL)
2935 find = ISC_LIST_NEXT(find, publink);
2937 find = ISC_LIST_HEAD(fctx->finds);
2938 } while (find != start);
2942 if (addrinfo != NULL)
2946 * No nameservers left. Try alternates.
2949 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2951 find = fctx->altfind;
2953 find = ISC_LIST_HEAD(fctx->altfinds);
2955 find = ISC_LIST_NEXT(find, publink);
2957 find = ISC_LIST_HEAD(fctx->altfinds);
2961 * Find the first unmarked addrinfo.
2967 for (addrinfo = ISC_LIST_HEAD(find->list);
2969 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2970 if (!UNMARKED(addrinfo))
2972 possibly_mark(fctx, addrinfo);
2973 if (UNMARKED(addrinfo)) {
2974 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2978 if (addrinfo != NULL)
2980 find = ISC_LIST_NEXT(find, publink);
2982 find = ISC_LIST_HEAD(fctx->altfinds);
2983 } while (find != start);
2986 faddrinfo = addrinfo;
2989 * See if we have a better alternate server by address.
2992 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2994 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2995 if (!UNMARKED(addrinfo))
2997 possibly_mark(fctx, addrinfo);
2998 if (UNMARKED(addrinfo) &&
2999 (faddrinfo == NULL ||
3000 addrinfo->srtt < faddrinfo->srtt)) {
3001 if (faddrinfo != NULL)
3002 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3003 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3008 if (addrinfo == NULL) {
3009 addrinfo = faddrinfo;
3010 fctx->altfind = find;
3017 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3018 isc_result_t result;
3019 dns_adbaddrinfo_t *addrinfo;
3023 REQUIRE(!ADDRWAIT(fctx));
3025 addrinfo = fctx_nextaddress(fctx);
3026 if (addrinfo == NULL) {
3028 * We have no more addresses. Start over.
3030 fctx_cancelqueries(fctx, ISC_TRUE);
3031 fctx_cleanupfinds(fctx);
3032 fctx_cleanupaltfinds(fctx);
3033 fctx_cleanupforwaddrs(fctx);
3034 fctx_cleanupaltaddrs(fctx);
3035 result = fctx_getaddresses(fctx, badcache);
3036 if (result == DNS_R_WAIT) {
3038 * Sleep waiting for addresses.
3040 FCTXTRACE("addrwait");
3041 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3043 } else if (result != ISC_R_SUCCESS) {
3045 * Something bad happened.
3047 fctx_done(fctx, result, __LINE__);
3051 addrinfo = fctx_nextaddress(fctx);
3053 * While we may have addresses from the ADB, they
3054 * might be bad ones. In this case, return SERVFAIL.
3056 if (addrinfo == NULL) {
3057 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3062 result = fctx_query(fctx, addrinfo, fctx->options);
3063 if (result != ISC_R_SUCCESS)
3064 fctx_done(fctx, result, __LINE__);
3066 inc_stats(fctx->res, dns_resstatscounter_retry);
3069 static isc_boolean_t
3070 fctx_unlink(fetchctx_t *fctx) {
3071 dns_resolver_t *res;
3072 unsigned int bucketnum;
3075 * Caller must be holding the bucket lock.
3078 REQUIRE(VALID_FCTX(fctx));
3079 REQUIRE(fctx->state == fetchstate_done ||
3080 fctx->state == fetchstate_init);
3081 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3082 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3083 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3084 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3085 REQUIRE(fctx->pending == 0);
3086 REQUIRE(fctx->references == 0);
3087 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3089 FCTXTRACE("unlink");
3092 bucketnum = fctx->bucketnum;
3094 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3098 UNLOCK(&res->nlock);
3100 if (res->buckets[bucketnum].exiting &&
3101 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3108 fctx_destroy(fetchctx_t *fctx) {
3109 isc_sockaddr_t *sa, *next_sa;
3111 REQUIRE(VALID_FCTX(fctx));
3112 REQUIRE(fctx->state == fetchstate_done ||
3113 fctx->state == fetchstate_init);
3114 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3115 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3116 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3117 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3118 REQUIRE(fctx->pending == 0);
3119 REQUIRE(fctx->references == 0);
3120 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3121 REQUIRE(!ISC_LINK_LINKED(fctx, link));
3123 FCTXTRACE("destroy");
3128 for (sa = ISC_LIST_HEAD(fctx->bad);
3131 next_sa = ISC_LIST_NEXT(sa, link);
3132 ISC_LIST_UNLINK(fctx->bad, sa, link);
3133 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3136 for (sa = ISC_LIST_HEAD(fctx->edns);
3139 next_sa = ISC_LIST_NEXT(sa, link);
3140 ISC_LIST_UNLINK(fctx->edns, sa, link);
3141 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3144 for (sa = ISC_LIST_HEAD(fctx->edns512);
3147 next_sa = ISC_LIST_NEXT(sa, link);
3148 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3149 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3152 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3155 next_sa = ISC_LIST_NEXT(sa, link);
3156 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3157 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3160 isc_timer_detach(&fctx->timer);
3161 dns_message_destroy(&fctx->rmessage);
3162 dns_message_destroy(&fctx->qmessage);
3163 if (dns_name_countlabels(&fctx->domain) > 0)
3164 dns_name_free(&fctx->domain, fctx->mctx);
3165 if (dns_rdataset_isassociated(&fctx->nameservers))
3166 dns_rdataset_disassociate(&fctx->nameservers);
3167 dns_name_free(&fctx->name, fctx->mctx);
3168 dns_db_detach(&fctx->cache);
3169 dns_adb_detach(&fctx->adb);
3170 isc_mem_free(fctx->mctx, fctx->info);
3171 isc_mem_putanddetach(&fctx->mctx, fctx, sizeof(*fctx));
3175 * Fetch event handlers.
3179 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3180 fetchctx_t *fctx = event->ev_arg;
3181 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3184 REQUIRE(VALID_FCTX(fctx));
3188 FCTXTRACE("timeout");
3190 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3192 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3193 fctx->reason = NULL;
3194 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3196 isc_result_t result;
3199 fctx->timeout = ISC_TRUE;
3201 * We could cancel the running queries here, or we could let
3202 * them keep going. Since we normally use separate sockets for
3203 * different queries, we adopt the former approach to reduce
3204 * the number of open sockets: cancel the oldest query if it
3205 * expired after the query had started (this is usually the
3206 * case but is not always so, depending on the task schedule
3209 query = ISC_LIST_HEAD(fctx->queries);
3210 if (query != NULL &&
3211 isc_time_compare(&tevent->due, &query->start) >= 0) {
3212 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3214 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3216 * Our timer has triggered. Reestablish the fctx lifetime
3219 result = fctx_starttimer(fctx);
3220 if (result != ISC_R_SUCCESS)
3221 fctx_done(fctx, result, __LINE__);
3226 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3229 isc_event_free(&event);
3233 fctx_shutdown(fetchctx_t *fctx) {
3234 isc_event_t *cevent;
3237 * Start the shutdown process for fctx, if it isn't already underway.
3240 FCTXTRACE("shutdown");
3243 * The caller must be holding the appropriate bucket lock.
3246 if (fctx->want_shutdown)
3249 fctx->want_shutdown = ISC_TRUE;
3252 * Unless we're still initializing (in which case the
3253 * control event is still outstanding), we need to post
3254 * the control event to tell the fetch we want it to
3257 if (fctx->state != fetchstate_init) {
3258 cevent = &fctx->control_event;
3259 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3265 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3266 fetchctx_t *fctx = event->ev_arg;
3267 isc_boolean_t bucket_empty = ISC_FALSE;
3268 dns_resolver_t *res;
3269 unsigned int bucketnum;
3270 dns_validator_t *validator;
3271 isc_boolean_t destroy = ISC_FALSE;
3273 REQUIRE(VALID_FCTX(fctx));
3278 bucketnum = fctx->bucketnum;
3280 FCTXTRACE("doshutdown");
3283 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3285 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3288 * Cancel all pending validators. Note that this must be done
3289 * without the bucket lock held, since that could cause deadlock.
3291 validator = ISC_LIST_HEAD(fctx->validators);
3292 while (validator != NULL) {
3293 dns_validator_cancel(validator);
3294 validator = ISC_LIST_NEXT(validator, link);
3297 if (fctx->nsfetch != NULL)
3298 dns_resolver_cancelfetch(fctx->nsfetch);
3301 * Shut down anything that is still running on behalf of this
3302 * fetch. To avoid deadlock with the ADB, we must do this
3303 * before we lock the bucket lock.
3305 fctx_stopeverything(fctx, ISC_FALSE);
3307 LOCK(&res->buckets[bucketnum].lock);
3309 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3311 INSIST(fctx->state == fetchstate_active ||
3312 fctx->state == fetchstate_done);
3313 INSIST(fctx->want_shutdown);
3315 if (fctx->state != fetchstate_done) {
3316 fctx->state = fetchstate_done;
3317 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3320 if (fctx->references == 0 && fctx->pending == 0 &&
3321 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3322 bucket_empty = fctx_unlink(fctx);
3326 UNLOCK(&res->buckets[bucketnum].lock);
3336 fctx_start(isc_task_t *task, isc_event_t *event) {
3337 fetchctx_t *fctx = event->ev_arg;
3338 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3339 dns_resolver_t *res;
3340 unsigned int bucketnum;
3341 isc_boolean_t destroy = ISC_FALSE;
3343 REQUIRE(VALID_FCTX(fctx));
3348 bucketnum = fctx->bucketnum;
3352 LOCK(&res->buckets[bucketnum].lock);
3354 INSIST(fctx->state == fetchstate_init);
3355 if (fctx->want_shutdown) {
3357 * We haven't started this fctx yet, and we've been requested
3360 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3361 fctx->state = fetchstate_done;
3362 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3364 * Since we haven't started, we INSIST that we have no
3365 * pending ADB finds and no pending validations.
3367 INSIST(fctx->pending == 0);
3368 INSIST(fctx->nqueries == 0);
3369 INSIST(ISC_LIST_EMPTY(fctx->validators));
3370 if (fctx->references == 0) {
3372 * It's now safe to destroy this fctx.
3374 bucket_empty = fctx_unlink(fctx);
3380 * Normal fctx startup.
3382 fctx->state = fetchstate_active;
3384 * Reset the control event for later use in shutting down
3387 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3388 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3392 UNLOCK(&res->buckets[bucketnum].lock);
3395 isc_result_t result;
3400 * All is well. Start working on the fetch.
3402 result = fctx_starttimer(fctx);
3403 if (result != ISC_R_SUCCESS)
3404 fctx_done(fctx, result, __LINE__);
3406 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3407 } else if (destroy) {
3415 * Fetch Creation, Joining, and Cancelation.
3418 static inline isc_result_t
3419 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3420 dns_messageid_t id, isc_taskaction_t action, void *arg,
3421 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3425 dns_fetchevent_t *event;
3430 * We store the task we're going to send this event to in the
3431 * sender field. We'll make the fetch the sender when we actually
3435 isc_task_attach(task, &clone);
3436 event = (dns_fetchevent_t *)
3437 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3438 action, arg, sizeof(*event));
3439 if (event == NULL) {
3440 isc_task_detach(&clone);
3441 return (ISC_R_NOMEMORY);
3443 event->result = DNS_R_SERVFAIL;
3444 event->qtype = fctx->type;
3447 event->rdataset = rdataset;
3448 event->sigrdataset = sigrdataset;
3449 event->fetch = fetch;
3450 event->client = client;
3452 dns_fixedname_init(&event->foundname);
3455 * Make sure that we can store the sigrdataset in the
3456 * first event if it is needed by any of the events.
3458 if (event->sigrdataset != NULL)
3459 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3461 ISC_LIST_APPEND(fctx->events, event, ev_link);
3463 fctx->client = client;
3465 fetch->magic = DNS_FETCH_MAGIC;
3466 fetch->private = fctx;
3468 return (ISC_R_SUCCESS);
3472 log_ns_ttl(fetchctx_t *fctx, const char *where) {
3473 char namebuf[DNS_NAME_FORMATSIZE];
3474 char domainbuf[DNS_NAME_FORMATSIZE];
3476 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3477 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3478 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3479 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
3480 "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u",
3481 fctx, where, namebuf, domainbuf,
3482 fctx->ns_ttl_ok, fctx->ns_ttl);
3486 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3487 dns_name_t *domain, dns_rdataset_t *nameservers,
3488 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3491 isc_result_t result;
3492 isc_result_t iresult;
3493 isc_interval_t interval;
3494 dns_fixedname_t fixed;
3495 unsigned int findoptions = 0;
3496 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3497 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3502 * Caller must be holding the lock for bucket number 'bucketnum'.
3504 REQUIRE(fctxp != NULL && *fctxp == NULL);
3506 mctx = res->buckets[bucketnum].mctx;
3507 fctx = isc_mem_get(mctx, sizeof(*fctx));
3509 return (ISC_R_NOMEMORY);
3510 dns_name_format(name, buf, sizeof(buf));
3511 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3512 strcat(buf, "/"); /* checked */
3513 strcat(buf, typebuf); /* checked */
3514 fctx->info = isc_mem_strdup(mctx, buf);
3515 if (fctx->info == NULL) {
3516 result = ISC_R_NOMEMORY;
3519 FCTXTRACE("create");
3520 dns_name_init(&fctx->name, NULL);
3521 result = dns_name_dup(name, mctx, &fctx->name);
3522 if (result != ISC_R_SUCCESS)
3524 dns_name_init(&fctx->domain, NULL);
3525 dns_rdataset_init(&fctx->nameservers);
3528 fctx->options = options;
3530 * Note! We do not attach to the task. We are relying on the
3531 * resolver to ensure that this task doesn't go away while we are
3535 fctx->references = 0;
3536 fctx->bucketnum = bucketnum;
3537 fctx->state = fetchstate_init;
3538 fctx->want_shutdown = ISC_FALSE;
3539 fctx->cloned = ISC_FALSE;
3540 ISC_LIST_INIT(fctx->queries);
3541 ISC_LIST_INIT(fctx->finds);
3542 ISC_LIST_INIT(fctx->altfinds);
3543 ISC_LIST_INIT(fctx->forwaddrs);
3544 ISC_LIST_INIT(fctx->altaddrs);
3545 ISC_LIST_INIT(fctx->forwarders);
3546 fctx->fwdpolicy = dns_fwdpolicy_none;
3547 ISC_LIST_INIT(fctx->bad);
3548 ISC_LIST_INIT(fctx->edns);
3549 ISC_LIST_INIT(fctx->edns512);
3550 ISC_LIST_INIT(fctx->bad_edns);
3551 ISC_LIST_INIT(fctx->validators);
3552 fctx->validator = NULL;
3554 fctx->altfind = NULL;
3557 fctx->querysent = 0;
3558 fctx->referrals = 0;
3559 TIME_NOW(&fctx->start);
3561 fctx->lamecount = 0;
3567 fctx->result = ISC_R_FAILURE;
3568 fctx->vresult = ISC_R_SUCCESS;
3569 fctx->exitline = -1; /* sentinel */
3570 fctx->logged = ISC_FALSE;
3571 fctx->attributes = 0;
3572 fctx->spilled = ISC_FALSE;
3574 fctx->reason = NULL;
3576 fctx->rand_bits = 0;
3577 fctx->timeout = ISC_FALSE;
3578 fctx->addrinfo = NULL;
3579 fctx->client = NULL;
3581 fctx->ns_ttl_ok = ISC_FALSE;
3583 dns_name_init(&fctx->nsname, NULL);
3584 fctx->nsfetch = NULL;
3585 dns_rdataset_init(&fctx->nsrrset);
3587 if (domain == NULL) {
3588 dns_forwarders_t *forwarders = NULL;
3589 unsigned int labels;
3590 dns_name_t *fwdname = name;
3593 * DS records are found in the parent server.
3594 * Strip label to get the correct forwarder (if any).
3596 if (dns_rdatatype_atparent(fctx->type) &&
3597 dns_name_countlabels(name) > 1) {
3598 dns_name_init(&suffix, NULL);
3599 labels = dns_name_countlabels(name);
3600 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3603 dns_fixedname_init(&fixed);
3604 domain = dns_fixedname_name(&fixed);
3605 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3606 domain, &forwarders);
3607 if (result == ISC_R_SUCCESS)
3608 fctx->fwdpolicy = forwarders->fwdpolicy;
3610 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3612 * The caller didn't supply a query domain and
3613 * nameservers, and we're not in forward-only mode,
3614 * so find the best nameservers to use.
3616 if (dns_rdatatype_atparent(fctx->type))
3617 findoptions |= DNS_DBFIND_NOEXACT;
3618 result = dns_view_findzonecut(res->view, fwdname,
3619 domain, 0, findoptions,
3623 if (result != ISC_R_SUCCESS)
3626 result = dns_name_dup(domain, mctx, &fctx->domain);
3627 if (result != ISC_R_SUCCESS) {
3628 dns_rdataset_disassociate(&fctx->nameservers);
3631 fctx->ns_ttl = fctx->nameservers.ttl;
3632 fctx->ns_ttl_ok = ISC_TRUE;
3635 * We're in forward-only mode. Set the query domain.
3637 result = dns_name_dup(domain, mctx, &fctx->domain);
3638 if (result != ISC_R_SUCCESS)
3642 result = dns_name_dup(domain, mctx, &fctx->domain);
3643 if (result != ISC_R_SUCCESS)
3645 dns_rdataset_clone(nameservers, &fctx->nameservers);
3646 fctx->ns_ttl = fctx->nameservers.ttl;
3647 fctx->ns_ttl_ok = ISC_TRUE;
3650 log_ns_ttl(fctx, "fctx_create");
3652 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3654 fctx->qmessage = NULL;
3655 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
3658 if (result != ISC_R_SUCCESS)
3659 goto cleanup_domain;
3661 fctx->rmessage = NULL;
3662 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
3665 if (result != ISC_R_SUCCESS)
3666 goto cleanup_qmessage;
3669 * Compute an expiration time for the entire fetch.
3671 isc_interval_set(&interval, res->query_timeout, 0);
3672 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3673 if (iresult != ISC_R_SUCCESS) {
3674 UNEXPECTED_ERROR(__FILE__, __LINE__,
3675 "isc_time_nowplusinterval: %s",
3676 isc_result_totext(iresult));
3677 result = ISC_R_UNEXPECTED;
3678 goto cleanup_rmessage;
3682 * Default retry interval initialization. We set the interval now
3683 * mostly so it won't be uninitialized. It will be set to the
3684 * correct value before a query is issued.
3686 isc_interval_set(&fctx->interval, 2, 0);
3689 * Create an inactive timer. It will be made active when the fetch
3690 * is actually started.
3693 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3695 res->buckets[bucketnum].task, fctx_timeout,
3696 fctx, &fctx->timer);
3697 if (iresult != ISC_R_SUCCESS) {
3698 UNEXPECTED_ERROR(__FILE__, __LINE__,
3699 "isc_timer_create: %s",
3700 isc_result_totext(iresult));
3701 result = ISC_R_UNEXPECTED;
3702 goto cleanup_rmessage;
3706 * Attach to the view's cache and adb.
3709 dns_db_attach(res->view->cachedb, &fctx->cache);
3711 dns_adb_attach(res->view->adb, &fctx->adb);
3713 isc_mem_attach(mctx, &fctx->mctx);
3715 ISC_LIST_INIT(fctx->events);
3716 ISC_LINK_INIT(fctx, link);
3717 fctx->magic = FCTX_MAGIC;
3719 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3723 UNLOCK(&res->nlock);
3727 return (ISC_R_SUCCESS);
3730 dns_message_destroy(&fctx->rmessage);
3733 dns_message_destroy(&fctx->qmessage);
3736 if (dns_name_countlabels(&fctx->domain) > 0)
3737 dns_name_free(&fctx->domain, mctx);
3738 if (dns_rdataset_isassociated(&fctx->nameservers))
3739 dns_rdataset_disassociate(&fctx->nameservers);
3742 dns_name_free(&fctx->name, mctx);
3745 isc_mem_free(mctx, fctx->info);
3748 isc_mem_put(mctx, fctx, sizeof(*fctx));
3756 static inline isc_boolean_t
3757 is_lame(fetchctx_t *fctx) {
3758 dns_message_t *message = fctx->rmessage;
3760 dns_rdataset_t *rdataset;
3761 isc_result_t result;
3763 if (message->rcode != dns_rcode_noerror &&
3764 message->rcode != dns_rcode_nxdomain)
3767 if (message->counts[DNS_SECTION_ANSWER] != 0)
3770 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3773 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3774 while (result == ISC_R_SUCCESS) {
3776 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3777 for (rdataset = ISC_LIST_HEAD(name->list);
3779 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3780 dns_namereln_t namereln;
3782 unsigned int labels;
3783 if (rdataset->type != dns_rdatatype_ns)
3785 namereln = dns_name_fullcompare(name, &fctx->domain,
3787 if (namereln == dns_namereln_equal &&
3788 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3790 if (namereln == dns_namereln_subdomain)
3794 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3801 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3802 char namebuf[DNS_NAME_FORMATSIZE];
3803 char domainbuf[DNS_NAME_FORMATSIZE];
3804 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3806 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3807 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3808 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3809 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3810 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3811 "lame server resolving '%s' (in '%s'?): %s",
3812 namebuf, domainbuf, addrbuf);
3816 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3817 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3818 char clbuf[ISC_SOCKADDR_FORMATSIZE];
3819 const char *clmsg = "";
3823 va_start(args, format);
3824 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3827 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3829 if (fctx->client != NULL) {
3830 clmsg = " for client ";
3831 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3836 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3837 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3838 "DNS format error from %s resolving %s%s%s: %s",
3839 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3842 static inline isc_result_t
3843 same_question(fetchctx_t *fctx) {
3844 isc_result_t result;
3845 dns_message_t *message = fctx->rmessage;
3847 dns_rdataset_t *rdataset;
3850 * Caller must be holding the fctx lock.
3854 * XXXRTH Currently we support only one question.
3856 if (message->counts[DNS_SECTION_QUESTION] != 1) {
3857 log_formerr(fctx, "too many questions");
3858 return (DNS_R_FORMERR);
3861 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3862 if (result != ISC_R_SUCCESS)
3865 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3866 rdataset = ISC_LIST_HEAD(name->list);
3867 INSIST(rdataset != NULL);
3868 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3870 if (fctx->type != rdataset->type ||
3871 fctx->res->rdclass != rdataset->rdclass ||
3872 !dns_name_equal(&fctx->name, name)) {
3873 char namebuf[DNS_NAME_FORMATSIZE];
3874 char class[DNS_RDATACLASS_FORMATSIZE];
3875 char type[DNS_RDATATYPE_FORMATSIZE];
3877 dns_name_format(name, namebuf, sizeof(namebuf));
3878 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3879 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3880 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3881 namebuf, class, type);
3882 return (DNS_R_FORMERR);
3885 return (ISC_R_SUCCESS);
3889 clone_results(fetchctx_t *fctx) {
3890 dns_fetchevent_t *event, *hevent;
3891 isc_result_t result;
3892 dns_name_t *name, *hname;
3894 FCTXTRACE("clone_results");
3897 * Set up any other events to have the same data as the first
3900 * Caller must be holding the appropriate lock.
3903 fctx->cloned = ISC_TRUE;
3904 hevent = ISC_LIST_HEAD(fctx->events);
3907 hname = dns_fixedname_name(&hevent->foundname);
3908 for (event = ISC_LIST_NEXT(hevent, ev_link);
3910 event = ISC_LIST_NEXT(event, ev_link)) {
3911 name = dns_fixedname_name(&event->foundname);
3912 result = dns_name_copy(hname, name, NULL);
3913 if (result != ISC_R_SUCCESS)
3914 event->result = result;
3916 event->result = hevent->result;
3917 dns_db_attach(hevent->db, &event->db);
3918 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3919 INSIST(hevent->rdataset != NULL);
3920 INSIST(event->rdataset != NULL);
3921 if (dns_rdataset_isassociated(hevent->rdataset))
3922 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3923 INSIST(! (hevent->sigrdataset == NULL &&
3924 event->sigrdataset != NULL));
3925 if (hevent->sigrdataset != NULL &&
3926 dns_rdataset_isassociated(hevent->sigrdataset) &&
3927 event->sigrdataset != NULL)
3928 dns_rdataset_clone(hevent->sigrdataset,
3929 event->sigrdataset);
3933 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3934 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3935 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3936 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3937 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3938 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3939 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3943 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3944 * no references and is no longer waiting for any events).
3947 * '*fctx' is shutting down.
3950 * true if the resolver is exiting and this is the last fctx in the bucket.
3952 static isc_boolean_t
3953 maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) {
3954 unsigned int bucketnum;
3955 isc_boolean_t bucket_empty = ISC_FALSE;
3956 dns_resolver_t *res = fctx->res;
3957 dns_validator_t *validator, *next_validator;
3958 isc_boolean_t destroy = ISC_FALSE;
3960 REQUIRE(SHUTTINGDOWN(fctx));
3962 bucketnum = fctx->bucketnum;
3964 LOCK(&res->buckets[bucketnum].lock);
3965 if (fctx->pending != 0 || fctx->nqueries != 0)
3968 for (validator = ISC_LIST_HEAD(fctx->validators);
3969 validator != NULL; validator = next_validator) {
3970 next_validator = ISC_LIST_NEXT(validator, link);
3971 dns_validator_cancel(validator);
3974 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3975 bucket_empty = fctx_unlink(fctx);
3980 UNLOCK(&res->buckets[bucketnum].lock);
3983 return (bucket_empty);
3987 * The validator has finished.
3990 validated(isc_task_t *task, isc_event_t *event) {
3991 dns_adbaddrinfo_t *addrinfo;
3992 dns_dbnode_t *node = NULL;
3993 dns_dbnode_t *nsnode = NULL;
3994 dns_fetchevent_t *hevent;
3996 dns_rdataset_t *ardataset = NULL;
3997 dns_rdataset_t *asigrdataset = NULL;
3998 dns_rdataset_t *rdataset;
3999 dns_rdataset_t *sigrdataset;
4000 dns_resolver_t *res;
4001 dns_valarg_t *valarg;
4002 dns_validatorevent_t *vevent;
4004 isc_boolean_t chaining;
4005 isc_boolean_t negative;
4006 isc_boolean_t sentresponse;
4007 isc_result_t eresult = ISC_R_SUCCESS;
4008 isc_result_t result = ISC_R_SUCCESS;
4012 UNUSED(task); /* for now */
4014 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
4015 valarg = event->ev_arg;
4016 fctx = valarg->fctx;
4018 addrinfo = valarg->addrinfo;
4019 REQUIRE(VALID_FCTX(fctx));
4020 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
4022 vevent = (dns_validatorevent_t *)event;
4023 fctx->vresult = vevent->result;
4025 FCTXTRACE("received validation completion event");
4027 LOCK(&res->buckets[fctx->bucketnum].lock);
4029 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
4030 fctx->validator = NULL;
4033 * Destroy the validator early so that we can
4034 * destroy the fctx if necessary.
4036 dns_validator_destroy(&vevent->validator);
4037 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
4039 negative = ISC_TF(vevent->rdataset == NULL);
4041 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
4044 * If shutting down, ignore the results. Check to see if we're
4045 * done waiting for validator completions and ADB pending events; if
4046 * so, destroy the fctx.
4048 if (SHUTTINGDOWN(fctx) && !sentresponse) {
4049 isc_uint32_t bucketnum = fctx->bucketnum;
4050 isc_boolean_t bucket_empty;
4051 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4052 UNLOCK(&res->buckets[bucketnum].lock);
4058 isc_stdtime_get(&now);
4061 * If chaining, we need to make sure that the right result code is
4062 * returned, and that the rdatasets are bound.
4064 if (vevent->result == ISC_R_SUCCESS &&
4066 vevent->rdataset != NULL &&
4067 CHAINING(vevent->rdataset))
4069 if (vevent->rdataset->type == dns_rdatatype_cname)
4070 eresult = DNS_R_CNAME;
4072 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4073 eresult = DNS_R_DNAME;
4075 chaining = ISC_TRUE;
4077 chaining = ISC_FALSE;
4080 * Either we're not shutting down, or we are shutting down but want
4081 * to cache the result anyway (if this was a validation started by
4082 * a query with cd set)
4085 hevent = ISC_LIST_HEAD(fctx->events);
4086 if (hevent != NULL) {
4087 if (!negative && !chaining &&
4088 (fctx->type == dns_rdatatype_any ||
4089 fctx->type == dns_rdatatype_rrsig ||
4090 fctx->type == dns_rdatatype_sig)) {
4092 * Don't bind rdatasets; the caller
4093 * will iterate the node.
4096 ardataset = hevent->rdataset;
4097 asigrdataset = hevent->sigrdataset;
4101 if (vevent->result != ISC_R_SUCCESS) {
4102 FCTXTRACE("validation failed");
4103 inc_stats(res, dns_resstatscounter_valfail);
4105 fctx->vresult = vevent->result;
4106 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4107 result = ISC_R_NOTFOUND;
4108 if (vevent->rdataset != NULL)
4109 result = dns_db_findnode(fctx->cache,
4112 if (result == ISC_R_SUCCESS)
4113 (void)dns_db_deleterdataset(fctx->cache, node,
4116 if (result == ISC_R_SUCCESS &&
4117 vevent->sigrdataset != NULL)
4118 (void)dns_db_deleterdataset(fctx->cache, node,
4120 dns_rdatatype_rrsig,
4122 if (result == ISC_R_SUCCESS)
4123 dns_db_detachnode(fctx->cache, &node);
4125 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4127 * Cache the data as pending for later validation.
4129 result = ISC_R_NOTFOUND;
4130 if (vevent->rdataset != NULL)
4131 result = dns_db_findnode(fctx->cache,
4134 if (result == ISC_R_SUCCESS) {
4135 (void)dns_db_addrdataset(fctx->cache, node,
4137 vevent->rdataset, 0,
4140 if (result == ISC_R_SUCCESS &&
4141 vevent->sigrdataset != NULL)
4142 (void)dns_db_addrdataset(fctx->cache, node,
4144 vevent->sigrdataset,
4146 if (result == ISC_R_SUCCESS)
4147 dns_db_detachnode(fctx->cache, &node);
4149 result = fctx->vresult;
4150 add_bad(fctx, addrinfo, result, badns_validation);
4151 isc_event_free(&event);
4152 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4153 INSIST(fctx->validator == NULL);
4154 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4155 if (fctx->validator != NULL)
4156 dns_validator_send(fctx->validator);
4157 else if (sentresponse)
4158 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4159 else if (result == DNS_R_BROKENCHAIN) {
4160 isc_result_t tresult;
4164 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4165 tresult = isc_time_nowplusinterval(&expire, &i);
4167 (fctx->type == dns_rdatatype_dnskey ||
4168 fctx->type == dns_rdatatype_dlv ||
4169 fctx->type == dns_rdatatype_ds) &&
4170 tresult == ISC_R_SUCCESS)
4171 dns_resolver_addbadcache(res, &fctx->name,
4172 fctx->type, &expire);
4173 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4175 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4181 dns_rdatatype_t covers;
4182 FCTXTRACE("nonexistence validation OK");
4184 inc_stats(res, dns_resstatscounter_valnegsuccess);
4186 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4187 covers = dns_rdatatype_any;
4189 covers = fctx->type;
4191 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4193 if (result != ISC_R_SUCCESS)
4194 goto noanswer_response;
4197 * If we are asking for a SOA record set the cache time
4198 * to zero to facilitate locating the containing zone of
4201 ttl = res->view->maxncachettl;
4202 if (fctx->type == dns_rdatatype_soa &&
4203 covers == dns_rdatatype_any && res->zero_no_soa_ttl)
4206 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4207 covers, now, ttl, vevent->optout,
4208 vevent->secure, ardataset, &eresult);
4209 if (result != ISC_R_SUCCESS)
4210 goto noanswer_response;
4211 goto answer_response;
4213 inc_stats(res, dns_resstatscounter_valsuccess);
4215 FCTXTRACE("validation OK");
4217 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4218 result = dns_rdataset_addnoqname(vevent->rdataset,
4219 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4220 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4221 INSIST(vevent->sigrdataset != NULL);
4222 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4223 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4224 result = dns_rdataset_addclosest(vevent->rdataset,
4225 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4226 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4228 } else if (vevent->rdataset->trust == dns_trust_answer &&
4229 vevent->rdataset->type != dns_rdatatype_rrsig)
4231 isc_result_t tresult;
4232 dns_name_t *noqname = NULL;
4233 tresult = findnoqname(fctx, vevent->name,
4234 vevent->rdataset->type, &noqname);
4235 if (tresult == ISC_R_SUCCESS && noqname != NULL) {
4236 tresult = dns_rdataset_addnoqname(vevent->rdataset,
4238 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4243 * The data was already cached as pending data.
4244 * Re-cache it as secure and bind the cached
4245 * rdatasets to the first event on the fetch
4248 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4249 if (result != ISC_R_SUCCESS)
4250 goto noanswer_response;
4252 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4253 vevent->rdataset, 0, ardataset);
4254 if (result != ISC_R_SUCCESS &&
4255 result != DNS_R_UNCHANGED)
4256 goto noanswer_response;
4257 if (ardataset != NULL && NEGATIVE(ardataset)) {
4258 if (NXDOMAIN(ardataset))
4259 eresult = DNS_R_NCACHENXDOMAIN;
4261 eresult = DNS_R_NCACHENXRRSET;
4262 } else if (vevent->sigrdataset != NULL) {
4263 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4264 vevent->sigrdataset, 0,
4266 if (result != ISC_R_SUCCESS &&
4267 result != DNS_R_UNCHANGED)
4268 goto noanswer_response;
4272 isc_boolean_t bucket_empty = ISC_FALSE;
4274 * If we only deferred the destroy because we wanted to cache
4275 * the data, destroy now.
4277 dns_db_detachnode(fctx->cache, &node);
4278 if (SHUTTINGDOWN(fctx))
4279 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4280 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4286 if (!ISC_LIST_EMPTY(fctx->validators)) {
4288 INSIST(fctx->type == dns_rdatatype_any ||
4289 fctx->type == dns_rdatatype_rrsig ||
4290 fctx->type == dns_rdatatype_sig);
4292 * Don't send a response yet - we have
4293 * more rdatasets that still need to
4296 dns_db_detachnode(fctx->cache, &node);
4297 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4298 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4304 * Cache any NS/NSEC records that happened to be validated.
4306 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4307 while (result == ISC_R_SUCCESS) {
4309 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4311 for (rdataset = ISC_LIST_HEAD(name->list);
4313 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4314 if ((rdataset->type != dns_rdatatype_ns &&
4315 rdataset->type != dns_rdatatype_nsec) ||
4316 rdataset->trust != dns_trust_secure)
4318 for (sigrdataset = ISC_LIST_HEAD(name->list);
4319 sigrdataset != NULL;
4320 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4321 if (sigrdataset->type != dns_rdatatype_rrsig ||
4322 sigrdataset->covers != rdataset->type)
4326 if (sigrdataset == NULL ||
4327 sigrdataset->trust != dns_trust_secure)
4329 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4331 if (result != ISC_R_SUCCESS)
4334 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4335 now, rdataset, 0, NULL);
4336 if (result == ISC_R_SUCCESS)
4337 result = dns_db_addrdataset(fctx->cache, nsnode,
4341 dns_db_detachnode(fctx->cache, &nsnode);
4342 if (result != ISC_R_SUCCESS)
4345 result = dns_message_nextname(fctx->rmessage,
4346 DNS_SECTION_AUTHORITY);
4349 result = ISC_R_SUCCESS;
4352 * Respond with an answer, positive or negative,
4353 * as opposed to an error. 'node' must be non-NULL.
4356 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4358 if (hevent != NULL) {
4360 * Negative results must be indicated in event->result.
4362 if (dns_rdataset_isassociated(hevent->rdataset) &&
4363 NEGATIVE(hevent->rdataset)) {
4364 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4365 eresult == DNS_R_NCACHENXRRSET);
4367 hevent->result = eresult;
4368 RUNTIME_CHECK(dns_name_copy(vevent->name,
4369 dns_fixedname_name(&hevent->foundname), NULL)
4371 dns_db_attach(fctx->cache, &hevent->db);
4372 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4373 clone_results(fctx);
4378 dns_db_detachnode(fctx->cache, &node);
4380 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4381 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4384 INSIST(node == NULL);
4385 isc_event_free(&event);
4389 fctx_log(void *arg, int level, const char *fmt, ...) {
4392 fetchctx_t *fctx = arg;
4394 va_start(args, fmt);
4395 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
4398 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4399 DNS_LOGMODULE_RESOLVER, level,
4400 "fctx %p(%s): %s", fctx, fctx->info, msgbuf);
4403 static inline isc_result_t
4404 findnoqname(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
4405 dns_name_t **noqnamep)
4407 dns_rdataset_t *nrdataset, *next, *sigrdataset;
4408 dns_rdata_rrsig_t rrsig;
4409 isc_result_t result;
4410 unsigned int labels;
4411 dns_section_t section;
4412 dns_name_t *zonename;
4413 dns_fixedname_t fzonename;
4414 dns_name_t *closest;
4415 dns_fixedname_t fclosest;
4416 dns_name_t *nearest;
4417 dns_fixedname_t fnearest;
4418 dns_rdatatype_t found = dns_rdatatype_none;
4419 dns_name_t *noqname = NULL;
4421 FCTXTRACE("findnoqname");
4423 REQUIRE(noqnamep != NULL && *noqnamep == NULL);
4426 * Find the SIG for this rdataset, if we have it.
4428 for (sigrdataset = ISC_LIST_HEAD(name->list);
4429 sigrdataset != NULL;
4430 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4431 if (sigrdataset->type == dns_rdatatype_rrsig &&
4432 sigrdataset->covers == type)
4436 if (sigrdataset == NULL)
4437 return (ISC_R_NOTFOUND);
4439 labels = dns_name_countlabels(name);
4441 for (result = dns_rdataset_first(sigrdataset);
4442 result == ISC_R_SUCCESS;
4443 result = dns_rdataset_next(sigrdataset)) {
4444 dns_rdata_t rdata = DNS_RDATA_INIT;
4445 dns_rdataset_current(sigrdataset, &rdata);
4446 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
4447 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4448 /* Wildcard has rrsig.labels < labels - 1. */
4449 if (rrsig.labels + 1U >= labels)
4454 if (result == ISC_R_NOMORE)
4455 return (ISC_R_NOTFOUND);
4456 if (result != ISC_R_SUCCESS)
4459 dns_fixedname_init(&fzonename);
4460 zonename = dns_fixedname_name(&fzonename);
4461 dns_fixedname_init(&fclosest);
4462 closest = dns_fixedname_name(&fclosest);
4463 dns_fixedname_init(&fnearest);
4464 nearest = dns_fixedname_name(&fnearest);
4466 #define NXND(x) ((x) == ISC_R_SUCCESS)
4468 section = DNS_SECTION_AUTHORITY;
4469 for (result = dns_message_firstname(fctx->rmessage, section);
4470 result == ISC_R_SUCCESS;
4471 result = dns_message_nextname(fctx->rmessage, section)) {
4472 dns_name_t *nsec = NULL;
4473 dns_message_currentname(fctx->rmessage, section, &nsec);
4474 for (nrdataset = ISC_LIST_HEAD(nsec->list);
4475 nrdataset != NULL; nrdataset = next) {
4476 isc_boolean_t data = ISC_FALSE, exists = ISC_FALSE;
4477 isc_boolean_t optout = ISC_FALSE, unknown = ISC_FALSE;
4478 isc_boolean_t setclosest = ISC_FALSE;
4479 isc_boolean_t setnearest = ISC_FALSE;
4481 next = ISC_LIST_NEXT(nrdataset, link);
4482 if (nrdataset->type != dns_rdatatype_nsec &&
4483 nrdataset->type != dns_rdatatype_nsec3)
4486 if (nrdataset->type == dns_rdatatype_nsec &&
4487 NXND(dns_nsec_noexistnodata(type, name, nsec,
4489 &data, NULL, fctx_log,
4494 found = dns_rdatatype_nsec;
4498 if (nrdataset->type == dns_rdatatype_nsec3 &&
4499 NXND(dns_nsec3_noexistnodata(type, name, nsec,
4500 nrdataset, zonename,
4508 if (!exists && setnearest) {
4510 found = dns_rdatatype_nsec3;
4515 if (result == ISC_R_NOMORE)
4516 result = ISC_R_SUCCESS;
4517 if (noqname != NULL) {
4518 for (sigrdataset = ISC_LIST_HEAD(noqname->list);
4519 sigrdataset != NULL;
4520 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4521 if (sigrdataset->type == dns_rdatatype_rrsig &&
4522 sigrdataset->covers == found)
4525 if (sigrdataset != NULL)
4526 *noqnamep = noqname;
4531 static inline isc_result_t
4532 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4535 dns_rdataset_t *rdataset, *sigrdataset;
4536 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4537 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4538 dns_dbnode_t *node, **anodep;
4541 dns_resolver_t *res;
4542 isc_boolean_t need_validation, secure_domain, have_answer;
4543 isc_result_t result, eresult;
4544 dns_fetchevent_t *event;
4545 unsigned int options;
4548 unsigned int valoptions = 0;
4551 * The appropriate bucket lock must be held.
4555 need_validation = ISC_FALSE;
4556 POST(need_validation);
4557 secure_domain = ISC_FALSE;
4558 have_answer = ISC_FALSE;
4559 eresult = ISC_R_SUCCESS;
4560 task = res->buckets[fctx->bucketnum].task;
4563 * Is DNSSEC validation required for this name?
4565 if (res->view->enablevalidation) {
4566 result = dns_view_issecuredomain(res->view, name,
4568 if (result != ISC_R_SUCCESS)
4571 if (!secure_domain && res->view->dlv != NULL) {
4572 valoptions = DNS_VALIDATOR_DLV;
4573 secure_domain = ISC_TRUE;
4577 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4578 need_validation = ISC_FALSE;
4580 need_validation = secure_domain;
4586 asigrdataset = NULL;
4588 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4590 have_answer = ISC_TRUE;
4591 event = ISC_LIST_HEAD(fctx->events);
4592 if (event != NULL) {
4594 aname = dns_fixedname_name(&event->foundname);
4595 result = dns_name_copy(name, aname, NULL);
4596 if (result != ISC_R_SUCCESS)
4598 anodep = &event->node;
4600 * If this is an ANY, SIG or RRSIG query, we're not
4601 * going to return any rdatasets, unless we encountered
4602 * a CNAME or DNAME as "the answer". In this case,
4603 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4604 * and we must set up the rdatasets.
4606 if ((fctx->type != dns_rdatatype_any &&
4607 fctx->type != dns_rdatatype_rrsig &&
4608 fctx->type != dns_rdatatype_sig) ||
4609 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4610 ardataset = event->rdataset;
4611 asigrdataset = event->sigrdataset;
4617 * Find or create the cache node.
4620 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4621 if (result != ISC_R_SUCCESS)
4625 * Cache or validate each cacheable rdataset.
4627 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4628 for (rdataset = ISC_LIST_HEAD(name->list);
4630 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4631 if (!CACHE(rdataset))
4633 if (CHECKNAMES(rdataset)) {
4634 char namebuf[DNS_NAME_FORMATSIZE];
4635 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4636 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4638 dns_name_format(name, namebuf, sizeof(namebuf));
4639 dns_rdatatype_format(rdataset->type, typebuf,
4641 dns_rdataclass_format(rdataset->rdclass, classbuf,
4643 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4644 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4645 "check-names %s %s/%s/%s",
4646 fail ? "failure" : "warning",
4647 namebuf, typebuf, classbuf);
4649 if (ANSWER(rdataset)) {
4650 dns_db_detachnode(fctx->cache, &node);
4651 return (DNS_R_BADNAME);
4658 * Enforce the configure maximum cache TTL.
4660 if (rdataset->ttl > res->view->maxcachettl)
4661 rdataset->ttl = res->view->maxcachettl;
4664 * Find the SIG for this rdataset, if we have it.
4666 for (sigrdataset = ISC_LIST_HEAD(name->list);
4667 sigrdataset != NULL;
4668 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4669 if (sigrdataset->type == dns_rdatatype_rrsig &&
4670 sigrdataset->covers == rdataset->type)
4675 * If this RRset is in a secure domain, is in bailiwick,
4676 * and is not glue, attempt DNSSEC validation. (We do not
4677 * attempt to validate glue or out-of-bailiwick data--even
4678 * though there might be some performance benefit to doing
4679 * so--because it makes it simpler and safer to ensure that
4680 * records from a secure domain are only cached if validated
4681 * within the context of a query to the domain that owns
4684 if (secure_domain && rdataset->trust != dns_trust_glue &&
4685 !EXTERNAL(rdataset)) {
4689 * RRSIGs are validated as part of validating the
4692 if (rdataset->type == dns_rdatatype_rrsig)
4695 if (sigrdataset == NULL) {
4696 if (!ANSWER(rdataset) && need_validation) {
4698 * Ignore non-answer rdatasets that
4699 * are missing signatures.
4706 * Normalize the rdataset and sigrdataset TTLs.
4708 if (sigrdataset != NULL) {
4709 rdataset->ttl = ISC_MIN(rdataset->ttl,
4711 sigrdataset->ttl = rdataset->ttl;
4715 * Cache this rdataset/sigrdataset pair as
4716 * pending data. Track whether it was additional
4719 if (rdataset->trust == dns_trust_additional)
4720 trust = dns_trust_pending_additional;
4722 trust = dns_trust_pending_answer;
4724 rdataset->trust = trust;
4725 if (sigrdataset != NULL)
4726 sigrdataset->trust = trust;
4727 if (!need_validation || !ANSWER(rdataset)) {
4728 if (ANSWER(rdataset) &&
4729 rdataset->type != dns_rdatatype_rrsig) {
4730 isc_result_t tresult;
4731 dns_name_t *noqname = NULL;
4732 tresult = findnoqname(fctx, name,
4735 if (tresult == ISC_R_SUCCESS &&
4738 dns_rdataset_addnoqname(
4740 RUNTIME_CHECK(tresult ==
4744 addedrdataset = ardataset;
4745 result = dns_db_addrdataset(fctx->cache, node,
4746 NULL, now, rdataset,
4748 if (result == DNS_R_UNCHANGED) {
4749 result = ISC_R_SUCCESS;
4750 if (!need_validation &&
4751 ardataset != NULL &&
4752 NEGATIVE(ardataset)) {
4754 * The answer in the cache is
4755 * better than the answer we
4756 * found, and is a negative
4757 * cache entry, so we must set
4758 * eresult appropriately.
4760 if (NXDOMAIN(ardataset))
4762 DNS_R_NCACHENXDOMAIN;
4765 DNS_R_NCACHENXRRSET;
4767 * We have a negative response
4768 * from the cache so don't
4769 * attempt to add the RRSIG
4775 if (result != ISC_R_SUCCESS)
4777 if (sigrdataset != NULL) {
4778 addedrdataset = asigrdataset;
4779 result = dns_db_addrdataset(fctx->cache,
4783 if (result == DNS_R_UNCHANGED)
4784 result = ISC_R_SUCCESS;
4785 if (result != ISC_R_SUCCESS)
4787 } else if (!ANSWER(rdataset))
4791 if (ANSWER(rdataset) && need_validation) {
4792 if (fctx->type != dns_rdatatype_any &&
4793 fctx->type != dns_rdatatype_rrsig &&
4794 fctx->type != dns_rdatatype_sig) {
4796 * This is The Answer. We will
4797 * validate it, but first we cache
4798 * the rest of the response - it may
4799 * contain useful keys.
4801 INSIST(valrdataset == NULL &&
4802 valsigrdataset == NULL);
4803 valrdataset = rdataset;
4804 valsigrdataset = sigrdataset;
4807 * This is one of (potentially)
4808 * multiple answers to an ANY
4809 * or SIG query. To keep things
4810 * simple, we just start the
4811 * validator right away rather
4812 * than caching first and
4813 * having to remember which
4814 * rdatasets needed validation.
4816 result = valcreate(fctx, addrinfo,
4817 name, rdataset->type,
4822 * Defer any further validations.
4823 * This prevents multiple validators
4824 * from manipulating fctx->rmessage
4827 valoptions |= DNS_VALIDATOR_DEFER;
4829 } else if (CHAINING(rdataset)) {
4830 if (rdataset->type == dns_rdatatype_cname)
4831 eresult = DNS_R_CNAME;
4833 INSIST(rdataset->type ==
4834 dns_rdatatype_dname);
4835 eresult = DNS_R_DNAME;
4838 } else if (!EXTERNAL(rdataset)) {
4840 * It's OK to cache this rdataset now.
4842 if (ANSWER(rdataset))
4843 addedrdataset = ardataset;
4844 else if (ANSWERSIG(rdataset))
4845 addedrdataset = asigrdataset;
4847 addedrdataset = NULL;
4848 if (CHAINING(rdataset)) {
4849 if (rdataset->type == dns_rdatatype_cname)
4850 eresult = DNS_R_CNAME;
4852 INSIST(rdataset->type ==
4853 dns_rdatatype_dname);
4854 eresult = DNS_R_DNAME;
4857 if (rdataset->trust == dns_trust_glue &&
4858 (rdataset->type == dns_rdatatype_ns ||
4859 (rdataset->type == dns_rdatatype_rrsig &&
4860 rdataset->covers == dns_rdatatype_ns))) {
4862 * If the trust level is 'dns_trust_glue'
4863 * then we are adding data from a referral
4864 * we got while executing the search algorithm.
4865 * New referral data always takes precedence
4866 * over the existing cache contents.
4868 options = DNS_DBADD_FORCE;
4872 if (ANSWER(rdataset) &&
4873 rdataset->type != dns_rdatatype_rrsig) {
4874 isc_result_t tresult;
4875 dns_name_t *noqname = NULL;
4876 tresult = findnoqname(fctx, name,
4877 rdataset->type, &noqname);
4878 if (tresult == ISC_R_SUCCESS &&
4880 tresult = dns_rdataset_addnoqname(
4882 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4887 * Now we can add the rdataset.
4889 result = dns_db_addrdataset(fctx->cache,
4895 if (result == DNS_R_UNCHANGED) {
4896 if (ANSWER(rdataset) &&
4897 ardataset != NULL &&
4898 NEGATIVE(ardataset)) {
4900 * The answer in the cache is better
4901 * than the answer we found, and is
4902 * a negative cache entry, so we
4903 * must set eresult appropriately.
4905 if (NXDOMAIN(ardataset))
4906 eresult = DNS_R_NCACHENXDOMAIN;
4908 eresult = DNS_R_NCACHENXRRSET;
4910 result = ISC_R_SUCCESS;
4911 } else if (result != ISC_R_SUCCESS)
4916 if (valrdataset != NULL)
4917 result = valcreate(fctx, addrinfo, name, fctx->type,
4918 valrdataset, valsigrdataset, valoptions,
4921 if (result == ISC_R_SUCCESS && have_answer) {
4922 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4923 if (event != NULL) {
4925 * Negative results must be indicated in event->result.
4927 if (dns_rdataset_isassociated(event->rdataset) &&
4928 NEGATIVE(event->rdataset)) {
4929 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4930 eresult == DNS_R_NCACHENXRRSET);
4932 event->result = eresult;
4933 dns_db_attach(fctx->cache, adbp);
4934 dns_db_transfernode(fctx->cache, &node, anodep);
4935 clone_results(fctx);
4940 dns_db_detachnode(fctx->cache, &node);
4945 static inline isc_result_t
4946 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4948 isc_result_t result;
4949 dns_section_t section;
4952 FCTXTRACE("cache_message");
4954 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4956 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4958 for (section = DNS_SECTION_ANSWER;
4959 section <= DNS_SECTION_ADDITIONAL;
4961 result = dns_message_firstname(fctx->rmessage, section);
4962 while (result == ISC_R_SUCCESS) {
4964 dns_message_currentname(fctx->rmessage, section,
4966 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4967 result = cache_name(fctx, name, addrinfo, now);
4968 if (result != ISC_R_SUCCESS)
4971 result = dns_message_nextname(fctx->rmessage, section);
4973 if (result != ISC_R_NOMORE)
4976 if (result == ISC_R_NOMORE)
4977 result = ISC_R_SUCCESS;
4979 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4985 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4988 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4989 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4990 isc_boolean_t optout, isc_boolean_t secure,
4991 dns_rdataset_t *ardataset, isc_result_t *eresultp)
4993 isc_result_t result;
4994 dns_rdataset_t rdataset;
4996 if (ardataset == NULL) {
4997 dns_rdataset_init(&rdataset);
4998 ardataset = &rdataset;
5001 result = dns_ncache_addoptout(message, cache, node, covers,
5002 now, maxttl, optout, ardataset);
5004 result = dns_ncache_add(message, cache, node, covers, now,
5006 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
5008 * If the cache now contains a negative entry and we
5009 * care about whether it is DNS_R_NCACHENXDOMAIN or
5010 * DNS_R_NCACHENXRRSET then extract it.
5012 if (NEGATIVE(ardataset)) {
5014 * The cache data is a negative cache entry.
5016 if (NXDOMAIN(ardataset))
5017 *eresultp = DNS_R_NCACHENXDOMAIN;
5019 *eresultp = DNS_R_NCACHENXRRSET;
5022 * Either we don't care about the nature of the
5023 * cache rdataset (because no fetch is interested
5024 * in the outcome), or the cache rdataset is not
5025 * a negative cache entry. Whichever case it is,
5026 * we can return success.
5028 * XXXRTH There's a CNAME/DNAME problem here.
5030 *eresultp = ISC_R_SUCCESS;
5032 result = ISC_R_SUCCESS;
5034 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
5035 dns_rdataset_disassociate(ardataset);
5040 static inline isc_result_t
5041 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
5042 dns_rdatatype_t covers, isc_stdtime_t now)
5044 isc_result_t result, eresult;
5046 dns_resolver_t *res;
5048 dns_dbnode_t *node, **anodep;
5049 dns_rdataset_t *ardataset;
5050 isc_boolean_t need_validation, secure_domain;
5052 dns_fetchevent_t *event;
5054 unsigned int valoptions = 0;
5056 FCTXTRACE("ncache_message");
5058 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
5061 need_validation = ISC_FALSE;
5062 POST(need_validation);
5063 secure_domain = ISC_FALSE;
5064 eresult = ISC_R_SUCCESS;
5069 * XXXMPA remove when we follow cnames and adjust the setting
5070 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
5072 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
5075 * Is DNSSEC validation required for this name?
5077 if (fctx->res->view->enablevalidation) {
5078 result = dns_view_issecuredomain(res->view, name,
5080 if (result != ISC_R_SUCCESS)
5083 if (!secure_domain && res->view->dlv != NULL) {
5084 valoptions = DNS_VALIDATOR_DLV;
5085 secure_domain = ISC_TRUE;
5089 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
5090 need_validation = ISC_FALSE;
5092 need_validation = secure_domain;
5094 if (secure_domain) {
5096 * Mark all rdatasets as pending.
5098 dns_rdataset_t *trdataset;
5101 result = dns_message_firstname(fctx->rmessage,
5102 DNS_SECTION_AUTHORITY);
5103 while (result == ISC_R_SUCCESS) {
5105 dns_message_currentname(fctx->rmessage,
5106 DNS_SECTION_AUTHORITY,
5108 for (trdataset = ISC_LIST_HEAD(tname->list);
5110 trdataset = ISC_LIST_NEXT(trdataset, link))
5111 trdataset->trust = dns_trust_pending_answer;
5112 result = dns_message_nextname(fctx->rmessage,
5113 DNS_SECTION_AUTHORITY);
5115 if (result != ISC_R_NOMORE)
5120 if (need_validation) {
5122 * Do negative response validation.
5124 result = valcreate(fctx, addrinfo, name, fctx->type,
5125 NULL, NULL, valoptions,
5126 res->buckets[fctx->bucketnum].task);
5128 * If validation is necessary, return now. Otherwise continue
5129 * to process the message, letting the validation complete
5130 * in its own good time.
5135 LOCK(&res->buckets[fctx->bucketnum].lock);
5141 if (!HAVE_ANSWER(fctx)) {
5142 event = ISC_LIST_HEAD(fctx->events);
5143 if (event != NULL) {
5145 aname = dns_fixedname_name(&event->foundname);
5146 result = dns_name_copy(name, aname, NULL);
5147 if (result != ISC_R_SUCCESS)
5149 anodep = &event->node;
5150 ardataset = event->rdataset;
5155 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
5156 if (result != ISC_R_SUCCESS)
5160 * If we are asking for a SOA record set the cache time
5161 * to zero to facilitate locating the containing zone of
5164 ttl = fctx->res->view->maxncachettl;
5165 if (fctx->type == dns_rdatatype_soa &&
5166 covers == dns_rdatatype_any &&
5167 fctx->res->zero_no_soa_ttl)
5170 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
5171 covers, now, ttl, ISC_FALSE,
5172 ISC_FALSE, ardataset, &eresult);
5173 if (result != ISC_R_SUCCESS)
5176 if (!HAVE_ANSWER(fctx)) {
5177 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
5178 if (event != NULL) {
5179 event->result = eresult;
5180 dns_db_attach(fctx->cache, adbp);
5181 dns_db_transfernode(fctx->cache, &node, anodep);
5182 clone_results(fctx);
5187 UNLOCK(&res->buckets[fctx->bucketnum].lock);
5190 dns_db_detachnode(fctx->cache, &node);
5196 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
5197 isc_boolean_t external, isc_boolean_t gluing)
5199 name->attributes |= DNS_NAMEATTR_CACHE;
5201 rdataset->trust = dns_trust_glue;
5203 * Glue with 0 TTL causes problems. We force the TTL to
5204 * 1 second to prevent this.
5206 if (rdataset->ttl == 0)
5209 rdataset->trust = dns_trust_additional;
5211 * Avoid infinite loops by only marking new rdatasets.
5213 if (!CACHE(rdataset)) {
5214 name->attributes |= DNS_NAMEATTR_CHASE;
5215 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
5217 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
5219 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
5223 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
5224 dns_section_t section)
5226 fetchctx_t *fctx = arg;
5227 isc_result_t result;
5229 dns_rdataset_t *rdataset;
5230 isc_boolean_t external;
5231 dns_rdatatype_t rtype;
5232 isc_boolean_t gluing;
5234 REQUIRE(VALID_FCTX(fctx));
5236 #if CHECK_FOR_GLUE_IN_ANSWER
5237 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
5238 return (ISC_R_SUCCESS);
5247 result = dns_message_findname(fctx->rmessage, section, addname,
5248 dns_rdatatype_any, 0, &name, NULL);
5249 if (result == ISC_R_SUCCESS) {
5250 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5251 if (type == dns_rdatatype_a) {
5252 for (rdataset = ISC_LIST_HEAD(name->list);
5254 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5255 if (rdataset->type == dns_rdatatype_rrsig)
5256 rtype = rdataset->covers;
5258 rtype = rdataset->type;
5259 if (rtype == dns_rdatatype_a ||
5260 rtype == dns_rdatatype_aaaa)
5261 mark_related(name, rdataset, external,
5265 result = dns_message_findtype(name, type, 0,
5267 if (result == ISC_R_SUCCESS) {
5268 mark_related(name, rdataset, external, gluing);
5270 * Do we have its SIG too?
5273 result = dns_message_findtype(name,
5274 dns_rdatatype_rrsig,
5276 if (result == ISC_R_SUCCESS)
5277 mark_related(name, rdataset, external,
5283 return (ISC_R_SUCCESS);
5287 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5288 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5291 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5292 #define CHECK_FOR_GLUE_IN_ANSWER 0
5294 #if CHECK_FOR_GLUE_IN_ANSWER
5296 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5297 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5302 chase_additional(fetchctx_t *fctx) {
5303 isc_boolean_t rescan;
5304 dns_section_t section = DNS_SECTION_ADDITIONAL;
5305 isc_result_t result;
5310 for (result = dns_message_firstname(fctx->rmessage, section);
5311 result == ISC_R_SUCCESS;
5312 result = dns_message_nextname(fctx->rmessage, section)) {
5313 dns_name_t *name = NULL;
5314 dns_rdataset_t *rdataset;
5315 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5317 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5319 name->attributes &= ~DNS_NAMEATTR_CHASE;
5320 for (rdataset = ISC_LIST_HEAD(name->list);
5322 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5323 if (CHASE(rdataset)) {
5324 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5325 (void)dns_rdataset_additionaldata(rdataset,
5336 static inline isc_result_t
5337 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5338 isc_result_t result;
5339 dns_rdata_t rdata = DNS_RDATA_INIT;
5340 dns_rdata_cname_t cname;
5342 result = dns_rdataset_first(rdataset);
5343 if (result != ISC_R_SUCCESS)
5345 dns_rdataset_current(rdataset, &rdata);
5346 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5347 if (result != ISC_R_SUCCESS)
5349 dns_name_init(tname, NULL);
5350 dns_name_clone(&cname.cname, tname);
5351 dns_rdata_freestruct(&cname);
5353 return (ISC_R_SUCCESS);
5356 static inline isc_result_t
5357 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5358 dns_name_t *oname, dns_fixedname_t *fixeddname)
5360 isc_result_t result;
5361 dns_rdata_t rdata = DNS_RDATA_INIT;
5362 unsigned int nlabels;
5364 dns_namereln_t namereln;
5365 dns_rdata_dname_t dname;
5366 dns_fixedname_t prefix;
5369 * Get the target name of the DNAME.
5371 result = dns_rdataset_first(rdataset);
5372 if (result != ISC_R_SUCCESS)
5374 dns_rdataset_current(rdataset, &rdata);
5375 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5376 if (result != ISC_R_SUCCESS)
5380 * Get the prefix of qname.
5382 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5383 if (namereln != dns_namereln_subdomain) {
5384 char qbuf[DNS_NAME_FORMATSIZE];
5385 char obuf[DNS_NAME_FORMATSIZE];
5387 dns_rdata_freestruct(&dname);
5388 dns_name_format(qname, qbuf, sizeof(qbuf));
5389 dns_name_format(oname, obuf, sizeof(obuf));
5390 log_formerr(fctx, "unrelated DNAME in answer: "
5391 "%s is not in %s", qbuf, obuf);
5392 return (DNS_R_FORMERR);
5394 dns_fixedname_init(&prefix);
5395 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5396 dns_fixedname_init(fixeddname);
5397 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5399 dns_fixedname_name(fixeddname), NULL);
5400 dns_rdata_freestruct(&dname);
5404 static isc_boolean_t
5405 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5406 dns_rdataset_t *rdataset)
5408 isc_result_t result;
5409 dns_rdata_t rdata = DNS_RDATA_INIT;
5411 struct in6_addr in6a;
5412 isc_netaddr_t netaddr;
5413 char addrbuf[ISC_NETADDR_FORMATSIZE];
5414 char namebuf[DNS_NAME_FORMATSIZE];
5419 /* By default, we allow any addresses. */
5420 if (view->denyansweracl == NULL)
5424 * If the owner name matches one in the exclusion list, either exactly
5425 * or partially, allow it.
5427 if (view->answeracl_exclude != NULL) {
5428 dns_rbtnode_t *node = NULL;
5430 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5431 &node, NULL, 0, NULL, NULL);
5433 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5438 * Otherwise, search the filter list for a match for each address
5439 * record. If a match is found, the address should be filtered,
5440 * so should the entire answer.
5442 for (result = dns_rdataset_first(rdataset);
5443 result == ISC_R_SUCCESS;
5444 result = dns_rdataset_next(rdataset)) {
5445 dns_rdata_reset(&rdata);
5446 dns_rdataset_current(rdataset, &rdata);
5447 if (rdataset->type == dns_rdatatype_a) {
5448 INSIST(rdata.length == sizeof(ina.s_addr));
5449 memmove(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5450 isc_netaddr_fromin(&netaddr, &ina);
5452 INSIST(rdata.length == sizeof(in6a.s6_addr));
5453 memmove(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5454 isc_netaddr_fromin6(&netaddr, &in6a);
5457 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5458 &view->aclenv, &match, NULL);
5460 if (result == ISC_R_SUCCESS && match > 0) {
5461 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5462 dns_name_format(name, namebuf, sizeof(namebuf));
5463 dns_rdatatype_format(rdataset->type, typebuf,
5465 dns_rdataclass_format(rdataset->rdclass, classbuf,
5467 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5468 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5469 "answer address %s denied for %s/%s/%s",
5470 addrbuf, namebuf, typebuf, classbuf);
5478 static isc_boolean_t
5479 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5480 dns_rdatatype_t type, dns_name_t *tname,
5483 isc_result_t result;
5484 dns_rbtnode_t *node = NULL;
5485 char qnamebuf[DNS_NAME_FORMATSIZE];
5486 char tnamebuf[DNS_NAME_FORMATSIZE];
5490 /* By default, we allow any target name. */
5491 if (view->denyanswernames == NULL)
5495 * If the owner name matches one in the exclusion list, either exactly
5496 * or partially, allow it.
5498 if (view->answernames_exclude != NULL) {
5499 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5500 &node, NULL, 0, NULL, NULL);
5501 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5506 * If the target name is a subdomain of the search domain, allow it.
5508 if (dns_name_issubdomain(tname, domain))
5512 * Otherwise, apply filters.
5514 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5515 NULL, 0, NULL, NULL);
5516 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5517 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5518 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5519 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5520 dns_rdataclass_format(view->rdclass, classbuf,
5522 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5523 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5524 "%s target %s denied for %s/%s",
5525 typebuf, tnamebuf, qnamebuf, classbuf);
5533 trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) {
5534 char ns_namebuf[DNS_NAME_FORMATSIZE];
5535 char namebuf[DNS_NAME_FORMATSIZE];
5536 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5538 if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) {
5539 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf));
5540 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
5541 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf));
5543 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5544 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
5545 "fctx %p: trimming ttl of %s/NS for %s/%s: "
5546 "%u -> %u", fctx, ns_namebuf, namebuf, tbuf,
5547 rdataset->ttl, fctx->ns_ttl);
5548 rdataset->ttl = fctx->ns_ttl;
5553 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5554 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5555 * section for the NS RRset if the query type is NS; if it has
5556 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5557 * section for A and AAAA queries.
5559 #define LOOK_FOR_NS_IN_ANSWER 0x1
5560 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5563 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5564 unsigned int look_in_options)
5566 isc_result_t result;
5567 dns_message_t *message;
5568 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name, *save_name;
5569 dns_rdataset_t *rdataset, *ns_rdataset;
5570 isc_boolean_t aa, negative_response;
5571 dns_rdatatype_t type, save_type;
5572 dns_section_t section;
5574 FCTXTRACE("noanswer_response");
5576 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5577 INSIST(fctx->type == dns_rdatatype_ns);
5578 section = DNS_SECTION_ANSWER;
5580 section = DNS_SECTION_AUTHORITY;
5582 message = fctx->rmessage;
5587 if (oqname == NULL) {
5589 * We have a normal, non-chained negative response or
5592 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5596 qname = &fctx->name;
5599 * We're being invoked by answer_response() after it has
5600 * followed a CNAME/DNAME chain.
5605 * If the current qname is not a subdomain of the query
5606 * domain, there's no point in looking at the authority
5607 * section without doing DNSSEC validation.
5609 * Until we do that validation, we'll just return success
5612 if (!dns_name_issubdomain(qname, &fctx->domain))
5613 return (ISC_R_SUCCESS);
5617 * We have to figure out if this is a negative response, or a
5622 * Sometimes we can tell if its a negative response by looking at
5623 * the message header.
5625 negative_response = ISC_FALSE;
5626 if (message->rcode == dns_rcode_nxdomain ||
5627 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5628 message->counts[DNS_SECTION_AUTHORITY] == 0))
5629 negative_response = ISC_TRUE;
5632 * Process the authority section.
5639 save_type = dns_rdatatype_none;
5640 result = dns_message_firstname(message, section);
5641 while (result == ISC_R_SUCCESS) {
5643 dns_message_currentname(message, section, &name);
5644 if (dns_name_issubdomain(name, &fctx->domain)) {
5646 * Look for NS/SOA RRsets first.
5648 for (rdataset = ISC_LIST_HEAD(name->list);
5650 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5651 type = rdataset->type;
5652 if (type == dns_rdatatype_rrsig)
5653 type = rdataset->covers;
5654 if (((type == dns_rdatatype_ns ||
5655 type == dns_rdatatype_soa) &&
5656 !dns_name_issubdomain(qname, name))) {
5657 char qbuf[DNS_NAME_FORMATSIZE];
5658 char nbuf[DNS_NAME_FORMATSIZE];
5659 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5660 dns_rdatatype_format(fctx->type, tbuf,
5662 dns_name_format(name, nbuf,
5664 dns_name_format(qname, qbuf,
5667 "unrelated %s %s in "
5668 "%s authority section",
5670 return (DNS_R_FORMERR);
5672 if (type == dns_rdatatype_ns) {
5676 * Only one set of NS RRs is allowed.
5678 if (rdataset->type ==
5680 if (ns_name != NULL &&
5687 return (DNS_R_FORMERR);
5690 ns_rdataset = rdataset;
5694 rdataset->attributes |=
5695 DNS_RDATASETATTR_CACHE;
5696 rdataset->trust = dns_trust_glue;
5698 if (type == dns_rdatatype_soa) {
5700 * SOA, or RRSIG SOA.
5702 * Only one SOA is allowed.
5704 if (rdataset->type ==
5705 dns_rdatatype_soa) {
5706 if (soa_name != NULL &&
5713 return (DNS_R_FORMERR);
5718 DNS_NAMEATTR_NCACHE;
5719 rdataset->attributes |=
5720 DNS_RDATASETATTR_NCACHE;
5723 dns_trust_authauthority;
5724 else if (ISFORWARDER(fctx->addrinfo))
5729 dns_trust_additional;
5733 result = dns_message_nextname(message, section);
5734 if (result == ISC_R_NOMORE)
5736 else if (result != ISC_R_SUCCESS)
5740 log_ns_ttl(fctx, "noanswer_response");
5742 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
5743 !dns_name_equal(ns_name, dns_rootname))
5744 trim_ns_ttl(fctx, ns_name, ns_rdataset);
5747 * A negative response has a SOA record (Type 2)
5748 * and a optional NS RRset (Type 1) or it has neither
5749 * a SOA or a NS RRset (Type 3, handled above) or
5750 * rcode is NXDOMAIN (handled above) in which case
5751 * the NS RRset is allowed (Type 4).
5753 if (soa_name != NULL)
5754 negative_response = ISC_TRUE;
5756 result = dns_message_firstname(message, section);
5757 while (result == ISC_R_SUCCESS) {
5759 dns_message_currentname(message, section, &name);
5760 if (dns_name_issubdomain(name, &fctx->domain)) {
5761 for (rdataset = ISC_LIST_HEAD(name->list);
5763 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5764 type = rdataset->type;
5765 if (type == dns_rdatatype_rrsig)
5766 type = rdataset->covers;
5767 if (type == dns_rdatatype_nsec ||
5768 type == dns_rdatatype_nsec3) {
5770 * NSEC or RRSIG NSEC.
5772 if (negative_response) {
5774 DNS_NAMEATTR_NCACHE;
5775 rdataset->attributes |=
5776 DNS_RDATASETATTR_NCACHE;
5777 } else if (type == dns_rdatatype_nsec) {
5780 rdataset->attributes |=
5781 DNS_RDATASETATTR_CACHE;
5785 dns_trust_authauthority;
5786 else if (ISFORWARDER(fctx->addrinfo))
5791 dns_trust_additional;
5793 * No additional data needs to be
5796 } else if (type == dns_rdatatype_ds) {
5800 * These should only be here if
5801 * this is a referral, and there
5802 * should only be one DS RRset.
5804 if (ns_name == NULL) {
5808 return (DNS_R_FORMERR);
5810 if (rdataset->type ==
5812 if (ds_name != NULL &&
5819 return (DNS_R_FORMERR);
5825 rdataset->attributes |=
5826 DNS_RDATASETATTR_CACHE;
5829 dns_trust_authauthority;
5830 else if (ISFORWARDER(fctx->addrinfo))
5835 dns_trust_additional;
5840 save_type = ISC_LIST_HEAD(name->list)->type;
5842 result = dns_message_nextname(message, section);
5843 if (result == ISC_R_NOMORE)
5845 else if (result != ISC_R_SUCCESS)
5850 * Trigger lookups for DNS nameservers.
5852 if (negative_response && message->rcode == dns_rcode_noerror &&
5853 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5854 dns_name_equal(soa_name, qname) &&
5855 !dns_name_equal(qname, dns_rootname))
5856 return (DNS_R_CHASEDSSERVERS);
5859 * Did we find anything?
5861 if (!negative_response && ns_name == NULL) {
5865 if (oqname != NULL) {
5867 * We've already got a partial CNAME/DNAME chain,
5868 * and haven't found else anything useful here, but
5869 * no error has occurred since we have an answer.
5871 return (ISC_R_SUCCESS);
5874 * The responder is insane.
5876 if (save_name == NULL) {
5877 log_formerr(fctx, "invalid response");
5878 return (DNS_R_FORMERR);
5880 if (!dns_name_issubdomain(save_name, &fctx->domain)) {
5881 char nbuf[DNS_NAME_FORMATSIZE];
5882 char dbuf[DNS_NAME_FORMATSIZE];
5883 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5885 dns_rdatatype_format(save_type, tbuf,
5887 dns_name_format(save_name, nbuf, sizeof(nbuf));
5888 dns_name_format(&fctx->domain, dbuf,
5891 log_formerr(fctx, "Name %s (%s) not subdomain"
5892 " of zone %s -- invalid response",
5895 log_formerr(fctx, "invalid response");
5897 return (DNS_R_FORMERR);
5902 * If we found both NS and SOA, they should be the same name.
5904 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5905 log_formerr(fctx, "NS/SOA mismatch");
5906 return (DNS_R_FORMERR);
5910 * Do we have a referral? (We only want to follow a referral if
5911 * we're not following a chain.)
5913 if (!negative_response && ns_name != NULL && oqname == NULL) {
5915 * We already know ns_name is a subdomain of fctx->domain.
5916 * If ns_name is equal to fctx->domain, we're not making
5917 * progress. We return DNS_R_FORMERR so that we'll keep
5918 * trying other servers.
5920 if (dns_name_equal(ns_name, &fctx->domain)) {
5921 log_formerr(fctx, "non-improving referral");
5922 return (DNS_R_FORMERR);
5926 * If the referral name is not a parent of the query
5927 * name, consider the responder insane.
5929 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5931 log_formerr(fctx, "referral to non-parent");
5932 FCTXTRACE("referral to non-parent");
5933 return (DNS_R_FORMERR);
5937 * Mark any additional data related to this rdataset.
5938 * It's important that we do this before we change the
5941 INSIST(ns_rdataset != NULL);
5942 fctx->attributes |= FCTX_ATTR_GLUING;
5943 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5945 #if CHECK_FOR_GLUE_IN_ANSWER
5947 * Look in the answer section for "glue" that is incorrectly
5948 * returned as a answer. This is needed if the server also
5949 * minimizes the response size by not adding records to the
5950 * additional section that are in the answer section or if
5951 * the record gets dropped due to message size constraints.
5953 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
5954 (fctx->type == dns_rdatatype_aaaa ||
5955 fctx->type == dns_rdatatype_a))
5956 (void)dns_rdataset_additionaldata(ns_rdataset,
5957 check_answer, fctx);
5959 fctx->attributes &= ~FCTX_ATTR_GLUING;
5961 * NS rdatasets with 0 TTL cause problems.
5962 * dns_view_findzonecut() will not find them when we
5963 * try to follow the referral, and we'll SERVFAIL
5964 * because the best nameservers are now above QDOMAIN.
5965 * We force the TTL to 1 second to prevent this.
5967 if (ns_rdataset->ttl == 0)
5968 ns_rdataset->ttl = 1;
5970 * Set the current query domain to the referral name.
5972 * XXXRTH We should check if we're in forward-only mode, and
5973 * if so we should bail out.
5975 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5976 dns_name_free(&fctx->domain, fctx->mctx);
5977 if (dns_rdataset_isassociated(&fctx->nameservers))
5978 dns_rdataset_disassociate(&fctx->nameservers);
5979 dns_name_init(&fctx->domain, NULL);
5980 result = dns_name_dup(ns_name, fctx->mctx, &fctx->domain);
5981 if (result != ISC_R_SUCCESS)
5983 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5984 fctx->ns_ttl_ok = ISC_FALSE;
5985 log_ns_ttl(fctx, "DELEGATION");
5986 return (DNS_R_DELEGATION);
5990 * Since we're not doing a referral, we don't want to cache any
5991 * NS RRs we may have found.
5993 if (ns_name != NULL)
5994 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5996 if (negative_response && oqname == NULL)
5997 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5999 return (ISC_R_SUCCESS);
6003 answer_response(fetchctx_t *fctx) {
6004 isc_result_t result;
6005 dns_message_t *message;
6006 dns_name_t *name, *qname, tname, *ns_name;
6007 dns_rdataset_t *rdataset, *ns_rdataset;
6008 isc_boolean_t done, external, chaining, aa, found, want_chaining;
6009 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
6011 dns_rdatatype_t type;
6012 dns_fixedname_t dname, fqname;
6015 FCTXTRACE("answer_response");
6017 message = fctx->rmessage;
6020 * Examine the answer section, marking those rdatasets which are
6021 * part of the answer and should be cached.
6025 found_cname = ISC_FALSE;
6026 found_type = ISC_FALSE;
6027 chaining = ISC_FALSE;
6028 have_answer = ISC_FALSE;
6029 want_chaining = ISC_FALSE;
6030 POST(want_chaining);
6031 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
6035 qname = &fctx->name;
6037 view = fctx->res->view;
6038 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
6039 while (!done && result == ISC_R_SUCCESS) {
6041 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
6042 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6043 if (dns_name_equal(name, qname)) {
6044 wanted_chaining = ISC_FALSE;
6045 for (rdataset = ISC_LIST_HEAD(name->list);
6047 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6049 want_chaining = ISC_FALSE;
6051 if (rdataset->type == dns_rdatatype_nsec3) {
6053 * NSEC3 records are not allowed to
6054 * appear in the answer section.
6056 log_formerr(fctx, "NSEC3 in answer");
6057 return (DNS_R_FORMERR);
6061 * Apply filters, if given, on answers to reject
6062 * a malicious attempt of rebinding.
6064 if ((rdataset->type == dns_rdatatype_a ||
6065 rdataset->type == dns_rdatatype_aaaa) &&
6066 !is_answeraddress_allowed(view, name,
6068 return (DNS_R_SERVFAIL);
6071 if (rdataset->type == type && !found_cname) {
6073 * We've found an ordinary answer.
6076 found_type = ISC_TRUE;
6078 aflag = DNS_RDATASETATTR_ANSWER;
6079 } else if (type == dns_rdatatype_any) {
6081 * We've found an answer matching
6082 * an ANY query. There may be
6086 aflag = DNS_RDATASETATTR_ANSWER;
6087 } else if (rdataset->type == dns_rdatatype_rrsig
6088 && rdataset->covers == type
6091 * We've found a signature that
6092 * covers the type we're looking for.
6095 found_type = ISC_TRUE;
6096 aflag = DNS_RDATASETATTR_ANSWERSIG;
6097 } else if (rdataset->type ==
6101 * We're looking for something else,
6102 * but we found a CNAME.
6104 * Getting a CNAME response for some
6105 * query types is an error, see
6106 * RFC 4035, Section 2.5.
6108 if (type == dns_rdatatype_rrsig ||
6109 type == dns_rdatatype_key ||
6110 type == dns_rdatatype_nsec) {
6111 char buf[DNS_RDATATYPE_FORMATSIZE];
6112 dns_rdatatype_format(fctx->type,
6117 return (DNS_R_FORMERR);
6120 found_cname = ISC_TRUE;
6121 want_chaining = ISC_TRUE;
6122 aflag = DNS_RDATASETATTR_ANSWER;
6123 result = cname_target(rdataset,
6125 if (result != ISC_R_SUCCESS)
6127 /* Apply filters on the target name. */
6128 if (!is_answertarget_allowed(view,
6133 return (DNS_R_SERVFAIL);
6135 } else if (rdataset->type == dns_rdatatype_rrsig
6136 && rdataset->covers ==
6140 * We're looking for something else,
6141 * but we found a SIG CNAME.
6144 found_cname = ISC_TRUE;
6145 aflag = DNS_RDATASETATTR_ANSWERSIG;
6150 * We've found an answer to our
6155 rdataset->attributes |=
6156 DNS_RDATASETATTR_CACHE;
6157 rdataset->trust = dns_trust_answer;
6160 * This data is "the" answer
6161 * to our question only if
6162 * we're not chaining (i.e.
6163 * if we haven't followed
6164 * a CNAME or DNAME).
6168 DNS_RDATASETATTR_ANSWER)
6169 have_answer = ISC_TRUE;
6171 DNS_NAMEATTR_ANSWER;
6172 rdataset->attributes |= aflag;
6175 dns_trust_authanswer;
6176 } else if (external) {
6178 * This data is outside of
6179 * our query domain, and
6180 * may not be cached.
6182 rdataset->attributes |=
6183 DNS_RDATASETATTR_EXTERNAL;
6187 * Mark any additional data related
6190 (void)dns_rdataset_additionaldata(
6198 if (want_chaining) {
6199 wanted_chaining = ISC_TRUE;
6201 DNS_NAMEATTR_CHAINING;
6202 rdataset->attributes |=
6203 DNS_RDATASETATTR_CHAINING;
6208 * We could add an "else" clause here and
6209 * log that we're ignoring this rdataset.
6213 * If wanted_chaining is true, we've done
6214 * some chaining as the result of processing
6215 * this node, and thus we need to set
6218 * We don't set chaining inside of the
6219 * rdataset loop because doing that would
6220 * cause us to ignore the signatures of
6223 if (wanted_chaining)
6224 chaining = ISC_TRUE;
6227 * Look for a DNAME (or its SIG). Anything else is
6230 wanted_chaining = ISC_FALSE;
6231 for (rdataset = ISC_LIST_HEAD(name->list);
6233 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6234 isc_boolean_t found_dname = ISC_FALSE;
6235 dns_name_t *dname_name;
6239 if (rdataset->type == dns_rdatatype_dname) {
6241 * We're looking for something else,
6242 * but we found a DNAME.
6244 * If we're not chaining, then the
6245 * DNAME should not be external.
6247 if (!chaining && external) {
6250 return (DNS_R_FORMERR);
6253 want_chaining = ISC_TRUE;
6254 POST(want_chaining);
6255 aflag = DNS_RDATASETATTR_ANSWER;
6256 result = dname_target(fctx, rdataset,
6259 if (result == ISC_R_NOSPACE) {
6261 * We can't construct the
6262 * DNAME target. Do not
6265 want_chaining = ISC_FALSE;
6266 POST(want_chaining);
6267 } else if (result != ISC_R_SUCCESS)
6270 found_dname = ISC_TRUE;
6272 dname_name = dns_fixedname_name(&dname);
6273 if (!is_answertarget_allowed(view,
6278 return (DNS_R_SERVFAIL);
6280 } else if (rdataset->type == dns_rdatatype_rrsig
6281 && rdataset->covers ==
6282 dns_rdatatype_dname) {
6284 * We've found a signature that
6288 aflag = DNS_RDATASETATTR_ANSWERSIG;
6293 * We've found an answer to our
6298 rdataset->attributes |=
6299 DNS_RDATASETATTR_CACHE;
6300 rdataset->trust = dns_trust_answer;
6303 * This data is "the" answer
6304 * to our question only if
6305 * we're not chaining.
6309 DNS_RDATASETATTR_ANSWER)
6310 have_answer = ISC_TRUE;
6312 DNS_NAMEATTR_ANSWER;
6313 rdataset->attributes |= aflag;
6316 dns_trust_authanswer;
6317 } else if (external) {
6318 rdataset->attributes |=
6319 DNS_RDATASETATTR_EXTERNAL;
6327 * Copy the dname into the
6330 * Although we check for
6331 * failure of the copy
6332 * operation, in practice it
6333 * should never fail since
6334 * we already know that the
6335 * result fits in a fixedname.
6337 dns_fixedname_init(&fqname);
6338 result = dns_name_copy(
6339 dns_fixedname_name(&dname),
6340 dns_fixedname_name(&fqname),
6342 if (result != ISC_R_SUCCESS)
6344 wanted_chaining = ISC_TRUE;
6346 DNS_NAMEATTR_CHAINING;
6347 rdataset->attributes |=
6348 DNS_RDATASETATTR_CHAINING;
6349 qname = dns_fixedname_name(
6354 if (wanted_chaining)
6355 chaining = ISC_TRUE;
6357 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6359 if (result == ISC_R_NOMORE)
6360 result = ISC_R_SUCCESS;
6361 if (result != ISC_R_SUCCESS)
6365 * We should have found an answer.
6368 log_formerr(fctx, "reply has no answer");
6369 return (DNS_R_FORMERR);
6373 * This response is now potentially cacheable.
6375 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6378 * Did chaining end before we got the final answer?
6382 * Yes. This may be a negative reply, so hand off
6383 * authority section processing to the noanswer code.
6384 * If it isn't a noanswer response, no harm will be
6387 return (noanswer_response(fctx, qname, 0));
6391 * We didn't end with an incomplete chain, so the rcode should be
6394 if (message->rcode != dns_rcode_noerror) {
6395 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6397 return (DNS_R_FORMERR);
6401 * Examine the authority section (if there is one).
6403 * We expect there to be only one owner name for all the rdatasets
6404 * in this section, and we expect that it is not external.
6409 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6410 while (!done && result == ISC_R_SUCCESS) {
6412 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6413 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6416 * We expect to find NS or SIG NS rdatasets, and
6419 for (rdataset = ISC_LIST_HEAD(name->list);
6421 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6422 if (rdataset->type == dns_rdatatype_ns ||
6423 (rdataset->type == dns_rdatatype_rrsig &&
6424 rdataset->covers == dns_rdatatype_ns)) {
6427 rdataset->attributes |=
6428 DNS_RDATASETATTR_CACHE;
6429 if (aa && !chaining)
6431 dns_trust_authauthority;
6434 dns_trust_additional;
6436 if (rdataset->type == dns_rdatatype_ns) {
6438 ns_rdataset = rdataset;
6441 * Mark any additional data related
6444 (void)dns_rdataset_additionaldata(
6452 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6454 if (result == ISC_R_NOMORE)
6455 result = ISC_R_SUCCESS;
6457 log_ns_ttl(fctx, "answer_response");
6459 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6460 !dns_name_equal(ns_name, dns_rootname))
6461 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6466 static isc_boolean_t
6467 fctx_decreference(fetchctx_t *fctx) {
6468 isc_boolean_t bucket_empty = ISC_FALSE;
6470 INSIST(fctx->references > 0);
6472 if (fctx->references == 0) {
6474 * No one cares about the result of this fetch anymore.
6476 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6477 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6479 * This fctx is already shutdown; we were just
6480 * waiting for the last reference to go away.
6482 bucket_empty = fctx_unlink(fctx);
6486 * Initiate shutdown.
6488 fctx_shutdown(fctx);
6491 return (bucket_empty);
6495 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6496 dns_fetchevent_t *fevent;
6497 dns_resolver_t *res;
6499 isc_result_t result;
6500 isc_boolean_t bucket_empty;
6501 isc_boolean_t locked = ISC_FALSE;
6502 unsigned int bucketnum;
6503 dns_rdataset_t nameservers;
6504 dns_fixedname_t fixed;
6507 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6508 fevent = (dns_fetchevent_t *)event;
6509 fctx = event->ev_arg;
6510 REQUIRE(VALID_FCTX(fctx));
6514 FCTXTRACE("resume_dslookup");
6516 if (fevent->node != NULL)
6517 dns_db_detachnode(fevent->db, &fevent->node);
6518 if (fevent->db != NULL)
6519 dns_db_detach(&fevent->db);
6521 dns_rdataset_init(&nameservers);
6523 bucketnum = fctx->bucketnum;
6524 if (fevent->result == ISC_R_CANCELED) {
6525 dns_resolver_destroyfetch(&fctx->nsfetch);
6526 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6527 } else if (fevent->result == ISC_R_SUCCESS) {
6529 FCTXTRACE("resuming DS lookup");
6531 dns_resolver_destroyfetch(&fctx->nsfetch);
6532 if (dns_rdataset_isassociated(&fctx->nameservers))
6533 dns_rdataset_disassociate(&fctx->nameservers);
6534 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6535 fctx->ns_ttl = fctx->nameservers.ttl;
6536 fctx->ns_ttl_ok = ISC_TRUE;
6537 log_ns_ttl(fctx, "resume_dslookup");
6538 dns_name_free(&fctx->domain, fctx->mctx);
6539 dns_name_init(&fctx->domain, NULL);
6540 result = dns_name_dup(&fctx->nsname, fctx->mctx, &fctx->domain);
6541 if (result != ISC_R_SUCCESS) {
6542 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6548 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6551 dns_rdataset_t *nsrdataset = NULL;
6554 * Retrieve state from fctx->nsfetch before we destroy it.
6556 dns_fixedname_init(&fixed);
6557 domain = dns_fixedname_name(&fixed);
6558 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6559 if (dns_name_equal(&fctx->nsname, domain)) {
6560 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6561 dns_resolver_destroyfetch(&fctx->nsfetch);
6564 if (dns_rdataset_isassociated(
6565 &fctx->nsfetch->private->nameservers)) {
6567 &fctx->nsfetch->private->nameservers,
6569 nsrdataset = &nameservers;
6572 dns_resolver_destroyfetch(&fctx->nsfetch);
6573 n = dns_name_countlabels(&fctx->nsname);
6574 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6577 if (dns_rdataset_isassociated(fevent->rdataset))
6578 dns_rdataset_disassociate(fevent->rdataset);
6579 FCTXTRACE("continuing to look for parent's NS records");
6580 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6581 dns_rdatatype_ns, domain,
6582 nsrdataset, NULL, 0, task,
6583 resume_dslookup, fctx,
6584 &fctx->nsrrset, NULL,
6586 if (result != ISC_R_SUCCESS)
6587 fctx_done(fctx, result, __LINE__);
6589 LOCK(&res->buckets[bucketnum].lock);
6596 if (dns_rdataset_isassociated(&nameservers))
6597 dns_rdataset_disassociate(&nameservers);
6598 if (dns_rdataset_isassociated(fevent->rdataset))
6599 dns_rdataset_disassociate(fevent->rdataset);
6600 INSIST(fevent->sigrdataset == NULL);
6601 isc_event_free(&event);
6603 LOCK(&res->buckets[bucketnum].lock);
6604 bucket_empty = fctx_decreference(fctx);
6605 UNLOCK(&res->buckets[bucketnum].lock);
6611 checknamessection(dns_message_t *message, dns_section_t section) {
6612 isc_result_t result;
6614 dns_rdata_t rdata = DNS_RDATA_INIT;
6615 dns_rdataset_t *rdataset;
6617 for (result = dns_message_firstname(message, section);
6618 result == ISC_R_SUCCESS;
6619 result = dns_message_nextname(message, section))
6622 dns_message_currentname(message, section, &name);
6623 for (rdataset = ISC_LIST_HEAD(name->list);
6625 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6626 for (result = dns_rdataset_first(rdataset);
6627 result == ISC_R_SUCCESS;
6628 result = dns_rdataset_next(rdataset)) {
6629 dns_rdataset_current(rdataset, &rdata);
6630 if (!dns_rdata_checkowner(name, rdata.rdclass,
6633 !dns_rdata_checknames(&rdata, name, NULL))
6635 rdataset->attributes |=
6636 DNS_RDATASETATTR_CHECKNAMES;
6638 dns_rdata_reset(&rdata);
6645 checknames(dns_message_t *message) {
6647 checknamessection(message, DNS_SECTION_ANSWER);
6648 checknamessection(message, DNS_SECTION_AUTHORITY);
6649 checknamessection(message, DNS_SECTION_ADDITIONAL);
6653 * Log server NSID at log level 'level'
6656 log_nsid(isc_buffer_t *opt, size_t nsid_len, resquery_t *query,
6657 int level, isc_mem_t *mctx)
6659 static const char hex[17] = "0123456789abcdef";
6660 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6661 isc_uint16_t buflen, i;
6662 unsigned char *p, *buf, *nsid;
6664 /* Allocate buffer for storing hex version of the NSID */
6665 buflen = (isc_uint16_t)nsid_len * 2 + 1;
6666 buf = isc_mem_get(mctx, buflen);
6670 /* Convert to hex */
6672 nsid = isc_buffer_current(opt);
6673 for (i = 0; i < nsid_len; i++) {
6674 *p++ = hex[(nsid[0] >> 4) & 0xf];
6675 *p++ = hex[nsid[0] & 0xf];
6680 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6682 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6683 DNS_LOGMODULE_RESOLVER, level,
6684 "received NSID '%s' from %s", buf, addrbuf);
6687 isc_mem_put(mctx, buf, buflen);
6692 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6693 isc_buffer_t buffer;
6696 isc_result_t result;
6698 if (! isc_log_wouldlog(dns_lctx, level))
6702 * Note that these are multiline debug messages. We want a newline
6703 * to appear in the log after each message.
6707 buf = isc_mem_get(mctx, len);
6710 isc_buffer_init(&buffer, buf, len);
6711 result = dns_message_totext(message, &dns_master_style_debug,
6713 if (result == ISC_R_NOSPACE) {
6714 isc_mem_put(mctx, buf, len);
6716 } else if (result == ISC_R_SUCCESS)
6717 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6718 DNS_LOGMODULE_RESOLVER, level,
6719 "received packet:\n%.*s",
6720 (int)isc_buffer_usedlength(&buffer),
6722 } while (result == ISC_R_NOSPACE);
6725 isc_mem_put(mctx, buf, len);
6728 static isc_boolean_t
6729 iscname(fetchctx_t *fctx) {
6730 isc_result_t result;
6732 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6733 &fctx->name, dns_rdatatype_cname, 0,
6735 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6738 static isc_boolean_t
6739 betterreferral(fetchctx_t *fctx) {
6740 isc_result_t result;
6742 dns_rdataset_t *rdataset;
6743 dns_message_t *message = fctx->rmessage;
6745 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6746 result == ISC_R_SUCCESS;
6747 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6749 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6750 if (!isstrictsubdomain(name, &fctx->domain))
6752 for (rdataset = ISC_LIST_HEAD(name->list);
6754 rdataset = ISC_LIST_NEXT(rdataset, link))
6755 if (rdataset->type == dns_rdatatype_ns)
6762 process_opt(resquery_t *query, dns_rdataset_t *opt) {
6764 isc_buffer_t optbuf;
6765 isc_result_t result;
6766 isc_uint16_t optcode;
6767 isc_uint16_t optlen;
6769 result = dns_rdataset_first(opt);
6770 if (result == ISC_R_SUCCESS) {
6771 dns_rdata_init(&rdata);
6772 dns_rdataset_current(opt, &rdata);
6773 isc_buffer_init(&optbuf, rdata.data, rdata.length);
6774 isc_buffer_add(&optbuf, rdata.length);
6775 while (isc_buffer_remaininglength(&optbuf) >= 4) {
6776 optcode = isc_buffer_getuint16(&optbuf);
6777 optlen = isc_buffer_getuint16(&optbuf);
6778 INSIST(optlen <= isc_buffer_remaininglength(&optbuf));
6781 if (query->options & DNS_FETCHOPT_WANTNSID)
6782 log_nsid(&optbuf, optlen, query,
6784 query->fctx->res->mctx);
6785 isc_buffer_forward(&optbuf, optlen);
6788 isc_buffer_forward(&optbuf, optlen);
6792 INSIST(isc_buffer_remaininglength(&optbuf) == 0U);
6797 resquery_response(isc_task_t *task, isc_event_t *event) {
6798 isc_result_t result = ISC_R_SUCCESS;
6799 resquery_t *query = event->ev_arg;
6800 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6801 isc_boolean_t keep_trying, get_nameservers, resend;
6802 isc_boolean_t truncated;
6803 dns_message_t *message;
6804 dns_rdataset_t *opt;
6807 dns_fixedname_t foundname;
6809 isc_time_t tnow, *finish;
6810 dns_adbaddrinfo_t *addrinfo;
6811 unsigned int options;
6812 unsigned int findoptions;
6813 isc_result_t broken_server;
6814 badnstype_t broken_type = badns_response;
6815 isc_boolean_t no_response;
6817 REQUIRE(VALID_QUERY(query));
6819 options = query->options;
6820 REQUIRE(VALID_FCTX(fctx));
6821 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6825 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6826 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6828 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6830 (void)isc_timer_touch(fctx->timer);
6832 keep_trying = ISC_FALSE;
6833 broken_server = ISC_R_SUCCESS;
6834 get_nameservers = ISC_FALSE;
6836 truncated = ISC_FALSE;
6838 no_response = ISC_FALSE;
6840 if (fctx->res->exiting) {
6841 result = ISC_R_SHUTTINGDOWN;
6846 fctx->timeout = ISC_FALSE;
6847 fctx->addrinfo = query->addrinfo;
6850 * XXXRTH We should really get the current time just once. We
6851 * need a routine to convert from an isc_time_t to an
6856 isc_stdtime_get(&now);
6859 * Did the dispatcher have a problem?
6861 if (devent->result != ISC_R_SUCCESS) {
6862 if (devent->result == ISC_R_EOF &&
6863 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6865 * The problem might be that they
6866 * don't understand EDNS0. Turn it
6867 * off and try again.
6869 options |= DNS_FETCHOPT_NOEDNS0;
6871 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6874 * There's no hope for this query.
6876 keep_trying = ISC_TRUE;
6879 * If this is a network error on an exclusive query
6880 * socket, mark the server as bad so that we won't try
6881 * it for this fetch again. Also adjust finish and
6882 * no_response so that we penalize this address in SRTT
6885 if (query->exclusivesocket &&
6886 (devent->result == ISC_R_HOSTUNREACH ||
6887 devent->result == ISC_R_NETUNREACH ||
6888 devent->result == ISC_R_CONNREFUSED ||
6889 devent->result == ISC_R_CANCELED)) {
6890 broken_server = devent->result;
6891 broken_type = badns_unreachable;
6893 no_response = ISC_TRUE;
6899 message = fctx->rmessage;
6901 if (query->tsig != NULL) {
6902 result = dns_message_setquerytsig(message, query->tsig);
6903 if (result != ISC_R_SUCCESS)
6907 if (query->tsigkey) {
6908 result = dns_message_settsigkey(message, query->tsigkey);
6909 if (result != ISC_R_SUCCESS)
6913 result = dns_message_parse(message, &devent->buffer, 0);
6914 if (result != ISC_R_SUCCESS) {
6916 case ISC_R_UNEXPECTEDEND:
6917 if (!message->question_ok ||
6918 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6919 (options & DNS_FETCHOPT_TCP) != 0) {
6921 * Either the message ended prematurely,
6922 * and/or wasn't marked as being truncated,
6923 * and/or this is a response to a query we
6924 * sent over TCP. In all of these cases,
6925 * something is wrong with the remote
6926 * server and we don't want to retry using
6929 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6932 * The problem might be that they
6933 * don't understand EDNS0. Turn it
6934 * off and try again.
6936 options |= DNS_FETCHOPT_NOEDNS0;
6939 &query->addrinfo->sockaddr);
6940 inc_stats(fctx->res,
6941 dns_resstatscounter_edns0fail);
6943 broken_server = result;
6944 keep_trying = ISC_TRUE;
6949 * We defer retrying via TCP for a bit so we can
6950 * check out this message further.
6952 truncated = ISC_TRUE;
6955 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6957 * The problem might be that they
6958 * don't understand EDNS0. Turn it
6959 * off and try again.
6961 options |= DNS_FETCHOPT_NOEDNS0;
6963 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6964 inc_stats(fctx->res,
6965 dns_resstatscounter_edns0fail);
6967 broken_server = DNS_R_UNEXPECTEDRCODE;
6968 keep_trying = ISC_TRUE;
6973 * Something bad has happened.
6981 * Log the incoming packet.
6983 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6986 * Process receive opt record.
6988 opt = dns_message_getopt(message);
6990 process_opt(query, opt);
6993 * If the message is signed, check the signature. If not, this
6994 * returns success anyway.
6996 result = dns_message_checksig(message, fctx->res->view);
6997 if (result != ISC_R_SUCCESS)
7001 * The dispatcher should ensure we only get responses with QR set.
7003 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
7005 * INSIST() that the message comes from the place we sent it to,
7006 * since the dispatch code should ensure this.
7008 * INSIST() that the message id is correct (this should also be
7009 * ensured by the dispatch code).
7013 * We have an affirmative response to the query and we have
7014 * previously got a response from this server which indicated
7015 * EDNS may not be supported so we can now cache the lack of
7019 (message->rcode == dns_rcode_noerror ||
7020 message->rcode == dns_rcode_nxdomain ||
7021 message->rcode == dns_rcode_refused ||
7022 message->rcode == dns_rcode_yxdomain) &&
7023 bad_edns(fctx, &query->addrinfo->sockaddr)) {
7024 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7025 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7027 dns_adb_changeflags(fctx->adb, query->addrinfo,
7028 DNS_FETCHOPT_NOEDNS0,
7029 DNS_FETCHOPT_NOEDNS0);
7033 * Deal with truncated responses by retrying using TCP.
7035 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
7036 truncated = ISC_TRUE;
7039 inc_stats(fctx->res, dns_resstatscounter_truncated);
7040 if ((options & DNS_FETCHOPT_TCP) != 0) {
7041 broken_server = DNS_R_TRUNCATEDTCP;
7042 keep_trying = ISC_TRUE;
7044 options |= DNS_FETCHOPT_TCP;
7051 * Is it a query response?
7053 if (message->opcode != dns_opcode_query) {
7055 broken_server = DNS_R_UNEXPECTEDOPCODE;
7056 keep_trying = ISC_TRUE;
7061 * Update statistics about erroneous responses.
7063 if (message->rcode != dns_rcode_noerror) {
7064 switch (message->rcode) {
7065 case dns_rcode_nxdomain:
7066 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
7068 case dns_rcode_servfail:
7069 inc_stats(fctx->res, dns_resstatscounter_servfail);
7071 case dns_rcode_formerr:
7072 inc_stats(fctx->res, dns_resstatscounter_formerr);
7075 inc_stats(fctx->res, dns_resstatscounter_othererror);
7081 * Is the remote server broken, or does it dislike us?
7083 if (message->rcode != dns_rcode_noerror &&
7084 message->rcode != dns_rcode_nxdomain) {
7085 if (((message->rcode == dns_rcode_formerr ||
7086 message->rcode == dns_rcode_notimp) ||
7087 (message->rcode == dns_rcode_servfail &&
7088 dns_message_getopt(message) == NULL)) &&
7089 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7091 * It's very likely they don't like EDNS0.
7092 * If the response code is SERVFAIL, also check if the
7093 * response contains an OPT RR and don't cache the
7094 * failure since it can be returned for various other
7097 * XXXRTH We should check if the question
7098 * we're asking requires EDNS0, and
7099 * if so, we should bail out.
7101 options |= DNS_FETCHOPT_NOEDNS0;
7104 * Remember that they may not like EDNS0.
7106 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7107 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
7108 } else if (message->rcode == dns_rcode_formerr) {
7109 if (ISFORWARDER(query->addrinfo)) {
7111 * This forwarder doesn't understand us,
7112 * but other forwarders might. Keep trying.
7114 broken_server = DNS_R_REMOTEFORMERR;
7115 keep_trying = ISC_TRUE;
7118 * The server doesn't understand us. Since
7119 * all servers for a zone need similar
7120 * capabilities, we assume that we will get
7121 * FORMERR from all servers, and thus we
7122 * cannot make any more progress with this
7125 log_formerr(fctx, "server sent FORMERR");
7126 result = DNS_R_FORMERR;
7128 } else if (message->rcode == dns_rcode_yxdomain) {
7130 * DNAME mapping failed because the new name
7131 * was too long. There's no chance of success
7134 result = DNS_R_YXDOMAIN;
7135 } else if (message->rcode == dns_rcode_badvers) {
7136 unsigned int flags, mask;
7137 unsigned int version;
7140 INSIST(opt != NULL);
7141 version = (opt->ttl >> 16) & 0xff;
7142 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
7143 DNS_FETCHOPT_EDNSVERSIONSET;
7144 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
7145 DNS_FETCHOPT_EDNSVERSIONSET;
7148 dns_adb_changeflags(fctx->adb, query->addrinfo,
7152 broken_server = DNS_R_BADVERS;
7153 keep_trying = ISC_TRUE;
7160 broken_server = DNS_R_UNEXPECTEDRCODE;
7161 INSIST(broken_server != ISC_R_SUCCESS);
7162 keep_trying = ISC_TRUE;
7168 * Is the question the same as the one we asked?
7170 result = same_question(fctx);
7171 if (result != ISC_R_SUCCESS) {
7173 if (result == DNS_R_FORMERR)
7174 keep_trying = ISC_TRUE;
7179 * Is the server lame?
7181 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
7183 inc_stats(fctx->res, dns_resstatscounter_lame);
7184 log_lame(fctx, query->addrinfo);
7185 result = dns_adb_marklame(fctx->adb, query->addrinfo,
7186 &fctx->name, fctx->type,
7187 now + fctx->res->lame_ttl);
7188 if (result != ISC_R_SUCCESS)
7189 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7190 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
7191 "could not mark server as lame: %s",
7192 isc_result_totext(result));
7193 broken_server = DNS_R_LAME;
7194 keep_trying = ISC_TRUE;
7199 * Enforce delegations only zones like NET and COM.
7201 if (!ISFORWARDER(query->addrinfo) &&
7202 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
7203 !dns_name_equal(&fctx->domain, &fctx->name) &&
7204 fix_mustbedelegationornxdomain(message, fctx)) {
7205 char namebuf[DNS_NAME_FORMATSIZE];
7206 char domainbuf[DNS_NAME_FORMATSIZE];
7207 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7211 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
7212 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7213 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
7214 dns_rdataclass_format(fctx->res->rdclass, classbuf,
7216 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7219 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
7220 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7221 "enforced delegation-only for '%s' (%s/%s/%s) "
7223 domainbuf, namebuf, typebuf, classbuf, addrbuf);
7226 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
7227 checknames(message);
7232 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
7235 * Did we get any answers?
7237 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
7238 (message->rcode == dns_rcode_noerror ||
7239 message->rcode == dns_rcode_nxdomain)) {
7242 * We've got answers. If it has an authoritative answer or an
7243 * answer from a forwarder, we're done.
7245 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
7246 ISFORWARDER(query->addrinfo))
7247 result = answer_response(fctx);
7248 else if (iscname(fctx) &&
7249 fctx->type != dns_rdatatype_any &&
7250 fctx->type != dns_rdatatype_cname) {
7252 * A BIND8 server could return a non-authoritative
7253 * answer when a CNAME is followed. We should treat
7254 * it as a valid answer.
7256 result = answer_response(fctx);
7257 } else if (fctx->type != dns_rdatatype_ns &&
7258 !betterreferral(fctx)) {
7260 * Lame response !!!.
7262 result = answer_response(fctx);
7264 if (fctx->type == dns_rdatatype_ns) {
7266 * A BIND 8 server could incorrectly return a
7267 * non-authoritative answer to an NS query
7268 * instead of a referral. Since this answer
7269 * lacks the SIGs necessary to do DNSSEC
7270 * validation, we must invoke the following
7271 * special kludge to treat it as a referral.
7273 result = noanswer_response(fctx, NULL,
7274 LOOK_FOR_NS_IN_ANSWER);
7277 * Some other servers may still somehow include
7278 * an answer when it should return a referral
7279 * with an empty answer. Check to see if we can
7280 * treat this as a referral by ignoring the
7281 * answer. Further more, there may be an
7282 * implementation that moves A/AAAA glue records
7283 * to the answer section for that type of
7284 * delegation when the query is for that glue
7285 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
7286 * such a corner case.
7288 result = noanswer_response(fctx, NULL,
7289 LOOK_FOR_GLUE_IN_ANSWER);
7291 if (result != DNS_R_DELEGATION) {
7293 * At this point, AA is not set, the response
7294 * is not a referral, and the server is not a
7295 * forwarder. It is technically lame and it's
7296 * easier to treat it as such than to figure out
7297 * some more elaborate course of action.
7299 broken_server = DNS_R_LAME;
7300 keep_trying = ISC_TRUE;
7303 goto force_referral;
7305 if (result != ISC_R_SUCCESS) {
7306 if (result == DNS_R_FORMERR)
7307 keep_trying = ISC_TRUE;
7310 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
7311 message->rcode == dns_rcode_noerror ||
7312 message->rcode == dns_rcode_nxdomain) {
7314 * NXDOMAIN, NXRDATASET, or referral.
7316 result = noanswer_response(fctx, NULL, 0);
7319 case DNS_R_CHASEDSSERVERS:
7321 case DNS_R_DELEGATION:
7324 * We don't have the answer, but we know a better
7327 get_nameservers = ISC_TRUE;
7328 keep_trying = ISC_TRUE;
7330 * We have a new set of name servers, and it
7331 * has not experienced any restarts yet.
7336 * Update local statistics counters collected for each
7340 fctx->querysent = 0;
7341 fctx->lamecount = 0;
7346 result = ISC_R_SUCCESS;
7350 * Something has gone wrong.
7352 if (result == DNS_R_FORMERR)
7353 keep_trying = ISC_TRUE;
7358 * The server is insane.
7361 broken_server = DNS_R_UNEXPECTEDRCODE;
7362 keep_trying = ISC_TRUE;
7367 * Follow additional section data chains.
7369 chase_additional(fctx);
7372 * Cache the cacheable parts of the message. This may also cause
7373 * work to be queued to the DNSSEC validator.
7375 if (WANTCACHE(fctx)) {
7376 result = cache_message(fctx, query->addrinfo, now);
7377 if (result != ISC_R_SUCCESS)
7382 * Ncache the negatively cacheable parts of the message. This may
7383 * also cause work to be queued to the DNSSEC validator.
7385 if (WANTNCACHE(fctx)) {
7386 dns_rdatatype_t covers;
7387 if (message->rcode == dns_rcode_nxdomain)
7388 covers = dns_rdatatype_any;
7390 covers = fctx->type;
7393 * Cache any negative cache entries in the message.
7395 result = ncache_message(fctx, query->addrinfo, covers, now);
7400 * Remember the query's addrinfo, in case we need to mark the
7403 addrinfo = query->addrinfo;
7408 * XXXRTH Don't cancel the query if waiting for validation?
7410 fctx_cancelquery(&query, &devent, finish, no_response);
7413 if (result == DNS_R_FORMERR)
7414 broken_server = DNS_R_FORMERR;
7415 if (broken_server != ISC_R_SUCCESS) {
7417 * Add this server to the list of bad servers for
7420 add_bad(fctx, addrinfo, broken_server, broken_type);
7423 if (get_nameservers) {
7425 dns_fixedname_init(&foundname);
7426 fname = dns_fixedname_name(&foundname);
7427 if (result != ISC_R_SUCCESS) {
7428 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7432 if (dns_rdatatype_atparent(fctx->type))
7433 findoptions |= DNS_DBFIND_NOEXACT;
7434 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7437 name = &fctx->domain;
7438 result = dns_view_findzonecut(fctx->res->view,
7444 if (result != ISC_R_SUCCESS) {
7445 FCTXTRACE("couldn't find a zonecut");
7446 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7449 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7451 * The best nameservers are now above our
7454 FCTXTRACE("nameservers now above QDOMAIN");
7455 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7458 dns_name_free(&fctx->domain, fctx->mctx);
7459 dns_name_init(&fctx->domain, NULL);
7460 result = dns_name_dup(fname, fctx->mctx, &fctx->domain);
7461 if (result != ISC_R_SUCCESS) {
7462 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7465 fctx->ns_ttl = fctx->nameservers.ttl;
7466 fctx->ns_ttl_ok = ISC_TRUE;
7467 fctx_cancelqueries(fctx, ISC_TRUE);
7468 fctx_cleanupfinds(fctx);
7469 fctx_cleanupaltfinds(fctx);
7470 fctx_cleanupforwaddrs(fctx);
7471 fctx_cleanupaltaddrs(fctx);
7476 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7477 } else if (resend) {
7479 * Resend (probably with changed options).
7481 FCTXTRACE("resend");
7482 inc_stats(fctx->res, dns_resstatscounter_retry);
7483 result = fctx_query(fctx, addrinfo, options);
7484 if (result != ISC_R_SUCCESS)
7485 fctx_done(fctx, result, __LINE__);
7486 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7488 * All has gone well so far, but we are waiting for the
7489 * DNSSEC validator to validate the answer.
7491 FCTXTRACE("wait for validator");
7492 fctx_cancelqueries(fctx, ISC_TRUE);
7494 * We must not retransmit while the validator is working;
7495 * it has references to the current rmessage.
7497 result = fctx_stopidletimer(fctx);
7498 if (result != ISC_R_SUCCESS)
7499 fctx_done(fctx, result, __LINE__);
7500 } else if (result == DNS_R_CHASEDSSERVERS) {
7502 add_bad(fctx, addrinfo, result, broken_type);
7503 fctx_cancelqueries(fctx, ISC_TRUE);
7504 fctx_cleanupfinds(fctx);
7505 fctx_cleanupforwaddrs(fctx);
7507 n = dns_name_countlabels(&fctx->name);
7508 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7510 FCTXTRACE("suspending DS lookup to find parent's NS records");
7512 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7514 NULL, NULL, NULL, 0, task,
7515 resume_dslookup, fctx,
7516 &fctx->nsrrset, NULL,
7518 if (result != ISC_R_SUCCESS)
7519 fctx_done(fctx, result, __LINE__);
7521 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7523 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7524 result = fctx_stopidletimer(fctx);
7525 if (result != ISC_R_SUCCESS)
7526 fctx_done(fctx, result, __LINE__);
7532 fctx_done(fctx, result, __LINE__);
7538 *** Resolver Methods
7541 destroy_badcache(dns_resolver_t *res) {
7542 dns_badcache_t *bad, *next;
7545 if (res->badcache != NULL) {
7546 for (i = 0; i < res->badhash; i++)
7547 for (bad = res->badcache[i]; bad != NULL;
7550 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7554 isc_mem_put(res->mctx, res->badcache,
7555 sizeof(*res->badcache) * res->badhash);
7556 res->badcache = NULL;
7558 INSIST(res->badcount == 0);
7563 destroy(dns_resolver_t *res) {
7567 REQUIRE(res->references == 0);
7568 REQUIRE(!res->priming);
7569 REQUIRE(res->primefetch == NULL);
7573 INSIST(res->nfctx == 0);
7575 DESTROYLOCK(&res->primelock);
7576 DESTROYLOCK(&res->nlock);
7577 DESTROYLOCK(&res->lock);
7578 for (i = 0; i < res->nbuckets; i++) {
7579 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7580 isc_task_shutdown(res->buckets[i].task);
7581 isc_task_detach(&res->buckets[i].task);
7582 DESTROYLOCK(&res->buckets[i].lock);
7583 isc_mem_detach(&res->buckets[i].mctx);
7585 isc_mem_put(res->mctx, res->buckets,
7586 res->nbuckets * sizeof(fctxbucket_t));
7587 if (res->dispatches4 != NULL)
7588 dns_dispatchset_destroy(&res->dispatches4);
7589 if (res->dispatches6 != NULL)
7590 dns_dispatchset_destroy(&res->dispatches6);
7591 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7592 ISC_LIST_UNLINK(res->alternates, a, link);
7594 dns_name_free(&a->_u._n.name, res->mctx);
7595 isc_mem_put(res->mctx, a, sizeof(*a));
7597 dns_resolver_reset_algorithms(res);
7598 destroy_badcache(res);
7599 dns_resolver_resetmustbesecure(res);
7601 isc_rwlock_destroy(&res->alglock);
7604 isc_rwlock_destroy(&res->mbslock);
7606 isc_timer_detach(&res->spillattimer);
7608 isc_mem_put(res->mctx, res, sizeof(*res));
7612 send_shutdown_events(dns_resolver_t *res) {
7613 isc_event_t *event, *next_event;
7617 * Caller must be holding the resolver lock.
7620 for (event = ISC_LIST_HEAD(res->whenshutdown);
7622 event = next_event) {
7623 next_event = ISC_LIST_NEXT(event, ev_link);
7624 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7625 etask = event->ev_sender;
7626 event->ev_sender = res;
7627 isc_task_sendanddetach(&etask, &event);
7632 empty_bucket(dns_resolver_t *res) {
7633 RTRACE("empty_bucket");
7637 INSIST(res->activebuckets > 0);
7638 res->activebuckets--;
7639 if (res->activebuckets == 0)
7640 send_shutdown_events(res);
7646 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7647 dns_resolver_t *res = event->ev_arg;
7648 isc_result_t result;
7650 isc_boolean_t logit = ISC_FALSE;
7652 REQUIRE(VALID_RESOLVER(res));
7657 INSIST(!res->exiting);
7658 if (res->spillat > res->spillatmin) {
7662 if (res->spillat <= res->spillatmin) {
7663 result = isc_timer_reset(res->spillattimer,
7664 isc_timertype_inactive, NULL,
7666 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7668 count = res->spillat;
7671 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7672 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7673 "clients-per-query decreased to %u", count);
7675 isc_event_free(&event);
7679 dns_resolver_create(dns_view_t *view,
7680 isc_taskmgr_t *taskmgr,
7681 unsigned int ntasks, unsigned int ndisp,
7682 isc_socketmgr_t *socketmgr,
7683 isc_timermgr_t *timermgr,
7684 unsigned int options,
7685 dns_dispatchmgr_t *dispatchmgr,
7686 dns_dispatch_t *dispatchv4,
7687 dns_dispatch_t *dispatchv6,
7688 dns_resolver_t **resp)
7690 dns_resolver_t *res;
7691 isc_result_t result = ISC_R_SUCCESS;
7692 unsigned int i, buckets_created = 0;
7693 isc_task_t *task = NULL;
7698 * Create a resolver.
7701 REQUIRE(DNS_VIEW_VALID(view));
7702 REQUIRE(ntasks > 0);
7704 REQUIRE(resp != NULL && *resp == NULL);
7705 REQUIRE(dispatchmgr != NULL);
7706 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7708 res = isc_mem_get(view->mctx, sizeof(*res));
7710 return (ISC_R_NOMEMORY);
7712 res->mctx = view->mctx;
7713 res->rdclass = view->rdclass;
7714 res->socketmgr = socketmgr;
7715 res->timermgr = timermgr;
7716 res->taskmgr = taskmgr;
7717 res->dispatchmgr = dispatchmgr;
7719 res->options = options;
7721 ISC_LIST_INIT(res->alternates);
7722 res->udpsize = RECV_BUFFER_SIZE;
7723 res->algorithms = NULL;
7724 res->badcache = NULL;
7728 res->mustbesecure = NULL;
7729 res->spillatmin = res->spillat = 10;
7730 res->spillatmax = 100;
7731 res->spillattimer = NULL;
7732 res->zero_no_soa_ttl = ISC_FALSE;
7733 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7734 res->nbuckets = ntasks;
7735 res->activebuckets = ntasks;
7736 res->buckets = isc_mem_get(view->mctx,
7737 ntasks * sizeof(fctxbucket_t));
7738 if (res->buckets == NULL) {
7739 result = ISC_R_NOMEMORY;
7742 for (i = 0; i < ntasks; i++) {
7743 result = isc_mutex_init(&res->buckets[i].lock);
7744 if (result != ISC_R_SUCCESS)
7745 goto cleanup_buckets;
7746 res->buckets[i].task = NULL;
7747 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7748 if (result != ISC_R_SUCCESS) {
7749 DESTROYLOCK(&res->buckets[i].lock);
7750 goto cleanup_buckets;
7752 res->buckets[i].mctx = NULL;
7753 snprintf(name, sizeof(name), "res%u", i);
7754 #ifdef ISC_PLATFORM_USETHREADS
7756 * Use a separate memory context for each bucket to reduce
7757 * contention among multiple threads. Do this only when
7758 * enabling threads because it will be require more memory.
7760 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7761 if (result != ISC_R_SUCCESS) {
7762 isc_task_detach(&res->buckets[i].task);
7763 DESTROYLOCK(&res->buckets[i].lock);
7764 goto cleanup_buckets;
7766 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7768 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7770 isc_task_setname(res->buckets[i].task, name, res);
7771 ISC_LIST_INIT(res->buckets[i].fctxs);
7772 res->buckets[i].exiting = ISC_FALSE;
7776 res->dispatches4 = NULL;
7777 if (dispatchv4 != NULL) {
7778 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
7779 dispatchv4, &res->dispatches4, ndisp);
7780 dispattr = dns_dispatch_getattributes(dispatchv4);
7782 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7785 res->dispatches6 = NULL;
7786 if (dispatchv6 != NULL) {
7787 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
7788 dispatchv6, &res->dispatches6, ndisp);
7789 dispattr = dns_dispatch_getattributes(dispatchv6);
7791 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7794 res->references = 1;
7795 res->exiting = ISC_FALSE;
7796 res->frozen = ISC_FALSE;
7797 ISC_LIST_INIT(res->whenshutdown);
7798 res->priming = ISC_FALSE;
7799 res->primefetch = NULL;
7802 result = isc_mutex_init(&res->lock);
7803 if (result != ISC_R_SUCCESS)
7804 goto cleanup_dispatches;
7806 result = isc_mutex_init(&res->nlock);
7807 if (result != ISC_R_SUCCESS)
7810 result = isc_mutex_init(&res->primelock);
7811 if (result != ISC_R_SUCCESS)
7815 result = isc_task_create(taskmgr, 0, &task);
7816 if (result != ISC_R_SUCCESS)
7817 goto cleanup_primelock;
7819 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7820 task, spillattimer_countdown, res,
7821 &res->spillattimer);
7822 isc_task_detach(&task);
7823 if (result != ISC_R_SUCCESS)
7824 goto cleanup_primelock;
7827 result = isc_rwlock_init(&res->alglock, 0, 0);
7828 if (result != ISC_R_SUCCESS)
7829 goto cleanup_spillattimer;
7832 result = isc_rwlock_init(&res->mbslock, 0, 0);
7833 if (result != ISC_R_SUCCESS)
7834 goto cleanup_alglock;
7837 res->magic = RES_MAGIC;
7841 return (ISC_R_SUCCESS);
7846 isc_rwlock_destroy(&res->alglock);
7849 #if USE_ALGLOCK || USE_MBSLOCK
7850 cleanup_spillattimer:
7851 isc_timer_detach(&res->spillattimer);
7855 DESTROYLOCK(&res->primelock);
7858 DESTROYLOCK(&res->nlock);
7861 DESTROYLOCK(&res->lock);
7864 if (res->dispatches6 != NULL)
7865 dns_dispatchset_destroy(&res->dispatches6);
7866 if (res->dispatches4 != NULL)
7867 dns_dispatchset_destroy(&res->dispatches4);
7870 for (i = 0; i < buckets_created; i++) {
7871 isc_mem_detach(&res->buckets[i].mctx);
7872 DESTROYLOCK(&res->buckets[i].lock);
7873 isc_task_shutdown(res->buckets[i].task);
7874 isc_task_detach(&res->buckets[i].task);
7876 isc_mem_put(view->mctx, res->buckets,
7877 res->nbuckets * sizeof(fctxbucket_t));
7880 isc_mem_put(view->mctx, res, sizeof(*res));
7887 prime_done(isc_task_t *task, isc_event_t *event) {
7888 dns_resolver_t *res;
7889 dns_fetchevent_t *fevent;
7891 dns_db_t *db = NULL;
7893 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7894 fevent = (dns_fetchevent_t *)event;
7895 res = event->ev_arg;
7896 REQUIRE(VALID_RESOLVER(res));
7902 INSIST(res->priming);
7903 res->priming = ISC_FALSE;
7904 LOCK(&res->primelock);
7905 fetch = res->primefetch;
7906 res->primefetch = NULL;
7907 UNLOCK(&res->primelock);
7911 if (fevent->result == ISC_R_SUCCESS &&
7912 res->view->cache != NULL && res->view->hints != NULL) {
7913 dns_cache_attachdb(res->view->cache, &db);
7914 dns_root_checkhints(res->view, res->view->hints, db);
7918 if (fevent->node != NULL)
7919 dns_db_detachnode(fevent->db, &fevent->node);
7920 if (fevent->db != NULL)
7921 dns_db_detach(&fevent->db);
7922 if (dns_rdataset_isassociated(fevent->rdataset))
7923 dns_rdataset_disassociate(fevent->rdataset);
7924 INSIST(fevent->sigrdataset == NULL);
7926 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7928 isc_event_free(&event);
7929 dns_resolver_destroyfetch(&fetch);
7933 dns_resolver_prime(dns_resolver_t *res) {
7934 isc_boolean_t want_priming = ISC_FALSE;
7935 dns_rdataset_t *rdataset;
7936 isc_result_t result;
7938 REQUIRE(VALID_RESOLVER(res));
7939 REQUIRE(res->frozen);
7941 RTRACE("dns_resolver_prime");
7945 if (!res->exiting && !res->priming) {
7946 INSIST(res->primefetch == NULL);
7947 res->priming = ISC_TRUE;
7948 want_priming = ISC_TRUE;
7955 * To avoid any possible recursive locking problems, we
7956 * start the priming fetch like any other fetch, and holding
7957 * no resolver locks. No one else will try to start it
7958 * because we're the ones who set res->priming to true.
7959 * Any other callers of dns_resolver_prime() while we're
7960 * running will see that res->priming is already true and
7964 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7965 if (rdataset == NULL) {
7967 INSIST(res->priming);
7968 INSIST(res->primefetch == NULL);
7969 res->priming = ISC_FALSE;
7973 dns_rdataset_init(rdataset);
7974 LOCK(&res->primelock);
7975 result = dns_resolver_createfetch(res, dns_rootname,
7977 NULL, NULL, NULL, 0,
7978 res->buckets[0].task,
7980 res, rdataset, NULL,
7982 UNLOCK(&res->primelock);
7983 if (result != ISC_R_SUCCESS) {
7985 INSIST(res->priming);
7986 res->priming = ISC_FALSE;
7994 dns_resolver_freeze(dns_resolver_t *res) {
7999 REQUIRE(VALID_RESOLVER(res));
8001 res->frozen = ISC_TRUE;
8005 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
8006 REQUIRE(VALID_RESOLVER(source));
8007 REQUIRE(targetp != NULL && *targetp == NULL);
8009 RRTRACE(source, "attach");
8010 LOCK(&source->lock);
8011 REQUIRE(!source->exiting);
8013 INSIST(source->references > 0);
8014 source->references++;
8015 INSIST(source->references != 0);
8016 UNLOCK(&source->lock);
8022 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
8023 isc_event_t **eventp)
8028 REQUIRE(VALID_RESOLVER(res));
8029 REQUIRE(eventp != NULL);
8036 if (res->exiting && res->activebuckets == 0) {
8038 * We're already shutdown. Send the event.
8040 event->ev_sender = res;
8041 isc_task_send(task, &event);
8044 isc_task_attach(task, &clone);
8045 event->ev_sender = clone;
8046 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
8053 dns_resolver_shutdown(dns_resolver_t *res) {
8056 isc_result_t result;
8058 REQUIRE(VALID_RESOLVER(res));
8064 if (!res->exiting) {
8066 res->exiting = ISC_TRUE;
8068 for (i = 0; i < res->nbuckets; i++) {
8069 LOCK(&res->buckets[i].lock);
8070 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
8072 fctx = ISC_LIST_NEXT(fctx, link))
8073 fctx_shutdown(fctx);
8074 if (res->dispatches4 != NULL && !res->exclusivev4) {
8075 dns_dispatchset_cancelall(res->dispatches4,
8076 res->buckets[i].task);
8078 if (res->dispatches6 != NULL && !res->exclusivev6) {
8079 dns_dispatchset_cancelall(res->dispatches6,
8080 res->buckets[i].task);
8082 res->buckets[i].exiting = ISC_TRUE;
8083 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
8084 INSIST(res->activebuckets > 0);
8085 res->activebuckets--;
8087 UNLOCK(&res->buckets[i].lock);
8089 if (res->activebuckets == 0)
8090 send_shutdown_events(res);
8091 result = isc_timer_reset(res->spillattimer,
8092 isc_timertype_inactive, NULL,
8094 RUNTIME_CHECK(result == ISC_R_SUCCESS);
8101 dns_resolver_detach(dns_resolver_t **resp) {
8102 dns_resolver_t *res;
8103 isc_boolean_t need_destroy = ISC_FALSE;
8105 REQUIRE(resp != NULL);
8107 REQUIRE(VALID_RESOLVER(res));
8113 INSIST(res->references > 0);
8115 if (res->references == 0) {
8116 INSIST(res->exiting && res->activebuckets == 0);
8117 need_destroy = ISC_TRUE;
8128 static inline isc_boolean_t
8129 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
8130 unsigned int options)
8133 * Don't match fetch contexts that are shutting down.
8135 if (fctx->cloned || fctx->state == fetchstate_done ||
8136 ISC_LIST_EMPTY(fctx->events))
8139 if (fctx->type != type || fctx->options != options)
8141 return (dns_name_equal(&fctx->name, name));
8145 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
8146 char namebuf[DNS_NAME_FORMATSIZE];
8147 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8148 int level = ISC_LOG_DEBUG(1);
8150 if (! isc_log_wouldlog(dns_lctx, level))
8153 dns_name_format(name, namebuf, sizeof(namebuf));
8154 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
8156 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
8157 DNS_LOGMODULE_RESOLVER, level,
8158 "createfetch: %s %s", namebuf, typebuf);
8162 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
8163 dns_rdatatype_t type,
8164 dns_name_t *domain, dns_rdataset_t *nameservers,
8165 dns_forwarders_t *forwarders,
8166 unsigned int options, isc_task_t *task,
8167 isc_taskaction_t action, void *arg,
8168 dns_rdataset_t *rdataset,
8169 dns_rdataset_t *sigrdataset,
8170 dns_fetch_t **fetchp)
8172 return (dns_resolver_createfetch2(res, name, type, domain,
8173 nameservers, forwarders, NULL, 0,
8174 options, task, action, arg,
8175 rdataset, sigrdataset, fetchp));
8179 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
8180 dns_rdatatype_t type,
8181 dns_name_t *domain, dns_rdataset_t *nameservers,
8182 dns_forwarders_t *forwarders,
8183 isc_sockaddr_t *client, dns_messageid_t id,
8184 unsigned int options, isc_task_t *task,
8185 isc_taskaction_t action, void *arg,
8186 dns_rdataset_t *rdataset,
8187 dns_rdataset_t *sigrdataset,
8188 dns_fetch_t **fetchp)
8191 fetchctx_t *fctx = NULL;
8192 isc_result_t result = ISC_R_SUCCESS;
8193 unsigned int bucketnum;
8194 isc_boolean_t new_fctx = ISC_FALSE;
8196 unsigned int count = 0;
8197 unsigned int spillat;
8198 unsigned int spillatmin;
8199 isc_boolean_t destroy = ISC_FALSE;
8203 REQUIRE(VALID_RESOLVER(res));
8204 REQUIRE(res->frozen);
8205 /* XXXRTH Check for meta type */
8206 if (domain != NULL) {
8207 REQUIRE(DNS_RDATASET_VALID(nameservers));
8208 REQUIRE(nameservers->type == dns_rdatatype_ns);
8210 REQUIRE(nameservers == NULL);
8211 REQUIRE(forwarders == NULL);
8212 REQUIRE(!dns_rdataset_isassociated(rdataset));
8213 REQUIRE(sigrdataset == NULL ||
8214 !dns_rdataset_isassociated(sigrdataset));
8215 REQUIRE(fetchp != NULL && *fetchp == NULL);
8217 log_fetch(name, type);
8220 * XXXRTH use a mempool?
8222 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
8224 return (ISC_R_NOMEMORY);
8226 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
8229 spillat = res->spillat;
8230 spillatmin = res->spillatmin;
8232 LOCK(&res->buckets[bucketnum].lock);
8234 if (res->buckets[bucketnum].exiting) {
8235 result = ISC_R_SHUTTINGDOWN;
8239 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
8240 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
8242 fctx = ISC_LIST_NEXT(fctx, link)) {
8243 if (fctx_match(fctx, name, type, options))
8249 * Is this a duplicate?
8251 if (fctx != NULL && client != NULL) {
8252 dns_fetchevent_t *fevent;
8253 for (fevent = ISC_LIST_HEAD(fctx->events);
8255 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
8256 if (fevent->client != NULL && fevent->id == id &&
8257 isc_sockaddr_equal(fevent->client, client)) {
8258 result = DNS_R_DUPLICATE;
8264 if (count >= spillatmin && spillatmin != 0) {
8265 INSIST(fctx != NULL);
8266 if (count >= spillat)
8267 fctx->spilled = ISC_TRUE;
8268 if (fctx->spilled) {
8269 result = DNS_R_DROP;
8275 result = fctx_create(res, name, type, domain, nameservers,
8276 options, bucketnum, &fctx);
8277 if (result != ISC_R_SUCCESS)
8279 new_fctx = ISC_TRUE;
8282 result = fctx_join(fctx, task, client, id, action, arg,
8283 rdataset, sigrdataset, fetch);
8285 if (result == ISC_R_SUCCESS) {
8289 event = &fctx->control_event;
8290 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
8291 DNS_EVENT_FETCHCONTROL,
8292 fctx_start, fctx, NULL,
8294 isc_task_send(res->buckets[bucketnum].task, &event);
8297 * We don't care about the result of fctx_unlink()
8298 * since we know we're not exiting.
8300 (void)fctx_unlink(fctx);
8306 UNLOCK(&res->buckets[bucketnum].lock);
8311 if (result == ISC_R_SUCCESS) {
8315 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8321 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
8323 dns_resolver_t *res;
8324 dns_fetchevent_t *event, *next_event;
8327 REQUIRE(DNS_FETCH_VALID(fetch));
8328 fctx = fetch->private;
8329 REQUIRE(VALID_FCTX(fctx));
8332 FTRACE("cancelfetch");
8334 LOCK(&res->buckets[fctx->bucketnum].lock);
8337 * Find the completion event for this fetch (as opposed
8338 * to those for other fetches that have joined the same
8339 * fctx) and send it with result = ISC_R_CANCELED.
8342 if (fctx->state != fetchstate_done) {
8343 for (event = ISC_LIST_HEAD(fctx->events);
8345 event = next_event) {
8346 next_event = ISC_LIST_NEXT(event, ev_link);
8347 if (event->fetch == fetch) {
8348 ISC_LIST_UNLINK(fctx->events, event, ev_link);
8353 if (event != NULL) {
8354 etask = event->ev_sender;
8355 event->ev_sender = fctx;
8356 event->result = ISC_R_CANCELED;
8357 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8360 * The fctx continues running even if no fetches remain;
8361 * the answer is still cached.
8364 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8368 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8370 dns_resolver_t *res;
8371 dns_fetchevent_t *event, *next_event;
8373 unsigned int bucketnum;
8374 isc_boolean_t bucket_empty;
8376 REQUIRE(fetchp != NULL);
8378 REQUIRE(DNS_FETCH_VALID(fetch));
8379 fctx = fetch->private;
8380 REQUIRE(VALID_FCTX(fctx));
8383 FTRACE("destroyfetch");
8385 bucketnum = fctx->bucketnum;
8386 LOCK(&res->buckets[bucketnum].lock);
8389 * Sanity check: the caller should have gotten its event before
8390 * trying to destroy the fetch.
8393 if (fctx->state != fetchstate_done) {
8394 for (event = ISC_LIST_HEAD(fctx->events);
8396 event = next_event) {
8397 next_event = ISC_LIST_NEXT(event, ev_link);
8398 RUNTIME_CHECK(event->fetch != fetch);
8402 bucket_empty = fctx_decreference(fctx);
8404 UNLOCK(&res->buckets[bucketnum].lock);
8406 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8414 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8415 isc_logcategory_t *category, isc_logmodule_t *module,
8416 int level, isc_boolean_t duplicateok)
8419 dns_resolver_t *res;
8420 char domainbuf[DNS_NAME_FORMATSIZE];
8422 REQUIRE(DNS_FETCH_VALID(fetch));
8423 fctx = fetch->private;
8424 REQUIRE(VALID_FCTX(fctx));
8427 LOCK(&res->buckets[fctx->bucketnum].lock);
8429 INSIST(fctx->exitline >= 0);
8430 if (!fctx->logged || duplicateok) {
8431 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8432 isc_log_write(lctx, category, module, level,
8433 "fetch completed at %s:%d for %s in "
8434 "%" ISC_PRINT_QUADFORMAT "u."
8435 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8436 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8437 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8438 "adberr:%u,findfail:%u,valfail:%u]",
8439 __FILE__, fctx->exitline, fctx->info,
8440 fctx->duration / US_PER_SEC,
8441 fctx->duration % US_PER_SEC,
8442 isc_result_totext(fctx->result),
8443 isc_result_totext(fctx->vresult), domainbuf,
8444 fctx->referrals, fctx->restarts,
8445 fctx->querysent, fctx->timeouts, fctx->lamecount,
8446 fctx->neterr, fctx->badresp, fctx->adberr,
8447 fctx->findfail, fctx->valfail);
8448 fctx->logged = ISC_TRUE;
8451 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8455 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8456 REQUIRE(VALID_RESOLVER(resolver));
8457 return (resolver->dispatchmgr);
8461 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8462 REQUIRE(VALID_RESOLVER(resolver));
8463 return (dns_dispatchset_get(resolver->dispatches4));
8467 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8468 REQUIRE(VALID_RESOLVER(resolver));
8469 return (dns_dispatchset_get(resolver->dispatches6));
8473 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8474 REQUIRE(VALID_RESOLVER(resolver));
8475 return (resolver->socketmgr);
8479 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8480 REQUIRE(VALID_RESOLVER(resolver));
8481 return (resolver->taskmgr);
8485 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8486 REQUIRE(VALID_RESOLVER(resolver));
8487 return (resolver->lame_ttl);
8491 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8492 REQUIRE(VALID_RESOLVER(resolver));
8493 resolver->lame_ttl = lame_ttl;
8497 dns_resolver_nrunning(dns_resolver_t *resolver) {
8499 LOCK(&resolver->nlock);
8500 n = resolver->nfctx;
8501 UNLOCK(&resolver->nlock);
8506 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8507 dns_name_t *name, in_port_t port) {
8509 isc_result_t result;
8511 REQUIRE(VALID_RESOLVER(resolver));
8512 REQUIRE(!resolver->frozen);
8513 REQUIRE((alt == NULL) ^ (name == NULL));
8515 a = isc_mem_get(resolver->mctx, sizeof(*a));
8517 return (ISC_R_NOMEMORY);
8519 a->isaddress = ISC_TRUE;
8522 a->isaddress = ISC_FALSE;
8523 a->_u._n.port = port;
8524 dns_name_init(&a->_u._n.name, NULL);
8525 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8526 if (result != ISC_R_SUCCESS) {
8527 isc_mem_put(resolver->mctx, a, sizeof(*a));
8531 ISC_LINK_INIT(a, link);
8532 ISC_LIST_APPEND(resolver->alternates, a, link);
8534 return (ISC_R_SUCCESS);
8538 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8539 REQUIRE(VALID_RESOLVER(resolver));
8540 resolver->udpsize = udpsize;
8544 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8545 REQUIRE(VALID_RESOLVER(resolver));
8546 return (resolver->udpsize);
8550 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8552 dns_badcache_t *bad, *prev, *next;
8554 REQUIRE(VALID_RESOLVER(resolver));
8556 LOCK(&resolver->lock);
8557 if (resolver->badcache == NULL)
8562 isc_result_t result;
8563 result = isc_time_now(&now);
8564 if (result != ISC_R_SUCCESS)
8565 isc_time_settoepoch(&now);
8566 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8568 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8571 n = isc_time_compare(&bad->expire, &now);
8572 if (n < 0 || dns_name_equal(name, &bad->name)) {
8574 resolver->badcache[i] = bad->next;
8576 prev->next = bad->next;
8577 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8579 resolver->badcount--;
8584 destroy_badcache(resolver);
8587 UNLOCK(&resolver->lock);
8592 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8593 unsigned int newsize;
8594 dns_badcache_t **new, *bad, *next;
8598 newsize = resolver->badhash * 2 + 1;
8600 newsize = (resolver->badhash - 1) / 2;
8602 new = isc_mem_get(resolver->mctx,
8603 sizeof(*resolver->badcache) * newsize);
8606 memset(new, 0, sizeof(*resolver->badcache) * newsize);
8607 for (i = 0; i < resolver->badhash; i++) {
8608 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8610 if (isc_time_compare(&bad->expire, now) < 0) {
8611 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8613 resolver->badcount--;
8615 bad->next = new[bad->hashval % newsize];
8616 new[bad->hashval % newsize] = bad;
8620 isc_mem_put(resolver->mctx, resolver->badcache,
8621 sizeof(*resolver->badcache) * resolver->badhash);
8622 resolver->badhash = newsize;
8623 resolver->badcache = new;
8627 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8628 dns_rdatatype_t type, isc_time_t *expire)
8631 isc_result_t result = ISC_R_SUCCESS;
8632 unsigned int i, hashval;
8633 dns_badcache_t *bad, *prev, *next;
8635 REQUIRE(VALID_RESOLVER(resolver));
8637 LOCK(&resolver->lock);
8638 if (resolver->badcache == NULL) {
8639 resolver->badcache = isc_mem_get(resolver->mctx,
8640 sizeof(*resolver->badcache) *
8642 if (resolver->badcache == NULL)
8644 resolver->badhash = DNS_BADCACHE_SIZE;
8645 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8649 result = isc_time_now(&now);
8650 if (result != ISC_R_SUCCESS)
8651 isc_time_settoepoch(&now);
8652 hashval = dns_name_hash(name, ISC_FALSE);
8653 i = hashval % resolver->badhash;
8655 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8657 if (bad->type == type && dns_name_equal(name, &bad->name))
8659 if (isc_time_compare(&bad->expire, &now) < 0) {
8661 resolver->badcache[i] = bad->next;
8663 prev->next = bad->next;
8664 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8666 resolver->badcount--;
8671 isc_buffer_t buffer;
8672 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8676 bad->hashval = hashval;
8677 bad->expire = *expire;
8678 isc_buffer_init(&buffer, bad + 1, name->length);
8679 dns_name_init(&bad->name, NULL);
8680 dns_name_copy(name, &bad->name, &buffer);
8681 bad->next = resolver->badcache[i];
8682 resolver->badcache[i] = bad;
8683 resolver->badcount++;
8684 if (resolver->badcount > resolver->badhash * 8)
8685 resizehash(resolver, &now, ISC_TRUE);
8686 if (resolver->badcount < resolver->badhash * 2 &&
8687 resolver->badhash > DNS_BADCACHE_SIZE)
8688 resizehash(resolver, &now, ISC_FALSE);
8690 bad->expire = *expire;
8692 UNLOCK(&resolver->lock);
8696 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8697 dns_rdatatype_t type, isc_time_t *now)
8699 dns_badcache_t *bad, *prev, *next;
8700 isc_boolean_t answer = ISC_FALSE;
8703 REQUIRE(VALID_RESOLVER(resolver));
8705 LOCK(&resolver->lock);
8706 if (resolver->badcache == NULL)
8709 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8711 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8714 * Search the hash list. Clean out expired records as we go.
8716 if (isc_time_compare(&bad->expire, now) < 0) {
8718 prev->next = bad->next;
8720 resolver->badcache[i] = bad->next;
8721 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8723 resolver->badcount--;
8726 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8734 * Slow sweep to clean out stale records.
8736 i = resolver->badsweep++ % resolver->badhash;
8737 bad = resolver->badcache[i];
8738 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8739 resolver->badcache[i] = bad->next;
8740 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8742 resolver->badcount--;
8746 UNLOCK(&resolver->lock);
8751 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8752 char namebuf[DNS_NAME_FORMATSIZE];
8753 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8754 dns_badcache_t *bad, *next, *prev;
8759 LOCK(&resolver->lock);
8760 fprintf(fp, ";\n; Bad cache\n;\n");
8762 if (resolver->badcache == NULL)
8766 for (i = 0; i < resolver->badhash; i++) {
8768 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8770 if (isc_time_compare(&bad->expire, &now) < 0) {
8772 prev->next = bad->next;
8774 resolver->badcache[i] = bad->next;
8775 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8777 resolver->badcount--;
8781 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8782 dns_rdatatype_format(bad->type, typebuf,
8784 t = isc_time_microdiff(&bad->expire, &now);
8786 fprintf(fp, "; %s/%s [ttl "
8787 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8788 namebuf, typebuf, t);
8793 UNLOCK(&resolver->lock);
8797 free_algorithm(void *node, void *arg) {
8798 unsigned char *algorithms = node;
8799 isc_mem_t *mctx = arg;
8801 isc_mem_put(mctx, algorithms, *algorithms);
8805 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8807 REQUIRE(VALID_RESOLVER(resolver));
8810 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8812 if (resolver->algorithms != NULL)
8813 dns_rbt_destroy(&resolver->algorithms);
8815 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8820 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8823 unsigned int len, mask;
8825 unsigned char *algorithms;
8826 isc_result_t result;
8827 dns_rbtnode_t *node = NULL;
8829 REQUIRE(VALID_RESOLVER(resolver));
8831 return (ISC_R_RANGE);
8834 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8836 if (resolver->algorithms == NULL) {
8837 result = dns_rbt_create(resolver->mctx, free_algorithm,
8838 resolver->mctx, &resolver->algorithms);
8839 if (result != ISC_R_SUCCESS)
8844 mask = 1 << (alg%8);
8846 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8848 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8849 algorithms = node->data;
8850 if (algorithms == NULL || len > *algorithms) {
8851 new = isc_mem_get(resolver->mctx, len);
8853 result = ISC_R_NOMEMORY;
8856 memset(new, 0, len);
8857 if (algorithms != NULL)
8858 memmove(new, algorithms, *algorithms);
8862 if (algorithms != NULL)
8863 isc_mem_put(resolver->mctx, algorithms,
8866 algorithms[len-1] |= mask;
8868 result = ISC_R_SUCCESS;
8871 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8877 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8880 unsigned int len, mask;
8881 unsigned char *algorithms;
8883 isc_result_t result;
8884 isc_boolean_t found = ISC_FALSE;
8886 REQUIRE(VALID_RESOLVER(resolver));
8889 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8891 if (resolver->algorithms == NULL)
8893 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8894 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8896 mask = 1 << (alg%8);
8898 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8903 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8907 return (dst_algorithm_supported(alg));
8911 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8914 return (dns_ds_digest_supported(digest));
8918 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8920 REQUIRE(VALID_RESOLVER(resolver));
8923 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8925 if (resolver->mustbesecure != NULL)
8926 dns_rbt_destroy(&resolver->mustbesecure);
8928 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8932 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8935 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8936 isc_boolean_t value)
8938 isc_result_t result;
8940 REQUIRE(VALID_RESOLVER(resolver));
8943 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8945 if (resolver->mustbesecure == NULL) {
8946 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8947 &resolver->mustbesecure);
8948 if (result != ISC_R_SUCCESS)
8951 result = dns_rbt_addname(resolver->mustbesecure, name,
8952 value ? &yes : &no);
8955 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8961 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8963 isc_boolean_t value = ISC_FALSE;
8964 isc_result_t result;
8966 REQUIRE(VALID_RESOLVER(resolver));
8969 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8971 if (resolver->mustbesecure == NULL)
8973 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8974 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8975 value = *(isc_boolean_t*)data;
8978 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8984 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8985 isc_uint32_t *min, isc_uint32_t *max)
8987 REQUIRE(VALID_RESOLVER(resolver));
8989 LOCK(&resolver->lock);
8991 *cur = resolver->spillat;
8993 *min = resolver->spillatmin;
8995 *max = resolver->spillatmax;
8996 UNLOCK(&resolver->lock);
9000 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
9003 REQUIRE(VALID_RESOLVER(resolver));
9005 LOCK(&resolver->lock);
9006 resolver->spillatmin = resolver->spillat = min;
9007 resolver->spillatmax = max;
9008 UNLOCK(&resolver->lock);
9012 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
9013 REQUIRE(VALID_RESOLVER(resolver));
9015 return (resolver->zero_no_soa_ttl);
9019 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
9020 REQUIRE(VALID_RESOLVER(resolver));
9022 resolver->zero_no_soa_ttl = state;
9026 dns_resolver_getoptions(dns_resolver_t *resolver) {
9027 REQUIRE(VALID_RESOLVER(resolver));
9029 return (resolver->options);
9033 dns_resolver_gettimeout(dns_resolver_t *resolver) {
9034 REQUIRE(VALID_RESOLVER(resolver));
9036 return (resolver->query_timeout);
9040 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
9041 REQUIRE(VALID_RESOLVER(resolver));
9044 seconds = DEFAULT_QUERY_TIMEOUT;
9045 if (seconds > MAXIMUM_QUERY_TIMEOUT)
9046 seconds = MAXIMUM_QUERY_TIMEOUT;
9047 if (seconds < MINIMUM_QUERY_TIMEOUT)
9048 seconds = MINIMUM_QUERY_TIMEOUT;
9050 resolver->query_timeout = seconds;