xref: /freebsd/contrib/unbound/iterator/iterator.h (revision ccdc986607d3a12def360e6c6d01b44f64cb0868)
1 /*
2  * iterator/iterator.h - iterative resolver DNS query response module
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 a module that performs recursive iterative DNS query
40  * processing.
41  */
42 
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"
48 struct delegpt;
49 struct iter_hints;
50 struct iter_forwards;
51 struct iter_donotq;
52 struct iter_prep_list;
53 struct iter_priv;
54 struct rbtree_type;
55 
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
67 /**
68  * max number of QNAME minimisation iterations. Limits number of queries for
69  * QNAMEs with a lot of labels.
70 */
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
75 /**
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.
85  */
86 extern int UNKNOWN_SERVER_NICENESS;
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
90  */
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. */
97 #define RTT_BAND 400
98 /** Start value for blacklisting a host, 2*USEFUL_SERVER_TOP_TIMEOUT in sec */
99 #define INFRA_BACKOFF_INITIAL 240
100 
101 /**
102  * Global state for the iterator.
103  */
104 struct iter_env {
105 	/** A flag to indicate whether or not we have an IPv6 route */
106 	int supports_ipv6;
107 
108 	/** A flag to indicate whether or not we have an IPv4 route */
109 	int supports_ipv4;
110 
111 	/** A set of inetaddrs that should never be queried. */
112 	struct iter_donotq* donotq;
113 
114 	/** private address space and private domains */
115 	struct iter_priv* priv;
116 
117 	/** whitelist for capsforid names */
118 	struct rbtree_type* caps_white;
119 
120 	/** The maximum dependency depth that this resolver will pursue. */
121 	int max_dependency_depth;
122 
123 	/**
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.
130 	 */
131 	int* target_fetch_policy;
132 
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;
137 };
138 
139 /**
140  * QNAME minimisation state
141  */
142 enum minimisation_state {
143 	/**
144 	 * (Re)start minimisation. Outgoing QNAME should be set to dp->name.
145 	 * State entered on new query or after following referral or CNAME.
146 	 */
147 	INIT_MINIMISE_STATE = 0,
148 	/**
149 	 * QNAME minimisation ongoing. Increase QNAME on every iteration.
150 	 */
151 	MINIMISE_STATE,
152 	/**
153 	 * Don't increment QNAME this iteration
154 	 */
155 	SKIP_MINIMISE_STATE,
156 	/**
157 	 * Send out full QNAME + original QTYPE
158 	 */
159 	DONOT_MINIMISE_STATE,
160 };
161 
162 /**
163  * State of the iterator for a query.
164  */
165 enum iter_state {
166 	/**
167 	 * Externally generated queries start at this state. Query restarts are
168 	 * reset to this state.
169 	 */
170 	INIT_REQUEST_STATE = 0,
171 
172 	/**
173 	 * Root priming events reactivate here, most other events pass
174 	 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
175 	 */
176 	INIT_REQUEST_2_STATE,
177 
178 	/**
179 	 * Stub priming events reactivate here, most other events pass
180 	 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
181 	 */
182 	INIT_REQUEST_3_STATE,
183 
184 	/**
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.
189 	 */
190 	QUERYTARGETS_STATE,
191 
192 	/**
193 	 * Responses to queries start at this state. This state handles
194 	 * the decision tree associated with handling responses.
195 	 */
196 	QUERY_RESP_STATE,
197 
198 	/** Responses to priming queries finish at this state. */
199 	PRIME_RESP_STATE,
200 
201 	/** Collecting query class information, for qclass=ANY, when
202 	 * it spawns off queries for every class, it returns here. */
203 	COLLECT_CLASS_STATE,
204 
205 	/** Find NS record to resolve DS record from, walking to the right
206 	 * NS spot until we find it */
207 	DSNS_FIND_STATE,
208 
209 	/** Responses that are to be returned upstream end at this state.
210 	 * As well as responses to target queries. */
211 	FINISHED_STATE
212 };
213 
214 /**
215  * Per query state for the iterator module.
216  */
217 struct iter_qstate {
218 	/**
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.
224 	 */
225 	enum iter_state state;
226 
227 	/**
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.
233 	 */
234 	enum iter_state final_state;
235 
236 	/**
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.
240 	 */
241 	int depth;
242 
243 	/**
244 	 * The response
245 	 */
246 	struct dns_msg* response;
247 
248 	/**
249 	 * This is a list of RRsets that must be prepended to the
250 	 * ANSWER section of a response before being sent upstream.
251 	 */
252 	struct iter_prep_list* an_prepend_list;
253 	/** Last element of the prepend list */
254 	struct iter_prep_list* an_prepend_last;
255 
256 	/**
257 	 * This is the list of RRsets that must be prepended to the
258 	 * AUTHORITY section of the response before being sent upstream.
259 	 */
260 	struct iter_prep_list* ns_prepend_list;
261 	/** Last element of the authority prepend list */
262 	struct iter_prep_list* ns_prepend_last;
263 
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*/
271 	int chase_to_rd;
272 
273 	/**
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.
277 	 */
278 	struct delegpt* dp;
279 
280 	/** state for 0x20 fallback when capsfail happens, 0 not a fallback */
281 	int caps_fallback;
282 	/** state for capsfail: current server number to try */
283 	size_t caps_server;
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;
288 
289 	/** Current delegation message - returned for non-RD queries */
290 	struct dns_msg* deleg_msg;
291 
292 	/** number of outstanding target sub queries */
293 	int num_target_queries;
294 
295 	/** outstanding direct queries */
296 	int num_current_queries;
297 
298 	/** the number of times this query has been restarted. */
299 	int query_restart_count;
300 
301 	/** the number of times this query as followed a referral. */
302 	int referral_count;
303 
304 	/** number of queries fired off */
305 	int sent_count;
306 
307 	/** number of target queries spawned in [1], for this query and its
308 	 * subqueries, the malloced-array is shared, [0] refcount. */
309 	int* target_count;
310 
311 	/** if true, already tested for ratelimiting and passed the test */
312 	int ratelimit_ok;
313 
314 	/**
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.
319 	 */
320 	struct delegpt* store_parent_NS;
321 
322 	/**
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
327 	 * must be created.
328 	 * The (data or negative) RR cache element then throttles retries.
329 	 */
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;
333 
334 	/** If nonNULL we are walking upwards from DS query to find NS */
335 	uint8_t* dsns_point;
336 	/** length of the dname in dsns_point */
337 	size_t dsns_point_len;
338 
339 	/**
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).
343 	 */
344 	int dnssec_expected;
345 
346 	/**
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.
349 	 */
350 	int dnssec_lame_query;
351 
352 	/**
353 	 * This is flag that, if true, means that this event is
354 	 * waiting for a stub priming query.
355 	 */
356 	int wait_priming_stub;
357 
358 	/**
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.
365 	 */
366 	int refetch_glue;
367 
368 	/** list of pending queries to authoritative servers. */
369 	struct outbound_list outlist;
370 
371 	/** QNAME minimisation state, RFC7816 */
372 	enum minimisation_state minimisation_state;
373 
374 	/** State for capsfail: QNAME minimisation state for comparisons. */
375 	enum minimisation_state caps_minimisation_state;
376 
377 	/**
378 	 * The query info that is sent upstream. Will be a subset of qchase
379 	 * when qname minimisation is enabled.
380 	 */
381 	struct query_info qinfo_out;
382 
383 	/**
384 	 * Count number of QNAME minimisation iterations. Used to limit number of
385 	 * outgoing queries when QNAME minimisation is enabled.
386 	 */
387 	int minimise_count;
388 
389 	/**
390 	 * Count number of time-outs. Used to prevent resolving failures when
391 	 * the QNAME minimisation QTYPE is blocked. */
392 	int minimise_timeout_count;
393 
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 */
397 	int auth_zone_avoid;
398 };
399 
400 /**
401  * List of prepend items
402  */
403 struct iter_prep_list {
404 	/** next in list */
405 	struct iter_prep_list* next;
406 	/** rrset */
407 	struct ub_packed_rrset_key* rrset;
408 };
409 
410 /**
411  * Get the iterator function block.
412  * @return: function block with function pointers to iterator methods.
413  */
414 struct module_func_block* iter_get_funcblock(void);
415 
416 /**
417  * Get iterator state as a string
418  * @param state: to convert
419  * @return constant string that is printable.
420  */
421 const char* iter_state_to_string(enum iter_state state);
422 
423 /**
424  * See if iterator state is a response state
425  * @param s: to inspect
426  * @return true if response state.
427  */
428 int iter_state_is_responsestate(enum iter_state s);
429 
430 /** iterator init */
431 int iter_init(struct module_env* env, int id);
432 
433 /** iterator deinit */
434 void iter_deinit(struct module_env* env, int id);
435 
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);
439 
440 /**
441  * Return priming query results to interested super querystates.
442  *
443  * Sets the delegation point and delegation message (not nonRD queries).
444  * This is a callback from walk_supers.
445  *
446  * @param qstate: query state that finished.
447  * @param id: module id.
448  * @param super: the qstate to inform.
449  */
450 void iter_inform_super(struct module_qstate* qstate, int id,
451 	struct module_qstate* super);
452 
453 /** iterator cleanup query state */
454 void iter_clear(struct module_qstate* qstate, int id);
455 
456 /** iterator alloc size routine */
457 size_t iter_get_mem(struct module_env* env, int id);
458 
459 #endif /* ITERATOR_ITERATOR_H */
460