2 * Copyright (C) 2004-2011 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.
18 /* $Id: resolver.c,v 1.428.6.7 2011-06-08 23:02:43 each Exp $ */
24 #include <isc/platform.h>
25 #include <isc/print.h>
26 #include <isc/string.h>
27 #include <isc/random.h>
29 #include <isc/stats.h>
30 #include <isc/timer.h>
35 #include <dns/cache.h>
37 #include <dns/dispatch.h>
39 #include <dns/events.h>
40 #include <dns/forward.h>
41 #include <dns/keytable.h>
43 #include <dns/message.h>
44 #include <dns/ncache.h>
45 #include <dns/opcode.h>
48 #include <dns/rcode.h>
49 #include <dns/rdata.h>
50 #include <dns/rdataclass.h>
51 #include <dns/rdatalist.h>
52 #include <dns/rdataset.h>
53 #include <dns/rdatastruct.h>
54 #include <dns/rdatatype.h>
55 #include <dns/resolver.h>
56 #include <dns/result.h>
57 #include <dns/rootns.h>
58 #include <dns/stats.h>
60 #include <dns/validator.h>
62 #define DNS_RESOLVER_TRACE
63 #ifdef DNS_RESOLVER_TRACE
64 #define RTRACE(m) isc_log_write(dns_lctx, \
65 DNS_LOGCATEGORY_RESOLVER, \
66 DNS_LOGMODULE_RESOLVER, \
68 "res %p: %s", res, (m))
69 #define RRTRACE(r, m) isc_log_write(dns_lctx, \
70 DNS_LOGCATEGORY_RESOLVER, \
71 DNS_LOGMODULE_RESOLVER, \
73 "res %p: %s", (r), (m))
74 #define FCTXTRACE(m) isc_log_write(dns_lctx, \
75 DNS_LOGCATEGORY_RESOLVER, \
76 DNS_LOGMODULE_RESOLVER, \
78 "fctx %p(%s'): %s", fctx, fctx->info, (m))
79 #define FCTXTRACE2(m1, m2) \
80 isc_log_write(dns_lctx, \
81 DNS_LOGCATEGORY_RESOLVER, \
82 DNS_LOGMODULE_RESOLVER, \
84 "fctx %p(%s): %s %s", \
85 fctx, fctx->info, (m1), (m2))
86 #define FTRACE(m) isc_log_write(dns_lctx, \
87 DNS_LOGCATEGORY_RESOLVER, \
88 DNS_LOGMODULE_RESOLVER, \
90 "fetch %p (fctx %p(%s)): %s", \
91 fetch, fetch->private, \
92 fetch->private->info, (m))
93 #define QTRACE(m) isc_log_write(dns_lctx, \
94 DNS_LOGCATEGORY_RESOLVER, \
95 DNS_LOGMODULE_RESOLVER, \
97 "resquery %p (fctx %p(%s)): %s", \
99 query->fctx->info, (m))
102 #define RRTRACE(r, m)
108 #ifndef DEFAULT_QUERY_TIMEOUT
109 #define DEFAULT_QUERY_TIMEOUT 30 /* The default time in seconds for the whole query to live. */
112 #ifndef MAXIMUM_QUERY_TIMEOUT
113 #define MAXIMUM_QUERY_TIMEOUT 30 /* The maximum time in seconds for the whole query to live. */
117 * Maximum EDNS0 input packet size.
119 #define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */
122 * This defines the maximum number of timeouts we will permit before we
123 * disable EDNS0 on the query.
125 #define MAX_EDNS0_TIMEOUTS 3
127 typedef struct fetchctx fetchctx_t;
129 typedef struct query {
130 /* Locked by task event serialization. */
134 dns_dispatchmgr_t * dispatchmgr;
135 dns_dispatch_t * dispatch;
136 isc_boolean_t exclusivesocket;
137 dns_adbaddrinfo_t * addrinfo;
138 isc_socket_t * tcpsocket;
141 dns_dispentry_t * dispentry;
142 ISC_LINK(struct query) link;
145 dns_tsigkey_t *tsigkey;
146 unsigned int options;
147 unsigned int attributes;
149 unsigned int connects;
150 unsigned char data[512];
153 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
154 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
156 #define RESQUERY_ATTR_CANCELED 0x02
158 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
159 #define RESQUERY_CANCELED(q) (((q)->attributes & \
160 RESQUERY_ATTR_CANCELED) != 0)
161 #define RESQUERY_SENDING(q) ((q)->sends > 0)
164 fetchstate_init = 0, /*%< Start event has not run yet. */
166 fetchstate_done /*%< FETCHDONE events posted. */
170 badns_unreachable = 0,
178 dns_resolver_t * res;
180 dns_rdatatype_t type;
181 unsigned int options;
182 unsigned int bucketnum;
184 /*% Locked by appropriate bucket lock. */
186 isc_boolean_t want_shutdown;
187 isc_boolean_t cloned;
188 isc_boolean_t spilled;
189 unsigned int references;
190 isc_event_t control_event;
191 ISC_LINK(struct fetchctx) link;
192 ISC_LIST(dns_fetchevent_t) events;
193 /*% Locked by task event serialization. */
195 dns_rdataset_t nameservers;
196 unsigned int attributes;
199 isc_interval_t interval;
200 dns_message_t * qmessage;
201 dns_message_t * rmessage;
202 ISC_LIST(resquery_t) queries;
203 dns_adbfindlist_t finds;
204 dns_adbfind_t * find;
205 dns_adbfindlist_t altfinds;
206 dns_adbfind_t * altfind;
207 dns_adbaddrinfolist_t forwaddrs;
208 dns_adbaddrinfolist_t altaddrs;
209 isc_sockaddrlist_t forwarders;
210 dns_fwdpolicy_t fwdpolicy;
211 isc_sockaddrlist_t bad;
212 isc_sockaddrlist_t edns;
213 isc_sockaddrlist_t edns512;
214 isc_sockaddrlist_t bad_edns;
215 dns_validator_t *validator;
216 ISC_LIST(dns_validator_t) validators;
221 * The number of events we're waiting for.
223 unsigned int pending;
226 * The number of times we've "restarted" the current
227 * nameserver set. This acts as a failsafe to prevent
228 * us from pounding constantly on a particular set of
229 * servers that, for whatever reason, are not giving
230 * us useful responses, but are responding in such a
231 * way that they are not marked "bad".
233 unsigned int restarts;
236 * The number of timeouts that have occurred since we
237 * last successfully received a response packet. This
238 * is used for EDNS0 black hole detection.
240 unsigned int timeouts;
243 * Look aside state for DS lookups.
246 dns_fetch_t * nsfetch;
247 dns_rdataset_t nsrrset;
250 * Number of queries that reference this context.
252 unsigned int nqueries;
255 * The reason to print when logging a successful
256 * response to a query.
261 * Random numbers to use for mixing up server addresses.
263 isc_uint32_t rand_buf;
264 isc_uint32_t rand_bits;
267 * Fetch-local statistics for detailed logging.
269 isc_result_t result; /*%< fetch result */
270 isc_result_t vresult; /*%< validation result */
273 isc_uint64_t duration;
274 isc_boolean_t logged;
275 unsigned int querysent;
276 unsigned int referrals;
277 unsigned int lamecount;
279 unsigned int badresp;
281 unsigned int findfail;
282 unsigned int valfail;
283 isc_boolean_t timeout;
284 dns_adbaddrinfo_t *addrinfo;
285 isc_sockaddr_t *client;
288 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
289 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
291 #define FCTX_ATTR_HAVEANSWER 0x0001
292 #define FCTX_ATTR_GLUING 0x0002
293 #define FCTX_ATTR_ADDRWAIT 0x0004
294 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
295 #define FCTX_ATTR_WANTCACHE 0x0010
296 #define FCTX_ATTR_WANTNCACHE 0x0020
297 #define FCTX_ATTR_NEEDEDNS0 0x0040
298 #define FCTX_ATTR_TRIEDFIND 0x0080
299 #define FCTX_ATTR_TRIEDALT 0x0100
301 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
303 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
305 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
307 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
309 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
310 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
311 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
312 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
313 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
316 dns_adbaddrinfo_t * addrinfo;
322 fetchctx_t * private;
325 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
326 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
328 typedef struct fctxbucket {
331 ISC_LIST(fetchctx_t) fctxs;
332 isc_boolean_t exiting;
336 typedef struct alternate {
337 isc_boolean_t isaddress;
345 ISC_LINK(struct alternate) link;
348 typedef struct dns_badcache dns_badcache_t;
349 struct dns_badcache {
350 dns_badcache_t * next;
351 dns_rdatatype_t type;
353 unsigned int hashval;
356 #define DNS_BADCACHE_SIZE 1021
357 #define DNS_BADCACHE_TTL(fctx) \
358 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
360 struct dns_resolver {
366 isc_mutex_t primelock;
367 dns_rdataclass_t rdclass;
368 isc_socketmgr_t * socketmgr;
369 isc_timermgr_t * timermgr;
370 isc_taskmgr_t * taskmgr;
372 isc_boolean_t frozen;
373 unsigned int options;
374 dns_dispatchmgr_t * dispatchmgr;
375 dns_dispatch_t * dispatchv4;
376 isc_boolean_t exclusivev4;
377 dns_dispatch_t * dispatchv6;
378 isc_boolean_t exclusivev6;
380 unsigned int nbuckets;
381 fctxbucket_t * buckets;
382 isc_uint32_t lame_ttl;
383 ISC_LIST(alternate_t) alternates;
384 isc_uint16_t udpsize;
386 isc_rwlock_t alglock;
388 dns_rbt_t * algorithms;
390 isc_rwlock_t mbslock;
392 dns_rbt_t * mustbesecure;
393 unsigned int spillatmax;
394 unsigned int spillatmin;
395 isc_timer_t * spillattimer;
396 isc_boolean_t zero_no_soa_ttl;
397 unsigned int query_timeout;
399 /* Locked by lock. */
400 unsigned int references;
401 isc_boolean_t exiting;
402 isc_eventlist_t whenshutdown;
403 unsigned int activebuckets;
404 isc_boolean_t priming;
405 unsigned int spillat; /* clients-per-query */
406 unsigned int nextdisp;
409 dns_badcache_t ** badcache;
410 unsigned int badcount;
411 unsigned int badhash;
412 unsigned int badsweep;
414 /* Locked by primelock. */
415 dns_fetch_t * primefetch;
416 /* Locked by nlock. */
420 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
421 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
424 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
425 * which we also use as an addrinfo flag.
427 #define FCTX_ADDRINFO_MARK 0x0001
428 #define FCTX_ADDRINFO_FORWARDER 0x1000
429 #define FCTX_ADDRINFO_TRIED 0x2000
430 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
432 #define ISFORWARDER(a) (((a)->flags & \
433 FCTX_ADDRINFO_FORWARDER) != 0)
434 #define TRIED(a) (((a)->flags & \
435 FCTX_ADDRINFO_TRIED) != 0)
437 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
438 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
440 static void destroy(dns_resolver_t *res);
441 static void empty_bucket(dns_resolver_t *res);
442 static isc_result_t resquery_send(resquery_t *query);
443 static void resquery_response(isc_task_t *task, isc_event_t *event);
444 static void resquery_connected(isc_task_t *task, isc_event_t *event);
445 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
446 isc_boolean_t badcache);
447 static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
448 static isc_result_t ncache_adderesult(dns_message_t *message,
449 dns_db_t *cache, dns_dbnode_t *node,
450 dns_rdatatype_t covers,
451 isc_stdtime_t now, dns_ttl_t maxttl,
452 isc_boolean_t optout,
453 dns_rdataset_t *ardataset,
454 isc_result_t *eresultp);
455 static void validated(isc_task_t *task, isc_event_t *event);
456 static void maybe_destroy(fetchctx_t *fctx);
457 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
458 isc_result_t reason, badnstype_t badtype);
461 * Increment resolver-related statistics counters.
464 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
465 if (res->view->resstats != NULL)
466 isc_stats_increment(res->view->resstats, counter);
470 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
471 dns_rdatatype_t type, dns_rdataset_t *rdataset,
472 dns_rdataset_t *sigrdataset, unsigned int valoptions,
475 dns_validator_t *validator = NULL;
476 dns_valarg_t *valarg;
479 valarg = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
482 return (ISC_R_NOMEMORY);
485 valarg->addrinfo = addrinfo;
487 if (!ISC_LIST_EMPTY(fctx->validators))
488 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
490 result = dns_validator_create(fctx->res->view, name, type, rdataset,
491 sigrdataset, fctx->rmessage,
492 valoptions, task, validated, valarg,
494 if (result == ISC_R_SUCCESS) {
495 inc_stats(fctx->res, dns_resstatscounter_val);
496 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
497 INSIST(fctx->validator == NULL);
498 fctx->validator = validator;
500 ISC_LIST_APPEND(fctx->validators, validator, link);
502 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
503 valarg, sizeof(*valarg));
508 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
509 dns_namereln_t namereln;
510 dns_rdata_rrsig_t rrsig;
511 dns_rdata_t rdata = DNS_RDATA_INIT;
516 for (result = dns_rdataset_first(rdataset);
517 result == ISC_R_SUCCESS;
518 result = dns_rdataset_next(rdataset)) {
519 dns_rdataset_current(rdataset, &rdata);
520 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
521 RUNTIME_CHECK(result == ISC_R_SUCCESS);
522 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
524 if (namereln == dns_namereln_subdomain)
526 dns_rdata_reset(&rdata);
532 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
534 dns_name_t *domain = &fctx->domain;
535 dns_rdataset_t *rdataset;
536 dns_rdatatype_t type;
538 isc_boolean_t keep_auth = ISC_FALSE;
540 if (message->rcode == dns_rcode_nxdomain)
544 * A DS RRset can appear anywhere in a zone, even for a delegation-only
545 * zone. So a response to an explicit query for this type should be
546 * excluded from delegation-only fixup.
548 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
549 * response to a query for these types can never violate the
550 * delegation-only assumption: if the query name is below a
551 * zone cut, the response should normally be a referral, which should
552 * be accepted; if the query name is below a zone cut but the server
553 * happens to have authority for the zone of the query name, the
554 * response is a (non-referral) answer. But this does not violate
555 * delegation-only because the query name must be in a different zone
556 * due to the "apex-only" nature of these types. Note that if the
557 * remote server happens to have authority for a child zone of a
558 * delegation-only zone, we may still incorrectly "fix" the response
559 * with NXDOMAIN for queries for other types. Unfortunately it's
560 * generally impossible to differentiate this case from violation of
561 * the delegation-only assumption. Once the resolver learns the
562 * correct zone cut, possibly via a separate query for an "apex-only"
563 * type, queries for other types will be resolved correctly.
565 * A query for type ANY will be accepted if it hits an exceptional
566 * type above in the answer section as it should be from a child
569 * Also accept answers with RRSIG records from the child zone.
570 * Direct queries for RRSIG records should not be answered from
574 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
575 (fctx->type == dns_rdatatype_ns ||
576 fctx->type == dns_rdatatype_ds ||
577 fctx->type == dns_rdatatype_soa ||
578 fctx->type == dns_rdatatype_any ||
579 fctx->type == dns_rdatatype_rrsig ||
580 fctx->type == dns_rdatatype_dnskey)) {
581 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
582 while (result == ISC_R_SUCCESS) {
584 dns_message_currentname(message, DNS_SECTION_ANSWER,
586 for (rdataset = ISC_LIST_HEAD(name->list);
588 rdataset = ISC_LIST_NEXT(rdataset, link)) {
589 if (!dns_name_equal(name, &fctx->name))
591 type = rdataset->type;
595 if (type == dns_rdatatype_rrsig &&
596 rrsig_fromchildzone(fctx, rdataset))
599 * Direct query for apex records or DS.
601 if (fctx->type == type &&
602 (type == dns_rdatatype_ds ||
603 type == dns_rdatatype_ns ||
604 type == dns_rdatatype_soa ||
605 type == dns_rdatatype_dnskey))
608 * Indirect query for apex records or DS.
610 if (fctx->type == dns_rdatatype_any &&
611 (type == dns_rdatatype_ns ||
612 type == dns_rdatatype_ds ||
613 type == dns_rdatatype_soa ||
614 type == dns_rdatatype_dnskey))
617 result = dns_message_nextname(message,
623 * A NODATA response to a DS query?
625 if (fctx->type == dns_rdatatype_ds &&
626 message->counts[DNS_SECTION_ANSWER] == 0)
629 /* Look for referral or indication of answer from child zone? */
630 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
633 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
634 while (result == ISC_R_SUCCESS) {
636 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
637 for (rdataset = ISC_LIST_HEAD(name->list);
639 rdataset = ISC_LIST_NEXT(rdataset, link)) {
640 type = rdataset->type;
641 if (type == dns_rdatatype_soa &&
642 dns_name_equal(name, domain))
643 keep_auth = ISC_TRUE;
645 if (type != dns_rdatatype_ns &&
646 type != dns_rdatatype_soa &&
647 type != dns_rdatatype_rrsig)
650 if (type == dns_rdatatype_rrsig) {
651 if (rrsig_fromchildzone(fctx, rdataset))
657 /* NS or SOA records. */
658 if (dns_name_equal(name, domain)) {
660 * If a query for ANY causes a negative
661 * response, we can be sure that this is
662 * an empty node. For other type of queries
663 * we cannot differentiate an empty node
664 * from a node that just doesn't have that
665 * type of record. We only accept the former
668 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
669 fctx->type == dns_rdatatype_any)
671 } else if (dns_name_issubdomain(name, domain)) {
672 /* Referral or answer from child zone. */
676 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
680 message->rcode = dns_rcode_nxdomain;
681 message->counts[DNS_SECTION_ANSWER] = 0;
683 message->counts[DNS_SECTION_AUTHORITY] = 0;
684 message->counts[DNS_SECTION_ADDITIONAL] = 0;
688 static inline isc_result_t
689 fctx_starttimer(fetchctx_t *fctx) {
691 * Start the lifetime timer for fctx.
693 * This is also used for stopping the idle timer; in that
694 * case we must purge events already posted to ensure that
695 * no further idle events are delivered.
697 return (isc_timer_reset(fctx->timer, isc_timertype_once,
698 &fctx->expires, NULL, ISC_TRUE));
702 fctx_stoptimer(fetchctx_t *fctx) {
706 * We don't return a result if resetting the timer to inactive fails
707 * since there's nothing to be done about it. Resetting to inactive
708 * should never fail anyway, since the code as currently written
709 * cannot fail in that case.
711 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
712 NULL, NULL, ISC_TRUE);
713 if (result != ISC_R_SUCCESS) {
714 UNEXPECTED_ERROR(__FILE__, __LINE__,
715 "isc_timer_reset(): %s",
716 isc_result_totext(result));
721 static inline isc_result_t
722 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
724 * Start the idle timer for fctx. The lifetime timer continues
727 return (isc_timer_reset(fctx->timer, isc_timertype_once,
728 &fctx->expires, interval, ISC_FALSE));
732 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
733 * we use fctx_stopidletimer for readability in the code below.
735 #define fctx_stopidletimer fctx_starttimer
739 resquery_destroy(resquery_t **queryp) {
742 REQUIRE(queryp != NULL);
744 REQUIRE(!ISC_LINK_LINKED(query, link));
746 INSIST(query->tcpsocket == NULL);
748 query->fctx->nqueries--;
749 if (SHUTTINGDOWN(query->fctx))
750 maybe_destroy(query->fctx); /* Locks bucket. */
752 isc_mem_put(query->mctx, query, sizeof(*query));
757 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
758 isc_time_t *finish, isc_boolean_t no_response)
762 unsigned int rtt, rttms;
765 dns_adbaddrinfo_t *addrinfo;
766 isc_socket_t *socket;
771 FCTXTRACE("cancelquery");
773 REQUIRE(!RESQUERY_CANCELED(query));
775 query->attributes |= RESQUERY_ATTR_CANCELED;
778 * Should we update the RTT?
780 if (finish != NULL || no_response) {
781 if (finish != NULL) {
783 * We have both the start and finish times for this
784 * packet, so we can compute a real RTT.
786 rtt = (unsigned int)isc_time_microdiff(finish,
788 factor = DNS_ADB_RTTADJDEFAULT;
791 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
793 dns_resstatscounter_queryrtt0);
794 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
796 dns_resstatscounter_queryrtt1);
797 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
799 dns_resstatscounter_queryrtt2);
800 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
802 dns_resstatscounter_queryrtt3);
803 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
805 dns_resstatscounter_queryrtt4);
808 dns_resstatscounter_queryrtt5);
812 * We don't have an RTT for this query. Maybe the
813 * packet was lost, or maybe this server is very
814 * slow. We don't know. Increase the RTT.
817 rtt = query->addrinfo->srtt + 200000;
821 * Replace the current RTT with our value.
823 factor = DNS_ADB_RTTADJREPLACE;
825 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
828 /* Remember that the server has been tried. */
829 if (!TRIED(query->addrinfo)) {
830 dns_adb_changeflags(fctx->adb, query->addrinfo,
831 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
835 * Age RTTs of servers not tried.
837 factor = DNS_ADB_RTTADJAGE;
839 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
841 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
842 if (UNMARKED(addrinfo))
843 dns_adb_adjustsrtt(fctx->adb, addrinfo,
846 if (finish != NULL && TRIEDFIND(fctx))
847 for (find = ISC_LIST_HEAD(fctx->finds);
849 find = ISC_LIST_NEXT(find, publink))
850 for (addrinfo = ISC_LIST_HEAD(find->list);
852 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
853 if (UNMARKED(addrinfo))
854 dns_adb_adjustsrtt(fctx->adb, addrinfo,
857 if (finish != NULL && TRIEDALT(fctx)) {
858 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
860 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
861 if (UNMARKED(addrinfo))
862 dns_adb_adjustsrtt(fctx->adb, addrinfo,
864 for (find = ISC_LIST_HEAD(fctx->altfinds);
866 find = ISC_LIST_NEXT(find, publink))
867 for (addrinfo = ISC_LIST_HEAD(find->list);
869 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
870 if (UNMARKED(addrinfo))
871 dns_adb_adjustsrtt(fctx->adb, addrinfo,
876 * Check for any outstanding socket events. If they exist, cancel
877 * them and let the event handlers finish the cleanup. The resolver
878 * only needs to worry about managing the connect and send events;
879 * the dispatcher manages the recv events.
881 if (RESQUERY_CONNECTING(query)) {
883 * Cancel the connect.
885 if (query->tcpsocket != NULL) {
886 isc_socket_cancel(query->tcpsocket, NULL,
887 ISC_SOCKCANCEL_CONNECT);
888 } else if (query->dispentry != NULL) {
889 INSIST(query->exclusivesocket);
890 socket = dns_dispatch_getentrysocket(query->dispentry);
892 isc_socket_cancel(socket, NULL,
893 ISC_SOCKCANCEL_CONNECT);
895 } else if (RESQUERY_SENDING(query)) {
897 * Cancel the pending send.
899 if (query->exclusivesocket && query->dispentry != NULL)
900 socket = dns_dispatch_getentrysocket(query->dispentry);
902 socket = dns_dispatch_getsocket(query->dispatch);
904 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
907 if (query->dispentry != NULL)
908 dns_dispatch_removeresponse(&query->dispentry, deventp);
910 ISC_LIST_UNLINK(fctx->queries, query, link);
912 if (query->tsig != NULL)
913 isc_buffer_free(&query->tsig);
915 if (query->tsigkey != NULL)
916 dns_tsigkey_detach(&query->tsigkey);
918 if (query->dispatch != NULL)
919 dns_dispatch_detach(&query->dispatch);
921 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
923 * It's safe to destroy the query now.
925 resquery_destroy(&query);
929 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
930 resquery_t *query, *next_query;
932 FCTXTRACE("cancelqueries");
934 for (query = ISC_LIST_HEAD(fctx->queries);
936 query = next_query) {
937 next_query = ISC_LIST_NEXT(query, link);
938 fctx_cancelquery(&query, NULL, NULL, no_response);
943 fctx_cleanupfinds(fetchctx_t *fctx) {
944 dns_adbfind_t *find, *next_find;
946 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
948 for (find = ISC_LIST_HEAD(fctx->finds);
951 next_find = ISC_LIST_NEXT(find, publink);
952 ISC_LIST_UNLINK(fctx->finds, find, publink);
953 dns_adb_destroyfind(&find);
959 fctx_cleanupaltfinds(fetchctx_t *fctx) {
960 dns_adbfind_t *find, *next_find;
962 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
964 for (find = ISC_LIST_HEAD(fctx->altfinds);
967 next_find = ISC_LIST_NEXT(find, publink);
968 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
969 dns_adb_destroyfind(&find);
971 fctx->altfind = NULL;
975 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
976 dns_adbaddrinfo_t *addr, *next_addr;
978 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
980 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
983 next_addr = ISC_LIST_NEXT(addr, publink);
984 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
985 dns_adb_freeaddrinfo(fctx->adb, &addr);
990 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
991 dns_adbaddrinfo_t *addr, *next_addr;
993 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
995 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
998 next_addr = ISC_LIST_NEXT(addr, publink);
999 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1000 dns_adb_freeaddrinfo(fctx->adb, &addr);
1005 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1006 FCTXTRACE("stopeverything");
1007 fctx_cancelqueries(fctx, no_response);
1008 fctx_cleanupfinds(fctx);
1009 fctx_cleanupaltfinds(fctx);
1010 fctx_cleanupforwaddrs(fctx);
1011 fctx_cleanupaltaddrs(fctx);
1012 fctx_stoptimer(fctx);
1016 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1017 dns_fetchevent_t *event, *next_event;
1019 unsigned int count = 0;
1021 isc_boolean_t logit = ISC_FALSE;
1023 unsigned int old_spillat;
1024 unsigned int new_spillat = 0; /* initialized to silence
1025 compiler warnings */
1028 * Caller must be holding the appropriate bucket lock.
1030 REQUIRE(fctx->state == fetchstate_done);
1032 FCTXTRACE("sendevents");
1035 * Keep some record of fetch result for logging later (if required).
1037 fctx->result = result;
1038 fctx->exitline = line;
1040 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1042 for (event = ISC_LIST_HEAD(fctx->events);
1044 event = next_event) {
1045 next_event = ISC_LIST_NEXT(event, ev_link);
1046 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1047 task = event->ev_sender;
1048 event->ev_sender = fctx;
1049 event->vresult = fctx->vresult;
1050 if (!HAVE_ANSWER(fctx))
1051 event->result = result;
1053 INSIST(result != ISC_R_SUCCESS ||
1054 dns_rdataset_isassociated(event->rdataset) ||
1055 fctx->type == dns_rdatatype_any ||
1056 fctx->type == dns_rdatatype_rrsig ||
1057 fctx->type == dns_rdatatype_sig);
1060 * Negative results must be indicated in event->result.
1062 if (dns_rdataset_isassociated(event->rdataset) &&
1063 NEGATIVE(event->rdataset)) {
1064 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1065 event->result == DNS_R_NCACHENXRRSET);
1068 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1072 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1074 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1075 LOCK(&fctx->res->lock);
1076 if (count == fctx->res->spillat && !fctx->res->exiting) {
1077 old_spillat = fctx->res->spillat;
1078 fctx->res->spillat += 5;
1079 if (fctx->res->spillat > fctx->res->spillatmax &&
1080 fctx->res->spillatmax != 0)
1081 fctx->res->spillat = fctx->res->spillatmax;
1082 new_spillat = fctx->res->spillat;
1083 if (new_spillat != old_spillat) {
1086 isc_interval_set(&i, 20 * 60, 0);
1087 result = isc_timer_reset(fctx->res->spillattimer,
1088 isc_timertype_ticker, NULL,
1090 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1092 UNLOCK(&fctx->res->lock);
1094 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1095 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1096 "clients-per-query increased to %u",
1102 log_edns(fetchctx_t *fctx) {
1103 char domainbuf[DNS_NAME_FORMATSIZE];
1105 if (fctx->reason == NULL)
1108 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1109 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1110 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1111 "success resolving '%s' (in '%s'?) after %s",
1112 fctx->info, domainbuf, fctx->reason);
1114 fctx->reason = NULL;
1118 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1119 dns_resolver_t *res;
1120 isc_boolean_t no_response;
1128 if (result == ISC_R_SUCCESS) {
1130 * Log any deferred EDNS timeout messages.
1133 no_response = ISC_TRUE;
1135 no_response = ISC_FALSE;
1137 fctx->reason = NULL;
1138 fctx_stopeverything(fctx, no_response);
1140 LOCK(&res->buckets[fctx->bucketnum].lock);
1142 fctx->state = fetchstate_done;
1143 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1144 fctx_sendevents(fctx, result, line);
1146 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1150 process_sendevent(resquery_t *query, isc_event_t *event) {
1151 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1152 isc_boolean_t retry = ISC_FALSE;
1153 isc_result_t result;
1158 if (RESQUERY_CANCELED(query)) {
1159 if (query->sends == 0 && query->connects == 0) {
1161 * This query was canceled while the
1162 * isc_socket_sendto/connect() was in progress.
1164 if (query->tcpsocket != NULL)
1165 isc_socket_detach(&query->tcpsocket);
1166 resquery_destroy(&query);
1169 switch (sevent->result) {
1173 case ISC_R_HOSTUNREACH:
1174 case ISC_R_NETUNREACH:
1176 case ISC_R_ADDRNOTAVAIL:
1177 case ISC_R_CONNREFUSED:
1180 * No route to remote.
1182 add_bad(fctx, query->addrinfo, sevent->result,
1184 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1189 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1194 isc_event_free(&event);
1198 * Behave as if the idle timer has expired. For TCP
1199 * this may not actually reflect the latest timer.
1201 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1202 result = fctx_stopidletimer(fctx);
1203 if (result != ISC_R_SUCCESS)
1204 fctx_done(fctx, result, __LINE__);
1206 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1211 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1212 resquery_t *query = event->ev_arg;
1214 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1216 QTRACE("udpconnected");
1220 INSIST(RESQUERY_CONNECTING(query));
1224 process_sendevent(query, event);
1228 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1229 resquery_t *query = event->ev_arg;
1231 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1238 * Currently we don't wait for the senddone event before retrying
1239 * a query. This means that if we get really behind, we may end
1240 * up doing extra work!
1245 INSIST(RESQUERY_SENDING(query));
1249 process_sendevent(query, event);
1252 static inline isc_result_t
1253 fctx_addopt(dns_message_t *message, unsigned int version,
1254 isc_uint16_t udpsize, isc_boolean_t request_nsid)
1256 dns_rdataset_t *rdataset;
1257 dns_rdatalist_t *rdatalist;
1259 isc_result_t result;
1262 result = dns_message_gettemprdatalist(message, &rdatalist);
1263 if (result != ISC_R_SUCCESS)
1266 result = dns_message_gettemprdata(message, &rdata);
1267 if (result != ISC_R_SUCCESS)
1270 result = dns_message_gettemprdataset(message, &rdataset);
1271 if (result != ISC_R_SUCCESS)
1273 dns_rdataset_init(rdataset);
1275 rdatalist->type = dns_rdatatype_opt;
1276 rdatalist->covers = 0;
1279 * Set Maximum UDP buffer size.
1281 rdatalist->rdclass = udpsize;
1284 * Set EXTENDED-RCODE and Z to 0, DO to 1.
1286 rdatalist->ttl = (version << 16);
1287 rdatalist->ttl |= DNS_MESSAGEEXTFLAG_DO;
1290 * Set EDNS options if applicable
1293 /* Send empty NSID option (RFC5001) */
1294 unsigned char data[4];
1297 isc_buffer_init(&buf, data, sizeof(data));
1298 isc_buffer_putuint16(&buf, DNS_OPT_NSID);
1299 isc_buffer_putuint16(&buf, 0);
1301 rdata->length = sizeof(data);
1307 rdata->rdclass = rdatalist->rdclass;
1308 rdata->type = rdatalist->type;
1311 ISC_LIST_INIT(rdatalist->rdata);
1312 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1313 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset) == ISC_R_SUCCESS);
1315 return (dns_message_setopt(message, rdataset));
1319 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1320 unsigned int seconds;
1324 * We retry every .8 seconds the first two times through the address
1325 * list, and then we do exponential back-off.
1327 if (fctx->restarts < 3)
1330 us = (800000 << (fctx->restarts - 2));
1333 * Double the round-trip time.
1338 * Always wait for at least the doubled round-trip time.
1344 * But don't ever wait for more than 10 seconds.
1349 seconds = us / 1000000;
1350 us -= seconds * 1000000;
1351 isc_interval_set(&fctx->interval, seconds, us * 1000);
1355 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1356 unsigned int options)
1358 dns_resolver_t *res;
1360 isc_result_t result;
1362 isc_sockaddr_t addr;
1363 isc_boolean_t have_addr = ISC_FALSE;
1369 task = res->buckets[fctx->bucketnum].task;
1371 srtt = addrinfo->srtt;
1372 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1375 fctx_setretryinterval(fctx, srtt);
1376 result = fctx_startidletimer(fctx, &fctx->interval);
1377 if (result != ISC_R_SUCCESS)
1380 INSIST(ISC_LIST_EMPTY(fctx->validators));
1382 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1384 query = isc_mem_get(res->buckets[fctx->bucketnum].mctx,
1386 if (query == NULL) {
1387 result = ISC_R_NOMEMORY;
1388 goto stop_idle_timer;
1390 query->mctx = res->buckets[fctx->bucketnum].mctx;
1391 query->options = options;
1392 query->attributes = 0;
1394 query->connects = 0;
1396 * Note that the caller MUST guarantee that 'addrinfo' will remain
1397 * valid until this query is canceled.
1399 query->addrinfo = addrinfo;
1400 TIME_NOW(&query->start);
1403 * If this is a TCP query, then we need to make a socket and
1404 * a dispatch for it here. Otherwise we use the resolver's
1407 query->dispatchmgr = res->dispatchmgr;
1408 query->dispatch = NULL;
1409 query->exclusivesocket = ISC_FALSE;
1410 query->tcpsocket = NULL;
1411 if (res->view->peers != NULL) {
1412 dns_peer_t *peer = NULL;
1413 isc_netaddr_t dstip;
1414 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1415 result = dns_peerlist_peerbyaddr(res->view->peers,
1417 if (result == ISC_R_SUCCESS) {
1418 result = dns_peer_getquerysource(peer, &addr);
1419 if (result == ISC_R_SUCCESS)
1420 have_addr = ISC_TRUE;
1424 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1427 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1432 dns_dispatch_getlocaladdress(res->dispatchv4,
1437 dns_dispatch_getlocaladdress(res->dispatchv6,
1441 result = ISC_R_NOTIMPLEMENTED;
1444 if (result != ISC_R_SUCCESS)
1447 isc_sockaddr_setport(&addr, 0);
1449 result = isc_socket_create(res->socketmgr, pf,
1452 if (result != ISC_R_SUCCESS)
1455 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1456 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1457 if (result != ISC_R_SUCCESS)
1458 goto cleanup_socket;
1462 * A dispatch will be created once the connect succeeds.
1466 unsigned int attrs, attrmask;
1467 attrs = DNS_DISPATCHATTR_UDP;
1468 switch (isc_sockaddr_pf(&addr)) {
1470 attrs |= DNS_DISPATCHATTR_IPV4;
1473 attrs |= DNS_DISPATCHATTR_IPV6;
1476 result = ISC_R_NOTIMPLEMENTED;
1479 attrmask = DNS_DISPATCHATTR_UDP;
1480 attrmask |= DNS_DISPATCHATTR_TCP;
1481 attrmask |= DNS_DISPATCHATTR_IPV4;
1482 attrmask |= DNS_DISPATCHATTR_IPV6;
1483 result = dns_dispatch_getudp(res->dispatchmgr,
1485 res->taskmgr, &addr,
1486 4096, 1000, 32768, 16411,
1487 16433, attrs, attrmask,
1489 if (result != ISC_R_SUCCESS)
1492 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1494 dns_dispatch_attach(res->dispatchv4,
1496 query->exclusivesocket = res->exclusivev4;
1499 dns_dispatch_attach(res->dispatchv6,
1501 query->exclusivesocket = res->exclusivev6;
1504 result = ISC_R_NOTIMPLEMENTED;
1509 * We should always have a valid dispatcher here. If we
1510 * don't support a protocol family, then its dispatcher
1511 * will be NULL, but we shouldn't be finding addresses for
1512 * protocol types we don't support, so the dispatcher
1513 * we found should never be NULL.
1515 INSIST(query->dispatch != NULL);
1518 query->dispentry = NULL;
1521 query->tsigkey = NULL;
1522 ISC_LINK_INIT(query, link);
1523 query->magic = QUERY_MAGIC;
1525 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1527 * Connect to the remote server.
1529 * XXXRTH Should we attach to the socket?
1531 result = isc_socket_connect(query->tcpsocket,
1532 &addrinfo->sockaddr, task,
1533 resquery_connected, query);
1534 if (result != ISC_R_SUCCESS)
1535 goto cleanup_socket;
1537 QTRACE("connecting via TCP");
1539 result = resquery_send(query);
1540 if (result != ISC_R_SUCCESS)
1541 goto cleanup_dispatch;
1545 ISC_LIST_APPEND(fctx->queries, query, link);
1546 query->fctx->nqueries++;
1547 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1548 inc_stats(res, dns_resstatscounter_queryv4);
1550 inc_stats(res, dns_resstatscounter_queryv6);
1551 if (res->view->resquerystats != NULL)
1552 dns_rdatatypestats_increment(res->view->resquerystats,
1555 return (ISC_R_SUCCESS);
1558 isc_socket_detach(&query->tcpsocket);
1561 if (query->dispatch != NULL)
1562 dns_dispatch_detach(&query->dispatch);
1566 isc_mem_put(res->buckets[fctx->bucketnum].mctx,
1567 query, sizeof(*query));
1570 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1575 static isc_boolean_t
1576 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1579 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1581 sa = ISC_LIST_NEXT(sa, link)) {
1582 if (isc_sockaddr_equal(sa, address))
1590 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1593 if (bad_edns(fctx, address))
1596 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1602 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1605 static isc_boolean_t
1606 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1609 for (sa = ISC_LIST_HEAD(fctx->edns);
1611 sa = ISC_LIST_NEXT(sa, link)) {
1612 if (isc_sockaddr_equal(sa, address))
1620 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1623 if (triededns(fctx, address))
1626 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1632 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1635 static isc_boolean_t
1636 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1639 for (sa = ISC_LIST_HEAD(fctx->edns512);
1641 sa = ISC_LIST_NEXT(sa, link)) {
1642 if (isc_sockaddr_equal(sa, address))
1650 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1653 if (triededns512(fctx, address))
1656 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1662 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1666 resquery_send(resquery_t *query) {
1668 isc_result_t result;
1669 dns_name_t *qname = NULL;
1670 dns_rdataset_t *qrdataset = NULL;
1672 dns_resolver_t *res;
1674 isc_socket_t *socket;
1675 isc_buffer_t tcpbuffer;
1676 isc_sockaddr_t *address;
1677 isc_buffer_t *buffer;
1678 isc_netaddr_t ipaddr;
1679 dns_tsigkey_t *tsigkey = NULL;
1680 dns_peer_t *peer = NULL;
1681 isc_boolean_t useedns;
1682 dns_compress_t cctx;
1683 isc_boolean_t cleanup_cctx = ISC_FALSE;
1684 isc_boolean_t secure_domain;
1690 task = res->buckets[fctx->bucketnum].task;
1693 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1695 * Reserve space for the TCP message length.
1697 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1698 isc_buffer_init(&query->buffer, query->data + 2,
1699 sizeof(query->data) - 2);
1700 buffer = &tcpbuffer;
1702 isc_buffer_init(&query->buffer, query->data,
1703 sizeof(query->data));
1704 buffer = &query->buffer;
1707 result = dns_message_gettempname(fctx->qmessage, &qname);
1708 if (result != ISC_R_SUCCESS)
1710 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1711 if (result != ISC_R_SUCCESS)
1715 * Get a query id from the dispatch.
1717 result = dns_dispatch_addresponse2(query->dispatch,
1718 &query->addrinfo->sockaddr,
1725 if (result != ISC_R_SUCCESS)
1728 fctx->qmessage->opcode = dns_opcode_query;
1733 dns_name_init(qname, NULL);
1734 dns_name_clone(&fctx->name, qname);
1735 dns_rdataset_init(qrdataset);
1736 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1737 ISC_LIST_APPEND(qname->list, qrdataset, link);
1738 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1743 * Set RD if the client has requested that we do a recursive query,
1744 * or if we're sending to a forwarder.
1746 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1747 ISFORWARDER(query->addrinfo))
1748 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1751 * Set CD if the client says don't validate or the question is
1752 * under a secure entry point.
1754 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1755 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1756 } else if (res->view->enablevalidation) {
1757 result = dns_view_issecuredomain(res->view, &fctx->name,
1759 if (result != ISC_R_SUCCESS)
1760 secure_domain = ISC_FALSE;
1761 if (res->view->dlv != NULL)
1762 secure_domain = ISC_TRUE;
1764 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1768 * We don't have to set opcode because it defaults to query.
1770 fctx->qmessage->id = query->id;
1773 * Convert the question to wire format.
1775 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1776 if (result != ISC_R_SUCCESS)
1777 goto cleanup_message;
1778 cleanup_cctx = ISC_TRUE;
1780 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1782 if (result != ISC_R_SUCCESS)
1783 goto cleanup_message;
1785 result = dns_message_rendersection(fctx->qmessage,
1786 DNS_SECTION_QUESTION, 0);
1787 if (result != ISC_R_SUCCESS)
1788 goto cleanup_message;
1791 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1792 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1795 * The ADB does not know about servers with "edns no". Check this,
1796 * and then inform the ADB for future use.
1798 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1800 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1803 query->options |= DNS_FETCHOPT_NOEDNS0;
1804 dns_adb_changeflags(fctx->adb, query->addrinfo,
1805 DNS_FETCHOPT_NOEDNS0,
1806 DNS_FETCHOPT_NOEDNS0);
1809 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1810 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1811 query->options |= DNS_FETCHOPT_NOEDNS0;
1814 * Handle timeouts by reducing the UDP response size to 512 bytes
1815 * then if that doesn't work disabling EDNS (includes DO) and CD.
1817 * These timeout can be due to:
1818 * * broken nameservers that don't respond to EDNS queries.
1819 * * broken/misconfigured firewalls and NAT implementations
1820 * that don't handle IP fragmentation.
1821 * * broken/misconfigured firewalls that don't handle responses
1822 * greater than 512 bytes.
1823 * * broken/misconfigured firewalls that don't handle EDNS, DO
1825 * * packet loss / link outage.
1827 if (fctx->timeout) {
1828 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1829 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1830 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1831 query->options |= DNS_FETCHOPT_NOEDNS0;
1832 fctx->reason = "disabling EDNS";
1833 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1834 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1835 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1836 query->options |= DNS_FETCHOPT_EDNS512;
1837 fctx->reason = "reducing the advertised EDNS UDP "
1838 "packet size to 512 octets";
1840 fctx->timeout = ISC_FALSE;
1844 * Use EDNS0, unless the caller doesn't want it, or we know that
1845 * the remote server doesn't like it.
1847 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1848 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1849 unsigned int version = 0; /* Default version. */
1851 isc_uint16_t udpsize = res->udpsize;
1852 isc_boolean_t reqnsid = res->view->requestnsid;
1854 flags = query->addrinfo->flags;
1855 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1856 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1857 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1859 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1861 else if (peer != NULL)
1862 (void)dns_peer_getudpsize(peer, &udpsize);
1864 /* request NSID for current view or peer? */
1866 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1867 result = fctx_addopt(fctx->qmessage, version,
1869 if (reqnsid && result == ISC_R_SUCCESS) {
1870 query->options |= DNS_FETCHOPT_WANTNSID;
1871 } else if (result != ISC_R_SUCCESS) {
1873 * We couldn't add the OPT, but we'll press on.
1874 * We're not using EDNS0, so set the NOEDNS0
1877 query->options |= DNS_FETCHOPT_NOEDNS0;
1881 * We know this server doesn't like EDNS0, so we
1882 * won't use it. Set the NOEDNS0 bit since we're
1885 query->options |= DNS_FETCHOPT_NOEDNS0;
1890 * If we need EDNS0 to do this query and aren't using it, we lose.
1892 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1893 result = DNS_R_SERVFAIL;
1894 goto cleanup_message;
1897 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1898 add_triededns(fctx, &query->addrinfo->sockaddr);
1900 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1901 add_triededns512(fctx, &query->addrinfo->sockaddr);
1904 * Clear CD if EDNS is not in use.
1906 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1907 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1910 * Add TSIG record tailored to the current recipient.
1912 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1913 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1914 goto cleanup_message;
1916 if (tsigkey != NULL) {
1917 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1918 dns_tsigkey_detach(&tsigkey);
1919 if (result != ISC_R_SUCCESS)
1920 goto cleanup_message;
1923 result = dns_message_rendersection(fctx->qmessage,
1924 DNS_SECTION_ADDITIONAL, 0);
1925 if (result != ISC_R_SUCCESS)
1926 goto cleanup_message;
1928 result = dns_message_renderend(fctx->qmessage);
1929 if (result != ISC_R_SUCCESS)
1930 goto cleanup_message;
1932 dns_compress_invalidate(&cctx);
1933 cleanup_cctx = ISC_FALSE;
1935 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1936 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1938 result = dns_message_getquerytsig(fctx->qmessage,
1941 if (result != ISC_R_SUCCESS)
1942 goto cleanup_message;
1946 * If using TCP, write the length of the message at the beginning
1949 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1950 isc_buffer_usedregion(&query->buffer, &r);
1951 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1952 isc_buffer_add(&tcpbuffer, r.length);
1956 * We're now done with the query message.
1958 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1960 if (query->exclusivesocket)
1961 socket = dns_dispatch_getentrysocket(query->dispentry);
1963 socket = dns_dispatch_getsocket(query->dispatch);
1967 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1968 address = &query->addrinfo->sockaddr;
1969 if (query->exclusivesocket) {
1970 result = isc_socket_connect(socket, address, task,
1971 resquery_udpconnected,
1973 if (result != ISC_R_SUCCESS)
1974 goto cleanup_message;
1978 isc_buffer_usedregion(buffer, &r);
1981 * XXXRTH Make sure we don't send to ourselves! We should probably
1982 * prune out these addresses when we get them from the ADB.
1984 result = isc_socket_sendto(socket, &r, task, resquery_senddone,
1985 query, address, NULL);
1986 if (result != ISC_R_SUCCESS)
1987 goto cleanup_message;
1993 return (ISC_R_SUCCESS);
1997 dns_compress_invalidate(&cctx);
1999 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2002 * Stop the dispatcher from listening.
2004 dns_dispatch_removeresponse(&query->dispentry, NULL);
2008 dns_message_puttempname(fctx->qmessage, &qname);
2009 if (qrdataset != NULL)
2010 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2016 resquery_connected(isc_task_t *task, isc_event_t *event) {
2017 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2018 resquery_t *query = event->ev_arg;
2019 isc_boolean_t retry = ISC_FALSE;
2020 isc_interval_t interval;
2021 isc_result_t result;
2025 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2026 REQUIRE(VALID_QUERY(query));
2028 QTRACE("connected");
2035 * Currently we don't wait for the connect event before retrying
2036 * a query. This means that if we get really behind, we may end
2037 * up doing extra work!
2043 if (RESQUERY_CANCELED(query)) {
2045 * This query was canceled while the connect() was in
2048 isc_socket_detach(&query->tcpsocket);
2049 resquery_destroy(&query);
2051 switch (sevent->result) {
2055 * Extend the idle timer for TCP. 20 seconds
2056 * should be long enough for a TCP connection to be
2057 * established, a single DNS request to be sent,
2058 * and the response received.
2060 isc_interval_set(&interval, 20, 0);
2061 result = fctx_startidletimer(query->fctx, &interval);
2062 if (result != ISC_R_SUCCESS) {
2063 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2064 fctx_done(fctx, result, __LINE__);
2068 * We are connected. Create a dispatcher and
2072 attrs |= DNS_DISPATCHATTR_TCP;
2073 attrs |= DNS_DISPATCHATTR_PRIVATE;
2074 attrs |= DNS_DISPATCHATTR_CONNECTED;
2075 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2077 attrs |= DNS_DISPATCHATTR_IPV4;
2079 attrs |= DNS_DISPATCHATTR_IPV6;
2080 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2082 result = dns_dispatch_createtcp(query->dispatchmgr,
2084 query->fctx->res->taskmgr,
2085 4096, 2, 1, 1, 3, attrs,
2089 * Regardless of whether dns_dispatch_create()
2090 * succeeded or not, we don't need our reference
2091 * to the socket anymore.
2093 isc_socket_detach(&query->tcpsocket);
2095 if (result == ISC_R_SUCCESS)
2096 result = resquery_send(query);
2098 if (result != ISC_R_SUCCESS) {
2099 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2100 fctx_done(fctx, result, __LINE__);
2104 case ISC_R_NETUNREACH:
2105 case ISC_R_HOSTUNREACH:
2106 case ISC_R_CONNREFUSED:
2108 case ISC_R_ADDRNOTAVAIL:
2109 case ISC_R_CONNECTIONRESET:
2111 * No route to remote.
2113 isc_socket_detach(&query->tcpsocket);
2114 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2119 isc_socket_detach(&query->tcpsocket);
2120 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2125 isc_event_free(&event);
2129 * Behave as if the idle timer has expired. For TCP
2130 * connections this may not actually reflect the latest timer.
2132 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2133 result = fctx_stopidletimer(fctx);
2134 if (result != ISC_R_SUCCESS)
2135 fctx_done(fctx, result, __LINE__);
2137 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2142 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2144 dns_adbfind_t *find;
2145 dns_resolver_t *res;
2146 isc_boolean_t want_try = ISC_FALSE;
2147 isc_boolean_t want_done = ISC_FALSE;
2148 isc_boolean_t bucket_empty = ISC_FALSE;
2149 unsigned int bucketnum;
2151 find = event->ev_sender;
2152 fctx = event->ev_arg;
2153 REQUIRE(VALID_FCTX(fctx));
2158 FCTXTRACE("finddone");
2160 INSIST(fctx->pending > 0);
2163 if (ADDRWAIT(fctx)) {
2165 * The fetch is waiting for a name to be found.
2167 INSIST(!SHUTTINGDOWN(fctx));
2168 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2169 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2170 want_try = ISC_TRUE;
2173 if (fctx->pending == 0) {
2175 * We've got nothing else to wait for and don't
2176 * know the answer. There's nothing to do but
2179 want_done = ISC_TRUE;
2182 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2183 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2184 bucketnum = fctx->bucketnum;
2185 LOCK(&res->buckets[bucketnum].lock);
2187 * Note that we had to wait until we had the lock before
2188 * looking at fctx->references.
2190 if (fctx->references == 0)
2191 bucket_empty = fctx_destroy(fctx);
2192 UNLOCK(&res->buckets[bucketnum].lock);
2195 isc_event_free(&event);
2196 dns_adb_destroyfind(&find);
2199 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2201 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2202 else if (bucket_empty)
2207 static inline isc_boolean_t
2208 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2211 for (sa = ISC_LIST_HEAD(fctx->bad);
2213 sa = ISC_LIST_NEXT(sa, link)) {
2214 if (isc_sockaddr_equal(sa, address))
2221 static inline isc_boolean_t
2222 mark_bad(fetchctx_t *fctx) {
2223 dns_adbfind_t *curr;
2224 dns_adbaddrinfo_t *addrinfo;
2225 isc_boolean_t all_bad = ISC_TRUE;
2228 * Mark all known bad servers, so we don't try to talk to them
2233 * Mark any bad nameservers.
2235 for (curr = ISC_LIST_HEAD(fctx->finds);
2237 curr = ISC_LIST_NEXT(curr, publink)) {
2238 for (addrinfo = ISC_LIST_HEAD(curr->list);
2240 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2241 if (bad_server(fctx, &addrinfo->sockaddr))
2242 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2244 all_bad = ISC_FALSE;
2249 * Mark any bad forwarders.
2251 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2253 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2254 if (bad_server(fctx, &addrinfo->sockaddr))
2255 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2257 all_bad = ISC_FALSE;
2261 * Mark any bad alternates.
2263 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2265 curr = ISC_LIST_NEXT(curr, publink)) {
2266 for (addrinfo = ISC_LIST_HEAD(curr->list);
2268 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2269 if (bad_server(fctx, &addrinfo->sockaddr))
2270 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2272 all_bad = ISC_FALSE;
2276 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2278 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2279 if (bad_server(fctx, &addrinfo->sockaddr))
2280 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2282 all_bad = ISC_FALSE;
2289 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2290 badnstype_t badtype)
2292 char namebuf[DNS_NAME_FORMATSIZE];
2293 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2299 const char *spc = "";
2300 isc_sockaddr_t *address = &addrinfo->sockaddr;
2302 if (reason == DNS_R_LAME)
2306 case badns_unreachable:
2309 case badns_response:
2312 case badns_validation:
2313 break; /* counted as 'valfail' */
2317 if (bad_server(fctx, address)) {
2319 * We already know this server is bad.
2324 FCTXTRACE("add_bad");
2326 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
2331 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2333 if (reason == DNS_R_LAME) /* already logged */
2336 if (reason == DNS_R_UNEXPECTEDRCODE &&
2337 fctx->rmessage->rcode == dns_rcode_servfail &&
2338 ISFORWARDER(addrinfo))
2341 if (reason == DNS_R_UNEXPECTEDRCODE) {
2342 isc_buffer_init(&b, code, sizeof(code) - 1);
2343 dns_rcode_totext(fctx->rmessage->rcode, &b);
2344 code[isc_buffer_usedlength(&b)] = '\0';
2346 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2347 isc_buffer_init(&b, code, sizeof(code) - 1);
2348 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2349 code[isc_buffer_usedlength(&b)] = '\0';
2354 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2355 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2356 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2357 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2358 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2359 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2360 "error (%s%s%s) resolving '%s/%s/%s': %s",
2361 dns_result_totext(reason), spc, code,
2362 namebuf, typebuf, classbuf, addrbuf);
2366 * Sort addrinfo list by RTT.
2369 sort_adbfind(dns_adbfind_t *find) {
2370 dns_adbaddrinfo_t *best, *curr;
2371 dns_adbaddrinfolist_t sorted;
2373 /* Lame N^2 bubble sort. */
2374 ISC_LIST_INIT(sorted);
2375 while (!ISC_LIST_EMPTY(find->list)) {
2376 best = ISC_LIST_HEAD(find->list);
2377 curr = ISC_LIST_NEXT(best, publink);
2378 while (curr != NULL) {
2379 if (curr->srtt < best->srtt)
2381 curr = ISC_LIST_NEXT(curr, publink);
2383 ISC_LIST_UNLINK(find->list, best, publink);
2384 ISC_LIST_APPEND(sorted, best, publink);
2386 find->list = sorted;
2390 * Sort a list of finds by server RTT.
2393 sort_finds(dns_adbfindlist_t *findlist) {
2394 dns_adbfind_t *best, *curr;
2395 dns_adbfindlist_t sorted;
2396 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2398 /* Sort each find's addrinfo list by SRTT. */
2399 for (curr = ISC_LIST_HEAD(*findlist);
2401 curr = ISC_LIST_NEXT(curr, publink))
2404 /* Lame N^2 bubble sort. */
2405 ISC_LIST_INIT(sorted);
2406 while (!ISC_LIST_EMPTY(*findlist)) {
2407 best = ISC_LIST_HEAD(*findlist);
2408 bestaddrinfo = ISC_LIST_HEAD(best->list);
2409 INSIST(bestaddrinfo != NULL);
2410 curr = ISC_LIST_NEXT(best, publink);
2411 while (curr != NULL) {
2412 addrinfo = ISC_LIST_HEAD(curr->list);
2413 INSIST(addrinfo != NULL);
2414 if (addrinfo->srtt < bestaddrinfo->srtt) {
2416 bestaddrinfo = addrinfo;
2418 curr = ISC_LIST_NEXT(curr, publink);
2420 ISC_LIST_UNLINK(*findlist, best, publink);
2421 ISC_LIST_APPEND(sorted, best, publink);
2427 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2428 unsigned int options, unsigned int flags, isc_stdtime_t now,
2429 isc_boolean_t *need_alternate)
2431 dns_adbaddrinfo_t *ai;
2432 dns_adbfind_t *find;
2433 dns_resolver_t *res;
2434 isc_boolean_t unshared;
2435 isc_result_t result;
2438 unshared = ISC_TF((fctx->options | DNS_FETCHOPT_UNSHARED) != 0);
2440 * If this name is a subdomain of the query domain, tell
2441 * the ADB to start looking using zone/hint data. This keeps us
2442 * from getting stuck if the nameserver is beneath the zone cut
2443 * and we don't know its address (e.g. because the A record has
2446 if (dns_name_issubdomain(name, &fctx->domain))
2447 options |= DNS_ADBFIND_STARTATZONE;
2448 options |= DNS_ADBFIND_GLUEOK;
2449 options |= DNS_ADBFIND_HINTOK;
2452 * See what we know about this address.
2455 result = dns_adb_createfind(fctx->adb,
2456 res->buckets[fctx->bucketnum].task,
2457 fctx_finddone, fctx, name,
2458 &fctx->name, fctx->type,
2460 res->view->dstport, &find);
2461 if (result != ISC_R_SUCCESS) {
2462 if (result == DNS_R_ALIAS) {
2464 * XXXRTH Follow the CNAME/DNAME chain?
2466 dns_adb_destroyfind(&find);
2469 } else if (!ISC_LIST_EMPTY(find->list)) {
2471 * We have at least some of the addresses for the
2474 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2475 if (flags != 0 || port != 0) {
2476 for (ai = ISC_LIST_HEAD(find->list);
2478 ai = ISC_LIST_NEXT(ai, publink)) {
2481 isc_sockaddr_setport(&ai->sockaddr,
2485 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2486 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2488 ISC_LIST_APPEND(fctx->finds, find, publink);
2491 * We don't know any of the addresses for this
2494 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2496 * We're looking for them and will get an
2497 * event about it later.
2503 if (need_alternate != NULL &&
2504 !*need_alternate && unshared &&
2505 ((res->dispatchv4 == NULL &&
2506 find->result_v6 != DNS_R_NXDOMAIN) ||
2507 (res->dispatchv6 == NULL &&
2508 find->result_v4 != DNS_R_NXDOMAIN)))
2509 *need_alternate = ISC_TRUE;
2511 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2512 fctx->lamecount++; /* cached lame server */
2514 fctx->adberr++; /* unreachable server, etc. */
2517 * If we know there are no addresses for
2518 * the family we are using then try to add
2519 * an alternative server.
2521 if (need_alternate != NULL && !*need_alternate &&
2522 ((res->dispatchv4 == NULL &&
2523 find->result_v6 == DNS_R_NXRRSET) ||
2524 (res->dispatchv6 == NULL &&
2525 find->result_v4 == DNS_R_NXRRSET)))
2526 *need_alternate = ISC_TRUE;
2527 dns_adb_destroyfind(&find);
2532 static isc_boolean_t
2533 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2535 unsigned int nlabels;
2536 dns_namereln_t namereln;
2538 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2539 return (ISC_TF(namereln == dns_namereln_subdomain));
2543 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2544 dns_rdata_t rdata = DNS_RDATA_INIT;
2545 isc_result_t result;
2546 dns_resolver_t *res;
2548 unsigned int stdoptions = 0;
2550 dns_adbaddrinfo_t *ai;
2551 isc_boolean_t all_bad;
2553 isc_boolean_t need_alternate = ISC_FALSE;
2555 FCTXTRACE("getaddresses");
2558 * Don't pound on remote servers. (Failsafe!)
2561 if (fctx->restarts > 10) {
2562 FCTXTRACE("too many restarts");
2563 return (DNS_R_SERVFAIL);
2572 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2573 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2576 * If this fctx has forwarders, use them; otherwise use any
2577 * selective forwarders specified in the view; otherwise use the
2578 * resolver's forwarders (if any).
2580 sa = ISC_LIST_HEAD(fctx->forwarders);
2582 dns_forwarders_t *forwarders = NULL;
2583 dns_name_t *name = &fctx->name;
2585 unsigned int labels;
2586 dns_fixedname_t fixed;
2590 * DS records are found in the parent server.
2591 * Strip label to get the correct forwarder (if any).
2593 if (dns_rdatatype_atparent(fctx->type) &&
2594 dns_name_countlabels(name) > 1) {
2595 dns_name_init(&suffix, NULL);
2596 labels = dns_name_countlabels(name);
2597 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2601 dns_fixedname_init(&fixed);
2602 domain = dns_fixedname_name(&fixed);
2603 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2604 domain, &forwarders);
2605 if (result == ISC_R_SUCCESS) {
2606 sa = ISC_LIST_HEAD(forwarders->addrs);
2607 fctx->fwdpolicy = forwarders->fwdpolicy;
2608 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2609 isstrictsubdomain(domain, &fctx->domain)) {
2612 mctx = res->buckets[fctx->bucketnum].mctx;
2613 dns_name_free(&fctx->domain, mctx);
2614 dns_name_init(&fctx->domain, NULL);
2615 result = dns_name_dup(domain, mctx,
2617 if (result != ISC_R_SUCCESS)
2623 while (sa != NULL) {
2624 if ((isc_sockaddr_pf(sa) == AF_INET &&
2625 fctx->res->dispatchv4 == NULL) ||
2626 (isc_sockaddr_pf(sa) == AF_INET6 &&
2627 fctx->res->dispatchv6 == NULL)) {
2628 sa = ISC_LIST_NEXT(sa, link);
2632 result = dns_adb_findaddrinfo(fctx->adb,
2633 sa, &ai, 0); /* XXXMLG */
2634 if (result == ISC_R_SUCCESS) {
2635 dns_adbaddrinfo_t *cur;
2636 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2637 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2638 while (cur != NULL && cur->srtt < ai->srtt)
2639 cur = ISC_LIST_NEXT(cur, publink);
2641 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2644 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2646 sa = ISC_LIST_NEXT(sa, link);
2650 * If the forwarding policy is "only", we don't need the addresses
2651 * of the nameservers.
2653 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2657 * Normal nameservers.
2660 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2661 if (fctx->restarts == 1) {
2663 * To avoid sending out a flood of queries likely to
2664 * result in NXRRSET, we suppress fetches for address
2665 * families we don't have the first time through,
2666 * provided that we have addresses in some family we
2669 * We don't want to set this option all the time, since
2670 * if fctx->restarts > 1, we've clearly been having trouble
2671 * with the addresses we had, so getting more could help.
2673 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2675 if (res->dispatchv4 != NULL)
2676 stdoptions |= DNS_ADBFIND_INET;
2677 if (res->dispatchv6 != NULL)
2678 stdoptions |= DNS_ADBFIND_INET6;
2679 isc_stdtime_get(&now);
2681 INSIST(ISC_LIST_EMPTY(fctx->finds));
2682 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2684 for (result = dns_rdataset_first(&fctx->nameservers);
2685 result == ISC_R_SUCCESS;
2686 result = dns_rdataset_next(&fctx->nameservers))
2688 dns_rdataset_current(&fctx->nameservers, &rdata);
2690 * Extract the name from the NS record.
2692 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2693 if (result != ISC_R_SUCCESS)
2696 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2698 dns_rdata_reset(&rdata);
2699 dns_rdata_freestruct(&ns);
2701 if (result != ISC_R_NOMORE)
2705 * Do we need to use 6 to 4?
2707 if (need_alternate) {
2710 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2711 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2713 a = ISC_LIST_NEXT(a, link)) {
2714 if (!a->isaddress) {
2715 findname(fctx, &a->_u._n.name, a->_u._n.port,
2716 stdoptions, FCTX_ADDRINFO_FORWARDER,
2720 if (isc_sockaddr_pf(&a->_u.addr) != family)
2723 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2725 if (result == ISC_R_SUCCESS) {
2726 dns_adbaddrinfo_t *cur;
2727 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2728 cur = ISC_LIST_HEAD(fctx->altaddrs);
2729 while (cur != NULL && cur->srtt < ai->srtt)
2730 cur = ISC_LIST_NEXT(cur, publink);
2732 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2735 ISC_LIST_APPEND(fctx->altaddrs, ai,
2743 * Mark all known bad servers.
2745 all_bad = mark_bad(fctx);
2752 * We've got no addresses.
2754 if (fctx->pending > 0) {
2756 * We're fetching the addresses, but don't have any
2757 * yet. Tell the caller to wait for an answer.
2759 result = DNS_R_WAIT;
2764 * We've lost completely. We don't know any
2765 * addresses, and the ADB has told us it can't get
2768 FCTXTRACE("no addresses");
2769 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2770 result = isc_time_nowplusinterval(&expire, &i);
2772 (fctx->type == dns_rdatatype_dnskey ||
2773 fctx->type == dns_rdatatype_dlv ||
2774 fctx->type == dns_rdatatype_ds) &&
2775 result == ISC_R_SUCCESS)
2776 dns_resolver_addbadcache(fctx->res,
2778 fctx->type, &expire);
2779 result = ISC_R_FAILURE;
2783 * We've found some addresses. We might still be looking
2784 * for more addresses.
2786 sort_finds(&fctx->finds);
2787 sort_finds(&fctx->altfinds);
2788 result = ISC_R_SUCCESS;
2795 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2798 char buf[ISC_NETADDR_FORMATSIZE];
2800 isc_boolean_t aborted = ISC_FALSE;
2801 isc_boolean_t bogus;
2802 dns_acl_t *blackhole;
2803 isc_netaddr_t ipaddr;
2804 dns_peer_t *peer = NULL;
2805 dns_resolver_t *res;
2806 const char *msg = NULL;
2808 sa = &addr->sockaddr;
2811 isc_netaddr_fromsockaddr(&ipaddr, sa);
2812 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2813 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2815 if (blackhole != NULL) {
2818 if (dns_acl_match(&ipaddr, NULL, blackhole,
2820 &match, NULL) == ISC_R_SUCCESS &&
2826 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2831 addr->flags |= FCTX_ADDRINFO_MARK;
2832 msg = "ignoring blackholed / bogus server: ";
2833 } else if (isc_sockaddr_ismulticast(sa)) {
2834 addr->flags |= FCTX_ADDRINFO_MARK;
2835 msg = "ignoring multicast address: ";
2836 } else if (isc_sockaddr_isexperimental(sa)) {
2837 addr->flags |= FCTX_ADDRINFO_MARK;
2838 msg = "ignoring experimental address: ";
2839 } else if (sa->type.sa.sa_family != AF_INET6) {
2841 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2842 addr->flags |= FCTX_ADDRINFO_MARK;
2843 msg = "ignoring IPv6 mapped IPV4 address: ";
2844 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2845 addr->flags |= FCTX_ADDRINFO_MARK;
2846 msg = "ignoring IPv6 compatibility IPV4 address: ";
2850 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2853 isc_netaddr_fromsockaddr(&na, sa);
2854 isc_netaddr_format(&na, buf, sizeof(buf));
2855 FCTXTRACE2(msg, buf);
2858 static inline dns_adbaddrinfo_t *
2859 fctx_nextaddress(fetchctx_t *fctx) {
2860 dns_adbfind_t *find, *start;
2861 dns_adbaddrinfo_t *addrinfo;
2862 dns_adbaddrinfo_t *faddrinfo;
2865 * Return the next untried address, if any.
2869 * Find the first unmarked forwarder (if any).
2871 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2873 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2874 if (!UNMARKED(addrinfo))
2876 possibly_mark(fctx, addrinfo);
2877 if (UNMARKED(addrinfo)) {
2878 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2885 * No forwarders. Move to the next find.
2888 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2892 find = ISC_LIST_HEAD(fctx->finds);
2894 find = ISC_LIST_NEXT(find, publink);
2896 find = ISC_LIST_HEAD(fctx->finds);
2900 * Find the first unmarked addrinfo.
2906 for (addrinfo = ISC_LIST_HEAD(find->list);
2908 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2909 if (!UNMARKED(addrinfo))
2911 possibly_mark(fctx, addrinfo);
2912 if (UNMARKED(addrinfo)) {
2913 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2917 if (addrinfo != NULL)
2919 find = ISC_LIST_NEXT(find, publink);
2921 find = ISC_LIST_HEAD(fctx->finds);
2922 } while (find != start);
2926 if (addrinfo != NULL)
2930 * No nameservers left. Try alternates.
2933 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2935 find = fctx->altfind;
2937 find = ISC_LIST_HEAD(fctx->altfinds);
2939 find = ISC_LIST_NEXT(find, publink);
2941 find = ISC_LIST_HEAD(fctx->altfinds);
2945 * Find the first unmarked addrinfo.
2951 for (addrinfo = ISC_LIST_HEAD(find->list);
2953 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2954 if (!UNMARKED(addrinfo))
2956 possibly_mark(fctx, addrinfo);
2957 if (UNMARKED(addrinfo)) {
2958 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2962 if (addrinfo != NULL)
2964 find = ISC_LIST_NEXT(find, publink);
2966 find = ISC_LIST_HEAD(fctx->altfinds);
2967 } while (find != start);
2970 faddrinfo = addrinfo;
2973 * See if we have a better alternate server by address.
2976 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2978 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2979 if (!UNMARKED(addrinfo))
2981 possibly_mark(fctx, addrinfo);
2982 if (UNMARKED(addrinfo) &&
2983 (faddrinfo == NULL ||
2984 addrinfo->srtt < faddrinfo->srtt)) {
2985 if (faddrinfo != NULL)
2986 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
2987 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2992 if (addrinfo == NULL) {
2993 addrinfo = faddrinfo;
2994 fctx->altfind = find;
3001 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3002 isc_result_t result;
3003 dns_adbaddrinfo_t *addrinfo;
3007 REQUIRE(!ADDRWAIT(fctx));
3009 addrinfo = fctx_nextaddress(fctx);
3010 if (addrinfo == NULL) {
3012 * We have no more addresses. Start over.
3014 fctx_cancelqueries(fctx, ISC_TRUE);
3015 fctx_cleanupfinds(fctx);
3016 fctx_cleanupaltfinds(fctx);
3017 fctx_cleanupforwaddrs(fctx);
3018 fctx_cleanupaltaddrs(fctx);
3019 result = fctx_getaddresses(fctx, badcache);
3020 if (result == DNS_R_WAIT) {
3022 * Sleep waiting for addresses.
3024 FCTXTRACE("addrwait");
3025 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3027 } else if (result != ISC_R_SUCCESS) {
3029 * Something bad happened.
3031 fctx_done(fctx, result, __LINE__);
3035 addrinfo = fctx_nextaddress(fctx);
3037 * While we may have addresses from the ADB, they
3038 * might be bad ones. In this case, return SERVFAIL.
3040 if (addrinfo == NULL) {
3041 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3046 result = fctx_query(fctx, addrinfo, fctx->options);
3047 if (result != ISC_R_SUCCESS)
3048 fctx_done(fctx, result, __LINE__);
3050 inc_stats(fctx->res, dns_resstatscounter_retry);
3053 static isc_boolean_t
3054 fctx_destroy(fetchctx_t *fctx) {
3055 dns_resolver_t *res;
3056 unsigned int bucketnum;
3057 isc_sockaddr_t *sa, *next_sa;
3060 * Caller must be holding the bucket lock.
3063 REQUIRE(VALID_FCTX(fctx));
3064 REQUIRE(fctx->state == fetchstate_done ||
3065 fctx->state == fetchstate_init);
3066 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3067 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3068 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3069 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3070 REQUIRE(fctx->pending == 0);
3071 REQUIRE(fctx->references == 0);
3072 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3074 FCTXTRACE("destroy");
3077 bucketnum = fctx->bucketnum;
3079 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3084 for (sa = ISC_LIST_HEAD(fctx->bad);
3087 next_sa = ISC_LIST_NEXT(sa, link);
3088 ISC_LIST_UNLINK(fctx->bad, sa, link);
3089 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3092 for (sa = ISC_LIST_HEAD(fctx->edns);
3095 next_sa = ISC_LIST_NEXT(sa, link);
3096 ISC_LIST_UNLINK(fctx->edns, sa, link);
3097 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3100 for (sa = ISC_LIST_HEAD(fctx->edns512);
3103 next_sa = ISC_LIST_NEXT(sa, link);
3104 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3105 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3108 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3111 next_sa = ISC_LIST_NEXT(sa, link);
3112 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3113 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3116 isc_timer_detach(&fctx->timer);
3117 dns_message_destroy(&fctx->rmessage);
3118 dns_message_destroy(&fctx->qmessage);
3119 if (dns_name_countlabels(&fctx->domain) > 0)
3120 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3121 if (dns_rdataset_isassociated(&fctx->nameservers))
3122 dns_rdataset_disassociate(&fctx->nameservers);
3123 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3124 dns_db_detach(&fctx->cache);
3125 dns_adb_detach(&fctx->adb);
3126 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3127 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3131 UNLOCK(&res->nlock);
3133 if (res->buckets[bucketnum].exiting &&
3134 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3141 * Fetch event handlers.
3145 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3146 fetchctx_t *fctx = event->ev_arg;
3147 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3150 REQUIRE(VALID_FCTX(fctx));
3154 FCTXTRACE("timeout");
3156 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3158 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3159 fctx->reason = NULL;
3160 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3162 isc_result_t result;
3165 fctx->timeout = ISC_TRUE;
3167 * We could cancel the running queries here, or we could let
3168 * them keep going. Since we normally use separate sockets for
3169 * different queries, we adopt the former approach to reduce
3170 * the number of open sockets: cancel the oldest query if it
3171 * expired after the query had started (this is usually the
3172 * case but is not always so, depending on the task schedule
3175 query = ISC_LIST_HEAD(fctx->queries);
3176 if (query != NULL &&
3177 isc_time_compare(&tevent->due, &query->start) >= 0) {
3178 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3180 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3182 * Our timer has triggered. Reestablish the fctx lifetime
3185 result = fctx_starttimer(fctx);
3186 if (result != ISC_R_SUCCESS)
3187 fctx_done(fctx, result, __LINE__);
3192 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3195 isc_event_free(&event);
3199 fctx_shutdown(fetchctx_t *fctx) {
3200 isc_event_t *cevent;
3203 * Start the shutdown process for fctx, if it isn't already underway.
3206 FCTXTRACE("shutdown");
3209 * The caller must be holding the appropriate bucket lock.
3212 if (fctx->want_shutdown)
3215 fctx->want_shutdown = ISC_TRUE;
3218 * Unless we're still initializing (in which case the
3219 * control event is still outstanding), we need to post
3220 * the control event to tell the fetch we want it to
3223 if (fctx->state != fetchstate_init) {
3224 cevent = &fctx->control_event;
3225 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3231 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3232 fetchctx_t *fctx = event->ev_arg;
3233 isc_boolean_t bucket_empty = ISC_FALSE;
3234 dns_resolver_t *res;
3235 unsigned int bucketnum;
3236 dns_validator_t *validator;
3238 REQUIRE(VALID_FCTX(fctx));
3243 bucketnum = fctx->bucketnum;
3245 FCTXTRACE("doshutdown");
3248 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3250 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3253 * Cancel all pending validators. Note that this must be done
3254 * without the bucket lock held, since that could cause deadlock.
3256 validator = ISC_LIST_HEAD(fctx->validators);
3257 while (validator != NULL) {
3258 dns_validator_cancel(validator);
3259 validator = ISC_LIST_NEXT(validator, link);
3262 if (fctx->nsfetch != NULL)
3263 dns_resolver_cancelfetch(fctx->nsfetch);
3266 * Shut down anything that is still running on behalf of this
3267 * fetch. To avoid deadlock with the ADB, we must do this
3268 * before we lock the bucket lock.
3270 fctx_stopeverything(fctx, ISC_FALSE);
3272 LOCK(&res->buckets[bucketnum].lock);
3274 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3276 INSIST(fctx->state == fetchstate_active ||
3277 fctx->state == fetchstate_done);
3278 INSIST(fctx->want_shutdown);
3280 if (fctx->state != fetchstate_done) {
3281 fctx->state = fetchstate_done;
3282 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3285 if (fctx->references == 0 && fctx->pending == 0 &&
3286 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3287 bucket_empty = fctx_destroy(fctx);
3289 UNLOCK(&res->buckets[bucketnum].lock);
3296 fctx_start(isc_task_t *task, isc_event_t *event) {
3297 fetchctx_t *fctx = event->ev_arg;
3298 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3299 dns_resolver_t *res;
3300 unsigned int bucketnum;
3302 REQUIRE(VALID_FCTX(fctx));
3307 bucketnum = fctx->bucketnum;
3311 LOCK(&res->buckets[bucketnum].lock);
3313 INSIST(fctx->state == fetchstate_init);
3314 if (fctx->want_shutdown) {
3316 * We haven't started this fctx yet, and we've been requested
3319 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3320 fctx->state = fetchstate_done;
3321 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3323 * Since we haven't started, we INSIST that we have no
3324 * pending ADB finds and no pending validations.
3326 INSIST(fctx->pending == 0);
3327 INSIST(fctx->nqueries == 0);
3328 INSIST(ISC_LIST_EMPTY(fctx->validators));
3329 if (fctx->references == 0) {
3331 * It's now safe to destroy this fctx.
3333 bucket_empty = fctx_destroy(fctx);
3338 * Normal fctx startup.
3340 fctx->state = fetchstate_active;
3342 * Reset the control event for later use in shutting down
3345 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3346 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3350 UNLOCK(&res->buckets[bucketnum].lock);
3353 isc_result_t result;
3356 * All is well. Start working on the fetch.
3358 result = fctx_starttimer(fctx);
3359 if (result != ISC_R_SUCCESS)
3360 fctx_done(fctx, result, __LINE__);
3362 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3363 } else if (bucket_empty)
3368 * Fetch Creation, Joining, and Cancelation.
3371 static inline isc_result_t
3372 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3373 dns_messageid_t id, isc_taskaction_t action, void *arg,
3374 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3378 dns_fetchevent_t *event;
3383 * We store the task we're going to send this event to in the
3384 * sender field. We'll make the fetch the sender when we actually
3388 isc_task_attach(task, &clone);
3389 event = (dns_fetchevent_t *)
3390 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3391 action, arg, sizeof(*event));
3392 if (event == NULL) {
3393 isc_task_detach(&clone);
3394 return (ISC_R_NOMEMORY);
3396 event->result = DNS_R_SERVFAIL;
3397 event->qtype = fctx->type;
3400 event->rdataset = rdataset;
3401 event->sigrdataset = sigrdataset;
3402 event->fetch = fetch;
3403 event->client = client;
3405 dns_fixedname_init(&event->foundname);
3408 * Make sure that we can store the sigrdataset in the
3409 * first event if it is needed by any of the events.
3411 if (event->sigrdataset != NULL)
3412 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3414 ISC_LIST_APPEND(fctx->events, event, ev_link);
3416 fctx->client = client;
3418 fetch->magic = DNS_FETCH_MAGIC;
3419 fetch->private = fctx;
3421 return (ISC_R_SUCCESS);
3425 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3426 dns_name_t *domain, dns_rdataset_t *nameservers,
3427 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3430 isc_result_t result;
3431 isc_result_t iresult;
3432 isc_interval_t interval;
3433 dns_fixedname_t fixed;
3434 unsigned int findoptions = 0;
3435 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3436 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3440 * Caller must be holding the lock for bucket number 'bucketnum'.
3442 REQUIRE(fctxp != NULL && *fctxp == NULL);
3444 fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3446 return (ISC_R_NOMEMORY);
3447 dns_name_format(name, buf, sizeof(buf));
3448 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3449 strcat(buf, "/"); /* checked */
3450 strcat(buf, typebuf); /* checked */
3451 fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3452 if (fctx->info == NULL) {
3453 result = ISC_R_NOMEMORY;
3456 FCTXTRACE("create");
3457 dns_name_init(&fctx->name, NULL);
3458 result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3459 if (result != ISC_R_SUCCESS)
3461 dns_name_init(&fctx->domain, NULL);
3462 dns_rdataset_init(&fctx->nameservers);
3465 fctx->options = options;
3467 * Note! We do not attach to the task. We are relying on the
3468 * resolver to ensure that this task doesn't go away while we are
3472 fctx->references = 0;
3473 fctx->bucketnum = bucketnum;
3474 fctx->state = fetchstate_init;
3475 fctx->want_shutdown = ISC_FALSE;
3476 fctx->cloned = ISC_FALSE;
3477 ISC_LIST_INIT(fctx->queries);
3478 ISC_LIST_INIT(fctx->finds);
3479 ISC_LIST_INIT(fctx->altfinds);
3480 ISC_LIST_INIT(fctx->forwaddrs);
3481 ISC_LIST_INIT(fctx->altaddrs);
3482 ISC_LIST_INIT(fctx->forwarders);
3483 fctx->fwdpolicy = dns_fwdpolicy_none;
3484 ISC_LIST_INIT(fctx->bad);
3485 ISC_LIST_INIT(fctx->edns);
3486 ISC_LIST_INIT(fctx->edns512);
3487 ISC_LIST_INIT(fctx->bad_edns);
3488 ISC_LIST_INIT(fctx->validators);
3489 fctx->validator = NULL;
3491 fctx->altfind = NULL;
3494 fctx->querysent = 0;
3495 fctx->referrals = 0;
3496 TIME_NOW(&fctx->start);
3498 fctx->lamecount = 0;
3504 fctx->result = ISC_R_FAILURE;
3505 fctx->vresult = ISC_R_SUCCESS;
3506 fctx->exitline = -1; /* sentinel */
3507 fctx->logged = ISC_FALSE;
3508 fctx->attributes = 0;
3509 fctx->spilled = ISC_FALSE;
3511 fctx->reason = NULL;
3513 fctx->rand_bits = 0;
3514 fctx->timeout = ISC_FALSE;
3515 fctx->addrinfo = NULL;
3516 fctx->client = NULL;
3518 dns_name_init(&fctx->nsname, NULL);
3519 fctx->nsfetch = NULL;
3520 dns_rdataset_init(&fctx->nsrrset);
3522 if (domain == NULL) {
3523 dns_forwarders_t *forwarders = NULL;
3524 unsigned int labels;
3525 dns_name_t *fwdname = name;
3528 * DS records are found in the parent server.
3529 * Strip label to get the correct forwarder (if any).
3531 if (dns_rdatatype_atparent(fctx->type) &&
3532 dns_name_countlabels(name) > 1) {
3533 dns_name_init(&suffix, NULL);
3534 labels = dns_name_countlabels(name);
3535 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3538 dns_fixedname_init(&fixed);
3539 domain = dns_fixedname_name(&fixed);
3540 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3541 domain, &forwarders);
3542 if (result == ISC_R_SUCCESS)
3543 fctx->fwdpolicy = forwarders->fwdpolicy;
3545 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3547 * The caller didn't supply a query domain and
3548 * nameservers, and we're not in forward-only mode,
3549 * so find the best nameservers to use.
3551 if (dns_rdatatype_atparent(fctx->type))
3552 findoptions |= DNS_DBFIND_NOEXACT;
3553 result = dns_view_findzonecut(res->view, name, domain,
3554 0, findoptions, ISC_TRUE,
3557 if (result != ISC_R_SUCCESS)
3559 result = dns_name_dup(domain,
3560 res->buckets[bucketnum].mctx,
3562 if (result != ISC_R_SUCCESS) {
3563 dns_rdataset_disassociate(&fctx->nameservers);
3568 * We're in forward-only mode. Set the query domain.
3570 result = dns_name_dup(domain,
3571 res->buckets[bucketnum].mctx,
3573 if (result != ISC_R_SUCCESS)
3577 result = dns_name_dup(domain,
3578 res->buckets[bucketnum].mctx,
3580 if (result != ISC_R_SUCCESS)
3582 dns_rdataset_clone(nameservers, &fctx->nameservers);
3585 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3587 fctx->qmessage = NULL;
3588 result = dns_message_create(res->buckets[bucketnum].mctx,
3589 DNS_MESSAGE_INTENTRENDER,
3592 if (result != ISC_R_SUCCESS)
3593 goto cleanup_domain;
3595 fctx->rmessage = NULL;
3596 result = dns_message_create(res->buckets[bucketnum].mctx,
3597 DNS_MESSAGE_INTENTPARSE,
3600 if (result != ISC_R_SUCCESS)
3601 goto cleanup_qmessage;
3604 * Compute an expiration time for the entire fetch.
3606 isc_interval_set(&interval, res->query_timeout, 0);
3607 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3608 if (iresult != ISC_R_SUCCESS) {
3609 UNEXPECTED_ERROR(__FILE__, __LINE__,
3610 "isc_time_nowplusinterval: %s",
3611 isc_result_totext(iresult));
3612 result = ISC_R_UNEXPECTED;
3613 goto cleanup_rmessage;
3617 * Default retry interval initialization. We set the interval now
3618 * mostly so it won't be uninitialized. It will be set to the
3619 * correct value before a query is issued.
3621 isc_interval_set(&fctx->interval, 2, 0);
3624 * Create an inactive timer. It will be made active when the fetch
3625 * is actually started.
3628 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3630 res->buckets[bucketnum].task, fctx_timeout,
3631 fctx, &fctx->timer);
3632 if (iresult != ISC_R_SUCCESS) {
3633 UNEXPECTED_ERROR(__FILE__, __LINE__,
3634 "isc_timer_create: %s",
3635 isc_result_totext(iresult));
3636 result = ISC_R_UNEXPECTED;
3637 goto cleanup_rmessage;
3641 * Attach to the view's cache and adb.
3644 dns_db_attach(res->view->cachedb, &fctx->cache);
3646 dns_adb_attach(res->view->adb, &fctx->adb);
3648 ISC_LIST_INIT(fctx->events);
3649 ISC_LINK_INIT(fctx, link);
3650 fctx->magic = FCTX_MAGIC;
3652 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3656 UNLOCK(&res->nlock);
3660 return (ISC_R_SUCCESS);
3663 dns_message_destroy(&fctx->rmessage);
3666 dns_message_destroy(&fctx->qmessage);
3669 if (dns_name_countlabels(&fctx->domain) > 0)
3670 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3671 if (dns_rdataset_isassociated(&fctx->nameservers))
3672 dns_rdataset_disassociate(&fctx->nameservers);
3675 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3678 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3681 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3689 static inline isc_boolean_t
3690 is_lame(fetchctx_t *fctx) {
3691 dns_message_t *message = fctx->rmessage;
3693 dns_rdataset_t *rdataset;
3694 isc_result_t result;
3696 if (message->rcode != dns_rcode_noerror &&
3697 message->rcode != dns_rcode_nxdomain)
3700 if (message->counts[DNS_SECTION_ANSWER] != 0)
3703 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3706 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3707 while (result == ISC_R_SUCCESS) {
3709 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3710 for (rdataset = ISC_LIST_HEAD(name->list);
3712 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3713 dns_namereln_t namereln;
3715 unsigned int labels;
3716 if (rdataset->type != dns_rdatatype_ns)
3718 namereln = dns_name_fullcompare(name, &fctx->domain,
3720 if (namereln == dns_namereln_equal &&
3721 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3723 if (namereln == dns_namereln_subdomain)
3727 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3734 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3735 char namebuf[DNS_NAME_FORMATSIZE];
3736 char domainbuf[DNS_NAME_FORMATSIZE];
3737 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3739 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3740 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3741 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3742 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3743 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3744 "lame server resolving '%s' (in '%s'?): %s",
3745 namebuf, domainbuf, addrbuf);
3749 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3750 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3751 char clbuf[ISC_SOCKADDR_FORMATSIZE];
3752 const char *clmsg = "";
3756 va_start(args, format);
3757 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3760 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3762 if (fctx->client != NULL) {
3763 clmsg = " for client ";
3764 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3769 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3770 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3771 "DNS format error from %s resolving %s%s%s: %s",
3772 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3775 static inline isc_result_t
3776 same_question(fetchctx_t *fctx) {
3777 isc_result_t result;
3778 dns_message_t *message = fctx->rmessage;
3780 dns_rdataset_t *rdataset;
3783 * Caller must be holding the fctx lock.
3787 * XXXRTH Currently we support only one question.
3789 if (message->counts[DNS_SECTION_QUESTION] != 1) {
3790 log_formerr(fctx, "too many questions");
3791 return (DNS_R_FORMERR);
3794 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3795 if (result != ISC_R_SUCCESS)
3798 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3799 rdataset = ISC_LIST_HEAD(name->list);
3800 INSIST(rdataset != NULL);
3801 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3803 if (fctx->type != rdataset->type ||
3804 fctx->res->rdclass != rdataset->rdclass ||
3805 !dns_name_equal(&fctx->name, name)) {
3806 char namebuf[DNS_NAME_FORMATSIZE];
3807 char class[DNS_RDATACLASS_FORMATSIZE];
3808 char type[DNS_RDATATYPE_FORMATSIZE];
3810 dns_name_format(name, namebuf, sizeof(namebuf));
3811 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3812 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3813 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3814 namebuf, class, type);
3815 return (DNS_R_FORMERR);
3818 return (ISC_R_SUCCESS);
3822 clone_results(fetchctx_t *fctx) {
3823 dns_fetchevent_t *event, *hevent;
3824 isc_result_t result;
3825 dns_name_t *name, *hname;
3827 FCTXTRACE("clone_results");
3830 * Set up any other events to have the same data as the first
3833 * Caller must be holding the appropriate lock.
3836 fctx->cloned = ISC_TRUE;
3837 hevent = ISC_LIST_HEAD(fctx->events);
3840 hname = dns_fixedname_name(&hevent->foundname);
3841 for (event = ISC_LIST_NEXT(hevent, ev_link);
3843 event = ISC_LIST_NEXT(event, ev_link)) {
3844 name = dns_fixedname_name(&event->foundname);
3845 result = dns_name_copy(hname, name, NULL);
3846 if (result != ISC_R_SUCCESS)
3847 event->result = result;
3849 event->result = hevent->result;
3850 dns_db_attach(hevent->db, &event->db);
3851 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3852 INSIST(hevent->rdataset != NULL);
3853 INSIST(event->rdataset != NULL);
3854 if (dns_rdataset_isassociated(hevent->rdataset))
3855 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3856 INSIST(! (hevent->sigrdataset == NULL &&
3857 event->sigrdataset != NULL));
3858 if (hevent->sigrdataset != NULL &&
3859 dns_rdataset_isassociated(hevent->sigrdataset) &&
3860 event->sigrdataset != NULL)
3861 dns_rdataset_clone(hevent->sigrdataset,
3862 event->sigrdataset);
3866 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3867 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3868 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3869 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3870 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3871 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3872 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3876 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3877 * no references and is no longer waiting for any events). If this
3878 * was the last fctx in the resolver, destroy the resolver.
3881 * '*fctx' is shutting down.
3884 maybe_destroy(fetchctx_t *fctx) {
3885 unsigned int bucketnum;
3886 isc_boolean_t bucket_empty = ISC_FALSE;
3887 dns_resolver_t *res = fctx->res;
3888 dns_validator_t *validator, *next_validator;
3890 REQUIRE(SHUTTINGDOWN(fctx));
3892 if (fctx->pending != 0 || fctx->nqueries != 0)
3895 for (validator = ISC_LIST_HEAD(fctx->validators);
3896 validator != NULL; validator = next_validator) {
3897 next_validator = ISC_LIST_NEXT(validator, link);
3898 dns_validator_cancel(validator);
3901 bucketnum = fctx->bucketnum;
3902 LOCK(&res->buckets[bucketnum].lock);
3903 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3904 bucket_empty = fctx_destroy(fctx);
3905 UNLOCK(&res->buckets[bucketnum].lock);
3912 * The validator has finished.
3915 validated(isc_task_t *task, isc_event_t *event) {
3916 isc_result_t result = ISC_R_SUCCESS;
3917 isc_result_t eresult = ISC_R_SUCCESS;
3920 dns_validatorevent_t *vevent;
3921 dns_fetchevent_t *hevent;
3922 dns_rdataset_t *ardataset = NULL;
3923 dns_rdataset_t *asigrdataset = NULL;
3924 dns_dbnode_t *node = NULL;
3925 isc_boolean_t negative;
3926 isc_boolean_t chaining;
3927 isc_boolean_t sentresponse;
3929 dns_dbnode_t *nsnode = NULL;
3931 dns_rdataset_t *rdataset;
3932 dns_rdataset_t *sigrdataset;
3933 dns_valarg_t *valarg;
3934 dns_adbaddrinfo_t *addrinfo;
3936 UNUSED(task); /* for now */
3938 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3939 valarg = event->ev_arg;
3940 fctx = valarg->fctx;
3941 addrinfo = valarg->addrinfo;
3942 REQUIRE(VALID_FCTX(fctx));
3943 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3945 vevent = (dns_validatorevent_t *)event;
3946 fctx->vresult = vevent->result;
3948 FCTXTRACE("received validation completion event");
3950 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
3951 fctx->validator = NULL;
3954 * Destroy the validator early so that we can
3955 * destroy the fctx if necessary.
3957 dns_validator_destroy(&vevent->validator);
3958 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
3959 valarg, sizeof(*valarg));
3961 negative = ISC_TF(vevent->rdataset == NULL);
3963 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
3966 * If shutting down, ignore the results. Check to see if we're
3967 * done waiting for validator completions and ADB pending events; if
3968 * so, destroy the fctx.
3970 if (SHUTTINGDOWN(fctx) && !sentresponse) {
3971 maybe_destroy(fctx); /* Locks bucket. */
3975 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3977 isc_stdtime_get(&now);
3980 * If chaining, we need to make sure that the right result code is
3981 * returned, and that the rdatasets are bound.
3983 if (vevent->result == ISC_R_SUCCESS &&
3985 vevent->rdataset != NULL &&
3986 CHAINING(vevent->rdataset))
3988 if (vevent->rdataset->type == dns_rdatatype_cname)
3989 eresult = DNS_R_CNAME;
3991 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
3992 eresult = DNS_R_DNAME;
3994 chaining = ISC_TRUE;
3996 chaining = ISC_FALSE;
3999 * Either we're not shutting down, or we are shutting down but want
4000 * to cache the result anyway (if this was a validation started by
4001 * a query with cd set)
4004 hevent = ISC_LIST_HEAD(fctx->events);
4005 if (hevent != NULL) {
4006 if (!negative && !chaining &&
4007 (fctx->type == dns_rdatatype_any ||
4008 fctx->type == dns_rdatatype_rrsig ||
4009 fctx->type == dns_rdatatype_sig)) {
4011 * Don't bind rdatasets; the caller
4012 * will iterate the node.
4015 ardataset = hevent->rdataset;
4016 asigrdataset = hevent->sigrdataset;
4020 if (vevent->result != ISC_R_SUCCESS) {
4021 FCTXTRACE("validation failed");
4022 inc_stats(fctx->res, dns_resstatscounter_valfail);
4024 fctx->vresult = vevent->result;
4025 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4026 result = ISC_R_NOTFOUND;
4027 if (vevent->rdataset != NULL)
4028 result = dns_db_findnode(fctx->cache,
4031 if (result == ISC_R_SUCCESS)
4032 (void)dns_db_deleterdataset(fctx->cache, node,
4035 if (result == ISC_R_SUCCESS &&
4036 vevent->sigrdataset != NULL)
4037 (void)dns_db_deleterdataset(fctx->cache, node,
4039 dns_rdatatype_rrsig,
4041 if (result == ISC_R_SUCCESS)
4042 dns_db_detachnode(fctx->cache, &node);
4044 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4046 * Cache the data as pending for later validation.
4048 result = ISC_R_NOTFOUND;
4049 if (vevent->rdataset != NULL)
4050 result = dns_db_findnode(fctx->cache,
4053 if (result == ISC_R_SUCCESS) {
4054 (void)dns_db_addrdataset(fctx->cache, node,
4056 vevent->rdataset, 0,
4059 if (result == ISC_R_SUCCESS &&
4060 vevent->sigrdataset != NULL)
4061 (void)dns_db_addrdataset(fctx->cache, node,
4063 vevent->sigrdataset,
4065 if (result == ISC_R_SUCCESS)
4066 dns_db_detachnode(fctx->cache, &node);
4068 result = fctx->vresult;
4069 add_bad(fctx, addrinfo, result, badns_validation);
4070 isc_event_free(&event);
4071 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4072 INSIST(fctx->validator == NULL);
4073 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4074 if (fctx->validator != NULL)
4075 dns_validator_send(fctx->validator);
4076 else if (sentresponse)
4077 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4078 else if (result == DNS_R_BROKENCHAIN) {
4079 isc_result_t tresult;
4083 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4084 tresult = isc_time_nowplusinterval(&expire, &i);
4086 (fctx->type == dns_rdatatype_dnskey ||
4087 fctx->type == dns_rdatatype_dlv ||
4088 fctx->type == dns_rdatatype_ds) &&
4089 tresult == ISC_R_SUCCESS)
4090 dns_resolver_addbadcache(fctx->res,
4092 fctx->type, &expire);
4093 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4095 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4101 dns_rdatatype_t covers;
4102 FCTXTRACE("nonexistence validation OK");
4104 inc_stats(fctx->res, dns_resstatscounter_valnegsuccess);
4106 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4107 covers = dns_rdatatype_any;
4109 covers = fctx->type;
4111 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4113 if (result != ISC_R_SUCCESS)
4114 goto noanswer_response;
4117 * If we are asking for a SOA record set the cache time
4118 * to zero to facilitate locating the containing zone of
4121 ttl = fctx->res->view->maxncachettl;
4122 if (fctx->type == dns_rdatatype_soa &&
4123 covers == dns_rdatatype_any &&
4124 fctx->res->zero_no_soa_ttl)
4127 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4128 covers, now, ttl, vevent->optout,
4129 ardataset, &eresult);
4130 if (result != ISC_R_SUCCESS)
4131 goto noanswer_response;
4132 goto answer_response;
4134 inc_stats(fctx->res, dns_resstatscounter_valsuccess);
4136 FCTXTRACE("validation OK");
4138 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4140 result = dns_rdataset_addnoqname(vevent->rdataset,
4141 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4142 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4143 INSIST(vevent->sigrdataset != NULL);
4144 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4145 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4146 result = dns_rdataset_addclosest(vevent->rdataset,
4147 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4148 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4153 * The data was already cached as pending data.
4154 * Re-cache it as secure and bind the cached
4155 * rdatasets to the first event on the fetch
4158 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4159 if (result != ISC_R_SUCCESS)
4160 goto noanswer_response;
4162 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4163 vevent->rdataset, 0, ardataset);
4164 if (result != ISC_R_SUCCESS &&
4165 result != DNS_R_UNCHANGED)
4166 goto noanswer_response;
4167 if (ardataset != NULL && NEGATIVE(ardataset)) {
4168 if (NXDOMAIN(ardataset))
4169 eresult = DNS_R_NCACHENXDOMAIN;
4171 eresult = DNS_R_NCACHENXRRSET;
4172 } else if (vevent->sigrdataset != NULL) {
4173 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4174 vevent->sigrdataset, 0,
4176 if (result != ISC_R_SUCCESS &&
4177 result != DNS_R_UNCHANGED)
4178 goto noanswer_response;
4183 * If we only deferred the destroy because we wanted to cache
4184 * the data, destroy now.
4186 dns_db_detachnode(fctx->cache, &node);
4187 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4188 if (SHUTTINGDOWN(fctx))
4189 maybe_destroy(fctx); /* Locks bucket. */
4193 if (!ISC_LIST_EMPTY(fctx->validators)) {
4195 INSIST(fctx->type == dns_rdatatype_any ||
4196 fctx->type == dns_rdatatype_rrsig ||
4197 fctx->type == dns_rdatatype_sig);
4199 * Don't send a response yet - we have
4200 * more rdatasets that still need to
4203 dns_db_detachnode(fctx->cache, &node);
4204 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4205 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4211 * Cache any NS/NSEC records that happened to be validated.
4213 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4214 while (result == ISC_R_SUCCESS) {
4216 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4218 for (rdataset = ISC_LIST_HEAD(name->list);
4220 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4221 if ((rdataset->type != dns_rdatatype_ns &&
4222 rdataset->type != dns_rdatatype_nsec) ||
4223 rdataset->trust != dns_trust_secure)
4225 for (sigrdataset = ISC_LIST_HEAD(name->list);
4226 sigrdataset != NULL;
4227 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4228 if (sigrdataset->type != dns_rdatatype_rrsig ||
4229 sigrdataset->covers != rdataset->type)
4233 if (sigrdataset == NULL ||
4234 sigrdataset->trust != dns_trust_secure)
4236 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4238 if (result != ISC_R_SUCCESS)
4241 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4242 now, rdataset, 0, NULL);
4243 if (result == ISC_R_SUCCESS)
4244 result = dns_db_addrdataset(fctx->cache, nsnode,
4248 dns_db_detachnode(fctx->cache, &nsnode);
4249 if (result != ISC_R_SUCCESS)
4252 result = dns_message_nextname(fctx->rmessage,
4253 DNS_SECTION_AUTHORITY);
4256 result = ISC_R_SUCCESS;
4259 * Respond with an answer, positive or negative,
4260 * as opposed to an error. 'node' must be non-NULL.
4263 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4265 if (hevent != NULL) {
4266 hevent->result = eresult;
4267 RUNTIME_CHECK(dns_name_copy(vevent->name,
4268 dns_fixedname_name(&hevent->foundname), NULL)
4270 dns_db_attach(fctx->cache, &hevent->db);
4271 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4272 clone_results(fctx);
4277 dns_db_detachnode(fctx->cache, &node);
4279 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4281 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4284 INSIST(node == NULL);
4285 isc_event_free(&event);
4288 static inline isc_result_t
4289 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4292 dns_rdataset_t *rdataset, *sigrdataset;
4293 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4294 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4295 dns_dbnode_t *node, **anodep;
4298 dns_resolver_t *res;
4299 isc_boolean_t need_validation, secure_domain, have_answer;
4300 isc_result_t result, eresult;
4301 dns_fetchevent_t *event;
4302 unsigned int options;
4305 unsigned int valoptions = 0;
4308 * The appropriate bucket lock must be held.
4312 need_validation = ISC_FALSE;
4313 POST(need_validation);
4314 secure_domain = ISC_FALSE;
4315 have_answer = ISC_FALSE;
4316 eresult = ISC_R_SUCCESS;
4317 task = res->buckets[fctx->bucketnum].task;
4320 * Is DNSSEC validation required for this name?
4322 if (res->view->enablevalidation) {
4323 result = dns_view_issecuredomain(res->view, name,
4325 if (result != ISC_R_SUCCESS)
4328 if (!secure_domain && res->view->dlv != NULL) {
4329 valoptions = DNS_VALIDATOR_DLV;
4330 secure_domain = ISC_TRUE;
4334 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4335 need_validation = ISC_FALSE;
4337 need_validation = secure_domain;
4343 asigrdataset = NULL;
4345 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4347 have_answer = ISC_TRUE;
4348 event = ISC_LIST_HEAD(fctx->events);
4349 if (event != NULL) {
4351 aname = dns_fixedname_name(&event->foundname);
4352 result = dns_name_copy(name, aname, NULL);
4353 if (result != ISC_R_SUCCESS)
4355 anodep = &event->node;
4357 * If this is an ANY, SIG or RRSIG query, we're not
4358 * going to return any rdatasets, unless we encountered
4359 * a CNAME or DNAME as "the answer". In this case,
4360 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4361 * and we must set up the rdatasets.
4363 if ((fctx->type != dns_rdatatype_any &&
4364 fctx->type != dns_rdatatype_rrsig &&
4365 fctx->type != dns_rdatatype_sig) ||
4366 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4367 ardataset = event->rdataset;
4368 asigrdataset = event->sigrdataset;
4374 * Find or create the cache node.
4377 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4378 if (result != ISC_R_SUCCESS)
4382 * Cache or validate each cacheable rdataset.
4384 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4385 for (rdataset = ISC_LIST_HEAD(name->list);
4387 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4388 if (!CACHE(rdataset))
4390 if (CHECKNAMES(rdataset)) {
4391 char namebuf[DNS_NAME_FORMATSIZE];
4392 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4393 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4395 dns_name_format(name, namebuf, sizeof(namebuf));
4396 dns_rdatatype_format(rdataset->type, typebuf,
4398 dns_rdataclass_format(rdataset->rdclass, classbuf,
4400 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4401 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4402 "check-names %s %s/%s/%s",
4403 fail ? "failure" : "warning",
4404 namebuf, typebuf, classbuf);
4406 if (ANSWER(rdataset)) {
4407 dns_db_detachnode(fctx->cache, &node);
4408 return (DNS_R_BADNAME);
4415 * Enforce the configure maximum cache TTL.
4417 if (rdataset->ttl > res->view->maxcachettl)
4418 rdataset->ttl = res->view->maxcachettl;
4421 * If this RRset is in a secure domain, is in bailiwick,
4422 * and is not glue, attempt DNSSEC validation. (We do not
4423 * attempt to validate glue or out-of-bailiwick data--even
4424 * though there might be some performance benefit to doing
4425 * so--because it makes it simpler and safer to ensure that
4426 * records from a secure domain are only cached if validated
4427 * within the context of a query to the domain that owns
4430 if (secure_domain && rdataset->trust != dns_trust_glue &&
4431 !EXTERNAL(rdataset)) {
4435 * RRSIGs are validated as part of validating the
4438 if (rdataset->type == dns_rdatatype_rrsig)
4441 * Find the SIG for this rdataset, if we have it.
4443 for (sigrdataset = ISC_LIST_HEAD(name->list);
4444 sigrdataset != NULL;
4445 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4446 if (sigrdataset->type == dns_rdatatype_rrsig &&
4447 sigrdataset->covers == rdataset->type)
4450 if (sigrdataset == NULL) {
4451 if (!ANSWER(rdataset) && need_validation) {
4453 * Ignore non-answer rdatasets that
4454 * are missing signatures.
4461 * Normalize the rdataset and sigrdataset TTLs.
4463 if (sigrdataset != NULL) {
4464 rdataset->ttl = ISC_MIN(rdataset->ttl,
4466 sigrdataset->ttl = rdataset->ttl;
4470 * Cache this rdataset/sigrdataset pair as
4471 * pending data. Track whether it was additional
4474 if (rdataset->trust == dns_trust_additional)
4475 trust = dns_trust_pending_additional;
4477 trust = dns_trust_pending_answer;
4479 rdataset->trust = trust;
4480 if (sigrdataset != NULL)
4481 sigrdataset->trust = trust;
4482 if (!need_validation || !ANSWER(rdataset)) {
4483 addedrdataset = ardataset;
4484 result = dns_db_addrdataset(fctx->cache, node,
4485 NULL, now, rdataset,
4487 if (result == DNS_R_UNCHANGED) {
4488 result = ISC_R_SUCCESS;
4489 if (!need_validation &&
4490 ardataset != NULL &&
4491 NEGATIVE(ardataset)) {
4493 * The answer in the cache is
4494 * better than the answer we
4495 * found, and is a negative
4496 * cache entry, so we must set
4497 * eresult appropriately.
4499 if (NXDOMAIN(ardataset))
4501 DNS_R_NCACHENXDOMAIN;
4504 DNS_R_NCACHENXRRSET;
4506 * We have a negative response
4507 * from the cache so don't
4508 * attempt to add the RRSIG
4514 if (result != ISC_R_SUCCESS)
4516 if (sigrdataset != NULL) {
4517 addedrdataset = asigrdataset;
4518 result = dns_db_addrdataset(fctx->cache,
4522 if (result == DNS_R_UNCHANGED)
4523 result = ISC_R_SUCCESS;
4524 if (result != ISC_R_SUCCESS)
4526 } else if (!ANSWER(rdataset))
4530 if (ANSWER(rdataset) && need_validation) {
4531 if (fctx->type != dns_rdatatype_any &&
4532 fctx->type != dns_rdatatype_rrsig &&
4533 fctx->type != dns_rdatatype_sig) {
4535 * This is The Answer. We will
4536 * validate it, but first we cache
4537 * the rest of the response - it may
4538 * contain useful keys.
4540 INSIST(valrdataset == NULL &&
4541 valsigrdataset == NULL);
4542 valrdataset = rdataset;
4543 valsigrdataset = sigrdataset;
4546 * This is one of (potentially)
4547 * multiple answers to an ANY
4548 * or SIG query. To keep things
4549 * simple, we just start the
4550 * validator right away rather
4551 * than caching first and
4552 * having to remember which
4553 * rdatasets needed validation.
4555 result = valcreate(fctx, addrinfo,
4556 name, rdataset->type,
4561 * Defer any further validations.
4562 * This prevents multiple validators
4563 * from manipulating fctx->rmessage
4566 valoptions |= DNS_VALIDATOR_DEFER;
4568 } else if (CHAINING(rdataset)) {
4569 if (rdataset->type == dns_rdatatype_cname)
4570 eresult = DNS_R_CNAME;
4572 INSIST(rdataset->type ==
4573 dns_rdatatype_dname);
4574 eresult = DNS_R_DNAME;
4577 } else if (!EXTERNAL(rdataset)) {
4579 * It's OK to cache this rdataset now.
4581 if (ANSWER(rdataset))
4582 addedrdataset = ardataset;
4583 else if (ANSWERSIG(rdataset))
4584 addedrdataset = asigrdataset;
4586 addedrdataset = NULL;
4587 if (CHAINING(rdataset)) {
4588 if (rdataset->type == dns_rdatatype_cname)
4589 eresult = DNS_R_CNAME;
4591 INSIST(rdataset->type ==
4592 dns_rdatatype_dname);
4593 eresult = DNS_R_DNAME;
4596 if (rdataset->trust == dns_trust_glue &&
4597 (rdataset->type == dns_rdatatype_ns ||
4598 (rdataset->type == dns_rdatatype_rrsig &&
4599 rdataset->covers == dns_rdatatype_ns))) {
4601 * If the trust level is 'dns_trust_glue'
4602 * then we are adding data from a referral
4603 * we got while executing the search algorithm.
4604 * New referral data always takes precedence
4605 * over the existing cache contents.
4607 options = DNS_DBADD_FORCE;
4611 * Now we can add the rdataset.
4613 result = dns_db_addrdataset(fctx->cache,
4618 if (result == DNS_R_UNCHANGED) {
4619 if (ANSWER(rdataset) &&
4620 ardataset != NULL &&
4621 NEGATIVE(ardataset)) {
4623 * The answer in the cache is better
4624 * than the answer we found, and is
4625 * a negative cache entry, so we
4626 * must set eresult appropriately.
4628 if (NXDOMAIN(ardataset))
4629 eresult = DNS_R_NCACHENXDOMAIN;
4631 eresult = DNS_R_NCACHENXRRSET;
4633 result = ISC_R_SUCCESS;
4634 } else if (result != ISC_R_SUCCESS)
4639 if (valrdataset != NULL)
4640 result = valcreate(fctx, addrinfo, name, fctx->type,
4641 valrdataset, valsigrdataset, valoptions,
4644 if (result == ISC_R_SUCCESS && have_answer) {
4645 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4646 if (event != NULL) {
4648 * Negative results must be indicated in event->result.
4650 if (dns_rdataset_isassociated(event->rdataset) &&
4651 NEGATIVE(event->rdataset)) {
4652 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4653 eresult == DNS_R_NCACHENXRRSET);
4655 event->result = eresult;
4656 dns_db_attach(fctx->cache, adbp);
4657 dns_db_transfernode(fctx->cache, &node, anodep);
4658 clone_results(fctx);
4663 dns_db_detachnode(fctx->cache, &node);
4668 static inline isc_result_t
4669 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4671 isc_result_t result;
4672 dns_section_t section;
4675 FCTXTRACE("cache_message");
4677 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4679 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4681 for (section = DNS_SECTION_ANSWER;
4682 section <= DNS_SECTION_ADDITIONAL;
4684 result = dns_message_firstname(fctx->rmessage, section);
4685 while (result == ISC_R_SUCCESS) {
4687 dns_message_currentname(fctx->rmessage, section,
4689 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4690 result = cache_name(fctx, name, addrinfo, now);
4691 if (result != ISC_R_SUCCESS)
4694 result = dns_message_nextname(fctx->rmessage, section);
4696 if (result != ISC_R_NOMORE)
4699 if (result == ISC_R_NOMORE)
4700 result = ISC_R_SUCCESS;
4702 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4708 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4711 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4712 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4713 isc_boolean_t optout, dns_rdataset_t *ardataset,
4714 isc_result_t *eresultp)
4716 isc_result_t result;
4717 dns_rdataset_t rdataset;
4719 if (ardataset == NULL) {
4720 dns_rdataset_init(&rdataset);
4721 ardataset = &rdataset;
4723 result = dns_ncache_addoptout(message, cache, node, covers, now,
4724 maxttl, optout, ardataset);
4725 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4727 * If the cache now contains a negative entry and we
4728 * care about whether it is DNS_R_NCACHENXDOMAIN or
4729 * DNS_R_NCACHENXRRSET then extract it.
4731 if (NEGATIVE(ardataset)) {
4733 * The cache data is a negative cache entry.
4735 if (NXDOMAIN(ardataset))
4736 *eresultp = DNS_R_NCACHENXDOMAIN;
4738 *eresultp = DNS_R_NCACHENXRRSET;
4741 * Either we don't care about the nature of the
4742 * cache rdataset (because no fetch is interested
4743 * in the outcome), or the cache rdataset is not
4744 * a negative cache entry. Whichever case it is,
4745 * we can return success.
4747 * XXXRTH There's a CNAME/DNAME problem here.
4749 *eresultp = ISC_R_SUCCESS;
4751 result = ISC_R_SUCCESS;
4753 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4754 dns_rdataset_disassociate(ardataset);
4759 static inline isc_result_t
4760 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4761 dns_rdatatype_t covers, isc_stdtime_t now)
4763 isc_result_t result, eresult;
4765 dns_resolver_t *res;
4767 dns_dbnode_t *node, **anodep;
4768 dns_rdataset_t *ardataset;
4769 isc_boolean_t need_validation, secure_domain;
4771 dns_fetchevent_t *event;
4773 unsigned int valoptions = 0;
4775 FCTXTRACE("ncache_message");
4777 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4780 need_validation = ISC_FALSE;
4781 POST(need_validation);
4782 secure_domain = ISC_FALSE;
4783 eresult = ISC_R_SUCCESS;
4788 * XXXMPA remove when we follow cnames and adjust the setting
4789 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4791 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4794 * Is DNSSEC validation required for this name?
4796 if (fctx->res->view->enablevalidation) {
4797 result = dns_view_issecuredomain(res->view, name,
4799 if (result != ISC_R_SUCCESS)
4802 if (!secure_domain && res->view->dlv != NULL) {
4803 valoptions = DNS_VALIDATOR_DLV;
4804 secure_domain = ISC_TRUE;
4808 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4809 need_validation = ISC_FALSE;
4811 need_validation = secure_domain;
4813 if (secure_domain) {
4815 * Mark all rdatasets as pending.
4817 dns_rdataset_t *trdataset;
4820 result = dns_message_firstname(fctx->rmessage,
4821 DNS_SECTION_AUTHORITY);
4822 while (result == ISC_R_SUCCESS) {
4824 dns_message_currentname(fctx->rmessage,
4825 DNS_SECTION_AUTHORITY,
4827 for (trdataset = ISC_LIST_HEAD(tname->list);
4829 trdataset = ISC_LIST_NEXT(trdataset, link))
4830 trdataset->trust = dns_trust_pending_answer;
4831 result = dns_message_nextname(fctx->rmessage,
4832 DNS_SECTION_AUTHORITY);
4834 if (result != ISC_R_NOMORE)
4839 if (need_validation) {
4841 * Do negative response validation.
4843 result = valcreate(fctx, addrinfo, name, fctx->type,
4844 NULL, NULL, valoptions,
4845 res->buckets[fctx->bucketnum].task);
4847 * If validation is necessary, return now. Otherwise continue
4848 * to process the message, letting the validation complete
4849 * in its own good time.
4854 LOCK(&res->buckets[fctx->bucketnum].lock);
4860 if (!HAVE_ANSWER(fctx)) {
4861 event = ISC_LIST_HEAD(fctx->events);
4862 if (event != NULL) {
4864 aname = dns_fixedname_name(&event->foundname);
4865 result = dns_name_copy(name, aname, NULL);
4866 if (result != ISC_R_SUCCESS)
4868 anodep = &event->node;
4869 ardataset = event->rdataset;
4874 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4875 if (result != ISC_R_SUCCESS)
4879 * If we are asking for a SOA record set the cache time
4880 * to zero to facilitate locating the containing zone of
4883 ttl = fctx->res->view->maxncachettl;
4884 if (fctx->type == dns_rdatatype_soa &&
4885 covers == dns_rdatatype_any &&
4886 fctx->res->zero_no_soa_ttl)
4889 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4890 covers, now, ttl, ISC_FALSE,
4891 ardataset, &eresult);
4892 if (result != ISC_R_SUCCESS)
4895 if (!HAVE_ANSWER(fctx)) {
4896 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4897 if (event != NULL) {
4898 event->result = eresult;
4899 dns_db_attach(fctx->cache, adbp);
4900 dns_db_transfernode(fctx->cache, &node, anodep);
4901 clone_results(fctx);
4906 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4909 dns_db_detachnode(fctx->cache, &node);
4915 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4916 isc_boolean_t external, isc_boolean_t gluing)
4918 name->attributes |= DNS_NAMEATTR_CACHE;
4920 rdataset->trust = dns_trust_glue;
4922 * Glue with 0 TTL causes problems. We force the TTL to
4923 * 1 second to prevent this.
4925 if (rdataset->ttl == 0)
4928 rdataset->trust = dns_trust_additional;
4930 * Avoid infinite loops by only marking new rdatasets.
4932 if (!CACHE(rdataset)) {
4933 name->attributes |= DNS_NAMEATTR_CHASE;
4934 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4936 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4938 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4942 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
4943 dns_section_t section)
4945 fetchctx_t *fctx = arg;
4946 isc_result_t result;
4948 dns_rdataset_t *rdataset;
4949 isc_boolean_t external;
4950 dns_rdatatype_t rtype;
4951 isc_boolean_t gluing;
4953 REQUIRE(VALID_FCTX(fctx));
4955 #if CHECK_FOR_GLUE_IN_ANSWER
4956 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
4957 return (ISC_R_SUCCESS);
4966 result = dns_message_findname(fctx->rmessage, section, addname,
4967 dns_rdatatype_any, 0, &name, NULL);
4968 if (result == ISC_R_SUCCESS) {
4969 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
4970 if (type == dns_rdatatype_a) {
4971 for (rdataset = ISC_LIST_HEAD(name->list);
4973 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4974 if (rdataset->type == dns_rdatatype_rrsig)
4975 rtype = rdataset->covers;
4977 rtype = rdataset->type;
4978 if (rtype == dns_rdatatype_a ||
4979 rtype == dns_rdatatype_aaaa)
4980 mark_related(name, rdataset, external,
4984 result = dns_message_findtype(name, type, 0,
4986 if (result == ISC_R_SUCCESS) {
4987 mark_related(name, rdataset, external, gluing);
4989 * Do we have its SIG too?
4992 result = dns_message_findtype(name,
4993 dns_rdatatype_rrsig,
4995 if (result == ISC_R_SUCCESS)
4996 mark_related(name, rdataset, external,
5002 return (ISC_R_SUCCESS);
5006 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5007 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5010 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5011 #define CHECK_FOR_GLUE_IN_ANSWER 0
5013 #if CHECK_FOR_GLUE_IN_ANSWER
5015 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5016 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5021 chase_additional(fetchctx_t *fctx) {
5022 isc_boolean_t rescan;
5023 dns_section_t section = DNS_SECTION_ADDITIONAL;
5024 isc_result_t result;
5029 for (result = dns_message_firstname(fctx->rmessage, section);
5030 result == ISC_R_SUCCESS;
5031 result = dns_message_nextname(fctx->rmessage, section)) {
5032 dns_name_t *name = NULL;
5033 dns_rdataset_t *rdataset;
5034 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5036 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5038 name->attributes &= ~DNS_NAMEATTR_CHASE;
5039 for (rdataset = ISC_LIST_HEAD(name->list);
5041 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5042 if (CHASE(rdataset)) {
5043 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5044 (void)dns_rdataset_additionaldata(rdataset,
5055 static inline isc_result_t
5056 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5057 isc_result_t result;
5058 dns_rdata_t rdata = DNS_RDATA_INIT;
5059 dns_rdata_cname_t cname;
5061 result = dns_rdataset_first(rdataset);
5062 if (result != ISC_R_SUCCESS)
5064 dns_rdataset_current(rdataset, &rdata);
5065 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5066 if (result != ISC_R_SUCCESS)
5068 dns_name_init(tname, NULL);
5069 dns_name_clone(&cname.cname, tname);
5070 dns_rdata_freestruct(&cname);
5072 return (ISC_R_SUCCESS);
5075 static inline isc_result_t
5076 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5077 dns_name_t *oname, dns_fixedname_t *fixeddname)
5079 isc_result_t result;
5080 dns_rdata_t rdata = DNS_RDATA_INIT;
5081 unsigned int nlabels;
5083 dns_namereln_t namereln;
5084 dns_rdata_dname_t dname;
5085 dns_fixedname_t prefix;
5088 * Get the target name of the DNAME.
5090 result = dns_rdataset_first(rdataset);
5091 if (result != ISC_R_SUCCESS)
5093 dns_rdataset_current(rdataset, &rdata);
5094 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5095 if (result != ISC_R_SUCCESS)
5099 * Get the prefix of qname.
5101 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5102 if (namereln != dns_namereln_subdomain) {
5103 char qbuf[DNS_NAME_FORMATSIZE];
5104 char obuf[DNS_NAME_FORMATSIZE];
5106 dns_rdata_freestruct(&dname);
5107 dns_name_format(qname, qbuf, sizeof(qbuf));
5108 dns_name_format(oname, obuf, sizeof(obuf));
5109 log_formerr(fctx, "unrelated DNAME in answer: "
5110 "%s is not in %s", qbuf, obuf);
5111 return (DNS_R_FORMERR);
5113 dns_fixedname_init(&prefix);
5114 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5115 dns_fixedname_init(fixeddname);
5116 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5118 dns_fixedname_name(fixeddname), NULL);
5119 dns_rdata_freestruct(&dname);
5123 static isc_boolean_t
5124 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5125 dns_rdataset_t *rdataset)
5127 isc_result_t result;
5128 dns_rdata_t rdata = DNS_RDATA_INIT;
5130 struct in6_addr in6a;
5131 isc_netaddr_t netaddr;
5132 char addrbuf[ISC_NETADDR_FORMATSIZE];
5133 char namebuf[DNS_NAME_FORMATSIZE];
5138 /* By default, we allow any addresses. */
5139 if (view->denyansweracl == NULL)
5143 * If the owner name matches one in the exclusion list, either exactly
5144 * or partially, allow it.
5146 if (view->answeracl_exclude != NULL) {
5147 dns_rbtnode_t *node = NULL;
5149 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5150 &node, NULL, 0, NULL, NULL);
5152 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5157 * Otherwise, search the filter list for a match for each address
5158 * record. If a match is found, the address should be filtered,
5159 * so should the entire answer.
5161 for (result = dns_rdataset_first(rdataset);
5162 result == ISC_R_SUCCESS;
5163 result = dns_rdataset_next(rdataset)) {
5164 dns_rdata_reset(&rdata);
5165 dns_rdataset_current(rdataset, &rdata);
5166 if (rdataset->type == dns_rdatatype_a) {
5167 INSIST(rdata.length == sizeof(ina.s_addr));
5168 memcpy(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5169 isc_netaddr_fromin(&netaddr, &ina);
5171 INSIST(rdata.length == sizeof(in6a.s6_addr));
5172 memcpy(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5173 isc_netaddr_fromin6(&netaddr, &in6a);
5176 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5177 &view->aclenv, &match, NULL);
5179 if (result == ISC_R_SUCCESS && match > 0) {
5180 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5181 dns_name_format(name, namebuf, sizeof(namebuf));
5182 dns_rdatatype_format(rdataset->type, typebuf,
5184 dns_rdataclass_format(rdataset->rdclass, classbuf,
5186 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5187 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5188 "answer address %s denied for %s/%s/%s",
5189 addrbuf, namebuf, typebuf, classbuf);
5197 static isc_boolean_t
5198 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5199 dns_rdatatype_t type, dns_name_t *tname,
5202 isc_result_t result;
5203 dns_rbtnode_t *node = NULL;
5204 char qnamebuf[DNS_NAME_FORMATSIZE];
5205 char tnamebuf[DNS_NAME_FORMATSIZE];
5209 /* By default, we allow any target name. */
5210 if (view->denyanswernames == NULL)
5214 * If the owner name matches one in the exclusion list, either exactly
5215 * or partially, allow it.
5217 if (view->answernames_exclude != NULL) {
5218 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5219 &node, NULL, 0, NULL, NULL);
5220 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5225 * If the target name is a subdomain of the search domain, allow it.
5227 if (dns_name_issubdomain(tname, domain))
5231 * Otherwise, apply filters.
5233 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5234 NULL, 0, NULL, NULL);
5235 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5236 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5237 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5238 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5239 dns_rdataclass_format(view->rdclass, classbuf,
5241 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5242 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5243 "%s target %s denied for %s/%s",
5244 typebuf, tnamebuf, qnamebuf, classbuf);
5252 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5253 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5254 * section for the NS RRset if the query type is NS; if it has
5255 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5256 * section for A and AAAA queries.
5258 #define LOOK_FOR_NS_IN_ANSWER 0x1
5259 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5262 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5263 unsigned int look_in_options)
5265 isc_result_t result;
5266 dns_message_t *message;
5267 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
5268 dns_rdataset_t *rdataset, *ns_rdataset;
5269 isc_boolean_t aa, negative_response;
5270 dns_rdatatype_t type;
5271 dns_section_t section;
5273 FCTXTRACE("noanswer_response");
5275 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5276 INSIST(fctx->type == dns_rdatatype_ns);
5277 section = DNS_SECTION_ANSWER;
5279 section = DNS_SECTION_AUTHORITY;
5281 message = fctx->rmessage;
5286 if (oqname == NULL) {
5288 * We have a normal, non-chained negative response or
5291 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5295 qname = &fctx->name;
5298 * We're being invoked by answer_response() after it has
5299 * followed a CNAME/DNAME chain.
5304 * If the current qname is not a subdomain of the query
5305 * domain, there's no point in looking at the authority
5306 * section without doing DNSSEC validation.
5308 * Until we do that validation, we'll just return success
5311 if (!dns_name_issubdomain(qname, &fctx->domain))
5312 return (ISC_R_SUCCESS);
5316 * We have to figure out if this is a negative response, or a
5321 * Sometimes we can tell if its a negative response by looking at
5322 * the message header.
5324 negative_response = ISC_FALSE;
5325 if (message->rcode == dns_rcode_nxdomain ||
5326 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5327 message->counts[DNS_SECTION_AUTHORITY] == 0))
5328 negative_response = ISC_TRUE;
5331 * Process the authority section.
5337 result = dns_message_firstname(message, section);
5338 while (result == ISC_R_SUCCESS) {
5340 dns_message_currentname(message, section, &name);
5341 if (dns_name_issubdomain(name, &fctx->domain)) {
5343 * Look for NS/SOA RRsets first.
5345 for (rdataset = ISC_LIST_HEAD(name->list);
5347 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5348 type = rdataset->type;
5349 if (type == dns_rdatatype_rrsig)
5350 type = rdataset->covers;
5351 if (((type == dns_rdatatype_ns ||
5352 type == dns_rdatatype_soa) &&
5353 !dns_name_issubdomain(qname, name))) {
5354 char qbuf[DNS_NAME_FORMATSIZE];
5355 char nbuf[DNS_NAME_FORMATSIZE];
5356 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5357 dns_rdatatype_format(fctx->type, tbuf,
5359 dns_name_format(name, nbuf,
5361 dns_name_format(qname, qbuf,
5364 "unrelated %s %s in "
5365 "%s authority section",
5367 return (DNS_R_FORMERR);
5369 if (type == dns_rdatatype_ns) {
5373 * Only one set of NS RRs is allowed.
5375 if (rdataset->type ==
5377 if (ns_name != NULL &&
5384 return (DNS_R_FORMERR);
5387 ns_rdataset = rdataset;
5391 rdataset->attributes |=
5392 DNS_RDATASETATTR_CACHE;
5393 rdataset->trust = dns_trust_glue;
5395 if (type == dns_rdatatype_soa) {
5397 * SOA, or RRSIG SOA.
5399 * Only one SOA is allowed.
5401 if (rdataset->type ==
5402 dns_rdatatype_soa) {
5403 if (soa_name != NULL &&
5410 return (DNS_R_FORMERR);
5415 DNS_NAMEATTR_NCACHE;
5416 rdataset->attributes |=
5417 DNS_RDATASETATTR_NCACHE;
5420 dns_trust_authauthority;
5423 dns_trust_additional;
5427 result = dns_message_nextname(message, section);
5428 if (result == ISC_R_NOMORE)
5430 else if (result != ISC_R_SUCCESS)
5435 * A negative response has a SOA record (Type 2)
5436 * and a optional NS RRset (Type 1) or it has neither
5437 * a SOA or a NS RRset (Type 3, handled above) or
5438 * rcode is NXDOMAIN (handled above) in which case
5439 * the NS RRset is allowed (Type 4).
5441 if (soa_name != NULL)
5442 negative_response = ISC_TRUE;
5444 result = dns_message_firstname(message, section);
5445 while (result == ISC_R_SUCCESS) {
5447 dns_message_currentname(message, section, &name);
5448 if (dns_name_issubdomain(name, &fctx->domain)) {
5449 for (rdataset = ISC_LIST_HEAD(name->list);
5451 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5452 type = rdataset->type;
5453 if (type == dns_rdatatype_rrsig)
5454 type = rdataset->covers;
5455 if (type == dns_rdatatype_nsec ||
5456 type == dns_rdatatype_nsec3) {
5458 * NSEC or RRSIG NSEC.
5460 if (negative_response) {
5462 DNS_NAMEATTR_NCACHE;
5463 rdataset->attributes |=
5464 DNS_RDATASETATTR_NCACHE;
5465 } else if (type == dns_rdatatype_nsec) {
5468 rdataset->attributes |=
5469 DNS_RDATASETATTR_CACHE;
5473 dns_trust_authauthority;
5476 dns_trust_additional;
5478 * No additional data needs to be
5481 } else if (type == dns_rdatatype_ds) {
5485 * These should only be here if
5486 * this is a referral, and there
5487 * should only be one DS RRset.
5489 if (ns_name == NULL) {
5493 return (DNS_R_FORMERR);
5495 if (rdataset->type ==
5497 if (ds_name != NULL &&
5504 return (DNS_R_FORMERR);
5510 rdataset->attributes |=
5511 DNS_RDATASETATTR_CACHE;
5514 dns_trust_authauthority;
5517 dns_trust_additional;
5521 result = dns_message_nextname(message, section);
5522 if (result == ISC_R_NOMORE)
5524 else if (result != ISC_R_SUCCESS)
5529 * Trigger lookups for DNS nameservers.
5531 if (negative_response && message->rcode == dns_rcode_noerror &&
5532 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5533 dns_name_equal(soa_name, qname) &&
5534 !dns_name_equal(qname, dns_rootname))
5535 return (DNS_R_CHASEDSSERVERS);
5538 * Did we find anything?
5540 if (!negative_response && ns_name == NULL) {
5544 if (oqname != NULL) {
5546 * We've already got a partial CNAME/DNAME chain,
5547 * and haven't found else anything useful here, but
5548 * no error has occurred since we have an answer.
5550 return (ISC_R_SUCCESS);
5553 * The responder is insane.
5555 log_formerr(fctx, "invalid response");
5556 return (DNS_R_FORMERR);
5561 * If we found both NS and SOA, they should be the same name.
5563 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5564 log_formerr(fctx, "NS/SOA mismatch");
5565 return (DNS_R_FORMERR);
5569 * Do we have a referral? (We only want to follow a referral if
5570 * we're not following a chain.)
5572 if (!negative_response && ns_name != NULL && oqname == NULL) {
5574 * We already know ns_name is a subdomain of fctx->domain.
5575 * If ns_name is equal to fctx->domain, we're not making
5576 * progress. We return DNS_R_FORMERR so that we'll keep
5577 * trying other servers.
5579 if (dns_name_equal(ns_name, &fctx->domain)) {
5580 log_formerr(fctx, "non-improving referral");
5581 return (DNS_R_FORMERR);
5585 * If the referral name is not a parent of the query
5586 * name, consider the responder insane.
5588 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5590 log_formerr(fctx, "referral to non-parent");
5591 FCTXTRACE("referral to non-parent");
5592 return (DNS_R_FORMERR);
5596 * Mark any additional data related to this rdataset.
5597 * It's important that we do this before we change the
5600 INSIST(ns_rdataset != NULL);
5601 fctx->attributes |= FCTX_ATTR_GLUING;
5602 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5604 #if CHECK_FOR_GLUE_IN_ANSWER
5606 * Look in the answer section for "glue" that is incorrectly
5607 * returned as a answer. This is needed if the server also
5608 * minimizes the response size by not adding records to the
5609 * additional section that are in the answer section or if
5610 * the record gets dropped due to message size constraints.
5612 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
5613 (fctx->type == dns_rdatatype_aaaa ||
5614 fctx->type == dns_rdatatype_a))
5615 (void)dns_rdataset_additionaldata(ns_rdataset,
5616 check_answer, fctx);
5618 fctx->attributes &= ~FCTX_ATTR_GLUING;
5620 * NS rdatasets with 0 TTL cause problems.
5621 * dns_view_findzonecut() will not find them when we
5622 * try to follow the referral, and we'll SERVFAIL
5623 * because the best nameservers are now above QDOMAIN.
5624 * We force the TTL to 1 second to prevent this.
5626 if (ns_rdataset->ttl == 0)
5627 ns_rdataset->ttl = 1;
5629 * Set the current query domain to the referral name.
5631 * XXXRTH We should check if we're in forward-only mode, and
5632 * if so we should bail out.
5634 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5635 dns_name_free(&fctx->domain,
5636 fctx->res->buckets[fctx->bucketnum].mctx);
5637 if (dns_rdataset_isassociated(&fctx->nameservers))
5638 dns_rdataset_disassociate(&fctx->nameservers);
5639 dns_name_init(&fctx->domain, NULL);
5640 result = dns_name_dup(ns_name,
5641 fctx->res->buckets[fctx->bucketnum].mctx,
5643 if (result != ISC_R_SUCCESS)
5645 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5646 return (DNS_R_DELEGATION);
5650 * Since we're not doing a referral, we don't want to cache any
5651 * NS RRs we may have found.
5653 if (ns_name != NULL)
5654 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5656 if (negative_response && oqname == NULL)
5657 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5659 return (ISC_R_SUCCESS);
5663 answer_response(fetchctx_t *fctx) {
5664 isc_result_t result;
5665 dns_message_t *message;
5666 dns_name_t *name, *qname, tname;
5667 dns_rdataset_t *rdataset;
5668 isc_boolean_t done, external, chaining, aa, found, want_chaining;
5669 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5671 dns_rdatatype_t type;
5672 dns_fixedname_t dname, fqname;
5675 FCTXTRACE("answer_response");
5677 message = fctx->rmessage;
5680 * Examine the answer section, marking those rdatasets which are
5681 * part of the answer and should be cached.
5685 found_cname = ISC_FALSE;
5686 found_type = ISC_FALSE;
5687 chaining = ISC_FALSE;
5688 have_answer = ISC_FALSE;
5689 want_chaining = ISC_FALSE;
5690 POST(want_chaining);
5691 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5695 qname = &fctx->name;
5697 view = fctx->res->view;
5698 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5699 while (!done && result == ISC_R_SUCCESS) {
5701 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5702 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5703 if (dns_name_equal(name, qname)) {
5704 wanted_chaining = ISC_FALSE;
5705 for (rdataset = ISC_LIST_HEAD(name->list);
5707 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5709 want_chaining = ISC_FALSE;
5711 if (rdataset->type == dns_rdatatype_nsec3) {
5713 * NSEC3 records are not allowed to
5714 * appear in the answer section.
5716 log_formerr(fctx, "NSEC3 in answer");
5717 return (DNS_R_FORMERR);
5721 * Apply filters, if given, on answers to reject
5722 * a malicious attempt of rebinding.
5724 if ((rdataset->type == dns_rdatatype_a ||
5725 rdataset->type == dns_rdatatype_aaaa) &&
5726 !is_answeraddress_allowed(view, name,
5728 return (DNS_R_SERVFAIL);
5731 if (rdataset->type == type && !found_cname) {
5733 * We've found an ordinary answer.
5736 found_type = ISC_TRUE;
5738 aflag = DNS_RDATASETATTR_ANSWER;
5739 } else if (type == dns_rdatatype_any) {
5741 * We've found an answer matching
5742 * an ANY query. There may be
5746 aflag = DNS_RDATASETATTR_ANSWER;
5747 } else if (rdataset->type == dns_rdatatype_rrsig
5748 && rdataset->covers == type
5751 * We've found a signature that
5752 * covers the type we're looking for.
5755 found_type = ISC_TRUE;
5756 aflag = DNS_RDATASETATTR_ANSWERSIG;
5757 } else if (rdataset->type ==
5761 * We're looking for something else,
5762 * but we found a CNAME.
5764 * Getting a CNAME response for some
5765 * query types is an error.
5767 if (type == dns_rdatatype_rrsig ||
5768 type == dns_rdatatype_dnskey ||
5769 type == dns_rdatatype_nsec ||
5770 type == dns_rdatatype_nsec3) {
5771 char buf[DNS_RDATATYPE_FORMATSIZE];
5772 dns_rdatatype_format(fctx->type,
5777 return (DNS_R_FORMERR);
5780 found_cname = ISC_TRUE;
5781 want_chaining = ISC_TRUE;
5782 aflag = DNS_RDATASETATTR_ANSWER;
5783 result = cname_target(rdataset,
5785 if (result != ISC_R_SUCCESS)
5787 /* Apply filters on the target name. */
5788 if (!is_answertarget_allowed(view,
5793 return (DNS_R_SERVFAIL);
5795 } else if (rdataset->type == dns_rdatatype_rrsig
5796 && rdataset->covers ==
5800 * We're looking for something else,
5801 * but we found a SIG CNAME.
5804 found_cname = ISC_TRUE;
5805 aflag = DNS_RDATASETATTR_ANSWERSIG;
5810 * We've found an answer to our
5815 rdataset->attributes |=
5816 DNS_RDATASETATTR_CACHE;
5817 rdataset->trust = dns_trust_answer;
5820 * This data is "the" answer
5821 * to our question only if
5822 * we're not chaining (i.e.
5823 * if we haven't followed
5824 * a CNAME or DNAME).
5828 DNS_RDATASETATTR_ANSWER)
5829 have_answer = ISC_TRUE;
5831 DNS_NAMEATTR_ANSWER;
5832 rdataset->attributes |= aflag;
5835 dns_trust_authanswer;
5836 } else if (external) {
5838 * This data is outside of
5839 * our query domain, and
5840 * may not be cached.
5842 rdataset->attributes |=
5843 DNS_RDATASETATTR_EXTERNAL;
5847 * Mark any additional data related
5850 (void)dns_rdataset_additionaldata(
5858 if (want_chaining) {
5859 wanted_chaining = ISC_TRUE;
5861 DNS_NAMEATTR_CHAINING;
5862 rdataset->attributes |=
5863 DNS_RDATASETATTR_CHAINING;
5868 * We could add an "else" clause here and
5869 * log that we're ignoring this rdataset.
5873 * If wanted_chaining is true, we've done
5874 * some chaining as the result of processing
5875 * this node, and thus we need to set
5878 * We don't set chaining inside of the
5879 * rdataset loop because doing that would
5880 * cause us to ignore the signatures of
5883 if (wanted_chaining)
5884 chaining = ISC_TRUE;
5887 * Look for a DNAME (or its SIG). Anything else is
5890 wanted_chaining = ISC_FALSE;
5891 for (rdataset = ISC_LIST_HEAD(name->list);
5893 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5894 isc_boolean_t found_dname = ISC_FALSE;
5895 dns_name_t *dname_name;
5899 if (rdataset->type == dns_rdatatype_dname) {
5901 * We're looking for something else,
5902 * but we found a DNAME.
5904 * If we're not chaining, then the
5905 * DNAME should not be external.
5907 if (!chaining && external) {
5910 return (DNS_R_FORMERR);
5913 want_chaining = ISC_TRUE;
5914 POST(want_chaining);
5915 aflag = DNS_RDATASETATTR_ANSWER;
5916 result = dname_target(fctx, rdataset,
5919 if (result == ISC_R_NOSPACE) {
5921 * We can't construct the
5922 * DNAME target. Do not
5925 want_chaining = ISC_FALSE;
5926 POST(want_chaining);
5927 } else if (result != ISC_R_SUCCESS)
5930 found_dname = ISC_TRUE;
5932 dname_name = dns_fixedname_name(&dname);
5933 if (!is_answertarget_allowed(view,
5938 return (DNS_R_SERVFAIL);
5940 } else if (rdataset->type == dns_rdatatype_rrsig
5941 && rdataset->covers ==
5942 dns_rdatatype_dname) {
5944 * We've found a signature that
5948 aflag = DNS_RDATASETATTR_ANSWERSIG;
5953 * We've found an answer to our
5958 rdataset->attributes |=
5959 DNS_RDATASETATTR_CACHE;
5960 rdataset->trust = dns_trust_answer;
5963 * This data is "the" answer
5964 * to our question only if
5965 * we're not chaining.
5969 DNS_RDATASETATTR_ANSWER)
5970 have_answer = ISC_TRUE;
5972 DNS_NAMEATTR_ANSWER;
5973 rdataset->attributes |= aflag;
5976 dns_trust_authanswer;
5977 } else if (external) {
5978 rdataset->attributes |=
5979 DNS_RDATASETATTR_EXTERNAL;
5987 * Copy the dname into the
5990 * Although we check for
5991 * failure of the copy
5992 * operation, in practice it
5993 * should never fail since
5994 * we already know that the
5995 * result fits in a fixedname.
5997 dns_fixedname_init(&fqname);
5998 result = dns_name_copy(
5999 dns_fixedname_name(&dname),
6000 dns_fixedname_name(&fqname),
6002 if (result != ISC_R_SUCCESS)
6004 wanted_chaining = ISC_TRUE;
6006 DNS_NAMEATTR_CHAINING;
6007 rdataset->attributes |=
6008 DNS_RDATASETATTR_CHAINING;
6009 qname = dns_fixedname_name(
6014 if (wanted_chaining)
6015 chaining = ISC_TRUE;
6017 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6019 if (result == ISC_R_NOMORE)
6020 result = ISC_R_SUCCESS;
6021 if (result != ISC_R_SUCCESS)
6025 * We should have found an answer.
6028 log_formerr(fctx, "reply has no answer");
6029 return (DNS_R_FORMERR);
6033 * This response is now potentially cacheable.
6035 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6038 * Did chaining end before we got the final answer?
6042 * Yes. This may be a negative reply, so hand off
6043 * authority section processing to the noanswer code.
6044 * If it isn't a noanswer response, no harm will be
6047 return (noanswer_response(fctx, qname, 0));
6051 * We didn't end with an incomplete chain, so the rcode should be
6054 if (message->rcode != dns_rcode_noerror) {
6055 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6057 return (DNS_R_FORMERR);
6061 * Examine the authority section (if there is one).
6063 * We expect there to be only one owner name for all the rdatasets
6064 * in this section, and we expect that it is not external.
6067 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6068 while (!done && result == ISC_R_SUCCESS) {
6070 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6071 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6074 * We expect to find NS or SIG NS rdatasets, and
6077 for (rdataset = ISC_LIST_HEAD(name->list);
6079 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6080 if (rdataset->type == dns_rdatatype_ns ||
6081 (rdataset->type == dns_rdatatype_rrsig &&
6082 rdataset->covers == dns_rdatatype_ns)) {
6085 rdataset->attributes |=
6086 DNS_RDATASETATTR_CACHE;
6087 if (aa && !chaining)
6089 dns_trust_authauthority;
6092 dns_trust_additional;
6095 * Mark any additional data related
6098 (void)dns_rdataset_additionaldata(
6106 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6108 if (result == ISC_R_NOMORE)
6109 result = ISC_R_SUCCESS;
6114 static isc_boolean_t
6115 fctx_decreference(fetchctx_t *fctx) {
6116 isc_boolean_t bucket_empty = ISC_FALSE;
6118 INSIST(fctx->references > 0);
6120 if (fctx->references == 0) {
6122 * No one cares about the result of this fetch anymore.
6124 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6125 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6127 * This fctx is already shutdown; we were just
6128 * waiting for the last reference to go away.
6130 bucket_empty = fctx_destroy(fctx);
6133 * Initiate shutdown.
6135 fctx_shutdown(fctx);
6138 return (bucket_empty);
6142 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6143 dns_fetchevent_t *fevent;
6144 dns_resolver_t *res;
6146 isc_result_t result;
6147 isc_boolean_t bucket_empty;
6148 isc_boolean_t locked = ISC_FALSE;
6149 unsigned int bucketnum;
6150 dns_rdataset_t nameservers;
6151 dns_fixedname_t fixed;
6154 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6155 fevent = (dns_fetchevent_t *)event;
6156 fctx = event->ev_arg;
6157 REQUIRE(VALID_FCTX(fctx));
6161 FCTXTRACE("resume_dslookup");
6163 if (fevent->node != NULL)
6164 dns_db_detachnode(fevent->db, &fevent->node);
6165 if (fevent->db != NULL)
6166 dns_db_detach(&fevent->db);
6168 dns_rdataset_init(&nameservers);
6170 bucketnum = fctx->bucketnum;
6171 if (fevent->result == ISC_R_CANCELED) {
6172 dns_resolver_destroyfetch(&fctx->nsfetch);
6173 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6174 } else if (fevent->result == ISC_R_SUCCESS) {
6176 FCTXTRACE("resuming DS lookup");
6178 dns_resolver_destroyfetch(&fctx->nsfetch);
6179 if (dns_rdataset_isassociated(&fctx->nameservers))
6180 dns_rdataset_disassociate(&fctx->nameservers);
6181 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6182 dns_name_free(&fctx->domain,
6183 fctx->res->buckets[bucketnum].mctx);
6184 dns_name_init(&fctx->domain, NULL);
6185 result = dns_name_dup(&fctx->nsname,
6186 fctx->res->buckets[bucketnum].mctx,
6188 if (result != ISC_R_SUCCESS) {
6189 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6195 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6198 dns_rdataset_t *nsrdataset = NULL;
6201 * Retrieve state from fctx->nsfetch before we destroy it.
6203 dns_fixedname_init(&fixed);
6204 domain = dns_fixedname_name(&fixed);
6205 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6206 if (dns_name_equal(&fctx->nsname, domain)) {
6207 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6208 dns_resolver_destroyfetch(&fctx->nsfetch);
6211 if (dns_rdataset_isassociated(
6212 &fctx->nsfetch->private->nameservers)) {
6214 &fctx->nsfetch->private->nameservers,
6216 nsrdataset = &nameservers;
6219 dns_resolver_destroyfetch(&fctx->nsfetch);
6220 n = dns_name_countlabels(&fctx->nsname);
6221 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6224 if (dns_rdataset_isassociated(fevent->rdataset))
6225 dns_rdataset_disassociate(fevent->rdataset);
6226 FCTXTRACE("continuing to look for parent's NS records");
6227 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6228 dns_rdatatype_ns, domain,
6229 nsrdataset, NULL, 0, task,
6230 resume_dslookup, fctx,
6231 &fctx->nsrrset, NULL,
6233 if (result != ISC_R_SUCCESS)
6234 fctx_done(fctx, result, __LINE__);
6236 LOCK(&res->buckets[bucketnum].lock);
6243 if (dns_rdataset_isassociated(&nameservers))
6244 dns_rdataset_disassociate(&nameservers);
6245 if (dns_rdataset_isassociated(fevent->rdataset))
6246 dns_rdataset_disassociate(fevent->rdataset);
6247 INSIST(fevent->sigrdataset == NULL);
6248 isc_event_free(&event);
6250 LOCK(&res->buckets[bucketnum].lock);
6251 bucket_empty = fctx_decreference(fctx);
6252 UNLOCK(&res->buckets[bucketnum].lock);
6258 checknamessection(dns_message_t *message, dns_section_t section) {
6259 isc_result_t result;
6261 dns_rdata_t rdata = DNS_RDATA_INIT;
6262 dns_rdataset_t *rdataset;
6264 for (result = dns_message_firstname(message, section);
6265 result == ISC_R_SUCCESS;
6266 result = dns_message_nextname(message, section))
6269 dns_message_currentname(message, section, &name);
6270 for (rdataset = ISC_LIST_HEAD(name->list);
6272 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6273 for (result = dns_rdataset_first(rdataset);
6274 result == ISC_R_SUCCESS;
6275 result = dns_rdataset_next(rdataset)) {
6276 dns_rdataset_current(rdataset, &rdata);
6277 if (!dns_rdata_checkowner(name, rdata.rdclass,
6280 !dns_rdata_checknames(&rdata, name, NULL))
6282 rdataset->attributes |=
6283 DNS_RDATASETATTR_CHECKNAMES;
6285 dns_rdata_reset(&rdata);
6292 checknames(dns_message_t *message) {
6294 checknamessection(message, DNS_SECTION_ANSWER);
6295 checknamessection(message, DNS_SECTION_AUTHORITY);
6296 checknamessection(message, DNS_SECTION_ADDITIONAL);
6300 * Log server NSID at log level 'level'
6303 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
6305 static const char hex[17] = "0123456789abcdef";
6306 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6307 isc_uint16_t optcode, nsid_len, buflen, i;
6308 isc_result_t result;
6309 isc_buffer_t nsidbuf;
6311 unsigned char *p, *buf, *nsid;
6313 /* Extract rdata from OPT rdataset */
6314 result = dns_rdataset_first(opt);
6315 if (result != ISC_R_SUCCESS)
6316 return (ISC_R_FAILURE);
6318 dns_rdata_init(&rdata);
6319 dns_rdataset_current(opt, &rdata);
6320 if (rdata.length < 4)
6321 return (ISC_R_FAILURE);
6323 /* Check for NSID */
6324 isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
6325 isc_buffer_add(&nsidbuf, rdata.length);
6326 optcode = isc_buffer_getuint16(&nsidbuf);
6327 nsid_len = isc_buffer_getuint16(&nsidbuf);
6328 if (optcode != DNS_OPT_NSID || nsid_len == 0)
6329 return (ISC_R_FAILURE);
6331 /* Allocate buffer for storing hex version of the NSID */
6332 buflen = nsid_len * 2 + 1;
6333 buf = isc_mem_get(mctx, buflen);
6335 return (ISC_R_NOSPACE);
6337 /* Convert to hex */
6339 nsid = rdata.data + 4;
6340 for (i = 0; i < nsid_len; i++) {
6341 *p++ = hex[(nsid[0] >> 4) & 0xf];
6342 *p++ = hex[nsid[0] & 0xf];
6347 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6349 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6350 DNS_LOGMODULE_RESOLVER, level,
6351 "received NSID '%s' from %s", buf, addrbuf);
6354 isc_mem_put(mctx, buf, buflen);
6355 return (ISC_R_SUCCESS);
6359 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6360 isc_buffer_t buffer;
6363 isc_result_t result;
6365 if (! isc_log_wouldlog(dns_lctx, level))
6369 * Note that these are multiline debug messages. We want a newline
6370 * to appear in the log after each message.
6374 buf = isc_mem_get(mctx, len);
6377 isc_buffer_init(&buffer, buf, len);
6378 result = dns_message_totext(message, &dns_master_style_debug,
6380 if (result == ISC_R_NOSPACE) {
6381 isc_mem_put(mctx, buf, len);
6383 } else if (result == ISC_R_SUCCESS)
6384 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6385 DNS_LOGMODULE_RESOLVER, level,
6386 "received packet:\n%.*s",
6387 (int)isc_buffer_usedlength(&buffer),
6389 } while (result == ISC_R_NOSPACE);
6392 isc_mem_put(mctx, buf, len);
6395 static isc_boolean_t
6396 iscname(fetchctx_t *fctx) {
6397 isc_result_t result;
6399 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6400 &fctx->name, dns_rdatatype_cname, 0,
6402 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6405 static isc_boolean_t
6406 betterreferral(fetchctx_t *fctx) {
6407 isc_result_t result;
6409 dns_rdataset_t *rdataset;
6410 dns_message_t *message = fctx->rmessage;
6412 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6413 result == ISC_R_SUCCESS;
6414 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6416 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6417 if (!isstrictsubdomain(name, &fctx->domain))
6419 for (rdataset = ISC_LIST_HEAD(name->list);
6421 rdataset = ISC_LIST_NEXT(rdataset, link))
6422 if (rdataset->type == dns_rdatatype_ns)
6429 resquery_response(isc_task_t *task, isc_event_t *event) {
6430 isc_result_t result = ISC_R_SUCCESS;
6431 resquery_t *query = event->ev_arg;
6432 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6433 isc_boolean_t keep_trying, get_nameservers, resend;
6434 isc_boolean_t truncated;
6435 dns_message_t *message;
6436 dns_rdataset_t *opt;
6439 dns_fixedname_t foundname;
6441 isc_time_t tnow, *finish;
6442 dns_adbaddrinfo_t *addrinfo;
6443 unsigned int options;
6444 unsigned int findoptions;
6445 isc_result_t broken_server;
6446 badnstype_t broken_type = badns_response;
6447 isc_boolean_t no_response;
6449 REQUIRE(VALID_QUERY(query));
6451 options = query->options;
6452 REQUIRE(VALID_FCTX(fctx));
6453 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6457 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6458 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6460 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6462 (void)isc_timer_touch(fctx->timer);
6464 keep_trying = ISC_FALSE;
6465 broken_server = ISC_R_SUCCESS;
6466 get_nameservers = ISC_FALSE;
6468 truncated = ISC_FALSE;
6470 no_response = ISC_FALSE;
6472 if (fctx->res->exiting) {
6473 result = ISC_R_SHUTTINGDOWN;
6478 fctx->timeout = ISC_FALSE;
6479 fctx->addrinfo = query->addrinfo;
6482 * XXXRTH We should really get the current time just once. We
6483 * need a routine to convert from an isc_time_t to an
6488 isc_stdtime_get(&now);
6491 * Did the dispatcher have a problem?
6493 if (devent->result != ISC_R_SUCCESS) {
6494 if (devent->result == ISC_R_EOF &&
6495 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6497 * The problem might be that they
6498 * don't understand EDNS0. Turn it
6499 * off and try again.
6501 options |= DNS_FETCHOPT_NOEDNS0;
6504 * Remember that they don't like EDNS0.
6506 dns_adb_changeflags(fctx->adb,
6508 DNS_FETCHOPT_NOEDNS0,
6509 DNS_FETCHOPT_NOEDNS0);
6512 * There's no hope for this query.
6514 keep_trying = ISC_TRUE;
6517 * If this is a network error on an exclusive query
6518 * socket, mark the server as bad so that we won't try
6519 * it for this fetch again. Also adjust finish and
6520 * no_response so that we penalize this address in SRTT
6523 if (query->exclusivesocket &&
6524 (devent->result == ISC_R_HOSTUNREACH ||
6525 devent->result == ISC_R_NETUNREACH ||
6526 devent->result == ISC_R_CONNREFUSED ||
6527 devent->result == ISC_R_CANCELED)) {
6528 broken_server = devent->result;
6529 broken_type = badns_unreachable;
6531 no_response = ISC_TRUE;
6537 message = fctx->rmessage;
6539 if (query->tsig != NULL) {
6540 result = dns_message_setquerytsig(message, query->tsig);
6541 if (result != ISC_R_SUCCESS)
6545 if (query->tsigkey) {
6546 result = dns_message_settsigkey(message, query->tsigkey);
6547 if (result != ISC_R_SUCCESS)
6551 result = dns_message_parse(message, &devent->buffer, 0);
6552 if (result != ISC_R_SUCCESS) {
6554 case ISC_R_UNEXPECTEDEND:
6555 if (!message->question_ok ||
6556 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6557 (options & DNS_FETCHOPT_TCP) != 0) {
6559 * Either the message ended prematurely,
6560 * and/or wasn't marked as being truncated,
6561 * and/or this is a response to a query we
6562 * sent over TCP. In all of these cases,
6563 * something is wrong with the remote
6564 * server and we don't want to retry using
6567 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6570 * The problem might be that they
6571 * don't understand EDNS0. Turn it
6572 * off and try again.
6574 options |= DNS_FETCHOPT_NOEDNS0;
6577 * Remember that they don't like EDNS0.
6579 dns_adb_changeflags(
6582 DNS_FETCHOPT_NOEDNS0,
6583 DNS_FETCHOPT_NOEDNS0);
6584 inc_stats(fctx->res,
6585 dns_resstatscounter_edns0fail);
6587 broken_server = result;
6588 keep_trying = ISC_TRUE;
6593 * We defer retrying via TCP for a bit so we can
6594 * check out this message further.
6596 truncated = ISC_TRUE;
6599 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6601 * The problem might be that they
6602 * don't understand EDNS0. Turn it
6603 * off and try again.
6605 options |= DNS_FETCHOPT_NOEDNS0;
6608 * Remember that they don't like EDNS0.
6610 dns_adb_changeflags(fctx->adb,
6612 DNS_FETCHOPT_NOEDNS0,
6613 DNS_FETCHOPT_NOEDNS0);
6614 inc_stats(fctx->res,
6615 dns_resstatscounter_edns0fail);
6617 broken_server = DNS_R_UNEXPECTEDRCODE;
6618 keep_trying = ISC_TRUE;
6623 * Something bad has happened.
6631 * Log the incoming packet.
6633 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6636 * Did we request NSID? If so, and if the response contains
6637 * NSID data, log it at INFO level.
6639 opt = dns_message_getopt(message);
6640 if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6641 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6644 * If the message is signed, check the signature. If not, this
6645 * returns success anyway.
6647 result = dns_message_checksig(message, fctx->res->view);
6648 if (result != ISC_R_SUCCESS)
6652 * The dispatcher should ensure we only get responses with QR set.
6654 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6656 * INSIST() that the message comes from the place we sent it to,
6657 * since the dispatch code should ensure this.
6659 * INSIST() that the message id is correct (this should also be
6660 * ensured by the dispatch code).
6664 * We have an affirmative response to the query and we have
6665 * previously got a response from this server which indicated
6666 * EDNS may not be supported so we can now cache the lack of
6670 (message->rcode == dns_rcode_noerror ||
6671 message->rcode == dns_rcode_nxdomain ||
6672 message->rcode == dns_rcode_refused ||
6673 message->rcode == dns_rcode_yxdomain) &&
6674 bad_edns(fctx, &query->addrinfo->sockaddr)) {
6675 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6676 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6678 dns_adb_changeflags(fctx->adb, query->addrinfo,
6679 DNS_FETCHOPT_NOEDNS0,
6680 DNS_FETCHOPT_NOEDNS0);
6684 * Deal with truncated responses by retrying using TCP.
6686 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6687 truncated = ISC_TRUE;
6690 inc_stats(fctx->res, dns_resstatscounter_truncated);
6691 if ((options & DNS_FETCHOPT_TCP) != 0) {
6692 broken_server = DNS_R_TRUNCATEDTCP;
6693 keep_trying = ISC_TRUE;
6695 options |= DNS_FETCHOPT_TCP;
6702 * Is it a query response?
6704 if (message->opcode != dns_opcode_query) {
6706 broken_server = DNS_R_UNEXPECTEDOPCODE;
6707 keep_trying = ISC_TRUE;
6712 * Update statistics about erroneous responses.
6714 if (message->rcode != dns_rcode_noerror) {
6715 switch (message->rcode) {
6716 case dns_rcode_nxdomain:
6717 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6719 case dns_rcode_servfail:
6720 inc_stats(fctx->res, dns_resstatscounter_servfail);
6722 case dns_rcode_formerr:
6723 inc_stats(fctx->res, dns_resstatscounter_formerr);
6726 inc_stats(fctx->res, dns_resstatscounter_othererror);
6732 * Is the remote server broken, or does it dislike us?
6734 if (message->rcode != dns_rcode_noerror &&
6735 message->rcode != dns_rcode_nxdomain) {
6736 if (((message->rcode == dns_rcode_formerr ||
6737 message->rcode == dns_rcode_notimp) ||
6738 (message->rcode == dns_rcode_servfail &&
6739 dns_message_getopt(message) == NULL)) &&
6740 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6742 * It's very likely they don't like EDNS0.
6743 * If the response code is SERVFAIL, also check if the
6744 * response contains an OPT RR and don't cache the
6745 * failure since it can be returned for various other
6748 * XXXRTH We should check if the question
6749 * we're asking requires EDNS0, and
6750 * if so, we should bail out.
6752 options |= DNS_FETCHOPT_NOEDNS0;
6755 * Remember that they may not like EDNS0.
6757 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6758 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6759 } else if (message->rcode == dns_rcode_formerr) {
6760 if (ISFORWARDER(query->addrinfo)) {
6762 * This forwarder doesn't understand us,
6763 * but other forwarders might. Keep trying.
6765 broken_server = DNS_R_REMOTEFORMERR;
6766 keep_trying = ISC_TRUE;
6769 * The server doesn't understand us. Since
6770 * all servers for a zone need similar
6771 * capabilities, we assume that we will get
6772 * FORMERR from all servers, and thus we
6773 * cannot make any more progress with this
6776 log_formerr(fctx, "server sent FORMERR");
6777 result = DNS_R_FORMERR;
6779 } else if (message->rcode == dns_rcode_yxdomain) {
6781 * DNAME mapping failed because the new name
6782 * was too long. There's no chance of success
6785 result = DNS_R_YXDOMAIN;
6786 } else if (message->rcode == dns_rcode_badvers) {
6787 unsigned int flags, mask;
6788 unsigned int version;
6791 INSIST(opt != NULL);
6792 version = (opt->ttl >> 16) & 0xff;
6793 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6794 DNS_FETCHOPT_EDNSVERSIONSET;
6795 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6796 DNS_FETCHOPT_EDNSVERSIONSET;
6799 dns_adb_changeflags(fctx->adb, query->addrinfo,
6803 broken_server = DNS_R_BADVERS;
6804 keep_trying = ISC_TRUE;
6811 broken_server = DNS_R_UNEXPECTEDRCODE;
6812 INSIST(broken_server != ISC_R_SUCCESS);
6813 keep_trying = ISC_TRUE;
6819 * Is the question the same as the one we asked?
6821 result = same_question(fctx);
6822 if (result != ISC_R_SUCCESS) {
6824 if (result == DNS_R_FORMERR)
6825 keep_trying = ISC_TRUE;
6830 * Is the server lame?
6832 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6834 inc_stats(fctx->res, dns_resstatscounter_lame);
6835 log_lame(fctx, query->addrinfo);
6836 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6837 &fctx->name, fctx->type,
6838 now + fctx->res->lame_ttl);
6839 if (result != ISC_R_SUCCESS)
6840 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6841 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6842 "could not mark server as lame: %s",
6843 isc_result_totext(result));
6844 broken_server = DNS_R_LAME;
6845 keep_trying = ISC_TRUE;
6850 * Enforce delegations only zones like NET and COM.
6852 if (!ISFORWARDER(query->addrinfo) &&
6853 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6854 !dns_name_equal(&fctx->domain, &fctx->name) &&
6855 fix_mustbedelegationornxdomain(message, fctx)) {
6856 char namebuf[DNS_NAME_FORMATSIZE];
6857 char domainbuf[DNS_NAME_FORMATSIZE];
6858 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6862 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6863 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6864 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6865 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6867 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6870 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6871 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6872 "enforced delegation-only for '%s' (%s/%s/%s) "
6874 domainbuf, namebuf, typebuf, classbuf, addrbuf);
6877 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6878 checknames(message);
6883 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6886 * Did we get any answers?
6888 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6889 (message->rcode == dns_rcode_noerror ||
6890 message->rcode == dns_rcode_nxdomain)) {
6893 * We've got answers. If it has an authoritative answer or an
6894 * answer from a forwarder, we're done.
6896 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
6897 ISFORWARDER(query->addrinfo))
6898 result = answer_response(fctx);
6899 else if (iscname(fctx) &&
6900 fctx->type != dns_rdatatype_any &&
6901 fctx->type != dns_rdatatype_cname) {
6903 * A BIND8 server could return a non-authoritative
6904 * answer when a CNAME is followed. We should treat
6905 * it as a valid answer.
6907 result = answer_response(fctx);
6908 } else if (fctx->type != dns_rdatatype_ns &&
6909 !betterreferral(fctx)) {
6911 * Lame response !!!.
6913 result = answer_response(fctx);
6915 if (fctx->type == dns_rdatatype_ns) {
6917 * A BIND 8 server could incorrectly return a
6918 * non-authoritative answer to an NS query
6919 * instead of a referral. Since this answer
6920 * lacks the SIGs necessary to do DNSSEC
6921 * validation, we must invoke the following
6922 * special kludge to treat it as a referral.
6924 result = noanswer_response(fctx, NULL,
6925 LOOK_FOR_NS_IN_ANSWER);
6928 * Some other servers may still somehow include
6929 * an answer when it should return a referral
6930 * with an empty answer. Check to see if we can
6931 * treat this as a referral by ignoring the
6932 * answer. Further more, there may be an
6933 * implementation that moves A/AAAA glue records
6934 * to the answer section for that type of
6935 * delegation when the query is for that glue
6936 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
6937 * such a corner case.
6939 result = noanswer_response(fctx, NULL,
6940 LOOK_FOR_GLUE_IN_ANSWER);
6942 if (result != DNS_R_DELEGATION) {
6944 * At this point, AA is not set, the response
6945 * is not a referral, and the server is not a
6946 * forwarder. It is technically lame and it's
6947 * easier to treat it as such than to figure out
6948 * some more elaborate course of action.
6950 broken_server = DNS_R_LAME;
6951 keep_trying = ISC_TRUE;
6954 goto force_referral;
6956 if (result != ISC_R_SUCCESS) {
6957 if (result == DNS_R_FORMERR)
6958 keep_trying = ISC_TRUE;
6961 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
6962 message->rcode == dns_rcode_noerror ||
6963 message->rcode == dns_rcode_nxdomain) {
6965 * NXDOMAIN, NXRDATASET, or referral.
6967 result = noanswer_response(fctx, NULL, 0);
6968 if (result == DNS_R_CHASEDSSERVERS) {
6969 } else if (result == DNS_R_DELEGATION) {
6972 * We don't have the answer, but we know a better
6975 get_nameservers = ISC_TRUE;
6976 keep_trying = ISC_TRUE;
6978 * We have a new set of name servers, and it
6979 * has not experienced any restarts yet.
6984 * Update local statistics counters collected for each
6988 fctx->querysent = 0;
6989 fctx->lamecount = 0;
6994 result = ISC_R_SUCCESS;
6995 } else if (result != ISC_R_SUCCESS) {
6997 * Something has gone wrong.
6999 if (result == DNS_R_FORMERR)
7000 keep_trying = ISC_TRUE;
7005 * The server is insane.
7008 broken_server = DNS_R_UNEXPECTEDRCODE;
7009 keep_trying = ISC_TRUE;
7014 * Follow additional section data chains.
7016 chase_additional(fctx);
7019 * Cache the cacheable parts of the message. This may also cause
7020 * work to be queued to the DNSSEC validator.
7022 if (WANTCACHE(fctx)) {
7023 result = cache_message(fctx, query->addrinfo, now);
7024 if (result != ISC_R_SUCCESS)
7029 * Ncache the negatively cacheable parts of the message. This may
7030 * also cause work to be queued to the DNSSEC validator.
7032 if (WANTNCACHE(fctx)) {
7033 dns_rdatatype_t covers;
7034 if (message->rcode == dns_rcode_nxdomain)
7035 covers = dns_rdatatype_any;
7037 covers = fctx->type;
7040 * Cache any negative cache entries in the message.
7042 result = ncache_message(fctx, query->addrinfo, covers, now);
7047 * Remember the query's addrinfo, in case we need to mark the
7050 addrinfo = query->addrinfo;
7055 * XXXRTH Don't cancel the query if waiting for validation?
7057 fctx_cancelquery(&query, &devent, finish, no_response);
7060 if (result == DNS_R_FORMERR)
7061 broken_server = DNS_R_FORMERR;
7062 if (broken_server != ISC_R_SUCCESS) {
7064 * Add this server to the list of bad servers for
7067 add_bad(fctx, addrinfo, broken_server, broken_type);
7070 if (get_nameservers) {
7072 dns_fixedname_init(&foundname);
7073 fname = dns_fixedname_name(&foundname);
7074 if (result != ISC_R_SUCCESS) {
7075 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7079 if (dns_rdatatype_atparent(fctx->type))
7080 findoptions |= DNS_DBFIND_NOEXACT;
7081 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7084 name = &fctx->domain;
7085 result = dns_view_findzonecut(fctx->res->view,
7091 if (result != ISC_R_SUCCESS) {
7092 FCTXTRACE("couldn't find a zonecut");
7093 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7096 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7098 * The best nameservers are now above our
7101 FCTXTRACE("nameservers now above QDOMAIN");
7102 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7105 dns_name_free(&fctx->domain,
7106 fctx->res->buckets[fctx->bucketnum].mctx);
7107 dns_name_init(&fctx->domain, NULL);
7108 result = dns_name_dup(fname,
7109 fctx->res->buckets[fctx->bucketnum].mctx,
7111 if (result != ISC_R_SUCCESS) {
7112 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7115 fctx_cancelqueries(fctx, ISC_TRUE);
7116 fctx_cleanupfinds(fctx);
7117 fctx_cleanupaltfinds(fctx);
7118 fctx_cleanupforwaddrs(fctx);
7119 fctx_cleanupaltaddrs(fctx);
7124 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7125 } else if (resend) {
7127 * Resend (probably with changed options).
7129 FCTXTRACE("resend");
7130 inc_stats(fctx->res, dns_resstatscounter_retry);
7131 result = fctx_query(fctx, addrinfo, options);
7132 if (result != ISC_R_SUCCESS)
7133 fctx_done(fctx, result, __LINE__);
7134 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7136 * All has gone well so far, but we are waiting for the
7137 * DNSSEC validator to validate the answer.
7139 FCTXTRACE("wait for validator");
7140 fctx_cancelqueries(fctx, ISC_TRUE);
7142 * We must not retransmit while the validator is working;
7143 * it has references to the current rmessage.
7145 result = fctx_stopidletimer(fctx);
7146 if (result != ISC_R_SUCCESS)
7147 fctx_done(fctx, result, __LINE__);
7148 } else if (result == DNS_R_CHASEDSSERVERS) {
7150 add_bad(fctx, addrinfo, result, broken_type);
7151 fctx_cancelqueries(fctx, ISC_TRUE);
7152 fctx_cleanupfinds(fctx);
7153 fctx_cleanupforwaddrs(fctx);
7155 n = dns_name_countlabels(&fctx->name);
7156 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7158 FCTXTRACE("suspending DS lookup to find parent's NS records");
7160 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7162 NULL, NULL, NULL, 0, task,
7163 resume_dslookup, fctx,
7164 &fctx->nsrrset, NULL,
7166 if (result != ISC_R_SUCCESS)
7167 fctx_done(fctx, result, __LINE__);
7169 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7171 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7172 result = fctx_stopidletimer(fctx);
7173 if (result != ISC_R_SUCCESS)
7174 fctx_done(fctx, result, __LINE__);
7180 fctx_done(fctx, result, __LINE__);
7186 *** Resolver Methods
7189 destroy_badcache(dns_resolver_t *res) {
7190 dns_badcache_t *bad, *next;
7193 if (res->badcache != NULL) {
7194 for (i = 0; i < res->badhash; i++)
7195 for (bad = res->badcache[i]; bad != NULL;
7198 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7202 isc_mem_put(res->mctx, res->badcache,
7203 sizeof(*res->badcache) * res->badhash);
7204 res->badcache = NULL;
7206 INSIST(res->badcount == 0);
7211 destroy(dns_resolver_t *res) {
7215 REQUIRE(res->references == 0);
7216 REQUIRE(!res->priming);
7217 REQUIRE(res->primefetch == NULL);
7221 INSIST(res->nfctx == 0);
7223 DESTROYLOCK(&res->primelock);
7224 DESTROYLOCK(&res->nlock);
7225 DESTROYLOCK(&res->lock);
7226 for (i = 0; i < res->nbuckets; i++) {
7227 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7228 isc_task_shutdown(res->buckets[i].task);
7229 isc_task_detach(&res->buckets[i].task);
7230 DESTROYLOCK(&res->buckets[i].lock);
7231 isc_mem_detach(&res->buckets[i].mctx);
7233 isc_mem_put(res->mctx, res->buckets,
7234 res->nbuckets * sizeof(fctxbucket_t));
7235 if (res->dispatchv4 != NULL)
7236 dns_dispatch_detach(&res->dispatchv4);
7237 if (res->dispatchv6 != NULL)
7238 dns_dispatch_detach(&res->dispatchv6);
7239 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7240 ISC_LIST_UNLINK(res->alternates, a, link);
7242 dns_name_free(&a->_u._n.name, res->mctx);
7243 isc_mem_put(res->mctx, a, sizeof(*a));
7245 dns_resolver_reset_algorithms(res);
7246 destroy_badcache(res);
7247 dns_resolver_resetmustbesecure(res);
7249 isc_rwlock_destroy(&res->alglock);
7252 isc_rwlock_destroy(&res->mbslock);
7254 isc_timer_detach(&res->spillattimer);
7256 isc_mem_put(res->mctx, res, sizeof(*res));
7260 send_shutdown_events(dns_resolver_t *res) {
7261 isc_event_t *event, *next_event;
7265 * Caller must be holding the resolver lock.
7268 for (event = ISC_LIST_HEAD(res->whenshutdown);
7270 event = next_event) {
7271 next_event = ISC_LIST_NEXT(event, ev_link);
7272 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7273 etask = event->ev_sender;
7274 event->ev_sender = res;
7275 isc_task_sendanddetach(&etask, &event);
7280 empty_bucket(dns_resolver_t *res) {
7281 RTRACE("empty_bucket");
7285 INSIST(res->activebuckets > 0);
7286 res->activebuckets--;
7287 if (res->activebuckets == 0)
7288 send_shutdown_events(res);
7294 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7295 dns_resolver_t *res = event->ev_arg;
7296 isc_result_t result;
7298 isc_boolean_t logit = ISC_FALSE;
7300 REQUIRE(VALID_RESOLVER(res));
7305 INSIST(!res->exiting);
7306 if (res->spillat > res->spillatmin) {
7310 if (res->spillat <= res->spillatmin) {
7311 result = isc_timer_reset(res->spillattimer,
7312 isc_timertype_inactive, NULL,
7314 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7316 count = res->spillat;
7319 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7320 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7321 "clients-per-query decreased to %u", count);
7323 isc_event_free(&event);
7327 dns_resolver_create(dns_view_t *view,
7328 isc_taskmgr_t *taskmgr, unsigned int ntasks,
7329 isc_socketmgr_t *socketmgr,
7330 isc_timermgr_t *timermgr,
7331 unsigned int options,
7332 dns_dispatchmgr_t *dispatchmgr,
7333 dns_dispatch_t *dispatchv4,
7334 dns_dispatch_t *dispatchv6,
7335 dns_resolver_t **resp)
7337 dns_resolver_t *res;
7338 isc_result_t result = ISC_R_SUCCESS;
7339 unsigned int i, buckets_created = 0;
7340 isc_task_t *task = NULL;
7345 * Create a resolver.
7348 REQUIRE(DNS_VIEW_VALID(view));
7349 REQUIRE(ntasks > 0);
7350 REQUIRE(resp != NULL && *resp == NULL);
7351 REQUIRE(dispatchmgr != NULL);
7352 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7354 res = isc_mem_get(view->mctx, sizeof(*res));
7356 return (ISC_R_NOMEMORY);
7358 res->mctx = view->mctx;
7359 res->rdclass = view->rdclass;
7360 res->socketmgr = socketmgr;
7361 res->timermgr = timermgr;
7362 res->taskmgr = taskmgr;
7363 res->dispatchmgr = dispatchmgr;
7365 res->options = options;
7367 ISC_LIST_INIT(res->alternates);
7368 res->udpsize = RECV_BUFFER_SIZE;
7369 res->algorithms = NULL;
7370 res->badcache = NULL;
7374 res->mustbesecure = NULL;
7375 res->spillatmin = res->spillat = 10;
7376 res->spillatmax = 100;
7377 res->spillattimer = NULL;
7378 res->zero_no_soa_ttl = ISC_FALSE;
7379 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7381 res->nextdisp = 0; /* meaningless at this point, but init it */
7382 res->nbuckets = ntasks;
7383 res->activebuckets = ntasks;
7384 res->buckets = isc_mem_get(view->mctx,
7385 ntasks * sizeof(fctxbucket_t));
7386 if (res->buckets == NULL) {
7387 result = ISC_R_NOMEMORY;
7390 for (i = 0; i < ntasks; i++) {
7391 result = isc_mutex_init(&res->buckets[i].lock);
7392 if (result != ISC_R_SUCCESS)
7393 goto cleanup_buckets;
7394 res->buckets[i].task = NULL;
7395 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7396 if (result != ISC_R_SUCCESS) {
7397 DESTROYLOCK(&res->buckets[i].lock);
7398 goto cleanup_buckets;
7400 res->buckets[i].mctx = NULL;
7401 snprintf(name, sizeof(name), "res%u", i);
7402 #ifdef ISC_PLATFORM_USETHREADS
7404 * Use a separate memory context for each bucket to reduce
7405 * contention among multiple threads. Do this only when
7406 * enabling threads because it will be require more memory.
7408 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7409 if (result != ISC_R_SUCCESS) {
7410 isc_task_detach(&res->buckets[i].task);
7411 DESTROYLOCK(&res->buckets[i].lock);
7412 goto cleanup_buckets;
7414 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7416 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7418 isc_task_setname(res->buckets[i].task, name, res);
7419 ISC_LIST_INIT(res->buckets[i].fctxs);
7420 res->buckets[i].exiting = ISC_FALSE;
7424 res->dispatchv4 = NULL;
7425 if (dispatchv4 != NULL) {
7426 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
7427 dispattr = dns_dispatch_getattributes(dispatchv4);
7429 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7432 res->dispatchv6 = NULL;
7433 if (dispatchv6 != NULL) {
7434 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
7435 dispattr = dns_dispatch_getattributes(dispatchv6);
7437 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7440 res->references = 1;
7441 res->exiting = ISC_FALSE;
7442 res->frozen = ISC_FALSE;
7443 ISC_LIST_INIT(res->whenshutdown);
7444 res->priming = ISC_FALSE;
7445 res->primefetch = NULL;
7448 result = isc_mutex_init(&res->lock);
7449 if (result != ISC_R_SUCCESS)
7450 goto cleanup_dispatches;
7452 result = isc_mutex_init(&res->nlock);
7453 if (result != ISC_R_SUCCESS)
7456 result = isc_mutex_init(&res->primelock);
7457 if (result != ISC_R_SUCCESS)
7461 result = isc_task_create(taskmgr, 0, &task);
7462 if (result != ISC_R_SUCCESS)
7463 goto cleanup_primelock;
7465 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7466 task, spillattimer_countdown, res,
7467 &res->spillattimer);
7468 isc_task_detach(&task);
7469 if (result != ISC_R_SUCCESS)
7470 goto cleanup_primelock;
7473 result = isc_rwlock_init(&res->alglock, 0, 0);
7474 if (result != ISC_R_SUCCESS)
7475 goto cleanup_spillattimer;
7478 result = isc_rwlock_init(&res->mbslock, 0, 0);
7479 if (result != ISC_R_SUCCESS)
7480 goto cleanup_alglock;
7483 res->magic = RES_MAGIC;
7487 return (ISC_R_SUCCESS);
7492 isc_rwlock_destroy(&res->alglock);
7495 #if USE_ALGLOCK || USE_MBSLOCK
7496 cleanup_spillattimer:
7497 isc_timer_detach(&res->spillattimer);
7501 DESTROYLOCK(&res->primelock);
7504 DESTROYLOCK(&res->nlock);
7507 DESTROYLOCK(&res->lock);
7510 if (res->dispatchv6 != NULL)
7511 dns_dispatch_detach(&res->dispatchv6);
7512 if (res->dispatchv4 != NULL)
7513 dns_dispatch_detach(&res->dispatchv4);
7516 for (i = 0; i < buckets_created; i++) {
7517 isc_mem_detach(&res->buckets[i].mctx);
7518 DESTROYLOCK(&res->buckets[i].lock);
7519 isc_task_shutdown(res->buckets[i].task);
7520 isc_task_detach(&res->buckets[i].task);
7522 isc_mem_put(view->mctx, res->buckets,
7523 res->nbuckets * sizeof(fctxbucket_t));
7526 isc_mem_put(view->mctx, res, sizeof(*res));
7533 prime_done(isc_task_t *task, isc_event_t *event) {
7534 dns_resolver_t *res;
7535 dns_fetchevent_t *fevent;
7537 dns_db_t *db = NULL;
7539 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7540 fevent = (dns_fetchevent_t *)event;
7541 res = event->ev_arg;
7542 REQUIRE(VALID_RESOLVER(res));
7548 INSIST(res->priming);
7549 res->priming = ISC_FALSE;
7550 LOCK(&res->primelock);
7551 fetch = res->primefetch;
7552 res->primefetch = NULL;
7553 UNLOCK(&res->primelock);
7557 if (fevent->result == ISC_R_SUCCESS &&
7558 res->view->cache != NULL && res->view->hints != NULL) {
7559 dns_cache_attachdb(res->view->cache, &db);
7560 dns_root_checkhints(res->view, res->view->hints, db);
7564 if (fevent->node != NULL)
7565 dns_db_detachnode(fevent->db, &fevent->node);
7566 if (fevent->db != NULL)
7567 dns_db_detach(&fevent->db);
7568 if (dns_rdataset_isassociated(fevent->rdataset))
7569 dns_rdataset_disassociate(fevent->rdataset);
7570 INSIST(fevent->sigrdataset == NULL);
7572 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7574 isc_event_free(&event);
7575 dns_resolver_destroyfetch(&fetch);
7579 dns_resolver_prime(dns_resolver_t *res) {
7580 isc_boolean_t want_priming = ISC_FALSE;
7581 dns_rdataset_t *rdataset;
7582 isc_result_t result;
7584 REQUIRE(VALID_RESOLVER(res));
7585 REQUIRE(res->frozen);
7587 RTRACE("dns_resolver_prime");
7591 if (!res->exiting && !res->priming) {
7592 INSIST(res->primefetch == NULL);
7593 res->priming = ISC_TRUE;
7594 want_priming = ISC_TRUE;
7601 * To avoid any possible recursive locking problems, we
7602 * start the priming fetch like any other fetch, and holding
7603 * no resolver locks. No one else will try to start it
7604 * because we're the ones who set res->priming to true.
7605 * Any other callers of dns_resolver_prime() while we're
7606 * running will see that res->priming is already true and
7610 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7611 if (rdataset == NULL) {
7613 INSIST(res->priming);
7614 INSIST(res->primefetch == NULL);
7615 res->priming = ISC_FALSE;
7619 dns_rdataset_init(rdataset);
7620 LOCK(&res->primelock);
7621 result = dns_resolver_createfetch(res, dns_rootname,
7623 NULL, NULL, NULL, 0,
7624 res->buckets[0].task,
7626 res, rdataset, NULL,
7628 UNLOCK(&res->primelock);
7629 if (result != ISC_R_SUCCESS) {
7631 INSIST(res->priming);
7632 res->priming = ISC_FALSE;
7640 dns_resolver_freeze(dns_resolver_t *res) {
7645 REQUIRE(VALID_RESOLVER(res));
7647 res->frozen = ISC_TRUE;
7651 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7652 REQUIRE(VALID_RESOLVER(source));
7653 REQUIRE(targetp != NULL && *targetp == NULL);
7655 RRTRACE(source, "attach");
7656 LOCK(&source->lock);
7657 REQUIRE(!source->exiting);
7659 INSIST(source->references > 0);
7660 source->references++;
7661 INSIST(source->references != 0);
7662 UNLOCK(&source->lock);
7668 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7669 isc_event_t **eventp)
7674 REQUIRE(VALID_RESOLVER(res));
7675 REQUIRE(eventp != NULL);
7682 if (res->exiting && res->activebuckets == 0) {
7684 * We're already shutdown. Send the event.
7686 event->ev_sender = res;
7687 isc_task_send(task, &event);
7690 isc_task_attach(task, &clone);
7691 event->ev_sender = clone;
7692 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7699 dns_resolver_shutdown(dns_resolver_t *res) {
7703 isc_result_t result;
7705 REQUIRE(VALID_RESOLVER(res));
7711 if (!res->exiting) {
7713 res->exiting = ISC_TRUE;
7715 for (i = 0; i < res->nbuckets; i++) {
7716 LOCK(&res->buckets[i].lock);
7717 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7719 fctx = ISC_LIST_NEXT(fctx, link))
7720 fctx_shutdown(fctx);
7721 if (res->dispatchv4 != NULL && !res->exclusivev4) {
7722 sock = dns_dispatch_getsocket(res->dispatchv4);
7723 isc_socket_cancel(sock, res->buckets[i].task,
7724 ISC_SOCKCANCEL_ALL);
7726 if (res->dispatchv6 != NULL && !res->exclusivev6) {
7727 sock = dns_dispatch_getsocket(res->dispatchv6);
7728 isc_socket_cancel(sock, res->buckets[i].task,
7729 ISC_SOCKCANCEL_ALL);
7731 res->buckets[i].exiting = ISC_TRUE;
7732 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7733 INSIST(res->activebuckets > 0);
7734 res->activebuckets--;
7736 UNLOCK(&res->buckets[i].lock);
7738 if (res->activebuckets == 0)
7739 send_shutdown_events(res);
7740 result = isc_timer_reset(res->spillattimer,
7741 isc_timertype_inactive, NULL,
7743 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7750 dns_resolver_detach(dns_resolver_t **resp) {
7751 dns_resolver_t *res;
7752 isc_boolean_t need_destroy = ISC_FALSE;
7754 REQUIRE(resp != NULL);
7756 REQUIRE(VALID_RESOLVER(res));
7762 INSIST(res->references > 0);
7764 if (res->references == 0) {
7765 INSIST(res->exiting && res->activebuckets == 0);
7766 need_destroy = ISC_TRUE;
7777 static inline isc_boolean_t
7778 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7779 unsigned int options)
7782 * Don't match fetch contexts that are shutting down.
7784 if (fctx->cloned || fctx->state == fetchstate_done ||
7785 ISC_LIST_EMPTY(fctx->events))
7788 if (fctx->type != type || fctx->options != options)
7790 return (dns_name_equal(&fctx->name, name));
7794 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7795 char namebuf[DNS_NAME_FORMATSIZE];
7796 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7797 int level = ISC_LOG_DEBUG(1);
7799 if (! isc_log_wouldlog(dns_lctx, level))
7802 dns_name_format(name, namebuf, sizeof(namebuf));
7803 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7805 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7806 DNS_LOGMODULE_RESOLVER, level,
7807 "createfetch: %s %s", namebuf, typebuf);
7811 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7812 dns_rdatatype_t type,
7813 dns_name_t *domain, dns_rdataset_t *nameservers,
7814 dns_forwarders_t *forwarders,
7815 unsigned int options, isc_task_t *task,
7816 isc_taskaction_t action, void *arg,
7817 dns_rdataset_t *rdataset,
7818 dns_rdataset_t *sigrdataset,
7819 dns_fetch_t **fetchp)
7821 return (dns_resolver_createfetch2(res, name, type, domain,
7822 nameservers, forwarders, NULL, 0,
7823 options, task, action, arg,
7824 rdataset, sigrdataset, fetchp));
7828 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7829 dns_rdatatype_t type,
7830 dns_name_t *domain, dns_rdataset_t *nameservers,
7831 dns_forwarders_t *forwarders,
7832 isc_sockaddr_t *client, dns_messageid_t id,
7833 unsigned int options, isc_task_t *task,
7834 isc_taskaction_t action, void *arg,
7835 dns_rdataset_t *rdataset,
7836 dns_rdataset_t *sigrdataset,
7837 dns_fetch_t **fetchp)
7840 fetchctx_t *fctx = NULL;
7841 isc_result_t result = ISC_R_SUCCESS;
7842 unsigned int bucketnum;
7843 isc_boolean_t new_fctx = ISC_FALSE;
7845 unsigned int count = 0;
7846 unsigned int spillat;
7847 unsigned int spillatmin;
7851 REQUIRE(VALID_RESOLVER(res));
7852 REQUIRE(res->frozen);
7853 /* XXXRTH Check for meta type */
7854 if (domain != NULL) {
7855 REQUIRE(DNS_RDATASET_VALID(nameservers));
7856 REQUIRE(nameservers->type == dns_rdatatype_ns);
7858 REQUIRE(nameservers == NULL);
7859 REQUIRE(forwarders == NULL);
7860 REQUIRE(!dns_rdataset_isassociated(rdataset));
7861 REQUIRE(sigrdataset == NULL ||
7862 !dns_rdataset_isassociated(sigrdataset));
7863 REQUIRE(fetchp != NULL && *fetchp == NULL);
7865 log_fetch(name, type);
7868 * XXXRTH use a mempool?
7870 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7872 return (ISC_R_NOMEMORY);
7874 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7877 spillat = res->spillat;
7878 spillatmin = res->spillatmin;
7880 LOCK(&res->buckets[bucketnum].lock);
7882 if (res->buckets[bucketnum].exiting) {
7883 result = ISC_R_SHUTTINGDOWN;
7887 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7888 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
7890 fctx = ISC_LIST_NEXT(fctx, link)) {
7891 if (fctx_match(fctx, name, type, options))
7897 * Is this a duplicate?
7899 if (fctx != NULL && client != NULL) {
7900 dns_fetchevent_t *fevent;
7901 for (fevent = ISC_LIST_HEAD(fctx->events);
7903 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
7904 if (fevent->client != NULL && fevent->id == id &&
7905 isc_sockaddr_equal(fevent->client, client)) {
7906 result = DNS_R_DUPLICATE;
7912 if (count >= spillatmin && spillatmin != 0) {
7913 INSIST(fctx != NULL);
7914 if (count >= spillat)
7915 fctx->spilled = ISC_TRUE;
7916 if (fctx->spilled) {
7917 result = DNS_R_DROP;
7923 result = fctx_create(res, name, type, domain, nameservers,
7924 options, bucketnum, &fctx);
7925 if (result != ISC_R_SUCCESS)
7927 new_fctx = ISC_TRUE;
7930 result = fctx_join(fctx, task, client, id, action, arg,
7931 rdataset, sigrdataset, fetch);
7933 if (result == ISC_R_SUCCESS) {
7937 event = &fctx->control_event;
7938 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
7939 DNS_EVENT_FETCHCONTROL,
7940 fctx_start, fctx, NULL,
7942 isc_task_send(res->buckets[bucketnum].task, &event);
7945 * We don't care about the result of fctx_destroy()
7946 * since we know we're not exiting.
7948 (void)fctx_destroy(fctx);
7953 UNLOCK(&res->buckets[bucketnum].lock);
7955 if (result == ISC_R_SUCCESS) {
7959 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7965 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7967 dns_resolver_t *res;
7968 dns_fetchevent_t *event, *next_event;
7971 REQUIRE(DNS_FETCH_VALID(fetch));
7972 fctx = fetch->private;
7973 REQUIRE(VALID_FCTX(fctx));
7976 FTRACE("cancelfetch");
7978 LOCK(&res->buckets[fctx->bucketnum].lock);
7981 * Find the completion event for this fetch (as opposed
7982 * to those for other fetches that have joined the same
7983 * fctx) and send it with result = ISC_R_CANCELED.
7986 if (fctx->state != fetchstate_done) {
7987 for (event = ISC_LIST_HEAD(fctx->events);
7989 event = next_event) {
7990 next_event = ISC_LIST_NEXT(event, ev_link);
7991 if (event->fetch == fetch) {
7992 ISC_LIST_UNLINK(fctx->events, event, ev_link);
7997 if (event != NULL) {
7998 etask = event->ev_sender;
7999 event->ev_sender = fctx;
8000 event->result = ISC_R_CANCELED;
8001 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8004 * The fctx continues running even if no fetches remain;
8005 * the answer is still cached.
8008 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8012 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8014 dns_resolver_t *res;
8015 dns_fetchevent_t *event, *next_event;
8017 unsigned int bucketnum;
8018 isc_boolean_t bucket_empty;
8020 REQUIRE(fetchp != NULL);
8022 REQUIRE(DNS_FETCH_VALID(fetch));
8023 fctx = fetch->private;
8024 REQUIRE(VALID_FCTX(fctx));
8027 FTRACE("destroyfetch");
8029 bucketnum = fctx->bucketnum;
8030 LOCK(&res->buckets[bucketnum].lock);
8033 * Sanity check: the caller should have gotten its event before
8034 * trying to destroy the fetch.
8037 if (fctx->state != fetchstate_done) {
8038 for (event = ISC_LIST_HEAD(fctx->events);
8040 event = next_event) {
8041 next_event = ISC_LIST_NEXT(event, ev_link);
8042 RUNTIME_CHECK(event->fetch != fetch);
8046 bucket_empty = fctx_decreference(fctx);
8048 UNLOCK(&res->buckets[bucketnum].lock);
8050 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8058 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8059 isc_logcategory_t *category, isc_logmodule_t *module,
8060 int level, isc_boolean_t duplicateok)
8063 dns_resolver_t *res;
8064 char domainbuf[DNS_NAME_FORMATSIZE];
8066 REQUIRE(DNS_FETCH_VALID(fetch));
8067 fctx = fetch->private;
8068 REQUIRE(VALID_FCTX(fctx));
8071 LOCK(&res->buckets[fctx->bucketnum].lock);
8073 INSIST(fctx->exitline >= 0);
8074 if (!fctx->logged || duplicateok) {
8075 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8076 isc_log_write(lctx, category, module, level,
8077 "fetch completed at %s:%d for %s in "
8078 "%" ISC_PRINT_QUADFORMAT "u."
8079 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8080 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8081 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8082 "adberr:%u,findfail:%u,valfail:%u]",
8083 __FILE__, fctx->exitline, fctx->info,
8084 fctx->duration / 1000000,
8085 fctx->duration % 1000000,
8086 isc_result_totext(fctx->result),
8087 isc_result_totext(fctx->vresult), domainbuf,
8088 fctx->referrals, fctx->restarts,
8089 fctx->querysent, fctx->timeouts, fctx->lamecount,
8090 fctx->neterr, fctx->badresp, fctx->adberr,
8091 fctx->findfail, fctx->valfail);
8092 fctx->logged = ISC_TRUE;
8095 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8099 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8100 REQUIRE(VALID_RESOLVER(resolver));
8101 return (resolver->dispatchmgr);
8105 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8106 REQUIRE(VALID_RESOLVER(resolver));
8107 return (resolver->dispatchv4);
8111 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8112 REQUIRE(VALID_RESOLVER(resolver));
8113 return (resolver->dispatchv6);
8117 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8118 REQUIRE(VALID_RESOLVER(resolver));
8119 return (resolver->socketmgr);
8123 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8124 REQUIRE(VALID_RESOLVER(resolver));
8125 return (resolver->taskmgr);
8129 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8130 REQUIRE(VALID_RESOLVER(resolver));
8131 return (resolver->lame_ttl);
8135 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8136 REQUIRE(VALID_RESOLVER(resolver));
8137 resolver->lame_ttl = lame_ttl;
8141 dns_resolver_nrunning(dns_resolver_t *resolver) {
8143 LOCK(&resolver->nlock);
8144 n = resolver->nfctx;
8145 UNLOCK(&resolver->nlock);
8150 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8151 dns_name_t *name, in_port_t port) {
8153 isc_result_t result;
8155 REQUIRE(VALID_RESOLVER(resolver));
8156 REQUIRE(!resolver->frozen);
8157 REQUIRE((alt == NULL) ^ (name == NULL));
8159 a = isc_mem_get(resolver->mctx, sizeof(*a));
8161 return (ISC_R_NOMEMORY);
8163 a->isaddress = ISC_TRUE;
8166 a->isaddress = ISC_FALSE;
8167 a->_u._n.port = port;
8168 dns_name_init(&a->_u._n.name, NULL);
8169 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8170 if (result != ISC_R_SUCCESS) {
8171 isc_mem_put(resolver->mctx, a, sizeof(*a));
8175 ISC_LINK_INIT(a, link);
8176 ISC_LIST_APPEND(resolver->alternates, a, link);
8178 return (ISC_R_SUCCESS);
8182 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8183 REQUIRE(VALID_RESOLVER(resolver));
8184 resolver->udpsize = udpsize;
8188 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8189 REQUIRE(VALID_RESOLVER(resolver));
8190 return (resolver->udpsize);
8194 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8196 dns_badcache_t *bad, *prev, *next;
8198 REQUIRE(VALID_RESOLVER(resolver));
8200 LOCK(&resolver->lock);
8201 if (resolver->badcache == NULL)
8206 isc_result_t result;
8207 result = isc_time_now(&now);
8208 if (result != ISC_R_SUCCESS)
8209 isc_time_settoepoch(&now);
8210 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8212 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8215 n = isc_time_compare(&bad->expire, &now);
8216 if (n < 0 || dns_name_equal(name, &bad->name)) {
8218 resolver->badcache[i] = bad->next;
8220 prev->next = bad->next;
8221 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8223 resolver->badcount--;
8228 destroy_badcache(resolver);
8231 UNLOCK(&resolver->lock);
8236 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8237 unsigned int newsize;
8238 dns_badcache_t **new, *bad, *next;
8242 newsize = resolver->badhash * 2 + 1;
8244 newsize = (resolver->badhash - 1) / 2;
8246 new = isc_mem_get(resolver->mctx,
8247 sizeof(*resolver->badcache) * newsize);
8250 memset(new, 0, sizeof(*resolver->badcache) * newsize);
8251 for (i = 0; i < resolver->badhash; i++) {
8252 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8254 if (isc_time_compare(&bad->expire, now) < 0) {
8255 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8257 resolver->badcount--;
8259 bad->next = new[bad->hashval % newsize];
8260 new[bad->hashval % newsize] = bad;
8264 isc_mem_put(resolver->mctx, resolver->badcache,
8265 sizeof(*resolver->badcache) * resolver->badhash);
8266 resolver->badhash = newsize;
8267 resolver->badcache = new;
8271 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8272 dns_rdatatype_t type, isc_time_t *expire)
8275 isc_result_t result = ISC_R_SUCCESS;
8276 unsigned int i, hashval;
8277 dns_badcache_t *bad, *prev, *next;
8279 REQUIRE(VALID_RESOLVER(resolver));
8281 LOCK(&resolver->lock);
8282 if (resolver->badcache == NULL) {
8283 resolver->badcache = isc_mem_get(resolver->mctx,
8284 sizeof(*resolver->badcache) *
8286 if (resolver->badcache == NULL)
8288 resolver->badhash = DNS_BADCACHE_SIZE;
8289 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8293 result = isc_time_now(&now);
8294 if (result != ISC_R_SUCCESS)
8295 isc_time_settoepoch(&now);
8296 hashval = dns_name_hash(name, ISC_FALSE);
8297 i = hashval % resolver->badhash;
8299 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8301 if (bad->type == type && dns_name_equal(name, &bad->name))
8303 if (isc_time_compare(&bad->expire, &now) < 0) {
8305 resolver->badcache[i] = bad->next;
8307 prev->next = bad->next;
8308 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8310 resolver->badcount--;
8315 isc_buffer_t buffer;
8316 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8320 bad->hashval = hashval;
8321 isc_buffer_init(&buffer, bad + 1, name->length);
8322 dns_name_init(&bad->name, NULL);
8323 dns_name_copy(name, &bad->name, &buffer);
8324 bad->next = resolver->badcache[i];
8325 resolver->badcache[i] = bad;
8326 resolver->badcount++;
8327 if (resolver->badcount > resolver->badhash * 8)
8328 resizehash(resolver, &now, ISC_TRUE);
8329 if (resolver->badcount < resolver->badhash * 2 &&
8330 resolver->badhash > DNS_BADCACHE_SIZE)
8331 resizehash(resolver, &now, ISC_FALSE);
8333 bad->expire = *expire;
8335 UNLOCK(&resolver->lock);
8339 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8340 dns_rdatatype_t type, isc_time_t *now)
8342 dns_badcache_t *bad, *prev, *next;
8343 isc_boolean_t answer = ISC_FALSE;
8346 REQUIRE(VALID_RESOLVER(resolver));
8348 LOCK(&resolver->lock);
8349 if (resolver->badcache == NULL)
8352 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8354 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8357 * Search the hash list. Clean out expired records as we go.
8359 if (isc_time_compare(&bad->expire, now) < 0) {
8361 prev->next = bad->next;
8363 resolver->badcache[i] = bad->next;
8364 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8366 resolver->badcount--;
8369 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8377 * Slow sweep to clean out stale records.
8379 i = resolver->badsweep++ % resolver->badhash;
8380 bad = resolver->badcache[i];
8381 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8382 resolver->badcache[i] = bad->next;
8383 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8385 resolver->badcount--;
8389 UNLOCK(&resolver->lock);
8394 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8395 char namebuf[DNS_NAME_FORMATSIZE];
8396 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8397 dns_badcache_t *bad, *next, *prev;
8402 LOCK(&resolver->lock);
8403 fprintf(fp, ";\n; Bad cache\n;\n");
8405 if (resolver->badcache == NULL)
8409 for (i = 0; i < resolver->badhash; i++) {
8411 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8413 if (isc_time_compare(&bad->expire, &now) < 0) {
8415 prev->next = bad->next;
8417 resolver->badcache[i] = bad->next;
8418 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8420 resolver->badcount--;
8424 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8425 dns_rdatatype_format(bad->type, typebuf,
8427 t = isc_time_microdiff(&bad->expire, &now);
8429 fprintf(fp, "; %s/%s [ttl "
8430 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8431 namebuf, typebuf, t);
8436 UNLOCK(&resolver->lock);
8440 free_algorithm(void *node, void *arg) {
8441 unsigned char *algorithms = node;
8442 isc_mem_t *mctx = arg;
8444 isc_mem_put(mctx, algorithms, *algorithms);
8448 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8450 REQUIRE(VALID_RESOLVER(resolver));
8453 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8455 if (resolver->algorithms != NULL)
8456 dns_rbt_destroy(&resolver->algorithms);
8458 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8463 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8466 unsigned int len, mask;
8468 unsigned char *algorithms;
8469 isc_result_t result;
8470 dns_rbtnode_t *node = NULL;
8472 REQUIRE(VALID_RESOLVER(resolver));
8474 return (ISC_R_RANGE);
8477 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8479 if (resolver->algorithms == NULL) {
8480 result = dns_rbt_create(resolver->mctx, free_algorithm,
8481 resolver->mctx, &resolver->algorithms);
8482 if (result != ISC_R_SUCCESS)
8487 mask = 1 << (alg%8);
8489 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8491 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8492 algorithms = node->data;
8493 if (algorithms == NULL || len > *algorithms) {
8494 new = isc_mem_get(resolver->mctx, len);
8496 result = ISC_R_NOMEMORY;
8499 memset(new, 0, len);
8500 if (algorithms != NULL)
8501 memcpy(new, algorithms, *algorithms);
8505 if (algorithms != NULL)
8506 isc_mem_put(resolver->mctx, algorithms,
8509 algorithms[len-1] |= mask;
8511 result = ISC_R_SUCCESS;
8514 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8520 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8523 unsigned int len, mask;
8524 unsigned char *algorithms;
8526 isc_result_t result;
8527 isc_boolean_t found = ISC_FALSE;
8529 REQUIRE(VALID_RESOLVER(resolver));
8532 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8534 if (resolver->algorithms == NULL)
8536 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8537 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8539 mask = 1 << (alg%8);
8541 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8546 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8550 return (dst_algorithm_supported(alg));
8554 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8557 return (dns_ds_digest_supported(digest));
8561 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8563 REQUIRE(VALID_RESOLVER(resolver));
8566 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8568 if (resolver->mustbesecure != NULL)
8569 dns_rbt_destroy(&resolver->mustbesecure);
8571 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8575 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8578 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8579 isc_boolean_t value)
8581 isc_result_t result;
8583 REQUIRE(VALID_RESOLVER(resolver));
8586 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8588 if (resolver->mustbesecure == NULL) {
8589 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8590 &resolver->mustbesecure);
8591 if (result != ISC_R_SUCCESS)
8594 result = dns_rbt_addname(resolver->mustbesecure, name,
8595 value ? &yes : &no);
8598 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8604 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8606 isc_boolean_t value = ISC_FALSE;
8607 isc_result_t result;
8609 REQUIRE(VALID_RESOLVER(resolver));
8612 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8614 if (resolver->mustbesecure == NULL)
8616 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8617 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8618 value = *(isc_boolean_t*)data;
8621 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8627 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8628 isc_uint32_t *min, isc_uint32_t *max)
8630 REQUIRE(VALID_RESOLVER(resolver));
8632 LOCK(&resolver->lock);
8634 *cur = resolver->spillat;
8636 *min = resolver->spillatmin;
8638 *max = resolver->spillatmax;
8639 UNLOCK(&resolver->lock);
8643 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
8646 REQUIRE(VALID_RESOLVER(resolver));
8648 LOCK(&resolver->lock);
8649 resolver->spillatmin = resolver->spillat = min;
8650 resolver->spillatmax = max;
8651 UNLOCK(&resolver->lock);
8655 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
8656 REQUIRE(VALID_RESOLVER(resolver));
8658 return (resolver->zero_no_soa_ttl);
8662 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
8663 REQUIRE(VALID_RESOLVER(resolver));
8665 resolver->zero_no_soa_ttl = state;
8669 dns_resolver_getoptions(dns_resolver_t *resolver) {
8670 REQUIRE(VALID_RESOLVER(resolver));
8672 return (resolver->options);
8676 dns_resolver_gettimeout(dns_resolver_t *resolver) {
8677 REQUIRE(VALID_RESOLVER(resolver));
8679 return (resolver->query_timeout);
8683 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
8684 REQUIRE(VALID_RESOLVER(resolver));
8687 seconds = DEFAULT_QUERY_TIMEOUT;
8688 if (seconds > MAXIMUM_QUERY_TIMEOUT)
8689 seconds = MAXIMUM_QUERY_TIMEOUT;
8691 resolver->query_timeout = seconds;