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