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