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 query restarts. Determines max number of CNAME chain. */
59 #define MAX_RESTART_COUNT 8
60 /** max number of referrals. Makes sure resolver does not run away */
61 #define MAX_REFERRAL_COUNT 130
62 /** max number of queries-sent-out. Make sure large NS set does not loop */
63 #define MAX_SENT_COUNT 32
64 /** max number of queries for which to perform dnsseclameness detection,
65 * (rrsigs missing detection) after that, just pick up that response */
66 #define DNSSEC_LAME_DETECT_COUNT 4
68 * max number of QNAME minimisation iterations. Limits number of queries for
69 * QNAMEs with a lot of labels.
71 #define MAX_MINIMISE_COUNT 10
72 /* max number of time-outs for minimised query. Prevents resolving failures
73 * when the QNAME minimisation QTYPE is blocked. */
74 #define MAX_MINIMISE_TIMEOUT_COUNT 3
76 * number of labels from QNAME that are always send individually when using
77 * QNAME minimisation, even when the number of labels of the QNAME is bigger
78 * tham MAX_MINIMISE_COUNT */
79 #define MINIMISE_ONE_LAB 4
80 #define MINIMISE_MULTIPLE_LABS (MAX_MINIMISE_COUNT - MINIMISE_ONE_LAB)
81 /** at what query-sent-count to stop target fetch policy */
82 #define TARGET_FETCH_STOP 3
83 /** how nice is a server without further information, in msec
84 * Equals rtt initial timeout value.
86 #define UNKNOWN_SERVER_NICENESS 376
87 /** maximum timeout before a host is deemed unsuitable, in msec.
88 * After host_ttl this will be timed out and the host will be tried again.
89 * Equals RTT_MAX_TIMEOUT
91 #define USEFUL_SERVER_TOP_TIMEOUT 120000
92 /** number of retries on outgoing queries */
93 #define OUTBOUND_MSG_RETRY 5
94 /** RTT band, within this amount from the best, servers are chosen randomly.
95 * Chosen so that the UNKNOWN_SERVER_NICENESS falls within the band of a
96 * fast server, this causes server exploration as a side benefit. msec. */
98 /** Start value for blacklisting a host, 2*USEFUL_SERVER_TOP_TIMEOUT in sec */
99 #define INFRA_BACKOFF_INITIAL 240
102 * Global state for the iterator.
105 /** A flag to indicate whether or not we have an IPv6 route */
108 /** A flag to indicate whether or not we have an IPv4 route */
111 /** A set of inetaddrs that should never be queried. */
112 struct iter_donotq* donotq;
114 /** private address space and private domains */
115 struct iter_priv* priv;
117 /** whitelist for capsforid names */
118 struct rbtree_type* caps_white;
120 /** The maximum dependency depth that this resolver will pursue. */
121 int max_dependency_depth;
124 * The target fetch policy for each dependency level. This is
125 * described as a simple number (per dependency level):
126 * negative numbers (usually just -1) mean fetch-all,
127 * 0 means only fetch on demand, and
128 * positive numbers mean to fetch at most that many targets.
129 * array of max_dependency_depth+1 size.
131 int* target_fetch_policy;
133 /** lock on ratelimit counter */
134 lock_basic_type queries_ratelimit_lock;
135 /** number of queries that have been ratelimited */
136 size_t num_queries_ratelimited;
140 * QNAME minimisation state
142 enum minimisation_state {
144 * (Re)start minimisation. Outgoing QNAME should be set to dp->name.
145 * State entered on new query or after following referral or CNAME.
147 INIT_MINIMISE_STATE = 0,
149 * QNAME minimisation ongoing. Increase QNAME on every iteration.
153 * Don't increment QNAME this iteration
157 * Send out full QNAME + original QTYPE
159 DONOT_MINIMISE_STATE,
163 * State of the iterator for a query.
167 * Externally generated queries start at this state. Query restarts are
168 * reset to this state.
170 INIT_REQUEST_STATE = 0,
173 * Root priming events reactivate here, most other events pass
174 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
176 INIT_REQUEST_2_STATE,
179 * Stub priming events reactivate here, most other events pass
180 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
182 INIT_REQUEST_3_STATE,
185 * Each time a delegation point changes for a given query or a
186 * query times out and/or wakes up, this state is (re)visited.
187 * This state is responsible for iterating through a list of
188 * nameserver targets.
193 * Responses to queries start at this state. This state handles
194 * the decision tree associated with handling responses.
198 /** Responses to priming queries finish at this state. */
201 /** Collecting query class information, for qclass=ANY, when
202 * it spawns off queries for every class, it returns here. */
205 /** Find NS record to resolve DS record from, walking to the right
206 * NS spot until we find it */
209 /** Responses that are to be returned upstream end at this state.
210 * As well as responses to target queries. */
215 * Per query state for the iterator module.
219 * State of the iterator module.
220 * This is the state that event is in or should sent to -- all
221 * requests should start with the INIT_REQUEST_STATE. All
222 * responses should start with QUERY_RESP_STATE. Subsequent
223 * processing of the event will change this state.
225 enum iter_state state;
228 * Final state for the iterator module.
229 * This is the state that responses should be routed to once the
230 * response is final. For externally initiated queries, this
231 * will be FINISHED_STATE, locally initiated queries will have
232 * different final states.
234 enum iter_state final_state;
237 * The depth of this query, this means the depth of recursion.
238 * This address is needed for another query, which is an address
239 * needed for another query, etc. Original client query has depth 0.
246 struct dns_msg* response;
249 * This is a list of RRsets that must be prepended to the
250 * ANSWER section of a response before being sent upstream.
252 struct iter_prep_list* an_prepend_list;
253 /** Last element of the prepend list */
254 struct iter_prep_list* an_prepend_last;
257 * This is the list of RRsets that must be prepended to the
258 * AUTHORITY section of the response before being sent upstream.
260 struct iter_prep_list* ns_prepend_list;
261 /** Last element of the authority prepend list */
262 struct iter_prep_list* ns_prepend_last;
264 /** query name used for chasing the results. Initially the same as
265 * the state qinfo, but after CNAMEs this will be different.
266 * The query info used to elicit the results needed. */
267 struct query_info qchase;
268 /** query flags to use when chasing the answer (i.e. RD flag) */
269 uint16_t chase_flags;
270 /** true if we set RD bit because of last resort recursion lame query*/
274 * This is the current delegation point for an in-progress query. This
275 * object retains state as to which delegation targets need to be
276 * (sub)queried for vs which ones have already been visited.
280 /** state for 0x20 fallback when capsfail happens, 0 not a fallback */
282 /** state for capsfail: current server number to try */
284 /** state for capsfail: stored query for comparisons. Can be NULL if
285 * no response had been seen prior to starting the fallback. */
286 struct reply_info* caps_reply;
287 struct dns_msg* caps_response;
289 /** Current delegation message - returned for non-RD queries */
290 struct dns_msg* deleg_msg;
292 /** number of outstanding target sub queries */
293 int num_target_queries;
295 /** outstanding direct queries */
296 int num_current_queries;
298 /** the number of times this query has been restarted. */
299 int query_restart_count;
301 /** the number of times this query as followed a referral. */
304 /** number of queries fired off */
307 /** number of target queries spawned in [1], for this query and its
308 * subqueries, the malloced-array is shared, [0] refcount. */
311 /** if true, already tested for ratelimiting and passed the test */
315 * The query must store NS records from referrals as parentside RRs
316 * Enabled once it hits resolution problems, to throttle retries.
317 * If enabled it is the pointer to the old delegation point with
318 * the old retry counts for bad-nameserver-addresses.
320 struct delegpt* store_parent_NS;
323 * The query is for parent-side glue(A or AAAA) for a nameserver.
324 * If the item is seen as glue in a referral, and pside_glue is NULL,
325 * then it is stored in pside_glue for later.
326 * If it was never seen, at the end, then a negative caching element
328 * The (data or negative) RR cache element then throttles retries.
330 int query_for_pside_glue;
331 /** the parent-side-glue element (NULL if none, its first match) */
332 struct ub_packed_rrset_key* pside_glue;
334 /** If nonNULL we are walking upwards from DS query to find NS */
336 /** length of the dname in dsns_point */
337 size_t dsns_point_len;
340 * expected dnssec information for this iteration step.
341 * If dnssec rrsigs are expected and not given, the server is marked
342 * lame (dnssec-lame).
347 * We are expecting dnssec information, but we also know the server
348 * is DNSSEC lame. The response need not be marked dnssec-lame again.
350 int dnssec_lame_query;
353 * This is flag that, if true, means that this event is
354 * waiting for a stub priming query.
356 int wait_priming_stub;
359 * This is a flag that, if true, means that this query is
360 * for (re)fetching glue from a zone. Since the address should
361 * have been glue, query again to the servers that should have
362 * been returning it as glue.
363 * The delegation point must be set to the one that should *not*
364 * be used when creating the state. A higher one will be attempted.
368 /** list of pending queries to authoritative servers. */
369 struct outbound_list outlist;
371 /** QNAME minimisation state, RFC7816 */
372 enum minimisation_state minimisation_state;
374 /** State for capsfail: QNAME minimisation state for comparisons. */
375 enum minimisation_state caps_minimisation_state;
378 * The query info that is sent upstream. Will be a subset of qchase
379 * when qname minimisation is enabled.
381 struct query_info qinfo_out;
384 * Count number of QNAME minimisation iterations. Used to limit number of
385 * outgoing queries when QNAME minimisation is enabled.
390 * Count number of time-outs. Used to prevent resolving failures when
391 * the QNAME minimisation QTYPE is blocked. */
392 int minimise_timeout_count;
394 /** True if the current response is from auth_zone */
395 int auth_zone_response;
396 /** True if the auth_zones should not be consulted for the query */
401 * List of prepend items
403 struct iter_prep_list {
405 struct iter_prep_list* next;
407 struct ub_packed_rrset_key* rrset;
411 * Get the iterator function block.
412 * @return: function block with function pointers to iterator methods.
414 struct module_func_block* iter_get_funcblock(void);
417 * Get iterator state as a string
418 * @param state: to convert
419 * @return constant string that is printable.
421 const char* iter_state_to_string(enum iter_state state);
424 * See if iterator state is a response state
425 * @param s: to inspect
426 * @return true if response state.
428 int iter_state_is_responsestate(enum iter_state s);
431 int iter_init(struct module_env* env, int id);
433 /** iterator deinit */
434 void iter_deinit(struct module_env* env, int id);
436 /** iterator operate on a query */
437 void iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
438 struct outbound_entry* outbound);
441 * Return priming query results to interested super querystates.
443 * Sets the delegation point and delegation message (not nonRD queries).
444 * This is a callback from walk_supers.
446 * @param qstate: query state that finished.
447 * @param id: module id.
448 * @param super: the qstate to inform.
450 void iter_inform_super(struct module_qstate* qstate, int id,
451 struct module_qstate* super);
453 /** iterator cleanup query state */
454 void iter_clear(struct module_qstate* qstate, int id);
456 /** iterator alloc size routine */
457 size_t iter_get_mem(struct module_env* env, int id);
459 #endif /* ITERATOR_ITERATOR_H */