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 "services/outside_network.h" 54 #include "util/net_help.h" 55 #include "util/module.h" 56 #include "util/log.h" 57 #include "util/config_file.h" 58 #include "util/regional.h" 59 #include "util/data/msgparse.h" 60 #include "util/data/dname.h" 61 #include "util/random.h" 62 #include "util/fptr_wlist.h" 63 #include "validator/val_anchor.h" 64 #include "validator/val_kcache.h" 65 #include "validator/val_kentry.h" 66 #include "validator/val_utils.h" 67 #include "validator/val_sigcrypt.h" 68 #include "sldns/sbuffer.h" 69 #include "sldns/str2wire.h" 70 71 /** time when nameserver glue is said to be 'recent' */ 72 #define SUSPICION_RECENT_EXPIRY 86400 73 74 /** if NAT64 is enabled and no NAT64 prefix is configured, first fall back to 75 * DNS64 prefix. If that is not configured, fall back to this default value. 76 */ 77 static const char DEFAULT_NAT64_PREFIX[] = "64:ff9b::/96"; 78 79 /** fillup fetch policy array */ 80 static int 81 fetch_fill(int* target_fetch_policy, int max_dependency_depth, const char* str) 82 { 83 char* s = (char*)str, *e; 84 int i; 85 for(i=0; i<max_dependency_depth+1; i++) { 86 target_fetch_policy[i] = strtol(s, &e, 10); 87 if(s == e) { 88 log_err("cannot parse fetch policy number %s", s); 89 return 0; 90 } 91 s = e; 92 } 93 return 1; 94 } 95 96 /** Read config string that represents the target fetch policy */ 97 int 98 read_fetch_policy(int** target_fetch_policy, int* max_dependency_depth, 99 const char* str) 100 { 101 int count = cfg_count_numbers(str); 102 if(count < 1) { 103 log_err("Cannot parse target fetch policy: \"%s\"", str); 104 return 0; 105 } 106 *max_dependency_depth = count - 1; 107 *target_fetch_policy = (int*)calloc( 108 (size_t)(*max_dependency_depth)+1, sizeof(int)); 109 if(!*target_fetch_policy) { 110 log_err("alloc fetch policy: out of memory"); 111 return 0; 112 } 113 if(!fetch_fill(*target_fetch_policy, *max_dependency_depth, str)) 114 return 0; 115 return 1; 116 } 117 118 struct rbtree_type* 119 caps_white_create(void) 120 { 121 struct rbtree_type* caps_white = rbtree_create(name_tree_compare); 122 if(!caps_white) 123 log_err("out of memory"); 124 return caps_white; 125 } 126 127 /** delete caps_whitelist element */ 128 static void 129 caps_free(struct rbnode_type* n, void* ATTR_UNUSED(d)) 130 { 131 if(n) { 132 free(((struct name_tree_node*)n)->name); 133 free(n); 134 } 135 } 136 137 void 138 caps_white_delete(struct rbtree_type* caps_white) 139 { 140 if(!caps_white) 141 return; 142 traverse_postorder(caps_white, caps_free, NULL); 143 free(caps_white); 144 } 145 146 int 147 caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg) 148 { 149 struct config_strlist* p; 150 for(p=cfg->caps_whitelist; p; p=p->next) { 151 struct name_tree_node* n; 152 size_t len; 153 uint8_t* nm = sldns_str2wire_dname(p->str, &len); 154 if(!nm) { 155 log_err("could not parse %s", p->str); 156 return 0; 157 } 158 n = (struct name_tree_node*)calloc(1, sizeof(*n)); 159 if(!n) { 160 log_err("out of memory"); 161 free(nm); 162 return 0; 163 } 164 n->node.key = n; 165 n->name = nm; 166 n->len = len; 167 n->labs = dname_count_labels(nm); 168 n->dclass = LDNS_RR_CLASS_IN; 169 if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) { 170 /* duplicate element ignored, idempotent */ 171 free(n->name); 172 free(n); 173 } 174 } 175 name_tree_init_parents(ntree); 176 return 1; 177 } 178 179 int 180 nat64_apply_cfg(struct iter_nat64* nat64, struct config_file* cfg) 181 { 182 const char *nat64_prefix; 183 184 nat64_prefix = cfg->nat64_prefix; 185 if(!nat64_prefix) 186 nat64_prefix = cfg->dns64_prefix; 187 if(!nat64_prefix) 188 nat64_prefix = DEFAULT_NAT64_PREFIX; 189 if(!netblockstrtoaddr(nat64_prefix, 0, &nat64->nat64_prefix_addr, 190 &nat64->nat64_prefix_addrlen, &nat64->nat64_prefix_net)) { 191 log_err("cannot parse nat64-prefix netblock: %s", nat64_prefix); 192 return 0; 193 } 194 if(!addr_is_ip6(&nat64->nat64_prefix_addr, 195 nat64->nat64_prefix_addrlen)) { 196 log_err("nat64-prefix is not IPv6: %s", cfg->nat64_prefix); 197 return 0; 198 } 199 if(!prefixnet_is_nat64(nat64->nat64_prefix_net)) { 200 log_err("nat64-prefix length it not 32, 40, 48, 56, 64 or 96: %s", 201 nat64_prefix); 202 return 0; 203 } 204 nat64->use_nat64 = cfg->do_nat64; 205 return 1; 206 } 207 208 int 209 iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg) 210 { 211 int i; 212 /* target fetch policy */ 213 if(!read_fetch_policy(&iter_env->target_fetch_policy, 214 &iter_env->max_dependency_depth, cfg->target_fetch_policy)) 215 return 0; 216 for(i=0; i<iter_env->max_dependency_depth+1; i++) 217 verbose(VERB_QUERY, "target fetch policy for level %d is %d", 218 i, iter_env->target_fetch_policy[i]); 219 220 if(!iter_env->donotq) 221 iter_env->donotq = donotq_create(); 222 if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) { 223 log_err("Could not set donotqueryaddresses"); 224 return 0; 225 } 226 if(!iter_env->priv) 227 iter_env->priv = priv_create(); 228 if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) { 229 log_err("Could not set private addresses"); 230 return 0; 231 } 232 if(cfg->caps_whitelist) { 233 if(!iter_env->caps_white) 234 iter_env->caps_white = caps_white_create(); 235 if(!iter_env->caps_white || !caps_white_apply_cfg( 236 iter_env->caps_white, cfg)) { 237 log_err("Could not set capsforid whitelist"); 238 return 0; 239 } 240 241 } 242 243 if(!nat64_apply_cfg(&iter_env->nat64, cfg)) { 244 log_err("Could not setup nat64"); 245 return 0; 246 } 247 248 iter_env->supports_ipv6 = cfg->do_ip6; 249 iter_env->supports_ipv4 = cfg->do_ip4; 250 iter_env->outbound_msg_retry = cfg->outbound_msg_retry; 251 iter_env->max_sent_count = cfg->max_sent_count; 252 iter_env->max_query_restarts = cfg->max_query_restarts; 253 return 1; 254 } 255 256 /** filter out unsuitable targets. 257 * Applies NAT64 if needed as well by replacing the IPv4 with the synthesized 258 * IPv6 address. 259 * @param iter_env: iterator environment with ipv6-support flag. 260 * @param env: module environment with infra cache. 261 * @param name: zone name 262 * @param namelen: length of name 263 * @param qtype: query type (host order). 264 * @param now: current time 265 * @param a: address in delegation point we are examining. 266 * @return an integer that signals the target suitability. 267 * as follows: 268 * -1: The address should be omitted from the list. 269 * Because: 270 * o The address is bogus (DNSSEC validation failure). 271 * o Listed as donotquery 272 * o is ipv6 but no ipv6 support (in operating system). 273 * o is ipv4 but no ipv4 support (in operating system). 274 * o is lame 275 * Otherwise, an rtt in milliseconds. 276 * 0 .. USEFUL_SERVER_TOP_TIMEOUT-1 277 * The roundtrip time timeout estimate. less than 2 minutes. 278 * Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus 279 * values 0 .. 49 are not used, unless that is changed. 280 * USEFUL_SERVER_TOP_TIMEOUT 281 * This value exactly is given for unresponsive blacklisted. 282 * USEFUL_SERVER_TOP_TIMEOUT+1 283 * For non-blacklisted servers: huge timeout, but has traffic. 284 * USEFUL_SERVER_TOP_TIMEOUT*1 .. 285 * parent-side lame servers get this penalty. A dispreferential 286 * server. (lame in delegpt). 287 * USEFUL_SERVER_TOP_TIMEOUT*2 .. 288 * dnsseclame servers get penalty 289 * USEFUL_SERVER_TOP_TIMEOUT*3 .. 290 * recursion lame servers get penalty 291 * UNKNOWN_SERVER_NICENESS 292 * If no information is known about the server, this is 293 * returned. 376 msec or so. 294 * +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs. 295 * 296 * When a final value is chosen that is dnsseclame ; dnsseclameness checking 297 * is turned off (so we do not discard the reply). 298 * When a final value is chosen that is recursionlame; RD bit is set on query. 299 * Because of the numbers this means recursionlame also have dnssec lameness 300 * checking turned off. 301 */ 302 static int 303 iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env, 304 uint8_t* name, size_t namelen, uint16_t qtype, time_t now, 305 struct delegpt_addr* a) 306 { 307 int rtt, lame, reclame, dnsseclame; 308 if(a->bogus) 309 return -1; /* address of server is bogus */ 310 if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) { 311 if(iter_env->nat64.use_nat64 && 312 addr_is_ip6(&a->addr, a->addrlen) && 313 a->addrlen == iter_env->nat64.nat64_prefix_addrlen && 314 addr_in_common(&a->addr, 128, 315 &iter_env->nat64.nat64_prefix_addr, 316 iter_env->nat64.nat64_prefix_net, 317 iter_env->nat64.nat64_prefix_addrlen) == 318 iter_env->nat64.nat64_prefix_net) { 319 /* The NAT64 is enabled, and address is IPv6, it is 320 * in the NAT64 prefix. It is allowed. 321 * So that in an IPv6-only cluster without internet 322 * access, that makes the NAT64 translation continue 323 * to work. The NAT64 prefix is allowed. */ 324 /* Otherwise, after a timeout, the already NAT64 325 * translated address would be treated differently, 326 * and that causes confusion. */ 327 log_addr(VERB_ALGO, "the addr is on the donotquery " 328 "list, but allowed because it is NAT64", 329 &a->addr, a->addrlen); 330 } else { 331 log_addr(VERB_ALGO, "skip addr on the donotquery list", 332 &a->addr, a->addrlen); 333 return -1; /* server is on the donotquery list */ 334 } 335 } 336 if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) { 337 return -1; /* there is no ip6 available */ 338 } 339 if(!iter_env->supports_ipv4 && !iter_env->nat64.use_nat64 && 340 !addr_is_ip6(&a->addr, a->addrlen)) { 341 return -1; /* there is no ip4 available */ 342 } 343 if(iter_env->nat64.use_nat64 && !addr_is_ip6(&a->addr, a->addrlen)) { 344 struct sockaddr_storage real_addr; 345 socklen_t real_addrlen; 346 addr_to_nat64(&a->addr, &iter_env->nat64.nat64_prefix_addr, 347 iter_env->nat64.nat64_prefix_addrlen, 348 iter_env->nat64.nat64_prefix_net, 349 &real_addr, &real_addrlen); 350 log_name_addr(VERB_QUERY, "NAT64 apply: from: ", 351 name, &a->addr, a->addrlen); 352 log_name_addr(VERB_QUERY, "NAT64 apply: to: ", 353 name, &real_addr, real_addrlen); 354 a->addr = real_addr; 355 a->addrlen = real_addrlen; 356 } 357 /* check lameness - need zone , class info */ 358 if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen, 359 name, namelen, qtype, &lame, &dnsseclame, &reclame, 360 &rtt, now)) { 361 log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen); 362 verbose(VERB_ALGO, " rtt=%d%s%s%s%s%s", rtt, 363 lame?" LAME":"", 364 dnsseclame?" DNSSEC_LAME":"", 365 a->dnsseclame?" ADDR_DNSSEC_LAME":"", 366 reclame?" REC_LAME":"", 367 a->lame?" ADDR_LAME":""); 368 if(lame) 369 return -1; /* server is lame */ 370 else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT) 371 /* server is unresponsive, 372 * we used to return TOP_TIMEOUT, but fairly useless, 373 * because if == TOP_TIMEOUT is dropped because 374 * blacklisted later, instead, remove it here, so 375 * other choices (that are not blacklisted) can be 376 * tried */ 377 return -1; 378 /* select remainder from worst to best */ 379 else if(reclame) 380 return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */ 381 else if(dnsseclame || a->dnsseclame) 382 return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */ 383 else if(a->lame) 384 return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */ 385 else return rtt; 386 } 387 /* no server information present */ 388 if(a->dnsseclame) 389 return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */ 390 else if(a->lame) 391 return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */ 392 return UNKNOWN_SERVER_NICENESS; 393 } 394 395 /** lookup RTT information, and also store fastest rtt (if any) */ 396 static int 397 iter_fill_rtt(struct iter_env* iter_env, struct module_env* env, 398 uint8_t* name, size_t namelen, uint16_t qtype, time_t now, 399 struct delegpt* dp, int* best_rtt, struct sock_list* blacklist, 400 size_t* num_suitable_results) 401 { 402 int got_it = 0; 403 struct delegpt_addr* a; 404 *num_suitable_results = 0; 405 406 if(dp->bogus) 407 return 0; /* NS bogus, all bogus, nothing found */ 408 for(a=dp->result_list; a; a = a->next_result) { 409 a->sel_rtt = iter_filter_unsuitable(iter_env, env, 410 name, namelen, qtype, now, a); 411 if(a->sel_rtt != -1) { 412 if(sock_list_find(blacklist, &a->addr, a->addrlen)) 413 a->sel_rtt += BLACKLIST_PENALTY; 414 415 if(!got_it) { 416 *best_rtt = a->sel_rtt; 417 got_it = 1; 418 } else if(a->sel_rtt < *best_rtt) { 419 *best_rtt = a->sel_rtt; 420 } 421 (*num_suitable_results)++; 422 } 423 } 424 return got_it; 425 } 426 427 /** compare two rtts, return -1, 0 or 1 */ 428 static int 429 rtt_compare(const void* x, const void* y) 430 { 431 if(*(int*)x == *(int*)y) 432 return 0; 433 if(*(int*)x > *(int*)y) 434 return 1; 435 return -1; 436 } 437 438 /** get RTT for the Nth fastest server */ 439 static int 440 nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n) 441 { 442 int rtt_band; 443 size_t i; 444 int* rtt_list, *rtt_index; 445 446 if(num_results < 1 || n >= num_results) { 447 return -1; 448 } 449 450 rtt_list = calloc(num_results, sizeof(int)); 451 if(!rtt_list) { 452 log_err("malloc failure: allocating rtt_list"); 453 return -1; 454 } 455 rtt_index = rtt_list; 456 457 for(i=0; i<num_results && result_list; i++) { 458 if(result_list->sel_rtt != -1) { 459 *rtt_index = result_list->sel_rtt; 460 rtt_index++; 461 } 462 result_list=result_list->next_result; 463 } 464 qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare); 465 466 log_assert(n > 0); 467 rtt_band = rtt_list[n-1]; 468 free(rtt_list); 469 470 return rtt_band; 471 } 472 473 /** filter the address list, putting best targets at front, 474 * returns number of best targets (or 0, no suitable targets) */ 475 static int 476 iter_filter_order(struct iter_env* iter_env, struct module_env* env, 477 uint8_t* name, size_t namelen, uint16_t qtype, time_t now, 478 struct delegpt* dp, int* selected_rtt, int open_target, 479 struct sock_list* blacklist, time_t prefetch) 480 { 481 int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth; 482 int alllame = 0; 483 size_t num_results; 484 struct delegpt_addr* a, *n, *prev=NULL; 485 486 /* fillup sel_rtt and find best rtt in the bunch */ 487 got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp, 488 &low_rtt, blacklist, &num_results); 489 if(got_num == 0) 490 return 0; 491 if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT && 492 /* If all missing (or not fully resolved) targets are lame, 493 * then use the remaining lame address. */ 494 ((delegpt_count_missing_targets(dp, &alllame) > 0 && !alllame) || 495 open_target > 0)) { 496 verbose(VERB_ALGO, "Bad choices, trying to get more choice"); 497 return 0; /* we want more choice. The best choice is a bad one. 498 return 0 to force the caller to fetch more */ 499 } 500 501 if(env->cfg->fast_server_permil != 0 && prefetch == 0 && 502 num_results > env->cfg->fast_server_num && 503 ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) { 504 /* the query is not prefetch, but for a downstream client, 505 * there are more servers available then the fastest N we want 506 * to choose from. Limit our choice to the fastest servers. */ 507 nth = nth_rtt(dp->result_list, num_results, 508 env->cfg->fast_server_num); 509 if(nth > 0) { 510 rtt_band = nth - low_rtt; 511 if(rtt_band > RTT_BAND) 512 rtt_band = RTT_BAND; 513 } 514 } 515 516 got_num = 0; 517 a = dp->result_list; 518 while(a) { 519 /* skip unsuitable targets */ 520 if(a->sel_rtt == -1) { 521 prev = a; 522 a = a->next_result; 523 continue; 524 } 525 /* classify the server address and determine what to do */ 526 swap_to_front = 0; 527 if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) { 528 got_num++; 529 swap_to_front = 1; 530 } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) { 531 got_num++; 532 swap_to_front = 1; 533 } 534 /* swap to front if necessary, or move to next result */ 535 if(swap_to_front && prev) { 536 n = a->next_result; 537 prev->next_result = n; 538 a->next_result = dp->result_list; 539 dp->result_list = a; 540 a = n; 541 } else { 542 prev = a; 543 a = a->next_result; 544 } 545 } 546 *selected_rtt = low_rtt; 547 548 if (env->cfg->prefer_ip6) { 549 int got_num6 = 0; 550 int low_rtt6 = 0; 551 int i; 552 int attempt = -1; /* filter to make sure addresses have 553 less attempts on them than the first, to force round 554 robin when all the IPv6 addresses fail */ 555 int num4ok = 0; /* number ip4 at low attempt count */ 556 int num4_lowrtt = 0; 557 prev = NULL; 558 a = dp->result_list; 559 for(i = 0; i < got_num; i++) { 560 if(!a) break; /* robustness */ 561 swap_to_front = 0; 562 if(a->addr.ss_family != AF_INET6 && attempt == -1) { 563 /* if we only have ip4 at low attempt count, 564 * then ip6 is failing, and we need to 565 * select one of the remaining IPv4 addrs */ 566 attempt = a->attempts; 567 num4ok++; 568 num4_lowrtt = a->sel_rtt; 569 } else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) { 570 num4ok++; 571 if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) { 572 num4_lowrtt = a->sel_rtt; 573 } 574 } 575 if(a->addr.ss_family == AF_INET6) { 576 if(attempt == -1) { 577 attempt = a->attempts; 578 } else if(a->attempts > attempt) { 579 break; 580 } 581 got_num6++; 582 swap_to_front = 1; 583 if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) { 584 low_rtt6 = a->sel_rtt; 585 } 586 } 587 /* swap to front if IPv6, or move to next result */ 588 if(swap_to_front && prev) { 589 n = a->next_result; 590 prev->next_result = n; 591 a->next_result = dp->result_list; 592 dp->result_list = a; 593 a = n; 594 } else { 595 prev = a; 596 a = a->next_result; 597 } 598 } 599 if(got_num6 > 0) { 600 got_num = got_num6; 601 *selected_rtt = low_rtt6; 602 } else if(num4ok > 0) { 603 got_num = num4ok; 604 *selected_rtt = num4_lowrtt; 605 } 606 } else if (env->cfg->prefer_ip4) { 607 int got_num4 = 0; 608 int low_rtt4 = 0; 609 int i; 610 int attempt = -1; /* filter to make sure addresses have 611 less attempts on them than the first, to force round 612 robin when all the IPv4 addresses fail */ 613 int num6ok = 0; /* number ip6 at low attempt count */ 614 int num6_lowrtt = 0; 615 prev = NULL; 616 a = dp->result_list; 617 for(i = 0; i < got_num; i++) { 618 if(!a) break; /* robustness */ 619 swap_to_front = 0; 620 if(a->addr.ss_family != AF_INET && attempt == -1) { 621 /* if we only have ip6 at low attempt count, 622 * then ip4 is failing, and we need to 623 * select one of the remaining IPv6 addrs */ 624 attempt = a->attempts; 625 num6ok++; 626 num6_lowrtt = a->sel_rtt; 627 } else if(a->addr.ss_family != AF_INET && attempt == a->attempts) { 628 num6ok++; 629 if(num6_lowrtt == 0 || a->sel_rtt < num6_lowrtt) { 630 num6_lowrtt = a->sel_rtt; 631 } 632 } 633 if(a->addr.ss_family == AF_INET) { 634 if(attempt == -1) { 635 attempt = a->attempts; 636 } else if(a->attempts > attempt) { 637 break; 638 } 639 got_num4++; 640 swap_to_front = 1; 641 if(low_rtt4 == 0 || a->sel_rtt < low_rtt4) { 642 low_rtt4 = a->sel_rtt; 643 } 644 } 645 /* swap to front if IPv4, or move to next result */ 646 if(swap_to_front && prev) { 647 n = a->next_result; 648 prev->next_result = n; 649 a->next_result = dp->result_list; 650 dp->result_list = a; 651 a = n; 652 } else { 653 prev = a; 654 a = a->next_result; 655 } 656 } 657 if(got_num4 > 0) { 658 got_num = got_num4; 659 *selected_rtt = low_rtt4; 660 } else if(num6ok > 0) { 661 got_num = num6ok; 662 *selected_rtt = num6_lowrtt; 663 } 664 } 665 return got_num; 666 } 667 668 struct delegpt_addr* 669 iter_server_selection(struct iter_env* iter_env, 670 struct module_env* env, struct delegpt* dp, 671 uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame, 672 int* chase_to_rd, int open_target, struct sock_list* blacklist, 673 time_t prefetch) 674 { 675 int sel; 676 int selrtt; 677 struct delegpt_addr* a, *prev; 678 int num = iter_filter_order(iter_env, env, name, namelen, qtype, 679 *env->now, dp, &selrtt, open_target, blacklist, prefetch); 680 681 if(num == 0) 682 return NULL; 683 verbose(VERB_ALGO, "selrtt %d", selrtt); 684 if(selrtt > BLACKLIST_PENALTY) { 685 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) { 686 verbose(VERB_ALGO, "chase to " 687 "blacklisted recursion lame server"); 688 *chase_to_rd = 1; 689 } 690 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) { 691 verbose(VERB_ALGO, "chase to " 692 "blacklisted dnssec lame server"); 693 *dnssec_lame = 1; 694 } 695 } else { 696 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) { 697 verbose(VERB_ALGO, "chase to recursion lame server"); 698 *chase_to_rd = 1; 699 } 700 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) { 701 verbose(VERB_ALGO, "chase to dnssec lame server"); 702 *dnssec_lame = 1; 703 } 704 if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) { 705 verbose(VERB_ALGO, "chase to blacklisted lame server"); 706 return NULL; 707 } 708 } 709 710 if(num == 1) { 711 a = dp->result_list; 712 if(++a->attempts < iter_env->outbound_msg_retry) 713 return a; 714 dp->result_list = a->next_result; 715 return a; 716 } 717 718 /* randomly select a target from the list */ 719 log_assert(num > 1); 720 /* grab secure random number, to pick unexpected server. 721 * also we need it to be threadsafe. */ 722 sel = ub_random_max(env->rnd, num); 723 a = dp->result_list; 724 prev = NULL; 725 while(sel > 0 && a) { 726 prev = a; 727 a = a->next_result; 728 sel--; 729 } 730 if(!a) /* robustness */ 731 return NULL; 732 if(++a->attempts < iter_env->outbound_msg_retry) 733 return a; 734 /* remove it from the delegation point result list */ 735 if(prev) 736 prev->next_result = a->next_result; 737 else dp->result_list = a->next_result; 738 return a; 739 } 740 741 struct dns_msg* 742 dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg, 743 struct regional* region) 744 { 745 struct dns_msg* m = (struct dns_msg*)regional_alloc(region, 746 sizeof(struct dns_msg)); 747 if(!m) 748 return NULL; 749 memset(m, 0, sizeof(*m)); 750 if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) { 751 log_err("malloc failure: allocating incoming dns_msg"); 752 return NULL; 753 } 754 return m; 755 } 756 757 struct dns_msg* 758 dns_copy_msg(struct dns_msg* from, struct regional* region) 759 { 760 struct dns_msg* m = (struct dns_msg*)regional_alloc(region, 761 sizeof(struct dns_msg)); 762 if(!m) 763 return NULL; 764 m->qinfo = from->qinfo; 765 if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname, 766 from->qinfo.qname_len))) 767 return NULL; 768 if(!(m->rep = reply_info_copy(from->rep, NULL, region))) 769 return NULL; 770 return m; 771 } 772 773 void 774 iter_dns_store(struct module_env* env, struct query_info* msgqinf, 775 struct reply_info* msgrep, int is_referral, time_t leeway, int pside, 776 struct regional* region, uint16_t flags, time_t qstarttime, 777 int is_valrec) 778 { 779 if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway, 780 pside, region, flags, qstarttime, is_valrec)) 781 log_err("out of memory: cannot store data in cache"); 782 } 783 784 int 785 iter_ns_probability(struct ub_randstate* rnd, int n, int m) 786 { 787 int sel; 788 if(n == m) /* 100% chance */ 789 return 1; 790 /* we do not need secure random numbers here, but 791 * we do need it to be threadsafe, so we use this */ 792 sel = ub_random_max(rnd, m); 793 return (sel < n); 794 } 795 796 /** detect dependency cycle for query and target */ 797 static int 798 causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen, 799 uint16_t t, uint16_t c) 800 { 801 struct query_info qinf; 802 qinf.qname = name; 803 qinf.qname_len = namelen; 804 qinf.qtype = t; 805 qinf.qclass = c; 806 qinf.local_alias = NULL; 807 fptr_ok(fptr_whitelist_modenv_detect_cycle( 808 qstate->env->detect_cycle)); 809 return (*qstate->env->detect_cycle)(qstate, &qinf, 810 (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming, 811 qstate->is_valrec); 812 } 813 814 void 815 iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) 816 { 817 struct delegpt_ns* ns; 818 for(ns = dp->nslist; ns; ns = ns->next) { 819 if(ns->resolved) 820 continue; 821 /* see if this ns as target causes dependency cycle */ 822 if(causes_cycle(qstate, ns->name, ns->namelen, 823 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) || 824 causes_cycle(qstate, ns->name, ns->namelen, 825 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { 826 log_nametypeclass(VERB_QUERY, "skipping target due " 827 "to dependency cycle (harden-glue: no may " 828 "fix some of the cycles)", 829 ns->name, LDNS_RR_TYPE_A, 830 qstate->qinfo.qclass); 831 ns->resolved = 1; 832 } 833 } 834 } 835 836 void 837 iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) 838 { 839 struct delegpt_ns* ns; 840 for(ns = dp->nslist; ns; ns = ns->next) { 841 if(ns->done_pside4 && ns->done_pside6) 842 continue; 843 /* see if this ns as target causes dependency cycle */ 844 if(causes_cycle(qstate, ns->name, ns->namelen, 845 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { 846 log_nametypeclass(VERB_QUERY, "skipping target due " 847 "to dependency cycle", ns->name, 848 LDNS_RR_TYPE_A, qstate->qinfo.qclass); 849 ns->done_pside4 = 1; 850 } 851 if(causes_cycle(qstate, ns->name, ns->namelen, 852 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) { 853 log_nametypeclass(VERB_QUERY, "skipping target due " 854 "to dependency cycle", ns->name, 855 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass); 856 ns->done_pside6 = 1; 857 } 858 } 859 } 860 861 int 862 iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags, 863 struct delegpt* dp, int supports_ipv4, int supports_ipv6, 864 int use_nat64) 865 { 866 struct delegpt_ns* ns; 867 struct delegpt_addr* a; 868 869 if(supports_ipv6 && use_nat64) 870 supports_ipv4 = 1; 871 872 /* check: 873 * o RD qflag is on. 874 * o no addresses are provided. 875 * o all NS items are required glue. 876 * OR 877 * o RD qflag is on. 878 * o no addresses are provided. 879 * o the query is for one of the nameservers in dp, 880 * and that nameserver is a glue-name for this dp. 881 */ 882 if(!(qflags&BIT_RD)) 883 return 0; 884 /* either available or unused targets, 885 * if they exist, the dp is not useless. */ 886 for(a = dp->usable_list; a; a = a->next_usable) { 887 if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4) 888 return 0; 889 else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6) 890 return 0; 891 } 892 for(a = dp->result_list; a; a = a->next_result) { 893 if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4) 894 return 0; 895 else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6) 896 return 0; 897 } 898 899 /* see if query is for one of the nameservers, which is glue */ 900 if( ((qinfo->qtype == LDNS_RR_TYPE_A && supports_ipv4) || 901 (qinfo->qtype == LDNS_RR_TYPE_AAAA && supports_ipv6)) && 902 dname_subdomain_c(qinfo->qname, dp->name) && 903 delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len)) 904 return 1; 905 906 for(ns = dp->nslist; ns; ns = ns->next) { 907 if(ns->resolved) /* skip failed targets */ 908 continue; 909 if(!dname_subdomain_c(ns->name, dp->name)) 910 return 0; /* one address is not required glue */ 911 } 912 return 1; 913 } 914 915 int 916 iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo) 917 { 918 struct trust_anchor* a; 919 if(!env || !env->anchors || !qinfo || !qinfo->qname) 920 return 0; 921 /* a trust anchor exists above the name? */ 922 if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len, 923 qinfo->qclass))) { 924 if(a->numDS == 0 && a->numDNSKEY == 0) { 925 /* insecure trust point */ 926 lock_basic_unlock(&a->lock); 927 return 0; 928 } 929 lock_basic_unlock(&a->lock); 930 return 1; 931 } 932 /* no trust anchor above it. */ 933 return 0; 934 } 935 936 int 937 iter_indicates_dnssec(struct module_env* env, struct delegpt* dp, 938 struct dns_msg* msg, uint16_t dclass) 939 { 940 struct trust_anchor* a; 941 /* information not available, !env->anchors can be common */ 942 if(!env || !env->anchors || !dp || !dp->name) 943 return 0; 944 /* a trust anchor exists with this name, RRSIGs expected */ 945 if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen, 946 dclass))) { 947 if(a->numDS == 0 && a->numDNSKEY == 0) { 948 /* insecure trust point */ 949 lock_basic_unlock(&a->lock); 950 return 0; 951 } 952 lock_basic_unlock(&a->lock); 953 return 1; 954 } 955 /* see if DS rrset was given, in AUTH section */ 956 if(msg && msg->rep && 957 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 958 LDNS_RR_TYPE_DS, dclass)) 959 return 1; 960 /* look in key cache */ 961 if(env->key_cache) { 962 struct key_entry_key* kk = key_cache_obtain(env->key_cache, 963 dp->name, dp->namelen, dclass, env->scratch, *env->now); 964 if(kk) { 965 if(query_dname_compare(kk->name, dp->name) == 0) { 966 if(key_entry_isgood(kk) || key_entry_isbad(kk)) { 967 regional_free_all(env->scratch); 968 return 1; 969 } else if(key_entry_isnull(kk)) { 970 regional_free_all(env->scratch); 971 return 0; 972 } 973 } 974 regional_free_all(env->scratch); 975 } 976 } 977 return 0; 978 } 979 980 int 981 iter_msg_has_dnssec(struct dns_msg* msg) 982 { 983 size_t i; 984 if(!msg || !msg->rep) 985 return 0; 986 for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { 987 if(((struct packed_rrset_data*)msg->rep->rrsets[i]-> 988 entry.data)->rrsig_count > 0) 989 return 1; 990 } 991 /* empty message has no DNSSEC info, with DNSSEC the reply is 992 * not empty (NSEC) */ 993 return 0; 994 } 995 996 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp, 997 enum response_type type, uint16_t dclass) 998 { 999 if(!msg || !dp || !msg->rep || !dp->name) 1000 return 0; 1001 /* SOA RRset - always from reply zone */ 1002 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 1003 LDNS_RR_TYPE_SOA, dclass) || 1004 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 1005 LDNS_RR_TYPE_SOA, dclass)) 1006 return 1; 1007 if(type == RESPONSE_TYPE_REFERRAL) { 1008 size_t i; 1009 /* if it adds a single label, i.e. we expect .com, 1010 * and referral to example.com. NS ... , then origin zone 1011 * is .com. For a referral to sub.example.com. NS ... then 1012 * we do not know, since example.com. may be in between. */ 1013 for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets; 1014 i++) { 1015 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1016 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS && 1017 ntohs(s->rk.rrset_class) == dclass) { 1018 int l = dname_count_labels(s->rk.dname); 1019 if(l == dp->namelabs + 1 && 1020 dname_strict_subdomain(s->rk.dname, 1021 l, dp->name, dp->namelabs)) 1022 return 1; 1023 } 1024 } 1025 return 0; 1026 } 1027 log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME); 1028 /* not a referral, and not lame delegation (upwards), so, 1029 * any NS rrset must be from the zone itself */ 1030 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 1031 LDNS_RR_TYPE_NS, dclass) || 1032 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, 1033 LDNS_RR_TYPE_NS, dclass)) 1034 return 1; 1035 /* a DNSKEY set is expected at the zone apex as well */ 1036 /* this is for 'minimal responses' for DNSKEYs */ 1037 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, 1038 LDNS_RR_TYPE_DNSKEY, dclass)) 1039 return 1; 1040 return 0; 1041 } 1042 1043 /** 1044 * check equality of two rrsets 1045 * @param k1: rrset 1046 * @param k2: rrset 1047 * @return true if equal 1048 */ 1049 static int 1050 rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2) 1051 { 1052 struct packed_rrset_data* d1 = (struct packed_rrset_data*) 1053 k1->entry.data; 1054 struct packed_rrset_data* d2 = (struct packed_rrset_data*) 1055 k2->entry.data; 1056 size_t i, t; 1057 if(k1->rk.dname_len != k2->rk.dname_len || 1058 k1->rk.flags != k2->rk.flags || 1059 k1->rk.type != k2->rk.type || 1060 k1->rk.rrset_class != k2->rk.rrset_class || 1061 query_dname_compare(k1->rk.dname, k2->rk.dname) != 0) 1062 return 0; 1063 if( /* do not check ttl: d1->ttl != d2->ttl || */ 1064 d1->count != d2->count || 1065 d1->rrsig_count != d2->rrsig_count || 1066 d1->trust != d2->trust || 1067 d1->security != d2->security) 1068 return 0; 1069 t = d1->count + d1->rrsig_count; 1070 for(i=0; i<t; i++) { 1071 if(d1->rr_len[i] != d2->rr_len[i] || 1072 /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/ 1073 memcmp(d1->rr_data[i], d2->rr_data[i], 1074 d1->rr_len[i]) != 0) 1075 return 0; 1076 } 1077 return 1; 1078 } 1079 1080 /** compare rrsets and sort canonically. Compares rrset name, type, class. 1081 * return 0 if equal, +1 if x > y, and -1 if x < y. 1082 */ 1083 static int 1084 rrset_canonical_sort_cmp(const void* x, const void* y) 1085 { 1086 struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x; 1087 struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y; 1088 int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname); 1089 if(r != 0) 1090 return r; 1091 if(rrx->rk.type != rry->rk.type) { 1092 if(ntohs(rrx->rk.type) > ntohs(rry->rk.type)) 1093 return 1; 1094 else return -1; 1095 } 1096 if(rrx->rk.rrset_class != rry->rk.rrset_class) { 1097 if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class)) 1098 return 1; 1099 else return -1; 1100 } 1101 return 0; 1102 } 1103 1104 int 1105 reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region) 1106 { 1107 size_t i; 1108 struct ub_packed_rrset_key** sorted_p, **sorted_q; 1109 if(p->flags != q->flags || 1110 p->qdcount != q->qdcount || 1111 /* do not check TTL, this may differ */ 1112 /* 1113 p->ttl != q->ttl || 1114 p->prefetch_ttl != q->prefetch_ttl || 1115 */ 1116 p->security != q->security || 1117 p->an_numrrsets != q->an_numrrsets || 1118 p->ns_numrrsets != q->ns_numrrsets || 1119 p->ar_numrrsets != q->ar_numrrsets || 1120 p->rrset_count != q->rrset_count) 1121 return 0; 1122 /* sort the rrsets in the authority and additional sections before 1123 * compare, the query and answer sections are ordered in the sequence 1124 * they should have (eg. one after the other for aliases). */ 1125 sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init( 1126 region, p->rrsets, sizeof(*sorted_p)*p->rrset_count); 1127 if(!sorted_p) return 0; 1128 log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <= 1129 p->rrset_count); 1130 qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets, 1131 sizeof(*sorted_p), rrset_canonical_sort_cmp); 1132 qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets, 1133 sizeof(*sorted_p), rrset_canonical_sort_cmp); 1134 1135 sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init( 1136 region, q->rrsets, sizeof(*sorted_q)*q->rrset_count); 1137 if(!sorted_q) { 1138 regional_free_all(region); 1139 return 0; 1140 } 1141 log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <= 1142 q->rrset_count); 1143 qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets, 1144 sizeof(*sorted_q), rrset_canonical_sort_cmp); 1145 qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets, 1146 sizeof(*sorted_q), rrset_canonical_sort_cmp); 1147 1148 /* compare the rrsets */ 1149 for(i=0; i<p->rrset_count; i++) { 1150 if(!rrset_equal(sorted_p[i], sorted_q[i])) { 1151 if(!rrset_canonical_equal(region, sorted_p[i], 1152 sorted_q[i])) { 1153 regional_free_all(region); 1154 return 0; 1155 } 1156 } 1157 } 1158 regional_free_all(region); 1159 return 1; 1160 } 1161 1162 void 1163 caps_strip_reply(struct reply_info* rep) 1164 { 1165 size_t i; 1166 if(!rep) return; 1167 /* see if message is a referral, in which case the additional and 1168 * NS record cannot be removed */ 1169 /* referrals have the AA flag unset (strict check, not elsewhere in 1170 * unbound, but for 0x20 this is very convenient). */ 1171 if(!(rep->flags&BIT_AA)) 1172 return; 1173 /* remove the additional section from the reply */ 1174 if(rep->ar_numrrsets != 0) { 1175 verbose(VERB_ALGO, "caps fallback: removing additional section"); 1176 rep->rrset_count -= rep->ar_numrrsets; 1177 rep->ar_numrrsets = 0; 1178 } 1179 /* is there an NS set in the authority section to remove? */ 1180 /* the failure case (Cisco firewalls) only has one rrset in authsec */ 1181 for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 1182 struct ub_packed_rrset_key* s = rep->rrsets[i]; 1183 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) { 1184 /* remove NS rrset and break from loop (loop limits 1185 * have changed) */ 1186 /* move last rrset into this position (there is no 1187 * additional section any more) */ 1188 verbose(VERB_ALGO, "caps fallback: removing NS rrset"); 1189 if(i < rep->rrset_count-1) 1190 rep->rrsets[i]=rep->rrsets[rep->rrset_count-1]; 1191 rep->rrset_count --; 1192 rep->ns_numrrsets --; 1193 break; 1194 } 1195 } 1196 } 1197 1198 int caps_failed_rcode(struct reply_info* rep) 1199 { 1200 return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR || 1201 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN); 1202 } 1203 1204 void 1205 iter_store_parentside_rrset(struct module_env* env, 1206 struct ub_packed_rrset_key* rrset) 1207 { 1208 struct rrset_ref ref; 1209 rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now); 1210 if(!rrset) { 1211 log_err("malloc failure in store_parentside_rrset"); 1212 return; 1213 } 1214 rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE; 1215 rrset->entry.hash = rrset_key_hash(&rrset->rk); 1216 ref.key = rrset; 1217 ref.id = rrset->id; 1218 /* ignore ret: if it was in the cache, ref updated */ 1219 (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now); 1220 } 1221 1222 /** fetch NS record from reply, if any */ 1223 static struct ub_packed_rrset_key* 1224 reply_get_NS_rrset(struct reply_info* rep) 1225 { 1226 size_t i; 1227 for(i=0; i<rep->rrset_count; i++) { 1228 if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) { 1229 return rep->rrsets[i]; 1230 } 1231 } 1232 return NULL; 1233 } 1234 1235 void 1236 iter_store_parentside_NS(struct module_env* env, struct reply_info* rep) 1237 { 1238 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); 1239 if(rrset) { 1240 log_rrset_key(VERB_ALGO, "store parent-side NS", rrset); 1241 iter_store_parentside_rrset(env, rrset); 1242 } 1243 } 1244 1245 void iter_store_parentside_neg(struct module_env* env, 1246 struct query_info* qinfo, struct reply_info* rep) 1247 { 1248 /* TTL: NS from referral in iq->deleg_msg, 1249 * or first RR from iq->response, 1250 * or servfail5secs if !iq->response */ 1251 time_t ttl = NORR_TTL; 1252 struct ub_packed_rrset_key* neg; 1253 struct packed_rrset_data* newd; 1254 if(rep) { 1255 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); 1256 if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0]; 1257 if(rrset) ttl = ub_packed_rrset_ttl(rrset); 1258 } 1259 /* create empty rrset to store */ 1260 neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch, 1261 sizeof(struct ub_packed_rrset_key)); 1262 if(!neg) { 1263 log_err("out of memory in store_parentside_neg"); 1264 return; 1265 } 1266 memset(&neg->entry, 0, sizeof(neg->entry)); 1267 neg->entry.key = neg; 1268 neg->rk.type = htons(qinfo->qtype); 1269 neg->rk.rrset_class = htons(qinfo->qclass); 1270 neg->rk.flags = 0; 1271 neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname, 1272 qinfo->qname_len); 1273 if(!neg->rk.dname) { 1274 log_err("out of memory in store_parentside_neg"); 1275 return; 1276 } 1277 neg->rk.dname_len = qinfo->qname_len; 1278 neg->entry.hash = rrset_key_hash(&neg->rk); 1279 newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch, 1280 sizeof(struct packed_rrset_data) + sizeof(size_t) + 1281 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)); 1282 if(!newd) { 1283 log_err("out of memory in store_parentside_neg"); 1284 return; 1285 } 1286 neg->entry.data = newd; 1287 newd->ttl = ttl; 1288 /* entry must have one RR, otherwise not valid in cache. 1289 * put in one RR with empty rdata: those are ignored as nameserver */ 1290 newd->count = 1; 1291 newd->rrsig_count = 0; 1292 newd->trust = rrset_trust_ans_noAA; 1293 newd->rr_len = (size_t*)((uint8_t*)newd + 1294 sizeof(struct packed_rrset_data)); 1295 newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t); 1296 packed_rrset_ptr_fixup(newd); 1297 newd->rr_ttl[0] = newd->ttl; 1298 sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */); 1299 /* store it */ 1300 log_rrset_key(VERB_ALGO, "store parent-side negative", neg); 1301 iter_store_parentside_rrset(env, neg); 1302 } 1303 1304 int 1305 iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp, 1306 struct regional* region, struct query_info* qinfo) 1307 { 1308 struct ub_packed_rrset_key* akey; 1309 akey = rrset_cache_lookup(env->rrset_cache, dp->name, 1310 dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass, 1311 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 1312 if(akey) { 1313 log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey); 1314 dp->has_parent_side_NS = 1; 1315 /* and mark the new names as lame */ 1316 if(!delegpt_rrset_add_ns(dp, region, akey, 1)) { 1317 lock_rw_unlock(&akey->entry.lock); 1318 return 0; 1319 } 1320 lock_rw_unlock(&akey->entry.lock); 1321 } 1322 return 1; 1323 } 1324 1325 int iter_lookup_parent_glue_from_cache(struct module_env* env, 1326 struct delegpt* dp, struct regional* region, struct query_info* qinfo) 1327 { 1328 struct ub_packed_rrset_key* akey; 1329 struct delegpt_ns* ns; 1330 size_t num = delegpt_count_targets(dp); 1331 for(ns = dp->nslist; ns; ns = ns->next) { 1332 if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE) 1333 continue; 1334 ns->cache_lookup_count++; 1335 /* get cached parentside A */ 1336 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 1337 ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass, 1338 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 1339 if(akey) { 1340 log_rrset_key(VERB_ALGO, "found parent-side", akey); 1341 ns->done_pside4 = 1; 1342 /* a negative-cache-element has no addresses it adds */ 1343 if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL)) 1344 log_err("malloc failure in lookup_parent_glue"); 1345 lock_rw_unlock(&akey->entry.lock); 1346 } 1347 /* get cached parentside AAAA */ 1348 akey = rrset_cache_lookup(env->rrset_cache, ns->name, 1349 ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass, 1350 PACKED_RRSET_PARENT_SIDE, *env->now, 0); 1351 if(akey) { 1352 log_rrset_key(VERB_ALGO, "found parent-side", akey); 1353 ns->done_pside6 = 1; 1354 /* a negative-cache-element has no addresses it adds */ 1355 if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL)) 1356 log_err("malloc failure in lookup_parent_glue"); 1357 lock_rw_unlock(&akey->entry.lock); 1358 } 1359 } 1360 /* see if new (but lame) addresses have become available */ 1361 return delegpt_count_targets(dp) != num; 1362 } 1363 1364 int 1365 iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd, 1366 uint16_t* c) 1367 { 1368 uint16_t c1 = *c, c2 = *c; 1369 int r1, r2; 1370 int nolock = 1; 1371 1372 /* prelock both forwards and hints for atomic read. */ 1373 lock_rw_rdlock(&fwd->lock); 1374 lock_rw_rdlock(&hints->lock); 1375 r1 = hints_next_root(hints, &c1, nolock); 1376 r2 = forwards_next_root(fwd, &c2, nolock); 1377 lock_rw_unlock(&fwd->lock); 1378 lock_rw_unlock(&hints->lock); 1379 1380 if(!r1 && !r2) /* got none, end of list */ 1381 return 0; 1382 else if(!r1) /* got one, return that */ 1383 *c = c2; 1384 else if(!r2) 1385 *c = c1; 1386 else if(c1 < c2) /* got both take smallest */ 1387 *c = c1; 1388 else *c = c2; 1389 return 1; 1390 } 1391 1392 void 1393 iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z) 1394 { 1395 /* Only the DS record for the delegation itself is expected. 1396 * We allow DS for everything between the bailiwick and the 1397 * zonecut, thus DS records must be at or above the zonecut. 1398 * And the DS records must be below the server authority zone. 1399 * The answer section is already scrubbed. */ 1400 size_t i = msg->rep->an_numrrsets; 1401 while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) { 1402 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1403 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS && 1404 (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname) 1405 || query_dname_compare(z, s->rk.dname) == 0)) { 1406 log_nametypeclass(VERB_ALGO, "removing irrelevant DS", 1407 s->rk.dname, ntohs(s->rk.type), 1408 ntohs(s->rk.rrset_class)); 1409 memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1, 1410 sizeof(struct ub_packed_rrset_key*) * 1411 (msg->rep->rrset_count-i-1)); 1412 msg->rep->ns_numrrsets--; 1413 msg->rep->rrset_count--; 1414 /* stay at same i, but new record */ 1415 continue; 1416 } 1417 i++; 1418 } 1419 } 1420 1421 void 1422 iter_scrub_nxdomain(struct dns_msg* msg) 1423 { 1424 if(msg->rep->an_numrrsets == 0) 1425 return; 1426 1427 memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets, 1428 sizeof(struct ub_packed_rrset_key*) * 1429 (msg->rep->rrset_count-msg->rep->an_numrrsets)); 1430 msg->rep->rrset_count -= msg->rep->an_numrrsets; 1431 msg->rep->an_numrrsets = 0; 1432 } 1433 1434 void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry) 1435 { 1436 struct delegpt_addr* a; 1437 for(a=dp->target_list; a; a = a->next_target) { 1438 if(a->attempts >= outbound_msg_retry) { 1439 /* add back to result list */ 1440 delegpt_add_to_result_list(dp, a); 1441 } 1442 if(a->attempts > d) 1443 a->attempts -= d; 1444 else a->attempts = 0; 1445 } 1446 } 1447 1448 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old, 1449 int outbound_msg_retry) 1450 { 1451 struct delegpt_addr* a, *o, *prev; 1452 for(a=dp->target_list; a; a = a->next_target) { 1453 o = delegpt_find_addr(old, &a->addr, a->addrlen); 1454 if(o) { 1455 log_addr(VERB_ALGO, "copy attempt count previous dp", 1456 &a->addr, a->addrlen); 1457 a->attempts = o->attempts; 1458 } 1459 } 1460 prev = NULL; 1461 a = dp->usable_list; 1462 while(a) { 1463 if(a->attempts >= outbound_msg_retry) { 1464 log_addr(VERB_ALGO, "remove from usable list dp", 1465 &a->addr, a->addrlen); 1466 /* remove from result list */ 1467 if(prev) 1468 prev->next_usable = a->next_usable; 1469 else dp->usable_list = a->next_usable; 1470 /* prev stays the same */ 1471 a = a->next_usable; 1472 continue; 1473 } 1474 prev = a; 1475 a = a->next_usable; 1476 } 1477 } 1478 1479 int 1480 iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp) 1481 { 1482 /* if for query example.com, there is example.com SOA or a subdomain 1483 * of example.com, then we are too low and need to fetch NS. */ 1484 size_t i; 1485 /* if we have a DNAME or CNAME we are probably wrong */ 1486 /* if we have a qtype DS in the answer section, its fine */ 1487 for(i=0; i < msg->rep->an_numrrsets; i++) { 1488 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1489 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME || 1490 ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) { 1491 /* not the right answer, maybe too low, check the 1492 * RRSIG signer name (if there is any) for a hint 1493 * that it is from the dp zone anyway */ 1494 uint8_t* sname; 1495 size_t slen; 1496 val_find_rrset_signer(s, &sname, &slen); 1497 if(sname && query_dname_compare(dp->name, sname)==0) 1498 return 0; /* it is fine, from the right dp */ 1499 return 1; 1500 } 1501 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS) 1502 return 0; /* fine, we have a DS record */ 1503 } 1504 for(i=msg->rep->an_numrrsets; 1505 i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { 1506 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1507 if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) { 1508 if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname)) 1509 return 1; /* point is too low */ 1510 if(query_dname_compare(s->rk.dname, dp->name)==0) 1511 return 0; /* right dp */ 1512 } 1513 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC || 1514 ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1515 uint8_t* sname; 1516 size_t slen; 1517 val_find_rrset_signer(s, &sname, &slen); 1518 if(sname && query_dname_compare(dp->name, sname)==0) 1519 return 0; /* it is fine, from the right dp */ 1520 return 1; 1521 } 1522 } 1523 /* we do not know */ 1524 return 1; 1525 } 1526 1527 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp) 1528 { 1529 /* no delegation point, do not see how we can go down, 1530 * robust check, it should really exist */ 1531 if(!dp) return 0; 1532 1533 /* see if dp equals the qname, then we cannot go down further */ 1534 if(query_dname_compare(qinfo->qname, dp->name) == 0) 1535 return 0; 1536 /* if dp is one label above the name we also cannot go down further */ 1537 if(dname_count_labels(qinfo->qname) == dp->namelabs+1) 1538 return 0; 1539 return 1; 1540 } 1541 1542 int 1543 iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf, 1544 uint8_t** retdpname, size_t* retdpnamelen, uint8_t* dpname_storage, 1545 size_t dpname_storage_len) 1546 { 1547 struct iter_hints_stub *stub; 1548 struct delegpt *dp; 1549 int nolock = 1; 1550 1551 log_assert((retdpname && retdpnamelen 1552 && dpname_storage && dpname_storage_len > 0) || 1553 (retdpname == NULL && retdpnamelen == NULL 1554 && dpname_storage == NULL && dpname_storage_len == 0)); 1555 1556 /* Check for stub. */ 1557 /* Lock both forwards and hints for atomic read. */ 1558 lock_rw_rdlock(&qstate->env->fwds->lock); 1559 lock_rw_rdlock(&qstate->env->hints->lock); 1560 stub = hints_lookup_stub(qstate->env->hints, qinf->qname, 1561 qinf->qclass, NULL, nolock); 1562 dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass, 1563 nolock); 1564 1565 /* see if forward or stub is more pertinent */ 1566 if(stub && stub->dp && dp) { 1567 if(dname_strict_subdomain(dp->name, dp->namelabs, 1568 stub->dp->name, stub->dp->namelabs)) { 1569 stub = NULL; /* ignore stub, forward is lower */ 1570 } else { 1571 dp = NULL; /* ignore forward, stub is lower */ 1572 } 1573 } 1574 1575 /* check stub */ 1576 if (stub != NULL && stub->dp != NULL) { 1577 enum verbosity_value level = VERB_ALGO; 1578 int stub_no_cache = stub->dp->no_cache; 1579 lock_rw_unlock(&qstate->env->fwds->lock); 1580 if(verbosity >= level && stub_no_cache) { 1581 char qname[LDNS_MAX_DOMAINLEN]; 1582 char dpname[LDNS_MAX_DOMAINLEN]; 1583 dname_str(qinf->qname, qname); 1584 dname_str(stub->dp->name, dpname); 1585 verbose(level, "stub for %s %s has no_cache", qname, dpname); 1586 } 1587 if(retdpname) { 1588 if(stub->dp->namelen > dpname_storage_len) { 1589 verbose(VERB_ALGO, "no cache stub dpname too long"); 1590 lock_rw_unlock(&qstate->env->hints->lock); 1591 *retdpname = NULL; 1592 *retdpnamelen = 0; 1593 return stub_no_cache; 1594 } 1595 memmove(dpname_storage, stub->dp->name, 1596 stub->dp->namelen); 1597 *retdpname = dpname_storage; 1598 *retdpnamelen = stub->dp->namelen; 1599 } 1600 lock_rw_unlock(&qstate->env->hints->lock); 1601 return stub_no_cache; 1602 } 1603 1604 /* Check for forward. */ 1605 if (dp) { 1606 enum verbosity_value level = VERB_ALGO; 1607 int dp_no_cache = dp->no_cache; 1608 lock_rw_unlock(&qstate->env->hints->lock); 1609 if(verbosity >= level && dp_no_cache) { 1610 char qname[LDNS_MAX_DOMAINLEN]; 1611 char dpname[LDNS_MAX_DOMAINLEN]; 1612 dname_str(qinf->qname, qname); 1613 dname_str(dp->name, dpname); 1614 verbose(level, "forward for %s %s has no_cache", qname, dpname); 1615 } 1616 if(retdpname) { 1617 if(dp->namelen > dpname_storage_len) { 1618 verbose(VERB_ALGO, "no cache dpname too long"); 1619 lock_rw_unlock(&qstate->env->fwds->lock); 1620 *retdpname = NULL; 1621 *retdpnamelen = 0; 1622 return dp_no_cache; 1623 } 1624 memmove(dpname_storage, dp->name, dp->namelen); 1625 *retdpname = dpname_storage; 1626 *retdpnamelen = dp->namelen; 1627 } 1628 lock_rw_unlock(&qstate->env->fwds->lock); 1629 return dp_no_cache; 1630 } 1631 lock_rw_unlock(&qstate->env->fwds->lock); 1632 lock_rw_unlock(&qstate->env->hints->lock); 1633 if(retdpname) { 1634 *retdpname = NULL; 1635 *retdpnamelen = 0; 1636 } 1637 return 0; 1638 } 1639 1640 void iterator_set_ip46_support(struct module_stack* mods, 1641 struct module_env* env, struct outside_network* outnet) 1642 { 1643 int m = modstack_find(mods, "iterator"); 1644 struct iter_env* ie = NULL; 1645 if(m == -1) 1646 return; 1647 ie = (struct iter_env*)env->modinfo[m]; 1648 if(outnet->pending == NULL) 1649 return; /* we are in testbound, no rbtree for UDP */ 1650 if(outnet->num_ip4 == 0) 1651 ie->supports_ipv4 = 0; 1652 if(outnet->num_ip6 == 0) 1653 ie->supports_ipv6 = 0; 1654 } 1655 1656 void 1657 limit_nsec_ttl(struct dns_msg* msg) 1658 { 1659 /* Limit NSEC and NSEC3 TTL in response, RFC9077 */ 1660 size_t i; 1661 int found = 0; 1662 time_t soa_ttl = 0; 1663 /* Limit the NSEC and NSEC3 TTL values to the SOA TTL and SOA minimum 1664 * TTL. That has already been applied to the SOA record ttl. */ 1665 for(i=0; i<msg->rep->rrset_count; i++) { 1666 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1667 if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) { 1668 struct packed_rrset_data* soadata = (struct packed_rrset_data*)s->entry.data; 1669 found = 1; 1670 soa_ttl = soadata->ttl; 1671 break; 1672 } 1673 } 1674 if(!found) 1675 return; 1676 for(i=0; i<msg->rep->rrset_count; i++) { 1677 struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; 1678 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC || 1679 ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1680 struct packed_rrset_data* data = (struct packed_rrset_data*)s->entry.data; 1681 /* Limit the negative TTL. */ 1682 if(data->ttl > soa_ttl) { 1683 if(verbosity >= VERB_ALGO) { 1684 char buf[256]; 1685 snprintf(buf, sizeof(buf), 1686 "limiting TTL %d of %s record to the SOA TTL of %d for", 1687 (int)data->ttl, ((ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC)?"NSEC":"NSEC3"), (int)soa_ttl); 1688 log_nametypeclass(VERB_ALGO, buf, 1689 s->rk.dname, ntohs(s->rk.type), 1690 ntohs(s->rk.rrset_class)); 1691 } 1692 data->ttl = soa_ttl; 1693 } 1694 } 1695 } 1696 } 1697 1698 void 1699 iter_make_minimal(struct reply_info* rep) 1700 { 1701 size_t rem = rep->ns_numrrsets + rep->ar_numrrsets; 1702 rep->ns_numrrsets = 0; 1703 rep->ar_numrrsets = 0; 1704 rep->rrset_count -= rem; 1705 } 1706