xref: /freebsd/contrib/unbound/iterator/iterator.h (revision 058ac3e8063366dafa634d9107642e12b038bf09)
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 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
61  * its subqueries */
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 query restarts. Determines max number of CNAME chain. */
67 #define MAX_RESTART_COUNT	11
68 /** max number of referrals. Makes sure resolver does not run away */
69 #define MAX_REFERRAL_COUNT	130
70 /** max number of queries-sent-out.  Make sure large NS set does not loop */
71 #define MAX_SENT_COUNT		32
72 /** max number of queries for which to perform dnsseclameness detection,
73  * (rrsigs missing detection) after that, just pick up that response */
74 #define DNSSEC_LAME_DETECT_COUNT 4
75 /**
76  * max number of QNAME minimisation iterations. Limits number of queries for
77  * QNAMEs with a lot of labels.
78 */
79 #define MAX_MINIMISE_COUNT	10
80 /* max number of time-outs for minimised query. Prevents resolving failures
81  * when the QNAME minimisation QTYPE is blocked. */
82 #define MAX_MINIMISE_TIMEOUT_COUNT 3
83 /**
84  * number of labels from QNAME that are always send individually when using
85  * QNAME minimisation, even when the number of labels of the QNAME is bigger
86  * than MAX_MINIMISE_COUNT */
87 #define MINIMISE_ONE_LAB	4
88 #define MINIMISE_MULTIPLE_LABS	(MAX_MINIMISE_COUNT - MINIMISE_ONE_LAB)
89 /** at what query-sent-count to stop target fetch policy */
90 #define TARGET_FETCH_STOP	3
91 /** how nice is a server without further information, in msec
92  * Equals rtt initial timeout value.
93  */
94 extern int UNKNOWN_SERVER_NICENESS;
95 /** maximum timeout before a host is deemed unsuitable, in msec.
96  * After host_ttl this will be timed out and the host will be tried again.
97  * Equals RTT_MAX_TIMEOUT, and thus when RTT_MAX_TIMEOUT is overwritten by
98  * config infra_cache_max_rtt, it will be overwritten as well. */
99 extern int USEFUL_SERVER_TOP_TIMEOUT;
100 /** penalty to validation failed blacklisted IPs
101  * Equals USEFUL_SERVER_TOP_TIMEOUT*4, and thus when RTT_MAX_TIMEOUT is
102  * overwritten by config infra_cache_max_rtt, it will be overwritten as well. */
103 extern int BLACKLIST_PENALTY;
104 /** RTT band, within this amount from the best, servers are chosen randomly.
105  * Chosen so that the UNKNOWN_SERVER_NICENESS falls within the band of a
106  * fast server, this causes server exploration as a side benefit. msec. */
107 #define RTT_BAND 400
108 
109 /**
110  * Global state for the iterator.
111  */
112 struct iter_env {
113 	/** A flag to indicate whether or not we have an IPv6 route */
114 	int supports_ipv6;
115 
116 	/** A flag to indicate whether or not we have an IPv4 route */
117 	int supports_ipv4;
118 
119 	/** A set of inetaddrs that should never be queried. */
120 	struct iter_donotq* donotq;
121 
122 	/** private address space and private domains */
123 	struct iter_priv* priv;
124 
125 	/** whitelist for capsforid names */
126 	struct rbtree_type* caps_white;
127 
128 	/** The maximum dependency depth that this resolver will pursue. */
129 	int max_dependency_depth;
130 
131 	/**
132 	 * The target fetch policy for each dependency level. This is
133 	 * described as a simple number (per dependency level):
134 	 *	negative numbers (usually just -1) mean fetch-all,
135 	 *	0 means only fetch on demand, and
136 	 *	positive numbers mean to fetch at most that many targets.
137 	 * array of max_dependency_depth+1 size.
138 	 */
139 	int* target_fetch_policy;
140 
141 	/** lock on ratelimit counter */
142 	lock_basic_type queries_ratelimit_lock;
143 	/** number of queries that have been ratelimited */
144 	size_t num_queries_ratelimited;
145 
146 	/** number of retries on outgoing queries */
147 	int outbound_msg_retry;
148 };
149 
150 /**
151  * QNAME minimisation state
152  */
153 enum minimisation_state {
154 	/**
155 	 * (Re)start minimisation. Outgoing QNAME should be set to dp->name.
156 	 * State entered on new query or after following referral or CNAME.
157 	 */
158 	INIT_MINIMISE_STATE = 0,
159 	/**
160 	 * QNAME minimisation ongoing. Increase QNAME on every iteration.
161 	 */
162 	MINIMISE_STATE,
163 	/**
164 	 * Don't increment QNAME this iteration
165 	 */
166 	SKIP_MINIMISE_STATE,
167 	/**
168 	 * Send out full QNAME + original QTYPE
169 	 */
170 	DONOT_MINIMISE_STATE,
171 };
172 
173 /**
174  * State of the iterator for a query.
175  */
176 enum iter_state {
177 	/**
178 	 * Externally generated queries start at this state. Query restarts are
179 	 * reset to this state.
180 	 */
181 	INIT_REQUEST_STATE = 0,
182 
183 	/**
184 	 * Root priming events reactivate here, most other events pass
185 	 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
186 	 */
187 	INIT_REQUEST_2_STATE,
188 
189 	/**
190 	 * Stub priming events reactivate here, most other events pass
191 	 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
192 	 */
193 	INIT_REQUEST_3_STATE,
194 
195 	/**
196 	 * Each time a delegation point changes for a given query or a
197 	 * query times out and/or wakes up, this state is (re)visited.
198 	 * This state is responsible for iterating through a list of
199 	 * nameserver targets.
200 	 */
201 	QUERYTARGETS_STATE,
202 
203 	/**
204 	 * Responses to queries start at this state. This state handles
205 	 * the decision tree associated with handling responses.
206 	 */
207 	QUERY_RESP_STATE,
208 
209 	/** Responses to priming queries finish at this state. */
210 	PRIME_RESP_STATE,
211 
212 	/** Collecting query class information, for qclass=ANY, when
213 	 * it spawns off queries for every class, it returns here. */
214 	COLLECT_CLASS_STATE,
215 
216 	/** Find NS record to resolve DS record from, walking to the right
217 	 * NS spot until we find it */
218 	DSNS_FIND_STATE,
219 
220 	/** Responses that are to be returned upstream end at this state.
221 	 * As well as responses to target queries. */
222 	FINISHED_STATE
223 };
224 
225 /**
226  * Shared counters for queries.
227  */
228 enum target_count_variables {
229 	/** Reference count for the shared iter_qstate->target_count. */
230 	TARGET_COUNT_REF = 0,
231 	/** Number of target queries spawned for the query and subqueries. */
232 	TARGET_COUNT_QUERIES,
233 	/** Number of nxdomain responses encountered. */
234 	TARGET_COUNT_NX,
235 
236 	/** This should stay last here, it is used for the allocation */
237 	TARGET_COUNT_MAX,
238 };
239 
240 /**
241  * Per query state for the iterator module.
242  */
243 struct iter_qstate {
244 	/**
245 	 * State of the iterator module.
246 	 * This is the state that event is in or should sent to -- all
247 	 * requests should start with the INIT_REQUEST_STATE. All
248 	 * responses should start with QUERY_RESP_STATE. Subsequent
249 	 * processing of the event will change this state.
250 	 */
251 	enum iter_state state;
252 
253 	/**
254 	 * Final state for the iterator module.
255 	 * This is the state that responses should be routed to once the
256 	 * response is final. For externally initiated queries, this
257 	 * will be FINISHED_STATE, locally initiated queries will have
258 	 * different final states.
259 	 */
260 	enum iter_state final_state;
261 
262 	/**
263 	 * The depth of this query, this means the depth of recursion.
264 	 * This address is needed for another query, which is an address
265 	 * needed for another query, etc. Original client query has depth 0.
266 	 */
267 	int depth;
268 
269 	/**
270 	 * The response
271 	 */
272 	struct dns_msg* response;
273 
274 	/**
275 	 * This is a list of RRsets that must be prepended to the
276 	 * ANSWER section of a response before being sent upstream.
277 	 */
278 	struct iter_prep_list* an_prepend_list;
279 	/** Last element of the prepend list */
280 	struct iter_prep_list* an_prepend_last;
281 
282 	/**
283 	 * This is the list of RRsets that must be prepended to the
284 	 * AUTHORITY section of the response before being sent upstream.
285 	 */
286 	struct iter_prep_list* ns_prepend_list;
287 	/** Last element of the authority prepend list */
288 	struct iter_prep_list* ns_prepend_last;
289 
290 	/** query name used for chasing the results. Initially the same as
291 	 * the state qinfo, but after CNAMEs this will be different.
292 	 * The query info used to elicit the results needed. */
293 	struct query_info qchase;
294 	/** query flags to use when chasing the answer (i.e. RD flag) */
295 	uint16_t chase_flags;
296 	/** true if we set RD bit because of last resort recursion lame query*/
297 	int chase_to_rd;
298 
299 	/**
300 	 * This is the current delegation point for an in-progress query. This
301 	 * object retains state as to which delegation targets need to be
302 	 * (sub)queried for vs which ones have already been visited.
303 	 */
304 	struct delegpt* dp;
305 
306 	/** state for 0x20 fallback when capsfail happens, 0 not a fallback */
307 	int caps_fallback;
308 	/** state for capsfail: current server number to try */
309 	size_t caps_server;
310 	/** state for capsfail: stored query for comparisons. Can be NULL if
311 	 * no response had been seen prior to starting the fallback. */
312 	struct reply_info* caps_reply;
313 	struct dns_msg* caps_response;
314 
315 	/** Current delegation message - returned for non-RD queries */
316 	struct dns_msg* deleg_msg;
317 
318 	/** number of outstanding target sub queries */
319 	int num_target_queries;
320 
321 	/** outstanding direct queries */
322 	int num_current_queries;
323 
324 	/** the number of times this query has been restarted. */
325 	int query_restart_count;
326 
327 	/** the number of times this query has followed a referral. */
328 	int referral_count;
329 
330 	/** number of queries fired off */
331 	int sent_count;
332 
333 	/** malloced-array shared with this query and its subqueries. It keeps
334 	 * track of the defined enum target_count_variables counters. */
335 	int* target_count;
336 
337 	/** number of target lookups per delegation point. Reset to 0 after
338 	 * receiving referral answer. Not shared with subqueries. */
339 	int dp_target_count;
340 
341 	/** Delegation point that triggered the NXNS fallback; shared with
342 	 * this query and its subqueries, count-referenced by the reference
343 	 * counter in target_count.
344 	 * This also marks the fallback activation. */
345 	uint8_t** nxns_dp;
346 
347 	/** if true, already tested for ratelimiting and passed the test */
348 	int ratelimit_ok;
349 
350 	/**
351 	 * The query must store NS records from referrals as parentside RRs
352 	 * Enabled once it hits resolution problems, to throttle retries.
353 	 * If enabled it is the pointer to the old delegation point with
354 	 * the old retry counts for bad-nameserver-addresses.
355 	 */
356 	struct delegpt* store_parent_NS;
357 
358 	/**
359 	 * The query is for parent-side glue(A or AAAA) for a nameserver.
360 	 * If the item is seen as glue in a referral, and pside_glue is NULL,
361 	 * then it is stored in pside_glue for later.
362 	 * If it was never seen, at the end, then a negative caching element
363 	 * must be created.
364 	 * The (data or negative) RR cache element then throttles retries.
365 	 */
366 	int query_for_pside_glue;
367 	/** the parent-side-glue element (NULL if none, its first match) */
368 	struct ub_packed_rrset_key* pside_glue;
369 
370 	/** If nonNULL we are walking upwards from DS query to find NS */
371 	uint8_t* dsns_point;
372 	/** length of the dname in dsns_point */
373 	size_t dsns_point_len;
374 
375 	/**
376 	 * expected dnssec information for this iteration step.
377 	 * If dnssec rrsigs are expected and not given, the server is marked
378 	 * lame (dnssec-lame).
379 	 */
380 	int dnssec_expected;
381 
382 	/**
383 	 * We are expecting dnssec information, but we also know the server
384 	 * is DNSSEC lame.  The response need not be marked dnssec-lame again.
385 	 */
386 	int dnssec_lame_query;
387 
388 	/**
389 	 * This is flag that, if true, means that this event is
390 	 * waiting for a stub priming query.
391 	 */
392 	int wait_priming_stub;
393 
394 	/**
395 	 * This is a flag that, if true, means that this query is
396 	 * for (re)fetching glue from a zone. Since the address should
397 	 * have been glue, query again to the servers that should have
398 	 * been returning it as glue.
399 	 * The delegation point must be set to the one that should *not*
400 	 * be used when creating the state. A higher one will be attempted.
401 	 */
402 	int refetch_glue;
403 
404 	/** list of pending queries to authoritative servers. */
405 	struct outbound_list outlist;
406 
407 	/** QNAME minimisation state, RFC9156 */
408 	enum minimisation_state minimisation_state;
409 
410 	/** State for capsfail: QNAME minimisation state for comparisons. */
411 	enum minimisation_state caps_minimisation_state;
412 
413 	/**
414 	 * The query info that is sent upstream. Will be a subset of qchase
415 	 * when qname minimisation is enabled.
416 	 */
417 	struct query_info qinfo_out;
418 
419 	/**
420 	 * Count number of QNAME minimisation iterations. Used to limit number of
421 	 * outgoing queries when QNAME minimisation is enabled.
422 	 */
423 	int minimise_count;
424 
425 	/**
426 	 * Count number of time-outs. Used to prevent resolving failures when
427 	 * the QNAME minimisation QTYPE is blocked. Used to determine if
428 	 * capsforid fallback should be started.*/
429 	int timeout_count;
430 
431 	/** True if the current response is from auth_zone */
432 	int auth_zone_response;
433 	/** True if the auth_zones should not be consulted for the query */
434 	int auth_zone_avoid;
435 	/** true if there have been scrubbing failures of reply packets */
436 	int scrub_failures;
437 	/** true if there have been parse failures of reply packets */
438 	int parse_failures;
439 	/** a failure printout address for last received answer */
440 	struct comm_reply* fail_reply;
441 };
442 
443 /**
444  * List of prepend items
445  */
446 struct iter_prep_list {
447 	/** next in list */
448 	struct iter_prep_list* next;
449 	/** rrset */
450 	struct ub_packed_rrset_key* rrset;
451 };
452 
453 /**
454  * Get the iterator function block.
455  * @return: function block with function pointers to iterator methods.
456  */
457 struct module_func_block* iter_get_funcblock(void);
458 
459 /**
460  * Get iterator state as a string
461  * @param state: to convert
462  * @return constant string that is printable.
463  */
464 const char* iter_state_to_string(enum iter_state state);
465 
466 /**
467  * See if iterator state is a response state
468  * @param s: to inspect
469  * @return true if response state.
470  */
471 int iter_state_is_responsestate(enum iter_state s);
472 
473 /** iterator init */
474 int iter_init(struct module_env* env, int id);
475 
476 /** iterator deinit */
477 void iter_deinit(struct module_env* env, int id);
478 
479 /** iterator operate on a query */
480 void iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
481 	struct outbound_entry* outbound);
482 
483 /**
484  * Return priming query results to interested super querystates.
485  *
486  * Sets the delegation point and delegation message (not nonRD queries).
487  * This is a callback from walk_supers.
488  *
489  * @param qstate: query state that finished.
490  * @param id: module id.
491  * @param super: the qstate to inform.
492  */
493 void iter_inform_super(struct module_qstate* qstate, int id,
494 	struct module_qstate* super);
495 
496 /** iterator cleanup query state */
497 void iter_clear(struct module_qstate* qstate, int id);
498 
499 /** iterator alloc size routine */
500 size_t iter_get_mem(struct module_env* env, int id);
501 
502 #endif /* ITERATOR_ITERATOR_H */
503