2 * iterator/iterator.h - iterative resolver DNS query response module
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains a module that performs recursive iterative DNS query
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"
52 struct iter_prep_list;
56 /** max number of targets spawned for a query and its subqueries */
57 #define MAX_TARGET_COUNT 64
58 /** max number of target lookups per qstate, per delegation point */
59 #define MAX_DP_TARGET_COUNT 16
60 /** max number of nxdomains allowed for target lookups for a query and
62 #define MAX_TARGET_NX 5
63 /** max number of nxdomains allowed for target lookups for a query and
64 * its subqueries when fallback has kicked in */
65 #define MAX_TARGET_NX_FALLBACK (MAX_TARGET_NX*2)
66 /** max number of referrals. Makes sure resolver does not run away */
67 #define MAX_REFERRAL_COUNT 130
68 /** max number of queries for which to perform dnsseclameness detection,
69 * (rrsigs missing detection) after that, just pick up that response */
70 #define DNSSEC_LAME_DETECT_COUNT 4
72 * max number of QNAME minimisation iterations. Limits number of queries for
73 * QNAMEs with a lot of labels.
75 #define MAX_MINIMISE_COUNT 10
76 /* max number of time-outs for minimised query. Prevents resolving failures
77 * when the QNAME minimisation QTYPE is blocked. */
78 #define MAX_MINIMISE_TIMEOUT_COUNT 3
80 * number of labels from QNAME that are always send individually when using
81 * QNAME minimisation, even when the number of labels of the QNAME is bigger
82 * than MAX_MINIMISE_COUNT */
83 #define MINIMISE_ONE_LAB 4
84 #define MINIMISE_MULTIPLE_LABS (MAX_MINIMISE_COUNT - MINIMISE_ONE_LAB)
85 /** at what query-sent-count to stop target fetch policy */
86 #define TARGET_FETCH_STOP 3
87 /** how nice is a server without further information, in msec
88 * Equals rtt initial timeout value.
90 extern int UNKNOWN_SERVER_NICENESS;
91 /** maximum timeout before a host is deemed unsuitable, in msec.
92 * After host_ttl this will be timed out and the host will be tried again.
93 * Equals RTT_MAX_TIMEOUT, and thus when RTT_MAX_TIMEOUT is overwritten by
94 * config infra_cache_max_rtt, it will be overwritten as well. */
95 extern int USEFUL_SERVER_TOP_TIMEOUT;
96 /** penalty to validation failed blacklisted IPs
97 * Equals USEFUL_SERVER_TOP_TIMEOUT*4, and thus when RTT_MAX_TIMEOUT is
98 * overwritten by config infra_cache_max_rtt, it will be overwritten as well. */
99 extern int BLACKLIST_PENALTY;
100 /** RTT band, within this amount from the best, servers are chosen randomly.
101 * Chosen so that the UNKNOWN_SERVER_NICENESS falls within the band of a
102 * fast server, this causes server exploration as a side benefit. msec. */
106 * Global state for the iterator.
109 /** A flag to indicate whether or not we have an IPv6 route */
112 /** A flag to indicate whether or not we have an IPv4 route */
115 /** A set of inetaddrs that should never be queried. */
116 struct iter_donotq* donotq;
118 /** private address space and private domains */
119 struct iter_priv* priv;
121 /** whitelist for capsforid names */
122 struct rbtree_type* caps_white;
124 /** The maximum dependency depth that this resolver will pursue. */
125 int max_dependency_depth;
128 * The target fetch policy for each dependency level. This is
129 * described as a simple number (per dependency level):
130 * negative numbers (usually just -1) mean fetch-all,
131 * 0 means only fetch on demand, and
132 * positive numbers mean to fetch at most that many targets.
133 * array of max_dependency_depth+1 size.
135 int* target_fetch_policy;
137 /** lock on ratelimit counter */
138 lock_basic_type queries_ratelimit_lock;
139 /** number of queries that have been ratelimited */
140 size_t num_queries_ratelimited;
142 /** number of retries on outgoing queries */
143 int outbound_msg_retry;
145 /** number of queries_sent */
148 /** max number of query restarts to limit length of CNAME chain */
149 int max_query_restarts;
153 * QNAME minimisation state
155 enum minimisation_state {
157 * (Re)start minimisation. Outgoing QNAME should be set to dp->name.
158 * State entered on new query or after following referral or CNAME.
160 INIT_MINIMISE_STATE = 0,
162 * QNAME minimisation ongoing. Increase QNAME on every iteration.
166 * Don't increment QNAME this iteration
170 * Send out full QNAME + original QTYPE
172 DONOT_MINIMISE_STATE,
176 * State of the iterator for a query.
180 * Externally generated queries start at this state. Query restarts are
181 * reset to this state.
183 INIT_REQUEST_STATE = 0,
186 * Root priming events reactivate here, most other events pass
187 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
189 INIT_REQUEST_2_STATE,
192 * Stub priming events reactivate here, most other events pass
193 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
195 INIT_REQUEST_3_STATE,
198 * Each time a delegation point changes for a given query or a
199 * query times out and/or wakes up, this state is (re)visited.
200 * This state is responsible for iterating through a list of
201 * nameserver targets.
206 * Responses to queries start at this state. This state handles
207 * the decision tree associated with handling responses.
211 /** Responses to priming queries finish at this state. */
214 /** Collecting query class information, for qclass=ANY, when
215 * it spawns off queries for every class, it returns here. */
218 /** Find NS record to resolve DS record from, walking to the right
219 * NS spot until we find it */
222 /** Responses that are to be returned upstream end at this state.
223 * As well as responses to target queries. */
228 * Shared counters for queries.
230 enum target_count_variables {
231 /** Reference count for the shared iter_qstate->target_count. */
232 TARGET_COUNT_REF = 0,
233 /** Number of target queries spawned for the query and subqueries. */
234 TARGET_COUNT_QUERIES,
235 /** Number of nxdomain responses encountered. */
238 /** This should stay last here, it is used for the allocation */
243 * Per query state for the iterator module.
247 * State of the iterator module.
248 * This is the state that event is in or should sent to -- all
249 * requests should start with the INIT_REQUEST_STATE. All
250 * responses should start with QUERY_RESP_STATE. Subsequent
251 * processing of the event will change this state.
253 enum iter_state state;
256 * Final state for the iterator module.
257 * This is the state that responses should be routed to once the
258 * response is final. For externally initiated queries, this
259 * will be FINISHED_STATE, locally initiated queries will have
260 * different final states.
262 enum iter_state final_state;
265 * The depth of this query, this means the depth of recursion.
266 * This address is needed for another query, which is an address
267 * needed for another query, etc. Original client query has depth 0.
274 struct dns_msg* response;
277 * This is a list of RRsets that must be prepended to the
278 * ANSWER section of a response before being sent upstream.
280 struct iter_prep_list* an_prepend_list;
281 /** Last element of the prepend list */
282 struct iter_prep_list* an_prepend_last;
285 * This is the list of RRsets that must be prepended to the
286 * AUTHORITY section of the response before being sent upstream.
288 struct iter_prep_list* ns_prepend_list;
289 /** Last element of the authority prepend list */
290 struct iter_prep_list* ns_prepend_last;
292 /** query name used for chasing the results. Initially the same as
293 * the state qinfo, but after CNAMEs this will be different.
294 * The query info used to elicit the results needed. */
295 struct query_info qchase;
296 /** query flags to use when chasing the answer (i.e. RD flag) */
297 uint16_t chase_flags;
298 /** true if we set RD bit because of last resort recursion lame query*/
302 * This is the current delegation point for an in-progress query. This
303 * object retains state as to which delegation targets need to be
304 * (sub)queried for vs which ones have already been visited.
308 /** state for 0x20 fallback when capsfail happens, 0 not a fallback */
310 /** state for capsfail: current server number to try */
312 /** state for capsfail: stored query for comparisons. Can be NULL if
313 * no response had been seen prior to starting the fallback. */
314 struct reply_info* caps_reply;
315 struct dns_msg* caps_response;
317 /** Current delegation message - returned for non-RD queries */
318 struct dns_msg* deleg_msg;
320 /** number of outstanding target sub queries */
321 int num_target_queries;
323 /** outstanding direct queries */
324 int num_current_queries;
326 /** the number of times this query has been restarted. */
327 int query_restart_count;
329 /** the number of times this query has followed a referral. */
332 /** number of queries fired off */
335 /** malloced-array shared with this query and its subqueries. It keeps
336 * track of the defined enum target_count_variables counters. */
339 /** number of target lookups per delegation point. Reset to 0 after
340 * receiving referral answer. Not shared with subqueries. */
343 /** Delegation point that triggered the NXNS fallback; shared with
344 * this query and its subqueries, count-referenced by the reference
345 * counter in target_count.
346 * This also marks the fallback activation. */
349 /** if true, already tested for ratelimiting and passed the test */
353 * The query must store NS records from referrals as parentside RRs
354 * Enabled once it hits resolution problems, to throttle retries.
355 * If enabled it is the pointer to the old delegation point with
356 * the old retry counts for bad-nameserver-addresses.
358 struct delegpt* store_parent_NS;
361 * The query is for parent-side glue(A or AAAA) for a nameserver.
362 * If the item is seen as glue in a referral, and pside_glue is NULL,
363 * then it is stored in pside_glue for later.
364 * If it was never seen, at the end, then a negative caching element
366 * The (data or negative) RR cache element then throttles retries.
368 int query_for_pside_glue;
369 /** the parent-side-glue element (NULL if none, its first match) */
370 struct ub_packed_rrset_key* pside_glue;
372 /** If nonNULL we are walking upwards from DS query to find NS */
374 /** length of the dname in dsns_point */
375 size_t dsns_point_len;
378 * expected dnssec information for this iteration step.
379 * If dnssec rrsigs are expected and not given, the server is marked
380 * lame (dnssec-lame).
385 * We are expecting dnssec information, but we also know the server
386 * is DNSSEC lame. The response need not be marked dnssec-lame again.
388 int dnssec_lame_query;
391 * This is flag that, if true, means that this event is
392 * waiting for a stub priming query.
394 int wait_priming_stub;
397 * This is a flag that, if true, means that this query is
398 * for (re)fetching glue from a zone. Since the address should
399 * have been glue, query again to the servers that should have
400 * been returning it as glue.
401 * The delegation point must be set to the one that should *not*
402 * be used when creating the state. A higher one will be attempted.
406 /** list of pending queries to authoritative servers. */
407 struct outbound_list outlist;
409 /** QNAME minimisation state, RFC9156 */
410 enum minimisation_state minimisation_state;
412 /** State for capsfail: QNAME minimisation state for comparisons. */
413 enum minimisation_state caps_minimisation_state;
416 * The query info that is sent upstream. Will be a subset of qchase
417 * when qname minimisation is enabled.
419 struct query_info qinfo_out;
422 * Count number of QNAME minimisation iterations. Used to limit number of
423 * outgoing queries when QNAME minimisation is enabled.
428 * Count number of time-outs. Used to prevent resolving failures when
429 * the QNAME minimisation QTYPE is blocked. Used to determine if
430 * capsforid fallback should be started.*/
433 /** True if the current response is from auth_zone */
434 int auth_zone_response;
435 /** True if the auth_zones should not be consulted for the query */
437 /** true if there have been scrubbing failures of reply packets */
439 /** true if there have been parse failures of reply packets */
441 /** a failure printout address for last received answer */
442 struct comm_reply* fail_reply;
446 * List of prepend items
448 struct iter_prep_list {
450 struct iter_prep_list* next;
452 struct ub_packed_rrset_key* rrset;
456 * Get the iterator function block.
457 * @return: function block with function pointers to iterator methods.
459 struct module_func_block* iter_get_funcblock(void);
462 * Get iterator state as a string
463 * @param state: to convert
464 * @return constant string that is printable.
466 const char* iter_state_to_string(enum iter_state state);
469 * See if iterator state is a response state
470 * @param s: to inspect
471 * @return true if response state.
473 int iter_state_is_responsestate(enum iter_state s);
476 int iter_init(struct module_env* env, int id);
478 /** iterator deinit */
479 void iter_deinit(struct module_env* env, int id);
481 /** iterator operate on a query */
482 void iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
483 struct outbound_entry* outbound);
486 * Return priming query results to interested super querystates.
488 * Sets the delegation point and delegation message (not nonRD queries).
489 * This is a callback from walk_supers.
491 * @param qstate: query state that finished.
492 * @param id: module id.
493 * @param super: the qstate to inform.
495 void iter_inform_super(struct module_qstate* qstate, int id,
496 struct module_qstate* super);
498 /** iterator cleanup query state */
499 void iter_clear(struct module_qstate* qstate, int id);
501 /** iterator alloc size routine */
502 size_t iter_get_mem(struct module_env* env, int id);
504 #endif /* ITERATOR_ITERATOR_H */