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