1 /* 2 * iterator/iter_utils.h - 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 43 #ifndef ITERATOR_ITER_UTILS_H 44 #define ITERATOR_ITER_UTILS_H 45 #include "iterator/iter_resptype.h" 46 struct sldns_buffer; 47 struct iter_env; 48 struct iter_hints; 49 struct iter_forwards; 50 struct config_file; 51 struct module_env; 52 struct delegpt_addr; 53 struct delegpt; 54 struct regional; 55 struct msg_parse; 56 struct ub_randstate; 57 struct query_info; 58 struct reply_info; 59 struct module_qstate; 60 struct sock_list; 61 struct ub_packed_rrset_key; 62 struct module_stack; 63 struct outside_network; 64 struct iter_nat64; 65 66 /* max number of lookups in the cache for target nameserver names. 67 * This stops, for large delegations, N*N lookups in the cache. */ 68 #define ITERATOR_NAME_CACHELOOKUP_MAX 3 69 /* max number of lookups in the cache for parentside glue for nameserver names 70 * This stops, for larger delegations, N*N lookups in the cache. 71 * It is a little larger than the nonpside max, so it allows a couple extra 72 * lookups of parent side glue. */ 73 #define ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE 5 74 75 /** 76 * Process config options and set iterator module state. 77 * Sets default values if no config is found. 78 * @param iter_env: iterator module state. 79 * @param cfg: config options. 80 * @return 0 on error. 81 */ 82 int iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg); 83 84 /** 85 * Select a valid, nice target to send query to. 86 * Sorting and removing unsuitable targets is combined. 87 * 88 * @param iter_env: iterator module global state, with ip6 enabled and 89 * do-not-query-addresses. 90 * @param env: environment with infra cache (lameness, rtt info). 91 * @param dp: delegation point with result list. 92 * @param name: zone name (for lameness check). 93 * @param namelen: length of name. 94 * @param qtype: query type that we want to send. 95 * @param dnssec_lame: set to 1, if a known dnssec-lame server is selected 96 * these are not preferred, but are used as a last resort. 97 * @param chase_to_rd: set to 1 if a known recursion lame server is selected 98 * these are not preferred, but are used as a last resort. 99 * @param open_target: number of currently outstanding target queries. 100 * If we wait for these, perhaps more server addresses become available. 101 * @param blacklist: the IP blacklist to use. 102 * @param prefetch: if not 0, prefetch is in use for this query. 103 * This means the query can have different timing, because prefetch is 104 * not waited upon by the downstream client, and thus a good time to 105 * perform exploration of other targets. 106 * @return best target or NULL if no target. 107 * if not null, that target is removed from the result list in the dp. 108 */ 109 struct delegpt_addr* iter_server_selection(struct iter_env* iter_env, 110 struct module_env* env, struct delegpt* dp, uint8_t* name, 111 size_t namelen, uint16_t qtype, int* dnssec_lame, 112 int* chase_to_rd, int open_target, struct sock_list* blacklist, 113 time_t prefetch); 114 115 /** 116 * Allocate dns_msg from parsed msg, in regional. 117 * @param pkt: packet. 118 * @param msg: parsed message (cleaned and ready for regional allocation). 119 * @param regional: regional to use for allocation. 120 * @return newly allocated dns_msg, or NULL on memory error. 121 */ 122 struct dns_msg* dns_alloc_msg(struct sldns_buffer* pkt, struct msg_parse* msg, 123 struct regional* regional); 124 125 /** 126 * Copy a dns_msg to this regional. 127 * @param from: dns message, also in regional. 128 * @param regional: regional to use for allocation. 129 * @return newly allocated dns_msg, or NULL on memory error. 130 */ 131 struct dns_msg* dns_copy_msg(struct dns_msg* from, struct regional* regional); 132 133 /** 134 * Allocate a dns_msg with malloc/alloc structure and store in dns cache. 135 * @param env: environment, with alloc structure and dns cache. 136 * @param qinf: query info, the query for which answer is stored. 137 * @param rep: reply in dns_msg from dns_alloc_msg for example. 138 * @param is_referral: If true, then the given message to be stored is a 139 * referral. The cache implementation may use this as a hint. 140 * @param leeway: prefetch TTL leeway to expire old rrsets quicker. 141 * @param pside: true if dp is parentside, thus message is 'fresh' and NS 142 * can be prefetch-updates. 143 * @param region: to copy modified (cache is better) rrs back to. 144 * @param flags: with BIT_CD for dns64 AAAA translated queries. 145 * @param qstarttime: time of query start. 146 * @param is_valrec: if the query is validation recursion and does not get 147 * return void, because we are not interested in alloc errors, 148 * the iterator and validator can operate on the results in their 149 * scratch space (the qstate.region) and are not dependent on the cache. 150 * It is useful to log the alloc failure (for the server operator), 151 * but the query resolution can continue without cache storage. 152 */ 153 void iter_dns_store(struct module_env* env, struct query_info* qinf, 154 struct reply_info* rep, int is_referral, time_t leeway, int pside, 155 struct regional* region, uint16_t flags, time_t qstarttime, 156 int is_valrec); 157 158 /** 159 * Select randomly with n/m probability. 160 * For shuffle NS records for address fetching. 161 * @param rnd: random table 162 * @param n: probability. 163 * @param m: divisor for probability. 164 * @return true with n/m probability. 165 */ 166 int iter_ns_probability(struct ub_randstate* rnd, int n, int m); 167 168 /** 169 * Mark targets that result in a dependency cycle as done, so they 170 * will not get selected as targets. 171 * @param qstate: query state. 172 * @param dp: delegpt to mark ns in. 173 */ 174 void iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp); 175 176 /** 177 * Mark targets that result in a dependency cycle as done, so they 178 * will not get selected as targets. For the parent-side lookups. 179 * @param qstate: query state. 180 * @param dp: delegpt to mark ns in. 181 */ 182 void iter_mark_pside_cycle_targets(struct module_qstate* qstate, 183 struct delegpt* dp); 184 185 /** 186 * See if delegation is useful or offers immediately no targets for 187 * further recursion. 188 * @param qinfo: query name and type 189 * @param qflags: query flags with RD flag 190 * @param dp: delegpt to check. 191 * @param supports_ipv4: if we support ipv4 for lookups to the target. 192 * if not, then the IPv4 addresses are useless. 193 * @param supports_ipv6: if we support ipv6 for lookups to the target. 194 * if not, then the IPv6 addresses are useless. 195 * @param use_nat64: if we support NAT64 for lookups to the target. 196 * if yes, IPv4 addresses are useful even if we don't support IPv4. 197 * @return true if dp is useless. 198 */ 199 int iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags, 200 struct delegpt* dp, int supports_ipv4, int supports_ipv6, 201 int use_nat64); 202 203 /** 204 * See if qname has DNSSEC needs. This is true if there is a trust anchor above 205 * it. Whether there is an insecure delegation to the data is unknown. 206 * @param env: environment with anchors. 207 * @param qinfo: query name and class. 208 * @return true if trust anchor above qname, false if no anchor or insecure 209 * point above qname. 210 */ 211 int iter_qname_indicates_dnssec(struct module_env* env, 212 struct query_info *qinfo); 213 214 /** 215 * See if delegation is expected to have DNSSEC information (RRSIGs) in 216 * its answers, or not. Inspects delegation point (name), trust anchors, 217 * and delegation message (DS RRset) to determine this. 218 * @param env: module env with trust anchors. 219 * @param dp: delegation point. 220 * @param msg: delegation message, with DS if a secure referral. 221 * @param dclass: class of query. 222 * @return 1 if dnssec is expected, 0 if not or insecure point above qname. 223 */ 224 int iter_indicates_dnssec(struct module_env* env, struct delegpt* dp, 225 struct dns_msg* msg, uint16_t dclass); 226 227 /** 228 * See if a message contains DNSSEC. 229 * This is examined by looking for RRSIGs. With DNSSEC a valid answer, 230 * nxdomain, nodata, referral or cname reply has RRSIGs in answer or auth 231 * sections, sigs on answer data, SOA, DS, or NSEC/NSEC3 records. 232 * @param msg: message to examine. 233 * @return true if DNSSEC information was found. 234 */ 235 int iter_msg_has_dnssec(struct dns_msg* msg); 236 237 /** 238 * See if a message is known to be from a certain zone. 239 * This looks for SOA or NS rrsets, for answers. 240 * For referrals, when one label is delegated, the zone is detected. 241 * Does not look at signatures. 242 * @param msg: the message to inspect. 243 * @param dp: delegation point with zone name to look for. 244 * @param type: type of message. 245 * @param dclass: class of query. 246 * @return true if message is certain to be from zone in dp->name. 247 * false if not sure (empty msg), or not from the zone. 248 */ 249 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp, 250 enum response_type type, uint16_t dclass); 251 252 /** 253 * Check if two replies are equal 254 * For fallback procedures 255 * @param p: reply one. The reply has rrset data pointers in region. 256 * Does not check rrset-IDs 257 * @param q: reply two 258 * @param region: scratch buffer. 259 * @return if one and two are equal. 260 */ 261 int reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region); 262 263 /** 264 * Remove unused bits from the reply if possible. 265 * So that caps-for-id (0x20) fallback is more likely to be successful. 266 * This removes like, the additional section, and NS record in the authority 267 * section if those records are gratuitous (not for a referral). 268 * @param rep: the reply to strip stuff out of. 269 */ 270 void caps_strip_reply(struct reply_info* rep); 271 272 /** 273 * see if reply has a 'useful' rcode for capsforid comparison, so 274 * not SERVFAIL or REFUSED, and thus NOERROR or NXDOMAIN. 275 * @param rep: reply to check. 276 * @return true if the rcode is a bad type of message. 277 */ 278 int caps_failed_rcode(struct reply_info* rep); 279 280 /** 281 * Store parent-side rrset in separate rrset cache entries for later 282 * last-resort * lookups in case the child-side versions of this information 283 * fails. 284 * @param env: environment with cache, time, ... 285 * @param rrset: the rrset to store (copied). 286 * Failure to store is logged, but otherwise ignored. 287 */ 288 void iter_store_parentside_rrset(struct module_env* env, 289 struct ub_packed_rrset_key* rrset); 290 291 /** 292 * Store parent-side NS records from a referral message 293 * @param env: environment with cache, time, ... 294 * @param rep: response with NS rrset. 295 * Failure to store is logged, but otherwise ignored. 296 */ 297 void iter_store_parentside_NS(struct module_env* env, struct reply_info* rep); 298 299 /** 300 * Store parent-side negative element, the parentside rrset does not exist, 301 * creates an rrset with empty rdata in the rrset cache with PARENTSIDE flag. 302 * @param env: environment with cache, time, ... 303 * @param qinfo: the identity of the rrset that is missing. 304 * @param rep: delegation response or answer response, to glean TTL from. 305 * (malloc) failure is logged but otherwise ignored. 306 */ 307 void iter_store_parentside_neg(struct module_env* env, 308 struct query_info* qinfo, struct reply_info* rep); 309 310 /** 311 * Add parent NS record if that exists in the cache. This is both new 312 * information and acts like a timeout throttle on retries. 313 * @param env: query env with rrset cache and time. 314 * @param dp: delegation point to store result in. Also this dp is used to 315 * see which NS name is needed. 316 * @param region: region to alloc result in. 317 * @param qinfo: pertinent information, the qclass. 318 * @return false on malloc failure. 319 * if true, the routine worked and if such cached information 320 * existed dp->has_parent_side_NS is set true. 321 */ 322 int iter_lookup_parent_NS_from_cache(struct module_env* env, 323 struct delegpt* dp, struct regional* region, struct query_info* qinfo); 324 325 /** 326 * Add parent-side glue if that exists in the cache. This is both new 327 * information and acts like a timeout throttle on retries to fetch them. 328 * @param env: query env with rrset cache and time. 329 * @param dp: delegation point to store result in. Also this dp is used to 330 * see which NS name is needed. 331 * @param region: region to alloc result in. 332 * @param qinfo: pertinent information, the qclass. 333 * @return: true, it worked, no malloc failures, and new addresses (lame) 334 * have been added, giving extra options as query targets. 335 */ 336 int iter_lookup_parent_glue_from_cache(struct module_env* env, 337 struct delegpt* dp, struct regional* region, struct query_info* qinfo); 338 339 /** 340 * Lookup next root-hint or root-forward entry. 341 * @param hints: the hints. 342 * @param fwd: the forwards. 343 * @param c: the class to start searching at. 0 means find first one. 344 * @return false if no classes found, true if found and returned in c. 345 */ 346 int iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd, 347 uint16_t* c); 348 349 /** 350 * Remove DS records that are inappropriate before they are cached. 351 * @param msg: the response to scrub. 352 * @param ns: RRSET that is the NS record for the referral. 353 * if NULL, then all DS records are removed from the authority section. 354 * @param z: zone name that the response is from. 355 */ 356 void iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, 357 uint8_t* z); 358 359 /** 360 * Prepare an NXDOMAIN message to be used for a subdomain answer by removing all 361 * RRs from the ANSWER section. 362 * @param msg: the response to scrub. 363 */ 364 void iter_scrub_nxdomain(struct dns_msg* msg); 365 366 /** 367 * Remove query attempts from all available ips. For 0x20. 368 * @param dp: delegpt. 369 * @param d: decrease. 370 * @param outbound_msg_retry: number of retries of outgoing queries 371 */ 372 void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry); 373 374 /** 375 * Add retry counts from older delegpt to newer delegpt. 376 * Does not waste time on timeout'd (or other failing) addresses. 377 * @param dp: new delegationpoint. 378 * @param old: old delegationpoint. 379 * @param outbound_msg_retry: number of retries of outgoing queries 380 */ 381 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old, 382 int outbound_msg_retry); 383 384 /** 385 * See if a DS response (type ANSWER) is too low: a nodata answer with 386 * a SOA record in the authority section at-or-below the qchase.qname. 387 * Also returns true if we are not sure (i.e. empty message, CNAME nosig). 388 * @param msg: the response. 389 * @param dp: the dp name is used to check if the RRSIG gives a clue that 390 * it was originated from the correct nameserver. 391 * @return true if too low. 392 */ 393 int iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp); 394 395 /** 396 * See if delegpt can go down a step to the qname or not 397 * @param qinfo: the query name looked up. 398 * @param dp: checked if the name can go lower to the qname 399 * @return true if can go down, false if that would not be possible. 400 * the current response seems to be the one and only, best possible, response. 401 */ 402 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp); 403 404 /** 405 * Lookup if no_cache is set in stub or fwd. 406 * @param qstate: query state with env with hints and fwds. 407 * @param qinf: query name to lookup for. 408 * @param retdpname: returns NULL or the deepest enclosing name of fwd or stub. 409 * This is the name under which the closest lookup is going to happen. 410 * Used for NXDOMAIN checks, above that it is an nxdomain from a 411 * different server and zone. You can pass NULL to not get it. 412 * @param retdpnamelen: returns the length of the dpname. 413 * @param dpname_storage: this is where the dpname buf is stored, if any. 414 * So that caller can manage the buffer. 415 * @param dpname_storage_len: size of dpname_storage buffer. 416 * @return true if no_cache is set in stub or fwd. 417 */ 418 int iter_stub_fwd_no_cache(struct module_qstate *qstate, 419 struct query_info *qinf, uint8_t** retdpname, size_t* retdpnamelen, 420 uint8_t* dpname_storage, size_t dpname_storage_len); 421 422 /** 423 * Set support for IP4 and IP6 depending on outgoing interfaces 424 * in the outside network. If none, no support, so no use to lookup 425 * the AAAA and then attempt to use it if there is no outgoing-interface 426 * for it. 427 * @param mods: modstack to find iterator module in. 428 * @param env: module env, find iterator module (if one) in there. 429 * @param outnet: outside network structure. 430 */ 431 void iterator_set_ip46_support(struct module_stack* mods, 432 struct module_env* env, struct outside_network* outnet); 433 434 /** 435 * Read config string that represents the target fetch policy. 436 * @param target_fetch_policy: alloced on return. 437 * @param max_dependency_depth: set on return. 438 * @param str: the config string 439 * @return false on failure. 440 */ 441 int read_fetch_policy(int** target_fetch_policy, int* max_dependency_depth, 442 const char* str); 443 444 /** 445 * Create caps exempt data structure. 446 * @return NULL on failure. 447 */ 448 struct rbtree_type* caps_white_create(void); 449 450 /** 451 * Delete caps exempt data structure. 452 * @param caps_white: caps exempt tree. 453 */ 454 void caps_white_delete(struct rbtree_type* caps_white); 455 456 /** 457 * Apply config caps whitelist items to name tree 458 * @param ntree: caps exempt tree. 459 * @param cfg: config with options. 460 */ 461 int caps_white_apply_cfg(struct rbtree_type* ntree, struct config_file* cfg); 462 463 /** 464 * Apply config for nat64 465 * @param nat64: the nat64 state. 466 * @param cfg: config with options. 467 * @return false on failure. 468 */ 469 int nat64_apply_cfg(struct iter_nat64* nat64, struct config_file* cfg); 470 471 /** 472 * Limit NSEC and NSEC3 TTL in response, RFC9077 473 * @param msg: dns message, the SOA record ttl is used to restrict ttls 474 * of NSEC and NSEC3 RRsets. If no SOA record, nothing happens. 475 */ 476 void limit_nsec_ttl(struct dns_msg* msg); 477 478 /** 479 * Make the response minimal. Removed authority and additional section, 480 * that works when there is an answer in the answer section. 481 * @param rep: reply to modify. 482 */ 483 void iter_make_minimal(struct reply_info* rep); 484 485 #endif /* ITERATOR_ITER_UTILS_H */ 486