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 recusive 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 misssing 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_t* 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 /** ip6.arpa dname in wireformat, used for qname-minimisation */
134 uint8_t* ip6arpa_dname;
138 * QNAME minimisation state
140 enum minimisation_state {
142 * (Re)start minimisation. Outgoing QNAME should be set to dp->name.
143 * State entered on new query or after following refferal or CNAME.
145 INIT_MINIMISE_STATE = 0,
147 * QNAME minimisataion ongoing. Increase QNAME on every iteration.
151 * Don't increment QNAME this iteration
155 * Send out full QNAME + original QTYPE
157 DONOT_MINIMISE_STATE,
161 * State of the iterator for a query.
165 * Externally generated queries start at this state. Query restarts are
166 * reset to this state.
168 INIT_REQUEST_STATE = 0,
171 * Root priming events reactivate here, most other events pass
172 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
174 INIT_REQUEST_2_STATE,
177 * Stub priming events reactivate here, most other events pass
178 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
180 INIT_REQUEST_3_STATE,
183 * Each time a delegation point changes for a given query or a
184 * query times out and/or wakes up, this state is (re)visited.
185 * This state is reponsible for iterating through a list of
186 * nameserver targets.
191 * Responses to queries start at this state. This state handles
192 * the decision tree associated with handling responses.
196 /** Responses to priming queries finish at this state. */
199 /** Collecting query class information, for qclass=ANY, when
200 * it spawns off queries for every class, it returns here. */
203 /** Find NS record to resolve DS record from, walking to the right
204 * NS spot until we find it */
207 /** Responses that are to be returned upstream end at this state.
208 * As well as responses to target queries. */
213 * Per query state for the iterator module.
217 * State of the iterator module.
218 * This is the state that event is in or should sent to -- all
219 * requests should start with the INIT_REQUEST_STATE. All
220 * responses should start with QUERY_RESP_STATE. Subsequent
221 * processing of the event will change this state.
223 enum iter_state state;
226 * Final state for the iterator module.
227 * This is the state that responses should be routed to once the
228 * response is final. For externally initiated queries, this
229 * will be FINISHED_STATE, locally initiated queries will have
230 * different final states.
232 enum iter_state final_state;
235 * The depth of this query, this means the depth of recursion.
236 * This address is needed for another query, which is an address
237 * needed for another query, etc. Original client query has depth 0.
244 struct dns_msg* response;
247 * This is a list of RRsets that must be prepended to the
248 * ANSWER section of a response before being sent upstream.
250 struct iter_prep_list* an_prepend_list;
251 /** Last element of the prepend list */
252 struct iter_prep_list* an_prepend_last;
255 * This is the list of RRsets that must be prepended to the
256 * AUTHORITY section of the response before being sent upstream.
258 struct iter_prep_list* ns_prepend_list;
259 /** Last element of the authority prepend list */
260 struct iter_prep_list* ns_prepend_last;
262 /** query name used for chasing the results. Initially the same as
263 * the state qinfo, but after CNAMEs this will be different.
264 * The query info used to elicit the results needed. */
265 struct query_info qchase;
266 /** query flags to use when chasing the answer (i.e. RD flag) */
267 uint16_t chase_flags;
268 /** true if we set RD bit because of last resort recursion lame query*/
272 * This is the current delegation point for an in-progress query. This
273 * object retains state as to which delegation targets need to be
274 * (sub)queried for vs which ones have already been visited.
278 /** state for 0x20 fallback when capsfail happens, 0 not a fallback */
280 /** state for capsfail: current server number to try */
282 /** state for capsfail: stored query for comparisons. Can be NULL if
283 * no response had been seen prior to starting the fallback. */
284 struct reply_info* caps_reply;
285 struct dns_msg* caps_response;
287 /** Current delegation message - returned for non-RD queries */
288 struct dns_msg* deleg_msg;
290 /** number of outstanding target sub queries */
291 int num_target_queries;
293 /** outstanding direct queries */
294 int num_current_queries;
296 /** the number of times this query has been restarted. */
297 int query_restart_count;
299 /** the number of times this query as followed a referral. */
302 /** number of queries fired off */
305 /** number of target queries spawned in [1], for this query and its
306 * subqueries, the malloced-array is shared, [0] refcount. */
309 /** if true, already tested for ratelimiting and passed the test */
313 * The query must store NS records from referrals as parentside RRs
314 * Enabled once it hits resolution problems, to throttle retries.
315 * If enabled it is the pointer to the old delegation point with
316 * the old retry counts for bad-nameserver-addresses.
318 struct delegpt* store_parent_NS;
321 * The query is for parent-side glue(A or AAAA) for a nameserver.
322 * If the item is seen as glue in a referral, and pside_glue is NULL,
323 * then it is stored in pside_glue for later.
324 * If it was never seen, at the end, then a negative caching element
326 * The (data or negative) RR cache element then throttles retries.
328 int query_for_pside_glue;
329 /** the parent-side-glue element (NULL if none, its first match) */
330 struct ub_packed_rrset_key* pside_glue;
332 /** If nonNULL we are walking upwards from DS query to find NS */
334 /** length of the dname in dsns_point */
335 size_t dsns_point_len;
338 * expected dnssec information for this iteration step.
339 * If dnssec rrsigs are expected and not given, the server is marked
340 * lame (dnssec-lame).
345 * We are expecting dnssec information, but we also know the server
346 * is DNSSEC lame. The response need not be marked dnssec-lame again.
348 int dnssec_lame_query;
351 * This is flag that, if true, means that this event is
352 * waiting for a stub priming query.
354 int wait_priming_stub;
357 * This is a flag that, if true, means that this query is
358 * for (re)fetching glue from a zone. Since the address should
359 * have been glue, query again to the servers that should have
360 * been returning it as glue.
361 * The delegation point must be set to the one that should *not*
362 * be used when creating the state. A higher one will be attempted.
366 /** list of pending queries to authoritative servers. */
367 struct outbound_list outlist;
369 /** QNAME minimisation state, RFC7816 */
370 enum minimisation_state minimisation_state;
373 * The query info that is sent upstream. Will be a subset of qchase
374 * when qname minimisation is enabled.
376 struct query_info qinfo_out;
379 * Count number of QNAME minisation iterations. Used to limit number of
380 * outgoing queries when QNAME minimisation is enabled.
385 * Count number of time-outs. Used to prevent resolving failures when
386 * the QNAME minimisation QTYPE is blocked. */
387 int minimise_timeout_count;
391 * List of prepend items
393 struct iter_prep_list {
395 struct iter_prep_list* next;
397 struct ub_packed_rrset_key* rrset;
401 * Get the iterator function block.
402 * @return: function block with function pointers to iterator methods.
404 struct module_func_block* iter_get_funcblock(void);
407 * Get iterator state as a string
408 * @param state: to convert
409 * @return constant string that is printable.
411 const char* iter_state_to_string(enum iter_state state);
414 * See if iterator state is a response state
415 * @param s: to inspect
416 * @return true if response state.
418 int iter_state_is_responsestate(enum iter_state s);
421 int iter_init(struct module_env* env, int id);
423 /** iterator deinit */
424 void iter_deinit(struct module_env* env, int id);
426 /** iterator operate on a query */
427 void iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
428 struct outbound_entry* outbound);
431 * Return priming query results to interestes super querystates.
433 * Sets the delegation point and delegation message (not nonRD queries).
434 * This is a callback from walk_supers.
436 * @param qstate: query state that finished.
437 * @param id: module id.
438 * @param super: the qstate to inform.
440 void iter_inform_super(struct module_qstate* qstate, int id,
441 struct module_qstate* super);
443 /** iterator cleanup query state */
444 void iter_clear(struct module_qstate* qstate, int id);
446 /** iterator alloc size routine */
447 size_t iter_get_mem(struct module_env* env, int id);
449 #endif /* ITERATOR_ITERATOR_H */