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.5.2.1 2011-06-21 20:15:53 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;
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);
2567 stdoptions = 0; /* Keep compiler happy. */
2573 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2574 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2577 * If this fctx has forwarders, use them; otherwise use any
2578 * selective forwarders specified in the view; otherwise use the
2579 * resolver's forwarders (if any).
2581 sa = ISC_LIST_HEAD(fctx->forwarders);
2583 dns_forwarders_t *forwarders = NULL;
2584 dns_name_t *name = &fctx->name;
2586 unsigned int labels;
2587 dns_fixedname_t fixed;
2591 * DS records are found in the parent server.
2592 * Strip label to get the correct forwarder (if any).
2594 if (dns_rdatatype_atparent(fctx->type) &&
2595 dns_name_countlabels(name) > 1) {
2596 dns_name_init(&suffix, NULL);
2597 labels = dns_name_countlabels(name);
2598 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2602 dns_fixedname_init(&fixed);
2603 domain = dns_fixedname_name(&fixed);
2604 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2605 domain, &forwarders);
2606 if (result == ISC_R_SUCCESS) {
2607 sa = ISC_LIST_HEAD(forwarders->addrs);
2608 fctx->fwdpolicy = forwarders->fwdpolicy;
2609 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2610 isstrictsubdomain(domain, &fctx->domain)) {
2613 mctx = res->buckets[fctx->bucketnum].mctx;
2614 dns_name_free(&fctx->domain, mctx);
2615 dns_name_init(&fctx->domain, NULL);
2616 result = dns_name_dup(domain, mctx,
2618 if (result != ISC_R_SUCCESS)
2624 while (sa != NULL) {
2625 if ((isc_sockaddr_pf(sa) == AF_INET &&
2626 fctx->res->dispatchv4 == NULL) ||
2627 (isc_sockaddr_pf(sa) == AF_INET6 &&
2628 fctx->res->dispatchv6 == NULL)) {
2629 sa = ISC_LIST_NEXT(sa, link);
2633 result = dns_adb_findaddrinfo(fctx->adb,
2634 sa, &ai, 0); /* XXXMLG */
2635 if (result == ISC_R_SUCCESS) {
2636 dns_adbaddrinfo_t *cur;
2637 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2638 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2639 while (cur != NULL && cur->srtt < ai->srtt)
2640 cur = ISC_LIST_NEXT(cur, publink);
2642 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2645 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2647 sa = ISC_LIST_NEXT(sa, link);
2651 * If the forwarding policy is "only", we don't need the addresses
2652 * of the nameservers.
2654 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2658 * Normal nameservers.
2661 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2662 if (fctx->restarts == 1) {
2664 * To avoid sending out a flood of queries likely to
2665 * result in NXRRSET, we suppress fetches for address
2666 * families we don't have the first time through,
2667 * provided that we have addresses in some family we
2670 * We don't want to set this option all the time, since
2671 * if fctx->restarts > 1, we've clearly been having trouble
2672 * with the addresses we had, so getting more could help.
2674 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2676 if (res->dispatchv4 != NULL)
2677 stdoptions |= DNS_ADBFIND_INET;
2678 if (res->dispatchv6 != NULL)
2679 stdoptions |= DNS_ADBFIND_INET6;
2680 isc_stdtime_get(&now);
2682 INSIST(ISC_LIST_EMPTY(fctx->finds));
2683 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2685 for (result = dns_rdataset_first(&fctx->nameservers);
2686 result == ISC_R_SUCCESS;
2687 result = dns_rdataset_next(&fctx->nameservers))
2689 dns_rdataset_current(&fctx->nameservers, &rdata);
2691 * Extract the name from the NS record.
2693 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2694 if (result != ISC_R_SUCCESS)
2697 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2699 dns_rdata_reset(&rdata);
2700 dns_rdata_freestruct(&ns);
2702 if (result != ISC_R_NOMORE)
2706 * Do we need to use 6 to 4?
2708 if (need_alternate) {
2711 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2712 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2714 a = ISC_LIST_NEXT(a, link)) {
2715 if (!a->isaddress) {
2716 findname(fctx, &a->_u._n.name, a->_u._n.port,
2717 stdoptions, FCTX_ADDRINFO_FORWARDER,
2721 if (isc_sockaddr_pf(&a->_u.addr) != family)
2724 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2726 if (result == ISC_R_SUCCESS) {
2727 dns_adbaddrinfo_t *cur;
2728 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2729 cur = ISC_LIST_HEAD(fctx->altaddrs);
2730 while (cur != NULL && cur->srtt < ai->srtt)
2731 cur = ISC_LIST_NEXT(cur, publink);
2733 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2736 ISC_LIST_APPEND(fctx->altaddrs, ai,
2744 * Mark all known bad servers.
2746 all_bad = mark_bad(fctx);
2753 * We've got no addresses.
2755 if (fctx->pending > 0) {
2757 * We're fetching the addresses, but don't have any
2758 * yet. Tell the caller to wait for an answer.
2760 result = DNS_R_WAIT;
2765 * We've lost completely. We don't know any
2766 * addresses, and the ADB has told us it can't get
2769 FCTXTRACE("no addresses");
2770 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2771 result = isc_time_nowplusinterval(&expire, &i);
2773 (fctx->type == dns_rdatatype_dnskey ||
2774 fctx->type == dns_rdatatype_dlv ||
2775 fctx->type == dns_rdatatype_ds) &&
2776 result == ISC_R_SUCCESS)
2777 dns_resolver_addbadcache(fctx->res,
2779 fctx->type, &expire);
2780 result = ISC_R_FAILURE;
2784 * We've found some addresses. We might still be looking
2785 * for more addresses.
2787 sort_finds(&fctx->finds);
2788 sort_finds(&fctx->altfinds);
2789 result = ISC_R_SUCCESS;
2796 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2799 char buf[ISC_NETADDR_FORMATSIZE];
2801 isc_boolean_t aborted = ISC_FALSE;
2802 isc_boolean_t bogus;
2803 dns_acl_t *blackhole;
2804 isc_netaddr_t ipaddr;
2805 dns_peer_t *peer = NULL;
2806 dns_resolver_t *res;
2807 const char *msg = NULL;
2809 sa = &addr->sockaddr;
2812 isc_netaddr_fromsockaddr(&ipaddr, sa);
2813 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2814 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2816 if (blackhole != NULL) {
2819 if (dns_acl_match(&ipaddr, NULL, blackhole,
2821 &match, NULL) == ISC_R_SUCCESS &&
2827 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2832 addr->flags |= FCTX_ADDRINFO_MARK;
2833 msg = "ignoring blackholed / bogus server: ";
2834 } else if (isc_sockaddr_ismulticast(sa)) {
2835 addr->flags |= FCTX_ADDRINFO_MARK;
2836 msg = "ignoring multicast address: ";
2837 } else if (isc_sockaddr_isexperimental(sa)) {
2838 addr->flags |= FCTX_ADDRINFO_MARK;
2839 msg = "ignoring experimental address: ";
2840 } else if (sa->type.sa.sa_family != AF_INET6) {
2842 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2843 addr->flags |= FCTX_ADDRINFO_MARK;
2844 msg = "ignoring IPv6 mapped IPV4 address: ";
2845 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2846 addr->flags |= FCTX_ADDRINFO_MARK;
2847 msg = "ignoring IPv6 compatibility IPV4 address: ";
2851 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2854 isc_netaddr_fromsockaddr(&na, sa);
2855 isc_netaddr_format(&na, buf, sizeof(buf));
2856 FCTXTRACE2(msg, buf);
2859 static inline dns_adbaddrinfo_t *
2860 fctx_nextaddress(fetchctx_t *fctx) {
2861 dns_adbfind_t *find, *start;
2862 dns_adbaddrinfo_t *addrinfo;
2863 dns_adbaddrinfo_t *faddrinfo;
2866 * Return the next untried address, if any.
2870 * Find the first unmarked forwarder (if any).
2872 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2874 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2875 if (!UNMARKED(addrinfo))
2877 possibly_mark(fctx, addrinfo);
2878 if (UNMARKED(addrinfo)) {
2879 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2886 * No forwarders. Move to the next find.
2889 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2893 find = ISC_LIST_HEAD(fctx->finds);
2895 find = ISC_LIST_NEXT(find, publink);
2897 find = ISC_LIST_HEAD(fctx->finds);
2901 * Find the first unmarked addrinfo.
2907 for (addrinfo = ISC_LIST_HEAD(find->list);
2909 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2910 if (!UNMARKED(addrinfo))
2912 possibly_mark(fctx, addrinfo);
2913 if (UNMARKED(addrinfo)) {
2914 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2918 if (addrinfo != NULL)
2920 find = ISC_LIST_NEXT(find, publink);
2922 find = ISC_LIST_HEAD(fctx->finds);
2923 } while (find != start);
2927 if (addrinfo != NULL)
2931 * No nameservers left. Try alternates.
2934 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2936 find = fctx->altfind;
2938 find = ISC_LIST_HEAD(fctx->altfinds);
2940 find = ISC_LIST_NEXT(find, publink);
2942 find = ISC_LIST_HEAD(fctx->altfinds);
2946 * Find the first unmarked addrinfo.
2952 for (addrinfo = ISC_LIST_HEAD(find->list);
2954 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2955 if (!UNMARKED(addrinfo))
2957 possibly_mark(fctx, addrinfo);
2958 if (UNMARKED(addrinfo)) {
2959 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2963 if (addrinfo != NULL)
2965 find = ISC_LIST_NEXT(find, publink);
2967 find = ISC_LIST_HEAD(fctx->altfinds);
2968 } while (find != start);
2971 faddrinfo = addrinfo;
2974 * See if we have a better alternate server by address.
2977 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2979 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2980 if (!UNMARKED(addrinfo))
2982 possibly_mark(fctx, addrinfo);
2983 if (UNMARKED(addrinfo) &&
2984 (faddrinfo == NULL ||
2985 addrinfo->srtt < faddrinfo->srtt)) {
2986 if (faddrinfo != NULL)
2987 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
2988 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2993 if (addrinfo == NULL) {
2994 addrinfo = faddrinfo;
2995 fctx->altfind = find;
3002 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3003 isc_result_t result;
3004 dns_adbaddrinfo_t *addrinfo;
3008 REQUIRE(!ADDRWAIT(fctx));
3010 addrinfo = fctx_nextaddress(fctx);
3011 if (addrinfo == NULL) {
3013 * We have no more addresses. Start over.
3015 fctx_cancelqueries(fctx, ISC_TRUE);
3016 fctx_cleanupfinds(fctx);
3017 fctx_cleanupaltfinds(fctx);
3018 fctx_cleanupforwaddrs(fctx);
3019 fctx_cleanupaltaddrs(fctx);
3020 result = fctx_getaddresses(fctx, badcache);
3021 if (result == DNS_R_WAIT) {
3023 * Sleep waiting for addresses.
3025 FCTXTRACE("addrwait");
3026 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3028 } else if (result != ISC_R_SUCCESS) {
3030 * Something bad happened.
3032 fctx_done(fctx, result, __LINE__);
3036 addrinfo = fctx_nextaddress(fctx);
3038 * While we may have addresses from the ADB, they
3039 * might be bad ones. In this case, return SERVFAIL.
3041 if (addrinfo == NULL) {
3042 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3047 result = fctx_query(fctx, addrinfo, fctx->options);
3048 if (result != ISC_R_SUCCESS)
3049 fctx_done(fctx, result, __LINE__);
3051 inc_stats(fctx->res, dns_resstatscounter_retry);
3054 static isc_boolean_t
3055 fctx_destroy(fetchctx_t *fctx) {
3056 dns_resolver_t *res;
3057 unsigned int bucketnum;
3058 isc_sockaddr_t *sa, *next_sa;
3061 * Caller must be holding the bucket lock.
3064 REQUIRE(VALID_FCTX(fctx));
3065 REQUIRE(fctx->state == fetchstate_done ||
3066 fctx->state == fetchstate_init);
3067 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3068 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3069 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3070 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3071 REQUIRE(fctx->pending == 0);
3072 REQUIRE(fctx->references == 0);
3073 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3075 FCTXTRACE("destroy");
3078 bucketnum = fctx->bucketnum;
3080 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3085 for (sa = ISC_LIST_HEAD(fctx->bad);
3088 next_sa = ISC_LIST_NEXT(sa, link);
3089 ISC_LIST_UNLINK(fctx->bad, sa, link);
3090 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3093 for (sa = ISC_LIST_HEAD(fctx->edns);
3096 next_sa = ISC_LIST_NEXT(sa, link);
3097 ISC_LIST_UNLINK(fctx->edns, sa, link);
3098 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3101 for (sa = ISC_LIST_HEAD(fctx->edns512);
3104 next_sa = ISC_LIST_NEXT(sa, link);
3105 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3106 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3109 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3112 next_sa = ISC_LIST_NEXT(sa, link);
3113 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3114 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3117 isc_timer_detach(&fctx->timer);
3118 dns_message_destroy(&fctx->rmessage);
3119 dns_message_destroy(&fctx->qmessage);
3120 if (dns_name_countlabels(&fctx->domain) > 0)
3121 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3122 if (dns_rdataset_isassociated(&fctx->nameservers))
3123 dns_rdataset_disassociate(&fctx->nameservers);
3124 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3125 dns_db_detach(&fctx->cache);
3126 dns_adb_detach(&fctx->adb);
3127 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3128 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3132 UNLOCK(&res->nlock);
3134 if (res->buckets[bucketnum].exiting &&
3135 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3142 * Fetch event handlers.
3146 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3147 fetchctx_t *fctx = event->ev_arg;
3148 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3151 REQUIRE(VALID_FCTX(fctx));
3155 FCTXTRACE("timeout");
3157 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3159 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3160 fctx->reason = NULL;
3161 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3163 isc_result_t result;
3166 fctx->timeout = ISC_TRUE;
3168 * We could cancel the running queries here, or we could let
3169 * them keep going. Since we normally use separate sockets for
3170 * different queries, we adopt the former approach to reduce
3171 * the number of open sockets: cancel the oldest query if it
3172 * expired after the query had started (this is usually the
3173 * case but is not always so, depending on the task schedule
3176 query = ISC_LIST_HEAD(fctx->queries);
3177 if (query != NULL &&
3178 isc_time_compare(&tevent->due, &query->start) >= 0) {
3179 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3181 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3183 * Our timer has triggered. Reestablish the fctx lifetime
3186 result = fctx_starttimer(fctx);
3187 if (result != ISC_R_SUCCESS)
3188 fctx_done(fctx, result, __LINE__);
3193 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3196 isc_event_free(&event);
3200 fctx_shutdown(fetchctx_t *fctx) {
3201 isc_event_t *cevent;
3204 * Start the shutdown process for fctx, if it isn't already underway.
3207 FCTXTRACE("shutdown");
3210 * The caller must be holding the appropriate bucket lock.
3213 if (fctx->want_shutdown)
3216 fctx->want_shutdown = ISC_TRUE;
3219 * Unless we're still initializing (in which case the
3220 * control event is still outstanding), we need to post
3221 * the control event to tell the fetch we want it to
3224 if (fctx->state != fetchstate_init) {
3225 cevent = &fctx->control_event;
3226 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3232 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3233 fetchctx_t *fctx = event->ev_arg;
3234 isc_boolean_t bucket_empty = ISC_FALSE;
3235 dns_resolver_t *res;
3236 unsigned int bucketnum;
3237 dns_validator_t *validator;
3239 REQUIRE(VALID_FCTX(fctx));
3244 bucketnum = fctx->bucketnum;
3246 FCTXTRACE("doshutdown");
3249 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3251 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3254 * Cancel all pending validators. Note that this must be done
3255 * without the bucket lock held, since that could cause deadlock.
3257 validator = ISC_LIST_HEAD(fctx->validators);
3258 while (validator != NULL) {
3259 dns_validator_cancel(validator);
3260 validator = ISC_LIST_NEXT(validator, link);
3263 if (fctx->nsfetch != NULL)
3264 dns_resolver_cancelfetch(fctx->nsfetch);
3267 * Shut down anything that is still running on behalf of this
3268 * fetch. To avoid deadlock with the ADB, we must do this
3269 * before we lock the bucket lock.
3271 fctx_stopeverything(fctx, ISC_FALSE);
3273 LOCK(&res->buckets[bucketnum].lock);
3275 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3277 INSIST(fctx->state == fetchstate_active ||
3278 fctx->state == fetchstate_done);
3279 INSIST(fctx->want_shutdown);
3281 if (fctx->state != fetchstate_done) {
3282 fctx->state = fetchstate_done;
3283 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3286 if (fctx->references == 0 && fctx->pending == 0 &&
3287 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3288 bucket_empty = fctx_destroy(fctx);
3290 UNLOCK(&res->buckets[bucketnum].lock);
3297 fctx_start(isc_task_t *task, isc_event_t *event) {
3298 fetchctx_t *fctx = event->ev_arg;
3299 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3300 dns_resolver_t *res;
3301 unsigned int bucketnum;
3303 REQUIRE(VALID_FCTX(fctx));
3308 bucketnum = fctx->bucketnum;
3312 LOCK(&res->buckets[bucketnum].lock);
3314 INSIST(fctx->state == fetchstate_init);
3315 if (fctx->want_shutdown) {
3317 * We haven't started this fctx yet, and we've been requested
3320 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3321 fctx->state = fetchstate_done;
3322 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3324 * Since we haven't started, we INSIST that we have no
3325 * pending ADB finds and no pending validations.
3327 INSIST(fctx->pending == 0);
3328 INSIST(fctx->nqueries == 0);
3329 INSIST(ISC_LIST_EMPTY(fctx->validators));
3330 if (fctx->references == 0) {
3332 * It's now safe to destroy this fctx.
3334 bucket_empty = fctx_destroy(fctx);
3339 * Normal fctx startup.
3341 fctx->state = fetchstate_active;
3343 * Reset the control event for later use in shutting down
3346 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3347 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3351 UNLOCK(&res->buckets[bucketnum].lock);
3354 isc_result_t result;
3357 * All is well. Start working on the fetch.
3359 result = fctx_starttimer(fctx);
3360 if (result != ISC_R_SUCCESS)
3361 fctx_done(fctx, result, __LINE__);
3363 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3364 } else if (bucket_empty)
3369 * Fetch Creation, Joining, and Cancelation.
3372 static inline isc_result_t
3373 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3374 dns_messageid_t id, isc_taskaction_t action, void *arg,
3375 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3379 dns_fetchevent_t *event;
3384 * We store the task we're going to send this event to in the
3385 * sender field. We'll make the fetch the sender when we actually
3389 isc_task_attach(task, &clone);
3390 event = (dns_fetchevent_t *)
3391 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3392 action, arg, sizeof(*event));
3393 if (event == NULL) {
3394 isc_task_detach(&clone);
3395 return (ISC_R_NOMEMORY);
3397 event->result = DNS_R_SERVFAIL;
3398 event->qtype = fctx->type;
3401 event->rdataset = rdataset;
3402 event->sigrdataset = sigrdataset;
3403 event->fetch = fetch;
3404 event->client = client;
3406 dns_fixedname_init(&event->foundname);
3409 * Make sure that we can store the sigrdataset in the
3410 * first event if it is needed by any of the events.
3412 if (event->sigrdataset != NULL)
3413 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3415 ISC_LIST_APPEND(fctx->events, event, ev_link);
3417 fctx->client = client;
3419 fetch->magic = DNS_FETCH_MAGIC;
3420 fetch->private = fctx;
3422 return (ISC_R_SUCCESS);
3426 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3427 dns_name_t *domain, dns_rdataset_t *nameservers,
3428 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3431 isc_result_t result;
3432 isc_result_t iresult;
3433 isc_interval_t interval;
3434 dns_fixedname_t fixed;
3435 unsigned int findoptions = 0;
3436 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3437 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3441 * Caller must be holding the lock for bucket number 'bucketnum'.
3443 REQUIRE(fctxp != NULL && *fctxp == NULL);
3445 fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3447 return (ISC_R_NOMEMORY);
3448 dns_name_format(name, buf, sizeof(buf));
3449 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3450 strcat(buf, "/"); /* checked */
3451 strcat(buf, typebuf); /* checked */
3452 fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3453 if (fctx->info == NULL) {
3454 result = ISC_R_NOMEMORY;
3457 FCTXTRACE("create");
3458 dns_name_init(&fctx->name, NULL);
3459 result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3460 if (result != ISC_R_SUCCESS)
3462 dns_name_init(&fctx->domain, NULL);
3463 dns_rdataset_init(&fctx->nameservers);
3466 fctx->options = options;
3468 * Note! We do not attach to the task. We are relying on the
3469 * resolver to ensure that this task doesn't go away while we are
3473 fctx->references = 0;
3474 fctx->bucketnum = bucketnum;
3475 fctx->state = fetchstate_init;
3476 fctx->want_shutdown = ISC_FALSE;
3477 fctx->cloned = ISC_FALSE;
3478 ISC_LIST_INIT(fctx->queries);
3479 ISC_LIST_INIT(fctx->finds);
3480 ISC_LIST_INIT(fctx->altfinds);
3481 ISC_LIST_INIT(fctx->forwaddrs);
3482 ISC_LIST_INIT(fctx->altaddrs);
3483 ISC_LIST_INIT(fctx->forwarders);
3484 fctx->fwdpolicy = dns_fwdpolicy_none;
3485 ISC_LIST_INIT(fctx->bad);
3486 ISC_LIST_INIT(fctx->edns);
3487 ISC_LIST_INIT(fctx->edns512);
3488 ISC_LIST_INIT(fctx->bad_edns);
3489 ISC_LIST_INIT(fctx->validators);
3490 fctx->validator = NULL;
3492 fctx->altfind = NULL;
3495 fctx->querysent = 0;
3496 fctx->referrals = 0;
3497 TIME_NOW(&fctx->start);
3499 fctx->lamecount = 0;
3505 fctx->result = ISC_R_FAILURE;
3506 fctx->vresult = ISC_R_SUCCESS;
3507 fctx->exitline = -1; /* sentinel */
3508 fctx->logged = ISC_FALSE;
3509 fctx->attributes = 0;
3510 fctx->spilled = ISC_FALSE;
3512 fctx->reason = NULL;
3514 fctx->rand_bits = 0;
3515 fctx->timeout = ISC_FALSE;
3516 fctx->addrinfo = NULL;
3517 fctx->client = NULL;
3519 dns_name_init(&fctx->nsname, NULL);
3520 fctx->nsfetch = NULL;
3521 dns_rdataset_init(&fctx->nsrrset);
3523 if (domain == NULL) {
3524 dns_forwarders_t *forwarders = NULL;
3525 unsigned int labels;
3526 dns_name_t *fwdname = name;
3529 * DS records are found in the parent server.
3530 * Strip label to get the correct forwarder (if any).
3532 if (dns_rdatatype_atparent(fctx->type) &&
3533 dns_name_countlabels(name) > 1) {
3534 dns_name_init(&suffix, NULL);
3535 labels = dns_name_countlabels(name);
3536 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3539 dns_fixedname_init(&fixed);
3540 domain = dns_fixedname_name(&fixed);
3541 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3542 domain, &forwarders);
3543 if (result == ISC_R_SUCCESS)
3544 fctx->fwdpolicy = forwarders->fwdpolicy;
3546 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3548 * The caller didn't supply a query domain and
3549 * nameservers, and we're not in forward-only mode,
3550 * so find the best nameservers to use.
3552 if (dns_rdatatype_atparent(fctx->type))
3553 findoptions |= DNS_DBFIND_NOEXACT;
3554 result = dns_view_findzonecut(res->view, name, domain,
3555 0, findoptions, ISC_TRUE,
3558 if (result != ISC_R_SUCCESS)
3560 result = dns_name_dup(domain,
3561 res->buckets[bucketnum].mctx,
3563 if (result != ISC_R_SUCCESS) {
3564 dns_rdataset_disassociate(&fctx->nameservers);
3569 * We're in forward-only mode. Set the query domain.
3571 result = dns_name_dup(domain,
3572 res->buckets[bucketnum].mctx,
3574 if (result != ISC_R_SUCCESS)
3578 result = dns_name_dup(domain,
3579 res->buckets[bucketnum].mctx,
3581 if (result != ISC_R_SUCCESS)
3583 dns_rdataset_clone(nameservers, &fctx->nameservers);
3586 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3588 fctx->qmessage = NULL;
3589 result = dns_message_create(res->buckets[bucketnum].mctx,
3590 DNS_MESSAGE_INTENTRENDER,
3593 if (result != ISC_R_SUCCESS)
3594 goto cleanup_domain;
3596 fctx->rmessage = NULL;
3597 result = dns_message_create(res->buckets[bucketnum].mctx,
3598 DNS_MESSAGE_INTENTPARSE,
3601 if (result != ISC_R_SUCCESS)
3602 goto cleanup_qmessage;
3605 * Compute an expiration time for the entire fetch.
3607 isc_interval_set(&interval, res->query_timeout, 0);
3608 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3609 if (iresult != ISC_R_SUCCESS) {
3610 UNEXPECTED_ERROR(__FILE__, __LINE__,
3611 "isc_time_nowplusinterval: %s",
3612 isc_result_totext(iresult));
3613 result = ISC_R_UNEXPECTED;
3614 goto cleanup_rmessage;
3618 * Default retry interval initialization. We set the interval now
3619 * mostly so it won't be uninitialized. It will be set to the
3620 * correct value before a query is issued.
3622 isc_interval_set(&fctx->interval, 2, 0);
3625 * Create an inactive timer. It will be made active when the fetch
3626 * is actually started.
3629 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3631 res->buckets[bucketnum].task, fctx_timeout,
3632 fctx, &fctx->timer);
3633 if (iresult != ISC_R_SUCCESS) {
3634 UNEXPECTED_ERROR(__FILE__, __LINE__,
3635 "isc_timer_create: %s",
3636 isc_result_totext(iresult));
3637 result = ISC_R_UNEXPECTED;
3638 goto cleanup_rmessage;
3642 * Attach to the view's cache and adb.
3645 dns_db_attach(res->view->cachedb, &fctx->cache);
3647 dns_adb_attach(res->view->adb, &fctx->adb);
3649 ISC_LIST_INIT(fctx->events);
3650 ISC_LINK_INIT(fctx, link);
3651 fctx->magic = FCTX_MAGIC;
3653 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3657 UNLOCK(&res->nlock);
3661 return (ISC_R_SUCCESS);
3664 dns_message_destroy(&fctx->rmessage);
3667 dns_message_destroy(&fctx->qmessage);
3670 if (dns_name_countlabels(&fctx->domain) > 0)
3671 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3672 if (dns_rdataset_isassociated(&fctx->nameservers))
3673 dns_rdataset_disassociate(&fctx->nameservers);
3676 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3679 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3682 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3690 static inline isc_boolean_t
3691 is_lame(fetchctx_t *fctx) {
3692 dns_message_t *message = fctx->rmessage;
3694 dns_rdataset_t *rdataset;
3695 isc_result_t result;
3697 if (message->rcode != dns_rcode_noerror &&
3698 message->rcode != dns_rcode_nxdomain)
3701 if (message->counts[DNS_SECTION_ANSWER] != 0)
3704 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3707 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3708 while (result == ISC_R_SUCCESS) {
3710 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3711 for (rdataset = ISC_LIST_HEAD(name->list);
3713 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3714 dns_namereln_t namereln;
3716 unsigned int labels;
3717 if (rdataset->type != dns_rdatatype_ns)
3719 namereln = dns_name_fullcompare(name, &fctx->domain,
3721 if (namereln == dns_namereln_equal &&
3722 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3724 if (namereln == dns_namereln_subdomain)
3728 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3735 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3736 char namebuf[DNS_NAME_FORMATSIZE];
3737 char domainbuf[DNS_NAME_FORMATSIZE];
3738 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3740 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3741 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3742 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3743 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3744 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3745 "lame server resolving '%s' (in '%s'?): %s",
3746 namebuf, domainbuf, addrbuf);
3750 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3751 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3752 char clbuf[ISC_SOCKADDR_FORMATSIZE];
3753 const char *clmsg = "";
3757 va_start(args, format);
3758 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3761 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3763 if (fctx->client != NULL) {
3764 clmsg = " for client ";
3765 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3770 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3771 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3772 "DNS format error from %s resolving %s%s%s: %s",
3773 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3776 static inline isc_result_t
3777 same_question(fetchctx_t *fctx) {
3778 isc_result_t result;
3779 dns_message_t *message = fctx->rmessage;
3781 dns_rdataset_t *rdataset;
3784 * Caller must be holding the fctx lock.
3788 * XXXRTH Currently we support only one question.
3790 if (message->counts[DNS_SECTION_QUESTION] != 1) {
3791 log_formerr(fctx, "too many questions");
3792 return (DNS_R_FORMERR);
3795 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3796 if (result != ISC_R_SUCCESS)
3799 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3800 rdataset = ISC_LIST_HEAD(name->list);
3801 INSIST(rdataset != NULL);
3802 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3804 if (fctx->type != rdataset->type ||
3805 fctx->res->rdclass != rdataset->rdclass ||
3806 !dns_name_equal(&fctx->name, name)) {
3807 char namebuf[DNS_NAME_FORMATSIZE];
3808 char class[DNS_RDATACLASS_FORMATSIZE];
3809 char type[DNS_RDATATYPE_FORMATSIZE];
3811 dns_name_format(name, namebuf, sizeof(namebuf));
3812 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3813 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3814 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3815 namebuf, class, type);
3816 return (DNS_R_FORMERR);
3819 return (ISC_R_SUCCESS);
3823 clone_results(fetchctx_t *fctx) {
3824 dns_fetchevent_t *event, *hevent;
3825 isc_result_t result;
3826 dns_name_t *name, *hname;
3828 FCTXTRACE("clone_results");
3831 * Set up any other events to have the same data as the first
3834 * Caller must be holding the appropriate lock.
3837 fctx->cloned = ISC_TRUE;
3838 hevent = ISC_LIST_HEAD(fctx->events);
3841 hname = dns_fixedname_name(&hevent->foundname);
3842 for (event = ISC_LIST_NEXT(hevent, ev_link);
3844 event = ISC_LIST_NEXT(event, ev_link)) {
3845 name = dns_fixedname_name(&event->foundname);
3846 result = dns_name_copy(hname, name, NULL);
3847 if (result != ISC_R_SUCCESS)
3848 event->result = result;
3850 event->result = hevent->result;
3851 dns_db_attach(hevent->db, &event->db);
3852 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3853 INSIST(hevent->rdataset != NULL);
3854 INSIST(event->rdataset != NULL);
3855 if (dns_rdataset_isassociated(hevent->rdataset))
3856 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3857 INSIST(! (hevent->sigrdataset == NULL &&
3858 event->sigrdataset != NULL));
3859 if (hevent->sigrdataset != NULL &&
3860 dns_rdataset_isassociated(hevent->sigrdataset) &&
3861 event->sigrdataset != NULL)
3862 dns_rdataset_clone(hevent->sigrdataset,
3863 event->sigrdataset);
3867 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3868 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3869 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3870 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3871 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3872 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3873 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3877 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3878 * no references and is no longer waiting for any events). If this
3879 * was the last fctx in the resolver, destroy the resolver.
3882 * '*fctx' is shutting down.
3885 maybe_destroy(fetchctx_t *fctx) {
3886 unsigned int bucketnum;
3887 isc_boolean_t bucket_empty = ISC_FALSE;
3888 dns_resolver_t *res = fctx->res;
3889 dns_validator_t *validator, *next_validator;
3891 REQUIRE(SHUTTINGDOWN(fctx));
3893 if (fctx->pending != 0 || fctx->nqueries != 0)
3896 for (validator = ISC_LIST_HEAD(fctx->validators);
3897 validator != NULL; validator = next_validator) {
3898 next_validator = ISC_LIST_NEXT(validator, link);
3899 dns_validator_cancel(validator);
3902 bucketnum = fctx->bucketnum;
3903 LOCK(&res->buckets[bucketnum].lock);
3904 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3905 bucket_empty = fctx_destroy(fctx);
3906 UNLOCK(&res->buckets[bucketnum].lock);
3913 * The validator has finished.
3916 validated(isc_task_t *task, isc_event_t *event) {
3917 isc_result_t result = ISC_R_SUCCESS;
3918 isc_result_t eresult = ISC_R_SUCCESS;
3921 dns_validatorevent_t *vevent;
3922 dns_fetchevent_t *hevent;
3923 dns_rdataset_t *ardataset = NULL;
3924 dns_rdataset_t *asigrdataset = NULL;
3925 dns_dbnode_t *node = NULL;
3926 isc_boolean_t negative;
3927 isc_boolean_t chaining;
3928 isc_boolean_t sentresponse;
3930 dns_dbnode_t *nsnode = NULL;
3932 dns_rdataset_t *rdataset;
3933 dns_rdataset_t *sigrdataset;
3934 dns_valarg_t *valarg;
3935 dns_adbaddrinfo_t *addrinfo;
3937 UNUSED(task); /* for now */
3939 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3940 valarg = event->ev_arg;
3941 fctx = valarg->fctx;
3942 addrinfo = valarg->addrinfo;
3943 REQUIRE(VALID_FCTX(fctx));
3944 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3946 vevent = (dns_validatorevent_t *)event;
3947 fctx->vresult = vevent->result;
3949 FCTXTRACE("received validation completion event");
3951 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
3952 fctx->validator = NULL;
3955 * Destroy the validator early so that we can
3956 * destroy the fctx if necessary.
3958 dns_validator_destroy(&vevent->validator);
3959 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
3960 valarg, sizeof(*valarg));
3962 negative = ISC_TF(vevent->rdataset == NULL);
3964 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
3967 * If shutting down, ignore the results. Check to see if we're
3968 * done waiting for validator completions and ADB pending events; if
3969 * so, destroy the fctx.
3971 if (SHUTTINGDOWN(fctx) && !sentresponse) {
3972 maybe_destroy(fctx); /* Locks bucket. */
3976 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3978 isc_stdtime_get(&now);
3981 * If chaining, we need to make sure that the right result code is
3982 * returned, and that the rdatasets are bound.
3984 if (vevent->result == ISC_R_SUCCESS &&
3986 vevent->rdataset != NULL &&
3987 CHAINING(vevent->rdataset))
3989 if (vevent->rdataset->type == dns_rdatatype_cname)
3990 eresult = DNS_R_CNAME;
3992 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
3993 eresult = DNS_R_DNAME;
3995 chaining = ISC_TRUE;
3997 chaining = ISC_FALSE;
4000 * Either we're not shutting down, or we are shutting down but want
4001 * to cache the result anyway (if this was a validation started by
4002 * a query with cd set)
4005 hevent = ISC_LIST_HEAD(fctx->events);
4006 if (hevent != NULL) {
4007 if (!negative && !chaining &&
4008 (fctx->type == dns_rdatatype_any ||
4009 fctx->type == dns_rdatatype_rrsig ||
4010 fctx->type == dns_rdatatype_sig)) {
4012 * Don't bind rdatasets; the caller
4013 * will iterate the node.
4016 ardataset = hevent->rdataset;
4017 asigrdataset = hevent->sigrdataset;
4021 if (vevent->result != ISC_R_SUCCESS) {
4022 FCTXTRACE("validation failed");
4023 inc_stats(fctx->res, dns_resstatscounter_valfail);
4025 fctx->vresult = vevent->result;
4026 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4027 result = ISC_R_NOTFOUND;
4028 if (vevent->rdataset != NULL)
4029 result = dns_db_findnode(fctx->cache,
4032 if (result == ISC_R_SUCCESS)
4033 (void)dns_db_deleterdataset(fctx->cache, node,
4036 if (result == ISC_R_SUCCESS &&
4037 vevent->sigrdataset != NULL)
4038 (void)dns_db_deleterdataset(fctx->cache, node,
4040 dns_rdatatype_rrsig,
4042 if (result == ISC_R_SUCCESS)
4043 dns_db_detachnode(fctx->cache, &node);
4045 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4047 * Cache the data as pending for later validation.
4049 result = ISC_R_NOTFOUND;
4050 if (vevent->rdataset != NULL)
4051 result = dns_db_findnode(fctx->cache,
4054 if (result == ISC_R_SUCCESS) {
4055 (void)dns_db_addrdataset(fctx->cache, node,
4057 vevent->rdataset, 0,
4060 if (result == ISC_R_SUCCESS &&
4061 vevent->sigrdataset != NULL)
4062 (void)dns_db_addrdataset(fctx->cache, node,
4064 vevent->sigrdataset,
4066 if (result == ISC_R_SUCCESS)
4067 dns_db_detachnode(fctx->cache, &node);
4069 result = fctx->vresult;
4070 add_bad(fctx, addrinfo, result, badns_validation);
4071 isc_event_free(&event);
4072 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4073 INSIST(fctx->validator == NULL);
4074 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4075 if (fctx->validator != NULL)
4076 dns_validator_send(fctx->validator);
4077 else if (sentresponse)
4078 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4079 else if (result == DNS_R_BROKENCHAIN) {
4080 isc_result_t tresult;
4084 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4085 tresult = isc_time_nowplusinterval(&expire, &i);
4087 (fctx->type == dns_rdatatype_dnskey ||
4088 fctx->type == dns_rdatatype_dlv ||
4089 fctx->type == dns_rdatatype_ds) &&
4090 tresult == ISC_R_SUCCESS)
4091 dns_resolver_addbadcache(fctx->res,
4093 fctx->type, &expire);
4094 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4096 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4102 dns_rdatatype_t covers;
4103 FCTXTRACE("nonexistence validation OK");
4105 inc_stats(fctx->res, dns_resstatscounter_valnegsuccess);
4107 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4108 covers = dns_rdatatype_any;
4110 covers = fctx->type;
4112 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4114 if (result != ISC_R_SUCCESS)
4115 goto noanswer_response;
4118 * If we are asking for a SOA record set the cache time
4119 * to zero to facilitate locating the containing zone of
4122 ttl = fctx->res->view->maxncachettl;
4123 if (fctx->type == dns_rdatatype_soa &&
4124 covers == dns_rdatatype_any &&
4125 fctx->res->zero_no_soa_ttl)
4128 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4129 covers, now, ttl, vevent->optout,
4130 ardataset, &eresult);
4131 if (result != ISC_R_SUCCESS)
4132 goto noanswer_response;
4133 goto answer_response;
4135 inc_stats(fctx->res, dns_resstatscounter_valsuccess);
4137 FCTXTRACE("validation OK");
4139 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4141 result = dns_rdataset_addnoqname(vevent->rdataset,
4142 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4143 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4144 INSIST(vevent->sigrdataset != NULL);
4145 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4146 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4147 result = dns_rdataset_addclosest(vevent->rdataset,
4148 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4149 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4154 * The data was already cached as pending data.
4155 * Re-cache it as secure and bind the cached
4156 * rdatasets to the first event on the fetch
4159 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4160 if (result != ISC_R_SUCCESS)
4161 goto noanswer_response;
4163 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4164 vevent->rdataset, 0, ardataset);
4165 if (result != ISC_R_SUCCESS &&
4166 result != DNS_R_UNCHANGED)
4167 goto noanswer_response;
4168 if (ardataset != NULL && NEGATIVE(ardataset)) {
4169 if (NXDOMAIN(ardataset))
4170 eresult = DNS_R_NCACHENXDOMAIN;
4172 eresult = DNS_R_NCACHENXRRSET;
4173 } else if (vevent->sigrdataset != NULL) {
4174 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4175 vevent->sigrdataset, 0,
4177 if (result != ISC_R_SUCCESS &&
4178 result != DNS_R_UNCHANGED)
4179 goto noanswer_response;
4184 * If we only deferred the destroy because we wanted to cache
4185 * the data, destroy now.
4187 dns_db_detachnode(fctx->cache, &node);
4188 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4189 if (SHUTTINGDOWN(fctx))
4190 maybe_destroy(fctx); /* Locks bucket. */
4194 if (!ISC_LIST_EMPTY(fctx->validators)) {
4196 INSIST(fctx->type == dns_rdatatype_any ||
4197 fctx->type == dns_rdatatype_rrsig ||
4198 fctx->type == dns_rdatatype_sig);
4200 * Don't send a response yet - we have
4201 * more rdatasets that still need to
4204 dns_db_detachnode(fctx->cache, &node);
4205 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4206 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4212 * Cache any NS/NSEC records that happened to be validated.
4214 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4215 while (result == ISC_R_SUCCESS) {
4217 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4219 for (rdataset = ISC_LIST_HEAD(name->list);
4221 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4222 if ((rdataset->type != dns_rdatatype_ns &&
4223 rdataset->type != dns_rdatatype_nsec) ||
4224 rdataset->trust != dns_trust_secure)
4226 for (sigrdataset = ISC_LIST_HEAD(name->list);
4227 sigrdataset != NULL;
4228 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4229 if (sigrdataset->type != dns_rdatatype_rrsig ||
4230 sigrdataset->covers != rdataset->type)
4234 if (sigrdataset == NULL ||
4235 sigrdataset->trust != dns_trust_secure)
4237 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4239 if (result != ISC_R_SUCCESS)
4242 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4243 now, rdataset, 0, NULL);
4244 if (result == ISC_R_SUCCESS)
4245 result = dns_db_addrdataset(fctx->cache, nsnode,
4249 dns_db_detachnode(fctx->cache, &nsnode);
4251 result = dns_message_nextname(fctx->rmessage,
4252 DNS_SECTION_AUTHORITY);
4255 result = ISC_R_SUCCESS;
4258 * Respond with an answer, positive or negative,
4259 * as opposed to an error. 'node' must be non-NULL.
4262 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4264 if (hevent != NULL) {
4265 hevent->result = eresult;
4266 RUNTIME_CHECK(dns_name_copy(vevent->name,
4267 dns_fixedname_name(&hevent->foundname), NULL)
4269 dns_db_attach(fctx->cache, &hevent->db);
4270 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4271 clone_results(fctx);
4276 dns_db_detachnode(fctx->cache, &node);
4278 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4280 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4283 INSIST(node == NULL);
4284 isc_event_free(&event);
4287 static inline isc_result_t
4288 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4291 dns_rdataset_t *rdataset, *sigrdataset;
4292 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4293 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4294 dns_dbnode_t *node, **anodep;
4297 dns_resolver_t *res;
4298 isc_boolean_t need_validation, secure_domain, have_answer;
4299 isc_result_t result, eresult;
4300 dns_fetchevent_t *event;
4301 unsigned int options;
4304 unsigned int valoptions = 0;
4307 * The appropriate bucket lock must be held.
4311 need_validation = ISC_FALSE;
4312 secure_domain = ISC_FALSE;
4313 have_answer = ISC_FALSE;
4314 eresult = ISC_R_SUCCESS;
4315 task = res->buckets[fctx->bucketnum].task;
4318 * Is DNSSEC validation required for this name?
4320 if (res->view->enablevalidation) {
4321 result = dns_view_issecuredomain(res->view, name,
4323 if (result != ISC_R_SUCCESS)
4326 if (!secure_domain && res->view->dlv != NULL) {
4327 valoptions = DNS_VALIDATOR_DLV;
4328 secure_domain = ISC_TRUE;
4332 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4333 need_validation = ISC_FALSE;
4335 need_validation = secure_domain;
4341 asigrdataset = NULL;
4343 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4345 have_answer = ISC_TRUE;
4346 event = ISC_LIST_HEAD(fctx->events);
4347 if (event != NULL) {
4349 aname = dns_fixedname_name(&event->foundname);
4350 result = dns_name_copy(name, aname, NULL);
4351 if (result != ISC_R_SUCCESS)
4353 anodep = &event->node;
4355 * If this is an ANY, SIG or RRSIG query, we're not
4356 * going to return any rdatasets, unless we encountered
4357 * a CNAME or DNAME as "the answer". In this case,
4358 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4359 * and we must set up the rdatasets.
4361 if ((fctx->type != dns_rdatatype_any &&
4362 fctx->type != dns_rdatatype_rrsig &&
4363 fctx->type != dns_rdatatype_sig) ||
4364 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4365 ardataset = event->rdataset;
4366 asigrdataset = event->sigrdataset;
4372 * Find or create the cache node.
4375 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4376 if (result != ISC_R_SUCCESS)
4380 * Cache or validate each cacheable rdataset.
4382 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4383 for (rdataset = ISC_LIST_HEAD(name->list);
4385 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4386 if (!CACHE(rdataset))
4388 if (CHECKNAMES(rdataset)) {
4389 char namebuf[DNS_NAME_FORMATSIZE];
4390 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4391 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4393 dns_name_format(name, namebuf, sizeof(namebuf));
4394 dns_rdatatype_format(rdataset->type, typebuf,
4396 dns_rdataclass_format(rdataset->rdclass, classbuf,
4398 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4399 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4400 "check-names %s %s/%s/%s",
4401 fail ? "failure" : "warning",
4402 namebuf, typebuf, classbuf);
4404 if (ANSWER(rdataset)) {
4405 dns_db_detachnode(fctx->cache, &node);
4406 return (DNS_R_BADNAME);
4413 * Enforce the configure maximum cache TTL.
4415 if (rdataset->ttl > res->view->maxcachettl)
4416 rdataset->ttl = res->view->maxcachettl;
4419 * If this RRset is in a secure domain, is in bailiwick,
4420 * and is not glue, attempt DNSSEC validation. (We do not
4421 * attempt to validate glue or out-of-bailiwick data--even
4422 * though there might be some performance benefit to doing
4423 * so--because it makes it simpler and safer to ensure that
4424 * records from a secure domain are only cached if validated
4425 * within the context of a query to the domain that owns
4428 if (secure_domain && rdataset->trust != dns_trust_glue &&
4429 !EXTERNAL(rdataset)) {
4433 * RRSIGs are validated as part of validating the
4436 if (rdataset->type == dns_rdatatype_rrsig)
4439 * Find the SIG for this rdataset, if we have it.
4441 for (sigrdataset = ISC_LIST_HEAD(name->list);
4442 sigrdataset != NULL;
4443 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4444 if (sigrdataset->type == dns_rdatatype_rrsig &&
4445 sigrdataset->covers == rdataset->type)
4448 if (sigrdataset == NULL) {
4449 if (!ANSWER(rdataset) && need_validation) {
4451 * Ignore non-answer rdatasets that
4452 * are missing signatures.
4459 * Normalize the rdataset and sigrdataset TTLs.
4461 if (sigrdataset != NULL) {
4462 rdataset->ttl = ISC_MIN(rdataset->ttl,
4464 sigrdataset->ttl = rdataset->ttl;
4468 * Cache this rdataset/sigrdataset pair as
4469 * pending data. Track whether it was additional
4472 if (rdataset->trust == dns_trust_additional)
4473 trust = dns_trust_pending_additional;
4475 trust = dns_trust_pending_answer;
4477 rdataset->trust = trust;
4478 if (sigrdataset != NULL)
4479 sigrdataset->trust = trust;
4480 if (!need_validation || !ANSWER(rdataset)) {
4481 addedrdataset = ardataset;
4482 result = dns_db_addrdataset(fctx->cache, node,
4483 NULL, now, rdataset,
4485 if (result == DNS_R_UNCHANGED) {
4486 result = ISC_R_SUCCESS;
4487 if (!need_validation &&
4488 ardataset != NULL &&
4489 NEGATIVE(ardataset)) {
4491 * The answer in the cache is
4492 * better than the answer we
4493 * found, and is a negative
4494 * cache entry, so we must set
4495 * eresult appropriately.
4497 if (NXDOMAIN(ardataset))
4499 DNS_R_NCACHENXDOMAIN;
4502 DNS_R_NCACHENXRRSET;
4504 * We have a negative response
4505 * from the cache so don't
4506 * attempt to add the RRSIG
4512 if (result != ISC_R_SUCCESS)
4514 if (sigrdataset != NULL) {
4515 addedrdataset = asigrdataset;
4516 result = dns_db_addrdataset(fctx->cache,
4520 if (result == DNS_R_UNCHANGED)
4521 result = ISC_R_SUCCESS;
4522 if (result != ISC_R_SUCCESS)
4524 } else if (!ANSWER(rdataset))
4528 if (ANSWER(rdataset) && need_validation) {
4529 if (fctx->type != dns_rdatatype_any &&
4530 fctx->type != dns_rdatatype_rrsig &&
4531 fctx->type != dns_rdatatype_sig) {
4533 * This is The Answer. We will
4534 * validate it, but first we cache
4535 * the rest of the response - it may
4536 * contain useful keys.
4538 INSIST(valrdataset == NULL &&
4539 valsigrdataset == NULL);
4540 valrdataset = rdataset;
4541 valsigrdataset = sigrdataset;
4544 * This is one of (potentially)
4545 * multiple answers to an ANY
4546 * or SIG query. To keep things
4547 * simple, we just start the
4548 * validator right away rather
4549 * than caching first and
4550 * having to remember which
4551 * rdatasets needed validation.
4553 result = valcreate(fctx, addrinfo,
4554 name, rdataset->type,
4559 * Defer any further validations.
4560 * This prevents multiple validators
4561 * from manipulating fctx->rmessage
4564 valoptions |= DNS_VALIDATOR_DEFER;
4566 } else if (CHAINING(rdataset)) {
4567 if (rdataset->type == dns_rdatatype_cname)
4568 eresult = DNS_R_CNAME;
4570 INSIST(rdataset->type ==
4571 dns_rdatatype_dname);
4572 eresult = DNS_R_DNAME;
4575 } else if (!EXTERNAL(rdataset)) {
4577 * It's OK to cache this rdataset now.
4579 if (ANSWER(rdataset))
4580 addedrdataset = ardataset;
4581 else if (ANSWERSIG(rdataset))
4582 addedrdataset = asigrdataset;
4584 addedrdataset = NULL;
4585 if (CHAINING(rdataset)) {
4586 if (rdataset->type == dns_rdatatype_cname)
4587 eresult = DNS_R_CNAME;
4589 INSIST(rdataset->type ==
4590 dns_rdatatype_dname);
4591 eresult = DNS_R_DNAME;
4594 if (rdataset->trust == dns_trust_glue &&
4595 (rdataset->type == dns_rdatatype_ns ||
4596 (rdataset->type == dns_rdatatype_rrsig &&
4597 rdataset->covers == dns_rdatatype_ns))) {
4599 * If the trust level is 'dns_trust_glue'
4600 * then we are adding data from a referral
4601 * we got while executing the search algorithm.
4602 * New referral data always takes precedence
4603 * over the existing cache contents.
4605 options = DNS_DBADD_FORCE;
4609 * Now we can add the rdataset.
4611 result = dns_db_addrdataset(fctx->cache,
4616 if (result == DNS_R_UNCHANGED) {
4617 if (ANSWER(rdataset) &&
4618 ardataset != NULL &&
4619 NEGATIVE(ardataset)) {
4621 * The answer in the cache is better
4622 * than the answer we found, and is
4623 * a negative cache entry, so we
4624 * must set eresult appropriately.
4626 if (NXDOMAIN(ardataset))
4627 eresult = DNS_R_NCACHENXDOMAIN;
4629 eresult = DNS_R_NCACHENXRRSET;
4631 result = ISC_R_SUCCESS;
4632 } else if (result != ISC_R_SUCCESS)
4637 if (valrdataset != NULL)
4638 result = valcreate(fctx, addrinfo, name, fctx->type,
4639 valrdataset, valsigrdataset, valoptions,
4642 if (result == ISC_R_SUCCESS && have_answer) {
4643 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4644 if (event != NULL) {
4646 * Negative results must be indicated in event->result.
4648 if (dns_rdataset_isassociated(event->rdataset) &&
4649 NEGATIVE(event->rdataset)) {
4650 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4651 eresult == DNS_R_NCACHENXRRSET);
4653 event->result = eresult;
4654 dns_db_attach(fctx->cache, adbp);
4655 dns_db_transfernode(fctx->cache, &node, anodep);
4656 clone_results(fctx);
4661 dns_db_detachnode(fctx->cache, &node);
4666 static inline isc_result_t
4667 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4669 isc_result_t result;
4670 dns_section_t section;
4673 FCTXTRACE("cache_message");
4675 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4677 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4679 for (section = DNS_SECTION_ANSWER;
4680 section <= DNS_SECTION_ADDITIONAL;
4682 result = dns_message_firstname(fctx->rmessage, section);
4683 while (result == ISC_R_SUCCESS) {
4685 dns_message_currentname(fctx->rmessage, section,
4687 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4688 result = cache_name(fctx, name, addrinfo, now);
4689 if (result != ISC_R_SUCCESS)
4692 result = dns_message_nextname(fctx->rmessage, section);
4694 if (result != ISC_R_NOMORE)
4697 if (result == ISC_R_NOMORE)
4698 result = ISC_R_SUCCESS;
4700 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4706 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4709 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4710 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4711 isc_boolean_t optout, dns_rdataset_t *ardataset,
4712 isc_result_t *eresultp)
4714 isc_result_t result;
4715 dns_rdataset_t rdataset;
4717 if (ardataset == NULL) {
4718 dns_rdataset_init(&rdataset);
4719 ardataset = &rdataset;
4721 result = dns_ncache_addoptout(message, cache, node, covers, now,
4722 maxttl, optout, ardataset);
4723 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4725 * If the cache now contains a negative entry and we
4726 * care about whether it is DNS_R_NCACHENXDOMAIN or
4727 * DNS_R_NCACHENXRRSET then extract it.
4729 if (NEGATIVE(ardataset)) {
4731 * The cache data is a negative cache entry.
4733 if (NXDOMAIN(ardataset))
4734 *eresultp = DNS_R_NCACHENXDOMAIN;
4736 *eresultp = DNS_R_NCACHENXRRSET;
4739 * Either we don't care about the nature of the
4740 * cache rdataset (because no fetch is interested
4741 * in the outcome), or the cache rdataset is not
4742 * a negative cache entry. Whichever case it is,
4743 * we can return success.
4745 * XXXRTH There's a CNAME/DNAME problem here.
4747 *eresultp = ISC_R_SUCCESS;
4749 result = ISC_R_SUCCESS;
4751 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4752 dns_rdataset_disassociate(ardataset);
4757 static inline isc_result_t
4758 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4759 dns_rdatatype_t covers, isc_stdtime_t now)
4761 isc_result_t result, eresult;
4763 dns_resolver_t *res;
4765 dns_dbnode_t *node, **anodep;
4766 dns_rdataset_t *ardataset;
4767 isc_boolean_t need_validation, secure_domain;
4769 dns_fetchevent_t *event;
4771 unsigned int valoptions = 0;
4773 FCTXTRACE("ncache_message");
4775 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4778 need_validation = ISC_FALSE;
4779 secure_domain = ISC_FALSE;
4780 eresult = ISC_R_SUCCESS;
4785 * XXXMPA remove when we follow cnames and adjust the setting
4786 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4788 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4791 * Is DNSSEC validation required for this name?
4793 if (fctx->res->view->enablevalidation) {
4794 result = dns_view_issecuredomain(res->view, name,
4796 if (result != ISC_R_SUCCESS)
4799 if (!secure_domain && res->view->dlv != NULL) {
4800 valoptions = DNS_VALIDATOR_DLV;
4801 secure_domain = ISC_TRUE;
4805 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4806 need_validation = ISC_FALSE;
4808 need_validation = secure_domain;
4810 if (secure_domain) {
4812 * Mark all rdatasets as pending.
4814 dns_rdataset_t *trdataset;
4817 result = dns_message_firstname(fctx->rmessage,
4818 DNS_SECTION_AUTHORITY);
4819 while (result == ISC_R_SUCCESS) {
4821 dns_message_currentname(fctx->rmessage,
4822 DNS_SECTION_AUTHORITY,
4824 for (trdataset = ISC_LIST_HEAD(tname->list);
4826 trdataset = ISC_LIST_NEXT(trdataset, link))
4827 trdataset->trust = dns_trust_pending_answer;
4828 result = dns_message_nextname(fctx->rmessage,
4829 DNS_SECTION_AUTHORITY);
4831 if (result != ISC_R_NOMORE)
4836 if (need_validation) {
4838 * Do negative response validation.
4840 result = valcreate(fctx, addrinfo, name, fctx->type,
4841 NULL, NULL, valoptions,
4842 res->buckets[fctx->bucketnum].task);
4844 * If validation is necessary, return now. Otherwise continue
4845 * to process the message, letting the validation complete
4846 * in its own good time.
4851 LOCK(&res->buckets[fctx->bucketnum].lock);
4857 if (!HAVE_ANSWER(fctx)) {
4858 event = ISC_LIST_HEAD(fctx->events);
4859 if (event != NULL) {
4861 aname = dns_fixedname_name(&event->foundname);
4862 result = dns_name_copy(name, aname, NULL);
4863 if (result != ISC_R_SUCCESS)
4865 anodep = &event->node;
4866 ardataset = event->rdataset;
4871 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4872 if (result != ISC_R_SUCCESS)
4876 * If we are asking for a SOA record set the cache time
4877 * to zero to facilitate locating the containing zone of
4880 ttl = fctx->res->view->maxncachettl;
4881 if (fctx->type == dns_rdatatype_soa &&
4882 covers == dns_rdatatype_any &&
4883 fctx->res->zero_no_soa_ttl)
4886 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4887 covers, now, ttl, ISC_FALSE,
4888 ardataset, &eresult);
4889 if (result != ISC_R_SUCCESS)
4892 if (!HAVE_ANSWER(fctx)) {
4893 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4894 if (event != NULL) {
4895 event->result = eresult;
4896 dns_db_attach(fctx->cache, adbp);
4897 dns_db_transfernode(fctx->cache, &node, anodep);
4898 clone_results(fctx);
4903 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4906 dns_db_detachnode(fctx->cache, &node);
4912 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4913 isc_boolean_t external, isc_boolean_t gluing)
4915 name->attributes |= DNS_NAMEATTR_CACHE;
4917 rdataset->trust = dns_trust_glue;
4919 * Glue with 0 TTL causes problems. We force the TTL to
4920 * 1 second to prevent this.
4922 if (rdataset->ttl == 0)
4925 rdataset->trust = dns_trust_additional;
4927 * Avoid infinite loops by only marking new rdatasets.
4929 if (!CACHE(rdataset)) {
4930 name->attributes |= DNS_NAMEATTR_CHASE;
4931 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4933 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4935 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4939 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
4940 dns_section_t section)
4942 fetchctx_t *fctx = arg;
4943 isc_result_t result;
4945 dns_rdataset_t *rdataset;
4946 isc_boolean_t external;
4947 dns_rdatatype_t rtype;
4948 isc_boolean_t gluing;
4950 REQUIRE(VALID_FCTX(fctx));
4952 #if CHECK_FOR_GLUE_IN_ANSWER
4953 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
4954 return (ISC_R_SUCCESS);
4963 result = dns_message_findname(fctx->rmessage, section, addname,
4964 dns_rdatatype_any, 0, &name, NULL);
4965 if (result == ISC_R_SUCCESS) {
4966 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
4967 if (type == dns_rdatatype_a) {
4968 for (rdataset = ISC_LIST_HEAD(name->list);
4970 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4971 if (rdataset->type == dns_rdatatype_rrsig)
4972 rtype = rdataset->covers;
4974 rtype = rdataset->type;
4975 if (rtype == dns_rdatatype_a ||
4976 rtype == dns_rdatatype_aaaa)
4977 mark_related(name, rdataset, external,
4981 result = dns_message_findtype(name, type, 0,
4983 if (result == ISC_R_SUCCESS) {
4984 mark_related(name, rdataset, external, gluing);
4986 * Do we have its SIG too?
4989 result = dns_message_findtype(name,
4990 dns_rdatatype_rrsig,
4992 if (result == ISC_R_SUCCESS)
4993 mark_related(name, rdataset, external,
4999 return (ISC_R_SUCCESS);
5003 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5004 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5007 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5008 #define CHECK_FOR_GLUE_IN_ANSWER 0
5010 #if CHECK_FOR_GLUE_IN_ANSWER
5012 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5013 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5018 chase_additional(fetchctx_t *fctx) {
5019 isc_boolean_t rescan;
5020 dns_section_t section = DNS_SECTION_ADDITIONAL;
5021 isc_result_t result;
5026 for (result = dns_message_firstname(fctx->rmessage, section);
5027 result == ISC_R_SUCCESS;
5028 result = dns_message_nextname(fctx->rmessage, section)) {
5029 dns_name_t *name = NULL;
5030 dns_rdataset_t *rdataset;
5031 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5033 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5035 name->attributes &= ~DNS_NAMEATTR_CHASE;
5036 for (rdataset = ISC_LIST_HEAD(name->list);
5038 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5039 if (CHASE(rdataset)) {
5040 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5041 (void)dns_rdataset_additionaldata(rdataset,
5052 static inline isc_result_t
5053 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5054 isc_result_t result;
5055 dns_rdata_t rdata = DNS_RDATA_INIT;
5056 dns_rdata_cname_t cname;
5058 result = dns_rdataset_first(rdataset);
5059 if (result != ISC_R_SUCCESS)
5061 dns_rdataset_current(rdataset, &rdata);
5062 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5063 if (result != ISC_R_SUCCESS)
5065 dns_name_init(tname, NULL);
5066 dns_name_clone(&cname.cname, tname);
5067 dns_rdata_freestruct(&cname);
5069 return (ISC_R_SUCCESS);
5072 static inline isc_result_t
5073 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5074 dns_name_t *oname, dns_fixedname_t *fixeddname)
5076 isc_result_t result;
5077 dns_rdata_t rdata = DNS_RDATA_INIT;
5078 unsigned int nlabels;
5080 dns_namereln_t namereln;
5081 dns_rdata_dname_t dname;
5082 dns_fixedname_t prefix;
5085 * Get the target name of the DNAME.
5087 result = dns_rdataset_first(rdataset);
5088 if (result != ISC_R_SUCCESS)
5090 dns_rdataset_current(rdataset, &rdata);
5091 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5092 if (result != ISC_R_SUCCESS)
5096 * Get the prefix of qname.
5098 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5099 if (namereln != dns_namereln_subdomain) {
5100 char qbuf[DNS_NAME_FORMATSIZE];
5101 char obuf[DNS_NAME_FORMATSIZE];
5103 dns_rdata_freestruct(&dname);
5104 dns_name_format(qname, qbuf, sizeof(qbuf));
5105 dns_name_format(oname, obuf, sizeof(obuf));
5106 log_formerr(fctx, "unrelated DNAME in answer: "
5107 "%s is not in %s", qbuf, obuf);
5108 return (DNS_R_FORMERR);
5110 dns_fixedname_init(&prefix);
5111 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5112 dns_fixedname_init(fixeddname);
5113 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5115 dns_fixedname_name(fixeddname), NULL);
5116 dns_rdata_freestruct(&dname);
5120 static isc_boolean_t
5121 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5122 dns_rdataset_t *rdataset)
5124 isc_result_t result;
5125 dns_rdata_t rdata = DNS_RDATA_INIT;
5127 struct in6_addr in6a;
5128 isc_netaddr_t netaddr;
5129 char addrbuf[ISC_NETADDR_FORMATSIZE];
5130 char namebuf[DNS_NAME_FORMATSIZE];
5135 /* By default, we allow any addresses. */
5136 if (view->denyansweracl == NULL)
5140 * If the owner name matches one in the exclusion list, either exactly
5141 * or partially, allow it.
5143 if (view->answeracl_exclude != NULL) {
5144 dns_rbtnode_t *node = NULL;
5146 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5147 &node, NULL, 0, NULL, NULL);
5149 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5154 * Otherwise, search the filter list for a match for each address
5155 * record. If a match is found, the address should be filtered,
5156 * so should the entire answer.
5158 for (result = dns_rdataset_first(rdataset);
5159 result == ISC_R_SUCCESS;
5160 result = dns_rdataset_next(rdataset)) {
5161 dns_rdata_reset(&rdata);
5162 dns_rdataset_current(rdataset, &rdata);
5163 if (rdataset->type == dns_rdatatype_a) {
5164 INSIST(rdata.length == sizeof(ina.s_addr));
5165 memcpy(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5166 isc_netaddr_fromin(&netaddr, &ina);
5168 INSIST(rdata.length == sizeof(in6a.s6_addr));
5169 memcpy(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5170 isc_netaddr_fromin6(&netaddr, &in6a);
5173 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5174 &view->aclenv, &match, NULL);
5176 if (result == ISC_R_SUCCESS && match > 0) {
5177 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5178 dns_name_format(name, namebuf, sizeof(namebuf));
5179 dns_rdatatype_format(rdataset->type, typebuf,
5181 dns_rdataclass_format(rdataset->rdclass, classbuf,
5183 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5184 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5185 "answer address %s denied for %s/%s/%s",
5186 addrbuf, namebuf, typebuf, classbuf);
5194 static isc_boolean_t
5195 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5196 dns_rdatatype_t type, dns_name_t *tname,
5199 isc_result_t result;
5200 dns_rbtnode_t *node = NULL;
5201 char qnamebuf[DNS_NAME_FORMATSIZE];
5202 char tnamebuf[DNS_NAME_FORMATSIZE];
5206 /* By default, we allow any target name. */
5207 if (view->denyanswernames == NULL)
5211 * If the owner name matches one in the exclusion list, either exactly
5212 * or partially, allow it.
5214 if (view->answernames_exclude != NULL) {
5215 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5216 &node, NULL, 0, NULL, NULL);
5217 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5222 * If the target name is a subdomain of the search domain, allow it.
5224 if (dns_name_issubdomain(tname, domain))
5228 * Otherwise, apply filters.
5230 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5231 NULL, 0, NULL, NULL);
5232 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5233 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5234 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5235 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5236 dns_rdataclass_format(view->rdclass, classbuf,
5238 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5239 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5240 "%s target %s denied for %s/%s",
5241 typebuf, tnamebuf, qnamebuf, classbuf);
5249 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5250 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5251 * section for the NS RRset if the query type is NS; if it has
5252 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5253 * section for A and AAAA queries.
5255 #define LOOK_FOR_NS_IN_ANSWER 0x1
5256 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5259 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5260 unsigned int look_in_options)
5262 isc_result_t result;
5263 dns_message_t *message;
5264 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
5265 dns_rdataset_t *rdataset, *ns_rdataset;
5266 isc_boolean_t aa, negative_response;
5267 dns_rdatatype_t type;
5268 dns_section_t section;
5270 FCTXTRACE("noanswer_response");
5272 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5273 INSIST(fctx->type == dns_rdatatype_ns);
5274 section = DNS_SECTION_ANSWER;
5276 section = DNS_SECTION_AUTHORITY;
5278 message = fctx->rmessage;
5283 if (oqname == NULL) {
5285 * We have a normal, non-chained negative response or
5288 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5292 qname = &fctx->name;
5295 * We're being invoked by answer_response() after it has
5296 * followed a CNAME/DNAME chain.
5301 * If the current qname is not a subdomain of the query
5302 * domain, there's no point in looking at the authority
5303 * section without doing DNSSEC validation.
5305 * Until we do that validation, we'll just return success
5308 if (!dns_name_issubdomain(qname, &fctx->domain))
5309 return (ISC_R_SUCCESS);
5313 * We have to figure out if this is a negative response, or a
5318 * Sometimes we can tell if its a negative response by looking at
5319 * the message header.
5321 negative_response = ISC_FALSE;
5322 if (message->rcode == dns_rcode_nxdomain ||
5323 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5324 message->counts[DNS_SECTION_AUTHORITY] == 0))
5325 negative_response = ISC_TRUE;
5328 * Process the authority section.
5334 result = dns_message_firstname(message, section);
5335 while (result == ISC_R_SUCCESS) {
5337 dns_message_currentname(message, section, &name);
5338 if (dns_name_issubdomain(name, &fctx->domain)) {
5340 * Look for NS/SOA RRsets first.
5342 for (rdataset = ISC_LIST_HEAD(name->list);
5344 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5345 type = rdataset->type;
5346 if (type == dns_rdatatype_rrsig)
5347 type = rdataset->covers;
5348 if (((type == dns_rdatatype_ns ||
5349 type == dns_rdatatype_soa) &&
5350 !dns_name_issubdomain(qname, name))) {
5351 char qbuf[DNS_NAME_FORMATSIZE];
5352 char nbuf[DNS_NAME_FORMATSIZE];
5353 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5354 dns_rdatatype_format(fctx->type, tbuf,
5356 dns_name_format(name, nbuf,
5358 dns_name_format(qname, qbuf,
5361 "unrelated %s %s in "
5362 "%s authority section",
5364 return (DNS_R_FORMERR);
5366 if (type == dns_rdatatype_ns) {
5370 * Only one set of NS RRs is allowed.
5372 if (rdataset->type ==
5374 if (ns_name != NULL &&
5381 return (DNS_R_FORMERR);
5384 ns_rdataset = rdataset;
5388 rdataset->attributes |=
5389 DNS_RDATASETATTR_CACHE;
5390 rdataset->trust = dns_trust_glue;
5392 if (type == dns_rdatatype_soa) {
5394 * SOA, or RRSIG SOA.
5396 * Only one SOA is allowed.
5398 if (rdataset->type ==
5399 dns_rdatatype_soa) {
5400 if (soa_name != NULL &&
5407 return (DNS_R_FORMERR);
5412 DNS_NAMEATTR_NCACHE;
5413 rdataset->attributes |=
5414 DNS_RDATASETATTR_NCACHE;
5417 dns_trust_authauthority;
5420 dns_trust_additional;
5424 result = dns_message_nextname(message, section);
5425 if (result == ISC_R_NOMORE)
5427 else if (result != ISC_R_SUCCESS)
5432 * A negative response has a SOA record (Type 2)
5433 * and a optional NS RRset (Type 1) or it has neither
5434 * a SOA or a NS RRset (Type 3, handled above) or
5435 * rcode is NXDOMAIN (handled above) in which case
5436 * the NS RRset is allowed (Type 4).
5438 if (soa_name != NULL)
5439 negative_response = ISC_TRUE;
5441 result = dns_message_firstname(message, section);
5442 while (result == ISC_R_SUCCESS) {
5444 dns_message_currentname(message, section, &name);
5445 if (dns_name_issubdomain(name, &fctx->domain)) {
5446 for (rdataset = ISC_LIST_HEAD(name->list);
5448 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5449 type = rdataset->type;
5450 if (type == dns_rdatatype_rrsig)
5451 type = rdataset->covers;
5452 if (type == dns_rdatatype_nsec ||
5453 type == dns_rdatatype_nsec3) {
5455 * NSEC or RRSIG NSEC.
5457 if (negative_response) {
5459 DNS_NAMEATTR_NCACHE;
5460 rdataset->attributes |=
5461 DNS_RDATASETATTR_NCACHE;
5462 } else if (type == dns_rdatatype_nsec) {
5465 rdataset->attributes |=
5466 DNS_RDATASETATTR_CACHE;
5470 dns_trust_authauthority;
5473 dns_trust_additional;
5475 * No additional data needs to be
5478 } else if (type == dns_rdatatype_ds) {
5482 * These should only be here if
5483 * this is a referral, and there
5484 * should only be one DS RRset.
5486 if (ns_name == NULL) {
5490 return (DNS_R_FORMERR);
5492 if (rdataset->type ==
5494 if (ds_name != NULL &&
5501 return (DNS_R_FORMERR);
5507 rdataset->attributes |=
5508 DNS_RDATASETATTR_CACHE;
5511 dns_trust_authauthority;
5514 dns_trust_additional;
5518 result = dns_message_nextname(message, section);
5519 if (result == ISC_R_NOMORE)
5521 else if (result != ISC_R_SUCCESS)
5526 * Trigger lookups for DNS nameservers.
5528 if (negative_response && message->rcode == dns_rcode_noerror &&
5529 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5530 dns_name_equal(soa_name, qname) &&
5531 !dns_name_equal(qname, dns_rootname))
5532 return (DNS_R_CHASEDSSERVERS);
5535 * Did we find anything?
5537 if (!negative_response && ns_name == NULL) {
5541 if (oqname != NULL) {
5543 * We've already got a partial CNAME/DNAME chain,
5544 * and haven't found else anything useful here, but
5545 * no error has occurred since we have an answer.
5547 return (ISC_R_SUCCESS);
5550 * The responder is insane.
5552 log_formerr(fctx, "invalid response");
5553 return (DNS_R_FORMERR);
5558 * If we found both NS and SOA, they should be the same name.
5560 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5561 log_formerr(fctx, "NS/SOA mismatch");
5562 return (DNS_R_FORMERR);
5566 * Do we have a referral? (We only want to follow a referral if
5567 * we're not following a chain.)
5569 if (!negative_response && ns_name != NULL && oqname == NULL) {
5571 * We already know ns_name is a subdomain of fctx->domain.
5572 * If ns_name is equal to fctx->domain, we're not making
5573 * progress. We return DNS_R_FORMERR so that we'll keep
5574 * trying other servers.
5576 if (dns_name_equal(ns_name, &fctx->domain)) {
5577 log_formerr(fctx, "non-improving referral");
5578 return (DNS_R_FORMERR);
5582 * If the referral name is not a parent of the query
5583 * name, consider the responder insane.
5585 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5587 log_formerr(fctx, "referral to non-parent");
5588 FCTXTRACE("referral to non-parent");
5589 return (DNS_R_FORMERR);
5593 * Mark any additional data related to this rdataset.
5594 * It's important that we do this before we change the
5597 INSIST(ns_rdataset != NULL);
5598 fctx->attributes |= FCTX_ATTR_GLUING;
5599 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5601 #if CHECK_FOR_GLUE_IN_ANSWER
5603 * Look in the answer section for "glue" that is incorrectly
5604 * returned as a answer. This is needed if the server also
5605 * minimizes the response size by not adding records to the
5606 * additional section that are in the answer section or if
5607 * the record gets dropped due to message size constraints.
5609 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
5610 (fctx->type == dns_rdatatype_aaaa ||
5611 fctx->type == dns_rdatatype_a))
5612 (void)dns_rdataset_additionaldata(ns_rdataset,
5613 check_answer, fctx);
5615 fctx->attributes &= ~FCTX_ATTR_GLUING;
5617 * NS rdatasets with 0 TTL cause problems.
5618 * dns_view_findzonecut() will not find them when we
5619 * try to follow the referral, and we'll SERVFAIL
5620 * because the best nameservers are now above QDOMAIN.
5621 * We force the TTL to 1 second to prevent this.
5623 if (ns_rdataset->ttl == 0)
5624 ns_rdataset->ttl = 1;
5626 * Set the current query domain to the referral name.
5628 * XXXRTH We should check if we're in forward-only mode, and
5629 * if so we should bail out.
5631 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5632 dns_name_free(&fctx->domain,
5633 fctx->res->buckets[fctx->bucketnum].mctx);
5634 if (dns_rdataset_isassociated(&fctx->nameservers))
5635 dns_rdataset_disassociate(&fctx->nameservers);
5636 dns_name_init(&fctx->domain, NULL);
5637 result = dns_name_dup(ns_name,
5638 fctx->res->buckets[fctx->bucketnum].mctx,
5640 if (result != ISC_R_SUCCESS)
5642 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5643 return (DNS_R_DELEGATION);
5647 * Since we're not doing a referral, we don't want to cache any
5648 * NS RRs we may have found.
5650 if (ns_name != NULL)
5651 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5653 if (negative_response && oqname == NULL)
5654 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5656 return (ISC_R_SUCCESS);
5660 answer_response(fetchctx_t *fctx) {
5661 isc_result_t result;
5662 dns_message_t *message;
5663 dns_name_t *name, *qname, tname;
5664 dns_rdataset_t *rdataset;
5665 isc_boolean_t done, external, chaining, aa, found, want_chaining;
5666 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5668 dns_rdatatype_t type;
5669 dns_fixedname_t dname, fqname;
5672 FCTXTRACE("answer_response");
5674 message = fctx->rmessage;
5677 * Examine the answer section, marking those rdatasets which are
5678 * part of the answer and should be cached.
5682 found_cname = ISC_FALSE;
5683 found_type = ISC_FALSE;
5684 chaining = ISC_FALSE;
5685 have_answer = ISC_FALSE;
5686 want_chaining = ISC_FALSE;
5687 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5691 qname = &fctx->name;
5693 view = fctx->res->view;
5694 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5695 while (!done && result == ISC_R_SUCCESS) {
5697 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5698 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5699 if (dns_name_equal(name, qname)) {
5700 wanted_chaining = ISC_FALSE;
5701 for (rdataset = ISC_LIST_HEAD(name->list);
5703 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5705 want_chaining = ISC_FALSE;
5707 if (rdataset->type == dns_rdatatype_nsec3) {
5709 * NSEC3 records are not allowed to
5710 * appear in the answer section.
5712 log_formerr(fctx, "NSEC3 in answer");
5713 return (DNS_R_FORMERR);
5717 * Apply filters, if given, on answers to reject
5718 * a malicious attempt of rebinding.
5720 if ((rdataset->type == dns_rdatatype_a ||
5721 rdataset->type == dns_rdatatype_aaaa) &&
5722 !is_answeraddress_allowed(view, name,
5724 return (DNS_R_SERVFAIL);
5727 if (rdataset->type == type && !found_cname) {
5729 * We've found an ordinary answer.
5732 found_type = ISC_TRUE;
5734 aflag = DNS_RDATASETATTR_ANSWER;
5735 } else if (type == dns_rdatatype_any) {
5737 * We've found an answer matching
5738 * an ANY query. There may be
5742 aflag = DNS_RDATASETATTR_ANSWER;
5743 } else if (rdataset->type == dns_rdatatype_rrsig
5744 && rdataset->covers == type
5747 * We've found a signature that
5748 * covers the type we're looking for.
5751 found_type = ISC_TRUE;
5752 aflag = DNS_RDATASETATTR_ANSWERSIG;
5753 } else if (rdataset->type ==
5757 * We're looking for something else,
5758 * but we found a CNAME.
5760 * Getting a CNAME response for some
5761 * query types is an error.
5763 if (type == dns_rdatatype_rrsig ||
5764 type == dns_rdatatype_dnskey ||
5765 type == dns_rdatatype_nsec ||
5766 type == dns_rdatatype_nsec3) {
5767 char buf[DNS_RDATATYPE_FORMATSIZE];
5768 dns_rdatatype_format(fctx->type,
5773 return (DNS_R_FORMERR);
5776 found_cname = ISC_TRUE;
5777 want_chaining = ISC_TRUE;
5778 aflag = DNS_RDATASETATTR_ANSWER;
5779 result = cname_target(rdataset,
5781 if (result != ISC_R_SUCCESS)
5783 /* Apply filters on the target name. */
5784 if (!is_answertarget_allowed(view,
5789 return (DNS_R_SERVFAIL);
5791 } else if (rdataset->type == dns_rdatatype_rrsig
5792 && rdataset->covers ==
5796 * We're looking for something else,
5797 * but we found a SIG CNAME.
5800 found_cname = ISC_TRUE;
5801 aflag = DNS_RDATASETATTR_ANSWERSIG;
5806 * We've found an answer to our
5811 rdataset->attributes |=
5812 DNS_RDATASETATTR_CACHE;
5813 rdataset->trust = dns_trust_answer;
5816 * This data is "the" answer
5817 * to our question only if
5818 * we're not chaining (i.e.
5819 * if we haven't followed
5820 * a CNAME or DNAME).
5824 DNS_RDATASETATTR_ANSWER)
5825 have_answer = ISC_TRUE;
5827 DNS_NAMEATTR_ANSWER;
5828 rdataset->attributes |= aflag;
5831 dns_trust_authanswer;
5832 } else if (external) {
5834 * This data is outside of
5835 * our query domain, and
5836 * may not be cached.
5838 rdataset->attributes |=
5839 DNS_RDATASETATTR_EXTERNAL;
5843 * Mark any additional data related
5846 (void)dns_rdataset_additionaldata(
5854 if (want_chaining) {
5855 wanted_chaining = ISC_TRUE;
5857 DNS_NAMEATTR_CHAINING;
5858 rdataset->attributes |=
5859 DNS_RDATASETATTR_CHAINING;
5864 * We could add an "else" clause here and
5865 * log that we're ignoring this rdataset.
5869 * If wanted_chaining is true, we've done
5870 * some chaining as the result of processing
5871 * this node, and thus we need to set
5874 * We don't set chaining inside of the
5875 * rdataset loop because doing that would
5876 * cause us to ignore the signatures of
5879 if (wanted_chaining)
5880 chaining = ISC_TRUE;
5883 * Look for a DNAME (or its SIG). Anything else is
5886 wanted_chaining = ISC_FALSE;
5887 for (rdataset = ISC_LIST_HEAD(name->list);
5889 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5890 isc_boolean_t found_dname = ISC_FALSE;
5891 dns_name_t *dname_name;
5895 if (rdataset->type == dns_rdatatype_dname) {
5897 * We're looking for something else,
5898 * but we found a DNAME.
5900 * If we're not chaining, then the
5901 * DNAME should not be external.
5903 if (!chaining && external) {
5906 return (DNS_R_FORMERR);
5909 want_chaining = ISC_TRUE;
5910 aflag = DNS_RDATASETATTR_ANSWER;
5911 result = dname_target(fctx, rdataset,
5914 if (result == ISC_R_NOSPACE) {
5916 * We can't construct the
5917 * DNAME target. Do not
5920 want_chaining = ISC_FALSE;
5921 } else if (result != ISC_R_SUCCESS)
5924 found_dname = ISC_TRUE;
5926 dname_name = dns_fixedname_name(&dname);
5927 if (!is_answertarget_allowed(view,
5932 return (DNS_R_SERVFAIL);
5934 } else if (rdataset->type == dns_rdatatype_rrsig
5935 && rdataset->covers ==
5936 dns_rdatatype_dname) {
5938 * We've found a signature that
5942 aflag = DNS_RDATASETATTR_ANSWERSIG;
5947 * We've found an answer to our
5952 rdataset->attributes |=
5953 DNS_RDATASETATTR_CACHE;
5954 rdataset->trust = dns_trust_answer;
5957 * This data is "the" answer
5958 * to our question only if
5959 * we're not chaining.
5963 DNS_RDATASETATTR_ANSWER)
5964 have_answer = ISC_TRUE;
5966 DNS_NAMEATTR_ANSWER;
5967 rdataset->attributes |= aflag;
5970 dns_trust_authanswer;
5971 } else if (external) {
5972 rdataset->attributes |=
5973 DNS_RDATASETATTR_EXTERNAL;
5981 * Copy the dname into the
5984 * Although we check for
5985 * failure of the copy
5986 * operation, in practice it
5987 * should never fail since
5988 * we already know that the
5989 * result fits in a fixedname.
5991 dns_fixedname_init(&fqname);
5992 result = dns_name_copy(
5993 dns_fixedname_name(&dname),
5994 dns_fixedname_name(&fqname),
5996 if (result != ISC_R_SUCCESS)
5998 wanted_chaining = ISC_TRUE;
6000 DNS_NAMEATTR_CHAINING;
6001 rdataset->attributes |=
6002 DNS_RDATASETATTR_CHAINING;
6003 qname = dns_fixedname_name(
6008 if (wanted_chaining)
6009 chaining = ISC_TRUE;
6011 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6013 if (result == ISC_R_NOMORE)
6014 result = ISC_R_SUCCESS;
6015 if (result != ISC_R_SUCCESS)
6019 * We should have found an answer.
6022 log_formerr(fctx, "reply has no answer");
6023 return (DNS_R_FORMERR);
6027 * This response is now potentially cacheable.
6029 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6032 * Did chaining end before we got the final answer?
6036 * Yes. This may be a negative reply, so hand off
6037 * authority section processing to the noanswer code.
6038 * If it isn't a noanswer response, no harm will be
6041 return (noanswer_response(fctx, qname, 0));
6045 * We didn't end with an incomplete chain, so the rcode should be
6048 if (message->rcode != dns_rcode_noerror) {
6049 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6051 return (DNS_R_FORMERR);
6055 * Examine the authority section (if there is one).
6057 * We expect there to be only one owner name for all the rdatasets
6058 * in this section, and we expect that it is not external.
6061 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6062 while (!done && result == ISC_R_SUCCESS) {
6064 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6065 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6068 * We expect to find NS or SIG NS rdatasets, and
6071 for (rdataset = ISC_LIST_HEAD(name->list);
6073 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6074 if (rdataset->type == dns_rdatatype_ns ||
6075 (rdataset->type == dns_rdatatype_rrsig &&
6076 rdataset->covers == dns_rdatatype_ns)) {
6079 rdataset->attributes |=
6080 DNS_RDATASETATTR_CACHE;
6081 if (aa && !chaining)
6083 dns_trust_authauthority;
6086 dns_trust_additional;
6089 * Mark any additional data related
6092 (void)dns_rdataset_additionaldata(
6100 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6102 if (result == ISC_R_NOMORE)
6103 result = ISC_R_SUCCESS;
6108 static isc_boolean_t
6109 fctx_decreference(fetchctx_t *fctx) {
6110 isc_boolean_t bucket_empty = ISC_FALSE;
6112 INSIST(fctx->references > 0);
6114 if (fctx->references == 0) {
6116 * No one cares about the result of this fetch anymore.
6118 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6119 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6121 * This fctx is already shutdown; we were just
6122 * waiting for the last reference to go away.
6124 bucket_empty = fctx_destroy(fctx);
6127 * Initiate shutdown.
6129 fctx_shutdown(fctx);
6132 return (bucket_empty);
6136 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6137 dns_fetchevent_t *fevent;
6138 dns_resolver_t *res;
6140 isc_result_t result;
6141 isc_boolean_t bucket_empty;
6142 isc_boolean_t locked = ISC_FALSE;
6143 unsigned int bucketnum;
6144 dns_rdataset_t nameservers;
6145 dns_fixedname_t fixed;
6148 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6149 fevent = (dns_fetchevent_t *)event;
6150 fctx = event->ev_arg;
6151 REQUIRE(VALID_FCTX(fctx));
6155 FCTXTRACE("resume_dslookup");
6157 if (fevent->node != NULL)
6158 dns_db_detachnode(fevent->db, &fevent->node);
6159 if (fevent->db != NULL)
6160 dns_db_detach(&fevent->db);
6162 dns_rdataset_init(&nameservers);
6164 bucketnum = fctx->bucketnum;
6165 if (fevent->result == ISC_R_CANCELED) {
6166 dns_resolver_destroyfetch(&fctx->nsfetch);
6167 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6168 } else if (fevent->result == ISC_R_SUCCESS) {
6170 FCTXTRACE("resuming DS lookup");
6172 dns_resolver_destroyfetch(&fctx->nsfetch);
6173 if (dns_rdataset_isassociated(&fctx->nameservers))
6174 dns_rdataset_disassociate(&fctx->nameservers);
6175 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6176 dns_name_free(&fctx->domain,
6177 fctx->res->buckets[bucketnum].mctx);
6178 dns_name_init(&fctx->domain, NULL);
6179 result = dns_name_dup(&fctx->nsname,
6180 fctx->res->buckets[bucketnum].mctx,
6182 if (result != ISC_R_SUCCESS) {
6183 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6189 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6192 dns_rdataset_t *nsrdataset = NULL;
6195 * Retrieve state from fctx->nsfetch before we destroy it.
6197 dns_fixedname_init(&fixed);
6198 domain = dns_fixedname_name(&fixed);
6199 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6200 if (dns_name_equal(&fctx->nsname, domain)) {
6201 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6202 dns_resolver_destroyfetch(&fctx->nsfetch);
6205 if (dns_rdataset_isassociated(
6206 &fctx->nsfetch->private->nameservers)) {
6208 &fctx->nsfetch->private->nameservers,
6210 nsrdataset = &nameservers;
6213 dns_resolver_destroyfetch(&fctx->nsfetch);
6214 n = dns_name_countlabels(&fctx->nsname);
6215 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6218 if (dns_rdataset_isassociated(fevent->rdataset))
6219 dns_rdataset_disassociate(fevent->rdataset);
6220 FCTXTRACE("continuing to look for parent's NS records");
6221 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6222 dns_rdatatype_ns, domain,
6223 nsrdataset, NULL, 0, task,
6224 resume_dslookup, fctx,
6225 &fctx->nsrrset, NULL,
6227 if (result != ISC_R_SUCCESS)
6228 fctx_done(fctx, result, __LINE__);
6230 LOCK(&res->buckets[bucketnum].lock);
6237 if (dns_rdataset_isassociated(&nameservers))
6238 dns_rdataset_disassociate(&nameservers);
6239 if (dns_rdataset_isassociated(fevent->rdataset))
6240 dns_rdataset_disassociate(fevent->rdataset);
6241 INSIST(fevent->sigrdataset == NULL);
6242 isc_event_free(&event);
6244 LOCK(&res->buckets[bucketnum].lock);
6245 bucket_empty = fctx_decreference(fctx);
6246 UNLOCK(&res->buckets[bucketnum].lock);
6252 checknamessection(dns_message_t *message, dns_section_t section) {
6253 isc_result_t result;
6255 dns_rdata_t rdata = DNS_RDATA_INIT;
6256 dns_rdataset_t *rdataset;
6258 for (result = dns_message_firstname(message, section);
6259 result == ISC_R_SUCCESS;
6260 result = dns_message_nextname(message, section))
6263 dns_message_currentname(message, section, &name);
6264 for (rdataset = ISC_LIST_HEAD(name->list);
6266 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6267 for (result = dns_rdataset_first(rdataset);
6268 result == ISC_R_SUCCESS;
6269 result = dns_rdataset_next(rdataset)) {
6270 dns_rdataset_current(rdataset, &rdata);
6271 if (!dns_rdata_checkowner(name, rdata.rdclass,
6274 !dns_rdata_checknames(&rdata, name, NULL))
6276 rdataset->attributes |=
6277 DNS_RDATASETATTR_CHECKNAMES;
6279 dns_rdata_reset(&rdata);
6286 checknames(dns_message_t *message) {
6288 checknamessection(message, DNS_SECTION_ANSWER);
6289 checknamessection(message, DNS_SECTION_AUTHORITY);
6290 checknamessection(message, DNS_SECTION_ADDITIONAL);
6294 * Log server NSID at log level 'level'
6297 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
6299 static const char hex[17] = "0123456789abcdef";
6300 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6301 isc_uint16_t optcode, nsid_len, buflen, i;
6302 isc_result_t result;
6303 isc_buffer_t nsidbuf;
6305 unsigned char *p, *buf, *nsid;
6307 /* Extract rdata from OPT rdataset */
6308 result = dns_rdataset_first(opt);
6309 if (result != ISC_R_SUCCESS)
6310 return (ISC_R_FAILURE);
6312 dns_rdata_init(&rdata);
6313 dns_rdataset_current(opt, &rdata);
6314 if (rdata.length < 4)
6315 return (ISC_R_FAILURE);
6317 /* Check for NSID */
6318 isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
6319 isc_buffer_add(&nsidbuf, rdata.length);
6320 optcode = isc_buffer_getuint16(&nsidbuf);
6321 nsid_len = isc_buffer_getuint16(&nsidbuf);
6322 if (optcode != DNS_OPT_NSID || nsid_len == 0)
6323 return (ISC_R_FAILURE);
6325 /* Allocate buffer for storing hex version of the NSID */
6326 buflen = nsid_len * 2 + 1;
6327 buf = isc_mem_get(mctx, buflen);
6329 return (ISC_R_NOSPACE);
6331 /* Convert to hex */
6333 nsid = rdata.data + 4;
6334 for (i = 0; i < nsid_len; i++) {
6335 *p++ = hex[(nsid[0] >> 4) & 0xf];
6336 *p++ = hex[nsid[0] & 0xf];
6341 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6343 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6344 DNS_LOGMODULE_RESOLVER, level,
6345 "received NSID '%s' from %s", buf, addrbuf);
6348 isc_mem_put(mctx, buf, buflen);
6349 return (ISC_R_SUCCESS);
6353 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6354 isc_buffer_t buffer;
6357 isc_result_t result;
6359 if (! isc_log_wouldlog(dns_lctx, level))
6363 * Note that these are multiline debug messages. We want a newline
6364 * to appear in the log after each message.
6368 buf = isc_mem_get(mctx, len);
6371 isc_buffer_init(&buffer, buf, len);
6372 result = dns_message_totext(message, &dns_master_style_debug,
6374 if (result == ISC_R_NOSPACE) {
6375 isc_mem_put(mctx, buf, len);
6377 } else if (result == ISC_R_SUCCESS)
6378 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6379 DNS_LOGMODULE_RESOLVER, level,
6380 "received packet:\n%.*s",
6381 (int)isc_buffer_usedlength(&buffer),
6383 } while (result == ISC_R_NOSPACE);
6386 isc_mem_put(mctx, buf, len);
6389 static isc_boolean_t
6390 iscname(fetchctx_t *fctx) {
6391 isc_result_t result;
6393 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6394 &fctx->name, dns_rdatatype_cname, 0,
6396 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6399 static isc_boolean_t
6400 betterreferral(fetchctx_t *fctx) {
6401 isc_result_t result;
6403 dns_rdataset_t *rdataset;
6404 dns_message_t *message = fctx->rmessage;
6406 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6407 result == ISC_R_SUCCESS;
6408 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6410 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6411 if (!isstrictsubdomain(name, &fctx->domain))
6413 for (rdataset = ISC_LIST_HEAD(name->list);
6415 rdataset = ISC_LIST_NEXT(rdataset, link))
6416 if (rdataset->type == dns_rdatatype_ns)
6423 resquery_response(isc_task_t *task, isc_event_t *event) {
6424 isc_result_t result = ISC_R_SUCCESS;
6425 resquery_t *query = event->ev_arg;
6426 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6427 isc_boolean_t keep_trying, get_nameservers, resend;
6428 isc_boolean_t truncated;
6429 dns_message_t *message;
6430 dns_rdataset_t *opt;
6433 dns_fixedname_t foundname;
6435 isc_time_t tnow, *finish;
6436 dns_adbaddrinfo_t *addrinfo;
6437 unsigned int options;
6438 unsigned int findoptions;
6439 isc_result_t broken_server;
6440 badnstype_t broken_type = badns_response;
6441 isc_boolean_t no_response;
6443 REQUIRE(VALID_QUERY(query));
6445 options = query->options;
6446 REQUIRE(VALID_FCTX(fctx));
6447 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6451 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6452 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6454 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6456 (void)isc_timer_touch(fctx->timer);
6458 keep_trying = ISC_FALSE;
6459 broken_server = ISC_R_SUCCESS;
6460 get_nameservers = ISC_FALSE;
6462 truncated = ISC_FALSE;
6464 no_response = ISC_FALSE;
6466 if (fctx->res->exiting) {
6467 result = ISC_R_SHUTTINGDOWN;
6472 fctx->timeout = ISC_FALSE;
6473 fctx->addrinfo = query->addrinfo;
6476 * XXXRTH We should really get the current time just once. We
6477 * need a routine to convert from an isc_time_t to an
6482 isc_stdtime_get(&now);
6485 * Did the dispatcher have a problem?
6487 if (devent->result != ISC_R_SUCCESS) {
6488 if (devent->result == ISC_R_EOF &&
6489 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6491 * The problem might be that they
6492 * don't understand EDNS0. Turn it
6493 * off and try again.
6495 options |= DNS_FETCHOPT_NOEDNS0;
6498 * Remember that they don't like EDNS0.
6500 dns_adb_changeflags(fctx->adb,
6502 DNS_FETCHOPT_NOEDNS0,
6503 DNS_FETCHOPT_NOEDNS0);
6506 * There's no hope for this query.
6508 keep_trying = ISC_TRUE;
6511 * If this is a network error on an exclusive query
6512 * socket, mark the server as bad so that we won't try
6513 * it for this fetch again. Also adjust finish and
6514 * no_response so that we penalize this address in SRTT
6517 if (query->exclusivesocket &&
6518 (devent->result == ISC_R_HOSTUNREACH ||
6519 devent->result == ISC_R_NETUNREACH ||
6520 devent->result == ISC_R_CONNREFUSED ||
6521 devent->result == ISC_R_CANCELED)) {
6522 broken_server = devent->result;
6523 broken_type = badns_unreachable;
6525 no_response = ISC_TRUE;
6531 message = fctx->rmessage;
6533 if (query->tsig != NULL) {
6534 result = dns_message_setquerytsig(message, query->tsig);
6535 if (result != ISC_R_SUCCESS)
6539 if (query->tsigkey) {
6540 result = dns_message_settsigkey(message, query->tsigkey);
6541 if (result != ISC_R_SUCCESS)
6545 result = dns_message_parse(message, &devent->buffer, 0);
6546 if (result != ISC_R_SUCCESS) {
6548 case ISC_R_UNEXPECTEDEND:
6549 if (!message->question_ok ||
6550 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6551 (options & DNS_FETCHOPT_TCP) != 0) {
6553 * Either the message ended prematurely,
6554 * and/or wasn't marked as being truncated,
6555 * and/or this is a response to a query we
6556 * sent over TCP. In all of these cases,
6557 * something is wrong with the remote
6558 * server and we don't want to retry using
6561 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6564 * The problem might be that they
6565 * don't understand EDNS0. Turn it
6566 * off and try again.
6568 options |= DNS_FETCHOPT_NOEDNS0;
6571 * Remember that they don't like EDNS0.
6573 dns_adb_changeflags(
6576 DNS_FETCHOPT_NOEDNS0,
6577 DNS_FETCHOPT_NOEDNS0);
6578 inc_stats(fctx->res,
6579 dns_resstatscounter_edns0fail);
6581 broken_server = result;
6582 keep_trying = ISC_TRUE;
6587 * We defer retrying via TCP for a bit so we can
6588 * check out this message further.
6590 truncated = ISC_TRUE;
6593 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6595 * The problem might be that they
6596 * don't understand EDNS0. Turn it
6597 * off and try again.
6599 options |= DNS_FETCHOPT_NOEDNS0;
6602 * Remember that they don't like EDNS0.
6604 dns_adb_changeflags(fctx->adb,
6606 DNS_FETCHOPT_NOEDNS0,
6607 DNS_FETCHOPT_NOEDNS0);
6608 inc_stats(fctx->res,
6609 dns_resstatscounter_edns0fail);
6611 broken_server = DNS_R_UNEXPECTEDRCODE;
6612 keep_trying = ISC_TRUE;
6617 * Something bad has happened.
6625 * Log the incoming packet.
6627 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6630 * Did we request NSID? If so, and if the response contains
6631 * NSID data, log it at INFO level.
6633 opt = dns_message_getopt(message);
6634 if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6635 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6638 * If the message is signed, check the signature. If not, this
6639 * returns success anyway.
6641 result = dns_message_checksig(message, fctx->res->view);
6642 if (result != ISC_R_SUCCESS)
6646 * The dispatcher should ensure we only get responses with QR set.
6648 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6650 * INSIST() that the message comes from the place we sent it to,
6651 * since the dispatch code should ensure this.
6653 * INSIST() that the message id is correct (this should also be
6654 * ensured by the dispatch code).
6658 * We have an affirmative response to the query and we have
6659 * previously got a response from this server which indicated
6660 * EDNS may not be supported so we can now cache the lack of
6664 (message->rcode == dns_rcode_noerror ||
6665 message->rcode == dns_rcode_nxdomain ||
6666 message->rcode == dns_rcode_refused ||
6667 message->rcode == dns_rcode_yxdomain) &&
6668 bad_edns(fctx, &query->addrinfo->sockaddr)) {
6669 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6670 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6672 dns_adb_changeflags(fctx->adb, query->addrinfo,
6673 DNS_FETCHOPT_NOEDNS0,
6674 DNS_FETCHOPT_NOEDNS0);
6678 * Deal with truncated responses by retrying using TCP.
6680 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6681 truncated = ISC_TRUE;
6684 inc_stats(fctx->res, dns_resstatscounter_truncated);
6685 if ((options & DNS_FETCHOPT_TCP) != 0) {
6686 broken_server = DNS_R_TRUNCATEDTCP;
6687 keep_trying = ISC_TRUE;
6689 options |= DNS_FETCHOPT_TCP;
6696 * Is it a query response?
6698 if (message->opcode != dns_opcode_query) {
6700 broken_server = DNS_R_UNEXPECTEDOPCODE;
6701 keep_trying = ISC_TRUE;
6706 * Update statistics about erroneous responses.
6708 if (message->rcode != dns_rcode_noerror) {
6709 switch (message->rcode) {
6710 case dns_rcode_nxdomain:
6711 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6713 case dns_rcode_servfail:
6714 inc_stats(fctx->res, dns_resstatscounter_servfail);
6716 case dns_rcode_formerr:
6717 inc_stats(fctx->res, dns_resstatscounter_formerr);
6720 inc_stats(fctx->res, dns_resstatscounter_othererror);
6726 * Is the remote server broken, or does it dislike us?
6728 if (message->rcode != dns_rcode_noerror &&
6729 message->rcode != dns_rcode_nxdomain) {
6730 if (((message->rcode == dns_rcode_formerr ||
6731 message->rcode == dns_rcode_notimp) ||
6732 (message->rcode == dns_rcode_servfail &&
6733 dns_message_getopt(message) == NULL)) &&
6734 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6736 * It's very likely they don't like EDNS0.
6737 * If the response code is SERVFAIL, also check if the
6738 * response contains an OPT RR and don't cache the
6739 * failure since it can be returned for various other
6742 * XXXRTH We should check if the question
6743 * we're asking requires EDNS0, and
6744 * if so, we should bail out.
6746 options |= DNS_FETCHOPT_NOEDNS0;
6749 * Remember that they may not like EDNS0.
6751 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6752 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6753 } else if (message->rcode == dns_rcode_formerr) {
6754 if (ISFORWARDER(query->addrinfo)) {
6756 * This forwarder doesn't understand us,
6757 * but other forwarders might. Keep trying.
6759 broken_server = DNS_R_REMOTEFORMERR;
6760 keep_trying = ISC_TRUE;
6763 * The server doesn't understand us. Since
6764 * all servers for a zone need similar
6765 * capabilities, we assume that we will get
6766 * FORMERR from all servers, and thus we
6767 * cannot make any more progress with this
6770 log_formerr(fctx, "server sent FORMERR");
6771 result = DNS_R_FORMERR;
6773 } else if (message->rcode == dns_rcode_yxdomain) {
6775 * DNAME mapping failed because the new name
6776 * was too long. There's no chance of success
6779 result = DNS_R_YXDOMAIN;
6780 } else if (message->rcode == dns_rcode_badvers) {
6781 unsigned int flags, mask;
6782 unsigned int version;
6785 version = (opt->ttl >> 16) & 0xff;
6786 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6787 DNS_FETCHOPT_EDNSVERSIONSET;
6788 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6789 DNS_FETCHOPT_EDNSVERSIONSET;
6792 dns_adb_changeflags(fctx->adb, query->addrinfo,
6796 broken_server = DNS_R_BADVERS;
6797 keep_trying = ISC_TRUE;
6804 broken_server = DNS_R_UNEXPECTEDRCODE;
6805 INSIST(broken_server != ISC_R_SUCCESS);
6806 keep_trying = ISC_TRUE;
6812 * Is the question the same as the one we asked?
6814 result = same_question(fctx);
6815 if (result != ISC_R_SUCCESS) {
6817 if (result == DNS_R_FORMERR)
6818 keep_trying = ISC_TRUE;
6823 * Is the server lame?
6825 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6827 inc_stats(fctx->res, dns_resstatscounter_lame);
6828 log_lame(fctx, query->addrinfo);
6829 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6830 &fctx->name, fctx->type,
6831 now + fctx->res->lame_ttl);
6832 if (result != ISC_R_SUCCESS)
6833 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6834 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6835 "could not mark server as lame: %s",
6836 isc_result_totext(result));
6837 broken_server = DNS_R_LAME;
6838 keep_trying = ISC_TRUE;
6843 * Enforce delegations only zones like NET and COM.
6845 if (!ISFORWARDER(query->addrinfo) &&
6846 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6847 !dns_name_equal(&fctx->domain, &fctx->name) &&
6848 fix_mustbedelegationornxdomain(message, fctx)) {
6849 char namebuf[DNS_NAME_FORMATSIZE];
6850 char domainbuf[DNS_NAME_FORMATSIZE];
6851 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6855 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6856 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6857 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6858 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6860 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6863 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6864 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6865 "enforced delegation-only for '%s' (%s/%s/%s) "
6867 domainbuf, namebuf, typebuf, classbuf, addrbuf);
6870 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6871 checknames(message);
6876 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6879 * Did we get any answers?
6881 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6882 (message->rcode == dns_rcode_noerror ||
6883 message->rcode == dns_rcode_nxdomain)) {
6886 * We've got answers. If it has an authoritative answer or an
6887 * answer from a forwarder, we're done.
6889 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
6890 ISFORWARDER(query->addrinfo))
6891 result = answer_response(fctx);
6892 else if (iscname(fctx) &&
6893 fctx->type != dns_rdatatype_any &&
6894 fctx->type != dns_rdatatype_cname) {
6896 * A BIND8 server could return a non-authoritative
6897 * answer when a CNAME is followed. We should treat
6898 * it as a valid answer.
6900 result = answer_response(fctx);
6901 } else if (fctx->type != dns_rdatatype_ns &&
6902 !betterreferral(fctx)) {
6904 * Lame response !!!.
6906 result = answer_response(fctx);
6908 if (fctx->type == dns_rdatatype_ns) {
6910 * A BIND 8 server could incorrectly return a
6911 * non-authoritative answer to an NS query
6912 * instead of a referral. Since this answer
6913 * lacks the SIGs necessary to do DNSSEC
6914 * validation, we must invoke the following
6915 * special kludge to treat it as a referral.
6917 result = noanswer_response(fctx, NULL,
6918 LOOK_FOR_NS_IN_ANSWER);
6921 * Some other servers may still somehow include
6922 * an answer when it should return a referral
6923 * with an empty answer. Check to see if we can
6924 * treat this as a referral by ignoring the
6925 * answer. Further more, there may be an
6926 * implementation that moves A/AAAA glue records
6927 * to the answer section for that type of
6928 * delegation when the query is for that glue
6929 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
6930 * such a corner case.
6932 result = noanswer_response(fctx, NULL,
6933 LOOK_FOR_GLUE_IN_ANSWER);
6935 if (result != DNS_R_DELEGATION) {
6937 * At this point, AA is not set, the response
6938 * is not a referral, and the server is not a
6939 * forwarder. It is technically lame and it's
6940 * easier to treat it as such than to figure out
6941 * some more elaborate course of action.
6943 broken_server = DNS_R_LAME;
6944 keep_trying = ISC_TRUE;
6947 goto force_referral;
6949 if (result != ISC_R_SUCCESS) {
6950 if (result == DNS_R_FORMERR)
6951 keep_trying = ISC_TRUE;
6954 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
6955 message->rcode == dns_rcode_noerror ||
6956 message->rcode == dns_rcode_nxdomain) {
6958 * NXDOMAIN, NXRDATASET, or referral.
6960 result = noanswer_response(fctx, NULL, 0);
6961 if (result == DNS_R_CHASEDSSERVERS) {
6962 } else if (result == DNS_R_DELEGATION) {
6965 * We don't have the answer, but we know a better
6968 get_nameservers = ISC_TRUE;
6969 keep_trying = ISC_TRUE;
6971 * We have a new set of name servers, and it
6972 * has not experienced any restarts yet.
6977 * Update local statistics counters collected for each
6981 fctx->querysent = 0;
6982 fctx->lamecount = 0;
6987 result = ISC_R_SUCCESS;
6988 } else if (result != ISC_R_SUCCESS) {
6990 * Something has gone wrong.
6992 if (result == DNS_R_FORMERR)
6993 keep_trying = ISC_TRUE;
6998 * The server is insane.
7001 broken_server = DNS_R_UNEXPECTEDRCODE;
7002 keep_trying = ISC_TRUE;
7007 * Follow additional section data chains.
7009 chase_additional(fctx);
7012 * Cache the cacheable parts of the message. This may also cause
7013 * work to be queued to the DNSSEC validator.
7015 if (WANTCACHE(fctx)) {
7016 result = cache_message(fctx, query->addrinfo, now);
7017 if (result != ISC_R_SUCCESS)
7022 * Ncache the negatively cacheable parts of the message. This may
7023 * also cause work to be queued to the DNSSEC validator.
7025 if (WANTNCACHE(fctx)) {
7026 dns_rdatatype_t covers;
7027 if (message->rcode == dns_rcode_nxdomain)
7028 covers = dns_rdatatype_any;
7030 covers = fctx->type;
7033 * Cache any negative cache entries in the message.
7035 result = ncache_message(fctx, query->addrinfo, covers, now);
7040 * Remember the query's addrinfo, in case we need to mark the
7043 addrinfo = query->addrinfo;
7048 * XXXRTH Don't cancel the query if waiting for validation?
7050 fctx_cancelquery(&query, &devent, finish, no_response);
7053 if (result == DNS_R_FORMERR)
7054 broken_server = DNS_R_FORMERR;
7055 if (broken_server != ISC_R_SUCCESS) {
7057 * Add this server to the list of bad servers for
7060 add_bad(fctx, addrinfo, broken_server, broken_type);
7063 if (get_nameservers) {
7065 dns_fixedname_init(&foundname);
7066 fname = dns_fixedname_name(&foundname);
7067 if (result != ISC_R_SUCCESS) {
7068 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7072 if (dns_rdatatype_atparent(fctx->type))
7073 findoptions |= DNS_DBFIND_NOEXACT;
7074 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7077 name = &fctx->domain;
7078 result = dns_view_findzonecut(fctx->res->view,
7084 if (result != ISC_R_SUCCESS) {
7085 FCTXTRACE("couldn't find a zonecut");
7086 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7089 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7091 * The best nameservers are now above our
7094 FCTXTRACE("nameservers now above QDOMAIN");
7095 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7098 dns_name_free(&fctx->domain,
7099 fctx->res->buckets[fctx->bucketnum].mctx);
7100 dns_name_init(&fctx->domain, NULL);
7101 result = dns_name_dup(fname,
7102 fctx->res->buckets[fctx->bucketnum].mctx,
7104 if (result != ISC_R_SUCCESS) {
7105 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7108 fctx_cancelqueries(fctx, ISC_TRUE);
7109 fctx_cleanupfinds(fctx);
7110 fctx_cleanupaltfinds(fctx);
7111 fctx_cleanupforwaddrs(fctx);
7112 fctx_cleanupaltaddrs(fctx);
7117 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7118 } else if (resend) {
7120 * Resend (probably with changed options).
7122 FCTXTRACE("resend");
7123 inc_stats(fctx->res, dns_resstatscounter_retry);
7124 result = fctx_query(fctx, addrinfo, options);
7125 if (result != ISC_R_SUCCESS)
7126 fctx_done(fctx, result, __LINE__);
7127 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7129 * All has gone well so far, but we are waiting for the
7130 * DNSSEC validator to validate the answer.
7132 FCTXTRACE("wait for validator");
7133 fctx_cancelqueries(fctx, ISC_TRUE);
7135 * We must not retransmit while the validator is working;
7136 * it has references to the current rmessage.
7138 result = fctx_stopidletimer(fctx);
7139 if (result != ISC_R_SUCCESS)
7140 fctx_done(fctx, result, __LINE__);
7141 } else if (result == DNS_R_CHASEDSSERVERS) {
7143 add_bad(fctx, addrinfo, result, broken_type);
7144 fctx_cancelqueries(fctx, ISC_TRUE);
7145 fctx_cleanupfinds(fctx);
7146 fctx_cleanupforwaddrs(fctx);
7148 n = dns_name_countlabels(&fctx->name);
7149 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7151 FCTXTRACE("suspending DS lookup to find parent's NS records");
7153 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7155 NULL, NULL, NULL, 0, task,
7156 resume_dslookup, fctx,
7157 &fctx->nsrrset, NULL,
7159 if (result != ISC_R_SUCCESS)
7160 fctx_done(fctx, result, __LINE__);
7162 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7164 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7165 result = fctx_stopidletimer(fctx);
7166 if (result != ISC_R_SUCCESS)
7167 fctx_done(fctx, result, __LINE__);
7173 fctx_done(fctx, result, __LINE__);
7179 *** Resolver Methods
7182 destroy_badcache(dns_resolver_t *res) {
7183 dns_badcache_t *bad, *next;
7186 if (res->badcache != NULL) {
7187 for (i = 0; i < res->badhash; i++)
7188 for (bad = res->badcache[i]; bad != NULL;
7191 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7195 isc_mem_put(res->mctx, res->badcache,
7196 sizeof(*res->badcache) * res->badhash);
7197 res->badcache = NULL;
7199 INSIST(res->badcount == 0);
7204 destroy(dns_resolver_t *res) {
7208 REQUIRE(res->references == 0);
7209 REQUIRE(!res->priming);
7210 REQUIRE(res->primefetch == NULL);
7214 INSIST(res->nfctx == 0);
7216 DESTROYLOCK(&res->primelock);
7217 DESTROYLOCK(&res->nlock);
7218 DESTROYLOCK(&res->lock);
7219 for (i = 0; i < res->nbuckets; i++) {
7220 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7221 isc_task_shutdown(res->buckets[i].task);
7222 isc_task_detach(&res->buckets[i].task);
7223 DESTROYLOCK(&res->buckets[i].lock);
7224 isc_mem_detach(&res->buckets[i].mctx);
7226 isc_mem_put(res->mctx, res->buckets,
7227 res->nbuckets * sizeof(fctxbucket_t));
7228 if (res->dispatchv4 != NULL)
7229 dns_dispatch_detach(&res->dispatchv4);
7230 if (res->dispatchv6 != NULL)
7231 dns_dispatch_detach(&res->dispatchv6);
7232 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7233 ISC_LIST_UNLINK(res->alternates, a, link);
7235 dns_name_free(&a->_u._n.name, res->mctx);
7236 isc_mem_put(res->mctx, a, sizeof(*a));
7238 dns_resolver_reset_algorithms(res);
7239 destroy_badcache(res);
7240 dns_resolver_resetmustbesecure(res);
7242 isc_rwlock_destroy(&res->alglock);
7245 isc_rwlock_destroy(&res->mbslock);
7247 isc_timer_detach(&res->spillattimer);
7249 isc_mem_put(res->mctx, res, sizeof(*res));
7253 send_shutdown_events(dns_resolver_t *res) {
7254 isc_event_t *event, *next_event;
7258 * Caller must be holding the resolver lock.
7261 for (event = ISC_LIST_HEAD(res->whenshutdown);
7263 event = next_event) {
7264 next_event = ISC_LIST_NEXT(event, ev_link);
7265 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7266 etask = event->ev_sender;
7267 event->ev_sender = res;
7268 isc_task_sendanddetach(&etask, &event);
7273 empty_bucket(dns_resolver_t *res) {
7274 RTRACE("empty_bucket");
7278 INSIST(res->activebuckets > 0);
7279 res->activebuckets--;
7280 if (res->activebuckets == 0)
7281 send_shutdown_events(res);
7287 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7288 dns_resolver_t *res = event->ev_arg;
7289 isc_result_t result;
7291 isc_boolean_t logit = ISC_FALSE;
7293 REQUIRE(VALID_RESOLVER(res));
7298 INSIST(!res->exiting);
7299 if (res->spillat > res->spillatmin) {
7303 if (res->spillat <= res->spillatmin) {
7304 result = isc_timer_reset(res->spillattimer,
7305 isc_timertype_inactive, NULL,
7307 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7309 count = res->spillat;
7312 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7313 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7314 "clients-per-query decreased to %u", count);
7316 isc_event_free(&event);
7320 dns_resolver_create(dns_view_t *view,
7321 isc_taskmgr_t *taskmgr, unsigned int ntasks,
7322 isc_socketmgr_t *socketmgr,
7323 isc_timermgr_t *timermgr,
7324 unsigned int options,
7325 dns_dispatchmgr_t *dispatchmgr,
7326 dns_dispatch_t *dispatchv4,
7327 dns_dispatch_t *dispatchv6,
7328 dns_resolver_t **resp)
7330 dns_resolver_t *res;
7331 isc_result_t result = ISC_R_SUCCESS;
7332 unsigned int i, buckets_created = 0;
7333 isc_task_t *task = NULL;
7338 * Create a resolver.
7341 REQUIRE(DNS_VIEW_VALID(view));
7342 REQUIRE(ntasks > 0);
7343 REQUIRE(resp != NULL && *resp == NULL);
7344 REQUIRE(dispatchmgr != NULL);
7345 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7347 res = isc_mem_get(view->mctx, sizeof(*res));
7349 return (ISC_R_NOMEMORY);
7351 res->mctx = view->mctx;
7352 res->rdclass = view->rdclass;
7353 res->socketmgr = socketmgr;
7354 res->timermgr = timermgr;
7355 res->taskmgr = taskmgr;
7356 res->dispatchmgr = dispatchmgr;
7358 res->options = options;
7360 ISC_LIST_INIT(res->alternates);
7361 res->udpsize = RECV_BUFFER_SIZE;
7362 res->algorithms = NULL;
7363 res->badcache = NULL;
7367 res->mustbesecure = NULL;
7368 res->spillatmin = res->spillat = 10;
7369 res->spillatmax = 100;
7370 res->spillattimer = NULL;
7371 res->zero_no_soa_ttl = ISC_FALSE;
7372 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7374 res->nextdisp = 0; /* meaningless at this point, but init it */
7375 res->nbuckets = ntasks;
7376 res->activebuckets = ntasks;
7377 res->buckets = isc_mem_get(view->mctx,
7378 ntasks * sizeof(fctxbucket_t));
7379 if (res->buckets == NULL) {
7380 result = ISC_R_NOMEMORY;
7383 for (i = 0; i < ntasks; i++) {
7384 result = isc_mutex_init(&res->buckets[i].lock);
7385 if (result != ISC_R_SUCCESS)
7386 goto cleanup_buckets;
7387 res->buckets[i].task = NULL;
7388 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7389 if (result != ISC_R_SUCCESS) {
7390 DESTROYLOCK(&res->buckets[i].lock);
7391 goto cleanup_buckets;
7393 res->buckets[i].mctx = NULL;
7394 snprintf(name, sizeof(name), "res%u", i);
7395 #ifdef ISC_PLATFORM_USETHREADS
7397 * Use a separate memory context for each bucket to reduce
7398 * contention among multiple threads. Do this only when
7399 * enabling threads because it will be require more memory.
7401 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7402 if (result != ISC_R_SUCCESS) {
7403 isc_task_detach(&res->buckets[i].task);
7404 DESTROYLOCK(&res->buckets[i].lock);
7405 goto cleanup_buckets;
7407 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7409 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7411 isc_task_setname(res->buckets[i].task, name, res);
7412 ISC_LIST_INIT(res->buckets[i].fctxs);
7413 res->buckets[i].exiting = ISC_FALSE;
7417 res->dispatchv4 = NULL;
7418 if (dispatchv4 != NULL) {
7419 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
7420 dispattr = dns_dispatch_getattributes(dispatchv4);
7422 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7425 res->dispatchv6 = NULL;
7426 if (dispatchv6 != NULL) {
7427 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
7428 dispattr = dns_dispatch_getattributes(dispatchv6);
7430 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7433 res->references = 1;
7434 res->exiting = ISC_FALSE;
7435 res->frozen = ISC_FALSE;
7436 ISC_LIST_INIT(res->whenshutdown);
7437 res->priming = ISC_FALSE;
7438 res->primefetch = NULL;
7441 result = isc_mutex_init(&res->lock);
7442 if (result != ISC_R_SUCCESS)
7443 goto cleanup_dispatches;
7445 result = isc_mutex_init(&res->nlock);
7446 if (result != ISC_R_SUCCESS)
7449 result = isc_mutex_init(&res->primelock);
7450 if (result != ISC_R_SUCCESS)
7454 result = isc_task_create(taskmgr, 0, &task);
7455 if (result != ISC_R_SUCCESS)
7456 goto cleanup_primelock;
7458 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7459 task, spillattimer_countdown, res,
7460 &res->spillattimer);
7461 isc_task_detach(&task);
7462 if (result != ISC_R_SUCCESS)
7463 goto cleanup_primelock;
7466 result = isc_rwlock_init(&res->alglock, 0, 0);
7467 if (result != ISC_R_SUCCESS)
7468 goto cleanup_spillattimer;
7471 result = isc_rwlock_init(&res->mbslock, 0, 0);
7472 if (result != ISC_R_SUCCESS)
7473 goto cleanup_alglock;
7476 res->magic = RES_MAGIC;
7480 return (ISC_R_SUCCESS);
7485 isc_rwlock_destroy(&res->alglock);
7488 #if USE_ALGLOCK || USE_MBSLOCK
7489 cleanup_spillattimer:
7490 isc_timer_detach(&res->spillattimer);
7494 DESTROYLOCK(&res->primelock);
7497 DESTROYLOCK(&res->nlock);
7500 DESTROYLOCK(&res->lock);
7503 if (res->dispatchv6 != NULL)
7504 dns_dispatch_detach(&res->dispatchv6);
7505 if (res->dispatchv4 != NULL)
7506 dns_dispatch_detach(&res->dispatchv4);
7509 for (i = 0; i < buckets_created; i++) {
7510 isc_mem_detach(&res->buckets[i].mctx);
7511 DESTROYLOCK(&res->buckets[i].lock);
7512 isc_task_shutdown(res->buckets[i].task);
7513 isc_task_detach(&res->buckets[i].task);
7515 isc_mem_put(view->mctx, res->buckets,
7516 res->nbuckets * sizeof(fctxbucket_t));
7519 isc_mem_put(view->mctx, res, sizeof(*res));
7526 prime_done(isc_task_t *task, isc_event_t *event) {
7527 dns_resolver_t *res;
7528 dns_fetchevent_t *fevent;
7530 dns_db_t *db = NULL;
7532 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7533 fevent = (dns_fetchevent_t *)event;
7534 res = event->ev_arg;
7535 REQUIRE(VALID_RESOLVER(res));
7541 INSIST(res->priming);
7542 res->priming = ISC_FALSE;
7543 LOCK(&res->primelock);
7544 fetch = res->primefetch;
7545 res->primefetch = NULL;
7546 UNLOCK(&res->primelock);
7550 if (fevent->result == ISC_R_SUCCESS &&
7551 res->view->cache != NULL && res->view->hints != NULL) {
7552 dns_cache_attachdb(res->view->cache, &db);
7553 dns_root_checkhints(res->view, res->view->hints, db);
7557 if (fevent->node != NULL)
7558 dns_db_detachnode(fevent->db, &fevent->node);
7559 if (fevent->db != NULL)
7560 dns_db_detach(&fevent->db);
7561 if (dns_rdataset_isassociated(fevent->rdataset))
7562 dns_rdataset_disassociate(fevent->rdataset);
7563 INSIST(fevent->sigrdataset == NULL);
7565 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7567 isc_event_free(&event);
7568 dns_resolver_destroyfetch(&fetch);
7572 dns_resolver_prime(dns_resolver_t *res) {
7573 isc_boolean_t want_priming = ISC_FALSE;
7574 dns_rdataset_t *rdataset;
7575 isc_result_t result;
7577 REQUIRE(VALID_RESOLVER(res));
7578 REQUIRE(res->frozen);
7580 RTRACE("dns_resolver_prime");
7584 if (!res->exiting && !res->priming) {
7585 INSIST(res->primefetch == NULL);
7586 res->priming = ISC_TRUE;
7587 want_priming = ISC_TRUE;
7594 * To avoid any possible recursive locking problems, we
7595 * start the priming fetch like any other fetch, and holding
7596 * no resolver locks. No one else will try to start it
7597 * because we're the ones who set res->priming to true.
7598 * Any other callers of dns_resolver_prime() while we're
7599 * running will see that res->priming is already true and
7603 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7604 if (rdataset == NULL) {
7606 INSIST(res->priming);
7607 INSIST(res->primefetch == NULL);
7608 res->priming = ISC_FALSE;
7612 dns_rdataset_init(rdataset);
7613 LOCK(&res->primelock);
7614 result = dns_resolver_createfetch(res, dns_rootname,
7616 NULL, NULL, NULL, 0,
7617 res->buckets[0].task,
7619 res, rdataset, NULL,
7621 UNLOCK(&res->primelock);
7622 if (result != ISC_R_SUCCESS) {
7624 INSIST(res->priming);
7625 res->priming = ISC_FALSE;
7633 dns_resolver_freeze(dns_resolver_t *res) {
7638 REQUIRE(VALID_RESOLVER(res));
7640 res->frozen = ISC_TRUE;
7644 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7645 REQUIRE(VALID_RESOLVER(source));
7646 REQUIRE(targetp != NULL && *targetp == NULL);
7648 RRTRACE(source, "attach");
7649 LOCK(&source->lock);
7650 REQUIRE(!source->exiting);
7652 INSIST(source->references > 0);
7653 source->references++;
7654 INSIST(source->references != 0);
7655 UNLOCK(&source->lock);
7661 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7662 isc_event_t **eventp)
7667 REQUIRE(VALID_RESOLVER(res));
7668 REQUIRE(eventp != NULL);
7675 if (res->exiting && res->activebuckets == 0) {
7677 * We're already shutdown. Send the event.
7679 event->ev_sender = res;
7680 isc_task_send(task, &event);
7683 isc_task_attach(task, &clone);
7684 event->ev_sender = clone;
7685 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7692 dns_resolver_shutdown(dns_resolver_t *res) {
7696 isc_result_t result;
7698 REQUIRE(VALID_RESOLVER(res));
7704 if (!res->exiting) {
7706 res->exiting = ISC_TRUE;
7708 for (i = 0; i < res->nbuckets; i++) {
7709 LOCK(&res->buckets[i].lock);
7710 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7712 fctx = ISC_LIST_NEXT(fctx, link))
7713 fctx_shutdown(fctx);
7714 if (res->dispatchv4 != NULL && !res->exclusivev4) {
7715 sock = dns_dispatch_getsocket(res->dispatchv4);
7716 isc_socket_cancel(sock, res->buckets[i].task,
7717 ISC_SOCKCANCEL_ALL);
7719 if (res->dispatchv6 != NULL && !res->exclusivev6) {
7720 sock = dns_dispatch_getsocket(res->dispatchv6);
7721 isc_socket_cancel(sock, res->buckets[i].task,
7722 ISC_SOCKCANCEL_ALL);
7724 res->buckets[i].exiting = ISC_TRUE;
7725 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7726 INSIST(res->activebuckets > 0);
7727 res->activebuckets--;
7729 UNLOCK(&res->buckets[i].lock);
7731 if (res->activebuckets == 0)
7732 send_shutdown_events(res);
7733 result = isc_timer_reset(res->spillattimer,
7734 isc_timertype_inactive, NULL,
7736 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7743 dns_resolver_detach(dns_resolver_t **resp) {
7744 dns_resolver_t *res;
7745 isc_boolean_t need_destroy = ISC_FALSE;
7747 REQUIRE(resp != NULL);
7749 REQUIRE(VALID_RESOLVER(res));
7755 INSIST(res->references > 0);
7757 if (res->references == 0) {
7758 INSIST(res->exiting && res->activebuckets == 0);
7759 need_destroy = ISC_TRUE;
7770 static inline isc_boolean_t
7771 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7772 unsigned int options)
7775 * Don't match fetch contexts that are shutting down.
7777 if (fctx->cloned || fctx->state == fetchstate_done ||
7778 ISC_LIST_EMPTY(fctx->events))
7781 if (fctx->type != type || fctx->options != options)
7783 return (dns_name_equal(&fctx->name, name));
7787 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7788 char namebuf[DNS_NAME_FORMATSIZE];
7789 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7790 int level = ISC_LOG_DEBUG(1);
7792 if (! isc_log_wouldlog(dns_lctx, level))
7795 dns_name_format(name, namebuf, sizeof(namebuf));
7796 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7798 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7799 DNS_LOGMODULE_RESOLVER, level,
7800 "createfetch: %s %s", namebuf, typebuf);
7804 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7805 dns_rdatatype_t type,
7806 dns_name_t *domain, dns_rdataset_t *nameservers,
7807 dns_forwarders_t *forwarders,
7808 unsigned int options, isc_task_t *task,
7809 isc_taskaction_t action, void *arg,
7810 dns_rdataset_t *rdataset,
7811 dns_rdataset_t *sigrdataset,
7812 dns_fetch_t **fetchp)
7814 return (dns_resolver_createfetch2(res, name, type, domain,
7815 nameservers, forwarders, NULL, 0,
7816 options, task, action, arg,
7817 rdataset, sigrdataset, fetchp));
7821 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7822 dns_rdatatype_t type,
7823 dns_name_t *domain, dns_rdataset_t *nameservers,
7824 dns_forwarders_t *forwarders,
7825 isc_sockaddr_t *client, dns_messageid_t id,
7826 unsigned int options, isc_task_t *task,
7827 isc_taskaction_t action, void *arg,
7828 dns_rdataset_t *rdataset,
7829 dns_rdataset_t *sigrdataset,
7830 dns_fetch_t **fetchp)
7833 fetchctx_t *fctx = NULL;
7834 isc_result_t result = ISC_R_SUCCESS;
7835 unsigned int bucketnum;
7836 isc_boolean_t new_fctx = ISC_FALSE;
7838 unsigned int count = 0;
7839 unsigned int spillat;
7840 unsigned int spillatmin;
7844 REQUIRE(VALID_RESOLVER(res));
7845 REQUIRE(res->frozen);
7846 /* XXXRTH Check for meta type */
7847 if (domain != NULL) {
7848 REQUIRE(DNS_RDATASET_VALID(nameservers));
7849 REQUIRE(nameservers->type == dns_rdatatype_ns);
7851 REQUIRE(nameservers == NULL);
7852 REQUIRE(forwarders == NULL);
7853 REQUIRE(!dns_rdataset_isassociated(rdataset));
7854 REQUIRE(sigrdataset == NULL ||
7855 !dns_rdataset_isassociated(sigrdataset));
7856 REQUIRE(fetchp != NULL && *fetchp == NULL);
7858 log_fetch(name, type);
7861 * XXXRTH use a mempool?
7863 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7865 return (ISC_R_NOMEMORY);
7867 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7870 spillat = res->spillat;
7871 spillatmin = res->spillatmin;
7873 LOCK(&res->buckets[bucketnum].lock);
7875 if (res->buckets[bucketnum].exiting) {
7876 result = ISC_R_SHUTTINGDOWN;
7880 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7881 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
7883 fctx = ISC_LIST_NEXT(fctx, link)) {
7884 if (fctx_match(fctx, name, type, options))
7890 * Is this a duplicate?
7892 if (fctx != NULL && client != NULL) {
7893 dns_fetchevent_t *fevent;
7894 for (fevent = ISC_LIST_HEAD(fctx->events);
7896 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
7897 if (fevent->client != NULL && fevent->id == id &&
7898 isc_sockaddr_equal(fevent->client, client)) {
7899 result = DNS_R_DUPLICATE;
7905 if (count >= spillatmin && spillatmin != 0) {
7906 INSIST(fctx != NULL);
7907 if (count >= spillat)
7908 fctx->spilled = ISC_TRUE;
7909 if (fctx->spilled) {
7910 result = DNS_R_DROP;
7916 result = fctx_create(res, name, type, domain, nameservers,
7917 options, bucketnum, &fctx);
7918 if (result != ISC_R_SUCCESS)
7920 new_fctx = ISC_TRUE;
7923 result = fctx_join(fctx, task, client, id, action, arg,
7924 rdataset, sigrdataset, fetch);
7926 if (result == ISC_R_SUCCESS) {
7930 event = &fctx->control_event;
7931 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
7932 DNS_EVENT_FETCHCONTROL,
7933 fctx_start, fctx, NULL,
7935 isc_task_send(res->buckets[bucketnum].task, &event);
7938 * We don't care about the result of fctx_destroy()
7939 * since we know we're not exiting.
7941 (void)fctx_destroy(fctx);
7946 UNLOCK(&res->buckets[bucketnum].lock);
7948 if (result == ISC_R_SUCCESS) {
7952 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7958 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7960 dns_resolver_t *res;
7961 dns_fetchevent_t *event, *next_event;
7964 REQUIRE(DNS_FETCH_VALID(fetch));
7965 fctx = fetch->private;
7966 REQUIRE(VALID_FCTX(fctx));
7969 FTRACE("cancelfetch");
7971 LOCK(&res->buckets[fctx->bucketnum].lock);
7974 * Find the completion event for this fetch (as opposed
7975 * to those for other fetches that have joined the same
7976 * fctx) and send it with result = ISC_R_CANCELED.
7979 if (fctx->state != fetchstate_done) {
7980 for (event = ISC_LIST_HEAD(fctx->events);
7982 event = next_event) {
7983 next_event = ISC_LIST_NEXT(event, ev_link);
7984 if (event->fetch == fetch) {
7985 ISC_LIST_UNLINK(fctx->events, event, ev_link);
7990 if (event != NULL) {
7991 etask = event->ev_sender;
7992 event->ev_sender = fctx;
7993 event->result = ISC_R_CANCELED;
7994 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
7997 * The fctx continues running even if no fetches remain;
7998 * the answer is still cached.
8001 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8005 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8007 dns_resolver_t *res;
8008 dns_fetchevent_t *event, *next_event;
8010 unsigned int bucketnum;
8011 isc_boolean_t bucket_empty;
8013 REQUIRE(fetchp != NULL);
8015 REQUIRE(DNS_FETCH_VALID(fetch));
8016 fctx = fetch->private;
8017 REQUIRE(VALID_FCTX(fctx));
8020 FTRACE("destroyfetch");
8022 bucketnum = fctx->bucketnum;
8023 LOCK(&res->buckets[bucketnum].lock);
8026 * Sanity check: the caller should have gotten its event before
8027 * trying to destroy the fetch.
8030 if (fctx->state != fetchstate_done) {
8031 for (event = ISC_LIST_HEAD(fctx->events);
8033 event = next_event) {
8034 next_event = ISC_LIST_NEXT(event, ev_link);
8035 RUNTIME_CHECK(event->fetch != fetch);
8039 bucket_empty = fctx_decreference(fctx);
8041 UNLOCK(&res->buckets[bucketnum].lock);
8043 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8051 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8052 isc_logcategory_t *category, isc_logmodule_t *module,
8053 int level, isc_boolean_t duplicateok)
8056 dns_resolver_t *res;
8057 char domainbuf[DNS_NAME_FORMATSIZE];
8059 REQUIRE(DNS_FETCH_VALID(fetch));
8060 fctx = fetch->private;
8061 REQUIRE(VALID_FCTX(fctx));
8064 LOCK(&res->buckets[fctx->bucketnum].lock);
8066 INSIST(fctx->exitline >= 0);
8067 if (!fctx->logged || duplicateok) {
8068 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8069 isc_log_write(lctx, category, module, level,
8070 "fetch completed at %s:%d for %s in "
8071 "%" ISC_PRINT_QUADFORMAT "u."
8072 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8073 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8074 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8075 "adberr:%u,findfail:%u,valfail:%u]",
8076 __FILE__, fctx->exitline, fctx->info,
8077 fctx->duration / 1000000,
8078 fctx->duration % 1000000,
8079 isc_result_totext(fctx->result),
8080 isc_result_totext(fctx->vresult), domainbuf,
8081 fctx->referrals, fctx->restarts,
8082 fctx->querysent, fctx->timeouts, fctx->lamecount,
8083 fctx->neterr, fctx->badresp, fctx->adberr,
8084 fctx->findfail, fctx->valfail);
8085 fctx->logged = ISC_TRUE;
8088 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8092 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8093 REQUIRE(VALID_RESOLVER(resolver));
8094 return (resolver->dispatchmgr);
8098 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8099 REQUIRE(VALID_RESOLVER(resolver));
8100 return (resolver->dispatchv4);
8104 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8105 REQUIRE(VALID_RESOLVER(resolver));
8106 return (resolver->dispatchv6);
8110 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8111 REQUIRE(VALID_RESOLVER(resolver));
8112 return (resolver->socketmgr);
8116 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8117 REQUIRE(VALID_RESOLVER(resolver));
8118 return (resolver->taskmgr);
8122 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8123 REQUIRE(VALID_RESOLVER(resolver));
8124 return (resolver->lame_ttl);
8128 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8129 REQUIRE(VALID_RESOLVER(resolver));
8130 resolver->lame_ttl = lame_ttl;
8134 dns_resolver_nrunning(dns_resolver_t *resolver) {
8136 LOCK(&resolver->nlock);
8137 n = resolver->nfctx;
8138 UNLOCK(&resolver->nlock);
8143 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8144 dns_name_t *name, in_port_t port) {
8146 isc_result_t result;
8148 REQUIRE(VALID_RESOLVER(resolver));
8149 REQUIRE(!resolver->frozen);
8150 REQUIRE((alt == NULL) ^ (name == NULL));
8152 a = isc_mem_get(resolver->mctx, sizeof(*a));
8154 return (ISC_R_NOMEMORY);
8156 a->isaddress = ISC_TRUE;
8159 a->isaddress = ISC_FALSE;
8160 a->_u._n.port = port;
8161 dns_name_init(&a->_u._n.name, NULL);
8162 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8163 if (result != ISC_R_SUCCESS) {
8164 isc_mem_put(resolver->mctx, a, sizeof(*a));
8168 ISC_LINK_INIT(a, link);
8169 ISC_LIST_APPEND(resolver->alternates, a, link);
8171 return (ISC_R_SUCCESS);
8175 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8176 REQUIRE(VALID_RESOLVER(resolver));
8177 resolver->udpsize = udpsize;
8181 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8182 REQUIRE(VALID_RESOLVER(resolver));
8183 return (resolver->udpsize);
8187 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8189 dns_badcache_t *bad, *prev, *next;
8191 REQUIRE(VALID_RESOLVER(resolver));
8193 LOCK(&resolver->lock);
8194 if (resolver->badcache == NULL)
8199 isc_result_t result;
8200 result = isc_time_now(&now);
8201 if (result != ISC_R_SUCCESS)
8202 isc_time_settoepoch(&now);
8203 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8205 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8208 n = isc_time_compare(&bad->expire, &now);
8209 if (n < 0 || dns_name_equal(name, &bad->name)) {
8211 resolver->badcache[i] = bad->next;
8213 prev->next = bad->next;
8214 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8216 resolver->badcount--;
8221 destroy_badcache(resolver);
8224 UNLOCK(&resolver->lock);
8229 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8230 unsigned int newsize;
8231 dns_badcache_t **new, *bad, *next;
8235 newsize = resolver->badhash * 2 + 1;
8237 newsize = (resolver->badhash - 1) / 2;
8239 new = isc_mem_get(resolver->mctx,
8240 sizeof(*resolver->badcache) * newsize);
8243 memset(new, 0, sizeof(*resolver->badcache) * newsize);
8244 for (i = 0; i < resolver->badhash; i++) {
8245 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8247 if (isc_time_compare(&bad->expire, now) < 0) {
8248 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8250 resolver->badcount--;
8252 bad->next = new[bad->hashval % newsize];
8253 new[bad->hashval % newsize] = bad;
8257 isc_mem_put(resolver->mctx, resolver->badcache,
8258 sizeof(*resolver->badcache) * resolver->badhash);
8259 resolver->badhash = newsize;
8260 resolver->badcache = new;
8264 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8265 dns_rdatatype_t type, isc_time_t *expire)
8268 isc_result_t result = ISC_R_SUCCESS;
8269 unsigned int i, hashval;
8270 dns_badcache_t *bad, *prev, *next;
8272 REQUIRE(VALID_RESOLVER(resolver));
8274 LOCK(&resolver->lock);
8275 if (resolver->badcache == NULL) {
8276 resolver->badcache = isc_mem_get(resolver->mctx,
8277 sizeof(*resolver->badcache) *
8279 if (resolver->badcache == NULL) {
8280 result = ISC_R_NOMEMORY;
8283 resolver->badhash = DNS_BADCACHE_SIZE;
8284 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8288 result = isc_time_now(&now);
8289 if (result != ISC_R_SUCCESS)
8290 isc_time_settoepoch(&now);
8291 hashval = dns_name_hash(name, ISC_FALSE);
8292 i = hashval % resolver->badhash;
8294 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8296 if (bad->type == type && dns_name_equal(name, &bad->name))
8298 if (isc_time_compare(&bad->expire, &now) < 0) {
8300 resolver->badcache[i] = bad->next;
8302 prev->next = bad->next;
8303 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8305 resolver->badcount--;
8310 isc_buffer_t buffer;
8311 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8313 result = ISC_R_NOMEMORY;
8317 bad->hashval = hashval;
8318 isc_buffer_init(&buffer, bad + 1, name->length);
8319 dns_name_init(&bad->name, NULL);
8320 dns_name_copy(name, &bad->name, &buffer);
8321 bad->next = resolver->badcache[i];
8322 resolver->badcache[i] = bad;
8323 resolver->badcount++;
8324 if (resolver->badcount > resolver->badhash * 8)
8325 resizehash(resolver, &now, ISC_TRUE);
8326 if (resolver->badcount < resolver->badhash * 2 &&
8327 resolver->badhash > DNS_BADCACHE_SIZE)
8328 resizehash(resolver, &now, ISC_FALSE);
8330 bad->expire = *expire;
8332 UNLOCK(&resolver->lock);
8336 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8337 dns_rdatatype_t type, isc_time_t *now)
8339 dns_badcache_t *bad, *prev, *next;
8340 isc_boolean_t answer = ISC_FALSE;
8343 REQUIRE(VALID_RESOLVER(resolver));
8345 LOCK(&resolver->lock);
8346 if (resolver->badcache == NULL)
8349 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8351 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8354 * Search the hash list. Clean out expired records as we go.
8356 if (isc_time_compare(&bad->expire, now) < 0) {
8358 prev->next = bad->next;
8360 resolver->badcache[i] = bad->next;
8361 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8363 resolver->badcount--;
8366 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8374 * Slow sweep to clean out stale records.
8376 i = resolver->badsweep++ % resolver->badhash;
8377 bad = resolver->badcache[i];
8378 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8379 resolver->badcache[i] = bad->next;
8380 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8382 resolver->badcount--;
8386 UNLOCK(&resolver->lock);
8391 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8392 char namebuf[DNS_NAME_FORMATSIZE];
8393 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8394 dns_badcache_t *bad, *next, *prev;
8399 LOCK(&resolver->lock);
8400 fprintf(fp, ";\n; Bad cache\n;\n");
8402 if (resolver->badcache == NULL)
8406 for (i = 0; i < resolver->badhash; i++) {
8408 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8410 if (isc_time_compare(&bad->expire, &now) < 0) {
8412 prev->next = bad->next;
8414 resolver->badcache[i] = bad->next;
8415 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8417 resolver->badcount--;
8421 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8422 dns_rdatatype_format(bad->type, typebuf,
8424 t = isc_time_microdiff(&bad->expire, &now);
8426 fprintf(fp, "; %s/%s [ttl "
8427 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8428 namebuf, typebuf, t);
8433 UNLOCK(&resolver->lock);
8437 free_algorithm(void *node, void *arg) {
8438 unsigned char *algorithms = node;
8439 isc_mem_t *mctx = arg;
8441 isc_mem_put(mctx, algorithms, *algorithms);
8445 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8447 REQUIRE(VALID_RESOLVER(resolver));
8450 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8452 if (resolver->algorithms != NULL)
8453 dns_rbt_destroy(&resolver->algorithms);
8455 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8460 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8463 unsigned int len, mask;
8465 unsigned char *algorithms;
8466 isc_result_t result;
8467 dns_rbtnode_t *node = NULL;
8469 REQUIRE(VALID_RESOLVER(resolver));
8471 return (ISC_R_RANGE);
8474 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8476 if (resolver->algorithms == NULL) {
8477 result = dns_rbt_create(resolver->mctx, free_algorithm,
8478 resolver->mctx, &resolver->algorithms);
8479 if (result != ISC_R_SUCCESS)
8484 mask = 1 << (alg%8);
8486 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8488 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8489 algorithms = node->data;
8490 if (algorithms == NULL || len > *algorithms) {
8491 new = isc_mem_get(resolver->mctx, len);
8493 result = ISC_R_NOMEMORY;
8496 memset(new, 0, len);
8497 if (algorithms != NULL)
8498 memcpy(new, algorithms, *algorithms);
8502 if (algorithms != NULL)
8503 isc_mem_put(resolver->mctx, algorithms,
8506 algorithms[len-1] |= mask;
8508 result = ISC_R_SUCCESS;
8511 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8517 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8520 unsigned int len, mask;
8521 unsigned char *algorithms;
8523 isc_result_t result;
8524 isc_boolean_t found = ISC_FALSE;
8526 REQUIRE(VALID_RESOLVER(resolver));
8529 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8531 if (resolver->algorithms == NULL)
8533 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8534 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8536 mask = 1 << (alg%8);
8538 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8543 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8547 return (dst_algorithm_supported(alg));
8551 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8554 return (dns_ds_digest_supported(digest));
8558 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8560 REQUIRE(VALID_RESOLVER(resolver));
8563 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8565 if (resolver->mustbesecure != NULL)
8566 dns_rbt_destroy(&resolver->mustbesecure);
8568 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8572 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8575 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8576 isc_boolean_t value)
8578 isc_result_t result;
8580 REQUIRE(VALID_RESOLVER(resolver));
8583 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8585 if (resolver->mustbesecure == NULL) {
8586 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8587 &resolver->mustbesecure);
8588 if (result != ISC_R_SUCCESS)
8591 result = dns_rbt_addname(resolver->mustbesecure, name,
8592 value ? &yes : &no);
8595 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8601 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8603 isc_boolean_t value = ISC_FALSE;
8604 isc_result_t result;
8606 REQUIRE(VALID_RESOLVER(resolver));
8609 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8611 if (resolver->mustbesecure == NULL)
8613 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8614 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8615 value = *(isc_boolean_t*)data;
8618 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8624 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8625 isc_uint32_t *min, isc_uint32_t *max)
8627 REQUIRE(VALID_RESOLVER(resolver));
8629 LOCK(&resolver->lock);
8631 *cur = resolver->spillat;
8633 *min = resolver->spillatmin;
8635 *max = resolver->spillatmax;
8636 UNLOCK(&resolver->lock);
8640 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
8643 REQUIRE(VALID_RESOLVER(resolver));
8645 LOCK(&resolver->lock);
8646 resolver->spillatmin = resolver->spillat = min;
8647 resolver->spillatmax = max;
8648 UNLOCK(&resolver->lock);
8652 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
8653 REQUIRE(VALID_RESOLVER(resolver));
8655 return (resolver->zero_no_soa_ttl);
8659 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
8660 REQUIRE(VALID_RESOLVER(resolver));
8662 resolver->zero_no_soa_ttl = state;
8666 dns_resolver_getoptions(dns_resolver_t *resolver) {
8667 REQUIRE(VALID_RESOLVER(resolver));
8669 return (resolver->options);
8673 dns_resolver_gettimeout(dns_resolver_t *resolver) {
8674 REQUIRE(VALID_RESOLVER(resolver));
8676 return (resolver->query_timeout);
8680 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
8681 REQUIRE(VALID_RESOLVER(resolver));
8684 seconds = DEFAULT_QUERY_TIMEOUT;
8685 if (seconds > MAXIMUM_QUERY_TIMEOUT)
8686 seconds = MAXIMUM_QUERY_TIMEOUT;
8688 resolver->query_timeout = seconds;