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 LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * 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 #include <ldns/buffer.h> 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 63 /** 64 * Process config options and set iterator module state. 65 * Sets default values if no config is found. 66 * @param iter_env: iterator module state. 67 * @param cfg: config options. 68 * @return 0 on error. 69 */ 70 int iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg); 71 72 /** 73 * Select a valid, nice target to send query to. 74 * Sorting and removing unsuitable targets is combined. 75 * 76 * @param iter_env: iterator module global state, with ip6 enabled and 77 * do-not-query-addresses. 78 * @param env: environment with infra cache (lameness, rtt info). 79 * @param dp: delegation point with result list. 80 * @param name: zone name (for lameness check). 81 * @param namelen: length of name. 82 * @param qtype: query type that we want to send. 83 * @param dnssec_lame: set to 1, if a known dnssec-lame server is selected 84 * these are not preferred, but are used as a last resort. 85 * @param chase_to_rd: set to 1 if a known recursion lame server is selected 86 * these are not preferred, but are used as a last resort. 87 * @param open_target: number of currently outstanding target queries. 88 * If we wait for these, perhaps more server addresses become available. 89 * @param blacklist: the IP blacklist to use. 90 * @return best target or NULL if no target. 91 * if not null, that target is removed from the result list in the dp. 92 */ 93 struct delegpt_addr* iter_server_selection(struct iter_env* iter_env, 94 struct module_env* env, struct delegpt* dp, uint8_t* name, 95 size_t namelen, uint16_t qtype, int* dnssec_lame, 96 int* chase_to_rd, int open_target, struct sock_list* blacklist); 97 98 /** 99 * Allocate dns_msg from parsed msg, in regional. 100 * @param pkt: packet. 101 * @param msg: parsed message (cleaned and ready for regional allocation). 102 * @param regional: regional to use for allocation. 103 * @return newly allocated dns_msg, or NULL on memory error. 104 */ 105 struct dns_msg* dns_alloc_msg(ldns_buffer* pkt, struct msg_parse* msg, 106 struct regional* regional); 107 108 /** 109 * Copy a dns_msg to this regional. 110 * @param from: dns message, also in regional. 111 * @param regional: regional to use for allocation. 112 * @return newly allocated dns_msg, or NULL on memory error. 113 */ 114 struct dns_msg* dns_copy_msg(struct dns_msg* from, struct regional* regional); 115 116 /** 117 * Allocate a dns_msg with malloc/alloc structure and store in dns cache. 118 * @param env: environment, with alloc structure and dns cache. 119 * @param qinf: query info, the query for which answer is stored. 120 * @param rep: reply in dns_msg from dns_alloc_msg for example. 121 * @param is_referral: If true, then the given message to be stored is a 122 * referral. The cache implementation may use this as a hint. 123 * @param leeway: prefetch TTL leeway to expire old rrsets quicker. 124 * @param region: to copy modified (cache is better) rrs back to. 125 * @return 0 on alloc error (out of memory). 126 */ 127 int iter_dns_store(struct module_env* env, struct query_info* qinf, 128 struct reply_info* rep, int is_referral, uint32_t leeway, 129 struct regional* region); 130 131 /** 132 * Select randomly with n/m probability. 133 * For shuffle NS records for address fetching. 134 * @param rnd: random table 135 * @param n: probability. 136 * @param m: divisor for probability. 137 * @return true with n/m probability. 138 */ 139 int iter_ns_probability(struct ub_randstate* rnd, int n, int m); 140 141 /** 142 * Mark targets that result in a dependency cycle as done, so they 143 * will not get selected as targets. 144 * @param qstate: query state. 145 * @param dp: delegpt to mark ns in. 146 */ 147 void iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp); 148 149 /** 150 * Mark targets that result in a dependency cycle as done, so they 151 * will not get selected as targets. For the parent-side lookups. 152 * @param qstate: query state. 153 * @param dp: delegpt to mark ns in. 154 */ 155 void iter_mark_pside_cycle_targets(struct module_qstate* qstate, 156 struct delegpt* dp); 157 158 /** 159 * See if delegation is useful or offers immediately no targets for 160 * further recursion. 161 * @param qinfo: query name and type 162 * @param qflags: query flags with RD flag 163 * @param dp: delegpt to check. 164 * @return true if dp is useless. 165 */ 166 int iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags, 167 struct delegpt* dp); 168 169 /** 170 * See if delegation is expected to have DNSSEC information (RRSIGs) in 171 * its answers, or not. Inspects delegation point (name), trust anchors, 172 * and delegation message (DS RRset) to determine this. 173 * @param env: module env with trust anchors. 174 * @param dp: delegation point. 175 * @param msg: delegation message, with DS if a secure referral. 176 * @param dclass: class of query. 177 * @return 1 if dnssec is expected, 0 if not. 178 */ 179 int iter_indicates_dnssec(struct module_env* env, struct delegpt* dp, 180 struct dns_msg* msg, uint16_t dclass); 181 182 /** 183 * See if a message contains DNSSEC. 184 * This is examined by looking for RRSIGs. With DNSSEC a valid answer, 185 * nxdomain, nodata, referral or cname reply has RRSIGs in answer or auth 186 * sections, sigs on answer data, SOA, DS, or NSEC/NSEC3 records. 187 * @param msg: message to examine. 188 * @return true if DNSSEC information was found. 189 */ 190 int iter_msg_has_dnssec(struct dns_msg* msg); 191 192 /** 193 * See if a message is known to be from a certain zone. 194 * This looks for SOA or NS rrsets, for answers. 195 * For referrals, when one label is delegated, the zone is detected. 196 * Does not look at signatures. 197 * @param msg: the message to inspect. 198 * @param dp: delegation point with zone name to look for. 199 * @param type: type of message. 200 * @param dclass: class of query. 201 * @return true if message is certain to be from zone in dp->name. 202 * false if not sure (empty msg), or not from the zone. 203 */ 204 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp, 205 enum response_type type, uint16_t dclass); 206 207 /** 208 * Check if two replies are equal 209 * For fallback procedures 210 * @param p: reply one. The reply has rrset data pointers in region. 211 * Does not check rrset-IDs 212 * @param q: reply two 213 * @param buf: scratch buffer. 214 * @return if one and two are equal. 215 */ 216 int reply_equal(struct reply_info* p, struct reply_info* q, ldns_buffer* buf); 217 218 /** 219 * Store parent-side rrset in seperate rrset cache entries for later 220 * last-resort * lookups in case the child-side versions of this information 221 * fails. 222 * @param env: environment with cache, time, ... 223 * @param rrset: the rrset to store (copied). 224 * Failure to store is logged, but otherwise ignored. 225 */ 226 void iter_store_parentside_rrset(struct module_env* env, 227 struct ub_packed_rrset_key* rrset); 228 229 /** 230 * Store parent-side NS records from a referral message 231 * @param env: environment with cache, time, ... 232 * @param rep: response with NS rrset. 233 * Failure to store is logged, but otherwise ignored. 234 */ 235 void iter_store_parentside_NS(struct module_env* env, struct reply_info* rep); 236 237 /** 238 * Store parent-side negative element, the parentside rrset does not exist, 239 * creates an rrset with empty rdata in the rrset cache with PARENTSIDE flag. 240 * @param env: environment with cache, time, ... 241 * @param qinfo: the identity of the rrset that is missing. 242 * @param rep: delegation response or answer response, to glean TTL from. 243 * (malloc) failure is logged but otherwise ignored. 244 */ 245 void iter_store_parentside_neg(struct module_env* env, 246 struct query_info* qinfo, struct reply_info* rep); 247 248 /** 249 * Add parent NS record if that exists in the cache. This is both new 250 * information and acts like a timeout throttle on retries. 251 * @param env: query env with rrset cache and time. 252 * @param dp: delegation point to store result in. Also this dp is used to 253 * see which NS name is needed. 254 * @param region: region to alloc result in. 255 * @param qinfo: pertinent information, the qclass. 256 * @return false on malloc failure. 257 * if true, the routine worked and if such cached information 258 * existed dp->has_parent_side_NS is set true. 259 */ 260 int iter_lookup_parent_NS_from_cache(struct module_env* env, 261 struct delegpt* dp, struct regional* region, struct query_info* qinfo); 262 263 /** 264 * Add parent-side glue if that exists in the cache. This is both new 265 * information and acts like a timeout throttle on retries to fetch them. 266 * @param env: query env with rrset cache and time. 267 * @param dp: delegation point to store result in. Also this dp is used to 268 * see which NS name is needed. 269 * @param region: region to alloc result in. 270 * @param qinfo: pertinent information, the qclass. 271 * @return: true, it worked, no malloc failures, and new addresses (lame) 272 * have been added, giving extra options as query targets. 273 */ 274 int iter_lookup_parent_glue_from_cache(struct module_env* env, 275 struct delegpt* dp, struct regional* region, struct query_info* qinfo); 276 277 /** 278 * Lookup next root-hint or root-forward entry. 279 * @param hints: the hints. 280 * @param fwd: the forwards. 281 * @param c: the class to start searching at. 0 means find first one. 282 * @return false if no classes found, true if found and returned in c. 283 */ 284 int iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd, 285 uint16_t* c); 286 287 /** 288 * Remove DS records that are inappropriate before they are cached. 289 * @param msg: the response to scrub. 290 * @param ns: RRSET that is the NS record for the referral. 291 * if NULL, then all DS records are removed from the authority section. 292 * @param z: zone name that the response is from. 293 */ 294 void iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, 295 uint8_t* z); 296 297 /** 298 * Remove query attempts from all available ips. For 0x20. 299 * @param dp: delegpt. 300 * @param d: decrease. 301 */ 302 void iter_dec_attempts(struct delegpt* dp, int d); 303 304 /** 305 * Add retry counts from older delegpt to newer delegpt. 306 * Does not waste time on timeout'd (or other failing) addresses. 307 * @param dp: new delegationpoint. 308 * @param old: old delegationpoint. 309 */ 310 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old); 311 312 #endif /* ITERATOR_ITER_UTILS_H */ 313