resolver.c revision 292321
1/* 2 * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 1999-2003 Internet Software Consortium. 4 * 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. 8 * 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. 16 */ 17 18/* $Id$ */ 19 20/*! \file */ 21 22#include <config.h> 23 24#include <isc/log.h> 25#include <isc/platform.h> 26#include <isc/print.h> 27#include <isc/string.h> 28#include <isc/random.h> 29#include <isc/socket.h> 30#include <isc/stats.h> 31#include <isc/task.h> 32#include <isc/timer.h> 33#include <isc/util.h> 34 35#include <dns/acl.h> 36#include <dns/adb.h> 37#include <dns/cache.h> 38#include <dns/db.h> 39#include <dns/dispatch.h> 40#include <dns/ds.h> 41#include <dns/events.h> 42#include <dns/forward.h> 43#include <dns/keytable.h> 44#include <dns/log.h> 45#include <dns/message.h> 46#include <dns/ncache.h> 47#include <dns/nsec.h> 48#include <dns/nsec3.h> 49#include <dns/opcode.h> 50#include <dns/peer.h> 51#include <dns/rbt.h> 52#include <dns/rcode.h> 53#include <dns/rdata.h> 54#include <dns/rdataclass.h> 55#include <dns/rdatalist.h> 56#include <dns/rdataset.h> 57#include <dns/rdatastruct.h> 58#include <dns/rdatatype.h> 59#include <dns/resolver.h> 60#include <dns/result.h> 61#include <dns/rootns.h> 62#include <dns/stats.h> 63#include <dns/tsig.h> 64#include <dns/validator.h> 65 66#define DNS_RESOLVER_TRACE 67#ifdef DNS_RESOLVER_TRACE 68#define RTRACE(m) isc_log_write(dns_lctx, \ 69 DNS_LOGCATEGORY_RESOLVER, \ 70 DNS_LOGMODULE_RESOLVER, \ 71 ISC_LOG_DEBUG(3), \ 72 "res %p: %s", res, (m)) 73#define RRTRACE(r, m) isc_log_write(dns_lctx, \ 74 DNS_LOGCATEGORY_RESOLVER, \ 75 DNS_LOGMODULE_RESOLVER, \ 76 ISC_LOG_DEBUG(3), \ 77 "res %p: %s", (r), (m)) 78#define FCTXTRACE(m) isc_log_write(dns_lctx, \ 79 DNS_LOGCATEGORY_RESOLVER, \ 80 DNS_LOGMODULE_RESOLVER, \ 81 ISC_LOG_DEBUG(3), \ 82 "fctx %p(%s): %s", fctx, fctx->info, (m)) 83#define FCTXTRACE2(m1, m2) \ 84 isc_log_write(dns_lctx, \ 85 DNS_LOGCATEGORY_RESOLVER, \ 86 DNS_LOGMODULE_RESOLVER, \ 87 ISC_LOG_DEBUG(3), \ 88 "fctx %p(%s): %s %s", \ 89 fctx, fctx->info, (m1), (m2)) 90#define FTRACE(m) isc_log_write(dns_lctx, \ 91 DNS_LOGCATEGORY_RESOLVER, \ 92 DNS_LOGMODULE_RESOLVER, \ 93 ISC_LOG_DEBUG(3), \ 94 "fetch %p (fctx %p(%s)): %s", \ 95 fetch, fetch->private, \ 96 fetch->private->info, (m)) 97#define QTRACE(m) isc_log_write(dns_lctx, \ 98 DNS_LOGCATEGORY_RESOLVER, \ 99 DNS_LOGMODULE_RESOLVER, \ 100 ISC_LOG_DEBUG(3), \ 101 "resquery %p (fctx %p(%s)): %s", \ 102 query, query->fctx, \ 103 query->fctx->info, (m)) 104#else 105#define RTRACE(m) 106#define RRTRACE(r, m) 107#define FCTXTRACE(m) 108#define FCTXTRACE2(m1, m2) 109#define FTRACE(m) 110#define QTRACE(m) 111#endif 112 113#define US_PER_SEC 1000000U 114/* 115 * The maximum time we will wait for a single query. 116 */ 117#define MAX_SINGLE_QUERY_TIMEOUT 9U 118#define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_SEC) 119 120/* 121 * We need to allow a individual query time to complete / timeout. 122 */ 123#define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1U) 124 125/* The default time in seconds for the whole query to live. */ 126#ifndef DEFAULT_QUERY_TIMEOUT 127#define DEFAULT_QUERY_TIMEOUT MINIMUM_QUERY_TIMEOUT 128#endif 129 130#ifndef MAXIMUM_QUERY_TIMEOUT 131#define MAXIMUM_QUERY_TIMEOUT 30 /* The maximum time in seconds for the whole query to live. */ 132#endif 133 134/* The default maximum number of recursions to follow before giving up. */ 135#ifndef DEFAULT_RECURSION_DEPTH 136#define DEFAULT_RECURSION_DEPTH 7 137#endif 138 139/* The default maximum number of iterative queries to allow before giving up. */ 140#ifndef DEFAULT_MAX_QUERIES 141#define DEFAULT_MAX_QUERIES 50 142#endif 143 144/*% 145 * Maximum EDNS0 input packet size. 146 */ 147#define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */ 148#define EDNSOPTS 2 149 150/*% 151 * This defines the maximum number of timeouts we will permit before we 152 * disable EDNS0 on the query. 153 */ 154#define MAX_EDNS0_TIMEOUTS 3 155 156typedef struct fetchctx fetchctx_t; 157 158typedef struct query { 159 /* Locked by task event serialization. */ 160 unsigned int magic; 161 fetchctx_t * fctx; 162 isc_mem_t * mctx; 163 dns_dispatchmgr_t * dispatchmgr; 164 dns_dispatch_t * dispatch; 165 isc_boolean_t exclusivesocket; 166 dns_adbaddrinfo_t * addrinfo; 167 isc_socket_t * tcpsocket; 168 isc_time_t start; 169 dns_messageid_t id; 170 dns_dispentry_t * dispentry; 171 ISC_LINK(struct query) link; 172 isc_buffer_t buffer; 173 isc_buffer_t *tsig; 174 dns_tsigkey_t *tsigkey; 175 isc_socketevent_t sendevent; 176 unsigned int options; 177 unsigned int attributes; 178 unsigned int sends; 179 unsigned int connects; 180 unsigned char data[512]; 181} resquery_t; 182 183#define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!') 184#define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC) 185 186#define RESQUERY_ATTR_CANCELED 0x02 187 188#define RESQUERY_CONNECTING(q) ((q)->connects > 0) 189#define RESQUERY_CANCELED(q) (((q)->attributes & \ 190 RESQUERY_ATTR_CANCELED) != 0) 191#define RESQUERY_SENDING(q) ((q)->sends > 0) 192 193typedef enum { 194 fetchstate_init = 0, /*%< Start event has not run yet. */ 195 fetchstate_active, 196 fetchstate_done /*%< FETCHDONE events posted. */ 197} fetchstate; 198 199typedef enum { 200 badns_unreachable = 0, 201 badns_response, 202 badns_validation 203} badnstype_t; 204 205struct fetchctx { 206 /*% Not locked. */ 207 unsigned int magic; 208 dns_resolver_t * res; 209 dns_name_t name; 210 dns_rdatatype_t type; 211 unsigned int options; 212 unsigned int bucketnum; 213 char * info; 214 isc_mem_t * mctx; 215 216 /*% Locked by appropriate bucket lock. */ 217 fetchstate state; 218 isc_boolean_t want_shutdown; 219 isc_boolean_t cloned; 220 isc_boolean_t spilled; 221 unsigned int references; 222 isc_event_t control_event; 223 ISC_LINK(struct fetchctx) link; 224 ISC_LIST(dns_fetchevent_t) events; 225 /*% Locked by task event serialization. */ 226 dns_name_t domain; 227 dns_rdataset_t nameservers; 228 unsigned int attributes; 229 isc_timer_t * timer; 230 isc_time_t expires; 231 isc_interval_t interval; 232 dns_message_t * qmessage; 233 dns_message_t * rmessage; 234 ISC_LIST(resquery_t) queries; 235 dns_adbfindlist_t finds; 236 dns_adbfind_t * find; 237 dns_adbfindlist_t altfinds; 238 dns_adbfind_t * altfind; 239 dns_adbaddrinfolist_t forwaddrs; 240 dns_adbaddrinfolist_t altaddrs; 241 isc_sockaddrlist_t forwarders; 242 dns_fwdpolicy_t fwdpolicy; 243 isc_sockaddrlist_t bad; 244 isc_sockaddrlist_t edns; 245 isc_sockaddrlist_t edns512; 246 isc_sockaddrlist_t bad_edns; 247 dns_validator_t *validator; 248 ISC_LIST(dns_validator_t) validators; 249 dns_db_t * cache; 250 dns_adb_t * adb; 251 isc_boolean_t ns_ttl_ok; 252 isc_uint32_t ns_ttl; 253 254 /*% 255 * The number of events we're waiting for. 256 */ 257 unsigned int pending; 258 259 /*% 260 * The number of times we've "restarted" the current 261 * nameserver set. This acts as a failsafe to prevent 262 * us from pounding constantly on a particular set of 263 * servers that, for whatever reason, are not giving 264 * us useful responses, but are responding in such a 265 * way that they are not marked "bad". 266 */ 267 unsigned int restarts; 268 269 /*% 270 * The number of timeouts that have occurred since we 271 * last successfully received a response packet. This 272 * is used for EDNS0 black hole detection. 273 */ 274 unsigned int timeouts; 275 276 /*% 277 * Look aside state for DS lookups. 278 */ 279 dns_name_t nsname; 280 dns_fetch_t * nsfetch; 281 dns_rdataset_t nsrrset; 282 283 /*% 284 * Number of queries that reference this context. 285 */ 286 unsigned int nqueries; 287 288 /*% 289 * The reason to print when logging a successful 290 * response to a query. 291 */ 292 const char * reason; 293 294 /*% 295 * Random numbers to use for mixing up server addresses. 296 */ 297 isc_uint32_t rand_buf; 298 isc_uint32_t rand_bits; 299 300 /*% 301 * Fetch-local statistics for detailed logging. 302 */ 303 isc_result_t result; /*%< fetch result */ 304 isc_result_t vresult; /*%< validation result */ 305 int exitline; 306 isc_time_t start; 307 isc_uint64_t duration; 308 isc_boolean_t logged; 309 unsigned int querysent; 310 unsigned int totalqueries; 311 unsigned int referrals; 312 unsigned int lamecount; 313 unsigned int neterr; 314 unsigned int badresp; 315 unsigned int adberr; 316 unsigned int findfail; 317 unsigned int valfail; 318 isc_boolean_t timeout; 319 dns_adbaddrinfo_t *addrinfo; 320 isc_sockaddr_t *client; 321 unsigned int depth; 322}; 323 324#define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!') 325#define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC) 326 327#define FCTX_ATTR_HAVEANSWER 0x0001 328#define FCTX_ATTR_GLUING 0x0002 329#define FCTX_ATTR_ADDRWAIT 0x0004 330#define FCTX_ATTR_SHUTTINGDOWN 0x0008 331#define FCTX_ATTR_WANTCACHE 0x0010 332#define FCTX_ATTR_WANTNCACHE 0x0020 333#define FCTX_ATTR_NEEDEDNS0 0x0040 334#define FCTX_ATTR_TRIEDFIND 0x0080 335#define FCTX_ATTR_TRIEDALT 0x0100 336 337#define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \ 338 0) 339#define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \ 340 0) 341#define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \ 342 0) 343#define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \ 344 != 0) 345#define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0) 346#define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0) 347#define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0) 348#define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0) 349#define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0) 350 351typedef struct { 352 dns_adbaddrinfo_t * addrinfo; 353 fetchctx_t * fctx; 354} dns_valarg_t; 355 356struct dns_fetch { 357 unsigned int magic; 358 fetchctx_t * private; 359}; 360 361#define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h') 362#define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC) 363 364typedef struct fctxbucket { 365 isc_task_t * task; 366 isc_mutex_t lock; 367 ISC_LIST(fetchctx_t) fctxs; 368 isc_boolean_t exiting; 369 isc_mem_t * mctx; 370} fctxbucket_t; 371 372typedef struct alternate { 373 isc_boolean_t isaddress; 374 union { 375 isc_sockaddr_t addr; 376 struct { 377 dns_name_t name; 378 in_port_t port; 379 } _n; 380 } _u; 381 ISC_LINK(struct alternate) link; 382} alternate_t; 383 384typedef struct dns_badcache dns_badcache_t; 385struct dns_badcache { 386 dns_badcache_t * next; 387 dns_rdatatype_t type; 388 isc_time_t expire; 389 unsigned int hashval; 390 dns_name_t name; 391}; 392#define DNS_BADCACHE_SIZE 1021 393#define DNS_BADCACHE_TTL(fctx) \ 394 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30) 395 396struct dns_resolver { 397 /* Unlocked. */ 398 unsigned int magic; 399 isc_mem_t * mctx; 400 isc_mutex_t lock; 401 isc_mutex_t nlock; 402 isc_mutex_t primelock; 403 dns_rdataclass_t rdclass; 404 isc_socketmgr_t * socketmgr; 405 isc_timermgr_t * timermgr; 406 isc_taskmgr_t * taskmgr; 407 dns_view_t * view; 408 isc_boolean_t frozen; 409 unsigned int options; 410 dns_dispatchmgr_t * dispatchmgr; 411 dns_dispatchset_t * dispatches4; 412 isc_boolean_t exclusivev4; 413 dns_dispatchset_t * dispatches6; 414 isc_boolean_t exclusivev6; 415 unsigned int nbuckets; 416 fctxbucket_t * buckets; 417 isc_uint32_t lame_ttl; 418 ISC_LIST(alternate_t) alternates; 419 isc_uint16_t udpsize; 420#if USE_ALGLOCK 421 isc_rwlock_t alglock; 422#endif 423 dns_rbt_t * algorithms; 424#if USE_MBSLOCK 425 isc_rwlock_t mbslock; 426#endif 427 dns_rbt_t * mustbesecure; 428 unsigned int spillatmax; 429 unsigned int spillatmin; 430 isc_timer_t * spillattimer; 431 isc_boolean_t zero_no_soa_ttl; 432 unsigned int query_timeout; 433 unsigned int maxdepth; 434 435 /* Locked by lock. */ 436 unsigned int references; 437 isc_boolean_t exiting; 438 isc_eventlist_t whenshutdown; 439 unsigned int activebuckets; 440 isc_boolean_t priming; 441 unsigned int spillat; /* clients-per-query */ 442 443 /* Bad cache. */ 444 dns_badcache_t ** badcache; 445 unsigned int badcount; 446 unsigned int badhash; 447 unsigned int badsweep; 448 449 /* Locked by primelock. */ 450 dns_fetch_t * primefetch; 451 /* Locked by nlock. */ 452 unsigned int nfctx; 453}; 454 455#define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!') 456#define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC) 457 458/*% 459 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0, 460 * which we also use as an addrinfo flag. 461 */ 462#define FCTX_ADDRINFO_MARK 0x0001 463#define FCTX_ADDRINFO_FORWARDER 0x1000 464#define FCTX_ADDRINFO_TRIED 0x2000 465#define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \ 466 == 0) 467#define ISFORWARDER(a) (((a)->flags & \ 468 FCTX_ADDRINFO_FORWARDER) != 0) 469#define TRIED(a) (((a)->flags & \ 470 FCTX_ADDRINFO_TRIED) != 0) 471 472#define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0) 473#define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) 474 475static void destroy(dns_resolver_t *res); 476static void empty_bucket(dns_resolver_t *res); 477static isc_result_t resquery_send(resquery_t *query); 478static void resquery_response(isc_task_t *task, isc_event_t *event); 479static void resquery_connected(isc_task_t *task, isc_event_t *event); 480static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, 481 isc_boolean_t badcache); 482static void fctx_destroy(fetchctx_t *fctx); 483static isc_boolean_t fctx_unlink(fetchctx_t *fctx); 484static isc_result_t ncache_adderesult(dns_message_t *message, 485 dns_db_t *cache, dns_dbnode_t *node, 486 dns_rdatatype_t covers, 487 isc_stdtime_t now, dns_ttl_t maxttl, 488 isc_boolean_t optout, 489 isc_boolean_t secure, 490 dns_rdataset_t *ardataset, 491 isc_result_t *eresultp); 492static void validated(isc_task_t *task, isc_event_t *event); 493static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked); 494static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, 495 isc_result_t reason, badnstype_t badtype); 496static inline isc_result_t findnoqname(fetchctx_t *fctx, dns_name_t *name, 497 dns_rdatatype_t type, 498 dns_name_t **noqname); 499 500/*% 501 * Increment resolver-related statistics counters. 502 */ 503static inline void 504inc_stats(dns_resolver_t *res, isc_statscounter_t counter) { 505 if (res->view->resstats != NULL) 506 isc_stats_increment(res->view->resstats, counter); 507} 508 509static isc_result_t 510valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name, 511 dns_rdatatype_t type, dns_rdataset_t *rdataset, 512 dns_rdataset_t *sigrdataset, unsigned int valoptions, 513 isc_task_t *task) 514{ 515 dns_validator_t *validator = NULL; 516 dns_valarg_t *valarg; 517 isc_result_t result; 518 519 valarg = isc_mem_get(fctx->mctx, sizeof(*valarg)); 520 if (valarg == NULL) 521 return (ISC_R_NOMEMORY); 522 523 valarg->fctx = fctx; 524 valarg->addrinfo = addrinfo; 525 526 if (!ISC_LIST_EMPTY(fctx->validators)) 527 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0); 528 529 result = dns_validator_create(fctx->res->view, name, type, rdataset, 530 sigrdataset, fctx->rmessage, 531 valoptions, task, validated, valarg, 532 &validator); 533 if (result == ISC_R_SUCCESS) { 534 inc_stats(fctx->res, dns_resstatscounter_val); 535 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) { 536 INSIST(fctx->validator == NULL); 537 fctx->validator = validator; 538 } 539 ISC_LIST_APPEND(fctx->validators, validator, link); 540 } else 541 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg)); 542 return (result); 543} 544 545static isc_boolean_t 546rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) { 547 dns_namereln_t namereln; 548 dns_rdata_rrsig_t rrsig; 549 dns_rdata_t rdata = DNS_RDATA_INIT; 550 int order; 551 isc_result_t result; 552 unsigned int labels; 553 554 for (result = dns_rdataset_first(rdataset); 555 result == ISC_R_SUCCESS; 556 result = dns_rdataset_next(rdataset)) { 557 dns_rdataset_current(rdataset, &rdata); 558 result = dns_rdata_tostruct(&rdata, &rrsig, NULL); 559 RUNTIME_CHECK(result == ISC_R_SUCCESS); 560 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain, 561 &order, &labels); 562 if (namereln == dns_namereln_subdomain) 563 return (ISC_TRUE); 564 dns_rdata_reset(&rdata); 565 } 566 return (ISC_FALSE); 567} 568 569static isc_boolean_t 570fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) { 571 dns_name_t *name; 572 dns_name_t *domain = &fctx->domain; 573 dns_rdataset_t *rdataset; 574 dns_rdatatype_t type; 575 isc_result_t result; 576 isc_boolean_t keep_auth = ISC_FALSE; 577 578 if (message->rcode == dns_rcode_nxdomain) 579 return (ISC_FALSE); 580 581 /* 582 * A DS RRset can appear anywhere in a zone, even for a delegation-only 583 * zone. So a response to an explicit query for this type should be 584 * excluded from delegation-only fixup. 585 * 586 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive 587 * response to a query for these types can never violate the 588 * delegation-only assumption: if the query name is below a 589 * zone cut, the response should normally be a referral, which should 590 * be accepted; if the query name is below a zone cut but the server 591 * happens to have authority for the zone of the query name, the 592 * response is a (non-referral) answer. But this does not violate 593 * delegation-only because the query name must be in a different zone 594 * due to the "apex-only" nature of these types. Note that if the 595 * remote server happens to have authority for a child zone of a 596 * delegation-only zone, we may still incorrectly "fix" the response 597 * with NXDOMAIN for queries for other types. Unfortunately it's 598 * generally impossible to differentiate this case from violation of 599 * the delegation-only assumption. Once the resolver learns the 600 * correct zone cut, possibly via a separate query for an "apex-only" 601 * type, queries for other types will be resolved correctly. 602 * 603 * A query for type ANY will be accepted if it hits an exceptional 604 * type above in the answer section as it should be from a child 605 * zone. 606 * 607 * Also accept answers with RRSIG records from the child zone. 608 * Direct queries for RRSIG records should not be answered from 609 * the parent zone. 610 */ 611 612 if (message->counts[DNS_SECTION_ANSWER] != 0 && 613 (fctx->type == dns_rdatatype_ns || 614 fctx->type == dns_rdatatype_ds || 615 fctx->type == dns_rdatatype_soa || 616 fctx->type == dns_rdatatype_any || 617 fctx->type == dns_rdatatype_rrsig || 618 fctx->type == dns_rdatatype_dnskey)) { 619 result = dns_message_firstname(message, DNS_SECTION_ANSWER); 620 while (result == ISC_R_SUCCESS) { 621 name = NULL; 622 dns_message_currentname(message, DNS_SECTION_ANSWER, 623 &name); 624 for (rdataset = ISC_LIST_HEAD(name->list); 625 rdataset != NULL; 626 rdataset = ISC_LIST_NEXT(rdataset, link)) { 627 if (!dns_name_equal(name, &fctx->name)) 628 continue; 629 type = rdataset->type; 630 /* 631 * RRsig from child? 632 */ 633 if (type == dns_rdatatype_rrsig && 634 rrsig_fromchildzone(fctx, rdataset)) 635 return (ISC_FALSE); 636 /* 637 * Direct query for apex records or DS. 638 */ 639 if (fctx->type == type && 640 (type == dns_rdatatype_ds || 641 type == dns_rdatatype_ns || 642 type == dns_rdatatype_soa || 643 type == dns_rdatatype_dnskey)) 644 return (ISC_FALSE); 645 /* 646 * Indirect query for apex records or DS. 647 */ 648 if (fctx->type == dns_rdatatype_any && 649 (type == dns_rdatatype_ns || 650 type == dns_rdatatype_ds || 651 type == dns_rdatatype_soa || 652 type == dns_rdatatype_dnskey)) 653 return (ISC_FALSE); 654 } 655 result = dns_message_nextname(message, 656 DNS_SECTION_ANSWER); 657 } 658 } 659 660 /* 661 * A NODATA response to a DS query? 662 */ 663 if (fctx->type == dns_rdatatype_ds && 664 message->counts[DNS_SECTION_ANSWER] == 0) 665 return (ISC_FALSE); 666 667 /* Look for referral or indication of answer from child zone? */ 668 if (message->counts[DNS_SECTION_AUTHORITY] == 0) 669 goto munge; 670 671 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY); 672 while (result == ISC_R_SUCCESS) { 673 name = NULL; 674 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name); 675 for (rdataset = ISC_LIST_HEAD(name->list); 676 rdataset != NULL; 677 rdataset = ISC_LIST_NEXT(rdataset, link)) { 678 type = rdataset->type; 679 if (type == dns_rdatatype_soa && 680 dns_name_equal(name, domain)) 681 keep_auth = ISC_TRUE; 682 683 if (type != dns_rdatatype_ns && 684 type != dns_rdatatype_soa && 685 type != dns_rdatatype_rrsig) 686 continue; 687 688 if (type == dns_rdatatype_rrsig) { 689 if (rrsig_fromchildzone(fctx, rdataset)) 690 return (ISC_FALSE); 691 else 692 continue; 693 } 694 695 /* NS or SOA records. */ 696 if (dns_name_equal(name, domain)) { 697 /* 698 * If a query for ANY causes a negative 699 * response, we can be sure that this is 700 * an empty node. For other type of queries 701 * we cannot differentiate an empty node 702 * from a node that just doesn't have that 703 * type of record. We only accept the former 704 * case. 705 */ 706 if (message->counts[DNS_SECTION_ANSWER] == 0 && 707 fctx->type == dns_rdatatype_any) 708 return (ISC_FALSE); 709 } else if (dns_name_issubdomain(name, domain)) { 710 /* Referral or answer from child zone. */ 711 return (ISC_FALSE); 712 } 713 } 714 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY); 715 } 716 717 munge: 718 message->rcode = dns_rcode_nxdomain; 719 message->counts[DNS_SECTION_ANSWER] = 0; 720 if (!keep_auth) 721 message->counts[DNS_SECTION_AUTHORITY] = 0; 722 message->counts[DNS_SECTION_ADDITIONAL] = 0; 723 return (ISC_TRUE); 724} 725 726static inline isc_result_t 727fctx_starttimer(fetchctx_t *fctx) { 728 /* 729 * Start the lifetime timer for fctx. 730 * 731 * This is also used for stopping the idle timer; in that 732 * case we must purge events already posted to ensure that 733 * no further idle events are delivered. 734 */ 735 return (isc_timer_reset(fctx->timer, isc_timertype_once, 736 &fctx->expires, NULL, ISC_TRUE)); 737} 738 739static inline void 740fctx_stoptimer(fetchctx_t *fctx) { 741 isc_result_t result; 742 743 /* 744 * We don't return a result if resetting the timer to inactive fails 745 * since there's nothing to be done about it. Resetting to inactive 746 * should never fail anyway, since the code as currently written 747 * cannot fail in that case. 748 */ 749 result = isc_timer_reset(fctx->timer, isc_timertype_inactive, 750 NULL, NULL, ISC_TRUE); 751 if (result != ISC_R_SUCCESS) { 752 UNEXPECTED_ERROR(__FILE__, __LINE__, 753 "isc_timer_reset(): %s", 754 isc_result_totext(result)); 755 } 756} 757 758 759static inline isc_result_t 760fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) { 761 /* 762 * Start the idle timer for fctx. The lifetime timer continues 763 * to be in effect. 764 */ 765 return (isc_timer_reset(fctx->timer, isc_timertype_once, 766 &fctx->expires, interval, ISC_FALSE)); 767} 768 769/* 770 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but 771 * we use fctx_stopidletimer for readability in the code below. 772 */ 773#define fctx_stopidletimer fctx_starttimer 774 775 776static inline void 777resquery_destroy(resquery_t **queryp) { 778 resquery_t *query; 779 780 REQUIRE(queryp != NULL); 781 query = *queryp; 782 REQUIRE(!ISC_LINK_LINKED(query, link)); 783 784 INSIST(query->tcpsocket == NULL); 785 786 query->fctx->nqueries--; 787 if (SHUTTINGDOWN(query->fctx)) { 788 dns_resolver_t *res = query->fctx->res; 789 if (maybe_destroy(query->fctx, ISC_FALSE)) 790 empty_bucket(res); 791 } 792 query->magic = 0; 793 isc_mem_put(query->mctx, query, sizeof(*query)); 794 *queryp = NULL; 795} 796 797static void 798fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp, 799 isc_time_t *finish, isc_boolean_t no_response) 800{ 801 fetchctx_t *fctx; 802 resquery_t *query; 803 unsigned int rtt, rttms; 804 unsigned int factor; 805 dns_adbfind_t *find; 806 dns_adbaddrinfo_t *addrinfo; 807 isc_socket_t *socket; 808 809 query = *queryp; 810 fctx = query->fctx; 811 812 FCTXTRACE("cancelquery"); 813 814 REQUIRE(!RESQUERY_CANCELED(query)); 815 816 query->attributes |= RESQUERY_ATTR_CANCELED; 817 818 /* 819 * Should we update the RTT? 820 */ 821 if (finish != NULL || no_response) { 822 if (finish != NULL) { 823 /* 824 * We have both the start and finish times for this 825 * packet, so we can compute a real RTT. 826 */ 827 rtt = (unsigned int)isc_time_microdiff(finish, 828 &query->start); 829 factor = DNS_ADB_RTTADJDEFAULT; 830 831 rttms = rtt / 1000; 832 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) { 833 inc_stats(fctx->res, 834 dns_resstatscounter_queryrtt0); 835 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) { 836 inc_stats(fctx->res, 837 dns_resstatscounter_queryrtt1); 838 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) { 839 inc_stats(fctx->res, 840 dns_resstatscounter_queryrtt2); 841 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) { 842 inc_stats(fctx->res, 843 dns_resstatscounter_queryrtt3); 844 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) { 845 inc_stats(fctx->res, 846 dns_resstatscounter_queryrtt4); 847 } else { 848 inc_stats(fctx->res, 849 dns_resstatscounter_queryrtt5); 850 } 851 } else { 852 /* 853 * We don't have an RTT for this query. Maybe the 854 * packet was lost, or maybe this server is very 855 * slow. We don't know. Increase the RTT. 856 */ 857 INSIST(no_response); 858 rtt = query->addrinfo->srtt + 200000; 859 if (rtt > MAX_SINGLE_QUERY_TIMEOUT_US) 860 rtt = MAX_SINGLE_QUERY_TIMEOUT_US; 861 /* 862 * Replace the current RTT with our value. 863 */ 864 factor = DNS_ADB_RTTADJREPLACE; 865 } 866 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor); 867 } 868 869 /* Remember that the server has been tried. */ 870 if (!TRIED(query->addrinfo)) { 871 dns_adb_changeflags(fctx->adb, query->addrinfo, 872 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED); 873 } 874 875 /* 876 * Age RTTs of servers not tried. 877 */ 878 factor = DNS_ADB_RTTADJAGE; 879 if (finish != NULL) 880 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs); 881 addrinfo != NULL; 882 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) 883 if (UNMARKED(addrinfo)) 884 dns_adb_adjustsrtt(fctx->adb, addrinfo, 885 0, factor); 886 887 if (finish != NULL && TRIEDFIND(fctx)) 888 for (find = ISC_LIST_HEAD(fctx->finds); 889 find != NULL; 890 find = ISC_LIST_NEXT(find, publink)) 891 for (addrinfo = ISC_LIST_HEAD(find->list); 892 addrinfo != NULL; 893 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) 894 if (UNMARKED(addrinfo)) 895 dns_adb_adjustsrtt(fctx->adb, addrinfo, 896 0, factor); 897 898 if (finish != NULL && TRIEDALT(fctx)) { 899 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs); 900 addrinfo != NULL; 901 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) 902 if (UNMARKED(addrinfo)) 903 dns_adb_adjustsrtt(fctx->adb, addrinfo, 904 0, factor); 905 for (find = ISC_LIST_HEAD(fctx->altfinds); 906 find != NULL; 907 find = ISC_LIST_NEXT(find, publink)) 908 for (addrinfo = ISC_LIST_HEAD(find->list); 909 addrinfo != NULL; 910 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) 911 if (UNMARKED(addrinfo)) 912 dns_adb_adjustsrtt(fctx->adb, addrinfo, 913 0, factor); 914 } 915 916 /* 917 * Check for any outstanding socket events. If they exist, cancel 918 * them and let the event handlers finish the cleanup. The resolver 919 * only needs to worry about managing the connect and send events; 920 * the dispatcher manages the recv events. 921 */ 922 if (RESQUERY_CONNECTING(query)) { 923 /* 924 * Cancel the connect. 925 */ 926 if (query->tcpsocket != NULL) { 927 isc_socket_cancel(query->tcpsocket, NULL, 928 ISC_SOCKCANCEL_CONNECT); 929 } else if (query->dispentry != NULL) { 930 INSIST(query->exclusivesocket); 931 socket = dns_dispatch_getentrysocket(query->dispentry); 932 if (socket != NULL) 933 isc_socket_cancel(socket, NULL, 934 ISC_SOCKCANCEL_CONNECT); 935 } 936 } else if (RESQUERY_SENDING(query)) { 937 /* 938 * Cancel the pending send. 939 */ 940 if (query->exclusivesocket && query->dispentry != NULL) 941 socket = dns_dispatch_getentrysocket(query->dispentry); 942 else 943 socket = dns_dispatch_getsocket(query->dispatch); 944 if (socket != NULL) 945 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND); 946 } 947 948 if (query->dispentry != NULL) 949 dns_dispatch_removeresponse(&query->dispentry, deventp); 950 951 ISC_LIST_UNLINK(fctx->queries, query, link); 952 953 if (query->tsig != NULL) 954 isc_buffer_free(&query->tsig); 955 956 if (query->tsigkey != NULL) 957 dns_tsigkey_detach(&query->tsigkey); 958 959 if (query->dispatch != NULL) 960 dns_dispatch_detach(&query->dispatch); 961 962 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query))) 963 /* 964 * It's safe to destroy the query now. 965 */ 966 resquery_destroy(&query); 967} 968 969static void 970fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) { 971 resquery_t *query, *next_query; 972 973 FCTXTRACE("cancelqueries"); 974 975 for (query = ISC_LIST_HEAD(fctx->queries); 976 query != NULL; 977 query = next_query) { 978 next_query = ISC_LIST_NEXT(query, link); 979 fctx_cancelquery(&query, NULL, NULL, no_response); 980 } 981} 982 983static void 984fctx_cleanupfinds(fetchctx_t *fctx) { 985 dns_adbfind_t *find, *next_find; 986 987 REQUIRE(ISC_LIST_EMPTY(fctx->queries)); 988 989 for (find = ISC_LIST_HEAD(fctx->finds); 990 find != NULL; 991 find = next_find) { 992 next_find = ISC_LIST_NEXT(find, publink); 993 ISC_LIST_UNLINK(fctx->finds, find, publink); 994 dns_adb_destroyfind(&find); 995 } 996 fctx->find = NULL; 997} 998 999static void 1000fctx_cleanupaltfinds(fetchctx_t *fctx) { 1001 dns_adbfind_t *find, *next_find; 1002 1003 REQUIRE(ISC_LIST_EMPTY(fctx->queries)); 1004 1005 for (find = ISC_LIST_HEAD(fctx->altfinds); 1006 find != NULL; 1007 find = next_find) { 1008 next_find = ISC_LIST_NEXT(find, publink); 1009 ISC_LIST_UNLINK(fctx->altfinds, find, publink); 1010 dns_adb_destroyfind(&find); 1011 } 1012 fctx->altfind = NULL; 1013} 1014 1015static void 1016fctx_cleanupforwaddrs(fetchctx_t *fctx) { 1017 dns_adbaddrinfo_t *addr, *next_addr; 1018 1019 REQUIRE(ISC_LIST_EMPTY(fctx->queries)); 1020 1021 for (addr = ISC_LIST_HEAD(fctx->forwaddrs); 1022 addr != NULL; 1023 addr = next_addr) { 1024 next_addr = ISC_LIST_NEXT(addr, publink); 1025 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink); 1026 dns_adb_freeaddrinfo(fctx->adb, &addr); 1027 } 1028} 1029 1030static void 1031fctx_cleanupaltaddrs(fetchctx_t *fctx) { 1032 dns_adbaddrinfo_t *addr, *next_addr; 1033 1034 REQUIRE(ISC_LIST_EMPTY(fctx->queries)); 1035 1036 for (addr = ISC_LIST_HEAD(fctx->altaddrs); 1037 addr != NULL; 1038 addr = next_addr) { 1039 next_addr = ISC_LIST_NEXT(addr, publink); 1040 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink); 1041 dns_adb_freeaddrinfo(fctx->adb, &addr); 1042 } 1043} 1044 1045static inline void 1046fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) { 1047 FCTXTRACE("stopeverything"); 1048 fctx_cancelqueries(fctx, no_response); 1049 fctx_cleanupfinds(fctx); 1050 fctx_cleanupaltfinds(fctx); 1051 fctx_cleanupforwaddrs(fctx); 1052 fctx_cleanupaltaddrs(fctx); 1053 fctx_stoptimer(fctx); 1054} 1055 1056static inline void 1057fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) { 1058 dns_fetchevent_t *event, *next_event; 1059 isc_task_t *task; 1060 unsigned int count = 0; 1061 isc_interval_t i; 1062 isc_boolean_t logit = ISC_FALSE; 1063 isc_time_t now; 1064 unsigned int old_spillat; 1065 unsigned int new_spillat = 0; /* initialized to silence 1066 compiler warnings */ 1067 1068 /* 1069 * Caller must be holding the appropriate bucket lock. 1070 */ 1071 REQUIRE(fctx->state == fetchstate_done); 1072 1073 FCTXTRACE("sendevents"); 1074 1075 /* 1076 * Keep some record of fetch result for logging later (if required). 1077 */ 1078 fctx->result = result; 1079 fctx->exitline = line; 1080 TIME_NOW(&now); 1081 fctx->duration = isc_time_microdiff(&now, &fctx->start); 1082 1083 for (event = ISC_LIST_HEAD(fctx->events); 1084 event != NULL; 1085 event = next_event) { 1086 next_event = ISC_LIST_NEXT(event, ev_link); 1087 ISC_LIST_UNLINK(fctx->events, event, ev_link); 1088 task = event->ev_sender; 1089 event->ev_sender = fctx; 1090 event->vresult = fctx->vresult; 1091 if (!HAVE_ANSWER(fctx)) 1092 event->result = result; 1093 1094 INSIST(result != ISC_R_SUCCESS || 1095 dns_rdataset_isassociated(event->rdataset) || 1096 fctx->type == dns_rdatatype_any || 1097 fctx->type == dns_rdatatype_rrsig || 1098 fctx->type == dns_rdatatype_sig); 1099 1100 /* 1101 * Negative results must be indicated in event->result. 1102 */ 1103 if (dns_rdataset_isassociated(event->rdataset) && 1104 NEGATIVE(event->rdataset)) { 1105 INSIST(event->result == DNS_R_NCACHENXDOMAIN || 1106 event->result == DNS_R_NCACHENXRRSET); 1107 } 1108 1109 event->qtotal = fctx->totalqueries; 1110 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event)); 1111 count++; 1112 } 1113 1114 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 && 1115 fctx->spilled && 1116 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) { 1117 LOCK(&fctx->res->lock); 1118 if (count == fctx->res->spillat && !fctx->res->exiting) { 1119 old_spillat = fctx->res->spillat; 1120 fctx->res->spillat += 5; 1121 if (fctx->res->spillat > fctx->res->spillatmax && 1122 fctx->res->spillatmax != 0) 1123 fctx->res->spillat = fctx->res->spillatmax; 1124 new_spillat = fctx->res->spillat; 1125 if (new_spillat != old_spillat) { 1126 logit = ISC_TRUE; 1127 } 1128 isc_interval_set(&i, 20 * 60, 0); 1129 result = isc_timer_reset(fctx->res->spillattimer, 1130 isc_timertype_ticker, NULL, 1131 &i, ISC_TRUE); 1132 RUNTIME_CHECK(result == ISC_R_SUCCESS); 1133 } 1134 UNLOCK(&fctx->res->lock); 1135 if (logit) 1136 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 1137 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 1138 "clients-per-query increased to %u", 1139 new_spillat); 1140 } 1141} 1142 1143static inline void 1144log_edns(fetchctx_t *fctx) { 1145 char domainbuf[DNS_NAME_FORMATSIZE]; 1146 1147 if (fctx->reason == NULL) 1148 return; 1149 1150 /* 1151 * We do not know if fctx->domain is the actual domain the record 1152 * lives in or a parent domain so we have a '?' after it. 1153 */ 1154 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf)); 1155 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED, 1156 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO, 1157 "success resolving '%s' (in '%s'?) after %s", 1158 fctx->info, domainbuf, fctx->reason); 1159 1160 fctx->reason = NULL; 1161} 1162 1163static void 1164fctx_done(fetchctx_t *fctx, isc_result_t result, int line) { 1165 dns_resolver_t *res; 1166 isc_boolean_t no_response; 1167 1168 REQUIRE(line >= 0); 1169 1170 FCTXTRACE("done"); 1171 1172 res = fctx->res; 1173 1174 if (result == ISC_R_SUCCESS) { 1175 /*% 1176 * Log any deferred EDNS timeout messages. 1177 */ 1178 log_edns(fctx); 1179 no_response = ISC_TRUE; 1180 } else 1181 no_response = ISC_FALSE; 1182 1183 fctx->reason = NULL; 1184 fctx_stopeverything(fctx, no_response); 1185 1186 LOCK(&res->buckets[fctx->bucketnum].lock); 1187 1188 fctx->state = fetchstate_done; 1189 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT; 1190 fctx_sendevents(fctx, result, line); 1191 1192 UNLOCK(&res->buckets[fctx->bucketnum].lock); 1193} 1194 1195static void 1196process_sendevent(resquery_t *query, isc_event_t *event) { 1197 isc_socketevent_t *sevent = (isc_socketevent_t *)event; 1198 isc_boolean_t retry = ISC_FALSE; 1199 isc_result_t result; 1200 fetchctx_t *fctx; 1201 1202 fctx = query->fctx; 1203 1204 if (RESQUERY_CANCELED(query)) { 1205 if (query->sends == 0 && query->connects == 0) { 1206 /* 1207 * This query was canceled while the 1208 * isc_socket_sendto/connect() was in progress. 1209 */ 1210 if (query->tcpsocket != NULL) 1211 isc_socket_detach(&query->tcpsocket); 1212 resquery_destroy(&query); 1213 } 1214 } else { 1215 switch (sevent->result) { 1216 case ISC_R_SUCCESS: 1217 break; 1218 1219 case ISC_R_HOSTUNREACH: 1220 case ISC_R_NETUNREACH: 1221 case ISC_R_NOPERM: 1222 case ISC_R_ADDRNOTAVAIL: 1223 case ISC_R_CONNREFUSED: 1224 1225 /* 1226 * No route to remote. 1227 */ 1228 add_bad(fctx, query->addrinfo, sevent->result, 1229 badns_unreachable); 1230 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE); 1231 retry = ISC_TRUE; 1232 break; 1233 1234 default: 1235 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE); 1236 break; 1237 } 1238 } 1239 1240 if (event->ev_type == ISC_SOCKEVENT_CONNECT) 1241 isc_event_free(&event); 1242 1243 if (retry) { 1244 /* 1245 * Behave as if the idle timer has expired. For TCP 1246 * this may not actually reflect the latest timer. 1247 */ 1248 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT; 1249 result = fctx_stopidletimer(fctx); 1250 if (result != ISC_R_SUCCESS) 1251 fctx_done(fctx, result, __LINE__); 1252 else 1253 fctx_try(fctx, ISC_TRUE, ISC_FALSE); 1254 } 1255} 1256 1257static void 1258resquery_udpconnected(isc_task_t *task, isc_event_t *event) { 1259 resquery_t *query = event->ev_arg; 1260 1261 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT); 1262 1263 QTRACE("udpconnected"); 1264 1265 UNUSED(task); 1266 1267 INSIST(RESQUERY_CONNECTING(query)); 1268 1269 query->connects--; 1270 1271 process_sendevent(query, event); 1272} 1273 1274static void 1275resquery_senddone(isc_task_t *task, isc_event_t *event) { 1276 resquery_t *query = event->ev_arg; 1277 1278 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE); 1279 1280 QTRACE("senddone"); 1281 1282 /* 1283 * XXXRTH 1284 * 1285 * Currently we don't wait for the senddone event before retrying 1286 * a query. This means that if we get really behind, we may end 1287 * up doing extra work! 1288 */ 1289 1290 UNUSED(task); 1291 1292 INSIST(RESQUERY_SENDING(query)); 1293 1294 query->sends--; 1295 1296 process_sendevent(query, event); 1297} 1298 1299static inline isc_result_t 1300fctx_addopt(dns_message_t *message, unsigned int version, 1301 isc_uint16_t udpsize, dns_ednsopt_t *ednsopts, size_t count) 1302{ 1303 dns_rdataset_t *rdataset = NULL; 1304 isc_result_t result; 1305 1306 result = dns_message_buildopt(message, &rdataset, version, udpsize, 1307 DNS_MESSAGEEXTFLAG_DO, ednsopts, count); 1308 if (result != ISC_R_SUCCESS) 1309 return (result); 1310 return (dns_message_setopt(message, rdataset)); 1311} 1312 1313static inline void 1314fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) { 1315 unsigned int seconds; 1316 unsigned int us; 1317 1318 /* 1319 * We retry every .8 seconds the first two times through the address 1320 * list, and then we do exponential back-off. 1321 */ 1322 if (fctx->restarts < 3) 1323 us = 800000; 1324 else 1325 us = (800000 << (fctx->restarts - 2)); 1326 1327 /* 1328 * Add a fudge factor to the expected rtt based on the current 1329 * estimate. 1330 */ 1331 if (rtt < 50000) 1332 rtt += 50000; 1333 else if (rtt < 100000) 1334 rtt += 100000; 1335 else 1336 rtt += 200000; 1337 1338 /* 1339 * Always wait for at least the expected rtt. 1340 */ 1341 if (us < rtt) 1342 us = rtt; 1343 1344 /* 1345 * But don't ever wait for more than 10 seconds. 1346 */ 1347 if (us > MAX_SINGLE_QUERY_TIMEOUT_US) 1348 us = MAX_SINGLE_QUERY_TIMEOUT_US; 1349 1350 seconds = us / US_PER_SEC; 1351 us -= seconds * US_PER_SEC; 1352 isc_interval_set(&fctx->interval, seconds, us * 1000); 1353} 1354 1355static isc_result_t 1356fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, 1357 unsigned int options) 1358{ 1359 dns_resolver_t *res; 1360 isc_task_t *task; 1361 isc_result_t result; 1362 resquery_t *query; 1363 isc_sockaddr_t addr; 1364 isc_boolean_t have_addr = ISC_FALSE; 1365 unsigned int srtt; 1366 1367 FCTXTRACE("query"); 1368 1369 res = fctx->res; 1370 task = res->buckets[fctx->bucketnum].task; 1371 1372 srtt = addrinfo->srtt; 1373 1374 /* 1375 * A forwarder needs to make multiple queries. Give it at least 1376 * a second to do these in. 1377 */ 1378 if (ISFORWARDER(addrinfo) && srtt < 1000000) 1379 srtt = 1000000; 1380 1381 fctx_setretryinterval(fctx, srtt); 1382 result = fctx_startidletimer(fctx, &fctx->interval); 1383 if (result != ISC_R_SUCCESS) 1384 return (result); 1385 1386 INSIST(ISC_LIST_EMPTY(fctx->validators)); 1387 1388 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE); 1389 1390 query = isc_mem_get(fctx->mctx, sizeof(*query)); 1391 if (query == NULL) { 1392 result = ISC_R_NOMEMORY; 1393 goto stop_idle_timer; 1394 } 1395 query->mctx = fctx->mctx; 1396 query->options = options; 1397 query->attributes = 0; 1398 query->sends = 0; 1399 query->connects = 0; 1400 /* 1401 * Note that the caller MUST guarantee that 'addrinfo' will remain 1402 * valid until this query is canceled. 1403 */ 1404 query->addrinfo = addrinfo; 1405 TIME_NOW(&query->start); 1406 1407 /* 1408 * If this is a TCP query, then we need to make a socket and 1409 * a dispatch for it here. Otherwise we use the resolver's 1410 * shared dispatch. 1411 */ 1412 query->dispatchmgr = res->dispatchmgr; 1413 query->dispatch = NULL; 1414 query->exclusivesocket = ISC_FALSE; 1415 query->tcpsocket = NULL; 1416 if (res->view->peers != NULL) { 1417 dns_peer_t *peer = NULL; 1418 isc_netaddr_t dstip; 1419 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr); 1420 result = dns_peerlist_peerbyaddr(res->view->peers, 1421 &dstip, &peer); 1422 if (result == ISC_R_SUCCESS) { 1423 result = dns_peer_getquerysource(peer, &addr); 1424 if (result == ISC_R_SUCCESS) 1425 have_addr = ISC_TRUE; 1426 } 1427 } 1428 1429 if ((query->options & DNS_FETCHOPT_TCP) != 0) { 1430 int pf; 1431 1432 pf = isc_sockaddr_pf(&addrinfo->sockaddr); 1433 if (!have_addr) { 1434 switch (pf) { 1435 case PF_INET: 1436 result = dns_dispatch_getlocaladdress( 1437 res->dispatches4->dispatches[0], 1438 &addr); 1439 break; 1440 case PF_INET6: 1441 result = dns_dispatch_getlocaladdress( 1442 res->dispatches6->dispatches[0], 1443 &addr); 1444 break; 1445 default: 1446 result = ISC_R_NOTIMPLEMENTED; 1447 break; 1448 } 1449 if (result != ISC_R_SUCCESS) 1450 goto cleanup_query; 1451 } 1452 isc_sockaddr_setport(&addr, 0); 1453 1454 result = isc_socket_create(res->socketmgr, pf, 1455 isc_sockettype_tcp, 1456 &query->tcpsocket); 1457 if (result != ISC_R_SUCCESS) 1458 goto cleanup_query; 1459 1460#ifndef BROKEN_TCP_BIND_BEFORE_CONNECT 1461 result = isc_socket_bind(query->tcpsocket, &addr, 0); 1462 if (result != ISC_R_SUCCESS) 1463 goto cleanup_socket; 1464#endif 1465 1466 /* 1467 * A dispatch will be created once the connect succeeds. 1468 */ 1469 } else { 1470 if (have_addr) { 1471 unsigned int attrs, attrmask; 1472 attrs = DNS_DISPATCHATTR_UDP; 1473 switch (isc_sockaddr_pf(&addr)) { 1474 case AF_INET: 1475 attrs |= DNS_DISPATCHATTR_IPV4; 1476 break; 1477 case AF_INET6: 1478 attrs |= DNS_DISPATCHATTR_IPV6; 1479 break; 1480 default: 1481 result = ISC_R_NOTIMPLEMENTED; 1482 goto cleanup_query; 1483 } 1484 attrmask = DNS_DISPATCHATTR_UDP; 1485 attrmask |= DNS_DISPATCHATTR_TCP; 1486 attrmask |= DNS_DISPATCHATTR_IPV4; 1487 attrmask |= DNS_DISPATCHATTR_IPV6; 1488 result = dns_dispatch_getudp(res->dispatchmgr, 1489 res->socketmgr, 1490 res->taskmgr, &addr, 1491 4096, 1000, 32768, 16411, 1492 16433, attrs, attrmask, 1493 &query->dispatch); 1494 if (result != ISC_R_SUCCESS) 1495 goto cleanup_query; 1496 } else { 1497 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) { 1498 case PF_INET: 1499 dns_dispatch_attach( 1500 dns_resolver_dispatchv4(res), 1501 &query->dispatch); 1502 query->exclusivesocket = res->exclusivev4; 1503 break; 1504 case PF_INET6: 1505 dns_dispatch_attach( 1506 dns_resolver_dispatchv6(res), 1507 &query->dispatch); 1508 query->exclusivesocket = res->exclusivev6; 1509 break; 1510 default: 1511 result = ISC_R_NOTIMPLEMENTED; 1512 goto cleanup_query; 1513 } 1514 } 1515 /* 1516 * We should always have a valid dispatcher here. If we 1517 * don't support a protocol family, then its dispatcher 1518 * will be NULL, but we shouldn't be finding addresses for 1519 * protocol types we don't support, so the dispatcher 1520 * we found should never be NULL. 1521 */ 1522 INSIST(query->dispatch != NULL); 1523 } 1524 1525 query->dispentry = NULL; 1526 query->fctx = fctx; 1527 query->tsig = NULL; 1528 query->tsigkey = NULL; 1529 ISC_LINK_INIT(query, link); 1530 query->magic = QUERY_MAGIC; 1531 1532 if ((query->options & DNS_FETCHOPT_TCP) != 0) { 1533 /* 1534 * Connect to the remote server. 1535 * 1536 * XXXRTH Should we attach to the socket? 1537 */ 1538 result = isc_socket_connect(query->tcpsocket, 1539 &addrinfo->sockaddr, task, 1540 resquery_connected, query); 1541 if (result != ISC_R_SUCCESS) 1542 goto cleanup_socket; 1543 query->connects++; 1544 QTRACE("connecting via TCP"); 1545 } else { 1546 result = resquery_send(query); 1547 if (result != ISC_R_SUCCESS) 1548 goto cleanup_dispatch; 1549 } 1550 1551 fctx->querysent++; 1552 fctx->totalqueries++; 1553 1554 ISC_LIST_APPEND(fctx->queries, query, link); 1555 query->fctx->nqueries++; 1556 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET) 1557 inc_stats(res, dns_resstatscounter_queryv4); 1558 else 1559 inc_stats(res, dns_resstatscounter_queryv6); 1560 if (res->view->resquerystats != NULL) 1561 dns_rdatatypestats_increment(res->view->resquerystats, 1562 fctx->type); 1563 1564 return (ISC_R_SUCCESS); 1565 1566 cleanup_socket: 1567 isc_socket_detach(&query->tcpsocket); 1568 1569 cleanup_dispatch: 1570 if (query->dispatch != NULL) 1571 dns_dispatch_detach(&query->dispatch); 1572 1573 cleanup_query: 1574 if (query->connects == 0) { 1575 query->magic = 0; 1576 isc_mem_put(fctx->mctx, query, sizeof(*query)); 1577 } 1578 1579 stop_idle_timer: 1580 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS); 1581 1582 return (result); 1583} 1584 1585static isc_boolean_t 1586bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) { 1587 isc_sockaddr_t *sa; 1588 1589 for (sa = ISC_LIST_HEAD(fctx->bad_edns); 1590 sa != NULL; 1591 sa = ISC_LIST_NEXT(sa, link)) { 1592 if (isc_sockaddr_equal(sa, address)) 1593 return (ISC_TRUE); 1594 } 1595 1596 return (ISC_FALSE); 1597} 1598 1599static void 1600add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) { 1601 isc_sockaddr_t *sa; 1602 1603 if (bad_edns(fctx, address)) 1604 return; 1605 1606 sa = isc_mem_get(fctx->mctx, sizeof(*sa)); 1607 if (sa == NULL) 1608 return; 1609 1610 *sa = *address; 1611 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link); 1612} 1613 1614static isc_boolean_t 1615triededns(fetchctx_t *fctx, isc_sockaddr_t *address) { 1616 isc_sockaddr_t *sa; 1617 1618 for (sa = ISC_LIST_HEAD(fctx->edns); 1619 sa != NULL; 1620 sa = ISC_LIST_NEXT(sa, link)) { 1621 if (isc_sockaddr_equal(sa, address)) 1622 return (ISC_TRUE); 1623 } 1624 1625 return (ISC_FALSE); 1626} 1627 1628static void 1629add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) { 1630 isc_sockaddr_t *sa; 1631 1632 if (triededns(fctx, address)) 1633 return; 1634 1635 sa = isc_mem_get(fctx->mctx, sizeof(*sa)); 1636 if (sa == NULL) 1637 return; 1638 1639 *sa = *address; 1640 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link); 1641} 1642 1643static isc_boolean_t 1644triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) { 1645 isc_sockaddr_t *sa; 1646 1647 for (sa = ISC_LIST_HEAD(fctx->edns512); 1648 sa != NULL; 1649 sa = ISC_LIST_NEXT(sa, link)) { 1650 if (isc_sockaddr_equal(sa, address)) 1651 return (ISC_TRUE); 1652 } 1653 1654 return (ISC_FALSE); 1655} 1656 1657static void 1658add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) { 1659 isc_sockaddr_t *sa; 1660 1661 if (triededns512(fctx, address)) 1662 return; 1663 1664 sa = isc_mem_get(fctx->mctx, sizeof(*sa)); 1665 if (sa == NULL) 1666 return; 1667 1668 *sa = *address; 1669 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link); 1670} 1671 1672static isc_result_t 1673resquery_send(resquery_t *query) { 1674 fetchctx_t *fctx; 1675 isc_result_t result; 1676 dns_name_t *qname = NULL; 1677 dns_rdataset_t *qrdataset = NULL; 1678 isc_region_t r; 1679 dns_resolver_t *res; 1680 isc_task_t *task; 1681 isc_socket_t *socket; 1682 isc_buffer_t tcpbuffer; 1683 isc_sockaddr_t *address; 1684 isc_buffer_t *buffer; 1685 isc_netaddr_t ipaddr; 1686 dns_tsigkey_t *tsigkey = NULL; 1687 dns_peer_t *peer = NULL; 1688 isc_boolean_t useedns; 1689 dns_compress_t cctx; 1690 isc_boolean_t cleanup_cctx = ISC_FALSE; 1691 isc_boolean_t secure_domain; 1692 isc_boolean_t connecting = ISC_FALSE; 1693 dns_ednsopt_t ednsopts[EDNSOPTS]; 1694 unsigned ednsopt = 0; 1695 1696 fctx = query->fctx; 1697 QTRACE("send"); 1698 1699 res = fctx->res; 1700 task = res->buckets[fctx->bucketnum].task; 1701 address = NULL; 1702 1703 if ((query->options & DNS_FETCHOPT_TCP) != 0) { 1704 /* 1705 * Reserve space for the TCP message length. 1706 */ 1707 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data)); 1708 isc_buffer_init(&query->buffer, query->data + 2, 1709 sizeof(query->data) - 2); 1710 buffer = &tcpbuffer; 1711 } else { 1712 isc_buffer_init(&query->buffer, query->data, 1713 sizeof(query->data)); 1714 buffer = &query->buffer; 1715 } 1716 1717 result = dns_message_gettempname(fctx->qmessage, &qname); 1718 if (result != ISC_R_SUCCESS) 1719 goto cleanup_temps; 1720 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset); 1721 if (result != ISC_R_SUCCESS) 1722 goto cleanup_temps; 1723 1724 /* 1725 * Get a query id from the dispatch. 1726 */ 1727 result = dns_dispatch_addresponse2(query->dispatch, 1728 &query->addrinfo->sockaddr, 1729 task, 1730 resquery_response, 1731 query, 1732 &query->id, 1733 &query->dispentry, 1734 res->socketmgr); 1735 if (result != ISC_R_SUCCESS) 1736 goto cleanup_temps; 1737 1738 fctx->qmessage->opcode = dns_opcode_query; 1739 1740 /* 1741 * Set up question. 1742 */ 1743 dns_name_init(qname, NULL); 1744 dns_name_clone(&fctx->name, qname); 1745 dns_rdataset_init(qrdataset); 1746 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type); 1747 ISC_LIST_APPEND(qname->list, qrdataset, link); 1748 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION); 1749 qname = NULL; 1750 qrdataset = NULL; 1751 1752 /* 1753 * Set RD if the client has requested that we do a recursive query, 1754 * or if we're sending to a forwarder. 1755 */ 1756 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 || 1757 ISFORWARDER(query->addrinfo)) 1758 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD; 1759 1760 /* 1761 * Set CD if the client says don't validate or the question is 1762 * under a secure entry point. 1763 */ 1764 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) { 1765 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD; 1766 } else if (res->view->enablevalidation) { 1767 result = dns_view_issecuredomain(res->view, &fctx->name, 1768 &secure_domain); 1769 if (result != ISC_R_SUCCESS) 1770 secure_domain = ISC_FALSE; 1771 if (res->view->dlv != NULL) 1772 secure_domain = ISC_TRUE; 1773 if (secure_domain) 1774 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD; 1775 } 1776 1777 /* 1778 * We don't have to set opcode because it defaults to query. 1779 */ 1780 fctx->qmessage->id = query->id; 1781 1782 /* 1783 * Convert the question to wire format. 1784 */ 1785 result = dns_compress_init(&cctx, -1, fctx->res->mctx); 1786 if (result != ISC_R_SUCCESS) 1787 goto cleanup_message; 1788 cleanup_cctx = ISC_TRUE; 1789 1790 result = dns_message_renderbegin(fctx->qmessage, &cctx, 1791 &query->buffer); 1792 if (result != ISC_R_SUCCESS) 1793 goto cleanup_message; 1794 1795 result = dns_message_rendersection(fctx->qmessage, 1796 DNS_SECTION_QUESTION, 0); 1797 if (result != ISC_R_SUCCESS) 1798 goto cleanup_message; 1799 1800 peer = NULL; 1801 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr); 1802 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer); 1803 1804 /* 1805 * The ADB does not know about servers with "edns no". Check this, 1806 * and then inform the ADB for future use. 1807 */ 1808 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 && 1809 peer != NULL && 1810 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS && 1811 !useedns) 1812 { 1813 query->options |= DNS_FETCHOPT_NOEDNS0; 1814 dns_adb_changeflags(fctx->adb, query->addrinfo, 1815 DNS_FETCHOPT_NOEDNS0, 1816 DNS_FETCHOPT_NOEDNS0); 1817 } 1818 1819 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */ 1820 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0) 1821 query->options |= DNS_FETCHOPT_NOEDNS0; 1822 1823 /* 1824 * Handle timeouts by reducing the UDP response size to 512 bytes 1825 * then if that doesn't work disabling EDNS (includes DO) and CD. 1826 * 1827 * These timeout can be due to: 1828 * * broken nameservers that don't respond to EDNS queries. 1829 * * broken/misconfigured firewalls and NAT implementations 1830 * that don't handle IP fragmentation. 1831 * * broken/misconfigured firewalls that don't handle responses 1832 * greater than 512 bytes. 1833 * * broken/misconfigured firewalls that don't handle EDNS, DO 1834 * or CD. 1835 * * packet loss / link outage. 1836 */ 1837 if (fctx->timeout) { 1838 if ((triededns512(fctx, &query->addrinfo->sockaddr) || 1839 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) && 1840 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) { 1841 query->options |= DNS_FETCHOPT_NOEDNS0; 1842 fctx->reason = "disabling EDNS"; 1843 } else if ((triededns(fctx, &query->addrinfo->sockaddr) || 1844 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) && 1845 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) { 1846 query->options |= DNS_FETCHOPT_EDNS512; 1847 fctx->reason = "reducing the advertised EDNS UDP " 1848 "packet size to 512 octets"; 1849 } 1850 fctx->timeout = ISC_FALSE; 1851 } 1852 1853 /* 1854 * Use EDNS0, unless the caller doesn't want it, or we know that 1855 * the remote server doesn't like it. 1856 */ 1857 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) { 1858 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) { 1859 unsigned int version = 0; /* Default version. */ 1860 unsigned int flags; 1861 isc_uint16_t udpsize = res->udpsize; 1862 isc_boolean_t reqnsid = res->view->requestnsid; 1863 1864 flags = query->addrinfo->flags; 1865 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) { 1866 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK; 1867 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT; 1868 } 1869 if ((query->options & DNS_FETCHOPT_EDNS512) != 0) 1870 udpsize = 512; 1871 else if (peer != NULL) 1872 (void)dns_peer_getudpsize(peer, &udpsize); 1873 1874 /* request NSID for current view or peer? */ 1875 if (peer != NULL) 1876 (void) dns_peer_getrequestnsid(peer, &reqnsid); 1877 if (reqnsid) { 1878 INSIST(ednsopt < EDNSOPTS); 1879 ednsopts[ednsopt].code = DNS_OPT_NSID; 1880 ednsopts[ednsopt].length = 0; 1881 ednsopts[ednsopt].value = NULL; 1882 ednsopt++; 1883 } 1884 result = fctx_addopt(fctx->qmessage, version, 1885 udpsize, ednsopts, ednsopt); 1886 if (reqnsid && result == ISC_R_SUCCESS) { 1887 query->options |= DNS_FETCHOPT_WANTNSID; 1888 } else if (result != ISC_R_SUCCESS) { 1889 /* 1890 * We couldn't add the OPT, but we'll press on. 1891 * We're not using EDNS0, so set the NOEDNS0 1892 * bit. 1893 */ 1894 query->options |= DNS_FETCHOPT_NOEDNS0; 1895 } 1896 } else { 1897 /* 1898 * We know this server doesn't like EDNS0, so we 1899 * won't use it. Set the NOEDNS0 bit since we're 1900 * not using EDNS0. 1901 */ 1902 query->options |= DNS_FETCHOPT_NOEDNS0; 1903 } 1904 } 1905 1906 /* 1907 * If we need EDNS0 to do this query and aren't using it, we lose. 1908 */ 1909 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) { 1910 result = DNS_R_SERVFAIL; 1911 goto cleanup_message; 1912 } 1913 1914 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) 1915 add_triededns(fctx, &query->addrinfo->sockaddr); 1916 1917 if ((query->options & DNS_FETCHOPT_EDNS512) != 0) 1918 add_triededns512(fctx, &query->addrinfo->sockaddr); 1919 1920 /* 1921 * Clear CD if EDNS is not in use. 1922 */ 1923 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0) 1924 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD; 1925 1926 /* 1927 * Add TSIG record tailored to the current recipient. 1928 */ 1929 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey); 1930 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) 1931 goto cleanup_message; 1932 1933 if (tsigkey != NULL) { 1934 result = dns_message_settsigkey(fctx->qmessage, tsigkey); 1935 dns_tsigkey_detach(&tsigkey); 1936 if (result != ISC_R_SUCCESS) 1937 goto cleanup_message; 1938 } 1939 1940 result = dns_message_rendersection(fctx->qmessage, 1941 DNS_SECTION_ADDITIONAL, 0); 1942 if (result != ISC_R_SUCCESS) 1943 goto cleanup_message; 1944 1945 result = dns_message_renderend(fctx->qmessage); 1946 if (result != ISC_R_SUCCESS) 1947 goto cleanup_message; 1948 1949 dns_compress_invalidate(&cctx); 1950 cleanup_cctx = ISC_FALSE; 1951 1952 if (dns_message_gettsigkey(fctx->qmessage) != NULL) { 1953 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage), 1954 &query->tsigkey); 1955 result = dns_message_getquerytsig(fctx->qmessage, 1956 fctx->res->mctx, 1957 &query->tsig); 1958 if (result != ISC_R_SUCCESS) 1959 goto cleanup_message; 1960 } 1961 1962 /* 1963 * If using TCP, write the length of the message at the beginning 1964 * of the buffer. 1965 */ 1966 if ((query->options & DNS_FETCHOPT_TCP) != 0) { 1967 isc_buffer_usedregion(&query->buffer, &r); 1968 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length); 1969 isc_buffer_add(&tcpbuffer, r.length); 1970 } 1971 1972 /* 1973 * We're now done with the query message. 1974 */ 1975 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER); 1976 1977 if (query->exclusivesocket) 1978 socket = dns_dispatch_getentrysocket(query->dispentry); 1979 else 1980 socket = dns_dispatch_getsocket(query->dispatch); 1981 /* 1982 * Send the query! 1983 */ 1984 if ((query->options & DNS_FETCHOPT_TCP) == 0) { 1985 address = &query->addrinfo->sockaddr; 1986 if (query->exclusivesocket) { 1987 result = isc_socket_connect(socket, address, task, 1988 resquery_udpconnected, 1989 query); 1990 if (result != ISC_R_SUCCESS) 1991 goto cleanup_message; 1992 connecting = ISC_TRUE; 1993 query->connects++; 1994 } 1995 } 1996 isc_buffer_usedregion(buffer, &r); 1997 1998 /* 1999 * XXXRTH Make sure we don't send to ourselves! We should probably 2000 * prune out these addresses when we get them from the ADB. 2001 */ 2002 ISC_EVENT_INIT(&query->sendevent, sizeof(query->sendevent), 0, NULL, 2003 ISC_SOCKEVENT_SENDDONE, resquery_senddone, query, 2004 NULL, NULL, NULL); 2005 result = isc_socket_sendto2(socket, &r, task, address, NULL, 2006 &query->sendevent, 0); 2007 if (result != ISC_R_SUCCESS) { 2008 if (connecting) { 2009 /* 2010 * This query is still connecting. 2011 * Mark it as canceled so that it will just be 2012 * cleaned up when the connected event is received. 2013 * Keep fctx around until the event is processed. 2014 */ 2015 query->fctx->nqueries++; 2016 query->attributes |= RESQUERY_ATTR_CANCELED; 2017 } 2018 goto cleanup_message; 2019 } 2020 2021 query->sends++; 2022 2023 QTRACE("sent"); 2024 2025 return (ISC_R_SUCCESS); 2026 2027 cleanup_message: 2028 if (cleanup_cctx) 2029 dns_compress_invalidate(&cctx); 2030 2031 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER); 2032 2033 /* 2034 * Stop the dispatcher from listening. 2035 */ 2036 dns_dispatch_removeresponse(&query->dispentry, NULL); 2037 2038 cleanup_temps: 2039 if (qname != NULL) 2040 dns_message_puttempname(fctx->qmessage, &qname); 2041 if (qrdataset != NULL) 2042 dns_message_puttemprdataset(fctx->qmessage, &qrdataset); 2043 2044 return (result); 2045} 2046 2047static void 2048resquery_connected(isc_task_t *task, isc_event_t *event) { 2049 isc_socketevent_t *sevent = (isc_socketevent_t *)event; 2050 resquery_t *query = event->ev_arg; 2051 isc_boolean_t retry = ISC_FALSE; 2052 isc_interval_t interval; 2053 isc_result_t result; 2054 unsigned int attrs; 2055 fetchctx_t *fctx; 2056 2057 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT); 2058 REQUIRE(VALID_QUERY(query)); 2059 2060 QTRACE("connected"); 2061 2062 UNUSED(task); 2063 2064 /* 2065 * XXXRTH 2066 * 2067 * Currently we don't wait for the connect event before retrying 2068 * a query. This means that if we get really behind, we may end 2069 * up doing extra work! 2070 */ 2071 2072 query->connects--; 2073 fctx = query->fctx; 2074 2075 if (RESQUERY_CANCELED(query)) { 2076 /* 2077 * This query was canceled while the connect() was in 2078 * progress. 2079 */ 2080 isc_socket_detach(&query->tcpsocket); 2081 resquery_destroy(&query); 2082 } else { 2083 switch (sevent->result) { 2084 case ISC_R_SUCCESS: 2085 2086 /* 2087 * Extend the idle timer for TCP. 20 seconds 2088 * should be long enough for a TCP connection to be 2089 * established, a single DNS request to be sent, 2090 * and the response received. 2091 */ 2092 isc_interval_set(&interval, 20, 0); 2093 result = fctx_startidletimer(query->fctx, &interval); 2094 if (result != ISC_R_SUCCESS) { 2095 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE); 2096 fctx_done(fctx, result, __LINE__); 2097 break; 2098 } 2099 /* 2100 * We are connected. Create a dispatcher and 2101 * send the query. 2102 */ 2103 attrs = 0; 2104 attrs |= DNS_DISPATCHATTR_TCP; 2105 attrs |= DNS_DISPATCHATTR_PRIVATE; 2106 attrs |= DNS_DISPATCHATTR_CONNECTED; 2107 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == 2108 AF_INET) 2109 attrs |= DNS_DISPATCHATTR_IPV4; 2110 else 2111 attrs |= DNS_DISPATCHATTR_IPV6; 2112 attrs |= DNS_DISPATCHATTR_MAKEQUERY; 2113 2114 result = dns_dispatch_createtcp(query->dispatchmgr, 2115 query->tcpsocket, 2116 query->fctx->res->taskmgr, 2117 4096, 2, 1, 1, 3, attrs, 2118 &query->dispatch); 2119 2120 /* 2121 * Regardless of whether dns_dispatch_create() 2122 * succeeded or not, we don't need our reference 2123 * to the socket anymore. 2124 */ 2125 isc_socket_detach(&query->tcpsocket); 2126 2127 if (result == ISC_R_SUCCESS) 2128 result = resquery_send(query); 2129 2130 if (result != ISC_R_SUCCESS) { 2131 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE); 2132 fctx_done(fctx, result, __LINE__); 2133 } 2134 break; 2135 2136 case ISC_R_NETUNREACH: 2137 case ISC_R_HOSTUNREACH: 2138 case ISC_R_CONNREFUSED: 2139 case ISC_R_NOPERM: 2140 case ISC_R_ADDRNOTAVAIL: 2141 case ISC_R_CONNECTIONRESET: 2142 /* 2143 * No route to remote. 2144 */ 2145 isc_socket_detach(&query->tcpsocket); 2146 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE); 2147 retry = ISC_TRUE; 2148 break; 2149 2150 default: 2151 isc_socket_detach(&query->tcpsocket); 2152 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE); 2153 break; 2154 } 2155 } 2156 2157 isc_event_free(&event); 2158 2159 if (retry) { 2160 /* 2161 * Behave as if the idle timer has expired. For TCP 2162 * connections this may not actually reflect the latest timer. 2163 */ 2164 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT; 2165 result = fctx_stopidletimer(fctx); 2166 if (result != ISC_R_SUCCESS) 2167 fctx_done(fctx, result, __LINE__); 2168 else 2169 fctx_try(fctx, ISC_TRUE, ISC_FALSE); 2170 } 2171} 2172 2173static void 2174fctx_finddone(isc_task_t *task, isc_event_t *event) { 2175 fetchctx_t *fctx; 2176 dns_adbfind_t *find; 2177 dns_resolver_t *res; 2178 isc_boolean_t want_try = ISC_FALSE; 2179 isc_boolean_t want_done = ISC_FALSE; 2180 isc_boolean_t bucket_empty = ISC_FALSE; 2181 unsigned int bucketnum; 2182 isc_boolean_t destroy = ISC_FALSE; 2183 2184 find = event->ev_sender; 2185 fctx = event->ev_arg; 2186 REQUIRE(VALID_FCTX(fctx)); 2187 res = fctx->res; 2188 2189 UNUSED(task); 2190 2191 FCTXTRACE("finddone"); 2192 2193 bucketnum = fctx->bucketnum; 2194 LOCK(&res->buckets[bucketnum].lock); 2195 2196 INSIST(fctx->pending > 0); 2197 fctx->pending--; 2198 2199 if (ADDRWAIT(fctx)) { 2200 /* 2201 * The fetch is waiting for a name to be found. 2202 */ 2203 INSIST(!SHUTTINGDOWN(fctx)); 2204 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT; 2205 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES) { 2206 want_try = ISC_TRUE; 2207 fctx->totalqueries += find->qtotal; 2208 } else { 2209 fctx->findfail++; 2210 if (fctx->pending == 0) { 2211 /* 2212 * We've got nothing else to wait for and don't 2213 * know the answer. There's nothing to do but 2214 * fail the fctx. 2215 */ 2216 want_done = ISC_TRUE; 2217 } 2218 } 2219 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 && 2220 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) { 2221 2222 if (fctx->references == 0) { 2223 bucket_empty = fctx_unlink(fctx); 2224 destroy = ISC_TRUE; 2225 } 2226 } 2227 UNLOCK(&res->buckets[bucketnum].lock); 2228 2229 isc_event_free(&event); 2230 dns_adb_destroyfind(&find); 2231 2232 if (want_try) 2233 fctx_try(fctx, ISC_TRUE, ISC_FALSE); 2234 else if (want_done) 2235 fctx_done(fctx, ISC_R_FAILURE, __LINE__); 2236 else if (destroy) { 2237 fctx_destroy(fctx); 2238 if (bucket_empty) 2239 empty_bucket(res); 2240 } 2241} 2242 2243 2244static inline isc_boolean_t 2245bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) { 2246 isc_sockaddr_t *sa; 2247 2248 for (sa = ISC_LIST_HEAD(fctx->bad); 2249 sa != NULL; 2250 sa = ISC_LIST_NEXT(sa, link)) { 2251 if (isc_sockaddr_equal(sa, address)) 2252 return (ISC_TRUE); 2253 } 2254 2255 return (ISC_FALSE); 2256} 2257 2258static inline isc_boolean_t 2259mark_bad(fetchctx_t *fctx) { 2260 dns_adbfind_t *curr; 2261 dns_adbaddrinfo_t *addrinfo; 2262 isc_boolean_t all_bad = ISC_TRUE; 2263 2264 /* 2265 * Mark all known bad servers, so we don't try to talk to them 2266 * again. 2267 */ 2268 2269 /* 2270 * Mark any bad nameservers. 2271 */ 2272 for (curr = ISC_LIST_HEAD(fctx->finds); 2273 curr != NULL; 2274 curr = ISC_LIST_NEXT(curr, publink)) { 2275 for (addrinfo = ISC_LIST_HEAD(curr->list); 2276 addrinfo != NULL; 2277 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2278 if (bad_server(fctx, &addrinfo->sockaddr)) 2279 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2280 else 2281 all_bad = ISC_FALSE; 2282 } 2283 } 2284 2285 /* 2286 * Mark any bad forwarders. 2287 */ 2288 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs); 2289 addrinfo != NULL; 2290 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2291 if (bad_server(fctx, &addrinfo->sockaddr)) 2292 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2293 else 2294 all_bad = ISC_FALSE; 2295 } 2296 2297 /* 2298 * Mark any bad alternates. 2299 */ 2300 for (curr = ISC_LIST_HEAD(fctx->altfinds); 2301 curr != NULL; 2302 curr = ISC_LIST_NEXT(curr, publink)) { 2303 for (addrinfo = ISC_LIST_HEAD(curr->list); 2304 addrinfo != NULL; 2305 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2306 if (bad_server(fctx, &addrinfo->sockaddr)) 2307 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2308 else 2309 all_bad = ISC_FALSE; 2310 } 2311 } 2312 2313 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs); 2314 addrinfo != NULL; 2315 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2316 if (bad_server(fctx, &addrinfo->sockaddr)) 2317 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2318 else 2319 all_bad = ISC_FALSE; 2320 } 2321 2322 return (all_bad); 2323} 2324 2325static void 2326add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason, 2327 badnstype_t badtype) 2328{ 2329 char namebuf[DNS_NAME_FORMATSIZE]; 2330 char addrbuf[ISC_SOCKADDR_FORMATSIZE]; 2331 char classbuf[64]; 2332 char typebuf[64]; 2333 char code[64]; 2334 isc_buffer_t b; 2335 isc_sockaddr_t *sa; 2336 const char *spc = ""; 2337 isc_sockaddr_t *address = &addrinfo->sockaddr; 2338 2339 if (reason == DNS_R_LAME) 2340 fctx->lamecount++; 2341 else { 2342 switch (badtype) { 2343 case badns_unreachable: 2344 fctx->neterr++; 2345 break; 2346 case badns_response: 2347 fctx->badresp++; 2348 break; 2349 case badns_validation: 2350 break; /* counted as 'valfail' */ 2351 } 2352 } 2353 2354 if (bad_server(fctx, address)) { 2355 /* 2356 * We already know this server is bad. 2357 */ 2358 return; 2359 } 2360 2361 FCTXTRACE("add_bad"); 2362 2363 sa = isc_mem_get(fctx->mctx, sizeof(*sa)); 2364 if (sa == NULL) 2365 return; 2366 *sa = *address; 2367 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link); 2368 2369 if (reason == DNS_R_LAME) /* already logged */ 2370 return; 2371 2372 if (reason == DNS_R_UNEXPECTEDRCODE && 2373 fctx->rmessage->rcode == dns_rcode_servfail && 2374 ISFORWARDER(addrinfo)) 2375 return; 2376 2377 if (reason == DNS_R_UNEXPECTEDRCODE) { 2378 isc_buffer_init(&b, code, sizeof(code) - 1); 2379 dns_rcode_totext(fctx->rmessage->rcode, &b); 2380 code[isc_buffer_usedlength(&b)] = '\0'; 2381 spc = " "; 2382 } else if (reason == DNS_R_UNEXPECTEDOPCODE) { 2383 isc_buffer_init(&b, code, sizeof(code) - 1); 2384 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b); 2385 code[isc_buffer_usedlength(&b)] = '\0'; 2386 spc = " "; 2387 } else { 2388 code[0] = '\0'; 2389 } 2390 dns_name_format(&fctx->name, namebuf, sizeof(namebuf)); 2391 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf)); 2392 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf)); 2393 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf)); 2394 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS, 2395 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO, 2396 "error (%s%s%s) resolving '%s/%s/%s': %s", 2397 dns_result_totext(reason), spc, code, 2398 namebuf, typebuf, classbuf, addrbuf); 2399} 2400 2401/* 2402 * Sort addrinfo list by RTT. 2403 */ 2404static void 2405sort_adbfind(dns_adbfind_t *find) { 2406 dns_adbaddrinfo_t *best, *curr; 2407 dns_adbaddrinfolist_t sorted; 2408 2409 /* Lame N^2 bubble sort. */ 2410 ISC_LIST_INIT(sorted); 2411 while (!ISC_LIST_EMPTY(find->list)) { 2412 best = ISC_LIST_HEAD(find->list); 2413 curr = ISC_LIST_NEXT(best, publink); 2414 while (curr != NULL) { 2415 if (curr->srtt < best->srtt) 2416 best = curr; 2417 curr = ISC_LIST_NEXT(curr, publink); 2418 } 2419 ISC_LIST_UNLINK(find->list, best, publink); 2420 ISC_LIST_APPEND(sorted, best, publink); 2421 } 2422 find->list = sorted; 2423} 2424 2425/* 2426 * Sort a list of finds by server RTT. 2427 */ 2428static void 2429sort_finds(dns_adbfindlist_t *findlist) { 2430 dns_adbfind_t *best, *curr; 2431 dns_adbfindlist_t sorted; 2432 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo; 2433 2434 /* Sort each find's addrinfo list by SRTT. */ 2435 for (curr = ISC_LIST_HEAD(*findlist); 2436 curr != NULL; 2437 curr = ISC_LIST_NEXT(curr, publink)) 2438 sort_adbfind(curr); 2439 2440 /* Lame N^2 bubble sort. */ 2441 ISC_LIST_INIT(sorted); 2442 while (!ISC_LIST_EMPTY(*findlist)) { 2443 best = ISC_LIST_HEAD(*findlist); 2444 bestaddrinfo = ISC_LIST_HEAD(best->list); 2445 INSIST(bestaddrinfo != NULL); 2446 curr = ISC_LIST_NEXT(best, publink); 2447 while (curr != NULL) { 2448 addrinfo = ISC_LIST_HEAD(curr->list); 2449 INSIST(addrinfo != NULL); 2450 if (addrinfo->srtt < bestaddrinfo->srtt) { 2451 best = curr; 2452 bestaddrinfo = addrinfo; 2453 } 2454 curr = ISC_LIST_NEXT(curr, publink); 2455 } 2456 ISC_LIST_UNLINK(*findlist, best, publink); 2457 ISC_LIST_APPEND(sorted, best, publink); 2458 } 2459 *findlist = sorted; 2460} 2461 2462static void 2463findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port, 2464 unsigned int options, unsigned int flags, isc_stdtime_t now, 2465 isc_boolean_t *need_alternate) 2466{ 2467 dns_adbaddrinfo_t *ai; 2468 dns_adbfind_t *find; 2469 dns_resolver_t *res; 2470 isc_boolean_t unshared; 2471 isc_result_t result; 2472 2473 res = fctx->res; 2474 unshared = ISC_TF((fctx->options & DNS_FETCHOPT_UNSHARED) != 0); 2475 /* 2476 * If this name is a subdomain of the query domain, tell 2477 * the ADB to start looking using zone/hint data. This keeps us 2478 * from getting stuck if the nameserver is beneath the zone cut 2479 * and we don't know its address (e.g. because the A record has 2480 * expired). 2481 */ 2482 if (dns_name_issubdomain(name, &fctx->domain)) 2483 options |= DNS_ADBFIND_STARTATZONE; 2484 options |= DNS_ADBFIND_GLUEOK; 2485 options |= DNS_ADBFIND_HINTOK; 2486 2487 /* 2488 * See what we know about this address. 2489 */ 2490 find = NULL; 2491 result = dns_adb_createfind2(fctx->adb, 2492 res->buckets[fctx->bucketnum].task, 2493 fctx_finddone, fctx, name, 2494 &fctx->name, fctx->type, 2495 options, now, NULL, 2496 res->view->dstport, 2497 fctx->depth + 1, &find); 2498 if (result != ISC_R_SUCCESS) { 2499 if (result == DNS_R_ALIAS) { 2500 /* 2501 * XXXRTH Follow the CNAME/DNAME chain? 2502 */ 2503 dns_adb_destroyfind(&find); 2504 fctx->adberr++; 2505 } 2506 } else if (!ISC_LIST_EMPTY(find->list)) { 2507 /* 2508 * We have at least some of the addresses for the 2509 * name. 2510 */ 2511 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0); 2512 if (flags != 0 || port != 0) { 2513 for (ai = ISC_LIST_HEAD(find->list); 2514 ai != NULL; 2515 ai = ISC_LIST_NEXT(ai, publink)) { 2516 ai->flags |= flags; 2517 if (port != 0) 2518 isc_sockaddr_setport(&ai->sockaddr, 2519 port); 2520 } 2521 } 2522 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0) 2523 ISC_LIST_APPEND(fctx->altfinds, find, publink); 2524 else 2525 ISC_LIST_APPEND(fctx->finds, find, publink); 2526 } else { 2527 /* 2528 * We don't know any of the addresses for this 2529 * name. 2530 */ 2531 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) { 2532 /* 2533 * We're looking for them and will get an 2534 * event about it later. 2535 */ 2536 fctx->pending++; 2537 /* 2538 * Bootstrap. 2539 */ 2540 if (need_alternate != NULL && 2541 !*need_alternate && unshared && 2542 ((res->dispatches4 == NULL && 2543 find->result_v6 != DNS_R_NXDOMAIN) || 2544 (res->dispatches6 == NULL && 2545 find->result_v4 != DNS_R_NXDOMAIN))) 2546 *need_alternate = ISC_TRUE; 2547 } else { 2548 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0) 2549 fctx->lamecount++; /* cached lame server */ 2550 else 2551 fctx->adberr++; /* unreachable server, etc. */ 2552 2553 /* 2554 * If we know there are no addresses for 2555 * the family we are using then try to add 2556 * an alternative server. 2557 */ 2558 if (need_alternate != NULL && !*need_alternate && 2559 ((res->dispatches4 == NULL && 2560 find->result_v6 == DNS_R_NXRRSET) || 2561 (res->dispatches6 == NULL && 2562 find->result_v4 == DNS_R_NXRRSET))) 2563 *need_alternate = ISC_TRUE; 2564 dns_adb_destroyfind(&find); 2565 } 2566 } 2567} 2568 2569static isc_boolean_t 2570isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) { 2571 int order; 2572 unsigned int nlabels; 2573 dns_namereln_t namereln; 2574 2575 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels); 2576 return (ISC_TF(namereln == dns_namereln_subdomain)); 2577} 2578 2579static isc_result_t 2580fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) { 2581 dns_rdata_t rdata = DNS_RDATA_INIT; 2582 isc_result_t result; 2583 dns_resolver_t *res; 2584 isc_stdtime_t now; 2585 unsigned int stdoptions = 0; 2586 isc_sockaddr_t *sa; 2587 dns_adbaddrinfo_t *ai; 2588 isc_boolean_t all_bad; 2589 dns_rdata_ns_t ns; 2590 isc_boolean_t need_alternate = ISC_FALSE; 2591 2592 FCTXTRACE("getaddresses"); 2593 2594 /* 2595 * Don't pound on remote servers. (Failsafe!) 2596 */ 2597 fctx->restarts++; 2598 if (fctx->restarts > 10) { 2599 FCTXTRACE("too many restarts"); 2600 return (DNS_R_SERVFAIL); 2601 } 2602 2603 res = fctx->res; 2604 2605 if (fctx->depth > res->maxdepth) { 2606 FCTXTRACE("too much NS indirection"); 2607 return (DNS_R_SERVFAIL); 2608 } 2609 2610 /* 2611 * Forwarders. 2612 */ 2613 2614 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs)); 2615 INSIST(ISC_LIST_EMPTY(fctx->altaddrs)); 2616 2617 /* 2618 * If this fctx has forwarders, use them; otherwise use any 2619 * selective forwarders specified in the view; otherwise use the 2620 * resolver's forwarders (if any). 2621 */ 2622 sa = ISC_LIST_HEAD(fctx->forwarders); 2623 if (sa == NULL) { 2624 dns_forwarders_t *forwarders = NULL; 2625 dns_name_t *name = &fctx->name; 2626 dns_name_t suffix; 2627 unsigned int labels; 2628 dns_fixedname_t fixed; 2629 dns_name_t *domain; 2630 2631 /* 2632 * DS records are found in the parent server. 2633 * Strip label to get the correct forwarder (if any). 2634 */ 2635 if (dns_rdatatype_atparent(fctx->type) && 2636 dns_name_countlabels(name) > 1) { 2637 dns_name_init(&suffix, NULL); 2638 labels = dns_name_countlabels(name); 2639 dns_name_getlabelsequence(name, 1, labels - 1, &suffix); 2640 name = &suffix; 2641 } 2642 2643 dns_fixedname_init(&fixed); 2644 domain = dns_fixedname_name(&fixed); 2645 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name, 2646 domain, &forwarders); 2647 if (result == ISC_R_SUCCESS) { 2648 sa = ISC_LIST_HEAD(forwarders->addrs); 2649 fctx->fwdpolicy = forwarders->fwdpolicy; 2650 if (fctx->fwdpolicy == dns_fwdpolicy_only && 2651 isstrictsubdomain(domain, &fctx->domain)) { 2652 dns_name_free(&fctx->domain, fctx->mctx); 2653 dns_name_init(&fctx->domain, NULL); 2654 result = dns_name_dup(domain, fctx->mctx, 2655 &fctx->domain); 2656 if (result != ISC_R_SUCCESS) 2657 return (result); 2658 } 2659 } 2660 } 2661 2662 while (sa != NULL) { 2663 if ((isc_sockaddr_pf(sa) == AF_INET && 2664 fctx->res->dispatches4 == NULL) || 2665 (isc_sockaddr_pf(sa) == AF_INET6 && 2666 fctx->res->dispatches6 == NULL)) { 2667 sa = ISC_LIST_NEXT(sa, link); 2668 continue; 2669 } 2670 ai = NULL; 2671 result = dns_adb_findaddrinfo(fctx->adb, 2672 sa, &ai, 0); /* XXXMLG */ 2673 if (result == ISC_R_SUCCESS) { 2674 dns_adbaddrinfo_t *cur; 2675 ai->flags |= FCTX_ADDRINFO_FORWARDER; 2676 cur = ISC_LIST_HEAD(fctx->forwaddrs); 2677 while (cur != NULL && cur->srtt < ai->srtt) 2678 cur = ISC_LIST_NEXT(cur, publink); 2679 if (cur != NULL) 2680 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur, 2681 ai, publink); 2682 else 2683 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink); 2684 } 2685 sa = ISC_LIST_NEXT(sa, link); 2686 } 2687 2688 /* 2689 * If the forwarding policy is "only", we don't need the addresses 2690 * of the nameservers. 2691 */ 2692 if (fctx->fwdpolicy == dns_fwdpolicy_only) 2693 goto out; 2694 2695 /* 2696 * Normal nameservers. 2697 */ 2698 2699 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT; 2700 if (fctx->restarts == 1) { 2701 /* 2702 * To avoid sending out a flood of queries likely to 2703 * result in NXRRSET, we suppress fetches for address 2704 * families we don't have the first time through, 2705 * provided that we have addresses in some family we 2706 * can use. 2707 * 2708 * We don't want to set this option all the time, since 2709 * if fctx->restarts > 1, we've clearly been having trouble 2710 * with the addresses we had, so getting more could help. 2711 */ 2712 stdoptions |= DNS_ADBFIND_AVOIDFETCHES; 2713 } 2714 if (res->dispatches4 != NULL) 2715 stdoptions |= DNS_ADBFIND_INET; 2716 if (res->dispatches6 != NULL) 2717 stdoptions |= DNS_ADBFIND_INET6; 2718 isc_stdtime_get(&now); 2719 2720 INSIST(ISC_LIST_EMPTY(fctx->finds)); 2721 INSIST(ISC_LIST_EMPTY(fctx->altfinds)); 2722 2723 for (result = dns_rdataset_first(&fctx->nameservers); 2724 result == ISC_R_SUCCESS; 2725 result = dns_rdataset_next(&fctx->nameservers)) 2726 { 2727 dns_rdataset_current(&fctx->nameservers, &rdata); 2728 /* 2729 * Extract the name from the NS record. 2730 */ 2731 result = dns_rdata_tostruct(&rdata, &ns, NULL); 2732 if (result != ISC_R_SUCCESS) 2733 continue; 2734 2735 findname(fctx, &ns.name, 0, stdoptions, 0, now, 2736 &need_alternate); 2737 dns_rdata_reset(&rdata); 2738 dns_rdata_freestruct(&ns); 2739 } 2740 if (result != ISC_R_NOMORE) 2741 return (result); 2742 2743 /* 2744 * Do we need to use 6 to 4? 2745 */ 2746 if (need_alternate) { 2747 int family; 2748 alternate_t *a; 2749 family = (res->dispatches6 != NULL) ? AF_INET6 : AF_INET; 2750 for (a = ISC_LIST_HEAD(fctx->res->alternates); 2751 a != NULL; 2752 a = ISC_LIST_NEXT(a, link)) { 2753 if (!a->isaddress) { 2754 findname(fctx, &a->_u._n.name, a->_u._n.port, 2755 stdoptions, FCTX_ADDRINFO_FORWARDER, 2756 now, NULL); 2757 continue; 2758 } 2759 if (isc_sockaddr_pf(&a->_u.addr) != family) 2760 continue; 2761 ai = NULL; 2762 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr, 2763 &ai, 0); 2764 if (result == ISC_R_SUCCESS) { 2765 dns_adbaddrinfo_t *cur; 2766 ai->flags |= FCTX_ADDRINFO_FORWARDER; 2767 cur = ISC_LIST_HEAD(fctx->altaddrs); 2768 while (cur != NULL && cur->srtt < ai->srtt) 2769 cur = ISC_LIST_NEXT(cur, publink); 2770 if (cur != NULL) 2771 ISC_LIST_INSERTBEFORE(fctx->altaddrs, 2772 cur, ai, publink); 2773 else 2774 ISC_LIST_APPEND(fctx->altaddrs, ai, 2775 publink); 2776 } 2777 } 2778 } 2779 2780 out: 2781 /* 2782 * Mark all known bad servers. 2783 */ 2784 all_bad = mark_bad(fctx); 2785 2786 /* 2787 * How are we doing? 2788 */ 2789 if (all_bad) { 2790 /* 2791 * We've got no addresses. 2792 */ 2793 if (fctx->pending > 0) { 2794 /* 2795 * We're fetching the addresses, but don't have any 2796 * yet. Tell the caller to wait for an answer. 2797 */ 2798 result = DNS_R_WAIT; 2799 } else { 2800 isc_time_t expire; 2801 isc_interval_t i; 2802 /* 2803 * We've lost completely. We don't know any 2804 * addresses, and the ADB has told us it can't get 2805 * them. 2806 */ 2807 FCTXTRACE("no addresses"); 2808 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0); 2809 result = isc_time_nowplusinterval(&expire, &i); 2810 if (badcache && 2811 (fctx->type == dns_rdatatype_dnskey || 2812 fctx->type == dns_rdatatype_dlv || 2813 fctx->type == dns_rdatatype_ds) && 2814 result == ISC_R_SUCCESS) 2815 dns_resolver_addbadcache(fctx->res, 2816 &fctx->name, 2817 fctx->type, &expire); 2818 result = ISC_R_FAILURE; 2819 } 2820 } else { 2821 /* 2822 * We've found some addresses. We might still be looking 2823 * for more addresses. 2824 */ 2825 sort_finds(&fctx->finds); 2826 sort_finds(&fctx->altfinds); 2827 result = ISC_R_SUCCESS; 2828 } 2829 2830 return (result); 2831} 2832 2833static inline void 2834possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr) 2835{ 2836 isc_netaddr_t na; 2837 char buf[ISC_NETADDR_FORMATSIZE]; 2838 isc_sockaddr_t *sa; 2839 isc_boolean_t aborted = ISC_FALSE; 2840 isc_boolean_t bogus; 2841 dns_acl_t *blackhole; 2842 isc_netaddr_t ipaddr; 2843 dns_peer_t *peer = NULL; 2844 dns_resolver_t *res; 2845 const char *msg = NULL; 2846 2847 sa = &addr->sockaddr; 2848 2849 res = fctx->res; 2850 isc_netaddr_fromsockaddr(&ipaddr, sa); 2851 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr); 2852 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer); 2853 2854 if (blackhole != NULL) { 2855 int match; 2856 2857 if (dns_acl_match(&ipaddr, NULL, blackhole, 2858 &res->view->aclenv, 2859 &match, NULL) == ISC_R_SUCCESS && 2860 match > 0) 2861 aborted = ISC_TRUE; 2862 } 2863 2864 if (peer != NULL && 2865 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS && 2866 bogus) 2867 aborted = ISC_TRUE; 2868 2869 if (aborted) { 2870 addr->flags |= FCTX_ADDRINFO_MARK; 2871 msg = "ignoring blackholed / bogus server: "; 2872 } else if (isc_sockaddr_ismulticast(sa)) { 2873 addr->flags |= FCTX_ADDRINFO_MARK; 2874 msg = "ignoring multicast address: "; 2875 } else if (isc_sockaddr_isexperimental(sa)) { 2876 addr->flags |= FCTX_ADDRINFO_MARK; 2877 msg = "ignoring experimental address: "; 2878 } else if (sa->type.sa.sa_family != AF_INET6) { 2879 return; 2880 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) { 2881 addr->flags |= FCTX_ADDRINFO_MARK; 2882 msg = "ignoring IPv6 mapped IPV4 address: "; 2883 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) { 2884 addr->flags |= FCTX_ADDRINFO_MARK; 2885 msg = "ignoring IPv6 compatibility IPV4 address: "; 2886 } else 2887 return; 2888 2889 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) 2890 return; 2891 2892 isc_netaddr_fromsockaddr(&na, sa); 2893 isc_netaddr_format(&na, buf, sizeof(buf)); 2894 FCTXTRACE2(msg, buf); 2895} 2896 2897static inline dns_adbaddrinfo_t * 2898fctx_nextaddress(fetchctx_t *fctx) { 2899 dns_adbfind_t *find, *start; 2900 dns_adbaddrinfo_t *addrinfo; 2901 dns_adbaddrinfo_t *faddrinfo; 2902 2903 /* 2904 * Return the next untried address, if any. 2905 */ 2906 2907 /* 2908 * Find the first unmarked forwarder (if any). 2909 */ 2910 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs); 2911 addrinfo != NULL; 2912 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2913 if (!UNMARKED(addrinfo)) 2914 continue; 2915 possibly_mark(fctx, addrinfo); 2916 if (UNMARKED(addrinfo)) { 2917 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2918 fctx->find = NULL; 2919 return (addrinfo); 2920 } 2921 } 2922 2923 /* 2924 * No forwarders. Move to the next find. 2925 */ 2926 2927 fctx->attributes |= FCTX_ATTR_TRIEDFIND; 2928 2929 find = fctx->find; 2930 if (find == NULL) 2931 find = ISC_LIST_HEAD(fctx->finds); 2932 else { 2933 find = ISC_LIST_NEXT(find, publink); 2934 if (find == NULL) 2935 find = ISC_LIST_HEAD(fctx->finds); 2936 } 2937 2938 /* 2939 * Find the first unmarked addrinfo. 2940 */ 2941 addrinfo = NULL; 2942 if (find != NULL) { 2943 start = find; 2944 do { 2945 for (addrinfo = ISC_LIST_HEAD(find->list); 2946 addrinfo != NULL; 2947 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2948 if (!UNMARKED(addrinfo)) 2949 continue; 2950 possibly_mark(fctx, addrinfo); 2951 if (UNMARKED(addrinfo)) { 2952 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2953 break; 2954 } 2955 } 2956 if (addrinfo != NULL) 2957 break; 2958 find = ISC_LIST_NEXT(find, publink); 2959 if (find == NULL) 2960 find = ISC_LIST_HEAD(fctx->finds); 2961 } while (find != start); 2962 } 2963 2964 fctx->find = find; 2965 if (addrinfo != NULL) 2966 return (addrinfo); 2967 2968 /* 2969 * No nameservers left. Try alternates. 2970 */ 2971 2972 fctx->attributes |= FCTX_ATTR_TRIEDALT; 2973 2974 find = fctx->altfind; 2975 if (find == NULL) 2976 find = ISC_LIST_HEAD(fctx->altfinds); 2977 else { 2978 find = ISC_LIST_NEXT(find, publink); 2979 if (find == NULL) 2980 find = ISC_LIST_HEAD(fctx->altfinds); 2981 } 2982 2983 /* 2984 * Find the first unmarked addrinfo. 2985 */ 2986 addrinfo = NULL; 2987 if (find != NULL) { 2988 start = find; 2989 do { 2990 for (addrinfo = ISC_LIST_HEAD(find->list); 2991 addrinfo != NULL; 2992 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 2993 if (!UNMARKED(addrinfo)) 2994 continue; 2995 possibly_mark(fctx, addrinfo); 2996 if (UNMARKED(addrinfo)) { 2997 addrinfo->flags |= FCTX_ADDRINFO_MARK; 2998 break; 2999 } 3000 } 3001 if (addrinfo != NULL) 3002 break; 3003 find = ISC_LIST_NEXT(find, publink); 3004 if (find == NULL) 3005 find = ISC_LIST_HEAD(fctx->altfinds); 3006 } while (find != start); 3007 } 3008 3009 faddrinfo = addrinfo; 3010 3011 /* 3012 * See if we have a better alternate server by address. 3013 */ 3014 3015 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs); 3016 addrinfo != NULL; 3017 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) { 3018 if (!UNMARKED(addrinfo)) 3019 continue; 3020 possibly_mark(fctx, addrinfo); 3021 if (UNMARKED(addrinfo) && 3022 (faddrinfo == NULL || 3023 addrinfo->srtt < faddrinfo->srtt)) { 3024 if (faddrinfo != NULL) 3025 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK; 3026 addrinfo->flags |= FCTX_ADDRINFO_MARK; 3027 break; 3028 } 3029 } 3030 3031 if (addrinfo == NULL) { 3032 addrinfo = faddrinfo; 3033 fctx->altfind = find; 3034 } 3035 3036 return (addrinfo); 3037} 3038 3039static void 3040fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) { 3041 isc_result_t result; 3042 dns_adbaddrinfo_t *addrinfo; 3043 3044 FCTXTRACE("try"); 3045 3046 REQUIRE(!ADDRWAIT(fctx)); 3047 3048 if (fctx->totalqueries > DEFAULT_MAX_QUERIES) 3049 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 3050 3051 addrinfo = fctx_nextaddress(fctx); 3052 if (addrinfo == NULL) { 3053 /* 3054 * We have no more addresses. Start over. 3055 */ 3056 fctx_cancelqueries(fctx, ISC_TRUE); 3057 fctx_cleanupfinds(fctx); 3058 fctx_cleanupaltfinds(fctx); 3059 fctx_cleanupforwaddrs(fctx); 3060 fctx_cleanupaltaddrs(fctx); 3061 result = fctx_getaddresses(fctx, badcache); 3062 if (result == DNS_R_WAIT) { 3063 /* 3064 * Sleep waiting for addresses. 3065 */ 3066 FCTXTRACE("addrwait"); 3067 fctx->attributes |= FCTX_ATTR_ADDRWAIT; 3068 return; 3069 } else if (result != ISC_R_SUCCESS) { 3070 /* 3071 * Something bad happened. 3072 */ 3073 fctx_done(fctx, result, __LINE__); 3074 return; 3075 } 3076 3077 addrinfo = fctx_nextaddress(fctx); 3078 /* 3079 * While we may have addresses from the ADB, they 3080 * might be bad ones. In this case, return SERVFAIL. 3081 */ 3082 if (addrinfo == NULL) { 3083 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 3084 return; 3085 } 3086 } 3087 3088 result = fctx_query(fctx, addrinfo, fctx->options); 3089 if (result != ISC_R_SUCCESS) 3090 fctx_done(fctx, result, __LINE__); 3091 else if (retrying) 3092 inc_stats(fctx->res, dns_resstatscounter_retry); 3093} 3094 3095static isc_boolean_t 3096fctx_unlink(fetchctx_t *fctx) { 3097 dns_resolver_t *res; 3098 unsigned int bucketnum; 3099 3100 /* 3101 * Caller must be holding the bucket lock. 3102 */ 3103 3104 REQUIRE(VALID_FCTX(fctx)); 3105 REQUIRE(fctx->state == fetchstate_done || 3106 fctx->state == fetchstate_init); 3107 REQUIRE(ISC_LIST_EMPTY(fctx->events)); 3108 REQUIRE(ISC_LIST_EMPTY(fctx->queries)); 3109 REQUIRE(ISC_LIST_EMPTY(fctx->finds)); 3110 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds)); 3111 REQUIRE(fctx->pending == 0); 3112 REQUIRE(fctx->references == 0); 3113 REQUIRE(ISC_LIST_EMPTY(fctx->validators)); 3114 3115 FCTXTRACE("unlink"); 3116 3117 res = fctx->res; 3118 bucketnum = fctx->bucketnum; 3119 3120 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link); 3121 3122 LOCK(&res->nlock); 3123 res->nfctx--; 3124 UNLOCK(&res->nlock); 3125 3126 if (res->buckets[bucketnum].exiting && 3127 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs)) 3128 return (ISC_TRUE); 3129 3130 return (ISC_FALSE); 3131} 3132 3133static void 3134fctx_destroy(fetchctx_t *fctx) { 3135 isc_sockaddr_t *sa, *next_sa; 3136 3137 REQUIRE(VALID_FCTX(fctx)); 3138 REQUIRE(fctx->state == fetchstate_done || 3139 fctx->state == fetchstate_init); 3140 REQUIRE(ISC_LIST_EMPTY(fctx->events)); 3141 REQUIRE(ISC_LIST_EMPTY(fctx->queries)); 3142 REQUIRE(ISC_LIST_EMPTY(fctx->finds)); 3143 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds)); 3144 REQUIRE(fctx->pending == 0); 3145 REQUIRE(fctx->references == 0); 3146 REQUIRE(ISC_LIST_EMPTY(fctx->validators)); 3147 REQUIRE(!ISC_LINK_LINKED(fctx, link)); 3148 3149 FCTXTRACE("destroy"); 3150 3151 /* 3152 * Free bad. 3153 */ 3154 for (sa = ISC_LIST_HEAD(fctx->bad); 3155 sa != NULL; 3156 sa = next_sa) { 3157 next_sa = ISC_LIST_NEXT(sa, link); 3158 ISC_LIST_UNLINK(fctx->bad, sa, link); 3159 isc_mem_put(fctx->mctx, sa, sizeof(*sa)); 3160 } 3161 3162 for (sa = ISC_LIST_HEAD(fctx->edns); 3163 sa != NULL; 3164 sa = next_sa) { 3165 next_sa = ISC_LIST_NEXT(sa, link); 3166 ISC_LIST_UNLINK(fctx->edns, sa, link); 3167 isc_mem_put(fctx->mctx, sa, sizeof(*sa)); 3168 } 3169 3170 for (sa = ISC_LIST_HEAD(fctx->edns512); 3171 sa != NULL; 3172 sa = next_sa) { 3173 next_sa = ISC_LIST_NEXT(sa, link); 3174 ISC_LIST_UNLINK(fctx->edns512, sa, link); 3175 isc_mem_put(fctx->mctx, sa, sizeof(*sa)); 3176 } 3177 3178 for (sa = ISC_LIST_HEAD(fctx->bad_edns); 3179 sa != NULL; 3180 sa = next_sa) { 3181 next_sa = ISC_LIST_NEXT(sa, link); 3182 ISC_LIST_UNLINK(fctx->bad_edns, sa, link); 3183 isc_mem_put(fctx->mctx, sa, sizeof(*sa)); 3184 } 3185 3186 isc_timer_detach(&fctx->timer); 3187 dns_message_destroy(&fctx->rmessage); 3188 dns_message_destroy(&fctx->qmessage); 3189 if (dns_name_countlabels(&fctx->domain) > 0) 3190 dns_name_free(&fctx->domain, fctx->mctx); 3191 if (dns_rdataset_isassociated(&fctx->nameservers)) 3192 dns_rdataset_disassociate(&fctx->nameservers); 3193 dns_name_free(&fctx->name, fctx->mctx); 3194 dns_db_detach(&fctx->cache); 3195 dns_adb_detach(&fctx->adb); 3196 isc_mem_free(fctx->mctx, fctx->info); 3197 isc_mem_putanddetach(&fctx->mctx, fctx, sizeof(*fctx)); 3198} 3199 3200/* 3201 * Fetch event handlers. 3202 */ 3203 3204static void 3205fctx_timeout(isc_task_t *task, isc_event_t *event) { 3206 fetchctx_t *fctx = event->ev_arg; 3207 isc_timerevent_t *tevent = (isc_timerevent_t *)event; 3208 resquery_t *query; 3209 3210 REQUIRE(VALID_FCTX(fctx)); 3211 3212 UNUSED(task); 3213 3214 FCTXTRACE("timeout"); 3215 3216 inc_stats(fctx->res, dns_resstatscounter_querytimeout); 3217 3218 if (event->ev_type == ISC_TIMEREVENT_LIFE) { 3219 fctx->reason = NULL; 3220 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__); 3221 } else { 3222 isc_result_t result; 3223 3224 fctx->timeouts++; 3225 fctx->timeout = ISC_TRUE; 3226 /* 3227 * We could cancel the running queries here, or we could let 3228 * them keep going. Since we normally use separate sockets for 3229 * different queries, we adopt the former approach to reduce 3230 * the number of open sockets: cancel the oldest query if it 3231 * expired after the query had started (this is usually the 3232 * case but is not always so, depending on the task schedule 3233 * timing). 3234 */ 3235 query = ISC_LIST_HEAD(fctx->queries); 3236 if (query != NULL && 3237 isc_time_compare(&tevent->due, &query->start) >= 0) { 3238 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE); 3239 } 3240 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT; 3241 /* 3242 * Our timer has triggered. Reestablish the fctx lifetime 3243 * timer. 3244 */ 3245 result = fctx_starttimer(fctx); 3246 if (result != ISC_R_SUCCESS) 3247 fctx_done(fctx, result, __LINE__); 3248 else 3249 /* 3250 * Keep trying. 3251 */ 3252 fctx_try(fctx, ISC_TRUE, ISC_FALSE); 3253 } 3254 3255 isc_event_free(&event); 3256} 3257 3258static void 3259fctx_shutdown(fetchctx_t *fctx) { 3260 isc_event_t *cevent; 3261 3262 /* 3263 * Start the shutdown process for fctx, if it isn't already underway. 3264 */ 3265 3266 FCTXTRACE("shutdown"); 3267 3268 /* 3269 * The caller must be holding the appropriate bucket lock. 3270 */ 3271 3272 if (fctx->want_shutdown) 3273 return; 3274 3275 fctx->want_shutdown = ISC_TRUE; 3276 3277 /* 3278 * Unless we're still initializing (in which case the 3279 * control event is still outstanding), we need to post 3280 * the control event to tell the fetch we want it to 3281 * exit. 3282 */ 3283 if (fctx->state != fetchstate_init) { 3284 cevent = &fctx->control_event; 3285 isc_task_send(fctx->res->buckets[fctx->bucketnum].task, 3286 &cevent); 3287 } 3288} 3289 3290static void 3291fctx_doshutdown(isc_task_t *task, isc_event_t *event) { 3292 fetchctx_t *fctx = event->ev_arg; 3293 isc_boolean_t bucket_empty = ISC_FALSE; 3294 dns_resolver_t *res; 3295 unsigned int bucketnum; 3296 dns_validator_t *validator; 3297 isc_boolean_t destroy = ISC_FALSE; 3298 3299 REQUIRE(VALID_FCTX(fctx)); 3300 3301 UNUSED(task); 3302 3303 res = fctx->res; 3304 bucketnum = fctx->bucketnum; 3305 3306 FCTXTRACE("doshutdown"); 3307 3308 /* 3309 * An fctx that is shutting down is no longer in ADDRWAIT mode. 3310 */ 3311 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT; 3312 3313 /* 3314 * Cancel all pending validators. Note that this must be done 3315 * without the bucket lock held, since that could cause deadlock. 3316 */ 3317 validator = ISC_LIST_HEAD(fctx->validators); 3318 while (validator != NULL) { 3319 dns_validator_cancel(validator); 3320 validator = ISC_LIST_NEXT(validator, link); 3321 } 3322 3323 if (fctx->nsfetch != NULL) 3324 dns_resolver_cancelfetch(fctx->nsfetch); 3325 3326 /* 3327 * Shut down anything that is still running on behalf of this 3328 * fetch. To avoid deadlock with the ADB, we must do this 3329 * before we lock the bucket lock. 3330 */ 3331 fctx_stopeverything(fctx, ISC_FALSE); 3332 3333 LOCK(&res->buckets[bucketnum].lock); 3334 3335 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN; 3336 3337 INSIST(fctx->state == fetchstate_active || 3338 fctx->state == fetchstate_done); 3339 INSIST(fctx->want_shutdown); 3340 3341 if (fctx->state != fetchstate_done) { 3342 fctx->state = fetchstate_done; 3343 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__); 3344 } 3345 3346 if (fctx->references == 0 && fctx->pending == 0 && 3347 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) { 3348 bucket_empty = fctx_unlink(fctx); 3349 destroy = ISC_TRUE; 3350 } 3351 3352 UNLOCK(&res->buckets[bucketnum].lock); 3353 3354 if (destroy) { 3355 fctx_destroy(fctx); 3356 if (bucket_empty) 3357 empty_bucket(res); 3358 } 3359} 3360 3361static void 3362fctx_start(isc_task_t *task, isc_event_t *event) { 3363 fetchctx_t *fctx = event->ev_arg; 3364 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE; 3365 dns_resolver_t *res; 3366 unsigned int bucketnum; 3367 isc_boolean_t destroy = ISC_FALSE; 3368 3369 REQUIRE(VALID_FCTX(fctx)); 3370 3371 UNUSED(task); 3372 3373 res = fctx->res; 3374 bucketnum = fctx->bucketnum; 3375 3376 FCTXTRACE("start"); 3377 3378 LOCK(&res->buckets[bucketnum].lock); 3379 3380 INSIST(fctx->state == fetchstate_init); 3381 if (fctx->want_shutdown) { 3382 /* 3383 * We haven't started this fctx yet, and we've been requested 3384 * to shut it down. 3385 */ 3386 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN; 3387 fctx->state = fetchstate_done; 3388 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__); 3389 /* 3390 * Since we haven't started, we INSIST that we have no 3391 * pending ADB finds and no pending validations. 3392 */ 3393 INSIST(fctx->pending == 0); 3394 INSIST(fctx->nqueries == 0); 3395 INSIST(ISC_LIST_EMPTY(fctx->validators)); 3396 if (fctx->references == 0) { 3397 /* 3398 * It's now safe to destroy this fctx. 3399 */ 3400 bucket_empty = fctx_unlink(fctx); 3401 destroy = ISC_TRUE; 3402 } 3403 done = ISC_TRUE; 3404 } else { 3405 /* 3406 * Normal fctx startup. 3407 */ 3408 fctx->state = fetchstate_active; 3409 fctx->totalqueries = 0; 3410 /* 3411 * Reset the control event for later use in shutting down 3412 * the fctx. 3413 */ 3414 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL, 3415 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx, 3416 NULL, NULL, NULL); 3417 } 3418 3419 UNLOCK(&res->buckets[bucketnum].lock); 3420 3421 if (!done) { 3422 isc_result_t result; 3423 3424 INSIST(!destroy); 3425 3426 /* 3427 * All is well. Start working on the fetch. 3428 */ 3429 result = fctx_starttimer(fctx); 3430 if (result != ISC_R_SUCCESS) 3431 fctx_done(fctx, result, __LINE__); 3432 else 3433 fctx_try(fctx, ISC_FALSE, ISC_FALSE); 3434 } else if (destroy) { 3435 fctx_destroy(fctx); 3436 if (bucket_empty) 3437 empty_bucket(res); 3438 } 3439} 3440 3441/* 3442 * Fetch Creation, Joining, and Cancelation. 3443 */ 3444 3445static inline isc_result_t 3446fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client, 3447 dns_messageid_t id, isc_taskaction_t action, void *arg, 3448 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset, 3449 dns_fetch_t *fetch) 3450{ 3451 isc_task_t *clone; 3452 dns_fetchevent_t *event; 3453 3454 FCTXTRACE("join"); 3455 3456 /* 3457 * We store the task we're going to send this event to in the 3458 * sender field. We'll make the fetch the sender when we actually 3459 * send the event. 3460 */ 3461 clone = NULL; 3462 isc_task_attach(task, &clone); 3463 event = (dns_fetchevent_t *) 3464 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE, 3465 action, arg, sizeof(*event)); 3466 if (event == NULL) { 3467 isc_task_detach(&clone); 3468 return (ISC_R_NOMEMORY); 3469 } 3470 event->result = DNS_R_SERVFAIL; 3471 event->qtype = fctx->type; 3472 event->db = NULL; 3473 event->node = NULL; 3474 event->rdataset = rdataset; 3475 event->sigrdataset = sigrdataset; 3476 event->fetch = fetch; 3477 event->client = client; 3478 event->id = id; 3479 event->qtotal = 0; 3480 dns_fixedname_init(&event->foundname); 3481 3482 /* 3483 * Make sure that we can store the sigrdataset in the 3484 * first event if it is needed by any of the events. 3485 */ 3486 if (event->sigrdataset != NULL) 3487 ISC_LIST_PREPEND(fctx->events, event, ev_link); 3488 else 3489 ISC_LIST_APPEND(fctx->events, event, ev_link); 3490 fctx->references++; 3491 fctx->client = client; 3492 3493 fetch->magic = DNS_FETCH_MAGIC; 3494 fetch->private = fctx; 3495 3496 return (ISC_R_SUCCESS); 3497} 3498 3499static inline void 3500log_ns_ttl(fetchctx_t *fctx, const char *where) { 3501 char namebuf[DNS_NAME_FORMATSIZE]; 3502 char domainbuf[DNS_NAME_FORMATSIZE]; 3503 3504 dns_name_format(&fctx->name, namebuf, sizeof(namebuf)); 3505 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf)); 3506 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 3507 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10), 3508 "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u", 3509 fctx, where, namebuf, domainbuf, 3510 fctx->ns_ttl_ok, fctx->ns_ttl); 3511} 3512 3513static isc_result_t 3514fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type, 3515 dns_name_t *domain, dns_rdataset_t *nameservers, 3516 unsigned int options, unsigned int bucketnum, unsigned int depth, 3517 fetchctx_t **fctxp) 3518{ 3519 fetchctx_t *fctx; 3520 isc_result_t result; 3521 isc_result_t iresult; 3522 isc_interval_t interval; 3523 dns_fixedname_t fixed; 3524 unsigned int findoptions = 0; 3525 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE]; 3526 char typebuf[DNS_RDATATYPE_FORMATSIZE]; 3527 dns_name_t suffix; 3528 isc_mem_t *mctx; 3529 3530 /* 3531 * Caller must be holding the lock for bucket number 'bucketnum'. 3532 */ 3533 REQUIRE(fctxp != NULL && *fctxp == NULL); 3534 3535 mctx = res->buckets[bucketnum].mctx; 3536 fctx = isc_mem_get(mctx, sizeof(*fctx)); 3537 if (fctx == NULL) 3538 return (ISC_R_NOMEMORY); 3539 dns_name_format(name, buf, sizeof(buf)); 3540 dns_rdatatype_format(type, typebuf, sizeof(typebuf)); 3541 strcat(buf, "/"); /* checked */ 3542 strcat(buf, typebuf); /* checked */ 3543 fctx->info = isc_mem_strdup(mctx, buf); 3544 if (fctx->info == NULL) { 3545 result = ISC_R_NOMEMORY; 3546 goto cleanup_fetch; 3547 } 3548 FCTXTRACE("create"); 3549 dns_name_init(&fctx->name, NULL); 3550 result = dns_name_dup(name, mctx, &fctx->name); 3551 if (result != ISC_R_SUCCESS) 3552 goto cleanup_info; 3553 dns_name_init(&fctx->domain, NULL); 3554 dns_rdataset_init(&fctx->nameservers); 3555 3556 fctx->type = type; 3557 fctx->options = options; 3558 /* 3559 * Note! We do not attach to the task. We are relying on the 3560 * resolver to ensure that this task doesn't go away while we are 3561 * using it. 3562 */ 3563 fctx->res = res; 3564 fctx->references = 0; 3565 fctx->bucketnum = bucketnum; 3566 fctx->state = fetchstate_init; 3567 fctx->want_shutdown = ISC_FALSE; 3568 fctx->cloned = ISC_FALSE; 3569 fctx->depth = depth; 3570 ISC_LIST_INIT(fctx->queries); 3571 ISC_LIST_INIT(fctx->finds); 3572 ISC_LIST_INIT(fctx->altfinds); 3573 ISC_LIST_INIT(fctx->forwaddrs); 3574 ISC_LIST_INIT(fctx->altaddrs); 3575 ISC_LIST_INIT(fctx->forwarders); 3576 fctx->fwdpolicy = dns_fwdpolicy_none; 3577 ISC_LIST_INIT(fctx->bad); 3578 ISC_LIST_INIT(fctx->edns); 3579 ISC_LIST_INIT(fctx->edns512); 3580 ISC_LIST_INIT(fctx->bad_edns); 3581 ISC_LIST_INIT(fctx->validators); 3582 fctx->validator = NULL; 3583 fctx->find = NULL; 3584 fctx->altfind = NULL; 3585 fctx->pending = 0; 3586 fctx->restarts = 0; 3587 fctx->querysent = 0; 3588 fctx->totalqueries = 0; 3589 fctx->referrals = 0; 3590 TIME_NOW(&fctx->start); 3591 fctx->timeouts = 0; 3592 fctx->lamecount = 0; 3593 fctx->adberr = 0; 3594 fctx->neterr = 0; 3595 fctx->badresp = 0; 3596 fctx->findfail = 0; 3597 fctx->valfail = 0; 3598 fctx->result = ISC_R_FAILURE; 3599 fctx->vresult = ISC_R_SUCCESS; 3600 fctx->exitline = -1; /* sentinel */ 3601 fctx->logged = ISC_FALSE; 3602 fctx->attributes = 0; 3603 fctx->spilled = ISC_FALSE; 3604 fctx->nqueries = 0; 3605 fctx->reason = NULL; 3606 fctx->rand_buf = 0; 3607 fctx->rand_bits = 0; 3608 fctx->timeout = ISC_FALSE; 3609 fctx->addrinfo = NULL; 3610 fctx->client = NULL; 3611 fctx->ns_ttl = 0; 3612 fctx->ns_ttl_ok = ISC_FALSE; 3613 3614 dns_name_init(&fctx->nsname, NULL); 3615 fctx->nsfetch = NULL; 3616 dns_rdataset_init(&fctx->nsrrset); 3617 3618 if (domain == NULL) { 3619 dns_forwarders_t *forwarders = NULL; 3620 unsigned int labels; 3621 dns_name_t *fwdname = name; 3622 3623 /* 3624 * DS records are found in the parent server. 3625 * Strip label to get the correct forwarder (if any). 3626 */ 3627 if (dns_rdatatype_atparent(fctx->type) && 3628 dns_name_countlabels(name) > 1) { 3629 dns_name_init(&suffix, NULL); 3630 labels = dns_name_countlabels(name); 3631 dns_name_getlabelsequence(name, 1, labels - 1, &suffix); 3632 fwdname = &suffix; 3633 } 3634 dns_fixedname_init(&fixed); 3635 domain = dns_fixedname_name(&fixed); 3636 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname, 3637 domain, &forwarders); 3638 if (result == ISC_R_SUCCESS) 3639 fctx->fwdpolicy = forwarders->fwdpolicy; 3640 3641 if (fctx->fwdpolicy != dns_fwdpolicy_only) { 3642 /* 3643 * The caller didn't supply a query domain and 3644 * nameservers, and we're not in forward-only mode, 3645 * so find the best nameservers to use. 3646 */ 3647 if (dns_rdatatype_atparent(fctx->type)) 3648 findoptions |= DNS_DBFIND_NOEXACT; 3649 result = dns_view_findzonecut(res->view, fwdname, 3650 domain, 0, findoptions, 3651 ISC_TRUE, 3652 &fctx->nameservers, 3653 NULL); 3654 if (result != ISC_R_SUCCESS) 3655 goto cleanup_name; 3656 3657 result = dns_name_dup(domain, mctx, &fctx->domain); 3658 if (result != ISC_R_SUCCESS) { 3659 dns_rdataset_disassociate(&fctx->nameservers); 3660 goto cleanup_name; 3661 } 3662 fctx->ns_ttl = fctx->nameservers.ttl; 3663 fctx->ns_ttl_ok = ISC_TRUE; 3664 } else { 3665 /* 3666 * We're in forward-only mode. Set the query domain. 3667 */ 3668 result = dns_name_dup(domain, mctx, &fctx->domain); 3669 if (result != ISC_R_SUCCESS) 3670 goto cleanup_name; 3671 } 3672 } else { 3673 result = dns_name_dup(domain, mctx, &fctx->domain); 3674 if (result != ISC_R_SUCCESS) 3675 goto cleanup_name; 3676 dns_rdataset_clone(nameservers, &fctx->nameservers); 3677 fctx->ns_ttl = fctx->nameservers.ttl; 3678 fctx->ns_ttl_ok = ISC_TRUE; 3679 } 3680 3681 log_ns_ttl(fctx, "fctx_create"); 3682 3683 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain)); 3684 3685 fctx->qmessage = NULL; 3686 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, 3687 &fctx->qmessage); 3688 3689 if (result != ISC_R_SUCCESS) 3690 goto cleanup_domain; 3691 3692 fctx->rmessage = NULL; 3693 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, 3694 &fctx->rmessage); 3695 3696 if (result != ISC_R_SUCCESS) 3697 goto cleanup_qmessage; 3698 3699 /* 3700 * Compute an expiration time for the entire fetch. 3701 */ 3702 isc_interval_set(&interval, res->query_timeout, 0); 3703 iresult = isc_time_nowplusinterval(&fctx->expires, &interval); 3704 if (iresult != ISC_R_SUCCESS) { 3705 UNEXPECTED_ERROR(__FILE__, __LINE__, 3706 "isc_time_nowplusinterval: %s", 3707 isc_result_totext(iresult)); 3708 result = ISC_R_UNEXPECTED; 3709 goto cleanup_rmessage; 3710 } 3711 3712 /* 3713 * Default retry interval initialization. We set the interval now 3714 * mostly so it won't be uninitialized. It will be set to the 3715 * correct value before a query is issued. 3716 */ 3717 isc_interval_set(&fctx->interval, 2, 0); 3718 3719 /* 3720 * Create an inactive timer. It will be made active when the fetch 3721 * is actually started. 3722 */ 3723 fctx->timer = NULL; 3724 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive, 3725 NULL, NULL, 3726 res->buckets[bucketnum].task, fctx_timeout, 3727 fctx, &fctx->timer); 3728 if (iresult != ISC_R_SUCCESS) { 3729 UNEXPECTED_ERROR(__FILE__, __LINE__, 3730 "isc_timer_create: %s", 3731 isc_result_totext(iresult)); 3732 result = ISC_R_UNEXPECTED; 3733 goto cleanup_rmessage; 3734 } 3735 3736 /* 3737 * Attach to the view's cache and adb. 3738 */ 3739 fctx->cache = NULL; 3740 dns_db_attach(res->view->cachedb, &fctx->cache); 3741 fctx->adb = NULL; 3742 dns_adb_attach(res->view->adb, &fctx->adb); 3743 fctx->mctx = NULL; 3744 isc_mem_attach(mctx, &fctx->mctx); 3745 3746 ISC_LIST_INIT(fctx->events); 3747 ISC_LINK_INIT(fctx, link); 3748 fctx->magic = FCTX_MAGIC; 3749 3750 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link); 3751 3752 LOCK(&res->nlock); 3753 res->nfctx++; 3754 UNLOCK(&res->nlock); 3755 3756 *fctxp = fctx; 3757 3758 return (ISC_R_SUCCESS); 3759 3760 cleanup_rmessage: 3761 dns_message_destroy(&fctx->rmessage); 3762 3763 cleanup_qmessage: 3764 dns_message_destroy(&fctx->qmessage); 3765 3766 cleanup_domain: 3767 if (dns_name_countlabels(&fctx->domain) > 0) 3768 dns_name_free(&fctx->domain, mctx); 3769 if (dns_rdataset_isassociated(&fctx->nameservers)) 3770 dns_rdataset_disassociate(&fctx->nameservers); 3771 3772 cleanup_name: 3773 dns_name_free(&fctx->name, mctx); 3774 3775 cleanup_info: 3776 isc_mem_free(mctx, fctx->info); 3777 3778 cleanup_fetch: 3779 isc_mem_put(mctx, fctx, sizeof(*fctx)); 3780 3781 return (result); 3782} 3783 3784/* 3785 * Handle Responses 3786 */ 3787static inline isc_boolean_t 3788is_lame(fetchctx_t *fctx) { 3789 dns_message_t *message = fctx->rmessage; 3790 dns_name_t *name; 3791 dns_rdataset_t *rdataset; 3792 isc_result_t result; 3793 3794 if (message->rcode != dns_rcode_noerror && 3795 message->rcode != dns_rcode_nxdomain) 3796 return (ISC_FALSE); 3797 3798 if (message->counts[DNS_SECTION_ANSWER] != 0) 3799 return (ISC_FALSE); 3800 3801 if (message->counts[DNS_SECTION_AUTHORITY] == 0) 3802 return (ISC_FALSE); 3803 3804 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY); 3805 while (result == ISC_R_SUCCESS) { 3806 name = NULL; 3807 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name); 3808 for (rdataset = ISC_LIST_HEAD(name->list); 3809 rdataset != NULL; 3810 rdataset = ISC_LIST_NEXT(rdataset, link)) { 3811 dns_namereln_t namereln; 3812 int order; 3813 unsigned int labels; 3814 if (rdataset->type != dns_rdatatype_ns) 3815 continue; 3816 namereln = dns_name_fullcompare(name, &fctx->domain, 3817 &order, &labels); 3818 if (namereln == dns_namereln_equal && 3819 (message->flags & DNS_MESSAGEFLAG_AA) != 0) 3820 return (ISC_FALSE); 3821 if (namereln == dns_namereln_subdomain) 3822 return (ISC_FALSE); 3823 return (ISC_TRUE); 3824 } 3825 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY); 3826 } 3827 3828 return (ISC_FALSE); 3829} 3830 3831static inline void 3832log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) { 3833 char namebuf[DNS_NAME_FORMATSIZE]; 3834 char domainbuf[DNS_NAME_FORMATSIZE]; 3835 char addrbuf[ISC_SOCKADDR_FORMATSIZE]; 3836 3837 dns_name_format(&fctx->name, namebuf, sizeof(namebuf)); 3838 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf)); 3839 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf)); 3840 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS, 3841 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO, 3842 "lame server resolving '%s' (in '%s'?): %s", 3843 namebuf, domainbuf, addrbuf); 3844} 3845 3846static inline void 3847log_formerr(fetchctx_t *fctx, const char *format, ...) { 3848 char nsbuf[ISC_SOCKADDR_FORMATSIZE]; 3849 char clbuf[ISC_SOCKADDR_FORMATSIZE]; 3850 const char *clmsg = ""; 3851 char msgbuf[2048]; 3852 va_list args; 3853 3854 va_start(args, format); 3855 vsnprintf(msgbuf, sizeof(msgbuf), format, args); 3856 va_end(args); 3857 3858 isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf)); 3859 3860 if (fctx->client != NULL) { 3861 clmsg = " for client "; 3862 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf)); 3863 } else { 3864 clbuf[0] = '\0'; 3865 } 3866 3867 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 3868 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 3869 "DNS format error from %s resolving %s%s%s: %s", 3870 nsbuf, fctx->info, clmsg, clbuf, msgbuf); 3871} 3872 3873static inline isc_result_t 3874same_question(fetchctx_t *fctx) { 3875 isc_result_t result; 3876 dns_message_t *message = fctx->rmessage; 3877 dns_name_t *name; 3878 dns_rdataset_t *rdataset; 3879 3880 /* 3881 * Caller must be holding the fctx lock. 3882 */ 3883 3884 /* 3885 * XXXRTH Currently we support only one question. 3886 */ 3887 if (message->counts[DNS_SECTION_QUESTION] != 1) { 3888 log_formerr(fctx, "too many questions"); 3889 return (DNS_R_FORMERR); 3890 } 3891 3892 result = dns_message_firstname(message, DNS_SECTION_QUESTION); 3893 if (result != ISC_R_SUCCESS) 3894 return (result); 3895 name = NULL; 3896 dns_message_currentname(message, DNS_SECTION_QUESTION, &name); 3897 rdataset = ISC_LIST_HEAD(name->list); 3898 INSIST(rdataset != NULL); 3899 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL); 3900 3901 if (fctx->type != rdataset->type || 3902 fctx->res->rdclass != rdataset->rdclass || 3903 !dns_name_equal(&fctx->name, name)) { 3904 char namebuf[DNS_NAME_FORMATSIZE]; 3905 char class[DNS_RDATACLASS_FORMATSIZE]; 3906 char type[DNS_RDATATYPE_FORMATSIZE]; 3907 3908 dns_name_format(name, namebuf, sizeof(namebuf)); 3909 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class)); 3910 dns_rdatatype_format(rdataset->type, type, sizeof(type)); 3911 log_formerr(fctx, "question section mismatch: got %s/%s/%s", 3912 namebuf, class, type); 3913 return (DNS_R_FORMERR); 3914 } 3915 3916 return (ISC_R_SUCCESS); 3917} 3918 3919static void 3920clone_results(fetchctx_t *fctx) { 3921 dns_fetchevent_t *event, *hevent; 3922 isc_result_t result; 3923 dns_name_t *name, *hname; 3924 3925 FCTXTRACE("clone_results"); 3926 3927 /* 3928 * Set up any other events to have the same data as the first 3929 * event. 3930 * 3931 * Caller must be holding the appropriate lock. 3932 */ 3933 3934 fctx->cloned = ISC_TRUE; 3935 hevent = ISC_LIST_HEAD(fctx->events); 3936 if (hevent == NULL) 3937 return; 3938 hname = dns_fixedname_name(&hevent->foundname); 3939 for (event = ISC_LIST_NEXT(hevent, ev_link); 3940 event != NULL; 3941 event = ISC_LIST_NEXT(event, ev_link)) { 3942 name = dns_fixedname_name(&event->foundname); 3943 result = dns_name_copy(hname, name, NULL); 3944 if (result != ISC_R_SUCCESS) 3945 event->result = result; 3946 else 3947 event->result = hevent->result; 3948 dns_db_attach(hevent->db, &event->db); 3949 dns_db_attachnode(hevent->db, hevent->node, &event->node); 3950 INSIST(hevent->rdataset != NULL); 3951 INSIST(event->rdataset != NULL); 3952 if (dns_rdataset_isassociated(hevent->rdataset)) 3953 dns_rdataset_clone(hevent->rdataset, event->rdataset); 3954 INSIST(! (hevent->sigrdataset == NULL && 3955 event->sigrdataset != NULL)); 3956 if (hevent->sigrdataset != NULL && 3957 dns_rdataset_isassociated(hevent->sigrdataset) && 3958 event->sigrdataset != NULL) 3959 dns_rdataset_clone(hevent->sigrdataset, 3960 event->sigrdataset); 3961 } 3962} 3963 3964#define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0) 3965#define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0) 3966#define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0) 3967#define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0) 3968#define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0) 3969#define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0) 3970#define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0) 3971 3972 3973/* 3974 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has 3975 * no references and is no longer waiting for any events). 3976 * 3977 * Requires: 3978 * '*fctx' is shutting down. 3979 * 3980 * Returns: 3981 * true if the resolver is exiting and this is the last fctx in the bucket. 3982 */ 3983static isc_boolean_t 3984maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) { 3985 unsigned int bucketnum; 3986 isc_boolean_t bucket_empty = ISC_FALSE; 3987 dns_resolver_t *res = fctx->res; 3988 dns_validator_t *validator, *next_validator; 3989 isc_boolean_t destroy = ISC_FALSE; 3990 3991 REQUIRE(SHUTTINGDOWN(fctx)); 3992 3993 bucketnum = fctx->bucketnum; 3994 if (!locked) 3995 LOCK(&res->buckets[bucketnum].lock); 3996 if (fctx->pending != 0 || fctx->nqueries != 0) 3997 goto unlock; 3998 3999 for (validator = ISC_LIST_HEAD(fctx->validators); 4000 validator != NULL; validator = next_validator) { 4001 next_validator = ISC_LIST_NEXT(validator, link); 4002 dns_validator_cancel(validator); 4003 } 4004 4005 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators)) { 4006 bucket_empty = fctx_unlink(fctx); 4007 destroy = ISC_TRUE; 4008 } 4009 unlock: 4010 if (!locked) 4011 UNLOCK(&res->buckets[bucketnum].lock); 4012 if (destroy) 4013 fctx_destroy(fctx); 4014 return (bucket_empty); 4015} 4016 4017/* 4018 * The validator has finished. 4019 */ 4020static void 4021validated(isc_task_t *task, isc_event_t *event) { 4022 dns_adbaddrinfo_t *addrinfo; 4023 dns_dbnode_t *node = NULL; 4024 dns_dbnode_t *nsnode = NULL; 4025 dns_fetchevent_t *hevent; 4026 dns_name_t *name; 4027 dns_rdataset_t *ardataset = NULL; 4028 dns_rdataset_t *asigrdataset = NULL; 4029 dns_rdataset_t *rdataset; 4030 dns_rdataset_t *sigrdataset; 4031 dns_resolver_t *res; 4032 dns_valarg_t *valarg; 4033 dns_validatorevent_t *vevent; 4034 fetchctx_t *fctx; 4035 isc_boolean_t chaining; 4036 isc_boolean_t negative; 4037 isc_boolean_t sentresponse; 4038 isc_result_t eresult = ISC_R_SUCCESS; 4039 isc_result_t result = ISC_R_SUCCESS; 4040 isc_stdtime_t now; 4041 isc_uint32_t ttl; 4042 4043 UNUSED(task); /* for now */ 4044 4045 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE); 4046 valarg = event->ev_arg; 4047 fctx = valarg->fctx; 4048 res = fctx->res; 4049 addrinfo = valarg->addrinfo; 4050 REQUIRE(VALID_FCTX(fctx)); 4051 REQUIRE(!ISC_LIST_EMPTY(fctx->validators)); 4052 4053 vevent = (dns_validatorevent_t *)event; 4054 fctx->vresult = vevent->result; 4055 4056 FCTXTRACE("received validation completion event"); 4057 4058 LOCK(&res->buckets[fctx->bucketnum].lock); 4059 4060 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link); 4061 fctx->validator = NULL; 4062 4063 /* 4064 * Destroy the validator early so that we can 4065 * destroy the fctx if necessary. 4066 */ 4067 dns_validator_destroy(&vevent->validator); 4068 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg)); 4069 4070 negative = ISC_TF(vevent->rdataset == NULL); 4071 4072 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0); 4073 4074 /* 4075 * If shutting down, ignore the results. Check to see if we're 4076 * done waiting for validator completions and ADB pending events; if 4077 * so, destroy the fctx. 4078 */ 4079 if (SHUTTINGDOWN(fctx) && !sentresponse) { 4080 isc_uint32_t bucketnum = fctx->bucketnum; 4081 isc_boolean_t bucket_empty; 4082 bucket_empty = maybe_destroy(fctx, ISC_TRUE); 4083 UNLOCK(&res->buckets[bucketnum].lock); 4084 if (bucket_empty) 4085 empty_bucket(res); 4086 goto cleanup_event; 4087 } 4088 4089 isc_stdtime_get(&now); 4090 4091 /* 4092 * If chaining, we need to make sure that the right result code is 4093 * returned, and that the rdatasets are bound. 4094 */ 4095 if (vevent->result == ISC_R_SUCCESS && 4096 !negative && 4097 vevent->rdataset != NULL && 4098 CHAINING(vevent->rdataset)) 4099 { 4100 if (vevent->rdataset->type == dns_rdatatype_cname) 4101 eresult = DNS_R_CNAME; 4102 else { 4103 INSIST(vevent->rdataset->type == dns_rdatatype_dname); 4104 eresult = DNS_R_DNAME; 4105 } 4106 chaining = ISC_TRUE; 4107 } else 4108 chaining = ISC_FALSE; 4109 4110 /* 4111 * Either we're not shutting down, or we are shutting down but want 4112 * to cache the result anyway (if this was a validation started by 4113 * a query with cd set) 4114 */ 4115 4116 hevent = ISC_LIST_HEAD(fctx->events); 4117 if (hevent != NULL) { 4118 if (!negative && !chaining && 4119 (fctx->type == dns_rdatatype_any || 4120 fctx->type == dns_rdatatype_rrsig || 4121 fctx->type == dns_rdatatype_sig)) { 4122 /* 4123 * Don't bind rdatasets; the caller 4124 * will iterate the node. 4125 */ 4126 } else { 4127 ardataset = hevent->rdataset; 4128 asigrdataset = hevent->sigrdataset; 4129 } 4130 } 4131 4132 if (vevent->result != ISC_R_SUCCESS) { 4133 FCTXTRACE("validation failed"); 4134 inc_stats(res, dns_resstatscounter_valfail); 4135 fctx->valfail++; 4136 fctx->vresult = vevent->result; 4137 if (fctx->vresult != DNS_R_BROKENCHAIN) { 4138 result = ISC_R_NOTFOUND; 4139 if (vevent->rdataset != NULL) 4140 result = dns_db_findnode(fctx->cache, 4141 vevent->name, 4142 ISC_TRUE, &node); 4143 if (result == ISC_R_SUCCESS) 4144 (void)dns_db_deleterdataset(fctx->cache, node, 4145 NULL, 4146 vevent->type, 0); 4147 if (result == ISC_R_SUCCESS && 4148 vevent->sigrdataset != NULL) 4149 (void)dns_db_deleterdataset(fctx->cache, node, 4150 NULL, 4151 dns_rdatatype_rrsig, 4152 vevent->type); 4153 if (result == ISC_R_SUCCESS) 4154 dns_db_detachnode(fctx->cache, &node); 4155 } 4156 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) { 4157 /* 4158 * Cache the data as pending for later validation. 4159 */ 4160 result = ISC_R_NOTFOUND; 4161 if (vevent->rdataset != NULL) 4162 result = dns_db_findnode(fctx->cache, 4163 vevent->name, 4164 ISC_TRUE, &node); 4165 if (result == ISC_R_SUCCESS) { 4166 (void)dns_db_addrdataset(fctx->cache, node, 4167 NULL, now, 4168 vevent->rdataset, 0, 4169 NULL); 4170 } 4171 if (result == ISC_R_SUCCESS && 4172 vevent->sigrdataset != NULL) 4173 (void)dns_db_addrdataset(fctx->cache, node, 4174 NULL, now, 4175 vevent->sigrdataset, 4176 0, NULL); 4177 if (result == ISC_R_SUCCESS) 4178 dns_db_detachnode(fctx->cache, &node); 4179 } 4180 result = fctx->vresult; 4181 add_bad(fctx, addrinfo, result, badns_validation); 4182 isc_event_free(&event); 4183 UNLOCK(&res->buckets[fctx->bucketnum].lock); 4184 INSIST(fctx->validator == NULL); 4185 fctx->validator = ISC_LIST_HEAD(fctx->validators); 4186 if (fctx->validator != NULL) 4187 dns_validator_send(fctx->validator); 4188 else if (sentresponse) 4189 fctx_done(fctx, result, __LINE__); /* Locks bucket. */ 4190 else if (result == DNS_R_BROKENCHAIN) { 4191 isc_result_t tresult; 4192 isc_time_t expire; 4193 isc_interval_t i; 4194 4195 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0); 4196 tresult = isc_time_nowplusinterval(&expire, &i); 4197 if (negative && 4198 (fctx->type == dns_rdatatype_dnskey || 4199 fctx->type == dns_rdatatype_dlv || 4200 fctx->type == dns_rdatatype_ds) && 4201 tresult == ISC_R_SUCCESS) 4202 dns_resolver_addbadcache(res, &fctx->name, 4203 fctx->type, &expire); 4204 fctx_done(fctx, result, __LINE__); /* Locks bucket. */ 4205 } else 4206 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */ 4207 return; 4208 } 4209 4210 4211 if (negative) { 4212 dns_rdatatype_t covers; 4213 FCTXTRACE("nonexistence validation OK"); 4214 4215 inc_stats(res, dns_resstatscounter_valnegsuccess); 4216 4217 if (fctx->rmessage->rcode == dns_rcode_nxdomain) 4218 covers = dns_rdatatype_any; 4219 else 4220 covers = fctx->type; 4221 4222 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, 4223 &node); 4224 if (result != ISC_R_SUCCESS) 4225 goto noanswer_response; 4226 4227 /* 4228 * If we are asking for a SOA record set the cache time 4229 * to zero to facilitate locating the containing zone of 4230 * a arbitrary zone. 4231 */ 4232 ttl = res->view->maxncachettl; 4233 if (fctx->type == dns_rdatatype_soa && 4234 covers == dns_rdatatype_any && res->zero_no_soa_ttl) 4235 ttl = 0; 4236 4237 result = ncache_adderesult(fctx->rmessage, fctx->cache, node, 4238 covers, now, ttl, vevent->optout, 4239 vevent->secure, ardataset, &eresult); 4240 if (result != ISC_R_SUCCESS) 4241 goto noanswer_response; 4242 goto answer_response; 4243 } else 4244 inc_stats(res, dns_resstatscounter_valsuccess); 4245 4246 FCTXTRACE("validation OK"); 4247 4248 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) { 4249 result = dns_rdataset_addnoqname(vevent->rdataset, 4250 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]); 4251 RUNTIME_CHECK(result == ISC_R_SUCCESS); 4252 INSIST(vevent->sigrdataset != NULL); 4253 vevent->sigrdataset->ttl = vevent->rdataset->ttl; 4254 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) { 4255 result = dns_rdataset_addclosest(vevent->rdataset, 4256 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]); 4257 RUNTIME_CHECK(result == ISC_R_SUCCESS); 4258 } 4259 } else if (vevent->rdataset->trust == dns_trust_answer && 4260 vevent->rdataset->type != dns_rdatatype_rrsig) 4261 { 4262 isc_result_t tresult; 4263 dns_name_t *noqname = NULL; 4264 tresult = findnoqname(fctx, vevent->name, 4265 vevent->rdataset->type, &noqname); 4266 if (tresult == ISC_R_SUCCESS && noqname != NULL) { 4267 tresult = dns_rdataset_addnoqname(vevent->rdataset, 4268 noqname); 4269 RUNTIME_CHECK(tresult == ISC_R_SUCCESS); 4270 } 4271 } 4272 4273 /* 4274 * The data was already cached as pending data. 4275 * Re-cache it as secure and bind the cached 4276 * rdatasets to the first event on the fetch 4277 * event list. 4278 */ 4279 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node); 4280 if (result != ISC_R_SUCCESS) 4281 goto noanswer_response; 4282 4283 result = dns_db_addrdataset(fctx->cache, node, NULL, now, 4284 vevent->rdataset, 0, ardataset); 4285 if (result != ISC_R_SUCCESS && 4286 result != DNS_R_UNCHANGED) 4287 goto noanswer_response; 4288 if (ardataset != NULL && NEGATIVE(ardataset)) { 4289 if (NXDOMAIN(ardataset)) 4290 eresult = DNS_R_NCACHENXDOMAIN; 4291 else 4292 eresult = DNS_R_NCACHENXRRSET; 4293 } else if (vevent->sigrdataset != NULL) { 4294 result = dns_db_addrdataset(fctx->cache, node, NULL, now, 4295 vevent->sigrdataset, 0, 4296 asigrdataset); 4297 if (result != ISC_R_SUCCESS && 4298 result != DNS_R_UNCHANGED) 4299 goto noanswer_response; 4300 } 4301 4302 if (sentresponse) { 4303 isc_boolean_t bucket_empty = ISC_FALSE; 4304 /* 4305 * If we only deferred the destroy because we wanted to cache 4306 * the data, destroy now. 4307 */ 4308 dns_db_detachnode(fctx->cache, &node); 4309 if (SHUTTINGDOWN(fctx)) 4310 bucket_empty = maybe_destroy(fctx, ISC_TRUE); 4311 UNLOCK(&res->buckets[fctx->bucketnum].lock); 4312 if (bucket_empty) 4313 empty_bucket(res); 4314 goto cleanup_event; 4315 } 4316 4317 if (!ISC_LIST_EMPTY(fctx->validators)) { 4318 INSIST(!negative); 4319 INSIST(fctx->type == dns_rdatatype_any || 4320 fctx->type == dns_rdatatype_rrsig || 4321 fctx->type == dns_rdatatype_sig); 4322 /* 4323 * Don't send a response yet - we have 4324 * more rdatasets that still need to 4325 * be validated. 4326 */ 4327 dns_db_detachnode(fctx->cache, &node); 4328 UNLOCK(&res->buckets[fctx->bucketnum].lock); 4329 dns_validator_send(ISC_LIST_HEAD(fctx->validators)); 4330 goto cleanup_event; 4331 } 4332 4333 answer_response: 4334 /* 4335 * Cache any NS/NSEC records that happened to be validated. 4336 */ 4337 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY); 4338 while (result == ISC_R_SUCCESS) { 4339 name = NULL; 4340 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY, 4341 &name); 4342 for (rdataset = ISC_LIST_HEAD(name->list); 4343 rdataset != NULL; 4344 rdataset = ISC_LIST_NEXT(rdataset, link)) { 4345 if ((rdataset->type != dns_rdatatype_ns && 4346 rdataset->type != dns_rdatatype_nsec) || 4347 rdataset->trust != dns_trust_secure) 4348 continue; 4349 for (sigrdataset = ISC_LIST_HEAD(name->list); 4350 sigrdataset != NULL; 4351 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) { 4352 if (sigrdataset->type != dns_rdatatype_rrsig || 4353 sigrdataset->covers != rdataset->type) 4354 continue; 4355 break; 4356 } 4357 if (sigrdataset == NULL || 4358 sigrdataset->trust != dns_trust_secure) 4359 continue; 4360 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, 4361 &nsnode); 4362 if (result != ISC_R_SUCCESS) 4363 continue; 4364 4365 result = dns_db_addrdataset(fctx->cache, nsnode, NULL, 4366 now, rdataset, 0, NULL); 4367 if (result == ISC_R_SUCCESS) 4368 result = dns_db_addrdataset(fctx->cache, nsnode, 4369 NULL, now, 4370 sigrdataset, 0, 4371 NULL); 4372 dns_db_detachnode(fctx->cache, &nsnode); 4373 if (result != ISC_R_SUCCESS) 4374 continue; 4375 } 4376 result = dns_message_nextname(fctx->rmessage, 4377 DNS_SECTION_AUTHORITY); 4378 } 4379 4380 result = ISC_R_SUCCESS; 4381 4382 /* 4383 * Respond with an answer, positive or negative, 4384 * as opposed to an error. 'node' must be non-NULL. 4385 */ 4386 4387 fctx->attributes |= FCTX_ATTR_HAVEANSWER; 4388 4389 if (hevent != NULL) { 4390 /* 4391 * Negative results must be indicated in event->result. 4392 */ 4393 if (dns_rdataset_isassociated(hevent->rdataset) && 4394 NEGATIVE(hevent->rdataset)) { 4395 INSIST(eresult == DNS_R_NCACHENXDOMAIN || 4396 eresult == DNS_R_NCACHENXRRSET); 4397 } 4398 hevent->result = eresult; 4399 RUNTIME_CHECK(dns_name_copy(vevent->name, 4400 dns_fixedname_name(&hevent->foundname), NULL) 4401 == ISC_R_SUCCESS); 4402 dns_db_attach(fctx->cache, &hevent->db); 4403 dns_db_transfernode(fctx->cache, &node, &hevent->node); 4404 clone_results(fctx); 4405 } 4406 4407 noanswer_response: 4408 if (node != NULL) 4409 dns_db_detachnode(fctx->cache, &node); 4410 4411 UNLOCK(&res->buckets[fctx->bucketnum].lock); 4412 fctx_done(fctx, result, __LINE__); /* Locks bucket. */ 4413 4414 cleanup_event: 4415 INSIST(node == NULL); 4416 isc_event_free(&event); 4417} 4418 4419static void 4420fctx_log(void *arg, int level, const char *fmt, ...) { 4421 char msgbuf[2048]; 4422 va_list args; 4423 fetchctx_t *fctx = arg; 4424 4425 va_start(args, fmt); 4426 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); 4427 va_end(args); 4428 4429 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 4430 DNS_LOGMODULE_RESOLVER, level, 4431 "fctx %p(%s): %s", fctx, fctx->info, msgbuf); 4432} 4433 4434static inline isc_result_t 4435findnoqname(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type, 4436 dns_name_t **noqnamep) 4437{ 4438 dns_rdataset_t *nrdataset, *next, *sigrdataset; 4439 dns_rdata_rrsig_t rrsig; 4440 isc_result_t result; 4441 unsigned int labels; 4442 dns_section_t section; 4443 dns_name_t *zonename; 4444 dns_fixedname_t fzonename; 4445 dns_name_t *closest; 4446 dns_fixedname_t fclosest; 4447 dns_name_t *nearest; 4448 dns_fixedname_t fnearest; 4449 dns_rdatatype_t found = dns_rdatatype_none; 4450 dns_name_t *noqname = NULL; 4451 4452 FCTXTRACE("findnoqname"); 4453 4454 REQUIRE(noqnamep != NULL && *noqnamep == NULL); 4455 4456 /* 4457 * Find the SIG for this rdataset, if we have it. 4458 */ 4459 for (sigrdataset = ISC_LIST_HEAD(name->list); 4460 sigrdataset != NULL; 4461 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) { 4462 if (sigrdataset->type == dns_rdatatype_rrsig && 4463 sigrdataset->covers == type) 4464 break; 4465 } 4466 4467 if (sigrdataset == NULL) 4468 return (ISC_R_NOTFOUND); 4469 4470 labels = dns_name_countlabels(name); 4471 4472 for (result = dns_rdataset_first(sigrdataset); 4473 result == ISC_R_SUCCESS; 4474 result = dns_rdataset_next(sigrdataset)) { 4475 dns_rdata_t rdata = DNS_RDATA_INIT; 4476 dns_rdataset_current(sigrdataset, &rdata); 4477 result = dns_rdata_tostruct(&rdata, &rrsig, NULL); 4478 RUNTIME_CHECK(result == ISC_R_SUCCESS); 4479 /* Wildcard has rrsig.labels < labels - 1. */ 4480 if (rrsig.labels + 1U >= labels) 4481 continue; 4482 break; 4483 } 4484 4485 if (result == ISC_R_NOMORE) 4486 return (ISC_R_NOTFOUND); 4487 if (result != ISC_R_SUCCESS) 4488 return (result); 4489 4490 dns_fixedname_init(&fzonename); 4491 zonename = dns_fixedname_name(&fzonename); 4492 dns_fixedname_init(&fclosest); 4493 closest = dns_fixedname_name(&fclosest); 4494 dns_fixedname_init(&fnearest); 4495 nearest = dns_fixedname_name(&fnearest); 4496 4497#define NXND(x) ((x) == ISC_R_SUCCESS) 4498 4499 section = DNS_SECTION_AUTHORITY; 4500 for (result = dns_message_firstname(fctx->rmessage, section); 4501 result == ISC_R_SUCCESS; 4502 result = dns_message_nextname(fctx->rmessage, section)) { 4503 dns_name_t *nsec = NULL; 4504 dns_message_currentname(fctx->rmessage, section, &nsec); 4505 for (nrdataset = ISC_LIST_HEAD(nsec->list); 4506 nrdataset != NULL; nrdataset = next) { 4507 isc_boolean_t data = ISC_FALSE, exists = ISC_FALSE; 4508 isc_boolean_t optout = ISC_FALSE, unknown = ISC_FALSE; 4509 isc_boolean_t setclosest = ISC_FALSE; 4510 isc_boolean_t setnearest = ISC_FALSE; 4511 4512 next = ISC_LIST_NEXT(nrdataset, link); 4513 if (nrdataset->type != dns_rdatatype_nsec && 4514 nrdataset->type != dns_rdatatype_nsec3) 4515 continue; 4516 4517 if (nrdataset->type == dns_rdatatype_nsec && 4518 NXND(dns_nsec_noexistnodata(type, name, nsec, 4519 nrdataset, &exists, 4520 &data, NULL, fctx_log, 4521 fctx))) 4522 { 4523 if (!exists) { 4524 noqname = nsec; 4525 found = dns_rdatatype_nsec; 4526 } 4527 } 4528 4529 if (nrdataset->type == dns_rdatatype_nsec3 && 4530 NXND(dns_nsec3_noexistnodata(type, name, nsec, 4531 nrdataset, zonename, 4532 &exists, &data, 4533 &optout, &unknown, 4534 &setclosest, 4535 &setnearest, 4536 closest, nearest, 4537 fctx_log, fctx))) 4538 { 4539 if (!exists && setnearest) { 4540 noqname = nsec; 4541 found = dns_rdatatype_nsec3; 4542 } 4543 } 4544 } 4545 } 4546 if (result == ISC_R_NOMORE) 4547 result = ISC_R_SUCCESS; 4548 if (noqname != NULL) { 4549 for (sigrdataset = ISC_LIST_HEAD(noqname->list); 4550 sigrdataset != NULL; 4551 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) { 4552 if (sigrdataset->type == dns_rdatatype_rrsig && 4553 sigrdataset->covers == found) 4554 break; 4555 } 4556 if (sigrdataset != NULL) 4557 *noqnamep = noqname; 4558 } 4559 return (result); 4560} 4561 4562static inline isc_result_t 4563cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo, 4564 isc_stdtime_t now) 4565{ 4566 dns_rdataset_t *rdataset, *sigrdataset; 4567 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset; 4568 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL; 4569 dns_dbnode_t *node, **anodep; 4570 dns_db_t **adbp; 4571 dns_name_t *aname; 4572 dns_resolver_t *res; 4573 isc_boolean_t need_validation, secure_domain, have_answer; 4574 isc_result_t result, eresult; 4575 dns_fetchevent_t *event; 4576 unsigned int options; 4577 isc_task_t *task; 4578 isc_boolean_t fail; 4579 unsigned int valoptions = 0; 4580 4581 /* 4582 * The appropriate bucket lock must be held. 4583 */ 4584 4585 res = fctx->res; 4586 need_validation = ISC_FALSE; 4587 POST(need_validation); 4588 secure_domain = ISC_FALSE; 4589 have_answer = ISC_FALSE; 4590 eresult = ISC_R_SUCCESS; 4591 task = res->buckets[fctx->bucketnum].task; 4592 4593 /* 4594 * Is DNSSEC validation required for this name? 4595 */ 4596 if (res->view->enablevalidation) { 4597 result = dns_view_issecuredomain(res->view, name, 4598 &secure_domain); 4599 if (result != ISC_R_SUCCESS) 4600 return (result); 4601 4602 if (!secure_domain && res->view->dlv != NULL) { 4603 valoptions = DNS_VALIDATOR_DLV; 4604 secure_domain = ISC_TRUE; 4605 } 4606 } 4607 4608 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0) 4609 need_validation = ISC_FALSE; 4610 else 4611 need_validation = secure_domain; 4612 4613 adbp = NULL; 4614 aname = NULL; 4615 anodep = NULL; 4616 ardataset = NULL; 4617 asigrdataset = NULL; 4618 event = NULL; 4619 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 && 4620 !need_validation) { 4621 have_answer = ISC_TRUE; 4622 event = ISC_LIST_HEAD(fctx->events); 4623 if (event != NULL) { 4624 adbp = &event->db; 4625 aname = dns_fixedname_name(&event->foundname); 4626 result = dns_name_copy(name, aname, NULL); 4627 if (result != ISC_R_SUCCESS) 4628 return (result); 4629 anodep = &event->node; 4630 /* 4631 * If this is an ANY, SIG or RRSIG query, we're not 4632 * going to return any rdatasets, unless we encountered 4633 * a CNAME or DNAME as "the answer". In this case, 4634 * we're going to return DNS_R_CNAME or DNS_R_DNAME 4635 * and we must set up the rdatasets. 4636 */ 4637 if ((fctx->type != dns_rdatatype_any && 4638 fctx->type != dns_rdatatype_rrsig && 4639 fctx->type != dns_rdatatype_sig) || 4640 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) { 4641 ardataset = event->rdataset; 4642 asigrdataset = event->sigrdataset; 4643 } 4644 } 4645 } 4646 4647 /* 4648 * Find or create the cache node. 4649 */ 4650 node = NULL; 4651 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node); 4652 if (result != ISC_R_SUCCESS) 4653 return (result); 4654 4655 /* 4656 * Cache or validate each cacheable rdataset. 4657 */ 4658 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0); 4659 for (rdataset = ISC_LIST_HEAD(name->list); 4660 rdataset != NULL; 4661 rdataset = ISC_LIST_NEXT(rdataset, link)) { 4662 if (!CACHE(rdataset)) 4663 continue; 4664 if (CHECKNAMES(rdataset)) { 4665 char namebuf[DNS_NAME_FORMATSIZE]; 4666 char typebuf[DNS_RDATATYPE_FORMATSIZE]; 4667 char classbuf[DNS_RDATATYPE_FORMATSIZE]; 4668 4669 dns_name_format(name, namebuf, sizeof(namebuf)); 4670 dns_rdatatype_format(rdataset->type, typebuf, 4671 sizeof(typebuf)); 4672 dns_rdataclass_format(rdataset->rdclass, classbuf, 4673 sizeof(classbuf)); 4674 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 4675 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 4676 "check-names %s %s/%s/%s", 4677 fail ? "failure" : "warning", 4678 namebuf, typebuf, classbuf); 4679 if (fail) { 4680 if (ANSWER(rdataset)) { 4681 dns_db_detachnode(fctx->cache, &node); 4682 return (DNS_R_BADNAME); 4683 } 4684 continue; 4685 } 4686 } 4687 4688 /* 4689 * Enforce the configure maximum cache TTL. 4690 */ 4691 if (rdataset->ttl > res->view->maxcachettl) 4692 rdataset->ttl = res->view->maxcachettl; 4693 4694 /* 4695 * Find the SIG for this rdataset, if we have it. 4696 */ 4697 for (sigrdataset = ISC_LIST_HEAD(name->list); 4698 sigrdataset != NULL; 4699 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) { 4700 if (sigrdataset->type == dns_rdatatype_rrsig && 4701 sigrdataset->covers == rdataset->type) 4702 break; 4703 } 4704 4705 /* 4706 * If this RRset is in a secure domain, is in bailiwick, 4707 * and is not glue, attempt DNSSEC validation. (We do not 4708 * attempt to validate glue or out-of-bailiwick data--even 4709 * though there might be some performance benefit to doing 4710 * so--because it makes it simpler and safer to ensure that 4711 * records from a secure domain are only cached if validated 4712 * within the context of a query to the domain that owns 4713 * them.) 4714 */ 4715 if (secure_domain && rdataset->trust != dns_trust_glue && 4716 !EXTERNAL(rdataset)) { 4717 dns_trust_t trust; 4718 4719 /* 4720 * RRSIGs are validated as part of validating the 4721 * type they cover. 4722 */ 4723 if (rdataset->type == dns_rdatatype_rrsig) 4724 continue; 4725 4726 if (sigrdataset == NULL) { 4727 if (!ANSWER(rdataset) && need_validation) { 4728 /* 4729 * Ignore non-answer rdatasets that 4730 * are missing signatures. 4731 */ 4732 continue; 4733 } 4734 } 4735 4736 /* 4737 * Normalize the rdataset and sigrdataset TTLs. 4738 */ 4739 if (sigrdataset != NULL) { 4740 rdataset->ttl = ISC_MIN(rdataset->ttl, 4741 sigrdataset->ttl); 4742 sigrdataset->ttl = rdataset->ttl; 4743 } 4744 4745 /* 4746 * Cache this rdataset/sigrdataset pair as 4747 * pending data. Track whether it was additional 4748 * or not. 4749 */ 4750 if (rdataset->trust == dns_trust_additional) 4751 trust = dns_trust_pending_additional; 4752 else 4753 trust = dns_trust_pending_answer; 4754 4755 rdataset->trust = trust; 4756 if (sigrdataset != NULL) 4757 sigrdataset->trust = trust; 4758 if (!need_validation || !ANSWER(rdataset)) { 4759 if (ANSWER(rdataset) && 4760 rdataset->type != dns_rdatatype_rrsig) { 4761 isc_result_t tresult; 4762 dns_name_t *noqname = NULL; 4763 tresult = findnoqname(fctx, name, 4764 rdataset->type, 4765 &noqname); 4766 if (tresult == ISC_R_SUCCESS && 4767 noqname != NULL) { 4768 tresult = 4769 dns_rdataset_addnoqname( 4770 rdataset, noqname); 4771 RUNTIME_CHECK(tresult == 4772 ISC_R_SUCCESS); 4773 } 4774 } 4775 addedrdataset = ardataset; 4776 result = dns_db_addrdataset(fctx->cache, node, 4777 NULL, now, rdataset, 4778 0, addedrdataset); 4779 if (result == DNS_R_UNCHANGED) { 4780 result = ISC_R_SUCCESS; 4781 if (!need_validation && 4782 ardataset != NULL && 4783 NEGATIVE(ardataset)) { 4784 /* 4785 * The answer in the cache is 4786 * better than the answer we 4787 * found, and is a negative 4788 * cache entry, so we must set 4789 * eresult appropriately. 4790 */ 4791 if (NXDOMAIN(ardataset)) 4792 eresult = 4793 DNS_R_NCACHENXDOMAIN; 4794 else 4795 eresult = 4796 DNS_R_NCACHENXRRSET; 4797 /* 4798 * We have a negative response 4799 * from the cache so don't 4800 * attempt to add the RRSIG 4801 * rrset. 4802 */ 4803 continue; 4804 } 4805 } 4806 if (result != ISC_R_SUCCESS) 4807 break; 4808 if (sigrdataset != NULL) { 4809 addedrdataset = asigrdataset; 4810 result = dns_db_addrdataset(fctx->cache, 4811 node, NULL, now, 4812 sigrdataset, 0, 4813 addedrdataset); 4814 if (result == DNS_R_UNCHANGED) 4815 result = ISC_R_SUCCESS; 4816 if (result != ISC_R_SUCCESS) 4817 break; 4818 } else if (!ANSWER(rdataset)) 4819 continue; 4820 } 4821 4822 if (ANSWER(rdataset) && need_validation) { 4823 if (fctx->type != dns_rdatatype_any && 4824 fctx->type != dns_rdatatype_rrsig && 4825 fctx->type != dns_rdatatype_sig) { 4826 /* 4827 * This is The Answer. We will 4828 * validate it, but first we cache 4829 * the rest of the response - it may 4830 * contain useful keys. 4831 */ 4832 INSIST(valrdataset == NULL && 4833 valsigrdataset == NULL); 4834 valrdataset = rdataset; 4835 valsigrdataset = sigrdataset; 4836 } else { 4837 /* 4838 * This is one of (potentially) 4839 * multiple answers to an ANY 4840 * or SIG query. To keep things 4841 * simple, we just start the 4842 * validator right away rather 4843 * than caching first and 4844 * having to remember which 4845 * rdatasets needed validation. 4846 */ 4847 result = valcreate(fctx, addrinfo, 4848 name, rdataset->type, 4849 rdataset, 4850 sigrdataset, 4851 valoptions, task); 4852 /* 4853 * Defer any further validations. 4854 * This prevents multiple validators 4855 * from manipulating fctx->rmessage 4856 * simultaneously. 4857 */ 4858 valoptions |= DNS_VALIDATOR_DEFER; 4859 } 4860 } else if (CHAINING(rdataset)) { 4861 if (rdataset->type == dns_rdatatype_cname) 4862 eresult = DNS_R_CNAME; 4863 else { 4864 INSIST(rdataset->type == 4865 dns_rdatatype_dname); 4866 eresult = DNS_R_DNAME; 4867 } 4868 } 4869 } else if (!EXTERNAL(rdataset)) { 4870 /* 4871 * It's OK to cache this rdataset now. 4872 */ 4873 if (ANSWER(rdataset)) 4874 addedrdataset = ardataset; 4875 else if (ANSWERSIG(rdataset)) 4876 addedrdataset = asigrdataset; 4877 else 4878 addedrdataset = NULL; 4879 if (CHAINING(rdataset)) { 4880 if (rdataset->type == dns_rdatatype_cname) 4881 eresult = DNS_R_CNAME; 4882 else { 4883 INSIST(rdataset->type == 4884 dns_rdatatype_dname); 4885 eresult = DNS_R_DNAME; 4886 } 4887 } 4888 if (rdataset->trust == dns_trust_glue && 4889 (rdataset->type == dns_rdatatype_ns || 4890 (rdataset->type == dns_rdatatype_rrsig && 4891 rdataset->covers == dns_rdatatype_ns))) { 4892 /* 4893 * If the trust level is 'dns_trust_glue' 4894 * then we are adding data from a referral 4895 * we got while executing the search algorithm. 4896 * New referral data always takes precedence 4897 * over the existing cache contents. 4898 */ 4899 options = DNS_DBADD_FORCE; 4900 } else 4901 options = 0; 4902 4903 if (ANSWER(rdataset) && 4904 rdataset->type != dns_rdatatype_rrsig) { 4905 isc_result_t tresult; 4906 dns_name_t *noqname = NULL; 4907 tresult = findnoqname(fctx, name, 4908 rdataset->type, &noqname); 4909 if (tresult == ISC_R_SUCCESS && 4910 noqname != NULL) { 4911 tresult = dns_rdataset_addnoqname( 4912 rdataset, noqname); 4913 RUNTIME_CHECK(tresult == ISC_R_SUCCESS); 4914 } 4915 } 4916 4917 /* 4918 * Now we can add the rdataset. 4919 */ 4920 result = dns_db_addrdataset(fctx->cache, 4921 node, NULL, now, 4922 rdataset, 4923 options, 4924 addedrdataset); 4925 4926 if (result == DNS_R_UNCHANGED) { 4927 if (ANSWER(rdataset) && 4928 ardataset != NULL && 4929 NEGATIVE(ardataset)) { 4930 /* 4931 * The answer in the cache is better 4932 * than the answer we found, and is 4933 * a negative cache entry, so we 4934 * must set eresult appropriately. 4935 */ 4936 if (NXDOMAIN(ardataset)) 4937 eresult = DNS_R_NCACHENXDOMAIN; 4938 else 4939 eresult = DNS_R_NCACHENXRRSET; 4940 } 4941 result = ISC_R_SUCCESS; 4942 } else if (result != ISC_R_SUCCESS) 4943 break; 4944 } 4945 } 4946 4947 if (valrdataset != NULL) 4948 result = valcreate(fctx, addrinfo, name, fctx->type, 4949 valrdataset, valsigrdataset, valoptions, 4950 task); 4951 4952 if (result == ISC_R_SUCCESS && have_answer) { 4953 fctx->attributes |= FCTX_ATTR_HAVEANSWER; 4954 if (event != NULL) { 4955 /* 4956 * Negative results must be indicated in event->result. 4957 */ 4958 if (dns_rdataset_isassociated(event->rdataset) && 4959 NEGATIVE(event->rdataset)) { 4960 INSIST(eresult == DNS_R_NCACHENXDOMAIN || 4961 eresult == DNS_R_NCACHENXRRSET); 4962 } 4963 event->result = eresult; 4964 dns_db_attach(fctx->cache, adbp); 4965 dns_db_transfernode(fctx->cache, &node, anodep); 4966 clone_results(fctx); 4967 } 4968 } 4969 4970 if (node != NULL) 4971 dns_db_detachnode(fctx->cache, &node); 4972 4973 return (result); 4974} 4975 4976static inline isc_result_t 4977cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now) 4978{ 4979 isc_result_t result; 4980 dns_section_t section; 4981 dns_name_t *name; 4982 4983 FCTXTRACE("cache_message"); 4984 4985 fctx->attributes &= ~FCTX_ATTR_WANTCACHE; 4986 4987 LOCK(&fctx->res->buckets[fctx->bucketnum].lock); 4988 4989 for (section = DNS_SECTION_ANSWER; 4990 section <= DNS_SECTION_ADDITIONAL; 4991 section++) { 4992 result = dns_message_firstname(fctx->rmessage, section); 4993 while (result == ISC_R_SUCCESS) { 4994 name = NULL; 4995 dns_message_currentname(fctx->rmessage, section, 4996 &name); 4997 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) { 4998 result = cache_name(fctx, name, addrinfo, now); 4999 if (result != ISC_R_SUCCESS) 5000 break; 5001 } 5002 result = dns_message_nextname(fctx->rmessage, section); 5003 } 5004 if (result != ISC_R_NOMORE) 5005 break; 5006 } 5007 if (result == ISC_R_NOMORE) 5008 result = ISC_R_SUCCESS; 5009 5010 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock); 5011 5012 return (result); 5013} 5014 5015/* 5016 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult. 5017 */ 5018static isc_result_t 5019ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node, 5020 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl, 5021 isc_boolean_t optout, isc_boolean_t secure, 5022 dns_rdataset_t *ardataset, isc_result_t *eresultp) 5023{ 5024 isc_result_t result; 5025 dns_rdataset_t rdataset; 5026 5027 if (ardataset == NULL) { 5028 dns_rdataset_init(&rdataset); 5029 ardataset = &rdataset; 5030 } 5031 if (secure) 5032 result = dns_ncache_addoptout(message, cache, node, covers, 5033 now, maxttl, optout, ardataset); 5034 else 5035 result = dns_ncache_add(message, cache, node, covers, now, 5036 maxttl, ardataset); 5037 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) { 5038 /* 5039 * If the cache now contains a negative entry and we 5040 * care about whether it is DNS_R_NCACHENXDOMAIN or 5041 * DNS_R_NCACHENXRRSET then extract it. 5042 */ 5043 if (NEGATIVE(ardataset)) { 5044 /* 5045 * The cache data is a negative cache entry. 5046 */ 5047 if (NXDOMAIN(ardataset)) 5048 *eresultp = DNS_R_NCACHENXDOMAIN; 5049 else 5050 *eresultp = DNS_R_NCACHENXRRSET; 5051 } else { 5052 /* 5053 * Either we don't care about the nature of the 5054 * cache rdataset (because no fetch is interested 5055 * in the outcome), or the cache rdataset is not 5056 * a negative cache entry. Whichever case it is, 5057 * we can return success. 5058 * 5059 * XXXRTH There's a CNAME/DNAME problem here. 5060 */ 5061 *eresultp = ISC_R_SUCCESS; 5062 } 5063 result = ISC_R_SUCCESS; 5064 } 5065 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset)) 5066 dns_rdataset_disassociate(ardataset); 5067 5068 return (result); 5069} 5070 5071static inline isc_result_t 5072ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, 5073 dns_rdatatype_t covers, isc_stdtime_t now) 5074{ 5075 isc_result_t result, eresult; 5076 dns_name_t *name; 5077 dns_resolver_t *res; 5078 dns_db_t **adbp; 5079 dns_dbnode_t *node, **anodep; 5080 dns_rdataset_t *ardataset; 5081 isc_boolean_t need_validation, secure_domain; 5082 dns_name_t *aname; 5083 dns_fetchevent_t *event; 5084 isc_uint32_t ttl; 5085 unsigned int valoptions = 0; 5086 5087 FCTXTRACE("ncache_message"); 5088 5089 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE; 5090 5091 res = fctx->res; 5092 need_validation = ISC_FALSE; 5093 POST(need_validation); 5094 secure_domain = ISC_FALSE; 5095 eresult = ISC_R_SUCCESS; 5096 name = &fctx->name; 5097 node = NULL; 5098 5099 /* 5100 * XXXMPA remove when we follow cnames and adjust the setting 5101 * of FCTX_ATTR_WANTNCACHE in noanswer_response(). 5102 */ 5103 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0); 5104 5105 /* 5106 * Is DNSSEC validation required for this name? 5107 */ 5108 if (fctx->res->view->enablevalidation) { 5109 result = dns_view_issecuredomain(res->view, name, 5110 &secure_domain); 5111 if (result != ISC_R_SUCCESS) 5112 return (result); 5113 5114 if (!secure_domain && res->view->dlv != NULL) { 5115 valoptions = DNS_VALIDATOR_DLV; 5116 secure_domain = ISC_TRUE; 5117 } 5118 } 5119 5120 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0) 5121 need_validation = ISC_FALSE; 5122 else 5123 need_validation = secure_domain; 5124 5125 if (secure_domain) { 5126 /* 5127 * Mark all rdatasets as pending. 5128 */ 5129 dns_rdataset_t *trdataset; 5130 dns_name_t *tname; 5131 5132 result = dns_message_firstname(fctx->rmessage, 5133 DNS_SECTION_AUTHORITY); 5134 while (result == ISC_R_SUCCESS) { 5135 tname = NULL; 5136 dns_message_currentname(fctx->rmessage, 5137 DNS_SECTION_AUTHORITY, 5138 &tname); 5139 for (trdataset = ISC_LIST_HEAD(tname->list); 5140 trdataset != NULL; 5141 trdataset = ISC_LIST_NEXT(trdataset, link)) 5142 trdataset->trust = dns_trust_pending_answer; 5143 result = dns_message_nextname(fctx->rmessage, 5144 DNS_SECTION_AUTHORITY); 5145 } 5146 if (result != ISC_R_NOMORE) 5147 return (result); 5148 5149 } 5150 5151 if (need_validation) { 5152 /* 5153 * Do negative response validation. 5154 */ 5155 result = valcreate(fctx, addrinfo, name, fctx->type, 5156 NULL, NULL, valoptions, 5157 res->buckets[fctx->bucketnum].task); 5158 /* 5159 * If validation is necessary, return now. Otherwise continue 5160 * to process the message, letting the validation complete 5161 * in its own good time. 5162 */ 5163 return (result); 5164 } 5165 5166 LOCK(&res->buckets[fctx->bucketnum].lock); 5167 5168 adbp = NULL; 5169 aname = NULL; 5170 anodep = NULL; 5171 ardataset = NULL; 5172 if (!HAVE_ANSWER(fctx)) { 5173 event = ISC_LIST_HEAD(fctx->events); 5174 if (event != NULL) { 5175 adbp = &event->db; 5176 aname = dns_fixedname_name(&event->foundname); 5177 result = dns_name_copy(name, aname, NULL); 5178 if (result != ISC_R_SUCCESS) 5179 goto unlock; 5180 anodep = &event->node; 5181 ardataset = event->rdataset; 5182 } 5183 } else 5184 event = NULL; 5185 5186 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node); 5187 if (result != ISC_R_SUCCESS) 5188 goto unlock; 5189 5190 /* 5191 * If we are asking for a SOA record set the cache time 5192 * to zero to facilitate locating the containing zone of 5193 * a arbitrary zone. 5194 */ 5195 ttl = fctx->res->view->maxncachettl; 5196 if (fctx->type == dns_rdatatype_soa && 5197 covers == dns_rdatatype_any && 5198 fctx->res->zero_no_soa_ttl) 5199 ttl = 0; 5200 5201 result = ncache_adderesult(fctx->rmessage, fctx->cache, node, 5202 covers, now, ttl, ISC_FALSE, 5203 ISC_FALSE, ardataset, &eresult); 5204 if (result != ISC_R_SUCCESS) 5205 goto unlock; 5206 5207 if (!HAVE_ANSWER(fctx)) { 5208 fctx->attributes |= FCTX_ATTR_HAVEANSWER; 5209 if (event != NULL) { 5210 event->result = eresult; 5211 dns_db_attach(fctx->cache, adbp); 5212 dns_db_transfernode(fctx->cache, &node, anodep); 5213 clone_results(fctx); 5214 } 5215 } 5216 5217 unlock: 5218 UNLOCK(&res->buckets[fctx->bucketnum].lock); 5219 5220 if (node != NULL) 5221 dns_db_detachnode(fctx->cache, &node); 5222 5223 return (result); 5224} 5225 5226static inline void 5227mark_related(dns_name_t *name, dns_rdataset_t *rdataset, 5228 isc_boolean_t external, isc_boolean_t gluing) 5229{ 5230 name->attributes |= DNS_NAMEATTR_CACHE; 5231 if (gluing) { 5232 rdataset->trust = dns_trust_glue; 5233 /* 5234 * Glue with 0 TTL causes problems. We force the TTL to 5235 * 1 second to prevent this. 5236 */ 5237 if (rdataset->ttl == 0) 5238 rdataset->ttl = 1; 5239 } else 5240 rdataset->trust = dns_trust_additional; 5241 /* 5242 * Avoid infinite loops by only marking new rdatasets. 5243 */ 5244 if (!CACHE(rdataset)) { 5245 name->attributes |= DNS_NAMEATTR_CHASE; 5246 rdataset->attributes |= DNS_RDATASETATTR_CHASE; 5247 } 5248 rdataset->attributes |= DNS_RDATASETATTR_CACHE; 5249 if (external) 5250 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL; 5251} 5252 5253static isc_result_t 5254check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type, 5255 dns_section_t section) 5256{ 5257 fetchctx_t *fctx = arg; 5258 isc_result_t result; 5259 dns_name_t *name; 5260 dns_rdataset_t *rdataset; 5261 isc_boolean_t external; 5262 dns_rdatatype_t rtype; 5263 isc_boolean_t gluing; 5264 5265 REQUIRE(VALID_FCTX(fctx)); 5266 5267#if CHECK_FOR_GLUE_IN_ANSWER 5268 if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a) 5269 return (ISC_R_SUCCESS); 5270#endif 5271 5272 if (GLUING(fctx)) 5273 gluing = ISC_TRUE; 5274 else 5275 gluing = ISC_FALSE; 5276 name = NULL; 5277 rdataset = NULL; 5278 result = dns_message_findname(fctx->rmessage, section, addname, 5279 dns_rdatatype_any, 0, &name, NULL); 5280 if (result == ISC_R_SUCCESS) { 5281 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain)); 5282 if (type == dns_rdatatype_a) { 5283 for (rdataset = ISC_LIST_HEAD(name->list); 5284 rdataset != NULL; 5285 rdataset = ISC_LIST_NEXT(rdataset, link)) { 5286 if (rdataset->type == dns_rdatatype_rrsig) 5287 rtype = rdataset->covers; 5288 else 5289 rtype = rdataset->type; 5290 if (rtype == dns_rdatatype_a || 5291 rtype == dns_rdatatype_aaaa) 5292 mark_related(name, rdataset, external, 5293 gluing); 5294 } 5295 } else { 5296 result = dns_message_findtype(name, type, 0, 5297 &rdataset); 5298 if (result == ISC_R_SUCCESS) { 5299 mark_related(name, rdataset, external, gluing); 5300 /* 5301 * Do we have its SIG too? 5302 */ 5303 rdataset = NULL; 5304 result = dns_message_findtype(name, 5305 dns_rdatatype_rrsig, 5306 type, &rdataset); 5307 if (result == ISC_R_SUCCESS) 5308 mark_related(name, rdataset, external, 5309 gluing); 5310 } 5311 } 5312 } 5313 5314 return (ISC_R_SUCCESS); 5315} 5316 5317static isc_result_t 5318check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) { 5319 return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL)); 5320} 5321 5322#ifndef CHECK_FOR_GLUE_IN_ANSWER 5323#define CHECK_FOR_GLUE_IN_ANSWER 0 5324#endif 5325#if CHECK_FOR_GLUE_IN_ANSWER 5326static isc_result_t 5327check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) { 5328 return (check_section(arg, addname, type, DNS_SECTION_ANSWER)); 5329} 5330#endif 5331 5332static void 5333chase_additional(fetchctx_t *fctx) { 5334 isc_boolean_t rescan; 5335 dns_section_t section = DNS_SECTION_ADDITIONAL; 5336 isc_result_t result; 5337 5338 again: 5339 rescan = ISC_FALSE; 5340 5341 for (result = dns_message_firstname(fctx->rmessage, section); 5342 result == ISC_R_SUCCESS; 5343 result = dns_message_nextname(fctx->rmessage, section)) { 5344 dns_name_t *name = NULL; 5345 dns_rdataset_t *rdataset; 5346 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL, 5347 &name); 5348 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0) 5349 continue; 5350 name->attributes &= ~DNS_NAMEATTR_CHASE; 5351 for (rdataset = ISC_LIST_HEAD(name->list); 5352 rdataset != NULL; 5353 rdataset = ISC_LIST_NEXT(rdataset, link)) { 5354 if (CHASE(rdataset)) { 5355 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE; 5356 (void)dns_rdataset_additionaldata(rdataset, 5357 check_related, 5358 fctx); 5359 rescan = ISC_TRUE; 5360 } 5361 } 5362 } 5363 if (rescan) 5364 goto again; 5365} 5366 5367static inline isc_result_t 5368cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) { 5369 isc_result_t result; 5370 dns_rdata_t rdata = DNS_RDATA_INIT; 5371 dns_rdata_cname_t cname; 5372 5373 result = dns_rdataset_first(rdataset); 5374 if (result != ISC_R_SUCCESS) 5375 return (result); 5376 dns_rdataset_current(rdataset, &rdata); 5377 result = dns_rdata_tostruct(&rdata, &cname, NULL); 5378 if (result != ISC_R_SUCCESS) 5379 return (result); 5380 dns_name_init(tname, NULL); 5381 dns_name_clone(&cname.cname, tname); 5382 dns_rdata_freestruct(&cname); 5383 5384 return (ISC_R_SUCCESS); 5385} 5386 5387static inline isc_result_t 5388dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname, 5389 dns_name_t *oname, dns_fixedname_t *fixeddname) 5390{ 5391 isc_result_t result; 5392 dns_rdata_t rdata = DNS_RDATA_INIT; 5393 unsigned int nlabels; 5394 int order; 5395 dns_namereln_t namereln; 5396 dns_rdata_dname_t dname; 5397 dns_fixedname_t prefix; 5398 5399 /* 5400 * Get the target name of the DNAME. 5401 */ 5402 result = dns_rdataset_first(rdataset); 5403 if (result != ISC_R_SUCCESS) 5404 return (result); 5405 dns_rdataset_current(rdataset, &rdata); 5406 result = dns_rdata_tostruct(&rdata, &dname, NULL); 5407 if (result != ISC_R_SUCCESS) 5408 return (result); 5409 5410 /* 5411 * Get the prefix of qname. 5412 */ 5413 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels); 5414 if (namereln != dns_namereln_subdomain) { 5415 char qbuf[DNS_NAME_FORMATSIZE]; 5416 char obuf[DNS_NAME_FORMATSIZE]; 5417 5418 dns_rdata_freestruct(&dname); 5419 dns_name_format(qname, qbuf, sizeof(qbuf)); 5420 dns_name_format(oname, obuf, sizeof(obuf)); 5421 log_formerr(fctx, "unrelated DNAME in answer: " 5422 "%s is not in %s", qbuf, obuf); 5423 return (DNS_R_FORMERR); 5424 } 5425 dns_fixedname_init(&prefix); 5426 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL); 5427 dns_fixedname_init(fixeddname); 5428 result = dns_name_concatenate(dns_fixedname_name(&prefix), 5429 &dname.dname, 5430 dns_fixedname_name(fixeddname), NULL); 5431 dns_rdata_freestruct(&dname); 5432 return (result); 5433} 5434 5435static isc_boolean_t 5436is_answeraddress_allowed(dns_view_t *view, dns_name_t *name, 5437 dns_rdataset_t *rdataset) 5438{ 5439 isc_result_t result; 5440 dns_rdata_t rdata = DNS_RDATA_INIT; 5441 struct in_addr ina; 5442 struct in6_addr in6a; 5443 isc_netaddr_t netaddr; 5444 char addrbuf[ISC_NETADDR_FORMATSIZE]; 5445 char namebuf[DNS_NAME_FORMATSIZE]; 5446 char classbuf[64]; 5447 char typebuf[64]; 5448 int match; 5449 5450 /* By default, we allow any addresses. */ 5451 if (view->denyansweracl == NULL) 5452 return (ISC_TRUE); 5453 5454 /* 5455 * If the owner name matches one in the exclusion list, either exactly 5456 * or partially, allow it. 5457 */ 5458 if (view->answeracl_exclude != NULL) { 5459 dns_rbtnode_t *node = NULL; 5460 5461 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL, 5462 &node, NULL, 0, NULL, NULL); 5463 5464 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) 5465 return (ISC_TRUE); 5466 } 5467 5468 /* 5469 * Otherwise, search the filter list for a match for each address 5470 * record. If a match is found, the address should be filtered, 5471 * so should the entire answer. 5472 */ 5473 for (result = dns_rdataset_first(rdataset); 5474 result == ISC_R_SUCCESS; 5475 result = dns_rdataset_next(rdataset)) { 5476 dns_rdata_reset(&rdata); 5477 dns_rdataset_current(rdataset, &rdata); 5478 if (rdataset->type == dns_rdatatype_a) { 5479 INSIST(rdata.length == sizeof(ina.s_addr)); 5480 memmove(&ina.s_addr, rdata.data, sizeof(ina.s_addr)); 5481 isc_netaddr_fromin(&netaddr, &ina); 5482 } else { 5483 INSIST(rdata.length == sizeof(in6a.s6_addr)); 5484 memmove(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr)); 5485 isc_netaddr_fromin6(&netaddr, &in6a); 5486 } 5487 5488 result = dns_acl_match(&netaddr, NULL, view->denyansweracl, 5489 &view->aclenv, &match, NULL); 5490 5491 if (result == ISC_R_SUCCESS && match > 0) { 5492 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf)); 5493 dns_name_format(name, namebuf, sizeof(namebuf)); 5494 dns_rdatatype_format(rdataset->type, typebuf, 5495 sizeof(typebuf)); 5496 dns_rdataclass_format(rdataset->rdclass, classbuf, 5497 sizeof(classbuf)); 5498 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 5499 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 5500 "answer address %s denied for %s/%s/%s", 5501 addrbuf, namebuf, typebuf, classbuf); 5502 return (ISC_FALSE); 5503 } 5504 } 5505 5506 return (ISC_TRUE); 5507} 5508 5509static isc_boolean_t 5510is_answertarget_allowed(dns_view_t *view, dns_name_t *name, 5511 dns_rdatatype_t type, dns_name_t *tname, 5512 dns_name_t *domain) 5513{ 5514 isc_result_t result; 5515 dns_rbtnode_t *node = NULL; 5516 char qnamebuf[DNS_NAME_FORMATSIZE]; 5517 char tnamebuf[DNS_NAME_FORMATSIZE]; 5518 char classbuf[64]; 5519 char typebuf[64]; 5520 5521 /* By default, we allow any target name. */ 5522 if (view->denyanswernames == NULL) 5523 return (ISC_TRUE); 5524 5525 /* 5526 * If the owner name matches one in the exclusion list, either exactly 5527 * or partially, allow it. 5528 */ 5529 if (view->answernames_exclude != NULL) { 5530 result = dns_rbt_findnode(view->answernames_exclude, name, NULL, 5531 &node, NULL, 0, NULL, NULL); 5532 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) 5533 return (ISC_TRUE); 5534 } 5535 5536 /* 5537 * If the target name is a subdomain of the search domain, allow it. 5538 */ 5539 if (dns_name_issubdomain(tname, domain)) 5540 return (ISC_TRUE); 5541 5542 /* 5543 * Otherwise, apply filters. 5544 */ 5545 result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node, 5546 NULL, 0, NULL, NULL); 5547 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) { 5548 dns_name_format(name, qnamebuf, sizeof(qnamebuf)); 5549 dns_name_format(tname, tnamebuf, sizeof(tnamebuf)); 5550 dns_rdatatype_format(type, typebuf, sizeof(typebuf)); 5551 dns_rdataclass_format(view->rdclass, classbuf, 5552 sizeof(classbuf)); 5553 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 5554 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 5555 "%s target %s denied for %s/%s", 5556 typebuf, tnamebuf, qnamebuf, classbuf); 5557 return (ISC_FALSE); 5558 } 5559 5560 return (ISC_TRUE); 5561} 5562 5563static void 5564trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) { 5565 char ns_namebuf[DNS_NAME_FORMATSIZE]; 5566 char namebuf[DNS_NAME_FORMATSIZE]; 5567 char tbuf[DNS_RDATATYPE_FORMATSIZE]; 5568 5569 if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) { 5570 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf)); 5571 dns_name_format(&fctx->name, namebuf, sizeof(namebuf)); 5572 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf)); 5573 5574 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 5575 DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10), 5576 "fctx %p: trimming ttl of %s/NS for %s/%s: " 5577 "%u -> %u", fctx, ns_namebuf, namebuf, tbuf, 5578 rdataset->ttl, fctx->ns_ttl); 5579 rdataset->ttl = fctx->ns_ttl; 5580 } 5581} 5582 5583/* 5584 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral). 5585 * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer 5586 * section for the NS RRset if the query type is NS; if it has 5587 * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer 5588 * section for A and AAAA queries. 5589 */ 5590#define LOOK_FOR_NS_IN_ANSWER 0x1 5591#define LOOK_FOR_GLUE_IN_ANSWER 0x2 5592 5593static isc_result_t 5594noanswer_response(fetchctx_t *fctx, dns_name_t *oqname, 5595 unsigned int look_in_options) 5596{ 5597 isc_result_t result; 5598 dns_message_t *message; 5599 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name, *save_name; 5600 dns_rdataset_t *rdataset, *ns_rdataset; 5601 isc_boolean_t aa, negative_response; 5602 dns_rdatatype_t type, save_type; 5603 dns_section_t section; 5604 5605 FCTXTRACE("noanswer_response"); 5606 5607 if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) { 5608 INSIST(fctx->type == dns_rdatatype_ns); 5609 section = DNS_SECTION_ANSWER; 5610 } else 5611 section = DNS_SECTION_AUTHORITY; 5612 5613 message = fctx->rmessage; 5614 5615 /* 5616 * Setup qname. 5617 */ 5618 if (oqname == NULL) { 5619 /* 5620 * We have a normal, non-chained negative response or 5621 * referral. 5622 */ 5623 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0) 5624 aa = ISC_TRUE; 5625 else 5626 aa = ISC_FALSE; 5627 qname = &fctx->name; 5628 } else { 5629 /* 5630 * We're being invoked by answer_response() after it has 5631 * followed a CNAME/DNAME chain. 5632 */ 5633 qname = oqname; 5634 aa = ISC_FALSE; 5635 /* 5636 * If the current qname is not a subdomain of the query 5637 * domain, there's no point in looking at the authority 5638 * section without doing DNSSEC validation. 5639 * 5640 * Until we do that validation, we'll just return success 5641 * in this case. 5642 */ 5643 if (!dns_name_issubdomain(qname, &fctx->domain)) 5644 return (ISC_R_SUCCESS); 5645 } 5646 5647 /* 5648 * We have to figure out if this is a negative response, or a 5649 * referral. 5650 */ 5651 5652 /* 5653 * Sometimes we can tell if its a negative response by looking at 5654 * the message header. 5655 */ 5656 negative_response = ISC_FALSE; 5657 if (message->rcode == dns_rcode_nxdomain || 5658 (message->counts[DNS_SECTION_ANSWER] == 0 && 5659 message->counts[DNS_SECTION_AUTHORITY] == 0)) 5660 negative_response = ISC_TRUE; 5661 5662 /* 5663 * Process the authority section. 5664 */ 5665 ns_name = NULL; 5666 ns_rdataset = NULL; 5667 soa_name = NULL; 5668 ds_name = NULL; 5669 save_name = NULL; 5670 save_type = dns_rdatatype_none; 5671 result = dns_message_firstname(message, section); 5672 while (result == ISC_R_SUCCESS) { 5673 name = NULL; 5674 dns_message_currentname(message, section, &name); 5675 if (dns_name_issubdomain(name, &fctx->domain)) { 5676 /* 5677 * Look for NS/SOA RRsets first. 5678 */ 5679 for (rdataset = ISC_LIST_HEAD(name->list); 5680 rdataset != NULL; 5681 rdataset = ISC_LIST_NEXT(rdataset, link)) { 5682 type = rdataset->type; 5683 if (type == dns_rdatatype_rrsig) 5684 type = rdataset->covers; 5685 if (((type == dns_rdatatype_ns || 5686 type == dns_rdatatype_soa) && 5687 !dns_name_issubdomain(qname, name))) { 5688 char qbuf[DNS_NAME_FORMATSIZE]; 5689 char nbuf[DNS_NAME_FORMATSIZE]; 5690 char tbuf[DNS_RDATATYPE_FORMATSIZE]; 5691 dns_rdatatype_format(fctx->type, tbuf, 5692 sizeof(tbuf)); 5693 dns_name_format(name, nbuf, 5694 sizeof(nbuf)); 5695 dns_name_format(qname, qbuf, 5696 sizeof(qbuf)); 5697 log_formerr(fctx, 5698 "unrelated %s %s in " 5699 "%s authority section", 5700 tbuf, qbuf, nbuf); 5701 return (DNS_R_FORMERR); 5702 } 5703 if (type == dns_rdatatype_ns) { 5704 /* 5705 * NS or RRSIG NS. 5706 * 5707 * Only one set of NS RRs is allowed. 5708 */ 5709 if (rdataset->type == 5710 dns_rdatatype_ns) { 5711 if (ns_name != NULL && 5712 name != ns_name) { 5713 log_formerr(fctx, 5714 "multiple NS " 5715 "RRsets in " 5716 "authority " 5717 "section"); 5718 return (DNS_R_FORMERR); 5719 } 5720 ns_name = name; 5721 ns_rdataset = rdataset; 5722 } 5723 name->attributes |= 5724 DNS_NAMEATTR_CACHE; 5725 rdataset->attributes |= 5726 DNS_RDATASETATTR_CACHE; 5727 rdataset->trust = dns_trust_glue; 5728 } 5729 if (type == dns_rdatatype_soa) { 5730 /* 5731 * SOA, or RRSIG SOA. 5732 * 5733 * Only one SOA is allowed. 5734 */ 5735 if (rdataset->type == 5736 dns_rdatatype_soa) { 5737 if (soa_name != NULL && 5738 name != soa_name) { 5739 log_formerr(fctx, 5740 "multiple SOA " 5741 "RRs in " 5742 "authority " 5743 "section"); 5744 return (DNS_R_FORMERR); 5745 } 5746 soa_name = name; 5747 } 5748 name->attributes |= 5749 DNS_NAMEATTR_NCACHE; 5750 rdataset->attributes |= 5751 DNS_RDATASETATTR_NCACHE; 5752 if (aa) 5753 rdataset->trust = 5754 dns_trust_authauthority; 5755 else if (ISFORWARDER(fctx->addrinfo)) 5756 rdataset->trust = 5757 dns_trust_answer; 5758 else 5759 rdataset->trust = 5760 dns_trust_additional; 5761 } 5762 } 5763 } 5764 result = dns_message_nextname(message, section); 5765 if (result == ISC_R_NOMORE) 5766 break; 5767 else if (result != ISC_R_SUCCESS) 5768 return (result); 5769 } 5770 5771 log_ns_ttl(fctx, "noanswer_response"); 5772 5773 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) && 5774 !dns_name_equal(ns_name, dns_rootname)) 5775 trim_ns_ttl(fctx, ns_name, ns_rdataset); 5776 5777 /* 5778 * A negative response has a SOA record (Type 2) 5779 * and a optional NS RRset (Type 1) or it has neither 5780 * a SOA or a NS RRset (Type 3, handled above) or 5781 * rcode is NXDOMAIN (handled above) in which case 5782 * the NS RRset is allowed (Type 4). 5783 */ 5784 if (soa_name != NULL) 5785 negative_response = ISC_TRUE; 5786 5787 result = dns_message_firstname(message, section); 5788 while (result == ISC_R_SUCCESS) { 5789 name = NULL; 5790 dns_message_currentname(message, section, &name); 5791 if (dns_name_issubdomain(name, &fctx->domain)) { 5792 for (rdataset = ISC_LIST_HEAD(name->list); 5793 rdataset != NULL; 5794 rdataset = ISC_LIST_NEXT(rdataset, link)) { 5795 type = rdataset->type; 5796 if (type == dns_rdatatype_rrsig) 5797 type = rdataset->covers; 5798 if (type == dns_rdatatype_nsec || 5799 type == dns_rdatatype_nsec3) { 5800 /* 5801 * NSEC or RRSIG NSEC. 5802 */ 5803 if (negative_response) { 5804 name->attributes |= 5805 DNS_NAMEATTR_NCACHE; 5806 rdataset->attributes |= 5807 DNS_RDATASETATTR_NCACHE; 5808 } else if (type == dns_rdatatype_nsec) { 5809 name->attributes |= 5810 DNS_NAMEATTR_CACHE; 5811 rdataset->attributes |= 5812 DNS_RDATASETATTR_CACHE; 5813 } 5814 if (aa) 5815 rdataset->trust = 5816 dns_trust_authauthority; 5817 else if (ISFORWARDER(fctx->addrinfo)) 5818 rdataset->trust = 5819 dns_trust_answer; 5820 else 5821 rdataset->trust = 5822 dns_trust_additional; 5823 /* 5824 * No additional data needs to be 5825 * marked. 5826 */ 5827 } else if (type == dns_rdatatype_ds) { 5828 /* 5829 * DS or SIG DS. 5830 * 5831 * These should only be here if 5832 * this is a referral, and there 5833 * should only be one DS RRset. 5834 */ 5835 if (ns_name == NULL) { 5836 log_formerr(fctx, 5837 "DS with no " 5838 "referral"); 5839 return (DNS_R_FORMERR); 5840 } 5841 if (rdataset->type == 5842 dns_rdatatype_ds) { 5843 if (ds_name != NULL && 5844 name != ds_name) { 5845 log_formerr(fctx, 5846 "DS doesn't " 5847 "match " 5848 "referral " 5849 "(NS)"); 5850 return (DNS_R_FORMERR); 5851 } 5852 ds_name = name; 5853 } 5854 name->attributes |= 5855 DNS_NAMEATTR_CACHE; 5856 rdataset->attributes |= 5857 DNS_RDATASETATTR_CACHE; 5858 if (aa) 5859 rdataset->trust = 5860 dns_trust_authauthority; 5861 else if (ISFORWARDER(fctx->addrinfo)) 5862 rdataset->trust = 5863 dns_trust_answer; 5864 else 5865 rdataset->trust = 5866 dns_trust_additional; 5867 } 5868 } 5869 } else { 5870 save_name = name; 5871 save_type = ISC_LIST_HEAD(name->list)->type; 5872 } 5873 result = dns_message_nextname(message, section); 5874 if (result == ISC_R_NOMORE) 5875 break; 5876 else if (result != ISC_R_SUCCESS) 5877 return (result); 5878 } 5879 5880 /* 5881 * Trigger lookups for DNS nameservers. 5882 */ 5883 if (negative_response && message->rcode == dns_rcode_noerror && 5884 fctx->type == dns_rdatatype_ds && soa_name != NULL && 5885 dns_name_equal(soa_name, qname) && 5886 !dns_name_equal(qname, dns_rootname)) 5887 return (DNS_R_CHASEDSSERVERS); 5888 5889 /* 5890 * Did we find anything? 5891 */ 5892 if (!negative_response && ns_name == NULL) { 5893 /* 5894 * Nope. 5895 */ 5896 if (oqname != NULL) { 5897 /* 5898 * We've already got a partial CNAME/DNAME chain, 5899 * and haven't found else anything useful here, but 5900 * no error has occurred since we have an answer. 5901 */ 5902 return (ISC_R_SUCCESS); 5903 } else { 5904 /* 5905 * The responder is insane. 5906 */ 5907 if (save_name == NULL) { 5908 log_formerr(fctx, "invalid response"); 5909 return (DNS_R_FORMERR); 5910 } 5911 if (!dns_name_issubdomain(save_name, &fctx->domain)) { 5912 char nbuf[DNS_NAME_FORMATSIZE]; 5913 char dbuf[DNS_NAME_FORMATSIZE]; 5914 char tbuf[DNS_RDATATYPE_FORMATSIZE]; 5915 5916 dns_rdatatype_format(save_type, tbuf, 5917 sizeof(tbuf)); 5918 dns_name_format(save_name, nbuf, sizeof(nbuf)); 5919 dns_name_format(&fctx->domain, dbuf, 5920 sizeof(dbuf)); 5921 5922 log_formerr(fctx, "Name %s (%s) not subdomain" 5923 " of zone %s -- invalid response", 5924 nbuf, tbuf, dbuf); 5925 } else { 5926 log_formerr(fctx, "invalid response"); 5927 } 5928 return (DNS_R_FORMERR); 5929 } 5930 } 5931 5932 /* 5933 * If we found both NS and SOA, they should be the same name. 5934 */ 5935 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) { 5936 log_formerr(fctx, "NS/SOA mismatch"); 5937 return (DNS_R_FORMERR); 5938 } 5939 5940 /* 5941 * Do we have a referral? (We only want to follow a referral if 5942 * we're not following a chain.) 5943 */ 5944 if (!negative_response && ns_name != NULL && oqname == NULL) { 5945 /* 5946 * We already know ns_name is a subdomain of fctx->domain. 5947 * If ns_name is equal to fctx->domain, we're not making 5948 * progress. We return DNS_R_FORMERR so that we'll keep 5949 * trying other servers. 5950 */ 5951 if (dns_name_equal(ns_name, &fctx->domain)) { 5952 log_formerr(fctx, "non-improving referral"); 5953 return (DNS_R_FORMERR); 5954 } 5955 5956 /* 5957 * If the referral name is not a parent of the query 5958 * name, consider the responder insane. 5959 */ 5960 if (! dns_name_issubdomain(&fctx->name, ns_name)) { 5961 /* Logged twice */ 5962 log_formerr(fctx, "referral to non-parent"); 5963 FCTXTRACE("referral to non-parent"); 5964 return (DNS_R_FORMERR); 5965 } 5966 5967 /* 5968 * Mark any additional data related to this rdataset. 5969 * It's important that we do this before we change the 5970 * query domain. 5971 */ 5972 INSIST(ns_rdataset != NULL); 5973 fctx->attributes |= FCTX_ATTR_GLUING; 5974 (void)dns_rdataset_additionaldata(ns_rdataset, check_related, 5975 fctx); 5976#if CHECK_FOR_GLUE_IN_ANSWER 5977 /* 5978 * Look in the answer section for "glue" that is incorrectly 5979 * returned as a answer. This is needed if the server also 5980 * minimizes the response size by not adding records to the 5981 * additional section that are in the answer section or if 5982 * the record gets dropped due to message size constraints. 5983 */ 5984 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 && 5985 (fctx->type == dns_rdatatype_aaaa || 5986 fctx->type == dns_rdatatype_a)) 5987 (void)dns_rdataset_additionaldata(ns_rdataset, 5988 check_answer, fctx); 5989#endif 5990 fctx->attributes &= ~FCTX_ATTR_GLUING; 5991 /* 5992 * NS rdatasets with 0 TTL cause problems. 5993 * dns_view_findzonecut() will not find them when we 5994 * try to follow the referral, and we'll SERVFAIL 5995 * because the best nameservers are now above QDOMAIN. 5996 * We force the TTL to 1 second to prevent this. 5997 */ 5998 if (ns_rdataset->ttl == 0) 5999 ns_rdataset->ttl = 1; 6000 /* 6001 * Set the current query domain to the referral name. 6002 * 6003 * XXXRTH We should check if we're in forward-only mode, and 6004 * if so we should bail out. 6005 */ 6006 INSIST(dns_name_countlabels(&fctx->domain) > 0); 6007 dns_name_free(&fctx->domain, fctx->mctx); 6008 if (dns_rdataset_isassociated(&fctx->nameservers)) 6009 dns_rdataset_disassociate(&fctx->nameservers); 6010 dns_name_init(&fctx->domain, NULL); 6011 result = dns_name_dup(ns_name, fctx->mctx, &fctx->domain); 6012 if (result != ISC_R_SUCCESS) 6013 return (result); 6014 fctx->attributes |= FCTX_ATTR_WANTCACHE; 6015 fctx->ns_ttl_ok = ISC_FALSE; 6016 log_ns_ttl(fctx, "DELEGATION"); 6017 return (DNS_R_DELEGATION); 6018 } 6019 6020 /* 6021 * Since we're not doing a referral, we don't want to cache any 6022 * NS RRs we may have found. 6023 */ 6024 if (ns_name != NULL) 6025 ns_name->attributes &= ~DNS_NAMEATTR_CACHE; 6026 6027 if (negative_response && oqname == NULL) 6028 fctx->attributes |= FCTX_ATTR_WANTNCACHE; 6029 6030 return (ISC_R_SUCCESS); 6031} 6032 6033static isc_result_t 6034answer_response(fetchctx_t *fctx) { 6035 isc_result_t result; 6036 dns_message_t *message; 6037 dns_name_t *name, *qname, tname, *ns_name; 6038 dns_rdataset_t *rdataset, *ns_rdataset; 6039 isc_boolean_t done, external, chaining, aa, found, want_chaining; 6040 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining; 6041 unsigned int aflag; 6042 dns_rdatatype_t type; 6043 dns_fixedname_t dname, fqname; 6044 dns_view_t *view; 6045 6046 FCTXTRACE("answer_response"); 6047 6048 message = fctx->rmessage; 6049 6050 /* 6051 * Examine the answer section, marking those rdatasets which are 6052 * part of the answer and should be cached. 6053 */ 6054 6055 done = ISC_FALSE; 6056 found_cname = ISC_FALSE; 6057 found_type = ISC_FALSE; 6058 chaining = ISC_FALSE; 6059 have_answer = ISC_FALSE; 6060 want_chaining = ISC_FALSE; 6061 POST(want_chaining); 6062 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0) 6063 aa = ISC_TRUE; 6064 else 6065 aa = ISC_FALSE; 6066 qname = &fctx->name; 6067 type = fctx->type; 6068 view = fctx->res->view; 6069 result = dns_message_firstname(message, DNS_SECTION_ANSWER); 6070 while (!done && result == ISC_R_SUCCESS) { 6071 name = NULL; 6072 dns_message_currentname(message, DNS_SECTION_ANSWER, &name); 6073 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain)); 6074 if (dns_name_equal(name, qname)) { 6075 wanted_chaining = ISC_FALSE; 6076 for (rdataset = ISC_LIST_HEAD(name->list); 6077 rdataset != NULL; 6078 rdataset = ISC_LIST_NEXT(rdataset, link)) { 6079 found = ISC_FALSE; 6080 want_chaining = ISC_FALSE; 6081 aflag = 0; 6082 if (rdataset->type == dns_rdatatype_nsec3) { 6083 /* 6084 * NSEC3 records are not allowed to 6085 * appear in the answer section. 6086 */ 6087 log_formerr(fctx, "NSEC3 in answer"); 6088 return (DNS_R_FORMERR); 6089 } 6090 6091 /* 6092 * Apply filters, if given, on answers to reject 6093 * a malicious attempt of rebinding. 6094 */ 6095 if ((rdataset->type == dns_rdatatype_a || 6096 rdataset->type == dns_rdatatype_aaaa) && 6097 !is_answeraddress_allowed(view, name, 6098 rdataset)) { 6099 return (DNS_R_SERVFAIL); 6100 } 6101 6102 if (rdataset->type == type && !found_cname) { 6103 /* 6104 * We've found an ordinary answer. 6105 */ 6106 found = ISC_TRUE; 6107 found_type = ISC_TRUE; 6108 done = ISC_TRUE; 6109 aflag = DNS_RDATASETATTR_ANSWER; 6110 } else if (type == dns_rdatatype_any) { 6111 /* 6112 * We've found an answer matching 6113 * an ANY query. There may be 6114 * more. 6115 */ 6116 found = ISC_TRUE; 6117 aflag = DNS_RDATASETATTR_ANSWER; 6118 } else if (rdataset->type == dns_rdatatype_rrsig 6119 && rdataset->covers == type 6120 && !found_cname) { 6121 /* 6122 * We've found a signature that 6123 * covers the type we're looking for. 6124 */ 6125 found = ISC_TRUE; 6126 found_type = ISC_TRUE; 6127 aflag = DNS_RDATASETATTR_ANSWERSIG; 6128 } else if (rdataset->type == 6129 dns_rdatatype_cname 6130 && !found_type) { 6131 /* 6132 * We're looking for something else, 6133 * but we found a CNAME. 6134 * 6135 * Getting a CNAME response for some 6136 * query types is an error, see 6137 * RFC 4035, Section 2.5. 6138 */ 6139 if (type == dns_rdatatype_rrsig || 6140 type == dns_rdatatype_key || 6141 type == dns_rdatatype_nsec) { 6142 char buf[DNS_RDATATYPE_FORMATSIZE]; 6143 dns_rdatatype_format(fctx->type, 6144 buf, sizeof(buf)); 6145 log_formerr(fctx, 6146 "CNAME response " 6147 "for %s RR", buf); 6148 return (DNS_R_FORMERR); 6149 } 6150 found = ISC_TRUE; 6151 found_cname = ISC_TRUE; 6152 want_chaining = ISC_TRUE; 6153 aflag = DNS_RDATASETATTR_ANSWER; 6154 result = cname_target(rdataset, 6155 &tname); 6156 if (result != ISC_R_SUCCESS) 6157 return (result); 6158 /* Apply filters on the target name. */ 6159 if (!is_answertarget_allowed(view, 6160 name, 6161 rdataset->type, 6162 &tname, 6163 &fctx->domain)) { 6164 return (DNS_R_SERVFAIL); 6165 } 6166 } else if (rdataset->type == dns_rdatatype_rrsig 6167 && rdataset->covers == 6168 dns_rdatatype_cname 6169 && !found_type) { 6170 /* 6171 * We're looking for something else, 6172 * but we found a SIG CNAME. 6173 */ 6174 found = ISC_TRUE; 6175 found_cname = ISC_TRUE; 6176 aflag = DNS_RDATASETATTR_ANSWERSIG; 6177 } 6178 6179 if (found) { 6180 /* 6181 * We've found an answer to our 6182 * question. 6183 */ 6184 name->attributes |= 6185 DNS_NAMEATTR_CACHE; 6186 rdataset->attributes |= 6187 DNS_RDATASETATTR_CACHE; 6188 rdataset->trust = dns_trust_answer; 6189 if (!chaining) { 6190 /* 6191 * This data is "the" answer 6192 * to our question only if 6193 * we're not chaining (i.e. 6194 * if we haven't followed 6195 * a CNAME or DNAME). 6196 */ 6197 INSIST(!external); 6198 if (aflag == 6199 DNS_RDATASETATTR_ANSWER) 6200 have_answer = ISC_TRUE; 6201 name->attributes |= 6202 DNS_NAMEATTR_ANSWER; 6203 rdataset->attributes |= aflag; 6204 if (aa) 6205 rdataset->trust = 6206 dns_trust_authanswer; 6207 } else if (external) { 6208 /* 6209 * This data is outside of 6210 * our query domain, and 6211 * may not be cached. 6212 */ 6213 rdataset->attributes |= 6214 DNS_RDATASETATTR_EXTERNAL; 6215 } 6216 6217 /* 6218 * Mark any additional data related 6219 * to this rdataset. 6220 */ 6221 (void)dns_rdataset_additionaldata( 6222 rdataset, 6223 check_related, 6224 fctx); 6225 6226 /* 6227 * CNAME chaining. 6228 */ 6229 if (want_chaining) { 6230 wanted_chaining = ISC_TRUE; 6231 name->attributes |= 6232 DNS_NAMEATTR_CHAINING; 6233 rdataset->attributes |= 6234 DNS_RDATASETATTR_CHAINING; 6235 qname = &tname; 6236 } 6237 } 6238 /* 6239 * We could add an "else" clause here and 6240 * log that we're ignoring this rdataset. 6241 */ 6242 } 6243 /* 6244 * If wanted_chaining is true, we've done 6245 * some chaining as the result of processing 6246 * this node, and thus we need to set 6247 * chaining to true. 6248 * 6249 * We don't set chaining inside of the 6250 * rdataset loop because doing that would 6251 * cause us to ignore the signatures of 6252 * CNAMEs. 6253 */ 6254 if (wanted_chaining) 6255 chaining = ISC_TRUE; 6256 } else { 6257 /* 6258 * Look for a DNAME (or its SIG). Anything else is 6259 * ignored. 6260 */ 6261 wanted_chaining = ISC_FALSE; 6262 for (rdataset = ISC_LIST_HEAD(name->list); 6263 rdataset != NULL; 6264 rdataset = ISC_LIST_NEXT(rdataset, link)) { 6265 isc_boolean_t found_dname = ISC_FALSE; 6266 dns_name_t *dname_name; 6267 6268 found = ISC_FALSE; 6269 aflag = 0; 6270 if (rdataset->type == dns_rdatatype_dname) { 6271 /* 6272 * We're looking for something else, 6273 * but we found a DNAME. 6274 * 6275 * If we're not chaining, then the 6276 * DNAME should not be external. 6277 */ 6278 if (!chaining && external) { 6279 log_formerr(fctx, 6280 "external DNAME"); 6281 return (DNS_R_FORMERR); 6282 } 6283 found = ISC_TRUE; 6284 want_chaining = ISC_TRUE; 6285 POST(want_chaining); 6286 aflag = DNS_RDATASETATTR_ANSWER; 6287 result = dname_target(fctx, rdataset, 6288 qname, name, 6289 &dname); 6290 if (result == ISC_R_NOSPACE) { 6291 /* 6292 * We can't construct the 6293 * DNAME target. Do not 6294 * try to continue. 6295 */ 6296 want_chaining = ISC_FALSE; 6297 POST(want_chaining); 6298 } else if (result != ISC_R_SUCCESS) 6299 return (result); 6300 else 6301 found_dname = ISC_TRUE; 6302 6303 dname_name = dns_fixedname_name(&dname); 6304 if (!is_answertarget_allowed(view, 6305 qname, 6306 rdataset->type, 6307 dname_name, 6308 &fctx->domain)) { 6309 return (DNS_R_SERVFAIL); 6310 } 6311 } else if (rdataset->type == dns_rdatatype_rrsig 6312 && rdataset->covers == 6313 dns_rdatatype_dname) { 6314 /* 6315 * We've found a signature that 6316 * covers the DNAME. 6317 */ 6318 found = ISC_TRUE; 6319 aflag = DNS_RDATASETATTR_ANSWERSIG; 6320 } 6321 6322 if (found) { 6323 /* 6324 * We've found an answer to our 6325 * question. 6326 */ 6327 name->attributes |= 6328 DNS_NAMEATTR_CACHE; 6329 rdataset->attributes |= 6330 DNS_RDATASETATTR_CACHE; 6331 rdataset->trust = dns_trust_answer; 6332 if (!chaining) { 6333 /* 6334 * This data is "the" answer 6335 * to our question only if 6336 * we're not chaining. 6337 */ 6338 INSIST(!external); 6339 if (aflag == 6340 DNS_RDATASETATTR_ANSWER) 6341 have_answer = ISC_TRUE; 6342 name->attributes |= 6343 DNS_NAMEATTR_ANSWER; 6344 rdataset->attributes |= aflag; 6345 if (aa) 6346 rdataset->trust = 6347 dns_trust_authanswer; 6348 } else if (external) { 6349 rdataset->attributes |= 6350 DNS_RDATASETATTR_EXTERNAL; 6351 } 6352 6353 /* 6354 * DNAME chaining. 6355 */ 6356 if (found_dname) { 6357 /* 6358 * Copy the dname into the 6359 * qname fixed name. 6360 * 6361 * Although we check for 6362 * failure of the copy 6363 * operation, in practice it 6364 * should never fail since 6365 * we already know that the 6366 * result fits in a fixedname. 6367 */ 6368 dns_fixedname_init(&fqname); 6369 result = dns_name_copy( 6370 dns_fixedname_name(&dname), 6371 dns_fixedname_name(&fqname), 6372 NULL); 6373 if (result != ISC_R_SUCCESS) 6374 return (result); 6375 wanted_chaining = ISC_TRUE; 6376 name->attributes |= 6377 DNS_NAMEATTR_CHAINING; 6378 rdataset->attributes |= 6379 DNS_RDATASETATTR_CHAINING; 6380 qname = dns_fixedname_name( 6381 &fqname); 6382 } 6383 } 6384 } 6385 if (wanted_chaining) 6386 chaining = ISC_TRUE; 6387 } 6388 result = dns_message_nextname(message, DNS_SECTION_ANSWER); 6389 } 6390 if (result == ISC_R_NOMORE) 6391 result = ISC_R_SUCCESS; 6392 if (result != ISC_R_SUCCESS) 6393 return (result); 6394 6395 /* 6396 * We should have found an answer. 6397 */ 6398 if (!have_answer) { 6399 log_formerr(fctx, "reply has no answer"); 6400 return (DNS_R_FORMERR); 6401 } 6402 6403 /* 6404 * This response is now potentially cacheable. 6405 */ 6406 fctx->attributes |= FCTX_ATTR_WANTCACHE; 6407 6408 /* 6409 * Did chaining end before we got the final answer? 6410 */ 6411 if (chaining) { 6412 /* 6413 * Yes. This may be a negative reply, so hand off 6414 * authority section processing to the noanswer code. 6415 * If it isn't a noanswer response, no harm will be 6416 * done. 6417 */ 6418 return (noanswer_response(fctx, qname, 0)); 6419 } 6420 6421 /* 6422 * We didn't end with an incomplete chain, so the rcode should be 6423 * "no error". 6424 */ 6425 if (message->rcode != dns_rcode_noerror) { 6426 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE " 6427 "indicates error"); 6428 return (DNS_R_FORMERR); 6429 } 6430 6431 /* 6432 * Examine the authority section (if there is one). 6433 * 6434 * We expect there to be only one owner name for all the rdatasets 6435 * in this section, and we expect that it is not external. 6436 */ 6437 done = ISC_FALSE; 6438 ns_name = NULL; 6439 ns_rdataset = NULL; 6440 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY); 6441 while (!done && result == ISC_R_SUCCESS) { 6442 name = NULL; 6443 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name); 6444 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain)); 6445 if (!external) { 6446 /* 6447 * We expect to find NS or SIG NS rdatasets, and 6448 * nothing else. 6449 */ 6450 for (rdataset = ISC_LIST_HEAD(name->list); 6451 rdataset != NULL; 6452 rdataset = ISC_LIST_NEXT(rdataset, link)) { 6453 if (rdataset->type == dns_rdatatype_ns || 6454 (rdataset->type == dns_rdatatype_rrsig && 6455 rdataset->covers == dns_rdatatype_ns)) { 6456 name->attributes |= 6457 DNS_NAMEATTR_CACHE; 6458 rdataset->attributes |= 6459 DNS_RDATASETATTR_CACHE; 6460 if (aa && !chaining) 6461 rdataset->trust = 6462 dns_trust_authauthority; 6463 else 6464 rdataset->trust = 6465 dns_trust_additional; 6466 6467 if (rdataset->type == dns_rdatatype_ns) { 6468 ns_name = name; 6469 ns_rdataset = rdataset; 6470 } 6471 /* 6472 * Mark any additional data related 6473 * to this rdataset. 6474 */ 6475 (void)dns_rdataset_additionaldata( 6476 rdataset, 6477 check_related, 6478 fctx); 6479 done = ISC_TRUE; 6480 } 6481 } 6482 } 6483 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY); 6484 } 6485 if (result == ISC_R_NOMORE) 6486 result = ISC_R_SUCCESS; 6487 6488 log_ns_ttl(fctx, "answer_response"); 6489 6490 if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) && 6491 !dns_name_equal(ns_name, dns_rootname)) 6492 trim_ns_ttl(fctx, ns_name, ns_rdataset); 6493 6494 return (result); 6495} 6496 6497static isc_boolean_t 6498fctx_decreference(fetchctx_t *fctx) { 6499 isc_boolean_t bucket_empty = ISC_FALSE; 6500 6501 INSIST(fctx->references > 0); 6502 fctx->references--; 6503 if (fctx->references == 0) { 6504 /* 6505 * No one cares about the result of this fetch anymore. 6506 */ 6507 if (fctx->pending == 0 && fctx->nqueries == 0 && 6508 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) { 6509 /* 6510 * This fctx is already shutdown; we were just 6511 * waiting for the last reference to go away. 6512 */ 6513 bucket_empty = fctx_unlink(fctx); 6514 fctx_destroy(fctx); 6515 } else { 6516 /* 6517 * Initiate shutdown. 6518 */ 6519 fctx_shutdown(fctx); 6520 } 6521 } 6522 return (bucket_empty); 6523} 6524 6525static void 6526resume_dslookup(isc_task_t *task, isc_event_t *event) { 6527 dns_fetchevent_t *fevent; 6528 dns_resolver_t *res; 6529 fetchctx_t *fctx; 6530 isc_result_t result; 6531 isc_boolean_t bucket_empty; 6532 isc_boolean_t locked = ISC_FALSE; 6533 unsigned int bucketnum; 6534 dns_rdataset_t nameservers; 6535 dns_fixedname_t fixed; 6536 dns_name_t *domain; 6537 6538 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE); 6539 fevent = (dns_fetchevent_t *)event; 6540 fctx = event->ev_arg; 6541 REQUIRE(VALID_FCTX(fctx)); 6542 res = fctx->res; 6543 6544 UNUSED(task); 6545 FCTXTRACE("resume_dslookup"); 6546 6547 if (fevent->node != NULL) 6548 dns_db_detachnode(fevent->db, &fevent->node); 6549 if (fevent->db != NULL) 6550 dns_db_detach(&fevent->db); 6551 6552 dns_rdataset_init(&nameservers); 6553 6554 bucketnum = fctx->bucketnum; 6555 if (fevent->result == ISC_R_CANCELED) { 6556 dns_resolver_destroyfetch(&fctx->nsfetch); 6557 fctx_done(fctx, ISC_R_CANCELED, __LINE__); 6558 } else if (fevent->result == ISC_R_SUCCESS) { 6559 6560 FCTXTRACE("resuming DS lookup"); 6561 6562 dns_resolver_destroyfetch(&fctx->nsfetch); 6563 if (dns_rdataset_isassociated(&fctx->nameservers)) 6564 dns_rdataset_disassociate(&fctx->nameservers); 6565 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers); 6566 fctx->ns_ttl = fctx->nameservers.ttl; 6567 fctx->ns_ttl_ok = ISC_TRUE; 6568 log_ns_ttl(fctx, "resume_dslookup"); 6569 dns_name_free(&fctx->domain, fctx->mctx); 6570 dns_name_init(&fctx->domain, NULL); 6571 result = dns_name_dup(&fctx->nsname, fctx->mctx, &fctx->domain); 6572 if (result != ISC_R_SUCCESS) { 6573 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 6574 goto cleanup; 6575 } 6576 /* 6577 * Try again. 6578 */ 6579 fctx_try(fctx, ISC_TRUE, ISC_FALSE); 6580 } else { 6581 unsigned int n; 6582 dns_rdataset_t *nsrdataset = NULL; 6583 6584 /* 6585 * Retrieve state from fctx->nsfetch before we destroy it. 6586 */ 6587 dns_fixedname_init(&fixed); 6588 domain = dns_fixedname_name(&fixed); 6589 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL); 6590 if (dns_name_equal(&fctx->nsname, domain)) { 6591 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 6592 dns_resolver_destroyfetch(&fctx->nsfetch); 6593 goto cleanup; 6594 } 6595 if (dns_rdataset_isassociated( 6596 &fctx->nsfetch->private->nameservers)) { 6597 dns_rdataset_clone( 6598 &fctx->nsfetch->private->nameservers, 6599 &nameservers); 6600 nsrdataset = &nameservers; 6601 } else 6602 domain = NULL; 6603 dns_resolver_destroyfetch(&fctx->nsfetch); 6604 n = dns_name_countlabels(&fctx->nsname); 6605 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1, 6606 &fctx->nsname); 6607 6608 if (dns_rdataset_isassociated(fevent->rdataset)) 6609 dns_rdataset_disassociate(fevent->rdataset); 6610 FCTXTRACE("continuing to look for parent's NS records"); 6611 result = dns_resolver_createfetch(fctx->res, &fctx->nsname, 6612 dns_rdatatype_ns, domain, 6613 nsrdataset, NULL, 0, task, 6614 resume_dslookup, fctx, 6615 &fctx->nsrrset, NULL, 6616 &fctx->nsfetch); 6617 if (result != ISC_R_SUCCESS) 6618 fctx_done(fctx, result, __LINE__); 6619 else { 6620 LOCK(&res->buckets[bucketnum].lock); 6621 locked = ISC_TRUE; 6622 fctx->references++; 6623 } 6624 } 6625 6626 cleanup: 6627 if (dns_rdataset_isassociated(&nameservers)) 6628 dns_rdataset_disassociate(&nameservers); 6629 if (dns_rdataset_isassociated(fevent->rdataset)) 6630 dns_rdataset_disassociate(fevent->rdataset); 6631 INSIST(fevent->sigrdataset == NULL); 6632 isc_event_free(&event); 6633 if (!locked) 6634 LOCK(&res->buckets[bucketnum].lock); 6635 bucket_empty = fctx_decreference(fctx); 6636 UNLOCK(&res->buckets[bucketnum].lock); 6637 if (bucket_empty) 6638 empty_bucket(res); 6639} 6640 6641static inline void 6642checknamessection(dns_message_t *message, dns_section_t section) { 6643 isc_result_t result; 6644 dns_name_t *name; 6645 dns_rdata_t rdata = DNS_RDATA_INIT; 6646 dns_rdataset_t *rdataset; 6647 6648 for (result = dns_message_firstname(message, section); 6649 result == ISC_R_SUCCESS; 6650 result = dns_message_nextname(message, section)) 6651 { 6652 name = NULL; 6653 dns_message_currentname(message, section, &name); 6654 for (rdataset = ISC_LIST_HEAD(name->list); 6655 rdataset != NULL; 6656 rdataset = ISC_LIST_NEXT(rdataset, link)) { 6657 for (result = dns_rdataset_first(rdataset); 6658 result == ISC_R_SUCCESS; 6659 result = dns_rdataset_next(rdataset)) { 6660 dns_rdataset_current(rdataset, &rdata); 6661 if (!dns_rdata_checkowner(name, rdata.rdclass, 6662 rdata.type, 6663 ISC_FALSE) || 6664 !dns_rdata_checknames(&rdata, name, NULL)) 6665 { 6666 rdataset->attributes |= 6667 DNS_RDATASETATTR_CHECKNAMES; 6668 } 6669 dns_rdata_reset(&rdata); 6670 } 6671 } 6672 } 6673} 6674 6675static void 6676checknames(dns_message_t *message) { 6677 6678 checknamessection(message, DNS_SECTION_ANSWER); 6679 checknamessection(message, DNS_SECTION_AUTHORITY); 6680 checknamessection(message, DNS_SECTION_ADDITIONAL); 6681} 6682 6683/* 6684 * Log server NSID at log level 'level' 6685 */ 6686static void 6687log_nsid(isc_buffer_t *opt, size_t nsid_len, resquery_t *query, 6688 int level, isc_mem_t *mctx) 6689{ 6690 static const char hex[17] = "0123456789abcdef"; 6691 char addrbuf[ISC_SOCKADDR_FORMATSIZE]; 6692 isc_uint16_t buflen, i; 6693 unsigned char *p, *buf, *nsid; 6694 6695 /* Allocate buffer for storing hex version of the NSID */ 6696 buflen = (isc_uint16_t)nsid_len * 2 + 1; 6697 buf = isc_mem_get(mctx, buflen); 6698 if (buf == NULL) 6699 return; 6700 6701 /* Convert to hex */ 6702 p = buf; 6703 nsid = isc_buffer_current(opt); 6704 for (i = 0; i < nsid_len; i++) { 6705 *p++ = hex[(nsid[0] >> 4) & 0xf]; 6706 *p++ = hex[nsid[0] & 0xf]; 6707 nsid++; 6708 } 6709 *p = '\0'; 6710 6711 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf, 6712 sizeof(addrbuf)); 6713 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 6714 DNS_LOGMODULE_RESOLVER, level, 6715 "received NSID '%s' from %s", buf, addrbuf); 6716 6717 /* Clean up */ 6718 isc_mem_put(mctx, buf, buflen); 6719 return; 6720} 6721 6722static void 6723log_packet(dns_message_t *message, int level, isc_mem_t *mctx) { 6724 isc_buffer_t buffer; 6725 char *buf = NULL; 6726 int len = 1024; 6727 isc_result_t result; 6728 6729 if (! isc_log_wouldlog(dns_lctx, level)) 6730 return; 6731 6732 /* 6733 * Note that these are multiline debug messages. We want a newline 6734 * to appear in the log after each message. 6735 */ 6736 6737 do { 6738 buf = isc_mem_get(mctx, len); 6739 if (buf == NULL) 6740 break; 6741 isc_buffer_init(&buffer, buf, len); 6742 result = dns_message_totext(message, &dns_master_style_debug, 6743 0, &buffer); 6744 if (result == ISC_R_NOSPACE) { 6745 isc_mem_put(mctx, buf, len); 6746 len += 1024; 6747 } else if (result == ISC_R_SUCCESS) 6748 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 6749 DNS_LOGMODULE_RESOLVER, level, 6750 "received packet:\n%.*s", 6751 (int)isc_buffer_usedlength(&buffer), 6752 buf); 6753 } while (result == ISC_R_NOSPACE); 6754 6755 if (buf != NULL) 6756 isc_mem_put(mctx, buf, len); 6757} 6758 6759static isc_boolean_t 6760iscname(fetchctx_t *fctx) { 6761 isc_result_t result; 6762 6763 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER, 6764 &fctx->name, dns_rdatatype_cname, 0, 6765 NULL, NULL); 6766 return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE); 6767} 6768 6769static isc_boolean_t 6770betterreferral(fetchctx_t *fctx) { 6771 isc_result_t result; 6772 dns_name_t *name; 6773 dns_rdataset_t *rdataset; 6774 dns_message_t *message = fctx->rmessage; 6775 6776 for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY); 6777 result == ISC_R_SUCCESS; 6778 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) { 6779 name = NULL; 6780 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name); 6781 if (!isstrictsubdomain(name, &fctx->domain)) 6782 continue; 6783 for (rdataset = ISC_LIST_HEAD(name->list); 6784 rdataset != NULL; 6785 rdataset = ISC_LIST_NEXT(rdataset, link)) 6786 if (rdataset->type == dns_rdatatype_ns) 6787 return (ISC_TRUE); 6788 } 6789 return (ISC_FALSE); 6790} 6791 6792static void 6793process_opt(resquery_t *query, dns_rdataset_t *opt) { 6794 dns_rdata_t rdata; 6795 isc_buffer_t optbuf; 6796 isc_result_t result; 6797 isc_uint16_t optcode; 6798 isc_uint16_t optlen; 6799 6800 result = dns_rdataset_first(opt); 6801 if (result == ISC_R_SUCCESS) { 6802 dns_rdata_init(&rdata); 6803 dns_rdataset_current(opt, &rdata); 6804 isc_buffer_init(&optbuf, rdata.data, rdata.length); 6805 isc_buffer_add(&optbuf, rdata.length); 6806 while (isc_buffer_remaininglength(&optbuf) >= 4) { 6807 optcode = isc_buffer_getuint16(&optbuf); 6808 optlen = isc_buffer_getuint16(&optbuf); 6809 INSIST(optlen <= isc_buffer_remaininglength(&optbuf)); 6810 switch (optcode) { 6811 case DNS_OPT_NSID: 6812 if (query->options & DNS_FETCHOPT_WANTNSID) 6813 log_nsid(&optbuf, optlen, query, 6814 ISC_LOG_INFO, 6815 query->fctx->res->mctx); 6816 isc_buffer_forward(&optbuf, optlen); 6817 break; 6818 default: 6819 isc_buffer_forward(&optbuf, optlen); 6820 break; 6821 } 6822 } 6823 INSIST(isc_buffer_remaininglength(&optbuf) == 0U); 6824 } 6825} 6826 6827static void 6828resquery_response(isc_task_t *task, isc_event_t *event) { 6829 isc_result_t result = ISC_R_SUCCESS; 6830 resquery_t *query = event->ev_arg; 6831 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event; 6832 isc_boolean_t keep_trying, get_nameservers, resend; 6833 isc_boolean_t truncated; 6834 dns_message_t *message; 6835 dns_rdataset_t *opt; 6836 fetchctx_t *fctx; 6837 dns_name_t *fname; 6838 dns_fixedname_t foundname; 6839 isc_stdtime_t now; 6840 isc_time_t tnow, *finish; 6841 dns_adbaddrinfo_t *addrinfo; 6842 unsigned int options; 6843 unsigned int findoptions; 6844 isc_result_t broken_server; 6845 badnstype_t broken_type = badns_response; 6846 isc_boolean_t no_response; 6847 6848 REQUIRE(VALID_QUERY(query)); 6849 fctx = query->fctx; 6850 options = query->options; 6851 REQUIRE(VALID_FCTX(fctx)); 6852 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH); 6853 6854 QTRACE("response"); 6855 6856 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET) 6857 inc_stats(fctx->res, dns_resstatscounter_responsev4); 6858 else 6859 inc_stats(fctx->res, dns_resstatscounter_responsev6); 6860 6861 (void)isc_timer_touch(fctx->timer); 6862 6863 keep_trying = ISC_FALSE; 6864 broken_server = ISC_R_SUCCESS; 6865 get_nameservers = ISC_FALSE; 6866 resend = ISC_FALSE; 6867 truncated = ISC_FALSE; 6868 finish = NULL; 6869 no_response = ISC_FALSE; 6870 6871 if (fctx->res->exiting) { 6872 result = ISC_R_SHUTTINGDOWN; 6873 goto done; 6874 } 6875 6876 fctx->timeouts = 0; 6877 fctx->timeout = ISC_FALSE; 6878 fctx->addrinfo = query->addrinfo; 6879 6880 /* 6881 * XXXRTH We should really get the current time just once. We 6882 * need a routine to convert from an isc_time_t to an 6883 * isc_stdtime_t. 6884 */ 6885 TIME_NOW(&tnow); 6886 finish = &tnow; 6887 isc_stdtime_get(&now); 6888 6889 /* 6890 * Did the dispatcher have a problem? 6891 */ 6892 if (devent->result != ISC_R_SUCCESS) { 6893 if (devent->result == ISC_R_EOF && 6894 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) { 6895 /* 6896 * The problem might be that they 6897 * don't understand EDNS0. Turn it 6898 * off and try again. 6899 */ 6900 options |= DNS_FETCHOPT_NOEDNS0; 6901 resend = ISC_TRUE; 6902 add_bad_edns(fctx, &query->addrinfo->sockaddr); 6903 } else { 6904 /* 6905 * There's no hope for this query. 6906 */ 6907 keep_trying = ISC_TRUE; 6908 6909 /* 6910 * If this is a network error on an exclusive query 6911 * socket, mark the server as bad so that we won't try 6912 * it for this fetch again. Also adjust finish and 6913 * no_response so that we penalize this address in SRTT 6914 * adjustment later. 6915 */ 6916 if (query->exclusivesocket && 6917 (devent->result == ISC_R_HOSTUNREACH || 6918 devent->result == ISC_R_NETUNREACH || 6919 devent->result == ISC_R_CONNREFUSED || 6920 devent->result == ISC_R_CANCELED)) { 6921 broken_server = devent->result; 6922 broken_type = badns_unreachable; 6923 finish = NULL; 6924 no_response = ISC_TRUE; 6925 } 6926 } 6927 goto done; 6928 } 6929 6930 message = fctx->rmessage; 6931 6932 if (query->tsig != NULL) { 6933 result = dns_message_setquerytsig(message, query->tsig); 6934 if (result != ISC_R_SUCCESS) 6935 goto done; 6936 } 6937 6938 if (query->tsigkey) { 6939 result = dns_message_settsigkey(message, query->tsigkey); 6940 if (result != ISC_R_SUCCESS) 6941 goto done; 6942 } 6943 6944 dns_message_setclass(message, fctx->res->rdclass); 6945 6946 result = dns_message_parse(message, &devent->buffer, 0); 6947 if (result != ISC_R_SUCCESS) { 6948 switch (result) { 6949 case ISC_R_UNEXPECTEDEND: 6950 if (!message->question_ok || 6951 (message->flags & DNS_MESSAGEFLAG_TC) == 0 || 6952 (options & DNS_FETCHOPT_TCP) != 0) { 6953 /* 6954 * Either the message ended prematurely, 6955 * and/or wasn't marked as being truncated, 6956 * and/or this is a response to a query we 6957 * sent over TCP. In all of these cases, 6958 * something is wrong with the remote 6959 * server and we don't want to retry using 6960 * TCP. 6961 */ 6962 if ((query->options & DNS_FETCHOPT_NOEDNS0) 6963 == 0) { 6964 /* 6965 * The problem might be that they 6966 * don't understand EDNS0. Turn it 6967 * off and try again. 6968 */ 6969 options |= DNS_FETCHOPT_NOEDNS0; 6970 resend = ISC_TRUE; 6971 add_bad_edns(fctx, 6972 &query->addrinfo->sockaddr); 6973 inc_stats(fctx->res, 6974 dns_resstatscounter_edns0fail); 6975 } else { 6976 broken_server = result; 6977 keep_trying = ISC_TRUE; 6978 } 6979 goto done; 6980 } 6981 /* 6982 * We defer retrying via TCP for a bit so we can 6983 * check out this message further. 6984 */ 6985 truncated = ISC_TRUE; 6986 break; 6987 case DNS_R_FORMERR: 6988 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) { 6989 /* 6990 * The problem might be that they 6991 * don't understand EDNS0. Turn it 6992 * off and try again. 6993 */ 6994 options |= DNS_FETCHOPT_NOEDNS0; 6995 resend = ISC_TRUE; 6996 add_bad_edns(fctx, &query->addrinfo->sockaddr); 6997 inc_stats(fctx->res, 6998 dns_resstatscounter_edns0fail); 6999 } else { 7000 broken_server = DNS_R_UNEXPECTEDRCODE; 7001 keep_trying = ISC_TRUE; 7002 } 7003 goto done; 7004 default: 7005 /* 7006 * Something bad has happened. 7007 */ 7008 goto done; 7009 } 7010 } 7011 7012 7013 /* 7014 * Log the incoming packet. 7015 */ 7016 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx); 7017 7018 if (message->rdclass != fctx->res->rdclass) { 7019 resend = ISC_TRUE; 7020 FCTXTRACE("bad class"); 7021 goto done; 7022 } 7023 7024 /* 7025 * Process receive opt record. 7026 */ 7027 opt = dns_message_getopt(message); 7028 if (opt != NULL) 7029 process_opt(query, opt); 7030 7031 /* 7032 * If the message is signed, check the signature. If not, this 7033 * returns success anyway. 7034 */ 7035 result = dns_message_checksig(message, fctx->res->view); 7036 if (result != ISC_R_SUCCESS) 7037 goto done; 7038 7039 /* 7040 * The dispatcher should ensure we only get responses with QR set. 7041 */ 7042 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0); 7043 /* 7044 * INSIST() that the message comes from the place we sent it to, 7045 * since the dispatch code should ensure this. 7046 * 7047 * INSIST() that the message id is correct (this should also be 7048 * ensured by the dispatch code). 7049 */ 7050 7051 /* 7052 * We have an affirmative response to the query and we have 7053 * previously got a response from this server which indicated 7054 * EDNS may not be supported so we can now cache the lack of 7055 * EDNS support. 7056 */ 7057 if (opt == NULL && 7058 (message->rcode == dns_rcode_noerror || 7059 message->rcode == dns_rcode_nxdomain || 7060 message->rcode == dns_rcode_refused || 7061 message->rcode == dns_rcode_yxdomain) && 7062 bad_edns(fctx, &query->addrinfo->sockaddr)) { 7063 char addrbuf[ISC_SOCKADDR_FORMATSIZE]; 7064 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf, 7065 sizeof(addrbuf)); 7066 dns_adb_changeflags(fctx->adb, query->addrinfo, 7067 DNS_FETCHOPT_NOEDNS0, 7068 DNS_FETCHOPT_NOEDNS0); 7069 } 7070 7071 /* 7072 * Deal with truncated responses by retrying using TCP. 7073 */ 7074 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0) 7075 truncated = ISC_TRUE; 7076 7077 if (truncated) { 7078 inc_stats(fctx->res, dns_resstatscounter_truncated); 7079 if ((options & DNS_FETCHOPT_TCP) != 0) { 7080 broken_server = DNS_R_TRUNCATEDTCP; 7081 keep_trying = ISC_TRUE; 7082 } else { 7083 options |= DNS_FETCHOPT_TCP; 7084 resend = ISC_TRUE; 7085 } 7086 goto done; 7087 } 7088 7089 /* 7090 * Is it a query response? 7091 */ 7092 if (message->opcode != dns_opcode_query) { 7093 /* XXXRTH Log */ 7094 broken_server = DNS_R_UNEXPECTEDOPCODE; 7095 keep_trying = ISC_TRUE; 7096 goto done; 7097 } 7098 7099 /* 7100 * Update statistics about erroneous responses. 7101 */ 7102 if (message->rcode != dns_rcode_noerror) { 7103 switch (message->rcode) { 7104 case dns_rcode_nxdomain: 7105 inc_stats(fctx->res, dns_resstatscounter_nxdomain); 7106 break; 7107 case dns_rcode_servfail: 7108 inc_stats(fctx->res, dns_resstatscounter_servfail); 7109 break; 7110 case dns_rcode_formerr: 7111 inc_stats(fctx->res, dns_resstatscounter_formerr); 7112 break; 7113 default: 7114 inc_stats(fctx->res, dns_resstatscounter_othererror); 7115 break; 7116 } 7117 } 7118 7119 /* 7120 * Is the remote server broken, or does it dislike us? 7121 */ 7122 if (message->rcode != dns_rcode_noerror && 7123 message->rcode != dns_rcode_nxdomain) { 7124 if (((message->rcode == dns_rcode_formerr || 7125 message->rcode == dns_rcode_notimp) || 7126 (message->rcode == dns_rcode_servfail && 7127 dns_message_getopt(message) == NULL)) && 7128 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) { 7129 /* 7130 * It's very likely they don't like EDNS0. 7131 * If the response code is SERVFAIL, also check if the 7132 * response contains an OPT RR and don't cache the 7133 * failure since it can be returned for various other 7134 * reasons. 7135 * 7136 * XXXRTH We should check if the question 7137 * we're asking requires EDNS0, and 7138 * if so, we should bail out. 7139 */ 7140 options |= DNS_FETCHOPT_NOEDNS0; 7141 resend = ISC_TRUE; 7142 /* 7143 * Remember that they may not like EDNS0. 7144 */ 7145 add_bad_edns(fctx, &query->addrinfo->sockaddr); 7146 inc_stats(fctx->res, dns_resstatscounter_edns0fail); 7147 } else if (message->rcode == dns_rcode_formerr) { 7148 if (ISFORWARDER(query->addrinfo)) { 7149 /* 7150 * This forwarder doesn't understand us, 7151 * but other forwarders might. Keep trying. 7152 */ 7153 broken_server = DNS_R_REMOTEFORMERR; 7154 keep_trying = ISC_TRUE; 7155 } else { 7156 /* 7157 * The server doesn't understand us. Since 7158 * all servers for a zone need similar 7159 * capabilities, we assume that we will get 7160 * FORMERR from all servers, and thus we 7161 * cannot make any more progress with this 7162 * fetch. 7163 */ 7164 log_formerr(fctx, "server sent FORMERR"); 7165 result = DNS_R_FORMERR; 7166 } 7167 } else if (message->rcode == dns_rcode_yxdomain) { 7168 /* 7169 * DNAME mapping failed because the new name 7170 * was too long. There's no chance of success 7171 * for this fetch. 7172 */ 7173 result = DNS_R_YXDOMAIN; 7174 } else if (message->rcode == dns_rcode_badvers) { 7175 unsigned int flags, mask; 7176 unsigned int version; 7177 7178 resend = ISC_TRUE; 7179 INSIST(opt != NULL); 7180 version = (opt->ttl >> 16) & 0xff; 7181 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) | 7182 DNS_FETCHOPT_EDNSVERSIONSET; 7183 mask = DNS_FETCHOPT_EDNSVERSIONMASK | 7184 DNS_FETCHOPT_EDNSVERSIONSET; 7185 switch (version) { 7186 case 0: 7187 dns_adb_changeflags(fctx->adb, query->addrinfo, 7188 flags, mask); 7189 break; 7190 default: 7191 broken_server = DNS_R_BADVERS; 7192 keep_trying = ISC_TRUE; 7193 break; 7194 } 7195 } else { 7196 /* 7197 * XXXRTH log. 7198 */ 7199 broken_server = DNS_R_UNEXPECTEDRCODE; 7200 INSIST(broken_server != ISC_R_SUCCESS); 7201 keep_trying = ISC_TRUE; 7202 } 7203 goto done; 7204 } 7205 7206 /* 7207 * Is the question the same as the one we asked? 7208 */ 7209 result = same_question(fctx); 7210 if (result != ISC_R_SUCCESS) { 7211 /* XXXRTH Log */ 7212 if (result == DNS_R_FORMERR) 7213 keep_trying = ISC_TRUE; 7214 goto done; 7215 } 7216 7217 /* 7218 * Is the server lame? 7219 */ 7220 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) && 7221 is_lame(fctx)) { 7222 inc_stats(fctx->res, dns_resstatscounter_lame); 7223 log_lame(fctx, query->addrinfo); 7224 result = dns_adb_marklame(fctx->adb, query->addrinfo, 7225 &fctx->name, fctx->type, 7226 now + fctx->res->lame_ttl); 7227 if (result != ISC_R_SUCCESS) 7228 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 7229 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR, 7230 "could not mark server as lame: %s", 7231 isc_result_totext(result)); 7232 broken_server = DNS_R_LAME; 7233 keep_trying = ISC_TRUE; 7234 goto done; 7235 } 7236 7237 /* 7238 * Enforce delegations only zones like NET and COM. 7239 */ 7240 if (!ISFORWARDER(query->addrinfo) && 7241 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) && 7242 !dns_name_equal(&fctx->domain, &fctx->name) && 7243 fix_mustbedelegationornxdomain(message, fctx)) { 7244 char namebuf[DNS_NAME_FORMATSIZE]; 7245 char domainbuf[DNS_NAME_FORMATSIZE]; 7246 char addrbuf[ISC_SOCKADDR_FORMATSIZE]; 7247 char classbuf[64]; 7248 char typebuf[64]; 7249 7250 dns_name_format(&fctx->name, namebuf, sizeof(namebuf)); 7251 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf)); 7252 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf)); 7253 dns_rdataclass_format(fctx->res->rdclass, classbuf, 7254 sizeof(classbuf)); 7255 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf, 7256 sizeof(addrbuf)); 7257 7258 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY, 7259 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 7260 "enforced delegation-only for '%s' (%s/%s/%s) " 7261 "from %s", 7262 domainbuf, namebuf, typebuf, classbuf, addrbuf); 7263 } 7264 7265 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0) 7266 checknames(message); 7267 7268 /* 7269 * Clear cache bits. 7270 */ 7271 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE); 7272 7273 /* 7274 * Did we get any answers? 7275 */ 7276 if (message->counts[DNS_SECTION_ANSWER] > 0 && 7277 (message->rcode == dns_rcode_noerror || 7278 message->rcode == dns_rcode_nxdomain)) { 7279 /* 7280 * [normal case] 7281 * We've got answers. If it has an authoritative answer or an 7282 * answer from a forwarder, we're done. 7283 */ 7284 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 || 7285 ISFORWARDER(query->addrinfo)) 7286 result = answer_response(fctx); 7287 else if (iscname(fctx) && 7288 fctx->type != dns_rdatatype_any && 7289 fctx->type != dns_rdatatype_cname) { 7290 /* 7291 * A BIND8 server could return a non-authoritative 7292 * answer when a CNAME is followed. We should treat 7293 * it as a valid answer. 7294 */ 7295 result = answer_response(fctx); 7296 } else if (fctx->type != dns_rdatatype_ns && 7297 !betterreferral(fctx)) { 7298 /* 7299 * Lame response !!!. 7300 */ 7301 result = answer_response(fctx); 7302 } else { 7303 if (fctx->type == dns_rdatatype_ns) { 7304 /* 7305 * A BIND 8 server could incorrectly return a 7306 * non-authoritative answer to an NS query 7307 * instead of a referral. Since this answer 7308 * lacks the SIGs necessary to do DNSSEC 7309 * validation, we must invoke the following 7310 * special kludge to treat it as a referral. 7311 */ 7312 result = noanswer_response(fctx, NULL, 7313 LOOK_FOR_NS_IN_ANSWER); 7314 } else { 7315 /* 7316 * Some other servers may still somehow include 7317 * an answer when it should return a referral 7318 * with an empty answer. Check to see if we can 7319 * treat this as a referral by ignoring the 7320 * answer. Further more, there may be an 7321 * implementation that moves A/AAAA glue records 7322 * to the answer section for that type of 7323 * delegation when the query is for that glue 7324 * record. LOOK_FOR_GLUE_IN_ANSWER will handle 7325 * such a corner case. 7326 */ 7327 result = noanswer_response(fctx, NULL, 7328 LOOK_FOR_GLUE_IN_ANSWER); 7329 } 7330 if (result != DNS_R_DELEGATION) { 7331 /* 7332 * At this point, AA is not set, the response 7333 * is not a referral, and the server is not a 7334 * forwarder. It is technically lame and it's 7335 * easier to treat it as such than to figure out 7336 * some more elaborate course of action. 7337 */ 7338 broken_server = DNS_R_LAME; 7339 keep_trying = ISC_TRUE; 7340 goto done; 7341 } 7342 goto force_referral; 7343 } 7344 if (result != ISC_R_SUCCESS) { 7345 if (result == DNS_R_FORMERR) 7346 keep_trying = ISC_TRUE; 7347 goto done; 7348 } 7349 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 || 7350 message->rcode == dns_rcode_noerror || 7351 message->rcode == dns_rcode_nxdomain) { 7352 /* 7353 * NXDOMAIN, NXRDATASET, or referral. 7354 */ 7355 result = noanswer_response(fctx, NULL, 0); 7356 switch (result) { 7357 case ISC_R_SUCCESS: 7358 case DNS_R_CHASEDSSERVERS: 7359 break; 7360 case DNS_R_DELEGATION: 7361 force_referral: 7362 /* 7363 * We don't have the answer, but we know a better 7364 * place to look. 7365 */ 7366 get_nameservers = ISC_TRUE; 7367 keep_trying = ISC_TRUE; 7368 /* 7369 * We have a new set of name servers, and it 7370 * has not experienced any restarts yet. 7371 */ 7372 fctx->restarts = 0; 7373 7374 /* 7375 * Update local statistics counters collected for each 7376 * new zone. 7377 */ 7378 fctx->referrals++; 7379 fctx->querysent = 0; 7380 fctx->lamecount = 0; 7381 fctx->neterr = 0; 7382 fctx->badresp = 0; 7383 fctx->adberr = 0; 7384 7385 result = ISC_R_SUCCESS; 7386 break; 7387 default: 7388 /* 7389 * Something has gone wrong. 7390 */ 7391 if (result == DNS_R_FORMERR) 7392 keep_trying = ISC_TRUE; 7393 goto done; 7394 } 7395 } else { 7396 /* 7397 * The server is insane. 7398 */ 7399 /* XXXRTH Log */ 7400 broken_server = DNS_R_UNEXPECTEDRCODE; 7401 keep_trying = ISC_TRUE; 7402 goto done; 7403 } 7404 7405 /* 7406 * Follow additional section data chains. 7407 */ 7408 chase_additional(fctx); 7409 7410 /* 7411 * Cache the cacheable parts of the message. This may also cause 7412 * work to be queued to the DNSSEC validator. 7413 */ 7414 if (WANTCACHE(fctx)) { 7415 result = cache_message(fctx, query->addrinfo, now); 7416 if (result != ISC_R_SUCCESS) 7417 goto done; 7418 } 7419 7420 /* 7421 * Ncache the negatively cacheable parts of the message. This may 7422 * also cause work to be queued to the DNSSEC validator. 7423 */ 7424 if (WANTNCACHE(fctx)) { 7425 dns_rdatatype_t covers; 7426 if (message->rcode == dns_rcode_nxdomain) 7427 covers = dns_rdatatype_any; 7428 else 7429 covers = fctx->type; 7430 7431 /* 7432 * Cache any negative cache entries in the message. 7433 */ 7434 result = ncache_message(fctx, query->addrinfo, covers, now); 7435 } 7436 7437 done: 7438 /* 7439 * Remember the query's addrinfo, in case we need to mark the 7440 * server as broken. 7441 */ 7442 addrinfo = query->addrinfo; 7443 7444 /* 7445 * Cancel the query. 7446 * 7447 * XXXRTH Don't cancel the query if waiting for validation? 7448 */ 7449 fctx_cancelquery(&query, &devent, finish, no_response); 7450 7451 if (keep_trying) { 7452 if (result == DNS_R_FORMERR) 7453 broken_server = DNS_R_FORMERR; 7454 if (broken_server != ISC_R_SUCCESS) { 7455 /* 7456 * Add this server to the list of bad servers for 7457 * this fctx. 7458 */ 7459 add_bad(fctx, addrinfo, broken_server, broken_type); 7460 } 7461 7462 if (get_nameservers) { 7463 dns_name_t *name; 7464 dns_fixedname_init(&foundname); 7465 fname = dns_fixedname_name(&foundname); 7466 if (result != ISC_R_SUCCESS) { 7467 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 7468 return; 7469 } 7470 findoptions = 0; 7471 if (dns_rdatatype_atparent(fctx->type)) 7472 findoptions |= DNS_DBFIND_NOEXACT; 7473 if ((options & DNS_FETCHOPT_UNSHARED) == 0) 7474 name = &fctx->name; 7475 else 7476 name = &fctx->domain; 7477 result = dns_view_findzonecut(fctx->res->view, 7478 name, fname, 7479 now, findoptions, 7480 ISC_TRUE, 7481 &fctx->nameservers, 7482 NULL); 7483 if (result != ISC_R_SUCCESS) { 7484 FCTXTRACE("couldn't find a zonecut"); 7485 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 7486 return; 7487 } 7488 if (!dns_name_issubdomain(fname, &fctx->domain)) { 7489 /* 7490 * The best nameservers are now above our 7491 * QDOMAIN. 7492 */ 7493 FCTXTRACE("nameservers now above QDOMAIN"); 7494 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 7495 return; 7496 } 7497 dns_name_free(&fctx->domain, fctx->mctx); 7498 dns_name_init(&fctx->domain, NULL); 7499 result = dns_name_dup(fname, fctx->mctx, &fctx->domain); 7500 if (result != ISC_R_SUCCESS) { 7501 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__); 7502 return; 7503 } 7504 fctx->ns_ttl = fctx->nameservers.ttl; 7505 fctx->ns_ttl_ok = ISC_TRUE; 7506 fctx_cancelqueries(fctx, ISC_TRUE); 7507 fctx_cleanupfinds(fctx); 7508 fctx_cleanupaltfinds(fctx); 7509 fctx_cleanupforwaddrs(fctx); 7510 fctx_cleanupaltaddrs(fctx); 7511 } 7512 /* 7513 * Try again. 7514 */ 7515 fctx_try(fctx, !get_nameservers, ISC_FALSE); 7516 } else if (resend) { 7517 /* 7518 * Resend (probably with changed options). 7519 */ 7520 FCTXTRACE("resend"); 7521 inc_stats(fctx->res, dns_resstatscounter_retry); 7522 result = fctx_query(fctx, addrinfo, options); 7523 if (result != ISC_R_SUCCESS) 7524 fctx_done(fctx, result, __LINE__); 7525 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) { 7526 /* 7527 * All has gone well so far, but we are waiting for the 7528 * DNSSEC validator to validate the answer. 7529 */ 7530 FCTXTRACE("wait for validator"); 7531 fctx_cancelqueries(fctx, ISC_TRUE); 7532 /* 7533 * We must not retransmit while the validator is working; 7534 * it has references to the current rmessage. 7535 */ 7536 result = fctx_stopidletimer(fctx); 7537 if (result != ISC_R_SUCCESS) 7538 fctx_done(fctx, result, __LINE__); 7539 } else if (result == DNS_R_CHASEDSSERVERS) { 7540 unsigned int n; 7541 add_bad(fctx, addrinfo, result, broken_type); 7542 fctx_cancelqueries(fctx, ISC_TRUE); 7543 fctx_cleanupfinds(fctx); 7544 fctx_cleanupforwaddrs(fctx); 7545 7546 n = dns_name_countlabels(&fctx->name); 7547 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname); 7548 7549 FCTXTRACE("suspending DS lookup to find parent's NS records"); 7550 7551 result = dns_resolver_createfetch(fctx->res, &fctx->nsname, 7552 dns_rdatatype_ns, 7553 NULL, NULL, NULL, 0, task, 7554 resume_dslookup, fctx, 7555 &fctx->nsrrset, NULL, 7556 &fctx->nsfetch); 7557 if (result != ISC_R_SUCCESS) 7558 fctx_done(fctx, result, __LINE__); 7559 else { 7560 LOCK(&fctx->res->buckets[fctx->bucketnum].lock); 7561 fctx->references++; 7562 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock); 7563 result = fctx_stopidletimer(fctx); 7564 if (result != ISC_R_SUCCESS) 7565 fctx_done(fctx, result, __LINE__); 7566 } 7567 } else { 7568 /* 7569 * We're done. 7570 */ 7571 fctx_done(fctx, result, __LINE__); 7572 } 7573} 7574 7575 7576/*** 7577 *** Resolver Methods 7578 ***/ 7579static void 7580destroy_badcache(dns_resolver_t *res) { 7581 dns_badcache_t *bad, *next; 7582 unsigned int i; 7583 7584 if (res->badcache != NULL) { 7585 for (i = 0; i < res->badhash; i++) 7586 for (bad = res->badcache[i]; bad != NULL; 7587 bad = next) { 7588 next = bad->next; 7589 isc_mem_put(res->mctx, bad, sizeof(*bad) + 7590 bad->name.length); 7591 res->badcount--; 7592 } 7593 isc_mem_put(res->mctx, res->badcache, 7594 sizeof(*res->badcache) * res->badhash); 7595 res->badcache = NULL; 7596 res->badhash = 0; 7597 INSIST(res->badcount == 0); 7598 } 7599} 7600 7601static void 7602destroy(dns_resolver_t *res) { 7603 unsigned int i; 7604 alternate_t *a; 7605 7606 REQUIRE(res->references == 0); 7607 REQUIRE(!res->priming); 7608 REQUIRE(res->primefetch == NULL); 7609 7610 RTRACE("destroy"); 7611 7612 INSIST(res->nfctx == 0); 7613 7614 DESTROYLOCK(&res->primelock); 7615 DESTROYLOCK(&res->nlock); 7616 DESTROYLOCK(&res->lock); 7617 for (i = 0; i < res->nbuckets; i++) { 7618 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs)); 7619 isc_task_shutdown(res->buckets[i].task); 7620 isc_task_detach(&res->buckets[i].task); 7621 DESTROYLOCK(&res->buckets[i].lock); 7622 isc_mem_detach(&res->buckets[i].mctx); 7623 } 7624 isc_mem_put(res->mctx, res->buckets, 7625 res->nbuckets * sizeof(fctxbucket_t)); 7626 if (res->dispatches4 != NULL) 7627 dns_dispatchset_destroy(&res->dispatches4); 7628 if (res->dispatches6 != NULL) 7629 dns_dispatchset_destroy(&res->dispatches6); 7630 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) { 7631 ISC_LIST_UNLINK(res->alternates, a, link); 7632 if (!a->isaddress) 7633 dns_name_free(&a->_u._n.name, res->mctx); 7634 isc_mem_put(res->mctx, a, sizeof(*a)); 7635 } 7636 dns_resolver_reset_algorithms(res); 7637 destroy_badcache(res); 7638 dns_resolver_resetmustbesecure(res); 7639#if USE_ALGLOCK 7640 isc_rwlock_destroy(&res->alglock); 7641#endif 7642#if USE_MBSLOCK 7643 isc_rwlock_destroy(&res->mbslock); 7644#endif 7645 isc_timer_detach(&res->spillattimer); 7646 res->magic = 0; 7647 isc_mem_put(res->mctx, res, sizeof(*res)); 7648} 7649 7650static void 7651send_shutdown_events(dns_resolver_t *res) { 7652 isc_event_t *event, *next_event; 7653 isc_task_t *etask; 7654 7655 /* 7656 * Caller must be holding the resolver lock. 7657 */ 7658 7659 for (event = ISC_LIST_HEAD(res->whenshutdown); 7660 event != NULL; 7661 event = next_event) { 7662 next_event = ISC_LIST_NEXT(event, ev_link); 7663 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link); 7664 etask = event->ev_sender; 7665 event->ev_sender = res; 7666 isc_task_sendanddetach(&etask, &event); 7667 } 7668} 7669 7670static void 7671empty_bucket(dns_resolver_t *res) { 7672 RTRACE("empty_bucket"); 7673 7674 LOCK(&res->lock); 7675 7676 INSIST(res->activebuckets > 0); 7677 res->activebuckets--; 7678 if (res->activebuckets == 0) 7679 send_shutdown_events(res); 7680 7681 UNLOCK(&res->lock); 7682} 7683 7684static void 7685spillattimer_countdown(isc_task_t *task, isc_event_t *event) { 7686 dns_resolver_t *res = event->ev_arg; 7687 isc_result_t result; 7688 unsigned int count; 7689 isc_boolean_t logit = ISC_FALSE; 7690 7691 REQUIRE(VALID_RESOLVER(res)); 7692 7693 UNUSED(task); 7694 7695 LOCK(&res->lock); 7696 INSIST(!res->exiting); 7697 if (res->spillat > res->spillatmin) { 7698 res->spillat--; 7699 logit = ISC_TRUE; 7700 } 7701 if (res->spillat <= res->spillatmin) { 7702 result = isc_timer_reset(res->spillattimer, 7703 isc_timertype_inactive, NULL, 7704 NULL, ISC_TRUE); 7705 RUNTIME_CHECK(result == ISC_R_SUCCESS); 7706 } 7707 count = res->spillat; 7708 UNLOCK(&res->lock); 7709 if (logit) 7710 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 7711 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE, 7712 "clients-per-query decreased to %u", count); 7713 7714 isc_event_free(&event); 7715} 7716 7717isc_result_t 7718dns_resolver_create(dns_view_t *view, 7719 isc_taskmgr_t *taskmgr, 7720 unsigned int ntasks, unsigned int ndisp, 7721 isc_socketmgr_t *socketmgr, 7722 isc_timermgr_t *timermgr, 7723 unsigned int options, 7724 dns_dispatchmgr_t *dispatchmgr, 7725 dns_dispatch_t *dispatchv4, 7726 dns_dispatch_t *dispatchv6, 7727 dns_resolver_t **resp) 7728{ 7729 dns_resolver_t *res; 7730 isc_result_t result = ISC_R_SUCCESS; 7731 unsigned int i, buckets_created = 0; 7732 isc_task_t *task = NULL; 7733 char name[16]; 7734 unsigned dispattr; 7735 7736 /* 7737 * Create a resolver. 7738 */ 7739 7740 REQUIRE(DNS_VIEW_VALID(view)); 7741 REQUIRE(ntasks > 0); 7742 REQUIRE(ndisp > 0); 7743 REQUIRE(resp != NULL && *resp == NULL); 7744 REQUIRE(dispatchmgr != NULL); 7745 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL); 7746 7747 res = isc_mem_get(view->mctx, sizeof(*res)); 7748 if (res == NULL) 7749 return (ISC_R_NOMEMORY); 7750 RTRACE("create"); 7751 res->mctx = view->mctx; 7752 res->rdclass = view->rdclass; 7753 res->socketmgr = socketmgr; 7754 res->timermgr = timermgr; 7755 res->taskmgr = taskmgr; 7756 res->dispatchmgr = dispatchmgr; 7757 res->view = view; 7758 res->options = options; 7759 res->lame_ttl = 0; 7760 ISC_LIST_INIT(res->alternates); 7761 res->udpsize = RECV_BUFFER_SIZE; 7762 res->algorithms = NULL; 7763 res->badcache = NULL; 7764 res->badcount = 0; 7765 res->badhash = 0; 7766 res->badsweep = 0; 7767 res->mustbesecure = NULL; 7768 res->spillatmin = res->spillat = 10; 7769 res->spillatmax = 100; 7770 res->spillattimer = NULL; 7771 res->zero_no_soa_ttl = ISC_FALSE; 7772 res->query_timeout = DEFAULT_QUERY_TIMEOUT; 7773 res->maxdepth = DEFAULT_RECURSION_DEPTH; 7774 res->nbuckets = ntasks; 7775 res->activebuckets = ntasks; 7776 res->buckets = isc_mem_get(view->mctx, 7777 ntasks * sizeof(fctxbucket_t)); 7778 if (res->buckets == NULL) { 7779 result = ISC_R_NOMEMORY; 7780 goto cleanup_res; 7781 } 7782 for (i = 0; i < ntasks; i++) { 7783 result = isc_mutex_init(&res->buckets[i].lock); 7784 if (result != ISC_R_SUCCESS) 7785 goto cleanup_buckets; 7786 res->buckets[i].task = NULL; 7787 result = isc_task_create(taskmgr, 0, &res->buckets[i].task); 7788 if (result != ISC_R_SUCCESS) { 7789 DESTROYLOCK(&res->buckets[i].lock); 7790 goto cleanup_buckets; 7791 } 7792 res->buckets[i].mctx = NULL; 7793 snprintf(name, sizeof(name), "res%u", i); 7794#ifdef ISC_PLATFORM_USETHREADS 7795 /* 7796 * Use a separate memory context for each bucket to reduce 7797 * contention among multiple threads. Do this only when 7798 * enabling threads because it will be require more memory. 7799 */ 7800 result = isc_mem_create(0, 0, &res->buckets[i].mctx); 7801 if (result != ISC_R_SUCCESS) { 7802 isc_task_detach(&res->buckets[i].task); 7803 DESTROYLOCK(&res->buckets[i].lock); 7804 goto cleanup_buckets; 7805 } 7806 isc_mem_setname(res->buckets[i].mctx, name, NULL); 7807#else 7808 isc_mem_attach(view->mctx, &res->buckets[i].mctx); 7809#endif 7810 isc_task_setname(res->buckets[i].task, name, res); 7811 ISC_LIST_INIT(res->buckets[i].fctxs); 7812 res->buckets[i].exiting = ISC_FALSE; 7813 buckets_created++; 7814 } 7815 7816 res->dispatches4 = NULL; 7817 if (dispatchv4 != NULL) { 7818 dns_dispatchset_create(view->mctx, socketmgr, taskmgr, 7819 dispatchv4, &res->dispatches4, ndisp); 7820 dispattr = dns_dispatch_getattributes(dispatchv4); 7821 res->exclusivev4 = 7822 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0); 7823 } 7824 7825 res->dispatches6 = NULL; 7826 if (dispatchv6 != NULL) { 7827 dns_dispatchset_create(view->mctx, socketmgr, taskmgr, 7828 dispatchv6, &res->dispatches6, ndisp); 7829 dispattr = dns_dispatch_getattributes(dispatchv6); 7830 res->exclusivev6 = 7831 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0); 7832 } 7833 7834 res->references = 1; 7835 res->exiting = ISC_FALSE; 7836 res->frozen = ISC_FALSE; 7837 ISC_LIST_INIT(res->whenshutdown); 7838 res->priming = ISC_FALSE; 7839 res->primefetch = NULL; 7840 res->nfctx = 0; 7841 7842 result = isc_mutex_init(&res->lock); 7843 if (result != ISC_R_SUCCESS) 7844 goto cleanup_dispatches; 7845 7846 result = isc_mutex_init(&res->nlock); 7847 if (result != ISC_R_SUCCESS) 7848 goto cleanup_lock; 7849 7850 result = isc_mutex_init(&res->primelock); 7851 if (result != ISC_R_SUCCESS) 7852 goto cleanup_nlock; 7853 7854 task = NULL; 7855 result = isc_task_create(taskmgr, 0, &task); 7856 if (result != ISC_R_SUCCESS) 7857 goto cleanup_primelock; 7858 7859 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL, 7860 task, spillattimer_countdown, res, 7861 &res->spillattimer); 7862 isc_task_detach(&task); 7863 if (result != ISC_R_SUCCESS) 7864 goto cleanup_primelock; 7865 7866#if USE_ALGLOCK 7867 result = isc_rwlock_init(&res->alglock, 0, 0); 7868 if (result != ISC_R_SUCCESS) 7869 goto cleanup_spillattimer; 7870#endif 7871#if USE_MBSLOCK 7872 result = isc_rwlock_init(&res->mbslock, 0, 0); 7873 if (result != ISC_R_SUCCESS) 7874 goto cleanup_alglock; 7875#endif 7876 7877 res->magic = RES_MAGIC; 7878 7879 *resp = res; 7880 7881 return (ISC_R_SUCCESS); 7882 7883#if USE_MBSLOCK 7884 cleanup_alglock: 7885#if USE_ALGLOCK 7886 isc_rwlock_destroy(&res->alglock); 7887#endif 7888#endif 7889#if USE_ALGLOCK || USE_MBSLOCK 7890 cleanup_spillattimer: 7891 isc_timer_detach(&res->spillattimer); 7892#endif 7893 7894 cleanup_primelock: 7895 DESTROYLOCK(&res->primelock); 7896 7897 cleanup_nlock: 7898 DESTROYLOCK(&res->nlock); 7899 7900 cleanup_lock: 7901 DESTROYLOCK(&res->lock); 7902 7903 cleanup_dispatches: 7904 if (res->dispatches6 != NULL) 7905 dns_dispatchset_destroy(&res->dispatches6); 7906 if (res->dispatches4 != NULL) 7907 dns_dispatchset_destroy(&res->dispatches4); 7908 7909 cleanup_buckets: 7910 for (i = 0; i < buckets_created; i++) { 7911 isc_mem_detach(&res->buckets[i].mctx); 7912 DESTROYLOCK(&res->buckets[i].lock); 7913 isc_task_shutdown(res->buckets[i].task); 7914 isc_task_detach(&res->buckets[i].task); 7915 } 7916 isc_mem_put(view->mctx, res->buckets, 7917 res->nbuckets * sizeof(fctxbucket_t)); 7918 7919 cleanup_res: 7920 isc_mem_put(view->mctx, res, sizeof(*res)); 7921 7922 return (result); 7923} 7924 7925#ifdef BIND9 7926static void 7927prime_done(isc_task_t *task, isc_event_t *event) { 7928 dns_resolver_t *res; 7929 dns_fetchevent_t *fevent; 7930 dns_fetch_t *fetch; 7931 dns_db_t *db = NULL; 7932 7933 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE); 7934 fevent = (dns_fetchevent_t *)event; 7935 res = event->ev_arg; 7936 REQUIRE(VALID_RESOLVER(res)); 7937 7938 UNUSED(task); 7939 7940 LOCK(&res->lock); 7941 7942 INSIST(res->priming); 7943 res->priming = ISC_FALSE; 7944 LOCK(&res->primelock); 7945 fetch = res->primefetch; 7946 res->primefetch = NULL; 7947 UNLOCK(&res->primelock); 7948 7949 UNLOCK(&res->lock); 7950 7951 if (fevent->result == ISC_R_SUCCESS && 7952 res->view->cache != NULL && res->view->hints != NULL) { 7953 dns_cache_attachdb(res->view->cache, &db); 7954 dns_root_checkhints(res->view, res->view->hints, db); 7955 dns_db_detach(&db); 7956 } 7957 7958 if (fevent->node != NULL) 7959 dns_db_detachnode(fevent->db, &fevent->node); 7960 if (fevent->db != NULL) 7961 dns_db_detach(&fevent->db); 7962 if (dns_rdataset_isassociated(fevent->rdataset)) 7963 dns_rdataset_disassociate(fevent->rdataset); 7964 INSIST(fevent->sigrdataset == NULL); 7965 7966 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset)); 7967 7968 isc_event_free(&event); 7969 dns_resolver_destroyfetch(&fetch); 7970} 7971 7972void 7973dns_resolver_prime(dns_resolver_t *res) { 7974 isc_boolean_t want_priming = ISC_FALSE; 7975 dns_rdataset_t *rdataset; 7976 isc_result_t result; 7977 7978 REQUIRE(VALID_RESOLVER(res)); 7979 REQUIRE(res->frozen); 7980 7981 RTRACE("dns_resolver_prime"); 7982 7983 LOCK(&res->lock); 7984 7985 if (!res->exiting && !res->priming) { 7986 INSIST(res->primefetch == NULL); 7987 res->priming = ISC_TRUE; 7988 want_priming = ISC_TRUE; 7989 } 7990 7991 UNLOCK(&res->lock); 7992 7993 if (want_priming) { 7994 /* 7995 * To avoid any possible recursive locking problems, we 7996 * start the priming fetch like any other fetch, and holding 7997 * no resolver locks. No one else will try to start it 7998 * because we're the ones who set res->priming to true. 7999 * Any other callers of dns_resolver_prime() while we're 8000 * running will see that res->priming is already true and 8001 * do nothing. 8002 */ 8003 RTRACE("priming"); 8004 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset)); 8005 if (rdataset == NULL) { 8006 LOCK(&res->lock); 8007 INSIST(res->priming); 8008 INSIST(res->primefetch == NULL); 8009 res->priming = ISC_FALSE; 8010 UNLOCK(&res->lock); 8011 return; 8012 } 8013 dns_rdataset_init(rdataset); 8014 LOCK(&res->primelock); 8015 result = dns_resolver_createfetch(res, dns_rootname, 8016 dns_rdatatype_ns, 8017 NULL, NULL, NULL, 0, 8018 res->buckets[0].task, 8019 prime_done, 8020 res, rdataset, NULL, 8021 &res->primefetch); 8022 UNLOCK(&res->primelock); 8023 if (result != ISC_R_SUCCESS) { 8024 LOCK(&res->lock); 8025 INSIST(res->priming); 8026 res->priming = ISC_FALSE; 8027 UNLOCK(&res->lock); 8028 } 8029 } 8030} 8031#endif /* BIND9 */ 8032 8033void 8034dns_resolver_freeze(dns_resolver_t *res) { 8035 /* 8036 * Freeze resolver. 8037 */ 8038 8039 REQUIRE(VALID_RESOLVER(res)); 8040 8041 res->frozen = ISC_TRUE; 8042} 8043 8044void 8045dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) { 8046 REQUIRE(VALID_RESOLVER(source)); 8047 REQUIRE(targetp != NULL && *targetp == NULL); 8048 8049 RRTRACE(source, "attach"); 8050 LOCK(&source->lock); 8051 REQUIRE(!source->exiting); 8052 8053 INSIST(source->references > 0); 8054 source->references++; 8055 INSIST(source->references != 0); 8056 UNLOCK(&source->lock); 8057 8058 *targetp = source; 8059} 8060 8061void 8062dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task, 8063 isc_event_t **eventp) 8064{ 8065 isc_task_t *clone; 8066 isc_event_t *event; 8067 8068 REQUIRE(VALID_RESOLVER(res)); 8069 REQUIRE(eventp != NULL); 8070 8071 event = *eventp; 8072 *eventp = NULL; 8073 8074 LOCK(&res->lock); 8075 8076 if (res->exiting && res->activebuckets == 0) { 8077 /* 8078 * We're already shutdown. Send the event. 8079 */ 8080 event->ev_sender = res; 8081 isc_task_send(task, &event); 8082 } else { 8083 clone = NULL; 8084 isc_task_attach(task, &clone); 8085 event->ev_sender = clone; 8086 ISC_LIST_APPEND(res->whenshutdown, event, ev_link); 8087 } 8088 8089 UNLOCK(&res->lock); 8090} 8091 8092void 8093dns_resolver_shutdown(dns_resolver_t *res) { 8094 unsigned int i; 8095 fetchctx_t *fctx; 8096 isc_result_t result; 8097 8098 REQUIRE(VALID_RESOLVER(res)); 8099 8100 RTRACE("shutdown"); 8101 8102 LOCK(&res->lock); 8103 8104 if (!res->exiting) { 8105 RTRACE("exiting"); 8106 res->exiting = ISC_TRUE; 8107 8108 for (i = 0; i < res->nbuckets; i++) { 8109 LOCK(&res->buckets[i].lock); 8110 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs); 8111 fctx != NULL; 8112 fctx = ISC_LIST_NEXT(fctx, link)) 8113 fctx_shutdown(fctx); 8114 if (res->dispatches4 != NULL && !res->exclusivev4) { 8115 dns_dispatchset_cancelall(res->dispatches4, 8116 res->buckets[i].task); 8117 } 8118 if (res->dispatches6 != NULL && !res->exclusivev6) { 8119 dns_dispatchset_cancelall(res->dispatches6, 8120 res->buckets[i].task); 8121 } 8122 res->buckets[i].exiting = ISC_TRUE; 8123 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) { 8124 INSIST(res->activebuckets > 0); 8125 res->activebuckets--; 8126 } 8127 UNLOCK(&res->buckets[i].lock); 8128 } 8129 if (res->activebuckets == 0) 8130 send_shutdown_events(res); 8131 result = isc_timer_reset(res->spillattimer, 8132 isc_timertype_inactive, NULL, 8133 NULL, ISC_TRUE); 8134 RUNTIME_CHECK(result == ISC_R_SUCCESS); 8135 } 8136 8137 UNLOCK(&res->lock); 8138} 8139 8140void 8141dns_resolver_detach(dns_resolver_t **resp) { 8142 dns_resolver_t *res; 8143 isc_boolean_t need_destroy = ISC_FALSE; 8144 8145 REQUIRE(resp != NULL); 8146 res = *resp; 8147 REQUIRE(VALID_RESOLVER(res)); 8148 8149 RTRACE("detach"); 8150 8151 LOCK(&res->lock); 8152 8153 INSIST(res->references > 0); 8154 res->references--; 8155 if (res->references == 0) { 8156 INSIST(res->exiting && res->activebuckets == 0); 8157 need_destroy = ISC_TRUE; 8158 } 8159 8160 UNLOCK(&res->lock); 8161 8162 if (need_destroy) 8163 destroy(res); 8164 8165 *resp = NULL; 8166} 8167 8168static inline isc_boolean_t 8169fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type, 8170 unsigned int options) 8171{ 8172 /* 8173 * Don't match fetch contexts that are shutting down. 8174 */ 8175 if (fctx->cloned || fctx->state == fetchstate_done || 8176 ISC_LIST_EMPTY(fctx->events)) 8177 return (ISC_FALSE); 8178 8179 if (fctx->type != type || fctx->options != options) 8180 return (ISC_FALSE); 8181 return (dns_name_equal(&fctx->name, name)); 8182} 8183 8184static inline void 8185log_fetch(dns_name_t *name, dns_rdatatype_t type) { 8186 char namebuf[DNS_NAME_FORMATSIZE]; 8187 char typebuf[DNS_RDATATYPE_FORMATSIZE]; 8188 int level = ISC_LOG_DEBUG(1); 8189 8190 if (! isc_log_wouldlog(dns_lctx, level)) 8191 return; 8192 8193 dns_name_format(name, namebuf, sizeof(namebuf)); 8194 dns_rdatatype_format(type, typebuf, sizeof(typebuf)); 8195 8196 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER, 8197 DNS_LOGMODULE_RESOLVER, level, 8198 "createfetch: %s %s", namebuf, typebuf); 8199} 8200 8201isc_result_t 8202dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name, 8203 dns_rdatatype_t type, 8204 dns_name_t *domain, dns_rdataset_t *nameservers, 8205 dns_forwarders_t *forwarders, 8206 unsigned int options, isc_task_t *task, 8207 isc_taskaction_t action, void *arg, 8208 dns_rdataset_t *rdataset, 8209 dns_rdataset_t *sigrdataset, 8210 dns_fetch_t **fetchp) 8211{ 8212 return (dns_resolver_createfetch3(res, name, type, domain, 8213 nameservers, forwarders, NULL, 0, 8214 options, 0, task, action, arg, 8215 rdataset, sigrdataset, fetchp)); 8216} 8217 8218isc_result_t 8219dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name, 8220 dns_rdatatype_t type, 8221 dns_name_t *domain, dns_rdataset_t *nameservers, 8222 dns_forwarders_t *forwarders, 8223 isc_sockaddr_t *client, dns_messageid_t id, 8224 unsigned int options, isc_task_t *task, 8225 isc_taskaction_t action, void *arg, 8226 dns_rdataset_t *rdataset, 8227 dns_rdataset_t *sigrdataset, 8228 dns_fetch_t **fetchp) 8229{ 8230 return (dns_resolver_createfetch3(res, name, type, domain, 8231 nameservers, forwarders, client, id, 8232 options, 0, task, action, arg, 8233 rdataset, sigrdataset, fetchp)); 8234} 8235 8236isc_result_t 8237dns_resolver_createfetch3(dns_resolver_t *res, dns_name_t *name, 8238 dns_rdatatype_t type, 8239 dns_name_t *domain, dns_rdataset_t *nameservers, 8240 dns_forwarders_t *forwarders, 8241 isc_sockaddr_t *client, dns_messageid_t id, 8242 unsigned int options, unsigned int depth, 8243 isc_task_t *task, 8244 isc_taskaction_t action, void *arg, 8245 dns_rdataset_t *rdataset, 8246 dns_rdataset_t *sigrdataset, 8247 dns_fetch_t **fetchp) 8248{ 8249 dns_fetch_t *fetch; 8250 fetchctx_t *fctx = NULL; 8251 isc_result_t result = ISC_R_SUCCESS; 8252 unsigned int bucketnum; 8253 isc_boolean_t new_fctx = ISC_FALSE; 8254 isc_event_t *event; 8255 unsigned int count = 0; 8256 unsigned int spillat; 8257 unsigned int spillatmin; 8258 isc_boolean_t destroy = ISC_FALSE; 8259 8260 UNUSED(forwarders); 8261 8262 REQUIRE(VALID_RESOLVER(res)); 8263 REQUIRE(res->frozen); 8264 /* XXXRTH Check for meta type */ 8265 if (domain != NULL) { 8266 REQUIRE(DNS_RDATASET_VALID(nameservers)); 8267 REQUIRE(nameservers->type == dns_rdatatype_ns); 8268 } else 8269 REQUIRE(nameservers == NULL); 8270 REQUIRE(forwarders == NULL); 8271 REQUIRE(!dns_rdataset_isassociated(rdataset)); 8272 REQUIRE(sigrdataset == NULL || 8273 !dns_rdataset_isassociated(sigrdataset)); 8274 REQUIRE(fetchp != NULL && *fetchp == NULL); 8275 8276 log_fetch(name, type); 8277 8278 /* 8279 * XXXRTH use a mempool? 8280 */ 8281 fetch = isc_mem_get(res->mctx, sizeof(*fetch)); 8282 if (fetch == NULL) 8283 return (ISC_R_NOMEMORY); 8284 8285 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets; 8286 8287 LOCK(&res->lock); 8288 spillat = res->spillat; 8289 spillatmin = res->spillatmin; 8290 UNLOCK(&res->lock); 8291 LOCK(&res->buckets[bucketnum].lock); 8292 8293 if (res->buckets[bucketnum].exiting) { 8294 result = ISC_R_SHUTTINGDOWN; 8295 goto unlock; 8296 } 8297 8298 if ((options & DNS_FETCHOPT_UNSHARED) == 0) { 8299 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs); 8300 fctx != NULL; 8301 fctx = ISC_LIST_NEXT(fctx, link)) { 8302 if (fctx_match(fctx, name, type, options)) 8303 break; 8304 } 8305 } 8306 8307 /* 8308 * Is this a duplicate? 8309 */ 8310 if (fctx != NULL && client != NULL) { 8311 dns_fetchevent_t *fevent; 8312 for (fevent = ISC_LIST_HEAD(fctx->events); 8313 fevent != NULL; 8314 fevent = ISC_LIST_NEXT(fevent, ev_link)) { 8315 if (fevent->client != NULL && fevent->id == id && 8316 isc_sockaddr_equal(fevent->client, client)) { 8317 result = DNS_R_DUPLICATE; 8318 goto unlock; 8319 } 8320 count++; 8321 } 8322 } 8323 if (count >= spillatmin && spillatmin != 0) { 8324 INSIST(fctx != NULL); 8325 if (count >= spillat) 8326 fctx->spilled = ISC_TRUE; 8327 if (fctx->spilled) { 8328 result = DNS_R_DROP; 8329 goto unlock; 8330 } 8331 } 8332 8333 if (fctx == NULL) { 8334 result = fctx_create(res, name, type, domain, nameservers, 8335 options, bucketnum, depth, &fctx); 8336 if (result != ISC_R_SUCCESS) 8337 goto unlock; 8338 new_fctx = ISC_TRUE; 8339 } else if (fctx->depth > depth) 8340 fctx->depth = depth; 8341 8342 result = fctx_join(fctx, task, client, id, action, arg, 8343 rdataset, sigrdataset, fetch); 8344 if (new_fctx) { 8345 if (result == ISC_R_SUCCESS) { 8346 /* 8347 * Launch this fctx. 8348 */ 8349 event = &fctx->control_event; 8350 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL, 8351 DNS_EVENT_FETCHCONTROL, 8352 fctx_start, fctx, NULL, 8353 NULL, NULL); 8354 isc_task_send(res->buckets[bucketnum].task, &event); 8355 } else { 8356 /* 8357 * We don't care about the result of fctx_unlink() 8358 * since we know we're not exiting. 8359 */ 8360 (void)fctx_unlink(fctx); 8361 destroy = ISC_TRUE; 8362 } 8363 } 8364 8365 unlock: 8366 UNLOCK(&res->buckets[bucketnum].lock); 8367 8368 if (destroy) 8369 fctx_destroy(fctx); 8370 8371 if (result == ISC_R_SUCCESS) { 8372 FTRACE("created"); 8373 *fetchp = fetch; 8374 } else 8375 isc_mem_put(res->mctx, fetch, sizeof(*fetch)); 8376 8377 return (result); 8378} 8379 8380void 8381dns_resolver_cancelfetch(dns_fetch_t *fetch) { 8382 fetchctx_t *fctx; 8383 dns_resolver_t *res; 8384 dns_fetchevent_t *event, *next_event; 8385 isc_task_t *etask; 8386 8387 REQUIRE(DNS_FETCH_VALID(fetch)); 8388 fctx = fetch->private; 8389 REQUIRE(VALID_FCTX(fctx)); 8390 res = fctx->res; 8391 8392 FTRACE("cancelfetch"); 8393 8394 LOCK(&res->buckets[fctx->bucketnum].lock); 8395 8396 /* 8397 * Find the completion event for this fetch (as opposed 8398 * to those for other fetches that have joined the same 8399 * fctx) and send it with result = ISC_R_CANCELED. 8400 */ 8401 event = NULL; 8402 if (fctx->state != fetchstate_done) { 8403 for (event = ISC_LIST_HEAD(fctx->events); 8404 event != NULL; 8405 event = next_event) { 8406 next_event = ISC_LIST_NEXT(event, ev_link); 8407 if (event->fetch == fetch) { 8408 ISC_LIST_UNLINK(fctx->events, event, ev_link); 8409 break; 8410 } 8411 } 8412 } 8413 if (event != NULL) { 8414 etask = event->ev_sender; 8415 event->ev_sender = fctx; 8416 event->result = ISC_R_CANCELED; 8417 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event)); 8418 } 8419 /* 8420 * The fctx continues running even if no fetches remain; 8421 * the answer is still cached. 8422 */ 8423 8424 UNLOCK(&res->buckets[fctx->bucketnum].lock); 8425} 8426 8427void 8428dns_resolver_destroyfetch(dns_fetch_t **fetchp) { 8429 dns_fetch_t *fetch; 8430 dns_resolver_t *res; 8431 dns_fetchevent_t *event, *next_event; 8432 fetchctx_t *fctx; 8433 unsigned int bucketnum; 8434 isc_boolean_t bucket_empty; 8435 8436 REQUIRE(fetchp != NULL); 8437 fetch = *fetchp; 8438 REQUIRE(DNS_FETCH_VALID(fetch)); 8439 fctx = fetch->private; 8440 REQUIRE(VALID_FCTX(fctx)); 8441 res = fctx->res; 8442 8443 FTRACE("destroyfetch"); 8444 8445 bucketnum = fctx->bucketnum; 8446 LOCK(&res->buckets[bucketnum].lock); 8447 8448 /* 8449 * Sanity check: the caller should have gotten its event before 8450 * trying to destroy the fetch. 8451 */ 8452 event = NULL; 8453 if (fctx->state != fetchstate_done) { 8454 for (event = ISC_LIST_HEAD(fctx->events); 8455 event != NULL; 8456 event = next_event) { 8457 next_event = ISC_LIST_NEXT(event, ev_link); 8458 RUNTIME_CHECK(event->fetch != fetch); 8459 } 8460 } 8461 8462 bucket_empty = fctx_decreference(fctx); 8463 8464 UNLOCK(&res->buckets[bucketnum].lock); 8465 8466 isc_mem_put(res->mctx, fetch, sizeof(*fetch)); 8467 *fetchp = NULL; 8468 8469 if (bucket_empty) 8470 empty_bucket(res); 8471} 8472 8473void 8474dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx, 8475 isc_logcategory_t *category, isc_logmodule_t *module, 8476 int level, isc_boolean_t duplicateok) 8477{ 8478 fetchctx_t *fctx; 8479 dns_resolver_t *res; 8480 char domainbuf[DNS_NAME_FORMATSIZE]; 8481 8482 REQUIRE(DNS_FETCH_VALID(fetch)); 8483 fctx = fetch->private; 8484 REQUIRE(VALID_FCTX(fctx)); 8485 res = fctx->res; 8486 8487 LOCK(&res->buckets[fctx->bucketnum].lock); 8488 8489 INSIST(fctx->exitline >= 0); 8490 if (!fctx->logged || duplicateok) { 8491 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf)); 8492 isc_log_write(lctx, category, module, level, 8493 "fetch completed at %s:%d for %s in " 8494 "%" ISC_PRINT_QUADFORMAT "u." 8495 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s " 8496 "[domain:%s,referral:%u,restart:%u,qrysent:%u," 8497 "timeout:%u,lame:%u,neterr:%u,badresp:%u," 8498 "adberr:%u,findfail:%u,valfail:%u]", 8499 __FILE__, fctx->exitline, fctx->info, 8500 fctx->duration / US_PER_SEC, 8501 fctx->duration % US_PER_SEC, 8502 isc_result_totext(fctx->result), 8503 isc_result_totext(fctx->vresult), domainbuf, 8504 fctx->referrals, fctx->restarts, 8505 fctx->querysent, fctx->timeouts, fctx->lamecount, 8506 fctx->neterr, fctx->badresp, fctx->adberr, 8507 fctx->findfail, fctx->valfail); 8508 fctx->logged = ISC_TRUE; 8509 } 8510 8511 UNLOCK(&res->buckets[fctx->bucketnum].lock); 8512} 8513 8514dns_dispatchmgr_t * 8515dns_resolver_dispatchmgr(dns_resolver_t *resolver) { 8516 REQUIRE(VALID_RESOLVER(resolver)); 8517 return (resolver->dispatchmgr); 8518} 8519 8520dns_dispatch_t * 8521dns_resolver_dispatchv4(dns_resolver_t *resolver) { 8522 REQUIRE(VALID_RESOLVER(resolver)); 8523 return (dns_dispatchset_get(resolver->dispatches4)); 8524} 8525 8526dns_dispatch_t * 8527dns_resolver_dispatchv6(dns_resolver_t *resolver) { 8528 REQUIRE(VALID_RESOLVER(resolver)); 8529 return (dns_dispatchset_get(resolver->dispatches6)); 8530} 8531 8532isc_socketmgr_t * 8533dns_resolver_socketmgr(dns_resolver_t *resolver) { 8534 REQUIRE(VALID_RESOLVER(resolver)); 8535 return (resolver->socketmgr); 8536} 8537 8538isc_taskmgr_t * 8539dns_resolver_taskmgr(dns_resolver_t *resolver) { 8540 REQUIRE(VALID_RESOLVER(resolver)); 8541 return (resolver->taskmgr); 8542} 8543 8544isc_uint32_t 8545dns_resolver_getlamettl(dns_resolver_t *resolver) { 8546 REQUIRE(VALID_RESOLVER(resolver)); 8547 return (resolver->lame_ttl); 8548} 8549 8550void 8551dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) { 8552 REQUIRE(VALID_RESOLVER(resolver)); 8553 resolver->lame_ttl = lame_ttl; 8554} 8555 8556unsigned int 8557dns_resolver_nrunning(dns_resolver_t *resolver) { 8558 unsigned int n; 8559 LOCK(&resolver->nlock); 8560 n = resolver->nfctx; 8561 UNLOCK(&resolver->nlock); 8562 return (n); 8563} 8564 8565isc_result_t 8566dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt, 8567 dns_name_t *name, in_port_t port) { 8568 alternate_t *a; 8569 isc_result_t result; 8570 8571 REQUIRE(VALID_RESOLVER(resolver)); 8572 REQUIRE(!resolver->frozen); 8573 REQUIRE((alt == NULL) ^ (name == NULL)); 8574 8575 a = isc_mem_get(resolver->mctx, sizeof(*a)); 8576 if (a == NULL) 8577 return (ISC_R_NOMEMORY); 8578 if (alt != NULL) { 8579 a->isaddress = ISC_TRUE; 8580 a->_u.addr = *alt; 8581 } else { 8582 a->isaddress = ISC_FALSE; 8583 a->_u._n.port = port; 8584 dns_name_init(&a->_u._n.name, NULL); 8585 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name); 8586 if (result != ISC_R_SUCCESS) { 8587 isc_mem_put(resolver->mctx, a, sizeof(*a)); 8588 return (result); 8589 } 8590 } 8591 ISC_LINK_INIT(a, link); 8592 ISC_LIST_APPEND(resolver->alternates, a, link); 8593 8594 return (ISC_R_SUCCESS); 8595} 8596 8597void 8598dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) { 8599 REQUIRE(VALID_RESOLVER(resolver)); 8600 resolver->udpsize = udpsize; 8601} 8602 8603isc_uint16_t 8604dns_resolver_getudpsize(dns_resolver_t *resolver) { 8605 REQUIRE(VALID_RESOLVER(resolver)); 8606 return (resolver->udpsize); 8607} 8608 8609void 8610dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) { 8611 unsigned int i; 8612 dns_badcache_t *bad, *prev, *next; 8613 8614 REQUIRE(VALID_RESOLVER(resolver)); 8615 8616 LOCK(&resolver->lock); 8617 if (resolver->badcache == NULL) 8618 goto unlock; 8619 8620 if (name != NULL) { 8621 isc_time_t now; 8622 isc_result_t result; 8623 result = isc_time_now(&now); 8624 if (result != ISC_R_SUCCESS) 8625 isc_time_settoepoch(&now); 8626 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash; 8627 prev = NULL; 8628 for (bad = resolver->badcache[i]; bad != NULL; bad = next) { 8629 int n; 8630 next = bad->next; 8631 n = isc_time_compare(&bad->expire, &now); 8632 if (n < 0 || dns_name_equal(name, &bad->name)) { 8633 if (prev == NULL) 8634 resolver->badcache[i] = bad->next; 8635 else 8636 prev->next = bad->next; 8637 isc_mem_put(resolver->mctx, bad, sizeof(*bad) + 8638 bad->name.length); 8639 resolver->badcount--; 8640 } else 8641 prev = bad; 8642 } 8643 } else 8644 destroy_badcache(resolver); 8645 8646 unlock: 8647 UNLOCK(&resolver->lock); 8648 8649} 8650 8651static void 8652resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) { 8653 unsigned int newsize; 8654 dns_badcache_t **new, *bad, *next; 8655 unsigned int i; 8656 8657 if (grow) 8658 newsize = resolver->badhash * 2 + 1; 8659 else 8660 newsize = (resolver->badhash - 1) / 2; 8661 8662 new = isc_mem_get(resolver->mctx, 8663 sizeof(*resolver->badcache) * newsize); 8664 if (new == NULL) 8665 return; 8666 memset(new, 0, sizeof(*resolver->badcache) * newsize); 8667 for (i = 0; i < resolver->badhash; i++) { 8668 for (bad = resolver->badcache[i]; bad != NULL; bad = next) { 8669 next = bad->next; 8670 if (isc_time_compare(&bad->expire, now) < 0) { 8671 isc_mem_put(resolver->mctx, bad, sizeof(*bad) + 8672 bad->name.length); 8673 resolver->badcount--; 8674 } else { 8675 bad->next = new[bad->hashval % newsize]; 8676 new[bad->hashval % newsize] = bad; 8677 } 8678 } 8679 } 8680 isc_mem_put(resolver->mctx, resolver->badcache, 8681 sizeof(*resolver->badcache) * resolver->badhash); 8682 resolver->badhash = newsize; 8683 resolver->badcache = new; 8684} 8685 8686void 8687dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name, 8688 dns_rdatatype_t type, isc_time_t *expire) 8689{ 8690 isc_time_t now; 8691 isc_result_t result = ISC_R_SUCCESS; 8692 unsigned int i, hashval; 8693 dns_badcache_t *bad, *prev, *next; 8694 8695 REQUIRE(VALID_RESOLVER(resolver)); 8696 8697 LOCK(&resolver->lock); 8698 if (resolver->badcache == NULL) { 8699 resolver->badcache = isc_mem_get(resolver->mctx, 8700 sizeof(*resolver->badcache) * 8701 DNS_BADCACHE_SIZE); 8702 if (resolver->badcache == NULL) 8703 goto cleanup; 8704 resolver->badhash = DNS_BADCACHE_SIZE; 8705 memset(resolver->badcache, 0, sizeof(*resolver->badcache) * 8706 resolver->badhash); 8707 } 8708 8709 result = isc_time_now(&now); 8710 if (result != ISC_R_SUCCESS) 8711 isc_time_settoepoch(&now); 8712 hashval = dns_name_hash(name, ISC_FALSE); 8713 i = hashval % resolver->badhash; 8714 prev = NULL; 8715 for (bad = resolver->badcache[i]; bad != NULL; bad = next) { 8716 next = bad->next; 8717 if (bad->type == type && dns_name_equal(name, &bad->name)) 8718 break; 8719 if (isc_time_compare(&bad->expire, &now) < 0) { 8720 if (prev == NULL) 8721 resolver->badcache[i] = bad->next; 8722 else 8723 prev->next = bad->next; 8724 isc_mem_put(resolver->mctx, bad, sizeof(*bad) + 8725 bad->name.length); 8726 resolver->badcount--; 8727 } else 8728 prev = bad; 8729 } 8730 if (bad == NULL) { 8731 isc_buffer_t buffer; 8732 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length); 8733 if (bad == NULL) 8734 goto cleanup; 8735 bad->type = type; 8736 bad->hashval = hashval; 8737 bad->expire = *expire; 8738 isc_buffer_init(&buffer, bad + 1, name->length); 8739 dns_name_init(&bad->name, NULL); 8740 dns_name_copy(name, &bad->name, &buffer); 8741 bad->next = resolver->badcache[i]; 8742 resolver->badcache[i] = bad; 8743 resolver->badcount++; 8744 if (resolver->badcount > resolver->badhash * 8) 8745 resizehash(resolver, &now, ISC_TRUE); 8746 if (resolver->badcount < resolver->badhash * 2 && 8747 resolver->badhash > DNS_BADCACHE_SIZE) 8748 resizehash(resolver, &now, ISC_FALSE); 8749 } else 8750 bad->expire = *expire; 8751 cleanup: 8752 UNLOCK(&resolver->lock); 8753} 8754 8755isc_boolean_t 8756dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name, 8757 dns_rdatatype_t type, isc_time_t *now) 8758{ 8759 dns_badcache_t *bad, *prev, *next; 8760 isc_boolean_t answer = ISC_FALSE; 8761 unsigned int i; 8762 8763 REQUIRE(VALID_RESOLVER(resolver)); 8764 8765 LOCK(&resolver->lock); 8766 if (resolver->badcache == NULL) 8767 goto unlock; 8768 8769 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash; 8770 prev = NULL; 8771 for (bad = resolver->badcache[i]; bad != NULL; bad = next) { 8772 next = bad->next; 8773 /* 8774 * Search the hash list. Clean out expired records as we go. 8775 */ 8776 if (isc_time_compare(&bad->expire, now) < 0) { 8777 if (prev != NULL) 8778 prev->next = bad->next; 8779 else 8780 resolver->badcache[i] = bad->next; 8781 isc_mem_put(resolver->mctx, bad, sizeof(*bad) + 8782 bad->name.length); 8783 resolver->badcount--; 8784 continue; 8785 } 8786 if (bad->type == type && dns_name_equal(name, &bad->name)) { 8787 answer = ISC_TRUE; 8788 break; 8789 } 8790 prev = bad; 8791 } 8792 8793 /* 8794 * Slow sweep to clean out stale records. 8795 */ 8796 i = resolver->badsweep++ % resolver->badhash; 8797 bad = resolver->badcache[i]; 8798 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) { 8799 resolver->badcache[i] = bad->next; 8800 isc_mem_put(resolver->mctx, bad, sizeof(*bad) + 8801 bad->name.length); 8802 resolver->badcount--; 8803 } 8804 8805 unlock: 8806 UNLOCK(&resolver->lock); 8807 return (answer); 8808} 8809 8810void 8811dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) { 8812 char namebuf[DNS_NAME_FORMATSIZE]; 8813 char typebuf[DNS_RDATATYPE_FORMATSIZE]; 8814 dns_badcache_t *bad, *next, *prev; 8815 isc_time_t now; 8816 unsigned int i; 8817 isc_uint64_t t; 8818 8819 LOCK(&resolver->lock); 8820 fprintf(fp, ";\n; Bad cache\n;\n"); 8821 8822 if (resolver->badcache == NULL) 8823 goto unlock; 8824 8825 TIME_NOW(&now); 8826 for (i = 0; i < resolver->badhash; i++) { 8827 prev = NULL; 8828 for (bad = resolver->badcache[i]; bad != NULL; bad = next) { 8829 next = bad->next; 8830 if (isc_time_compare(&bad->expire, &now) < 0) { 8831 if (prev != NULL) 8832 prev->next = bad->next; 8833 else 8834 resolver->badcache[i] = bad->next; 8835 isc_mem_put(resolver->mctx, bad, sizeof(*bad) + 8836 bad->name.length); 8837 resolver->badcount--; 8838 continue; 8839 } 8840 prev = bad; 8841 dns_name_format(&bad->name, namebuf, sizeof(namebuf)); 8842 dns_rdatatype_format(bad->type, typebuf, 8843 sizeof(typebuf)); 8844 t = isc_time_microdiff(&bad->expire, &now); 8845 t /= 1000; 8846 fprintf(fp, "; %s/%s [ttl " 8847 "%" ISC_PLATFORM_QUADFORMAT "u]\n", 8848 namebuf, typebuf, t); 8849 } 8850 } 8851 8852 unlock: 8853 UNLOCK(&resolver->lock); 8854} 8855 8856static void 8857free_algorithm(void *node, void *arg) { 8858 unsigned char *algorithms = node; 8859 isc_mem_t *mctx = arg; 8860 8861 isc_mem_put(mctx, algorithms, *algorithms); 8862} 8863 8864void 8865dns_resolver_reset_algorithms(dns_resolver_t *resolver) { 8866 8867 REQUIRE(VALID_RESOLVER(resolver)); 8868 8869#if USE_ALGLOCK 8870 RWLOCK(&resolver->alglock, isc_rwlocktype_write); 8871#endif 8872 if (resolver->algorithms != NULL) 8873 dns_rbt_destroy(&resolver->algorithms); 8874#if USE_ALGLOCK 8875 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write); 8876#endif 8877} 8878 8879isc_result_t 8880dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name, 8881 unsigned int alg) 8882{ 8883 unsigned int len, mask; 8884 unsigned char *new; 8885 unsigned char *algorithms; 8886 isc_result_t result; 8887 dns_rbtnode_t *node = NULL; 8888 8889 REQUIRE(VALID_RESOLVER(resolver)); 8890 if (alg > 255) 8891 return (ISC_R_RANGE); 8892 8893#if USE_ALGLOCK 8894 RWLOCK(&resolver->alglock, isc_rwlocktype_write); 8895#endif 8896 if (resolver->algorithms == NULL) { 8897 result = dns_rbt_create(resolver->mctx, free_algorithm, 8898 resolver->mctx, &resolver->algorithms); 8899 if (result != ISC_R_SUCCESS) 8900 goto cleanup; 8901 } 8902 8903 len = alg/8 + 2; 8904 mask = 1 << (alg%8); 8905 8906 result = dns_rbt_addnode(resolver->algorithms, name, &node); 8907 8908 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) { 8909 algorithms = node->data; 8910 if (algorithms == NULL || len > *algorithms) { 8911 new = isc_mem_get(resolver->mctx, len); 8912 if (new == NULL) { 8913 result = ISC_R_NOMEMORY; 8914 goto cleanup; 8915 } 8916 memset(new, 0, len); 8917 if (algorithms != NULL) 8918 memmove(new, algorithms, *algorithms); 8919 new[len-1] |= mask; 8920 *new = len; 8921 node->data = new; 8922 if (algorithms != NULL) 8923 isc_mem_put(resolver->mctx, algorithms, 8924 *algorithms); 8925 } else 8926 algorithms[len-1] |= mask; 8927 } 8928 result = ISC_R_SUCCESS; 8929 cleanup: 8930#if USE_ALGLOCK 8931 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write); 8932#endif 8933 return (result); 8934} 8935 8936isc_boolean_t 8937dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name, 8938 unsigned int alg) 8939{ 8940 unsigned int len, mask; 8941 unsigned char *algorithms; 8942 void *data = NULL; 8943 isc_result_t result; 8944 isc_boolean_t found = ISC_FALSE; 8945 8946 REQUIRE(VALID_RESOLVER(resolver)); 8947 8948 /* 8949 * DH is unsupported for DNSKEYs, see RFC 4034 sec. A.1. 8950 */ 8951 if ((alg == DST_ALG_DH) || (alg == DST_ALG_INDIRECT)) 8952 return (ISC_FALSE); 8953 8954#if USE_ALGLOCK 8955 RWLOCK(&resolver->alglock, isc_rwlocktype_read); 8956#endif 8957 if (resolver->algorithms == NULL) 8958 goto unlock; 8959 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data); 8960 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) { 8961 len = alg/8 + 2; 8962 mask = 1 << (alg%8); 8963 algorithms = data; 8964 if (len <= *algorithms && (algorithms[len-1] & mask) != 0) 8965 found = ISC_TRUE; 8966 } 8967 unlock: 8968#if USE_ALGLOCK 8969 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read); 8970#endif 8971 if (found) 8972 return (ISC_FALSE); 8973 8974 return (dst_algorithm_supported(alg)); 8975} 8976 8977isc_boolean_t 8978dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) { 8979 8980 UNUSED(resolver); 8981 return (dns_ds_digest_supported(digest)); 8982} 8983 8984void 8985dns_resolver_resetmustbesecure(dns_resolver_t *resolver) { 8986 8987 REQUIRE(VALID_RESOLVER(resolver)); 8988 8989#if USE_MBSLOCK 8990 RWLOCK(&resolver->mbslock, isc_rwlocktype_write); 8991#endif 8992 if (resolver->mustbesecure != NULL) 8993 dns_rbt_destroy(&resolver->mustbesecure); 8994#if USE_MBSLOCK 8995 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write); 8996#endif 8997} 8998 8999static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE; 9000 9001isc_result_t 9002dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name, 9003 isc_boolean_t value) 9004{ 9005 isc_result_t result; 9006 9007 REQUIRE(VALID_RESOLVER(resolver)); 9008 9009#if USE_MBSLOCK 9010 RWLOCK(&resolver->mbslock, isc_rwlocktype_write); 9011#endif 9012 if (resolver->mustbesecure == NULL) { 9013 result = dns_rbt_create(resolver->mctx, NULL, NULL, 9014 &resolver->mustbesecure); 9015 if (result != ISC_R_SUCCESS) 9016 goto cleanup; 9017 } 9018 result = dns_rbt_addname(resolver->mustbesecure, name, 9019 value ? &yes : &no); 9020 cleanup: 9021#if USE_MBSLOCK 9022 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write); 9023#endif 9024 return (result); 9025} 9026 9027isc_boolean_t 9028dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) { 9029 void *data = NULL; 9030 isc_boolean_t value = ISC_FALSE; 9031 isc_result_t result; 9032 9033 REQUIRE(VALID_RESOLVER(resolver)); 9034 9035#if USE_MBSLOCK 9036 RWLOCK(&resolver->mbslock, isc_rwlocktype_read); 9037#endif 9038 if (resolver->mustbesecure == NULL) 9039 goto unlock; 9040 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data); 9041 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) 9042 value = *(isc_boolean_t*)data; 9043 unlock: 9044#if USE_MBSLOCK 9045 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read); 9046#endif 9047 return (value); 9048} 9049 9050void 9051dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur, 9052 isc_uint32_t *min, isc_uint32_t *max) 9053{ 9054 REQUIRE(VALID_RESOLVER(resolver)); 9055 9056 LOCK(&resolver->lock); 9057 if (cur != NULL) 9058 *cur = resolver->spillat; 9059 if (min != NULL) 9060 *min = resolver->spillatmin; 9061 if (max != NULL) 9062 *max = resolver->spillatmax; 9063 UNLOCK(&resolver->lock); 9064} 9065 9066void 9067dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min, 9068 isc_uint32_t max) 9069{ 9070 REQUIRE(VALID_RESOLVER(resolver)); 9071 9072 LOCK(&resolver->lock); 9073 resolver->spillatmin = resolver->spillat = min; 9074 resolver->spillatmax = max; 9075 UNLOCK(&resolver->lock); 9076} 9077 9078isc_boolean_t 9079dns_resolver_getzeronosoattl(dns_resolver_t *resolver) { 9080 REQUIRE(VALID_RESOLVER(resolver)); 9081 9082 return (resolver->zero_no_soa_ttl); 9083} 9084 9085void 9086dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) { 9087 REQUIRE(VALID_RESOLVER(resolver)); 9088 9089 resolver->zero_no_soa_ttl = state; 9090} 9091 9092unsigned int 9093dns_resolver_getoptions(dns_resolver_t *resolver) { 9094 REQUIRE(VALID_RESOLVER(resolver)); 9095 9096 return (resolver->options); 9097} 9098 9099unsigned int 9100dns_resolver_gettimeout(dns_resolver_t *resolver) { 9101 REQUIRE(VALID_RESOLVER(resolver)); 9102 9103 return (resolver->query_timeout); 9104} 9105 9106void 9107dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) { 9108 REQUIRE(VALID_RESOLVER(resolver)); 9109 9110 if (seconds == 0) 9111 seconds = DEFAULT_QUERY_TIMEOUT; 9112 if (seconds > MAXIMUM_QUERY_TIMEOUT) 9113 seconds = MAXIMUM_QUERY_TIMEOUT; 9114 if (seconds < MINIMUM_QUERY_TIMEOUT) 9115 seconds = MINIMUM_QUERY_TIMEOUT; 9116 9117 resolver->query_timeout = seconds; 9118} 9119 9120void 9121dns_resolver_setmaxdepth(dns_resolver_t *resolver, unsigned int maxdepth) { 9122 REQUIRE(VALID_RESOLVER(resolver)); 9123 resolver->maxdepth = maxdepth; 9124} 9125 9126unsigned int 9127dns_resolver_getmaxdepth(dns_resolver_t *resolver) { 9128 REQUIRE(VALID_RESOLVER(resolver)); 9129 return (resolver->maxdepth); 9130} 9131