xref: /freebsd/contrib/unbound/iterator/iterator.h (revision 5685098846d7f11ad642d9804d94dc7429a7b212)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * iterator/iterator.h - iterative resolver DNS query response module
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
398a384985SDag-Erling Smørgrav  * This file contains a module that performs recursive iterative DNS query
40b7579f77SDag-Erling Smørgrav  * processing.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #ifndef ITERATOR_ITERATOR_H
44b7579f77SDag-Erling Smørgrav #define ITERATOR_ITERATOR_H
45b7579f77SDag-Erling Smørgrav #include "services/outbound_list.h"
46b7579f77SDag-Erling Smørgrav #include "util/data/msgreply.h"
47b7579f77SDag-Erling Smørgrav #include "util/module.h"
48b7579f77SDag-Erling Smørgrav struct delegpt;
49b7579f77SDag-Erling Smørgrav struct iter_hints;
50b7579f77SDag-Erling Smørgrav struct iter_forwards;
51b7579f77SDag-Erling Smørgrav struct iter_donotq;
52b7579f77SDag-Erling Smørgrav struct iter_prep_list;
53b7579f77SDag-Erling Smørgrav struct iter_priv;
543005e0a3SDag-Erling Smørgrav struct rbtree_type;
55b7579f77SDag-Erling Smørgrav 
5652df462fSXin LI /** max number of targets spawned for a query and its subqueries */
57b75612f8SDag-Erling Smørgrav #define MAX_TARGET_COUNT	64
58*56850988SCy Schubert /** max number of upstream queries for a query and its subqueries, it is
59*56850988SCy Schubert  * never reset. */
60*56850988SCy Schubert #define MAX_GLOBAL_QUOTA	128
61091e9e46SCy Schubert /** max number of target lookups per qstate, per delegation point */
62091e9e46SCy Schubert #define MAX_DP_TARGET_COUNT	16
63091e9e46SCy Schubert /** max number of nxdomains allowed for target lookups for a query and
64091e9e46SCy Schubert  * its subqueries */
65091e9e46SCy Schubert #define MAX_TARGET_NX		5
660a92a9fcSCy Schubert /** max number of nxdomains allowed for target lookups for a query and
670a92a9fcSCy Schubert  * its subqueries when fallback has kicked in */
680a92a9fcSCy Schubert #define MAX_TARGET_NX_FALLBACK	(MAX_TARGET_NX*2)
69b7579f77SDag-Erling Smørgrav /** max number of referrals. Makes sure resolver does not run away */
70b7579f77SDag-Erling Smørgrav #define MAX_REFERRAL_COUNT	130
71e2d15004SDag-Erling Smørgrav /** max number of queries for which to perform dnsseclameness detection,
728a384985SDag-Erling Smørgrav  * (rrsigs missing detection) after that, just pick up that response */
73e2d15004SDag-Erling Smørgrav #define DNSSEC_LAME_DETECT_COUNT 4
74e2d15004SDag-Erling Smørgrav /**
75e2d15004SDag-Erling Smørgrav  * max number of QNAME minimisation iterations. Limits number of queries for
76e2d15004SDag-Erling Smørgrav  * QNAMEs with a lot of labels.
77e2d15004SDag-Erling Smørgrav */
78e2d15004SDag-Erling Smørgrav #define MAX_MINIMISE_COUNT	10
79b5663de9SDag-Erling Smørgrav /* max number of time-outs for minimised query. Prevents resolving failures
80b5663de9SDag-Erling Smørgrav  * when the QNAME minimisation QTYPE is blocked. */
81b5663de9SDag-Erling Smørgrav #define MAX_MINIMISE_TIMEOUT_COUNT 3
82e2d15004SDag-Erling Smørgrav /**
83e2d15004SDag-Erling Smørgrav  * number of labels from QNAME that are always send individually when using
84e2d15004SDag-Erling Smørgrav  * QNAME minimisation, even when the number of labels of the QNAME is bigger
859cf5bc93SCy Schubert  * than MAX_MINIMISE_COUNT */
86e2d15004SDag-Erling Smørgrav #define MINIMISE_ONE_LAB	4
87e2d15004SDag-Erling Smørgrav #define MINIMISE_MULTIPLE_LABS	(MAX_MINIMISE_COUNT - MINIMISE_ONE_LAB)
88b7579f77SDag-Erling Smørgrav /** at what query-sent-count to stop target fetch policy */
89b7579f77SDag-Erling Smørgrav #define TARGET_FETCH_STOP	3
90b7579f77SDag-Erling Smørgrav /** how nice is a server without further information, in msec
91b7579f77SDag-Erling Smørgrav  * Equals rtt initial timeout value.
92b7579f77SDag-Erling Smørgrav  */
93e86b9096SDag-Erling Smørgrav extern int UNKNOWN_SERVER_NICENESS;
94b7579f77SDag-Erling Smørgrav /** maximum timeout before a host is deemed unsuitable, in msec.
95b7579f77SDag-Erling Smørgrav  * After host_ttl this will be timed out and the host will be tried again.
96790c6b24SCy Schubert  * Equals RTT_MAX_TIMEOUT, and thus when RTT_MAX_TIMEOUT is overwritten by
97790c6b24SCy Schubert  * config infra_cache_max_rtt, it will be overwritten as well. */
98790c6b24SCy Schubert extern int USEFUL_SERVER_TOP_TIMEOUT;
99790c6b24SCy Schubert /** penalty to validation failed blacklisted IPs
100790c6b24SCy Schubert  * Equals USEFUL_SERVER_TOP_TIMEOUT*4, and thus when RTT_MAX_TIMEOUT is
101790c6b24SCy Schubert  * overwritten by config infra_cache_max_rtt, it will be overwritten as well. */
102790c6b24SCy Schubert extern int BLACKLIST_PENALTY;
103b7579f77SDag-Erling Smørgrav /** RTT band, within this amount from the best, servers are chosen randomly.
104b7579f77SDag-Erling Smørgrav  * Chosen so that the UNKNOWN_SERVER_NICENESS falls within the band of a
105b7579f77SDag-Erling Smørgrav  * fast server, this causes server exploration as a side benefit. msec. */
106b7579f77SDag-Erling Smørgrav #define RTT_BAND 400
107103ba509SCy Schubert /** Number of retries for empty nodata packets before it is accepted. */
108103ba509SCy Schubert #define EMPTY_NODATA_RETRY_COUNT 2
109b7579f77SDag-Erling Smørgrav 
110b7579f77SDag-Erling Smørgrav /**
111b7579f77SDag-Erling Smørgrav  * Global state for the iterator.
112b7579f77SDag-Erling Smørgrav  */
113b7579f77SDag-Erling Smørgrav struct iter_env {
114b7579f77SDag-Erling Smørgrav 	/** A flag to indicate whether or not we have an IPv6 route */
115b7579f77SDag-Erling Smørgrav 	int supports_ipv6;
116b7579f77SDag-Erling Smørgrav 
117b7579f77SDag-Erling Smørgrav 	/** A flag to indicate whether or not we have an IPv4 route */
118b7579f77SDag-Erling Smørgrav 	int supports_ipv4;
119b7579f77SDag-Erling Smørgrav 
1208f76bb7dSCy Schubert 	/** A flag to locally apply NAT64 to make IPv4 addrs into IPv6 */
1218f76bb7dSCy Schubert 	int use_nat64;
1228f76bb7dSCy Schubert 
1238f76bb7dSCy Schubert 	/** NAT64 prefix address, cf. dns64_env->prefix_addr */
1248f76bb7dSCy Schubert 	struct sockaddr_storage nat64_prefix_addr;
1258f76bb7dSCy Schubert 
1268f76bb7dSCy Schubert 	/** sizeof(sockaddr_in6) */
1278f76bb7dSCy Schubert 	socklen_t nat64_prefix_addrlen;
1288f76bb7dSCy Schubert 
1298f76bb7dSCy Schubert 	/** CIDR mask length of NAT64 prefix */
1308f76bb7dSCy Schubert 	int nat64_prefix_net;
1318f76bb7dSCy Schubert 
132b7579f77SDag-Erling Smørgrav 	/** A set of inetaddrs that should never be queried. */
133b7579f77SDag-Erling Smørgrav 	struct iter_donotq* donotq;
134b7579f77SDag-Erling Smørgrav 
135b7579f77SDag-Erling Smørgrav 	/** private address space and private domains */
136b7579f77SDag-Erling Smørgrav 	struct iter_priv* priv;
137b7579f77SDag-Erling Smørgrav 
13809a3aaf3SDag-Erling Smørgrav 	/** whitelist for capsforid names */
1393005e0a3SDag-Erling Smørgrav 	struct rbtree_type* caps_white;
14009a3aaf3SDag-Erling Smørgrav 
141b7579f77SDag-Erling Smørgrav 	/** The maximum dependency depth that this resolver will pursue. */
142b7579f77SDag-Erling Smørgrav 	int max_dependency_depth;
143b7579f77SDag-Erling Smørgrav 
144b7579f77SDag-Erling Smørgrav 	/**
145b7579f77SDag-Erling Smørgrav 	 * The target fetch policy for each dependency level. This is
146b7579f77SDag-Erling Smørgrav 	 * described as a simple number (per dependency level):
147b7579f77SDag-Erling Smørgrav 	 *	negative numbers (usually just -1) mean fetch-all,
148b7579f77SDag-Erling Smørgrav 	 *	0 means only fetch on demand, and
149b7579f77SDag-Erling Smørgrav 	 *	positive numbers mean to fetch at most that many targets.
150b7579f77SDag-Erling Smørgrav 	 * array of max_dependency_depth+1 size.
151b7579f77SDag-Erling Smørgrav 	 */
152b7579f77SDag-Erling Smørgrav 	int* target_fetch_policy;
15305ab2901SDag-Erling Smørgrav 
154971980c3SDag-Erling Smørgrav 	/** lock on ratelimit counter */
155971980c3SDag-Erling Smørgrav 	lock_basic_type queries_ratelimit_lock;
156971980c3SDag-Erling Smørgrav 	/** number of queries that have been ratelimited */
157971980c3SDag-Erling Smørgrav 	size_t num_queries_ratelimited;
15824e36522SCy Schubert 
15924e36522SCy Schubert 	/** number of retries on outgoing queries */
16024e36522SCy Schubert 	int outbound_msg_retry;
1611838dec3SCy Schubert 
1621838dec3SCy Schubert 	/** number of queries_sent */
1631838dec3SCy Schubert 	int max_sent_count;
1641838dec3SCy Schubert 
1651838dec3SCy Schubert 	/** max number of query restarts to limit length of CNAME chain */
1661838dec3SCy Schubert 	int max_query_restarts;
16705ab2901SDag-Erling Smørgrav };
16805ab2901SDag-Erling Smørgrav 
16905ab2901SDag-Erling Smørgrav /**
17005ab2901SDag-Erling Smørgrav  * QNAME minimisation state
17105ab2901SDag-Erling Smørgrav  */
17205ab2901SDag-Erling Smørgrav enum minimisation_state {
17305ab2901SDag-Erling Smørgrav 	/**
17405ab2901SDag-Erling Smørgrav 	 * (Re)start minimisation. Outgoing QNAME should be set to dp->name.
1758a384985SDag-Erling Smørgrav 	 * State entered on new query or after following referral or CNAME.
17605ab2901SDag-Erling Smørgrav 	 */
17705ab2901SDag-Erling Smørgrav 	INIT_MINIMISE_STATE = 0,
17805ab2901SDag-Erling Smørgrav 	/**
1798a384985SDag-Erling Smørgrav 	 * QNAME minimisation ongoing. Increase QNAME on every iteration.
18005ab2901SDag-Erling Smørgrav 	 */
18105ab2901SDag-Erling Smørgrav 	MINIMISE_STATE,
18205ab2901SDag-Erling Smørgrav 	/**
18305ab2901SDag-Erling Smørgrav 	 * Don't increment QNAME this iteration
18405ab2901SDag-Erling Smørgrav 	 */
18505ab2901SDag-Erling Smørgrav 	SKIP_MINIMISE_STATE,
18605ab2901SDag-Erling Smørgrav 	/**
18705ab2901SDag-Erling Smørgrav 	 * Send out full QNAME + original QTYPE
18805ab2901SDag-Erling Smørgrav 	 */
18905ab2901SDag-Erling Smørgrav 	DONOT_MINIMISE_STATE,
190b7579f77SDag-Erling Smørgrav };
191b7579f77SDag-Erling Smørgrav 
192b7579f77SDag-Erling Smørgrav /**
193b7579f77SDag-Erling Smørgrav  * State of the iterator for a query.
194b7579f77SDag-Erling Smørgrav  */
195b7579f77SDag-Erling Smørgrav enum iter_state {
196b7579f77SDag-Erling Smørgrav 	/**
197b7579f77SDag-Erling Smørgrav 	 * Externally generated queries start at this state. Query restarts are
198b7579f77SDag-Erling Smørgrav 	 * reset to this state.
199b7579f77SDag-Erling Smørgrav 	 */
200b7579f77SDag-Erling Smørgrav 	INIT_REQUEST_STATE = 0,
201b7579f77SDag-Erling Smørgrav 
202b7579f77SDag-Erling Smørgrav 	/**
203b7579f77SDag-Erling Smørgrav 	 * Root priming events reactivate here, most other events pass
204b7579f77SDag-Erling Smørgrav 	 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
205b7579f77SDag-Erling Smørgrav 	 */
206b7579f77SDag-Erling Smørgrav 	INIT_REQUEST_2_STATE,
207b7579f77SDag-Erling Smørgrav 
208b7579f77SDag-Erling Smørgrav 	/**
209b7579f77SDag-Erling Smørgrav 	 * Stub priming events reactivate here, most other events pass
210b7579f77SDag-Erling Smørgrav 	 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
211b7579f77SDag-Erling Smørgrav 	 */
212b7579f77SDag-Erling Smørgrav 	INIT_REQUEST_3_STATE,
213b7579f77SDag-Erling Smørgrav 
214b7579f77SDag-Erling Smørgrav 	/**
215b7579f77SDag-Erling Smørgrav 	 * Each time a delegation point changes for a given query or a
216b7579f77SDag-Erling Smørgrav 	 * query times out and/or wakes up, this state is (re)visited.
2178a384985SDag-Erling Smørgrav 	 * This state is responsible for iterating through a list of
218b7579f77SDag-Erling Smørgrav 	 * nameserver targets.
219b7579f77SDag-Erling Smørgrav 	 */
220b7579f77SDag-Erling Smørgrav 	QUERYTARGETS_STATE,
221b7579f77SDag-Erling Smørgrav 
222b7579f77SDag-Erling Smørgrav 	/**
223b7579f77SDag-Erling Smørgrav 	 * Responses to queries start at this state. This state handles
224b7579f77SDag-Erling Smørgrav 	 * the decision tree associated with handling responses.
225b7579f77SDag-Erling Smørgrav 	 */
226b7579f77SDag-Erling Smørgrav 	QUERY_RESP_STATE,
227b7579f77SDag-Erling Smørgrav 
228b7579f77SDag-Erling Smørgrav 	/** Responses to priming queries finish at this state. */
229b7579f77SDag-Erling Smørgrav 	PRIME_RESP_STATE,
230b7579f77SDag-Erling Smørgrav 
231b7579f77SDag-Erling Smørgrav 	/** Collecting query class information, for qclass=ANY, when
232b7579f77SDag-Erling Smørgrav 	 * it spawns off queries for every class, it returns here. */
233b7579f77SDag-Erling Smørgrav 	COLLECT_CLASS_STATE,
234b7579f77SDag-Erling Smørgrav 
235b7579f77SDag-Erling Smørgrav 	/** Find NS record to resolve DS record from, walking to the right
236b7579f77SDag-Erling Smørgrav 	 * NS spot until we find it */
237b7579f77SDag-Erling Smørgrav 	DSNS_FIND_STATE,
238b7579f77SDag-Erling Smørgrav 
239b7579f77SDag-Erling Smørgrav 	/** Responses that are to be returned upstream end at this state.
240b7579f77SDag-Erling Smørgrav 	 * As well as responses to target queries. */
241b7579f77SDag-Erling Smørgrav 	FINISHED_STATE
242b7579f77SDag-Erling Smørgrav };
243b7579f77SDag-Erling Smørgrav 
244b7579f77SDag-Erling Smørgrav /**
2450a92a9fcSCy Schubert  * Shared counters for queries.
2460a92a9fcSCy Schubert  */
2470a92a9fcSCy Schubert enum target_count_variables {
2480a92a9fcSCy Schubert 	/** Reference count for the shared iter_qstate->target_count. */
2490a92a9fcSCy Schubert 	TARGET_COUNT_REF = 0,
2500a92a9fcSCy Schubert 	/** Number of target queries spawned for the query and subqueries. */
2510a92a9fcSCy Schubert 	TARGET_COUNT_QUERIES,
2520a92a9fcSCy Schubert 	/** Number of nxdomain responses encountered. */
2530a92a9fcSCy Schubert 	TARGET_COUNT_NX,
254*56850988SCy Schubert 	/** Global quota on number of queries to upstream servers per
255*56850988SCy Schubert 	 * client request, that is never reset. */
256*56850988SCy Schubert 	TARGET_COUNT_GLOBAL_QUOTA,
2570a92a9fcSCy Schubert 
2580a92a9fcSCy Schubert 	/** This should stay last here, it is used for the allocation */
2590a92a9fcSCy Schubert 	TARGET_COUNT_MAX,
2600a92a9fcSCy Schubert };
2610a92a9fcSCy Schubert 
2620a92a9fcSCy Schubert /**
263b7579f77SDag-Erling Smørgrav  * Per query state for the iterator module.
264b7579f77SDag-Erling Smørgrav  */
265b7579f77SDag-Erling Smørgrav struct iter_qstate {
266b7579f77SDag-Erling Smørgrav 	/**
267b7579f77SDag-Erling Smørgrav 	 * State of the iterator module.
268b7579f77SDag-Erling Smørgrav 	 * This is the state that event is in or should sent to -- all
269b7579f77SDag-Erling Smørgrav 	 * requests should start with the INIT_REQUEST_STATE. All
270b7579f77SDag-Erling Smørgrav 	 * responses should start with QUERY_RESP_STATE. Subsequent
271b7579f77SDag-Erling Smørgrav 	 * processing of the event will change this state.
272b7579f77SDag-Erling Smørgrav 	 */
273b7579f77SDag-Erling Smørgrav 	enum iter_state state;
274b7579f77SDag-Erling Smørgrav 
275b7579f77SDag-Erling Smørgrav 	/**
276b7579f77SDag-Erling Smørgrav 	 * Final state for the iterator module.
277b7579f77SDag-Erling Smørgrav 	 * This is the state that responses should be routed to once the
278b7579f77SDag-Erling Smørgrav 	 * response is final. For externally initiated queries, this
279b7579f77SDag-Erling Smørgrav 	 * will be FINISHED_STATE, locally initiated queries will have
280b7579f77SDag-Erling Smørgrav 	 * different final states.
281b7579f77SDag-Erling Smørgrav 	 */
282b7579f77SDag-Erling Smørgrav 	enum iter_state final_state;
283b7579f77SDag-Erling Smørgrav 
284b7579f77SDag-Erling Smørgrav 	/**
285b7579f77SDag-Erling Smørgrav 	 * The depth of this query, this means the depth of recursion.
286b7579f77SDag-Erling Smørgrav 	 * This address is needed for another query, which is an address
287b7579f77SDag-Erling Smørgrav 	 * needed for another query, etc. Original client query has depth 0.
288b7579f77SDag-Erling Smørgrav 	 */
289b7579f77SDag-Erling Smørgrav 	int depth;
290b7579f77SDag-Erling Smørgrav 
291b7579f77SDag-Erling Smørgrav 	/**
292b7579f77SDag-Erling Smørgrav 	 * The response
293b7579f77SDag-Erling Smørgrav 	 */
294b7579f77SDag-Erling Smørgrav 	struct dns_msg* response;
295b7579f77SDag-Erling Smørgrav 
296b7579f77SDag-Erling Smørgrav 	/**
297b7579f77SDag-Erling Smørgrav 	 * This is a list of RRsets that must be prepended to the
298b7579f77SDag-Erling Smørgrav 	 * ANSWER section of a response before being sent upstream.
299b7579f77SDag-Erling Smørgrav 	 */
300b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* an_prepend_list;
301b7579f77SDag-Erling Smørgrav 	/** Last element of the prepend list */
302b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* an_prepend_last;
303b7579f77SDag-Erling Smørgrav 
304b7579f77SDag-Erling Smørgrav 	/**
305b7579f77SDag-Erling Smørgrav 	 * This is the list of RRsets that must be prepended to the
306b7579f77SDag-Erling Smørgrav 	 * AUTHORITY section of the response before being sent upstream.
307b7579f77SDag-Erling Smørgrav 	 */
308b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* ns_prepend_list;
309b7579f77SDag-Erling Smørgrav 	/** Last element of the authority prepend list */
310b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* ns_prepend_last;
311b7579f77SDag-Erling Smørgrav 
312b7579f77SDag-Erling Smørgrav 	/** query name used for chasing the results. Initially the same as
313b7579f77SDag-Erling Smørgrav 	 * the state qinfo, but after CNAMEs this will be different.
314b7579f77SDag-Erling Smørgrav 	 * The query info used to elicit the results needed. */
315b7579f77SDag-Erling Smørgrav 	struct query_info qchase;
316b7579f77SDag-Erling Smørgrav 	/** query flags to use when chasing the answer (i.e. RD flag) */
317b7579f77SDag-Erling Smørgrav 	uint16_t chase_flags;
318b7579f77SDag-Erling Smørgrav 	/** true if we set RD bit because of last resort recursion lame query*/
319b7579f77SDag-Erling Smørgrav 	int chase_to_rd;
320b7579f77SDag-Erling Smørgrav 
321b7579f77SDag-Erling Smørgrav 	/**
322b7579f77SDag-Erling Smørgrav 	 * This is the current delegation point for an in-progress query. This
323b7579f77SDag-Erling Smørgrav 	 * object retains state as to which delegation targets need to be
324b7579f77SDag-Erling Smørgrav 	 * (sub)queried for vs which ones have already been visited.
325b7579f77SDag-Erling Smørgrav 	 */
326b7579f77SDag-Erling Smørgrav 	struct delegpt* dp;
327b7579f77SDag-Erling Smørgrav 
328b7579f77SDag-Erling Smørgrav 	/** state for 0x20 fallback when capsfail happens, 0 not a fallback */
329b7579f77SDag-Erling Smørgrav 	int caps_fallback;
330b7579f77SDag-Erling Smørgrav 	/** state for capsfail: current server number to try */
331b7579f77SDag-Erling Smørgrav 	size_t caps_server;
332ff825849SDag-Erling Smørgrav 	/** state for capsfail: stored query for comparisons. Can be NULL if
333ff825849SDag-Erling Smørgrav 	 * no response had been seen prior to starting the fallback. */
334b7579f77SDag-Erling Smørgrav 	struct reply_info* caps_reply;
33509a3aaf3SDag-Erling Smørgrav 	struct dns_msg* caps_response;
336b7579f77SDag-Erling Smørgrav 
337b7579f77SDag-Erling Smørgrav 	/** Current delegation message - returned for non-RD queries */
338b7579f77SDag-Erling Smørgrav 	struct dns_msg* deleg_msg;
339b7579f77SDag-Erling Smørgrav 
340b7579f77SDag-Erling Smørgrav 	/** number of outstanding target sub queries */
341b7579f77SDag-Erling Smørgrav 	int num_target_queries;
342b7579f77SDag-Erling Smørgrav 
343b7579f77SDag-Erling Smørgrav 	/** outstanding direct queries */
344b7579f77SDag-Erling Smørgrav 	int num_current_queries;
345b7579f77SDag-Erling Smørgrav 
346b7579f77SDag-Erling Smørgrav 	/** the number of times this query has been restarted. */
347b7579f77SDag-Erling Smørgrav 	int query_restart_count;
348b7579f77SDag-Erling Smørgrav 
349865f46b2SCy Schubert 	/** the number of times this query has followed a referral. */
350b7579f77SDag-Erling Smørgrav 	int referral_count;
351b7579f77SDag-Erling Smørgrav 
352b7579f77SDag-Erling Smørgrav 	/** number of queries fired off */
353b7579f77SDag-Erling Smørgrav 	int sent_count;
354b7579f77SDag-Erling Smørgrav 
3550a92a9fcSCy Schubert 	/** malloced-array shared with this query and its subqueries. It keeps
3560a92a9fcSCy Schubert 	 * track of the defined enum target_count_variables counters. */
35752df462fSXin LI 	int* target_count;
35852df462fSXin LI 
359091e9e46SCy Schubert 	/** number of target lookups per delegation point. Reset to 0 after
360091e9e46SCy Schubert 	 * receiving referral answer. Not shared with subqueries. */
361091e9e46SCy Schubert 	int dp_target_count;
362091e9e46SCy Schubert 
3630a92a9fcSCy Schubert 	/** Delegation point that triggered the NXNS fallback; shared with
3640a92a9fcSCy Schubert 	 * this query and its subqueries, count-referenced by the reference
3650a92a9fcSCy Schubert 	 * counter in target_count.
3660a92a9fcSCy Schubert 	 * This also marks the fallback activation. */
3670a92a9fcSCy Schubert 	uint8_t** nxns_dp;
3680a92a9fcSCy Schubert 
36909a3aaf3SDag-Erling Smørgrav 	/** if true, already tested for ratelimiting and passed the test */
37009a3aaf3SDag-Erling Smørgrav 	int ratelimit_ok;
37109a3aaf3SDag-Erling Smørgrav 
372b7579f77SDag-Erling Smørgrav 	/**
373b7579f77SDag-Erling Smørgrav 	 * The query must store NS records from referrals as parentside RRs
374b7579f77SDag-Erling Smørgrav 	 * Enabled once it hits resolution problems, to throttle retries.
375b7579f77SDag-Erling Smørgrav 	 * If enabled it is the pointer to the old delegation point with
376b7579f77SDag-Erling Smørgrav 	 * the old retry counts for bad-nameserver-addresses.
377b7579f77SDag-Erling Smørgrav 	 */
378b7579f77SDag-Erling Smørgrav 	struct delegpt* store_parent_NS;
379b7579f77SDag-Erling Smørgrav 
380b7579f77SDag-Erling Smørgrav 	/**
381b7579f77SDag-Erling Smørgrav 	 * The query is for parent-side glue(A or AAAA) for a nameserver.
382b7579f77SDag-Erling Smørgrav 	 * If the item is seen as glue in a referral, and pside_glue is NULL,
383b7579f77SDag-Erling Smørgrav 	 * then it is stored in pside_glue for later.
384b7579f77SDag-Erling Smørgrav 	 * If it was never seen, at the end, then a negative caching element
385b7579f77SDag-Erling Smørgrav 	 * must be created.
386b7579f77SDag-Erling Smørgrav 	 * The (data or negative) RR cache element then throttles retries.
387b7579f77SDag-Erling Smørgrav 	 */
388b7579f77SDag-Erling Smørgrav 	int query_for_pside_glue;
389b7579f77SDag-Erling Smørgrav 	/** the parent-side-glue element (NULL if none, its first match) */
390b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* pside_glue;
391b7579f77SDag-Erling Smørgrav 
392b7579f77SDag-Erling Smørgrav 	/** If nonNULL we are walking upwards from DS query to find NS */
393b7579f77SDag-Erling Smørgrav 	uint8_t* dsns_point;
394b7579f77SDag-Erling Smørgrav 	/** length of the dname in dsns_point */
395b7579f77SDag-Erling Smørgrav 	size_t dsns_point_len;
396b7579f77SDag-Erling Smørgrav 
397b7579f77SDag-Erling Smørgrav 	/**
398b7579f77SDag-Erling Smørgrav 	 * expected dnssec information for this iteration step.
399b7579f77SDag-Erling Smørgrav 	 * If dnssec rrsigs are expected and not given, the server is marked
400b7579f77SDag-Erling Smørgrav 	 * lame (dnssec-lame).
401b7579f77SDag-Erling Smørgrav 	 */
402b7579f77SDag-Erling Smørgrav 	int dnssec_expected;
403b7579f77SDag-Erling Smørgrav 
404b7579f77SDag-Erling Smørgrav 	/**
405b7579f77SDag-Erling Smørgrav 	 * We are expecting dnssec information, but we also know the server
406b7579f77SDag-Erling Smørgrav 	 * is DNSSEC lame.  The response need not be marked dnssec-lame again.
407b7579f77SDag-Erling Smørgrav 	 */
408b7579f77SDag-Erling Smørgrav 	int dnssec_lame_query;
409b7579f77SDag-Erling Smørgrav 
410b7579f77SDag-Erling Smørgrav 	/**
411b7579f77SDag-Erling Smørgrav 	 * This is flag that, if true, means that this event is
412b7579f77SDag-Erling Smørgrav 	 * waiting for a stub priming query.
413b7579f77SDag-Erling Smørgrav 	 */
414b7579f77SDag-Erling Smørgrav 	int wait_priming_stub;
415b7579f77SDag-Erling Smørgrav 
416b7579f77SDag-Erling Smørgrav 	/**
417b7579f77SDag-Erling Smørgrav 	 * This is a flag that, if true, means that this query is
418b7579f77SDag-Erling Smørgrav 	 * for (re)fetching glue from a zone. Since the address should
419b7579f77SDag-Erling Smørgrav 	 * have been glue, query again to the servers that should have
420b7579f77SDag-Erling Smørgrav 	 * been returning it as glue.
421b7579f77SDag-Erling Smørgrav 	 * The delegation point must be set to the one that should *not*
422b7579f77SDag-Erling Smørgrav 	 * be used when creating the state. A higher one will be attempted.
423b7579f77SDag-Erling Smørgrav 	 */
424b7579f77SDag-Erling Smørgrav 	int refetch_glue;
425b7579f77SDag-Erling Smørgrav 
426103ba509SCy Schubert 	/**
427103ba509SCy Schubert 	 * This flag detects that a completely empty nodata was received,
428103ba509SCy Schubert 	 * already so that it is accepted later. */
429103ba509SCy Schubert 	int empty_nodata_found;
430103ba509SCy Schubert 
431b7579f77SDag-Erling Smørgrav 	/** list of pending queries to authoritative servers. */
432b7579f77SDag-Erling Smørgrav 	struct outbound_list outlist;
43305ab2901SDag-Erling Smørgrav 
4349cf5bc93SCy Schubert 	/** QNAME minimisation state, RFC9156 */
43505ab2901SDag-Erling Smørgrav 	enum minimisation_state minimisation_state;
43605ab2901SDag-Erling Smørgrav 
4374c75e3aaSDag-Erling Smørgrav 	/** State for capsfail: QNAME minimisation state for comparisons. */
4384c75e3aaSDag-Erling Smørgrav 	enum minimisation_state caps_minimisation_state;
4394c75e3aaSDag-Erling Smørgrav 
44005ab2901SDag-Erling Smørgrav 	/**
44105ab2901SDag-Erling Smørgrav 	 * The query info that is sent upstream. Will be a subset of qchase
44205ab2901SDag-Erling Smørgrav 	 * when qname minimisation is enabled.
44305ab2901SDag-Erling Smørgrav 	 */
44405ab2901SDag-Erling Smørgrav 	struct query_info qinfo_out;
445e2d15004SDag-Erling Smørgrav 
446e2d15004SDag-Erling Smørgrav 	/**
4478a384985SDag-Erling Smørgrav 	 * Count number of QNAME minimisation iterations. Used to limit number of
448e2d15004SDag-Erling Smørgrav 	 * outgoing queries when QNAME minimisation is enabled.
449e2d15004SDag-Erling Smørgrav 	 */
450e2d15004SDag-Erling Smørgrav 	int minimise_count;
451b5663de9SDag-Erling Smørgrav 
452b5663de9SDag-Erling Smørgrav 	/**
453b5663de9SDag-Erling Smørgrav 	 * Count number of time-outs. Used to prevent resolving failures when
45425039b37SCy Schubert 	 * the QNAME minimisation QTYPE is blocked. Used to determine if
45525039b37SCy Schubert 	 * capsforid fallback should be started.*/
45625039b37SCy Schubert 	int timeout_count;
45757bddd21SDag-Erling Smørgrav 
45857bddd21SDag-Erling Smørgrav 	/** True if the current response is from auth_zone */
45957bddd21SDag-Erling Smørgrav 	int auth_zone_response;
46057bddd21SDag-Erling Smørgrav 	/** True if the auth_zones should not be consulted for the query */
46157bddd21SDag-Erling Smørgrav 	int auth_zone_avoid;
4625469a995SCy Schubert 	/** true if there have been scrubbing failures of reply packets */
4635469a995SCy Schubert 	int scrub_failures;
4645469a995SCy Schubert 	/** true if there have been parse failures of reply packets */
4655469a995SCy Schubert 	int parse_failures;
4665469a995SCy Schubert 	/** a failure printout address for last received answer */
4678f76bb7dSCy Schubert 	union {
4688f76bb7dSCy Schubert 		struct in_addr in;
4698f76bb7dSCy Schubert #ifdef AF_INET6
4708f76bb7dSCy Schubert 		struct in6_addr in6;
4718f76bb7dSCy Schubert #endif
4728f76bb7dSCy Schubert 	} fail_addr;
4738f76bb7dSCy Schubert 	/** which fail_addr, 0 is nothing, 4 or 6 */
4748f76bb7dSCy Schubert 	int fail_addr_type;
475b7579f77SDag-Erling Smørgrav };
476b7579f77SDag-Erling Smørgrav 
477b7579f77SDag-Erling Smørgrav /**
478b7579f77SDag-Erling Smørgrav  * List of prepend items
479b7579f77SDag-Erling Smørgrav  */
480b7579f77SDag-Erling Smørgrav struct iter_prep_list {
481b7579f77SDag-Erling Smørgrav 	/** next in list */
482b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* next;
483b7579f77SDag-Erling Smørgrav 	/** rrset */
484b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset;
485b7579f77SDag-Erling Smørgrav };
486b7579f77SDag-Erling Smørgrav 
487b7579f77SDag-Erling Smørgrav /**
488b7579f77SDag-Erling Smørgrav  * Get the iterator function block.
489b7579f77SDag-Erling Smørgrav  * @return: function block with function pointers to iterator methods.
490b7579f77SDag-Erling Smørgrav  */
491b7579f77SDag-Erling Smørgrav struct module_func_block* iter_get_funcblock(void);
492b7579f77SDag-Erling Smørgrav 
493b7579f77SDag-Erling Smørgrav /**
494b7579f77SDag-Erling Smørgrav  * Get iterator state as a string
495b7579f77SDag-Erling Smørgrav  * @param state: to convert
496b7579f77SDag-Erling Smørgrav  * @return constant string that is printable.
497b7579f77SDag-Erling Smørgrav  */
498b7579f77SDag-Erling Smørgrav const char* iter_state_to_string(enum iter_state state);
499b7579f77SDag-Erling Smørgrav 
500b7579f77SDag-Erling Smørgrav /**
501b7579f77SDag-Erling Smørgrav  * See if iterator state is a response state
502b7579f77SDag-Erling Smørgrav  * @param s: to inspect
503b7579f77SDag-Erling Smørgrav  * @return true if response state.
504b7579f77SDag-Erling Smørgrav  */
505b7579f77SDag-Erling Smørgrav int iter_state_is_responsestate(enum iter_state s);
506b7579f77SDag-Erling Smørgrav 
507b7579f77SDag-Erling Smørgrav /** iterator init */
508b7579f77SDag-Erling Smørgrav int iter_init(struct module_env* env, int id);
509b7579f77SDag-Erling Smørgrav 
510b7579f77SDag-Erling Smørgrav /** iterator deinit */
511b7579f77SDag-Erling Smørgrav void iter_deinit(struct module_env* env, int id);
512b7579f77SDag-Erling Smørgrav 
513b7579f77SDag-Erling Smørgrav /** iterator operate on a query */
514b7579f77SDag-Erling Smørgrav void iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
515b7579f77SDag-Erling Smørgrav 	struct outbound_entry* outbound);
516b7579f77SDag-Erling Smørgrav 
517b7579f77SDag-Erling Smørgrav /**
5188a384985SDag-Erling Smørgrav  * Return priming query results to interested super querystates.
519b7579f77SDag-Erling Smørgrav  *
520b7579f77SDag-Erling Smørgrav  * Sets the delegation point and delegation message (not nonRD queries).
521b7579f77SDag-Erling Smørgrav  * This is a callback from walk_supers.
522b7579f77SDag-Erling Smørgrav  *
523b7579f77SDag-Erling Smørgrav  * @param qstate: query state that finished.
524b7579f77SDag-Erling Smørgrav  * @param id: module id.
525b7579f77SDag-Erling Smørgrav  * @param super: the qstate to inform.
526b7579f77SDag-Erling Smørgrav  */
527b7579f77SDag-Erling Smørgrav void iter_inform_super(struct module_qstate* qstate, int id,
528b7579f77SDag-Erling Smørgrav 	struct module_qstate* super);
529b7579f77SDag-Erling Smørgrav 
530b7579f77SDag-Erling Smørgrav /** iterator cleanup query state */
531b7579f77SDag-Erling Smørgrav void iter_clear(struct module_qstate* qstate, int id);
532b7579f77SDag-Erling Smørgrav 
533b7579f77SDag-Erling Smørgrav /** iterator alloc size routine */
534b7579f77SDag-Erling Smørgrav size_t iter_get_mem(struct module_env* env, int id);
535b7579f77SDag-Erling Smørgrav 
536b7579f77SDag-Erling Smørgrav #endif /* ITERATOR_ITERATOR_H */
537