2 * Copyright (C) 2004-2012 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.
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;
219 isc_boolean_t ns_ttl_ok;
223 * The number of events we're waiting for.
225 unsigned int pending;
228 * The number of times we've "restarted" the current
229 * nameserver set. This acts as a failsafe to prevent
230 * us from pounding constantly on a particular set of
231 * servers that, for whatever reason, are not giving
232 * us useful responses, but are responding in such a
233 * way that they are not marked "bad".
235 unsigned int restarts;
238 * The number of timeouts that have occurred since we
239 * last successfully received a response packet. This
240 * is used for EDNS0 black hole detection.
242 unsigned int timeouts;
245 * Look aside state for DS lookups.
248 dns_fetch_t * nsfetch;
249 dns_rdataset_t nsrrset;
252 * Number of queries that reference this context.
254 unsigned int nqueries;
257 * The reason to print when logging a successful
258 * response to a query.
263 * Random numbers to use for mixing up server addresses.
265 isc_uint32_t rand_buf;
266 isc_uint32_t rand_bits;
269 * Fetch-local statistics for detailed logging.
271 isc_result_t result; /*%< fetch result */
272 isc_result_t vresult; /*%< validation result */
275 isc_uint64_t duration;
276 isc_boolean_t logged;
277 unsigned int querysent;
278 unsigned int referrals;
279 unsigned int lamecount;
281 unsigned int badresp;
283 unsigned int findfail;
284 unsigned int valfail;
285 isc_boolean_t timeout;
286 dns_adbaddrinfo_t *addrinfo;
287 isc_sockaddr_t *client;
290 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
291 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
293 #define FCTX_ATTR_HAVEANSWER 0x0001
294 #define FCTX_ATTR_GLUING 0x0002
295 #define FCTX_ATTR_ADDRWAIT 0x0004
296 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
297 #define FCTX_ATTR_WANTCACHE 0x0010
298 #define FCTX_ATTR_WANTNCACHE 0x0020
299 #define FCTX_ATTR_NEEDEDNS0 0x0040
300 #define FCTX_ATTR_TRIEDFIND 0x0080
301 #define FCTX_ATTR_TRIEDALT 0x0100
303 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
305 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
307 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
309 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
311 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
312 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
313 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
314 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
315 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
318 dns_adbaddrinfo_t * addrinfo;
324 fetchctx_t * private;
327 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
328 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
330 typedef struct fctxbucket {
333 ISC_LIST(fetchctx_t) fctxs;
334 isc_boolean_t exiting;
338 typedef struct alternate {
339 isc_boolean_t isaddress;
347 ISC_LINK(struct alternate) link;
350 typedef struct dns_badcache dns_badcache_t;
351 struct dns_badcache {
352 dns_badcache_t * next;
353 dns_rdatatype_t type;
355 unsigned int hashval;
358 #define DNS_BADCACHE_SIZE 1021
359 #define DNS_BADCACHE_TTL(fctx) \
360 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
362 struct dns_resolver {
368 isc_mutex_t primelock;
369 dns_rdataclass_t rdclass;
370 isc_socketmgr_t * socketmgr;
371 isc_timermgr_t * timermgr;
372 isc_taskmgr_t * taskmgr;
374 isc_boolean_t frozen;
375 unsigned int options;
376 dns_dispatchmgr_t * dispatchmgr;
377 dns_dispatch_t * dispatchv4;
378 isc_boolean_t exclusivev4;
379 dns_dispatch_t * dispatchv6;
380 isc_boolean_t exclusivev6;
382 unsigned int nbuckets;
383 fctxbucket_t * buckets;
384 isc_uint32_t lame_ttl;
385 ISC_LIST(alternate_t) alternates;
386 isc_uint16_t udpsize;
388 isc_rwlock_t alglock;
390 dns_rbt_t * algorithms;
392 isc_rwlock_t mbslock;
394 dns_rbt_t * mustbesecure;
395 unsigned int spillatmax;
396 unsigned int spillatmin;
397 isc_timer_t * spillattimer;
398 isc_boolean_t zero_no_soa_ttl;
399 unsigned int query_timeout;
401 /* Locked by lock. */
402 unsigned int references;
403 isc_boolean_t exiting;
404 isc_eventlist_t whenshutdown;
405 unsigned int activebuckets;
406 isc_boolean_t priming;
407 unsigned int spillat; /* clients-per-query */
408 unsigned int nextdisp;
411 dns_badcache_t ** badcache;
412 unsigned int badcount;
413 unsigned int badhash;
414 unsigned int badsweep;
416 /* Locked by primelock. */
417 dns_fetch_t * primefetch;
418 /* Locked by nlock. */
422 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
423 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
426 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
427 * which we also use as an addrinfo flag.
429 #define FCTX_ADDRINFO_MARK 0x0001
430 #define FCTX_ADDRINFO_FORWARDER 0x1000
431 #define FCTX_ADDRINFO_TRIED 0x2000
432 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
434 #define ISFORWARDER(a) (((a)->flags & \
435 FCTX_ADDRINFO_FORWARDER) != 0)
436 #define TRIED(a) (((a)->flags & \
437 FCTX_ADDRINFO_TRIED) != 0)
439 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
440 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
442 static void destroy(dns_resolver_t *res);
443 static void empty_bucket(dns_resolver_t *res);
444 static isc_result_t resquery_send(resquery_t *query);
445 static void resquery_response(isc_task_t *task, isc_event_t *event);
446 static void resquery_connected(isc_task_t *task, isc_event_t *event);
447 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
448 isc_boolean_t badcache);
449 static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
450 static isc_result_t ncache_adderesult(dns_message_t *message,
451 dns_db_t *cache, dns_dbnode_t *node,
452 dns_rdatatype_t covers,
453 isc_stdtime_t now, dns_ttl_t maxttl,
454 isc_boolean_t optout,
455 dns_rdataset_t *ardataset,
456 isc_result_t *eresultp);
457 static void validated(isc_task_t *task, isc_event_t *event);
458 static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked);
459 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
460 isc_result_t reason, badnstype_t badtype);
463 * Increment resolver-related statistics counters.
466 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
467 if (res->view->resstats != NULL)
468 isc_stats_increment(res->view->resstats, counter);
472 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
473 dns_rdatatype_t type, dns_rdataset_t *rdataset,
474 dns_rdataset_t *sigrdataset, unsigned int valoptions,
477 dns_validator_t *validator = NULL;
478 dns_valarg_t *valarg;
481 valarg = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
484 return (ISC_R_NOMEMORY);
487 valarg->addrinfo = addrinfo;
489 if (!ISC_LIST_EMPTY(fctx->validators))
490 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
492 result = dns_validator_create(fctx->res->view, name, type, rdataset,
493 sigrdataset, fctx->rmessage,
494 valoptions, task, validated, valarg,
496 if (result == ISC_R_SUCCESS) {
497 inc_stats(fctx->res, dns_resstatscounter_val);
498 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
499 INSIST(fctx->validator == NULL);
500 fctx->validator = validator;
502 ISC_LIST_APPEND(fctx->validators, validator, link);
504 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
505 valarg, sizeof(*valarg));
510 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
511 dns_namereln_t namereln;
512 dns_rdata_rrsig_t rrsig;
513 dns_rdata_t rdata = DNS_RDATA_INIT;
518 for (result = dns_rdataset_first(rdataset);
519 result == ISC_R_SUCCESS;
520 result = dns_rdataset_next(rdataset)) {
521 dns_rdataset_current(rdataset, &rdata);
522 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
523 RUNTIME_CHECK(result == ISC_R_SUCCESS);
524 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
526 if (namereln == dns_namereln_subdomain)
528 dns_rdata_reset(&rdata);
534 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
536 dns_name_t *domain = &fctx->domain;
537 dns_rdataset_t *rdataset;
538 dns_rdatatype_t type;
540 isc_boolean_t keep_auth = ISC_FALSE;
542 if (message->rcode == dns_rcode_nxdomain)
546 * A DS RRset can appear anywhere in a zone, even for a delegation-only
547 * zone. So a response to an explicit query for this type should be
548 * excluded from delegation-only fixup.
550 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
551 * response to a query for these types can never violate the
552 * delegation-only assumption: if the query name is below a
553 * zone cut, the response should normally be a referral, which should
554 * be accepted; if the query name is below a zone cut but the server
555 * happens to have authority for the zone of the query name, the
556 * response is a (non-referral) answer. But this does not violate
557 * delegation-only because the query name must be in a different zone
558 * due to the "apex-only" nature of these types. Note that if the
559 * remote server happens to have authority for a child zone of a
560 * delegation-only zone, we may still incorrectly "fix" the response
561 * with NXDOMAIN for queries for other types. Unfortunately it's
562 * generally impossible to differentiate this case from violation of
563 * the delegation-only assumption. Once the resolver learns the
564 * correct zone cut, possibly via a separate query for an "apex-only"
565 * type, queries for other types will be resolved correctly.
567 * A query for type ANY will be accepted if it hits an exceptional
568 * type above in the answer section as it should be from a child
571 * Also accept answers with RRSIG records from the child zone.
572 * Direct queries for RRSIG records should not be answered from
576 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
577 (fctx->type == dns_rdatatype_ns ||
578 fctx->type == dns_rdatatype_ds ||
579 fctx->type == dns_rdatatype_soa ||
580 fctx->type == dns_rdatatype_any ||
581 fctx->type == dns_rdatatype_rrsig ||
582 fctx->type == dns_rdatatype_dnskey)) {
583 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
584 while (result == ISC_R_SUCCESS) {
586 dns_message_currentname(message, DNS_SECTION_ANSWER,
588 for (rdataset = ISC_LIST_HEAD(name->list);
590 rdataset = ISC_LIST_NEXT(rdataset, link)) {
591 if (!dns_name_equal(name, &fctx->name))
593 type = rdataset->type;
597 if (type == dns_rdatatype_rrsig &&
598 rrsig_fromchildzone(fctx, rdataset))
601 * Direct query for apex records or DS.
603 if (fctx->type == type &&
604 (type == dns_rdatatype_ds ||
605 type == dns_rdatatype_ns ||
606 type == dns_rdatatype_soa ||
607 type == dns_rdatatype_dnskey))
610 * Indirect query for apex records or DS.
612 if (fctx->type == dns_rdatatype_any &&
613 (type == dns_rdatatype_ns ||
614 type == dns_rdatatype_ds ||
615 type == dns_rdatatype_soa ||
616 type == dns_rdatatype_dnskey))
619 result = dns_message_nextname(message,
625 * A NODATA response to a DS query?
627 if (fctx->type == dns_rdatatype_ds &&
628 message->counts[DNS_SECTION_ANSWER] == 0)
631 /* Look for referral or indication of answer from child zone? */
632 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
635 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
636 while (result == ISC_R_SUCCESS) {
638 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
639 for (rdataset = ISC_LIST_HEAD(name->list);
641 rdataset = ISC_LIST_NEXT(rdataset, link)) {
642 type = rdataset->type;
643 if (type == dns_rdatatype_soa &&
644 dns_name_equal(name, domain))
645 keep_auth = ISC_TRUE;
647 if (type != dns_rdatatype_ns &&
648 type != dns_rdatatype_soa &&
649 type != dns_rdatatype_rrsig)
652 if (type == dns_rdatatype_rrsig) {
653 if (rrsig_fromchildzone(fctx, rdataset))
659 /* NS or SOA records. */
660 if (dns_name_equal(name, domain)) {
662 * If a query for ANY causes a negative
663 * response, we can be sure that this is
664 * an empty node. For other type of queries
665 * we cannot differentiate an empty node
666 * from a node that just doesn't have that
667 * type of record. We only accept the former
670 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
671 fctx->type == dns_rdatatype_any)
673 } else if (dns_name_issubdomain(name, domain)) {
674 /* Referral or answer from child zone. */
678 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
682 message->rcode = dns_rcode_nxdomain;
683 message->counts[DNS_SECTION_ANSWER] = 0;
685 message->counts[DNS_SECTION_AUTHORITY] = 0;
686 message->counts[DNS_SECTION_ADDITIONAL] = 0;
690 static inline isc_result_t
691 fctx_starttimer(fetchctx_t *fctx) {
693 * Start the lifetime timer for fctx.
695 * This is also used for stopping the idle timer; in that
696 * case we must purge events already posted to ensure that
697 * no further idle events are delivered.
699 return (isc_timer_reset(fctx->timer, isc_timertype_once,
700 &fctx->expires, NULL, ISC_TRUE));
704 fctx_stoptimer(fetchctx_t *fctx) {
708 * We don't return a result if resetting the timer to inactive fails
709 * since there's nothing to be done about it. Resetting to inactive
710 * should never fail anyway, since the code as currently written
711 * cannot fail in that case.
713 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
714 NULL, NULL, ISC_TRUE);
715 if (result != ISC_R_SUCCESS) {
716 UNEXPECTED_ERROR(__FILE__, __LINE__,
717 "isc_timer_reset(): %s",
718 isc_result_totext(result));
723 static inline isc_result_t
724 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
726 * Start the idle timer for fctx. The lifetime timer continues
729 return (isc_timer_reset(fctx->timer, isc_timertype_once,
730 &fctx->expires, interval, ISC_FALSE));
734 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
735 * we use fctx_stopidletimer for readability in the code below.
737 #define fctx_stopidletimer fctx_starttimer
741 resquery_destroy(resquery_t **queryp) {
744 REQUIRE(queryp != NULL);
746 REQUIRE(!ISC_LINK_LINKED(query, link));
748 INSIST(query->tcpsocket == NULL);
750 query->fctx->nqueries--;
751 if (SHUTTINGDOWN(query->fctx)) {
752 dns_resolver_t *res = query->fctx->res;
753 if (maybe_destroy(query->fctx, ISC_FALSE))
757 isc_mem_put(query->mctx, query, sizeof(*query));
762 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
763 isc_time_t *finish, isc_boolean_t no_response)
767 unsigned int rtt, rttms;
770 dns_adbaddrinfo_t *addrinfo;
771 isc_socket_t *socket;
776 FCTXTRACE("cancelquery");
778 REQUIRE(!RESQUERY_CANCELED(query));
780 query->attributes |= RESQUERY_ATTR_CANCELED;
783 * Should we update the RTT?
785 if (finish != NULL || no_response) {
786 if (finish != NULL) {
788 * We have both the start and finish times for this
789 * packet, so we can compute a real RTT.
791 rtt = (unsigned int)isc_time_microdiff(finish,
793 factor = DNS_ADB_RTTADJDEFAULT;
796 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
798 dns_resstatscounter_queryrtt0);
799 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
801 dns_resstatscounter_queryrtt1);
802 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
804 dns_resstatscounter_queryrtt2);
805 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
807 dns_resstatscounter_queryrtt3);
808 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
810 dns_resstatscounter_queryrtt4);
813 dns_resstatscounter_queryrtt5);
817 * We don't have an RTT for this query. Maybe the
818 * packet was lost, or maybe this server is very
819 * slow. We don't know. Increase the RTT.
822 rtt = query->addrinfo->srtt + 200000;
826 * Replace the current RTT with our value.
828 factor = DNS_ADB_RTTADJREPLACE;
830 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
833 /* Remember that the server has been tried. */
834 if (!TRIED(query->addrinfo)) {
835 dns_adb_changeflags(fctx->adb, query->addrinfo,
836 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
840 * Age RTTs of servers not tried.
842 factor = DNS_ADB_RTTADJAGE;
844 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
846 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
847 if (UNMARKED(addrinfo))
848 dns_adb_adjustsrtt(fctx->adb, addrinfo,
851 if (finish != NULL && TRIEDFIND(fctx))
852 for (find = ISC_LIST_HEAD(fctx->finds);
854 find = ISC_LIST_NEXT(find, publink))
855 for (addrinfo = ISC_LIST_HEAD(find->list);
857 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
858 if (UNMARKED(addrinfo))
859 dns_adb_adjustsrtt(fctx->adb, addrinfo,
862 if (finish != NULL && TRIEDALT(fctx)) {
863 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
865 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
866 if (UNMARKED(addrinfo))
867 dns_adb_adjustsrtt(fctx->adb, addrinfo,
869 for (find = ISC_LIST_HEAD(fctx->altfinds);
871 find = ISC_LIST_NEXT(find, publink))
872 for (addrinfo = ISC_LIST_HEAD(find->list);
874 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
875 if (UNMARKED(addrinfo))
876 dns_adb_adjustsrtt(fctx->adb, addrinfo,
881 * Check for any outstanding socket events. If they exist, cancel
882 * them and let the event handlers finish the cleanup. The resolver
883 * only needs to worry about managing the connect and send events;
884 * the dispatcher manages the recv events.
886 if (RESQUERY_CONNECTING(query)) {
888 * Cancel the connect.
890 if (query->tcpsocket != NULL) {
891 isc_socket_cancel(query->tcpsocket, NULL,
892 ISC_SOCKCANCEL_CONNECT);
893 } else if (query->dispentry != NULL) {
894 INSIST(query->exclusivesocket);
895 socket = dns_dispatch_getentrysocket(query->dispentry);
897 isc_socket_cancel(socket, NULL,
898 ISC_SOCKCANCEL_CONNECT);
900 } else if (RESQUERY_SENDING(query)) {
902 * Cancel the pending send.
904 if (query->exclusivesocket && query->dispentry != NULL)
905 socket = dns_dispatch_getentrysocket(query->dispentry);
907 socket = dns_dispatch_getsocket(query->dispatch);
909 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
912 if (query->dispentry != NULL)
913 dns_dispatch_removeresponse(&query->dispentry, deventp);
915 ISC_LIST_UNLINK(fctx->queries, query, link);
917 if (query->tsig != NULL)
918 isc_buffer_free(&query->tsig);
920 if (query->tsigkey != NULL)
921 dns_tsigkey_detach(&query->tsigkey);
923 if (query->dispatch != NULL)
924 dns_dispatch_detach(&query->dispatch);
926 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
928 * It's safe to destroy the query now.
930 resquery_destroy(&query);
934 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
935 resquery_t *query, *next_query;
937 FCTXTRACE("cancelqueries");
939 for (query = ISC_LIST_HEAD(fctx->queries);
941 query = next_query) {
942 next_query = ISC_LIST_NEXT(query, link);
943 fctx_cancelquery(&query, NULL, NULL, no_response);
948 fctx_cleanupfinds(fetchctx_t *fctx) {
949 dns_adbfind_t *find, *next_find;
951 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
953 for (find = ISC_LIST_HEAD(fctx->finds);
956 next_find = ISC_LIST_NEXT(find, publink);
957 ISC_LIST_UNLINK(fctx->finds, find, publink);
958 dns_adb_destroyfind(&find);
964 fctx_cleanupaltfinds(fetchctx_t *fctx) {
965 dns_adbfind_t *find, *next_find;
967 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
969 for (find = ISC_LIST_HEAD(fctx->altfinds);
972 next_find = ISC_LIST_NEXT(find, publink);
973 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
974 dns_adb_destroyfind(&find);
976 fctx->altfind = NULL;
980 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
981 dns_adbaddrinfo_t *addr, *next_addr;
983 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
985 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
988 next_addr = ISC_LIST_NEXT(addr, publink);
989 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
990 dns_adb_freeaddrinfo(fctx->adb, &addr);
995 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
996 dns_adbaddrinfo_t *addr, *next_addr;
998 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1000 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
1003 next_addr = ISC_LIST_NEXT(addr, publink);
1004 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1005 dns_adb_freeaddrinfo(fctx->adb, &addr);
1010 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1011 FCTXTRACE("stopeverything");
1012 fctx_cancelqueries(fctx, no_response);
1013 fctx_cleanupfinds(fctx);
1014 fctx_cleanupaltfinds(fctx);
1015 fctx_cleanupforwaddrs(fctx);
1016 fctx_cleanupaltaddrs(fctx);
1017 fctx_stoptimer(fctx);
1021 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1022 dns_fetchevent_t *event, *next_event;
1024 unsigned int count = 0;
1026 isc_boolean_t logit = ISC_FALSE;
1028 unsigned int old_spillat;
1029 unsigned int new_spillat = 0; /* initialized to silence
1030 compiler warnings */
1033 * Caller must be holding the appropriate bucket lock.
1035 REQUIRE(fctx->state == fetchstate_done);
1037 FCTXTRACE("sendevents");
1040 * Keep some record of fetch result for logging later (if required).
1042 fctx->result = result;
1043 fctx->exitline = line;
1045 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1047 for (event = ISC_LIST_HEAD(fctx->events);
1049 event = next_event) {
1050 next_event = ISC_LIST_NEXT(event, ev_link);
1051 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1052 task = event->ev_sender;
1053 event->ev_sender = fctx;
1054 event->vresult = fctx->vresult;
1055 if (!HAVE_ANSWER(fctx))
1056 event->result = result;
1058 INSIST(result != ISC_R_SUCCESS ||
1059 dns_rdataset_isassociated(event->rdataset) ||
1060 fctx->type == dns_rdatatype_any ||
1061 fctx->type == dns_rdatatype_rrsig ||
1062 fctx->type == dns_rdatatype_sig);
1065 * Negative results must be indicated in event->result.
1067 if (dns_rdataset_isassociated(event->rdataset) &&
1068 NEGATIVE(event->rdataset)) {
1069 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1070 event->result == DNS_R_NCACHENXRRSET);
1073 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1077 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1079 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1080 LOCK(&fctx->res->lock);
1081 if (count == fctx->res->spillat && !fctx->res->exiting) {
1082 old_spillat = fctx->res->spillat;
1083 fctx->res->spillat += 5;
1084 if (fctx->res->spillat > fctx->res->spillatmax &&
1085 fctx->res->spillatmax != 0)
1086 fctx->res->spillat = fctx->res->spillatmax;
1087 new_spillat = fctx->res->spillat;
1088 if (new_spillat != old_spillat) {
1091 isc_interval_set(&i, 20 * 60, 0);
1092 result = isc_timer_reset(fctx->res->spillattimer,
1093 isc_timertype_ticker, NULL,
1095 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1097 UNLOCK(&fctx->res->lock);
1099 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1100 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1101 "clients-per-query increased to %u",
1107 log_edns(fetchctx_t *fctx) {
1108 char domainbuf[DNS_NAME_FORMATSIZE];
1110 if (fctx->reason == NULL)
1113 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1114 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1115 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1116 "success resolving '%s' (in '%s'?) after %s",
1117 fctx->info, domainbuf, fctx->reason);
1119 fctx->reason = NULL;
1123 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1124 dns_resolver_t *res;
1125 isc_boolean_t no_response;
1133 if (result == ISC_R_SUCCESS) {
1135 * Log any deferred EDNS timeout messages.
1138 no_response = ISC_TRUE;
1140 no_response = ISC_FALSE;
1142 fctx->reason = NULL;
1143 fctx_stopeverything(fctx, no_response);
1145 LOCK(&res->buckets[fctx->bucketnum].lock);
1147 fctx->state = fetchstate_done;
1148 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1149 fctx_sendevents(fctx, result, line);
1151 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1155 process_sendevent(resquery_t *query, isc_event_t *event) {
1156 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1157 isc_boolean_t retry = ISC_FALSE;
1158 isc_result_t result;
1163 if (RESQUERY_CANCELED(query)) {
1164 if (query->sends == 0 && query->connects == 0) {
1166 * This query was canceled while the
1167 * isc_socket_sendto/connect() was in progress.
1169 if (query->tcpsocket != NULL)
1170 isc_socket_detach(&query->tcpsocket);
1171 resquery_destroy(&query);
1174 switch (sevent->result) {
1178 case ISC_R_HOSTUNREACH:
1179 case ISC_R_NETUNREACH:
1181 case ISC_R_ADDRNOTAVAIL:
1182 case ISC_R_CONNREFUSED:
1185 * No route to remote.
1187 add_bad(fctx, query->addrinfo, sevent->result,
1189 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1194 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1199 isc_event_free(&event);
1203 * Behave as if the idle timer has expired. For TCP
1204 * this may not actually reflect the latest timer.
1206 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1207 result = fctx_stopidletimer(fctx);
1208 if (result != ISC_R_SUCCESS)
1209 fctx_done(fctx, result, __LINE__);
1211 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1216 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1217 resquery_t *query = event->ev_arg;
1219 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1221 QTRACE("udpconnected");
1225 INSIST(RESQUERY_CONNECTING(query));
1229 process_sendevent(query, event);
1233 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1234 resquery_t *query = event->ev_arg;
1236 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1243 * Currently we don't wait for the senddone event before retrying
1244 * a query. This means that if we get really behind, we may end
1245 * up doing extra work!
1250 INSIST(RESQUERY_SENDING(query));
1254 process_sendevent(query, event);
1257 static inline isc_result_t
1258 fctx_addopt(dns_message_t *message, unsigned int version,
1259 isc_uint16_t udpsize, isc_boolean_t request_nsid)
1261 dns_rdataset_t *rdataset;
1262 dns_rdatalist_t *rdatalist;
1264 isc_result_t result;
1267 result = dns_message_gettemprdatalist(message, &rdatalist);
1268 if (result != ISC_R_SUCCESS)
1271 result = dns_message_gettemprdata(message, &rdata);
1272 if (result != ISC_R_SUCCESS)
1275 result = dns_message_gettemprdataset(message, &rdataset);
1276 if (result != ISC_R_SUCCESS)
1278 dns_rdataset_init(rdataset);
1280 rdatalist->type = dns_rdatatype_opt;
1281 rdatalist->covers = 0;
1284 * Set Maximum UDP buffer size.
1286 rdatalist->rdclass = udpsize;
1289 * Set EXTENDED-RCODE and Z to 0, DO to 1.
1291 rdatalist->ttl = (version << 16);
1292 rdatalist->ttl |= DNS_MESSAGEEXTFLAG_DO;
1295 * Set EDNS options if applicable
1298 /* Send empty NSID option (RFC5001) */
1299 unsigned char data[4];
1302 isc_buffer_init(&buf, data, sizeof(data));
1303 isc_buffer_putuint16(&buf, DNS_OPT_NSID);
1304 isc_buffer_putuint16(&buf, 0);
1306 rdata->length = sizeof(data);
1312 rdata->rdclass = rdatalist->rdclass;
1313 rdata->type = rdatalist->type;
1316 ISC_LIST_INIT(rdatalist->rdata);
1317 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1318 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset) == ISC_R_SUCCESS);
1320 return (dns_message_setopt(message, rdataset));
1324 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1325 unsigned int seconds;
1329 * We retry every .8 seconds the first two times through the address
1330 * list, and then we do exponential back-off.
1332 if (fctx->restarts < 3)
1335 us = (800000 << (fctx->restarts - 2));
1338 * Double the round-trip time.
1343 * Always wait for at least the doubled round-trip time.
1349 * But don't ever wait for more than 10 seconds.
1354 seconds = us / 1000000;
1355 us -= seconds * 1000000;
1356 isc_interval_set(&fctx->interval, seconds, us * 1000);
1360 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1361 unsigned int options)
1363 dns_resolver_t *res;
1365 isc_result_t result;
1367 isc_sockaddr_t addr;
1368 isc_boolean_t have_addr = ISC_FALSE;
1374 task = res->buckets[fctx->bucketnum].task;
1376 srtt = addrinfo->srtt;
1377 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1380 fctx_setretryinterval(fctx, srtt);
1381 result = fctx_startidletimer(fctx, &fctx->interval);
1382 if (result != ISC_R_SUCCESS)
1385 INSIST(ISC_LIST_EMPTY(fctx->validators));
1387 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1389 query = isc_mem_get(res->buckets[fctx->bucketnum].mctx,
1391 if (query == NULL) {
1392 result = ISC_R_NOMEMORY;
1393 goto stop_idle_timer;
1395 query->mctx = res->buckets[fctx->bucketnum].mctx;
1396 query->options = options;
1397 query->attributes = 0;
1399 query->connects = 0;
1401 * Note that the caller MUST guarantee that 'addrinfo' will remain
1402 * valid until this query is canceled.
1404 query->addrinfo = addrinfo;
1405 TIME_NOW(&query->start);
1408 * If this is a TCP query, then we need to make a socket and
1409 * a dispatch for it here. Otherwise we use the resolver's
1412 query->dispatchmgr = res->dispatchmgr;
1413 query->dispatch = NULL;
1414 query->exclusivesocket = ISC_FALSE;
1415 query->tcpsocket = NULL;
1416 if (res->view->peers != NULL) {
1417 dns_peer_t *peer = NULL;
1418 isc_netaddr_t dstip;
1419 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1420 result = dns_peerlist_peerbyaddr(res->view->peers,
1422 if (result == ISC_R_SUCCESS) {
1423 result = dns_peer_getquerysource(peer, &addr);
1424 if (result == ISC_R_SUCCESS)
1425 have_addr = ISC_TRUE;
1429 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1432 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1437 dns_dispatch_getlocaladdress(res->dispatchv4,
1442 dns_dispatch_getlocaladdress(res->dispatchv6,
1446 result = ISC_R_NOTIMPLEMENTED;
1449 if (result != ISC_R_SUCCESS)
1452 isc_sockaddr_setport(&addr, 0);
1454 result = isc_socket_create(res->socketmgr, pf,
1457 if (result != ISC_R_SUCCESS)
1460 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1461 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1462 if (result != ISC_R_SUCCESS)
1463 goto cleanup_socket;
1467 * A dispatch will be created once the connect succeeds.
1471 unsigned int attrs, attrmask;
1472 attrs = DNS_DISPATCHATTR_UDP;
1473 switch (isc_sockaddr_pf(&addr)) {
1475 attrs |= DNS_DISPATCHATTR_IPV4;
1478 attrs |= DNS_DISPATCHATTR_IPV6;
1481 result = ISC_R_NOTIMPLEMENTED;
1484 attrmask = DNS_DISPATCHATTR_UDP;
1485 attrmask |= DNS_DISPATCHATTR_TCP;
1486 attrmask |= DNS_DISPATCHATTR_IPV4;
1487 attrmask |= DNS_DISPATCHATTR_IPV6;
1488 result = dns_dispatch_getudp(res->dispatchmgr,
1490 res->taskmgr, &addr,
1491 4096, 1000, 32768, 16411,
1492 16433, attrs, attrmask,
1494 if (result != ISC_R_SUCCESS)
1497 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1499 dns_dispatch_attach(res->dispatchv4,
1501 query->exclusivesocket = res->exclusivev4;
1504 dns_dispatch_attach(res->dispatchv6,
1506 query->exclusivesocket = res->exclusivev6;
1509 result = ISC_R_NOTIMPLEMENTED;
1514 * We should always have a valid dispatcher here. If we
1515 * don't support a protocol family, then its dispatcher
1516 * will be NULL, but we shouldn't be finding addresses for
1517 * protocol types we don't support, so the dispatcher
1518 * we found should never be NULL.
1520 INSIST(query->dispatch != NULL);
1523 query->dispentry = NULL;
1526 query->tsigkey = NULL;
1527 ISC_LINK_INIT(query, link);
1528 query->magic = QUERY_MAGIC;
1530 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1532 * Connect to the remote server.
1534 * XXXRTH Should we attach to the socket?
1536 result = isc_socket_connect(query->tcpsocket,
1537 &addrinfo->sockaddr, task,
1538 resquery_connected, query);
1539 if (result != ISC_R_SUCCESS)
1540 goto cleanup_socket;
1542 QTRACE("connecting via TCP");
1544 result = resquery_send(query);
1545 if (result != ISC_R_SUCCESS)
1546 goto cleanup_dispatch;
1550 ISC_LIST_APPEND(fctx->queries, query, link);
1551 query->fctx->nqueries++;
1552 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1553 inc_stats(res, dns_resstatscounter_queryv4);
1555 inc_stats(res, dns_resstatscounter_queryv6);
1556 if (res->view->resquerystats != NULL)
1557 dns_rdatatypestats_increment(res->view->resquerystats,
1560 return (ISC_R_SUCCESS);
1563 isc_socket_detach(&query->tcpsocket);
1566 if (query->dispatch != NULL)
1567 dns_dispatch_detach(&query->dispatch);
1570 if (query->connects == 0) {
1572 isc_mem_put(res->buckets[fctx->bucketnum].mctx,
1573 query, sizeof(*query));
1577 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1582 static isc_boolean_t
1583 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1586 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1588 sa = ISC_LIST_NEXT(sa, link)) {
1589 if (isc_sockaddr_equal(sa, address))
1597 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1600 if (bad_edns(fctx, address))
1603 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1609 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1612 static isc_boolean_t
1613 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1616 for (sa = ISC_LIST_HEAD(fctx->edns);
1618 sa = ISC_LIST_NEXT(sa, link)) {
1619 if (isc_sockaddr_equal(sa, address))
1627 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1630 if (triededns(fctx, address))
1633 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1639 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1642 static isc_boolean_t
1643 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1646 for (sa = ISC_LIST_HEAD(fctx->edns512);
1648 sa = ISC_LIST_NEXT(sa, link)) {
1649 if (isc_sockaddr_equal(sa, address))
1657 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1660 if (triededns512(fctx, address))
1663 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1669 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1673 resquery_send(resquery_t *query) {
1675 isc_result_t result;
1676 dns_name_t *qname = NULL;
1677 dns_rdataset_t *qrdataset = NULL;
1679 dns_resolver_t *res;
1681 isc_socket_t *socket;
1682 isc_buffer_t tcpbuffer;
1683 isc_sockaddr_t *address;
1684 isc_buffer_t *buffer;
1685 isc_netaddr_t ipaddr;
1686 dns_tsigkey_t *tsigkey = NULL;
1687 dns_peer_t *peer = NULL;
1688 isc_boolean_t useedns;
1689 dns_compress_t cctx;
1690 isc_boolean_t cleanup_cctx = ISC_FALSE;
1691 isc_boolean_t secure_domain;
1692 isc_boolean_t connecting = ISC_FALSE;
1698 task = res->buckets[fctx->bucketnum].task;
1701 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1703 * Reserve space for the TCP message length.
1705 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1706 isc_buffer_init(&query->buffer, query->data + 2,
1707 sizeof(query->data) - 2);
1708 buffer = &tcpbuffer;
1710 isc_buffer_init(&query->buffer, query->data,
1711 sizeof(query->data));
1712 buffer = &query->buffer;
1715 result = dns_message_gettempname(fctx->qmessage, &qname);
1716 if (result != ISC_R_SUCCESS)
1718 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1719 if (result != ISC_R_SUCCESS)
1723 * Get a query id from the dispatch.
1725 result = dns_dispatch_addresponse2(query->dispatch,
1726 &query->addrinfo->sockaddr,
1733 if (result != ISC_R_SUCCESS)
1736 fctx->qmessage->opcode = dns_opcode_query;
1741 dns_name_init(qname, NULL);
1742 dns_name_clone(&fctx->name, qname);
1743 dns_rdataset_init(qrdataset);
1744 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1745 ISC_LIST_APPEND(qname->list, qrdataset, link);
1746 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1751 * Set RD if the client has requested that we do a recursive query,
1752 * or if we're sending to a forwarder.
1754 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1755 ISFORWARDER(query->addrinfo))
1756 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1759 * Set CD if the client says don't validate or the question is
1760 * under a secure entry point.
1762 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1763 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1764 } else if (res->view->enablevalidation) {
1765 result = dns_view_issecuredomain(res->view, &fctx->name,
1767 if (result != ISC_R_SUCCESS)
1768 secure_domain = ISC_FALSE;
1769 if (res->view->dlv != NULL)
1770 secure_domain = ISC_TRUE;
1772 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1776 * We don't have to set opcode because it defaults to query.
1778 fctx->qmessage->id = query->id;
1781 * Convert the question to wire format.
1783 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1784 if (result != ISC_R_SUCCESS)
1785 goto cleanup_message;
1786 cleanup_cctx = ISC_TRUE;
1788 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1790 if (result != ISC_R_SUCCESS)
1791 goto cleanup_message;
1793 result = dns_message_rendersection(fctx->qmessage,
1794 DNS_SECTION_QUESTION, 0);
1795 if (result != ISC_R_SUCCESS)
1796 goto cleanup_message;
1799 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1800 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1803 * The ADB does not know about servers with "edns no". Check this,
1804 * and then inform the ADB for future use.
1806 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1808 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1811 query->options |= DNS_FETCHOPT_NOEDNS0;
1812 dns_adb_changeflags(fctx->adb, query->addrinfo,
1813 DNS_FETCHOPT_NOEDNS0,
1814 DNS_FETCHOPT_NOEDNS0);
1817 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1818 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1819 query->options |= DNS_FETCHOPT_NOEDNS0;
1822 * Handle timeouts by reducing the UDP response size to 512 bytes
1823 * then if that doesn't work disabling EDNS (includes DO) and CD.
1825 * These timeout can be due to:
1826 * * broken nameservers that don't respond to EDNS queries.
1827 * * broken/misconfigured firewalls and NAT implementations
1828 * that don't handle IP fragmentation.
1829 * * broken/misconfigured firewalls that don't handle responses
1830 * greater than 512 bytes.
1831 * * broken/misconfigured firewalls that don't handle EDNS, DO
1833 * * packet loss / link outage.
1835 if (fctx->timeout) {
1836 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1837 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1838 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1839 query->options |= DNS_FETCHOPT_NOEDNS0;
1840 fctx->reason = "disabling EDNS";
1841 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1842 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1843 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1844 query->options |= DNS_FETCHOPT_EDNS512;
1845 fctx->reason = "reducing the advertised EDNS UDP "
1846 "packet size to 512 octets";
1848 fctx->timeout = ISC_FALSE;
1852 * Use EDNS0, unless the caller doesn't want it, or we know that
1853 * the remote server doesn't like it.
1855 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1856 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1857 unsigned int version = 0; /* Default version. */
1859 isc_uint16_t udpsize = res->udpsize;
1860 isc_boolean_t reqnsid = res->view->requestnsid;
1862 flags = query->addrinfo->flags;
1863 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1864 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1865 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1867 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1869 else if (peer != NULL)
1870 (void)dns_peer_getudpsize(peer, &udpsize);
1872 /* request NSID for current view or peer? */
1874 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1875 result = fctx_addopt(fctx->qmessage, version,
1877 if (reqnsid && result == ISC_R_SUCCESS) {
1878 query->options |= DNS_FETCHOPT_WANTNSID;
1879 } else if (result != ISC_R_SUCCESS) {
1881 * We couldn't add the OPT, but we'll press on.
1882 * We're not using EDNS0, so set the NOEDNS0
1885 query->options |= DNS_FETCHOPT_NOEDNS0;
1889 * We know this server doesn't like EDNS0, so we
1890 * won't use it. Set the NOEDNS0 bit since we're
1893 query->options |= DNS_FETCHOPT_NOEDNS0;
1898 * If we need EDNS0 to do this query and aren't using it, we lose.
1900 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1901 result = DNS_R_SERVFAIL;
1902 goto cleanup_message;
1905 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1906 add_triededns(fctx, &query->addrinfo->sockaddr);
1908 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1909 add_triededns512(fctx, &query->addrinfo->sockaddr);
1912 * Clear CD if EDNS is not in use.
1914 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1915 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1918 * Add TSIG record tailored to the current recipient.
1920 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1921 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1922 goto cleanup_message;
1924 if (tsigkey != NULL) {
1925 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1926 dns_tsigkey_detach(&tsigkey);
1927 if (result != ISC_R_SUCCESS)
1928 goto cleanup_message;
1931 result = dns_message_rendersection(fctx->qmessage,
1932 DNS_SECTION_ADDITIONAL, 0);
1933 if (result != ISC_R_SUCCESS)
1934 goto cleanup_message;
1936 result = dns_message_renderend(fctx->qmessage);
1937 if (result != ISC_R_SUCCESS)
1938 goto cleanup_message;
1940 dns_compress_invalidate(&cctx);
1941 cleanup_cctx = ISC_FALSE;
1943 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1944 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1946 result = dns_message_getquerytsig(fctx->qmessage,
1949 if (result != ISC_R_SUCCESS)
1950 goto cleanup_message;
1954 * If using TCP, write the length of the message at the beginning
1957 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1958 isc_buffer_usedregion(&query->buffer, &r);
1959 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1960 isc_buffer_add(&tcpbuffer, r.length);
1964 * We're now done with the query message.
1966 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1968 if (query->exclusivesocket)
1969 socket = dns_dispatch_getentrysocket(query->dispentry);
1971 socket = dns_dispatch_getsocket(query->dispatch);
1975 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1976 address = &query->addrinfo->sockaddr;
1977 if (query->exclusivesocket) {
1978 result = isc_socket_connect(socket, address, task,
1979 resquery_udpconnected,
1981 if (result != ISC_R_SUCCESS)
1982 goto cleanup_message;
1983 connecting = ISC_TRUE;
1987 isc_buffer_usedregion(buffer, &r);
1990 * XXXRTH Make sure we don't send to ourselves! We should probably
1991 * prune out these addresses when we get them from the ADB.
1993 result = isc_socket_sendto(socket, &r, task, resquery_senddone,
1994 query, address, NULL);
1995 if (result != ISC_R_SUCCESS) {
1998 * This query is still connecting.
1999 * Mark it as canceled so that it will just be
2000 * cleaned up when the connected event is received.
2001 * Keep fctx around until the event is processed.
2003 query->fctx->nqueries++;
2004 query->attributes |= RESQUERY_ATTR_CANCELED;
2006 goto cleanup_message;
2013 return (ISC_R_SUCCESS);
2017 dns_compress_invalidate(&cctx);
2019 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2022 * Stop the dispatcher from listening.
2024 dns_dispatch_removeresponse(&query->dispentry, NULL);
2028 dns_message_puttempname(fctx->qmessage, &qname);
2029 if (qrdataset != NULL)
2030 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2036 resquery_connected(isc_task_t *task, isc_event_t *event) {
2037 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2038 resquery_t *query = event->ev_arg;
2039 isc_boolean_t retry = ISC_FALSE;
2040 isc_interval_t interval;
2041 isc_result_t result;
2045 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2046 REQUIRE(VALID_QUERY(query));
2048 QTRACE("connected");
2055 * Currently we don't wait for the connect event before retrying
2056 * a query. This means that if we get really behind, we may end
2057 * up doing extra work!
2063 if (RESQUERY_CANCELED(query)) {
2065 * This query was canceled while the connect() was in
2068 isc_socket_detach(&query->tcpsocket);
2069 resquery_destroy(&query);
2071 switch (sevent->result) {
2075 * Extend the idle timer for TCP. 20 seconds
2076 * should be long enough for a TCP connection to be
2077 * established, a single DNS request to be sent,
2078 * and the response received.
2080 isc_interval_set(&interval, 20, 0);
2081 result = fctx_startidletimer(query->fctx, &interval);
2082 if (result != ISC_R_SUCCESS) {
2083 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2084 fctx_done(fctx, result, __LINE__);
2088 * We are connected. Create a dispatcher and
2092 attrs |= DNS_DISPATCHATTR_TCP;
2093 attrs |= DNS_DISPATCHATTR_PRIVATE;
2094 attrs |= DNS_DISPATCHATTR_CONNECTED;
2095 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2097 attrs |= DNS_DISPATCHATTR_IPV4;
2099 attrs |= DNS_DISPATCHATTR_IPV6;
2100 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2102 result = dns_dispatch_createtcp(query->dispatchmgr,
2104 query->fctx->res->taskmgr,
2105 4096, 2, 1, 1, 3, attrs,
2109 * Regardless of whether dns_dispatch_create()
2110 * succeeded or not, we don't need our reference
2111 * to the socket anymore.
2113 isc_socket_detach(&query->tcpsocket);
2115 if (result == ISC_R_SUCCESS)
2116 result = resquery_send(query);
2118 if (result != ISC_R_SUCCESS) {
2119 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2120 fctx_done(fctx, result, __LINE__);
2124 case ISC_R_NETUNREACH:
2125 case ISC_R_HOSTUNREACH:
2126 case ISC_R_CONNREFUSED:
2128 case ISC_R_ADDRNOTAVAIL:
2129 case ISC_R_CONNECTIONRESET:
2131 * No route to remote.
2133 isc_socket_detach(&query->tcpsocket);
2134 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2139 isc_socket_detach(&query->tcpsocket);
2140 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2145 isc_event_free(&event);
2149 * Behave as if the idle timer has expired. For TCP
2150 * connections this may not actually reflect the latest timer.
2152 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2153 result = fctx_stopidletimer(fctx);
2154 if (result != ISC_R_SUCCESS)
2155 fctx_done(fctx, result, __LINE__);
2157 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2162 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2164 dns_adbfind_t *find;
2165 dns_resolver_t *res;
2166 isc_boolean_t want_try = ISC_FALSE;
2167 isc_boolean_t want_done = ISC_FALSE;
2168 isc_boolean_t bucket_empty = ISC_FALSE;
2169 isc_boolean_t destroy = ISC_FALSE;
2170 unsigned int bucketnum;
2172 find = event->ev_sender;
2173 fctx = event->ev_arg;
2174 REQUIRE(VALID_FCTX(fctx));
2179 FCTXTRACE("finddone");
2181 bucketnum = fctx->bucketnum;
2182 LOCK(&res->buckets[bucketnum].lock);
2184 INSIST(fctx->pending > 0);
2187 if (ADDRWAIT(fctx)) {
2189 * The fetch is waiting for a name to be found.
2191 INSIST(!SHUTTINGDOWN(fctx));
2192 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2193 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2194 want_try = ISC_TRUE;
2197 if (fctx->pending == 0) {
2199 * We've got nothing else to wait for and don't
2200 * know the answer. There's nothing to do but
2203 want_done = ISC_TRUE;
2206 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2207 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2209 * Note that we had to wait until we had the lock before
2210 * looking at fctx->references.
2212 if (fctx->references == 0)
2215 UNLOCK(&res->buckets[bucketnum].lock);
2218 bucket_empty = fctx_destroy(fctx);
2219 isc_event_free(&event);
2220 dns_adb_destroyfind(&find);
2223 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2225 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2226 else if (bucket_empty)
2231 static inline isc_boolean_t
2232 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2235 for (sa = ISC_LIST_HEAD(fctx->bad);
2237 sa = ISC_LIST_NEXT(sa, link)) {
2238 if (isc_sockaddr_equal(sa, address))
2245 static inline isc_boolean_t
2246 mark_bad(fetchctx_t *fctx) {
2247 dns_adbfind_t *curr;
2248 dns_adbaddrinfo_t *addrinfo;
2249 isc_boolean_t all_bad = ISC_TRUE;
2252 * Mark all known bad servers, so we don't try to talk to them
2257 * Mark any bad nameservers.
2259 for (curr = ISC_LIST_HEAD(fctx->finds);
2261 curr = ISC_LIST_NEXT(curr, publink)) {
2262 for (addrinfo = ISC_LIST_HEAD(curr->list);
2264 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2265 if (bad_server(fctx, &addrinfo->sockaddr))
2266 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2268 all_bad = ISC_FALSE;
2273 * Mark any bad forwarders.
2275 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2277 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2278 if (bad_server(fctx, &addrinfo->sockaddr))
2279 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2281 all_bad = ISC_FALSE;
2285 * Mark any bad alternates.
2287 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2289 curr = ISC_LIST_NEXT(curr, publink)) {
2290 for (addrinfo = ISC_LIST_HEAD(curr->list);
2292 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2293 if (bad_server(fctx, &addrinfo->sockaddr))
2294 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2296 all_bad = ISC_FALSE;
2300 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2302 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2303 if (bad_server(fctx, &addrinfo->sockaddr))
2304 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2306 all_bad = ISC_FALSE;
2313 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2314 badnstype_t badtype)
2316 char namebuf[DNS_NAME_FORMATSIZE];
2317 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2323 const char *spc = "";
2324 isc_sockaddr_t *address = &addrinfo->sockaddr;
2326 if (reason == DNS_R_LAME)
2330 case badns_unreachable:
2333 case badns_response:
2336 case badns_validation:
2337 break; /* counted as 'valfail' */
2341 if (bad_server(fctx, address)) {
2343 * We already know this server is bad.
2348 FCTXTRACE("add_bad");
2350 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
2355 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2357 if (reason == DNS_R_LAME) /* already logged */
2360 if (reason == DNS_R_UNEXPECTEDRCODE &&
2361 fctx->rmessage->rcode == dns_rcode_servfail &&
2362 ISFORWARDER(addrinfo))
2365 if (reason == DNS_R_UNEXPECTEDRCODE) {
2366 isc_buffer_init(&b, code, sizeof(code) - 1);
2367 dns_rcode_totext(fctx->rmessage->rcode, &b);
2368 code[isc_buffer_usedlength(&b)] = '\0';
2370 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2371 isc_buffer_init(&b, code, sizeof(code) - 1);
2372 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2373 code[isc_buffer_usedlength(&b)] = '\0';
2378 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2379 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2380 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2381 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2382 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2383 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2384 "error (%s%s%s) resolving '%s/%s/%s': %s",
2385 dns_result_totext(reason), spc, code,
2386 namebuf, typebuf, classbuf, addrbuf);
2390 * Sort addrinfo list by RTT.
2393 sort_adbfind(dns_adbfind_t *find) {
2394 dns_adbaddrinfo_t *best, *curr;
2395 dns_adbaddrinfolist_t sorted;
2397 /* Lame N^2 bubble sort. */
2398 ISC_LIST_INIT(sorted);
2399 while (!ISC_LIST_EMPTY(find->list)) {
2400 best = ISC_LIST_HEAD(find->list);
2401 curr = ISC_LIST_NEXT(best, publink);
2402 while (curr != NULL) {
2403 if (curr->srtt < best->srtt)
2405 curr = ISC_LIST_NEXT(curr, publink);
2407 ISC_LIST_UNLINK(find->list, best, publink);
2408 ISC_LIST_APPEND(sorted, best, publink);
2410 find->list = sorted;
2414 * Sort a list of finds by server RTT.
2417 sort_finds(dns_adbfindlist_t *findlist) {
2418 dns_adbfind_t *best, *curr;
2419 dns_adbfindlist_t sorted;
2420 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2422 /* Sort each find's addrinfo list by SRTT. */
2423 for (curr = ISC_LIST_HEAD(*findlist);
2425 curr = ISC_LIST_NEXT(curr, publink))
2428 /* Lame N^2 bubble sort. */
2429 ISC_LIST_INIT(sorted);
2430 while (!ISC_LIST_EMPTY(*findlist)) {
2431 best = ISC_LIST_HEAD(*findlist);
2432 bestaddrinfo = ISC_LIST_HEAD(best->list);
2433 INSIST(bestaddrinfo != NULL);
2434 curr = ISC_LIST_NEXT(best, publink);
2435 while (curr != NULL) {
2436 addrinfo = ISC_LIST_HEAD(curr->list);
2437 INSIST(addrinfo != NULL);
2438 if (addrinfo->srtt < bestaddrinfo->srtt) {
2440 bestaddrinfo = addrinfo;
2442 curr = ISC_LIST_NEXT(curr, publink);
2444 ISC_LIST_UNLINK(*findlist, best, publink);
2445 ISC_LIST_APPEND(sorted, best, publink);
2451 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2452 unsigned int options, unsigned int flags, isc_stdtime_t now,
2453 isc_boolean_t *need_alternate)
2455 dns_adbaddrinfo_t *ai;
2456 dns_adbfind_t *find;
2457 dns_resolver_t *res;
2458 isc_boolean_t unshared;
2459 isc_result_t result;
2462 unshared = ISC_TF((fctx->options | DNS_FETCHOPT_UNSHARED) != 0);
2464 * If this name is a subdomain of the query domain, tell
2465 * the ADB to start looking using zone/hint data. This keeps us
2466 * from getting stuck if the nameserver is beneath the zone cut
2467 * and we don't know its address (e.g. because the A record has
2470 if (dns_name_issubdomain(name, &fctx->domain))
2471 options |= DNS_ADBFIND_STARTATZONE;
2472 options |= DNS_ADBFIND_GLUEOK;
2473 options |= DNS_ADBFIND_HINTOK;
2476 * See what we know about this address.
2479 result = dns_adb_createfind(fctx->adb,
2480 res->buckets[fctx->bucketnum].task,
2481 fctx_finddone, fctx, name,
2482 &fctx->name, fctx->type,
2484 res->view->dstport, &find);
2485 if (result != ISC_R_SUCCESS) {
2486 if (result == DNS_R_ALIAS) {
2488 * XXXRTH Follow the CNAME/DNAME chain?
2490 dns_adb_destroyfind(&find);
2493 } else if (!ISC_LIST_EMPTY(find->list)) {
2495 * We have at least some of the addresses for the
2498 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2499 if (flags != 0 || port != 0) {
2500 for (ai = ISC_LIST_HEAD(find->list);
2502 ai = ISC_LIST_NEXT(ai, publink)) {
2505 isc_sockaddr_setport(&ai->sockaddr,
2509 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2510 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2512 ISC_LIST_APPEND(fctx->finds, find, publink);
2515 * We don't know any of the addresses for this
2518 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2520 * We're looking for them and will get an
2521 * event about it later.
2527 if (need_alternate != NULL &&
2528 !*need_alternate && unshared &&
2529 ((res->dispatchv4 == NULL &&
2530 find->result_v6 != DNS_R_NXDOMAIN) ||
2531 (res->dispatchv6 == NULL &&
2532 find->result_v4 != DNS_R_NXDOMAIN)))
2533 *need_alternate = ISC_TRUE;
2535 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2536 fctx->lamecount++; /* cached lame server */
2538 fctx->adberr++; /* unreachable server, etc. */
2541 * If we know there are no addresses for
2542 * the family we are using then try to add
2543 * an alternative server.
2545 if (need_alternate != NULL && !*need_alternate &&
2546 ((res->dispatchv4 == NULL &&
2547 find->result_v6 == DNS_R_NXRRSET) ||
2548 (res->dispatchv6 == NULL &&
2549 find->result_v4 == DNS_R_NXRRSET)))
2550 *need_alternate = ISC_TRUE;
2551 dns_adb_destroyfind(&find);
2556 static isc_boolean_t
2557 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2559 unsigned int nlabels;
2560 dns_namereln_t namereln;
2562 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2563 return (ISC_TF(namereln == dns_namereln_subdomain));
2567 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2568 dns_rdata_t rdata = DNS_RDATA_INIT;
2569 isc_result_t result;
2570 dns_resolver_t *res;
2572 unsigned int stdoptions = 0;
2574 dns_adbaddrinfo_t *ai;
2575 isc_boolean_t all_bad;
2577 isc_boolean_t need_alternate = ISC_FALSE;
2579 FCTXTRACE("getaddresses");
2582 * Don't pound on remote servers. (Failsafe!)
2585 if (fctx->restarts > 10) {
2586 FCTXTRACE("too many restarts");
2587 return (DNS_R_SERVFAIL);
2596 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2597 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2600 * If this fctx has forwarders, use them; otherwise use any
2601 * selective forwarders specified in the view; otherwise use the
2602 * resolver's forwarders (if any).
2604 sa = ISC_LIST_HEAD(fctx->forwarders);
2606 dns_forwarders_t *forwarders = NULL;
2607 dns_name_t *name = &fctx->name;
2609 unsigned int labels;
2610 dns_fixedname_t fixed;
2614 * DS records are found in the parent server.
2615 * Strip label to get the correct forwarder (if any).
2617 if (dns_rdatatype_atparent(fctx->type) &&
2618 dns_name_countlabels(name) > 1) {
2619 dns_name_init(&suffix, NULL);
2620 labels = dns_name_countlabels(name);
2621 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2625 dns_fixedname_init(&fixed);
2626 domain = dns_fixedname_name(&fixed);
2627 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2628 domain, &forwarders);
2629 if (result == ISC_R_SUCCESS) {
2630 sa = ISC_LIST_HEAD(forwarders->addrs);
2631 fctx->fwdpolicy = forwarders->fwdpolicy;
2632 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2633 isstrictsubdomain(domain, &fctx->domain)) {
2636 mctx = res->buckets[fctx->bucketnum].mctx;
2637 dns_name_free(&fctx->domain, mctx);
2638 dns_name_init(&fctx->domain, NULL);
2639 result = dns_name_dup(domain, mctx,
2641 if (result != ISC_R_SUCCESS)
2647 while (sa != NULL) {
2648 if ((isc_sockaddr_pf(sa) == AF_INET &&
2649 fctx->res->dispatchv4 == NULL) ||
2650 (isc_sockaddr_pf(sa) == AF_INET6 &&
2651 fctx->res->dispatchv6 == NULL)) {
2652 sa = ISC_LIST_NEXT(sa, link);
2656 result = dns_adb_findaddrinfo(fctx->adb,
2657 sa, &ai, 0); /* XXXMLG */
2658 if (result == ISC_R_SUCCESS) {
2659 dns_adbaddrinfo_t *cur;
2660 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2661 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2662 while (cur != NULL && cur->srtt < ai->srtt)
2663 cur = ISC_LIST_NEXT(cur, publink);
2665 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2668 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2670 sa = ISC_LIST_NEXT(sa, link);
2674 * If the forwarding policy is "only", we don't need the addresses
2675 * of the nameservers.
2677 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2681 * Normal nameservers.
2684 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2685 if (fctx->restarts == 1) {
2687 * To avoid sending out a flood of queries likely to
2688 * result in NXRRSET, we suppress fetches for address
2689 * families we don't have the first time through,
2690 * provided that we have addresses in some family we
2693 * We don't want to set this option all the time, since
2694 * if fctx->restarts > 1, we've clearly been having trouble
2695 * with the addresses we had, so getting more could help.
2697 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2699 if (res->dispatchv4 != NULL)
2700 stdoptions |= DNS_ADBFIND_INET;
2701 if (res->dispatchv6 != NULL)
2702 stdoptions |= DNS_ADBFIND_INET6;
2703 isc_stdtime_get(&now);
2705 INSIST(ISC_LIST_EMPTY(fctx->finds));
2706 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2708 for (result = dns_rdataset_first(&fctx->nameservers);
2709 result == ISC_R_SUCCESS;
2710 result = dns_rdataset_next(&fctx->nameservers))
2712 dns_rdataset_current(&fctx->nameservers, &rdata);
2714 * Extract the name from the NS record.
2716 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2717 if (result != ISC_R_SUCCESS)
2720 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2722 dns_rdata_reset(&rdata);
2723 dns_rdata_freestruct(&ns);
2725 if (result != ISC_R_NOMORE)
2729 * Do we need to use 6 to 4?
2731 if (need_alternate) {
2734 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2735 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2737 a = ISC_LIST_NEXT(a, link)) {
2738 if (!a->isaddress) {
2739 findname(fctx, &a->_u._n.name, a->_u._n.port,
2740 stdoptions, FCTX_ADDRINFO_FORWARDER,
2744 if (isc_sockaddr_pf(&a->_u.addr) != family)
2747 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2749 if (result == ISC_R_SUCCESS) {
2750 dns_adbaddrinfo_t *cur;
2751 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2752 cur = ISC_LIST_HEAD(fctx->altaddrs);
2753 while (cur != NULL && cur->srtt < ai->srtt)
2754 cur = ISC_LIST_NEXT(cur, publink);
2756 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2759 ISC_LIST_APPEND(fctx->altaddrs, ai,
2767 * Mark all known bad servers.
2769 all_bad = mark_bad(fctx);
2776 * We've got no addresses.
2778 if (fctx->pending > 0) {
2780 * We're fetching the addresses, but don't have any
2781 * yet. Tell the caller to wait for an answer.
2783 result = DNS_R_WAIT;
2788 * We've lost completely. We don't know any
2789 * addresses, and the ADB has told us it can't get
2792 FCTXTRACE("no addresses");
2793 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2794 result = isc_time_nowplusinterval(&expire, &i);
2796 (fctx->type == dns_rdatatype_dnskey ||
2797 fctx->type == dns_rdatatype_dlv ||
2798 fctx->type == dns_rdatatype_ds) &&
2799 result == ISC_R_SUCCESS)
2800 dns_resolver_addbadcache(fctx->res,
2802 fctx->type, &expire);
2803 result = ISC_R_FAILURE;
2807 * We've found some addresses. We might still be looking
2808 * for more addresses.
2810 sort_finds(&fctx->finds);
2811 sort_finds(&fctx->altfinds);
2812 result = ISC_R_SUCCESS;
2819 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2822 char buf[ISC_NETADDR_FORMATSIZE];
2824 isc_boolean_t aborted = ISC_FALSE;
2825 isc_boolean_t bogus;
2826 dns_acl_t *blackhole;
2827 isc_netaddr_t ipaddr;
2828 dns_peer_t *peer = NULL;
2829 dns_resolver_t *res;
2830 const char *msg = NULL;
2832 sa = &addr->sockaddr;
2835 isc_netaddr_fromsockaddr(&ipaddr, sa);
2836 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2837 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2839 if (blackhole != NULL) {
2842 if (dns_acl_match(&ipaddr, NULL, blackhole,
2844 &match, NULL) == ISC_R_SUCCESS &&
2850 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2855 addr->flags |= FCTX_ADDRINFO_MARK;
2856 msg = "ignoring blackholed / bogus server: ";
2857 } else if (isc_sockaddr_ismulticast(sa)) {
2858 addr->flags |= FCTX_ADDRINFO_MARK;
2859 msg = "ignoring multicast address: ";
2860 } else if (isc_sockaddr_isexperimental(sa)) {
2861 addr->flags |= FCTX_ADDRINFO_MARK;
2862 msg = "ignoring experimental address: ";
2863 } else if (sa->type.sa.sa_family != AF_INET6) {
2865 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2866 addr->flags |= FCTX_ADDRINFO_MARK;
2867 msg = "ignoring IPv6 mapped IPV4 address: ";
2868 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2869 addr->flags |= FCTX_ADDRINFO_MARK;
2870 msg = "ignoring IPv6 compatibility IPV4 address: ";
2874 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2877 isc_netaddr_fromsockaddr(&na, sa);
2878 isc_netaddr_format(&na, buf, sizeof(buf));
2879 FCTXTRACE2(msg, buf);
2882 static inline dns_adbaddrinfo_t *
2883 fctx_nextaddress(fetchctx_t *fctx) {
2884 dns_adbfind_t *find, *start;
2885 dns_adbaddrinfo_t *addrinfo;
2886 dns_adbaddrinfo_t *faddrinfo;
2889 * Return the next untried address, if any.
2893 * Find the first unmarked forwarder (if any).
2895 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2897 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2898 if (!UNMARKED(addrinfo))
2900 possibly_mark(fctx, addrinfo);
2901 if (UNMARKED(addrinfo)) {
2902 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2909 * No forwarders. Move to the next find.
2912 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2916 find = ISC_LIST_HEAD(fctx->finds);
2918 find = ISC_LIST_NEXT(find, publink);
2920 find = ISC_LIST_HEAD(fctx->finds);
2924 * Find the first unmarked addrinfo.
2930 for (addrinfo = ISC_LIST_HEAD(find->list);
2932 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2933 if (!UNMARKED(addrinfo))
2935 possibly_mark(fctx, addrinfo);
2936 if (UNMARKED(addrinfo)) {
2937 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2941 if (addrinfo != NULL)
2943 find = ISC_LIST_NEXT(find, publink);
2945 find = ISC_LIST_HEAD(fctx->finds);
2946 } while (find != start);
2950 if (addrinfo != NULL)
2954 * No nameservers left. Try alternates.
2957 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2959 find = fctx->altfind;
2961 find = ISC_LIST_HEAD(fctx->altfinds);
2963 find = ISC_LIST_NEXT(find, publink);
2965 find = ISC_LIST_HEAD(fctx->altfinds);
2969 * Find the first unmarked addrinfo.
2975 for (addrinfo = ISC_LIST_HEAD(find->list);
2977 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2978 if (!UNMARKED(addrinfo))
2980 possibly_mark(fctx, addrinfo);
2981 if (UNMARKED(addrinfo)) {
2982 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2986 if (addrinfo != NULL)
2988 find = ISC_LIST_NEXT(find, publink);
2990 find = ISC_LIST_HEAD(fctx->altfinds);
2991 } while (find != start);
2994 faddrinfo = addrinfo;
2997 * See if we have a better alternate server by address.
3000 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
3002 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3003 if (!UNMARKED(addrinfo))
3005 possibly_mark(fctx, addrinfo);
3006 if (UNMARKED(addrinfo) &&
3007 (faddrinfo == NULL ||
3008 addrinfo->srtt < faddrinfo->srtt)) {
3009 if (faddrinfo != NULL)
3010 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3011 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3016 if (addrinfo == NULL) {
3017 addrinfo = faddrinfo;
3018 fctx->altfind = find;
3025 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3026 isc_result_t result;
3027 dns_adbaddrinfo_t *addrinfo;
3031 REQUIRE(!ADDRWAIT(fctx));
3033 addrinfo = fctx_nextaddress(fctx);
3034 if (addrinfo == NULL) {
3036 * We have no more addresses. Start over.
3038 fctx_cancelqueries(fctx, ISC_TRUE);
3039 fctx_cleanupfinds(fctx);
3040 fctx_cleanupaltfinds(fctx);
3041 fctx_cleanupforwaddrs(fctx);
3042 fctx_cleanupaltaddrs(fctx);
3043 result = fctx_getaddresses(fctx, badcache);
3044 if (result == DNS_R_WAIT) {
3046 * Sleep waiting for addresses.
3048 FCTXTRACE("addrwait");
3049 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3051 } else if (result != ISC_R_SUCCESS) {
3053 * Something bad happened.
3055 fctx_done(fctx, result, __LINE__);
3059 addrinfo = fctx_nextaddress(fctx);
3061 * While we may have addresses from the ADB, they
3062 * might be bad ones. In this case, return SERVFAIL.
3064 if (addrinfo == NULL) {
3065 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3070 result = fctx_query(fctx, addrinfo, fctx->options);
3071 if (result != ISC_R_SUCCESS)
3072 fctx_done(fctx, result, __LINE__);
3074 inc_stats(fctx->res, dns_resstatscounter_retry);
3077 static isc_boolean_t
3078 fctx_destroy(fetchctx_t *fctx) {
3079 dns_resolver_t *res;
3080 unsigned int bucketnum;
3081 isc_sockaddr_t *sa, *next_sa;
3084 * Caller must be holding the bucket lock.
3087 REQUIRE(VALID_FCTX(fctx));
3088 REQUIRE(fctx->state == fetchstate_done ||
3089 fctx->state == fetchstate_init);
3090 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3091 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3092 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3093 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3094 REQUIRE(fctx->pending == 0);
3095 REQUIRE(fctx->references == 0);
3096 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3098 FCTXTRACE("destroy");
3101 bucketnum = fctx->bucketnum;
3103 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3108 for (sa = ISC_LIST_HEAD(fctx->bad);
3111 next_sa = ISC_LIST_NEXT(sa, link);
3112 ISC_LIST_UNLINK(fctx->bad, sa, link);
3113 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3116 for (sa = ISC_LIST_HEAD(fctx->edns);
3119 next_sa = ISC_LIST_NEXT(sa, link);
3120 ISC_LIST_UNLINK(fctx->edns, sa, link);
3121 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3124 for (sa = ISC_LIST_HEAD(fctx->edns512);
3127 next_sa = ISC_LIST_NEXT(sa, link);
3128 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3129 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3132 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3135 next_sa = ISC_LIST_NEXT(sa, link);
3136 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3137 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3140 isc_timer_detach(&fctx->timer);
3141 dns_message_destroy(&fctx->rmessage);
3142 dns_message_destroy(&fctx->qmessage);
3143 if (dns_name_countlabels(&fctx->domain) > 0)
3144 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3145 if (dns_rdataset_isassociated(&fctx->nameservers))
3146 dns_rdataset_disassociate(&fctx->nameservers);
3147 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3148 dns_db_detach(&fctx->cache);
3149 dns_adb_detach(&fctx->adb);
3150 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3151 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3155 UNLOCK(&res->nlock);
3157 if (res->buckets[bucketnum].exiting &&
3158 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3165 * Fetch event handlers.
3169 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3170 fetchctx_t *fctx = event->ev_arg;
3171 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3174 REQUIRE(VALID_FCTX(fctx));
3178 FCTXTRACE("timeout");
3180 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3182 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3183 fctx->reason = NULL;
3184 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3186 isc_result_t result;
3189 fctx->timeout = ISC_TRUE;
3191 * We could cancel the running queries here, or we could let
3192 * them keep going. Since we normally use separate sockets for
3193 * different queries, we adopt the former approach to reduce
3194 * the number of open sockets: cancel the oldest query if it
3195 * expired after the query had started (this is usually the
3196 * case but is not always so, depending on the task schedule
3199 query = ISC_LIST_HEAD(fctx->queries);
3200 if (query != NULL &&
3201 isc_time_compare(&tevent->due, &query->start) >= 0) {
3202 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3204 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3206 * Our timer has triggered. Reestablish the fctx lifetime
3209 result = fctx_starttimer(fctx);
3210 if (result != ISC_R_SUCCESS)
3211 fctx_done(fctx, result, __LINE__);
3216 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3219 isc_event_free(&event);
3223 fctx_shutdown(fetchctx_t *fctx) {
3224 isc_event_t *cevent;
3227 * Start the shutdown process for fctx, if it isn't already underway.
3230 FCTXTRACE("shutdown");
3233 * The caller must be holding the appropriate bucket lock.
3236 if (fctx->want_shutdown)
3239 fctx->want_shutdown = ISC_TRUE;
3242 * Unless we're still initializing (in which case the
3243 * control event is still outstanding), we need to post
3244 * the control event to tell the fetch we want it to
3247 if (fctx->state != fetchstate_init) {
3248 cevent = &fctx->control_event;
3249 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3255 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3256 fetchctx_t *fctx = event->ev_arg;
3257 isc_boolean_t bucket_empty = ISC_FALSE;
3258 dns_resolver_t *res;
3259 unsigned int bucketnum;
3260 dns_validator_t *validator;
3262 REQUIRE(VALID_FCTX(fctx));
3267 bucketnum = fctx->bucketnum;
3269 FCTXTRACE("doshutdown");
3272 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3274 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3277 * Cancel all pending validators. Note that this must be done
3278 * without the bucket lock held, since that could cause deadlock.
3280 validator = ISC_LIST_HEAD(fctx->validators);
3281 while (validator != NULL) {
3282 dns_validator_cancel(validator);
3283 validator = ISC_LIST_NEXT(validator, link);
3286 if (fctx->nsfetch != NULL)
3287 dns_resolver_cancelfetch(fctx->nsfetch);
3290 * Shut down anything that is still running on behalf of this
3291 * fetch. To avoid deadlock with the ADB, we must do this
3292 * before we lock the bucket lock.
3294 fctx_stopeverything(fctx, ISC_FALSE);
3296 LOCK(&res->buckets[bucketnum].lock);
3298 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3300 INSIST(fctx->state == fetchstate_active ||
3301 fctx->state == fetchstate_done);
3302 INSIST(fctx->want_shutdown);
3304 if (fctx->state != fetchstate_done) {
3305 fctx->state = fetchstate_done;
3306 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3309 if (fctx->references == 0 && fctx->pending == 0 &&
3310 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3311 bucket_empty = fctx_destroy(fctx);
3313 UNLOCK(&res->buckets[bucketnum].lock);
3320 fctx_start(isc_task_t *task, isc_event_t *event) {
3321 fetchctx_t *fctx = event->ev_arg;
3322 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3323 dns_resolver_t *res;
3324 unsigned int bucketnum;
3326 REQUIRE(VALID_FCTX(fctx));
3331 bucketnum = fctx->bucketnum;
3335 LOCK(&res->buckets[bucketnum].lock);
3337 INSIST(fctx->state == fetchstate_init);
3338 if (fctx->want_shutdown) {
3340 * We haven't started this fctx yet, and we've been requested
3343 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3344 fctx->state = fetchstate_done;
3345 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3347 * Since we haven't started, we INSIST that we have no
3348 * pending ADB finds and no pending validations.
3350 INSIST(fctx->pending == 0);
3351 INSIST(fctx->nqueries == 0);
3352 INSIST(ISC_LIST_EMPTY(fctx->validators));
3353 if (fctx->references == 0) {
3355 * It's now safe to destroy this fctx.
3357 bucket_empty = fctx_destroy(fctx);
3362 * Normal fctx startup.
3364 fctx->state = fetchstate_active;
3366 * Reset the control event for later use in shutting down
3369 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3370 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3374 UNLOCK(&res->buckets[bucketnum].lock);
3377 isc_result_t result;
3380 * All is well. Start working on the fetch.
3382 result = fctx_starttimer(fctx);
3383 if (result != ISC_R_SUCCESS)
3384 fctx_done(fctx, result, __LINE__);
3386 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3387 } else if (bucket_empty)
3392 * Fetch Creation, Joining, and Cancelation.
3395 static inline isc_result_t
3396 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3397 dns_messageid_t id, isc_taskaction_t action, void *arg,
3398 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3402 dns_fetchevent_t *event;
3407 * We store the task we're going to send this event to in the
3408 * sender field. We'll make the fetch the sender when we actually
3412 isc_task_attach(task, &clone);
3413 event = (dns_fetchevent_t *)
3414 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3415 action, arg, sizeof(*event));
3416 if (event == NULL) {
3417 isc_task_detach(&clone);
3418 return (ISC_R_NOMEMORY);
3420 event->result = DNS_R_SERVFAIL;
3421 event->qtype = fctx->type;
3424 event->rdataset = rdataset;
3425 event->sigrdataset = sigrdataset;
3426 event->fetch = fetch;
3427 event->client = client;
3429 dns_fixedname_init(&event->foundname);
3432 * Make sure that we can store the sigrdataset in the
3433 * first event if it is needed by any of the events.
3435 if (event->sigrdataset != NULL)
3436 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3438 ISC_LIST_APPEND(fctx->events, event, ev_link);
3440 fctx->client = client;
3442 fetch->magic = DNS_FETCH_MAGIC;
3443 fetch->private = fctx;
3445 return (ISC_R_SUCCESS);
3449 log_ns_ttl(fetchctx_t *fctx, const char *where) {
3450 char namebuf[DNS_NAME_FORMATSIZE];
3451 char domainbuf[DNS_NAME_FORMATSIZE];
3453 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3454 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3455 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3456 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
3457 "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u",
3458 fctx, where, namebuf, domainbuf,
3459 fctx->ns_ttl_ok, fctx->ns_ttl);
3463 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3464 dns_name_t *domain, dns_rdataset_t *nameservers,
3465 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3468 isc_result_t result;
3469 isc_result_t iresult;
3470 isc_interval_t interval;
3471 dns_fixedname_t fixed;
3472 unsigned int findoptions = 0;
3473 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3474 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3478 * Caller must be holding the lock for bucket number 'bucketnum'.
3480 REQUIRE(fctxp != NULL && *fctxp == NULL);
3482 fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3484 return (ISC_R_NOMEMORY);
3485 dns_name_format(name, buf, sizeof(buf));
3486 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3487 strcat(buf, "/"); /* checked */
3488 strcat(buf, typebuf); /* checked */
3489 fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3490 if (fctx->info == NULL) {
3491 result = ISC_R_NOMEMORY;
3494 FCTXTRACE("create");
3495 dns_name_init(&fctx->name, NULL);
3496 result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3497 if (result != ISC_R_SUCCESS)
3499 dns_name_init(&fctx->domain, NULL);
3500 dns_rdataset_init(&fctx->nameservers);
3503 fctx->options = options;
3505 * Note! We do not attach to the task. We are relying on the
3506 * resolver to ensure that this task doesn't go away while we are
3510 fctx->references = 0;
3511 fctx->bucketnum = bucketnum;
3512 fctx->state = fetchstate_init;
3513 fctx->want_shutdown = ISC_FALSE;
3514 fctx->cloned = ISC_FALSE;
3515 ISC_LIST_INIT(fctx->queries);
3516 ISC_LIST_INIT(fctx->finds);
3517 ISC_LIST_INIT(fctx->altfinds);
3518 ISC_LIST_INIT(fctx->forwaddrs);
3519 ISC_LIST_INIT(fctx->altaddrs);
3520 ISC_LIST_INIT(fctx->forwarders);
3521 fctx->fwdpolicy = dns_fwdpolicy_none;
3522 ISC_LIST_INIT(fctx->bad);
3523 ISC_LIST_INIT(fctx->edns);
3524 ISC_LIST_INIT(fctx->edns512);
3525 ISC_LIST_INIT(fctx->bad_edns);
3526 ISC_LIST_INIT(fctx->validators);
3527 fctx->validator = NULL;
3529 fctx->altfind = NULL;
3532 fctx->querysent = 0;
3533 fctx->referrals = 0;
3534 TIME_NOW(&fctx->start);
3536 fctx->lamecount = 0;
3542 fctx->result = ISC_R_FAILURE;
3543 fctx->vresult = ISC_R_SUCCESS;
3544 fctx->exitline = -1; /* sentinel */
3545 fctx->logged = ISC_FALSE;
3546 fctx->attributes = 0;
3547 fctx->spilled = ISC_FALSE;
3549 fctx->reason = NULL;
3551 fctx->rand_bits = 0;
3552 fctx->timeout = ISC_FALSE;
3553 fctx->addrinfo = NULL;
3554 fctx->client = NULL;
3556 fctx->ns_ttl_ok = ISC_FALSE;
3558 dns_name_init(&fctx->nsname, NULL);
3559 fctx->nsfetch = NULL;
3560 dns_rdataset_init(&fctx->nsrrset);
3562 if (domain == NULL) {
3563 dns_forwarders_t *forwarders = NULL;
3564 unsigned int labels;
3565 dns_name_t *fwdname = name;
3568 * DS records are found in the parent server.
3569 * Strip label to get the correct forwarder (if any).
3571 if (dns_rdatatype_atparent(fctx->type) &&
3572 dns_name_countlabels(name) > 1) {
3573 dns_name_init(&suffix, NULL);
3574 labels = dns_name_countlabels(name);
3575 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3578 dns_fixedname_init(&fixed);
3579 domain = dns_fixedname_name(&fixed);
3580 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3581 domain, &forwarders);
3582 if (result == ISC_R_SUCCESS)
3583 fctx->fwdpolicy = forwarders->fwdpolicy;
3585 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3587 * The caller didn't supply a query domain and
3588 * nameservers, and we're not in forward-only mode,
3589 * so find the best nameservers to use.
3591 if (dns_rdatatype_atparent(fctx->type))
3592 findoptions |= DNS_DBFIND_NOEXACT;
3593 result = dns_view_findzonecut(res->view, name, domain,
3594 0, findoptions, ISC_TRUE,
3597 if (result != ISC_R_SUCCESS)
3599 result = dns_name_dup(domain,
3600 res->buckets[bucketnum].mctx,
3602 if (result != ISC_R_SUCCESS) {
3603 dns_rdataset_disassociate(&fctx->nameservers);
3606 fctx->ns_ttl = fctx->nameservers.ttl;
3607 fctx->ns_ttl_ok = ISC_TRUE;
3610 * We're in forward-only mode. Set the query domain.
3612 result = dns_name_dup(domain,
3613 res->buckets[bucketnum].mctx,
3615 if (result != ISC_R_SUCCESS)
3619 result = dns_name_dup(domain,
3620 res->buckets[bucketnum].mctx,
3622 if (result != ISC_R_SUCCESS)
3624 dns_rdataset_clone(nameservers, &fctx->nameservers);
3625 fctx->ns_ttl = fctx->nameservers.ttl;
3626 fctx->ns_ttl_ok = ISC_TRUE;
3629 log_ns_ttl(fctx, "fctx_create");
3631 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3633 fctx->qmessage = NULL;
3634 result = dns_message_create(res->buckets[bucketnum].mctx,
3635 DNS_MESSAGE_INTENTRENDER,
3638 if (result != ISC_R_SUCCESS)
3639 goto cleanup_domain;
3641 fctx->rmessage = NULL;
3642 result = dns_message_create(res->buckets[bucketnum].mctx,
3643 DNS_MESSAGE_INTENTPARSE,
3646 if (result != ISC_R_SUCCESS)
3647 goto cleanup_qmessage;
3650 * Compute an expiration time for the entire fetch.
3652 isc_interval_set(&interval, res->query_timeout, 0);
3653 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3654 if (iresult != ISC_R_SUCCESS) {
3655 UNEXPECTED_ERROR(__FILE__, __LINE__,
3656 "isc_time_nowplusinterval: %s",
3657 isc_result_totext(iresult));
3658 result = ISC_R_UNEXPECTED;
3659 goto cleanup_rmessage;
3663 * Default retry interval initialization. We set the interval now
3664 * mostly so it won't be uninitialized. It will be set to the
3665 * correct value before a query is issued.
3667 isc_interval_set(&fctx->interval, 2, 0);
3670 * Create an inactive timer. It will be made active when the fetch
3671 * is actually started.
3674 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3676 res->buckets[bucketnum].task, fctx_timeout,
3677 fctx, &fctx->timer);
3678 if (iresult != ISC_R_SUCCESS) {
3679 UNEXPECTED_ERROR(__FILE__, __LINE__,
3680 "isc_timer_create: %s",
3681 isc_result_totext(iresult));
3682 result = ISC_R_UNEXPECTED;
3683 goto cleanup_rmessage;
3687 * Attach to the view's cache and adb.
3690 dns_db_attach(res->view->cachedb, &fctx->cache);
3692 dns_adb_attach(res->view->adb, &fctx->adb);
3694 ISC_LIST_INIT(fctx->events);
3695 ISC_LINK_INIT(fctx, link);
3696 fctx->magic = FCTX_MAGIC;
3698 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3702 UNLOCK(&res->nlock);
3706 return (ISC_R_SUCCESS);
3709 dns_message_destroy(&fctx->rmessage);
3712 dns_message_destroy(&fctx->qmessage);
3715 if (dns_name_countlabels(&fctx->domain) > 0)
3716 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3717 if (dns_rdataset_isassociated(&fctx->nameservers))
3718 dns_rdataset_disassociate(&fctx->nameservers);
3721 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3724 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3727 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3735 static inline isc_boolean_t
3736 is_lame(fetchctx_t *fctx) {
3737 dns_message_t *message = fctx->rmessage;
3739 dns_rdataset_t *rdataset;
3740 isc_result_t result;
3742 if (message->rcode != dns_rcode_noerror &&
3743 message->rcode != dns_rcode_nxdomain)
3746 if (message->counts[DNS_SECTION_ANSWER] != 0)
3749 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3752 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3753 while (result == ISC_R_SUCCESS) {
3755 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3756 for (rdataset = ISC_LIST_HEAD(name->list);
3758 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3759 dns_namereln_t namereln;
3761 unsigned int labels;
3762 if (rdataset->type != dns_rdatatype_ns)
3764 namereln = dns_name_fullcompare(name, &fctx->domain,
3766 if (namereln == dns_namereln_equal &&
3767 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3769 if (namereln == dns_namereln_subdomain)
3773 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3780 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3781 char namebuf[DNS_NAME_FORMATSIZE];
3782 char domainbuf[DNS_NAME_FORMATSIZE];
3783 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3785 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3786 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3787 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3788 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3789 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3790 "lame server resolving '%s' (in '%s'?): %s",
3791 namebuf, domainbuf, addrbuf);
3795 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3796 char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3797 char clbuf[ISC_SOCKADDR_FORMATSIZE];
3798 const char *clmsg = "";
3802 va_start(args, format);
3803 vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3806 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3808 if (fctx->client != NULL) {
3809 clmsg = " for client ";
3810 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3815 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3816 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3817 "DNS format error from %s resolving %s%s%s: %s",
3818 nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3821 static inline isc_result_t
3822 same_question(fetchctx_t *fctx) {
3823 isc_result_t result;
3824 dns_message_t *message = fctx->rmessage;
3826 dns_rdataset_t *rdataset;
3829 * Caller must be holding the fctx lock.
3833 * XXXRTH Currently we support only one question.
3835 if (message->counts[DNS_SECTION_QUESTION] != 1) {
3836 log_formerr(fctx, "too many questions");
3837 return (DNS_R_FORMERR);
3840 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3841 if (result != ISC_R_SUCCESS)
3844 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3845 rdataset = ISC_LIST_HEAD(name->list);
3846 INSIST(rdataset != NULL);
3847 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3849 if (fctx->type != rdataset->type ||
3850 fctx->res->rdclass != rdataset->rdclass ||
3851 !dns_name_equal(&fctx->name, name)) {
3852 char namebuf[DNS_NAME_FORMATSIZE];
3853 char class[DNS_RDATACLASS_FORMATSIZE];
3854 char type[DNS_RDATATYPE_FORMATSIZE];
3856 dns_name_format(name, namebuf, sizeof(namebuf));
3857 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3858 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3859 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3860 namebuf, class, type);
3861 return (DNS_R_FORMERR);
3864 return (ISC_R_SUCCESS);
3868 clone_results(fetchctx_t *fctx) {
3869 dns_fetchevent_t *event, *hevent;
3870 isc_result_t result;
3871 dns_name_t *name, *hname;
3873 FCTXTRACE("clone_results");
3876 * Set up any other events to have the same data as the first
3879 * Caller must be holding the appropriate lock.
3882 fctx->cloned = ISC_TRUE;
3883 hevent = ISC_LIST_HEAD(fctx->events);
3886 hname = dns_fixedname_name(&hevent->foundname);
3887 for (event = ISC_LIST_NEXT(hevent, ev_link);
3889 event = ISC_LIST_NEXT(event, ev_link)) {
3890 name = dns_fixedname_name(&event->foundname);
3891 result = dns_name_copy(hname, name, NULL);
3892 if (result != ISC_R_SUCCESS)
3893 event->result = result;
3895 event->result = hevent->result;
3896 dns_db_attach(hevent->db, &event->db);
3897 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3898 INSIST(hevent->rdataset != NULL);
3899 INSIST(event->rdataset != NULL);
3900 if (dns_rdataset_isassociated(hevent->rdataset))
3901 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3902 INSIST(! (hevent->sigrdataset == NULL &&
3903 event->sigrdataset != NULL));
3904 if (hevent->sigrdataset != NULL &&
3905 dns_rdataset_isassociated(hevent->sigrdataset) &&
3906 event->sigrdataset != NULL)
3907 dns_rdataset_clone(hevent->sigrdataset,
3908 event->sigrdataset);
3912 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3913 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3914 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3915 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3916 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3917 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3918 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3922 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3923 * no references and is no longer waiting for any events).
3926 * '*fctx' is shutting down.
3929 * true if the resolver is exiting and this is the last fctx in the bucket.
3931 static isc_boolean_t
3932 maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) {
3933 unsigned int bucketnum;
3934 isc_boolean_t bucket_empty = ISC_FALSE;
3935 dns_resolver_t *res = fctx->res;
3936 dns_validator_t *validator, *next_validator;
3938 REQUIRE(SHUTTINGDOWN(fctx));
3940 bucketnum = fctx->bucketnum;
3942 LOCK(&res->buckets[bucketnum].lock);
3943 if (fctx->pending != 0 || fctx->nqueries != 0)
3946 for (validator = ISC_LIST_HEAD(fctx->validators);
3947 validator != NULL; validator = next_validator) {
3948 next_validator = ISC_LIST_NEXT(validator, link);
3949 dns_validator_cancel(validator);
3952 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3953 bucket_empty = fctx_destroy(fctx);
3956 UNLOCK(&res->buckets[bucketnum].lock);
3957 return (bucket_empty);
3961 * The validator has finished.
3964 validated(isc_task_t *task, isc_event_t *event) {
3965 dns_adbaddrinfo_t *addrinfo;
3966 dns_dbnode_t *node = NULL;
3967 dns_dbnode_t *nsnode = NULL;
3968 dns_fetchevent_t *hevent;
3970 dns_rdataset_t *ardataset = NULL;
3971 dns_rdataset_t *asigrdataset = NULL;
3972 dns_rdataset_t *rdataset;
3973 dns_rdataset_t *sigrdataset;
3974 dns_resolver_t *res;
3975 dns_valarg_t *valarg;
3976 dns_validatorevent_t *vevent;
3978 isc_boolean_t chaining;
3979 isc_boolean_t negative;
3980 isc_boolean_t sentresponse;
3981 isc_result_t eresult = ISC_R_SUCCESS;
3982 isc_result_t result = ISC_R_SUCCESS;
3986 UNUSED(task); /* for now */
3988 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3989 valarg = event->ev_arg;
3990 fctx = valarg->fctx;
3992 addrinfo = valarg->addrinfo;
3993 REQUIRE(VALID_FCTX(fctx));
3994 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3996 vevent = (dns_validatorevent_t *)event;
3997 fctx->vresult = vevent->result;
3999 FCTXTRACE("received validation completion event");
4001 LOCK(&res->buckets[fctx->bucketnum].lock);
4003 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
4004 fctx->validator = NULL;
4007 * Destroy the validator early so that we can
4008 * destroy the fctx if necessary.
4010 dns_validator_destroy(&vevent->validator);
4011 isc_mem_put(res->buckets[fctx->bucketnum].mctx,
4012 valarg, sizeof(*valarg));
4014 negative = ISC_TF(vevent->rdataset == NULL);
4016 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
4019 * If shutting down, ignore the results. Check to see if we're
4020 * done waiting for validator completions and ADB pending events; if
4021 * so, destroy the fctx.
4023 if (SHUTTINGDOWN(fctx) && !sentresponse) {
4024 isc_uint32_t bucketnum = fctx->bucketnum;
4025 isc_boolean_t bucket_empty;
4026 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4027 UNLOCK(&res->buckets[bucketnum].lock);
4033 isc_stdtime_get(&now);
4036 * If chaining, we need to make sure that the right result code is
4037 * returned, and that the rdatasets are bound.
4039 if (vevent->result == ISC_R_SUCCESS &&
4041 vevent->rdataset != NULL &&
4042 CHAINING(vevent->rdataset))
4044 if (vevent->rdataset->type == dns_rdatatype_cname)
4045 eresult = DNS_R_CNAME;
4047 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4048 eresult = DNS_R_DNAME;
4050 chaining = ISC_TRUE;
4052 chaining = ISC_FALSE;
4055 * Either we're not shutting down, or we are shutting down but want
4056 * to cache the result anyway (if this was a validation started by
4057 * a query with cd set)
4060 hevent = ISC_LIST_HEAD(fctx->events);
4061 if (hevent != NULL) {
4062 if (!negative && !chaining &&
4063 (fctx->type == dns_rdatatype_any ||
4064 fctx->type == dns_rdatatype_rrsig ||
4065 fctx->type == dns_rdatatype_sig)) {
4067 * Don't bind rdatasets; the caller
4068 * will iterate the node.
4071 ardataset = hevent->rdataset;
4072 asigrdataset = hevent->sigrdataset;
4076 if (vevent->result != ISC_R_SUCCESS) {
4077 FCTXTRACE("validation failed");
4078 inc_stats(res, dns_resstatscounter_valfail);
4080 fctx->vresult = vevent->result;
4081 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4082 result = ISC_R_NOTFOUND;
4083 if (vevent->rdataset != NULL)
4084 result = dns_db_findnode(fctx->cache,
4087 if (result == ISC_R_SUCCESS)
4088 (void)dns_db_deleterdataset(fctx->cache, node,
4091 if (result == ISC_R_SUCCESS &&
4092 vevent->sigrdataset != NULL)
4093 (void)dns_db_deleterdataset(fctx->cache, node,
4095 dns_rdatatype_rrsig,
4097 if (result == ISC_R_SUCCESS)
4098 dns_db_detachnode(fctx->cache, &node);
4100 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4102 * Cache the data as pending for later validation.
4104 result = ISC_R_NOTFOUND;
4105 if (vevent->rdataset != NULL)
4106 result = dns_db_findnode(fctx->cache,
4109 if (result == ISC_R_SUCCESS) {
4110 (void)dns_db_addrdataset(fctx->cache, node,
4112 vevent->rdataset, 0,
4115 if (result == ISC_R_SUCCESS &&
4116 vevent->sigrdataset != NULL)
4117 (void)dns_db_addrdataset(fctx->cache, node,
4119 vevent->sigrdataset,
4121 if (result == ISC_R_SUCCESS)
4122 dns_db_detachnode(fctx->cache, &node);
4124 result = fctx->vresult;
4125 add_bad(fctx, addrinfo, result, badns_validation);
4126 isc_event_free(&event);
4127 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4128 INSIST(fctx->validator == NULL);
4129 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4130 if (fctx->validator != NULL)
4131 dns_validator_send(fctx->validator);
4132 else if (sentresponse)
4133 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4134 else if (result == DNS_R_BROKENCHAIN) {
4135 isc_result_t tresult;
4139 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4140 tresult = isc_time_nowplusinterval(&expire, &i);
4142 (fctx->type == dns_rdatatype_dnskey ||
4143 fctx->type == dns_rdatatype_dlv ||
4144 fctx->type == dns_rdatatype_ds) &&
4145 tresult == ISC_R_SUCCESS)
4146 dns_resolver_addbadcache(res, &fctx->name,
4147 fctx->type, &expire);
4148 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4150 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4156 dns_rdatatype_t covers;
4157 FCTXTRACE("nonexistence validation OK");
4159 inc_stats(res, dns_resstatscounter_valnegsuccess);
4161 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4162 covers = dns_rdatatype_any;
4164 covers = fctx->type;
4166 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4168 if (result != ISC_R_SUCCESS)
4169 goto noanswer_response;
4172 * If we are asking for a SOA record set the cache time
4173 * to zero to facilitate locating the containing zone of
4176 ttl = res->view->maxncachettl;
4177 if (fctx->type == dns_rdatatype_soa &&
4178 covers == dns_rdatatype_any && res->zero_no_soa_ttl)
4181 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4182 covers, now, ttl, vevent->optout,
4183 ardataset, &eresult);
4184 if (result != ISC_R_SUCCESS)
4185 goto noanswer_response;
4186 goto answer_response;
4188 inc_stats(res, dns_resstatscounter_valsuccess);
4190 FCTXTRACE("validation OK");
4192 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4194 result = dns_rdataset_addnoqname(vevent->rdataset,
4195 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4196 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4197 INSIST(vevent->sigrdataset != NULL);
4198 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4199 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4200 result = dns_rdataset_addclosest(vevent->rdataset,
4201 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4202 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4207 * The data was already cached as pending data.
4208 * Re-cache it as secure and bind the cached
4209 * rdatasets to the first event on the fetch
4212 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4213 if (result != ISC_R_SUCCESS)
4214 goto noanswer_response;
4216 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4217 vevent->rdataset, 0, ardataset);
4218 if (result != ISC_R_SUCCESS &&
4219 result != DNS_R_UNCHANGED)
4220 goto noanswer_response;
4221 if (ardataset != NULL && NEGATIVE(ardataset)) {
4222 if (NXDOMAIN(ardataset))
4223 eresult = DNS_R_NCACHENXDOMAIN;
4225 eresult = DNS_R_NCACHENXRRSET;
4226 } else if (vevent->sigrdataset != NULL) {
4227 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4228 vevent->sigrdataset, 0,
4230 if (result != ISC_R_SUCCESS &&
4231 result != DNS_R_UNCHANGED)
4232 goto noanswer_response;
4236 isc_boolean_t bucket_empty = ISC_FALSE;
4238 * If we only deferred the destroy because we wanted to cache
4239 * the data, destroy now.
4241 dns_db_detachnode(fctx->cache, &node);
4242 if (SHUTTINGDOWN(fctx))
4243 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4244 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4250 if (!ISC_LIST_EMPTY(fctx->validators)) {
4252 INSIST(fctx->type == dns_rdatatype_any ||
4253 fctx->type == dns_rdatatype_rrsig ||
4254 fctx->type == dns_rdatatype_sig);
4256 * Don't send a response yet - we have
4257 * more rdatasets that still need to
4260 dns_db_detachnode(fctx->cache, &node);
4261 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4262 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4268 * Cache any NS/NSEC records that happened to be validated.
4270 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4271 while (result == ISC_R_SUCCESS) {
4273 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4275 for (rdataset = ISC_LIST_HEAD(name->list);
4277 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4278 if ((rdataset->type != dns_rdatatype_ns &&
4279 rdataset->type != dns_rdatatype_nsec) ||
4280 rdataset->trust != dns_trust_secure)
4282 for (sigrdataset = ISC_LIST_HEAD(name->list);
4283 sigrdataset != NULL;
4284 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4285 if (sigrdataset->type != dns_rdatatype_rrsig ||
4286 sigrdataset->covers != rdataset->type)
4290 if (sigrdataset == NULL ||
4291 sigrdataset->trust != dns_trust_secure)
4293 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4295 if (result != ISC_R_SUCCESS)
4298 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4299 now, rdataset, 0, NULL);
4300 if (result == ISC_R_SUCCESS)
4301 result = dns_db_addrdataset(fctx->cache, nsnode,
4305 dns_db_detachnode(fctx->cache, &nsnode);
4306 if (result != ISC_R_SUCCESS)
4309 result = dns_message_nextname(fctx->rmessage,
4310 DNS_SECTION_AUTHORITY);
4313 result = ISC_R_SUCCESS;
4316 * Respond with an answer, positive or negative,
4317 * as opposed to an error. 'node' must be non-NULL.
4320 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4322 if (hevent != NULL) {
4323 hevent->result = eresult;
4324 RUNTIME_CHECK(dns_name_copy(vevent->name,
4325 dns_fixedname_name(&hevent->foundname), NULL)
4327 dns_db_attach(fctx->cache, &hevent->db);
4328 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4329 clone_results(fctx);
4334 dns_db_detachnode(fctx->cache, &node);
4336 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4337 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4340 INSIST(node == NULL);
4341 isc_event_free(&event);
4344 static inline isc_result_t
4345 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4348 dns_rdataset_t *rdataset, *sigrdataset;
4349 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4350 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4351 dns_dbnode_t *node, **anodep;
4354 dns_resolver_t *res;
4355 isc_boolean_t need_validation, secure_domain, have_answer;
4356 isc_result_t result, eresult;
4357 dns_fetchevent_t *event;
4358 unsigned int options;
4361 unsigned int valoptions = 0;
4364 * The appropriate bucket lock must be held.
4368 need_validation = ISC_FALSE;
4369 POST(need_validation);
4370 secure_domain = ISC_FALSE;
4371 have_answer = ISC_FALSE;
4372 eresult = ISC_R_SUCCESS;
4373 task = res->buckets[fctx->bucketnum].task;
4376 * Is DNSSEC validation required for this name?
4378 if (res->view->enablevalidation) {
4379 result = dns_view_issecuredomain(res->view, name,
4381 if (result != ISC_R_SUCCESS)
4384 if (!secure_domain && res->view->dlv != NULL) {
4385 valoptions = DNS_VALIDATOR_DLV;
4386 secure_domain = ISC_TRUE;
4390 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4391 need_validation = ISC_FALSE;
4393 need_validation = secure_domain;
4399 asigrdataset = NULL;
4401 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4403 have_answer = ISC_TRUE;
4404 event = ISC_LIST_HEAD(fctx->events);
4405 if (event != NULL) {
4407 aname = dns_fixedname_name(&event->foundname);
4408 result = dns_name_copy(name, aname, NULL);
4409 if (result != ISC_R_SUCCESS)
4411 anodep = &event->node;
4413 * If this is an ANY, SIG or RRSIG query, we're not
4414 * going to return any rdatasets, unless we encountered
4415 * a CNAME or DNAME as "the answer". In this case,
4416 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4417 * and we must set up the rdatasets.
4419 if ((fctx->type != dns_rdatatype_any &&
4420 fctx->type != dns_rdatatype_rrsig &&
4421 fctx->type != dns_rdatatype_sig) ||
4422 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4423 ardataset = event->rdataset;
4424 asigrdataset = event->sigrdataset;
4430 * Find or create the cache node.
4433 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4434 if (result != ISC_R_SUCCESS)
4438 * Cache or validate each cacheable rdataset.
4440 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4441 for (rdataset = ISC_LIST_HEAD(name->list);
4443 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4444 if (!CACHE(rdataset))
4446 if (CHECKNAMES(rdataset)) {
4447 char namebuf[DNS_NAME_FORMATSIZE];
4448 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4449 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4451 dns_name_format(name, namebuf, sizeof(namebuf));
4452 dns_rdatatype_format(rdataset->type, typebuf,
4454 dns_rdataclass_format(rdataset->rdclass, classbuf,
4456 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4457 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4458 "check-names %s %s/%s/%s",
4459 fail ? "failure" : "warning",
4460 namebuf, typebuf, classbuf);
4462 if (ANSWER(rdataset)) {
4463 dns_db_detachnode(fctx->cache, &node);
4464 return (DNS_R_BADNAME);
4471 * Enforce the configure maximum cache TTL.
4473 if (rdataset->ttl > res->view->maxcachettl)
4474 rdataset->ttl = res->view->maxcachettl;
4477 * If this RRset is in a secure domain, is in bailiwick,
4478 * and is not glue, attempt DNSSEC validation. (We do not
4479 * attempt to validate glue or out-of-bailiwick data--even
4480 * though there might be some performance benefit to doing
4481 * so--because it makes it simpler and safer to ensure that
4482 * records from a secure domain are only cached if validated
4483 * within the context of a query to the domain that owns
4486 if (secure_domain && rdataset->trust != dns_trust_glue &&
4487 !EXTERNAL(rdataset)) {
4491 * RRSIGs are validated as part of validating the
4494 if (rdataset->type == dns_rdatatype_rrsig)
4497 * Find the SIG for this rdataset, if we have it.
4499 for (sigrdataset = ISC_LIST_HEAD(name->list);
4500 sigrdataset != NULL;
4501 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4502 if (sigrdataset->type == dns_rdatatype_rrsig &&
4503 sigrdataset->covers == rdataset->type)
4506 if (sigrdataset == NULL) {
4507 if (!ANSWER(rdataset) && need_validation) {
4509 * Ignore non-answer rdatasets that
4510 * are missing signatures.
4517 * Normalize the rdataset and sigrdataset TTLs.
4519 if (sigrdataset != NULL) {
4520 rdataset->ttl = ISC_MIN(rdataset->ttl,
4522 sigrdataset->ttl = rdataset->ttl;
4526 * Cache this rdataset/sigrdataset pair as
4527 * pending data. Track whether it was additional
4530 if (rdataset->trust == dns_trust_additional)
4531 trust = dns_trust_pending_additional;
4533 trust = dns_trust_pending_answer;
4535 rdataset->trust = trust;
4536 if (sigrdataset != NULL)
4537 sigrdataset->trust = trust;
4538 if (!need_validation || !ANSWER(rdataset)) {
4539 addedrdataset = ardataset;
4540 result = dns_db_addrdataset(fctx->cache, node,
4541 NULL, now, rdataset,
4543 if (result == DNS_R_UNCHANGED) {
4544 result = ISC_R_SUCCESS;
4545 if (!need_validation &&
4546 ardataset != NULL &&
4547 NEGATIVE(ardataset)) {
4549 * The answer in the cache is
4550 * better than the answer we
4551 * found, and is a negative
4552 * cache entry, so we must set
4553 * eresult appropriately.
4555 if (NXDOMAIN(ardataset))
4557 DNS_R_NCACHENXDOMAIN;
4560 DNS_R_NCACHENXRRSET;
4562 * We have a negative response
4563 * from the cache so don't
4564 * attempt to add the RRSIG
4570 if (result != ISC_R_SUCCESS)
4572 if (sigrdataset != NULL) {
4573 addedrdataset = asigrdataset;
4574 result = dns_db_addrdataset(fctx->cache,
4578 if (result == DNS_R_UNCHANGED)
4579 result = ISC_R_SUCCESS;
4580 if (result != ISC_R_SUCCESS)
4582 } else if (!ANSWER(rdataset))
4586 if (ANSWER(rdataset) && need_validation) {
4587 if (fctx->type != dns_rdatatype_any &&
4588 fctx->type != dns_rdatatype_rrsig &&
4589 fctx->type != dns_rdatatype_sig) {
4591 * This is The Answer. We will
4592 * validate it, but first we cache
4593 * the rest of the response - it may
4594 * contain useful keys.
4596 INSIST(valrdataset == NULL &&
4597 valsigrdataset == NULL);
4598 valrdataset = rdataset;
4599 valsigrdataset = sigrdataset;
4602 * This is one of (potentially)
4603 * multiple answers to an ANY
4604 * or SIG query. To keep things
4605 * simple, we just start the
4606 * validator right away rather
4607 * than caching first and
4608 * having to remember which
4609 * rdatasets needed validation.
4611 result = valcreate(fctx, addrinfo,
4612 name, rdataset->type,
4617 * Defer any further validations.
4618 * This prevents multiple validators
4619 * from manipulating fctx->rmessage
4622 valoptions |= DNS_VALIDATOR_DEFER;
4624 } else if (CHAINING(rdataset)) {
4625 if (rdataset->type == dns_rdatatype_cname)
4626 eresult = DNS_R_CNAME;
4628 INSIST(rdataset->type ==
4629 dns_rdatatype_dname);
4630 eresult = DNS_R_DNAME;
4633 } else if (!EXTERNAL(rdataset)) {
4635 * It's OK to cache this rdataset now.
4637 if (ANSWER(rdataset))
4638 addedrdataset = ardataset;
4639 else if (ANSWERSIG(rdataset))
4640 addedrdataset = asigrdataset;
4642 addedrdataset = NULL;
4643 if (CHAINING(rdataset)) {
4644 if (rdataset->type == dns_rdatatype_cname)
4645 eresult = DNS_R_CNAME;
4647 INSIST(rdataset->type ==
4648 dns_rdatatype_dname);
4649 eresult = DNS_R_DNAME;
4652 if (rdataset->trust == dns_trust_glue &&
4653 (rdataset->type == dns_rdatatype_ns ||
4654 (rdataset->type == dns_rdatatype_rrsig &&
4655 rdataset->covers == dns_rdatatype_ns))) {
4657 * If the trust level is 'dns_trust_glue'
4658 * then we are adding data from a referral
4659 * we got while executing the search algorithm.
4660 * New referral data always takes precedence
4661 * over the existing cache contents.
4663 options = DNS_DBADD_FORCE;
4667 * Now we can add the rdataset.
4669 result = dns_db_addrdataset(fctx->cache,
4674 if (result == DNS_R_UNCHANGED) {
4675 if (ANSWER(rdataset) &&
4676 ardataset != NULL &&
4677 NEGATIVE(ardataset)) {
4679 * The answer in the cache is better
4680 * than the answer we found, and is
4681 * a negative cache entry, so we
4682 * must set eresult appropriately.
4684 if (NXDOMAIN(ardataset))
4685 eresult = DNS_R_NCACHENXDOMAIN;
4687 eresult = DNS_R_NCACHENXRRSET;
4689 result = ISC_R_SUCCESS;
4690 } else if (result != ISC_R_SUCCESS)
4695 if (valrdataset != NULL)
4696 result = valcreate(fctx, addrinfo, name, fctx->type,
4697 valrdataset, valsigrdataset, valoptions,
4700 if (result == ISC_R_SUCCESS && have_answer) {
4701 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4702 if (event != NULL) {
4704 * Negative results must be indicated in event->result.
4706 if (dns_rdataset_isassociated(event->rdataset) &&
4707 NEGATIVE(event->rdataset)) {
4708 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4709 eresult == DNS_R_NCACHENXRRSET);
4711 event->result = eresult;
4712 dns_db_attach(fctx->cache, adbp);
4713 dns_db_transfernode(fctx->cache, &node, anodep);
4714 clone_results(fctx);
4719 dns_db_detachnode(fctx->cache, &node);
4724 static inline isc_result_t
4725 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4727 isc_result_t result;
4728 dns_section_t section;
4731 FCTXTRACE("cache_message");
4733 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4735 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4737 for (section = DNS_SECTION_ANSWER;
4738 section <= DNS_SECTION_ADDITIONAL;
4740 result = dns_message_firstname(fctx->rmessage, section);
4741 while (result == ISC_R_SUCCESS) {
4743 dns_message_currentname(fctx->rmessage, section,
4745 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4746 result = cache_name(fctx, name, addrinfo, now);
4747 if (result != ISC_R_SUCCESS)
4750 result = dns_message_nextname(fctx->rmessage, section);
4752 if (result != ISC_R_NOMORE)
4755 if (result == ISC_R_NOMORE)
4756 result = ISC_R_SUCCESS;
4758 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4764 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4767 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4768 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4769 isc_boolean_t optout, dns_rdataset_t *ardataset,
4770 isc_result_t *eresultp)
4772 isc_result_t result;
4773 dns_rdataset_t rdataset;
4775 if (ardataset == NULL) {
4776 dns_rdataset_init(&rdataset);
4777 ardataset = &rdataset;
4779 result = dns_ncache_addoptout(message, cache, node, covers, now,
4780 maxttl, optout, ardataset);
4781 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4783 * If the cache now contains a negative entry and we
4784 * care about whether it is DNS_R_NCACHENXDOMAIN or
4785 * DNS_R_NCACHENXRRSET then extract it.
4787 if (NEGATIVE(ardataset)) {
4789 * The cache data is a negative cache entry.
4791 if (NXDOMAIN(ardataset))
4792 *eresultp = DNS_R_NCACHENXDOMAIN;
4794 *eresultp = DNS_R_NCACHENXRRSET;
4797 * Either we don't care about the nature of the
4798 * cache rdataset (because no fetch is interested
4799 * in the outcome), or the cache rdataset is not
4800 * a negative cache entry. Whichever case it is,
4801 * we can return success.
4803 * XXXRTH There's a CNAME/DNAME problem here.
4805 *eresultp = ISC_R_SUCCESS;
4807 result = ISC_R_SUCCESS;
4809 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4810 dns_rdataset_disassociate(ardataset);
4815 static inline isc_result_t
4816 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4817 dns_rdatatype_t covers, isc_stdtime_t now)
4819 isc_result_t result, eresult;
4821 dns_resolver_t *res;
4823 dns_dbnode_t *node, **anodep;
4824 dns_rdataset_t *ardataset;
4825 isc_boolean_t need_validation, secure_domain;
4827 dns_fetchevent_t *event;
4829 unsigned int valoptions = 0;
4831 FCTXTRACE("ncache_message");
4833 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4836 need_validation = ISC_FALSE;
4837 POST(need_validation);
4838 secure_domain = ISC_FALSE;
4839 eresult = ISC_R_SUCCESS;
4844 * XXXMPA remove when we follow cnames and adjust the setting
4845 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4847 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4850 * Is DNSSEC validation required for this name?
4852 if (fctx->res->view->enablevalidation) {
4853 result = dns_view_issecuredomain(res->view, name,
4855 if (result != ISC_R_SUCCESS)
4858 if (!secure_domain && res->view->dlv != NULL) {
4859 valoptions = DNS_VALIDATOR_DLV;
4860 secure_domain = ISC_TRUE;
4864 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4865 need_validation = ISC_FALSE;
4867 need_validation = secure_domain;
4869 if (secure_domain) {
4871 * Mark all rdatasets as pending.
4873 dns_rdataset_t *trdataset;
4876 result = dns_message_firstname(fctx->rmessage,
4877 DNS_SECTION_AUTHORITY);
4878 while (result == ISC_R_SUCCESS) {
4880 dns_message_currentname(fctx->rmessage,
4881 DNS_SECTION_AUTHORITY,
4883 for (trdataset = ISC_LIST_HEAD(tname->list);
4885 trdataset = ISC_LIST_NEXT(trdataset, link))
4886 trdataset->trust = dns_trust_pending_answer;
4887 result = dns_message_nextname(fctx->rmessage,
4888 DNS_SECTION_AUTHORITY);
4890 if (result != ISC_R_NOMORE)
4895 if (need_validation) {
4897 * Do negative response validation.
4899 result = valcreate(fctx, addrinfo, name, fctx->type,
4900 NULL, NULL, valoptions,
4901 res->buckets[fctx->bucketnum].task);
4903 * If validation is necessary, return now. Otherwise continue
4904 * to process the message, letting the validation complete
4905 * in its own good time.
4910 LOCK(&res->buckets[fctx->bucketnum].lock);
4916 if (!HAVE_ANSWER(fctx)) {
4917 event = ISC_LIST_HEAD(fctx->events);
4918 if (event != NULL) {
4920 aname = dns_fixedname_name(&event->foundname);
4921 result = dns_name_copy(name, aname, NULL);
4922 if (result != ISC_R_SUCCESS)
4924 anodep = &event->node;
4925 ardataset = event->rdataset;
4930 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4931 if (result != ISC_R_SUCCESS)
4935 * If we are asking for a SOA record set the cache time
4936 * to zero to facilitate locating the containing zone of
4939 ttl = fctx->res->view->maxncachettl;
4940 if (fctx->type == dns_rdatatype_soa &&
4941 covers == dns_rdatatype_any &&
4942 fctx->res->zero_no_soa_ttl)
4945 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4946 covers, now, ttl, ISC_FALSE,
4947 ardataset, &eresult);
4948 if (result != ISC_R_SUCCESS)
4951 if (!HAVE_ANSWER(fctx)) {
4952 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4953 if (event != NULL) {
4954 event->result = eresult;
4955 dns_db_attach(fctx->cache, adbp);
4956 dns_db_transfernode(fctx->cache, &node, anodep);
4957 clone_results(fctx);
4962 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4965 dns_db_detachnode(fctx->cache, &node);
4971 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4972 isc_boolean_t external, isc_boolean_t gluing)
4974 name->attributes |= DNS_NAMEATTR_CACHE;
4976 rdataset->trust = dns_trust_glue;
4978 * Glue with 0 TTL causes problems. We force the TTL to
4979 * 1 second to prevent this.
4981 if (rdataset->ttl == 0)
4984 rdataset->trust = dns_trust_additional;
4986 * Avoid infinite loops by only marking new rdatasets.
4988 if (!CACHE(rdataset)) {
4989 name->attributes |= DNS_NAMEATTR_CHASE;
4990 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4992 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4994 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4998 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
4999 dns_section_t section)
5001 fetchctx_t *fctx = arg;
5002 isc_result_t result;
5004 dns_rdataset_t *rdataset;
5005 isc_boolean_t external;
5006 dns_rdatatype_t rtype;
5007 isc_boolean_t gluing;
5009 REQUIRE(VALID_FCTX(fctx));
5011 #if CHECK_FOR_GLUE_IN_ANSWER
5012 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
5013 return (ISC_R_SUCCESS);
5022 result = dns_message_findname(fctx->rmessage, section, addname,
5023 dns_rdatatype_any, 0, &name, NULL);
5024 if (result == ISC_R_SUCCESS) {
5025 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5026 if (type == dns_rdatatype_a) {
5027 for (rdataset = ISC_LIST_HEAD(name->list);
5029 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5030 if (rdataset->type == dns_rdatatype_rrsig)
5031 rtype = rdataset->covers;
5033 rtype = rdataset->type;
5034 if (rtype == dns_rdatatype_a ||
5035 rtype == dns_rdatatype_aaaa)
5036 mark_related(name, rdataset, external,
5040 result = dns_message_findtype(name, type, 0,
5042 if (result == ISC_R_SUCCESS) {
5043 mark_related(name, rdataset, external, gluing);
5045 * Do we have its SIG too?
5048 result = dns_message_findtype(name,
5049 dns_rdatatype_rrsig,
5051 if (result == ISC_R_SUCCESS)
5052 mark_related(name, rdataset, external,
5058 return (ISC_R_SUCCESS);
5062 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5063 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5066 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5067 #define CHECK_FOR_GLUE_IN_ANSWER 0
5069 #if CHECK_FOR_GLUE_IN_ANSWER
5071 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5072 return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5077 chase_additional(fetchctx_t *fctx) {
5078 isc_boolean_t rescan;
5079 dns_section_t section = DNS_SECTION_ADDITIONAL;
5080 isc_result_t result;
5085 for (result = dns_message_firstname(fctx->rmessage, section);
5086 result == ISC_R_SUCCESS;
5087 result = dns_message_nextname(fctx->rmessage, section)) {
5088 dns_name_t *name = NULL;
5089 dns_rdataset_t *rdataset;
5090 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5092 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5094 name->attributes &= ~DNS_NAMEATTR_CHASE;
5095 for (rdataset = ISC_LIST_HEAD(name->list);
5097 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5098 if (CHASE(rdataset)) {
5099 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5100 (void)dns_rdataset_additionaldata(rdataset,
5111 static inline isc_result_t
5112 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5113 isc_result_t result;
5114 dns_rdata_t rdata = DNS_RDATA_INIT;
5115 dns_rdata_cname_t cname;
5117 result = dns_rdataset_first(rdataset);
5118 if (result != ISC_R_SUCCESS)
5120 dns_rdataset_current(rdataset, &rdata);
5121 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5122 if (result != ISC_R_SUCCESS)
5124 dns_name_init(tname, NULL);
5125 dns_name_clone(&cname.cname, tname);
5126 dns_rdata_freestruct(&cname);
5128 return (ISC_R_SUCCESS);
5131 static inline isc_result_t
5132 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5133 dns_name_t *oname, dns_fixedname_t *fixeddname)
5135 isc_result_t result;
5136 dns_rdata_t rdata = DNS_RDATA_INIT;
5137 unsigned int nlabels;
5139 dns_namereln_t namereln;
5140 dns_rdata_dname_t dname;
5141 dns_fixedname_t prefix;
5144 * Get the target name of the DNAME.
5146 result = dns_rdataset_first(rdataset);
5147 if (result != ISC_R_SUCCESS)
5149 dns_rdataset_current(rdataset, &rdata);
5150 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5151 if (result != ISC_R_SUCCESS)
5155 * Get the prefix of qname.
5157 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5158 if (namereln != dns_namereln_subdomain) {
5159 char qbuf[DNS_NAME_FORMATSIZE];
5160 char obuf[DNS_NAME_FORMATSIZE];
5162 dns_rdata_freestruct(&dname);
5163 dns_name_format(qname, qbuf, sizeof(qbuf));
5164 dns_name_format(oname, obuf, sizeof(obuf));
5165 log_formerr(fctx, "unrelated DNAME in answer: "
5166 "%s is not in %s", qbuf, obuf);
5167 return (DNS_R_FORMERR);
5169 dns_fixedname_init(&prefix);
5170 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5171 dns_fixedname_init(fixeddname);
5172 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5174 dns_fixedname_name(fixeddname), NULL);
5175 dns_rdata_freestruct(&dname);
5179 static isc_boolean_t
5180 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5181 dns_rdataset_t *rdataset)
5183 isc_result_t result;
5184 dns_rdata_t rdata = DNS_RDATA_INIT;
5186 struct in6_addr in6a;
5187 isc_netaddr_t netaddr;
5188 char addrbuf[ISC_NETADDR_FORMATSIZE];
5189 char namebuf[DNS_NAME_FORMATSIZE];
5194 /* By default, we allow any addresses. */
5195 if (view->denyansweracl == NULL)
5199 * If the owner name matches one in the exclusion list, either exactly
5200 * or partially, allow it.
5202 if (view->answeracl_exclude != NULL) {
5203 dns_rbtnode_t *node = NULL;
5205 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5206 &node, NULL, 0, NULL, NULL);
5208 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5213 * Otherwise, search the filter list for a match for each address
5214 * record. If a match is found, the address should be filtered,
5215 * so should the entire answer.
5217 for (result = dns_rdataset_first(rdataset);
5218 result == ISC_R_SUCCESS;
5219 result = dns_rdataset_next(rdataset)) {
5220 dns_rdata_reset(&rdata);
5221 dns_rdataset_current(rdataset, &rdata);
5222 if (rdataset->type == dns_rdatatype_a) {
5223 INSIST(rdata.length == sizeof(ina.s_addr));
5224 memcpy(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5225 isc_netaddr_fromin(&netaddr, &ina);
5227 INSIST(rdata.length == sizeof(in6a.s6_addr));
5228 memcpy(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5229 isc_netaddr_fromin6(&netaddr, &in6a);
5232 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5233 &view->aclenv, &match, NULL);
5235 if (result == ISC_R_SUCCESS && match > 0) {
5236 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5237 dns_name_format(name, namebuf, sizeof(namebuf));
5238 dns_rdatatype_format(rdataset->type, typebuf,
5240 dns_rdataclass_format(rdataset->rdclass, classbuf,
5242 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5243 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5244 "answer address %s denied for %s/%s/%s",
5245 addrbuf, namebuf, typebuf, classbuf);
5253 static isc_boolean_t
5254 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5255 dns_rdatatype_t type, dns_name_t *tname,
5258 isc_result_t result;
5259 dns_rbtnode_t *node = NULL;
5260 char qnamebuf[DNS_NAME_FORMATSIZE];
5261 char tnamebuf[DNS_NAME_FORMATSIZE];
5265 /* By default, we allow any target name. */
5266 if (view->denyanswernames == NULL)
5270 * If the owner name matches one in the exclusion list, either exactly
5271 * or partially, allow it.
5273 if (view->answernames_exclude != NULL) {
5274 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5275 &node, NULL, 0, NULL, NULL);
5276 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5281 * If the target name is a subdomain of the search domain, allow it.
5283 if (dns_name_issubdomain(tname, domain))
5287 * Otherwise, apply filters.
5289 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5290 NULL, 0, NULL, NULL);
5291 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5292 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5293 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5294 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5295 dns_rdataclass_format(view->rdclass, classbuf,
5297 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5298 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5299 "%s target %s denied for %s/%s",
5300 typebuf, tnamebuf, qnamebuf, classbuf);
5308 trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) {
5309 char ns_namebuf[DNS_NAME_FORMATSIZE];
5310 char namebuf[DNS_NAME_FORMATSIZE];
5311 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5313 if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) {
5314 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf));
5315 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
5316 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf));
5318 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5319 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
5320 "fctx %p: trimming ttl of %s/NS for %s/%s: "
5321 "%u -> %u", fctx, ns_namebuf, namebuf, tbuf,
5322 rdataset->ttl, fctx->ns_ttl);
5323 rdataset->ttl = fctx->ns_ttl;
5328 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5329 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5330 * section for the NS RRset if the query type is NS; if it has
5331 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5332 * section for A and AAAA queries.
5334 #define LOOK_FOR_NS_IN_ANSWER 0x1
5335 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5338 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5339 unsigned int look_in_options)
5341 isc_result_t result;
5342 dns_message_t *message;
5343 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
5344 dns_rdataset_t *rdataset, *ns_rdataset;
5345 isc_boolean_t aa, negative_response;
5346 dns_rdatatype_t type;
5347 dns_section_t section;
5349 FCTXTRACE("noanswer_response");
5351 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5352 INSIST(fctx->type == dns_rdatatype_ns);
5353 section = DNS_SECTION_ANSWER;
5355 section = DNS_SECTION_AUTHORITY;
5357 message = fctx->rmessage;
5362 if (oqname == NULL) {
5364 * We have a normal, non-chained negative response or
5367 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5371 qname = &fctx->name;
5374 * We're being invoked by answer_response() after it has
5375 * followed a CNAME/DNAME chain.
5380 * If the current qname is not a subdomain of the query
5381 * domain, there's no point in looking at the authority
5382 * section without doing DNSSEC validation.
5384 * Until we do that validation, we'll just return success
5387 if (!dns_name_issubdomain(qname, &fctx->domain))
5388 return (ISC_R_SUCCESS);
5392 * We have to figure out if this is a negative response, or a
5397 * Sometimes we can tell if its a negative response by looking at
5398 * the message header.
5400 negative_response = ISC_FALSE;
5401 if (message->rcode == dns_rcode_nxdomain ||
5402 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5403 message->counts[DNS_SECTION_AUTHORITY] == 0))
5404 negative_response = ISC_TRUE;
5407 * Process the authority section.
5413 result = dns_message_firstname(message, section);
5414 while (result == ISC_R_SUCCESS) {
5416 dns_message_currentname(message, section, &name);
5417 if (dns_name_issubdomain(name, &fctx->domain)) {
5419 * Look for NS/SOA RRsets first.
5421 for (rdataset = ISC_LIST_HEAD(name->list);
5423 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5424 type = rdataset->type;
5425 if (type == dns_rdatatype_rrsig)
5426 type = rdataset->covers;
5427 if (((type == dns_rdatatype_ns ||
5428 type == dns_rdatatype_soa) &&
5429 !dns_name_issubdomain(qname, name))) {
5430 char qbuf[DNS_NAME_FORMATSIZE];
5431 char nbuf[DNS_NAME_FORMATSIZE];
5432 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5433 dns_rdatatype_format(fctx->type, tbuf,
5435 dns_name_format(name, nbuf,
5437 dns_name_format(qname, qbuf,
5440 "unrelated %s %s in "
5441 "%s authority section",
5443 return (DNS_R_FORMERR);
5445 if (type == dns_rdatatype_ns) {
5449 * Only one set of NS RRs is allowed.
5451 if (rdataset->type ==
5453 if (ns_name != NULL &&
5460 return (DNS_R_FORMERR);
5463 ns_rdataset = rdataset;
5467 rdataset->attributes |=
5468 DNS_RDATASETATTR_CACHE;
5469 rdataset->trust = dns_trust_glue;
5471 if (type == dns_rdatatype_soa) {
5473 * SOA, or RRSIG SOA.
5475 * Only one SOA is allowed.
5477 if (rdataset->type ==
5478 dns_rdatatype_soa) {
5479 if (soa_name != NULL &&
5486 return (DNS_R_FORMERR);
5491 DNS_NAMEATTR_NCACHE;
5492 rdataset->attributes |=
5493 DNS_RDATASETATTR_NCACHE;
5496 dns_trust_authauthority;
5497 else if (ISFORWARDER(fctx->addrinfo))
5502 dns_trust_additional;
5506 result = dns_message_nextname(message, section);
5507 if (result == ISC_R_NOMORE)
5509 else if (result != ISC_R_SUCCESS)
5513 log_ns_ttl(fctx, "noanswer_response");
5515 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
5516 !dns_name_equal(ns_name, dns_rootname))
5517 trim_ns_ttl(fctx, ns_name, ns_rdataset);
5520 * A negative response has a SOA record (Type 2)
5521 * and a optional NS RRset (Type 1) or it has neither
5522 * a SOA or a NS RRset (Type 3, handled above) or
5523 * rcode is NXDOMAIN (handled above) in which case
5524 * the NS RRset is allowed (Type 4).
5526 if (soa_name != NULL)
5527 negative_response = ISC_TRUE;
5529 result = dns_message_firstname(message, section);
5530 while (result == ISC_R_SUCCESS) {
5532 dns_message_currentname(message, section, &name);
5533 if (dns_name_issubdomain(name, &fctx->domain)) {
5534 for (rdataset = ISC_LIST_HEAD(name->list);
5536 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5537 type = rdataset->type;
5538 if (type == dns_rdatatype_rrsig)
5539 type = rdataset->covers;
5540 if (type == dns_rdatatype_nsec ||
5541 type == dns_rdatatype_nsec3) {
5543 * NSEC or RRSIG NSEC.
5545 if (negative_response) {
5547 DNS_NAMEATTR_NCACHE;
5548 rdataset->attributes |=
5549 DNS_RDATASETATTR_NCACHE;
5550 } else if (type == dns_rdatatype_nsec) {
5553 rdataset->attributes |=
5554 DNS_RDATASETATTR_CACHE;
5558 dns_trust_authauthority;
5559 else if (ISFORWARDER(fctx->addrinfo))
5564 dns_trust_additional;
5566 * No additional data needs to be
5569 } else if (type == dns_rdatatype_ds) {
5573 * These should only be here if
5574 * this is a referral, and there
5575 * should only be one DS RRset.
5577 if (ns_name == NULL) {
5581 return (DNS_R_FORMERR);
5583 if (rdataset->type ==
5585 if (ds_name != NULL &&
5592 return (DNS_R_FORMERR);
5598 rdataset->attributes |=
5599 DNS_RDATASETATTR_CACHE;
5602 dns_trust_authauthority;
5603 else if (ISFORWARDER(fctx->addrinfo))
5608 dns_trust_additional;
5612 result = dns_message_nextname(message, section);
5613 if (result == ISC_R_NOMORE)
5615 else if (result != ISC_R_SUCCESS)
5620 * Trigger lookups for DNS nameservers.
5622 if (negative_response && message->rcode == dns_rcode_noerror &&
5623 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5624 dns_name_equal(soa_name, qname) &&
5625 !dns_name_equal(qname, dns_rootname))
5626 return (DNS_R_CHASEDSSERVERS);
5629 * Did we find anything?
5631 if (!negative_response && ns_name == NULL) {
5635 if (oqname != NULL) {
5637 * We've already got a partial CNAME/DNAME chain,
5638 * and haven't found else anything useful here, but
5639 * no error has occurred since we have an answer.
5641 return (ISC_R_SUCCESS);
5644 * The responder is insane.
5646 log_formerr(fctx, "invalid response");
5647 return (DNS_R_FORMERR);
5652 * If we found both NS and SOA, they should be the same name.
5654 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5655 log_formerr(fctx, "NS/SOA mismatch");
5656 return (DNS_R_FORMERR);
5660 * Do we have a referral? (We only want to follow a referral if
5661 * we're not following a chain.)
5663 if (!negative_response && ns_name != NULL && oqname == NULL) {
5665 * We already know ns_name is a subdomain of fctx->domain.
5666 * If ns_name is equal to fctx->domain, we're not making
5667 * progress. We return DNS_R_FORMERR so that we'll keep
5668 * trying other servers.
5670 if (dns_name_equal(ns_name, &fctx->domain)) {
5671 log_formerr(fctx, "non-improving referral");
5672 return (DNS_R_FORMERR);
5676 * If the referral name is not a parent of the query
5677 * name, consider the responder insane.
5679 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5681 log_formerr(fctx, "referral to non-parent");
5682 FCTXTRACE("referral to non-parent");
5683 return (DNS_R_FORMERR);
5687 * Mark any additional data related to this rdataset.
5688 * It's important that we do this before we change the
5691 INSIST(ns_rdataset != NULL);
5692 fctx->attributes |= FCTX_ATTR_GLUING;
5693 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5695 #if CHECK_FOR_GLUE_IN_ANSWER
5697 * Look in the answer section for "glue" that is incorrectly
5698 * returned as a answer. This is needed if the server also
5699 * minimizes the response size by not adding records to the
5700 * additional section that are in the answer section or if
5701 * the record gets dropped due to message size constraints.
5703 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
5704 (fctx->type == dns_rdatatype_aaaa ||
5705 fctx->type == dns_rdatatype_a))
5706 (void)dns_rdataset_additionaldata(ns_rdataset,
5707 check_answer, fctx);
5709 fctx->attributes &= ~FCTX_ATTR_GLUING;
5711 * NS rdatasets with 0 TTL cause problems.
5712 * dns_view_findzonecut() will not find them when we
5713 * try to follow the referral, and we'll SERVFAIL
5714 * because the best nameservers are now above QDOMAIN.
5715 * We force the TTL to 1 second to prevent this.
5717 if (ns_rdataset->ttl == 0)
5718 ns_rdataset->ttl = 1;
5720 * Set the current query domain to the referral name.
5722 * XXXRTH We should check if we're in forward-only mode, and
5723 * if so we should bail out.
5725 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5726 dns_name_free(&fctx->domain,
5727 fctx->res->buckets[fctx->bucketnum].mctx);
5728 if (dns_rdataset_isassociated(&fctx->nameservers))
5729 dns_rdataset_disassociate(&fctx->nameservers);
5730 dns_name_init(&fctx->domain, NULL);
5731 result = dns_name_dup(ns_name,
5732 fctx->res->buckets[fctx->bucketnum].mctx,
5734 if (result != ISC_R_SUCCESS)
5736 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5737 fctx->ns_ttl_ok = ISC_FALSE;
5738 log_ns_ttl(fctx, "DELEGATION");
5739 return (DNS_R_DELEGATION);
5743 * Since we're not doing a referral, we don't want to cache any
5744 * NS RRs we may have found.
5746 if (ns_name != NULL)
5747 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5749 if (negative_response && oqname == NULL)
5750 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5752 return (ISC_R_SUCCESS);
5756 answer_response(fetchctx_t *fctx) {
5757 isc_result_t result;
5758 dns_message_t *message;
5759 dns_name_t *name, *qname, tname, *ns_name;
5760 dns_rdataset_t *rdataset, *ns_rdataset;
5761 isc_boolean_t done, external, chaining, aa, found, want_chaining;
5762 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5764 dns_rdatatype_t type;
5765 dns_fixedname_t dname, fqname;
5768 FCTXTRACE("answer_response");
5770 message = fctx->rmessage;
5773 * Examine the answer section, marking those rdatasets which are
5774 * part of the answer and should be cached.
5778 found_cname = ISC_FALSE;
5779 found_type = ISC_FALSE;
5780 chaining = ISC_FALSE;
5781 have_answer = ISC_FALSE;
5782 want_chaining = ISC_FALSE;
5783 POST(want_chaining);
5784 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5788 qname = &fctx->name;
5790 view = fctx->res->view;
5791 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5792 while (!done && result == ISC_R_SUCCESS) {
5794 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5795 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5796 if (dns_name_equal(name, qname)) {
5797 wanted_chaining = ISC_FALSE;
5798 for (rdataset = ISC_LIST_HEAD(name->list);
5800 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5802 want_chaining = ISC_FALSE;
5804 if (rdataset->type == dns_rdatatype_nsec3) {
5806 * NSEC3 records are not allowed to
5807 * appear in the answer section.
5809 log_formerr(fctx, "NSEC3 in answer");
5810 return (DNS_R_FORMERR);
5814 * Apply filters, if given, on answers to reject
5815 * a malicious attempt of rebinding.
5817 if ((rdataset->type == dns_rdatatype_a ||
5818 rdataset->type == dns_rdatatype_aaaa) &&
5819 !is_answeraddress_allowed(view, name,
5821 return (DNS_R_SERVFAIL);
5824 if (rdataset->type == type && !found_cname) {
5826 * We've found an ordinary answer.
5829 found_type = ISC_TRUE;
5831 aflag = DNS_RDATASETATTR_ANSWER;
5832 } else if (type == dns_rdatatype_any) {
5834 * We've found an answer matching
5835 * an ANY query. There may be
5839 aflag = DNS_RDATASETATTR_ANSWER;
5840 } else if (rdataset->type == dns_rdatatype_rrsig
5841 && rdataset->covers == type
5844 * We've found a signature that
5845 * covers the type we're looking for.
5848 found_type = ISC_TRUE;
5849 aflag = DNS_RDATASETATTR_ANSWERSIG;
5850 } else if (rdataset->type ==
5854 * We're looking for something else,
5855 * but we found a CNAME.
5857 * Getting a CNAME response for some
5858 * query types is an error.
5860 if (type == dns_rdatatype_rrsig ||
5861 type == dns_rdatatype_dnskey ||
5862 type == dns_rdatatype_nsec ||
5863 type == dns_rdatatype_nsec3) {
5864 char buf[DNS_RDATATYPE_FORMATSIZE];
5865 dns_rdatatype_format(fctx->type,
5870 return (DNS_R_FORMERR);
5873 found_cname = ISC_TRUE;
5874 want_chaining = ISC_TRUE;
5875 aflag = DNS_RDATASETATTR_ANSWER;
5876 result = cname_target(rdataset,
5878 if (result != ISC_R_SUCCESS)
5880 /* Apply filters on the target name. */
5881 if (!is_answertarget_allowed(view,
5886 return (DNS_R_SERVFAIL);
5888 } else if (rdataset->type == dns_rdatatype_rrsig
5889 && rdataset->covers ==
5893 * We're looking for something else,
5894 * but we found a SIG CNAME.
5897 found_cname = ISC_TRUE;
5898 aflag = DNS_RDATASETATTR_ANSWERSIG;
5903 * We've found an answer to our
5908 rdataset->attributes |=
5909 DNS_RDATASETATTR_CACHE;
5910 rdataset->trust = dns_trust_answer;
5913 * This data is "the" answer
5914 * to our question only if
5915 * we're not chaining (i.e.
5916 * if we haven't followed
5917 * a CNAME or DNAME).
5921 DNS_RDATASETATTR_ANSWER)
5922 have_answer = ISC_TRUE;
5924 DNS_NAMEATTR_ANSWER;
5925 rdataset->attributes |= aflag;
5928 dns_trust_authanswer;
5929 } else if (external) {
5931 * This data is outside of
5932 * our query domain, and
5933 * may not be cached.
5935 rdataset->attributes |=
5936 DNS_RDATASETATTR_EXTERNAL;
5940 * Mark any additional data related
5943 (void)dns_rdataset_additionaldata(
5951 if (want_chaining) {
5952 wanted_chaining = ISC_TRUE;
5954 DNS_NAMEATTR_CHAINING;
5955 rdataset->attributes |=
5956 DNS_RDATASETATTR_CHAINING;
5961 * We could add an "else" clause here and
5962 * log that we're ignoring this rdataset.
5966 * If wanted_chaining is true, we've done
5967 * some chaining as the result of processing
5968 * this node, and thus we need to set
5971 * We don't set chaining inside of the
5972 * rdataset loop because doing that would
5973 * cause us to ignore the signatures of
5976 if (wanted_chaining)
5977 chaining = ISC_TRUE;
5980 * Look for a DNAME (or its SIG). Anything else is
5983 wanted_chaining = ISC_FALSE;
5984 for (rdataset = ISC_LIST_HEAD(name->list);
5986 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5987 isc_boolean_t found_dname = ISC_FALSE;
5988 dns_name_t *dname_name;
5992 if (rdataset->type == dns_rdatatype_dname) {
5994 * We're looking for something else,
5995 * but we found a DNAME.
5997 * If we're not chaining, then the
5998 * DNAME should not be external.
6000 if (!chaining && external) {
6003 return (DNS_R_FORMERR);
6006 want_chaining = ISC_TRUE;
6007 POST(want_chaining);
6008 aflag = DNS_RDATASETATTR_ANSWER;
6009 result = dname_target(fctx, rdataset,
6012 if (result == ISC_R_NOSPACE) {
6014 * We can't construct the
6015 * DNAME target. Do not
6018 want_chaining = ISC_FALSE;
6019 POST(want_chaining);
6020 } else if (result != ISC_R_SUCCESS)
6023 found_dname = ISC_TRUE;
6025 dname_name = dns_fixedname_name(&dname);
6026 if (!is_answertarget_allowed(view,
6031 return (DNS_R_SERVFAIL);
6033 } else if (rdataset->type == dns_rdatatype_rrsig
6034 && rdataset->covers ==
6035 dns_rdatatype_dname) {
6037 * We've found a signature that
6041 aflag = DNS_RDATASETATTR_ANSWERSIG;
6046 * We've found an answer to our
6051 rdataset->attributes |=
6052 DNS_RDATASETATTR_CACHE;
6053 rdataset->trust = dns_trust_answer;
6056 * This data is "the" answer
6057 * to our question only if
6058 * we're not chaining.
6062 DNS_RDATASETATTR_ANSWER)
6063 have_answer = ISC_TRUE;
6065 DNS_NAMEATTR_ANSWER;
6066 rdataset->attributes |= aflag;
6069 dns_trust_authanswer;
6070 } else if (external) {
6071 rdataset->attributes |=
6072 DNS_RDATASETATTR_EXTERNAL;
6080 * Copy the dname into the
6083 * Although we check for
6084 * failure of the copy
6085 * operation, in practice it
6086 * should never fail since
6087 * we already know that the
6088 * result fits in a fixedname.
6090 dns_fixedname_init(&fqname);
6091 result = dns_name_copy(
6092 dns_fixedname_name(&dname),
6093 dns_fixedname_name(&fqname),
6095 if (result != ISC_R_SUCCESS)
6097 wanted_chaining = ISC_TRUE;
6099 DNS_NAMEATTR_CHAINING;
6100 rdataset->attributes |=
6101 DNS_RDATASETATTR_CHAINING;
6102 qname = dns_fixedname_name(
6107 if (wanted_chaining)
6108 chaining = ISC_TRUE;
6110 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6112 if (result == ISC_R_NOMORE)
6113 result = ISC_R_SUCCESS;
6114 if (result != ISC_R_SUCCESS)
6118 * We should have found an answer.
6121 log_formerr(fctx, "reply has no answer");
6122 return (DNS_R_FORMERR);
6126 * This response is now potentially cacheable.
6128 fctx->attributes |= FCTX_ATTR_WANTCACHE;
6131 * Did chaining end before we got the final answer?
6135 * Yes. This may be a negative reply, so hand off
6136 * authority section processing to the noanswer code.
6137 * If it isn't a noanswer response, no harm will be
6140 return (noanswer_response(fctx, qname, 0));
6144 * We didn't end with an incomplete chain, so the rcode should be
6147 if (message->rcode != dns_rcode_noerror) {
6148 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6150 return (DNS_R_FORMERR);
6154 * Examine the authority section (if there is one).
6156 * We expect there to be only one owner name for all the rdatasets
6157 * in this section, and we expect that it is not external.
6162 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6163 while (!done && result == ISC_R_SUCCESS) {
6165 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6166 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6169 * We expect to find NS or SIG NS rdatasets, and
6172 for (rdataset = ISC_LIST_HEAD(name->list);
6174 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6175 if (rdataset->type == dns_rdatatype_ns ||
6176 (rdataset->type == dns_rdatatype_rrsig &&
6177 rdataset->covers == dns_rdatatype_ns)) {
6180 rdataset->attributes |=
6181 DNS_RDATASETATTR_CACHE;
6182 if (aa && !chaining)
6184 dns_trust_authauthority;
6187 dns_trust_additional;
6189 if (rdataset->type == dns_rdatatype_ns) {
6191 ns_rdataset = rdataset;
6194 * Mark any additional data related
6197 (void)dns_rdataset_additionaldata(
6205 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6207 if (result == ISC_R_NOMORE)
6208 result = ISC_R_SUCCESS;
6210 log_ns_ttl(fctx, "answer_response");
6212 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6213 !dns_name_equal(ns_name, dns_rootname))
6214 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6219 static isc_boolean_t
6220 fctx_decreference(fetchctx_t *fctx) {
6221 isc_boolean_t bucket_empty = ISC_FALSE;
6223 INSIST(fctx->references > 0);
6225 if (fctx->references == 0) {
6227 * No one cares about the result of this fetch anymore.
6229 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6230 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6232 * This fctx is already shutdown; we were just
6233 * waiting for the last reference to go away.
6235 bucket_empty = fctx_destroy(fctx);
6238 * Initiate shutdown.
6240 fctx_shutdown(fctx);
6243 return (bucket_empty);
6247 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6248 dns_fetchevent_t *fevent;
6249 dns_resolver_t *res;
6251 isc_result_t result;
6252 isc_boolean_t bucket_empty;
6253 isc_boolean_t locked = ISC_FALSE;
6254 unsigned int bucketnum;
6255 dns_rdataset_t nameservers;
6256 dns_fixedname_t fixed;
6259 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6260 fevent = (dns_fetchevent_t *)event;
6261 fctx = event->ev_arg;
6262 REQUIRE(VALID_FCTX(fctx));
6266 FCTXTRACE("resume_dslookup");
6268 if (fevent->node != NULL)
6269 dns_db_detachnode(fevent->db, &fevent->node);
6270 if (fevent->db != NULL)
6271 dns_db_detach(&fevent->db);
6273 dns_rdataset_init(&nameservers);
6275 bucketnum = fctx->bucketnum;
6276 if (fevent->result == ISC_R_CANCELED) {
6277 dns_resolver_destroyfetch(&fctx->nsfetch);
6278 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6279 } else if (fevent->result == ISC_R_SUCCESS) {
6281 FCTXTRACE("resuming DS lookup");
6283 dns_resolver_destroyfetch(&fctx->nsfetch);
6284 if (dns_rdataset_isassociated(&fctx->nameservers))
6285 dns_rdataset_disassociate(&fctx->nameservers);
6286 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6287 fctx->ns_ttl = fctx->nameservers.ttl;
6288 fctx->ns_ttl_ok = ISC_TRUE;
6289 log_ns_ttl(fctx, "resume_dslookup");
6290 dns_name_free(&fctx->domain,
6291 fctx->res->buckets[bucketnum].mctx);
6292 dns_name_init(&fctx->domain, NULL);
6293 result = dns_name_dup(&fctx->nsname,
6294 fctx->res->buckets[bucketnum].mctx,
6296 if (result != ISC_R_SUCCESS) {
6297 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6303 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6306 dns_rdataset_t *nsrdataset = NULL;
6309 * Retrieve state from fctx->nsfetch before we destroy it.
6311 dns_fixedname_init(&fixed);
6312 domain = dns_fixedname_name(&fixed);
6313 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6314 if (dns_name_equal(&fctx->nsname, domain)) {
6315 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6316 dns_resolver_destroyfetch(&fctx->nsfetch);
6319 if (dns_rdataset_isassociated(
6320 &fctx->nsfetch->private->nameservers)) {
6322 &fctx->nsfetch->private->nameservers,
6324 nsrdataset = &nameservers;
6327 dns_resolver_destroyfetch(&fctx->nsfetch);
6328 n = dns_name_countlabels(&fctx->nsname);
6329 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6332 if (dns_rdataset_isassociated(fevent->rdataset))
6333 dns_rdataset_disassociate(fevent->rdataset);
6334 FCTXTRACE("continuing to look for parent's NS records");
6335 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6336 dns_rdatatype_ns, domain,
6337 nsrdataset, NULL, 0, task,
6338 resume_dslookup, fctx,
6339 &fctx->nsrrset, NULL,
6341 if (result != ISC_R_SUCCESS)
6342 fctx_done(fctx, result, __LINE__);
6344 LOCK(&res->buckets[bucketnum].lock);
6351 if (dns_rdataset_isassociated(&nameservers))
6352 dns_rdataset_disassociate(&nameservers);
6353 if (dns_rdataset_isassociated(fevent->rdataset))
6354 dns_rdataset_disassociate(fevent->rdataset);
6355 INSIST(fevent->sigrdataset == NULL);
6356 isc_event_free(&event);
6358 LOCK(&res->buckets[bucketnum].lock);
6359 bucket_empty = fctx_decreference(fctx);
6360 UNLOCK(&res->buckets[bucketnum].lock);
6366 checknamessection(dns_message_t *message, dns_section_t section) {
6367 isc_result_t result;
6369 dns_rdata_t rdata = DNS_RDATA_INIT;
6370 dns_rdataset_t *rdataset;
6372 for (result = dns_message_firstname(message, section);
6373 result == ISC_R_SUCCESS;
6374 result = dns_message_nextname(message, section))
6377 dns_message_currentname(message, section, &name);
6378 for (rdataset = ISC_LIST_HEAD(name->list);
6380 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6381 for (result = dns_rdataset_first(rdataset);
6382 result == ISC_R_SUCCESS;
6383 result = dns_rdataset_next(rdataset)) {
6384 dns_rdataset_current(rdataset, &rdata);
6385 if (!dns_rdata_checkowner(name, rdata.rdclass,
6388 !dns_rdata_checknames(&rdata, name, NULL))
6390 rdataset->attributes |=
6391 DNS_RDATASETATTR_CHECKNAMES;
6393 dns_rdata_reset(&rdata);
6400 checknames(dns_message_t *message) {
6402 checknamessection(message, DNS_SECTION_ANSWER);
6403 checknamessection(message, DNS_SECTION_AUTHORITY);
6404 checknamessection(message, DNS_SECTION_ADDITIONAL);
6408 * Log server NSID at log level 'level'
6411 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
6413 static const char hex[17] = "0123456789abcdef";
6414 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6415 isc_uint16_t optcode, nsid_len, buflen, i;
6416 isc_result_t result;
6417 isc_buffer_t nsidbuf;
6419 unsigned char *p, *buf, *nsid;
6421 /* Extract rdata from OPT rdataset */
6422 result = dns_rdataset_first(opt);
6423 if (result != ISC_R_SUCCESS)
6424 return (ISC_R_FAILURE);
6426 dns_rdata_init(&rdata);
6427 dns_rdataset_current(opt, &rdata);
6428 if (rdata.length < 4)
6429 return (ISC_R_FAILURE);
6431 /* Check for NSID */
6432 isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
6433 isc_buffer_add(&nsidbuf, rdata.length);
6434 optcode = isc_buffer_getuint16(&nsidbuf);
6435 nsid_len = isc_buffer_getuint16(&nsidbuf);
6436 if (optcode != DNS_OPT_NSID || nsid_len == 0)
6437 return (ISC_R_FAILURE);
6439 /* Allocate buffer for storing hex version of the NSID */
6440 buflen = nsid_len * 2 + 1;
6441 buf = isc_mem_get(mctx, buflen);
6443 return (ISC_R_NOSPACE);
6445 /* Convert to hex */
6447 nsid = rdata.data + 4;
6448 for (i = 0; i < nsid_len; i++) {
6449 *p++ = hex[(nsid[0] >> 4) & 0xf];
6450 *p++ = hex[nsid[0] & 0xf];
6455 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6457 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6458 DNS_LOGMODULE_RESOLVER, level,
6459 "received NSID '%s' from %s", buf, addrbuf);
6462 isc_mem_put(mctx, buf, buflen);
6463 return (ISC_R_SUCCESS);
6467 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6468 isc_buffer_t buffer;
6471 isc_result_t result;
6473 if (! isc_log_wouldlog(dns_lctx, level))
6477 * Note that these are multiline debug messages. We want a newline
6478 * to appear in the log after each message.
6482 buf = isc_mem_get(mctx, len);
6485 isc_buffer_init(&buffer, buf, len);
6486 result = dns_message_totext(message, &dns_master_style_debug,
6488 if (result == ISC_R_NOSPACE) {
6489 isc_mem_put(mctx, buf, len);
6491 } else if (result == ISC_R_SUCCESS)
6492 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6493 DNS_LOGMODULE_RESOLVER, level,
6494 "received packet:\n%.*s",
6495 (int)isc_buffer_usedlength(&buffer),
6497 } while (result == ISC_R_NOSPACE);
6500 isc_mem_put(mctx, buf, len);
6503 static isc_boolean_t
6504 iscname(fetchctx_t *fctx) {
6505 isc_result_t result;
6507 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6508 &fctx->name, dns_rdatatype_cname, 0,
6510 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6513 static isc_boolean_t
6514 betterreferral(fetchctx_t *fctx) {
6515 isc_result_t result;
6517 dns_rdataset_t *rdataset;
6518 dns_message_t *message = fctx->rmessage;
6520 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6521 result == ISC_R_SUCCESS;
6522 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6524 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6525 if (!isstrictsubdomain(name, &fctx->domain))
6527 for (rdataset = ISC_LIST_HEAD(name->list);
6529 rdataset = ISC_LIST_NEXT(rdataset, link))
6530 if (rdataset->type == dns_rdatatype_ns)
6537 resquery_response(isc_task_t *task, isc_event_t *event) {
6538 isc_result_t result = ISC_R_SUCCESS;
6539 resquery_t *query = event->ev_arg;
6540 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6541 isc_boolean_t keep_trying, get_nameservers, resend;
6542 isc_boolean_t truncated;
6543 dns_message_t *message;
6544 dns_rdataset_t *opt;
6547 dns_fixedname_t foundname;
6549 isc_time_t tnow, *finish;
6550 dns_adbaddrinfo_t *addrinfo;
6551 unsigned int options;
6552 unsigned int findoptions;
6553 isc_result_t broken_server;
6554 badnstype_t broken_type = badns_response;
6555 isc_boolean_t no_response;
6557 REQUIRE(VALID_QUERY(query));
6559 options = query->options;
6560 REQUIRE(VALID_FCTX(fctx));
6561 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6565 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6566 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6568 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6570 (void)isc_timer_touch(fctx->timer);
6572 keep_trying = ISC_FALSE;
6573 broken_server = ISC_R_SUCCESS;
6574 get_nameservers = ISC_FALSE;
6576 truncated = ISC_FALSE;
6578 no_response = ISC_FALSE;
6580 if (fctx->res->exiting) {
6581 result = ISC_R_SHUTTINGDOWN;
6586 fctx->timeout = ISC_FALSE;
6587 fctx->addrinfo = query->addrinfo;
6590 * XXXRTH We should really get the current time just once. We
6591 * need a routine to convert from an isc_time_t to an
6596 isc_stdtime_get(&now);
6599 * Did the dispatcher have a problem?
6601 if (devent->result != ISC_R_SUCCESS) {
6602 if (devent->result == ISC_R_EOF &&
6603 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6605 * The problem might be that they
6606 * don't understand EDNS0. Turn it
6607 * off and try again.
6609 options |= DNS_FETCHOPT_NOEDNS0;
6612 * Remember that they don't like EDNS0.
6614 dns_adb_changeflags(fctx->adb,
6616 DNS_FETCHOPT_NOEDNS0,
6617 DNS_FETCHOPT_NOEDNS0);
6620 * There's no hope for this query.
6622 keep_trying = ISC_TRUE;
6625 * If this is a network error on an exclusive query
6626 * socket, mark the server as bad so that we won't try
6627 * it for this fetch again. Also adjust finish and
6628 * no_response so that we penalize this address in SRTT
6631 if (query->exclusivesocket &&
6632 (devent->result == ISC_R_HOSTUNREACH ||
6633 devent->result == ISC_R_NETUNREACH ||
6634 devent->result == ISC_R_CONNREFUSED ||
6635 devent->result == ISC_R_CANCELED)) {
6636 broken_server = devent->result;
6637 broken_type = badns_unreachable;
6639 no_response = ISC_TRUE;
6645 message = fctx->rmessage;
6647 if (query->tsig != NULL) {
6648 result = dns_message_setquerytsig(message, query->tsig);
6649 if (result != ISC_R_SUCCESS)
6653 if (query->tsigkey) {
6654 result = dns_message_settsigkey(message, query->tsigkey);
6655 if (result != ISC_R_SUCCESS)
6659 result = dns_message_parse(message, &devent->buffer, 0);
6660 if (result != ISC_R_SUCCESS) {
6662 case ISC_R_UNEXPECTEDEND:
6663 if (!message->question_ok ||
6664 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6665 (options & DNS_FETCHOPT_TCP) != 0) {
6667 * Either the message ended prematurely,
6668 * and/or wasn't marked as being truncated,
6669 * and/or this is a response to a query we
6670 * sent over TCP. In all of these cases,
6671 * something is wrong with the remote
6672 * server and we don't want to retry using
6675 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6678 * The problem might be that they
6679 * don't understand EDNS0. Turn it
6680 * off and try again.
6682 options |= DNS_FETCHOPT_NOEDNS0;
6685 * Remember that they don't like EDNS0.
6687 dns_adb_changeflags(
6690 DNS_FETCHOPT_NOEDNS0,
6691 DNS_FETCHOPT_NOEDNS0);
6692 inc_stats(fctx->res,
6693 dns_resstatscounter_edns0fail);
6695 broken_server = result;
6696 keep_trying = ISC_TRUE;
6701 * We defer retrying via TCP for a bit so we can
6702 * check out this message further.
6704 truncated = ISC_TRUE;
6707 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6709 * The problem might be that they
6710 * don't understand EDNS0. Turn it
6711 * off and try again.
6713 options |= DNS_FETCHOPT_NOEDNS0;
6716 * Remember that they don't like EDNS0.
6718 dns_adb_changeflags(fctx->adb,
6720 DNS_FETCHOPT_NOEDNS0,
6721 DNS_FETCHOPT_NOEDNS0);
6722 inc_stats(fctx->res,
6723 dns_resstatscounter_edns0fail);
6725 broken_server = DNS_R_UNEXPECTEDRCODE;
6726 keep_trying = ISC_TRUE;
6731 * Something bad has happened.
6739 * Log the incoming packet.
6741 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6744 * Did we request NSID? If so, and if the response contains
6745 * NSID data, log it at INFO level.
6747 opt = dns_message_getopt(message);
6748 if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6749 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6752 * If the message is signed, check the signature. If not, this
6753 * returns success anyway.
6755 result = dns_message_checksig(message, fctx->res->view);
6756 if (result != ISC_R_SUCCESS)
6760 * The dispatcher should ensure we only get responses with QR set.
6762 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6764 * INSIST() that the message comes from the place we sent it to,
6765 * since the dispatch code should ensure this.
6767 * INSIST() that the message id is correct (this should also be
6768 * ensured by the dispatch code).
6772 * We have an affirmative response to the query and we have
6773 * previously got a response from this server which indicated
6774 * EDNS may not be supported so we can now cache the lack of
6778 (message->rcode == dns_rcode_noerror ||
6779 message->rcode == dns_rcode_nxdomain ||
6780 message->rcode == dns_rcode_refused ||
6781 message->rcode == dns_rcode_yxdomain) &&
6782 bad_edns(fctx, &query->addrinfo->sockaddr)) {
6783 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6784 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6786 dns_adb_changeflags(fctx->adb, query->addrinfo,
6787 DNS_FETCHOPT_NOEDNS0,
6788 DNS_FETCHOPT_NOEDNS0);
6792 * Deal with truncated responses by retrying using TCP.
6794 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6795 truncated = ISC_TRUE;
6798 inc_stats(fctx->res, dns_resstatscounter_truncated);
6799 if ((options & DNS_FETCHOPT_TCP) != 0) {
6800 broken_server = DNS_R_TRUNCATEDTCP;
6801 keep_trying = ISC_TRUE;
6803 options |= DNS_FETCHOPT_TCP;
6810 * Is it a query response?
6812 if (message->opcode != dns_opcode_query) {
6814 broken_server = DNS_R_UNEXPECTEDOPCODE;
6815 keep_trying = ISC_TRUE;
6820 * Update statistics about erroneous responses.
6822 if (message->rcode != dns_rcode_noerror) {
6823 switch (message->rcode) {
6824 case dns_rcode_nxdomain:
6825 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6827 case dns_rcode_servfail:
6828 inc_stats(fctx->res, dns_resstatscounter_servfail);
6830 case dns_rcode_formerr:
6831 inc_stats(fctx->res, dns_resstatscounter_formerr);
6834 inc_stats(fctx->res, dns_resstatscounter_othererror);
6840 * Is the remote server broken, or does it dislike us?
6842 if (message->rcode != dns_rcode_noerror &&
6843 message->rcode != dns_rcode_nxdomain) {
6844 if (((message->rcode == dns_rcode_formerr ||
6845 message->rcode == dns_rcode_notimp) ||
6846 (message->rcode == dns_rcode_servfail &&
6847 dns_message_getopt(message) == NULL)) &&
6848 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6850 * It's very likely they don't like EDNS0.
6851 * If the response code is SERVFAIL, also check if the
6852 * response contains an OPT RR and don't cache the
6853 * failure since it can be returned for various other
6856 * XXXRTH We should check if the question
6857 * we're asking requires EDNS0, and
6858 * if so, we should bail out.
6860 options |= DNS_FETCHOPT_NOEDNS0;
6863 * Remember that they may not like EDNS0.
6865 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6866 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6867 } else if (message->rcode == dns_rcode_formerr) {
6868 if (ISFORWARDER(query->addrinfo)) {
6870 * This forwarder doesn't understand us,
6871 * but other forwarders might. Keep trying.
6873 broken_server = DNS_R_REMOTEFORMERR;
6874 keep_trying = ISC_TRUE;
6877 * The server doesn't understand us. Since
6878 * all servers for a zone need similar
6879 * capabilities, we assume that we will get
6880 * FORMERR from all servers, and thus we
6881 * cannot make any more progress with this
6884 log_formerr(fctx, "server sent FORMERR");
6885 result = DNS_R_FORMERR;
6887 } else if (message->rcode == dns_rcode_yxdomain) {
6889 * DNAME mapping failed because the new name
6890 * was too long. There's no chance of success
6893 result = DNS_R_YXDOMAIN;
6894 } else if (message->rcode == dns_rcode_badvers) {
6895 unsigned int flags, mask;
6896 unsigned int version;
6899 INSIST(opt != NULL);
6900 version = (opt->ttl >> 16) & 0xff;
6901 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6902 DNS_FETCHOPT_EDNSVERSIONSET;
6903 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6904 DNS_FETCHOPT_EDNSVERSIONSET;
6907 dns_adb_changeflags(fctx->adb, query->addrinfo,
6911 broken_server = DNS_R_BADVERS;
6912 keep_trying = ISC_TRUE;
6919 broken_server = DNS_R_UNEXPECTEDRCODE;
6920 INSIST(broken_server != ISC_R_SUCCESS);
6921 keep_trying = ISC_TRUE;
6927 * Is the question the same as the one we asked?
6929 result = same_question(fctx);
6930 if (result != ISC_R_SUCCESS) {
6932 if (result == DNS_R_FORMERR)
6933 keep_trying = ISC_TRUE;
6938 * Is the server lame?
6940 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6942 inc_stats(fctx->res, dns_resstatscounter_lame);
6943 log_lame(fctx, query->addrinfo);
6944 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6945 &fctx->name, fctx->type,
6946 now + fctx->res->lame_ttl);
6947 if (result != ISC_R_SUCCESS)
6948 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6949 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6950 "could not mark server as lame: %s",
6951 isc_result_totext(result));
6952 broken_server = DNS_R_LAME;
6953 keep_trying = ISC_TRUE;
6958 * Enforce delegations only zones like NET and COM.
6960 if (!ISFORWARDER(query->addrinfo) &&
6961 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6962 !dns_name_equal(&fctx->domain, &fctx->name) &&
6963 fix_mustbedelegationornxdomain(message, fctx)) {
6964 char namebuf[DNS_NAME_FORMATSIZE];
6965 char domainbuf[DNS_NAME_FORMATSIZE];
6966 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6970 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6971 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6972 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6973 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6975 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6978 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6979 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6980 "enforced delegation-only for '%s' (%s/%s/%s) "
6982 domainbuf, namebuf, typebuf, classbuf, addrbuf);
6985 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6986 checknames(message);
6991 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6994 * Did we get any answers?
6996 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6997 (message->rcode == dns_rcode_noerror ||
6998 message->rcode == dns_rcode_nxdomain)) {
7001 * We've got answers. If it has an authoritative answer or an
7002 * answer from a forwarder, we're done.
7004 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
7005 ISFORWARDER(query->addrinfo))
7006 result = answer_response(fctx);
7007 else if (iscname(fctx) &&
7008 fctx->type != dns_rdatatype_any &&
7009 fctx->type != dns_rdatatype_cname) {
7011 * A BIND8 server could return a non-authoritative
7012 * answer when a CNAME is followed. We should treat
7013 * it as a valid answer.
7015 result = answer_response(fctx);
7016 } else if (fctx->type != dns_rdatatype_ns &&
7017 !betterreferral(fctx)) {
7019 * Lame response !!!.
7021 result = answer_response(fctx);
7023 if (fctx->type == dns_rdatatype_ns) {
7025 * A BIND 8 server could incorrectly return a
7026 * non-authoritative answer to an NS query
7027 * instead of a referral. Since this answer
7028 * lacks the SIGs necessary to do DNSSEC
7029 * validation, we must invoke the following
7030 * special kludge to treat it as a referral.
7032 result = noanswer_response(fctx, NULL,
7033 LOOK_FOR_NS_IN_ANSWER);
7036 * Some other servers may still somehow include
7037 * an answer when it should return a referral
7038 * with an empty answer. Check to see if we can
7039 * treat this as a referral by ignoring the
7040 * answer. Further more, there may be an
7041 * implementation that moves A/AAAA glue records
7042 * to the answer section for that type of
7043 * delegation when the query is for that glue
7044 * record. LOOK_FOR_GLUE_IN_ANSWER will handle
7045 * such a corner case.
7047 result = noanswer_response(fctx, NULL,
7048 LOOK_FOR_GLUE_IN_ANSWER);
7050 if (result != DNS_R_DELEGATION) {
7052 * At this point, AA is not set, the response
7053 * is not a referral, and the server is not a
7054 * forwarder. It is technically lame and it's
7055 * easier to treat it as such than to figure out
7056 * some more elaborate course of action.
7058 broken_server = DNS_R_LAME;
7059 keep_trying = ISC_TRUE;
7062 goto force_referral;
7064 if (result != ISC_R_SUCCESS) {
7065 if (result == DNS_R_FORMERR)
7066 keep_trying = ISC_TRUE;
7069 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
7070 message->rcode == dns_rcode_noerror ||
7071 message->rcode == dns_rcode_nxdomain) {
7073 * NXDOMAIN, NXRDATASET, or referral.
7075 result = noanswer_response(fctx, NULL, 0);
7076 if (result == DNS_R_CHASEDSSERVERS) {
7077 } else if (result == DNS_R_DELEGATION) {
7080 * We don't have the answer, but we know a better
7083 get_nameservers = ISC_TRUE;
7084 keep_trying = ISC_TRUE;
7086 * We have a new set of name servers, and it
7087 * has not experienced any restarts yet.
7092 * Update local statistics counters collected for each
7096 fctx->querysent = 0;
7097 fctx->lamecount = 0;
7102 result = ISC_R_SUCCESS;
7103 } else if (result != ISC_R_SUCCESS) {
7105 * Something has gone wrong.
7107 if (result == DNS_R_FORMERR)
7108 keep_trying = ISC_TRUE;
7113 * The server is insane.
7116 broken_server = DNS_R_UNEXPECTEDRCODE;
7117 keep_trying = ISC_TRUE;
7122 * Follow additional section data chains.
7124 chase_additional(fctx);
7127 * Cache the cacheable parts of the message. This may also cause
7128 * work to be queued to the DNSSEC validator.
7130 if (WANTCACHE(fctx)) {
7131 result = cache_message(fctx, query->addrinfo, now);
7132 if (result != ISC_R_SUCCESS)
7137 * Ncache the negatively cacheable parts of the message. This may
7138 * also cause work to be queued to the DNSSEC validator.
7140 if (WANTNCACHE(fctx)) {
7141 dns_rdatatype_t covers;
7142 if (message->rcode == dns_rcode_nxdomain)
7143 covers = dns_rdatatype_any;
7145 covers = fctx->type;
7148 * Cache any negative cache entries in the message.
7150 result = ncache_message(fctx, query->addrinfo, covers, now);
7155 * Remember the query's addrinfo, in case we need to mark the
7158 addrinfo = query->addrinfo;
7163 * XXXRTH Don't cancel the query if waiting for validation?
7165 fctx_cancelquery(&query, &devent, finish, no_response);
7168 if (result == DNS_R_FORMERR)
7169 broken_server = DNS_R_FORMERR;
7170 if (broken_server != ISC_R_SUCCESS) {
7172 * Add this server to the list of bad servers for
7175 add_bad(fctx, addrinfo, broken_server, broken_type);
7178 if (get_nameservers) {
7180 dns_fixedname_init(&foundname);
7181 fname = dns_fixedname_name(&foundname);
7182 if (result != ISC_R_SUCCESS) {
7183 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7187 if (dns_rdatatype_atparent(fctx->type))
7188 findoptions |= DNS_DBFIND_NOEXACT;
7189 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7192 name = &fctx->domain;
7193 result = dns_view_findzonecut(fctx->res->view,
7199 if (result != ISC_R_SUCCESS) {
7200 FCTXTRACE("couldn't find a zonecut");
7201 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7204 if (!dns_name_issubdomain(fname, &fctx->domain)) {
7206 * The best nameservers are now above our
7209 FCTXTRACE("nameservers now above QDOMAIN");
7210 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7213 dns_name_free(&fctx->domain,
7214 fctx->res->buckets[fctx->bucketnum].mctx);
7215 dns_name_init(&fctx->domain, NULL);
7216 result = dns_name_dup(fname,
7217 fctx->res->buckets[fctx->bucketnum].mctx,
7219 if (result != ISC_R_SUCCESS) {
7220 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7223 fctx->ns_ttl = fctx->nameservers.ttl;
7224 fctx->ns_ttl_ok = ISC_TRUE;
7225 fctx_cancelqueries(fctx, ISC_TRUE);
7226 fctx_cleanupfinds(fctx);
7227 fctx_cleanupaltfinds(fctx);
7228 fctx_cleanupforwaddrs(fctx);
7229 fctx_cleanupaltaddrs(fctx);
7234 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7235 } else if (resend) {
7237 * Resend (probably with changed options).
7239 FCTXTRACE("resend");
7240 inc_stats(fctx->res, dns_resstatscounter_retry);
7241 result = fctx_query(fctx, addrinfo, options);
7242 if (result != ISC_R_SUCCESS)
7243 fctx_done(fctx, result, __LINE__);
7244 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7246 * All has gone well so far, but we are waiting for the
7247 * DNSSEC validator to validate the answer.
7249 FCTXTRACE("wait for validator");
7250 fctx_cancelqueries(fctx, ISC_TRUE);
7252 * We must not retransmit while the validator is working;
7253 * it has references to the current rmessage.
7255 result = fctx_stopidletimer(fctx);
7256 if (result != ISC_R_SUCCESS)
7257 fctx_done(fctx, result, __LINE__);
7258 } else if (result == DNS_R_CHASEDSSERVERS) {
7260 add_bad(fctx, addrinfo, result, broken_type);
7261 fctx_cancelqueries(fctx, ISC_TRUE);
7262 fctx_cleanupfinds(fctx);
7263 fctx_cleanupforwaddrs(fctx);
7265 n = dns_name_countlabels(&fctx->name);
7266 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7268 FCTXTRACE("suspending DS lookup to find parent's NS records");
7270 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7272 NULL, NULL, NULL, 0, task,
7273 resume_dslookup, fctx,
7274 &fctx->nsrrset, NULL,
7276 if (result != ISC_R_SUCCESS)
7277 fctx_done(fctx, result, __LINE__);
7279 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7281 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7282 result = fctx_stopidletimer(fctx);
7283 if (result != ISC_R_SUCCESS)
7284 fctx_done(fctx, result, __LINE__);
7290 fctx_done(fctx, result, __LINE__);
7296 *** Resolver Methods
7299 destroy_badcache(dns_resolver_t *res) {
7300 dns_badcache_t *bad, *next;
7303 if (res->badcache != NULL) {
7304 for (i = 0; i < res->badhash; i++)
7305 for (bad = res->badcache[i]; bad != NULL;
7308 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7312 isc_mem_put(res->mctx, res->badcache,
7313 sizeof(*res->badcache) * res->badhash);
7314 res->badcache = NULL;
7316 INSIST(res->badcount == 0);
7321 destroy(dns_resolver_t *res) {
7325 REQUIRE(res->references == 0);
7326 REQUIRE(!res->priming);
7327 REQUIRE(res->primefetch == NULL);
7331 INSIST(res->nfctx == 0);
7333 DESTROYLOCK(&res->primelock);
7334 DESTROYLOCK(&res->nlock);
7335 DESTROYLOCK(&res->lock);
7336 for (i = 0; i < res->nbuckets; i++) {
7337 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7338 isc_task_shutdown(res->buckets[i].task);
7339 isc_task_detach(&res->buckets[i].task);
7340 DESTROYLOCK(&res->buckets[i].lock);
7341 isc_mem_detach(&res->buckets[i].mctx);
7343 isc_mem_put(res->mctx, res->buckets,
7344 res->nbuckets * sizeof(fctxbucket_t));
7345 if (res->dispatchv4 != NULL)
7346 dns_dispatch_detach(&res->dispatchv4);
7347 if (res->dispatchv6 != NULL)
7348 dns_dispatch_detach(&res->dispatchv6);
7349 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7350 ISC_LIST_UNLINK(res->alternates, a, link);
7352 dns_name_free(&a->_u._n.name, res->mctx);
7353 isc_mem_put(res->mctx, a, sizeof(*a));
7355 dns_resolver_reset_algorithms(res);
7356 destroy_badcache(res);
7357 dns_resolver_resetmustbesecure(res);
7359 isc_rwlock_destroy(&res->alglock);
7362 isc_rwlock_destroy(&res->mbslock);
7364 isc_timer_detach(&res->spillattimer);
7366 isc_mem_put(res->mctx, res, sizeof(*res));
7370 send_shutdown_events(dns_resolver_t *res) {
7371 isc_event_t *event, *next_event;
7375 * Caller must be holding the resolver lock.
7378 for (event = ISC_LIST_HEAD(res->whenshutdown);
7380 event = next_event) {
7381 next_event = ISC_LIST_NEXT(event, ev_link);
7382 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7383 etask = event->ev_sender;
7384 event->ev_sender = res;
7385 isc_task_sendanddetach(&etask, &event);
7390 empty_bucket(dns_resolver_t *res) {
7391 RTRACE("empty_bucket");
7395 INSIST(res->activebuckets > 0);
7396 res->activebuckets--;
7397 if (res->activebuckets == 0)
7398 send_shutdown_events(res);
7404 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7405 dns_resolver_t *res = event->ev_arg;
7406 isc_result_t result;
7408 isc_boolean_t logit = ISC_FALSE;
7410 REQUIRE(VALID_RESOLVER(res));
7415 INSIST(!res->exiting);
7416 if (res->spillat > res->spillatmin) {
7420 if (res->spillat <= res->spillatmin) {
7421 result = isc_timer_reset(res->spillattimer,
7422 isc_timertype_inactive, NULL,
7424 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7426 count = res->spillat;
7429 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7430 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7431 "clients-per-query decreased to %u", count);
7433 isc_event_free(&event);
7437 dns_resolver_create(dns_view_t *view,
7438 isc_taskmgr_t *taskmgr, unsigned int ntasks,
7439 isc_socketmgr_t *socketmgr,
7440 isc_timermgr_t *timermgr,
7441 unsigned int options,
7442 dns_dispatchmgr_t *dispatchmgr,
7443 dns_dispatch_t *dispatchv4,
7444 dns_dispatch_t *dispatchv6,
7445 dns_resolver_t **resp)
7447 dns_resolver_t *res;
7448 isc_result_t result = ISC_R_SUCCESS;
7449 unsigned int i, buckets_created = 0;
7450 isc_task_t *task = NULL;
7455 * Create a resolver.
7458 REQUIRE(DNS_VIEW_VALID(view));
7459 REQUIRE(ntasks > 0);
7460 REQUIRE(resp != NULL && *resp == NULL);
7461 REQUIRE(dispatchmgr != NULL);
7462 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7464 res = isc_mem_get(view->mctx, sizeof(*res));
7466 return (ISC_R_NOMEMORY);
7468 res->mctx = view->mctx;
7469 res->rdclass = view->rdclass;
7470 res->socketmgr = socketmgr;
7471 res->timermgr = timermgr;
7472 res->taskmgr = taskmgr;
7473 res->dispatchmgr = dispatchmgr;
7475 res->options = options;
7477 ISC_LIST_INIT(res->alternates);
7478 res->udpsize = RECV_BUFFER_SIZE;
7479 res->algorithms = NULL;
7480 res->badcache = NULL;
7484 res->mustbesecure = NULL;
7485 res->spillatmin = res->spillat = 10;
7486 res->spillatmax = 100;
7487 res->spillattimer = NULL;
7488 res->zero_no_soa_ttl = ISC_FALSE;
7489 res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7491 res->nextdisp = 0; /* meaningless at this point, but init it */
7492 res->nbuckets = ntasks;
7493 res->activebuckets = ntasks;
7494 res->buckets = isc_mem_get(view->mctx,
7495 ntasks * sizeof(fctxbucket_t));
7496 if (res->buckets == NULL) {
7497 result = ISC_R_NOMEMORY;
7500 for (i = 0; i < ntasks; i++) {
7501 result = isc_mutex_init(&res->buckets[i].lock);
7502 if (result != ISC_R_SUCCESS)
7503 goto cleanup_buckets;
7504 res->buckets[i].task = NULL;
7505 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7506 if (result != ISC_R_SUCCESS) {
7507 DESTROYLOCK(&res->buckets[i].lock);
7508 goto cleanup_buckets;
7510 res->buckets[i].mctx = NULL;
7511 snprintf(name, sizeof(name), "res%u", i);
7512 #ifdef ISC_PLATFORM_USETHREADS
7514 * Use a separate memory context for each bucket to reduce
7515 * contention among multiple threads. Do this only when
7516 * enabling threads because it will be require more memory.
7518 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7519 if (result != ISC_R_SUCCESS) {
7520 isc_task_detach(&res->buckets[i].task);
7521 DESTROYLOCK(&res->buckets[i].lock);
7522 goto cleanup_buckets;
7524 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7526 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7528 isc_task_setname(res->buckets[i].task, name, res);
7529 ISC_LIST_INIT(res->buckets[i].fctxs);
7530 res->buckets[i].exiting = ISC_FALSE;
7534 res->dispatchv4 = NULL;
7535 if (dispatchv4 != NULL) {
7536 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
7537 dispattr = dns_dispatch_getattributes(dispatchv4);
7539 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7542 res->dispatchv6 = NULL;
7543 if (dispatchv6 != NULL) {
7544 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
7545 dispattr = dns_dispatch_getattributes(dispatchv6);
7547 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7550 res->references = 1;
7551 res->exiting = ISC_FALSE;
7552 res->frozen = ISC_FALSE;
7553 ISC_LIST_INIT(res->whenshutdown);
7554 res->priming = ISC_FALSE;
7555 res->primefetch = NULL;
7558 result = isc_mutex_init(&res->lock);
7559 if (result != ISC_R_SUCCESS)
7560 goto cleanup_dispatches;
7562 result = isc_mutex_init(&res->nlock);
7563 if (result != ISC_R_SUCCESS)
7566 result = isc_mutex_init(&res->primelock);
7567 if (result != ISC_R_SUCCESS)
7571 result = isc_task_create(taskmgr, 0, &task);
7572 if (result != ISC_R_SUCCESS)
7573 goto cleanup_primelock;
7575 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7576 task, spillattimer_countdown, res,
7577 &res->spillattimer);
7578 isc_task_detach(&task);
7579 if (result != ISC_R_SUCCESS)
7580 goto cleanup_primelock;
7583 result = isc_rwlock_init(&res->alglock, 0, 0);
7584 if (result != ISC_R_SUCCESS)
7585 goto cleanup_spillattimer;
7588 result = isc_rwlock_init(&res->mbslock, 0, 0);
7589 if (result != ISC_R_SUCCESS)
7590 goto cleanup_alglock;
7593 res->magic = RES_MAGIC;
7597 return (ISC_R_SUCCESS);
7602 isc_rwlock_destroy(&res->alglock);
7605 #if USE_ALGLOCK || USE_MBSLOCK
7606 cleanup_spillattimer:
7607 isc_timer_detach(&res->spillattimer);
7611 DESTROYLOCK(&res->primelock);
7614 DESTROYLOCK(&res->nlock);
7617 DESTROYLOCK(&res->lock);
7620 if (res->dispatchv6 != NULL)
7621 dns_dispatch_detach(&res->dispatchv6);
7622 if (res->dispatchv4 != NULL)
7623 dns_dispatch_detach(&res->dispatchv4);
7626 for (i = 0; i < buckets_created; i++) {
7627 isc_mem_detach(&res->buckets[i].mctx);
7628 DESTROYLOCK(&res->buckets[i].lock);
7629 isc_task_shutdown(res->buckets[i].task);
7630 isc_task_detach(&res->buckets[i].task);
7632 isc_mem_put(view->mctx, res->buckets,
7633 res->nbuckets * sizeof(fctxbucket_t));
7636 isc_mem_put(view->mctx, res, sizeof(*res));
7643 prime_done(isc_task_t *task, isc_event_t *event) {
7644 dns_resolver_t *res;
7645 dns_fetchevent_t *fevent;
7647 dns_db_t *db = NULL;
7649 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7650 fevent = (dns_fetchevent_t *)event;
7651 res = event->ev_arg;
7652 REQUIRE(VALID_RESOLVER(res));
7658 INSIST(res->priming);
7659 res->priming = ISC_FALSE;
7660 LOCK(&res->primelock);
7661 fetch = res->primefetch;
7662 res->primefetch = NULL;
7663 UNLOCK(&res->primelock);
7667 if (fevent->result == ISC_R_SUCCESS &&
7668 res->view->cache != NULL && res->view->hints != NULL) {
7669 dns_cache_attachdb(res->view->cache, &db);
7670 dns_root_checkhints(res->view, res->view->hints, db);
7674 if (fevent->node != NULL)
7675 dns_db_detachnode(fevent->db, &fevent->node);
7676 if (fevent->db != NULL)
7677 dns_db_detach(&fevent->db);
7678 if (dns_rdataset_isassociated(fevent->rdataset))
7679 dns_rdataset_disassociate(fevent->rdataset);
7680 INSIST(fevent->sigrdataset == NULL);
7682 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7684 isc_event_free(&event);
7685 dns_resolver_destroyfetch(&fetch);
7689 dns_resolver_prime(dns_resolver_t *res) {
7690 isc_boolean_t want_priming = ISC_FALSE;
7691 dns_rdataset_t *rdataset;
7692 isc_result_t result;
7694 REQUIRE(VALID_RESOLVER(res));
7695 REQUIRE(res->frozen);
7697 RTRACE("dns_resolver_prime");
7701 if (!res->exiting && !res->priming) {
7702 INSIST(res->primefetch == NULL);
7703 res->priming = ISC_TRUE;
7704 want_priming = ISC_TRUE;
7711 * To avoid any possible recursive locking problems, we
7712 * start the priming fetch like any other fetch, and holding
7713 * no resolver locks. No one else will try to start it
7714 * because we're the ones who set res->priming to true.
7715 * Any other callers of dns_resolver_prime() while we're
7716 * running will see that res->priming is already true and
7720 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7721 if (rdataset == NULL) {
7723 INSIST(res->priming);
7724 INSIST(res->primefetch == NULL);
7725 res->priming = ISC_FALSE;
7729 dns_rdataset_init(rdataset);
7730 LOCK(&res->primelock);
7731 result = dns_resolver_createfetch(res, dns_rootname,
7733 NULL, NULL, NULL, 0,
7734 res->buckets[0].task,
7736 res, rdataset, NULL,
7738 UNLOCK(&res->primelock);
7739 if (result != ISC_R_SUCCESS) {
7741 INSIST(res->priming);
7742 res->priming = ISC_FALSE;
7750 dns_resolver_freeze(dns_resolver_t *res) {
7755 REQUIRE(VALID_RESOLVER(res));
7757 res->frozen = ISC_TRUE;
7761 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7762 REQUIRE(VALID_RESOLVER(source));
7763 REQUIRE(targetp != NULL && *targetp == NULL);
7765 RRTRACE(source, "attach");
7766 LOCK(&source->lock);
7767 REQUIRE(!source->exiting);
7769 INSIST(source->references > 0);
7770 source->references++;
7771 INSIST(source->references != 0);
7772 UNLOCK(&source->lock);
7778 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7779 isc_event_t **eventp)
7784 REQUIRE(VALID_RESOLVER(res));
7785 REQUIRE(eventp != NULL);
7792 if (res->exiting && res->activebuckets == 0) {
7794 * We're already shutdown. Send the event.
7796 event->ev_sender = res;
7797 isc_task_send(task, &event);
7800 isc_task_attach(task, &clone);
7801 event->ev_sender = clone;
7802 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7809 dns_resolver_shutdown(dns_resolver_t *res) {
7813 isc_result_t result;
7815 REQUIRE(VALID_RESOLVER(res));
7821 if (!res->exiting) {
7823 res->exiting = ISC_TRUE;
7825 for (i = 0; i < res->nbuckets; i++) {
7826 LOCK(&res->buckets[i].lock);
7827 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7829 fctx = ISC_LIST_NEXT(fctx, link))
7830 fctx_shutdown(fctx);
7831 if (res->dispatchv4 != NULL && !res->exclusivev4) {
7832 sock = dns_dispatch_getsocket(res->dispatchv4);
7833 isc_socket_cancel(sock, res->buckets[i].task,
7834 ISC_SOCKCANCEL_ALL);
7836 if (res->dispatchv6 != NULL && !res->exclusivev6) {
7837 sock = dns_dispatch_getsocket(res->dispatchv6);
7838 isc_socket_cancel(sock, res->buckets[i].task,
7839 ISC_SOCKCANCEL_ALL);
7841 res->buckets[i].exiting = ISC_TRUE;
7842 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7843 INSIST(res->activebuckets > 0);
7844 res->activebuckets--;
7846 UNLOCK(&res->buckets[i].lock);
7848 if (res->activebuckets == 0)
7849 send_shutdown_events(res);
7850 result = isc_timer_reset(res->spillattimer,
7851 isc_timertype_inactive, NULL,
7853 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7860 dns_resolver_detach(dns_resolver_t **resp) {
7861 dns_resolver_t *res;
7862 isc_boolean_t need_destroy = ISC_FALSE;
7864 REQUIRE(resp != NULL);
7866 REQUIRE(VALID_RESOLVER(res));
7872 INSIST(res->references > 0);
7874 if (res->references == 0) {
7875 INSIST(res->exiting && res->activebuckets == 0);
7876 need_destroy = ISC_TRUE;
7887 static inline isc_boolean_t
7888 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7889 unsigned int options)
7892 * Don't match fetch contexts that are shutting down.
7894 if (fctx->cloned || fctx->state == fetchstate_done ||
7895 ISC_LIST_EMPTY(fctx->events))
7898 if (fctx->type != type || fctx->options != options)
7900 return (dns_name_equal(&fctx->name, name));
7904 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7905 char namebuf[DNS_NAME_FORMATSIZE];
7906 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7907 int level = ISC_LOG_DEBUG(1);
7909 if (! isc_log_wouldlog(dns_lctx, level))
7912 dns_name_format(name, namebuf, sizeof(namebuf));
7913 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7915 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7916 DNS_LOGMODULE_RESOLVER, level,
7917 "createfetch: %s %s", namebuf, typebuf);
7921 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7922 dns_rdatatype_t type,
7923 dns_name_t *domain, dns_rdataset_t *nameservers,
7924 dns_forwarders_t *forwarders,
7925 unsigned int options, isc_task_t *task,
7926 isc_taskaction_t action, void *arg,
7927 dns_rdataset_t *rdataset,
7928 dns_rdataset_t *sigrdataset,
7929 dns_fetch_t **fetchp)
7931 return (dns_resolver_createfetch2(res, name, type, domain,
7932 nameservers, forwarders, NULL, 0,
7933 options, task, action, arg,
7934 rdataset, sigrdataset, fetchp));
7938 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7939 dns_rdatatype_t type,
7940 dns_name_t *domain, dns_rdataset_t *nameservers,
7941 dns_forwarders_t *forwarders,
7942 isc_sockaddr_t *client, dns_messageid_t id,
7943 unsigned int options, isc_task_t *task,
7944 isc_taskaction_t action, void *arg,
7945 dns_rdataset_t *rdataset,
7946 dns_rdataset_t *sigrdataset,
7947 dns_fetch_t **fetchp)
7950 fetchctx_t *fctx = NULL;
7951 isc_result_t result = ISC_R_SUCCESS;
7952 unsigned int bucketnum;
7953 isc_boolean_t new_fctx = ISC_FALSE;
7955 unsigned int count = 0;
7956 unsigned int spillat;
7957 unsigned int spillatmin;
7961 REQUIRE(VALID_RESOLVER(res));
7962 REQUIRE(res->frozen);
7963 /* XXXRTH Check for meta type */
7964 if (domain != NULL) {
7965 REQUIRE(DNS_RDATASET_VALID(nameservers));
7966 REQUIRE(nameservers->type == dns_rdatatype_ns);
7968 REQUIRE(nameservers == NULL);
7969 REQUIRE(forwarders == NULL);
7970 REQUIRE(!dns_rdataset_isassociated(rdataset));
7971 REQUIRE(sigrdataset == NULL ||
7972 !dns_rdataset_isassociated(sigrdataset));
7973 REQUIRE(fetchp != NULL && *fetchp == NULL);
7975 log_fetch(name, type);
7978 * XXXRTH use a mempool?
7980 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7982 return (ISC_R_NOMEMORY);
7984 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7987 spillat = res->spillat;
7988 spillatmin = res->spillatmin;
7990 LOCK(&res->buckets[bucketnum].lock);
7992 if (res->buckets[bucketnum].exiting) {
7993 result = ISC_R_SHUTTINGDOWN;
7997 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7998 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
8000 fctx = ISC_LIST_NEXT(fctx, link)) {
8001 if (fctx_match(fctx, name, type, options))
8007 * Is this a duplicate?
8009 if (fctx != NULL && client != NULL) {
8010 dns_fetchevent_t *fevent;
8011 for (fevent = ISC_LIST_HEAD(fctx->events);
8013 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
8014 if (fevent->client != NULL && fevent->id == id &&
8015 isc_sockaddr_equal(fevent->client, client)) {
8016 result = DNS_R_DUPLICATE;
8022 if (count >= spillatmin && spillatmin != 0) {
8023 INSIST(fctx != NULL);
8024 if (count >= spillat)
8025 fctx->spilled = ISC_TRUE;
8026 if (fctx->spilled) {
8027 result = DNS_R_DROP;
8033 result = fctx_create(res, name, type, domain, nameservers,
8034 options, bucketnum, &fctx);
8035 if (result != ISC_R_SUCCESS)
8037 new_fctx = ISC_TRUE;
8040 result = fctx_join(fctx, task, client, id, action, arg,
8041 rdataset, sigrdataset, fetch);
8043 if (result == ISC_R_SUCCESS) {
8047 event = &fctx->control_event;
8048 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
8049 DNS_EVENT_FETCHCONTROL,
8050 fctx_start, fctx, NULL,
8052 isc_task_send(res->buckets[bucketnum].task, &event);
8055 * We don't care about the result of fctx_destroy()
8056 * since we know we're not exiting.
8058 (void)fctx_destroy(fctx);
8063 UNLOCK(&res->buckets[bucketnum].lock);
8065 if (result == ISC_R_SUCCESS) {
8069 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8075 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
8077 dns_resolver_t *res;
8078 dns_fetchevent_t *event, *next_event;
8081 REQUIRE(DNS_FETCH_VALID(fetch));
8082 fctx = fetch->private;
8083 REQUIRE(VALID_FCTX(fctx));
8086 FTRACE("cancelfetch");
8088 LOCK(&res->buckets[fctx->bucketnum].lock);
8091 * Find the completion event for this fetch (as opposed
8092 * to those for other fetches that have joined the same
8093 * fctx) and send it with result = ISC_R_CANCELED.
8096 if (fctx->state != fetchstate_done) {
8097 for (event = ISC_LIST_HEAD(fctx->events);
8099 event = next_event) {
8100 next_event = ISC_LIST_NEXT(event, ev_link);
8101 if (event->fetch == fetch) {
8102 ISC_LIST_UNLINK(fctx->events, event, ev_link);
8107 if (event != NULL) {
8108 etask = event->ev_sender;
8109 event->ev_sender = fctx;
8110 event->result = ISC_R_CANCELED;
8111 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8114 * The fctx continues running even if no fetches remain;
8115 * the answer is still cached.
8118 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8122 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8124 dns_resolver_t *res;
8125 dns_fetchevent_t *event, *next_event;
8127 unsigned int bucketnum;
8128 isc_boolean_t bucket_empty;
8130 REQUIRE(fetchp != NULL);
8132 REQUIRE(DNS_FETCH_VALID(fetch));
8133 fctx = fetch->private;
8134 REQUIRE(VALID_FCTX(fctx));
8137 FTRACE("destroyfetch");
8139 bucketnum = fctx->bucketnum;
8140 LOCK(&res->buckets[bucketnum].lock);
8143 * Sanity check: the caller should have gotten its event before
8144 * trying to destroy the fetch.
8147 if (fctx->state != fetchstate_done) {
8148 for (event = ISC_LIST_HEAD(fctx->events);
8150 event = next_event) {
8151 next_event = ISC_LIST_NEXT(event, ev_link);
8152 RUNTIME_CHECK(event->fetch != fetch);
8156 bucket_empty = fctx_decreference(fctx);
8158 UNLOCK(&res->buckets[bucketnum].lock);
8160 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8168 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8169 isc_logcategory_t *category, isc_logmodule_t *module,
8170 int level, isc_boolean_t duplicateok)
8173 dns_resolver_t *res;
8174 char domainbuf[DNS_NAME_FORMATSIZE];
8176 REQUIRE(DNS_FETCH_VALID(fetch));
8177 fctx = fetch->private;
8178 REQUIRE(VALID_FCTX(fctx));
8181 LOCK(&res->buckets[fctx->bucketnum].lock);
8183 INSIST(fctx->exitline >= 0);
8184 if (!fctx->logged || duplicateok) {
8185 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8186 isc_log_write(lctx, category, module, level,
8187 "fetch completed at %s:%d for %s in "
8188 "%" ISC_PRINT_QUADFORMAT "u."
8189 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8190 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8191 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8192 "adberr:%u,findfail:%u,valfail:%u]",
8193 __FILE__, fctx->exitline, fctx->info,
8194 fctx->duration / 1000000,
8195 fctx->duration % 1000000,
8196 isc_result_totext(fctx->result),
8197 isc_result_totext(fctx->vresult), domainbuf,
8198 fctx->referrals, fctx->restarts,
8199 fctx->querysent, fctx->timeouts, fctx->lamecount,
8200 fctx->neterr, fctx->badresp, fctx->adberr,
8201 fctx->findfail, fctx->valfail);
8202 fctx->logged = ISC_TRUE;
8205 UNLOCK(&res->buckets[fctx->bucketnum].lock);
8209 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8210 REQUIRE(VALID_RESOLVER(resolver));
8211 return (resolver->dispatchmgr);
8215 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8216 REQUIRE(VALID_RESOLVER(resolver));
8217 return (resolver->dispatchv4);
8221 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8222 REQUIRE(VALID_RESOLVER(resolver));
8223 return (resolver->dispatchv6);
8227 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8228 REQUIRE(VALID_RESOLVER(resolver));
8229 return (resolver->socketmgr);
8233 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8234 REQUIRE(VALID_RESOLVER(resolver));
8235 return (resolver->taskmgr);
8239 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8240 REQUIRE(VALID_RESOLVER(resolver));
8241 return (resolver->lame_ttl);
8245 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8246 REQUIRE(VALID_RESOLVER(resolver));
8247 resolver->lame_ttl = lame_ttl;
8251 dns_resolver_nrunning(dns_resolver_t *resolver) {
8253 LOCK(&resolver->nlock);
8254 n = resolver->nfctx;
8255 UNLOCK(&resolver->nlock);
8260 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8261 dns_name_t *name, in_port_t port) {
8263 isc_result_t result;
8265 REQUIRE(VALID_RESOLVER(resolver));
8266 REQUIRE(!resolver->frozen);
8267 REQUIRE((alt == NULL) ^ (name == NULL));
8269 a = isc_mem_get(resolver->mctx, sizeof(*a));
8271 return (ISC_R_NOMEMORY);
8273 a->isaddress = ISC_TRUE;
8276 a->isaddress = ISC_FALSE;
8277 a->_u._n.port = port;
8278 dns_name_init(&a->_u._n.name, NULL);
8279 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8280 if (result != ISC_R_SUCCESS) {
8281 isc_mem_put(resolver->mctx, a, sizeof(*a));
8285 ISC_LINK_INIT(a, link);
8286 ISC_LIST_APPEND(resolver->alternates, a, link);
8288 return (ISC_R_SUCCESS);
8292 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8293 REQUIRE(VALID_RESOLVER(resolver));
8294 resolver->udpsize = udpsize;
8298 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8299 REQUIRE(VALID_RESOLVER(resolver));
8300 return (resolver->udpsize);
8304 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8306 dns_badcache_t *bad, *prev, *next;
8308 REQUIRE(VALID_RESOLVER(resolver));
8310 LOCK(&resolver->lock);
8311 if (resolver->badcache == NULL)
8316 isc_result_t result;
8317 result = isc_time_now(&now);
8318 if (result != ISC_R_SUCCESS)
8319 isc_time_settoepoch(&now);
8320 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8322 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8325 n = isc_time_compare(&bad->expire, &now);
8326 if (n < 0 || dns_name_equal(name, &bad->name)) {
8328 resolver->badcache[i] = bad->next;
8330 prev->next = bad->next;
8331 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8333 resolver->badcount--;
8338 destroy_badcache(resolver);
8341 UNLOCK(&resolver->lock);
8346 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8347 unsigned int newsize;
8348 dns_badcache_t **new, *bad, *next;
8352 newsize = resolver->badhash * 2 + 1;
8354 newsize = (resolver->badhash - 1) / 2;
8356 new = isc_mem_get(resolver->mctx,
8357 sizeof(*resolver->badcache) * newsize);
8360 memset(new, 0, sizeof(*resolver->badcache) * newsize);
8361 for (i = 0; i < resolver->badhash; i++) {
8362 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8364 if (isc_time_compare(&bad->expire, now) < 0) {
8365 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8367 resolver->badcount--;
8369 bad->next = new[bad->hashval % newsize];
8370 new[bad->hashval % newsize] = bad;
8374 isc_mem_put(resolver->mctx, resolver->badcache,
8375 sizeof(*resolver->badcache) * resolver->badhash);
8376 resolver->badhash = newsize;
8377 resolver->badcache = new;
8381 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8382 dns_rdatatype_t type, isc_time_t *expire)
8385 isc_result_t result = ISC_R_SUCCESS;
8386 unsigned int i, hashval;
8387 dns_badcache_t *bad, *prev, *next;
8389 REQUIRE(VALID_RESOLVER(resolver));
8391 LOCK(&resolver->lock);
8392 if (resolver->badcache == NULL) {
8393 resolver->badcache = isc_mem_get(resolver->mctx,
8394 sizeof(*resolver->badcache) *
8396 if (resolver->badcache == NULL)
8398 resolver->badhash = DNS_BADCACHE_SIZE;
8399 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8403 result = isc_time_now(&now);
8404 if (result != ISC_R_SUCCESS)
8405 isc_time_settoepoch(&now);
8406 hashval = dns_name_hash(name, ISC_FALSE);
8407 i = hashval % resolver->badhash;
8409 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8411 if (bad->type == type && dns_name_equal(name, &bad->name))
8413 if (isc_time_compare(&bad->expire, &now) < 0) {
8415 resolver->badcache[i] = bad->next;
8417 prev->next = bad->next;
8418 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8420 resolver->badcount--;
8425 isc_buffer_t buffer;
8426 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8430 bad->hashval = hashval;
8431 isc_buffer_init(&buffer, bad + 1, name->length);
8432 dns_name_init(&bad->name, NULL);
8433 dns_name_copy(name, &bad->name, &buffer);
8434 bad->next = resolver->badcache[i];
8435 resolver->badcache[i] = bad;
8436 resolver->badcount++;
8437 if (resolver->badcount > resolver->badhash * 8)
8438 resizehash(resolver, &now, ISC_TRUE);
8439 if (resolver->badcount < resolver->badhash * 2 &&
8440 resolver->badhash > DNS_BADCACHE_SIZE)
8441 resizehash(resolver, &now, ISC_FALSE);
8443 bad->expire = *expire;
8445 UNLOCK(&resolver->lock);
8449 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8450 dns_rdatatype_t type, isc_time_t *now)
8452 dns_badcache_t *bad, *prev, *next;
8453 isc_boolean_t answer = ISC_FALSE;
8456 REQUIRE(VALID_RESOLVER(resolver));
8458 LOCK(&resolver->lock);
8459 if (resolver->badcache == NULL)
8462 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8464 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8467 * Search the hash list. Clean out expired records as we go.
8469 if (isc_time_compare(&bad->expire, now) < 0) {
8471 prev->next = bad->next;
8473 resolver->badcache[i] = bad->next;
8474 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8476 resolver->badcount--;
8479 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8487 * Slow sweep to clean out stale records.
8489 i = resolver->badsweep++ % resolver->badhash;
8490 bad = resolver->badcache[i];
8491 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8492 resolver->badcache[i] = bad->next;
8493 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8495 resolver->badcount--;
8499 UNLOCK(&resolver->lock);
8504 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8505 char namebuf[DNS_NAME_FORMATSIZE];
8506 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8507 dns_badcache_t *bad, *next, *prev;
8512 LOCK(&resolver->lock);
8513 fprintf(fp, ";\n; Bad cache\n;\n");
8515 if (resolver->badcache == NULL)
8519 for (i = 0; i < resolver->badhash; i++) {
8521 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8523 if (isc_time_compare(&bad->expire, &now) < 0) {
8525 prev->next = bad->next;
8527 resolver->badcache[i] = bad->next;
8528 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8530 resolver->badcount--;
8534 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8535 dns_rdatatype_format(bad->type, typebuf,
8537 t = isc_time_microdiff(&bad->expire, &now);
8539 fprintf(fp, "; %s/%s [ttl "
8540 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8541 namebuf, typebuf, t);
8546 UNLOCK(&resolver->lock);
8550 free_algorithm(void *node, void *arg) {
8551 unsigned char *algorithms = node;
8552 isc_mem_t *mctx = arg;
8554 isc_mem_put(mctx, algorithms, *algorithms);
8558 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8560 REQUIRE(VALID_RESOLVER(resolver));
8563 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8565 if (resolver->algorithms != NULL)
8566 dns_rbt_destroy(&resolver->algorithms);
8568 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8573 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8576 unsigned int len, mask;
8578 unsigned char *algorithms;
8579 isc_result_t result;
8580 dns_rbtnode_t *node = NULL;
8582 REQUIRE(VALID_RESOLVER(resolver));
8584 return (ISC_R_RANGE);
8587 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8589 if (resolver->algorithms == NULL) {
8590 result = dns_rbt_create(resolver->mctx, free_algorithm,
8591 resolver->mctx, &resolver->algorithms);
8592 if (result != ISC_R_SUCCESS)
8597 mask = 1 << (alg%8);
8599 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8601 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8602 algorithms = node->data;
8603 if (algorithms == NULL || len > *algorithms) {
8604 new = isc_mem_get(resolver->mctx, len);
8606 result = ISC_R_NOMEMORY;
8609 memset(new, 0, len);
8610 if (algorithms != NULL)
8611 memcpy(new, algorithms, *algorithms);
8615 if (algorithms != NULL)
8616 isc_mem_put(resolver->mctx, algorithms,
8619 algorithms[len-1] |= mask;
8621 result = ISC_R_SUCCESS;
8624 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8630 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8633 unsigned int len, mask;
8634 unsigned char *algorithms;
8636 isc_result_t result;
8637 isc_boolean_t found = ISC_FALSE;
8639 REQUIRE(VALID_RESOLVER(resolver));
8642 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8644 if (resolver->algorithms == NULL)
8646 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8647 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8649 mask = 1 << (alg%8);
8651 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8656 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8660 return (dst_algorithm_supported(alg));
8664 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8667 return (dns_ds_digest_supported(digest));
8671 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8673 REQUIRE(VALID_RESOLVER(resolver));
8676 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8678 if (resolver->mustbesecure != NULL)
8679 dns_rbt_destroy(&resolver->mustbesecure);
8681 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8685 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8688 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8689 isc_boolean_t value)
8691 isc_result_t result;
8693 REQUIRE(VALID_RESOLVER(resolver));
8696 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8698 if (resolver->mustbesecure == NULL) {
8699 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8700 &resolver->mustbesecure);
8701 if (result != ISC_R_SUCCESS)
8704 result = dns_rbt_addname(resolver->mustbesecure, name,
8705 value ? &yes : &no);
8708 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8714 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8716 isc_boolean_t value = ISC_FALSE;
8717 isc_result_t result;
8719 REQUIRE(VALID_RESOLVER(resolver));
8722 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8724 if (resolver->mustbesecure == NULL)
8726 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8727 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8728 value = *(isc_boolean_t*)data;
8731 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8737 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8738 isc_uint32_t *min, isc_uint32_t *max)
8740 REQUIRE(VALID_RESOLVER(resolver));
8742 LOCK(&resolver->lock);
8744 *cur = resolver->spillat;
8746 *min = resolver->spillatmin;
8748 *max = resolver->spillatmax;
8749 UNLOCK(&resolver->lock);
8753 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
8756 REQUIRE(VALID_RESOLVER(resolver));
8758 LOCK(&resolver->lock);
8759 resolver->spillatmin = resolver->spillat = min;
8760 resolver->spillatmax = max;
8761 UNLOCK(&resolver->lock);
8765 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
8766 REQUIRE(VALID_RESOLVER(resolver));
8768 return (resolver->zero_no_soa_ttl);
8772 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
8773 REQUIRE(VALID_RESOLVER(resolver));
8775 resolver->zero_no_soa_ttl = state;
8779 dns_resolver_getoptions(dns_resolver_t *resolver) {
8780 REQUIRE(VALID_RESOLVER(resolver));
8782 return (resolver->options);
8786 dns_resolver_gettimeout(dns_resolver_t *resolver) {
8787 REQUIRE(VALID_RESOLVER(resolver));
8789 return (resolver->query_timeout);
8793 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
8794 REQUIRE(VALID_RESOLVER(resolver));
8797 seconds = DEFAULT_QUERY_TIMEOUT;
8798 if (seconds > MAXIMUM_QUERY_TIMEOUT)
8799 seconds = MAXIMUM_QUERY_TIMEOUT;
8801 resolver->query_timeout = seconds;