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