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_t* 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) 316 { 317 int got_num = 0, low_rtt = 0, swap_to_front; 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 got_num = 0; 333 a = dp->result_list; 334 while(a) { 335 /* skip unsuitable targets */ 336 if(a->sel_rtt == -1) { 337 prev = a; 338 a = a->next_result; 339 continue; 340 } 341 /* classify the server address and determine what to do */ 342 swap_to_front = 0; 343 if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= RTT_BAND) { 344 got_num++; 345 swap_to_front = 1; 346 } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=RTT_BAND) { 347 got_num++; 348 swap_to_front = 1; 349 } 350 /* swap to front if necessary, or move to next result */ 351 if(swap_to_front && prev) { 352 n = a->next_result; 353 prev->next_result = n; 354 a->next_result = dp->result_list; 355 dp->result_list = a; 356 a = n; 357 } else { 358 prev = a; 359 a = a->next_result; 360 } 361 } 362 *selected_rtt = low_rtt; 363 return got_num; 364 } 365 366 struct delegpt_addr* 367 iter_server_selection(struct iter_env* iter_env, 368 struct module_env* env, struct delegpt* dp, 369 uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame, 370 int* chase_to_rd, int open_target, struct sock_list* blacklist) 371 { 372 int sel; 373 int selrtt; 374 struct delegpt_addr* a, *prev; 375 int num = iter_filter_order(iter_env, env, name, namelen, qtype, 376 *env->now, dp, &selrtt, open_target, blacklist); 377 378 if(num == 0) 379 return NULL; 380 verbose(VERB_ALGO, "selrtt %d", selrtt); 381 if(selrtt > BLACKLIST_PENALTY) { 382 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) { 383 verbose(VERB_ALGO, "chase to " 384 "blacklisted recursion lame server"); 385 *chase_to_rd = 1; 386 } 387 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) { 388 verbose(VERB_ALGO, "chase to " 389 "blacklisted dnssec lame server"); 390 *dnssec_lame = 1; 391 } 392 } else { 393 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) { 394 verbose(VERB_ALGO, "chase to recursion lame server"); 395 *chase_to_rd = 1; 396 } 397 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) { 398 verbose(VERB_ALGO, "chase to dnssec lame server"); 399 *dnssec_lame = 1; 400 } 401 if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) { 402 verbose(VERB_ALGO, "chase to blacklisted lame server"); 403 return NULL; 404 } 405 } 406 407 if(num == 1) { 408 a = dp->result_list; 409 if(++a->attempts < OUTBOUND_MSG_RETRY) 410 return a; 411 dp->result_list = a->next_result; 412 return a; 413 } 414 415 /* randomly select a target from the list */ 416 log_assert(num > 1); 417 /* grab secure random number, to pick unexpected server. 418 * also we need it to be threadsafe. */ 419 sel = ub_random_max(env->rnd, num); 420 a = dp->result_list; 421 prev = NULL; 422 while(sel > 0 && a) { 423 prev = a; 424 a = a->next_result; 425 sel--; 426 } 427 if(!a) /* robustness */ 428 return NULL; 429 if(++a->attempts < OUTBOUND_MSG_RETRY) 430 return a; 431 /* remove it from the delegation point result list */ 432 if(prev) 433 prev->next_result = a->next_result; 434 else dp->result_list = a->next_result; 435 return a; 436 } 437 438 struct dns_msg* 439 dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg, 440 struct regional* region) 441 { 442 struct dns_msg* m = (struct dns_msg*)regional_alloc(region, 443 sizeof(struct dns_msg)); 444 if(!m) 445 return NULL; 446 memset(m, 0, sizeof(*m)); 447 if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) { 448 log_err("malloc failure: allocating incoming dns_msg"); 449 return NULL; 450 } 451 return m; 452 } 453 454 struct dns_msg* 455 dns_copy_msg(struct dns_msg* from, struct regional* region) 456 { 457 struct dns_msg* m = (struct dns_msg*)regional_alloc(region, 458 sizeof(struct dns_msg)); 459 if(!m) 460 return NULL; 461 m->qinfo = from->qinfo; 462 if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname, 463 from->qinfo.qname_len))) 464 return NULL; 465 if(!(m->rep = reply_info_copy(from->rep, NULL, region))) 466 return NULL; 467 return m; 468 } 469 470 void 471 iter_dns_store(struct module_env* env, struct query_info* msgqinf, 472 struct reply_info* msgrep, int is_referral, time_t leeway, int pside, 473 struct regional* region, uint16_t flags) 474 { 475 if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway, 476 pside, region, flags)) 477 log_err("out of memory: cannot store data in cache"); 478 } 479 480 int 481 iter_ns_probability(struct ub_randstate* rnd, int n, int m) 482 { 483 int sel; 484 if(n == m) /* 100% chance */ 485 return 1; 486 /* we do not need secure random numbers here, but 487 * we do need it to be threadsafe, so we use this */ 488 sel = ub_random_max(rnd, m); 489 return (sel < n); 490 } 491 492 /** detect dependency cycle for query and target */ 493 static int 494 causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen, 495 uint16_t t, uint16_t c) 496 { 497 struct query_info qinf; 498 qinf.qname = name; 499 qinf.qname_len = namelen; 500 qinf.qtype = t; 501 qinf.qclass = c; 502 fptr_ok(fptr_whitelist_modenv_detect_cycle( 503 qstate->env->detect_cycle)); 504 return (*qstate->env->detect_cycle)(qstate, &qinf, 505 (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming, 506 qstate->is_valrec); 507 } 508 509 void 510 iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) 511 { 512 struct delegpt_ns* ns; 513 for(ns = dp->nslist; ns; ns = ns->next) { 514 if(ns->resolved) 515 continue; 516 /* see if this ns as target causes dependency cycle */ 517 if(causes_cycle(qstate, ns->name, ns->namelen, 518 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) || 519 causes_cycle(qstate, ns->name, ns->namelen, 520 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { 521 log_nametypeclass(VERB_QUERY, "skipping target due " 522 "to dependency cycle (harden-glue: no may " 523 "fix some of the cycles)", 524 ns->name, LDNS_RR_TYPE_A, 525 qstate->qinfo.qclass); 526 ns->resolved = 1; 527 } 528 } 529 } 530 531 void 532 iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) 533 { 534 struct delegpt_ns* ns; 535 for(ns = dp->nslist; ns; ns = ns->next) { 536 if(ns->done_pside4 && ns->done_pside6) 537 continue; 538 /* see if this ns as target causes dependency cycle */ 539 if(causes_cycle(qstate, ns->name, ns->namelen, 540 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { 541 log_nametypeclass(VERB_QUERY, "skipping target due " 542 "to dependency cycle", ns->name, 543 LDNS_RR_TYPE_A, qstate->qinfo.qclass); 544 ns->done_pside4 = 1; 545 } 546 if(causes_cycle(qstate, ns->name, ns->namelen, 547 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) { 548 log_nametypeclass(VERB_QUERY, "skipping target due " 549 "to dependency cycle", ns->name, 550 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass); 551 ns->done_pside6 = 1; 552 } 553 } 554 } 555 556 int 557 iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags, 558 struct delegpt* dp) 559 { 560 struct delegpt_ns* ns; 561 /* check: 562 * o RD qflag is on. 563 * o no addresses are provided. 564 * o all NS items are required glue. 565 * OR 566 * o RD qflag is on. 567 * o no addresses are provided. 568 * o the query is for one of the nameservers in dp, 569 * and that nameserver is a glue-name for this dp. 570 */ 571 if(!(qflags&BIT_RD)) 572 return 0; 573 /* either available or unused targets */ 574 if(dp->usable_list || dp->result_list) 575 return 0; 576 577 /* see if query is for one of the nameservers, which is glue */ 578 if( (qinfo->qtype == LDNS_RR_TYPE_A || 579 qinfo->qtype == LDNS_RR_TYPE_AAAA) && 580 dname_subdomain_c(qinfo->qname, dp->name) && 581 delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len)) 582 return 1; 583 584 for(ns = dp->nslist; ns; ns = ns->next) { 585 if(ns->resolved) /* skip failed targets */ 586 continue; 587 if(!dname_subdomain_c(ns->name, dp->name)) 588 return 0; /* one address is not required glue */ 589 } 590 return 1; 591 } 592 593 int 594 iter_indicates_dnssec_fwd(struct module_env* env, struct query_info *qinfo) 595 { 596 struct trust_anchor* a; 597 if(!env || !env->anchors || !qinfo || !qinfo->qname) 598 return 0; 599 /* a trust anchor exists above the name? */ 600 if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len, 601 qinfo->qclass))) { 602 if(a->numDS == 0 && a->numDNSKEY == 0) { 603 /* insecure trust point */ 604 lock_basic_unlock(&a->lock); 605 return 0; 606 } 607 lock_basic_unlock(&a->lock); 608 return 1; 609 } 610 /* no trust anchor above it. */ 611 return 0; 612 } 613 614 int 615 iter_indicates_dnssec(struct module_env* env, struct delegpt* dp, 616 struct dns_msg* msg, uint16_t dclass) 617 { 618 struct trust_anchor* a; 619 /* information not available, !env->anchors can be common */ 620 if(!env || !env->anchors || !dp || !dp->name) 621 return 0; 622 /* a trust anchor exists with this name, RRSIGs expected */ 623 if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen, 624 dclass))) { 625 lock_basic_unlock(&a->lock); 626 return 1; 627 } 628 /* see if DS rrset was given, in AUTH section */ 629 if(msg && msg->rep && 630 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 631 LDNS_RR_TYPE_DS, dclass)) 632 return 1; 633 /* look in key cache */ 634 if(env->key_cache) { 635 struct key_entry_key* kk = key_cache_obtain(env->key_cache, 636 dp->name, dp->namelen, dclass, env->scratch, *env->now); 637 if(kk) { 638 if(query_dname_compare(kk->name, dp->name) == 0) { 639 if(key_entry_isgood(kk) || key_entry_isbad(kk)) { 640 regional_free_all(env->scratch); 641 return 1; 642 } else if(key_entry_isnull(kk)) { 643 regional_free_all(env->scratch); 644 return 0; 645 } 646 } 647 regional_free_all(env->scratch); 648 } 649 } 650 return 0; 651 } 652 653 int 654 iter_msg_has_dnssec(struct dns_msg* msg) 655 { 656 size_t i; 657 if(!msg || !msg->rep) 658 return 0; 659 for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { 660 if(((struct packed_rrset_data*)msg->rep->rrsets[i]-> 661 entry.data)->rrsig_count > 0) 662 return 1; 663 } 664 /* empty message has no DNSSEC info, with DNSSEC the reply is 665 * not empty (NSEC) */ 666 return 0; 667 } 668 669 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp, 670 enum response_type type, uint16_t dclass) 671 { 672 if(!msg || !dp || !msg->rep || !dp->name) 673 return 0; 674 /* SOA RRset - always from reply zone */ 675 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 676 LDNS_RR_TYPE_SOA, dclass) || 677 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 678 LDNS_RR_TYPE_SOA, dclass)) 679 return 1; 680 if(type == RESPONSE_TYPE_REFERRAL) { 681 size_t i; 682 /* if it adds a single label, i.e. we expect .com, 683 * and referral to example.com. NS ... , then origin zone 684 * is .com. For a referral to sub.example.com. NS ... then 685 * we do not know, since example.com. may be in between. */ 686 for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets; 687 i++) { 688 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 689 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS && 690 ntohs(s->rk.rrset_class) == dclass) { 691 int l = dname_count_labels(s->rk.dname); 692 if(l == dp->namelabs + 1 && 693 dname_strict_subdomain(s->rk.dname, 694 l, dp->name, dp->namelabs)) 695 return 1; 696 } 697 } 698 return 0; 699 } 700 log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME); 701 /* not a referral, and not lame delegation (upwards), so, 702 * any NS rrset must be from the zone itself */ 703 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 704 LDNS_RR_TYPE_NS, dclass) || 705 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 706 LDNS_RR_TYPE_NS, dclass)) 707 return 1; 708 /* a DNSKEY set is expected at the zone apex as well */ 709 /* this is for 'minimal responses' for DNSKEYs */ 710 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 711 LDNS_RR_TYPE_DNSKEY, dclass)) 712 return 1; 713 return 0; 714 } 715 716 /** 717 * check equality of two rrsets 718 * @param k1: rrset 719 * @param k2: rrset 720 * @return true if equal 721 */ 722 static int 723 rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2) 724 { 725 struct packed_rrset_data* d1 = (struct packed_rrset_data*) 726 k1->entry.data; 727 struct packed_rrset_data* d2 = (struct packed_rrset_data*) 728 k2->entry.data; 729 size_t i, t; 730 if(k1->rk.dname_len != k2->rk.dname_len || 731 k1->rk.flags != k2->rk.flags || 732 k1->rk.type != k2->rk.type || 733 k1->rk.rrset_class != k2->rk.rrset_class || 734 query_dname_compare(k1->rk.dname, k2->rk.dname) != 0) 735 return 0; 736 if( /* do not check ttl: d1->ttl != d2->ttl || */ 737 d1->count != d2->count || 738 d1->rrsig_count != d2->rrsig_count || 739 d1->trust != d2->trust || 740 d1->security != d2->security) 741 return 0; 742 t = d1->count + d1->rrsig_count; 743 for(i=0; i<t; i++) { 744 if(d1->rr_len[i] != d2->rr_len[i] || 745 /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/ 746 memcmp(d1->rr_data[i], d2->rr_data[i], 747 d1->rr_len[i]) != 0) 748 return 0; 749 } 750 return 1; 751 } 752 753 int 754 reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region) 755 { 756 size_t i; 757 if(p->flags != q->flags || 758 p->qdcount != q->qdcount || 759 /* do not check TTL, this may differ */ 760 /* 761 p->ttl != q->ttl || 762 p->prefetch_ttl != q->prefetch_ttl || 763 */ 764 p->security != q->security || 765 p->an_numrrsets != q->an_numrrsets || 766 p->ns_numrrsets != q->ns_numrrsets || 767 p->ar_numrrsets != q->ar_numrrsets || 768 p->rrset_count != q->rrset_count) 769 return 0; 770 for(i=0; i<p->rrset_count; i++) { 771 if(!rrset_equal(p->rrsets[i], q->rrsets[i])) { 772 if(!rrset_canonical_equal(region, p->rrsets[i], 773 q->rrsets[i])) { 774 regional_free_all(region); 775 return 0; 776 } 777 regional_free_all(region); 778 } 779 } 780 return 1; 781 } 782 783 void 784 caps_strip_reply(struct reply_info* rep) 785 { 786 size_t i; 787 if(!rep) return; 788 /* see if message is a referral, in which case the additional and 789 * NS record cannot be removed */ 790 /* referrals have the AA flag unset (strict check, not elsewhere in 791 * unbound, but for 0x20 this is very convenient). */ 792 if(!(rep->flags&BIT_AA)) 793 return; 794 /* remove the additional section from the reply */ 795 if(rep->ar_numrrsets != 0) { 796 verbose(VERB_ALGO, "caps fallback: removing additional section"); 797 rep->rrset_count -= rep->ar_numrrsets; 798 rep->ar_numrrsets = 0; 799 } 800 /* is there an NS set in the authority section to remove? */ 801 /* the failure case (Cisco firewalls) only has one rrset in authsec */ 802 for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 803 struct ub_packed_rrset_key* s = rep->rrsets[i]; 804 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) { 805 /* remove NS rrset and break from loop (loop limits 806 * have changed) */ 807 /* move last rrset into this position (there is no 808 * additional section any more) */ 809 verbose(VERB_ALGO, "caps fallback: removing NS rrset"); 810 if(i < rep->rrset_count-1) 811 rep->rrsets[i]=rep->rrsets[rep->rrset_count-1]; 812 rep->rrset_count --; 813 rep->ns_numrrsets --; 814 break; 815 } 816 } 817 } 818 819 int caps_failed_rcode(struct reply_info* rep) 820 { 821 return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR || 822 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN); 823 } 824 825 void 826 iter_store_parentside_rrset(struct module_env* env, 827 struct ub_packed_rrset_key* rrset) 828 { 829 struct rrset_ref ref; 830 rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now); 831 if(!rrset) { 832 log_err("malloc failure in store_parentside_rrset"); 833 return; 834 } 835 rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE; 836 rrset->entry.hash = rrset_key_hash(&rrset->rk); 837 ref.key = rrset; 838 ref.id = rrset->id; 839 /* ignore ret: if it was in the cache, ref updated */ 840 (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now); 841 } 842 843 /** fetch NS record from reply, if any */ 844 static struct ub_packed_rrset_key* 845 reply_get_NS_rrset(struct reply_info* rep) 846 { 847 size_t i; 848 for(i=0; i<rep->rrset_count; i++) { 849 if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) { 850 return rep->rrsets[i]; 851 } 852 } 853 return NULL; 854 } 855 856 void 857 iter_store_parentside_NS(struct module_env* env, struct reply_info* rep) 858 { 859 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); 860 if(rrset) { 861 log_rrset_key(VERB_ALGO, "store parent-side NS", rrset); 862 iter_store_parentside_rrset(env, rrset); 863 } 864 } 865 866 void iter_store_parentside_neg(struct module_env* env, 867 struct query_info* qinfo, struct reply_info* rep) 868 { 869 /* TTL: NS from referral in iq->deleg_msg, 870 * or first RR from iq->response, 871 * or servfail5secs if !iq->response */ 872 time_t ttl = NORR_TTL; 873 struct ub_packed_rrset_key* neg; 874 struct packed_rrset_data* newd; 875 if(rep) { 876 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); 877 if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0]; 878 if(rrset) ttl = ub_packed_rrset_ttl(rrset); 879 } 880 /* create empty rrset to store */ 881 neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch, 882 sizeof(struct ub_packed_rrset_key)); 883 if(!neg) { 884 log_err("out of memory in store_parentside_neg"); 885 return; 886 } 887 memset(&neg->entry, 0, sizeof(neg->entry)); 888 neg->entry.key = neg; 889 neg->rk.type = htons(qinfo->qtype); 890 neg->rk.rrset_class = htons(qinfo->qclass); 891 neg->rk.flags = 0; 892 neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname, 893 qinfo->qname_len); 894 if(!neg->rk.dname) { 895 log_err("out of memory in store_parentside_neg"); 896 return; 897 } 898 neg->rk.dname_len = qinfo->qname_len; 899 neg->entry.hash = rrset_key_hash(&neg->rk); 900 newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch, 901 sizeof(struct packed_rrset_data) + sizeof(size_t) + 902 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)); 903 if(!newd) { 904 log_err("out of memory in store_parentside_neg"); 905 return; 906 } 907 neg->entry.data = newd; 908 newd->ttl = ttl; 909 /* entry must have one RR, otherwise not valid in cache. 910 * put in one RR with empty rdata: those are ignored as nameserver */ 911 newd->count = 1; 912 newd->rrsig_count = 0; 913 newd->trust = rrset_trust_ans_noAA; 914 newd->rr_len = (size_t*)((uint8_t*)newd + 915 sizeof(struct packed_rrset_data)); 916 newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t); 917 packed_rrset_ptr_fixup(newd); 918 newd->rr_ttl[0] = newd->ttl; 919 sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */); 920 /* store it */ 921 log_rrset_key(VERB_ALGO, "store parent-side negative", neg); 922 iter_store_parentside_rrset(env, neg); 923 } 924 925 int 926 iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp, 927 struct regional* region, struct query_info* qinfo) 928 { 929 struct ub_packed_rrset_key* akey; 930 akey = rrset_cache_lookup(env->rrset_cache, dp->name, 931 dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass, 932 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 933 if(akey) { 934 log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey); 935 dp->has_parent_side_NS = 1; 936 /* and mark the new names as lame */ 937 if(!delegpt_rrset_add_ns(dp, region, akey, 1)) { 938 lock_rw_unlock(&akey->entry.lock); 939 return 0; 940 } 941 lock_rw_unlock(&akey->entry.lock); 942 } 943 return 1; 944 } 945 946 int iter_lookup_parent_glue_from_cache(struct module_env* env, 947 struct delegpt* dp, struct regional* region, struct query_info* qinfo) 948 { 949 struct ub_packed_rrset_key* akey; 950 struct delegpt_ns* ns; 951 size_t num = delegpt_count_targets(dp); 952 for(ns = dp->nslist; ns; ns = ns->next) { 953 /* get cached parentside A */ 954 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 955 ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass, 956 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 957 if(akey) { 958 log_rrset_key(VERB_ALGO, "found parent-side", akey); 959 ns->done_pside4 = 1; 960 /* a negative-cache-element has no addresses it adds */ 961 if(!delegpt_add_rrset_A(dp, region, akey, 1)) 962 log_err("malloc failure in lookup_parent_glue"); 963 lock_rw_unlock(&akey->entry.lock); 964 } 965 /* get cached parentside AAAA */ 966 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 967 ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass, 968 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 969 if(akey) { 970 log_rrset_key(VERB_ALGO, "found parent-side", akey); 971 ns->done_pside6 = 1; 972 /* a negative-cache-element has no addresses it adds */ 973 if(!delegpt_add_rrset_AAAA(dp, region, akey, 1)) 974 log_err("malloc failure in lookup_parent_glue"); 975 lock_rw_unlock(&akey->entry.lock); 976 } 977 } 978 /* see if new (but lame) addresses have become available */ 979 return delegpt_count_targets(dp) != num; 980 } 981 982 int 983 iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd, 984 uint16_t* c) 985 { 986 uint16_t c1 = *c, c2 = *c; 987 int r1 = hints_next_root(hints, &c1); 988 int r2 = forwards_next_root(fwd, &c2); 989 if(!r1 && !r2) /* got none, end of list */ 990 return 0; 991 else if(!r1) /* got one, return that */ 992 *c = c2; 993 else if(!r2) 994 *c = c1; 995 else if(c1 < c2) /* got both take smallest */ 996 *c = c1; 997 else *c = c2; 998 return 1; 999 } 1000 1001 void 1002 iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z) 1003 { 1004 /* Only the DS record for the delegation itself is expected. 1005 * We allow DS for everything between the bailiwick and the 1006 * zonecut, thus DS records must be at or above the zonecut. 1007 * And the DS records must be below the server authority zone. 1008 * The answer section is already scrubbed. */ 1009 size_t i = msg->rep->an_numrrsets; 1010 while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) { 1011 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1012 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS && 1013 (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname) 1014 || query_dname_compare(z, s->rk.dname) == 0)) { 1015 log_nametypeclass(VERB_ALGO, "removing irrelevant DS", 1016 s->rk.dname, ntohs(s->rk.type), 1017 ntohs(s->rk.rrset_class)); 1018 memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1, 1019 sizeof(struct ub_packed_rrset_key*) * 1020 (msg->rep->rrset_count-i-1)); 1021 msg->rep->ns_numrrsets--; 1022 msg->rep->rrset_count--; 1023 /* stay at same i, but new record */ 1024 continue; 1025 } 1026 i++; 1027 } 1028 } 1029 1030 void iter_dec_attempts(struct delegpt* dp, int d) 1031 { 1032 struct delegpt_addr* a; 1033 for(a=dp->target_list; a; a = a->next_target) { 1034 if(a->attempts >= OUTBOUND_MSG_RETRY) { 1035 /* add back to result list */ 1036 a->next_result = dp->result_list; 1037 dp->result_list = a; 1038 } 1039 if(a->attempts > d) 1040 a->attempts -= d; 1041 else a->attempts = 0; 1042 } 1043 } 1044 1045 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old) 1046 { 1047 struct delegpt_addr* a, *o, *prev; 1048 for(a=dp->target_list; a; a = a->next_target) { 1049 o = delegpt_find_addr(old, &a->addr, a->addrlen); 1050 if(o) { 1051 log_addr(VERB_ALGO, "copy attempt count previous dp", 1052 &a->addr, a->addrlen); 1053 a->attempts = o->attempts; 1054 } 1055 } 1056 prev = NULL; 1057 a = dp->usable_list; 1058 while(a) { 1059 if(a->attempts >= OUTBOUND_MSG_RETRY) { 1060 log_addr(VERB_ALGO, "remove from usable list dp", 1061 &a->addr, a->addrlen); 1062 /* remove from result list */ 1063 if(prev) 1064 prev->next_usable = a->next_usable; 1065 else dp->usable_list = a->next_usable; 1066 /* prev stays the same */ 1067 a = a->next_usable; 1068 continue; 1069 } 1070 prev = a; 1071 a = a->next_usable; 1072 } 1073 } 1074 1075 int 1076 iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp) 1077 { 1078 /* if for query example.com, there is example.com SOA or a subdomain 1079 * of example.com, then we are too low and need to fetch NS. */ 1080 size_t i; 1081 /* if we have a DNAME or CNAME we are probably wrong */ 1082 /* if we have a qtype DS in the answer section, its fine */ 1083 for(i=0; i < msg->rep->an_numrrsets; i++) { 1084 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1085 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME || 1086 ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) { 1087 /* not the right answer, maybe too low, check the 1088 * RRSIG signer name (if there is any) for a hint 1089 * that it is from the dp zone anyway */ 1090 uint8_t* sname; 1091 size_t slen; 1092 val_find_rrset_signer(s, &sname, &slen); 1093 if(sname && query_dname_compare(dp->name, sname)==0) 1094 return 0; /* it is fine, from the right dp */ 1095 return 1; 1096 } 1097 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS) 1098 return 0; /* fine, we have a DS record */ 1099 } 1100 for(i=msg->rep->an_numrrsets; 1101 i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { 1102 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1103 if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) { 1104 if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname)) 1105 return 1; /* point is too low */ 1106 if(query_dname_compare(s->rk.dname, dp->name)==0) 1107 return 0; /* right dp */ 1108 } 1109 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC || 1110 ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1111 uint8_t* sname; 1112 size_t slen; 1113 val_find_rrset_signer(s, &sname, &slen); 1114 if(sname && query_dname_compare(dp->name, sname)==0) 1115 return 0; /* it is fine, from the right dp */ 1116 return 1; 1117 } 1118 } 1119 /* we do not know */ 1120 return 1; 1121 } 1122 1123 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp) 1124 { 1125 /* no delegation point, do not see how we can go down, 1126 * robust check, it should really exist */ 1127 if(!dp) return 0; 1128 1129 /* see if dp equals the qname, then we cannot go down further */ 1130 if(query_dname_compare(qinfo->qname, dp->name) == 0) 1131 return 0; 1132 /* if dp is one label above the name we also cannot go down further */ 1133 if(dname_count_labels(qinfo->qname) == dp->namelabs+1) 1134 return 0; 1135 return 1; 1136 } 1137