2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: resolver.c,v 1.355.12.44.2.5 2010/02/25 10:56:01 tbox Exp $ */
24 #include <isc/platform.h>
25 #include <isc/print.h>
26 #include <isc/string.h>
27 #include <isc/random.h>
29 #include <isc/stats.h>
30 #include <isc/timer.h>
35 #include <dns/cache.h>
37 #include <dns/dispatch.h>
39 #include <dns/events.h>
40 #include <dns/forward.h>
41 #include <dns/keytable.h>
43 #include <dns/message.h>
44 #include <dns/ncache.h>
45 #include <dns/opcode.h>
48 #include <dns/rcode.h>
49 #include <dns/rdata.h>
50 #include <dns/rdataclass.h>
51 #include <dns/rdatalist.h>
52 #include <dns/rdataset.h>
53 #include <dns/rdatastruct.h>
54 #include <dns/rdatatype.h>
55 #include <dns/resolver.h>
56 #include <dns/result.h>
57 #include <dns/rootns.h>
58 #include <dns/stats.h>
60 #include <dns/validator.h>
62 #define DNS_RESOLVER_TRACE
63 #ifdef DNS_RESOLVER_TRACE
64 #define RTRACE(m) isc_log_write(dns_lctx, \
65 DNS_LOGCATEGORY_RESOLVER, \
66 DNS_LOGMODULE_RESOLVER, \
68 "res %p: %s", res, (m))
69 #define RRTRACE(r, m) isc_log_write(dns_lctx, \
70 DNS_LOGCATEGORY_RESOLVER, \
71 DNS_LOGMODULE_RESOLVER, \
73 "res %p: %s", (r), (m))
74 #define FCTXTRACE(m) isc_log_write(dns_lctx, \
75 DNS_LOGCATEGORY_RESOLVER, \
76 DNS_LOGMODULE_RESOLVER, \
78 "fctx %p(%s'): %s", fctx, fctx->info, (m))
79 #define FCTXTRACE2(m1, m2) \
80 isc_log_write(dns_lctx, \
81 DNS_LOGCATEGORY_RESOLVER, \
82 DNS_LOGMODULE_RESOLVER, \
84 "fctx %p(%s): %s %s", \
85 fctx, fctx->info, (m1), (m2))
86 #define FTRACE(m) isc_log_write(dns_lctx, \
87 DNS_LOGCATEGORY_RESOLVER, \
88 DNS_LOGMODULE_RESOLVER, \
90 "fetch %p (fctx %p(%s)): %s", \
91 fetch, fetch->private, \
92 fetch->private->info, (m))
93 #define QTRACE(m) isc_log_write(dns_lctx, \
94 DNS_LOGCATEGORY_RESOLVER, \
95 DNS_LOGMODULE_RESOLVER, \
97 "resquery %p (fctx %p(%s)): %s", \
99 query->fctx->info, (m))
102 #define RRTRACE(r, m)
109 * Maximum EDNS0 input packet size.
111 #define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */
114 * This defines the maximum number of timeouts we will permit before we
115 * disable EDNS0 on the query.
117 #define MAX_EDNS0_TIMEOUTS 3
119 typedef struct fetchctx fetchctx_t;
121 typedef struct query {
122 /* Locked by task event serialization. */
126 dns_dispatchmgr_t * dispatchmgr;
127 dns_dispatch_t * dispatch;
128 isc_boolean_t exclusivesocket;
129 dns_adbaddrinfo_t * addrinfo;
130 isc_socket_t * tcpsocket;
133 dns_dispentry_t * dispentry;
134 ISC_LINK(struct query) link;
137 dns_tsigkey_t *tsigkey;
138 unsigned int options;
139 unsigned int attributes;
141 unsigned int connects;
142 unsigned char data[512];
145 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
146 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
148 #define RESQUERY_ATTR_CANCELED 0x02
150 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
151 #define RESQUERY_CANCELED(q) (((q)->attributes & \
152 RESQUERY_ATTR_CANCELED) != 0)
153 #define RESQUERY_SENDING(q) ((q)->sends > 0)
156 fetchstate_init = 0, /*%< Start event has not run yet. */
158 fetchstate_done /*%< FETCHDONE events posted. */
162 badns_unreachable = 0,
170 dns_resolver_t * res;
172 dns_rdatatype_t type;
173 unsigned int options;
174 unsigned int bucketnum;
176 /*% Locked by appropriate bucket lock. */
178 isc_boolean_t want_shutdown;
179 isc_boolean_t cloned;
180 isc_boolean_t spilled;
181 unsigned int references;
182 isc_event_t control_event;
183 ISC_LINK(struct fetchctx) link;
184 ISC_LIST(dns_fetchevent_t) events;
185 /*% Locked by task event serialization. */
187 dns_rdataset_t nameservers;
188 unsigned int attributes;
191 isc_interval_t interval;
192 dns_message_t * qmessage;
193 dns_message_t * rmessage;
194 ISC_LIST(resquery_t) queries;
195 dns_adbfindlist_t finds;
196 dns_adbfind_t * find;
197 dns_adbfindlist_t altfinds;
198 dns_adbfind_t * altfind;
199 dns_adbaddrinfolist_t forwaddrs;
200 dns_adbaddrinfolist_t altaddrs;
201 isc_sockaddrlist_t forwarders;
202 dns_fwdpolicy_t fwdpolicy;
203 isc_sockaddrlist_t bad;
204 isc_sockaddrlist_t edns;
205 isc_sockaddrlist_t edns512;
206 dns_validator_t *validator;
207 ISC_LIST(dns_validator_t) validators;
212 * The number of events we're waiting for.
214 unsigned int pending;
217 * The number of times we've "restarted" the current
218 * nameserver set. This acts as a failsafe to prevent
219 * us from pounding constantly on a particular set of
220 * servers that, for whatever reason, are not giving
221 * us useful responses, but are responding in such a
222 * way that they are not marked "bad".
224 unsigned int restarts;
227 * The number of timeouts that have occurred since we
228 * last successfully received a response packet. This
229 * is used for EDNS0 black hole detection.
231 unsigned int timeouts;
234 * Look aside state for DS lookups.
237 dns_fetch_t * nsfetch;
238 dns_rdataset_t nsrrset;
241 * Number of queries that reference this context.
243 unsigned int nqueries;
246 * The reason to print when logging a successful
247 * response to a query.
252 * Fetch-local statistics for detailed logging.
254 isc_result_t result; /*%< fetch result */
255 isc_result_t vresult; /*%< validation result */
258 isc_uint64_t duration;
259 isc_boolean_t logged;
260 unsigned int querysent;
261 unsigned int referrals;
262 unsigned int lamecount;
264 unsigned int badresp;
266 unsigned int findfail;
267 unsigned int valfail;
268 isc_boolean_t timeout;
271 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
272 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
274 #define FCTX_ATTR_HAVEANSWER 0x0001
275 #define FCTX_ATTR_GLUING 0x0002
276 #define FCTX_ATTR_ADDRWAIT 0x0004
277 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
278 #define FCTX_ATTR_WANTCACHE 0x0010
279 #define FCTX_ATTR_WANTNCACHE 0x0020
280 #define FCTX_ATTR_NEEDEDNS0 0x0040
281 #define FCTX_ATTR_TRIEDFIND 0x0080
282 #define FCTX_ATTR_TRIEDALT 0x0100
284 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
286 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
288 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
290 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
292 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
293 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
294 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
295 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
296 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
299 dns_adbaddrinfo_t * addrinfo;
305 fetchctx_t * private;
308 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
309 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
311 typedef struct fctxbucket {
314 ISC_LIST(fetchctx_t) fctxs;
315 isc_boolean_t exiting;
319 typedef struct alternate {
320 isc_boolean_t isaddress;
328 ISC_LINK(struct alternate) link;
331 typedef struct dns_badcache dns_badcache_t;
332 struct dns_badcache {
333 dns_badcache_t * next;
334 dns_rdatatype_t type;
336 unsigned int hashval;
339 #define DNS_BADCACHE_SIZE 1021
340 #define DNS_BADCACHE_TTL(fctx) \
341 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
343 struct dns_resolver {
349 isc_mutex_t primelock;
350 dns_rdataclass_t rdclass;
351 isc_socketmgr_t * socketmgr;
352 isc_timermgr_t * timermgr;
353 isc_taskmgr_t * taskmgr;
355 isc_boolean_t frozen;
356 unsigned int options;
357 dns_dispatchmgr_t * dispatchmgr;
358 dns_dispatch_t * dispatchv4;
359 isc_boolean_t exclusivev4;
360 dns_dispatch_t * dispatchv6;
361 isc_boolean_t exclusivev6;
363 unsigned int nbuckets;
364 fctxbucket_t * buckets;
365 isc_uint32_t lame_ttl;
366 ISC_LIST(alternate_t) alternates;
367 isc_uint16_t udpsize;
369 isc_rwlock_t alglock;
371 dns_rbt_t * algorithms;
373 isc_rwlock_t mbslock;
375 dns_rbt_t * mustbesecure;
376 unsigned int spillatmax;
377 unsigned int spillatmin;
378 isc_timer_t * spillattimer;
379 isc_boolean_t zero_no_soa_ttl;
381 /* Locked by lock. */
382 unsigned int references;
383 isc_boolean_t exiting;
384 isc_eventlist_t whenshutdown;
385 unsigned int activebuckets;
386 isc_boolean_t priming;
387 unsigned int spillat; /* clients-per-query */
388 unsigned int nextdisp;
391 dns_badcache_t ** badcache;
392 unsigned int badcount;
393 unsigned int badhash;
394 unsigned int badsweep;
396 /* Locked by primelock. */
397 dns_fetch_t * primefetch;
398 /* Locked by nlock. */
402 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
403 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
406 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
407 * which we also use as an addrinfo flag.
409 #define FCTX_ADDRINFO_MARK 0x0001
410 #define FCTX_ADDRINFO_FORWARDER 0x1000
411 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
413 #define ISFORWARDER(a) (((a)->flags & \
414 FCTX_ADDRINFO_FORWARDER) != 0)
416 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
418 static void destroy(dns_resolver_t *res);
419 static void empty_bucket(dns_resolver_t *res);
420 static isc_result_t resquery_send(resquery_t *query);
421 static void resquery_response(isc_task_t *task, isc_event_t *event);
422 static void resquery_connected(isc_task_t *task, isc_event_t *event);
423 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
424 isc_boolean_t badcache);
425 static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
426 static isc_result_t ncache_adderesult(dns_message_t *message,
427 dns_db_t *cache, dns_dbnode_t *node,
428 dns_rdatatype_t covers,
429 isc_stdtime_t now, dns_ttl_t maxttl,
430 dns_rdataset_t *ardataset,
431 isc_result_t *eresultp);
432 static void validated(isc_task_t *task, isc_event_t *event);
433 static void maybe_destroy(fetchctx_t *fctx);
434 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
435 isc_result_t reason, badnstype_t badtype);
438 * Increment resolver-related statistics counters.
441 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
442 if (res->view->resstats != NULL)
443 isc_stats_increment(res->view->resstats, counter);
447 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
448 dns_rdatatype_t type, dns_rdataset_t *rdataset,
449 dns_rdataset_t *sigrdataset, unsigned int valoptions,
452 dns_validator_t *validator = NULL;
453 dns_valarg_t *valarg;
456 valarg = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
459 return (ISC_R_NOMEMORY);
462 valarg->addrinfo = addrinfo;
464 if (!ISC_LIST_EMPTY(fctx->validators))
465 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
467 result = dns_validator_create(fctx->res->view, name, type, rdataset,
468 sigrdataset, fctx->rmessage,
469 valoptions, task, validated, valarg,
471 if (result == ISC_R_SUCCESS) {
472 inc_stats(fctx->res, dns_resstatscounter_val);
473 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
474 INSIST(fctx->validator == NULL);
475 fctx->validator = validator;
477 ISC_LIST_APPEND(fctx->validators, validator, link);
479 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
480 valarg, sizeof(*valarg));
485 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
486 dns_namereln_t namereln;
487 dns_rdata_rrsig_t rrsig;
488 dns_rdata_t rdata = DNS_RDATA_INIT;
493 for (result = dns_rdataset_first(rdataset);
494 result == ISC_R_SUCCESS;
495 result = dns_rdataset_next(rdataset)) {
496 dns_rdataset_current(rdataset, &rdata);
497 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
498 RUNTIME_CHECK(result == ISC_R_SUCCESS);
499 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
501 if (namereln == dns_namereln_subdomain)
503 dns_rdata_reset(&rdata);
509 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
511 dns_name_t *domain = &fctx->domain;
512 dns_rdataset_t *rdataset;
513 dns_rdatatype_t type;
515 isc_boolean_t keep_auth = ISC_FALSE;
517 if (message->rcode == dns_rcode_nxdomain)
521 * A DS RRset can appear anywhere in a zone, even for a delegation-only
522 * zone. So a response to an explicit query for this type should be
523 * excluded from delegation-only fixup.
525 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
526 * response to a query for these types can never violate the
527 * delegation-only assumption: if the query name is below a
528 * zone cut, the response should normally be a referral, which should
529 * be accepted; if the query name is below a zone cut but the server
530 * happens to have authority for the zone of the query name, the
531 * response is a (non-referral) answer. But this does not violate
532 * delegation-only because the query name must be in a different zone
533 * due to the "apex-only" nature of these types. Note that if the
534 * remote server happens to have authority for a child zone of a
535 * delegation-only zone, we may still incorrectly "fix" the response
536 * with NXDOMAIN for queries for other types. Unfortunately it's
537 * generally impossible to differentiate this case from violation of
538 * the delegation-only assumption. Once the resolver learns the
539 * correct zone cut, possibly via a separate query for an "apex-only"
540 * type, queries for other types will be resolved correctly.
542 * A query for type ANY will be accepted if it hits an exceptional
543 * type above in the answer section as it should be from a child
546 * Also accept answers with RRSIG records from the child zone.
547 * Direct queries for RRSIG records should not be answered from
551 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
552 (fctx->type == dns_rdatatype_ns ||
553 fctx->type == dns_rdatatype_ds ||
554 fctx->type == dns_rdatatype_soa ||
555 fctx->type == dns_rdatatype_any ||
556 fctx->type == dns_rdatatype_rrsig ||
557 fctx->type == dns_rdatatype_dnskey)) {
558 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
559 while (result == ISC_R_SUCCESS) {
561 dns_message_currentname(message, DNS_SECTION_ANSWER,
563 for (rdataset = ISC_LIST_HEAD(name->list);
565 rdataset = ISC_LIST_NEXT(rdataset, link)) {
566 if (!dns_name_equal(name, &fctx->name))
568 type = rdataset->type;
572 if (type == dns_rdatatype_rrsig &&
573 rrsig_fromchildzone(fctx, rdataset))
576 * Direct query for apex records or DS.
578 if (fctx->type == type &&
579 (type == dns_rdatatype_ds ||
580 type == dns_rdatatype_ns ||
581 type == dns_rdatatype_soa ||
582 type == dns_rdatatype_dnskey))
585 * Indirect query for apex records or DS.
587 if (fctx->type == dns_rdatatype_any &&
588 (type == dns_rdatatype_ns ||
589 type == dns_rdatatype_ds ||
590 type == dns_rdatatype_soa ||
591 type == dns_rdatatype_dnskey))
594 result = dns_message_nextname(message,
600 * A NODATA response to a DS query?
602 if (fctx->type == dns_rdatatype_ds &&
603 message->counts[DNS_SECTION_ANSWER] == 0)
606 /* Look for referral or indication of answer from child zone? */
607 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
610 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
611 while (result == ISC_R_SUCCESS) {
613 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
614 for (rdataset = ISC_LIST_HEAD(name->list);
616 rdataset = ISC_LIST_NEXT(rdataset, link)) {
617 type = rdataset->type;
618 if (type == dns_rdatatype_soa &&
619 dns_name_equal(name, domain))
620 keep_auth = ISC_TRUE;
622 if (type != dns_rdatatype_ns &&
623 type != dns_rdatatype_soa &&
624 type != dns_rdatatype_rrsig)
627 if (type == dns_rdatatype_rrsig) {
628 if (rrsig_fromchildzone(fctx, rdataset))
634 /* NS or SOA records. */
635 if (dns_name_equal(name, domain)) {
637 * If a query for ANY causes a negative
638 * response, we can be sure that this is
639 * an empty node. For other type of queries
640 * we cannot differentiate an empty node
641 * from a node that just doesn't have that
642 * type of record. We only accept the former
645 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
646 fctx->type == dns_rdatatype_any)
648 } else if (dns_name_issubdomain(name, domain)) {
649 /* Referral or answer from child zone. */
653 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
657 message->rcode = dns_rcode_nxdomain;
658 message->counts[DNS_SECTION_ANSWER] = 0;
660 message->counts[DNS_SECTION_AUTHORITY] = 0;
661 message->counts[DNS_SECTION_ADDITIONAL] = 0;
665 static inline isc_result_t
666 fctx_starttimer(fetchctx_t *fctx) {
668 * Start the lifetime timer for fctx.
670 * This is also used for stopping the idle timer; in that
671 * case we must purge events already posted to ensure that
672 * no further idle events are delivered.
674 return (isc_timer_reset(fctx->timer, isc_timertype_once,
675 &fctx->expires, NULL, ISC_TRUE));
679 fctx_stoptimer(fetchctx_t *fctx) {
683 * We don't return a result if resetting the timer to inactive fails
684 * since there's nothing to be done about it. Resetting to inactive
685 * should never fail anyway, since the code as currently written
686 * cannot fail in that case.
688 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
689 NULL, NULL, ISC_TRUE);
690 if (result != ISC_R_SUCCESS) {
691 UNEXPECTED_ERROR(__FILE__, __LINE__,
692 "isc_timer_reset(): %s",
693 isc_result_totext(result));
698 static inline isc_result_t
699 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
701 * Start the idle timer for fctx. The lifetime timer continues
704 return (isc_timer_reset(fctx->timer, isc_timertype_once,
705 &fctx->expires, interval, ISC_FALSE));
709 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
710 * we use fctx_stopidletimer for readability in the code below.
712 #define fctx_stopidletimer fctx_starttimer
716 resquery_destroy(resquery_t **queryp) {
719 REQUIRE(queryp != NULL);
721 REQUIRE(!ISC_LINK_LINKED(query, link));
723 INSIST(query->tcpsocket == NULL);
725 query->fctx->nqueries--;
726 if (SHUTTINGDOWN(query->fctx))
727 maybe_destroy(query->fctx); /* Locks bucket. */
729 isc_mem_put(query->mctx, query, sizeof(*query));
734 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
735 isc_time_t *finish, isc_boolean_t no_response)
739 unsigned int rtt, rttms;
742 dns_adbaddrinfo_t *addrinfo;
743 isc_socket_t *socket;
748 FCTXTRACE("cancelquery");
750 REQUIRE(!RESQUERY_CANCELED(query));
752 query->attributes |= RESQUERY_ATTR_CANCELED;
755 * Should we update the RTT?
757 if (finish != NULL || no_response) {
758 if (finish != NULL) {
760 * We have both the start and finish times for this
761 * packet, so we can compute a real RTT.
763 rtt = (unsigned int)isc_time_microdiff(finish,
765 factor = DNS_ADB_RTTADJDEFAULT;
768 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
770 dns_resstatscounter_queryrtt0);
771 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
773 dns_resstatscounter_queryrtt1);
774 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
776 dns_resstatscounter_queryrtt2);
777 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
779 dns_resstatscounter_queryrtt3);
780 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
782 dns_resstatscounter_queryrtt4);
785 dns_resstatscounter_queryrtt5);
789 * We don't have an RTT for this query. Maybe the
790 * packet was lost, or maybe this server is very
791 * slow. We don't know. Increase the RTT.
794 rtt = query->addrinfo->srtt + 200000;
798 * Replace the current RTT with our value.
800 factor = DNS_ADB_RTTADJREPLACE;
802 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
806 * Age RTTs of servers not tried.
808 factor = DNS_ADB_RTTADJAGE;
810 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
812 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
813 if (UNMARKED(addrinfo))
814 dns_adb_adjustsrtt(fctx->adb, addrinfo,
817 if (finish != NULL && TRIEDFIND(fctx))
818 for (find = ISC_LIST_HEAD(fctx->finds);
820 find = ISC_LIST_NEXT(find, publink))
821 for (addrinfo = ISC_LIST_HEAD(find->list);
823 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
824 if (UNMARKED(addrinfo))
825 dns_adb_adjustsrtt(fctx->adb, addrinfo,
828 if (finish != NULL && TRIEDALT(fctx)) {
829 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
831 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
832 if (UNMARKED(addrinfo))
833 dns_adb_adjustsrtt(fctx->adb, addrinfo,
835 for (find = ISC_LIST_HEAD(fctx->altfinds);
837 find = ISC_LIST_NEXT(find, publink))
838 for (addrinfo = ISC_LIST_HEAD(find->list);
840 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
841 if (UNMARKED(addrinfo))
842 dns_adb_adjustsrtt(fctx->adb, addrinfo,
847 * Check for any outstanding socket events. If they exist, cancel
848 * them and let the event handlers finish the cleanup. The resolver
849 * only needs to worry about managing the connect and send events;
850 * the dispatcher manages the recv events.
852 if (RESQUERY_CONNECTING(query)) {
854 * Cancel the connect.
856 if (query->tcpsocket != NULL) {
857 isc_socket_cancel(query->tcpsocket, NULL,
858 ISC_SOCKCANCEL_CONNECT);
859 } else if (query->dispentry != NULL) {
860 INSIST(query->exclusivesocket);
861 socket = dns_dispatch_getentrysocket(query->dispentry);
863 isc_socket_cancel(socket, NULL,
864 ISC_SOCKCANCEL_CONNECT);
866 } else if (RESQUERY_SENDING(query)) {
868 * Cancel the pending send.
870 if (query->exclusivesocket && query->dispentry != NULL)
871 socket = dns_dispatch_getentrysocket(query->dispentry);
873 socket = dns_dispatch_getsocket(query->dispatch);
875 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
878 if (query->dispentry != NULL)
879 dns_dispatch_removeresponse(&query->dispentry, deventp);
881 ISC_LIST_UNLINK(fctx->queries, query, link);
883 if (query->tsig != NULL)
884 isc_buffer_free(&query->tsig);
886 if (query->tsigkey != NULL)
887 dns_tsigkey_detach(&query->tsigkey);
889 if (query->dispatch != NULL)
890 dns_dispatch_detach(&query->dispatch);
892 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
894 * It's safe to destroy the query now.
896 resquery_destroy(&query);
900 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
901 resquery_t *query, *next_query;
903 FCTXTRACE("cancelqueries");
905 for (query = ISC_LIST_HEAD(fctx->queries);
907 query = next_query) {
908 next_query = ISC_LIST_NEXT(query, link);
909 fctx_cancelquery(&query, NULL, NULL, no_response);
914 fctx_cleanupfinds(fetchctx_t *fctx) {
915 dns_adbfind_t *find, *next_find;
917 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
919 for (find = ISC_LIST_HEAD(fctx->finds);
922 next_find = ISC_LIST_NEXT(find, publink);
923 ISC_LIST_UNLINK(fctx->finds, find, publink);
924 dns_adb_destroyfind(&find);
930 fctx_cleanupaltfinds(fetchctx_t *fctx) {
931 dns_adbfind_t *find, *next_find;
933 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
935 for (find = ISC_LIST_HEAD(fctx->altfinds);
938 next_find = ISC_LIST_NEXT(find, publink);
939 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
940 dns_adb_destroyfind(&find);
942 fctx->altfind = NULL;
946 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
947 dns_adbaddrinfo_t *addr, *next_addr;
949 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
951 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
954 next_addr = ISC_LIST_NEXT(addr, publink);
955 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
956 dns_adb_freeaddrinfo(fctx->adb, &addr);
961 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
962 dns_adbaddrinfo_t *addr, *next_addr;
964 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
966 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
969 next_addr = ISC_LIST_NEXT(addr, publink);
970 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
971 dns_adb_freeaddrinfo(fctx->adb, &addr);
976 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
977 FCTXTRACE("stopeverything");
978 fctx_cancelqueries(fctx, no_response);
979 fctx_cleanupfinds(fctx);
980 fctx_cleanupaltfinds(fctx);
981 fctx_cleanupforwaddrs(fctx);
982 fctx_cleanupaltaddrs(fctx);
983 fctx_stoptimer(fctx);
987 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
988 dns_fetchevent_t *event, *next_event;
990 unsigned int count = 0;
992 isc_boolean_t logit = ISC_FALSE;
994 unsigned int old_spillat;
995 unsigned int new_spillat = 0; /* initialized to silence
999 * Caller must be holding the appropriate bucket lock.
1001 REQUIRE(fctx->state == fetchstate_done);
1003 FCTXTRACE("sendevents");
1006 * Keep some record of fetch result for logging later (if required).
1008 fctx->result = result;
1009 fctx->exitline = line;
1011 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1013 for (event = ISC_LIST_HEAD(fctx->events);
1015 event = next_event) {
1016 next_event = ISC_LIST_NEXT(event, ev_link);
1017 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1018 task = event->ev_sender;
1019 event->ev_sender = fctx;
1020 if (!HAVE_ANSWER(fctx))
1021 event->result = result;
1023 INSIST(result != ISC_R_SUCCESS ||
1024 dns_rdataset_isassociated(event->rdataset) ||
1025 fctx->type == dns_rdatatype_any ||
1026 fctx->type == dns_rdatatype_rrsig ||
1027 fctx->type == dns_rdatatype_sig);
1030 * Negative results must be indicated in event->result.
1032 if (dns_rdataset_isassociated(event->rdataset) &&
1033 event->rdataset->type == dns_rdatatype_none) {
1034 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1035 event->result == DNS_R_NCACHENXRRSET);
1038 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1042 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1044 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1045 LOCK(&fctx->res->lock);
1046 if (count == fctx->res->spillat && !fctx->res->exiting) {
1047 old_spillat = fctx->res->spillat;
1048 fctx->res->spillat += 5;
1049 if (fctx->res->spillat > fctx->res->spillatmax &&
1050 fctx->res->spillatmax != 0)
1051 fctx->res->spillat = fctx->res->spillatmax;
1052 new_spillat = fctx->res->spillat;
1053 if (new_spillat != old_spillat) {
1056 isc_interval_set(&i, 20 * 60, 0);
1057 result = isc_timer_reset(fctx->res->spillattimer,
1058 isc_timertype_ticker, NULL,
1060 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1062 UNLOCK(&fctx->res->lock);
1064 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1065 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1066 "clients-per-query increased to %u",
1072 log_edns(fetchctx_t *fctx) {
1073 char domainbuf[DNS_NAME_FORMATSIZE];
1075 if (fctx->reason == NULL)
1078 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1079 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1080 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1081 "too many timeouts resolving '%s' (in '%s'?): %s",
1082 fctx->info, domainbuf, fctx->reason);
1084 fctx->reason = NULL;
1088 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1089 dns_resolver_t *res;
1090 isc_boolean_t no_response;
1098 if (result == ISC_R_SUCCESS) {
1100 * Log any deferred EDNS timeout messages.
1103 no_response = ISC_TRUE;
1105 no_response = ISC_FALSE;
1107 fctx->reason = NULL;
1108 fctx_stopeverything(fctx, no_response);
1110 LOCK(&res->buckets[fctx->bucketnum].lock);
1112 fctx->state = fetchstate_done;
1113 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1114 fctx_sendevents(fctx, result, line);
1116 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1120 process_sendevent(resquery_t *query, isc_event_t *event) {
1121 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1122 isc_boolean_t retry = ISC_FALSE;
1123 isc_result_t result;
1128 if (RESQUERY_CANCELED(query)) {
1129 if (query->sends == 0 && query->connects == 0) {
1131 * This query was canceled while the
1132 * isc_socket_sendto/connect() was in progress.
1134 if (query->tcpsocket != NULL)
1135 isc_socket_detach(&query->tcpsocket);
1136 resquery_destroy(&query);
1139 switch (sevent->result) {
1143 case ISC_R_HOSTUNREACH:
1144 case ISC_R_NETUNREACH:
1146 case ISC_R_ADDRNOTAVAIL:
1147 case ISC_R_CONNREFUSED:
1150 * No route to remote.
1152 add_bad(fctx, query->addrinfo, sevent->result,
1154 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1159 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1164 isc_event_free(&event);
1168 * Behave as if the idle timer has expired. For TCP
1169 * this may not actually reflect the latest timer.
1171 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1172 result = fctx_stopidletimer(fctx);
1173 if (result != ISC_R_SUCCESS)
1174 fctx_done(fctx, result, __LINE__);
1176 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1181 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1182 resquery_t *query = event->ev_arg;
1184 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1186 QTRACE("udpconnected");
1190 INSIST(RESQUERY_CONNECTING(query));
1194 process_sendevent(query, event);
1198 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1199 resquery_t *query = event->ev_arg;
1201 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1208 * Currently we don't wait for the senddone event before retrying
1209 * a query. This means that if we get really behind, we may end
1210 * up doing extra work!
1215 INSIST(RESQUERY_SENDING(query));
1219 process_sendevent(query, event);
1222 static inline isc_result_t
1223 fctx_addopt(dns_message_t *message, unsigned int version,
1224 isc_uint16_t udpsize, isc_boolean_t request_nsid)
1226 dns_rdataset_t *rdataset;
1227 dns_rdatalist_t *rdatalist;
1229 isc_result_t result;
1232 result = dns_message_gettemprdatalist(message, &rdatalist);
1233 if (result != ISC_R_SUCCESS)
1236 result = dns_message_gettemprdata(message, &rdata);
1237 if (result != ISC_R_SUCCESS)
1240 result = dns_message_gettemprdataset(message, &rdataset);
1241 if (result != ISC_R_SUCCESS)
1243 dns_rdataset_init(rdataset);
1245 rdatalist->type = dns_rdatatype_opt;
1246 rdatalist->covers = 0;
1249 * Set Maximum UDP buffer size.
1251 rdatalist->rdclass = udpsize;
1254 * Set EXTENDED-RCODE and Z to 0, DO to 1.
1256 rdatalist->ttl = (version << 16);
1257 rdatalist->ttl |= DNS_MESSAGEEXTFLAG_DO;
1260 * Set EDNS options if applicable
1263 /* Send empty NSID option (RFC5001) */
1264 unsigned char data[4];
1267 isc_buffer_init(&buf, data, sizeof(data));
1268 isc_buffer_putuint16(&buf, DNS_OPT_NSID);
1269 isc_buffer_putuint16(&buf, 0);
1271 rdata->length = sizeof(data);
1277 rdata->rdclass = rdatalist->rdclass;
1278 rdata->type = rdatalist->type;
1281 ISC_LIST_INIT(rdatalist->rdata);
1282 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1283 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset) == ISC_R_SUCCESS);
1285 return (dns_message_setopt(message, rdataset));
1289 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1290 unsigned int seconds;
1294 * We retry every .8 seconds the first two times through the address
1295 * list, and then we do exponential back-off.
1297 if (fctx->restarts < 3)
1300 us = (800000 << (fctx->restarts - 2));
1303 * Double the round-trip time.
1308 * Always wait for at least the doubled round-trip time.
1314 * But don't ever wait for more than 10 seconds.
1319 seconds = us / 1000000;
1320 us -= seconds * 1000000;
1321 isc_interval_set(&fctx->interval, seconds, us * 1000);
1325 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1326 unsigned int options)
1328 dns_resolver_t *res;
1330 isc_result_t result;
1332 isc_sockaddr_t addr;
1333 isc_boolean_t have_addr = ISC_FALSE;
1338 task = res->buckets[fctx->bucketnum].task;
1340 fctx_setretryinterval(fctx, addrinfo->srtt);
1341 result = fctx_startidletimer(fctx, &fctx->interval);
1342 if (result != ISC_R_SUCCESS)
1345 INSIST(ISC_LIST_EMPTY(fctx->validators));
1347 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1349 query = isc_mem_get(res->buckets[fctx->bucketnum].mctx,
1351 if (query == NULL) {
1352 result = ISC_R_NOMEMORY;
1353 goto stop_idle_timer;
1355 query->mctx = res->buckets[fctx->bucketnum].mctx;
1356 query->options = options;
1357 query->attributes = 0;
1359 query->connects = 0;
1361 * Note that the caller MUST guarantee that 'addrinfo' will remain
1362 * valid until this query is canceled.
1364 query->addrinfo = addrinfo;
1365 TIME_NOW(&query->start);
1368 * If this is a TCP query, then we need to make a socket and
1369 * a dispatch for it here. Otherwise we use the resolver's
1372 query->dispatchmgr = res->dispatchmgr;
1373 query->dispatch = NULL;
1374 query->exclusivesocket = ISC_FALSE;
1375 query->tcpsocket = NULL;
1376 if (res->view->peers != NULL) {
1377 dns_peer_t *peer = NULL;
1378 isc_netaddr_t dstip;
1379 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1380 result = dns_peerlist_peerbyaddr(res->view->peers,
1382 if (result == ISC_R_SUCCESS) {
1383 result = dns_peer_getquerysource(peer, &addr);
1384 if (result == ISC_R_SUCCESS)
1385 have_addr = ISC_TRUE;
1389 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1392 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1397 dns_dispatch_getlocaladdress(res->dispatchv4,
1402 dns_dispatch_getlocaladdress(res->dispatchv6,
1406 result = ISC_R_NOTIMPLEMENTED;
1409 if (result != ISC_R_SUCCESS)
1412 isc_sockaddr_setport(&addr, 0);
1414 result = isc_socket_create(res->socketmgr, pf,
1417 if (result != ISC_R_SUCCESS)
1420 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1421 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1422 if (result != ISC_R_SUCCESS)
1423 goto cleanup_socket;
1427 * A dispatch will be created once the connect succeeds.
1431 unsigned int attrs, attrmask;
1432 attrs = DNS_DISPATCHATTR_UDP;
1433 switch (isc_sockaddr_pf(&addr)) {
1435 attrs |= DNS_DISPATCHATTR_IPV4;
1438 attrs |= DNS_DISPATCHATTR_IPV6;
1441 result = ISC_R_NOTIMPLEMENTED;
1444 attrmask = DNS_DISPATCHATTR_UDP;
1445 attrmask |= DNS_DISPATCHATTR_TCP;
1446 attrmask |= DNS_DISPATCHATTR_IPV4;
1447 attrmask |= DNS_DISPATCHATTR_IPV6;
1448 result = dns_dispatch_getudp(res->dispatchmgr,
1450 res->taskmgr, &addr,
1451 4096, 1000, 32768, 16411,
1452 16433, attrs, attrmask,
1454 if (result != ISC_R_SUCCESS)
1457 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1459 dns_dispatch_attach(res->dispatchv4,
1461 query->exclusivesocket = res->exclusivev4;
1464 dns_dispatch_attach(res->dispatchv6,
1466 query->exclusivesocket = res->exclusivev6;
1469 result = ISC_R_NOTIMPLEMENTED;
1474 * We should always have a valid dispatcher here. If we
1475 * don't support a protocol family, then its dispatcher
1476 * will be NULL, but we shouldn't be finding addresses for
1477 * protocol types we don't support, so the dispatcher
1478 * we found should never be NULL.
1480 INSIST(query->dispatch != NULL);
1483 query->dispentry = NULL;
1486 query->tsigkey = NULL;
1487 ISC_LINK_INIT(query, link);
1488 query->magic = QUERY_MAGIC;
1490 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1492 * Connect to the remote server.
1494 * XXXRTH Should we attach to the socket?
1496 result = isc_socket_connect(query->tcpsocket,
1497 &addrinfo->sockaddr, task,
1498 resquery_connected, query);
1499 if (result != ISC_R_SUCCESS)
1500 goto cleanup_socket;
1502 QTRACE("connecting via TCP");
1504 result = resquery_send(query);
1505 if (result != ISC_R_SUCCESS)
1506 goto cleanup_dispatch;
1510 ISC_LIST_APPEND(fctx->queries, query, link);
1511 query->fctx->nqueries++;
1512 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1513 inc_stats(res, dns_resstatscounter_queryv4);
1515 inc_stats(res, dns_resstatscounter_queryv6);
1516 if (res->view->resquerystats != NULL)
1517 dns_rdatatypestats_increment(res->view->resquerystats,
1520 return (ISC_R_SUCCESS);
1523 isc_socket_detach(&query->tcpsocket);
1526 if (query->dispatch != NULL)
1527 dns_dispatch_detach(&query->dispatch);
1531 isc_mem_put(res->buckets[fctx->bucketnum].mctx,
1532 query, sizeof(*query));
1535 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1540 static isc_boolean_t
1541 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1544 for (sa = ISC_LIST_HEAD(fctx->edns);
1546 sa = ISC_LIST_NEXT(sa, link)) {
1547 if (isc_sockaddr_equal(sa, address))
1555 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1558 if (triededns(fctx, address))
1561 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1567 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1570 static isc_boolean_t
1571 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1574 for (sa = ISC_LIST_HEAD(fctx->edns512);
1576 sa = ISC_LIST_NEXT(sa, link)) {
1577 if (isc_sockaddr_equal(sa, address))
1585 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1588 if (triededns512(fctx, address))
1591 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1597 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1601 resquery_send(resquery_t *query) {
1603 isc_result_t result;
1604 dns_name_t *qname = NULL;
1605 dns_rdataset_t *qrdataset = NULL;
1607 dns_resolver_t *res;
1609 isc_socket_t *socket;
1610 isc_buffer_t tcpbuffer;
1611 isc_sockaddr_t *address;
1612 isc_buffer_t *buffer;
1613 isc_netaddr_t ipaddr;
1614 dns_tsigkey_t *tsigkey = NULL;
1615 dns_peer_t *peer = NULL;
1616 isc_boolean_t useedns;
1617 dns_compress_t cctx;
1618 isc_boolean_t cleanup_cctx = ISC_FALSE;
1619 isc_boolean_t secure_domain;
1625 task = res->buckets[fctx->bucketnum].task;
1628 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1630 * Reserve space for the TCP message length.
1632 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1633 isc_buffer_init(&query->buffer, query->data + 2,
1634 sizeof(query->data) - 2);
1635 buffer = &tcpbuffer;
1637 isc_buffer_init(&query->buffer, query->data,
1638 sizeof(query->data));
1639 buffer = &query->buffer;
1642 result = dns_message_gettempname(fctx->qmessage, &qname);
1643 if (result != ISC_R_SUCCESS)
1645 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1646 if (result != ISC_R_SUCCESS)
1650 * Get a query id from the dispatch.
1652 result = dns_dispatch_addresponse2(query->dispatch,
1653 &query->addrinfo->sockaddr,
1660 if (result != ISC_R_SUCCESS)
1663 fctx->qmessage->opcode = dns_opcode_query;
1668 dns_name_init(qname, NULL);
1669 dns_name_clone(&fctx->name, qname);
1670 dns_rdataset_init(qrdataset);
1671 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1672 ISC_LIST_APPEND(qname->list, qrdataset, link);
1673 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1678 * Set RD if the client has requested that we do a recursive query,
1679 * or if we're sending to a forwarder.
1681 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1682 ISFORWARDER(query->addrinfo))
1683 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1686 * Set CD if the client says don't validate or the question is
1687 * under a secure entry point.
1689 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1690 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1691 } else if (res->view->enablevalidation) {
1692 result = dns_keytable_issecuredomain(res->view->secroots,
1695 if (result != ISC_R_SUCCESS)
1696 secure_domain = ISC_FALSE;
1697 if (res->view->dlv != NULL)
1698 secure_domain = ISC_TRUE;
1700 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1704 * We don't have to set opcode because it defaults to query.
1706 fctx->qmessage->id = query->id;
1709 * Convert the question to wire format.
1711 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1712 if (result != ISC_R_SUCCESS)
1713 goto cleanup_message;
1714 cleanup_cctx = ISC_TRUE;
1716 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1718 if (result != ISC_R_SUCCESS)
1719 goto cleanup_message;
1721 result = dns_message_rendersection(fctx->qmessage,
1722 DNS_SECTION_QUESTION, 0);
1723 if (result != ISC_R_SUCCESS)
1724 goto cleanup_message;
1727 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1728 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1731 * The ADB does not know about servers with "edns no". Check this,
1732 * and then inform the ADB for future use.
1734 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1736 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1739 query->options |= DNS_FETCHOPT_NOEDNS0;
1740 dns_adb_changeflags(fctx->adb, query->addrinfo,
1741 DNS_FETCHOPT_NOEDNS0,
1742 DNS_FETCHOPT_NOEDNS0);
1745 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1746 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1747 query->options |= DNS_FETCHOPT_NOEDNS0;
1750 * Handle timeouts by reducing the UDP response size to 512 bytes
1751 * then if that doesn't work disabling EDNS (includes DO) and CD.
1753 * These timeout can be due to:
1754 * * broken nameservers that don't respond to EDNS queries.
1755 * * broken/misconfigured firewalls and NAT implementations
1756 * that don't handle IP fragmentation.
1757 * * broken/misconfigured firewalls that don't handle responses
1758 * greater than 512 bytes.
1759 * * broken/misconfigured firewalls that don't handle EDNS, DO
1761 * * packet loss / link outage.
1763 if (fctx->timeout) {
1764 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1765 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1766 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1767 query->options |= DNS_FETCHOPT_NOEDNS0;
1768 fctx->reason = "disabling EDNS";
1769 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1770 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1771 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1772 query->options |= DNS_FETCHOPT_EDNS512;
1773 fctx->reason = "reducing the advertised EDNS UDP "
1774 "packet size to 512 octets";
1776 fctx->timeout = ISC_FALSE;
1780 * Use EDNS0, unless the caller doesn't want it, or we know that
1781 * the remote server doesn't like it.
1783 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1784 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1785 unsigned int version = 0; /* Default version. */
1787 isc_uint16_t udpsize = res->udpsize;
1788 isc_boolean_t reqnsid = res->view->requestnsid;
1790 flags = query->addrinfo->flags;
1791 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1792 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1793 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1795 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1797 else if (peer != NULL)
1798 (void)dns_peer_getudpsize(peer, &udpsize);
1800 /* request NSID for current view or peer? */
1802 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1803 result = fctx_addopt(fctx->qmessage, version,
1805 if (reqnsid && result == ISC_R_SUCCESS) {
1806 query->options |= DNS_FETCHOPT_WANTNSID;
1807 } else if (result != ISC_R_SUCCESS) {
1809 * We couldn't add the OPT, but we'll press on.
1810 * We're not using EDNS0, so set the NOEDNS0
1813 query->options |= DNS_FETCHOPT_NOEDNS0;
1817 * We know this server doesn't like EDNS0, so we
1818 * won't use it. Set the NOEDNS0 bit since we're
1821 query->options |= DNS_FETCHOPT_NOEDNS0;
1826 * If we need EDNS0 to do this query and aren't using it, we lose.
1828 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1829 result = DNS_R_SERVFAIL;
1830 goto cleanup_message;
1833 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1834 add_triededns(fctx, &query->addrinfo->sockaddr);
1836 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1837 add_triededns512(fctx, &query->addrinfo->sockaddr);
1840 * Clear CD if EDNS is not in use.
1842 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1843 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1846 * Add TSIG record tailored to the current recipient.
1848 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1849 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1850 goto cleanup_message;
1852 if (tsigkey != NULL) {
1853 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1854 dns_tsigkey_detach(&tsigkey);
1855 if (result != ISC_R_SUCCESS)
1856 goto cleanup_message;
1859 result = dns_message_rendersection(fctx->qmessage,
1860 DNS_SECTION_ADDITIONAL, 0);
1861 if (result != ISC_R_SUCCESS)
1862 goto cleanup_message;
1864 result = dns_message_renderend(fctx->qmessage);
1865 if (result != ISC_R_SUCCESS)
1866 goto cleanup_message;
1868 dns_compress_invalidate(&cctx);
1869 cleanup_cctx = ISC_FALSE;
1871 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1872 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1874 result = dns_message_getquerytsig(fctx->qmessage,
1877 if (result != ISC_R_SUCCESS)
1878 goto cleanup_message;
1882 * If using TCP, write the length of the message at the beginning
1885 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1886 isc_buffer_usedregion(&query->buffer, &r);
1887 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1888 isc_buffer_add(&tcpbuffer, r.length);
1892 * We're now done with the query message.
1894 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1896 if (query->exclusivesocket)
1897 socket = dns_dispatch_getentrysocket(query->dispentry);
1899 socket = dns_dispatch_getsocket(query->dispatch);
1903 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1904 address = &query->addrinfo->sockaddr;
1905 if (query->exclusivesocket) {
1906 result = isc_socket_connect(socket, address, task,
1907 resquery_udpconnected,
1909 if (result != ISC_R_SUCCESS)
1910 goto cleanup_message;
1914 isc_buffer_usedregion(buffer, &r);
1917 * XXXRTH Make sure we don't send to ourselves! We should probably
1918 * prune out these addresses when we get them from the ADB.
1920 result = isc_socket_sendto(socket, &r, task, resquery_senddone,
1921 query, address, NULL);
1922 if (result != ISC_R_SUCCESS)
1923 goto cleanup_message;
1929 return (ISC_R_SUCCESS);
1933 dns_compress_invalidate(&cctx);
1935 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1938 * Stop the dispatcher from listening.
1940 dns_dispatch_removeresponse(&query->dispentry, NULL);
1944 dns_message_puttempname(fctx->qmessage, &qname);
1945 if (qrdataset != NULL)
1946 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
1952 resquery_connected(isc_task_t *task, isc_event_t *event) {
1953 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1954 resquery_t *query = event->ev_arg;
1955 isc_boolean_t retry = ISC_FALSE;
1956 isc_interval_t interval;
1957 isc_result_t result;
1961 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1962 REQUIRE(VALID_QUERY(query));
1964 QTRACE("connected");
1971 * Currently we don't wait for the connect event before retrying
1972 * a query. This means that if we get really behind, we may end
1973 * up doing extra work!
1979 if (RESQUERY_CANCELED(query)) {
1981 * This query was canceled while the connect() was in
1984 isc_socket_detach(&query->tcpsocket);
1985 resquery_destroy(&query);
1987 switch (sevent->result) {
1991 * Extend the idle timer for TCP. 20 seconds
1992 * should be long enough for a TCP connection to be
1993 * established, a single DNS request to be sent,
1994 * and the response received.
1996 isc_interval_set(&interval, 20, 0);
1997 result = fctx_startidletimer(query->fctx, &interval);
1998 if (result != ISC_R_SUCCESS) {
1999 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2000 fctx_done(fctx, result, __LINE__);
2004 * We are connected. Create a dispatcher and
2008 attrs |= DNS_DISPATCHATTR_TCP;
2009 attrs |= DNS_DISPATCHATTR_PRIVATE;
2010 attrs |= DNS_DISPATCHATTR_CONNECTED;
2011 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2013 attrs |= DNS_DISPATCHATTR_IPV4;
2015 attrs |= DNS_DISPATCHATTR_IPV6;
2016 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2018 result = dns_dispatch_createtcp(query->dispatchmgr,
2020 query->fctx->res->taskmgr,
2021 4096, 2, 1, 1, 3, attrs,
2025 * Regardless of whether dns_dispatch_create()
2026 * succeeded or not, we don't need our reference
2027 * to the socket anymore.
2029 isc_socket_detach(&query->tcpsocket);
2031 if (result == ISC_R_SUCCESS)
2032 result = resquery_send(query);
2034 if (result != ISC_R_SUCCESS) {
2035 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2036 fctx_done(fctx, result, __LINE__);
2040 case ISC_R_NETUNREACH:
2041 case ISC_R_HOSTUNREACH:
2042 case ISC_R_CONNREFUSED:
2044 case ISC_R_ADDRNOTAVAIL:
2045 case ISC_R_CONNECTIONRESET:
2047 * No route to remote.
2049 isc_socket_detach(&query->tcpsocket);
2050 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2055 isc_socket_detach(&query->tcpsocket);
2056 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2061 isc_event_free(&event);
2065 * Behave as if the idle timer has expired. For TCP
2066 * connections this may not actually reflect the latest timer.
2068 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2069 result = fctx_stopidletimer(fctx);
2070 if (result != ISC_R_SUCCESS)
2071 fctx_done(fctx, result, __LINE__);
2073 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2078 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2080 dns_adbfind_t *find;
2081 dns_resolver_t *res;
2082 isc_boolean_t want_try = ISC_FALSE;
2083 isc_boolean_t want_done = ISC_FALSE;
2084 isc_boolean_t bucket_empty = ISC_FALSE;
2085 unsigned int bucketnum;
2087 find = event->ev_sender;
2088 fctx = event->ev_arg;
2089 REQUIRE(VALID_FCTX(fctx));
2094 FCTXTRACE("finddone");
2096 INSIST(fctx->pending > 0);
2099 if (ADDRWAIT(fctx)) {
2101 * The fetch is waiting for a name to be found.
2103 INSIST(!SHUTTINGDOWN(fctx));
2104 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2105 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2106 want_try = ISC_TRUE;
2109 if (fctx->pending == 0) {
2111 * We've got nothing else to wait for and don't
2112 * know the answer. There's nothing to do but
2115 want_done = ISC_TRUE;
2118 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2119 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2120 bucketnum = fctx->bucketnum;
2121 LOCK(&res->buckets[bucketnum].lock);
2123 * Note that we had to wait until we had the lock before
2124 * looking at fctx->references.
2126 if (fctx->references == 0)
2127 bucket_empty = fctx_destroy(fctx);
2128 UNLOCK(&res->buckets[bucketnum].lock);
2131 isc_event_free(&event);
2132 dns_adb_destroyfind(&find);
2135 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2137 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2138 else if (bucket_empty)
2143 static inline isc_boolean_t
2144 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2147 for (sa = ISC_LIST_HEAD(fctx->bad);
2149 sa = ISC_LIST_NEXT(sa, link)) {
2150 if (isc_sockaddr_equal(sa, address))
2157 static inline isc_boolean_t
2158 mark_bad(fetchctx_t *fctx) {
2159 dns_adbfind_t *curr;
2160 dns_adbaddrinfo_t *addrinfo;
2161 isc_boolean_t all_bad = ISC_TRUE;
2164 * Mark all known bad servers, so we don't try to talk to them
2169 * Mark any bad nameservers.
2171 for (curr = ISC_LIST_HEAD(fctx->finds);
2173 curr = ISC_LIST_NEXT(curr, publink)) {
2174 for (addrinfo = ISC_LIST_HEAD(curr->list);
2176 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2177 if (bad_server(fctx, &addrinfo->sockaddr))
2178 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2180 all_bad = ISC_FALSE;
2185 * Mark any bad forwarders.
2187 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2189 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2190 if (bad_server(fctx, &addrinfo->sockaddr))
2191 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2193 all_bad = ISC_FALSE;
2197 * Mark any bad alternates.
2199 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2201 curr = ISC_LIST_NEXT(curr, publink)) {
2202 for (addrinfo = ISC_LIST_HEAD(curr->list);
2204 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2205 if (bad_server(fctx, &addrinfo->sockaddr))
2206 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2208 all_bad = ISC_FALSE;
2212 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2214 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2215 if (bad_server(fctx, &addrinfo->sockaddr))
2216 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2218 all_bad = ISC_FALSE;
2225 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2226 badnstype_t badtype)
2228 char namebuf[DNS_NAME_FORMATSIZE];
2229 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2235 const char *sep1, *sep2;
2236 isc_sockaddr_t *address = &addrinfo->sockaddr;
2238 if (reason == DNS_R_LAME)
2242 case badns_unreachable:
2245 case badns_response:
2248 case badns_validation:
2249 break; /* counted as 'valfail' */
2253 if (bad_server(fctx, address)) {
2255 * We already know this server is bad.
2260 FCTXTRACE("add_bad");
2262 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
2267 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2269 if (reason == DNS_R_LAME) /* already logged */
2272 if (reason == DNS_R_UNEXPECTEDRCODE &&
2273 fctx->rmessage->rcode == dns_rcode_servfail &&
2274 ISFORWARDER(addrinfo))
2277 if (reason == DNS_R_UNEXPECTEDRCODE) {
2278 isc_buffer_init(&b, code, sizeof(code) - 1);
2279 dns_rcode_totext(fctx->rmessage->rcode, &b);
2280 code[isc_buffer_usedlength(&b)] = '\0';
2283 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2284 isc_buffer_init(&b, code, sizeof(code) - 1);
2285 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2286 code[isc_buffer_usedlength(&b)] = '\0';
2294 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2295 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2296 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2297 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2298 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2299 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2300 "%s %s%s%sresolving '%s/%s/%s': %s",
2301 dns_result_totext(reason), sep1, code, sep2,
2302 namebuf, typebuf, classbuf, addrbuf);
2306 sort_adbfind(dns_adbfind_t *find) {
2307 dns_adbaddrinfo_t *best, *curr;
2308 dns_adbaddrinfolist_t sorted;
2311 * Lame N^2 bubble sort.
2314 ISC_LIST_INIT(sorted);
2315 while (!ISC_LIST_EMPTY(find->list)) {
2316 best = ISC_LIST_HEAD(find->list);
2317 curr = ISC_LIST_NEXT(best, publink);
2318 while (curr != NULL) {
2319 if (curr->srtt < best->srtt)
2321 curr = ISC_LIST_NEXT(curr, publink);
2323 ISC_LIST_UNLINK(find->list, best, publink);
2324 ISC_LIST_APPEND(sorted, best, publink);
2326 find->list = sorted;
2330 sort_finds(fetchctx_t *fctx) {
2331 dns_adbfind_t *best, *curr;
2332 dns_adbfindlist_t sorted;
2333 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2336 * Lame N^2 bubble sort.
2339 ISC_LIST_INIT(sorted);
2340 while (!ISC_LIST_EMPTY(fctx->finds)) {
2341 best = ISC_LIST_HEAD(fctx->finds);
2342 bestaddrinfo = ISC_LIST_HEAD(best->list);
2343 INSIST(bestaddrinfo != NULL);
2344 curr = ISC_LIST_NEXT(best, publink);
2345 while (curr != NULL) {
2346 addrinfo = ISC_LIST_HEAD(curr->list);
2347 INSIST(addrinfo != NULL);
2348 if (addrinfo->srtt < bestaddrinfo->srtt) {
2350 bestaddrinfo = addrinfo;
2352 curr = ISC_LIST_NEXT(curr, publink);
2354 ISC_LIST_UNLINK(fctx->finds, best, publink);
2355 ISC_LIST_APPEND(sorted, best, publink);
2357 fctx->finds = sorted;
2359 ISC_LIST_INIT(sorted);
2360 while (!ISC_LIST_EMPTY(fctx->altfinds)) {
2361 best = ISC_LIST_HEAD(fctx->altfinds);
2362 bestaddrinfo = ISC_LIST_HEAD(best->list);
2363 INSIST(bestaddrinfo != NULL);
2364 curr = ISC_LIST_NEXT(best, publink);
2365 while (curr != NULL) {
2366 addrinfo = ISC_LIST_HEAD(curr->list);
2367 INSIST(addrinfo != NULL);
2368 if (addrinfo->srtt < bestaddrinfo->srtt) {
2370 bestaddrinfo = addrinfo;
2372 curr = ISC_LIST_NEXT(curr, publink);
2374 ISC_LIST_UNLINK(fctx->altfinds, best, publink);
2375 ISC_LIST_APPEND(sorted, best, publink);
2377 fctx->altfinds = sorted;
2381 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2382 unsigned int options, unsigned int flags, isc_stdtime_t now,
2383 isc_boolean_t *need_alternate)
2385 dns_adbaddrinfo_t *ai;
2386 dns_adbfind_t *find;
2387 dns_resolver_t *res;
2388 isc_boolean_t unshared;
2389 isc_result_t result;
2392 unshared = ISC_TF((fctx->options | DNS_FETCHOPT_UNSHARED) != 0);
2394 * If this name is a subdomain of the query domain, tell
2395 * the ADB to start looking using zone/hint data. This keeps us
2396 * from getting stuck if the nameserver is beneath the zone cut
2397 * and we don't know its address (e.g. because the A record has
2400 if (dns_name_issubdomain(name, &fctx->domain))
2401 options |= DNS_ADBFIND_STARTATZONE;
2402 options |= DNS_ADBFIND_GLUEOK;
2403 options |= DNS_ADBFIND_HINTOK;
2406 * See what we know about this address.
2409 result = dns_adb_createfind(fctx->adb,
2410 res->buckets[fctx->bucketnum].task,
2411 fctx_finddone, fctx, name,
2412 &fctx->name, fctx->type,
2414 res->view->dstport, &find);
2415 if (result != ISC_R_SUCCESS) {
2416 if (result == DNS_R_ALIAS) {
2418 * XXXRTH Follow the CNAME/DNAME chain?
2420 dns_adb_destroyfind(&find);
2423 } else if (!ISC_LIST_EMPTY(find->list)) {
2425 * We have at least some of the addresses for the
2428 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2430 if (flags != 0 || port != 0) {
2431 for (ai = ISC_LIST_HEAD(find->list);
2433 ai = ISC_LIST_NEXT(ai, publink)) {
2436 isc_sockaddr_setport(&ai->sockaddr,
2440 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2441 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2443 ISC_LIST_APPEND(fctx->finds, find, publink);
2446 * We don't know any of the addresses for this
2449 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2451 * We're looking for them and will get an
2452 * event about it later.
2458 if (need_alternate != NULL &&
2459 !*need_alternate && unshared &&
2460 ((res->dispatchv4 == NULL &&
2461 find->result_v6 != DNS_R_NXDOMAIN) ||
2462 (res->dispatchv6 == NULL &&
2463 find->result_v4 != DNS_R_NXDOMAIN)))
2464 *need_alternate = ISC_TRUE;
2466 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2467 fctx->lamecount++; /* cached lame server */
2469 fctx->adberr++; /* unreachable server, etc. */
2472 * If we know there are no addresses for
2473 * the family we are using then try to add
2474 * an alternative server.
2476 if (need_alternate != NULL && !*need_alternate &&
2477 ((res->dispatchv4 == NULL &&
2478 find->result_v6 == DNS_R_NXRRSET) ||
2479 (res->dispatchv6 == NULL &&
2480 find->result_v4 == DNS_R_NXRRSET)))
2481 *need_alternate = ISC_TRUE;
2482 dns_adb_destroyfind(&find);
2487 static isc_boolean_t
2488 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2490 unsigned int nlabels;
2491 dns_namereln_t namereln;
2493 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2494 return (ISC_TF(namereln == dns_namereln_subdomain));
2498 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2499 dns_rdata_t rdata = DNS_RDATA_INIT;
2500 isc_result_t result;
2501 dns_resolver_t *res;
2503 unsigned int stdoptions;
2505 dns_adbaddrinfo_t *ai;
2506 isc_boolean_t all_bad;
2508 isc_boolean_t need_alternate = ISC_FALSE;
2510 FCTXTRACE("getaddresses");
2513 * Don't pound on remote servers. (Failsafe!)
2516 if (fctx->restarts > 10) {
2517 FCTXTRACE("too many restarts");
2518 return (DNS_R_SERVFAIL);
2522 stdoptions = 0; /* Keep compiler happy. */
2528 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2529 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2532 * If this fctx has forwarders, use them; otherwise use any
2533 * selective forwarders specified in the view; otherwise use the
2534 * resolver's forwarders (if any).
2536 sa = ISC_LIST_HEAD(fctx->forwarders);
2538 dns_forwarders_t *forwarders = NULL;
2539 dns_name_t *name = &fctx->name;
2541 unsigned int labels;
2542 dns_fixedname_t fixed;
2546 * DS records are found in the parent server.
2547 * Strip label to get the correct forwarder (if any).
2549 if (dns_rdatatype_atparent(fctx->type) &&
2550 dns_name_countlabels(name) > 1) {
2551 dns_name_init(&suffix, NULL);
2552 labels = dns_name_countlabels(name);
2553 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2557 dns_fixedname_init(&fixed);
2558 domain = dns_fixedname_name(&fixed);
2559 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2560 domain, &forwarders);
2561 if (result == ISC_R_SUCCESS) {
2562 sa = ISC_LIST_HEAD(forwarders->addrs);
2563 fctx->fwdpolicy = forwarders->fwdpolicy;
2564 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2565 isstrictsubdomain(domain, &fctx->domain)) {
2568 mctx = res->buckets[fctx->bucketnum].mctx;
2569 dns_name_free(&fctx->domain, mctx);
2570 dns_name_init(&fctx->domain, NULL);
2571 result = dns_name_dup(domain, mctx,
2573 if (result != ISC_R_SUCCESS)
2579 while (sa != NULL) {
2580 if ((isc_sockaddr_pf(sa) == AF_INET &&
2581 fctx->res->dispatchv4 == NULL) ||
2582 (isc_sockaddr_pf(sa) == AF_INET6 &&
2583 fctx->res->dispatchv6 == NULL)) {
2584 sa = ISC_LIST_NEXT(sa, link);
2588 result = dns_adb_findaddrinfo(fctx->adb,
2589 sa, &ai, 0); /* XXXMLG */
2590 if (result == ISC_R_SUCCESS) {
2591 dns_adbaddrinfo_t *cur;
2592 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2593 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2594 while (cur != NULL && cur->srtt < ai->srtt)
2595 cur = ISC_LIST_NEXT(cur, publink);
2597 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2600 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2602 sa = ISC_LIST_NEXT(sa, link);
2606 * If the forwarding policy is "only", we don't need the addresses
2607 * of the nameservers.
2609 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2613 * Normal nameservers.
2616 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2617 if (fctx->restarts == 1) {
2619 * To avoid sending out a flood of queries likely to
2620 * result in NXRRSET, we suppress fetches for address
2621 * families we don't have the first time through,
2622 * provided that we have addresses in some family we
2625 * We don't want to set this option all the time, since
2626 * if fctx->restarts > 1, we've clearly been having trouble
2627 * with the addresses we had, so getting more could help.
2629 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2631 if (res->dispatchv4 != NULL)
2632 stdoptions |= DNS_ADBFIND_INET;
2633 if (res->dispatchv6 != NULL)
2634 stdoptions |= DNS_ADBFIND_INET6;
2635 isc_stdtime_get(&now);
2637 INSIST(ISC_LIST_EMPTY(fctx->finds));
2638 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2640 for (result = dns_rdataset_first(&fctx->nameservers);
2641 result == ISC_R_SUCCESS;
2642 result = dns_rdataset_next(&fctx->nameservers))
2644 dns_rdataset_current(&fctx->nameservers, &rdata);
2646 * Extract the name from the NS record.
2648 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2649 if (result != ISC_R_SUCCESS)
2652 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2654 dns_rdata_reset(&rdata);
2655 dns_rdata_freestruct(&ns);
2657 if (result != ISC_R_NOMORE)
2661 * Do we need to use 6 to 4?
2663 if (need_alternate) {
2666 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2667 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2669 a = ISC_LIST_NEXT(a, link)) {
2670 if (!a->isaddress) {
2671 findname(fctx, &a->_u._n.name, a->_u._n.port,
2672 stdoptions, FCTX_ADDRINFO_FORWARDER,
2676 if (isc_sockaddr_pf(&a->_u.addr) != family)
2679 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2681 if (result == ISC_R_SUCCESS) {
2682 dns_adbaddrinfo_t *cur;
2683 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2684 cur = ISC_LIST_HEAD(fctx->altaddrs);
2685 while (cur != NULL && cur->srtt < ai->srtt)
2686 cur = ISC_LIST_NEXT(cur, publink);
2688 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2691 ISC_LIST_APPEND(fctx->altaddrs, ai,
2699 * Mark all known bad servers.
2701 all_bad = mark_bad(fctx);
2708 * We've got no addresses.
2710 if (fctx->pending > 0) {
2712 * We're fetching the addresses, but don't have any
2713 * yet. Tell the caller to wait for an answer.
2715 result = DNS_R_WAIT;
2720 * We've lost completely. We don't know any
2721 * addresses, and the ADB has told us it can't get
2724 FCTXTRACE("no addresses");
2725 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2726 result = isc_time_nowplusinterval(&expire, &i);
2728 (fctx->type == dns_rdatatype_dnskey ||
2729 fctx->type == dns_rdatatype_dlv ||
2730 fctx->type == dns_rdatatype_ds) &&
2731 result == ISC_R_SUCCESS)
2732 dns_resolver_addbadcache(fctx->res,
2734 fctx->type, &expire);
2735 result = ISC_R_FAILURE;
2739 * We've found some addresses. We might still be looking
2740 * for more addresses.
2743 result = ISC_R_SUCCESS;
2750 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2753 char buf[ISC_NETADDR_FORMATSIZE];
2755 isc_boolean_t aborted = ISC_FALSE;
2756 isc_boolean_t bogus;
2757 dns_acl_t *blackhole;
2758 isc_netaddr_t ipaddr;
2759 dns_peer_t *peer = NULL;
2760 dns_resolver_t *res;
2761 const char *msg = NULL;
2763 sa = &addr->sockaddr;
2766 isc_netaddr_fromsockaddr(&ipaddr, sa);
2767 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2768 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2770 if (blackhole != NULL) {
2773 if (dns_acl_match(&ipaddr, NULL, blackhole,
2775 &match, NULL) == ISC_R_SUCCESS &&
2781 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2786 addr->flags |= FCTX_ADDRINFO_MARK;
2787 msg = "ignoring blackholed / bogus server: ";
2788 } else if (isc_sockaddr_ismulticast(sa)) {
2789 addr->flags |= FCTX_ADDRINFO_MARK;
2790 msg = "ignoring multicast address: ";
2791 } else if (isc_sockaddr_isexperimental(sa)) {
2792 addr->flags |= FCTX_ADDRINFO_MARK;
2793 msg = "ignoring experimental address: ";
2794 } else if (sa->type.sa.sa_family != AF_INET6) {
2796 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2797 addr->flags |= FCTX_ADDRINFO_MARK;
2798 msg = "ignoring IPv6 mapped IPV4 address: ";
2799 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2800 addr->flags |= FCTX_ADDRINFO_MARK;
2801 msg = "ignoring IPv6 compatibility IPV4 address: ";
2805 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2808 isc_netaddr_fromsockaddr(&na, sa);
2809 isc_netaddr_format(&na, buf, sizeof(buf));
2810 FCTXTRACE2(msg, buf);
2813 static inline dns_adbaddrinfo_t *
2814 fctx_nextaddress(fetchctx_t *fctx) {
2815 dns_adbfind_t *find, *start;
2816 dns_adbaddrinfo_t *addrinfo;
2817 dns_adbaddrinfo_t *faddrinfo;
2820 * Return the next untried address, if any.
2824 * Find the first unmarked forwarder (if any).
2826 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2828 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2829 if (!UNMARKED(addrinfo))
2831 possibly_mark(fctx, addrinfo);
2832 if (UNMARKED(addrinfo)) {
2833 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2840 * No forwarders. Move to the next find.
2843 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2847 find = ISC_LIST_HEAD(fctx->finds);
2849 find = ISC_LIST_NEXT(find, publink);
2851 find = ISC_LIST_HEAD(fctx->finds);
2855 * Find the first unmarked addrinfo.
2861 for (addrinfo = ISC_LIST_HEAD(find->list);
2863 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2864 if (!UNMARKED(addrinfo))
2866 possibly_mark(fctx, addrinfo);
2867 if (UNMARKED(addrinfo)) {
2868 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2872 if (addrinfo != NULL)
2874 find = ISC_LIST_NEXT(find, publink);
2876 find = ISC_LIST_HEAD(fctx->finds);
2877 } while (find != start);
2881 if (addrinfo != NULL)
2885 * No nameservers left. Try alternates.
2888 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2890 find = fctx->altfind;
2892 find = ISC_LIST_HEAD(fctx->altfinds);
2894 find = ISC_LIST_NEXT(find, publink);
2896 find = ISC_LIST_HEAD(fctx->altfinds);
2900 * Find the first unmarked addrinfo.
2906 for (addrinfo = ISC_LIST_HEAD(find->list);
2908 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2909 if (!UNMARKED(addrinfo))
2911 possibly_mark(fctx, addrinfo);
2912 if (UNMARKED(addrinfo)) {
2913 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2917 if (addrinfo != NULL)
2919 find = ISC_LIST_NEXT(find, publink);
2921 find = ISC_LIST_HEAD(fctx->altfinds);
2922 } while (find != start);
2925 faddrinfo = addrinfo;
2928 * See if we have a better alternate server by address.
2931 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2933 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2934 if (!UNMARKED(addrinfo))
2936 possibly_mark(fctx, addrinfo);
2937 if (UNMARKED(addrinfo) &&
2938 (faddrinfo == NULL ||
2939 addrinfo->srtt < faddrinfo->srtt)) {
2940 if (faddrinfo != NULL)
2941 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
2942 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2947 if (addrinfo == NULL) {
2948 addrinfo = faddrinfo;
2949 fctx->altfind = find;
2956 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
2957 isc_result_t result;
2958 dns_adbaddrinfo_t *addrinfo;
2962 REQUIRE(!ADDRWAIT(fctx));
2964 addrinfo = fctx_nextaddress(fctx);
2965 if (addrinfo == NULL) {
2967 * We have no more addresses. Start over.
2969 fctx_cancelqueries(fctx, ISC_TRUE);
2970 fctx_cleanupfinds(fctx);
2971 fctx_cleanupaltfinds(fctx);
2972 fctx_cleanupforwaddrs(fctx);
2973 fctx_cleanupaltaddrs(fctx);
2974 result = fctx_getaddresses(fctx, badcache);
2975 if (result == DNS_R_WAIT) {
2977 * Sleep waiting for addresses.
2979 FCTXTRACE("addrwait");
2980 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
2982 } else if (result != ISC_R_SUCCESS) {
2984 * Something bad happened.
2986 fctx_done(fctx, result, __LINE__);
2990 addrinfo = fctx_nextaddress(fctx);
2992 * While we may have addresses from the ADB, they
2993 * might be bad ones. In this case, return SERVFAIL.
2995 if (addrinfo == NULL) {
2996 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3001 result = fctx_query(fctx, addrinfo, fctx->options);
3002 if (result != ISC_R_SUCCESS)
3003 fctx_done(fctx, result, __LINE__);
3005 inc_stats(fctx->res, dns_resstatscounter_retry);
3008 static isc_boolean_t
3009 fctx_destroy(fetchctx_t *fctx) {
3010 dns_resolver_t *res;
3011 unsigned int bucketnum;
3012 isc_sockaddr_t *sa, *next_sa;
3015 * Caller must be holding the bucket lock.
3018 REQUIRE(VALID_FCTX(fctx));
3019 REQUIRE(fctx->state == fetchstate_done ||
3020 fctx->state == fetchstate_init);
3021 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3022 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3023 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3024 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3025 REQUIRE(fctx->pending == 0);
3026 REQUIRE(fctx->references == 0);
3027 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3029 FCTXTRACE("destroy");
3032 bucketnum = fctx->bucketnum;
3034 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3039 for (sa = ISC_LIST_HEAD(fctx->bad);
3042 next_sa = ISC_LIST_NEXT(sa, link);
3043 ISC_LIST_UNLINK(fctx->bad, sa, link);
3044 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3047 for (sa = ISC_LIST_HEAD(fctx->edns);
3050 next_sa = ISC_LIST_NEXT(sa, link);
3051 ISC_LIST_UNLINK(fctx->edns, sa, link);
3052 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3055 for (sa = ISC_LIST_HEAD(fctx->edns512);
3058 next_sa = ISC_LIST_NEXT(sa, link);
3059 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3060 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3063 isc_timer_detach(&fctx->timer);
3064 dns_message_destroy(&fctx->rmessage);
3065 dns_message_destroy(&fctx->qmessage);
3066 if (dns_name_countlabels(&fctx->domain) > 0)
3067 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3068 if (dns_rdataset_isassociated(&fctx->nameservers))
3069 dns_rdataset_disassociate(&fctx->nameservers);
3070 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3071 dns_db_detach(&fctx->cache);
3072 dns_adb_detach(&fctx->adb);
3073 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3074 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3078 UNLOCK(&res->nlock);
3080 if (res->buckets[bucketnum].exiting &&
3081 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3088 * Fetch event handlers.
3092 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3093 fetchctx_t *fctx = event->ev_arg;
3094 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3097 REQUIRE(VALID_FCTX(fctx));
3101 FCTXTRACE("timeout");
3103 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3105 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3106 fctx->reason = NULL;
3107 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3109 isc_result_t result;
3112 fctx->timeout = ISC_TRUE;
3114 * We could cancel the running queries here, or we could let
3115 * them keep going. Since we normally use separate sockets for
3116 * different queries, we adopt the former approach to reduce
3117 * the number of open sockets: cancel the oldest query if it
3118 * expired after the query had started (this is usually the
3119 * case but is not always so, depending on the task schedule
3122 query = ISC_LIST_HEAD(fctx->queries);
3123 if (query != NULL &&
3124 isc_time_compare(&tevent->due, &query->start) >= 0) {
3125 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3127 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3129 * Our timer has triggered. Reestablish the fctx lifetime
3132 result = fctx_starttimer(fctx);
3133 if (result != ISC_R_SUCCESS)
3134 fctx_done(fctx, result, __LINE__);
3139 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3142 isc_event_free(&event);
3146 fctx_shutdown(fetchctx_t *fctx) {
3147 isc_event_t *cevent;
3150 * Start the shutdown process for fctx, if it isn't already underway.
3153 FCTXTRACE("shutdown");
3156 * The caller must be holding the appropriate bucket lock.
3159 if (fctx->want_shutdown)
3162 fctx->want_shutdown = ISC_TRUE;
3165 * Unless we're still initializing (in which case the
3166 * control event is still outstanding), we need to post
3167 * the control event to tell the fetch we want it to
3170 if (fctx->state != fetchstate_init) {
3171 cevent = &fctx->control_event;
3172 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3178 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3179 fetchctx_t *fctx = event->ev_arg;
3180 isc_boolean_t bucket_empty = ISC_FALSE;
3181 dns_resolver_t *res;
3182 unsigned int bucketnum;
3183 dns_validator_t *validator;
3185 REQUIRE(VALID_FCTX(fctx));
3190 bucketnum = fctx->bucketnum;
3192 FCTXTRACE("doshutdown");
3195 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3197 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3200 * Cancel all pending validators. Note that this must be done
3201 * without the bucket lock held, since that could cause deadlock.
3203 validator = ISC_LIST_HEAD(fctx->validators);
3204 while (validator != NULL) {
3205 dns_validator_cancel(validator);
3206 validator = ISC_LIST_NEXT(validator, link);
3209 if (fctx->nsfetch != NULL)
3210 dns_resolver_cancelfetch(fctx->nsfetch);
3213 * Shut down anything that is still running on behalf of this
3214 * fetch. To avoid deadlock with the ADB, we must do this
3215 * before we lock the bucket lock.
3217 fctx_stopeverything(fctx, ISC_FALSE);
3219 LOCK(&res->buckets[bucketnum].lock);
3221 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3223 INSIST(fctx->state == fetchstate_active ||
3224 fctx->state == fetchstate_done);
3225 INSIST(fctx->want_shutdown);
3227 if (fctx->state != fetchstate_done) {
3228 fctx->state = fetchstate_done;
3229 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3232 if (fctx->references == 0 && fctx->pending == 0 &&
3233 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3234 bucket_empty = fctx_destroy(fctx);
3236 UNLOCK(&res->buckets[bucketnum].lock);
3243 fctx_start(isc_task_t *task, isc_event_t *event) {
3244 fetchctx_t *fctx = event->ev_arg;
3245 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3246 dns_resolver_t *res;
3247 unsigned int bucketnum;
3249 REQUIRE(VALID_FCTX(fctx));
3254 bucketnum = fctx->bucketnum;
3258 LOCK(&res->buckets[bucketnum].lock);
3260 INSIST(fctx->state == fetchstate_init);
3261 if (fctx->want_shutdown) {
3263 * We haven't started this fctx yet, and we've been requested
3266 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3267 fctx->state = fetchstate_done;
3268 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3270 * Since we haven't started, we INSIST that we have no
3271 * pending ADB finds and no pending validations.
3273 INSIST(fctx->pending == 0);
3274 INSIST(fctx->nqueries == 0);
3275 INSIST(ISC_LIST_EMPTY(fctx->validators));
3276 if (fctx->references == 0) {
3278 * It's now safe to destroy this fctx.
3280 bucket_empty = fctx_destroy(fctx);
3285 * Normal fctx startup.
3287 fctx->state = fetchstate_active;
3289 * Reset the control event for later use in shutting down
3292 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3293 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3297 UNLOCK(&res->buckets[bucketnum].lock);
3300 isc_result_t result;
3303 * All is well. Start working on the fetch.
3305 result = fctx_starttimer(fctx);
3306 if (result != ISC_R_SUCCESS)
3307 fctx_done(fctx, result, __LINE__);
3309 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3310 } else if (bucket_empty)
3315 * Fetch Creation, Joining, and Cancelation.
3318 static inline isc_result_t
3319 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3320 dns_messageid_t id, isc_taskaction_t action, void *arg,
3321 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3325 dns_fetchevent_t *event;
3330 * We store the task we're going to send this event to in the
3331 * sender field. We'll make the fetch the sender when we actually
3335 isc_task_attach(task, &clone);
3336 event = (dns_fetchevent_t *)
3337 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3338 action, arg, sizeof(*event));
3339 if (event == NULL) {
3340 isc_task_detach(&clone);
3341 return (ISC_R_NOMEMORY);
3343 event->result = DNS_R_SERVFAIL;
3344 event->qtype = fctx->type;
3347 event->rdataset = rdataset;
3348 event->sigrdataset = sigrdataset;
3349 event->fetch = fetch;
3350 event->client = client;
3352 dns_fixedname_init(&event->foundname);
3355 * Make sure that we can store the sigrdataset in the
3356 * first event if it is needed by any of the events.
3358 if (event->sigrdataset != NULL)
3359 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3361 ISC_LIST_APPEND(fctx->events, event, ev_link);
3364 fetch->magic = DNS_FETCH_MAGIC;
3365 fetch->private = fctx;
3367 return (ISC_R_SUCCESS);
3371 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3372 dns_name_t *domain, dns_rdataset_t *nameservers,
3373 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3376 isc_result_t result;
3377 isc_result_t iresult;
3378 isc_interval_t interval;
3379 dns_fixedname_t fixed;
3380 unsigned int findoptions = 0;
3381 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3382 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3386 * Caller must be holding the lock for bucket number 'bucketnum'.
3388 REQUIRE(fctxp != NULL && *fctxp == NULL);
3390 fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3392 return (ISC_R_NOMEMORY);
3393 dns_name_format(name, buf, sizeof(buf));
3394 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3395 strcat(buf, "/"); /* checked */
3396 strcat(buf, typebuf); /* checked */
3397 fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3398 if (fctx->info == NULL) {
3399 result = ISC_R_NOMEMORY;
3402 FCTXTRACE("create");
3403 dns_name_init(&fctx->name, NULL);
3404 result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3405 if (result != ISC_R_SUCCESS)
3407 dns_name_init(&fctx->domain, NULL);
3408 dns_rdataset_init(&fctx->nameservers);
3411 fctx->options = options;
3413 * Note! We do not attach to the task. We are relying on the
3414 * resolver to ensure that this task doesn't go away while we are
3418 fctx->references = 0;
3419 fctx->bucketnum = bucketnum;
3420 fctx->state = fetchstate_init;
3421 fctx->want_shutdown = ISC_FALSE;
3422 fctx->cloned = ISC_FALSE;
3423 ISC_LIST_INIT(fctx->queries);
3424 ISC_LIST_INIT(fctx->finds);
3425 ISC_LIST_INIT(fctx->altfinds);
3426 ISC_LIST_INIT(fctx->forwaddrs);
3427 ISC_LIST_INIT(fctx->altaddrs);
3428 ISC_LIST_INIT(fctx->forwarders);
3429 fctx->fwdpolicy = dns_fwdpolicy_none;
3430 ISC_LIST_INIT(fctx->bad);
3431 ISC_LIST_INIT(fctx->edns);
3432 ISC_LIST_INIT(fctx->edns512);
3433 ISC_LIST_INIT(fctx->validators);
3434 fctx->validator = NULL;
3436 fctx->altfind = NULL;
3439 fctx->querysent = 0;
3440 fctx->referrals = 0;
3441 TIME_NOW(&fctx->start);
3443 fctx->lamecount = 0;
3449 fctx->result = ISC_R_FAILURE;
3450 fctx->vresult = ISC_R_SUCCESS;
3451 fctx->exitline = -1; /* sentinel */
3452 fctx->logged = ISC_FALSE;
3453 fctx->attributes = 0;
3454 fctx->spilled = ISC_FALSE;
3456 fctx->reason = NULL;
3457 fctx->timeout = ISC_FALSE;
3459 dns_name_init(&fctx->nsname, NULL);
3460 fctx->nsfetch = NULL;
3461 dns_rdataset_init(&fctx->nsrrset);
3463 if (domain == NULL) {
3464 dns_forwarders_t *forwarders = NULL;
3465 unsigned int labels;
3466 dns_name_t *fwdname = name;
3469 * DS records are found in the parent server.
3470 * Strip label to get the correct forwarder (if any).
3472 if (dns_rdatatype_atparent(fctx->type) &&
3473 dns_name_countlabels(name) > 1) {
3474 dns_name_init(&suffix, NULL);
3475 labels = dns_name_countlabels(name);
3476 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3479 dns_fixedname_init(&fixed);
3480 domain = dns_fixedname_name(&fixed);
3481 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3482 domain, &forwarders);
3483 if (result == ISC_R_SUCCESS)
3484 fctx->fwdpolicy = forwarders->fwdpolicy;
3486 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3488 * The caller didn't supply a query domain and
3489 * nameservers, and we're not in forward-only mode,
3490 * so find the best nameservers to use.
3492 if (dns_rdatatype_atparent(fctx->type))
3493 findoptions |= DNS_DBFIND_NOEXACT;
3494 result = dns_view_findzonecut(res->view, name, domain,
3495 0, findoptions, ISC_TRUE,
3498 if (result != ISC_R_SUCCESS)
3500 result = dns_name_dup(domain,
3501 res->buckets[bucketnum].mctx,
3503 if (result != ISC_R_SUCCESS) {
3504 dns_rdataset_disassociate(&fctx->nameservers);
3509 * We're in forward-only mode. Set the query domain.
3511 result = dns_name_dup(domain,
3512 res->buckets[bucketnum].mctx,
3514 if (result != ISC_R_SUCCESS)
3518 result = dns_name_dup(domain,
3519 res->buckets[bucketnum].mctx,
3521 if (result != ISC_R_SUCCESS)
3523 dns_rdataset_clone(nameservers, &fctx->nameservers);
3526 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3528 fctx->qmessage = NULL;
3529 result = dns_message_create(res->buckets[bucketnum].mctx,
3530 DNS_MESSAGE_INTENTRENDER,
3533 if (result != ISC_R_SUCCESS)
3534 goto cleanup_domain;
3536 fctx->rmessage = NULL;
3537 result = dns_message_create(res->buckets[bucketnum].mctx,
3538 DNS_MESSAGE_INTENTPARSE,
3541 if (result != ISC_R_SUCCESS)
3542 goto cleanup_qmessage;
3545 * Compute an expiration time for the entire fetch.
3547 isc_interval_set(&interval, 30, 0); /* XXXRTH constant */
3548 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3549 if (iresult != ISC_R_SUCCESS) {
3550 UNEXPECTED_ERROR(__FILE__, __LINE__,
3551 "isc_time_nowplusinterval: %s",
3552 isc_result_totext(iresult));
3553 result = ISC_R_UNEXPECTED;
3554 goto cleanup_rmessage;
3558 * Default retry interval initialization. We set the interval now
3559 * mostly so it won't be uninitialized. It will be set to the
3560 * correct value before a query is issued.
3562 isc_interval_set(&fctx->interval, 2, 0);
3565 * Create an inactive timer. It will be made active when the fetch
3566 * is actually started.
3569 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3571 res->buckets[bucketnum].task, fctx_timeout,
3572 fctx, &fctx->timer);
3573 if (iresult != ISC_R_SUCCESS) {
3574 UNEXPECTED_ERROR(__FILE__, __LINE__,
3575 "isc_timer_create: %s",
3576 isc_result_totext(iresult));
3577 result = ISC_R_UNEXPECTED;
3578 goto cleanup_rmessage;
3582 * Attach to the view's cache and adb.
3585 dns_db_attach(res->view->cachedb, &fctx->cache);
3587 dns_adb_attach(res->view->adb, &fctx->adb);
3589 ISC_LIST_INIT(fctx->events);
3590 ISC_LINK_INIT(fctx, link);
3591 fctx->magic = FCTX_MAGIC;
3593 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3597 UNLOCK(&res->nlock);
3601 return (ISC_R_SUCCESS);
3604 dns_message_destroy(&fctx->rmessage);
3607 dns_message_destroy(&fctx->qmessage);
3610 if (dns_name_countlabels(&fctx->domain) > 0)
3611 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3612 if (dns_rdataset_isassociated(&fctx->nameservers))
3613 dns_rdataset_disassociate(&fctx->nameservers);
3616 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3619 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3622 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3630 static inline isc_boolean_t
3631 is_lame(fetchctx_t *fctx) {
3632 dns_message_t *message = fctx->rmessage;
3634 dns_rdataset_t *rdataset;
3635 isc_result_t result;
3637 if (message->rcode != dns_rcode_noerror &&
3638 message->rcode != dns_rcode_nxdomain)
3641 if (message->counts[DNS_SECTION_ANSWER] != 0)
3644 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3647 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3648 while (result == ISC_R_SUCCESS) {
3650 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3651 for (rdataset = ISC_LIST_HEAD(name->list);
3653 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3654 dns_namereln_t namereln;
3656 unsigned int labels;
3657 if (rdataset->type != dns_rdatatype_ns)
3659 namereln = dns_name_fullcompare(name, &fctx->domain,
3661 if (namereln == dns_namereln_equal &&
3662 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3664 if (namereln == dns_namereln_subdomain)
3668 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3675 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3676 char namebuf[DNS_NAME_FORMATSIZE];
3677 char domainbuf[DNS_NAME_FORMATSIZE];
3678 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3680 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3681 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3682 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3683 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3684 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3685 "lame server resolving '%s' (in '%s'?): %s",
3686 namebuf, domainbuf, addrbuf);
3689 static inline isc_result_t
3690 same_question(fetchctx_t *fctx) {
3691 isc_result_t result;
3692 dns_message_t *message = fctx->rmessage;
3694 dns_rdataset_t *rdataset;
3697 * Caller must be holding the fctx lock.
3701 * XXXRTH Currently we support only one question.
3703 if (message->counts[DNS_SECTION_QUESTION] != 1)
3704 return (DNS_R_FORMERR);
3706 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3707 if (result != ISC_R_SUCCESS)
3710 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3711 rdataset = ISC_LIST_HEAD(name->list);
3712 INSIST(rdataset != NULL);
3713 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3714 if (fctx->type != rdataset->type ||
3715 fctx->res->rdclass != rdataset->rdclass ||
3716 !dns_name_equal(&fctx->name, name))
3717 return (DNS_R_FORMERR);
3719 return (ISC_R_SUCCESS);
3723 clone_results(fetchctx_t *fctx) {
3724 dns_fetchevent_t *event, *hevent;
3725 isc_result_t result;
3726 dns_name_t *name, *hname;
3728 FCTXTRACE("clone_results");
3731 * Set up any other events to have the same data as the first
3734 * Caller must be holding the appropriate lock.
3737 fctx->cloned = ISC_TRUE;
3738 hevent = ISC_LIST_HEAD(fctx->events);
3741 hname = dns_fixedname_name(&hevent->foundname);
3742 for (event = ISC_LIST_NEXT(hevent, ev_link);
3744 event = ISC_LIST_NEXT(event, ev_link)) {
3745 name = dns_fixedname_name(&event->foundname);
3746 result = dns_name_copy(hname, name, NULL);
3747 if (result != ISC_R_SUCCESS)
3748 event->result = result;
3750 event->result = hevent->result;
3751 dns_db_attach(hevent->db, &event->db);
3752 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3753 INSIST(hevent->rdataset != NULL);
3754 INSIST(event->rdataset != NULL);
3755 if (dns_rdataset_isassociated(hevent->rdataset))
3756 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3757 INSIST(! (hevent->sigrdataset == NULL &&
3758 event->sigrdataset != NULL));
3759 if (hevent->sigrdataset != NULL &&
3760 dns_rdataset_isassociated(hevent->sigrdataset) &&
3761 event->sigrdataset != NULL)
3762 dns_rdataset_clone(hevent->sigrdataset,
3763 event->sigrdataset);
3767 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3768 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3769 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3770 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3771 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3772 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3773 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3777 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3778 * no references and is no longer waiting for any events). If this
3779 * was the last fctx in the resolver, destroy the resolver.
3782 * '*fctx' is shutting down.
3785 maybe_destroy(fetchctx_t *fctx) {
3786 unsigned int bucketnum;
3787 isc_boolean_t bucket_empty = ISC_FALSE;
3788 dns_resolver_t *res = fctx->res;
3789 dns_validator_t *validator, *next_validator;
3791 REQUIRE(SHUTTINGDOWN(fctx));
3793 if (fctx->pending != 0 || fctx->nqueries != 0)
3796 for (validator = ISC_LIST_HEAD(fctx->validators);
3797 validator != NULL; validator = next_validator) {
3798 next_validator = ISC_LIST_NEXT(validator, link);
3799 dns_validator_cancel(validator);
3801 * If this is a active validator wait for the cancel
3802 * to complete before calling dns_validator_destroy().
3804 if (validator == fctx->validator)
3806 ISC_LIST_UNLINK(fctx->validators, validator, link);
3807 dns_validator_destroy(&validator);
3810 bucketnum = fctx->bucketnum;
3811 LOCK(&res->buckets[bucketnum].lock);
3812 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3813 bucket_empty = fctx_destroy(fctx);
3814 UNLOCK(&res->buckets[bucketnum].lock);
3821 * The validator has finished.
3824 validated(isc_task_t *task, isc_event_t *event) {
3825 isc_result_t result = ISC_R_SUCCESS;
3826 isc_result_t eresult = ISC_R_SUCCESS;
3829 dns_validatorevent_t *vevent;
3830 dns_fetchevent_t *hevent;
3831 dns_rdataset_t *ardataset = NULL;
3832 dns_rdataset_t *asigrdataset = NULL;
3833 dns_dbnode_t *node = NULL;
3834 isc_boolean_t negative;
3835 isc_boolean_t chaining;
3836 isc_boolean_t sentresponse;
3838 dns_dbnode_t *nsnode = NULL;
3840 dns_rdataset_t *rdataset;
3841 dns_rdataset_t *sigrdataset;
3842 dns_valarg_t *valarg;
3843 dns_adbaddrinfo_t *addrinfo;
3845 UNUSED(task); /* for now */
3847 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3848 valarg = event->ev_arg;
3849 fctx = valarg->fctx;
3850 addrinfo = valarg->addrinfo;
3851 REQUIRE(VALID_FCTX(fctx));
3852 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3854 vevent = (dns_validatorevent_t *)event;
3856 FCTXTRACE("received validation completion event");
3858 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
3859 fctx->validator = NULL;
3862 * Destroy the validator early so that we can
3863 * destroy the fctx if necessary.
3865 dns_validator_destroy(&vevent->validator);
3866 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
3867 valarg, sizeof(*valarg));
3869 negative = ISC_TF(vevent->rdataset == NULL);
3871 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
3874 * If shutting down, ignore the results. Check to see if we're
3875 * done waiting for validator completions and ADB pending events; if
3876 * so, destroy the fctx.
3878 if (SHUTTINGDOWN(fctx) && !sentresponse) {
3879 maybe_destroy(fctx); /* Locks bucket. */
3883 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3885 isc_stdtime_get(&now);
3888 * If chaining, we need to make sure that the right result code is
3889 * returned, and that the rdatasets are bound.
3891 if (vevent->result == ISC_R_SUCCESS &&
3893 vevent->rdataset != NULL &&
3894 CHAINING(vevent->rdataset))
3896 if (vevent->rdataset->type == dns_rdatatype_cname)
3897 eresult = DNS_R_CNAME;
3899 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
3900 eresult = DNS_R_DNAME;
3902 chaining = ISC_TRUE;
3904 chaining = ISC_FALSE;
3907 * Either we're not shutting down, or we are shutting down but want
3908 * to cache the result anyway (if this was a validation started by
3909 * a query with cd set)
3912 hevent = ISC_LIST_HEAD(fctx->events);
3913 if (hevent != NULL) {
3914 if (!negative && !chaining &&
3915 (fctx->type == dns_rdatatype_any ||
3916 fctx->type == dns_rdatatype_rrsig ||
3917 fctx->type == dns_rdatatype_sig)) {
3919 * Don't bind rdatasets; the caller
3920 * will iterate the node.
3923 ardataset = hevent->rdataset;
3924 asigrdataset = hevent->sigrdataset;
3928 if (vevent->result != ISC_R_SUCCESS) {
3929 FCTXTRACE("validation failed");
3930 inc_stats(fctx->res, dns_resstatscounter_valfail);
3932 fctx->vresult = vevent->result;
3933 if (fctx->vresult != DNS_R_BROKENCHAIN) {
3934 result = ISC_R_NOTFOUND;
3935 if (vevent->rdataset != NULL)
3936 result = dns_db_findnode(fctx->cache,
3939 if (result == ISC_R_SUCCESS)
3940 (void)dns_db_deleterdataset(fctx->cache, node,
3943 if (result == ISC_R_SUCCESS &&
3944 vevent->sigrdataset != NULL)
3945 (void)dns_db_deleterdataset(fctx->cache, node,
3947 dns_rdatatype_rrsig,
3949 if (result == ISC_R_SUCCESS)
3950 dns_db_detachnode(fctx->cache, &node);
3952 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
3954 * Cache the data as pending for later validation.
3956 result = ISC_R_NOTFOUND;
3957 if (vevent->rdataset != NULL)
3958 result = dns_db_findnode(fctx->cache,
3961 if (result == ISC_R_SUCCESS) {
3962 (void)dns_db_addrdataset(fctx->cache, node,
3964 vevent->rdataset, 0,
3967 if (result == ISC_R_SUCCESS &&
3968 vevent->sigrdataset != NULL)
3969 (void)dns_db_addrdataset(fctx->cache, node,
3971 vevent->sigrdataset,
3973 if (result == ISC_R_SUCCESS)
3974 dns_db_detachnode(fctx->cache, &node);
3976 result = fctx->vresult;
3977 add_bad(fctx, addrinfo, result, badns_validation);
3978 isc_event_free(&event);
3979 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3980 INSIST(fctx->validator == NULL);
3981 fctx->validator = ISC_LIST_HEAD(fctx->validators);
3982 if (fctx->validator != NULL)
3983 dns_validator_send(fctx->validator);
3984 else if (sentresponse)
3985 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
3986 else if (result == DNS_R_BROKENCHAIN) {
3987 isc_result_t tresult;
3991 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
3992 tresult = isc_time_nowplusinterval(&expire, &i);
3994 (fctx->type == dns_rdatatype_dnskey ||
3995 fctx->type == dns_rdatatype_dlv ||
3996 fctx->type == dns_rdatatype_ds) &&
3997 tresult == ISC_R_SUCCESS)
3998 dns_resolver_addbadcache(fctx->res,
4000 fctx->type, &expire);
4001 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4003 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4009 dns_rdatatype_t covers;
4010 FCTXTRACE("nonexistence validation OK");
4012 inc_stats(fctx->res, dns_resstatscounter_valnegsuccess);
4014 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4015 covers = dns_rdatatype_any;
4017 covers = fctx->type;
4019 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4021 if (result != ISC_R_SUCCESS)
4022 goto noanswer_response;
4025 * If we are asking for a SOA record set the cache time
4026 * to zero to facilitate locating the containing zone of
4029 ttl = fctx->res->view->maxncachettl;
4030 if (fctx->type == dns_rdatatype_soa &&
4031 covers == dns_rdatatype_any &&
4032 fctx->res->zero_no_soa_ttl)
4035 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4037 ardataset, &eresult);
4038 if (result != ISC_R_SUCCESS)
4039 goto noanswer_response;
4040 goto answer_response;
4042 inc_stats(fctx->res, dns_resstatscounter_valsuccess);
4044 FCTXTRACE("validation OK");
4046 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4048 result = dns_rdataset_addnoqname(vevent->rdataset,
4049 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4050 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4051 INSIST(vevent->sigrdataset != NULL);
4052 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4056 * The data was already cached as pending data.
4057 * Re-cache it as secure and bind the cached
4058 * rdatasets to the first event on the fetch
4061 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4062 if (result != ISC_R_SUCCESS)
4063 goto noanswer_response;
4065 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4066 vevent->rdataset, 0, ardataset);
4067 if (result != ISC_R_SUCCESS &&
4068 result != DNS_R_UNCHANGED)
4069 goto noanswer_response;
4070 if (ardataset != NULL && ardataset->type == 0) {
4071 if (NXDOMAIN(ardataset))
4072 eresult = DNS_R_NCACHENXDOMAIN;
4074 eresult = DNS_R_NCACHENXRRSET;
4075 } else if (vevent->sigrdataset != NULL) {
4076 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4077 vevent->sigrdataset, 0,
4079 if (result != ISC_R_SUCCESS &&
4080 result != DNS_R_UNCHANGED)
4081 goto noanswer_response;
4086 * If we only deferred the destroy because we wanted to cache
4087 * the data, destroy now.
4089 dns_db_detachnode(fctx->cache, &node);
4090 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4091 if (SHUTTINGDOWN(fctx))
4092 maybe_destroy(fctx); /* Locks bucket. */
4096 if (!ISC_LIST_EMPTY(fctx->validators)) {
4098 INSIST(fctx->type == dns_rdatatype_any ||
4099 fctx->type == dns_rdatatype_rrsig ||
4100 fctx->type == dns_rdatatype_sig);
4102 * Don't send a response yet - we have
4103 * more rdatasets that still need to
4106 dns_db_detachnode(fctx->cache, &node);
4107 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4108 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4114 * Cache any NS/NSEC records that happened to be validated.
4116 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4117 while (result == ISC_R_SUCCESS) {
4119 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4121 for (rdataset = ISC_LIST_HEAD(name->list);
4123 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4124 if ((rdataset->type != dns_rdatatype_ns &&
4125 rdataset->type != dns_rdatatype_nsec) ||
4126 rdataset->trust != dns_trust_secure)
4128 for (sigrdataset = ISC_LIST_HEAD(name->list);
4129 sigrdataset != NULL;
4130 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4131 if (sigrdataset->type != dns_rdatatype_rrsig ||
4132 sigrdataset->covers != rdataset->type)
4136 if (sigrdataset == NULL ||
4137 sigrdataset->trust != dns_trust_secure)
4139 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4141 if (result != ISC_R_SUCCESS)
4144 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4145 now, rdataset, 0, NULL);
4146 if (result == ISC_R_SUCCESS)
4147 result = dns_db_addrdataset(fctx->cache, nsnode,
4151 dns_db_detachnode(fctx->cache, &nsnode);
4153 result = dns_message_nextname(fctx->rmessage,
4154 DNS_SECTION_AUTHORITY);
4157 result = ISC_R_SUCCESS;
4160 * Respond with an answer, positive or negative,
4161 * as opposed to an error. 'node' must be non-NULL.
4164 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4166 if (hevent != NULL) {
4167 hevent->result = eresult;
4168 RUNTIME_CHECK(dns_name_copy(vevent->name,
4169 dns_fixedname_name(&hevent->foundname), NULL)
4171 dns_db_attach(fctx->cache, &hevent->db);
4172 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4173 clone_results(fctx);
4178 dns_db_detachnode(fctx->cache, &node);
4180 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4182 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4185 INSIST(node == NULL);
4186 isc_event_free(&event);
4189 static inline isc_result_t
4190 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4193 dns_rdataset_t *rdataset, *sigrdataset;
4194 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4195 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4196 dns_dbnode_t *node, **anodep;
4199 dns_resolver_t *res;
4200 isc_boolean_t need_validation, secure_domain, have_answer;
4201 isc_result_t result, eresult;
4202 dns_fetchevent_t *event;
4203 unsigned int options;
4206 unsigned int valoptions = 0;
4209 * The appropriate bucket lock must be held.
4213 need_validation = ISC_FALSE;
4214 secure_domain = ISC_FALSE;
4215 have_answer = ISC_FALSE;
4216 eresult = ISC_R_SUCCESS;
4217 task = res->buckets[fctx->bucketnum].task;
4220 * Is DNSSEC validation required for this name?
4222 if (res->view->enablevalidation) {
4223 result = dns_keytable_issecuredomain(res->view->secroots, name,
4225 if (result != ISC_R_SUCCESS)
4228 if (!secure_domain && res->view->dlv != NULL) {
4229 valoptions = DNS_VALIDATOR_DLV;
4230 secure_domain = ISC_TRUE;
4234 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4235 need_validation = ISC_FALSE;
4237 need_validation = secure_domain;
4243 asigrdataset = NULL;
4245 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4247 have_answer = ISC_TRUE;
4248 event = ISC_LIST_HEAD(fctx->events);
4249 if (event != NULL) {
4251 aname = dns_fixedname_name(&event->foundname);
4252 result = dns_name_copy(name, aname, NULL);
4253 if (result != ISC_R_SUCCESS)
4255 anodep = &event->node;
4257 * If this is an ANY, SIG or RRSIG query, we're not
4258 * going to return any rdatasets, unless we encountered
4259 * a CNAME or DNAME as "the answer". In this case,
4260 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4261 * and we must set up the rdatasets.
4263 if ((fctx->type != dns_rdatatype_any &&
4264 fctx->type != dns_rdatatype_rrsig &&
4265 fctx->type != dns_rdatatype_sig) ||
4266 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4267 ardataset = event->rdataset;
4268 asigrdataset = event->sigrdataset;
4274 * Find or create the cache node.
4277 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4278 if (result != ISC_R_SUCCESS)
4282 * Cache or validate each cacheable rdataset.
4284 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4285 for (rdataset = ISC_LIST_HEAD(name->list);
4287 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4288 if (!CACHE(rdataset))
4290 if (CHECKNAMES(rdataset)) {
4291 char namebuf[DNS_NAME_FORMATSIZE];
4292 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4293 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4295 dns_name_format(name, namebuf, sizeof(namebuf));
4296 dns_rdatatype_format(rdataset->type, typebuf,
4298 dns_rdataclass_format(rdataset->rdclass, classbuf,
4300 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4301 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4302 "check-names %s %s/%s/%s",
4303 fail ? "failure" : "warning",
4304 namebuf, typebuf, classbuf);
4306 if (ANSWER(rdataset)) {
4307 dns_db_detachnode(fctx->cache, &node);
4308 return (DNS_R_BADNAME);
4315 * Enforce the configure maximum cache TTL.
4317 if (rdataset->ttl > res->view->maxcachettl)
4318 rdataset->ttl = res->view->maxcachettl;
4321 * If this RRset is in a secure domain, is in bailiwick,
4322 * and is not glue, attempt DNSSEC validation. (We do not
4323 * attempt to validate glue or out-of-bailiwick data--even
4324 * though there might be some performance benefit to doing
4325 * so--because it makes it simpler and safer to ensure that
4326 * records from a secure domain are only cached if validated
4327 * within the context of a query to the domain that owns
4330 if (secure_domain && rdataset->trust != dns_trust_glue &&
4331 !EXTERNAL(rdataset)) {
4335 * RRSIGs are validated as part of validating the
4338 if (rdataset->type == dns_rdatatype_rrsig)
4341 * Find the SIG for this rdataset, if we have it.
4343 for (sigrdataset = ISC_LIST_HEAD(name->list);
4344 sigrdataset != NULL;
4345 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4346 if (sigrdataset->type == dns_rdatatype_rrsig &&
4347 sigrdataset->covers == rdataset->type)
4350 if (sigrdataset == NULL) {
4351 if (!ANSWER(rdataset) && need_validation) {
4353 * Ignore non-answer rdatasets that
4354 * are missing signatures.
4361 * Normalize the rdataset and sigrdataset TTLs.
4363 if (sigrdataset != NULL) {
4364 rdataset->ttl = ISC_MIN(rdataset->ttl,
4366 sigrdataset->ttl = rdataset->ttl;
4370 * Cache this rdataset/sigrdataset pair as
4371 * pending data. Track whether it was additional
4374 if (rdataset->trust == dns_trust_additional)
4375 trust = dns_trust_pending_additional;
4377 trust = dns_trust_pending_answer;
4379 rdataset->trust = trust;
4380 if (sigrdataset != NULL)
4381 sigrdataset->trust = trust;
4382 if (!need_validation || !ANSWER(rdataset)) {
4383 addedrdataset = ardataset;
4384 result = dns_db_addrdataset(fctx->cache, node,
4385 NULL, now, rdataset,
4387 if (result == DNS_R_UNCHANGED) {
4388 result = ISC_R_SUCCESS;
4389 if (!need_validation &&
4390 ardataset != NULL &&
4391 ardataset->type == 0) {
4393 * The answer in the cache is
4394 * better than the answer we
4395 * found, and is a negative
4396 * cache entry, so we must set
4397 * eresult appropriately.
4399 if (NXDOMAIN(ardataset))
4401 DNS_R_NCACHENXDOMAIN;
4404 DNS_R_NCACHENXRRSET;
4406 * We have a negative response
4407 * from the cache so don't
4408 * attempt to add the RRSIG
4414 if (result != ISC_R_SUCCESS)
4416 if (sigrdataset != NULL) {
4417 addedrdataset = asigrdataset;
4418 result = dns_db_addrdataset(fctx->cache,
4422 if (result == DNS_R_UNCHANGED)
4423 result = ISC_R_SUCCESS;
4424 if (result != ISC_R_SUCCESS)
4426 } else if (!ANSWER(rdataset))
4430 if (ANSWER(rdataset) && need_validation) {
4431 if (fctx->type != dns_rdatatype_any &&
4432 fctx->type != dns_rdatatype_rrsig &&
4433 fctx->type != dns_rdatatype_sig) {
4435 * This is The Answer. We will
4436 * validate it, but first we cache
4437 * the rest of the response - it may
4438 * contain useful keys.
4440 INSIST(valrdataset == NULL &&
4441 valsigrdataset == NULL);
4442 valrdataset = rdataset;
4443 valsigrdataset = sigrdataset;
4446 * This is one of (potentially)
4447 * multiple answers to an ANY
4448 * or SIG query. To keep things
4449 * simple, we just start the
4450 * validator right away rather
4451 * than caching first and
4452 * having to remember which
4453 * rdatasets needed validation.
4455 result = valcreate(fctx, addrinfo,
4456 name, rdataset->type,
4461 * Defer any further validations.
4462 * This prevents multiple validators
4463 * from manipulating fctx->rmessage
4466 valoptions |= DNS_VALIDATOR_DEFER;
4468 } else if (CHAINING(rdataset)) {
4469 if (rdataset->type == dns_rdatatype_cname)
4470 eresult = DNS_R_CNAME;
4472 INSIST(rdataset->type ==
4473 dns_rdatatype_dname);
4474 eresult = DNS_R_DNAME;
4477 } else if (!EXTERNAL(rdataset)) {
4479 * It's OK to cache this rdataset now.
4481 if (ANSWER(rdataset))
4482 addedrdataset = ardataset;
4483 else if (ANSWERSIG(rdataset))
4484 addedrdataset = asigrdataset;
4486 addedrdataset = NULL;
4487 if (CHAINING(rdataset)) {
4488 if (rdataset->type == dns_rdatatype_cname)
4489 eresult = DNS_R_CNAME;
4491 INSIST(rdataset->type ==
4492 dns_rdatatype_dname);
4493 eresult = DNS_R_DNAME;
4496 if (rdataset->trust == dns_trust_glue &&
4497 (rdataset->type == dns_rdatatype_ns ||
4498 (rdataset->type == dns_rdatatype_rrsig &&
4499 rdataset->covers == dns_rdatatype_ns))) {
4501 * If the trust level is 'dns_trust_glue'
4502 * then we are adding data from a referral
4503 * we got while executing the search algorithm.
4504 * New referral data always takes precedence
4505 * over the existing cache contents.
4507 options = DNS_DBADD_FORCE;
4511 * Now we can add the rdataset.
4513 result = dns_db_addrdataset(fctx->cache,
4518 if (result == DNS_R_UNCHANGED) {
4519 if (ANSWER(rdataset) &&
4520 ardataset != NULL &&
4521 ardataset->type == 0) {
4523 * The answer in the cache is better
4524 * than the answer we found, and is
4525 * a negative cache entry, so we
4526 * must set eresult appropriately.
4528 if (NXDOMAIN(ardataset))
4529 eresult = DNS_R_NCACHENXDOMAIN;
4531 eresult = DNS_R_NCACHENXRRSET;
4533 result = ISC_R_SUCCESS;
4534 } else if (result != ISC_R_SUCCESS)
4539 if (valrdataset != NULL)
4540 result = valcreate(fctx, addrinfo, name, fctx->type,
4541 valrdataset, valsigrdataset, valoptions,
4544 if (result == ISC_R_SUCCESS && have_answer) {
4545 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4546 if (event != NULL) {
4548 * Negative results must be indicated in event->result.
4550 if (dns_rdataset_isassociated(event->rdataset) &&
4551 event->rdataset->type == dns_rdatatype_none) {
4552 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4553 eresult == DNS_R_NCACHENXRRSET);
4555 event->result = eresult;
4556 dns_db_attach(fctx->cache, adbp);
4557 dns_db_transfernode(fctx->cache, &node, anodep);
4558 clone_results(fctx);
4563 dns_db_detachnode(fctx->cache, &node);
4568 static inline isc_result_t
4569 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4571 isc_result_t result;
4572 dns_section_t section;
4575 FCTXTRACE("cache_message");
4577 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4579 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4581 for (section = DNS_SECTION_ANSWER;
4582 section <= DNS_SECTION_ADDITIONAL;
4584 result = dns_message_firstname(fctx->rmessage, section);
4585 while (result == ISC_R_SUCCESS) {
4587 dns_message_currentname(fctx->rmessage, section,
4589 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4590 result = cache_name(fctx, name, addrinfo, now);
4591 if (result != ISC_R_SUCCESS)
4594 result = dns_message_nextname(fctx->rmessage, section);
4596 if (result != ISC_R_NOMORE)
4599 if (result == ISC_R_NOMORE)
4600 result = ISC_R_SUCCESS;
4602 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4608 * Do what dns_ncache_add() does, and then compute an appropriate eresult.
4611 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4612 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4613 dns_rdataset_t *ardataset,
4614 isc_result_t *eresultp)
4616 isc_result_t result;
4617 dns_rdataset_t rdataset;
4619 if (ardataset == NULL) {
4620 dns_rdataset_init(&rdataset);
4621 ardataset = &rdataset;
4623 result = dns_ncache_add(message, cache, node, covers, now,
4625 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4627 * If the cache now contains a negative entry and we
4628 * care about whether it is DNS_R_NCACHENXDOMAIN or
4629 * DNS_R_NCACHENXRRSET then extract it.
4631 if (ardataset->type == 0) {
4633 * The cache data is a negative cache entry.
4635 if (NXDOMAIN(ardataset))
4636 *eresultp = DNS_R_NCACHENXDOMAIN;
4638 *eresultp = DNS_R_NCACHENXRRSET;
4641 * Either we don't care about the nature of the
4642 * cache rdataset (because no fetch is interested
4643 * in the outcome), or the cache rdataset is not
4644 * a negative cache entry. Whichever case it is,
4645 * we can return success.
4647 * XXXRTH There's a CNAME/DNAME problem here.
4649 *eresultp = ISC_R_SUCCESS;
4651 result = ISC_R_SUCCESS;
4653 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4654 dns_rdataset_disassociate(ardataset);
4659 static inline isc_result_t
4660 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4661 dns_rdatatype_t covers, isc_stdtime_t now)
4663 isc_result_t result, eresult;
4665 dns_resolver_t *res;
4667 dns_dbnode_t *node, **anodep;
4668 dns_rdataset_t *ardataset;
4669 isc_boolean_t need_validation, secure_domain;
4671 dns_fetchevent_t *event;
4673 unsigned int valoptions = 0;
4675 FCTXTRACE("ncache_message");
4677 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4680 need_validation = ISC_FALSE;
4681 secure_domain = ISC_FALSE;
4682 eresult = ISC_R_SUCCESS;
4687 * XXXMPA remove when we follow cnames and adjust the setting
4688 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4690 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4693 * Is DNSSEC validation required for this name?
4695 if (fctx->res->view->enablevalidation) {
4696 result = dns_keytable_issecuredomain(res->view->secroots, name,
4698 if (result != ISC_R_SUCCESS)
4701 if (!secure_domain && res->view->dlv != NULL) {
4702 valoptions = DNS_VALIDATOR_DLV;
4703 secure_domain = ISC_TRUE;
4707 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4708 need_validation = ISC_FALSE;
4710 need_validation = secure_domain;
4712 if (secure_domain) {
4714 * Mark all rdatasets as pending.
4716 dns_rdataset_t *trdataset;
4719 result = dns_message_firstname(fctx->rmessage,
4720 DNS_SECTION_AUTHORITY);
4721 while (result == ISC_R_SUCCESS) {
4723 dns_message_currentname(fctx->rmessage,
4724 DNS_SECTION_AUTHORITY,
4726 for (trdataset = ISC_LIST_HEAD(tname->list);
4728 trdataset = ISC_LIST_NEXT(trdataset, link))
4729 trdataset->trust = dns_trust_pending_answer;
4730 result = dns_message_nextname(fctx->rmessage,
4731 DNS_SECTION_AUTHORITY);
4733 if (result != ISC_R_NOMORE)
4738 if (need_validation) {
4740 * Do negative response validation.
4742 result = valcreate(fctx, addrinfo, name, fctx->type,
4743 NULL, NULL, valoptions,
4744 res->buckets[fctx->bucketnum].task);
4746 * If validation is necessary, return now. Otherwise continue
4747 * to process the message, letting the validation complete
4748 * in its own good time.
4753 LOCK(&res->buckets[fctx->bucketnum].lock);
4759 if (!HAVE_ANSWER(fctx)) {
4760 event = ISC_LIST_HEAD(fctx->events);
4761 if (event != NULL) {
4763 aname = dns_fixedname_name(&event->foundname);
4764 result = dns_name_copy(name, aname, NULL);
4765 if (result != ISC_R_SUCCESS)
4767 anodep = &event->node;
4768 ardataset = event->rdataset;
4773 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4774 if (result != ISC_R_SUCCESS)
4778 * If we are asking for a SOA record set the cache time
4779 * to zero to facilitate locating the containing zone of
4782 ttl = fctx->res->view->maxncachettl;
4783 if (fctx->type == dns_rdatatype_soa &&
4784 covers == dns_rdatatype_any &&
4785 fctx->res->zero_no_soa_ttl)
4788 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4789 covers, now, ttl, ardataset, &eresult);
4790 if (result != ISC_R_SUCCESS)
4793 if (!HAVE_ANSWER(fctx)) {
4794 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4795 if (event != NULL) {
4796 event->result = eresult;
4797 dns_db_attach(fctx->cache, adbp);
4798 dns_db_transfernode(fctx->cache, &node, anodep);
4799 clone_results(fctx);
4804 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4807 dns_db_detachnode(fctx->cache, &node);
4813 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4814 isc_boolean_t external, isc_boolean_t gluing)
4816 name->attributes |= DNS_NAMEATTR_CACHE;
4818 rdataset->trust = dns_trust_glue;
4820 * Glue with 0 TTL causes problems. We force the TTL to
4821 * 1 second to prevent this.
4823 if (rdataset->ttl == 0)
4826 rdataset->trust = dns_trust_additional;
4828 * Avoid infinite loops by only marking new rdatasets.
4830 if (!CACHE(rdataset)) {
4831 name->attributes |= DNS_NAMEATTR_CHASE;
4832 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4834 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4836 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4840 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
4841 fetchctx_t *fctx = arg;
4842 isc_result_t result;
4844 dns_rdataset_t *rdataset;
4845 isc_boolean_t external;
4846 dns_rdatatype_t rtype;
4847 isc_boolean_t gluing;
4849 REQUIRE(VALID_FCTX(fctx));
4857 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4858 addname, dns_rdatatype_any, 0, &name,
4860 if (result == ISC_R_SUCCESS) {
4861 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
4862 if (type == dns_rdatatype_a) {
4863 for (rdataset = ISC_LIST_HEAD(name->list);
4865 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4866 if (rdataset->type == dns_rdatatype_rrsig)
4867 rtype = rdataset->covers;
4869 rtype = rdataset->type;
4870 if (rtype == dns_rdatatype_a ||
4871 rtype == dns_rdatatype_aaaa)
4872 mark_related(name, rdataset, external,
4876 result = dns_message_findtype(name, type, 0,
4878 if (result == ISC_R_SUCCESS) {
4879 mark_related(name, rdataset, external, gluing);
4881 * Do we have its SIG too?
4884 result = dns_message_findtype(name,
4885 dns_rdatatype_rrsig,
4887 if (result == ISC_R_SUCCESS)
4888 mark_related(name, rdataset, external,
4894 return (ISC_R_SUCCESS);
4898 chase_additional(fetchctx_t *fctx) {
4899 isc_boolean_t rescan;
4900 dns_section_t section = DNS_SECTION_ADDITIONAL;
4901 isc_result_t result;
4906 for (result = dns_message_firstname(fctx->rmessage, section);
4907 result == ISC_R_SUCCESS;
4908 result = dns_message_nextname(fctx->rmessage, section)) {
4909 dns_name_t *name = NULL;
4910 dns_rdataset_t *rdataset;
4911 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4913 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
4915 name->attributes &= ~DNS_NAMEATTR_CHASE;
4916 for (rdataset = ISC_LIST_HEAD(name->list);
4918 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4919 if (CHASE(rdataset)) {
4920 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
4921 (void)dns_rdataset_additionaldata(rdataset,
4932 static inline isc_result_t
4933 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
4934 isc_result_t result;
4935 dns_rdata_t rdata = DNS_RDATA_INIT;
4936 dns_rdata_cname_t cname;
4938 result = dns_rdataset_first(rdataset);
4939 if (result != ISC_R_SUCCESS)
4941 dns_rdataset_current(rdataset, &rdata);
4942 result = dns_rdata_tostruct(&rdata, &cname, NULL);
4943 if (result != ISC_R_SUCCESS)
4945 dns_name_init(tname, NULL);
4946 dns_name_clone(&cname.cname, tname);
4947 dns_rdata_freestruct(&cname);
4949 return (ISC_R_SUCCESS);
4952 static inline isc_result_t
4953 dname_target(dns_rdataset_t *rdataset, dns_name_t *qname, dns_name_t *oname,
4954 dns_fixedname_t *fixeddname)
4956 isc_result_t result;
4957 dns_rdata_t rdata = DNS_RDATA_INIT;
4958 unsigned int nlabels;
4960 dns_namereln_t namereln;
4961 dns_rdata_dname_t dname;
4962 dns_fixedname_t prefix;
4965 * Get the target name of the DNAME.
4968 result = dns_rdataset_first(rdataset);
4969 if (result != ISC_R_SUCCESS)
4971 dns_rdataset_current(rdataset, &rdata);
4972 result = dns_rdata_tostruct(&rdata, &dname, NULL);
4973 if (result != ISC_R_SUCCESS)
4977 * Get the prefix of qname.
4979 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
4980 if (namereln != dns_namereln_subdomain) {
4981 dns_rdata_freestruct(&dname);
4982 return (DNS_R_FORMERR);
4984 dns_fixedname_init(&prefix);
4985 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
4986 dns_fixedname_init(fixeddname);
4987 result = dns_name_concatenate(dns_fixedname_name(&prefix),
4989 dns_fixedname_name(fixeddname), NULL);
4990 dns_rdata_freestruct(&dname);
4995 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
4996 * If bind8_ns_resp is ISC_TRUE, this is a suspected BIND 8
4997 * response to an NS query that should be treated as a referral
4998 * even though the NS records occur in the answer section
4999 * rather than the authority section.
5002 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5003 isc_boolean_t bind8_ns_resp)
5005 isc_result_t result;
5006 dns_message_t *message;
5007 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
5008 dns_rdataset_t *rdataset, *ns_rdataset;
5009 isc_boolean_t aa, negative_response;
5010 dns_rdatatype_t type;
5011 dns_section_t section =
5012 bind8_ns_resp ? DNS_SECTION_ANSWER : DNS_SECTION_AUTHORITY;
5014 FCTXTRACE("noanswer_response");
5016 message = fctx->rmessage;
5021 if (oqname == NULL) {
5023 * We have a normal, non-chained negative response or
5026 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5030 qname = &fctx->name;
5033 * We're being invoked by answer_response() after it has
5034 * followed a CNAME/DNAME chain.
5039 * If the current qname is not a subdomain of the query
5040 * domain, there's no point in looking at the authority
5041 * section without doing DNSSEC validation.
5043 * Until we do that validation, we'll just return success
5046 if (!dns_name_issubdomain(qname, &fctx->domain))
5047 return (ISC_R_SUCCESS);
5051 * We have to figure out if this is a negative response, or a
5056 * Sometimes we can tell if its a negative response by looking at
5057 * the message header.
5059 negative_response = ISC_FALSE;
5060 if (message->rcode == dns_rcode_nxdomain ||
5061 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5062 message->counts[DNS_SECTION_AUTHORITY] == 0))
5063 negative_response = ISC_TRUE;
5066 * Process the authority section.
5072 result = dns_message_firstname(message, section);
5073 while (result == ISC_R_SUCCESS) {
5075 dns_message_currentname(message, section, &name);
5076 if (dns_name_issubdomain(name, &fctx->domain)) {
5078 * Look for NS/SOA RRsets first.
5080 for (rdataset = ISC_LIST_HEAD(name->list);
5082 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5083 type = rdataset->type;
5084 if (type == dns_rdatatype_rrsig)
5085 type = rdataset->covers;
5086 if (((type == dns_rdatatype_ns ||
5087 type == dns_rdatatype_soa) &&
5088 !dns_name_issubdomain(qname, name)))
5089 return (DNS_R_FORMERR);
5090 if (type == dns_rdatatype_ns) {
5094 * Only one set of NS RRs is allowed.
5096 if (rdataset->type ==
5098 if (ns_name != NULL &&
5100 return (DNS_R_FORMERR);
5102 ns_rdataset = rdataset;
5106 rdataset->attributes |=
5107 DNS_RDATASETATTR_CACHE;
5108 rdataset->trust = dns_trust_glue;
5110 if (type == dns_rdatatype_soa) {
5112 * SOA, or RRSIG SOA.
5114 * Only one SOA is allowed.
5116 if (rdataset->type ==
5117 dns_rdatatype_soa) {
5118 if (soa_name != NULL &&
5120 return (DNS_R_FORMERR);
5124 DNS_NAMEATTR_NCACHE;
5125 rdataset->attributes |=
5126 DNS_RDATASETATTR_NCACHE;
5129 dns_trust_authauthority;
5132 dns_trust_additional;
5136 result = dns_message_nextname(message, section);
5137 if (result == ISC_R_NOMORE)
5139 else if (result != ISC_R_SUCCESS)
5144 * A negative response has a SOA record (Type 2)
5145 * and a optional NS RRset (Type 1) or it has neither
5146 * a SOA or a NS RRset (Type 3, handled above) or
5147 * rcode is NXDOMAIN (handled above) in which case
5148 * the NS RRset is allowed (Type 4).
5150 if (soa_name != NULL)
5151 negative_response = ISC_TRUE;
5153 result = dns_message_firstname(message, section);
5154 while (result == ISC_R_SUCCESS) {
5156 dns_message_currentname(message, section, &name);
5157 if (dns_name_issubdomain(name, &fctx->domain)) {
5158 for (rdataset = ISC_LIST_HEAD(name->list);
5160 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5161 type = rdataset->type;
5162 if (type == dns_rdatatype_rrsig)
5163 type = rdataset->covers;
5164 if (type == dns_rdatatype_nsec) {
5166 * NSEC or RRSIG NSEC.
5168 if (negative_response) {
5170 DNS_NAMEATTR_NCACHE;
5171 rdataset->attributes |=
5172 DNS_RDATASETATTR_NCACHE;
5176 rdataset->attributes |=
5177 DNS_RDATASETATTR_CACHE;
5181 dns_trust_authauthority;
5184 dns_trust_additional;
5186 * No additional data needs to be
5189 } else if (type == dns_rdatatype_ds) {
5193 * These should only be here if
5194 * this is a referral, and there
5195 * should only be one DS.
5197 if (ns_name == NULL)
5198 return (DNS_R_FORMERR);
5199 if (rdataset->type ==
5201 if (ds_name != NULL &&
5203 return (DNS_R_FORMERR);
5208 rdataset->attributes |=
5209 DNS_RDATASETATTR_CACHE;
5212 dns_trust_authauthority;
5215 dns_trust_additional;
5219 result = dns_message_nextname(message, section);
5220 if (result == ISC_R_NOMORE)
5222 else if (result != ISC_R_SUCCESS)
5227 * Trigger lookups for DNS nameservers.
5229 if (negative_response && message->rcode == dns_rcode_noerror &&
5230 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5231 dns_name_equal(soa_name, qname) &&
5232 !dns_name_equal(qname, dns_rootname))
5233 return (DNS_R_CHASEDSSERVERS);
5236 * Did we find anything?
5238 if (!negative_response && ns_name == NULL) {
5242 if (oqname != NULL) {
5244 * We've already got a partial CNAME/DNAME chain,
5245 * and haven't found else anything useful here, but
5246 * no error has occurred since we have an answer.
5248 return (ISC_R_SUCCESS);
5251 * The responder is insane.
5253 return (DNS_R_FORMERR);
5258 * If we found both NS and SOA, they should be the same name.
5260 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name)
5261 return (DNS_R_FORMERR);
5264 * Do we have a referral? (We only want to follow a referral if
5265 * we're not following a chain.)
5267 if (!negative_response && ns_name != NULL && oqname == NULL) {
5269 * We already know ns_name is a subdomain of fctx->domain.
5270 * If ns_name is equal to fctx->domain, we're not making
5271 * progress. We return DNS_R_FORMERR so that we'll keep
5272 * trying other servers.
5274 if (dns_name_equal(ns_name, &fctx->domain))
5275 return (DNS_R_FORMERR);
5278 * If the referral name is not a parent of the query
5279 * name, consider the responder insane.
5281 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5282 FCTXTRACE("referral to non-parent");
5283 return (DNS_R_FORMERR);
5287 * Mark any additional data related to this rdataset.
5288 * It's important that we do this before we change the
5291 INSIST(ns_rdataset != NULL);
5292 fctx->attributes |= FCTX_ATTR_GLUING;
5293 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5295 fctx->attributes &= ~FCTX_ATTR_GLUING;
5297 * NS rdatasets with 0 TTL cause problems.
5298 * dns_view_findzonecut() will not find them when we
5299 * try to follow the referral, and we'll SERVFAIL
5300 * because the best nameservers are now above QDOMAIN.
5301 * We force the TTL to 1 second to prevent this.
5303 if (ns_rdataset->ttl == 0)
5304 ns_rdataset->ttl = 1;
5306 * Set the current query domain to the referral name.
5308 * XXXRTH We should check if we're in forward-only mode, and
5309 * if so we should bail out.
5311 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5312 dns_name_free(&fctx->domain,
5313 fctx->res->buckets[fctx->bucketnum].mctx);
5314 if (dns_rdataset_isassociated(&fctx->nameservers))
5315 dns_rdataset_disassociate(&fctx->nameservers);
5316 dns_name_init(&fctx->domain, NULL);
5317 result = dns_name_dup(ns_name,
5318 fctx->res->buckets[fctx->bucketnum].mctx,
5320 if (result != ISC_R_SUCCESS)
5322 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5323 return (DNS_R_DELEGATION);
5327 * Since we're not doing a referral, we don't want to cache any
5328 * NS RRs we may have found.
5330 if (ns_name != NULL)
5331 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5333 if (negative_response && oqname == NULL)
5334 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5336 return (ISC_R_SUCCESS);
5340 answer_response(fetchctx_t *fctx) {
5341 isc_result_t result;
5342 dns_message_t *message;
5343 dns_name_t *name, *qname, tname;
5344 dns_rdataset_t *rdataset;
5345 isc_boolean_t done, external, chaining, aa, found, want_chaining;
5346 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5348 dns_rdatatype_t type;
5349 dns_fixedname_t dname, fqname;
5351 FCTXTRACE("answer_response");
5353 message = fctx->rmessage;
5356 * Examine the answer section, marking those rdatasets which are
5357 * part of the answer and should be cached.
5361 found_cname = ISC_FALSE;
5362 found_type = ISC_FALSE;
5363 chaining = ISC_FALSE;
5364 have_answer = ISC_FALSE;
5365 want_chaining = ISC_FALSE;
5366 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5370 qname = &fctx->name;
5372 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5373 while (!done && result == ISC_R_SUCCESS) {
5375 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5376 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5377 if (dns_name_equal(name, qname)) {
5378 wanted_chaining = ISC_FALSE;
5379 for (rdataset = ISC_LIST_HEAD(name->list);
5381 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5383 want_chaining = ISC_FALSE;
5385 if (rdataset->type == type && !found_cname) {
5387 * We've found an ordinary answer.
5390 found_type = ISC_TRUE;
5392 aflag = DNS_RDATASETATTR_ANSWER;
5393 } else if (type == dns_rdatatype_any) {
5395 * We've found an answer matching
5396 * an ANY query. There may be
5400 aflag = DNS_RDATASETATTR_ANSWER;
5401 } else if (rdataset->type == dns_rdatatype_rrsig
5402 && rdataset->covers == type
5405 * We've found a signature that
5406 * covers the type we're looking for.
5409 found_type = ISC_TRUE;
5410 aflag = DNS_RDATASETATTR_ANSWERSIG;
5411 } else if (rdataset->type ==
5415 * We're looking for something else,
5416 * but we found a CNAME.
5418 * Getting a CNAME response for some
5419 * query types is an error.
5421 if (type == dns_rdatatype_rrsig ||
5422 type == dns_rdatatype_dnskey ||
5423 type == dns_rdatatype_nsec)
5424 return (DNS_R_FORMERR);
5426 found_cname = ISC_TRUE;
5427 want_chaining = ISC_TRUE;
5428 aflag = DNS_RDATASETATTR_ANSWER;
5429 result = cname_target(rdataset,
5431 if (result != ISC_R_SUCCESS)
5433 } else if (rdataset->type == dns_rdatatype_rrsig
5434 && rdataset->covers ==
5438 * We're looking for something else,
5439 * but we found a SIG CNAME.
5442 found_cname = ISC_TRUE;
5443 aflag = DNS_RDATASETATTR_ANSWERSIG;
5448 * We've found an answer to our
5453 rdataset->attributes |=
5454 DNS_RDATASETATTR_CACHE;
5455 rdataset->trust = dns_trust_answer;
5458 * This data is "the" answer
5459 * to our question only if
5460 * we're not chaining (i.e.
5461 * if we haven't followed
5462 * a CNAME or DNAME).
5466 DNS_RDATASETATTR_ANSWER)
5467 have_answer = ISC_TRUE;
5469 DNS_NAMEATTR_ANSWER;
5470 rdataset->attributes |= aflag;
5473 dns_trust_authanswer;
5474 } else if (external) {
5476 * This data is outside of
5477 * our query domain, and
5478 * may not be cached.
5480 rdataset->attributes |=
5481 DNS_RDATASETATTR_EXTERNAL;
5485 * Mark any additional data related
5488 (void)dns_rdataset_additionaldata(
5496 if (want_chaining) {
5497 wanted_chaining = ISC_TRUE;
5499 DNS_NAMEATTR_CHAINING;
5500 rdataset->attributes |=
5501 DNS_RDATASETATTR_CHAINING;
5506 * We could add an "else" clause here and
5507 * log that we're ignoring this rdataset.
5511 * If wanted_chaining is true, we've done
5512 * some chaining as the result of processing
5513 * this node, and thus we need to set
5516 * We don't set chaining inside of the
5517 * rdataset loop because doing that would
5518 * cause us to ignore the signatures of
5521 if (wanted_chaining)
5522 chaining = ISC_TRUE;
5525 * Look for a DNAME (or its SIG). Anything else is
5528 wanted_chaining = ISC_FALSE;
5529 for (rdataset = ISC_LIST_HEAD(name->list);
5531 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5532 isc_boolean_t found_dname = ISC_FALSE;
5535 if (rdataset->type == dns_rdatatype_dname) {
5537 * We're looking for something else,
5538 * but we found a DNAME.
5540 * If we're not chaining, then the
5541 * DNAME should not be external.
5543 if (!chaining && external)
5544 return (DNS_R_FORMERR);
5546 want_chaining = ISC_TRUE;
5547 aflag = DNS_RDATASETATTR_ANSWER;
5548 result = dname_target(rdataset,
5551 if (result == ISC_R_NOSPACE) {
5553 * We can't construct the
5554 * DNAME target. Do not
5557 want_chaining = ISC_FALSE;
5558 } else if (result != ISC_R_SUCCESS)
5561 found_dname = ISC_TRUE;
5562 } else if (rdataset->type == dns_rdatatype_rrsig
5563 && rdataset->covers ==
5564 dns_rdatatype_dname) {
5566 * We've found a signature that
5570 aflag = DNS_RDATASETATTR_ANSWERSIG;
5575 * We've found an answer to our
5580 rdataset->attributes |=
5581 DNS_RDATASETATTR_CACHE;
5582 rdataset->trust = dns_trust_answer;
5585 * This data is "the" answer
5586 * to our question only if
5587 * we're not chaining.
5591 DNS_RDATASETATTR_ANSWER)
5592 have_answer = ISC_TRUE;
5594 DNS_NAMEATTR_ANSWER;
5595 rdataset->attributes |= aflag;
5598 dns_trust_authanswer;
5599 } else if (external) {
5600 rdataset->attributes |=
5601 DNS_RDATASETATTR_EXTERNAL;
5609 * Copy the dname into the
5612 * Although we check for
5613 * failure of the copy
5614 * operation, in practice it
5615 * should never fail since
5616 * we already know that the
5617 * result fits in a fixedname.
5619 dns_fixedname_init(&fqname);
5620 result = dns_name_copy(
5621 dns_fixedname_name(&dname),
5622 dns_fixedname_name(&fqname),
5624 if (result != ISC_R_SUCCESS)
5626 wanted_chaining = ISC_TRUE;
5628 DNS_NAMEATTR_CHAINING;
5629 rdataset->attributes |=
5630 DNS_RDATASETATTR_CHAINING;
5631 qname = dns_fixedname_name(
5636 if (wanted_chaining)
5637 chaining = ISC_TRUE;
5639 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
5641 if (result == ISC_R_NOMORE)
5642 result = ISC_R_SUCCESS;
5643 if (result != ISC_R_SUCCESS)
5647 * We should have found an answer.
5650 return (DNS_R_FORMERR);
5653 * This response is now potentially cacheable.
5655 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5658 * Did chaining end before we got the final answer?
5662 * Yes. This may be a negative reply, so hand off
5663 * authority section processing to the noanswer code.
5664 * If it isn't a noanswer response, no harm will be
5667 return (noanswer_response(fctx, qname, ISC_FALSE));
5671 * We didn't end with an incomplete chain, so the rcode should be
5674 if (message->rcode != dns_rcode_noerror)
5675 return (DNS_R_FORMERR);
5678 * Examine the authority section (if there is one).
5680 * We expect there to be only one owner name for all the rdatasets
5681 * in this section, and we expect that it is not external.
5684 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
5685 while (!done && result == ISC_R_SUCCESS) {
5687 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
5688 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5691 * We expect to find NS or SIG NS rdatasets, and
5694 for (rdataset = ISC_LIST_HEAD(name->list);
5696 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5697 if (rdataset->type == dns_rdatatype_ns ||
5698 (rdataset->type == dns_rdatatype_rrsig &&
5699 rdataset->covers == dns_rdatatype_ns)) {
5702 rdataset->attributes |=
5703 DNS_RDATASETATTR_CACHE;
5704 if (aa && !chaining)
5706 dns_trust_authauthority;
5709 dns_trust_additional;
5712 * Mark any additional data related
5715 (void)dns_rdataset_additionaldata(
5723 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
5725 if (result == ISC_R_NOMORE)
5726 result = ISC_R_SUCCESS;
5732 resume_dslookup(isc_task_t *task, isc_event_t *event) {
5733 dns_fetchevent_t *fevent;
5734 dns_resolver_t *res;
5736 isc_result_t result;
5737 isc_boolean_t bucket_empty = ISC_FALSE;
5738 isc_boolean_t locked = ISC_FALSE;
5739 unsigned int bucketnum;
5740 dns_rdataset_t nameservers;
5741 dns_fixedname_t fixed;
5744 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
5745 fevent = (dns_fetchevent_t *)event;
5746 fctx = event->ev_arg;
5747 REQUIRE(VALID_FCTX(fctx));
5751 FCTXTRACE("resume_dslookup");
5753 if (fevent->node != NULL)
5754 dns_db_detachnode(fevent->db, &fevent->node);
5755 if (fevent->db != NULL)
5756 dns_db_detach(&fevent->db);
5758 dns_rdataset_init(&nameservers);
5760 bucketnum = fctx->bucketnum;
5761 if (fevent->result == ISC_R_CANCELED) {
5762 dns_resolver_destroyfetch(&fctx->nsfetch);
5763 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
5764 } else if (fevent->result == ISC_R_SUCCESS) {
5766 FCTXTRACE("resuming DS lookup");
5768 dns_resolver_destroyfetch(&fctx->nsfetch);
5769 if (dns_rdataset_isassociated(&fctx->nameservers))
5770 dns_rdataset_disassociate(&fctx->nameservers);
5771 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
5772 dns_name_free(&fctx->domain,
5773 fctx->res->buckets[bucketnum].mctx);
5774 dns_name_init(&fctx->domain, NULL);
5775 result = dns_name_dup(&fctx->nsname,
5776 fctx->res->buckets[bucketnum].mctx,
5778 if (result != ISC_R_SUCCESS) {
5779 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5785 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
5788 dns_rdataset_t *nsrdataset = NULL;
5791 * Retrieve state from fctx->nsfetch before we destroy it.
5793 dns_fixedname_init(&fixed);
5794 domain = dns_fixedname_name(&fixed);
5795 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
5796 if (dns_name_equal(&fctx->nsname, domain)) {
5797 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5798 dns_resolver_destroyfetch(&fctx->nsfetch);
5801 if (dns_rdataset_isassociated(
5802 &fctx->nsfetch->private->nameservers)) {
5804 &fctx->nsfetch->private->nameservers,
5806 nsrdataset = &nameservers;
5809 dns_resolver_destroyfetch(&fctx->nsfetch);
5810 n = dns_name_countlabels(&fctx->nsname);
5811 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
5814 if (dns_rdataset_isassociated(fevent->rdataset))
5815 dns_rdataset_disassociate(fevent->rdataset);
5816 FCTXTRACE("continuing to look for parent's NS records");
5817 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
5818 dns_rdatatype_ns, domain,
5819 nsrdataset, NULL, 0, task,
5820 resume_dslookup, fctx,
5821 &fctx->nsrrset, NULL,
5823 if (result != ISC_R_SUCCESS)
5824 fctx_done(fctx, result, __LINE__);
5826 LOCK(&res->buckets[bucketnum].lock);
5833 if (dns_rdataset_isassociated(&nameservers))
5834 dns_rdataset_disassociate(&nameservers);
5835 if (dns_rdataset_isassociated(fevent->rdataset))
5836 dns_rdataset_disassociate(fevent->rdataset);
5837 INSIST(fevent->sigrdataset == NULL);
5838 isc_event_free(&event);
5840 LOCK(&res->buckets[bucketnum].lock);
5842 if (fctx->references == 0)
5843 bucket_empty = fctx_destroy(fctx);
5844 UNLOCK(&res->buckets[bucketnum].lock);
5850 checknamessection(dns_message_t *message, dns_section_t section) {
5851 isc_result_t result;
5853 dns_rdata_t rdata = DNS_RDATA_INIT;
5854 dns_rdataset_t *rdataset;
5856 for (result = dns_message_firstname(message, section);
5857 result == ISC_R_SUCCESS;
5858 result = dns_message_nextname(message, section))
5861 dns_message_currentname(message, section, &name);
5862 for (rdataset = ISC_LIST_HEAD(name->list);
5864 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5865 for (result = dns_rdataset_first(rdataset);
5866 result == ISC_R_SUCCESS;
5867 result = dns_rdataset_next(rdataset)) {
5868 dns_rdataset_current(rdataset, &rdata);
5869 if (!dns_rdata_checkowner(name, rdata.rdclass,
5872 !dns_rdata_checknames(&rdata, name, NULL))
5874 rdataset->attributes |=
5875 DNS_RDATASETATTR_CHECKNAMES;
5877 dns_rdata_reset(&rdata);
5884 checknames(dns_message_t *message) {
5886 checknamessection(message, DNS_SECTION_ANSWER);
5887 checknamessection(message, DNS_SECTION_AUTHORITY);
5888 checknamessection(message, DNS_SECTION_ADDITIONAL);
5892 * Log server NSID at log level 'level'
5895 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
5897 static const char hex[17] = "0123456789abcdef";
5898 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
5899 isc_uint16_t optcode, nsid_len, buflen, i;
5900 isc_result_t result;
5901 isc_buffer_t nsidbuf;
5903 unsigned char *p, *buf, *nsid;
5905 /* Extract rdata from OPT rdataset */
5906 result = dns_rdataset_first(opt);
5907 if (result != ISC_R_SUCCESS)
5908 return (ISC_R_FAILURE);
5910 dns_rdata_init(&rdata);
5911 dns_rdataset_current(opt, &rdata);
5912 if (rdata.length < 4)
5913 return (ISC_R_FAILURE);
5915 /* Check for NSID */
5916 isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
5917 isc_buffer_add(&nsidbuf, rdata.length);
5918 optcode = isc_buffer_getuint16(&nsidbuf);
5919 nsid_len = isc_buffer_getuint16(&nsidbuf);
5920 if (optcode != DNS_OPT_NSID || nsid_len == 0)
5921 return (ISC_R_FAILURE);
5923 /* Allocate buffer for storing hex version of the NSID */
5924 buflen = nsid_len * 2 + 1;
5925 buf = isc_mem_get(mctx, buflen);
5927 return (ISC_R_NOSPACE);
5929 /* Convert to hex */
5931 nsid = rdata.data + 4;
5932 for (i = 0; i < nsid_len; i++) {
5933 *p++ = hex[(nsid[0] >> 4) & 0xf];
5934 *p++ = hex[nsid[0] & 0xf];
5939 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
5941 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5942 DNS_LOGMODULE_RESOLVER, level,
5943 "received NSID '%s' from %s", buf, addrbuf);
5946 isc_mem_put(mctx, buf, buflen);
5947 return (ISC_R_SUCCESS);
5951 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
5952 isc_buffer_t buffer;
5955 isc_result_t result;
5957 if (! isc_log_wouldlog(dns_lctx, level))
5961 * Note that these are multiline debug messages. We want a newline
5962 * to appear in the log after each message.
5966 buf = isc_mem_get(mctx, len);
5969 isc_buffer_init(&buffer, buf, len);
5970 result = dns_message_totext(message, &dns_master_style_debug,
5972 if (result == ISC_R_NOSPACE) {
5973 isc_mem_put(mctx, buf, len);
5975 } else if (result == ISC_R_SUCCESS)
5976 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5977 DNS_LOGMODULE_RESOLVER, level,
5978 "received packet:\n%.*s",
5979 (int)isc_buffer_usedlength(&buffer),
5981 } while (result == ISC_R_NOSPACE);
5984 isc_mem_put(mctx, buf, len);
5988 resquery_response(isc_task_t *task, isc_event_t *event) {
5989 isc_result_t result = ISC_R_SUCCESS;
5990 resquery_t *query = event->ev_arg;
5991 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
5992 isc_boolean_t keep_trying, get_nameservers, resend;
5993 isc_boolean_t truncated;
5994 dns_message_t *message;
5995 dns_rdataset_t *opt;
5998 dns_fixedname_t foundname;
6000 isc_time_t tnow, *finish;
6001 dns_adbaddrinfo_t *addrinfo;
6002 unsigned int options;
6003 unsigned int findoptions;
6004 isc_result_t broken_server;
6005 badnstype_t broken_type = badns_response;
6007 REQUIRE(VALID_QUERY(query));
6009 options = query->options;
6010 REQUIRE(VALID_FCTX(fctx));
6011 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6015 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6016 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6018 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6020 (void)isc_timer_touch(fctx->timer);
6022 keep_trying = ISC_FALSE;
6023 broken_server = ISC_R_SUCCESS;
6024 get_nameservers = ISC_FALSE;
6026 truncated = ISC_FALSE;
6029 if (fctx->res->exiting) {
6030 result = ISC_R_SHUTTINGDOWN;
6035 fctx->timeout = ISC_FALSE;
6038 * XXXRTH We should really get the current time just once. We
6039 * need a routine to convert from an isc_time_t to an
6044 isc_stdtime_get(&now);
6047 * Did the dispatcher have a problem?
6049 if (devent->result != ISC_R_SUCCESS) {
6050 if (devent->result == ISC_R_EOF &&
6051 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6053 * The problem might be that they
6054 * don't understand EDNS0. Turn it
6055 * off and try again.
6057 options |= DNS_FETCHOPT_NOEDNS0;
6060 * Remember that they don't like EDNS0.
6062 dns_adb_changeflags(fctx->adb,
6064 DNS_FETCHOPT_NOEDNS0,
6065 DNS_FETCHOPT_NOEDNS0);
6068 * There's no hope for this query.
6070 keep_trying = ISC_TRUE;
6073 * If this is a network error on an exclusive query
6074 * socket, mark the server as bad so that we won't try
6075 * it for this fetch again.
6077 if (query->exclusivesocket &&
6078 (devent->result == ISC_R_HOSTUNREACH ||
6079 devent->result == ISC_R_NETUNREACH ||
6080 devent->result == ISC_R_CONNREFUSED ||
6081 devent->result == ISC_R_CANCELED)) {
6082 broken_server = devent->result;
6083 broken_type = badns_unreachable;
6089 message = fctx->rmessage;
6091 if (query->tsig != NULL) {
6092 result = dns_message_setquerytsig(message, query->tsig);
6093 if (result != ISC_R_SUCCESS)
6097 if (query->tsigkey) {
6098 result = dns_message_settsigkey(message, query->tsigkey);
6099 if (result != ISC_R_SUCCESS)
6103 result = dns_message_parse(message, &devent->buffer, 0);
6104 if (result != ISC_R_SUCCESS) {
6106 case ISC_R_UNEXPECTEDEND:
6107 if (!message->question_ok ||
6108 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6109 (options & DNS_FETCHOPT_TCP) != 0) {
6111 * Either the message ended prematurely,
6112 * and/or wasn't marked as being truncated,
6113 * and/or this is a response to a query we
6114 * sent over TCP. In all of these cases,
6115 * something is wrong with the remote
6116 * server and we don't want to retry using
6119 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6122 * The problem might be that they
6123 * don't understand EDNS0. Turn it
6124 * off and try again.
6126 options |= DNS_FETCHOPT_NOEDNS0;
6129 * Remember that they don't like EDNS0.
6131 dns_adb_changeflags(
6134 DNS_FETCHOPT_NOEDNS0,
6135 DNS_FETCHOPT_NOEDNS0);
6136 inc_stats(fctx->res,
6137 dns_resstatscounter_edns0fail);
6139 broken_server = result;
6140 keep_trying = ISC_TRUE;
6145 * We defer retrying via TCP for a bit so we can
6146 * check out this message further.
6148 truncated = ISC_TRUE;
6151 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6153 * The problem might be that they
6154 * don't understand EDNS0. Turn it
6155 * off and try again.
6157 options |= DNS_FETCHOPT_NOEDNS0;
6160 * Remember that they don't like EDNS0.
6162 dns_adb_changeflags(fctx->adb,
6164 DNS_FETCHOPT_NOEDNS0,
6165 DNS_FETCHOPT_NOEDNS0);
6166 inc_stats(fctx->res,
6167 dns_resstatscounter_edns0fail);
6169 broken_server = DNS_R_UNEXPECTEDRCODE;
6170 keep_trying = ISC_TRUE;
6175 * Something bad has happened.
6183 * Log the incoming packet.
6185 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6188 * Did we request NSID? If so, and if the response contains
6189 * NSID data, log it at INFO level.
6191 opt = dns_message_getopt(message);
6192 if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6193 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6196 * If the message is signed, check the signature. If not, this
6197 * returns success anyway.
6199 result = dns_message_checksig(message, fctx->res->view);
6200 if (result != ISC_R_SUCCESS)
6204 * The dispatcher should ensure we only get responses with QR set.
6206 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6208 * INSIST() that the message comes from the place we sent it to,
6209 * since the dispatch code should ensure this.
6211 * INSIST() that the message id is correct (this should also be
6212 * ensured by the dispatch code).
6217 * Deal with truncated responses by retrying using TCP.
6219 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6220 truncated = ISC_TRUE;
6223 inc_stats(fctx->res, dns_resstatscounter_truncated);
6224 if ((options & DNS_FETCHOPT_TCP) != 0) {
6225 broken_server = DNS_R_TRUNCATEDTCP;
6226 keep_trying = ISC_TRUE;
6228 options |= DNS_FETCHOPT_TCP;
6235 * Is it a query response?
6237 if (message->opcode != dns_opcode_query) {
6239 broken_server = DNS_R_UNEXPECTEDOPCODE;
6240 keep_trying = ISC_TRUE;
6245 * Update statistics about erroneous responses.
6247 if (message->rcode != dns_rcode_noerror) {
6248 switch (message->rcode) {
6249 case dns_rcode_nxdomain:
6250 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6252 case dns_rcode_servfail:
6253 inc_stats(fctx->res, dns_resstatscounter_servfail);
6255 case dns_rcode_formerr:
6256 inc_stats(fctx->res, dns_resstatscounter_formerr);
6259 inc_stats(fctx->res, dns_resstatscounter_othererror);
6265 * Is the remote server broken, or does it dislike us?
6267 if (message->rcode != dns_rcode_noerror &&
6268 message->rcode != dns_rcode_nxdomain) {
6269 if (((message->rcode == dns_rcode_formerr ||
6270 message->rcode == dns_rcode_notimp) ||
6271 (message->rcode == dns_rcode_servfail &&
6272 dns_message_getopt(message) == NULL)) &&
6273 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6275 * It's very likely they don't like EDNS0.
6276 * If the response code is SERVFAIL, also check if the
6277 * response contains an OPT RR and don't cache the
6278 * failure since it can be returned for various other
6281 * XXXRTH We should check if the question
6282 * we're asking requires EDNS0, and
6283 * if so, we should bail out.
6285 options |= DNS_FETCHOPT_NOEDNS0;
6288 * Remember that they don't like EDNS0.
6290 if (message->rcode != dns_rcode_servfail)
6291 dns_adb_changeflags(fctx->adb, query->addrinfo,
6292 DNS_FETCHOPT_NOEDNS0,
6293 DNS_FETCHOPT_NOEDNS0);
6294 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6295 } else if (message->rcode == dns_rcode_formerr) {
6296 if (ISFORWARDER(query->addrinfo)) {
6298 * This forwarder doesn't understand us,
6299 * but other forwarders might. Keep trying.
6301 broken_server = DNS_R_REMOTEFORMERR;
6302 keep_trying = ISC_TRUE;
6305 * The server doesn't understand us. Since
6306 * all servers for a zone need similar
6307 * capabilities, we assume that we will get
6308 * FORMERR from all servers, and thus we
6309 * cannot make any more progress with this
6312 result = DNS_R_FORMERR;
6314 } else if (message->rcode == dns_rcode_yxdomain) {
6316 * DNAME mapping failed because the new name
6317 * was too long. There's no chance of success
6320 result = DNS_R_YXDOMAIN;
6321 } else if (message->rcode == dns_rcode_badvers) {
6322 unsigned int flags, mask;
6323 unsigned int version;
6326 version = (opt->ttl >> 16) & 0xff;
6327 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6328 DNS_FETCHOPT_EDNSVERSIONSET;
6329 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6330 DNS_FETCHOPT_EDNSVERSIONSET;
6333 dns_adb_changeflags(fctx->adb, query->addrinfo,
6337 broken_server = DNS_R_BADVERS;
6338 keep_trying = ISC_TRUE;
6345 broken_server = DNS_R_UNEXPECTEDRCODE;
6346 INSIST(broken_server != ISC_R_SUCCESS);
6347 keep_trying = ISC_TRUE;
6353 * Is the question the same as the one we asked?
6355 result = same_question(fctx);
6356 if (result != ISC_R_SUCCESS) {
6358 if (result == DNS_R_FORMERR)
6359 keep_trying = ISC_TRUE;
6364 * Is the server lame?
6366 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6368 inc_stats(fctx->res, dns_resstatscounter_lame);
6369 log_lame(fctx, query->addrinfo);
6370 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6371 &fctx->name, fctx->type,
6372 now + fctx->res->lame_ttl);
6373 if (result != ISC_R_SUCCESS)
6374 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6375 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6376 "could not mark server as lame: %s",
6377 isc_result_totext(result));
6378 broken_server = DNS_R_LAME;
6379 keep_trying = ISC_TRUE;
6384 * Enforce delegations only zones like NET and COM.
6386 if (!ISFORWARDER(query->addrinfo) &&
6387 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6388 !dns_name_equal(&fctx->domain, &fctx->name) &&
6389 fix_mustbedelegationornxdomain(message, fctx)) {
6390 char namebuf[DNS_NAME_FORMATSIZE];
6391 char domainbuf[DNS_NAME_FORMATSIZE];
6392 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6396 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6397 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6398 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6399 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6401 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6404 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6405 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6406 "enforced delegation-only for '%s' (%s/%s/%s) "
6408 domainbuf, namebuf, typebuf, classbuf, addrbuf);
6411 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6412 checknames(message);
6417 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6420 * Did we get any answers?
6422 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6423 (message->rcode == dns_rcode_noerror ||
6424 message->rcode == dns_rcode_nxdomain)) {
6426 * We've got answers. However, if we sent
6427 * a BIND 8 server an NS query, it may have
6428 * incorrectly responded with a non-authoritative
6429 * answer instead of a referral. Since this
6430 * answer lacks the SIGs necessary to do DNSSEC
6431 * validation, we must invoke the following special
6432 * kludge to treat it as a referral.
6434 if (fctx->type == dns_rdatatype_ns &&
6435 (message->flags & DNS_MESSAGEFLAG_AA) == 0 &&
6436 !ISFORWARDER(query->addrinfo))
6438 result = noanswer_response(fctx, NULL, ISC_TRUE);
6439 if (result != DNS_R_DELEGATION) {
6441 * The answer section must have contained
6442 * something other than the NS records
6443 * we asked for. Since AA is not set
6444 * and the server is not a forwarder,
6445 * it is technically lame and it's easier
6446 * to treat it as such than to figure out
6447 * some more elaborate course of action.
6449 broken_server = DNS_R_LAME;
6450 keep_trying = ISC_TRUE;
6453 goto force_referral;
6455 result = answer_response(fctx);
6456 if (result != ISC_R_SUCCESS) {
6457 if (result == DNS_R_FORMERR)
6458 keep_trying = ISC_TRUE;
6461 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
6462 message->rcode == dns_rcode_noerror ||
6463 message->rcode == dns_rcode_nxdomain) {
6465 * NXDOMAIN, NXRDATASET, or referral.
6467 result = noanswer_response(fctx, NULL, ISC_FALSE);
6468 if (result == DNS_R_CHASEDSSERVERS) {
6469 } else if (result == DNS_R_DELEGATION) {
6472 * We don't have the answer, but we know a better
6475 get_nameservers = ISC_TRUE;
6476 keep_trying = ISC_TRUE;
6478 * We have a new set of name servers, and it
6479 * has not experienced any restarts yet.
6484 * Update local statistics counters collected for each
6488 fctx->querysent = 0;
6489 fctx->lamecount = 0;
6494 result = ISC_R_SUCCESS;
6495 } else if (result != ISC_R_SUCCESS) {
6497 * Something has gone wrong.
6499 if (result == DNS_R_FORMERR)
6500 keep_trying = ISC_TRUE;
6505 * The server is insane.
6508 broken_server = DNS_R_UNEXPECTEDRCODE;
6509 keep_trying = ISC_TRUE;
6514 * Follow additional section data chains.
6516 chase_additional(fctx);
6519 * Cache the cacheable parts of the message. This may also cause
6520 * work to be queued to the DNSSEC validator.
6522 if (WANTCACHE(fctx)) {
6523 result = cache_message(fctx, query->addrinfo, now);
6524 if (result != ISC_R_SUCCESS)
6529 * Ncache the negatively cacheable parts of the message. This may
6530 * also cause work to be queued to the DNSSEC validator.
6532 if (WANTNCACHE(fctx)) {
6533 dns_rdatatype_t covers;
6534 if (message->rcode == dns_rcode_nxdomain)
6535 covers = dns_rdatatype_any;
6537 covers = fctx->type;
6540 * Cache any negative cache entries in the message.
6542 result = ncache_message(fctx, query->addrinfo, covers, now);
6547 * Remember the query's addrinfo, in case we need to mark the
6550 addrinfo = query->addrinfo;
6555 * XXXRTH Don't cancel the query if waiting for validation?
6557 fctx_cancelquery(&query, &devent, finish, ISC_FALSE);
6560 if (result == DNS_R_FORMERR)
6561 broken_server = DNS_R_FORMERR;
6562 if (broken_server != ISC_R_SUCCESS) {
6564 * Add this server to the list of bad servers for
6567 add_bad(fctx, addrinfo, broken_server, broken_type);
6570 if (get_nameservers) {
6572 dns_fixedname_init(&foundname);
6573 fname = dns_fixedname_name(&foundname);
6574 if (result != ISC_R_SUCCESS) {
6575 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6579 if (dns_rdatatype_atparent(fctx->type))
6580 findoptions |= DNS_DBFIND_NOEXACT;
6581 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
6584 name = &fctx->domain;
6585 result = dns_view_findzonecut(fctx->res->view,
6591 if (result != ISC_R_SUCCESS) {
6592 FCTXTRACE("couldn't find a zonecut");
6593 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6596 if (!dns_name_issubdomain(fname, &fctx->domain)) {
6598 * The best nameservers are now above our
6601 FCTXTRACE("nameservers now above QDOMAIN");
6602 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6605 dns_name_free(&fctx->domain,
6606 fctx->res->buckets[fctx->bucketnum].mctx);
6607 dns_name_init(&fctx->domain, NULL);
6608 result = dns_name_dup(fname,
6609 fctx->res->buckets[fctx->bucketnum].mctx,
6611 if (result != ISC_R_SUCCESS) {
6612 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6615 fctx_cancelqueries(fctx, ISC_TRUE);
6616 fctx_cleanupfinds(fctx);
6617 fctx_cleanupaltfinds(fctx);
6618 fctx_cleanupforwaddrs(fctx);
6619 fctx_cleanupaltaddrs(fctx);
6624 fctx_try(fctx, !get_nameservers, ISC_FALSE);
6625 } else if (resend) {
6627 * Resend (probably with changed options).
6629 FCTXTRACE("resend");
6630 inc_stats(fctx->res, dns_resstatscounter_retry);
6631 result = fctx_query(fctx, addrinfo, options);
6632 if (result != ISC_R_SUCCESS)
6633 fctx_done(fctx, result, __LINE__);
6634 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
6636 * All has gone well so far, but we are waiting for the
6637 * DNSSEC validator to validate the answer.
6639 FCTXTRACE("wait for validator");
6640 fctx_cancelqueries(fctx, ISC_TRUE);
6642 * We must not retransmit while the validator is working;
6643 * it has references to the current rmessage.
6645 result = fctx_stopidletimer(fctx);
6646 if (result != ISC_R_SUCCESS)
6647 fctx_done(fctx, result, __LINE__);
6648 } else if (result == DNS_R_CHASEDSSERVERS) {
6650 add_bad(fctx, addrinfo, result, broken_type);
6651 fctx_cancelqueries(fctx, ISC_TRUE);
6652 fctx_cleanupfinds(fctx);
6653 fctx_cleanupforwaddrs(fctx);
6655 n = dns_name_countlabels(&fctx->name);
6656 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
6658 FCTXTRACE("suspending DS lookup to find parent's NS records");
6660 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6662 NULL, NULL, NULL, 0, task,
6663 resume_dslookup, fctx,
6664 &fctx->nsrrset, NULL,
6666 if (result != ISC_R_SUCCESS)
6667 fctx_done(fctx, result, __LINE__);
6668 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6670 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6671 result = fctx_stopidletimer(fctx);
6672 if (result != ISC_R_SUCCESS)
6673 fctx_done(fctx, result, __LINE__);
6678 fctx_done(fctx, result, __LINE__);
6684 *** Resolver Methods
6687 destroy_badcache(dns_resolver_t *res) {
6688 dns_badcache_t *bad, *next;
6691 if (res->badcache != NULL) {
6692 for (i = 0; i < res->badhash; i++)
6693 for (bad = res->badcache[i]; bad != NULL;
6696 isc_mem_put(res->mctx, bad, sizeof(*bad) +
6700 isc_mem_put(res->mctx, res->badcache,
6701 sizeof(*res->badcache) * res->badhash);
6702 res->badcache = NULL;
6704 INSIST(res->badcount == 0);
6709 destroy(dns_resolver_t *res) {
6713 REQUIRE(res->references == 0);
6714 REQUIRE(!res->priming);
6715 REQUIRE(res->primefetch == NULL);
6719 INSIST(res->nfctx == 0);
6721 DESTROYLOCK(&res->primelock);
6722 DESTROYLOCK(&res->nlock);
6723 DESTROYLOCK(&res->lock);
6724 for (i = 0; i < res->nbuckets; i++) {
6725 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
6726 isc_task_shutdown(res->buckets[i].task);
6727 isc_task_detach(&res->buckets[i].task);
6728 DESTROYLOCK(&res->buckets[i].lock);
6729 isc_mem_detach(&res->buckets[i].mctx);
6731 isc_mem_put(res->mctx, res->buckets,
6732 res->nbuckets * sizeof(fctxbucket_t));
6733 if (res->dispatchv4 != NULL)
6734 dns_dispatch_detach(&res->dispatchv4);
6735 if (res->dispatchv6 != NULL)
6736 dns_dispatch_detach(&res->dispatchv6);
6737 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
6738 ISC_LIST_UNLINK(res->alternates, a, link);
6740 dns_name_free(&a->_u._n.name, res->mctx);
6741 isc_mem_put(res->mctx, a, sizeof(*a));
6743 dns_resolver_reset_algorithms(res);
6744 destroy_badcache(res);
6745 dns_resolver_resetmustbesecure(res);
6747 isc_rwlock_destroy(&res->alglock);
6750 isc_rwlock_destroy(&res->mbslock);
6752 isc_timer_detach(&res->spillattimer);
6754 isc_mem_put(res->mctx, res, sizeof(*res));
6758 send_shutdown_events(dns_resolver_t *res) {
6759 isc_event_t *event, *next_event;
6763 * Caller must be holding the resolver lock.
6766 for (event = ISC_LIST_HEAD(res->whenshutdown);
6768 event = next_event) {
6769 next_event = ISC_LIST_NEXT(event, ev_link);
6770 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
6771 etask = event->ev_sender;
6772 event->ev_sender = res;
6773 isc_task_sendanddetach(&etask, &event);
6778 empty_bucket(dns_resolver_t *res) {
6779 RTRACE("empty_bucket");
6783 INSIST(res->activebuckets > 0);
6784 res->activebuckets--;
6785 if (res->activebuckets == 0)
6786 send_shutdown_events(res);
6792 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
6793 dns_resolver_t *res = event->ev_arg;
6794 isc_result_t result;
6796 isc_boolean_t logit = ISC_FALSE;
6798 REQUIRE(VALID_RESOLVER(res));
6803 INSIST(!res->exiting);
6804 if (res->spillat > res->spillatmin) {
6808 if (res->spillat <= res->spillatmin) {
6809 result = isc_timer_reset(res->spillattimer,
6810 isc_timertype_inactive, NULL,
6812 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6814 count = res->spillat;
6817 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6818 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6819 "clients-per-query decreased to %u", count);
6821 isc_event_free(&event);
6825 dns_resolver_create(dns_view_t *view,
6826 isc_taskmgr_t *taskmgr, unsigned int ntasks,
6827 isc_socketmgr_t *socketmgr,
6828 isc_timermgr_t *timermgr,
6829 unsigned int options,
6830 dns_dispatchmgr_t *dispatchmgr,
6831 dns_dispatch_t *dispatchv4,
6832 dns_dispatch_t *dispatchv6,
6833 dns_resolver_t **resp)
6835 dns_resolver_t *res;
6836 isc_result_t result = ISC_R_SUCCESS;
6837 unsigned int i, buckets_created = 0;
6838 isc_task_t *task = NULL;
6843 * Create a resolver.
6846 REQUIRE(DNS_VIEW_VALID(view));
6847 REQUIRE(ntasks > 0);
6848 REQUIRE(resp != NULL && *resp == NULL);
6849 REQUIRE(dispatchmgr != NULL);
6850 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
6852 res = isc_mem_get(view->mctx, sizeof(*res));
6854 return (ISC_R_NOMEMORY);
6856 res->mctx = view->mctx;
6857 res->rdclass = view->rdclass;
6858 res->socketmgr = socketmgr;
6859 res->timermgr = timermgr;
6860 res->taskmgr = taskmgr;
6861 res->dispatchmgr = dispatchmgr;
6863 res->options = options;
6865 ISC_LIST_INIT(res->alternates);
6866 res->udpsize = RECV_BUFFER_SIZE;
6867 res->algorithms = NULL;
6868 res->badcache = NULL;
6872 res->mustbesecure = NULL;
6873 res->spillatmin = res->spillat = 10;
6874 res->spillatmax = 100;
6875 res->spillattimer = NULL;
6876 res->zero_no_soa_ttl = ISC_FALSE;
6878 res->nextdisp = 0; /* meaningless at this point, but init it */
6879 res->nbuckets = ntasks;
6880 res->activebuckets = ntasks;
6881 res->buckets = isc_mem_get(view->mctx,
6882 ntasks * sizeof(fctxbucket_t));
6883 if (res->buckets == NULL) {
6884 result = ISC_R_NOMEMORY;
6887 for (i = 0; i < ntasks; i++) {
6888 result = isc_mutex_init(&res->buckets[i].lock);
6889 if (result != ISC_R_SUCCESS)
6890 goto cleanup_buckets;
6891 res->buckets[i].task = NULL;
6892 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
6893 if (result != ISC_R_SUCCESS) {
6894 DESTROYLOCK(&res->buckets[i].lock);
6895 goto cleanup_buckets;
6897 res->buckets[i].mctx = NULL;
6898 snprintf(name, sizeof(name), "res%u", i);
6899 #ifdef ISC_PLATFORM_USETHREADS
6901 * Use a separate memory context for each bucket to reduce
6902 * contention among multiple threads. Do this only when
6903 * enabling threads because it will be require more memory.
6905 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
6906 if (result != ISC_R_SUCCESS) {
6907 isc_task_detach(&res->buckets[i].task);
6908 DESTROYLOCK(&res->buckets[i].lock);
6909 goto cleanup_buckets;
6911 isc_mem_setname(res->buckets[i].mctx, name, NULL);
6913 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
6915 isc_task_setname(res->buckets[i].task, name, res);
6916 ISC_LIST_INIT(res->buckets[i].fctxs);
6917 res->buckets[i].exiting = ISC_FALSE;
6921 res->dispatchv4 = NULL;
6922 if (dispatchv4 != NULL) {
6923 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
6924 dispattr = dns_dispatch_getattributes(dispatchv4);
6926 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
6929 res->dispatchv6 = NULL;
6930 if (dispatchv6 != NULL) {
6931 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
6932 dispattr = dns_dispatch_getattributes(dispatchv6);
6934 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
6937 res->references = 1;
6938 res->exiting = ISC_FALSE;
6939 res->frozen = ISC_FALSE;
6940 ISC_LIST_INIT(res->whenshutdown);
6941 res->priming = ISC_FALSE;
6942 res->primefetch = NULL;
6945 result = isc_mutex_init(&res->lock);
6946 if (result != ISC_R_SUCCESS)
6947 goto cleanup_dispatches;
6949 result = isc_mutex_init(&res->nlock);
6950 if (result != ISC_R_SUCCESS)
6953 result = isc_mutex_init(&res->primelock);
6954 if (result != ISC_R_SUCCESS)
6958 result = isc_task_create(taskmgr, 0, &task);
6959 if (result != ISC_R_SUCCESS)
6960 goto cleanup_primelock;
6962 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
6963 task, spillattimer_countdown, res,
6964 &res->spillattimer);
6965 isc_task_detach(&task);
6966 if (result != ISC_R_SUCCESS)
6967 goto cleanup_primelock;
6970 result = isc_rwlock_init(&res->alglock, 0, 0);
6971 if (result != ISC_R_SUCCESS)
6972 goto cleanup_spillattimer;
6975 result = isc_rwlock_init(&res->mbslock, 0, 0);
6976 if (result != ISC_R_SUCCESS)
6977 goto cleanup_alglock;
6980 res->magic = RES_MAGIC;
6984 return (ISC_R_SUCCESS);
6989 isc_rwlock_destroy(&res->alglock);
6992 #if USE_ALGLOCK || USE_MBSLOCK
6993 cleanup_spillattimer:
6994 isc_timer_detach(&res->spillattimer);
6998 DESTROYLOCK(&res->primelock);
7001 DESTROYLOCK(&res->nlock);
7004 DESTROYLOCK(&res->lock);
7007 if (res->dispatchv6 != NULL)
7008 dns_dispatch_detach(&res->dispatchv6);
7009 if (res->dispatchv4 != NULL)
7010 dns_dispatch_detach(&res->dispatchv4);
7013 for (i = 0; i < buckets_created; i++) {
7014 isc_mem_detach(&res->buckets[i].mctx);
7015 DESTROYLOCK(&res->buckets[i].lock);
7016 isc_task_shutdown(res->buckets[i].task);
7017 isc_task_detach(&res->buckets[i].task);
7019 isc_mem_put(view->mctx, res->buckets,
7020 res->nbuckets * sizeof(fctxbucket_t));
7023 isc_mem_put(view->mctx, res, sizeof(*res));
7029 prime_done(isc_task_t *task, isc_event_t *event) {
7030 dns_resolver_t *res;
7031 dns_fetchevent_t *fevent;
7033 dns_db_t *db = NULL;
7035 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7036 fevent = (dns_fetchevent_t *)event;
7037 res = event->ev_arg;
7038 REQUIRE(VALID_RESOLVER(res));
7044 INSIST(res->priming);
7045 res->priming = ISC_FALSE;
7046 LOCK(&res->primelock);
7047 fetch = res->primefetch;
7048 res->primefetch = NULL;
7049 UNLOCK(&res->primelock);
7053 if (fevent->result == ISC_R_SUCCESS &&
7054 res->view->cache != NULL && res->view->hints != NULL) {
7055 dns_cache_attachdb(res->view->cache, &db);
7056 dns_root_checkhints(res->view, res->view->hints, db);
7060 if (fevent->node != NULL)
7061 dns_db_detachnode(fevent->db, &fevent->node);
7062 if (fevent->db != NULL)
7063 dns_db_detach(&fevent->db);
7064 if (dns_rdataset_isassociated(fevent->rdataset))
7065 dns_rdataset_disassociate(fevent->rdataset);
7066 INSIST(fevent->sigrdataset == NULL);
7068 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7070 isc_event_free(&event);
7071 dns_resolver_destroyfetch(&fetch);
7075 dns_resolver_prime(dns_resolver_t *res) {
7076 isc_boolean_t want_priming = ISC_FALSE;
7077 dns_rdataset_t *rdataset;
7078 isc_result_t result;
7080 REQUIRE(VALID_RESOLVER(res));
7081 REQUIRE(res->frozen);
7083 RTRACE("dns_resolver_prime");
7087 if (!res->exiting && !res->priming) {
7088 INSIST(res->primefetch == NULL);
7089 res->priming = ISC_TRUE;
7090 want_priming = ISC_TRUE;
7097 * To avoid any possible recursive locking problems, we
7098 * start the priming fetch like any other fetch, and holding
7099 * no resolver locks. No one else will try to start it
7100 * because we're the ones who set res->priming to true.
7101 * Any other callers of dns_resolver_prime() while we're
7102 * running will see that res->priming is already true and
7106 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7107 if (rdataset == NULL) {
7109 INSIST(res->priming);
7110 INSIST(res->primefetch == NULL);
7111 res->priming = ISC_FALSE;
7115 dns_rdataset_init(rdataset);
7116 LOCK(&res->primelock);
7117 result = dns_resolver_createfetch(res, dns_rootname,
7119 NULL, NULL, NULL, 0,
7120 res->buckets[0].task,
7122 res, rdataset, NULL,
7124 UNLOCK(&res->primelock);
7125 if (result != ISC_R_SUCCESS) {
7127 INSIST(res->priming);
7128 res->priming = ISC_FALSE;
7135 dns_resolver_freeze(dns_resolver_t *res) {
7141 REQUIRE(VALID_RESOLVER(res));
7142 REQUIRE(!res->frozen);
7144 res->frozen = ISC_TRUE;
7148 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7149 REQUIRE(VALID_RESOLVER(source));
7150 REQUIRE(targetp != NULL && *targetp == NULL);
7152 RRTRACE(source, "attach");
7153 LOCK(&source->lock);
7154 REQUIRE(!source->exiting);
7156 INSIST(source->references > 0);
7157 source->references++;
7158 INSIST(source->references != 0);
7159 UNLOCK(&source->lock);
7165 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7166 isc_event_t **eventp)
7171 REQUIRE(VALID_RESOLVER(res));
7172 REQUIRE(eventp != NULL);
7179 if (res->exiting && res->activebuckets == 0) {
7181 * We're already shutdown. Send the event.
7183 event->ev_sender = res;
7184 isc_task_send(task, &event);
7187 isc_task_attach(task, &clone);
7188 event->ev_sender = clone;
7189 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7196 dns_resolver_shutdown(dns_resolver_t *res) {
7200 isc_result_t result;
7202 REQUIRE(VALID_RESOLVER(res));
7208 if (!res->exiting) {
7210 res->exiting = ISC_TRUE;
7212 for (i = 0; i < res->nbuckets; i++) {
7213 LOCK(&res->buckets[i].lock);
7214 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7216 fctx = ISC_LIST_NEXT(fctx, link))
7217 fctx_shutdown(fctx);
7218 if (res->dispatchv4 != NULL && !res->exclusivev4) {
7219 sock = dns_dispatch_getsocket(res->dispatchv4);
7220 isc_socket_cancel(sock, res->buckets[i].task,
7221 ISC_SOCKCANCEL_ALL);
7223 if (res->dispatchv6 != NULL && !res->exclusivev6) {
7224 sock = dns_dispatch_getsocket(res->dispatchv6);
7225 isc_socket_cancel(sock, res->buckets[i].task,
7226 ISC_SOCKCANCEL_ALL);
7228 res->buckets[i].exiting = ISC_TRUE;
7229 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7230 INSIST(res->activebuckets > 0);
7231 res->activebuckets--;
7233 UNLOCK(&res->buckets[i].lock);
7235 if (res->activebuckets == 0)
7236 send_shutdown_events(res);
7237 result = isc_timer_reset(res->spillattimer,
7238 isc_timertype_inactive, NULL,
7240 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7247 dns_resolver_detach(dns_resolver_t **resp) {
7248 dns_resolver_t *res;
7249 isc_boolean_t need_destroy = ISC_FALSE;
7251 REQUIRE(resp != NULL);
7253 REQUIRE(VALID_RESOLVER(res));
7259 INSIST(res->references > 0);
7261 if (res->references == 0) {
7262 INSIST(res->exiting && res->activebuckets == 0);
7263 need_destroy = ISC_TRUE;
7274 static inline isc_boolean_t
7275 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7276 unsigned int options)
7278 if (fctx->type != type || fctx->options != options)
7280 return (dns_name_equal(&fctx->name, name));
7284 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7285 char namebuf[DNS_NAME_FORMATSIZE];
7286 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7287 int level = ISC_LOG_DEBUG(1);
7289 if (! isc_log_wouldlog(dns_lctx, level))
7292 dns_name_format(name, namebuf, sizeof(namebuf));
7293 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7295 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7296 DNS_LOGMODULE_RESOLVER, level,
7297 "createfetch: %s %s", namebuf, typebuf);
7301 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7302 dns_rdatatype_t type,
7303 dns_name_t *domain, dns_rdataset_t *nameservers,
7304 dns_forwarders_t *forwarders,
7305 unsigned int options, isc_task_t *task,
7306 isc_taskaction_t action, void *arg,
7307 dns_rdataset_t *rdataset,
7308 dns_rdataset_t *sigrdataset,
7309 dns_fetch_t **fetchp)
7311 return (dns_resolver_createfetch2(res, name, type, domain,
7312 nameservers, forwarders, NULL, 0,
7313 options, task, action, arg,
7314 rdataset, sigrdataset, fetchp));
7318 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7319 dns_rdatatype_t type,
7320 dns_name_t *domain, dns_rdataset_t *nameservers,
7321 dns_forwarders_t *forwarders,
7322 isc_sockaddr_t *client, dns_messageid_t id,
7323 unsigned int options, isc_task_t *task,
7324 isc_taskaction_t action, void *arg,
7325 dns_rdataset_t *rdataset,
7326 dns_rdataset_t *sigrdataset,
7327 dns_fetch_t **fetchp)
7330 fetchctx_t *fctx = NULL;
7331 isc_result_t result = ISC_R_SUCCESS;
7332 unsigned int bucketnum;
7333 isc_boolean_t new_fctx = ISC_FALSE;
7335 unsigned int count = 0;
7336 unsigned int spillat;
7337 unsigned int spillatmin;
7341 REQUIRE(VALID_RESOLVER(res));
7342 REQUIRE(res->frozen);
7343 /* XXXRTH Check for meta type */
7344 if (domain != NULL) {
7345 REQUIRE(DNS_RDATASET_VALID(nameservers));
7346 REQUIRE(nameservers->type == dns_rdatatype_ns);
7348 REQUIRE(nameservers == NULL);
7349 REQUIRE(forwarders == NULL);
7350 REQUIRE(!dns_rdataset_isassociated(rdataset));
7351 REQUIRE(sigrdataset == NULL ||
7352 !dns_rdataset_isassociated(sigrdataset));
7353 REQUIRE(fetchp != NULL && *fetchp == NULL);
7355 log_fetch(name, type);
7358 * XXXRTH use a mempool?
7360 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7362 return (ISC_R_NOMEMORY);
7364 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7367 spillat = res->spillat;
7368 spillatmin = res->spillatmin;
7370 LOCK(&res->buckets[bucketnum].lock);
7372 if (res->buckets[bucketnum].exiting) {
7373 result = ISC_R_SHUTTINGDOWN;
7377 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7378 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
7380 fctx = ISC_LIST_NEXT(fctx, link)) {
7381 if (fctx_match(fctx, name, type, options))
7387 * Is this a duplicate?
7389 if (fctx != NULL && client != NULL) {
7390 dns_fetchevent_t *fevent;
7391 for (fevent = ISC_LIST_HEAD(fctx->events);
7393 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
7394 if (fevent->client != NULL && fevent->id == id &&
7395 isc_sockaddr_equal(fevent->client, client)) {
7396 result = DNS_R_DUPLICATE;
7402 if (count >= spillatmin && spillatmin != 0) {
7403 INSIST(fctx != NULL);
7404 if (count >= spillat)
7405 fctx->spilled = ISC_TRUE;
7406 if (fctx->spilled) {
7407 result = DNS_R_DROP;
7413 * If we didn't have a fetch, would attach to a done fetch, this
7414 * fetch has already cloned its results, or if the fetch has gone
7415 * "idle" (no one was interested in it), we need to start a new
7416 * fetch instead of joining with the existing one.
7419 fctx->state == fetchstate_done ||
7421 ISC_LIST_EMPTY(fctx->events)) {
7423 result = fctx_create(res, name, type, domain, nameservers,
7424 options, bucketnum, &fctx);
7425 if (result != ISC_R_SUCCESS)
7427 new_fctx = ISC_TRUE;
7430 result = fctx_join(fctx, task, client, id, action, arg,
7431 rdataset, sigrdataset, fetch);
7433 if (result == ISC_R_SUCCESS) {
7437 event = &fctx->control_event;
7438 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
7439 DNS_EVENT_FETCHCONTROL,
7440 fctx_start, fctx, NULL,
7442 isc_task_send(res->buckets[bucketnum].task, &event);
7445 * We don't care about the result of fctx_destroy()
7446 * since we know we're not exiting.
7448 (void)fctx_destroy(fctx);
7453 UNLOCK(&res->buckets[bucketnum].lock);
7455 if (result == ISC_R_SUCCESS) {
7459 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7465 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7467 dns_resolver_t *res;
7468 dns_fetchevent_t *event, *next_event;
7471 REQUIRE(DNS_FETCH_VALID(fetch));
7472 fctx = fetch->private;
7473 REQUIRE(VALID_FCTX(fctx));
7476 FTRACE("cancelfetch");
7478 LOCK(&res->buckets[fctx->bucketnum].lock);
7481 * Find the completion event for this fetch (as opposed
7482 * to those for other fetches that have joined the same
7483 * fctx) and send it with result = ISC_R_CANCELED.
7486 if (fctx->state != fetchstate_done) {
7487 for (event = ISC_LIST_HEAD(fctx->events);
7489 event = next_event) {
7490 next_event = ISC_LIST_NEXT(event, ev_link);
7491 if (event->fetch == fetch) {
7492 ISC_LIST_UNLINK(fctx->events, event, ev_link);
7497 if (event != NULL) {
7498 etask = event->ev_sender;
7499 event->ev_sender = fctx;
7500 event->result = ISC_R_CANCELED;
7501 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
7504 * The fctx continues running even if no fetches remain;
7505 * the answer is still cached.
7508 UNLOCK(&res->buckets[fctx->bucketnum].lock);
7512 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
7514 dns_resolver_t *res;
7515 dns_fetchevent_t *event, *next_event;
7517 unsigned int bucketnum;
7518 isc_boolean_t bucket_empty = ISC_FALSE;
7520 REQUIRE(fetchp != NULL);
7522 REQUIRE(DNS_FETCH_VALID(fetch));
7523 fctx = fetch->private;
7524 REQUIRE(VALID_FCTX(fctx));
7527 FTRACE("destroyfetch");
7529 bucketnum = fctx->bucketnum;
7530 LOCK(&res->buckets[bucketnum].lock);
7533 * Sanity check: the caller should have gotten its event before
7534 * trying to destroy the fetch.
7537 if (fctx->state != fetchstate_done) {
7538 for (event = ISC_LIST_HEAD(fctx->events);
7540 event = next_event) {
7541 next_event = ISC_LIST_NEXT(event, ev_link);
7542 RUNTIME_CHECK(event->fetch != fetch);
7546 INSIST(fctx->references > 0);
7548 if (fctx->references == 0) {
7550 * No one cares about the result of this fetch anymore.
7552 if (fctx->pending == 0 && fctx->nqueries == 0 &&
7553 ISC_LIST_EMPTY(fctx->validators) &&
7554 SHUTTINGDOWN(fctx)) {
7556 * This fctx is already shutdown; we were just
7557 * waiting for the last reference to go away.
7559 bucket_empty = fctx_destroy(fctx);
7562 * Initiate shutdown.
7564 fctx_shutdown(fctx);
7568 UNLOCK(&res->buckets[bucketnum].lock);
7570 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7578 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
7579 isc_logcategory_t *category, isc_logmodule_t *module,
7580 int level, isc_boolean_t duplicateok)
7583 dns_resolver_t *res;
7584 char domainbuf[DNS_NAME_FORMATSIZE];
7586 REQUIRE(DNS_FETCH_VALID(fetch));
7587 fctx = fetch->private;
7588 REQUIRE(VALID_FCTX(fctx));
7591 LOCK(&res->buckets[fctx->bucketnum].lock);
7593 INSIST(fctx->exitline >= 0);
7594 if (!fctx->logged || duplicateok) {
7595 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7596 isc_log_write(lctx, category, module, level,
7597 "fetch completed at %s:%d for %s in "
7598 "%" ISC_PRINT_QUADFORMAT "u."
7599 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
7600 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
7601 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
7602 "adberr:%u,findfail:%u,valfail:%u]",
7603 __FILE__, fctx->exitline, fctx->info,
7604 fctx->duration / 1000000,
7605 fctx->duration % 1000000,
7606 isc_result_totext(fctx->result),
7607 isc_result_totext(fctx->vresult), domainbuf,
7608 fctx->referrals, fctx->restarts,
7609 fctx->querysent, fctx->timeouts, fctx->lamecount,
7610 fctx->neterr, fctx->badresp, fctx->adberr,
7611 fctx->findfail, fctx->valfail);
7612 fctx->logged = ISC_TRUE;
7615 UNLOCK(&res->buckets[fctx->bucketnum].lock);
7619 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
7620 REQUIRE(VALID_RESOLVER(resolver));
7621 return (resolver->dispatchmgr);
7625 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
7626 REQUIRE(VALID_RESOLVER(resolver));
7627 return (resolver->dispatchv4);
7631 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
7632 REQUIRE(VALID_RESOLVER(resolver));
7633 return (resolver->dispatchv6);
7637 dns_resolver_socketmgr(dns_resolver_t *resolver) {
7638 REQUIRE(VALID_RESOLVER(resolver));
7639 return (resolver->socketmgr);
7643 dns_resolver_taskmgr(dns_resolver_t *resolver) {
7644 REQUIRE(VALID_RESOLVER(resolver));
7645 return (resolver->taskmgr);
7649 dns_resolver_getlamettl(dns_resolver_t *resolver) {
7650 REQUIRE(VALID_RESOLVER(resolver));
7651 return (resolver->lame_ttl);
7655 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
7656 REQUIRE(VALID_RESOLVER(resolver));
7657 resolver->lame_ttl = lame_ttl;
7661 dns_resolver_nrunning(dns_resolver_t *resolver) {
7663 LOCK(&resolver->nlock);
7664 n = resolver->nfctx;
7665 UNLOCK(&resolver->nlock);
7670 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
7671 dns_name_t *name, in_port_t port) {
7673 isc_result_t result;
7675 REQUIRE(VALID_RESOLVER(resolver));
7676 REQUIRE(!resolver->frozen);
7677 REQUIRE((alt == NULL) ^ (name == NULL));
7679 a = isc_mem_get(resolver->mctx, sizeof(*a));
7681 return (ISC_R_NOMEMORY);
7683 a->isaddress = ISC_TRUE;
7686 a->isaddress = ISC_FALSE;
7687 a->_u._n.port = port;
7688 dns_name_init(&a->_u._n.name, NULL);
7689 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
7690 if (result != ISC_R_SUCCESS) {
7691 isc_mem_put(resolver->mctx, a, sizeof(*a));
7695 ISC_LINK_INIT(a, link);
7696 ISC_LIST_APPEND(resolver->alternates, a, link);
7698 return (ISC_R_SUCCESS);
7702 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
7703 REQUIRE(VALID_RESOLVER(resolver));
7704 resolver->udpsize = udpsize;
7708 dns_resolver_getudpsize(dns_resolver_t *resolver) {
7709 REQUIRE(VALID_RESOLVER(resolver));
7710 return (resolver->udpsize);
7714 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
7716 dns_badcache_t *bad, *prev, *next;
7718 REQUIRE(VALID_RESOLVER(resolver));
7720 LOCK(&resolver->lock);
7721 if (resolver->badcache == NULL)
7726 isc_result_t result;
7727 result = isc_time_now(&now);
7728 if (result != ISC_R_SUCCESS)
7729 isc_time_settoepoch(&now);
7730 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
7732 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7735 n = isc_time_compare(&bad->expire, &now);
7736 if (n < 0 || dns_name_equal(name, &bad->name)) {
7738 resolver->badcache[i] = bad->next;
7740 prev->next = bad->next;
7741 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7743 resolver->badcount--;
7748 destroy_badcache(resolver);
7751 UNLOCK(&resolver->lock);
7756 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
7757 unsigned int newsize;
7758 dns_badcache_t **new, *bad, *next;
7762 newsize = resolver->badhash * 2 + 1;
7764 newsize = (resolver->badhash - 1) / 2;
7766 new = isc_mem_get(resolver->mctx,
7767 sizeof(*resolver->badcache) * newsize);
7770 memset(new, 0, sizeof(*resolver->badcache) * newsize);
7771 for (i = 0; i < resolver->badhash; i++) {
7772 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7774 if (isc_time_compare(&bad->expire, now) < 0) {
7775 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7777 resolver->badcount--;
7779 bad->next = new[bad->hashval % newsize];
7780 new[bad->hashval % newsize] = bad;
7784 isc_mem_put(resolver->mctx, resolver->badcache,
7785 sizeof(*resolver->badcache) * resolver->badhash);
7786 resolver->badhash = newsize;
7787 resolver->badcache = new;
7791 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
7792 dns_rdatatype_t type, isc_time_t *expire)
7795 isc_result_t result = ISC_R_SUCCESS;
7796 unsigned int i, hashval;
7797 dns_badcache_t *bad, *prev, *next;
7799 REQUIRE(VALID_RESOLVER(resolver));
7801 LOCK(&resolver->lock);
7802 if (resolver->badcache == NULL) {
7803 resolver->badcache = isc_mem_get(resolver->mctx,
7804 sizeof(*resolver->badcache) *
7806 if (resolver->badcache == NULL) {
7807 result = ISC_R_NOMEMORY;
7810 resolver->badhash = DNS_BADCACHE_SIZE;
7811 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
7815 result = isc_time_now(&now);
7816 if (result != ISC_R_SUCCESS)
7817 isc_time_settoepoch(&now);
7818 hashval = dns_name_hash(name, ISC_FALSE);
7819 i = hashval % resolver->badhash;
7821 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7823 if (bad->type == type && dns_name_equal(name, &bad->name))
7825 if (isc_time_compare(&bad->expire, &now) < 0) {
7827 resolver->badcache[i] = bad->next;
7829 prev->next = bad->next;
7830 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7832 resolver->badcount--;
7837 isc_buffer_t buffer;
7838 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
7840 result = ISC_R_NOMEMORY;
7844 bad->hashval = hashval;
7845 isc_buffer_init(&buffer, bad + 1, name->length);
7846 dns_name_init(&bad->name, NULL);
7847 dns_name_copy(name, &bad->name, &buffer);
7848 bad->next = resolver->badcache[i];
7849 resolver->badcache[i] = bad;
7850 resolver->badcount++;
7851 if (resolver->badcount > resolver->badhash * 8)
7852 resizehash(resolver, &now, ISC_TRUE);
7853 if (resolver->badcount < resolver->badhash * 2 &&
7854 resolver->badhash > DNS_BADCACHE_SIZE)
7855 resizehash(resolver, &now, ISC_FALSE);
7857 bad->expire = *expire;
7859 UNLOCK(&resolver->lock);
7863 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
7864 dns_rdatatype_t type, isc_time_t *now)
7866 dns_badcache_t *bad, *prev, *next;
7867 isc_boolean_t answer = ISC_FALSE;
7870 REQUIRE(VALID_RESOLVER(resolver));
7872 LOCK(&resolver->lock);
7873 if (resolver->badcache == NULL)
7876 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
7878 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7881 * Search the hash list. Clean out expired records as we go.
7883 if (isc_time_compare(&bad->expire, now) < 0) {
7885 prev->next = bad->next;
7887 resolver->badcache[i] = bad->next;
7888 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7890 resolver->badcount--;
7893 if (bad->type == type && dns_name_equal(name, &bad->name)) {
7901 * Slow sweep to clean out stale records.
7903 i = resolver->badsweep++ % resolver->badhash;
7904 bad = resolver->badcache[i];
7905 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
7906 resolver->badcache[i] = bad->next;
7907 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7909 resolver->badcount--;
7913 UNLOCK(&resolver->lock);
7918 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
7919 char namebuf[DNS_NAME_FORMATSIZE];
7920 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7921 dns_badcache_t *bad, *next, *prev;
7926 LOCK(&resolver->lock);
7927 fprintf(fp, ";\n; Bad cache\n;\n");
7929 if (resolver->badcache == NULL)
7933 for (i = 0; i < resolver->badhash; i++) {
7935 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7937 if (isc_time_compare(&bad->expire, &now) < 0) {
7939 prev->next = bad->next;
7941 resolver->badcache[i] = bad->next;
7942 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7944 resolver->badcount--;
7948 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
7949 dns_rdatatype_format(bad->type, typebuf,
7951 t = isc_time_microdiff(&bad->expire, &now);
7953 fprintf(fp, "; %s/%s [ttl "
7954 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
7955 namebuf, typebuf, t);
7960 UNLOCK(&resolver->lock);
7964 free_algorithm(void *node, void *arg) {
7965 unsigned char *algorithms = node;
7966 isc_mem_t *mctx = arg;
7968 isc_mem_put(mctx, algorithms, *algorithms);
7972 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
7974 REQUIRE(VALID_RESOLVER(resolver));
7977 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
7979 if (resolver->algorithms != NULL)
7980 dns_rbt_destroy(&resolver->algorithms);
7982 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
7987 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
7990 unsigned int len, mask;
7992 unsigned char *algorithms;
7993 isc_result_t result;
7994 dns_rbtnode_t *node = NULL;
7996 REQUIRE(VALID_RESOLVER(resolver));
7998 return (ISC_R_RANGE);
8001 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8003 if (resolver->algorithms == NULL) {
8004 result = dns_rbt_create(resolver->mctx, free_algorithm,
8005 resolver->mctx, &resolver->algorithms);
8006 if (result != ISC_R_SUCCESS)
8011 mask = 1 << (alg%8);
8013 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8015 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8016 algorithms = node->data;
8017 if (algorithms == NULL || len > *algorithms) {
8018 new = isc_mem_get(resolver->mctx, len);
8020 result = ISC_R_NOMEMORY;
8023 memset(new, 0, len);
8024 if (algorithms != NULL)
8025 memcpy(new, algorithms, *algorithms);
8029 if (algorithms != NULL)
8030 isc_mem_put(resolver->mctx, algorithms,
8033 algorithms[len-1] |= mask;
8035 result = ISC_R_SUCCESS;
8038 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8044 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8047 unsigned int len, mask;
8048 unsigned char *algorithms;
8050 isc_result_t result;
8051 isc_boolean_t found = ISC_FALSE;
8053 REQUIRE(VALID_RESOLVER(resolver));
8056 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8058 if (resolver->algorithms == NULL)
8060 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8061 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8063 mask = 1 << (alg%8);
8065 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8070 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8074 return (dst_algorithm_supported(alg));
8078 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8081 return (dns_ds_digest_supported(digest));
8085 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8087 REQUIRE(VALID_RESOLVER(resolver));
8090 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8092 if (resolver->mustbesecure != NULL)
8093 dns_rbt_destroy(&resolver->mustbesecure);
8095 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8099 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8102 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8103 isc_boolean_t value)
8105 isc_result_t result;
8107 REQUIRE(VALID_RESOLVER(resolver));
8110 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8112 if (resolver->mustbesecure == NULL) {
8113 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8114 &resolver->mustbesecure);
8115 if (result != ISC_R_SUCCESS)
8118 result = dns_rbt_addname(resolver->mustbesecure, name,
8119 value ? &yes : &no);
8122 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8128 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8130 isc_boolean_t value = ISC_FALSE;
8131 isc_result_t result;
8133 REQUIRE(VALID_RESOLVER(resolver));
8136 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8138 if (resolver->mustbesecure == NULL)
8140 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8141 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8142 value = *(isc_boolean_t*)data;
8145 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8151 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8152 isc_uint32_t *min, isc_uint32_t *max)
8154 REQUIRE(VALID_RESOLVER(resolver));
8156 LOCK(&resolver->lock);
8158 *cur = resolver->spillat;
8160 *min = resolver->spillatmin;
8162 *max = resolver->spillatmax;
8163 UNLOCK(&resolver->lock);
8167 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
8170 REQUIRE(VALID_RESOLVER(resolver));
8172 LOCK(&resolver->lock);
8173 resolver->spillatmin = resolver->spillat = min;
8174 resolver->spillatmax = max;
8175 UNLOCK(&resolver->lock);
8179 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
8180 REQUIRE(VALID_RESOLVER(resolver));
8182 return (resolver->zero_no_soa_ttl);
8186 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
8187 REQUIRE(VALID_RESOLVER(resolver));
8189 resolver->zero_no_soa_ttl = state;
8193 dns_resolver_getoptions(dns_resolver_t *resolver) {
8194 REQUIRE(VALID_RESOLVER(resolver));
8196 return (resolver->options);