1 /* 2 * iterator/iter_utils.c - iterative resolver module utility functions. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains functions to assist the iterator module. 40 * Configuration options. Forward zones. 41 */ 42 #include "config.h" 43 #include "iterator/iter_utils.h" 44 #include "iterator/iterator.h" 45 #include "iterator/iter_hints.h" 46 #include "iterator/iter_fwd.h" 47 #include "iterator/iter_donotq.h" 48 #include "iterator/iter_delegpt.h" 49 #include "iterator/iter_priv.h" 50 #include "services/cache/infra.h" 51 #include "services/cache/dns.h" 52 #include "services/cache/rrset.h" 53 #include "util/net_help.h" 54 #include "util/module.h" 55 #include "util/log.h" 56 #include "util/config_file.h" 57 #include "util/regional.h" 58 #include "util/data/msgparse.h" 59 #include "util/data/dname.h" 60 #include "util/random.h" 61 #include "util/fptr_wlist.h" 62 #include "validator/val_anchor.h" 63 #include "validator/val_kcache.h" 64 #include "validator/val_kentry.h" 65 #include "validator/val_utils.h" 66 #include "validator/val_sigcrypt.h" 67 #include "sldns/sbuffer.h" 68 #include "sldns/str2wire.h" 69 70 /** time when nameserver glue is said to be 'recent' */ 71 #define SUSPICION_RECENT_EXPIRY 86400 72 /** penalty to validation failed blacklisted IPs */ 73 #define BLACKLIST_PENALTY (USEFUL_SERVER_TOP_TIMEOUT*4) 74 75 /** fillup fetch policy array */ 76 static void 77 fetch_fill(struct iter_env* ie, const char* str) 78 { 79 char* s = (char*)str, *e; 80 int i; 81 for(i=0; i<ie->max_dependency_depth+1; i++) { 82 ie->target_fetch_policy[i] = strtol(s, &e, 10); 83 if(s == e) 84 fatal_exit("cannot parse fetch policy number %s", s); 85 s = e; 86 } 87 } 88 89 /** Read config string that represents the target fetch policy */ 90 static int 91 read_fetch_policy(struct iter_env* ie, const char* str) 92 { 93 int count = cfg_count_numbers(str); 94 if(count < 1) { 95 log_err("Cannot parse target fetch policy: \"%s\"", str); 96 return 0; 97 } 98 ie->max_dependency_depth = count - 1; 99 ie->target_fetch_policy = (int*)calloc( 100 (size_t)ie->max_dependency_depth+1, sizeof(int)); 101 if(!ie->target_fetch_policy) { 102 log_err("alloc fetch policy: out of memory"); 103 return 0; 104 } 105 fetch_fill(ie, str); 106 return 1; 107 } 108 109 /** apply config caps whitelist items to name tree */ 110 static int 111 caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg) 112 { 113 struct config_strlist* p; 114 for(p=cfg->caps_whitelist; p; p=p->next) { 115 struct name_tree_node* n; 116 size_t len; 117 uint8_t* nm = sldns_str2wire_dname(p->str, &len); 118 if(!nm) { 119 log_err("could not parse %s", p->str); 120 return 0; 121 } 122 n = (struct name_tree_node*)calloc(1, sizeof(*n)); 123 if(!n) { 124 log_err("out of memory"); 125 free(nm); 126 return 0; 127 } 128 n->node.key = n; 129 n->name = nm; 130 n->len = len; 131 n->labs = dname_count_labels(nm); 132 n->dclass = LDNS_RR_CLASS_IN; 133 if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) { 134 /* duplicate element ignored, idempotent */ 135 free(n->name); 136 free(n); 137 } 138 } 139 name_tree_init_parents(ntree); 140 return 1; 141 } 142 143 int 144 iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg) 145 { 146 int i; 147 /* target fetch policy */ 148 if(!read_fetch_policy(iter_env, cfg->target_fetch_policy)) 149 return 0; 150 for(i=0; i<iter_env->max_dependency_depth+1; i++) 151 verbose(VERB_QUERY, "target fetch policy for level %d is %d", 152 i, iter_env->target_fetch_policy[i]); 153 154 if(!iter_env->donotq) 155 iter_env->donotq = donotq_create(); 156 if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) { 157 log_err("Could not set donotqueryaddresses"); 158 return 0; 159 } 160 if(!iter_env->priv) 161 iter_env->priv = priv_create(); 162 if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) { 163 log_err("Could not set private addresses"); 164 return 0; 165 } 166 if(cfg->caps_whitelist) { 167 if(!iter_env->caps_white) 168 iter_env->caps_white = rbtree_create(name_tree_compare); 169 if(!iter_env->caps_white || !caps_white_apply_cfg( 170 iter_env->caps_white, cfg)) { 171 log_err("Could not set capsforid whitelist"); 172 return 0; 173 } 174 175 } 176 iter_env->supports_ipv6 = cfg->do_ip6; 177 iter_env->supports_ipv4 = cfg->do_ip4; 178 return 1; 179 } 180 181 /** filter out unsuitable targets 182 * @param iter_env: iterator environment with ipv6-support flag. 183 * @param env: module environment with infra cache. 184 * @param name: zone name 185 * @param namelen: length of name 186 * @param qtype: query type (host order). 187 * @param now: current time 188 * @param a: address in delegation point we are examining. 189 * @return an integer that signals the target suitability. 190 * as follows: 191 * -1: The address should be omitted from the list. 192 * Because: 193 * o The address is bogus (DNSSEC validation failure). 194 * o Listed as donotquery 195 * o is ipv6 but no ipv6 support (in operating system). 196 * o is ipv4 but no ipv4 support (in operating system). 197 * o is lame 198 * Otherwise, an rtt in milliseconds. 199 * 0 .. USEFUL_SERVER_TOP_TIMEOUT-1 200 * The roundtrip time timeout estimate. less than 2 minutes. 201 * Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus 202 * values 0 .. 49 are not used, unless that is changed. 203 * USEFUL_SERVER_TOP_TIMEOUT 204 * This value exactly is given for unresponsive blacklisted. 205 * USEFUL_SERVER_TOP_TIMEOUT+1 206 * For non-blacklisted servers: huge timeout, but has traffic. 207 * USEFUL_SERVER_TOP_TIMEOUT*1 .. 208 * parent-side lame servers get this penalty. A dispreferential 209 * server. (lame in delegpt). 210 * USEFUL_SERVER_TOP_TIMEOUT*2 .. 211 * dnsseclame servers get penalty 212 * USEFUL_SERVER_TOP_TIMEOUT*3 .. 213 * recursion lame servers get penalty 214 * UNKNOWN_SERVER_NICENESS 215 * If no information is known about the server, this is 216 * returned. 376 msec or so. 217 * +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs. 218 * 219 * When a final value is chosen that is dnsseclame ; dnsseclameness checking 220 * is turned off (so we do not discard the reply). 221 * When a final value is chosen that is recursionlame; RD bit is set on query. 222 * Because of the numbers this means recursionlame also have dnssec lameness 223 * checking turned off. 224 */ 225 static int 226 iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env, 227 uint8_t* name, size_t namelen, uint16_t qtype, time_t now, 228 struct delegpt_addr* a) 229 { 230 int rtt, lame, reclame, dnsseclame; 231 if(a->bogus) 232 return -1; /* address of server is bogus */ 233 if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) { 234 log_addr(VERB_ALGO, "skip addr on the donotquery list", 235 &a->addr, a->addrlen); 236 return -1; /* server is on the donotquery list */ 237 } 238 if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) { 239 return -1; /* there is no ip6 available */ 240 } 241 if(!iter_env->supports_ipv4 && !addr_is_ip6(&a->addr, a->addrlen)) { 242 return -1; /* there is no ip4 available */ 243 } 244 /* check lameness - need zone , class info */ 245 if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen, 246 name, namelen, qtype, &lame, &dnsseclame, &reclame, 247 &rtt, now)) { 248 log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen); 249 verbose(VERB_ALGO, " rtt=%d%s%s%s%s", rtt, 250 lame?" LAME":"", 251 dnsseclame?" DNSSEC_LAME":"", 252 reclame?" REC_LAME":"", 253 a->lame?" ADDR_LAME":""); 254 if(lame) 255 return -1; /* server is lame */ 256 else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT) 257 /* server is unresponsive, 258 * we used to return TOP_TIMEOUT, but fairly useless, 259 * because if == TOP_TIMEOUT is dropped because 260 * blacklisted later, instead, remove it here, so 261 * other choices (that are not blacklisted) can be 262 * tried */ 263 return -1; 264 /* select remainder from worst to best */ 265 else if(reclame) 266 return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */ 267 else if(dnsseclame || a->dnsseclame) 268 return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */ 269 else if(a->lame) 270 return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */ 271 else return rtt; 272 } 273 /* no server information present */ 274 if(a->dnsseclame) 275 return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */ 276 else if(a->lame) 277 return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */ 278 return UNKNOWN_SERVER_NICENESS; 279 } 280 281 /** lookup RTT information, and also store fastest rtt (if any) */ 282 static int 283 iter_fill_rtt(struct iter_env* iter_env, struct module_env* env, 284 uint8_t* name, size_t namelen, uint16_t qtype, time_t now, 285 struct delegpt* dp, int* best_rtt, struct sock_list* blacklist) 286 { 287 int got_it = 0; 288 struct delegpt_addr* a; 289 if(dp->bogus) 290 return 0; /* NS bogus, all bogus, nothing found */ 291 for(a=dp->result_list; a; a = a->next_result) { 292 a->sel_rtt = iter_filter_unsuitable(iter_env, env, 293 name, namelen, qtype, now, a); 294 if(a->sel_rtt != -1) { 295 if(sock_list_find(blacklist, &a->addr, a->addrlen)) 296 a->sel_rtt += BLACKLIST_PENALTY; 297 298 if(!got_it) { 299 *best_rtt = a->sel_rtt; 300 got_it = 1; 301 } else if(a->sel_rtt < *best_rtt) { 302 *best_rtt = a->sel_rtt; 303 } 304 } 305 } 306 return got_it; 307 } 308 309 /** filter the address list, putting best targets at front, 310 * returns number of best targets (or 0, no suitable targets) */ 311 static int 312 iter_filter_order(struct iter_env* iter_env, struct module_env* env, 313 uint8_t* name, size_t namelen, uint16_t qtype, time_t now, 314 struct delegpt* dp, int* selected_rtt, int open_target, 315 struct sock_list* blacklist, time_t prefetch) 316 { 317 int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND; 318 struct delegpt_addr* a, *n, *prev=NULL; 319 320 /* fillup sel_rtt and find best rtt in the bunch */ 321 got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp, 322 &low_rtt, blacklist); 323 if(got_num == 0) 324 return 0; 325 if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT && 326 (delegpt_count_missing_targets(dp) > 0 || open_target > 0)) { 327 verbose(VERB_ALGO, "Bad choices, trying to get more choice"); 328 return 0; /* we want more choice. The best choice is a bad one. 329 return 0 to force the caller to fetch more */ 330 } 331 332 if(env->cfg->low_rtt_pct != 0 && prefetch == 0 && 333 low_rtt < env->cfg->low_rtt && 334 ub_random_max(env->rnd, 1000) < env->cfg->low_rtt_pct) { 335 /* the query is not prefetch, but for a downstream client, 336 * there is a low_rtt (fast) server. We choose that x% of the 337 * time */ 338 /* pick rtt numbers from 0..LOWBAND_RTT */ 339 rtt_band = env->cfg->low_rtt - low_rtt; 340 } 341 342 got_num = 0; 343 a = dp->result_list; 344 while(a) { 345 /* skip unsuitable targets */ 346 if(a->sel_rtt == -1) { 347 prev = a; 348 a = a->next_result; 349 continue; 350 } 351 /* classify the server address and determine what to do */ 352 swap_to_front = 0; 353 if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) { 354 got_num++; 355 swap_to_front = 1; 356 } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) { 357 got_num++; 358 swap_to_front = 1; 359 } 360 /* swap to front if necessary, or move to next result */ 361 if(swap_to_front && prev) { 362 n = a->next_result; 363 prev->next_result = n; 364 a->next_result = dp->result_list; 365 dp->result_list = a; 366 a = n; 367 } else { 368 prev = a; 369 a = a->next_result; 370 } 371 } 372 *selected_rtt = low_rtt; 373 374 if (env->cfg->prefer_ip6) { 375 int got_num6 = 0; 376 int low_rtt6 = 0; 377 int i; 378 prev = NULL; 379 a = dp->result_list; 380 for(i = 0; i < got_num; i++) { 381 swap_to_front = 0; 382 if(a->addr.ss_family == AF_INET6) { 383 got_num6++; 384 swap_to_front = 1; 385 if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) { 386 low_rtt6 = a->sel_rtt; 387 } 388 } 389 /* swap to front if IPv6, or move to next result */ 390 if(swap_to_front && prev) { 391 n = a->next_result; 392 prev->next_result = n; 393 a->next_result = dp->result_list; 394 dp->result_list = a; 395 a = n; 396 } else { 397 prev = a; 398 a = a->next_result; 399 } 400 } 401 if(got_num6 > 0) { 402 got_num = got_num6; 403 *selected_rtt = low_rtt6; 404 } 405 } 406 return got_num; 407 } 408 409 struct delegpt_addr* 410 iter_server_selection(struct iter_env* iter_env, 411 struct module_env* env, struct delegpt* dp, 412 uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame, 413 int* chase_to_rd, int open_target, struct sock_list* blacklist, 414 time_t prefetch) 415 { 416 int sel; 417 int selrtt; 418 struct delegpt_addr* a, *prev; 419 int num = iter_filter_order(iter_env, env, name, namelen, qtype, 420 *env->now, dp, &selrtt, open_target, blacklist, prefetch); 421 422 if(num == 0) 423 return NULL; 424 verbose(VERB_ALGO, "selrtt %d", selrtt); 425 if(selrtt > BLACKLIST_PENALTY) { 426 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) { 427 verbose(VERB_ALGO, "chase to " 428 "blacklisted recursion lame server"); 429 *chase_to_rd = 1; 430 } 431 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) { 432 verbose(VERB_ALGO, "chase to " 433 "blacklisted dnssec lame server"); 434 *dnssec_lame = 1; 435 } 436 } else { 437 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) { 438 verbose(VERB_ALGO, "chase to recursion lame server"); 439 *chase_to_rd = 1; 440 } 441 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) { 442 verbose(VERB_ALGO, "chase to dnssec lame server"); 443 *dnssec_lame = 1; 444 } 445 if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) { 446 verbose(VERB_ALGO, "chase to blacklisted lame server"); 447 return NULL; 448 } 449 } 450 451 if(num == 1) { 452 a = dp->result_list; 453 if(++a->attempts < OUTBOUND_MSG_RETRY) 454 return a; 455 dp->result_list = a->next_result; 456 return a; 457 } 458 459 /* randomly select a target from the list */ 460 log_assert(num > 1); 461 /* grab secure random number, to pick unexpected server. 462 * also we need it to be threadsafe. */ 463 sel = ub_random_max(env->rnd, num); 464 a = dp->result_list; 465 prev = NULL; 466 while(sel > 0 && a) { 467 prev = a; 468 a = a->next_result; 469 sel--; 470 } 471 if(!a) /* robustness */ 472 return NULL; 473 if(++a->attempts < OUTBOUND_MSG_RETRY) 474 return a; 475 /* remove it from the delegation point result list */ 476 if(prev) 477 prev->next_result = a->next_result; 478 else dp->result_list = a->next_result; 479 return a; 480 } 481 482 struct dns_msg* 483 dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg, 484 struct regional* region) 485 { 486 struct dns_msg* m = (struct dns_msg*)regional_alloc(region, 487 sizeof(struct dns_msg)); 488 if(!m) 489 return NULL; 490 memset(m, 0, sizeof(*m)); 491 if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) { 492 log_err("malloc failure: allocating incoming dns_msg"); 493 return NULL; 494 } 495 return m; 496 } 497 498 struct dns_msg* 499 dns_copy_msg(struct dns_msg* from, struct regional* region) 500 { 501 struct dns_msg* m = (struct dns_msg*)regional_alloc(region, 502 sizeof(struct dns_msg)); 503 if(!m) 504 return NULL; 505 m->qinfo = from->qinfo; 506 if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname, 507 from->qinfo.qname_len))) 508 return NULL; 509 if(!(m->rep = reply_info_copy(from->rep, NULL, region))) 510 return NULL; 511 return m; 512 } 513 514 void 515 iter_dns_store(struct module_env* env, struct query_info* msgqinf, 516 struct reply_info* msgrep, int is_referral, time_t leeway, int pside, 517 struct regional* region, uint16_t flags) 518 { 519 if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway, 520 pside, region, flags)) 521 log_err("out of memory: cannot store data in cache"); 522 } 523 524 int 525 iter_ns_probability(struct ub_randstate* rnd, int n, int m) 526 { 527 int sel; 528 if(n == m) /* 100% chance */ 529 return 1; 530 /* we do not need secure random numbers here, but 531 * we do need it to be threadsafe, so we use this */ 532 sel = ub_random_max(rnd, m); 533 return (sel < n); 534 } 535 536 /** detect dependency cycle for query and target */ 537 static int 538 causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen, 539 uint16_t t, uint16_t c) 540 { 541 struct query_info qinf; 542 qinf.qname = name; 543 qinf.qname_len = namelen; 544 qinf.qtype = t; 545 qinf.qclass = c; 546 qinf.local_alias = NULL; 547 fptr_ok(fptr_whitelist_modenv_detect_cycle( 548 qstate->env->detect_cycle)); 549 return (*qstate->env->detect_cycle)(qstate, &qinf, 550 (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming, 551 qstate->is_valrec); 552 } 553 554 void 555 iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) 556 { 557 struct delegpt_ns* ns; 558 for(ns = dp->nslist; ns; ns = ns->next) { 559 if(ns->resolved) 560 continue; 561 /* see if this ns as target causes dependency cycle */ 562 if(causes_cycle(qstate, ns->name, ns->namelen, 563 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) || 564 causes_cycle(qstate, ns->name, ns->namelen, 565 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { 566 log_nametypeclass(VERB_QUERY, "skipping target due " 567 "to dependency cycle (harden-glue: no may " 568 "fix some of the cycles)", 569 ns->name, LDNS_RR_TYPE_A, 570 qstate->qinfo.qclass); 571 ns->resolved = 1; 572 } 573 } 574 } 575 576 void 577 iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) 578 { 579 struct delegpt_ns* ns; 580 for(ns = dp->nslist; ns; ns = ns->next) { 581 if(ns->done_pside4 && ns->done_pside6) 582 continue; 583 /* see if this ns as target causes dependency cycle */ 584 if(causes_cycle(qstate, ns->name, ns->namelen, 585 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { 586 log_nametypeclass(VERB_QUERY, "skipping target due " 587 "to dependency cycle", ns->name, 588 LDNS_RR_TYPE_A, qstate->qinfo.qclass); 589 ns->done_pside4 = 1; 590 } 591 if(causes_cycle(qstate, ns->name, ns->namelen, 592 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) { 593 log_nametypeclass(VERB_QUERY, "skipping target due " 594 "to dependency cycle", ns->name, 595 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass); 596 ns->done_pside6 = 1; 597 } 598 } 599 } 600 601 int 602 iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags, 603 struct delegpt* dp) 604 { 605 struct delegpt_ns* ns; 606 /* check: 607 * o RD qflag is on. 608 * o no addresses are provided. 609 * o all NS items are required glue. 610 * OR 611 * o RD qflag is on. 612 * o no addresses are provided. 613 * o the query is for one of the nameservers in dp, 614 * and that nameserver is a glue-name for this dp. 615 */ 616 if(!(qflags&BIT_RD)) 617 return 0; 618 /* either available or unused targets */ 619 if(dp->usable_list || dp->result_list) 620 return 0; 621 622 /* see if query is for one of the nameservers, which is glue */ 623 if( (qinfo->qtype == LDNS_RR_TYPE_A || 624 qinfo->qtype == LDNS_RR_TYPE_AAAA) && 625 dname_subdomain_c(qinfo->qname, dp->name) && 626 delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len)) 627 return 1; 628 629 for(ns = dp->nslist; ns; ns = ns->next) { 630 if(ns->resolved) /* skip failed targets */ 631 continue; 632 if(!dname_subdomain_c(ns->name, dp->name)) 633 return 0; /* one address is not required glue */ 634 } 635 return 1; 636 } 637 638 int 639 iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo) 640 { 641 struct trust_anchor* a; 642 if(!env || !env->anchors || !qinfo || !qinfo->qname) 643 return 0; 644 /* a trust anchor exists above the name? */ 645 if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len, 646 qinfo->qclass))) { 647 if(a->numDS == 0 && a->numDNSKEY == 0) { 648 /* insecure trust point */ 649 lock_basic_unlock(&a->lock); 650 return 0; 651 } 652 lock_basic_unlock(&a->lock); 653 return 1; 654 } 655 /* no trust anchor above it. */ 656 return 0; 657 } 658 659 int 660 iter_indicates_dnssec(struct module_env* env, struct delegpt* dp, 661 struct dns_msg* msg, uint16_t dclass) 662 { 663 struct trust_anchor* a; 664 /* information not available, !env->anchors can be common */ 665 if(!env || !env->anchors || !dp || !dp->name) 666 return 0; 667 /* a trust anchor exists with this name, RRSIGs expected */ 668 if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen, 669 dclass))) { 670 if(a->numDS == 0 && a->numDNSKEY == 0) { 671 /* insecure trust point */ 672 lock_basic_unlock(&a->lock); 673 return 0; 674 } 675 lock_basic_unlock(&a->lock); 676 return 1; 677 } 678 /* see if DS rrset was given, in AUTH section */ 679 if(msg && msg->rep && 680 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 681 LDNS_RR_TYPE_DS, dclass)) 682 return 1; 683 /* look in key cache */ 684 if(env->key_cache) { 685 struct key_entry_key* kk = key_cache_obtain(env->key_cache, 686 dp->name, dp->namelen, dclass, env->scratch, *env->now); 687 if(kk) { 688 if(query_dname_compare(kk->name, dp->name) == 0) { 689 if(key_entry_isgood(kk) || key_entry_isbad(kk)) { 690 regional_free_all(env->scratch); 691 return 1; 692 } else if(key_entry_isnull(kk)) { 693 regional_free_all(env->scratch); 694 return 0; 695 } 696 } 697 regional_free_all(env->scratch); 698 } 699 } 700 return 0; 701 } 702 703 int 704 iter_msg_has_dnssec(struct dns_msg* msg) 705 { 706 size_t i; 707 if(!msg || !msg->rep) 708 return 0; 709 for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { 710 if(((struct packed_rrset_data*)msg->rep->rrsets[i]-> 711 entry.data)->rrsig_count > 0) 712 return 1; 713 } 714 /* empty message has no DNSSEC info, with DNSSEC the reply is 715 * not empty (NSEC) */ 716 return 0; 717 } 718 719 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp, 720 enum response_type type, uint16_t dclass) 721 { 722 if(!msg || !dp || !msg->rep || !dp->name) 723 return 0; 724 /* SOA RRset - always from reply zone */ 725 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 726 LDNS_RR_TYPE_SOA, dclass) || 727 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 728 LDNS_RR_TYPE_SOA, dclass)) 729 return 1; 730 if(type == RESPONSE_TYPE_REFERRAL) { 731 size_t i; 732 /* if it adds a single label, i.e. we expect .com, 733 * and referral to example.com. NS ... , then origin zone 734 * is .com. For a referral to sub.example.com. NS ... then 735 * we do not know, since example.com. may be in between. */ 736 for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets; 737 i++) { 738 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 739 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS && 740 ntohs(s->rk.rrset_class) == dclass) { 741 int l = dname_count_labels(s->rk.dname); 742 if(l == dp->namelabs + 1 && 743 dname_strict_subdomain(s->rk.dname, 744 l, dp->name, dp->namelabs)) 745 return 1; 746 } 747 } 748 return 0; 749 } 750 log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME); 751 /* not a referral, and not lame delegation (upwards), so, 752 * any NS rrset must be from the zone itself */ 753 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 754 LDNS_RR_TYPE_NS, dclass) || 755 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 756 LDNS_RR_TYPE_NS, dclass)) 757 return 1; 758 /* a DNSKEY set is expected at the zone apex as well */ 759 /* this is for 'minimal responses' for DNSKEYs */ 760 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 761 LDNS_RR_TYPE_DNSKEY, dclass)) 762 return 1; 763 return 0; 764 } 765 766 /** 767 * check equality of two rrsets 768 * @param k1: rrset 769 * @param k2: rrset 770 * @return true if equal 771 */ 772 static int 773 rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2) 774 { 775 struct packed_rrset_data* d1 = (struct packed_rrset_data*) 776 k1->entry.data; 777 struct packed_rrset_data* d2 = (struct packed_rrset_data*) 778 k2->entry.data; 779 size_t i, t; 780 if(k1->rk.dname_len != k2->rk.dname_len || 781 k1->rk.flags != k2->rk.flags || 782 k1->rk.type != k2->rk.type || 783 k1->rk.rrset_class != k2->rk.rrset_class || 784 query_dname_compare(k1->rk.dname, k2->rk.dname) != 0) 785 return 0; 786 if( /* do not check ttl: d1->ttl != d2->ttl || */ 787 d1->count != d2->count || 788 d1->rrsig_count != d2->rrsig_count || 789 d1->trust != d2->trust || 790 d1->security != d2->security) 791 return 0; 792 t = d1->count + d1->rrsig_count; 793 for(i=0; i<t; i++) { 794 if(d1->rr_len[i] != d2->rr_len[i] || 795 /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/ 796 memcmp(d1->rr_data[i], d2->rr_data[i], 797 d1->rr_len[i]) != 0) 798 return 0; 799 } 800 return 1; 801 } 802 803 int 804 reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region) 805 { 806 size_t i; 807 if(p->flags != q->flags || 808 p->qdcount != q->qdcount || 809 /* do not check TTL, this may differ */ 810 /* 811 p->ttl != q->ttl || 812 p->prefetch_ttl != q->prefetch_ttl || 813 */ 814 p->security != q->security || 815 p->an_numrrsets != q->an_numrrsets || 816 p->ns_numrrsets != q->ns_numrrsets || 817 p->ar_numrrsets != q->ar_numrrsets || 818 p->rrset_count != q->rrset_count) 819 return 0; 820 for(i=0; i<p->rrset_count; i++) { 821 if(!rrset_equal(p->rrsets[i], q->rrsets[i])) { 822 if(!rrset_canonical_equal(region, p->rrsets[i], 823 q->rrsets[i])) { 824 regional_free_all(region); 825 return 0; 826 } 827 regional_free_all(region); 828 } 829 } 830 return 1; 831 } 832 833 void 834 caps_strip_reply(struct reply_info* rep) 835 { 836 size_t i; 837 if(!rep) return; 838 /* see if message is a referral, in which case the additional and 839 * NS record cannot be removed */ 840 /* referrals have the AA flag unset (strict check, not elsewhere in 841 * unbound, but for 0x20 this is very convenient). */ 842 if(!(rep->flags&BIT_AA)) 843 return; 844 /* remove the additional section from the reply */ 845 if(rep->ar_numrrsets != 0) { 846 verbose(VERB_ALGO, "caps fallback: removing additional section"); 847 rep->rrset_count -= rep->ar_numrrsets; 848 rep->ar_numrrsets = 0; 849 } 850 /* is there an NS set in the authority section to remove? */ 851 /* the failure case (Cisco firewalls) only has one rrset in authsec */ 852 for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 853 struct ub_packed_rrset_key* s = rep->rrsets[i]; 854 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) { 855 /* remove NS rrset and break from loop (loop limits 856 * have changed) */ 857 /* move last rrset into this position (there is no 858 * additional section any more) */ 859 verbose(VERB_ALGO, "caps fallback: removing NS rrset"); 860 if(i < rep->rrset_count-1) 861 rep->rrsets[i]=rep->rrsets[rep->rrset_count-1]; 862 rep->rrset_count --; 863 rep->ns_numrrsets --; 864 break; 865 } 866 } 867 } 868 869 int caps_failed_rcode(struct reply_info* rep) 870 { 871 return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR || 872 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN); 873 } 874 875 void 876 iter_store_parentside_rrset(struct module_env* env, 877 struct ub_packed_rrset_key* rrset) 878 { 879 struct rrset_ref ref; 880 rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now); 881 if(!rrset) { 882 log_err("malloc failure in store_parentside_rrset"); 883 return; 884 } 885 rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE; 886 rrset->entry.hash = rrset_key_hash(&rrset->rk); 887 ref.key = rrset; 888 ref.id = rrset->id; 889 /* ignore ret: if it was in the cache, ref updated */ 890 (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now); 891 } 892 893 /** fetch NS record from reply, if any */ 894 static struct ub_packed_rrset_key* 895 reply_get_NS_rrset(struct reply_info* rep) 896 { 897 size_t i; 898 for(i=0; i<rep->rrset_count; i++) { 899 if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) { 900 return rep->rrsets[i]; 901 } 902 } 903 return NULL; 904 } 905 906 void 907 iter_store_parentside_NS(struct module_env* env, struct reply_info* rep) 908 { 909 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); 910 if(rrset) { 911 log_rrset_key(VERB_ALGO, "store parent-side NS", rrset); 912 iter_store_parentside_rrset(env, rrset); 913 } 914 } 915 916 void iter_store_parentside_neg(struct module_env* env, 917 struct query_info* qinfo, struct reply_info* rep) 918 { 919 /* TTL: NS from referral in iq->deleg_msg, 920 * or first RR from iq->response, 921 * or servfail5secs if !iq->response */ 922 time_t ttl = NORR_TTL; 923 struct ub_packed_rrset_key* neg; 924 struct packed_rrset_data* newd; 925 if(rep) { 926 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); 927 if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0]; 928 if(rrset) ttl = ub_packed_rrset_ttl(rrset); 929 } 930 /* create empty rrset to store */ 931 neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch, 932 sizeof(struct ub_packed_rrset_key)); 933 if(!neg) { 934 log_err("out of memory in store_parentside_neg"); 935 return; 936 } 937 memset(&neg->entry, 0, sizeof(neg->entry)); 938 neg->entry.key = neg; 939 neg->rk.type = htons(qinfo->qtype); 940 neg->rk.rrset_class = htons(qinfo->qclass); 941 neg->rk.flags = 0; 942 neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname, 943 qinfo->qname_len); 944 if(!neg->rk.dname) { 945 log_err("out of memory in store_parentside_neg"); 946 return; 947 } 948 neg->rk.dname_len = qinfo->qname_len; 949 neg->entry.hash = rrset_key_hash(&neg->rk); 950 newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch, 951 sizeof(struct packed_rrset_data) + sizeof(size_t) + 952 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)); 953 if(!newd) { 954 log_err("out of memory in store_parentside_neg"); 955 return; 956 } 957 neg->entry.data = newd; 958 newd->ttl = ttl; 959 /* entry must have one RR, otherwise not valid in cache. 960 * put in one RR with empty rdata: those are ignored as nameserver */ 961 newd->count = 1; 962 newd->rrsig_count = 0; 963 newd->trust = rrset_trust_ans_noAA; 964 newd->rr_len = (size_t*)((uint8_t*)newd + 965 sizeof(struct packed_rrset_data)); 966 newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t); 967 packed_rrset_ptr_fixup(newd); 968 newd->rr_ttl[0] = newd->ttl; 969 sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */); 970 /* store it */ 971 log_rrset_key(VERB_ALGO, "store parent-side negative", neg); 972 iter_store_parentside_rrset(env, neg); 973 } 974 975 int 976 iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp, 977 struct regional* region, struct query_info* qinfo) 978 { 979 struct ub_packed_rrset_key* akey; 980 akey = rrset_cache_lookup(env->rrset_cache, dp->name, 981 dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass, 982 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 983 if(akey) { 984 log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey); 985 dp->has_parent_side_NS = 1; 986 /* and mark the new names as lame */ 987 if(!delegpt_rrset_add_ns(dp, region, akey, 1)) { 988 lock_rw_unlock(&akey->entry.lock); 989 return 0; 990 } 991 lock_rw_unlock(&akey->entry.lock); 992 } 993 return 1; 994 } 995 996 int iter_lookup_parent_glue_from_cache(struct module_env* env, 997 struct delegpt* dp, struct regional* region, struct query_info* qinfo) 998 { 999 struct ub_packed_rrset_key* akey; 1000 struct delegpt_ns* ns; 1001 size_t num = delegpt_count_targets(dp); 1002 for(ns = dp->nslist; ns; ns = ns->next) { 1003 /* get cached parentside A */ 1004 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 1005 ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass, 1006 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 1007 if(akey) { 1008 log_rrset_key(VERB_ALGO, "found parent-side", akey); 1009 ns->done_pside4 = 1; 1010 /* a negative-cache-element has no addresses it adds */ 1011 if(!delegpt_add_rrset_A(dp, region, akey, 1)) 1012 log_err("malloc failure in lookup_parent_glue"); 1013 lock_rw_unlock(&akey->entry.lock); 1014 } 1015 /* get cached parentside AAAA */ 1016 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 1017 ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass, 1018 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 1019 if(akey) { 1020 log_rrset_key(VERB_ALGO, "found parent-side", akey); 1021 ns->done_pside6 = 1; 1022 /* a negative-cache-element has no addresses it adds */ 1023 if(!delegpt_add_rrset_AAAA(dp, region, akey, 1)) 1024 log_err("malloc failure in lookup_parent_glue"); 1025 lock_rw_unlock(&akey->entry.lock); 1026 } 1027 } 1028 /* see if new (but lame) addresses have become available */ 1029 return delegpt_count_targets(dp) != num; 1030 } 1031 1032 int 1033 iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd, 1034 uint16_t* c) 1035 { 1036 uint16_t c1 = *c, c2 = *c; 1037 int r1 = hints_next_root(hints, &c1); 1038 int r2 = forwards_next_root(fwd, &c2); 1039 if(!r1 && !r2) /* got none, end of list */ 1040 return 0; 1041 else if(!r1) /* got one, return that */ 1042 *c = c2; 1043 else if(!r2) 1044 *c = c1; 1045 else if(c1 < c2) /* got both take smallest */ 1046 *c = c1; 1047 else *c = c2; 1048 return 1; 1049 } 1050 1051 void 1052 iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z) 1053 { 1054 /* Only the DS record for the delegation itself is expected. 1055 * We allow DS for everything between the bailiwick and the 1056 * zonecut, thus DS records must be at or above the zonecut. 1057 * And the DS records must be below the server authority zone. 1058 * The answer section is already scrubbed. */ 1059 size_t i = msg->rep->an_numrrsets; 1060 while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) { 1061 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1062 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS && 1063 (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname) 1064 || query_dname_compare(z, s->rk.dname) == 0)) { 1065 log_nametypeclass(VERB_ALGO, "removing irrelevant DS", 1066 s->rk.dname, ntohs(s->rk.type), 1067 ntohs(s->rk.rrset_class)); 1068 memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1, 1069 sizeof(struct ub_packed_rrset_key*) * 1070 (msg->rep->rrset_count-i-1)); 1071 msg->rep->ns_numrrsets--; 1072 msg->rep->rrset_count--; 1073 /* stay at same i, but new record */ 1074 continue; 1075 } 1076 i++; 1077 } 1078 } 1079 1080 void iter_dec_attempts(struct delegpt* dp, int d) 1081 { 1082 struct delegpt_addr* a; 1083 for(a=dp->target_list; a; a = a->next_target) { 1084 if(a->attempts >= OUTBOUND_MSG_RETRY) { 1085 /* add back to result list */ 1086 a->next_result = dp->result_list; 1087 dp->result_list = a; 1088 } 1089 if(a->attempts > d) 1090 a->attempts -= d; 1091 else a->attempts = 0; 1092 } 1093 } 1094 1095 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old) 1096 { 1097 struct delegpt_addr* a, *o, *prev; 1098 for(a=dp->target_list; a; a = a->next_target) { 1099 o = delegpt_find_addr(old, &a->addr, a->addrlen); 1100 if(o) { 1101 log_addr(VERB_ALGO, "copy attempt count previous dp", 1102 &a->addr, a->addrlen); 1103 a->attempts = o->attempts; 1104 } 1105 } 1106 prev = NULL; 1107 a = dp->usable_list; 1108 while(a) { 1109 if(a->attempts >= OUTBOUND_MSG_RETRY) { 1110 log_addr(VERB_ALGO, "remove from usable list dp", 1111 &a->addr, a->addrlen); 1112 /* remove from result list */ 1113 if(prev) 1114 prev->next_usable = a->next_usable; 1115 else dp->usable_list = a->next_usable; 1116 /* prev stays the same */ 1117 a = a->next_usable; 1118 continue; 1119 } 1120 prev = a; 1121 a = a->next_usable; 1122 } 1123 } 1124 1125 int 1126 iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp) 1127 { 1128 /* if for query example.com, there is example.com SOA or a subdomain 1129 * of example.com, then we are too low and need to fetch NS. */ 1130 size_t i; 1131 /* if we have a DNAME or CNAME we are probably wrong */ 1132 /* if we have a qtype DS in the answer section, its fine */ 1133 for(i=0; i < msg->rep->an_numrrsets; i++) { 1134 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1135 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME || 1136 ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) { 1137 /* not the right answer, maybe too low, check the 1138 * RRSIG signer name (if there is any) for a hint 1139 * that it is from the dp zone anyway */ 1140 uint8_t* sname; 1141 size_t slen; 1142 val_find_rrset_signer(s, &sname, &slen); 1143 if(sname && query_dname_compare(dp->name, sname)==0) 1144 return 0; /* it is fine, from the right dp */ 1145 return 1; 1146 } 1147 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS) 1148 return 0; /* fine, we have a DS record */ 1149 } 1150 for(i=msg->rep->an_numrrsets; 1151 i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { 1152 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1153 if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) { 1154 if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname)) 1155 return 1; /* point is too low */ 1156 if(query_dname_compare(s->rk.dname, dp->name)==0) 1157 return 0; /* right dp */ 1158 } 1159 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC || 1160 ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1161 uint8_t* sname; 1162 size_t slen; 1163 val_find_rrset_signer(s, &sname, &slen); 1164 if(sname && query_dname_compare(dp->name, sname)==0) 1165 return 0; /* it is fine, from the right dp */ 1166 return 1; 1167 } 1168 } 1169 /* we do not know */ 1170 return 1; 1171 } 1172 1173 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp) 1174 { 1175 /* no delegation point, do not see how we can go down, 1176 * robust check, it should really exist */ 1177 if(!dp) return 0; 1178 1179 /* see if dp equals the qname, then we cannot go down further */ 1180 if(query_dname_compare(qinfo->qname, dp->name) == 0) 1181 return 0; 1182 /* if dp is one label above the name we also cannot go down further */ 1183 if(dname_count_labels(qinfo->qname) == dp->namelabs+1) 1184 return 0; 1185 return 1; 1186 } 1187