2 * Copyright (C) 2004-2015 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.
22 #include <isc/counter.h>
24 #include <isc/platform.h>
25 #include <isc/print.h>
26 #include <isc/string.h>
27 #include <isc/random.h>
28 #include <isc/socket.h>
29 #include <isc/stats.h>
31 #include <isc/timer.h>
36 #include <dns/cache.h>
38 #include <dns/dispatch.h>
40 #include <dns/events.h>
41 #include <dns/forward.h>
42 #include <dns/keytable.h>
44 #include <dns/message.h>
45 #include <dns/ncache.h>
47 #include <dns/nsec3.h>
48 #include <dns/opcode.h>
51 #include <dns/rcode.h>
52 #include <dns/rdata.h>
53 #include <dns/rdataclass.h>
54 #include <dns/rdatalist.h>
55 #include <dns/rdataset.h>
56 #include <dns/rdatastruct.h>
57 #include <dns/rdatatype.h>
58 #include <dns/resolver.h>
59 #include <dns/result.h>
60 #include <dns/rootns.h>
61 #include <dns/stats.h>
63 #include <dns/validator.h>
65 #ifdef WANT_QUERYTRACE
66 #define RTRACE(m) isc_log_write(dns_lctx, \
67 DNS_LOGCATEGORY_RESOLVER, \
68 DNS_LOGMODULE_RESOLVER, \
70 "res %p: %s", res, (m))
71 #define RRTRACE(r, m) isc_log_write(dns_lctx, \
72 DNS_LOGCATEGORY_RESOLVER, \
73 DNS_LOGMODULE_RESOLVER, \
75 "res %p: %s", (r), (m))
76 #define FCTXTRACE(m) \
77 isc_log_write(dns_lctx, \
78 DNS_LOGCATEGORY_RESOLVER, \
79 DNS_LOGMODULE_RESOLVER, \
82 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 FCTXTRACE3(m, res) \
91 isc_log_write(dns_lctx, \
92 DNS_LOGCATEGORY_RESOLVER, \
93 DNS_LOGMODULE_RESOLVER, \
95 "fctx %p(%s): [result: %s] %s", \
97 isc_result_totext(res), (m))
98 #define FCTXTRACE4(m1, m2, res) \
99 isc_log_write(dns_lctx, \
100 DNS_LOGCATEGORY_RESOLVER, \
101 DNS_LOGMODULE_RESOLVER, \
103 "fctx %p(%s): [result: %s] %s %s", \
105 isc_result_totext(res), (m1), (m2))
106 #define FTRACE(m) isc_log_write(dns_lctx, \
107 DNS_LOGCATEGORY_RESOLVER, \
108 DNS_LOGMODULE_RESOLVER, \
110 "fetch %p (fctx %p(%s)): %s", \
111 fetch, fetch->private, \
112 fetch->private->info, (m))
113 #define QTRACE(m) isc_log_write(dns_lctx, \
114 DNS_LOGCATEGORY_RESOLVER, \
115 DNS_LOGMODULE_RESOLVER, \
117 "resquery %p (fctx %p(%s)): %s", \
118 query, query->fctx, \
119 query->fctx->info, (m))
121 #define RTRACE(m) do { UNUSED(m); } while (0)
122 #define RRTRACE(r, m) do { UNUSED(r); UNUSED(m); } while (0)
123 #define FCTXTRACE(m) do { UNUSED(m); } while (0)
124 #define FCTXTRACE2(m1, m2) do { UNUSED(m1); UNUSED(m2); } while (0)
125 #define FCTXTRACE3(m1, res) do { UNUSED(m1); UNUSED(res); } while (0)
126 #define FCTXTRACE4(m1, m2, res) \
127 do { UNUSED(m1); UNUSED(m2); UNUSED(res); } while (0)
128 #define FTRACE(m) do { UNUSED(m); } while (0)
129 #define QTRACE(m) do { UNUSED(m); } while (0)
130 #endif /* WANT_QUERYTRACE */
132 #define US_PER_SEC 1000000U
134 * The maximum time we will wait for a single query.
136 #define MAX_SINGLE_QUERY_TIMEOUT 9U
137 #define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_SEC)
140 * We need to allow a individual query time to complete / timeout.
142 #define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1U)
144 /* The default time in seconds for the whole query to live. */
145 #ifndef DEFAULT_QUERY_TIMEOUT
146 #define DEFAULT_QUERY_TIMEOUT MINIMUM_QUERY_TIMEOUT
149 /* The maximum time in seconds for the whole query to live. */
150 #ifndef MAXIMUM_QUERY_TIMEOUT
151 #define MAXIMUM_QUERY_TIMEOUT 30
154 /* The default maximum number of recursions to follow before giving up. */
155 #ifndef DEFAULT_RECURSION_DEPTH
156 #define DEFAULT_RECURSION_DEPTH 7
159 /* The default maximum number of iterative queries to allow before giving up. */
160 #ifndef DEFAULT_MAX_QUERIES
161 #define DEFAULT_MAX_QUERIES 50
164 /* Number of hash buckets for zone counters */
165 #ifndef RES_DOMAIN_BUCKETS
166 #define RES_DOMAIN_BUCKETS 523
168 #define RES_NOBUCKET 0xffffffff
171 * Maximum EDNS0 input packet size.
173 #define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */
176 * This defines the maximum number of timeouts we will permit before we
177 * disable EDNS0 on the query.
179 #define MAX_EDNS0_TIMEOUTS 3
181 typedef struct fetchctx fetchctx_t;
183 typedef struct query {
184 /* Locked by task event serialization. */
188 dns_dispatchmgr_t * dispatchmgr;
189 dns_dispatch_t * dispatch;
190 isc_boolean_t exclusivesocket;
191 dns_adbaddrinfo_t * addrinfo;
192 isc_socket_t * tcpsocket;
195 dns_dispentry_t * dispentry;
196 ISC_LINK(struct query) link;
199 dns_tsigkey_t *tsigkey;
200 isc_socketevent_t sendevent;
202 unsigned int options;
203 unsigned int attributes;
205 unsigned int connects;
206 unsigned char data[512];
209 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
210 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
212 #define RESQUERY_ATTR_CANCELED 0x02
214 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
215 #define RESQUERY_CANCELED(q) (((q)->attributes & \
216 RESQUERY_ATTR_CANCELED) != 0)
217 #define RESQUERY_SENDING(q) ((q)->sends > 0)
220 fetchstate_init = 0, /*%< Start event has not run yet. */
222 fetchstate_done /*%< FETCHDONE events posted. */
226 badns_unreachable = 0,
234 dns_resolver_t * res;
236 dns_rdatatype_t type;
237 unsigned int options;
238 unsigned int bucketnum;
239 unsigned int dbucketnum;
243 /*% Locked by appropriate bucket lock. */
245 isc_boolean_t want_shutdown;
246 isc_boolean_t cloned;
247 isc_boolean_t spilled;
248 unsigned int references;
249 isc_event_t control_event;
250 ISC_LINK(struct fetchctx) link;
251 ISC_LIST(dns_fetchevent_t) events;
252 /*% Locked by task event serialization. */
254 dns_rdataset_t nameservers;
255 unsigned int attributes;
258 isc_interval_t interval;
259 dns_message_t * qmessage;
260 dns_message_t * rmessage;
261 ISC_LIST(resquery_t) queries;
262 dns_adbfindlist_t finds;
263 dns_adbfind_t * find;
264 dns_adbfindlist_t altfinds;
265 dns_adbfind_t * altfind;
266 dns_adbaddrinfolist_t forwaddrs;
267 dns_adbaddrinfolist_t altaddrs;
268 isc_sockaddrlist_t forwarders;
269 dns_fwdpolicy_t fwdpolicy;
270 isc_sockaddrlist_t bad;
271 isc_sockaddrlist_t edns;
272 isc_sockaddrlist_t edns512;
273 isc_sockaddrlist_t bad_edns;
274 dns_validator_t * validator;
275 ISC_LIST(dns_validator_t) validators;
278 isc_boolean_t ns_ttl_ok;
283 * The number of events we're waiting for.
285 unsigned int pending;
288 * The number of times we've "restarted" the current
289 * nameserver set. This acts as a failsafe to prevent
290 * us from pounding constantly on a particular set of
291 * servers that, for whatever reason, are not giving
292 * us useful responses, but are responding in such a
293 * way that they are not marked "bad".
295 unsigned int restarts;
298 * The number of timeouts that have occurred since we
299 * last successfully received a response packet. This
300 * is used for EDNS0 black hole detection.
302 unsigned int timeouts;
305 * Look aside state for DS lookups.
308 dns_fetch_t * nsfetch;
309 dns_rdataset_t nsrrset;
312 * Number of queries that reference this context.
314 unsigned int nqueries;
317 * The reason to print when logging a successful
318 * response to a query.
323 * Random numbers to use for mixing up server addresses.
325 isc_uint32_t rand_buf;
326 isc_uint32_t rand_bits;
329 * Fetch-local statistics for detailed logging.
331 isc_result_t result; /*%< fetch result */
332 isc_result_t vresult; /*%< validation result */
335 isc_uint64_t duration;
336 isc_boolean_t logged;
337 unsigned int querysent;
338 unsigned int referrals;
339 unsigned int lamecount;
340 unsigned int quotacount;
342 unsigned int badresp;
344 unsigned int findfail;
345 unsigned int valfail;
346 isc_boolean_t timeout;
347 dns_adbaddrinfo_t *addrinfo;
348 isc_sockaddr_t *client;
352 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
353 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
355 #define FCTX_ATTR_HAVEANSWER 0x0001
356 #define FCTX_ATTR_GLUING 0x0002
357 #define FCTX_ATTR_ADDRWAIT 0x0004
358 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
359 #define FCTX_ATTR_WANTCACHE 0x0010
360 #define FCTX_ATTR_WANTNCACHE 0x0020
361 #define FCTX_ATTR_NEEDEDNS0 0x0040
362 #define FCTX_ATTR_TRIEDFIND 0x0080
363 #define FCTX_ATTR_TRIEDALT 0x0100
365 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
367 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
369 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
371 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
373 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
374 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
375 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
376 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
377 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
380 dns_adbaddrinfo_t * addrinfo;
387 fetchctx_t * private;
390 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
391 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
393 typedef struct fctxbucket {
396 ISC_LIST(fetchctx_t) fctxs;
397 isc_boolean_t exiting;
401 #ifdef ENABLE_FETCHLIMIT
402 typedef struct fctxcount fctxcount_t;
404 dns_fixedname_t fdname;
407 isc_uint32_t allowed;
408 isc_uint32_t dropped;
409 isc_stdtime_t logged;
410 ISC_LINK(fctxcount_t) link;
413 typedef struct zonebucket {
416 ISC_LIST(fctxcount_t) list;
418 #endif /* ENABLE_FETCHLIMIT */
420 typedef struct alternate {
421 isc_boolean_t isaddress;
429 ISC_LINK(struct alternate) link;
432 typedef struct dns_badcache dns_badcache_t;
433 struct dns_badcache {
434 dns_badcache_t * next;
435 dns_rdatatype_t type;
437 unsigned int hashval;
440 #define DNS_BADCACHE_SIZE 1021
441 #define DNS_BADCACHE_TTL(fctx) \
442 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
444 struct dns_resolver {
450 isc_mutex_t primelock;
451 dns_rdataclass_t rdclass;
452 isc_socketmgr_t * socketmgr;
453 isc_timermgr_t * timermgr;
454 isc_taskmgr_t * taskmgr;
456 isc_boolean_t frozen;
457 unsigned int options;
458 dns_dispatchmgr_t * dispatchmgr;
459 dns_dispatchset_t * dispatches4;
460 isc_boolean_t exclusivev4;
461 dns_dispatchset_t * dispatches6;
462 isc_boolean_t exclusivev6;
463 unsigned int nbuckets;
464 fctxbucket_t * buckets;
465 #ifdef ENABLE_FETCHLIMIT
466 zonebucket_t * dbuckets;
467 #endif /* ENABLE_FETCHLIMIT */
468 isc_uint32_t lame_ttl;
469 ISC_LIST(alternate_t) alternates;
470 isc_uint16_t udpsize;
472 isc_rwlock_t alglock;
474 dns_rbt_t * algorithms;
476 isc_rwlock_t mbslock;
478 dns_rbt_t * mustbesecure;
479 unsigned int spillatmax;
480 unsigned int spillatmin;
481 isc_timer_t * spillattimer;
482 isc_boolean_t zero_no_soa_ttl;
483 unsigned int query_timeout;
484 unsigned int maxdepth;
485 unsigned int maxqueries;
486 isc_result_t quotaresp[2];
488 /* Locked by lock. */
489 unsigned int references;
490 isc_boolean_t exiting;
491 isc_eventlist_t whenshutdown;
492 unsigned int activebuckets;
493 isc_boolean_t priming;
494 unsigned int spillat; /* clients-per-query */
495 unsigned int zspill; /* fetches-per-zone */
498 dns_badcache_t ** badcache;
499 unsigned int badcount;
500 unsigned int badhash;
501 unsigned int badsweep;
503 /* Locked by primelock. */
504 dns_fetch_t * primefetch;
505 /* Locked by nlock. */
509 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
510 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
513 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
514 * which we also use as an addrinfo flag.
516 #define FCTX_ADDRINFO_MARK 0x0001
517 #define FCTX_ADDRINFO_FORWARDER 0x1000
518 #define FCTX_ADDRINFO_TRIED 0x2000
519 #define FCTX_ADDRINFO_EDNSOK 0x4000
521 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
523 #define ISFORWARDER(a) (((a)->flags & \
524 FCTX_ADDRINFO_FORWARDER) != 0)
525 #define TRIED(a) (((a)->flags & \
526 FCTX_ADDRINFO_TRIED) != 0)
527 #define EDNSOK(a) (((a)->flags & \
528 FCTX_ADDRINFO_EDNSOK) != 0)
530 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
531 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
533 static void destroy(dns_resolver_t *res);
534 static void empty_bucket(dns_resolver_t *res);
535 static isc_result_t resquery_send(resquery_t *query);
536 static void resquery_response(isc_task_t *task, isc_event_t *event);
537 static void resquery_connected(isc_task_t *task, isc_event_t *event);
538 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
539 isc_boolean_t badcache);
540 static void fctx_destroy(fetchctx_t *fctx);
541 static isc_boolean_t fctx_unlink(fetchctx_t *fctx);
542 static isc_result_t ncache_adderesult(dns_message_t *message,
543 dns_db_t *cache, dns_dbnode_t *node,
544 dns_rdatatype_t covers,
545 isc_stdtime_t now, dns_ttl_t maxttl,
546 isc_boolean_t optout,
547 isc_boolean_t secure,
548 dns_rdataset_t *ardataset,
549 isc_result_t *eresultp);
550 static void validated(isc_task_t *task, isc_event_t *event);
551 static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked);
552 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
553 isc_result_t reason, badnstype_t badtype);
554 static inline isc_result_t findnoqname(fetchctx_t *fctx, dns_name_t *name,
555 dns_rdatatype_t type,
556 dns_name_t **noqname);
557 static void fctx_increference(fetchctx_t *fctx);
558 static isc_boolean_t fctx_decreference(fetchctx_t *fctx);
561 * Increment resolver-related statistics counters.
564 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
565 if (res->view->resstats != NULL)
566 isc_stats_increment(res->view->resstats, counter);
570 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
571 dns_rdatatype_t type, dns_rdataset_t *rdataset,
572 dns_rdataset_t *sigrdataset, unsigned int valoptions,
575 dns_validator_t *validator = NULL;
576 dns_valarg_t *valarg;
579 valarg = isc_mem_get(fctx->mctx, sizeof(*valarg));
581 return (ISC_R_NOMEMORY);
584 valarg->addrinfo = addrinfo;
586 if (!ISC_LIST_EMPTY(fctx->validators))
587 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
589 result = dns_validator_create(fctx->res->view, name, type, rdataset,
590 sigrdataset, fctx->rmessage,
591 valoptions, task, validated, valarg,
593 if (result == ISC_R_SUCCESS) {
594 inc_stats(fctx->res, dns_resstatscounter_val);
595 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
596 INSIST(fctx->validator == NULL);
597 fctx->validator = validator;
599 ISC_LIST_APPEND(fctx->validators, validator, link);
601 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
606 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
607 dns_namereln_t namereln;
608 dns_rdata_rrsig_t rrsig;
609 dns_rdata_t rdata = DNS_RDATA_INIT;
614 for (result = dns_rdataset_first(rdataset);
615 result == ISC_R_SUCCESS;
616 result = dns_rdataset_next(rdataset)) {
617 dns_rdataset_current(rdataset, &rdata);
618 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
619 RUNTIME_CHECK(result == ISC_R_SUCCESS);
620 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
622 if (namereln == dns_namereln_subdomain)
624 dns_rdata_reset(&rdata);
630 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
632 dns_name_t *domain = &fctx->domain;
633 dns_rdataset_t *rdataset;
634 dns_rdatatype_t type;
636 isc_boolean_t keep_auth = ISC_FALSE;
638 if (message->rcode == dns_rcode_nxdomain)
642 * A DS RRset can appear anywhere in a zone, even for a delegation-only
643 * zone. So a response to an explicit query for this type should be
644 * excluded from delegation-only fixup.
646 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
647 * response to a query for these types can never violate the
648 * delegation-only assumption: if the query name is below a
649 * zone cut, the response should normally be a referral, which should
650 * be accepted; if the query name is below a zone cut but the server
651 * happens to have authority for the zone of the query name, the
652 * response is a (non-referral) answer. But this does not violate
653 * delegation-only because the query name must be in a different zone
654 * due to the "apex-only" nature of these types. Note that if the
655 * remote server happens to have authority for a child zone of a
656 * delegation-only zone, we may still incorrectly "fix" the response
657 * with NXDOMAIN for queries for other types. Unfortunately it's
658 * generally impossible to differentiate this case from violation of
659 * the delegation-only assumption. Once the resolver learns the
660 * correct zone cut, possibly via a separate query for an "apex-only"
661 * type, queries for other types will be resolved correctly.
663 * A query for type ANY will be accepted if it hits an exceptional
664 * type above in the answer section as it should be from a child
667 * Also accept answers with RRSIG records from the child zone.
668 * Direct queries for RRSIG records should not be answered from
672 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
673 (fctx->type == dns_rdatatype_ns ||
674 fctx->type == dns_rdatatype_ds ||
675 fctx->type == dns_rdatatype_soa ||
676 fctx->type == dns_rdatatype_any ||
677 fctx->type == dns_rdatatype_rrsig ||
678 fctx->type == dns_rdatatype_dnskey)) {
679 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
680 while (result == ISC_R_SUCCESS) {
682 dns_message_currentname(message, DNS_SECTION_ANSWER,
684 for (rdataset = ISC_LIST_HEAD(name->list);
686 rdataset = ISC_LIST_NEXT(rdataset, link)) {
687 if (!dns_name_equal(name, &fctx->name))
689 type = rdataset->type;
693 if (type == dns_rdatatype_rrsig &&
694 rrsig_fromchildzone(fctx, rdataset))
697 * Direct query for apex records or DS.
699 if (fctx->type == type &&
700 (type == dns_rdatatype_ds ||
701 type == dns_rdatatype_ns ||
702 type == dns_rdatatype_soa ||
703 type == dns_rdatatype_dnskey))
706 * Indirect query for apex records or DS.
708 if (fctx->type == dns_rdatatype_any &&
709 (type == dns_rdatatype_ns ||
710 type == dns_rdatatype_ds ||
711 type == dns_rdatatype_soa ||
712 type == dns_rdatatype_dnskey))
715 result = dns_message_nextname(message,
721 * A NODATA response to a DS query?
723 if (fctx->type == dns_rdatatype_ds &&
724 message->counts[DNS_SECTION_ANSWER] == 0)
727 /* Look for referral or indication of answer from child zone? */
728 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
731 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
732 while (result == ISC_R_SUCCESS) {
734 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
735 for (rdataset = ISC_LIST_HEAD(name->list);
737 rdataset = ISC_LIST_NEXT(rdataset, link)) {
738 type = rdataset->type;
739 if (type == dns_rdatatype_soa &&
740 dns_name_equal(name, domain))
741 keep_auth = ISC_TRUE;
743 if (type != dns_rdatatype_ns &&
744 type != dns_rdatatype_soa &&
745 type != dns_rdatatype_rrsig)
748 if (type == dns_rdatatype_rrsig) {
749 if (rrsig_fromchildzone(fctx, rdataset))
755 /* NS or SOA records. */
756 if (dns_name_equal(name, domain)) {
758 * If a query for ANY causes a negative
759 * response, we can be sure that this is
760 * an empty node. For other type of queries
761 * we cannot differentiate an empty node
762 * from a node that just doesn't have that
763 * type of record. We only accept the former
766 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
767 fctx->type == dns_rdatatype_any)
769 } else if (dns_name_issubdomain(name, domain)) {
770 /* Referral or answer from child zone. */
774 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
778 message->rcode = dns_rcode_nxdomain;
779 message->counts[DNS_SECTION_ANSWER] = 0;
781 message->counts[DNS_SECTION_AUTHORITY] = 0;
782 message->counts[DNS_SECTION_ADDITIONAL] = 0;
786 static inline isc_result_t
787 fctx_starttimer(fetchctx_t *fctx) {
789 * Start the lifetime timer for fctx.
791 * This is also used for stopping the idle timer; in that
792 * case we must purge events already posted to ensure that
793 * no further idle events are delivered.
795 return (isc_timer_reset(fctx->timer, isc_timertype_once,
796 &fctx->expires, NULL, ISC_TRUE));
800 fctx_stoptimer(fetchctx_t *fctx) {
804 * We don't return a result if resetting the timer to inactive fails
805 * since there's nothing to be done about it. Resetting to inactive
806 * should never fail anyway, since the code as currently written
807 * cannot fail in that case.
809 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
810 NULL, NULL, ISC_TRUE);
811 if (result != ISC_R_SUCCESS) {
812 UNEXPECTED_ERROR(__FILE__, __LINE__,
813 "isc_timer_reset(): %s",
814 isc_result_totext(result));
819 static inline isc_result_t
820 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
822 * Start the idle timer for fctx. The lifetime timer continues
825 return (isc_timer_reset(fctx->timer, isc_timertype_once,
826 &fctx->expires, interval, ISC_FALSE));
830 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
831 * we use fctx_stopidletimer for readability in the code below.
833 #define fctx_stopidletimer fctx_starttimer
836 resquery_destroy(resquery_t **queryp) {
843 REQUIRE(queryp != NULL);
845 REQUIRE(!ISC_LINK_LINKED(query, link));
847 INSIST(query->tcpsocket == NULL);
851 bucket = fctx->bucketnum;
855 LOCK(&res->buckets[bucket].lock);
856 empty = fctx_decreference(query->fctx);
857 UNLOCK(&res->buckets[bucket].lock);
860 isc_mem_put(query->mctx, query, sizeof(*query));
868 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
869 isc_time_t *finish, isc_boolean_t no_response)
873 unsigned int rtt, rttms;
876 dns_adbaddrinfo_t *addrinfo;
883 FCTXTRACE("cancelquery");
885 REQUIRE(!RESQUERY_CANCELED(query));
887 query->attributes |= RESQUERY_ATTR_CANCELED;
890 * Should we update the RTT?
892 if (finish != NULL || no_response) {
893 if (finish != NULL) {
895 * We have both the start and finish times for this
896 * packet, so we can compute a real RTT.
898 rtt = (unsigned int)isc_time_microdiff(finish,
900 factor = DNS_ADB_RTTADJDEFAULT;
903 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
905 dns_resstatscounter_queryrtt0);
906 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
908 dns_resstatscounter_queryrtt1);
909 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
911 dns_resstatscounter_queryrtt2);
912 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
914 dns_resstatscounter_queryrtt3);
915 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
917 dns_resstatscounter_queryrtt4);
920 dns_resstatscounter_queryrtt5);
924 * We don't have an RTT for this query. Maybe the
925 * packet was lost, or maybe this server is very
926 * slow. We don't know. Increase the RTT.
929 rtt = query->addrinfo->srtt + 200000;
930 if (rtt > MAX_SINGLE_QUERY_TIMEOUT_US)
931 rtt = MAX_SINGLE_QUERY_TIMEOUT_US;
934 * Replace the current RTT with our value.
936 factor = DNS_ADB_RTTADJREPLACE;
937 dns_adb_timeout(fctx->adb, query->addrinfo);
940 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
943 #ifdef ENABLE_FETCHLIMIT
944 dns_adb_endudpfetch(fctx->adb, query->addrinfo);
945 #endif /* ENABLE_FETCHLIMIT */
948 * Age RTTs of servers not tried.
950 isc_stdtime_get(&now);
952 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
954 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
955 if (UNMARKED(addrinfo))
956 dns_adb_agesrtt(fctx->adb, addrinfo, now);
958 if (finish != NULL && TRIEDFIND(fctx))
959 for (find = ISC_LIST_HEAD(fctx->finds);
961 find = ISC_LIST_NEXT(find, publink))
962 for (addrinfo = ISC_LIST_HEAD(find->list);
964 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
965 if (UNMARKED(addrinfo))
966 dns_adb_agesrtt(fctx->adb, addrinfo,
969 if (finish != NULL && TRIEDALT(fctx)) {
970 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
972 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
973 if (UNMARKED(addrinfo))
974 dns_adb_agesrtt(fctx->adb, addrinfo, now);
975 for (find = ISC_LIST_HEAD(fctx->altfinds);
977 find = ISC_LIST_NEXT(find, publink))
978 for (addrinfo = ISC_LIST_HEAD(find->list);
980 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
981 if (UNMARKED(addrinfo))
982 dns_adb_agesrtt(fctx->adb, addrinfo,
987 * Check for any outstanding socket events. If they exist, cancel
988 * them and let the event handlers finish the cleanup. The resolver
989 * only needs to worry about managing the connect and send events;
990 * the dispatcher manages the recv events.
992 if (RESQUERY_CONNECTING(query)) {
994 * Cancel the connect.
996 if (query->tcpsocket != NULL) {
997 isc_socket_cancel(query->tcpsocket, NULL,
998 ISC_SOCKCANCEL_CONNECT);
999 } else if (query->dispentry != NULL) {
1000 INSIST(query->exclusivesocket);
1001 sock = dns_dispatch_getentrysocket(query->dispentry);
1003 isc_socket_cancel(sock, NULL,
1004 ISC_SOCKCANCEL_CONNECT);
1006 } else if (RESQUERY_SENDING(query)) {
1008 * Cancel the pending send.
1010 if (query->exclusivesocket && query->dispentry != NULL)
1011 sock = dns_dispatch_getentrysocket(query->dispentry);
1013 sock = dns_dispatch_getsocket(query->dispatch);
1015 isc_socket_cancel(sock, NULL, ISC_SOCKCANCEL_SEND);
1018 if (query->dispentry != NULL)
1019 dns_dispatch_removeresponse(&query->dispentry, deventp);
1021 ISC_LIST_UNLINK(fctx->queries, query, link);
1023 if (query->tsig != NULL)
1024 isc_buffer_free(&query->tsig);
1026 if (query->tsigkey != NULL)
1027 dns_tsigkey_detach(&query->tsigkey);
1029 if (query->dispatch != NULL)
1030 dns_dispatch_detach(&query->dispatch);
1032 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
1034 * It's safe to destroy the query now.
1036 resquery_destroy(&query);
1040 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
1041 resquery_t *query, *next_query;
1043 FCTXTRACE("cancelqueries");
1045 for (query = ISC_LIST_HEAD(fctx->queries);
1047 query = next_query) {
1048 next_query = ISC_LIST_NEXT(query, link);
1049 fctx_cancelquery(&query, NULL, NULL, no_response);
1054 fctx_cleanupfinds(fetchctx_t *fctx) {
1055 dns_adbfind_t *find, *next_find;
1057 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1059 for (find = ISC_LIST_HEAD(fctx->finds);
1062 next_find = ISC_LIST_NEXT(find, publink);
1063 ISC_LIST_UNLINK(fctx->finds, find, publink);
1064 dns_adb_destroyfind(&find);
1070 fctx_cleanupaltfinds(fetchctx_t *fctx) {
1071 dns_adbfind_t *find, *next_find;
1073 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1075 for (find = ISC_LIST_HEAD(fctx->altfinds);
1078 next_find = ISC_LIST_NEXT(find, publink);
1079 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
1080 dns_adb_destroyfind(&find);
1082 fctx->altfind = NULL;
1086 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
1087 dns_adbaddrinfo_t *addr, *next_addr;
1089 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1091 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
1094 next_addr = ISC_LIST_NEXT(addr, publink);
1095 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
1096 dns_adb_freeaddrinfo(fctx->adb, &addr);
1101 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
1102 dns_adbaddrinfo_t *addr, *next_addr;
1104 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1106 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
1109 next_addr = ISC_LIST_NEXT(addr, publink);
1110 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1111 dns_adb_freeaddrinfo(fctx->adb, &addr);
1116 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1117 FCTXTRACE("stopeverything");
1118 fctx_cancelqueries(fctx, no_response);
1119 fctx_cleanupfinds(fctx);
1120 fctx_cleanupaltfinds(fctx);
1121 fctx_cleanupforwaddrs(fctx);
1122 fctx_cleanupaltaddrs(fctx);
1123 fctx_stoptimer(fctx);
1126 #ifdef ENABLE_FETCHLIMIT
1128 fcount_logspill(fetchctx_t *fctx, fctxcount_t *counter) {
1129 char dbuf[DNS_NAME_FORMATSIZE];
1132 if (! isc_log_wouldlog(dns_lctx, ISC_LOG_INFO))
1135 isc_stdtime_get(&now);
1136 if (counter->logged > now - 60)
1139 dns_name_format(&fctx->domain, dbuf, sizeof(dbuf));
1141 isc_log_write(dns_lctx, DNS_LOGCATEGORY_SPILL,
1142 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1143 "too many simultaneous fetches for %s "
1144 "(allowed %d spilled %d)",
1145 dbuf, counter->allowed, counter->dropped);
1147 counter->logged = now;
1151 fcount_incr(fetchctx_t *fctx, isc_boolean_t force) {
1152 isc_result_t result = ISC_R_SUCCESS;
1153 zonebucket_t *dbucket;
1154 fctxcount_t *counter;
1155 unsigned int bucketnum, spill;
1157 REQUIRE(fctx != NULL);
1158 REQUIRE(fctx->res != NULL);
1160 INSIST(fctx->dbucketnum == RES_NOBUCKET);
1161 bucketnum = dns_name_fullhash(&fctx->domain, ISC_FALSE)
1162 % RES_DOMAIN_BUCKETS;
1164 LOCK(&fctx->res->lock);
1165 spill = fctx->res->zspill;
1166 UNLOCK(&fctx->res->lock);
1168 dbucket = &fctx->res->dbuckets[bucketnum];
1170 LOCK(&dbucket->lock);
1171 for (counter = ISC_LIST_HEAD(dbucket->list);
1173 counter = ISC_LIST_NEXT(counter, link))
1175 if (dns_name_equal(counter->domain, &fctx->domain))
1179 if (counter == NULL) {
1180 counter = isc_mem_get(dbucket->mctx, sizeof(fctxcount_t));
1181 if (counter == NULL)
1182 result = ISC_R_NOMEMORY;
1184 ISC_LINK_INIT(counter, link);
1186 counter->logged = 0;
1187 counter->allowed = 1;
1188 counter->dropped = 0;
1189 dns_fixedname_init(&counter->fdname);
1190 counter->domain = dns_fixedname_name(&counter->fdname);
1191 dns_name_copy(&fctx->domain, counter->domain, NULL);
1192 ISC_LIST_APPEND(dbucket->list, counter, link);
1195 if (!force && spill != 0 && counter->count >= spill) {
1197 fcount_logspill(fctx, counter);
1198 result = ISC_R_QUOTA;
1204 UNLOCK(&dbucket->lock);
1206 if (result == ISC_R_SUCCESS)
1207 fctx->dbucketnum = bucketnum;
1213 fcount_decr(fetchctx_t *fctx) {
1214 zonebucket_t *dbucket;
1215 fctxcount_t *counter;
1217 REQUIRE(fctx != NULL);
1219 if (fctx->dbucketnum == RES_NOBUCKET)
1222 dbucket = &fctx->res->dbuckets[fctx->dbucketnum];
1224 LOCK(&dbucket->lock);
1225 for (counter = ISC_LIST_HEAD(dbucket->list);
1227 counter = ISC_LIST_NEXT(counter, link))
1229 if (dns_name_equal(counter->domain, &fctx->domain))
1233 if (counter != NULL) {
1234 INSIST(counter->count != 0);
1236 fctx->dbucketnum = RES_NOBUCKET;
1238 if (counter->count == 0) {
1239 ISC_LIST_UNLINK(dbucket->list, counter, link);
1240 isc_mem_put(dbucket->mctx, counter, sizeof(*counter));
1244 UNLOCK(&dbucket->lock);
1246 #endif /* ENABLE_FETCHLIMIT */
1249 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1250 dns_fetchevent_t *event, *next_event;
1252 unsigned int count = 0;
1254 isc_boolean_t logit = ISC_FALSE;
1256 unsigned int old_spillat;
1257 unsigned int new_spillat = 0; /* initialized to silence
1258 compiler warnings */
1261 * Caller must be holding the appropriate bucket lock.
1263 REQUIRE(fctx->state == fetchstate_done);
1265 FCTXTRACE("sendevents");
1268 * Keep some record of fetch result for logging later (if required).
1270 fctx->result = result;
1271 fctx->exitline = line;
1273 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1275 for (event = ISC_LIST_HEAD(fctx->events);
1277 event = next_event) {
1278 next_event = ISC_LIST_NEXT(event, ev_link);
1279 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1280 task = event->ev_sender;
1281 event->ev_sender = fctx;
1282 event->vresult = fctx->vresult;
1283 if (!HAVE_ANSWER(fctx))
1284 event->result = result;
1286 INSIST(result != ISC_R_SUCCESS ||
1287 dns_rdataset_isassociated(event->rdataset) ||
1288 fctx->type == dns_rdatatype_any ||
1289 fctx->type == dns_rdatatype_rrsig ||
1290 fctx->type == dns_rdatatype_sig);
1293 * Negative results must be indicated in event->result.
1295 if (dns_rdataset_isassociated(event->rdataset) &&
1296 NEGATIVE(event->rdataset)) {
1297 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1298 event->result == DNS_R_NCACHENXRRSET);
1301 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1305 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1307 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1308 LOCK(&fctx->res->lock);
1309 if (count == fctx->res->spillat && !fctx->res->exiting) {
1310 old_spillat = fctx->res->spillat;
1311 fctx->res->spillat += 5;
1312 if (fctx->res->spillat > fctx->res->spillatmax &&
1313 fctx->res->spillatmax != 0)
1314 fctx->res->spillat = fctx->res->spillatmax;
1315 new_spillat = fctx->res->spillat;
1316 if (new_spillat != old_spillat) {
1319 isc_interval_set(&i, 20 * 60, 0);
1320 result = isc_timer_reset(fctx->res->spillattimer,
1321 isc_timertype_ticker, NULL,
1323 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1325 UNLOCK(&fctx->res->lock);
1327 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1328 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1329 "clients-per-query increased to %u",
1335 log_edns(fetchctx_t *fctx) {
1336 char domainbuf[DNS_NAME_FORMATSIZE];
1338 if (fctx->reason == NULL)
1342 * We do not know if fctx->domain is the actual domain the record
1343 * lives in or a parent domain so we have a '?' after it.
1345 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1346 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1347 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1348 "success resolving '%s' (in '%s'?) after %s",
1349 fctx->info, domainbuf, fctx->reason);
1351 fctx->reason = NULL;
1355 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1356 dns_resolver_t *res;
1357 isc_boolean_t no_response;
1365 if (result == ISC_R_SUCCESS) {
1367 * Log any deferred EDNS timeout messages.
1370 no_response = ISC_TRUE;
1372 no_response = ISC_FALSE;
1374 fctx->reason = NULL;
1375 fctx_stopeverything(fctx, no_response);
1377 LOCK(&res->buckets[fctx->bucketnum].lock);
1379 fctx->state = fetchstate_done;
1380 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1381 fctx_sendevents(fctx, result, line);
1383 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1387 process_sendevent(resquery_t *query, isc_event_t *event) {
1388 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1389 isc_boolean_t destroy_query = ISC_FALSE;
1390 isc_boolean_t retry = ISC_FALSE;
1391 isc_result_t result;
1396 if (RESQUERY_CANCELED(query)) {
1397 if (query->sends == 0 && query->connects == 0) {
1399 * This query was canceled while the
1400 * isc_socket_sendto/connect() was in progress.
1402 if (query->tcpsocket != NULL)
1403 isc_socket_detach(&query->tcpsocket);
1404 destroy_query = ISC_TRUE;
1407 switch (sevent->result) {
1411 case ISC_R_HOSTUNREACH:
1412 case ISC_R_NETUNREACH:
1414 case ISC_R_ADDRNOTAVAIL:
1415 case ISC_R_CONNREFUSED:
1416 FCTXTRACE3("query canceled in sendevent(): "
1417 "no route to host; no response",
1421 * No route to remote.
1423 add_bad(fctx, query->addrinfo, sevent->result,
1425 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1430 FCTXTRACE3("query canceled in sendevent() due to "
1431 "unexpected event result; responding",
1434 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1439 if (event->ev_type == ISC_SOCKEVENT_CONNECT)
1440 isc_event_free(&event);
1444 * Behave as if the idle timer has expired. For TCP
1445 * this may not actually reflect the latest timer.
1447 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1448 result = fctx_stopidletimer(fctx);
1449 if (result != ISC_R_SUCCESS)
1450 fctx_done(fctx, result, __LINE__);
1452 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1456 resquery_destroy(&query);
1460 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1461 resquery_t *query = event->ev_arg;
1463 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1465 QTRACE("udpconnected");
1469 INSIST(RESQUERY_CONNECTING(query));
1473 process_sendevent(query, event);
1477 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1478 resquery_t *query = event->ev_arg;
1480 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1487 * Currently we don't wait for the senddone event before retrying
1488 * a query. This means that if we get really behind, we may end
1489 * up doing extra work!
1494 INSIST(RESQUERY_SENDING(query));
1498 process_sendevent(query, event);
1501 static inline isc_result_t
1502 fctx_addopt(dns_message_t *message, unsigned int version,
1503 isc_uint16_t udpsize, dns_ednsopt_t *ednsopts, size_t count)
1505 dns_rdataset_t *rdataset = NULL;
1506 isc_result_t result;
1508 result = dns_message_buildopt(message, &rdataset, version, udpsize,
1509 DNS_MESSAGEEXTFLAG_DO, ednsopts, count);
1510 if (result != ISC_R_SUCCESS)
1512 return (dns_message_setopt(message, rdataset));
1516 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1517 unsigned int seconds;
1521 * We retry every .8 seconds the first two times through the address
1522 * list, and then we do exponential back-off.
1524 if (fctx->restarts < 3)
1527 us = (800000 << (fctx->restarts - 2));
1530 * Add a fudge factor to the expected rtt based on the current
1535 else if (rtt < 100000)
1541 * Always wait for at least the expected rtt.
1547 * But don't ever wait for more than 10 seconds.
1549 if (us > MAX_SINGLE_QUERY_TIMEOUT_US)
1550 us = MAX_SINGLE_QUERY_TIMEOUT_US;
1552 seconds = us / US_PER_SEC;
1553 us -= seconds * US_PER_SEC;
1554 isc_interval_set(&fctx->interval, seconds, us * 1000);
1558 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1559 unsigned int options)
1561 dns_resolver_t *res;
1563 isc_result_t result;
1565 isc_sockaddr_t addr;
1566 isc_boolean_t have_addr = ISC_FALSE;
1572 task = res->buckets[fctx->bucketnum].task;
1574 srtt = addrinfo->srtt;
1577 * A forwarder needs to make multiple queries. Give it at least
1578 * a second to do these in.
1580 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1583 fctx_setretryinterval(fctx, srtt);
1584 result = fctx_startidletimer(fctx, &fctx->interval);
1585 if (result != ISC_R_SUCCESS)
1588 INSIST(ISC_LIST_EMPTY(fctx->validators));
1590 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1592 query = isc_mem_get(fctx->mctx, sizeof(*query));
1593 if (query == NULL) {
1594 result = ISC_R_NOMEMORY;
1595 goto stop_idle_timer;
1597 query->mctx = fctx->mctx;
1598 query->options = options;
1599 query->attributes = 0;
1601 query->connects = 0;
1603 * Note that the caller MUST guarantee that 'addrinfo' will remain
1604 * valid until this query is canceled.
1606 query->addrinfo = addrinfo;
1607 TIME_NOW(&query->start);
1610 * If this is a TCP query, then we need to make a socket and
1611 * a dispatch for it here. Otherwise we use the resolver's
1614 query->dispatchmgr = res->dispatchmgr;
1615 query->dispatch = NULL;
1616 query->exclusivesocket = ISC_FALSE;
1617 query->tcpsocket = NULL;
1618 if (res->view->peers != NULL) {
1619 dns_peer_t *peer = NULL;
1620 isc_netaddr_t dstip;
1621 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1622 result = dns_peerlist_peerbyaddr(res->view->peers,
1624 if (result == ISC_R_SUCCESS) {
1625 result = dns_peer_getquerysource(peer, &addr);
1626 if (result == ISC_R_SUCCESS)
1627 have_addr = ISC_TRUE;
1631 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1634 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1638 result = dns_dispatch_getlocaladdress(
1639 res->dispatches4->dispatches[0],
1643 result = dns_dispatch_getlocaladdress(
1644 res->dispatches6->dispatches[0],
1648 result = ISC_R_NOTIMPLEMENTED;
1651 if (result != ISC_R_SUCCESS)
1654 isc_sockaddr_setport(&addr, 0);
1656 result = isc_socket_create(res->socketmgr, pf,
1659 if (result != ISC_R_SUCCESS)
1662 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1663 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1664 if (result != ISC_R_SUCCESS)
1665 goto cleanup_socket;
1669 * A dispatch will be created once the connect succeeds.
1673 unsigned int attrs, attrmask;
1674 attrs = DNS_DISPATCHATTR_UDP;
1675 switch (isc_sockaddr_pf(&addr)) {
1677 attrs |= DNS_DISPATCHATTR_IPV4;
1680 attrs |= DNS_DISPATCHATTR_IPV6;
1683 result = ISC_R_NOTIMPLEMENTED;
1686 attrmask = DNS_DISPATCHATTR_UDP;
1687 attrmask |= DNS_DISPATCHATTR_TCP;
1688 attrmask |= DNS_DISPATCHATTR_IPV4;
1689 attrmask |= DNS_DISPATCHATTR_IPV6;
1690 result = dns_dispatch_getudp(res->dispatchmgr,
1692 res->taskmgr, &addr,
1693 4096, 1000, 32768, 16411,
1694 16433, attrs, attrmask,
1696 if (result != ISC_R_SUCCESS)
1699 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1701 dns_dispatch_attach(
1702 dns_resolver_dispatchv4(res),
1704 query->exclusivesocket = res->exclusivev4;
1707 dns_dispatch_attach(
1708 dns_resolver_dispatchv6(res),
1710 query->exclusivesocket = res->exclusivev6;
1713 result = ISC_R_NOTIMPLEMENTED;
1718 * We should always have a valid dispatcher here. If we
1719 * don't support a protocol family, then its dispatcher
1720 * will be NULL, but we shouldn't be finding addresses for
1721 * protocol types we don't support, so the dispatcher
1722 * we found should never be NULL.
1724 INSIST(query->dispatch != NULL);
1727 query->dispentry = NULL;
1728 query->fctx = fctx; /* reference added by caller */
1730 query->tsigkey = NULL;
1731 ISC_LINK_INIT(query, link);
1732 query->magic = QUERY_MAGIC;
1734 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1736 * Connect to the remote server.
1738 * XXXRTH Should we attach to the socket?
1740 result = isc_socket_connect(query->tcpsocket,
1741 &addrinfo->sockaddr, task,
1742 resquery_connected, query);
1743 if (result != ISC_R_SUCCESS)
1744 goto cleanup_socket;
1746 QTRACE("connecting via TCP");
1748 #ifdef ENABLE_FETCHLIMIT
1749 if (dns_adbentry_overquota(addrinfo->entry))
1750 goto cleanup_dispatch;
1752 /* Inform the ADB that we're starting a fetch */
1753 dns_adb_beginudpfetch(fctx->adb, addrinfo);
1754 #endif /* ENABLE_FETCHLIMIT */
1756 result = resquery_send(query);
1757 if (result != ISC_R_SUCCESS)
1758 goto cleanup_dispatch;
1763 ISC_LIST_APPEND(fctx->queries, query, link);
1764 query->fctx->nqueries++;
1765 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1766 inc_stats(res, dns_resstatscounter_queryv4);
1768 inc_stats(res, dns_resstatscounter_queryv6);
1769 if (res->view->resquerystats != NULL)
1770 dns_rdatatypestats_increment(res->view->resquerystats,
1773 return (ISC_R_SUCCESS);
1776 isc_socket_detach(&query->tcpsocket);
1779 if (query->dispatch != NULL)
1780 dns_dispatch_detach(&query->dispatch);
1783 if (query->connects == 0) {
1785 isc_mem_put(fctx->mctx, query, sizeof(*query));
1789 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1794 static isc_boolean_t
1795 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1798 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1800 sa = ISC_LIST_NEXT(sa, link)) {
1801 if (isc_sockaddr_equal(sa, address))
1809 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1812 if (bad_edns(fctx, address))
1815 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1820 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1823 static isc_boolean_t
1824 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1827 for (sa = ISC_LIST_HEAD(fctx->edns);
1829 sa = ISC_LIST_NEXT(sa, link)) {
1830 if (isc_sockaddr_equal(sa, address))
1838 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1841 if (triededns(fctx, address))
1844 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1849 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1852 static isc_boolean_t
1853 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1856 for (sa = ISC_LIST_HEAD(fctx->edns512);
1858 sa = ISC_LIST_NEXT(sa, link)) {
1859 if (isc_sockaddr_equal(sa, address))
1867 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1870 if (triededns512(fctx, address))
1873 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1878 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1881 static isc_boolean_t
1882 wouldvalidate(fetchctx_t *fctx) {
1883 isc_boolean_t secure_domain;
1884 isc_result_t result;
1886 if (!fctx->res->view->enablevalidation)
1889 if (fctx->res->view->dlv != NULL)
1892 result = dns_view_issecuredomain(fctx->res->view, &fctx->name,
1894 if (result != ISC_R_SUCCESS)
1896 return (secure_domain);
1900 resquery_send(resquery_t *query) {
1902 isc_result_t result;
1903 dns_name_t *qname = NULL;
1904 dns_rdataset_t *qrdataset = NULL;
1906 dns_resolver_t *res;
1909 isc_buffer_t tcpbuffer;
1910 isc_sockaddr_t *address;
1911 isc_buffer_t *buffer;
1912 isc_netaddr_t ipaddr;
1913 dns_tsigkey_t *tsigkey = NULL;
1914 dns_peer_t *peer = NULL;
1915 isc_boolean_t useedns;
1916 dns_compress_t cctx;
1917 isc_boolean_t cleanup_cctx = ISC_FALSE;
1918 isc_boolean_t secure_domain;
1919 isc_boolean_t connecting = ISC_FALSE;
1920 dns_ednsopt_t ednsopts[DNS_EDNSOPTIONS];
1921 unsigned ednsopt = 0;
1927 task = res->buckets[fctx->bucketnum].task;
1930 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1932 * Reserve space for the TCP message length.
1934 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1935 isc_buffer_init(&query->buffer, query->data + 2,
1936 sizeof(query->data) - 2);
1937 buffer = &tcpbuffer;
1939 isc_buffer_init(&query->buffer, query->data,
1940 sizeof(query->data));
1941 buffer = &query->buffer;
1944 result = dns_message_gettempname(fctx->qmessage, &qname);
1945 if (result != ISC_R_SUCCESS)
1947 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1948 if (result != ISC_R_SUCCESS)
1952 * Get a query id from the dispatch.
1954 result = dns_dispatch_addresponse2(query->dispatch,
1955 &query->addrinfo->sockaddr,
1962 if (result != ISC_R_SUCCESS)
1965 fctx->qmessage->opcode = dns_opcode_query;
1970 dns_name_init(qname, NULL);
1971 dns_name_clone(&fctx->name, qname);
1972 dns_rdataset_init(qrdataset);
1973 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1974 ISC_LIST_APPEND(qname->list, qrdataset, link);
1975 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1980 * Set RD if the client has requested that we do a recursive query,
1981 * or if we're sending to a forwarder.
1983 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1984 ISFORWARDER(query->addrinfo))
1985 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1988 * Set CD if the client says don't validate or the question is
1989 * under a secure entry point.
1991 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1992 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1993 } else if (res->view->enablevalidation) {
1994 result = dns_view_issecuredomain(res->view, &fctx->name,
1996 if (result != ISC_R_SUCCESS)
1997 secure_domain = ISC_FALSE;
1998 if (res->view->dlv != NULL)
1999 secure_domain = ISC_TRUE;
2001 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
2005 * We don't have to set opcode because it defaults to query.
2007 fctx->qmessage->id = query->id;
2010 * Convert the question to wire format.
2012 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
2013 if (result != ISC_R_SUCCESS)
2014 goto cleanup_message;
2015 cleanup_cctx = ISC_TRUE;
2017 result = dns_message_renderbegin(fctx->qmessage, &cctx,
2019 if (result != ISC_R_SUCCESS)
2020 goto cleanup_message;
2022 result = dns_message_rendersection(fctx->qmessage,
2023 DNS_SECTION_QUESTION, 0);
2024 if (result != ISC_R_SUCCESS)
2025 goto cleanup_message;
2028 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
2029 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
2032 * The ADB does not know about servers with "edns no". Check this,
2033 * and then inform the ADB for future use.
2035 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
2037 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
2040 query->options |= DNS_FETCHOPT_NOEDNS0;
2041 dns_adb_changeflags(fctx->adb, query->addrinfo,
2042 DNS_FETCHOPT_NOEDNS0,
2043 DNS_FETCHOPT_NOEDNS0);
2046 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
2047 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
2048 query->options |= DNS_FETCHOPT_NOEDNS0;
2051 * Handle timeouts by reducing the UDP response size to 512 bytes
2052 * then if that doesn't work disabling EDNS (includes DO) and CD.
2054 * These timeout can be due to:
2055 * * broken nameservers that don't respond to EDNS queries.
2056 * * broken/misconfigured firewalls and NAT implementations
2057 * that don't handle IP fragmentation.
2058 * * broken/misconfigured firewalls that don't handle responses
2059 * greater than 512 bytes.
2060 * * broken/misconfigured firewalls that don't handle EDNS, DO
2062 * * packet loss / link outage.
2064 if (fctx->timeout) {
2065 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
2066 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
2067 (query->options & DNS_FETCHOPT_NOEDNS0) == 0 &&
2068 (!EDNSOK(query->addrinfo) || !wouldvalidate(fctx))) {
2069 query->options |= DNS_FETCHOPT_NOEDNS0;
2070 fctx->reason = "disabling EDNS";
2071 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
2072 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
2073 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
2074 query->options |= DNS_FETCHOPT_EDNS512;
2075 fctx->reason = "reducing the advertised EDNS UDP "
2076 "packet size to 512 octets";
2078 fctx->timeout = ISC_FALSE;
2082 * Use EDNS0, unless the caller doesn't want it, or we know that
2083 * the remote server doesn't like it.
2085 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
2086 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
2087 unsigned int version = 0; /* Default version. */
2089 isc_uint16_t udpsize = res->udpsize;
2090 isc_boolean_t reqnsid = res->view->requestnsid;
2092 flags = query->addrinfo->flags;
2093 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
2094 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
2095 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
2097 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
2099 else if (peer != NULL)
2100 (void)dns_peer_getudpsize(peer, &udpsize);
2102 /* request NSID for current view or peer? */
2104 (void) dns_peer_getrequestnsid(peer, &reqnsid);
2106 INSIST(ednsopt < DNS_EDNSOPTIONS);
2107 ednsopts[ednsopt].code = DNS_OPT_NSID;
2108 ednsopts[ednsopt].length = 0;
2109 ednsopts[ednsopt].value = NULL;
2112 query->ednsversion = version;
2113 result = fctx_addopt(fctx->qmessage, version,
2114 udpsize, ednsopts, ednsopt);
2115 if (reqnsid && result == ISC_R_SUCCESS) {
2116 query->options |= DNS_FETCHOPT_WANTNSID;
2117 } else if (result != ISC_R_SUCCESS) {
2119 * We couldn't add the OPT, but we'll press on.
2120 * We're not using EDNS0, so set the NOEDNS0
2123 query->options |= DNS_FETCHOPT_NOEDNS0;
2124 query->ednsversion = -1;
2128 * We know this server doesn't like EDNS0, so we
2129 * won't use it. Set the NOEDNS0 bit since we're
2132 query->options |= DNS_FETCHOPT_NOEDNS0;
2133 query->ednsversion = -1;
2136 query->ednsversion = -1;
2139 * If we need EDNS0 to do this query and aren't using it, we lose.
2141 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
2142 result = DNS_R_SERVFAIL;
2143 goto cleanup_message;
2146 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
2147 add_triededns(fctx, &query->addrinfo->sockaddr);
2149 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
2150 add_triededns512(fctx, &query->addrinfo->sockaddr);
2153 * Clear CD if EDNS is not in use.
2155 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
2156 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
2159 * Add TSIG record tailored to the current recipient.
2161 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
2162 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
2163 goto cleanup_message;
2165 if (tsigkey != NULL) {
2166 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
2167 dns_tsigkey_detach(&tsigkey);
2168 if (result != ISC_R_SUCCESS)
2169 goto cleanup_message;
2172 result = dns_message_rendersection(fctx->qmessage,
2173 DNS_SECTION_ADDITIONAL, 0);
2174 if (result != ISC_R_SUCCESS)
2175 goto cleanup_message;
2177 result = dns_message_renderend(fctx->qmessage);
2178 if (result != ISC_R_SUCCESS)
2179 goto cleanup_message;
2181 dns_compress_invalidate(&cctx);
2182 cleanup_cctx = ISC_FALSE;
2184 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
2185 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
2187 result = dns_message_getquerytsig(fctx->qmessage,
2190 if (result != ISC_R_SUCCESS)
2191 goto cleanup_message;
2195 * If using TCP, write the length of the message at the beginning
2198 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
2199 isc_buffer_usedregion(&query->buffer, &r);
2200 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
2201 isc_buffer_add(&tcpbuffer, r.length);
2205 * We're now done with the query message.
2207 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2209 if (query->exclusivesocket)
2210 sock = dns_dispatch_getentrysocket(query->dispentry);
2212 sock = dns_dispatch_getsocket(query->dispatch);
2216 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
2217 address = &query->addrinfo->sockaddr;
2218 if (query->exclusivesocket) {
2219 result = isc_socket_connect(sock, address, task,
2220 resquery_udpconnected,
2222 if (result != ISC_R_SUCCESS)
2223 goto cleanup_message;
2224 connecting = ISC_TRUE;
2228 isc_buffer_usedregion(buffer, &r);
2231 * XXXRTH Make sure we don't send to ourselves! We should probably
2232 * prune out these addresses when we get them from the ADB.
2234 ISC_EVENT_INIT(&query->sendevent, sizeof(query->sendevent), 0, NULL,
2235 ISC_SOCKEVENT_SENDDONE, resquery_senddone, query,
2237 result = isc_socket_sendto2(sock, &r, task, address, NULL,
2238 &query->sendevent, 0);
2239 if (result != ISC_R_SUCCESS) {
2242 * This query is still connecting.
2243 * Mark it as canceled so that it will just be
2244 * cleaned up when the connected event is received.
2245 * Keep fctx around until the event is processed.
2247 query->fctx->nqueries++;
2248 query->attributes |= RESQUERY_ATTR_CANCELED;
2250 goto cleanup_message;
2257 return (ISC_R_SUCCESS);
2261 dns_compress_invalidate(&cctx);
2263 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2266 * Stop the dispatcher from listening.
2268 dns_dispatch_removeresponse(&query->dispentry, NULL);
2272 dns_message_puttempname(fctx->qmessage, &qname);
2273 if (qrdataset != NULL)
2274 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2280 resquery_connected(isc_task_t *task, isc_event_t *event) {
2281 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2282 resquery_t *query = event->ev_arg;
2283 isc_boolean_t retry = ISC_FALSE;
2284 isc_interval_t interval;
2285 isc_result_t result;
2289 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2290 REQUIRE(VALID_QUERY(query));
2292 QTRACE("connected");
2299 * Currently we don't wait for the connect event before retrying
2300 * a query. This means that if we get really behind, we may end
2301 * up doing extra work!
2307 if (RESQUERY_CANCELED(query)) {
2309 * This query was canceled while the connect() was in
2312 isc_socket_detach(&query->tcpsocket);
2313 resquery_destroy(&query);
2315 switch (sevent->result) {
2319 * Extend the idle timer for TCP. 20 seconds
2320 * should be long enough for a TCP connection to be
2321 * established, a single DNS request to be sent,
2322 * and the response received.
2324 isc_interval_set(&interval, 20, 0);
2325 result = fctx_startidletimer(query->fctx, &interval);
2326 if (result != ISC_R_SUCCESS) {
2327 FCTXTRACE("query canceled: idle timer failed; "
2330 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2331 fctx_done(fctx, result, __LINE__);
2335 * We are connected. Create a dispatcher and
2339 attrs |= DNS_DISPATCHATTR_TCP;
2340 attrs |= DNS_DISPATCHATTR_PRIVATE;
2341 attrs |= DNS_DISPATCHATTR_CONNECTED;
2342 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2344 attrs |= DNS_DISPATCHATTR_IPV4;
2346 attrs |= DNS_DISPATCHATTR_IPV6;
2347 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2349 result = dns_dispatch_createtcp(query->dispatchmgr,
2351 query->fctx->res->taskmgr,
2352 4096, 2, 1, 1, 3, attrs,
2356 * Regardless of whether dns_dispatch_create()
2357 * succeeded or not, we don't need our reference
2358 * to the socket anymore.
2360 isc_socket_detach(&query->tcpsocket);
2362 if (result == ISC_R_SUCCESS)
2363 result = resquery_send(query);
2365 if (result != ISC_R_SUCCESS) {
2366 FCTXTRACE("query canceled: "
2367 "resquery_send() failed; responding");
2369 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2370 fctx_done(fctx, result, __LINE__);
2374 case ISC_R_NETUNREACH:
2375 case ISC_R_HOSTUNREACH:
2376 case ISC_R_CONNREFUSED:
2378 case ISC_R_ADDRNOTAVAIL:
2379 case ISC_R_CONNECTIONRESET:
2380 FCTXTRACE3("query canceled in connected(): "
2381 "no route to host; no response",
2385 * No route to remote.
2387 isc_socket_detach(&query->tcpsocket);
2388 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2393 FCTXTRACE3("query canceled in connected() due to "
2394 "unexpected event result; responding",
2397 isc_socket_detach(&query->tcpsocket);
2398 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2403 isc_event_free(&event);
2407 * Behave as if the idle timer has expired. For TCP
2408 * connections this may not actually reflect the latest timer.
2410 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2411 result = fctx_stopidletimer(fctx);
2412 if (result != ISC_R_SUCCESS)
2413 fctx_done(fctx, result, __LINE__);
2415 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2420 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2422 dns_adbfind_t *find;
2423 dns_resolver_t *res;
2424 isc_boolean_t want_try = ISC_FALSE;
2425 isc_boolean_t want_done = ISC_FALSE;
2426 isc_boolean_t bucket_empty = ISC_FALSE;
2427 unsigned int bucketnum;
2428 isc_boolean_t dodestroy = ISC_FALSE;
2430 find = event->ev_sender;
2431 fctx = event->ev_arg;
2432 REQUIRE(VALID_FCTX(fctx));
2437 FCTXTRACE("finddone");
2439 bucketnum = fctx->bucketnum;
2440 LOCK(&res->buckets[bucketnum].lock);
2442 INSIST(fctx->pending > 0);
2445 if (ADDRWAIT(fctx)) {
2447 * The fetch is waiting for a name to be found.
2449 INSIST(!SHUTTINGDOWN(fctx));
2450 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2451 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES) {
2452 want_try = ISC_TRUE;
2455 if (fctx->pending == 0) {
2457 * We've got nothing else to wait for and don't
2458 * know the answer. There's nothing to do but
2461 want_done = ISC_TRUE;
2464 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2465 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2467 if (fctx->references == 0) {
2468 bucket_empty = fctx_unlink(fctx);
2469 dodestroy = ISC_TRUE;
2472 UNLOCK(&res->buckets[bucketnum].lock);
2474 isc_event_free(&event);
2475 dns_adb_destroyfind(&find);
2478 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2479 } else if (want_done) {
2480 FCTXTRACE("fetch failed in finddone(); return ISC_R_FAILURE");
2481 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2482 } else if (dodestroy) {
2490 static inline isc_boolean_t
2491 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2494 for (sa = ISC_LIST_HEAD(fctx->bad);
2496 sa = ISC_LIST_NEXT(sa, link)) {
2497 if (isc_sockaddr_equal(sa, address))
2504 static inline isc_boolean_t
2505 mark_bad(fetchctx_t *fctx) {
2506 dns_adbfind_t *curr;
2507 dns_adbaddrinfo_t *addrinfo;
2508 isc_boolean_t all_bad = ISC_TRUE;
2511 * Mark all known bad servers, so we don't try to talk to them
2516 * Mark any bad nameservers.
2518 for (curr = ISC_LIST_HEAD(fctx->finds);
2520 curr = ISC_LIST_NEXT(curr, publink)) {
2521 for (addrinfo = ISC_LIST_HEAD(curr->list);
2523 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2524 if (bad_server(fctx, &addrinfo->sockaddr))
2525 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2527 all_bad = ISC_FALSE;
2532 * Mark any bad forwarders.
2534 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2536 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2537 if (bad_server(fctx, &addrinfo->sockaddr))
2538 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2540 all_bad = ISC_FALSE;
2544 * Mark any bad alternates.
2546 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2548 curr = ISC_LIST_NEXT(curr, publink)) {
2549 for (addrinfo = ISC_LIST_HEAD(curr->list);
2551 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2552 if (bad_server(fctx, &addrinfo->sockaddr))
2553 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2555 all_bad = ISC_FALSE;
2559 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2561 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2562 if (bad_server(fctx, &addrinfo->sockaddr))
2563 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2565 all_bad = ISC_FALSE;
2572 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2573 badnstype_t badtype)
2575 char namebuf[DNS_NAME_FORMATSIZE];
2576 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2582 const char *spc = "";
2583 isc_sockaddr_t *address = &addrinfo->sockaddr;
2585 if (reason == DNS_R_LAME)
2589 case badns_unreachable:
2592 case badns_response:
2595 case badns_validation:
2596 break; /* counted as 'valfail' */
2600 if (bad_server(fctx, address)) {
2602 * We already know this server is bad.
2607 FCTXTRACE("add_bad");
2609 sa = isc_mem_get(fctx->mctx, sizeof(*sa));
2613 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2615 if (reason == DNS_R_LAME) /* already logged */
2618 if (reason == DNS_R_UNEXPECTEDRCODE &&
2619 fctx->rmessage->rcode == dns_rcode_servfail &&
2620 ISFORWARDER(addrinfo))
2623 if (reason == DNS_R_UNEXPECTEDRCODE) {
2624 isc_buffer_init(&b, code, sizeof(code) - 1);
2625 dns_rcode_totext(fctx->rmessage->rcode, &b);
2626 code[isc_buffer_usedlength(&b)] = '\0';
2628 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2629 isc_buffer_init(&b, code, sizeof(code) - 1);
2630 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2631 code[isc_buffer_usedlength(&b)] = '\0';
2636 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2637 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2638 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2639 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2640 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2641 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2642 "error (%s%s%s) resolving '%s/%s/%s': %s",
2643 dns_result_totext(reason), spc, code,
2644 namebuf, typebuf, classbuf, addrbuf);
2648 * Sort addrinfo list by RTT.
2651 sort_adbfind(dns_adbfind_t *find) {
2652 dns_adbaddrinfo_t *best, *curr;
2653 dns_adbaddrinfolist_t sorted;
2655 /* Lame N^2 bubble sort. */
2656 ISC_LIST_INIT(sorted);
2657 while (!ISC_LIST_EMPTY(find->list)) {
2658 best = ISC_LIST_HEAD(find->list);
2659 curr = ISC_LIST_NEXT(best, publink);
2660 while (curr != NULL) {
2661 if (curr->srtt < best->srtt)
2663 curr = ISC_LIST_NEXT(curr, publink);
2665 ISC_LIST_UNLINK(find->list, best, publink);
2666 ISC_LIST_APPEND(sorted, best, publink);
2668 find->list = sorted;
2672 * Sort a list of finds by server RTT.
2675 sort_finds(dns_adbfindlist_t *findlist) {
2676 dns_adbfind_t *best, *curr;
2677 dns_adbfindlist_t sorted;
2678 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2680 /* Sort each find's addrinfo list by SRTT. */
2681 for (curr = ISC_LIST_HEAD(*findlist);
2683 curr = ISC_LIST_NEXT(curr, publink))
2686 /* Lame N^2 bubble sort. */
2687 ISC_LIST_INIT(sorted);
2688 while (!ISC_LIST_EMPTY(*findlist)) {
2689 best = ISC_LIST_HEAD(*findlist);
2690 bestaddrinfo = ISC_LIST_HEAD(best->list);
2691 INSIST(bestaddrinfo != NULL);
2692 curr = ISC_LIST_NEXT(best, publink);
2693 while (curr != NULL) {
2694 addrinfo = ISC_LIST_HEAD(curr->list);
2695 INSIST(addrinfo != NULL);
2696 if (addrinfo->srtt < bestaddrinfo->srtt) {
2698 bestaddrinfo = addrinfo;
2700 curr = ISC_LIST_NEXT(curr, publink);
2702 ISC_LIST_UNLINK(*findlist, best, publink);
2703 ISC_LIST_APPEND(sorted, best, publink);
2709 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2710 unsigned int options, unsigned int flags, isc_stdtime_t now,
2711 isc_boolean_t *overquota, isc_boolean_t *need_alternate)
2713 dns_adbaddrinfo_t *ai;
2714 dns_adbfind_t *find;
2715 dns_resolver_t *res;
2716 isc_boolean_t unshared;
2717 isc_result_t result;
2719 #ifndef ENABLE_FETCHLIMIT
2721 #endif /* !ENABLE_FETCHLIMIT */
2724 unshared = ISC_TF((fctx->options & DNS_FETCHOPT_UNSHARED) != 0);
2726 * If this name is a subdomain of the query domain, tell
2727 * the ADB to start looking using zone/hint data. This keeps us
2728 * from getting stuck if the nameserver is beneath the zone cut
2729 * and we don't know its address (e.g. because the A record has
2732 if (dns_name_issubdomain(name, &fctx->domain))
2733 options |= DNS_ADBFIND_STARTATZONE;
2734 options |= DNS_ADBFIND_GLUEOK;
2735 options |= DNS_ADBFIND_HINTOK;
2738 * See what we know about this address.
2741 result = dns_adb_createfind2(fctx->adb,
2742 res->buckets[fctx->bucketnum].task,
2743 fctx_finddone, fctx, name,
2744 &fctx->name, fctx->type,
2747 fctx->depth + 1, fctx->qc, &find);
2748 if (result != ISC_R_SUCCESS) {
2749 if (result == DNS_R_ALIAS) {
2750 char namebuf[DNS_NAME_FORMATSIZE];
2753 * XXXRTH Follow the CNAME/DNAME chain?
2755 dns_adb_destroyfind(&find);
2757 dns_name_format(name, namebuf, sizeof(namebuf));
2758 isc_log_write(dns_lctx, DNS_LOGCATEGORY_CNAME,
2759 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2760 "skipping nameserver '%s' because it "
2761 "is a CNAME, while resolving '%s'",
2762 namebuf, fctx->info);
2764 } else if (!ISC_LIST_EMPTY(find->list)) {
2766 * We have at least some of the addresses for the
2769 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2770 if (flags != 0 || port != 0) {
2771 for (ai = ISC_LIST_HEAD(find->list);
2773 ai = ISC_LIST_NEXT(ai, publink)) {
2776 isc_sockaddr_setport(&ai->sockaddr,
2780 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2781 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2783 ISC_LIST_APPEND(fctx->finds, find, publink);
2786 * We don't know any of the addresses for this
2789 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2791 * We're looking for them and will get an
2792 * event about it later.
2798 if (need_alternate != NULL &&
2799 !*need_alternate && unshared &&
2800 ((res->dispatches4 == NULL &&
2801 find->result_v6 != DNS_R_NXDOMAIN) ||
2802 (res->dispatches6 == NULL &&
2803 find->result_v4 != DNS_R_NXDOMAIN)))
2804 *need_alternate = ISC_TRUE;
2806 #ifdef ENABLE_FETCHLIMIT
2807 if ((find->options & DNS_ADBFIND_OVERQUOTA) != 0) {
2808 if (overquota != NULL)
2809 *overquota = ISC_TRUE;
2810 fctx->quotacount++; /* quota exceeded */
2813 #endif /* ENABLE_FETCHLIMIT */
2814 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2815 fctx->lamecount++; /* cached lame server */
2817 fctx->adberr++; /* unreachable server, etc. */
2820 * If we know there are no addresses for
2821 * the family we are using then try to add
2822 * an alternative server.
2824 if (need_alternate != NULL && !*need_alternate &&
2825 ((res->dispatches4 == NULL &&
2826 find->result_v6 == DNS_R_NXRRSET) ||
2827 (res->dispatches6 == NULL &&
2828 find->result_v4 == DNS_R_NXRRSET)))
2829 *need_alternate = ISC_TRUE;
2830 dns_adb_destroyfind(&find);
2835 static isc_boolean_t
2836 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2838 unsigned int nlabels;
2839 dns_namereln_t namereln;
2841 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2842 return (ISC_TF(namereln == dns_namereln_subdomain));
2846 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2847 dns_rdata_t rdata = DNS_RDATA_INIT;
2848 isc_result_t result;
2849 dns_resolver_t *res;
2851 unsigned int stdoptions = 0;
2853 dns_adbaddrinfo_t *ai;
2854 isc_boolean_t all_bad;
2856 isc_boolean_t need_alternate = ISC_FALSE;
2857 #ifdef ENABLE_FETCHLIMIT
2858 isc_boolean_t all_spilled = ISC_TRUE;
2859 #endif /* ENABLE_FETCHLIMIT */
2861 FCTXTRACE("getaddresses");
2864 * Don't pound on remote servers. (Failsafe!)
2867 if (fctx->restarts > 10) {
2868 FCTXTRACE("too many restarts");
2869 return (DNS_R_SERVFAIL);
2874 if (fctx->depth > res->maxdepth) {
2875 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
2876 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3),
2877 "too much NS indirection resolving '%s'",
2879 return (DNS_R_SERVFAIL);
2886 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2887 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2890 * If this fctx has forwarders, use them; otherwise use any
2891 * selective forwarders specified in the view; otherwise use the
2892 * resolver's forwarders (if any).
2894 sa = ISC_LIST_HEAD(fctx->forwarders);
2896 dns_forwarders_t *forwarders = NULL;
2897 dns_name_t *name = &fctx->name;
2899 unsigned int labels;
2900 dns_fixedname_t fixed;
2904 * DS records are found in the parent server.
2905 * Strip label to get the correct forwarder (if any).
2907 if (dns_rdatatype_atparent(fctx->type) &&
2908 dns_name_countlabels(name) > 1) {
2909 dns_name_init(&suffix, NULL);
2910 labels = dns_name_countlabels(name);
2911 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2915 dns_fixedname_init(&fixed);
2916 domain = dns_fixedname_name(&fixed);
2917 result = dns_fwdtable_find2(res->view->fwdtable, name,
2918 domain, &forwarders);
2919 if (result == ISC_R_SUCCESS) {
2920 sa = ISC_LIST_HEAD(forwarders->addrs);
2921 fctx->fwdpolicy = forwarders->fwdpolicy;
2922 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2923 isstrictsubdomain(domain, &fctx->domain)) {
2924 #ifdef ENABLE_FETCHLIMIT
2926 #endif /* ENABLE_FETCHLIMIT */
2927 dns_name_free(&fctx->domain, fctx->mctx);
2928 dns_name_init(&fctx->domain, NULL);
2929 result = dns_name_dup(domain, fctx->mctx,
2931 if (result != ISC_R_SUCCESS)
2933 #ifdef ENABLE_FETCHLIMIT
2934 result = fcount_incr(fctx, ISC_TRUE);
2935 if (result != ISC_R_SUCCESS)
2937 #endif /* ENABLE_FETCHLIMIT */
2942 while (sa != NULL) {
2943 if ((isc_sockaddr_pf(sa) == AF_INET &&
2944 fctx->res->dispatches4 == NULL) ||
2945 (isc_sockaddr_pf(sa) == AF_INET6 &&
2946 fctx->res->dispatches6 == NULL)) {
2947 sa = ISC_LIST_NEXT(sa, link);
2951 result = dns_adb_findaddrinfo(fctx->adb,
2952 sa, &ai, 0); /* XXXMLG */
2953 if (result == ISC_R_SUCCESS) {
2954 dns_adbaddrinfo_t *cur;
2955 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2956 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2957 while (cur != NULL && cur->srtt < ai->srtt)
2958 cur = ISC_LIST_NEXT(cur, publink);
2960 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2963 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2965 sa = ISC_LIST_NEXT(sa, link);
2969 * If the forwarding policy is "only", we don't need the addresses
2970 * of the nameservers.
2972 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2976 * Normal nameservers.
2979 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2980 if (fctx->restarts == 1) {
2982 * To avoid sending out a flood of queries likely to
2983 * result in NXRRSET, we suppress fetches for address
2984 * families we don't have the first time through,
2985 * provided that we have addresses in some family we
2988 * We don't want to set this option all the time, since
2989 * if fctx->restarts > 1, we've clearly been having trouble
2990 * with the addresses we had, so getting more could help.
2992 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2994 if (res->dispatches4 != NULL)
2995 stdoptions |= DNS_ADBFIND_INET;
2996 if (res->dispatches6 != NULL)
2997 stdoptions |= DNS_ADBFIND_INET6;
2999 if ((stdoptions & DNS_ADBFIND_ADDRESSMASK) == 0)
3000 return (DNS_R_SERVFAIL);
3002 isc_stdtime_get(&now);
3004 INSIST(ISC_LIST_EMPTY(fctx->finds));
3005 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
3007 for (result = dns_rdataset_first(&fctx->nameservers);
3008 result == ISC_R_SUCCESS;
3009 result = dns_rdataset_next(&fctx->nameservers))
3011 isc_boolean_t overquota = ISC_FALSE;
3013 dns_rdataset_current(&fctx->nameservers, &rdata);
3015 * Extract the name from the NS record.
3017 result = dns_rdata_tostruct(&rdata, &ns, NULL);
3018 if (result != ISC_R_SUCCESS)
3021 findname(fctx, &ns.name, 0, stdoptions, 0, now,
3022 &overquota, &need_alternate);
3024 #ifdef ENABLE_FETCHLIMIT
3026 all_spilled = ISC_FALSE;
3027 #endif /* ENABLE_FETCHLIMIT */
3029 dns_rdata_reset(&rdata);
3030 dns_rdata_freestruct(&ns);
3032 if (result != ISC_R_NOMORE)
3036 * Do we need to use 6 to 4?
3038 if (need_alternate) {
3041 family = (res->dispatches6 != NULL) ? AF_INET6 : AF_INET;
3042 for (a = ISC_LIST_HEAD(res->alternates);
3044 a = ISC_LIST_NEXT(a, link)) {
3045 if (!a->isaddress) {
3046 findname(fctx, &a->_u._n.name, a->_u._n.port,
3047 stdoptions, FCTX_ADDRINFO_FORWARDER,
3051 if (isc_sockaddr_pf(&a->_u.addr) != family)
3054 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
3056 if (result == ISC_R_SUCCESS) {
3057 dns_adbaddrinfo_t *cur;
3058 ai->flags |= FCTX_ADDRINFO_FORWARDER;
3059 cur = ISC_LIST_HEAD(fctx->altaddrs);
3060 while (cur != NULL && cur->srtt < ai->srtt)
3061 cur = ISC_LIST_NEXT(cur, publink);
3063 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
3066 ISC_LIST_APPEND(fctx->altaddrs, ai,
3074 * Mark all known bad servers.
3076 all_bad = mark_bad(fctx);
3083 * We've got no addresses.
3085 if (fctx->pending > 0) {
3087 * We're fetching the addresses, but don't have any
3088 * yet. Tell the caller to wait for an answer.
3090 result = DNS_R_WAIT;
3095 * We've lost completely. We don't know any
3096 * addresses, and the ADB has told us it can't get
3099 FCTXTRACE("no addresses");
3100 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
3101 result = isc_time_nowplusinterval(&expire, &i);
3103 (fctx->type == dns_rdatatype_dnskey ||
3104 fctx->type == dns_rdatatype_dlv ||
3105 fctx->type == dns_rdatatype_ds) &&
3106 result == ISC_R_SUCCESS)
3107 dns_resolver_addbadcache(res, &fctx->name,
3108 fctx->type, &expire);
3110 #ifdef ENABLE_FETCHLIMIT
3112 * If all of the addresses found were over the
3113 * fetches-per-server quota, return the configured
3117 result = res->quotaresp[dns_quotatype_server];
3118 inc_stats(res, dns_resstatscounter_serverquota);
3120 result = ISC_R_FAILURE;
3121 #endif /* ENABLE_FETCHLIMIT */
3125 * We've found some addresses. We might still be looking
3126 * for more addresses.
3128 sort_finds(&fctx->finds);
3129 sort_finds(&fctx->altfinds);
3130 result = ISC_R_SUCCESS;
3137 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
3140 char buf[ISC_NETADDR_FORMATSIZE];
3142 isc_boolean_t aborted = ISC_FALSE;
3143 isc_boolean_t bogus;
3144 dns_acl_t *blackhole;
3145 isc_netaddr_t ipaddr;
3146 dns_peer_t *peer = NULL;
3147 dns_resolver_t *res;
3148 const char *msg = NULL;
3150 sa = &addr->sockaddr;
3153 isc_netaddr_fromsockaddr(&ipaddr, sa);
3154 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
3155 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
3157 if (blackhole != NULL) {
3160 if (dns_acl_match(&ipaddr, NULL, blackhole,
3162 &match, NULL) == ISC_R_SUCCESS &&
3168 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
3173 addr->flags |= FCTX_ADDRINFO_MARK;
3174 msg = "ignoring blackholed / bogus server: ";
3175 } else if (isc_sockaddr_ismulticast(sa)) {
3176 addr->flags |= FCTX_ADDRINFO_MARK;
3177 msg = "ignoring multicast address: ";
3178 } else if (isc_sockaddr_isexperimental(sa)) {
3179 addr->flags |= FCTX_ADDRINFO_MARK;
3180 msg = "ignoring experimental address: ";
3181 } else if (sa->type.sa.sa_family != AF_INET6) {
3183 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
3184 addr->flags |= FCTX_ADDRINFO_MARK;
3185 msg = "ignoring IPv6 mapped IPV4 address: ";
3186 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
3187 addr->flags |= FCTX_ADDRINFO_MARK;
3188 msg = "ignoring IPv6 compatibility IPV4 address: ";
3192 if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) {
3193 isc_netaddr_fromsockaddr(&na, sa);
3194 isc_netaddr_format(&na, buf, sizeof(buf));
3195 FCTXTRACE2(msg, buf);
3199 static inline dns_adbaddrinfo_t *
3200 fctx_nextaddress(fetchctx_t *fctx) {
3201 dns_adbfind_t *find, *start;
3202 dns_adbaddrinfo_t *addrinfo;
3203 dns_adbaddrinfo_t *faddrinfo;
3206 * Return the next untried address, if any.
3210 * Find the first unmarked forwarder (if any).
3212 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
3214 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3215 if (!UNMARKED(addrinfo))
3217 possibly_mark(fctx, addrinfo);
3218 if (UNMARKED(addrinfo)) {
3219 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3226 * No forwarders. Move to the next find.
3229 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
3233 find = ISC_LIST_HEAD(fctx->finds);
3235 find = ISC_LIST_NEXT(find, publink);
3237 find = ISC_LIST_HEAD(fctx->finds);
3241 * Find the first unmarked addrinfo.
3247 for (addrinfo = ISC_LIST_HEAD(find->list);
3249 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3250 if (!UNMARKED(addrinfo))
3252 possibly_mark(fctx, addrinfo);
3253 if (UNMARKED(addrinfo)) {
3254 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3258 if (addrinfo != NULL)
3260 find = ISC_LIST_NEXT(find, publink);
3262 find = ISC_LIST_HEAD(fctx->finds);
3263 } while (find != start);
3267 if (addrinfo != NULL)
3271 * No nameservers left. Try alternates.
3274 fctx->attributes |= FCTX_ATTR_TRIEDALT;
3276 find = fctx->altfind;
3278 find = ISC_LIST_HEAD(fctx->altfinds);
3280 find = ISC_LIST_NEXT(find, publink);
3282 find = ISC_LIST_HEAD(fctx->altfinds);
3286 * Find the first unmarked addrinfo.
3292 for (addrinfo = ISC_LIST_HEAD(find->list);
3294 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3295 if (!UNMARKED(addrinfo))
3297 possibly_mark(fctx, addrinfo);
3298 if (UNMARKED(addrinfo)) {
3299 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3303 if (addrinfo != NULL)
3305 find = ISC_LIST_NEXT(find, publink);
3307 find = ISC_LIST_HEAD(fctx->altfinds);
3308 } while (find != start);
3311 faddrinfo = addrinfo;
3314 * See if we have a better alternate server by address.
3317 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
3319 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3320 if (!UNMARKED(addrinfo))
3322 possibly_mark(fctx, addrinfo);
3323 if (UNMARKED(addrinfo) &&
3324 (faddrinfo == NULL ||
3325 addrinfo->srtt < faddrinfo->srtt)) {
3326 if (faddrinfo != NULL)
3327 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3328 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3333 if (addrinfo == NULL) {
3334 addrinfo = faddrinfo;
3335 fctx->altfind = find;
3342 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3343 isc_result_t result;
3344 dns_adbaddrinfo_t *addrinfo = NULL;
3345 dns_resolver_t *res;
3346 unsigned int bucketnum;
3347 isc_boolean_t bucket_empty;
3351 REQUIRE(!ADDRWAIT(fctx));
3355 /* We've already exceeded maximum query count */
3356 if (isc_counter_used(fctx->qc) > res->maxqueries) {
3357 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3358 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3),
3359 "exceeded max queries resolving '%s'",
3361 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3365 #ifdef ENABLE_FETCHLIMIT
3366 /* Try to find an address that isn't over quota */
3367 while ((addrinfo = fctx_nextaddress(fctx)) != NULL)
3368 if (! dns_adbentry_overquota(addrinfo->entry))
3370 #endif /* ENABLE_FETCHLIMIT */
3372 if (addrinfo == NULL) {
3373 /* We have no more addresses. Start over. */
3374 fctx_cancelqueries(fctx, ISC_TRUE);
3375 fctx_cleanupfinds(fctx);
3376 fctx_cleanupaltfinds(fctx);
3377 fctx_cleanupforwaddrs(fctx);
3378 fctx_cleanupaltaddrs(fctx);
3379 result = fctx_getaddresses(fctx, badcache);
3380 if (result == DNS_R_WAIT) {
3382 * Sleep waiting for addresses.
3384 FCTXTRACE("addrwait");
3385 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3387 } else if (result != ISC_R_SUCCESS) {
3389 * Something bad happened.
3391 fctx_done(fctx, result, __LINE__);
3395 #ifdef ENABLE_FETCHLIMIT
3396 while ((addrinfo = fctx_nextaddress(fctx)) != NULL) {
3397 if (! dns_adbentry_overquota(addrinfo->entry))
3401 addrinfo = fctx_nextaddress(fctx);
3402 #endif /* !ENABLE_FETCHLIMIT */
3405 * While we may have addresses from the ADB, they
3406 * might be bad ones. In this case, return SERVFAIL.
3408 if (addrinfo == NULL) {
3409 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3414 if (dns_name_countlabels(&fctx->domain) > 2) {
3415 result = isc_counter_increment(fctx->qc);
3416 if (result != ISC_R_SUCCESS) {
3417 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3418 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(3),
3419 "exceeded max queries resolving '%s'",
3421 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3426 bucketnum = fctx->bucketnum;
3427 fctx_increference(fctx);
3428 result = fctx_query(fctx, addrinfo, fctx->options);
3429 if (result != ISC_R_SUCCESS) {
3430 fctx_done(fctx, result, __LINE__);
3431 LOCK(&res->buckets[bucketnum].lock);
3432 bucket_empty = fctx_decreference(fctx);
3433 UNLOCK(&res->buckets[bucketnum].lock);
3436 } else if (retrying)
3437 inc_stats(res, dns_resstatscounter_retry);
3440 static isc_boolean_t
3441 fctx_unlink(fetchctx_t *fctx) {
3442 dns_resolver_t *res;
3443 unsigned int bucketnum;
3446 * Caller must be holding the bucket lock.
3449 REQUIRE(VALID_FCTX(fctx));
3450 REQUIRE(fctx->state == fetchstate_done ||
3451 fctx->state == fetchstate_init);
3452 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3453 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3454 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3455 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3456 REQUIRE(fctx->pending == 0);
3457 REQUIRE(fctx->references == 0);
3458 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3460 FCTXTRACE("unlink");
3463 bucketnum = fctx->bucketnum;
3465 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3469 UNLOCK(&res->nlock);
3471 if (res->buckets[bucketnum].exiting &&
3472 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3479 fctx_destroy(fetchctx_t *fctx) {
3480 isc_sockaddr_t *sa, *next_sa;
3482 REQUIRE(VALID_FCTX(fctx));
3483 REQUIRE(fctx->state == fetchstate_done ||
3484 fctx->state == fetchstate_init);
3485 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3486 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3487 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3488 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3489 REQUIRE(fctx->pending == 0);
3490 REQUIRE(fctx->references == 0);
3491 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3492 REQUIRE(!ISC_LINK_LINKED(fctx, link));
3494 FCTXTRACE("destroy");
3499 for (sa = ISC_LIST_HEAD(fctx->bad);
3502 next_sa = ISC_LIST_NEXT(sa, link);
3503 ISC_LIST_UNLINK(fctx->bad, sa, link);
3504 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3507 for (sa = ISC_LIST_HEAD(fctx->edns);
3510 next_sa = ISC_LIST_NEXT(sa, link);
3511 ISC_LIST_UNLINK(fctx->edns, sa, link);
3512 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3515 for (sa = ISC_LIST_HEAD(fctx->edns512);
3518 next_sa = ISC_LIST_NEXT(sa, link);
3519 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3520 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3523 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3526 next_sa = ISC_LIST_NEXT(sa, link);
3527 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3528 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3531 isc_counter_detach(&fctx->qc);
3533 #ifdef ENABLE_FETCHLIMIT
3535 #endif /* ENABLE_FETCHLIMIT */
3537 isc_timer_detach(&fctx->timer);
3538 dns_message_destroy(&fctx->rmessage);
3539 dns_message_destroy(&fctx->qmessage);
3540 if (dns_name_countlabels(&fctx->domain) > 0)
3541 dns_name_free(&fctx->domain, fctx->mctx);
3542 if (dns_rdataset_isassociated(&fctx->nameservers))
3543 dns_rdataset_disassociate(&fctx->nameservers);
3544 dns_name_free(&fctx->name, fctx->mctx);
3545 dns_db_detach(&fctx->cache);
3546 dns_adb_detach(&fctx->adb);
3547 isc_mem_free(fctx->mctx, fctx->info);
3548 isc_mem_putanddetach(&fctx->mctx, fctx, sizeof(*fctx));
3552 * Fetch event handlers.
3556 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3557 fetchctx_t *fctx = event->ev_arg;
3558 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3561 REQUIRE(VALID_FCTX(fctx));
3565 FCTXTRACE("timeout");
3567 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3569 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3570 fctx->reason = NULL;
3571 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3573 isc_result_t result;
3576 fctx->timeout = ISC_TRUE;
3579 * We could cancel the running queries here, or we could let
3580 * them keep going. Since we normally use separate sockets for
3581 * different queries, we adopt the former approach to reduce
3582 * the number of open sockets: cancel the oldest query if it
3583 * expired after the query had started (this is usually the
3584 * case but is not always so, depending on the task schedule
3587 query = ISC_LIST_HEAD(fctx->queries);
3588 if (query != NULL &&
3589 isc_time_compare(&tevent->due, &query->start) >= 0)
3591 FCTXTRACE("query timed out; no response");
3592 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3594 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3597 * Our timer has triggered. Reestablish the fctx lifetime
3600 result = fctx_starttimer(fctx);
3601 if (result != ISC_R_SUCCESS)
3602 fctx_done(fctx, result, __LINE__);
3607 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3610 isc_event_free(&event);
3614 fctx_shutdown(fetchctx_t *fctx) {
3615 isc_event_t *cevent;
3618 * Start the shutdown process for fctx, if it isn't already underway.
3621 FCTXTRACE("shutdown");
3624 * The caller must be holding the appropriate bucket lock.
3627 if (fctx->want_shutdown)
3630 fctx->want_shutdown = ISC_TRUE;
3633 * Unless we're still initializing (in which case the
3634 * control event is still outstanding), we need to post
3635 * the control event to tell the fetch we want it to
3638 if (fctx->state != fetchstate_init) {
3639 cevent = &fctx->control_event;
3640 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3646 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3647 fetchctx_t *fctx = event->ev_arg;
3648 isc_boolean_t bucket_empty = ISC_FALSE;
3649 dns_resolver_t *res;
3650 unsigned int bucketnum;
3651 dns_validator_t *validator;
3652 isc_boolean_t dodestroy = ISC_FALSE;
3654 REQUIRE(VALID_FCTX(fctx));
3659 bucketnum = fctx->bucketnum;
3661 FCTXTRACE("doshutdown");
3664 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3666 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3669 * Cancel all pending validators. Note that this must be done
3670 * without the bucket lock held, since that could cause deadlock.
3672 validator = ISC_LIST_HEAD(fctx->validators);
3673 while (validator != NULL) {
3674 dns_validator_cancel(validator);
3675 validator = ISC_LIST_NEXT(validator, link);
3678 if (fctx->nsfetch != NULL)
3679 dns_resolver_cancelfetch(fctx->nsfetch);
3682 * Shut down anything that is still running on behalf of this
3683 * fetch. To avoid deadlock with the ADB, we must do this
3684 * before we lock the bucket lock.
3686 fctx_stopeverything(fctx, ISC_FALSE);
3688 LOCK(&res->buckets[bucketnum].lock);
3690 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3692 INSIST(fctx->state == fetchstate_active ||
3693 fctx->state == fetchstate_done);
3694 INSIST(fctx->want_shutdown);
3696 if (fctx->state != fetchstate_done) {
3697 fctx->state = fetchstate_done;
3698 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3701 if (fctx->references == 0 && fctx->pending == 0 &&
3702 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3703 bucket_empty = fctx_unlink(fctx);
3704 dodestroy = ISC_TRUE;
3707 UNLOCK(&res->buckets[bucketnum].lock);
3717 fctx_start(isc_task_t *task, isc_event_t *event) {
3718 fetchctx_t *fctx = event->ev_arg;
3719 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3720 dns_resolver_t *res;
3721 unsigned int bucketnum;
3722 isc_boolean_t dodestroy = ISC_FALSE;
3724 REQUIRE(VALID_FCTX(fctx));
3729 bucketnum = fctx->bucketnum;
3733 LOCK(&res->buckets[bucketnum].lock);
3735 INSIST(fctx->state == fetchstate_init);
3736 if (fctx->want_shutdown) {
3738 * We haven't started this fctx yet, and we've been requested
3741 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3742 fctx->state = fetchstate_done;
3743 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3745 * Since we haven't started, we INSIST that we have no
3746 * pending ADB finds and no pending validations.
3748 INSIST(fctx->pending == 0);
3749 INSIST(fctx->nqueries == 0);
3750 INSIST(ISC_LIST_EMPTY(fctx->validators));
3751 if (fctx->references == 0) {
3753 * It's now safe to destroy this fctx.
3755 bucket_empty = fctx_unlink(fctx);
3756 dodestroy = ISC_TRUE;
3761 * Normal fctx startup.
3763 fctx->state = fetchstate_active;
3765 * Reset the control event for later use in shutting down
3768 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3769 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3773 UNLOCK(&res->buckets[bucketnum].lock);
3776 isc_result_t result;
3781 * All is well. Start working on the fetch.
3783 result = fctx_starttimer(fctx);
3784 if (result != ISC_R_SUCCESS)
3785 fctx_done(fctx, result, __LINE__);
3787 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3788 } else if (dodestroy) {
3796 * Fetch Creation, Joining, and Cancelation.
3799 static inline isc_result_t
3800 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3801 dns_messageid_t id, isc_taskaction_t action, void *arg,
3802 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3806 dns_fetchevent_t *event;
3811 * We store the task we're going to send this event to in the
3812 * sender field. We'll make the fetch the sender when we actually
3816 isc_task_attach(task, &clone);
3817 event = (dns_fetchevent_t *)
3818 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3819 action, arg, sizeof(*event));
3820 if (event == NULL) {
3821 isc_task_detach(&clone);
3822 return (ISC_R_NOMEMORY);
3824 event->result = DNS_R_SERVFAIL;
3825 event->qtype = fctx->type;
3828 event->rdataset = rdataset;
3829 event->sigrdataset = sigrdataset;
3830 event->fetch = fetch;
3831 event->client = client;
3833 dns_fixedname_init(&event->foundname);
3836 * Make sure that we can store the sigrdataset in the
3837 * first event if it is needed by any of the events.
3839 if (event->sigrdataset != NULL)
3840 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3842 ISC_LIST_APPEND(fctx->events, event, ev_link);
3844 fctx->client = client;
3846 fetch->magic = DNS_FETCH_MAGIC;
3847 fetch->private = fctx;
3849 return (ISC_R_SUCCESS);
3853 log_ns_ttl(fetchctx_t *fctx, const char *where) {
3854 char namebuf[DNS_NAME_FORMATSIZE];
3855 char domainbuf[DNS_NAME_FORMATSIZE];
3857 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3858 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3859 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3860 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
3861 "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u",
3862 fctx, where, namebuf, domainbuf,
3863 fctx->ns_ttl_ok, fctx->ns_ttl);
3867 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3868 dns_name_t *domain, dns_rdataset_t *nameservers,
3869 unsigned int options, unsigned int bucketnum, unsigned int depth,
3870 isc_counter_t *qc, fetchctx_t **fctxp)
3873 isc_result_t result;
3874 isc_result_t iresult;
3875 isc_interval_t interval;
3876 dns_fixedname_t fixed;
3877 unsigned int findoptions = 0;
3878 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3879 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3883 * Caller must be holding the lock for bucket number 'bucketnum'.
3885 REQUIRE(fctxp != NULL && *fctxp == NULL);
3887 mctx = res->buckets[bucketnum].mctx;
3888 fctx = isc_mem_get(mctx, sizeof(*fctx));
3890 return (ISC_R_NOMEMORY);
3894 isc_counter_attach(qc, &fctx->qc);
3896 result = isc_counter_create(res->mctx,
3897 res->maxqueries, &fctx->qc);
3898 if (result != ISC_R_SUCCESS)
3903 * Make fctx->info point to a copy of a formatted string
3906 dns_name_format(name, buf, sizeof(buf));
3907 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3908 strcat(buf, "/"); /* checked */
3909 strcat(buf, typebuf); /* checked */
3910 fctx->info = isc_mem_strdup(mctx, buf);
3911 if (fctx->info == NULL) {
3912 result = ISC_R_NOMEMORY;
3913 goto cleanup_counter;
3916 FCTXTRACE("create");
3917 dns_name_init(&fctx->name, NULL);
3918 result = dns_name_dup(name, mctx, &fctx->name);
3919 if (result != ISC_R_SUCCESS)
3921 dns_name_init(&fctx->domain, NULL);
3922 dns_rdataset_init(&fctx->nameservers);
3925 fctx->options = options;
3927 * Note! We do not attach to the task. We are relying on the
3928 * resolver to ensure that this task doesn't go away while we are
3932 fctx->references = 0;
3933 fctx->bucketnum = bucketnum;
3934 fctx->dbucketnum = RES_NOBUCKET;
3935 fctx->state = fetchstate_init;
3936 fctx->want_shutdown = ISC_FALSE;
3937 fctx->cloned = ISC_FALSE;
3938 fctx->depth = depth;
3939 ISC_LIST_INIT(fctx->queries);
3940 ISC_LIST_INIT(fctx->finds);
3941 ISC_LIST_INIT(fctx->altfinds);
3942 ISC_LIST_INIT(fctx->forwaddrs);
3943 ISC_LIST_INIT(fctx->altaddrs);
3944 ISC_LIST_INIT(fctx->forwarders);
3945 fctx->fwdpolicy = dns_fwdpolicy_none;
3946 ISC_LIST_INIT(fctx->bad);
3947 ISC_LIST_INIT(fctx->edns);
3948 ISC_LIST_INIT(fctx->edns512);
3949 ISC_LIST_INIT(fctx->bad_edns);
3950 ISC_LIST_INIT(fctx->validators);
3951 fctx->validator = NULL;
3953 fctx->altfind = NULL;
3956 fctx->querysent = 0;
3957 fctx->referrals = 0;
3958 TIME_NOW(&fctx->start);
3960 fctx->lamecount = 0;
3961 fctx->quotacount = 0;
3967 fctx->result = ISC_R_FAILURE;
3968 fctx->vresult = ISC_R_SUCCESS;
3969 fctx->exitline = -1; /* sentinel */
3970 fctx->logged = ISC_FALSE;
3971 fctx->attributes = 0;
3972 fctx->spilled = ISC_FALSE;
3974 fctx->reason = NULL;
3976 fctx->rand_bits = 0;
3977 fctx->timeout = ISC_FALSE;
3978 fctx->addrinfo = NULL;
3979 fctx->client = NULL;
3981 fctx->ns_ttl_ok = ISC_FALSE;
3983 dns_name_init(&fctx->nsname, NULL);
3984 fctx->nsfetch = NULL;
3985 dns_rdataset_init(&fctx->nsrrset);
3987 if (domain == NULL) {
3988 dns_forwarders_t *forwarders = NULL;
3989 unsigned int labels;
3990 dns_name_t *fwdname = name;
3994 * DS records are found in the parent server. Strip one
3995 * leading label from the name (to be used in finding
3998 if (dns_rdatatype_atparent(fctx->type) &&
3999 dns_name_countlabels(name) > 1) {
4000 dns_name_init(&suffix, NULL);
4001 labels = dns_name_countlabels(name);
4002 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
4006 /* Find the forwarder for this name. */
4007 dns_fixedname_init(&fixed);
4008 domain = dns_fixedname_name(&fixed);
4009 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
4010 domain, &forwarders);
4011 if (result == ISC_R_SUCCESS)
4012 fctx->fwdpolicy = forwarders->fwdpolicy;
4014 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
4016 * The caller didn't supply a query domain and
4017 * nameservers, and we're not in forward-only mode,
4018 * so find the best nameservers to use.
4020 if (dns_rdatatype_atparent(fctx->type))
4021 findoptions |= DNS_DBFIND_NOEXACT;
4022 result = dns_view_findzonecut(res->view, name,
4023 domain, 0, findoptions,
4027 if (result != ISC_R_SUCCESS)
4030 result = dns_name_dup(domain, mctx, &fctx->domain);
4031 if (result != ISC_R_SUCCESS) {
4032 dns_rdataset_disassociate(&fctx->nameservers);
4035 fctx->ns_ttl = fctx->nameservers.ttl;
4036 fctx->ns_ttl_ok = ISC_TRUE;
4039 * We're in forward-only mode. Set the query domain.
4041 result = dns_name_dup(domain, mctx, &fctx->domain);
4042 if (result != ISC_R_SUCCESS)
4046 result = dns_name_dup(domain, mctx, &fctx->domain);
4047 if (result != ISC_R_SUCCESS)
4049 dns_rdataset_clone(nameservers, &fctx->nameservers);
4050 fctx->ns_ttl = fctx->nameservers.ttl;
4051 fctx->ns_ttl_ok = ISC_TRUE;
4054 #ifdef ENABLE_FETCHLIMIT
4056 * Are there too many simultaneous queries for this domain?
4058 result = fcount_incr(fctx, ISC_FALSE);
4059 if (result != ISC_R_SUCCESS) {
4060 result = fctx->res->quotaresp[dns_quotatype_zone];
4061 inc_stats(res, dns_resstatscounter_zonequota);
4062 goto cleanup_domain;
4064 #endif /* ENABLE_FETCHLIMIT */
4066 log_ns_ttl(fctx, "fctx_create");
4068 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
4070 fctx->qmessage = NULL;
4071 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
4074 if (result != ISC_R_SUCCESS)
4075 #ifdef ENABLE_FETCHLIMIT
4076 goto cleanup_fcount;
4078 goto cleanup_domain;
4079 #endif /* !ENABLE_FETCHLIMIT */
4081 fctx->rmessage = NULL;
4082 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
4085 if (result != ISC_R_SUCCESS)
4086 goto cleanup_qmessage;
4089 * Compute an expiration time for the entire fetch.
4091 isc_interval_set(&interval, res->query_timeout, 0);
4092 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
4093 if (iresult != ISC_R_SUCCESS) {
4094 UNEXPECTED_ERROR(__FILE__, __LINE__,
4095 "isc_time_nowplusinterval: %s",
4096 isc_result_totext(iresult));
4097 result = ISC_R_UNEXPECTED;
4098 goto cleanup_rmessage;
4102 * Default retry interval initialization. We set the interval now
4103 * mostly so it won't be uninitialized. It will be set to the
4104 * correct value before a query is issued.
4106 isc_interval_set(&fctx->interval, 2, 0);
4109 * Create an inactive timer. It will be made active when the fetch
4110 * is actually started.
4113 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
4115 res->buckets[bucketnum].task, fctx_timeout,
4116 fctx, &fctx->timer);
4117 if (iresult != ISC_R_SUCCESS) {
4118 UNEXPECTED_ERROR(__FILE__, __LINE__,
4119 "isc_timer_create: %s",
4120 isc_result_totext(iresult));
4121 result = ISC_R_UNEXPECTED;
4122 goto cleanup_rmessage;
4126 * Attach to the view's cache and adb.
4129 dns_db_attach(res->view->cachedb, &fctx->cache);
4131 dns_adb_attach(res->view->adb, &fctx->adb);
4133 isc_mem_attach(mctx, &fctx->mctx);
4135 ISC_LIST_INIT(fctx->events);
4136 ISC_LINK_INIT(fctx, link);
4137 fctx->magic = FCTX_MAGIC;
4139 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
4143 UNLOCK(&res->nlock);
4147 return (ISC_R_SUCCESS);
4150 dns_message_destroy(&fctx->rmessage);
4153 dns_message_destroy(&fctx->qmessage);
4155 #ifdef ENABLE_FETCHLIMIT
4158 #endif /* ENABLE_FETCHLIMIT */
4161 if (dns_name_countlabels(&fctx->domain) > 0)
4162 dns_name_free(&fctx->domain, mctx);
4163 if (dns_rdataset_isassociated(&fctx->nameservers))
4164 dns_rdataset_disassociate(&fctx->nameservers);
4167 dns_name_free(&fctx->name, mctx);
4170 isc_mem_free(mctx, fctx->info);
4173 isc_counter_detach(&fctx->qc);
4176 isc_mem_put(mctx, fctx, sizeof(*fctx));
4184 static inline isc_boolean_t
4185 is_lame(fetchctx_t *fctx) {
4186 dns_message_t *message = fctx->rmessage;
4188 dns_rdataset_t *rdataset;
4189 isc_result_t result;
4191 if (message->rcode != dns_rcode_noerror &&
4192 message->rcode != dns_rcode_nxdomain)
4195 if (message->counts[DNS_SECTION_ANSWER] != 0)
4198 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
4201 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
4202 while (result == ISC_R_SUCCESS) {
4204 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
4205 for (rdataset = ISC_LIST_HEAD(name->list);
4207 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4208 dns_namereln_t namereln;
4210 unsigned int labels;
4211 if (rdataset->type != dns_rdatatype_ns)
4213 namereln = dns_name_fullcompare(name, &fctx->domain,
4215 if (namereln == dns_namereln_equal &&
4216 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
4218 if (namereln == dns_namereln_subdomain)
4222 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
4229 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
4230 char namebuf[DNS_NAME_FORMATSIZE];
4231 char domainbuf[DNS_NAME_FORMATSIZE];
4232 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
4234 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
4235 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
4236 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
4237 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
4238 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
4239 "lame server resolving '%s' (in '%s'?): %s",
4240 namebuf, domainbuf, addrbuf);
4244 log_formerr(fetchctx_t *fctx, const char *format, ...) {
4245 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
4246 char clbuf[ISC_SOCKADDR_FORMATSIZE];
4247 const char *clmsg = "";
4251 va_start(args, format);
4252 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
4255 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
4257 if (fctx->client != NULL) {
4258 clmsg = " for client ";
4259 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
4264 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4265 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4266 "DNS format error from %s resolving %s%s%s: %s",
4267 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
4270 static inline isc_result_t
4271 same_question(fetchctx_t *fctx) {
4272 isc_result_t result;
4273 dns_message_t *message = fctx->rmessage;
4275 dns_rdataset_t *rdataset;
4278 * Caller must be holding the fctx lock.
4282 * XXXRTH Currently we support only one question.
4284 if (message->counts[DNS_SECTION_QUESTION] != 1) {
4285 log_formerr(fctx, "too many questions");
4286 return (DNS_R_FORMERR);
4289 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
4290 if (result != ISC_R_SUCCESS)
4293 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
4294 rdataset = ISC_LIST_HEAD(name->list);
4295 INSIST(rdataset != NULL);
4296 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
4298 if (fctx->type != rdataset->type ||
4299 fctx->res->rdclass != rdataset->rdclass ||
4300 !dns_name_equal(&fctx->name, name)) {
4301 char namebuf[DNS_NAME_FORMATSIZE];
4302 char class[DNS_RDATACLASS_FORMATSIZE];
4303 char type[DNS_RDATATYPE_FORMATSIZE];
4305 dns_name_format(name, namebuf, sizeof(namebuf));
4306 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
4307 dns_rdatatype_format(rdataset->type, type, sizeof(type));
4308 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
4309 namebuf, class, type);
4310 return (DNS_R_FORMERR);
4313 return (ISC_R_SUCCESS);
4317 clone_results(fetchctx_t *fctx) {
4318 dns_fetchevent_t *event, *hevent;
4319 isc_result_t result;
4320 dns_name_t *name, *hname;
4322 FCTXTRACE("clone_results");
4325 * Set up any other events to have the same data as the first
4328 * Caller must be holding the appropriate lock.
4331 fctx->cloned = ISC_TRUE;
4332 hevent = ISC_LIST_HEAD(fctx->events);
4335 hname = dns_fixedname_name(&hevent->foundname);
4336 for (event = ISC_LIST_NEXT(hevent, ev_link);
4338 event = ISC_LIST_NEXT(event, ev_link)) {
4339 name = dns_fixedname_name(&event->foundname);
4340 result = dns_name_copy(hname, name, NULL);
4341 if (result != ISC_R_SUCCESS)
4342 event->result = result;
4344 event->result = hevent->result;
4345 dns_db_attach(hevent->db, &event->db);
4346 dns_db_attachnode(hevent->db, hevent->node, &event->node);
4347 INSIST(hevent->rdataset != NULL);
4348 INSIST(event->rdataset != NULL);
4349 if (dns_rdataset_isassociated(hevent->rdataset))
4350 dns_rdataset_clone(hevent->rdataset, event->rdataset);
4351 INSIST(! (hevent->sigrdataset == NULL &&
4352 event->sigrdataset != NULL));
4353 if (hevent->sigrdataset != NULL &&
4354 dns_rdataset_isassociated(hevent->sigrdataset) &&
4355 event->sigrdataset != NULL)
4356 dns_rdataset_clone(hevent->sigrdataset,
4357 event->sigrdataset);
4361 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
4362 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
4363 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
4364 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
4365 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
4366 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
4367 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
4371 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
4372 * no references and is no longer waiting for any events).
4375 * '*fctx' is shutting down.
4378 * true if the resolver is exiting and this is the last fctx in the bucket.
4380 static isc_boolean_t
4381 maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) {
4382 unsigned int bucketnum;
4383 isc_boolean_t bucket_empty = ISC_FALSE;
4384 dns_resolver_t *res = fctx->res;
4385 dns_validator_t *validator, *next_validator;
4386 isc_boolean_t dodestroy = ISC_FALSE;
4388 REQUIRE(SHUTTINGDOWN(fctx));
4390 bucketnum = fctx->bucketnum;
4392 LOCK(&res->buckets[bucketnum].lock);
4393 if (fctx->pending != 0 || fctx->nqueries != 0)
4396 for (validator = ISC_LIST_HEAD(fctx->validators);
4397 validator != NULL; validator = next_validator) {
4398 next_validator = ISC_LIST_NEXT(validator, link);
4399 dns_validator_cancel(validator);
4402 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators)) {
4403 bucket_empty = fctx_unlink(fctx);
4404 dodestroy = ISC_TRUE;
4408 UNLOCK(&res->buckets[bucketnum].lock);
4411 return (bucket_empty);
4415 * The validator has finished.
4418 validated(isc_task_t *task, isc_event_t *event) {
4419 dns_adbaddrinfo_t *addrinfo;
4420 dns_dbnode_t *node = NULL;
4421 dns_dbnode_t *nsnode = NULL;
4422 dns_fetchevent_t *hevent;
4424 dns_rdataset_t *ardataset = NULL;
4425 dns_rdataset_t *asigrdataset = NULL;
4426 dns_rdataset_t *rdataset;
4427 dns_rdataset_t *sigrdataset;
4428 dns_resolver_t *res;
4429 dns_valarg_t *valarg;
4430 dns_validatorevent_t *vevent;
4432 isc_boolean_t chaining;
4433 isc_boolean_t negative;
4434 isc_boolean_t sentresponse;
4435 isc_result_t eresult = ISC_R_SUCCESS;
4436 isc_result_t result = ISC_R_SUCCESS;
4439 isc_uint32_t bucketnum;
4441 UNUSED(task); /* for now */
4443 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
4444 valarg = event->ev_arg;
4445 fctx = valarg->fctx;
4447 addrinfo = valarg->addrinfo;
4448 REQUIRE(VALID_FCTX(fctx));
4449 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
4451 vevent = (dns_validatorevent_t *)event;
4452 fctx->vresult = vevent->result;
4454 FCTXTRACE("received validation completion event");
4456 bucketnum = fctx->bucketnum;
4457 LOCK(&res->buckets[bucketnum].lock);
4459 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
4460 fctx->validator = NULL;
4463 * Destroy the validator early so that we can
4464 * destroy the fctx if necessary.
4466 dns_validator_destroy(&vevent->validator);
4467 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
4469 negative = ISC_TF(vevent->rdataset == NULL);
4471 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
4474 * If shutting down, ignore the results. Check to see if we're
4475 * done waiting for validator completions and ADB pending events; if
4476 * so, destroy the fctx.
4478 if (SHUTTINGDOWN(fctx) && !sentresponse) {
4479 isc_boolean_t bucket_empty;
4480 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4481 UNLOCK(&res->buckets[bucketnum].lock);
4487 isc_stdtime_get(&now);
4490 * If chaining, we need to make sure that the right result code is
4491 * returned, and that the rdatasets are bound.
4493 if (vevent->result == ISC_R_SUCCESS &&
4495 vevent->rdataset != NULL &&
4496 CHAINING(vevent->rdataset))
4498 if (vevent->rdataset->type == dns_rdatatype_cname)
4499 eresult = DNS_R_CNAME;
4501 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4502 eresult = DNS_R_DNAME;
4504 chaining = ISC_TRUE;
4506 chaining = ISC_FALSE;
4509 * Either we're not shutting down, or we are shutting down but want
4510 * to cache the result anyway (if this was a validation started by
4511 * a query with cd set)
4514 hevent = ISC_LIST_HEAD(fctx->events);
4515 if (hevent != NULL) {
4516 if (!negative && !chaining &&
4517 (fctx->type == dns_rdatatype_any ||
4518 fctx->type == dns_rdatatype_rrsig ||
4519 fctx->type == dns_rdatatype_sig)) {
4521 * Don't bind rdatasets; the caller
4522 * will iterate the node.
4525 ardataset = hevent->rdataset;
4526 asigrdataset = hevent->sigrdataset;
4530 if (vevent->result != ISC_R_SUCCESS) {
4531 FCTXTRACE("validation failed");
4532 inc_stats(res, dns_resstatscounter_valfail);
4534 fctx->vresult = vevent->result;
4535 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4536 result = ISC_R_NOTFOUND;
4537 if (vevent->rdataset != NULL)
4538 result = dns_db_findnode(fctx->cache,
4541 if (result == ISC_R_SUCCESS)
4542 (void)dns_db_deleterdataset(fctx->cache, node,
4545 if (result == ISC_R_SUCCESS &&
4546 vevent->sigrdataset != NULL)
4547 (void)dns_db_deleterdataset(fctx->cache, node,
4549 dns_rdatatype_rrsig,
4551 if (result == ISC_R_SUCCESS)
4552 dns_db_detachnode(fctx->cache, &node);
4554 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4556 * Cache the data as pending for later validation.
4558 result = ISC_R_NOTFOUND;
4559 if (vevent->rdataset != NULL)
4560 result = dns_db_findnode(fctx->cache,
4563 if (result == ISC_R_SUCCESS) {
4564 (void)dns_db_addrdataset(fctx->cache, node,
4566 vevent->rdataset, 0,
4569 if (result == ISC_R_SUCCESS &&
4570 vevent->sigrdataset != NULL)
4571 (void)dns_db_addrdataset(fctx->cache, node,
4573 vevent->sigrdataset,
4575 if (result == ISC_R_SUCCESS)
4576 dns_db_detachnode(fctx->cache, &node);
4578 result = fctx->vresult;
4579 add_bad(fctx, addrinfo, result, badns_validation);
4580 isc_event_free(&event);
4581 UNLOCK(&res->buckets[bucketnum].lock);
4582 INSIST(fctx->validator == NULL);
4583 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4584 if (fctx->validator != NULL)
4585 dns_validator_send(fctx->validator);
4586 else if (sentresponse)
4587 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4588 else if (result == DNS_R_BROKENCHAIN) {
4589 isc_result_t tresult;
4593 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4594 tresult = isc_time_nowplusinterval(&expire, &i);
4596 (fctx->type == dns_rdatatype_dnskey ||
4597 fctx->type == dns_rdatatype_dlv ||
4598 fctx->type == dns_rdatatype_ds) &&
4599 tresult == ISC_R_SUCCESS)
4600 dns_resolver_addbadcache(res, &fctx->name,
4601 fctx->type, &expire);
4602 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4604 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4610 dns_rdatatype_t covers;
4611 FCTXTRACE("nonexistence validation OK");
4613 inc_stats(res, dns_resstatscounter_valnegsuccess);
4616 * Cache DS NXDOMAIN seperately to other types.
4618 if (fctx->rmessage->rcode == dns_rcode_nxdomain &&
4619 fctx->type != dns_rdatatype_ds)
4620 covers = dns_rdatatype_any;
4622 covers = fctx->type;
4624 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4626 if (result != ISC_R_SUCCESS)
4627 goto noanswer_response;
4630 * If we are asking for a SOA record set the cache time
4631 * to zero to facilitate locating the containing zone of
4634 ttl = res->view->maxncachettl;
4635 if (fctx->type == dns_rdatatype_soa &&
4636 covers == dns_rdatatype_any && res->zero_no_soa_ttl)
4639 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4640 covers, now, ttl, vevent->optout,
4641 vevent->secure, ardataset, &eresult);
4642 if (result != ISC_R_SUCCESS)
4643 goto noanswer_response;
4644 goto answer_response;
4646 inc_stats(res, dns_resstatscounter_valsuccess);
4648 FCTXTRACE("validation OK");
4650 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4651 result = dns_rdataset_addnoqname(vevent->rdataset,
4652 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4653 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4654 INSIST(vevent->sigrdataset != NULL);
4655 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4656 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4657 result = dns_rdataset_addclosest(vevent->rdataset,
4658 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4659 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4661 } else if (vevent->rdataset->trust == dns_trust_answer &&
4662 vevent->rdataset->type != dns_rdatatype_rrsig)
4664 isc_result_t tresult;
4665 dns_name_t *noqname = NULL;
4666 tresult = findnoqname(fctx, vevent->name,
4667 vevent->rdataset->type, &noqname);
4668 if (tresult == ISC_R_SUCCESS && noqname != NULL) {
4669 tresult = dns_rdataset_addnoqname(vevent->rdataset,
4671 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4676 * The data was already cached as pending data.
4677 * Re-cache it as secure and bind the cached
4678 * rdatasets to the first event on the fetch
4681 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4682 if (result != ISC_R_SUCCESS)
4683 goto noanswer_response;
4685 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4686 vevent->rdataset, 0, ardataset);
4687 if (result != ISC_R_SUCCESS &&
4688 result != DNS_R_UNCHANGED)
4689 goto noanswer_response;
4690 if (ardataset != NULL && NEGATIVE(ardataset)) {
4691 if (NXDOMAIN(ardataset))
4692 eresult = DNS_R_NCACHENXDOMAIN;
4694 eresult = DNS_R_NCACHENXRRSET;
4695 } else if (vevent->sigrdataset != NULL) {
4696 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4697 vevent->sigrdataset, 0,
4699 if (result != ISC_R_SUCCESS &&
4700 result != DNS_R_UNCHANGED)
4701 goto noanswer_response;
4705 isc_boolean_t bucket_empty = ISC_FALSE;
4707 * If we only deferred the destroy because we wanted to cache
4708 * the data, destroy now.
4710 dns_db_detachnode(fctx->cache, &node);
4711 if (SHUTTINGDOWN(fctx))
4712 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4713 UNLOCK(&res->buckets[bucketnum].lock);
4719 if (!ISC_LIST_EMPTY(fctx->validators)) {
4721 INSIST(fctx->type == dns_rdatatype_any ||
4722 fctx->type == dns_rdatatype_rrsig ||
4723 fctx->type == dns_rdatatype_sig);
4725 * Don't send a response yet - we have
4726 * more rdatasets that still need to
4729 dns_db_detachnode(fctx->cache, &node);
4730 UNLOCK(&res->buckets[bucketnum].lock);
4731 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4737 * Cache any NS/NSEC records that happened to be validated.
4739 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4740 while (result == ISC_R_SUCCESS) {
4742 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4744 for (rdataset = ISC_LIST_HEAD(name->list);
4746 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4747 if ((rdataset->type != dns_rdatatype_ns &&
4748 rdataset->type != dns_rdatatype_nsec) ||
4749 rdataset->trust != dns_trust_secure)
4751 for (sigrdataset = ISC_LIST_HEAD(name->list);
4752 sigrdataset != NULL;
4753 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4754 if (sigrdataset->type != dns_rdatatype_rrsig ||
4755 sigrdataset->covers != rdataset->type)
4759 if (sigrdataset == NULL ||
4760 sigrdataset->trust != dns_trust_secure)
4762 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4764 if (result != ISC_R_SUCCESS)
4767 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4768 now, rdataset, 0, NULL);
4769 if (result == ISC_R_SUCCESS)
4770 result = dns_db_addrdataset(fctx->cache, nsnode,
4774 dns_db_detachnode(fctx->cache, &nsnode);
4775 if (result != ISC_R_SUCCESS)
4778 result = dns_message_nextname(fctx->rmessage,
4779 DNS_SECTION_AUTHORITY);
4782 result = ISC_R_SUCCESS;
4785 * Respond with an answer, positive or negative,
4786 * as opposed to an error. 'node' must be non-NULL.
4789 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4791 if (hevent != NULL) {
4793 * Negative results must be indicated in event->result.
4795 if (dns_rdataset_isassociated(hevent->rdataset) &&
4796 NEGATIVE(hevent->rdataset)) {
4797 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4798 eresult == DNS_R_NCACHENXRRSET);
4800 hevent->result = eresult;
4801 RUNTIME_CHECK(dns_name_copy(vevent->name,
4802 dns_fixedname_name(&hevent->foundname), NULL)
4804 dns_db_attach(fctx->cache, &hevent->db);
4805 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4806 clone_results(fctx);
4811 dns_db_detachnode(fctx->cache, &node);
4813 UNLOCK(&res->buckets[bucketnum].lock);
4814 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4817 INSIST(node == NULL);
4818 isc_event_free(&event);
4822 fctx_log(void *arg, int level, const char *fmt, ...) {
4825 fetchctx_t *fctx = arg;
4827 va_start(args, fmt);
4828 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
4831 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4832 DNS_LOGMODULE_RESOLVER, level,
4833 "fctx %p(%s): %s", fctx, fctx->info, msgbuf);
4836 static inline isc_result_t
4837 findnoqname(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
4838 dns_name_t **noqnamep)
4840 dns_rdataset_t *nrdataset, *next, *sigrdataset;
4841 dns_rdata_rrsig_t rrsig;
4842 isc_result_t result;
4843 unsigned int labels;
4844 dns_section_t section;
4845 dns_name_t *zonename;
4846 dns_fixedname_t fzonename;
4847 dns_name_t *closest;
4848 dns_fixedname_t fclosest;
4849 dns_name_t *nearest;
4850 dns_fixedname_t fnearest;
4851 dns_rdatatype_t found = dns_rdatatype_none;
4852 dns_name_t *noqname = NULL;
4854 FCTXTRACE("findnoqname");
4856 REQUIRE(noqnamep != NULL && *noqnamep == NULL);
4859 * Find the SIG for this rdataset, if we have it.
4861 for (sigrdataset = ISC_LIST_HEAD(name->list);
4862 sigrdataset != NULL;
4863 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4864 if (sigrdataset->type == dns_rdatatype_rrsig &&
4865 sigrdataset->covers == type)
4869 if (sigrdataset == NULL)
4870 return (ISC_R_NOTFOUND);
4872 labels = dns_name_countlabels(name);
4874 for (result = dns_rdataset_first(sigrdataset);
4875 result == ISC_R_SUCCESS;
4876 result = dns_rdataset_next(sigrdataset)) {
4877 dns_rdata_t rdata = DNS_RDATA_INIT;
4878 dns_rdataset_current(sigrdataset, &rdata);
4879 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
4880 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4881 /* Wildcard has rrsig.labels < labels - 1. */
4882 if (rrsig.labels + 1U >= labels)
4887 if (result == ISC_R_NOMORE)
4888 return (ISC_R_NOTFOUND);
4889 if (result != ISC_R_SUCCESS)
4892 dns_fixedname_init(&fzonename);
4893 zonename = dns_fixedname_name(&fzonename);
4894 dns_fixedname_init(&fclosest);
4895 closest = dns_fixedname_name(&fclosest);
4896 dns_fixedname_init(&fnearest);
4897 nearest = dns_fixedname_name(&fnearest);
4899 #define NXND(x) ((x) == ISC_R_SUCCESS)
4901 section = DNS_SECTION_AUTHORITY;
4902 for (result = dns_message_firstname(fctx->rmessage, section);
4903 result == ISC_R_SUCCESS;
4904 result = dns_message_nextname(fctx->rmessage, section)) {
4905 dns_name_t *nsec = NULL;
4906 dns_message_currentname(fctx->rmessage, section, &nsec);
4907 for (nrdataset = ISC_LIST_HEAD(nsec->list);
4908 nrdataset != NULL; nrdataset = next) {
4909 isc_boolean_t data = ISC_FALSE, exists = ISC_FALSE;
4910 isc_boolean_t optout = ISC_FALSE, unknown = ISC_FALSE;
4911 isc_boolean_t setclosest = ISC_FALSE;
4912 isc_boolean_t setnearest = ISC_FALSE;
4914 next = ISC_LIST_NEXT(nrdataset, link);
4915 if (nrdataset->type != dns_rdatatype_nsec &&
4916 nrdataset->type != dns_rdatatype_nsec3)
4919 if (nrdataset->type == dns_rdatatype_nsec &&
4920 NXND(dns_nsec_noexistnodata(type, name, nsec,
4922 &data, NULL, fctx_log,
4927 found = dns_rdatatype_nsec;
4931 if (nrdataset->type == dns_rdatatype_nsec3 &&
4932 NXND(dns_nsec3_noexistnodata(type, name, nsec,
4933 nrdataset, zonename,
4941 if (!exists && setnearest) {
4943 found = dns_rdatatype_nsec3;
4948 if (result == ISC_R_NOMORE)
4949 result = ISC_R_SUCCESS;
4950 if (noqname != NULL) {
4951 for (sigrdataset = ISC_LIST_HEAD(noqname->list);
4952 sigrdataset != NULL;
4953 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4954 if (sigrdataset->type == dns_rdatatype_rrsig &&
4955 sigrdataset->covers == found)
4958 if (sigrdataset != NULL)
4959 *noqnamep = noqname;
4964 static inline isc_result_t
4965 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4968 dns_rdataset_t *rdataset, *sigrdataset;
4969 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4970 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4971 dns_dbnode_t *node, **anodep;
4974 dns_resolver_t *res;
4975 isc_boolean_t need_validation, secure_domain, have_answer;
4976 isc_result_t result, eresult;
4977 dns_fetchevent_t *event;
4978 unsigned int options;
4981 unsigned int valoptions = 0;
4984 * The appropriate bucket lock must be held.
4988 need_validation = ISC_FALSE;
4989 POST(need_validation);
4990 secure_domain = ISC_FALSE;
4991 have_answer = ISC_FALSE;
4992 eresult = ISC_R_SUCCESS;
4993 task = res->buckets[fctx->bucketnum].task;
4996 * Is DNSSEC validation required for this name?
4998 if (res->view->enablevalidation) {
4999 result = dns_view_issecuredomain(res->view, name,
5001 if (result != ISC_R_SUCCESS)
5004 if (!secure_domain && res->view->dlv != NULL) {
5005 valoptions = DNS_VALIDATOR_DLV;
5006 secure_domain = ISC_TRUE;
5010 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
5011 need_validation = ISC_FALSE;
5013 need_validation = secure_domain;
5019 asigrdataset = NULL;
5021 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
5023 have_answer = ISC_TRUE;
5024 event = ISC_LIST_HEAD(fctx->events);
5025 if (event != NULL) {
5027 aname = dns_fixedname_name(&event->foundname);
5028 result = dns_name_copy(name, aname, NULL);
5029 if (result != ISC_R_SUCCESS)
5031 anodep = &event->node;
5033 * If this is an ANY, SIG or RRSIG query, we're not
5034 * going to return any rdatasets, unless we encountered
5035 * a CNAME or DNAME as "the answer". In this case,
5036 * we're going to return DNS_R_CNAME or DNS_R_DNAME
5037 * and we must set up the rdatasets.
5039 if ((fctx->type != dns_rdatatype_any &&
5040 fctx->type != dns_rdatatype_rrsig &&
5041 fctx->type != dns_rdatatype_sig) ||
5042 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
5043 ardataset = event->rdataset;
5044 asigrdataset = event->sigrdataset;
5050 * Find or create the cache node.
5053 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
5054 if (result != ISC_R_SUCCESS)
5058 * Cache or validate each cacheable rdataset.
5060 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
5061 for (rdataset = ISC_LIST_HEAD(name->list);
5063 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5064 if (!CACHE(rdataset))
5066 if (CHECKNAMES(rdataset)) {
5067 char namebuf[DNS_NAME_FORMATSIZE];
5068 char typebuf[DNS_RDATATYPE_FORMATSIZE];
5069 char classbuf[DNS_RDATATYPE_FORMATSIZE];
5071 dns_name_format(name, namebuf, sizeof(namebuf));
5072 dns_rdatatype_format(rdataset->type, typebuf,
5074 dns_rdataclass_format(rdataset->rdclass, classbuf,
5076 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5077 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5078 "check-names %s %s/%s/%s",
5079 fail ? "failure" : "warning",
5080 namebuf, typebuf, classbuf);
5082 if (ANSWER(rdataset)) {
5083 dns_db_detachnode(fctx->cache, &node);
5084 return (DNS_R_BADNAME);
5091 * Enforce the configure maximum cache TTL.
5093 if (rdataset->ttl > res->view->maxcachettl)
5094 rdataset->ttl = res->view->maxcachettl;
5097 * Find the SIG for this rdataset, if we have it.
5099 for (sigrdataset = ISC_LIST_HEAD(name->list);
5100 sigrdataset != NULL;
5101 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
5102 if (sigrdataset->type == dns_rdatatype_rrsig &&
5103 sigrdataset->covers == rdataset->type)
5108 * If this RRset is in a secure domain, is in bailiwick,
5109 * and is not glue, attempt DNSSEC validation. (We do not
5110 * attempt to validate glue or out-of-bailiwick data--even
5111 * though there might be some performance benefit to doing
5112 * so--because it makes it simpler and safer to ensure that
5113 * records from a secure domain are only cached if validated
5114 * within the context of a query to the domain that owns
5117 if (secure_domain && rdataset->trust != dns_trust_glue &&
5118 !EXTERNAL(rdataset)) {
5122 * RRSIGs are validated as part of validating the
5125 if (rdataset->type == dns_rdatatype_rrsig)
5128 if (sigrdataset == NULL) {
5129 if (!ANSWER(rdataset) && need_validation) {
5131 * Ignore non-answer rdatasets that
5132 * are missing signatures.
5139 * Normalize the rdataset and sigrdataset TTLs.
5141 if (sigrdataset != NULL) {
5142 rdataset->ttl = ISC_MIN(rdataset->ttl,
5144 sigrdataset->ttl = rdataset->ttl;
5148 * Cache this rdataset/sigrdataset pair as
5149 * pending data. Track whether it was additional
5152 if (rdataset->trust == dns_trust_additional)
5153 trust = dns_trust_pending_additional;
5155 trust = dns_trust_pending_answer;
5157 rdataset->trust = trust;
5158 if (sigrdataset != NULL)
5159 sigrdataset->trust = trust;
5160 if (!need_validation || !ANSWER(rdataset)) {
5161 if (ANSWER(rdataset) &&
5162 rdataset->type != dns_rdatatype_rrsig) {
5163 isc_result_t tresult;
5164 dns_name_t *noqname = NULL;
5165 tresult = findnoqname(fctx, name,
5168 if (tresult == ISC_R_SUCCESS &&
5171 dns_rdataset_addnoqname(
5173 RUNTIME_CHECK(tresult ==
5177 addedrdataset = ardataset;
5178 result = dns_db_addrdataset(fctx->cache, node,
5179 NULL, now, rdataset,
5181 if (result == DNS_R_UNCHANGED) {
5182 result = ISC_R_SUCCESS;
5183 if (!need_validation &&
5184 ardataset != NULL &&
5185 NEGATIVE(ardataset)) {
5187 * The answer in the cache is
5188 * better than the answer we
5189 * found, and is a negative
5190 * cache entry, so we must set
5191 * eresult appropriately.
5193 if (NXDOMAIN(ardataset))
5195 DNS_R_NCACHENXDOMAIN;
5198 DNS_R_NCACHENXRRSET;
5200 * We have a negative response
5201 * from the cache so don't
5202 * attempt to add the RRSIG
5208 if (result != ISC_R_SUCCESS)
5210 if (sigrdataset != NULL) {
5211 addedrdataset = asigrdataset;
5212 result = dns_db_addrdataset(fctx->cache,
5216 if (result == DNS_R_UNCHANGED)
5217 result = ISC_R_SUCCESS;
5218 if (result != ISC_R_SUCCESS)
5220 } else if (!ANSWER(rdataset))
5224 if (ANSWER(rdataset) && need_validation) {
5225 if (fctx->type != dns_rdatatype_any &&
5226 fctx->type != dns_rdatatype_rrsig &&
5227 fctx->type != dns_rdatatype_sig) {
5229 * This is The Answer. We will
5230 * validate it, but first we cache
5231 * the rest of the response - it may
5232 * contain useful keys.
5234 INSIST(valrdataset == NULL &&
5235 valsigrdataset == NULL);
5236 valrdataset = rdataset;
5237 valsigrdataset = sigrdataset;
5240 * This is one of (potentially)
5241 * multiple answers to an ANY
5242 * or SIG query. To keep things
5243 * simple, we just start the
5244 * validator right away rather
5245 * than caching first and
5246 * having to remember which
5247 * rdatasets needed validation.
5249 result = valcreate(fctx, addrinfo,
5250 name, rdataset->type,
5255 * Defer any further validations.
5256 * This prevents multiple validators
5257 * from manipulating fctx->rmessage
5260 valoptions |= DNS_VALIDATOR_DEFER;
5262 } else if (CHAINING(rdataset)) {
5263 if (rdataset->type == dns_rdatatype_cname)
5264 eresult = DNS_R_CNAME;
5266 INSIST(rdataset->type ==
5267 dns_rdatatype_dname);
5268 eresult = DNS_R_DNAME;
5271 } else if (!EXTERNAL(rdataset)) {
5273 * It's OK to cache this rdataset now.
5275 if (ANSWER(rdataset))
5276 addedrdataset = ardataset;
5277 else if (ANSWERSIG(rdataset))
5278 addedrdataset = asigrdataset;
5280 addedrdataset = NULL;
5281 if (CHAINING(rdataset)) {
5282 if (rdataset->type == dns_rdatatype_cname)
5283 eresult = DNS_R_CNAME;
5285 INSIST(rdataset->type ==
5286 dns_rdatatype_dname);
5287 eresult = DNS_R_DNAME;
5290 if (rdataset->trust == dns_trust_glue &&
5291 (rdataset->type == dns_rdatatype_ns ||
5292 (rdataset->type == dns_rdatatype_rrsig &&
5293 rdataset->covers == dns_rdatatype_ns))) {
5295 * If the trust level is 'dns_trust_glue'
5296 * then we are adding data from a referral
5297 * we got while executing the search algorithm.
5298 * New referral data always takes precedence
5299 * over the existing cache contents.
5301 options = DNS_DBADD_FORCE;
5305 if (ANSWER(rdataset) &&
5306 rdataset->type != dns_rdatatype_rrsig) {
5307 isc_result_t tresult;
5308 dns_name_t *noqname = NULL;
5309 tresult = findnoqname(fctx, name,
5310 rdataset->type, &noqname);
5311 if (tresult == ISC_R_SUCCESS &&
5313 tresult = dns_rdataset_addnoqname(
5315 RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
5320 * Now we can add the rdataset.
5322 result = dns_db_addrdataset(fctx->cache,
5328 if (result == DNS_R_UNCHANGED) {
5329 if (ANSWER(rdataset) &&
5330 ardataset != NULL &&
5331 NEGATIVE(ardataset)) {
5333 * The answer in the cache is better
5334 * than the answer we found, and is
5335 * a negative cache entry, so we
5336 * must set eresult appropriately.
5338 if (NXDOMAIN(ardataset))
5339 eresult = DNS_R_NCACHENXDOMAIN;
5341 eresult = DNS_R_NCACHENXRRSET;
5343 result = ISC_R_SUCCESS;
5344 } else if (result != ISC_R_SUCCESS)
5349 if (valrdataset != NULL) {
5350 dns_rdatatype_t vtype = fctx->type;
5351 if (CHAINING(valrdataset)) {
5352 if (valrdataset->type == dns_rdatatype_cname)
5353 vtype = dns_rdatatype_cname;
5355 vtype = dns_rdatatype_dname;
5357 result = valcreate(fctx, addrinfo, name, vtype, valrdataset,
5358 valsigrdataset, valoptions, task);
5361 if (result == ISC_R_SUCCESS && have_answer) {
5362 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
5363 if (event != NULL) {
5365 * Negative results must be indicated in event->result.
5367 if (dns_rdataset_isassociated(event->rdataset) &&
5368 NEGATIVE(event->rdataset)) {
5369 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
5370 eresult == DNS_R_NCACHENXRRSET);
5372 event->result = eresult;
5373 dns_db_attach(fctx->cache, adbp);
5374 dns_db_transfernode(fctx->cache, &node, anodep);
5375 clone_results(fctx);
5380 dns_db_detachnode(fctx->cache, &node);
5385 static inline isc_result_t
5386 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
5388 isc_result_t result;
5389 dns_section_t section;
5392 FCTXTRACE("cache_message");
5394 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
5396 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
5398 for (section = DNS_SECTION_ANSWER;
5399 section <= DNS_SECTION_ADDITIONAL;
5401 result = dns_message_firstname(fctx->rmessage, section);
5402 while (result == ISC_R_SUCCESS) {
5404 dns_message_currentname(fctx->rmessage, section,
5406 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
5407 result = cache_name(fctx, name, addrinfo, now);
5408 if (result != ISC_R_SUCCESS)
5411 result = dns_message_nextname(fctx->rmessage, section);
5413 if (result != ISC_R_NOMORE)
5416 if (result == ISC_R_NOMORE)
5417 result = ISC_R_SUCCESS;
5419 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
5425 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
5428 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
5429 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
5430 isc_boolean_t optout, isc_boolean_t secure,
5431 dns_rdataset_t *ardataset, isc_result_t *eresultp)
5433 isc_result_t result;
5434 dns_rdataset_t rdataset;
5436 if (ardataset == NULL) {
5437 dns_rdataset_init(&rdataset);
5438 ardataset = &rdataset;
5441 result = dns_ncache_addoptout(message, cache, node, covers,
5442 now, maxttl, optout, ardataset);
5444 result = dns_ncache_add(message, cache, node, covers, now,
5446 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
5448 * If the cache now contains a negative entry and we
5449 * care about whether it is DNS_R_NCACHENXDOMAIN or
5450 * DNS_R_NCACHENXRRSET then extract it.
5452 if (NEGATIVE(ardataset)) {
5454 * The cache data is a negative cache entry.
5456 if (NXDOMAIN(ardataset))
5457 *eresultp = DNS_R_NCACHENXDOMAIN;
5459 *eresultp = DNS_R_NCACHENXRRSET;
5462 * Either we don't care about the nature of the
5463 * cache rdataset (because no fetch is interested
5464 * in the outcome), or the cache rdataset is not
5465 * a negative cache entry. Whichever case it is,
5466 * we can return success.
5468 * XXXRTH There's a CNAME/DNAME problem here.
5470 *eresultp = ISC_R_SUCCESS;
5472 result = ISC_R_SUCCESS;
5474 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
5475 dns_rdataset_disassociate(ardataset);
5480 static inline isc_result_t
5481 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
5482 dns_rdatatype_t covers, isc_stdtime_t now)
5484 isc_result_t result, eresult;
5486 dns_resolver_t *res;
5488 dns_dbnode_t *node, **anodep;
5489 dns_rdataset_t *ardataset;
5490 isc_boolean_t need_validation, secure_domain;
5492 dns_fetchevent_t *event;
5494 unsigned int valoptions = 0;
5496 FCTXTRACE("ncache_message");
5498 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
5501 need_validation = ISC_FALSE;
5502 POST(need_validation);
5503 secure_domain = ISC_FALSE;
5504 eresult = ISC_R_SUCCESS;
5509 * XXXMPA remove when we follow cnames and adjust the setting
5510 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
5512 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
5515 * Is DNSSEC validation required for this name?
5517 if (fctx->res->view->enablevalidation) {
5518 result = dns_view_issecuredomain(res->view, name,
5520 if (result != ISC_R_SUCCESS)
5523 if (!secure_domain && res->view->dlv != NULL) {
5524 valoptions = DNS_VALIDATOR_DLV;
5525 secure_domain = ISC_TRUE;
5529 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
5530 need_validation = ISC_FALSE;
5532 need_validation = secure_domain;
5534 if (secure_domain) {
5536 * Mark all rdatasets as pending.
5538 dns_rdataset_t *trdataset;
5541 result = dns_message_firstname(fctx->rmessage,
5542 DNS_SECTION_AUTHORITY);
5543 while (result == ISC_R_SUCCESS) {
5545 dns_message_currentname(fctx->rmessage,
5546 DNS_SECTION_AUTHORITY,
5548 for (trdataset = ISC_LIST_HEAD(tname->list);
5550 trdataset = ISC_LIST_NEXT(trdataset, link))
5551 trdataset->trust = dns_trust_pending_answer;
5552 result = dns_message_nextname(fctx->rmessage,
5553 DNS_SECTION_AUTHORITY);
5555 if (result != ISC_R_NOMORE)
5560 if (need_validation) {
5562 * Do negative response validation.
5564 result = valcreate(fctx, addrinfo, name, fctx->type,
5565 NULL, NULL, valoptions,
5566 res->buckets[fctx->bucketnum].task);
5568 * If validation is necessary, return now. Otherwise continue
5569 * to process the message, letting the validation complete
5570 * in its own good time.
5575 LOCK(&res->buckets[fctx->bucketnum].lock);
5581 if (!HAVE_ANSWER(fctx)) {
5582 event = ISC_LIST_HEAD(fctx->events);
5583 if (event != NULL) {
5585 aname = dns_fixedname_name(&event->foundname);
5586 result = dns_name_copy(name, aname, NULL);
5587 if (result != ISC_R_SUCCESS)
5589 anodep = &event->node;
5590 ardataset = event->rdataset;
5595 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
5596 if (result != ISC_R_SUCCESS)
5600 * If we are asking for a SOA record set the cache time
5601 * to zero to facilitate locating the containing zone of
5604 ttl = fctx->res->view->maxncachettl;
5605 if (fctx->type == dns_rdatatype_soa &&
5606 covers == dns_rdatatype_any &&
5607 fctx->res->zero_no_soa_ttl)
5610 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
5611 covers, now, ttl, ISC_FALSE,
5612 ISC_FALSE, ardataset, &eresult);
5613 if (result != ISC_R_SUCCESS)
5616 if (!HAVE_ANSWER(fctx)) {
5617 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
5618 if (event != NULL) {
5619 event->result = eresult;
5620 dns_db_attach(fctx->cache, adbp);
5621 dns_db_transfernode(fctx->cache, &node, anodep);
5622 clone_results(fctx);
5627 UNLOCK(&res->buckets[fctx->bucketnum].lock);
5630 dns_db_detachnode(fctx->cache, &node);
5636 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
5637 isc_boolean_t external, isc_boolean_t gluing)
5639 name->attributes |= DNS_NAMEATTR_CACHE;
5641 rdataset->trust = dns_trust_glue;
5643 * Glue with 0 TTL causes problems. We force the TTL to
5644 * 1 second to prevent this.
5646 if (rdataset->ttl == 0)
5649 rdataset->trust = dns_trust_additional;
5651 * Avoid infinite loops by only marking new rdatasets.
5653 if (!CACHE(rdataset)) {
5654 name->attributes |= DNS_NAMEATTR_CHASE;
5655 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
5657 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
5659 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
5663 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
5664 dns_section_t section)
5666 fetchctx_t *fctx = arg;
5667 isc_result_t result;
5669 dns_rdataset_t *rdataset;
5670 isc_boolean_t external;
5671 dns_rdatatype_t rtype;
5672 isc_boolean_t gluing;
5674 REQUIRE(VALID_FCTX(fctx));
5676 #if CHECK_FOR_GLUE_IN_ANSWER
5677 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
5678 return (ISC_R_SUCCESS);
5687 result = dns_message_findname(fctx->rmessage, section, addname,
5688 dns_rdatatype_any, 0, &name, NULL);
5689 if (result == ISC_R_SUCCESS) {
5690 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5691 if (type == dns_rdatatype_a) {
5692 for (rdataset = ISC_LIST_HEAD(name->list);
5694 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5695 if (rdataset->type == dns_rdatatype_rrsig)
5696 rtype = rdataset->covers;
5698 rtype = rdataset->type;
5699 if (rtype == dns_rdatatype_a ||
5700 rtype == dns_rdatatype_aaaa)
5701 mark_related(name, rdataset, external,
5705 result = dns_message_findtype(name, type, 0,
5707 if (result == ISC_R_SUCCESS) {
5708 mark_related(name, rdataset, external, gluing);
5710 * Do we have its SIG too?
5713 result = dns_message_findtype(name,
5714 dns_rdatatype_rrsig,
5716 if (result == ISC_R_SUCCESS)
5717 mark_related(name, rdataset, external,
5723 return (ISC_R_SUCCESS);
5727 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5728 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5731 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5732 #define CHECK_FOR_GLUE_IN_ANSWER 0
5734 #if CHECK_FOR_GLUE_IN_ANSWER
5736 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5737 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5742 chase_additional(fetchctx_t *fctx) {
5743 isc_boolean_t rescan;
5744 dns_section_t section = DNS_SECTION_ADDITIONAL;
5745 isc_result_t result;
5750 for (result = dns_message_firstname(fctx->rmessage, section);
5751 result == ISC_R_SUCCESS;
5752 result = dns_message_nextname(fctx->rmessage, section)) {
5753 dns_name_t *name = NULL;
5754 dns_rdataset_t *rdataset;
5755 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5757 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5759 name->attributes &= ~DNS_NAMEATTR_CHASE;
5760 for (rdataset = ISC_LIST_HEAD(name->list);
5762 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5763 if (CHASE(rdataset)) {
5764 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5765 (void)dns_rdataset_additionaldata(rdataset,
5776 static inline isc_result_t
5777 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5778 isc_result_t result;
5779 dns_rdata_t rdata = DNS_RDATA_INIT;
5780 dns_rdata_cname_t cname;
5782 result = dns_rdataset_first(rdataset);
5783 if (result != ISC_R_SUCCESS)
5785 dns_rdataset_current(rdataset, &rdata);
5786 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5787 if (result != ISC_R_SUCCESS)
5789 dns_name_init(tname, NULL);
5790 dns_name_clone(&cname.cname, tname);
5791 dns_rdata_freestruct(&cname);
5793 return (ISC_R_SUCCESS);
5796 static inline isc_result_t
5797 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5798 dns_name_t *oname, dns_fixedname_t *fixeddname)
5800 isc_result_t result;
5801 dns_rdata_t rdata = DNS_RDATA_INIT;
5802 unsigned int nlabels;
5804 dns_namereln_t namereln;
5805 dns_rdata_dname_t dname;
5806 dns_fixedname_t prefix;
5809 * Get the target name of the DNAME.
5811 result = dns_rdataset_first(rdataset);
5812 if (result != ISC_R_SUCCESS)
5814 dns_rdataset_current(rdataset, &rdata);
5815 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5816 if (result != ISC_R_SUCCESS)
5820 * Get the prefix of qname.
5822 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5823 if (namereln != dns_namereln_subdomain) {
5824 char qbuf[DNS_NAME_FORMATSIZE];
5825 char obuf[DNS_NAME_FORMATSIZE];
5827 dns_rdata_freestruct(&dname);
5828 dns_name_format(qname, qbuf, sizeof(qbuf));
5829 dns_name_format(oname, obuf, sizeof(obuf));
5830 log_formerr(fctx, "unrelated DNAME in answer: "
5831 "%s is not in %s", qbuf, obuf);
5832 return (DNS_R_FORMERR);
5834 dns_fixedname_init(&prefix);
5835 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5836 dns_fixedname_init(fixeddname);
5837 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5839 dns_fixedname_name(fixeddname), NULL);
5840 dns_rdata_freestruct(&dname);
5844 static isc_boolean_t
5845 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5846 dns_rdataset_t *rdataset)
5848 isc_result_t result;
5849 dns_rdata_t rdata = DNS_RDATA_INIT;
5851 struct in6_addr in6a;
5852 isc_netaddr_t netaddr;
5853 char addrbuf[ISC_NETADDR_FORMATSIZE];
5854 char namebuf[DNS_NAME_FORMATSIZE];
5859 /* By default, we allow any addresses. */
5860 if (view->denyansweracl == NULL)
5864 * If the owner name matches one in the exclusion list, either exactly
5865 * or partially, allow it.
5867 if (view->answeracl_exclude != NULL) {
5868 dns_rbtnode_t *node = NULL;
5870 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5871 &node, NULL, 0, NULL, NULL);
5873 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5878 * Otherwise, search the filter list for a match for each address
5879 * record. If a match is found, the address should be filtered,
5880 * so should the entire answer.
5882 for (result = dns_rdataset_first(rdataset);
5883 result == ISC_R_SUCCESS;
5884 result = dns_rdataset_next(rdataset)) {
5885 dns_rdata_reset(&rdata);
5886 dns_rdataset_current(rdataset, &rdata);
5887 if (rdataset->type == dns_rdatatype_a) {
5888 INSIST(rdata.length == sizeof(ina.s_addr));
5889 memmove(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5890 isc_netaddr_fromin(&netaddr, &ina);
5892 INSIST(rdata.length == sizeof(in6a.s6_addr));
5893 memmove(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5894 isc_netaddr_fromin6(&netaddr, &in6a);
5897 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5898 &view->aclenv, &match, NULL);
5900 if (result == ISC_R_SUCCESS && match > 0) {
5901 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5902 dns_name_format(name, namebuf, sizeof(namebuf));
5903 dns_rdatatype_format(rdataset->type, typebuf,
5905 dns_rdataclass_format(rdataset->rdclass, classbuf,
5907 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5908 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5909 "answer address %s denied for %s/%s/%s",
5910 addrbuf, namebuf, typebuf, classbuf);
5918 static isc_boolean_t
5919 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5920 dns_rdatatype_t type, dns_name_t *tname,
5923 isc_result_t result;
5924 dns_rbtnode_t *node = NULL;
5925 char qnamebuf[DNS_NAME_FORMATSIZE];
5926 char tnamebuf[DNS_NAME_FORMATSIZE];
5930 /* By default, we allow any target name. */
5931 if (view->denyanswernames == NULL)
5935 * If the owner name matches one in the exclusion list, either exactly
5936 * or partially, allow it.
5938 if (view->answernames_exclude != NULL) {
5939 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5940 &node, NULL, 0, NULL, NULL);
5941 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5946 * If the target name is a subdomain of the search domain, allow it.
5948 if (dns_name_issubdomain(tname, domain))
5952 * Otherwise, apply filters.
5954 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5955 NULL, 0, NULL, NULL);
5956 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5957 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5958 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5959 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5960 dns_rdataclass_format(view->rdclass, classbuf,
5962 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5963 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5964 "%s target %s denied for %s/%s",
5965 typebuf, tnamebuf, qnamebuf, classbuf);
5973 trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) {
5974 char ns_namebuf[DNS_NAME_FORMATSIZE];
5975 char namebuf[DNS_NAME_FORMATSIZE];
5976 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5978 if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) {
5979 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf));
5980 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
5981 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf));
5983 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5984 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
5985 "fctx %p: trimming ttl of %s/NS for %s/%s: "
5986 "%u -> %u", fctx, ns_namebuf, namebuf, tbuf,
5987 rdataset->ttl, fctx->ns_ttl);
5988 rdataset->ttl = fctx->ns_ttl;
5993 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5994 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5995 * section for the NS RRset if the query type is NS; if it has
5996 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5997 * section for A and AAAA queries.
5999 #define LOOK_FOR_NS_IN_ANSWER 0x1
6000 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
6003 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
6004 unsigned int look_in_options)
6006 isc_result_t result;
6007 dns_message_t *message;
6008 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name, *save_name;
6009 dns_rdataset_t *rdataset, *ns_rdataset;
6010 isc_boolean_t aa, negative_response;
6011 dns_rdatatype_t type, save_type;
6012 dns_section_t section;
6014 FCTXTRACE("noanswer_response");
6016 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
6017 INSIST(fctx->type == dns_rdatatype_ns);
6018 section = DNS_SECTION_ANSWER;
6020 section = DNS_SECTION_AUTHORITY;
6022 message = fctx->rmessage;
6027 if (oqname == NULL) {
6029 * We have a normal, non-chained negative response or
6032 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
6036 qname = &fctx->name;
6039 * We're being invoked by answer_response() after it has
6040 * followed a CNAME/DNAME chain.
6045 * If the current qname is not a subdomain of the query
6046 * domain, there's no point in looking at the authority
6047 * section without doing DNSSEC validation.
6049 * Until we do that validation, we'll just return success
6052 if (!dns_name_issubdomain(qname, &fctx->domain))
6053 return (ISC_R_SUCCESS);
6057 * We have to figure out if this is a negative response, or a
6062 * Sometimes we can tell if its a negative response by looking at
6063 * the message header.
6065 negative_response = ISC_FALSE;
6066 if (message->rcode == dns_rcode_nxdomain ||
6067 (message->counts[DNS_SECTION_ANSWER] == 0 &&
6068 message->counts[DNS_SECTION_AUTHORITY] == 0))
6069 negative_response = ISC_TRUE;
6072 * Process the authority section.
6079 save_type = dns_rdatatype_none;
6080 result = dns_message_firstname(message, section);
6081 while (result == ISC_R_SUCCESS) {
6083 dns_message_currentname(message, section, &name);
6084 if (dns_name_issubdomain(name, &fctx->domain)) {
6086 * Look for NS/SOA RRsets first.
6088 for (rdataset = ISC_LIST_HEAD(name->list);
6090 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6091 type = rdataset->type;
6092 if (type == dns_rdatatype_rrsig)
6093 type = rdataset->covers;
6094 if (((type == dns_rdatatype_ns ||
6095 type == dns_rdatatype_soa) &&
6096 !dns_name_issubdomain(qname, name))) {
6097 char qbuf[DNS_NAME_FORMATSIZE];
6098 char nbuf[DNS_NAME_FORMATSIZE];
6099 char tbuf[DNS_RDATATYPE_FORMATSIZE];
6100 dns_rdatatype_format(type, tbuf,
6102 dns_name_format(name, nbuf,
6104 dns_name_format(qname, qbuf,
6107 "unrelated %s %s in "
6108 "%s authority section",
6112 if (type == dns_rdatatype_ns) {
6116 * Only one set of NS RRs is allowed.
6118 if (rdataset->type ==
6120 if (ns_name != NULL &&
6127 return (DNS_R_FORMERR);
6130 ns_rdataset = rdataset;
6134 rdataset->attributes |=
6135 DNS_RDATASETATTR_CACHE;
6136 rdataset->trust = dns_trust_glue;
6138 if (type == dns_rdatatype_soa) {
6140 * SOA, or RRSIG SOA.
6142 * Only one SOA is allowed.
6144 if (rdataset->type ==
6145 dns_rdatatype_soa) {
6146 if (soa_name != NULL &&
6153 return (DNS_R_FORMERR);
6158 DNS_NAMEATTR_NCACHE;
6159 rdataset->attributes |=
6160 DNS_RDATASETATTR_NCACHE;
6163 dns_trust_authauthority;
6164 else if (ISFORWARDER(fctx->addrinfo))
6169 dns_trust_additional;
6174 result = dns_message_nextname(message, section);
6175 if (result == ISC_R_NOMORE)
6177 else if (result != ISC_R_SUCCESS)
6181 log_ns_ttl(fctx, "noanswer_response");
6183 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6184 !dns_name_equal(ns_name, dns_rootname))
6185 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6188 * A negative response has a SOA record (Type 2)
6189 * and a optional NS RRset (Type 1) or it has neither
6190 * a SOA or a NS RRset (Type 3, handled above) or
6191 * rcode is NXDOMAIN (handled above) in which case
6192 * the NS RRset is allowed (Type 4).
6194 if (soa_name != NULL)
6195 negative_response = ISC_TRUE;
6197 result = dns_message_firstname(message, section);
6198 while (result == ISC_R_SUCCESS) {
6200 dns_message_currentname(message, section, &name);
6201 if (dns_name_issubdomain(name, &fctx->domain)) {
6202 for (rdataset = ISC_LIST_HEAD(name->list);
6204 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6205 type = rdataset->type;
6206 if (type == dns_rdatatype_rrsig)
6207 type = rdataset->covers;
6208 if (type == dns_rdatatype_nsec ||
6209 type == dns_rdatatype_nsec3) {
6211 * NSEC or RRSIG NSEC.
6213 if (negative_response) {
6215 DNS_NAMEATTR_NCACHE;
6216 rdataset->attributes |=
6217 DNS_RDATASETATTR_NCACHE;
6218 } else if (type == dns_rdatatype_nsec) {
6221 rdataset->attributes |=
6222 DNS_RDATASETATTR_CACHE;
6226 dns_trust_authauthority;
6227 else if (ISFORWARDER(fctx->addrinfo))
6232 dns_trust_additional;
6234 * No additional data needs to be
6237 } else if (type == dns_rdatatype_ds) {
6241 * These should only be here if
6242 * this is a referral, and there
6243 * should only be one DS RRset.
6245 if (ns_name == NULL) {
6249 return (DNS_R_FORMERR);
6251 if (rdataset->type ==
6253 if (ds_name != NULL &&
6260 return (DNS_R_FORMERR);
6266 rdataset->attributes |=
6267 DNS_RDATASETATTR_CACHE;
6270 dns_trust_authauthority;
6271 else if (ISFORWARDER(fctx->addrinfo))
6276 dns_trust_additional;
6281 save_type = ISC_LIST_HEAD(name->list)->type;
6283 result = dns_message_nextname(message, section);
6284 if (result == ISC_R_NOMORE)
6286 else if (result != ISC_R_SUCCESS)
6291 * Trigger lookups for DNS nameservers.
6293 if (negative_response && message->rcode == dns_rcode_noerror &&
6294 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
6295 dns_name_equal(soa_name, qname) &&
6296 !dns_name_equal(qname, dns_rootname))
6297 return (DNS_R_CHASEDSSERVERS);
6300 * Did we find anything?
6302 if (!negative_response && ns_name == NULL) {
6306 if (oqname != NULL) {
6308 * We've already got a partial CNAME/DNAME chain,
6309 * and haven't found else anything useful here, but
6310 * no error has occurred since we have an answer.
6312 return (ISC_R_SUCCESS);
6315 * The responder is insane.
6317 if (save_name == NULL) {
6318 log_formerr(fctx, "invalid response");
6319 return (DNS_R_FORMERR);
6321 if (!dns_name_issubdomain(save_name, &fctx->domain)) {
6322 char nbuf[DNS_NAME_FORMATSIZE];
6323 char dbuf[DNS_NAME_FORMATSIZE];
6324 char tbuf[DNS_RDATATYPE_FORMATSIZE];
6326 dns_rdatatype_format(save_type, tbuf,
6328 dns_name_format(save_name, nbuf, sizeof(nbuf));
6329 dns_name_format(&fctx->domain, dbuf,
6332 log_formerr(fctx, "Name %s (%s) not subdomain"
6333 " of zone %s -- invalid response",
6336 log_formerr(fctx, "invalid response");
6338 return (DNS_R_FORMERR);
6343 * If we found both NS and SOA, they should be the same name.
6345 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
6346 log_formerr(fctx, "NS/SOA mismatch");
6347 return (DNS_R_FORMERR);
6351 * Do we have a referral? (We only want to follow a referral if
6352 * we're not following a chain.)
6354 if (!negative_response && ns_name != NULL && oqname == NULL) {
6356 * We already know ns_name is a subdomain of fctx->domain.
6357 * If ns_name is equal to fctx->domain, we're not making
6358 * progress. We return DNS_R_FORMERR so that we'll keep
6359 * trying other servers.
6361 if (dns_name_equal(ns_name, &fctx->domain)) {
6362 log_formerr(fctx, "non-improving referral");
6363 return (DNS_R_FORMERR);
6367 * If the referral name is not a parent of the query
6368 * name, consider the responder insane.
6370 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
6372 log_formerr(fctx, "referral to non-parent");
6373 FCTXTRACE("referral to non-parent");
6374 return (DNS_R_FORMERR);
6378 * Mark any additional data related to this rdataset.
6379 * It's important that we do this before we change the
6382 INSIST(ns_rdataset != NULL);
6383 fctx->attributes |= FCTX_ATTR_GLUING;
6384 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
6386 #if CHECK_FOR_GLUE_IN_ANSWER
6388 * Look in the answer section for "glue" that is incorrectly
6389 * returned as a answer. This is needed if the server also
6390 * minimizes the response size by not adding records to the
6391 * additional section that are in the answer section or if
6392 * the record gets dropped due to message size constraints.
6394 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
6395 (fctx->type == dns_rdatatype_aaaa ||
6396 fctx->type == dns_rdatatype_a))
6397 (void)dns_rdataset_additionaldata(ns_rdataset,
6398 check_answer, fctx);
6400 fctx->attributes &= ~FCTX_ATTR_GLUING;
6402 * NS rdatasets with 0 TTL cause problems.
6403 * dns_view_findzonecut() will not find them when we
6404 * try to follow the referral, and we'll SERVFAIL
6405 * because the best nameservers are now above QDOMAIN.
6406 * We force the TTL to 1 second to prevent this.
6408 if (ns_rdataset->ttl == 0)
6409 ns_rdataset->ttl = 1;
6411 * Set the current query domain to the referral name.
6413 * XXXRTH We should check if we're in forward-only mode, and
6414 * if so we should bail out.
6416 INSIST(dns_name_countlabels(&fctx->domain) > 0);
6418 #ifdef ENABLE_FETCHLIMIT
6420 #endif /* ENABLE_FETCHLIMIT */
6422 dns_name_free(&fctx->domain, fctx->mctx);
6423 if (dns_rdataset_isassociated(&fctx->nameservers))
6424 dns_rdataset_disassociate(&fctx->nameservers);
6425 dns_name_init(&fctx->domain, NULL);
6426 result = dns_name_dup(ns_name, fctx->mctx, &fctx->domain);
6427 if (result != ISC_R_SUCCESS)
6430 #ifdef ENABLE_FETCHLIMIT
6431 result = fcount_incr(fctx, ISC_TRUE);
6432 if (result != ISC_R_SUCCESS)
6434 #endif /* ENABLE_FETCHLIMIT */
6436 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6437 fctx->ns_ttl_ok = ISC_FALSE;
6438 log_ns_ttl(fctx, "DELEGATION");
6439 return (DNS_R_DELEGATION);
6443 * Since we're not doing a referral, we don't want to cache any
6444 * NS RRs we may have found.
6446 if (ns_name != NULL)
6447 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
6449 if (negative_response && oqname == NULL)
6450 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
6452 return (ISC_R_SUCCESS);
6456 answer_response(fetchctx_t *fctx) {
6457 isc_result_t result;
6458 dns_message_t *message;
6459 dns_name_t *name, *qname, tname, *ns_name;
6460 dns_rdataset_t *rdataset, *ns_rdataset;
6461 isc_boolean_t done, external, chaining, aa, found, want_chaining;
6462 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
6464 dns_rdatatype_t type;
6465 dns_fixedname_t dname, fqname;
6468 FCTXTRACE("answer_response");
6470 message = fctx->rmessage;
6473 * Examine the answer section, marking those rdatasets which are
6474 * part of the answer and should be cached.
6478 found_cname = ISC_FALSE;
6479 found_type = ISC_FALSE;
6480 chaining = ISC_FALSE;
6481 have_answer = ISC_FALSE;
6482 want_chaining = ISC_FALSE;
6483 POST(want_chaining);
6484 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
6488 qname = &fctx->name;
6490 view = fctx->res->view;
6491 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
6492 while (!done && result == ISC_R_SUCCESS) {
6494 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
6495 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6496 if (dns_name_equal(name, qname)) {
6497 wanted_chaining = ISC_FALSE;
6498 for (rdataset = ISC_LIST_HEAD(name->list);
6500 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6502 want_chaining = ISC_FALSE;
6504 if (rdataset->type == dns_rdatatype_nsec3) {
6506 * NSEC3 records are not allowed to
6507 * appear in the answer section.
6509 log_formerr(fctx, "NSEC3 in answer");
6510 return (DNS_R_FORMERR);
6514 * Apply filters, if given, on answers to reject
6515 * a malicious attempt of rebinding.
6517 if ((rdataset->type == dns_rdatatype_a ||
6518 rdataset->type == dns_rdatatype_aaaa) &&
6519 !is_answeraddress_allowed(view, name,
6521 return (DNS_R_SERVFAIL);
6524 if (rdataset->type == type && !found_cname) {
6526 * We've found an ordinary answer.
6529 found_type = ISC_TRUE;
6531 aflag = DNS_RDATASETATTR_ANSWER;
6532 } else if (type == dns_rdatatype_any) {
6534 * We've found an answer matching
6535 * an ANY query. There may be
6539 aflag = DNS_RDATASETATTR_ANSWER;
6540 } else if (rdataset->type == dns_rdatatype_rrsig
6541 && rdataset->covers == type
6544 * We've found a signature that
6545 * covers the type we're looking for.
6548 found_type = ISC_TRUE;
6549 aflag = DNS_RDATASETATTR_ANSWERSIG;
6550 } else if (rdataset->type ==
6554 * We're looking for something else,
6555 * but we found a CNAME.
6557 * Getting a CNAME response for some
6558 * query types is an error, see
6559 * RFC 4035, Section 2.5.
6561 if (type == dns_rdatatype_rrsig ||
6562 type == dns_rdatatype_key ||
6563 type == dns_rdatatype_nsec) {
6564 char buf[DNS_RDATATYPE_FORMATSIZE];
6565 dns_rdatatype_format(fctx->type,
6570 return (DNS_R_FORMERR);
6573 found_cname = ISC_TRUE;
6574 want_chaining = ISC_TRUE;
6575 aflag = DNS_RDATASETATTR_ANSWER;
6576 result = cname_target(rdataset,
6578 if (result != ISC_R_SUCCESS)
6580 /* Apply filters on the target name. */
6581 if (!is_answertarget_allowed(view,
6586 return (DNS_R_SERVFAIL);
6588 } else if (rdataset->type == dns_rdatatype_rrsig
6589 && rdataset->covers ==
6593 * We're looking for something else,
6594 * but we found a SIG CNAME.
6597 found_cname = ISC_TRUE;
6598 aflag = DNS_RDATASETATTR_ANSWERSIG;
6603 * We've found an answer to our
6608 rdataset->attributes |=
6609 DNS_RDATASETATTR_CACHE;
6610 rdataset->trust = dns_trust_answer;
6613 * This data is "the" answer
6614 * to our question only if
6615 * we're not chaining (i.e.
6616 * if we haven't followed
6617 * a CNAME or DNAME).
6621 DNS_RDATASETATTR_ANSWER)
6622 have_answer = ISC_TRUE;
6624 DNS_NAMEATTR_ANSWER;
6625 rdataset->attributes |= aflag;
6628 dns_trust_authanswer;
6629 } else if (external) {
6631 * This data is outside of
6632 * our query domain, and
6633 * may not be cached.
6635 rdataset->attributes |=
6636 DNS_RDATASETATTR_EXTERNAL;
6640 * Mark any additional data related
6643 (void)dns_rdataset_additionaldata(
6651 if (want_chaining) {
6652 wanted_chaining = ISC_TRUE;
6654 DNS_NAMEATTR_CHAINING;
6655 rdataset->attributes |=
6656 DNS_RDATASETATTR_CHAINING;
6661 * We could add an "else" clause here and
6662 * log that we're ignoring this rdataset.
6666 * If wanted_chaining is true, we've done
6667 * some chaining as the result of processing
6668 * this node, and thus we need to set
6671 * We don't set chaining inside of the
6672 * rdataset loop because doing that would
6673 * cause us to ignore the signatures of
6676 if (wanted_chaining)
6677 chaining = ISC_TRUE;
6680 * Look for a DNAME (or its SIG). Anything else is
6683 wanted_chaining = ISC_FALSE;
6684 for (rdataset = ISC_LIST_HEAD(name->list);
6686 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6687 isc_boolean_t found_dname = ISC_FALSE;
6688 dns_name_t *dname_name;
6692 if (rdataset->type == dns_rdatatype_dname) {
6694 * We're looking for something else,
6695 * but we found a DNAME.
6697 * If we're not chaining, then the
6698 * DNAME should not be external.
6700 if (!chaining && external) {
6703 return (DNS_R_FORMERR);
6706 want_chaining = ISC_TRUE;
6707 POST(want_chaining);
6708 aflag = DNS_RDATASETATTR_ANSWER;
6709 result = dname_target(fctx, rdataset,
6712 if (result == ISC_R_NOSPACE) {
6714 * We can't construct the
6715 * DNAME target. Do not
6718 want_chaining = ISC_FALSE;
6719 POST(want_chaining);
6720 } else if (result != ISC_R_SUCCESS)
6723 found_dname = ISC_TRUE;
6725 dname_name = dns_fixedname_name(&dname);
6726 if (!is_answertarget_allowed(view,
6731 return (DNS_R_SERVFAIL);
6733 } else if (rdataset->type == dns_rdatatype_rrsig
6734 && rdataset->covers ==
6735 dns_rdatatype_dname) {
6737 * We've found a signature that
6741 aflag = DNS_RDATASETATTR_ANSWERSIG;
6746 * We've found an answer to our
6751 rdataset->attributes |=
6752 DNS_RDATASETATTR_CACHE;
6753 rdataset->trust = dns_trust_answer;
6756 * This data is "the" answer
6757 * to our question only if
6758 * we're not chaining.
6762 DNS_RDATASETATTR_ANSWER)
6763 have_answer = ISC_TRUE;
6765 DNS_NAMEATTR_ANSWER;
6766 rdataset->attributes |= aflag;
6769 dns_trust_authanswer;
6770 } else if (external) {
6771 rdataset->attributes |=
6772 DNS_RDATASETATTR_EXTERNAL;
6780 * Copy the dname into the
6783 * Although we check for
6784 * failure of the copy
6785 * operation, in practice it
6786 * should never fail since
6787 * we already know that the
6788 * result fits in a fixedname.
6790 dns_fixedname_init(&fqname);
6791 result = dns_name_copy(
6792 dns_fixedname_name(&dname),
6793 dns_fixedname_name(&fqname),
6795 if (result != ISC_R_SUCCESS)
6797 wanted_chaining = ISC_TRUE;
6799 DNS_NAMEATTR_CHAINING;
6800 rdataset->attributes |=
6801 DNS_RDATASETATTR_CHAINING;
6802 qname = dns_fixedname_name(
6807 if (wanted_chaining)
6808 chaining = ISC_TRUE;
6810 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6812 if (result == ISC_R_NOMORE)
6813 result = ISC_R_SUCCESS;
6814 if (result != ISC_R_SUCCESS)
6818 * We should have found an answer.
6821 log_formerr(fctx, "reply has no answer");
6822 return (DNS_R_FORMERR);
6826 * This response is now potentially cacheable.
6828 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6831 * Did chaining end before we got the final answer?
6835 * Yes. This may be a negative reply, so hand off
6836 * authority section processing to the noanswer code.
6837 * If it isn't a noanswer response, no harm will be
6840 return (noanswer_response(fctx, qname, 0));
6844 * We didn't end with an incomplete chain, so the rcode should be
6847 if (message->rcode != dns_rcode_noerror) {
6848 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6850 return (DNS_R_FORMERR);
6854 * Examine the authority section (if there is one).
6856 * We expect there to be only one owner name for all the rdatasets
6857 * in this section, and we expect that it is not external.
6862 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6863 while (!done && result == ISC_R_SUCCESS) {
6865 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6866 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6869 * We expect to find NS or SIG NS rdatasets, and
6872 for (rdataset = ISC_LIST_HEAD(name->list);
6874 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6875 if (rdataset->type == dns_rdatatype_ns ||
6876 (rdataset->type == dns_rdatatype_rrsig &&
6877 rdataset->covers == dns_rdatatype_ns)) {
6880 rdataset->attributes |=
6881 DNS_RDATASETATTR_CACHE;
6882 if (aa && !chaining)
6884 dns_trust_authauthority;
6887 dns_trust_additional;
6889 if (rdataset->type == dns_rdatatype_ns) {
6891 ns_rdataset = rdataset;
6894 * Mark any additional data related
6897 (void)dns_rdataset_additionaldata(
6905 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6907 if (result == ISC_R_NOMORE)
6908 result = ISC_R_SUCCESS;
6910 log_ns_ttl(fctx, "answer_response");
6912 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6913 !dns_name_equal(ns_name, dns_rootname))
6914 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6920 fctx_increference(fetchctx_t *fctx) {
6921 REQUIRE(VALID_FCTX(fctx));
6923 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6925 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6928 static isc_boolean_t
6929 fctx_decreference(fetchctx_t *fctx) {
6930 isc_boolean_t bucket_empty = ISC_FALSE;
6932 REQUIRE(VALID_FCTX(fctx));
6934 INSIST(fctx->references > 0);
6936 if (fctx->references == 0) {
6938 * No one cares about the result of this fetch anymore.
6940 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6941 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6943 * This fctx is already shutdown; we were just
6944 * waiting for the last reference to go away.
6946 bucket_empty = fctx_unlink(fctx);
6950 * Initiate shutdown.
6952 fctx_shutdown(fctx);
6955 return (bucket_empty);
6959 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6960 dns_fetchevent_t *fevent;
6961 dns_resolver_t *res;
6963 isc_result_t result;
6964 isc_boolean_t bucket_empty;
6965 isc_boolean_t locked = ISC_FALSE;
6966 unsigned int bucketnum;
6967 dns_rdataset_t nameservers;
6968 dns_fixedname_t fixed;
6971 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6972 fevent = (dns_fetchevent_t *)event;
6973 fctx = event->ev_arg;
6974 REQUIRE(VALID_FCTX(fctx));
6978 FCTXTRACE("resume_dslookup");
6980 if (fevent->node != NULL)
6981 dns_db_detachnode(fevent->db, &fevent->node);
6982 if (fevent->db != NULL)
6983 dns_db_detach(&fevent->db);
6985 dns_rdataset_init(&nameservers);
6987 bucketnum = fctx->bucketnum;
6988 if (fevent->result == ISC_R_CANCELED) {
6989 dns_resolver_destroyfetch(&fctx->nsfetch);
6990 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6991 } else if (fevent->result == ISC_R_SUCCESS) {
6993 FCTXTRACE("resuming DS lookup");
6995 dns_resolver_destroyfetch(&fctx->nsfetch);
6996 if (dns_rdataset_isassociated(&fctx->nameservers))
6997 dns_rdataset_disassociate(&fctx->nameservers);
6998 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6999 fctx->ns_ttl = fctx->nameservers.ttl;
7000 fctx->ns_ttl_ok = ISC_TRUE;
7001 log_ns_ttl(fctx, "resume_dslookup");
7003 #ifdef ENABLE_FETCHLIMIT
7005 #endif /* ENABLE_FETCHLIMIT */
7007 dns_name_free(&fctx->domain, fctx->mctx);
7008 dns_name_init(&fctx->domain, NULL);
7009 result = dns_name_dup(&fctx->nsname, fctx->mctx, &fctx->domain);
7010 if (result != ISC_R_SUCCESS) {
7011 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7015 #ifdef ENABLE_FETCHLIMIT
7016 result = fcount_incr(fctx, ISC_TRUE);
7017 if (result != ISC_R_SUCCESS) {
7018 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7021 #endif /* ENABLE_FETCHLIMIT */
7026 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
7029 dns_rdataset_t *nsrdataset = NULL;
7032 * Retrieve state from fctx->nsfetch before we destroy it.
7034 dns_fixedname_init(&fixed);
7035 domain = dns_fixedname_name(&fixed);
7036 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
7037 if (dns_name_equal(&fctx->nsname, domain)) {
7038 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7039 dns_resolver_destroyfetch(&fctx->nsfetch);
7042 if (dns_rdataset_isassociated(
7043 &fctx->nsfetch->private->nameservers)) {
7045 &fctx->nsfetch->private->nameservers,
7047 nsrdataset = &nameservers;
7050 dns_resolver_destroyfetch(&fctx->nsfetch);
7051 n = dns_name_countlabels(&fctx->nsname);
7052 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
7055 if (dns_rdataset_isassociated(fevent->rdataset))
7056 dns_rdataset_disassociate(fevent->rdataset);
7057 FCTXTRACE("continuing to look for parent's NS records");
7058 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7059 dns_rdatatype_ns, domain,
7060 nsrdataset, NULL, 0, task,
7061 resume_dslookup, fctx,
7062 &fctx->nsrrset, NULL,
7064 if (result != ISC_R_SUCCESS)
7065 fctx_done(fctx, result, __LINE__);
7067 LOCK(&res->buckets[bucketnum].lock);
7074 if (dns_rdataset_isassociated(&nameservers))
7075 dns_rdataset_disassociate(&nameservers);
7076 if (dns_rdataset_isassociated(fevent->rdataset))
7077 dns_rdataset_disassociate(fevent->rdataset);
7078 INSIST(fevent->sigrdataset == NULL);
7079 isc_event_free(&event);
7081 LOCK(&res->buckets[bucketnum].lock);
7082 bucket_empty = fctx_decreference(fctx);
7083 UNLOCK(&res->buckets[bucketnum].lock);
7089 checknamessection(dns_message_t *message, dns_section_t section) {
7090 isc_result_t result;
7092 dns_rdata_t rdata = DNS_RDATA_INIT;
7093 dns_rdataset_t *rdataset;
7095 for (result = dns_message_firstname(message, section);
7096 result == ISC_R_SUCCESS;
7097 result = dns_message_nextname(message, section))
7100 dns_message_currentname(message, section, &name);
7101 for (rdataset = ISC_LIST_HEAD(name->list);
7103 rdataset = ISC_LIST_NEXT(rdataset, link)) {
7104 for (result = dns_rdataset_first(rdataset);
7105 result == ISC_R_SUCCESS;
7106 result = dns_rdataset_next(rdataset)) {
7107 dns_rdataset_current(rdataset, &rdata);
7108 if (!dns_rdata_checkowner(name, rdata.rdclass,
7111 !dns_rdata_checknames(&rdata, name, NULL))
7113 rdataset->attributes |=
7114 DNS_RDATASETATTR_CHECKNAMES;
7116 dns_rdata_reset(&rdata);
7123 checknames(dns_message_t *message) {
7125 checknamessection(message, DNS_SECTION_ANSWER);
7126 checknamessection(message, DNS_SECTION_AUTHORITY);
7127 checknamessection(message, DNS_SECTION_ADDITIONAL);
7131 * Log server NSID at log level 'level'
7134 log_nsid(isc_buffer_t *opt, size_t nsid_len, resquery_t *query,
7135 int level, isc_mem_t *mctx)
7137 static const char hex[17] = "0123456789abcdef";
7138 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7139 isc_uint16_t buflen, i;
7140 unsigned char *p, *buf, *nsid;
7142 /* Allocate buffer for storing hex version of the NSID */
7143 buflen = (isc_uint16_t)nsid_len * 2 + 1;
7144 buf = isc_mem_get(mctx, buflen);
7148 /* Convert to hex */
7150 nsid = isc_buffer_current(opt);
7151 for (i = 0; i < nsid_len; i++) {
7152 *p++ = hex[(nsid[0] >> 4) & 0xf];
7153 *p++ = hex[nsid[0] & 0xf];
7158 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7160 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7161 DNS_LOGMODULE_RESOLVER, level,
7162 "received NSID '%s' from %s", buf, addrbuf);
7165 isc_mem_put(mctx, buf, buflen);
7170 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
7171 isc_buffer_t buffer;
7174 isc_result_t result;
7176 if (! isc_log_wouldlog(dns_lctx, level))
7180 * Note that these are multiline debug messages. We want a newline
7181 * to appear in the log after each message.
7185 buf = isc_mem_get(mctx, len);
7188 isc_buffer_init(&buffer, buf, len);
7189 result = dns_message_totext(message, &dns_master_style_debug,
7191 if (result == ISC_R_NOSPACE) {
7192 isc_mem_put(mctx, buf, len);
7194 } else if (result == ISC_R_SUCCESS)
7195 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7196 DNS_LOGMODULE_RESOLVER, level,
7197 "received packet:\n%.*s",
7198 (int)isc_buffer_usedlength(&buffer),
7200 } while (result == ISC_R_NOSPACE);
7203 isc_mem_put(mctx, buf, len);
7206 static isc_boolean_t
7207 iscname(fetchctx_t *fctx) {
7208 isc_result_t result;
7210 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
7211 &fctx->name, dns_rdatatype_cname, 0,
7213 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
7216 static isc_boolean_t
7217 betterreferral(fetchctx_t *fctx) {
7218 isc_result_t result;
7220 dns_rdataset_t *rdataset;
7221 dns_message_t *message = fctx->rmessage;
7223 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
7224 result == ISC_R_SUCCESS;
7225 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
7227 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
7228 if (!isstrictsubdomain(name, &fctx->domain))
7230 for (rdataset = ISC_LIST_HEAD(name->list);
7232 rdataset = ISC_LIST_NEXT(rdataset, link))
7233 if (rdataset->type == dns_rdatatype_ns)
7240 process_opt(resquery_t *query, dns_rdataset_t *opt) {
7242 isc_buffer_t optbuf;
7243 isc_result_t result;
7244 isc_uint16_t optcode;
7245 isc_uint16_t optlen;
7247 result = dns_rdataset_first(opt);
7248 if (result == ISC_R_SUCCESS) {
7249 dns_rdata_init(&rdata);
7250 dns_rdataset_current(opt, &rdata);
7251 isc_buffer_init(&optbuf, rdata.data, rdata.length);
7252 isc_buffer_add(&optbuf, rdata.length);
7253 while (isc_buffer_remaininglength(&optbuf) >= 4) {
7254 optcode = isc_buffer_getuint16(&optbuf);
7255 optlen = isc_buffer_getuint16(&optbuf);
7256 INSIST(optlen <= isc_buffer_remaininglength(&optbuf));
7259 if (query->options & DNS_FETCHOPT_WANTNSID)
7260 log_nsid(&optbuf, optlen, query,
7262 query->fctx->res->mctx);
7263 isc_buffer_forward(&optbuf, optlen);
7266 isc_buffer_forward(&optbuf, optlen);
7270 INSIST(isc_buffer_remaininglength(&optbuf) == 0U);
7275 resquery_response(isc_task_t *task, isc_event_t *event) {
7276 isc_result_t result = ISC_R_SUCCESS;
7277 resquery_t *query = event->ev_arg;
7278 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
7279 isc_boolean_t keep_trying, get_nameservers, resend;
7280 isc_boolean_t truncated;
7281 dns_message_t *message;
7282 dns_rdataset_t *opt;
7285 dns_fixedname_t foundname;
7287 isc_time_t tnow, *finish;
7288 dns_adbaddrinfo_t *addrinfo;
7289 unsigned int options;
7290 unsigned int findoptions;
7291 isc_result_t broken_server;
7292 badnstype_t broken_type = badns_response;
7293 isc_boolean_t no_response;
7294 unsigned int bucketnum;
7295 dns_resolver_t *res;
7296 isc_boolean_t bucket_empty;
7298 REQUIRE(VALID_QUERY(query));
7300 options = query->options;
7301 REQUIRE(VALID_FCTX(fctx));
7302 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
7307 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
7308 inc_stats(res, dns_resstatscounter_responsev4);
7310 inc_stats(res, dns_resstatscounter_responsev6);
7312 (void)isc_timer_touch(fctx->timer);
7314 keep_trying = ISC_FALSE;
7315 broken_server = ISC_R_SUCCESS;
7316 get_nameservers = ISC_FALSE;
7318 truncated = ISC_FALSE;
7320 no_response = ISC_FALSE;
7323 result = ISC_R_SHUTTINGDOWN;
7324 FCTXTRACE("resolver shutting down");
7329 fctx->timeout = ISC_FALSE;
7330 fctx->addrinfo = query->addrinfo;
7333 * XXXRTH We should really get the current time just once. We
7334 * need a routine to convert from an isc_time_t to an
7339 isc_stdtime_get(&now);
7342 * Did the dispatcher have a problem?
7344 if (devent->result != ISC_R_SUCCESS) {
7345 if (devent->result == ISC_R_EOF &&
7346 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7348 * The problem might be that they
7349 * don't understand EDNS0. Turn it
7350 * off and try again.
7352 options |= DNS_FETCHOPT_NOEDNS0;
7354 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7357 * There's no hope for this query.
7359 keep_trying = ISC_TRUE;
7362 * If this is a network error on an exclusive query
7363 * socket, mark the server as bad so that we won't try
7364 * it for this fetch again. Also adjust finish and
7365 * no_response so that we penalize this address in SRTT
7368 if (query->exclusivesocket &&
7369 (devent->result == ISC_R_HOSTUNREACH ||
7370 devent->result == ISC_R_NETUNREACH ||
7371 devent->result == ISC_R_CONNREFUSED ||
7372 devent->result == ISC_R_CANCELED)) {
7373 broken_server = devent->result;
7374 broken_type = badns_unreachable;
7376 no_response = ISC_TRUE;
7379 FCTXTRACE3("dispatcher failure", devent->result);
7383 message = fctx->rmessage;
7385 if (query->tsig != NULL) {
7386 result = dns_message_setquerytsig(message, query->tsig);
7387 if (result != ISC_R_SUCCESS) {
7388 FCTXTRACE3("unable to set query tsig", result);
7393 if (query->tsigkey) {
7394 result = dns_message_settsigkey(message, query->tsigkey);
7395 if (result != ISC_R_SUCCESS) {
7396 FCTXTRACE3("unable to set tsig key", result);
7401 dns_message_setclass(message, fctx->res->rdclass);
7403 if ((options & DNS_FETCHOPT_TCP) == 0)
7404 dns_adb_plainresponse(fctx->adb, query->addrinfo);
7406 result = dns_message_parse(message, &devent->buffer, 0);
7407 if (result != ISC_R_SUCCESS) {
7408 FCTXTRACE3("message failed to parse", result);
7410 case ISC_R_UNEXPECTEDEND:
7411 if (!message->question_ok ||
7412 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
7413 (options & DNS_FETCHOPT_TCP) != 0) {
7415 * Either the message ended prematurely,
7416 * and/or wasn't marked as being truncated,
7417 * and/or this is a response to a query we
7418 * sent over TCP. In all of these cases,
7419 * something is wrong with the remote
7420 * server and we don't want to retry using
7423 if ((query->options & DNS_FETCHOPT_NOEDNS0)
7426 * The problem might be that they
7427 * don't understand EDNS0. Turn it
7428 * off and try again.
7430 options |= DNS_FETCHOPT_NOEDNS0;
7433 &query->addrinfo->sockaddr);
7435 dns_resstatscounter_edns0fail);
7437 broken_server = result;
7438 keep_trying = ISC_TRUE;
7443 * We defer retrying via TCP for a bit so we can
7444 * check out this message further.
7446 truncated = ISC_TRUE;
7449 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7451 * The problem might be that they
7452 * don't understand EDNS0. Turn it
7453 * off and try again.
7455 options |= DNS_FETCHOPT_NOEDNS0;
7457 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7458 inc_stats(res, dns_resstatscounter_edns0fail);
7460 broken_server = DNS_R_UNEXPECTEDRCODE;
7461 keep_trying = ISC_TRUE;
7466 * Something bad has happened.
7474 * Log the incoming packet.
7476 log_packet(message, ISC_LOG_DEBUG(10), res->mctx);
7478 if (message->rdclass != fctx->res->rdclass) {
7480 FCTXTRACE("bad class");
7485 * Process receive opt record.
7487 opt = dns_message_getopt(message);
7489 process_opt(query, opt);
7492 * If the message is signed, check the signature. If not, this
7493 * returns success anyway.
7495 result = dns_message_checksig(message, res->view);
7496 if (result != ISC_R_SUCCESS) {
7497 FCTXTRACE3("signature check failed", result);
7502 * The dispatcher should ensure we only get responses with QR set.
7504 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
7506 * INSIST() that the message comes from the place we sent it to,
7507 * since the dispatch code should ensure this.
7509 * INSIST() that the message id is correct (this should also be
7510 * ensured by the dispatch code).
7514 * We have an affirmative response to the query and we have
7515 * previously got a response from this server which indicated
7516 * EDNS may not be supported so we can now cache the lack of
7519 if (opt == NULL && !EDNSOK(query->addrinfo) &&
7520 (message->rcode == dns_rcode_noerror ||
7521 message->rcode == dns_rcode_nxdomain ||
7522 message->rcode == dns_rcode_refused ||
7523 message->rcode == dns_rcode_yxdomain) &&
7524 bad_edns(fctx, &query->addrinfo->sockaddr)) {
7525 dns_adb_changeflags(fctx->adb, query->addrinfo,
7526 DNS_FETCHOPT_NOEDNS0,
7527 DNS_FETCHOPT_NOEDNS0);
7531 * If we get a non error EDNS response record the fact so we
7532 * won't fallback to plain DNS in the future for this server.
7534 if (opt != NULL && !EDNSOK(query->addrinfo) &&
7535 (query->options & DNS_FETCHOPT_NOEDNS0) == 0 &&
7536 (message->rcode == dns_rcode_noerror ||
7537 message->rcode == dns_rcode_nxdomain ||
7538 message->rcode == dns_rcode_refused ||
7539 message->rcode == dns_rcode_yxdomain)) {
7540 dns_adb_changeflags(fctx->adb, query->addrinfo,
7541 FCTX_ADDRINFO_EDNSOK,
7542 FCTX_ADDRINFO_EDNSOK);
7546 * Deal with truncated responses by retrying using TCP.
7548 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
7549 truncated = ISC_TRUE;
7552 inc_stats(res, dns_resstatscounter_truncated);
7553 if ((options & DNS_FETCHOPT_TCP) != 0) {
7554 broken_server = DNS_R_TRUNCATEDTCP;
7555 keep_trying = ISC_TRUE;
7557 options |= DNS_FETCHOPT_TCP;
7560 FCTXTRACE3("message truncated", result);
7565 * Is it a query response?
7567 if (message->opcode != dns_opcode_query) {
7569 broken_server = DNS_R_UNEXPECTEDOPCODE;
7570 keep_trying = ISC_TRUE;
7571 FCTXTRACE("invalid message opcode");
7576 * Update statistics about erroneous responses.
7578 if (message->rcode != dns_rcode_noerror) {
7579 switch (message->rcode) {
7580 case dns_rcode_nxdomain:
7581 inc_stats(res, dns_resstatscounter_nxdomain);
7583 case dns_rcode_servfail:
7584 inc_stats(res, dns_resstatscounter_servfail);
7586 case dns_rcode_formerr:
7587 inc_stats(res, dns_resstatscounter_formerr);
7590 inc_stats(res, dns_resstatscounter_othererror);
7596 * Is the remote server broken, or does it dislike us?
7598 if (message->rcode != dns_rcode_noerror &&
7599 message->rcode != dns_rcode_nxdomain) {
7602 if (((message->rcode == dns_rcode_formerr ||
7603 message->rcode == dns_rcode_notimp) ||
7604 (message->rcode == dns_rcode_servfail &&
7605 dns_message_getopt(message) == NULL)) &&
7606 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7608 * It's very likely they don't like EDNS0.
7609 * If the response code is SERVFAIL, also check if the
7610 * response contains an OPT RR and don't cache the
7611 * failure since it can be returned for various other
7614 * XXXRTH We should check if the question
7615 * we're asking requires EDNS0, and
7616 * if so, we should bail out.
7618 options |= DNS_FETCHOPT_NOEDNS0;
7621 * Remember that they may not like EDNS0.
7623 add_bad_edns(fctx, &query->addrinfo->sockaddr);
7624 inc_stats(res, dns_resstatscounter_edns0fail);
7625 } else if (message->rcode == dns_rcode_formerr) {
7626 if (ISFORWARDER(query->addrinfo)) {
7628 * This forwarder doesn't understand us,
7629 * but other forwarders might. Keep trying.
7631 broken_server = DNS_R_REMOTEFORMERR;
7632 keep_trying = ISC_TRUE;
7635 * The server doesn't understand us. Since
7636 * all servers for a zone need similar
7637 * capabilities, we assume that we will get
7638 * FORMERR from all servers, and thus we
7639 * cannot make any more progress with this
7642 log_formerr(fctx, "server sent FORMERR");
7643 result = DNS_R_FORMERR;
7645 } else if (message->rcode == dns_rcode_yxdomain) {
7647 * DNAME mapping failed because the new name
7648 * was too long. There's no chance of success
7651 result = DNS_R_YXDOMAIN;
7652 } else if (message->rcode == dns_rcode_badvers) {
7654 * This should be impossible as we only send EDNS
7655 * version 0 requests and to return BADVERS you
7656 * need to support EDNS as it is a extended rcode.
7658 broken_server = DNS_R_BADVERS;
7659 keep_trying = ISC_TRUE;
7664 broken_server = DNS_R_UNEXPECTEDRCODE;
7665 INSIST(broken_server != ISC_R_SUCCESS);
7666 keep_trying = ISC_TRUE;
7669 isc_buffer_init(&b, code, sizeof(code) - 1);
7670 dns_rcode_totext(fctx->rmessage->rcode, &b);
7671 code[isc_buffer_usedlength(&b)] = '\0';
7672 FCTXTRACE2("remote server broken: returned ", code);
7677 * Is the question the same as the one we asked?
7679 result = same_question(fctx);
7680 if (result != ISC_R_SUCCESS) {
7682 if (result == DNS_R_FORMERR)
7683 keep_trying = ISC_TRUE;
7684 FCTXTRACE3("response did not match question", result);
7689 * Is the server lame?
7691 if (res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
7693 inc_stats(res, dns_resstatscounter_lame);
7694 log_lame(fctx, query->addrinfo);
7695 result = dns_adb_marklame(fctx->adb, query->addrinfo,
7696 &fctx->name, fctx->type,
7697 now + res->lame_ttl);
7698 if (result != ISC_R_SUCCESS)
7699 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7700 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
7701 "could not mark server as lame: %s",
7702 isc_result_totext(result));
7703 broken_server = DNS_R_LAME;
7704 keep_trying = ISC_TRUE;
7705 FCTXTRACE("lame server");
7710 * Enforce delegations only zones like NET and COM.
7712 if (!ISFORWARDER(query->addrinfo) &&
7713 dns_view_isdelegationonly(res->view, &fctx->domain) &&
7714 !dns_name_equal(&fctx->domain, &fctx->name) &&
7715 fix_mustbedelegationornxdomain(message, fctx)) {
7716 char namebuf[DNS_NAME_FORMATSIZE];
7717 char domainbuf[DNS_NAME_FORMATSIZE];
7718 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7722 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
7723 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7724 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
7725 dns_rdataclass_format(res->rdclass, classbuf,
7727 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7730 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
7731 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7732 "enforced delegation-only for '%s' (%s/%s/%s) "
7734 domainbuf, namebuf, typebuf, classbuf, addrbuf);
7737 if ((res->options & DNS_RESOLVER_CHECKNAMES) != 0)
7738 checknames(message);
7743 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
7746 * Did we get any answers?
7748 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
7749 (message->rcode == dns_rcode_noerror ||
7750 message->rcode == dns_rcode_nxdomain)) {
7753 * We've got answers. If it has an authoritative answer or an
7754 * answer from a forwarder, we're done.
7756 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
7757 ISFORWARDER(query->addrinfo))
7759 result = answer_response(fctx);
7760 if (result != ISC_R_SUCCESS)
7761 FCTXTRACE3("answer_response (AA/fwd)", result);
7762 } else if (iscname(fctx) &&
7763 fctx->type != dns_rdatatype_any &&
7764 fctx->type != dns_rdatatype_cname)
7767 * A BIND8 server could return a non-authoritative
7768 * answer when a CNAME is followed. We should treat
7769 * it as a valid answer.
7771 result = answer_response(fctx);
7772 if (result != ISC_R_SUCCESS)
7773 FCTXTRACE3("answer_response (!ANY/!CNAME)",
7775 } else if (fctx->type != dns_rdatatype_ns &&
7776 !betterreferral(fctx)) {
7778 * Lame response !!!.
7780 result = answer_response(fctx);
7781 if (result != ISC_R_SUCCESS)
7782 FCTXTRACE3("answer_response (!NS)", result);
7784 if (fctx->type == dns_rdatatype_ns) {
7786 * A BIND 8 server could incorrectly return a
7787 * non-authoritative answer to an NS query
7788 * instead of a referral. Since this answer
7789 * lacks the SIGs necessary to do DNSSEC
7790 * validation, we must invoke the following
7791 * special kludge to treat it as a referral.
7793 result = noanswer_response(fctx, NULL,
7794 LOOK_FOR_NS_IN_ANSWER);
7795 if (result != ISC_R_SUCCESS)
7796 FCTXTRACE3("noanswer_response (NS)",
7800 * Some other servers may still somehow include
7801 * an answer when it should return a referral
7802 * with an empty answer. Check to see if we can
7803 * treat this as a referral by ignoring the
7804 * answer. Further more, there may be an
7805 * implementation that moves A/AAAA glue records
7806 * to the answer section for that type of
7807 * delegation when the query is for that glue
7808 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
7809 * such a corner case.
7811 result = noanswer_response(fctx, NULL,
7812 LOOK_FOR_GLUE_IN_ANSWER);
7813 if (result != ISC_R_SUCCESS)
7814 FCTXTRACE3("noanswer_response", result);
7816 if (result != DNS_R_DELEGATION) {
7818 * At this point, AA is not set, the response
7819 * is not a referral, and the server is not a
7820 * forwarder. It is technically lame and it's
7821 * easier to treat it as such than to figure out
7822 * some more elaborate course of action.
7824 broken_server = DNS_R_LAME;
7825 keep_trying = ISC_TRUE;
7828 goto force_referral;
7830 if (result != ISC_R_SUCCESS) {
7831 if (result == DNS_R_FORMERR)
7832 keep_trying = ISC_TRUE;
7835 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
7836 message->rcode == dns_rcode_noerror ||
7837 message->rcode == dns_rcode_nxdomain) {
7839 * NXDOMAIN, NXRDATASET, or referral.
7841 result = noanswer_response(fctx, NULL, 0);
7844 case DNS_R_CHASEDSSERVERS:
7846 case DNS_R_DELEGATION:
7849 * We don't have the answer, but we know a better
7852 get_nameservers = ISC_TRUE;
7853 keep_trying = ISC_TRUE;
7855 * We have a new set of name servers, and it
7856 * has not experienced any restarts yet.
7861 * Update local statistics counters collected for each
7865 fctx->querysent = 0;
7866 fctx->lamecount = 0;
7867 fctx->quotacount = 0;
7872 result = ISC_R_SUCCESS;
7876 * Something has gone wrong.
7878 if (result == DNS_R_FORMERR)
7879 keep_trying = ISC_TRUE;
7880 FCTXTRACE3("noanswer_response", result);
7885 * The server is insane.
7888 broken_server = DNS_R_UNEXPECTEDRCODE;
7889 keep_trying = ISC_TRUE;
7890 FCTXTRACE("broken server: unexpected rcode");
7895 * Follow additional section data chains.
7897 chase_additional(fctx);
7900 * Cache the cacheable parts of the message. This may also cause
7901 * work to be queued to the DNSSEC validator.
7903 if (WANTCACHE(fctx)) {
7904 result = cache_message(fctx, query->addrinfo, now);
7905 if (result != ISC_R_SUCCESS) {
7906 FCTXTRACE3("cache_message complete", result);
7912 * Ncache the negatively cacheable parts of the message. This may
7913 * also cause work to be queued to the DNSSEC validator.
7915 if (WANTNCACHE(fctx)) {
7916 dns_rdatatype_t covers;
7919 * Cache DS NXDOMAIN seperately to other types.
7921 if (message->rcode == dns_rcode_nxdomain &&
7922 fctx->type != dns_rdatatype_ds)
7923 covers = dns_rdatatype_any;
7925 covers = fctx->type;
7928 * Cache any negative cache entries in the message.
7930 result = ncache_message(fctx, query->addrinfo, covers, now);
7931 if (result != ISC_R_SUCCESS)
7932 FCTXTRACE3("ncache_message complete", result);
7937 * Remember the query's addrinfo, in case we need to mark the
7940 addrinfo = query->addrinfo;
7942 FCTXTRACE4("query canceled in response(); ",
7943 no_response ? "no response" : "responding",
7949 * XXXRTH Don't cancel the query if waiting for validation?
7951 fctx_cancelquery(&query, &devent, finish, no_response);
7954 if (result == DNS_R_FORMERR)
7955 broken_server = DNS_R_FORMERR;
7956 if (broken_server != ISC_R_SUCCESS) {
7958 * Add this server to the list of bad servers for
7961 add_bad(fctx, addrinfo, broken_server, broken_type);
7964 if (get_nameservers) {
7966 dns_fixedname_init(&foundname);
7967 fname = dns_fixedname_name(&foundname);
7968 if (result != ISC_R_SUCCESS) {
7969 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7973 if (dns_rdatatype_atparent(fctx->type))
7974 findoptions |= DNS_DBFIND_NOEXACT;
7975 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7978 name = &fctx->domain;
7979 result = dns_view_findzonecut(res->view,
7985 if (result != ISC_R_SUCCESS) {
7986 FCTXTRACE("couldn't find a zonecut");
7987 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7990 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7992 * The best nameservers are now above our
7995 FCTXTRACE("nameservers now above QDOMAIN");
7996 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
8000 #ifdef ENABLE_FETCHLIMIT
8002 #endif /* ENABLE_FETCHLIMIT */
8004 dns_name_free(&fctx->domain, fctx->mctx);
8005 dns_name_init(&fctx->domain, NULL);
8006 result = dns_name_dup(fname, fctx->mctx, &fctx->domain);
8007 if (result != ISC_R_SUCCESS) {
8008 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
8012 #ifdef ENABLE_FETCHLIMIT
8013 result = fcount_incr(fctx, ISC_TRUE);
8014 if (result != ISC_R_SUCCESS) {
8015 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
8018 #endif /* ENABLE_FETCHLIMIT */
8020 fctx->ns_ttl = fctx->nameservers.ttl;
8021 fctx->ns_ttl_ok = ISC_TRUE;
8022 fctx_cancelqueries(fctx, ISC_TRUE);
8023 fctx_cleanupfinds(fctx);
8024 fctx_cleanupaltfinds(fctx);
8025 fctx_cleanupforwaddrs(fctx);
8026 fctx_cleanupaltaddrs(fctx);
8031 fctx_try(fctx, !get_nameservers, ISC_FALSE);
8032 } else if (resend) {
8034 * Resend (probably with changed options).
8036 FCTXTRACE("resend");
8037 inc_stats(res, dns_resstatscounter_retry);
8038 bucketnum = fctx->bucketnum;
8039 fctx_increference(fctx);
8040 result = fctx_query(fctx, addrinfo, options);
8041 if (result != ISC_R_SUCCESS) {
8042 fctx_done(fctx, result, __LINE__);
8043 LOCK(&res->buckets[bucketnum].lock);
8044 bucket_empty = fctx_decreference(fctx);
8045 UNLOCK(&res->buckets[bucketnum].lock);
8049 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
8051 * All has gone well so far, but we are waiting for the
8052 * DNSSEC validator to validate the answer.
8054 FCTXTRACE("wait for validator");
8055 fctx_cancelqueries(fctx, ISC_TRUE);
8057 * We must not retransmit while the validator is working;
8058 * it has references to the current rmessage.
8060 result = fctx_stopidletimer(fctx);
8061 if (result != ISC_R_SUCCESS)
8062 fctx_done(fctx, result, __LINE__);
8063 } else if (result == DNS_R_CHASEDSSERVERS) {
8065 add_bad(fctx, addrinfo, result, broken_type);
8066 fctx_cancelqueries(fctx, ISC_TRUE);
8067 fctx_cleanupfinds(fctx);
8068 fctx_cleanupforwaddrs(fctx);
8070 n = dns_name_countlabels(&fctx->name);
8071 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
8073 FCTXTRACE("suspending DS lookup to find parent's NS records");
8075 result = dns_resolver_createfetch(res, &fctx->nsname,
8077 NULL, NULL, NULL, 0, task,
8078 resume_dslookup, fctx,
8079 &fctx->nsrrset, NULL,
8081 if (result != ISC_R_SUCCESS)
8082 fctx_done(fctx, result, __LINE__);
8084 fctx_increference(fctx);
8085 result = fctx_stopidletimer(fctx);
8086 if (result != ISC_R_SUCCESS)
8087 fctx_done(fctx, result, __LINE__);
8093 fctx_done(fctx, result, __LINE__);
8099 *** Resolver Methods
8102 destroy_badcache(dns_resolver_t *res) {
8103 dns_badcache_t *bad, *next;
8106 if (res->badcache != NULL) {
8107 for (i = 0; i < res->badhash; i++)
8108 for (bad = res->badcache[i]; bad != NULL;
8111 isc_mem_put(res->mctx, bad, sizeof(*bad) +
8115 isc_mem_put(res->mctx, res->badcache,
8116 sizeof(*res->badcache) * res->badhash);
8117 res->badcache = NULL;
8119 INSIST(res->badcount == 0);
8124 destroy(dns_resolver_t *res) {
8128 REQUIRE(res->references == 0);
8129 REQUIRE(!res->priming);
8130 REQUIRE(res->primefetch == NULL);
8134 INSIST(res->nfctx == 0);
8136 DESTROYLOCK(&res->primelock);
8137 DESTROYLOCK(&res->nlock);
8138 DESTROYLOCK(&res->lock);
8139 for (i = 0; i < res->nbuckets; i++) {
8140 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
8141 isc_task_shutdown(res->buckets[i].task);
8142 isc_task_detach(&res->buckets[i].task);
8143 DESTROYLOCK(&res->buckets[i].lock);
8144 isc_mem_detach(&res->buckets[i].mctx);
8146 isc_mem_put(res->mctx, res->buckets,
8147 res->nbuckets * sizeof(fctxbucket_t));
8148 #ifdef ENABLE_FETCHLIMIT
8149 for (i = 0; i < RES_DOMAIN_BUCKETS; i++) {
8150 INSIST(ISC_LIST_EMPTY(res->dbuckets[i].list));
8151 isc_mem_detach(&res->dbuckets[i].mctx);
8152 DESTROYLOCK(&res->dbuckets[i].lock);
8154 isc_mem_put(res->mctx, res->dbuckets,
8155 RES_DOMAIN_BUCKETS * sizeof(zonebucket_t));
8156 #endif /* ENABLE_FETCHLIMIT */
8157 if (res->dispatches4 != NULL)
8158 dns_dispatchset_destroy(&res->dispatches4);
8159 if (res->dispatches6 != NULL)
8160 dns_dispatchset_destroy(&res->dispatches6);
8161 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
8162 ISC_LIST_UNLINK(res->alternates, a, link);
8164 dns_name_free(&a->_u._n.name, res->mctx);
8165 isc_mem_put(res->mctx, a, sizeof(*a));
8167 dns_resolver_reset_algorithms(res);
8168 destroy_badcache(res);
8169 dns_resolver_resetmustbesecure(res);
8171 isc_rwlock_destroy(&res->alglock);
8174 isc_rwlock_destroy(&res->mbslock);
8176 isc_timer_detach(&res->spillattimer);
8178 isc_mem_put(res->mctx, res, sizeof(*res));
8182 send_shutdown_events(dns_resolver_t *res) {
8183 isc_event_t *event, *next_event;
8187 * Caller must be holding the resolver lock.
8190 for (event = ISC_LIST_HEAD(res->whenshutdown);
8192 event = next_event) {
8193 next_event = ISC_LIST_NEXT(event, ev_link);
8194 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
8195 etask = event->ev_sender;
8196 event->ev_sender = res;
8197 isc_task_sendanddetach(&etask, &event);
8202 empty_bucket(dns_resolver_t *res) {
8203 RTRACE("empty_bucket");
8207 INSIST(res->activebuckets > 0);
8208 res->activebuckets--;
8209 if (res->activebuckets == 0)
8210 send_shutdown_events(res);
8216 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
8217 dns_resolver_t *res = event->ev_arg;
8218 isc_result_t result;
8220 isc_boolean_t logit = ISC_FALSE;
8222 REQUIRE(VALID_RESOLVER(res));
8227 INSIST(!res->exiting);
8228 if (res->spillat > res->spillatmin) {
8232 if (res->spillat <= res->spillatmin) {
8233 result = isc_timer_reset(res->spillattimer,
8234 isc_timertype_inactive, NULL,
8236 RUNTIME_CHECK(result == ISC_R_SUCCESS);
8238 count = res->spillat;
8241 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
8242 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
8243 "clients-per-query decreased to %u", count);
8245 isc_event_free(&event);
8249 dns_resolver_create(dns_view_t *view,
8250 isc_taskmgr_t *taskmgr,
8251 unsigned int ntasks, unsigned int ndisp,
8252 isc_socketmgr_t *socketmgr,
8253 isc_timermgr_t *timermgr,
8254 unsigned int options,
8255 dns_dispatchmgr_t *dispatchmgr,
8256 dns_dispatch_t *dispatchv4,
8257 dns_dispatch_t *dispatchv6,
8258 dns_resolver_t **resp)
8260 dns_resolver_t *res;
8261 isc_result_t result = ISC_R_SUCCESS;
8262 unsigned int i, buckets_created = 0;
8263 isc_task_t *task = NULL;
8266 #ifdef ENABLE_FETCHLIMIT
8267 unsigned int dbuckets_created = 0;
8268 #endif /* ENABLE_FETCHLIMIT */
8271 * Create a resolver.
8274 REQUIRE(DNS_VIEW_VALID(view));
8275 REQUIRE(ntasks > 0);
8277 REQUIRE(resp != NULL && *resp == NULL);
8278 REQUIRE(dispatchmgr != NULL);
8279 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
8281 res = isc_mem_get(view->mctx, sizeof(*res));
8283 return (ISC_R_NOMEMORY);
8285 res->mctx = view->mctx;
8286 res->rdclass = view->rdclass;
8287 res->socketmgr = socketmgr;
8288 res->timermgr = timermgr;
8289 res->taskmgr = taskmgr;
8290 res->dispatchmgr = dispatchmgr;
8292 res->options = options;
8294 ISC_LIST_INIT(res->alternates);
8295 res->udpsize = RECV_BUFFER_SIZE;
8296 res->algorithms = NULL;
8297 res->badcache = NULL;
8301 res->mustbesecure = NULL;
8302 res->spillatmin = res->spillat = 10;
8303 res->spillatmax = 100;
8304 res->spillattimer = NULL;
8306 res->zero_no_soa_ttl = ISC_FALSE;
8307 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
8308 res->maxdepth = DEFAULT_RECURSION_DEPTH;
8309 res->maxqueries = DEFAULT_MAX_QUERIES;
8310 res->quotaresp[dns_quotatype_zone] = DNS_R_DROP;
8311 res->quotaresp[dns_quotatype_server] = DNS_R_SERVFAIL;
8312 res->nbuckets = ntasks;
8313 res->activebuckets = ntasks;
8314 res->buckets = isc_mem_get(view->mctx,
8315 ntasks * sizeof(fctxbucket_t));
8316 if (res->buckets == NULL) {
8317 result = ISC_R_NOMEMORY;
8320 for (i = 0; i < ntasks; i++) {
8321 result = isc_mutex_init(&res->buckets[i].lock);
8322 if (result != ISC_R_SUCCESS)
8323 goto cleanup_buckets;
8324 res->buckets[i].task = NULL;
8325 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
8326 if (result != ISC_R_SUCCESS) {
8327 DESTROYLOCK(&res->buckets[i].lock);
8328 goto cleanup_buckets;
8330 res->buckets[i].mctx = NULL;
8331 snprintf(name, sizeof(name), "res%u", i);
8332 #ifdef ISC_PLATFORM_USETHREADS
8334 * Use a separate memory context for each bucket to reduce
8335 * contention among multiple threads. Do this only when
8336 * enabling threads because it will be require more memory.
8338 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
8339 if (result != ISC_R_SUCCESS) {
8340 isc_task_detach(&res->buckets[i].task);
8341 DESTROYLOCK(&res->buckets[i].lock);
8342 goto cleanup_buckets;
8344 isc_mem_setname(res->buckets[i].mctx, name, NULL);
8346 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
8348 isc_task_setname(res->buckets[i].task, name, res);
8349 ISC_LIST_INIT(res->buckets[i].fctxs);
8350 res->buckets[i].exiting = ISC_FALSE;
8354 #ifdef ENABLE_FETCHLIMIT
8355 res->dbuckets = isc_mem_get(view->mctx,
8356 RES_DOMAIN_BUCKETS * sizeof(zonebucket_t));
8357 if (res->dbuckets == NULL) {
8358 result = ISC_R_NOMEMORY;
8359 goto cleanup_buckets;
8361 for (i = 0; i < RES_DOMAIN_BUCKETS; i++) {
8362 ISC_LIST_INIT(res->dbuckets[i].list);
8363 res->dbuckets[i].mctx = NULL;
8364 isc_mem_attach(view->mctx, &res->dbuckets[i].mctx);
8365 result = isc_mutex_init(&res->dbuckets[i].lock);
8366 if (result != ISC_R_SUCCESS) {
8367 isc_mem_detach(&res->dbuckets[i].mctx);
8368 goto cleanup_dbuckets;
8372 #endif /* ENABLE_FETCHLIMIT */
8374 res->dispatches4 = NULL;
8375 if (dispatchv4 != NULL) {
8376 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
8377 dispatchv4, &res->dispatches4, ndisp);
8378 dispattr = dns_dispatch_getattributes(dispatchv4);
8380 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
8383 res->dispatches6 = NULL;
8384 if (dispatchv6 != NULL) {
8385 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
8386 dispatchv6, &res->dispatches6, ndisp);
8387 dispattr = dns_dispatch_getattributes(dispatchv6);
8389 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
8392 res->references = 1;
8393 res->exiting = ISC_FALSE;
8394 res->frozen = ISC_FALSE;
8395 ISC_LIST_INIT(res->whenshutdown);
8396 res->priming = ISC_FALSE;
8397 res->primefetch = NULL;
8400 result = isc_mutex_init(&res->lock);
8401 if (result != ISC_R_SUCCESS)
8402 goto cleanup_dispatches;
8404 result = isc_mutex_init(&res->nlock);
8405 if (result != ISC_R_SUCCESS)
8408 result = isc_mutex_init(&res->primelock);
8409 if (result != ISC_R_SUCCESS)
8413 result = isc_task_create(taskmgr, 0, &task);
8414 if (result != ISC_R_SUCCESS)
8415 goto cleanup_primelock;
8417 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
8418 task, spillattimer_countdown, res,
8419 &res->spillattimer);
8420 isc_task_detach(&task);
8421 if (result != ISC_R_SUCCESS)
8422 goto cleanup_primelock;
8425 result = isc_rwlock_init(&res->alglock, 0, 0);
8426 if (result != ISC_R_SUCCESS)
8427 goto cleanup_spillattimer;
8430 result = isc_rwlock_init(&res->mbslock, 0, 0);
8431 if (result != ISC_R_SUCCESS)
8432 goto cleanup_alglock;
8435 res->magic = RES_MAGIC;
8439 return (ISC_R_SUCCESS);
8444 isc_rwlock_destroy(&res->alglock);
8447 #if USE_ALGLOCK || USE_MBSLOCK
8448 cleanup_spillattimer:
8449 isc_timer_detach(&res->spillattimer);
8453 DESTROYLOCK(&res->primelock);
8456 DESTROYLOCK(&res->nlock);
8459 DESTROYLOCK(&res->lock);
8462 if (res->dispatches6 != NULL)
8463 dns_dispatchset_destroy(&res->dispatches6);
8464 if (res->dispatches4 != NULL)
8465 dns_dispatchset_destroy(&res->dispatches4);
8467 #ifdef ENABLE_FETCHLIMIT
8469 for (i = 0; i < dbuckets_created; i++) {
8470 DESTROYLOCK(&res->dbuckets[i].lock);
8471 isc_mem_detach(&res->dbuckets[i].mctx);
8473 isc_mem_put(view->mctx, res->dbuckets,
8474 RES_DOMAIN_BUCKETS * sizeof(zonebucket_t));
8475 #endif /* ENABLE_FETCHLIMIT*/
8478 for (i = 0; i < buckets_created; i++) {
8479 isc_mem_detach(&res->buckets[i].mctx);
8480 DESTROYLOCK(&res->buckets[i].lock);
8481 isc_task_shutdown(res->buckets[i].task);
8482 isc_task_detach(&res->buckets[i].task);
8484 isc_mem_put(view->mctx, res->buckets,
8485 res->nbuckets * sizeof(fctxbucket_t));
8488 isc_mem_put(view->mctx, res, sizeof(*res));
8495 prime_done(isc_task_t *task, isc_event_t *event) {
8496 dns_resolver_t *res;
8497 dns_fetchevent_t *fevent;
8499 dns_db_t *db = NULL;
8501 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
8502 fevent = (dns_fetchevent_t *)event;
8503 res = event->ev_arg;
8504 REQUIRE(VALID_RESOLVER(res));
8510 INSIST(res->priming);
8511 res->priming = ISC_FALSE;
8512 LOCK(&res->primelock);
8513 fetch = res->primefetch;
8514 res->primefetch = NULL;
8515 UNLOCK(&res->primelock);
8519 if (fevent->result == ISC_R_SUCCESS &&
8520 res->view->cache != NULL && res->view->hints != NULL) {
8521 dns_cache_attachdb(res->view->cache, &db);
8522 dns_root_checkhints(res->view, res->view->hints, db);
8526 if (fevent->node != NULL)
8527 dns_db_detachnode(fevent->db, &fevent->node);
8528 if (fevent->db != NULL)
8529 dns_db_detach(&fevent->db);
8530 if (dns_rdataset_isassociated(fevent->rdataset))
8531 dns_rdataset_disassociate(fevent->rdataset);
8532 INSIST(fevent->sigrdataset == NULL);
8534 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
8536 isc_event_free(&event);
8537 dns_resolver_destroyfetch(&fetch);
8541 dns_resolver_prime(dns_resolver_t *res) {
8542 isc_boolean_t want_priming = ISC_FALSE;
8543 dns_rdataset_t *rdataset;
8544 isc_result_t result;
8546 REQUIRE(VALID_RESOLVER(res));
8547 REQUIRE(res->frozen);
8549 RTRACE("dns_resolver_prime");
8553 if (!res->exiting && !res->priming) {
8554 INSIST(res->primefetch == NULL);
8555 res->priming = ISC_TRUE;
8556 want_priming = ISC_TRUE;
8563 * To avoid any possible recursive locking problems, we
8564 * start the priming fetch like any other fetch, and holding
8565 * no resolver locks. No one else will try to start it
8566 * because we're the ones who set res->priming to true.
8567 * Any other callers of dns_resolver_prime() while we're
8568 * running will see that res->priming is already true and
8572 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
8573 if (rdataset == NULL) {
8575 INSIST(res->priming);
8576 INSIST(res->primefetch == NULL);
8577 res->priming = ISC_FALSE;
8581 dns_rdataset_init(rdataset);
8582 LOCK(&res->primelock);
8583 result = dns_resolver_createfetch(res, dns_rootname,
8585 NULL, NULL, NULL, 0,
8586 res->buckets[0].task,
8588 res, rdataset, NULL,
8590 UNLOCK(&res->primelock);
8591 if (result != ISC_R_SUCCESS) {
8593 INSIST(res->priming);
8594 res->priming = ISC_FALSE;
8602 dns_resolver_freeze(dns_resolver_t *res) {
8607 REQUIRE(VALID_RESOLVER(res));
8609 res->frozen = ISC_TRUE;
8613 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
8614 REQUIRE(VALID_RESOLVER(source));
8615 REQUIRE(targetp != NULL && *targetp == NULL);
8617 RRTRACE(source, "attach");
8618 LOCK(&source->lock);
8619 REQUIRE(!source->exiting);
8621 INSIST(source->references > 0);
8622 source->references++;
8623 INSIST(source->references != 0);
8624 UNLOCK(&source->lock);
8630 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
8631 isc_event_t **eventp)
8636 REQUIRE(VALID_RESOLVER(res));
8637 REQUIRE(eventp != NULL);
8644 if (res->exiting && res->activebuckets == 0) {
8646 * We're already shutdown. Send the event.
8648 event->ev_sender = res;
8649 isc_task_send(task, &event);
8652 isc_task_attach(task, &clone);
8653 event->ev_sender = clone;
8654 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
8661 dns_resolver_shutdown(dns_resolver_t *res) {
8664 isc_result_t result;
8666 REQUIRE(VALID_RESOLVER(res));
8672 if (!res->exiting) {
8674 res->exiting = ISC_TRUE;
8676 for (i = 0; i < res->nbuckets; i++) {
8677 LOCK(&res->buckets[i].lock);
8678 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
8680 fctx = ISC_LIST_NEXT(fctx, link))
8681 fctx_shutdown(fctx);
8682 if (res->dispatches4 != NULL && !res->exclusivev4) {
8683 dns_dispatchset_cancelall(res->dispatches4,
8684 res->buckets[i].task);
8686 if (res->dispatches6 != NULL && !res->exclusivev6) {
8687 dns_dispatchset_cancelall(res->dispatches6,
8688 res->buckets[i].task);
8690 res->buckets[i].exiting = ISC_TRUE;
8691 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
8692 INSIST(res->activebuckets > 0);
8693 res->activebuckets--;
8695 UNLOCK(&res->buckets[i].lock);
8697 if (res->activebuckets == 0)
8698 send_shutdown_events(res);
8699 result = isc_timer_reset(res->spillattimer,
8700 isc_timertype_inactive, NULL,
8702 RUNTIME_CHECK(result == ISC_R_SUCCESS);
8709 dns_resolver_detach(dns_resolver_t **resp) {
8710 dns_resolver_t *res;
8711 isc_boolean_t need_destroy = ISC_FALSE;
8713 REQUIRE(resp != NULL);
8715 REQUIRE(VALID_RESOLVER(res));
8721 INSIST(res->references > 0);
8723 if (res->references == 0) {
8724 INSIST(res->exiting && res->activebuckets == 0);
8725 need_destroy = ISC_TRUE;
8736 static inline isc_boolean_t
8737 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
8738 unsigned int options)
8741 * Don't match fetch contexts that are shutting down.
8743 if (fctx->cloned || fctx->state == fetchstate_done ||
8744 ISC_LIST_EMPTY(fctx->events))
8747 if (fctx->type != type || fctx->options != options)
8749 return (dns_name_equal(&fctx->name, name));
8753 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
8754 char namebuf[DNS_NAME_FORMATSIZE];
8755 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8756 int level = ISC_LOG_DEBUG(1);
8759 * If there's no chance of logging it, don't render (format) the
8760 * name and RDATA type (further below), and return early.
8762 if (! isc_log_wouldlog(dns_lctx, level))
8765 dns_name_format(name, namebuf, sizeof(namebuf));
8766 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
8768 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
8769 DNS_LOGMODULE_RESOLVER, level,
8770 "createfetch: %s %s", namebuf, typebuf);
8774 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
8775 dns_rdatatype_t type,
8776 dns_name_t *domain, dns_rdataset_t *nameservers,
8777 dns_forwarders_t *forwarders,
8778 unsigned int options, isc_task_t *task,
8779 isc_taskaction_t action, void *arg,
8780 dns_rdataset_t *rdataset,
8781 dns_rdataset_t *sigrdataset,
8782 dns_fetch_t **fetchp)
8784 return (dns_resolver_createfetch3(res, name, type, domain,
8785 nameservers, forwarders, NULL, 0,
8786 options, 0, NULL, task, action, arg,
8787 rdataset, sigrdataset, fetchp));
8791 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
8792 dns_rdatatype_t type,
8793 dns_name_t *domain, dns_rdataset_t *nameservers,
8794 dns_forwarders_t *forwarders,
8795 isc_sockaddr_t *client, dns_messageid_t id,
8796 unsigned int options, isc_task_t *task,
8797 isc_taskaction_t action, void *arg,
8798 dns_rdataset_t *rdataset,
8799 dns_rdataset_t *sigrdataset,
8800 dns_fetch_t **fetchp)
8802 return (dns_resolver_createfetch3(res, name, type, domain,
8803 nameservers, forwarders, client, id,
8804 options, 0, NULL, task, action, arg,
8805 rdataset, sigrdataset, fetchp));
8809 dns_resolver_createfetch3(dns_resolver_t *res, dns_name_t *name,
8810 dns_rdatatype_t type,
8811 dns_name_t *domain, dns_rdataset_t *nameservers,
8812 dns_forwarders_t *forwarders,
8813 isc_sockaddr_t *client, dns_messageid_t id,
8814 unsigned int options, unsigned int depth,
8815 isc_counter_t *qc, isc_task_t *task,
8816 isc_taskaction_t action, void *arg,
8817 dns_rdataset_t *rdataset,
8818 dns_rdataset_t *sigrdataset,
8819 dns_fetch_t **fetchp)
8822 fetchctx_t *fctx = NULL;
8823 isc_result_t result = ISC_R_SUCCESS;
8824 unsigned int bucketnum;
8825 isc_boolean_t new_fctx = ISC_FALSE;
8827 unsigned int count = 0;
8828 unsigned int spillat;
8829 unsigned int spillatmin;
8830 isc_boolean_t dodestroy = ISC_FALSE;
8834 REQUIRE(VALID_RESOLVER(res));
8835 REQUIRE(res->frozen);
8836 /* XXXRTH Check for meta type */
8837 if (domain != NULL) {
8838 REQUIRE(DNS_RDATASET_VALID(nameservers));
8839 REQUIRE(nameservers->type == dns_rdatatype_ns);
8841 REQUIRE(nameservers == NULL);
8842 REQUIRE(forwarders == NULL);
8843 REQUIRE(!dns_rdataset_isassociated(rdataset));
8844 REQUIRE(sigrdataset == NULL ||
8845 !dns_rdataset_isassociated(sigrdataset));
8846 REQUIRE(fetchp != NULL && *fetchp == NULL);
8848 log_fetch(name, type);
8851 * XXXRTH use a mempool?
8853 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
8855 return (ISC_R_NOMEMORY);
8857 isc_mem_attach(res->mctx, &fetch->mctx);
8859 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
8862 spillat = res->spillat;
8863 spillatmin = res->spillatmin;
8865 LOCK(&res->buckets[bucketnum].lock);
8867 if (res->buckets[bucketnum].exiting) {
8868 result = ISC_R_SHUTTINGDOWN;
8872 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
8873 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
8875 fctx = ISC_LIST_NEXT(fctx, link)) {
8876 if (fctx_match(fctx, name, type, options))
8882 * Is this a duplicate?
8884 if (fctx != NULL && client != NULL) {
8885 dns_fetchevent_t *fevent;
8886 for (fevent = ISC_LIST_HEAD(fctx->events);
8888 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
8889 if (fevent->client != NULL && fevent->id == id &&
8890 isc_sockaddr_equal(fevent->client, client)) {
8891 result = DNS_R_DUPLICATE;
8897 if (count >= spillatmin && spillatmin != 0) {
8898 INSIST(fctx != NULL);
8899 if (count >= spillat)
8900 fctx->spilled = ISC_TRUE;
8901 if (fctx->spilled) {
8902 result = DNS_R_DROP;
8908 result = fctx_create(res, name, type, domain, nameservers,
8909 options, bucketnum, depth, qc, &fctx);
8910 if (result != ISC_R_SUCCESS)
8912 new_fctx = ISC_TRUE;
8913 } else if (fctx->depth > depth)
8914 fctx->depth = depth;
8916 result = fctx_join(fctx, task, client, id, action, arg,
8917 rdataset, sigrdataset, fetch);
8919 if (result == ISC_R_SUCCESS) {
8923 event = &fctx->control_event;
8924 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
8925 DNS_EVENT_FETCHCONTROL,
8926 fctx_start, fctx, NULL,
8928 isc_task_send(res->buckets[bucketnum].task, &event);
8931 * We don't care about the result of fctx_unlink()
8932 * since we know we're not exiting.
8934 (void)fctx_unlink(fctx);
8935 dodestroy = ISC_TRUE;
8940 UNLOCK(&res->buckets[bucketnum].lock);
8945 if (result == ISC_R_SUCCESS) {
8949 isc_mem_putanddetach(&fetch->mctx, fetch, sizeof(*fetch));
8955 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
8957 dns_resolver_t *res;
8958 dns_fetchevent_t *event, *next_event;
8961 REQUIRE(DNS_FETCH_VALID(fetch));
8962 fctx = fetch->private;
8963 REQUIRE(VALID_FCTX(fctx));
8966 FTRACE("cancelfetch");
8968 LOCK(&res->buckets[fctx->bucketnum].lock);
8971 * Find the completion event for this fetch (as opposed
8972 * to those for other fetches that have joined the same
8973 * fctx) and send it with result = ISC_R_CANCELED.
8976 if (fctx->state != fetchstate_done) {
8977 for (event = ISC_LIST_HEAD(fctx->events);
8979 event = next_event) {
8980 next_event = ISC_LIST_NEXT(event, ev_link);
8981 if (event->fetch == fetch) {
8982 ISC_LIST_UNLINK(fctx->events, event, ev_link);
8987 if (event != NULL) {
8988 etask = event->ev_sender;
8989 event->ev_sender = fctx;
8990 event->result = ISC_R_CANCELED;
8991 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8994 * The fctx continues running even if no fetches remain;
8995 * the answer is still cached.
8998 UNLOCK(&res->buckets[fctx->bucketnum].lock);
9002 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
9004 dns_resolver_t *res;
9005 dns_fetchevent_t *event, *next_event;
9007 unsigned int bucketnum;
9008 isc_boolean_t bucket_empty;
9010 REQUIRE(fetchp != NULL);
9012 REQUIRE(DNS_FETCH_VALID(fetch));
9013 fctx = fetch->private;
9014 REQUIRE(VALID_FCTX(fctx));
9017 FTRACE("destroyfetch");
9019 bucketnum = fctx->bucketnum;
9020 LOCK(&res->buckets[bucketnum].lock);
9023 * Sanity check: the caller should have gotten its event before
9024 * trying to destroy the fetch.
9027 if (fctx->state != fetchstate_done) {
9028 for (event = ISC_LIST_HEAD(fctx->events);
9030 event = next_event) {
9031 next_event = ISC_LIST_NEXT(event, ev_link);
9032 RUNTIME_CHECK(event->fetch != fetch);
9036 bucket_empty = fctx_decreference(fctx);
9038 UNLOCK(&res->buckets[bucketnum].lock);
9040 isc_mem_putanddetach(&fetch->mctx, fetch, sizeof(*fetch));
9048 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
9049 isc_logcategory_t *category, isc_logmodule_t *module,
9050 int level, isc_boolean_t duplicateok)
9053 dns_resolver_t *res;
9054 char domainbuf[DNS_NAME_FORMATSIZE];
9056 REQUIRE(DNS_FETCH_VALID(fetch));
9057 fctx = fetch->private;
9058 REQUIRE(VALID_FCTX(fctx));
9061 LOCK(&res->buckets[fctx->bucketnum].lock);
9063 INSIST(fctx->exitline >= 0);
9064 if (!fctx->logged || duplicateok) {
9065 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
9066 isc_log_write(lctx, category, module, level,
9067 "fetch completed at %s:%d for %s in "
9068 "%" ISC_PRINT_QUADFORMAT "u."
9069 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
9070 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
9071 "timeout:%u,lame:%u,"
9072 #ifdef ENABLE_FETCHLIMIT
9074 #endif /* ENABLE_FETCHLIMIT */
9076 "badresp:%u,adberr:%u,findfail:%u,valfail:%u]",
9077 __FILE__, fctx->exitline, fctx->info,
9078 fctx->duration / US_PER_SEC,
9079 fctx->duration % US_PER_SEC,
9080 isc_result_totext(fctx->result),
9081 isc_result_totext(fctx->vresult), domainbuf,
9082 fctx->referrals, fctx->restarts,
9083 fctx->querysent, fctx->timeouts, fctx->lamecount,
9084 #ifdef ENABLE_FETCHLIMIT
9086 #endif /* ENABLE_FETCHLIMIT */
9087 fctx->neterr, fctx->badresp, fctx->adberr,
9088 fctx->findfail, fctx->valfail);
9089 fctx->logged = ISC_TRUE;
9092 UNLOCK(&res->buckets[fctx->bucketnum].lock);
9096 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
9097 REQUIRE(VALID_RESOLVER(resolver));
9098 return (resolver->dispatchmgr);
9102 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
9103 REQUIRE(VALID_RESOLVER(resolver));
9104 return (dns_dispatchset_get(resolver->dispatches4));
9108 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
9109 REQUIRE(VALID_RESOLVER(resolver));
9110 return (dns_dispatchset_get(resolver->dispatches6));
9114 dns_resolver_socketmgr(dns_resolver_t *resolver) {
9115 REQUIRE(VALID_RESOLVER(resolver));
9116 return (resolver->socketmgr);
9120 dns_resolver_taskmgr(dns_resolver_t *resolver) {
9121 REQUIRE(VALID_RESOLVER(resolver));
9122 return (resolver->taskmgr);
9126 dns_resolver_getlamettl(dns_resolver_t *resolver) {
9127 REQUIRE(VALID_RESOLVER(resolver));
9128 return (resolver->lame_ttl);
9132 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
9133 REQUIRE(VALID_RESOLVER(resolver));
9134 resolver->lame_ttl = lame_ttl;
9138 dns_resolver_nrunning(dns_resolver_t *resolver) {
9140 LOCK(&resolver->nlock);
9141 n = resolver->nfctx;
9142 UNLOCK(&resolver->nlock);
9147 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
9148 dns_name_t *name, in_port_t port) {
9150 isc_result_t result;
9152 REQUIRE(VALID_RESOLVER(resolver));
9153 REQUIRE(!resolver->frozen);
9154 REQUIRE((alt == NULL) ^ (name == NULL));
9156 a = isc_mem_get(resolver->mctx, sizeof(*a));
9158 return (ISC_R_NOMEMORY);
9160 a->isaddress = ISC_TRUE;
9163 a->isaddress = ISC_FALSE;
9164 a->_u._n.port = port;
9165 dns_name_init(&a->_u._n.name, NULL);
9166 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
9167 if (result != ISC_R_SUCCESS) {
9168 isc_mem_put(resolver->mctx, a, sizeof(*a));
9172 ISC_LINK_INIT(a, link);
9173 ISC_LIST_APPEND(resolver->alternates, a, link);
9175 return (ISC_R_SUCCESS);
9179 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
9180 REQUIRE(VALID_RESOLVER(resolver));
9181 resolver->udpsize = udpsize;
9185 dns_resolver_getudpsize(dns_resolver_t *resolver) {
9186 REQUIRE(VALID_RESOLVER(resolver));
9187 return (resolver->udpsize);
9191 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
9193 dns_badcache_t *bad, *prev, *next;
9196 * Drop all entries that match the name, and also all expired
9197 * entries from the badcache.
9200 REQUIRE(VALID_RESOLVER(resolver));
9202 LOCK(&resolver->lock);
9203 if (resolver->badcache == NULL)
9208 isc_result_t result;
9209 result = isc_time_now(&now);
9210 if (result != ISC_R_SUCCESS)
9211 isc_time_settoepoch(&now);
9212 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
9214 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
9217 n = isc_time_compare(&bad->expire, &now);
9218 if (n < 0 || dns_name_equal(name, &bad->name)) {
9220 resolver->badcache[i] = bad->next;
9222 prev->next = bad->next;
9223 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
9225 resolver->badcount--;
9230 destroy_badcache(resolver);
9233 UNLOCK(&resolver->lock);
9238 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
9239 unsigned int newsize;
9240 dns_badcache_t **new, *bad, *next;
9244 * The number of buckets in the hashtable is modified in this
9245 * function. Afterwards, all the entries are remapped into the
9246 * corresponding new slot. Rehashing (hash computation) is
9247 * unnecessary as the hash values had been saved.
9251 newsize = resolver->badhash * 2 + 1;
9253 newsize = (resolver->badhash - 1) / 2;
9255 new = isc_mem_get(resolver->mctx,
9256 sizeof(*resolver->badcache) * newsize);
9259 memset(new, 0, sizeof(*resolver->badcache) * newsize);
9262 * Because the hashtable implements a simple modulus mapping
9263 * from hash to bucket (no extendible hashing is used), every
9264 * name in the hashtable has to be remapped to its new slot.
9265 * Entries that have expired (time) are dropped.
9267 for (i = 0; i < resolver->badhash; i++) {
9268 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
9270 if (isc_time_compare(&bad->expire, now) < 0) {
9271 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
9273 resolver->badcount--;
9275 bad->next = new[bad->hashval % newsize];
9276 new[bad->hashval % newsize] = bad;
9280 isc_mem_put(resolver->mctx, resolver->badcache,
9281 sizeof(*resolver->badcache) * resolver->badhash);
9282 resolver->badhash = newsize;
9283 resolver->badcache = new;
9287 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
9288 dns_rdatatype_t type, isc_time_t *expire)
9291 isc_result_t result = ISC_R_SUCCESS;
9292 unsigned int i, hashval;
9293 dns_badcache_t *bad, *prev, *next;
9296 * The badcache is implemented as a hashtable keyed on the name,
9297 * and each bucket slot points to a linked list (separate
9300 * To avoid long list chains, if the number of entries in the
9301 * hashtable goes over number-of-buckets * 8, the
9302 * number-of-buckets is doubled. Similarly, if the number of
9303 * entries goes below number-of-buckets * 2, the number-of-buckets
9304 * is halved. See resizehash().
9307 REQUIRE(VALID_RESOLVER(resolver));
9309 LOCK(&resolver->lock);
9310 if (resolver->badcache == NULL) {
9311 resolver->badcache = isc_mem_get(resolver->mctx,
9312 sizeof(*resolver->badcache) *
9314 if (resolver->badcache == NULL)
9316 resolver->badhash = DNS_BADCACHE_SIZE;
9317 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
9321 result = isc_time_now(&now);
9322 if (result != ISC_R_SUCCESS)
9323 isc_time_settoepoch(&now);
9324 hashval = dns_name_hash(name, ISC_FALSE);
9325 i = hashval % resolver->badhash;
9327 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
9329 if (bad->type == type && dns_name_equal(name, &bad->name))
9331 /* Drop expired entries when walking the chain. */
9332 if (isc_time_compare(&bad->expire, &now) < 0) {
9334 resolver->badcache[i] = bad->next;
9336 prev->next = bad->next;
9337 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
9339 resolver->badcount--;
9345 * Insert the name into the badcache hashtable at the
9346 * head of the linked list at the appropriate slot. The
9347 * name data follows right after the allocation for the
9350 isc_buffer_t buffer;
9351 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
9355 bad->hashval = hashval;
9356 bad->expire = *expire;
9357 isc_buffer_init(&buffer, bad + 1, name->length);
9358 dns_name_init(&bad->name, NULL);
9359 dns_name_copy(name, &bad->name, &buffer);
9360 bad->next = resolver->badcache[i];
9361 resolver->badcache[i] = bad;
9362 resolver->badcount++;
9363 if (resolver->badcount > resolver->badhash * 8)
9364 resizehash(resolver, &now, ISC_TRUE);
9365 if (resolver->badcount < resolver->badhash * 2 &&
9366 resolver->badhash > DNS_BADCACHE_SIZE)
9367 resizehash(resolver, &now, ISC_FALSE);
9369 bad->expire = *expire;
9371 UNLOCK(&resolver->lock);
9375 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
9376 dns_rdatatype_t type, isc_time_t *now)
9378 dns_badcache_t *bad, *prev, *next;
9379 isc_boolean_t answer = ISC_FALSE;
9382 REQUIRE(VALID_RESOLVER(resolver));
9384 LOCK(&resolver->lock);
9385 if (resolver->badcache == NULL)
9388 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
9390 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
9393 * Search the hash list. Clean out expired records as we go.
9395 if (isc_time_compare(&bad->expire, now) < 0) {
9397 prev->next = bad->next;
9399 resolver->badcache[i] = bad->next;
9400 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
9402 resolver->badcount--;
9405 if (bad->type == type && dns_name_equal(name, &bad->name)) {
9413 * Slow sweep to clean out stale records.
9415 i = resolver->badsweep++ % resolver->badhash;
9416 bad = resolver->badcache[i];
9417 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
9418 resolver->badcache[i] = bad->next;
9419 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
9421 resolver->badcount--;
9425 UNLOCK(&resolver->lock);
9430 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
9431 char namebuf[DNS_NAME_FORMATSIZE];
9432 char typebuf[DNS_RDATATYPE_FORMATSIZE];
9433 dns_badcache_t *bad, *next, *prev;
9438 LOCK(&resolver->lock);
9439 fprintf(fp, ";\n; Bad cache\n;\n");
9441 if (resolver->badcache == NULL)
9445 for (i = 0; i < resolver->badhash; i++) {
9447 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
9449 if (isc_time_compare(&bad->expire, &now) < 0) {
9451 prev->next = bad->next;
9453 resolver->badcache[i] = bad->next;
9454 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
9456 resolver->badcount--;
9460 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
9461 dns_rdatatype_format(bad->type, typebuf,
9463 t = isc_time_microdiff(&bad->expire, &now);
9465 fprintf(fp, "; %s/%s [ttl "
9466 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
9467 namebuf, typebuf, t);
9472 UNLOCK(&resolver->lock);
9476 free_algorithm(void *node, void *arg) {
9477 unsigned char *algorithms = node;
9478 isc_mem_t *mctx = arg;
9480 isc_mem_put(mctx, algorithms, *algorithms);
9484 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
9486 REQUIRE(VALID_RESOLVER(resolver));
9489 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
9491 if (resolver->algorithms != NULL)
9492 dns_rbt_destroy(&resolver->algorithms);
9494 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
9499 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
9502 unsigned int len, mask;
9504 unsigned char *algorithms;
9505 isc_result_t result;
9506 dns_rbtnode_t *node = NULL;
9509 * Whether an algorithm is disabled (or not) is stored in a
9510 * per-name bitfield that is stored as the node data of an
9514 REQUIRE(VALID_RESOLVER(resolver));
9516 return (ISC_R_RANGE);
9519 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
9521 if (resolver->algorithms == NULL) {
9522 result = dns_rbt_create(resolver->mctx, free_algorithm,
9523 resolver->mctx, &resolver->algorithms);
9524 if (result != ISC_R_SUCCESS)
9529 mask = 1 << (alg%8);
9531 result = dns_rbt_addnode(resolver->algorithms, name, &node);
9533 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
9534 algorithms = node->data;
9536 * If algorithms is set, algorithms[0] contains its
9539 if (algorithms == NULL || len > *algorithms) {
9541 * If no bitfield exists in the node data, or if
9542 * it is not long enough, allocate a new
9543 * bitfield and copy the old (smaller) bitfield
9544 * into it if one exists.
9546 new = isc_mem_get(resolver->mctx, len);
9548 result = ISC_R_NOMEMORY;
9551 memset(new, 0, len);
9552 if (algorithms != NULL)
9553 memmove(new, algorithms, *algorithms);
9555 /* new[0] should contain the length of new. */
9558 /* Free the older bitfield. */
9559 if (algorithms != NULL)
9560 isc_mem_put(resolver->mctx, algorithms,
9563 algorithms[len-1] |= mask;
9565 result = ISC_R_SUCCESS;
9568 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
9574 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
9577 unsigned int len, mask;
9578 unsigned char *algorithms;
9580 isc_result_t result;
9581 isc_boolean_t found = ISC_FALSE;
9583 REQUIRE(VALID_RESOLVER(resolver));
9586 * DH is unsupported for DNSKEYs, see RFC 4034 sec. A.1.
9588 if ((alg == DST_ALG_DH) || (alg == DST_ALG_INDIRECT))
9592 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
9594 if (resolver->algorithms == NULL)
9596 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
9597 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
9599 mask = 1 << (alg%8);
9601 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
9606 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
9611 return (dst_algorithm_supported(alg));
9615 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
9618 return (dns_ds_digest_supported(digest));
9622 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
9624 REQUIRE(VALID_RESOLVER(resolver));
9627 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
9629 if (resolver->mustbesecure != NULL)
9630 dns_rbt_destroy(&resolver->mustbesecure);
9632 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
9636 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
9639 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
9640 isc_boolean_t value)
9642 isc_result_t result;
9644 REQUIRE(VALID_RESOLVER(resolver));
9647 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
9649 if (resolver->mustbesecure == NULL) {
9650 result = dns_rbt_create(resolver->mctx, NULL, NULL,
9651 &resolver->mustbesecure);
9652 if (result != ISC_R_SUCCESS)
9655 result = dns_rbt_addname(resolver->mustbesecure, name,
9656 value ? &yes : &no);
9659 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
9665 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
9667 isc_boolean_t value = ISC_FALSE;
9668 isc_result_t result;
9670 REQUIRE(VALID_RESOLVER(resolver));
9673 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
9675 if (resolver->mustbesecure == NULL)
9677 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
9678 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
9679 value = *(isc_boolean_t*)data;
9682 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
9688 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
9689 isc_uint32_t *min, isc_uint32_t *max)
9691 REQUIRE(VALID_RESOLVER(resolver));
9693 LOCK(&resolver->lock);
9695 *cur = resolver->spillat;
9697 *min = resolver->spillatmin;
9699 *max = resolver->spillatmax;
9700 UNLOCK(&resolver->lock);
9704 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
9707 REQUIRE(VALID_RESOLVER(resolver));
9709 LOCK(&resolver->lock);
9710 resolver->spillatmin = resolver->spillat = min;
9711 resolver->spillatmax = max;
9712 UNLOCK(&resolver->lock);
9716 dns_resolver_setfetchesperzone(dns_resolver_t *resolver, isc_uint32_t clients)
9718 #ifdef ENABLE_FETCHLIMIT
9719 REQUIRE(VALID_RESOLVER(resolver));
9721 LOCK(&resolver->lock);
9722 resolver->zspill = clients;
9723 UNLOCK(&resolver->lock);
9729 #endif /* !ENABLE_FETCHLIMIT */
9734 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
9735 REQUIRE(VALID_RESOLVER(resolver));
9737 return (resolver->zero_no_soa_ttl);
9741 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
9742 REQUIRE(VALID_RESOLVER(resolver));
9744 resolver->zero_no_soa_ttl = state;
9748 dns_resolver_getoptions(dns_resolver_t *resolver) {
9749 REQUIRE(VALID_RESOLVER(resolver));
9751 return (resolver->options);
9755 dns_resolver_gettimeout(dns_resolver_t *resolver) {
9756 REQUIRE(VALID_RESOLVER(resolver));
9758 return (resolver->query_timeout);
9762 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
9763 REQUIRE(VALID_RESOLVER(resolver));
9766 seconds = DEFAULT_QUERY_TIMEOUT;
9767 if (seconds > MAXIMUM_QUERY_TIMEOUT)
9768 seconds = MAXIMUM_QUERY_TIMEOUT;
9769 if (seconds < MINIMUM_QUERY_TIMEOUT)
9770 seconds = MINIMUM_QUERY_TIMEOUT;
9772 resolver->query_timeout = seconds;
9776 dns_resolver_setmaxdepth(dns_resolver_t *resolver, unsigned int maxdepth) {
9777 REQUIRE(VALID_RESOLVER(resolver));
9778 resolver->maxdepth = maxdepth;
9782 dns_resolver_getmaxdepth(dns_resolver_t *resolver) {
9783 REQUIRE(VALID_RESOLVER(resolver));
9784 return (resolver->maxdepth);
9788 dns_resolver_setmaxqueries(dns_resolver_t *resolver, unsigned int queries) {
9789 REQUIRE(VALID_RESOLVER(resolver));
9790 resolver->maxqueries = queries;
9794 dns_resolver_getmaxqueries(dns_resolver_t *resolver) {
9795 REQUIRE(VALID_RESOLVER(resolver));
9796 return (resolver->maxqueries);
9800 dns_resolver_dumpfetches(dns_resolver_t *resolver, FILE *fp) {
9801 #ifdef ENABLE_FETCHLIMIT
9804 REQUIRE(VALID_RESOLVER(resolver));
9805 REQUIRE(fp != NULL);
9807 for (i = 0; i < RES_DOMAIN_BUCKETS; i++) {
9809 LOCK(&resolver->dbuckets[i].lock);
9810 for (fc = ISC_LIST_HEAD(resolver->dbuckets[i].list);
9812 fc = ISC_LIST_NEXT(fc, link))
9814 dns_name_print(fc->domain, fp);
9815 fprintf(fp, ": %d active (%d spilled, %d allowed)\n",
9816 fc->count, fc->dropped, fc->allowed);
9818 UNLOCK(&resolver->dbuckets[i].lock);
9825 #endif /* !ENABLE_FETCHLIMIT */
9829 dns_resolver_setquotaresponse(dns_resolver_t *resolver,
9830 dns_quotatype_t which, isc_result_t resp)
9832 #ifdef ENABLE_FETCHLIMIT
9833 REQUIRE(VALID_RESOLVER(resolver));
9834 REQUIRE(which == dns_quotatype_zone || which == dns_quotatype_server);
9835 REQUIRE(resp == DNS_R_DROP || resp == DNS_R_SERVFAIL);
9837 resolver->quotaresp[which] = resp;
9844 #endif /* !ENABLE_FETCHLIMIT */
9848 dns_resolver_getquotaresponse(dns_resolver_t *resolver, dns_quotatype_t which)
9850 #ifdef ENABLE_FETCHLIMIT
9851 REQUIRE(VALID_RESOLVER(resolver));
9852 REQUIRE(which == dns_quotatype_zone || which == dns_quotatype_server);
9854 return (resolver->quotaresp[which]);
9859 return (ISC_R_NOTIMPLEMENTED);
9860 #endif /* !ENABLE_FETCHLIMIT */