1 /* 2 * iterator/iterator.h - iterative resolver DNS query response module 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 a module that performs recursive iterative DNS query 40 * processing. 41 */ 42 43 #ifndef ITERATOR_ITERATOR_H 44 #define ITERATOR_ITERATOR_H 45 #include "services/outbound_list.h" 46 #include "util/data/msgreply.h" 47 #include "util/module.h" 48 struct delegpt; 49 struct iter_donotq; 50 struct iter_prep_list; 51 struct iter_priv; 52 struct rbtree_type; 53 54 /** max number of targets spawned for a query and its subqueries */ 55 #define MAX_TARGET_COUNT 64 56 /** max number of upstream queries for a query and its subqueries, it is 57 * never reset. */ 58 extern int MAX_GLOBAL_QUOTA; 59 /** max number of target lookups per qstate, per delegation point */ 60 #define MAX_DP_TARGET_COUNT 16 61 /** max number of nxdomains allowed for target lookups for a query and 62 * its subqueries */ 63 #define MAX_TARGET_NX 5 64 /** max number of nxdomains allowed for target lookups for a query and 65 * its subqueries when fallback has kicked in */ 66 #define MAX_TARGET_NX_FALLBACK (MAX_TARGET_NX*2) 67 /** max number of referrals. Makes sure resolver does not run away */ 68 #define MAX_REFERRAL_COUNT 130 69 /** max number of queries for which to perform dnsseclameness detection, 70 * (rrsigs missing detection) after that, just pick up that response */ 71 #define DNSSEC_LAME_DETECT_COUNT 4 72 /** 73 * max number of QNAME minimisation iterations. Limits number of queries for 74 * QNAMEs with a lot of labels. 75 */ 76 #define MAX_MINIMISE_COUNT 10 77 /* max number of time-outs for minimised query. Prevents resolving failures 78 * when the QNAME minimisation QTYPE is blocked. */ 79 #define MAX_MINIMISE_TIMEOUT_COUNT 3 80 /** 81 * number of labels from QNAME that are always send individually when using 82 * QNAME minimisation, even when the number of labels of the QNAME is bigger 83 * than MAX_MINIMISE_COUNT */ 84 #define MINIMISE_ONE_LAB 4 85 #define MINIMISE_MULTIPLE_LABS (MAX_MINIMISE_COUNT - MINIMISE_ONE_LAB) 86 /** at what query-sent-count to stop target fetch policy */ 87 #define TARGET_FETCH_STOP 3 88 /** how nice is a server without further information, in msec 89 * Equals rtt initial timeout value. 90 */ 91 extern int UNKNOWN_SERVER_NICENESS; 92 /** maximum timeout before a host is deemed unsuitable, in msec. 93 * After host_ttl this will be timed out and the host will be tried again. 94 * Equals RTT_MAX_TIMEOUT, and thus when RTT_MAX_TIMEOUT is overwritten by 95 * config infra_cache_max_rtt, it will be overwritten as well. */ 96 extern int USEFUL_SERVER_TOP_TIMEOUT; 97 /** penalty to validation failed blacklisted IPs 98 * Equals USEFUL_SERVER_TOP_TIMEOUT*4, and thus when RTT_MAX_TIMEOUT is 99 * overwritten by config infra_cache_max_rtt, it will be overwritten as well. */ 100 extern int BLACKLIST_PENALTY; 101 /** RTT band, within this amount from the best, servers are chosen randomly. 102 * Chosen so that the UNKNOWN_SERVER_NICENESS falls within the band of a 103 * fast server, this causes server exploration as a side benefit. msec. */ 104 #define RTT_BAND 400 105 /** Number of retries for empty nodata packets before it is accepted. */ 106 #define EMPTY_NODATA_RETRY_COUNT 2 107 /** max label-strip iterations in DSNS_FIND_STATE (RFC 4035 4.2 parent-NS 108 * search) before giving up; bounds upstream NS sends per client DS. 109 * Means the max number of labels in grandchild to the grandparent zone that 110 * are co-hosted. */ 111 #define MAX_DSNS_FIND_COUNT 20 112 113 /** 114 * Iterator global state for nat64. 115 */ 116 struct iter_nat64 { 117 /** A flag to locally apply NAT64 to make IPv4 addrs into IPv6 */ 118 int use_nat64; 119 120 /** NAT64 prefix address, cf. dns64_env->prefix_addr */ 121 struct sockaddr_storage nat64_prefix_addr; 122 123 /** sizeof(sockaddr_in6) */ 124 socklen_t nat64_prefix_addrlen; 125 126 /** CIDR mask length of NAT64 prefix */ 127 int nat64_prefix_net; 128 }; 129 130 /** 131 * Global state for the iterator. 132 */ 133 struct iter_env { 134 /** A flag to indicate whether or not we have an IPv6 route */ 135 int supports_ipv6; 136 137 /** A flag to indicate whether or not we have an IPv4 route */ 138 int supports_ipv4; 139 140 /** State for nat64 */ 141 struct iter_nat64 nat64; 142 143 /** A set of inetaddrs that should never be queried. */ 144 struct iter_donotq* donotq; 145 146 /** private address space and private domains */ 147 struct iter_priv* priv; 148 149 /** whitelist for capsforid names */ 150 struct rbtree_type* caps_white; 151 152 /** The maximum dependency depth that this resolver will pursue. */ 153 int max_dependency_depth; 154 155 /** 156 * The target fetch policy for each dependency level. This is 157 * described as a simple number (per dependency level): 158 * negative numbers (usually just -1) mean fetch-all, 159 * 0 means only fetch on demand, and 160 * positive numbers mean to fetch at most that many targets. 161 * array of max_dependency_depth+1 size. 162 */ 163 int* target_fetch_policy; 164 165 /** lock on ratelimit counter */ 166 lock_basic_type queries_ratelimit_lock; 167 /** number of queries that have been ratelimited */ 168 size_t num_queries_ratelimited; 169 170 /** number of retries on outgoing queries */ 171 int outbound_msg_retry; 172 173 /** number of queries_sent */ 174 int max_sent_count; 175 176 /** max number of query restarts to limit length of CNAME chain */ 177 int max_query_restarts; 178 }; 179 180 /** 181 * QNAME minimisation state 182 */ 183 enum minimisation_state { 184 /** 185 * (Re)start minimisation. Outgoing QNAME should be set to dp->name. 186 * State entered on new query or after following referral or CNAME. 187 */ 188 INIT_MINIMISE_STATE = 0, 189 /** 190 * QNAME minimisation ongoing. Increase QNAME on every iteration. 191 */ 192 MINIMISE_STATE, 193 /** 194 * Don't increment QNAME this iteration 195 */ 196 SKIP_MINIMISE_STATE, 197 /** 198 * Send out full QNAME + original QTYPE 199 */ 200 DONOT_MINIMISE_STATE, 201 }; 202 203 /** 204 * State of the iterator for a query. 205 */ 206 enum iter_state { 207 /** 208 * Externally generated queries start at this state. Query restarts are 209 * reset to this state. 210 */ 211 INIT_REQUEST_STATE = 0, 212 213 /** 214 * Root priming events reactivate here, most other events pass 215 * through this naturally as the 2nd part of the INIT_REQUEST_STATE. 216 */ 217 INIT_REQUEST_2_STATE, 218 219 /** 220 * Stub priming events reactivate here, most other events pass 221 * through this naturally as the 3rd part of the INIT_REQUEST_STATE. 222 */ 223 INIT_REQUEST_3_STATE, 224 225 /** 226 * Each time a delegation point changes for a given query or a 227 * query times out and/or wakes up, this state is (re)visited. 228 * This state is responsible for iterating through a list of 229 * nameserver targets. 230 */ 231 QUERYTARGETS_STATE, 232 233 /** 234 * Responses to queries start at this state. This state handles 235 * the decision tree associated with handling responses. 236 */ 237 QUERY_RESP_STATE, 238 239 /** Responses to priming queries finish at this state. */ 240 PRIME_RESP_STATE, 241 242 /** Collecting query class information, for qclass=ANY, when 243 * it spawns off queries for every class, it returns here. */ 244 COLLECT_CLASS_STATE, 245 246 /** Find NS record to resolve DS record from, walking to the right 247 * NS spot until we find it */ 248 DSNS_FIND_STATE, 249 250 /** Responses that are to be returned upstream end at this state. 251 * As well as responses to target queries. */ 252 FINISHED_STATE 253 }; 254 255 /** 256 * Shared counters for queries. 257 */ 258 enum target_count_variables { 259 /** Reference count for the shared iter_qstate->target_count. */ 260 TARGET_COUNT_REF = 0, 261 /** Number of target queries spawned for the query and subqueries. */ 262 TARGET_COUNT_QUERIES, 263 /** Number of nxdomain responses encountered. */ 264 TARGET_COUNT_NX, 265 /** Global quota on number of queries to upstream servers per 266 * client request, that is never reset. */ 267 TARGET_COUNT_GLOBAL_QUOTA, 268 269 /** This should stay last here, it is used for the allocation */ 270 TARGET_COUNT_MAX, 271 }; 272 273 /** 274 * Per query state for the iterator module. 275 */ 276 struct iter_qstate { 277 /** 278 * State of the iterator module. 279 * This is the state that event is in or should sent to -- all 280 * requests should start with the INIT_REQUEST_STATE. All 281 * responses should start with QUERY_RESP_STATE. Subsequent 282 * processing of the event will change this state. 283 */ 284 enum iter_state state; 285 286 /** 287 * Final state for the iterator module. 288 * This is the state that responses should be routed to once the 289 * response is final. For externally initiated queries, this 290 * will be FINISHED_STATE, locally initiated queries will have 291 * different final states. 292 */ 293 enum iter_state final_state; 294 295 /** 296 * The depth of this query, this means the depth of recursion. 297 * This address is needed for another query, which is an address 298 * needed for another query, etc. Original client query has depth 0. 299 */ 300 int depth; 301 302 /** 303 * The response 304 */ 305 struct dns_msg* response; 306 307 /** 308 * This is a list of RRsets that must be prepended to the 309 * ANSWER section of a response before being sent upstream. 310 */ 311 struct iter_prep_list* an_prepend_list; 312 /** Last element of the prepend list */ 313 struct iter_prep_list* an_prepend_last; 314 315 /** 316 * This is the list of RRsets that must be prepended to the 317 * AUTHORITY section of the response before being sent upstream. 318 */ 319 struct iter_prep_list* ns_prepend_list; 320 /** Last element of the authority prepend list */ 321 struct iter_prep_list* ns_prepend_last; 322 323 /** query name used for chasing the results. Initially the same as 324 * the state qinfo, but after CNAMEs this will be different. 325 * The query info used to elicit the results needed. */ 326 struct query_info qchase; 327 /** query flags to use when chasing the answer (i.e. RD flag) */ 328 uint16_t chase_flags; 329 /** true if we set RD bit because of last resort recursion lame query*/ 330 int chase_to_rd; 331 332 /** 333 * This is the current delegation point for an in-progress query. This 334 * object retains state as to which delegation targets need to be 335 * (sub)queried for vs which ones have already been visited. 336 */ 337 struct delegpt* dp; 338 339 /** state for 0x20 fallback when capsfail happens, 0 not a fallback */ 340 int caps_fallback; 341 /** state for capsfail: current server number to try */ 342 size_t caps_server; 343 /** state for capsfail: stored query for comparisons. Can be NULL if 344 * no response had been seen prior to starting the fallback. */ 345 struct reply_info* caps_reply; 346 struct dns_msg* caps_response; 347 348 /** Current delegation message - returned for non-RD queries */ 349 struct dns_msg* deleg_msg; 350 351 /** number of outstanding target sub queries */ 352 int num_target_queries; 353 354 /** outstanding direct queries */ 355 int num_current_queries; 356 357 /** the number of times this query has been restarted. */ 358 int query_restart_count; 359 360 /** the number of times this query has followed a referral. */ 361 int referral_count; 362 363 /** number of queries fired off */ 364 int sent_count; 365 366 /** malloced-array shared with this query and its subqueries. It keeps 367 * track of the defined enum target_count_variables counters. */ 368 int* target_count; 369 370 /** number of target lookups per delegation point. Reset to 0 after 371 * receiving referral answer. Not shared with subqueries. */ 372 int dp_target_count; 373 374 /** Delegation point that triggered the NXNS fallback; shared with 375 * this query and its subqueries, count-referenced by the reference 376 * counter in target_count. 377 * This also marks the fallback activation. */ 378 uint8_t** nxns_dp; 379 380 /** if true, already tested for ratelimiting and passed the test */ 381 int ratelimit_ok; 382 383 /** If the last query, that may be a referral, incremented the 384 * ratelimit counter. */ 385 int ratelimit_incremented; 386 387 /** 388 * The query must store NS records from referrals as parentside RRs 389 * Enabled once it hits resolution problems, to throttle retries. 390 * If enabled it is the pointer to the old delegation point with 391 * the old retry counts for bad-nameserver-addresses. 392 */ 393 struct delegpt* store_parent_NS; 394 395 /** 396 * The query is for parent-side glue(A or AAAA) for a nameserver. 397 * If the item is seen as glue in a referral, and pside_glue is NULL, 398 * then it is stored in pside_glue for later. 399 * If it was never seen, at the end, then a negative caching element 400 * must be created. 401 * The (data or negative) RR cache element then throttles retries. 402 */ 403 int query_for_pside_glue; 404 /** the parent-side-glue element (NULL if none, its first match) */ 405 struct ub_packed_rrset_key* pside_glue; 406 407 /** If nonNULL we are walking upwards from DS query to find NS */ 408 uint8_t* dsns_point; 409 /** length of the dname in dsns_point */ 410 size_t dsns_point_len; 411 /** number of label-strip iterations performed in DSNS_FIND_STATE */ 412 int dsns_count; 413 414 /** 415 * expected dnssec information for this iteration step. 416 * If dnssec rrsigs are expected and not given, the server is marked 417 * lame (dnssec-lame). 418 */ 419 int dnssec_expected; 420 421 /** 422 * We are expecting dnssec information, but we also know the server 423 * is DNSSEC lame. The response need not be marked dnssec-lame again. 424 */ 425 int dnssec_lame_query; 426 427 /** 428 * This is flag that, if true, means that this event is 429 * waiting for a stub priming query. 430 */ 431 int wait_priming_stub; 432 433 /** 434 * This is a flag that, if true, means that this query is 435 * for (re)fetching glue from a zone. Since the address should 436 * have been glue, query again to the servers that should have 437 * been returning it as glue. 438 * The delegation point must be set to the one that should *not* 439 * be used when creating the state. A higher one will be attempted. 440 */ 441 int refetch_glue; 442 443 /** 444 * This flag detects that a completely empty nodata was received, 445 * already so that it is accepted later. */ 446 int empty_nodata_found; 447 448 /** Store if the answer was empty, but lame, before it became empty.*/ 449 int msg_lame_empty; 450 451 /** Store if the answer was a referral, to self, before scrub. So the 452 * it is not some sort of answer. */ 453 int msg_lame_referral; 454 455 /** list of pending queries to authoritative servers. */ 456 struct outbound_list outlist; 457 458 /** QNAME minimisation state, RFC9156 */ 459 enum minimisation_state minimisation_state; 460 461 /** State for capsfail: QNAME minimisation state for comparisons. */ 462 enum minimisation_state caps_minimisation_state; 463 464 /** 465 * The query info that is sent upstream. Will be a subset of qchase 466 * when qname minimisation is enabled. 467 */ 468 struct query_info qinfo_out; 469 470 /** 471 * Count number of QNAME minimisation iterations. Used to limit number of 472 * outgoing queries when QNAME minimisation is enabled. 473 */ 474 int minimise_count; 475 476 /** 477 * Count number of time-outs. Used to prevent resolving failures when 478 * the QNAME minimisation QTYPE is blocked. Used to determine if 479 * capsforid fallback should be started.*/ 480 int timeout_count; 481 482 /** True if the current response is from auth_zone */ 483 int auth_zone_response; 484 /** True if the auth_zones should not be consulted for the query */ 485 int auth_zone_avoid; 486 /** true if there have been scrubbing failures of reply packets */ 487 int scrub_failures; 488 /** true if there have been parse failures of reply packets */ 489 int parse_failures; 490 /** a failure printout address for last received answer */ 491 union { 492 struct in_addr in; 493 #ifdef AF_INET6 494 struct in6_addr in6; 495 #endif 496 } fail_addr; 497 /** which fail_addr, 0 is nothing, 4 or 6 */ 498 int fail_addr_type; 499 }; 500 501 /** 502 * List of prepend items 503 */ 504 struct iter_prep_list { 505 /** next in list */ 506 struct iter_prep_list* next; 507 /** rrset */ 508 struct ub_packed_rrset_key* rrset; 509 }; 510 511 /** 512 * Get the iterator function block. 513 * @return: function block with function pointers to iterator methods. 514 */ 515 struct module_func_block* iter_get_funcblock(void); 516 517 /** 518 * Get iterator state as a string 519 * @param state: to convert 520 * @return constant string that is printable. 521 */ 522 const char* iter_state_to_string(enum iter_state state); 523 524 /** 525 * See if iterator state is a response state 526 * @param s: to inspect 527 * @return true if response state. 528 */ 529 int iter_state_is_responsestate(enum iter_state s); 530 531 /** iterator init */ 532 int iter_init(struct module_env* env, int id); 533 534 /** iterator deinit */ 535 void iter_deinit(struct module_env* env, int id); 536 537 /** iterator operate on a query */ 538 void iter_operate(struct module_qstate* qstate, enum module_ev event, int id, 539 struct outbound_entry* outbound); 540 541 /** 542 * Return priming query results to interested super querystates. 543 * 544 * Sets the delegation point and delegation message (not nonRD queries). 545 * This is a callback from walk_supers. 546 * 547 * @param qstate: query state that finished. 548 * @param id: module id. 549 * @param super: the qstate to inform. 550 */ 551 void iter_inform_super(struct module_qstate* qstate, int id, 552 struct module_qstate* super); 553 554 /** iterator cleanup query state */ 555 void iter_clear(struct module_qstate* qstate, int id); 556 557 /** iterator alloc size routine */ 558 size_t iter_get_mem(struct module_env* env, int id); 559 560 #endif /* ITERATOR_ITERATOR_H */ 561