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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * 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 
55 /** max number of query restarts. Determines max number of CNAME chain. */
56 #define MAX_RESTART_COUNT       8
57 /** max number of referrals. Makes sure resolver does not run away */
58 #define MAX_REFERRAL_COUNT	130
59 /** max number of queries-sent-out.  Make sure large NS set does not loop */
60 #define MAX_SENT_COUNT		16
61 /** at what query-sent-count to stop target fetch policy */
62 #define TARGET_FETCH_STOP	3
63 /** how nice is a server without further information, in msec
64  * Equals rtt initial timeout value.
65  */
66 #define UNKNOWN_SERVER_NICENESS 376
67 /** maximum timeout before a host is deemed unsuitable, in msec.
68  * After host_ttl this will be timed out and the host will be tried again.
69  * Equals RTT_MAX_TIMEOUT
70  */
71 #define USEFUL_SERVER_TOP_TIMEOUT	120000
72 /** Number of lost messages in a row that get a host blacklisted.
73  * With 16, a couple different queries have to time out and no working
74  * queries are happening */
75 #define USEFUL_SERVER_MAX_LOST	16
76 /** number of retries on outgoing queries */
77 #define OUTBOUND_MSG_RETRY 5
78 /** RTT band, within this amount from the best, servers are chosen randomly.
79  * Chosen so that the UNKNOWN_SERVER_NICENESS falls within the band of a
80  * fast server, this causes server exploration as a side benefit. msec. */
81 #define RTT_BAND 400
82 /** Start value for blacklisting a host, 2*USEFUL_SERVER_TOP_TIMEOUT in sec */
83 #define INFRA_BACKOFF_INITIAL 240
84 
85 /**
86  * Global state for the iterator.
87  */
88 struct iter_env {
89 	/**
90 	 * The hints -- these aren't stored in the cache because they don't
91 	 * expire. The hints are always used to "prime" the cache. Note
92 	 * that both root hints and stub zone "hints" are stored in this
93 	 * data structure.
94 	 */
95 	struct iter_hints* hints;
96 
97 	/** A flag to indicate whether or not we have an IPv6 route */
98 	int supports_ipv6;
99 
100 	/** A flag to indicate whether or not we have an IPv4 route */
101 	int supports_ipv4;
102 
103 	/** A set of inetaddrs that should never be queried. */
104 	struct iter_donotq* donotq;
105 
106 	/** private address space and private domains */
107 	struct iter_priv* priv;
108 
109 	/** The maximum dependency depth that this resolver will pursue. */
110 	int max_dependency_depth;
111 
112 	/**
113 	 * The target fetch policy for each dependency level. This is
114 	 * described as a simple number (per dependency level):
115 	 *	negative numbers (usually just -1) mean fetch-all,
116 	 *	0 means only fetch on demand, and
117 	 *	positive numbers mean to fetch at most that many targets.
118 	 * array of max_dependency_depth+1 size.
119 	 */
120 	int* target_fetch_policy;
121 };
122 
123 /**
124  * State of the iterator for a query.
125  */
126 enum iter_state {
127 	/**
128 	 * Externally generated queries start at this state. Query restarts are
129 	 * reset to this state.
130 	 */
131 	INIT_REQUEST_STATE = 0,
132 
133 	/**
134 	 * Root priming events reactivate here, most other events pass
135 	 * through this naturally as the 2nd part of the INIT_REQUEST_STATE.
136 	 */
137 	INIT_REQUEST_2_STATE,
138 
139 	/**
140 	 * Stub priming events reactivate here, most other events pass
141 	 * through this naturally as the 3rd part of the INIT_REQUEST_STATE.
142 	 */
143 	INIT_REQUEST_3_STATE,
144 
145 	/**
146 	 * Each time a delegation point changes for a given query or a
147 	 * query times out and/or wakes up, this state is (re)visited.
148 	 * This state is reponsible for iterating through a list of
149 	 * nameserver targets.
150 	 */
151 	QUERYTARGETS_STATE,
152 
153 	/**
154 	 * Responses to queries start at this state. This state handles
155 	 * the decision tree associated with handling responses.
156 	 */
157 	QUERY_RESP_STATE,
158 
159 	/** Responses to priming queries finish at this state. */
160 	PRIME_RESP_STATE,
161 
162 	/** Collecting query class information, for qclass=ANY, when
163 	 * it spawns off queries for every class, it returns here. */
164 	COLLECT_CLASS_STATE,
165 
166 	/** Responses that are to be returned upstream end at this state.
167 	 * As well as responses to target queries. */
168 	FINISHED_STATE
169 };
170 
171 /**
172  * Per query state for the iterator module.
173  */
174 struct iter_qstate {
175 	/**
176 	 * State of the iterator module.
177 	 * This is the state that event is in or should sent to -- all
178 	 * requests should start with the INIT_REQUEST_STATE. All
179 	 * responses should start with QUERY_RESP_STATE. Subsequent
180 	 * processing of the event will change this state.
181 	 */
182 	enum iter_state state;
183 
184 	/**
185 	 * Final state for the iterator module.
186 	 * This is the state that responses should be routed to once the
187 	 * response is final. For externally initiated queries, this
188 	 * will be FINISHED_STATE, locally initiated queries will have
189 	 * different final states.
190 	 */
191 	enum iter_state final_state;
192 
193 	/**
194 	 * The depth of this query, this means the depth of recursion.
195 	 * This address is needed for another query, which is an address
196 	 * needed for another query, etc. Original client query has depth 0.
197 	 */
198 	int depth;
199 
200 	/**
201 	 * The response
202 	 */
203 	struct dns_msg* response;
204 
205 	/**
206 	 * This is a list of RRsets that must be prepended to the
207 	 * ANSWER section of a response before being sent upstream.
208 	 */
209 	struct iter_prep_list* an_prepend_list;
210 	/** Last element of the prepend list */
211 	struct iter_prep_list* an_prepend_last;
212 
213 	/**
214 	 * This is the list of RRsets that must be prepended to the
215 	 * AUTHORITY section of the response before being sent upstream.
216 	 */
217 	struct iter_prep_list* ns_prepend_list;
218 	/** Last element of the authority prepend list */
219 	struct iter_prep_list* ns_prepend_last;
220 
221 	/** query name used for chasing the results. Initially the same as
222 	 * the state qinfo, but after CNAMEs this will be different.
223 	 * The query info used to elicit the results needed. */
224 	struct query_info qchase;
225 	/** query flags to use when chasing the answer (i.e. RD flag) */
226 	uint16_t chase_flags;
227 	/** true if we set RD bit because of last resort recursion lame query*/
228 	int chase_to_rd;
229 
230 	/**
231 	 * This is the current delegation point for an in-progress query. This
232 	 * object retains state as to which delegation targets need to be
233 	 * (sub)queried for vs which ones have already been visited.
234 	 */
235 	struct delegpt* dp;
236 
237 	/** state for 0x20 fallback when capsfail happens, 0 not a fallback */
238 	int caps_fallback;
239 	/** state for capsfail: current server number to try */
240 	size_t caps_server;
241 	/** state for capsfail: stored query for comparisons */
242 	struct reply_info* caps_reply;
243 
244 	/** Current delegation message - returned for non-RD queries */
245 	struct dns_msg* deleg_msg;
246 
247 	/** number of outstanding target sub queries */
248 	int num_target_queries;
249 
250 	/** outstanding direct queries */
251 	int num_current_queries;
252 
253 	/** the number of times this query has been restarted. */
254 	int query_restart_count;
255 
256 	/** the number of times this query as followed a referral. */
257 	int referral_count;
258 
259 	/** number of queries fired off */
260 	int sent_count;
261 
262 	/**
263 	 * The query must store NS records from referrals as parentside RRs
264 	 * Enabled once it hits resolution problems, to throttle retries.
265 	 * If enabled it is the pointer to the old delegation point with
266 	 * the old retry counts for bad-nameserver-addresses.
267 	 */
268 	struct delegpt* store_parent_NS;
269 
270 	/**
271 	 * The query is for parent-side glue(A or AAAA) for a nameserver.
272 	 * If the item is seen as glue in a referral, and pside_glue is NULL,
273 	 * then it is stored in pside_glue for later.
274 	 * If it was never seen, at the end, then a negative caching element
275 	 * must be created.
276 	 * The (data or negative) RR cache element then throttles retries.
277 	 */
278 	int query_for_pside_glue;
279 	/** the parent-side-glue element (NULL if none, its first match) */
280 	struct ub_packed_rrset_key* pside_glue;
281 
282 	/**
283 	 * expected dnssec information for this iteration step.
284 	 * If dnssec rrsigs are expected and not given, the server is marked
285 	 * lame (dnssec-lame).
286 	 */
287 	int dnssec_expected;
288 
289 	/**
290 	 * We are expecting dnssec information, but we also know the server
291 	 * is DNSSEC lame.  The response need not be marked dnssec-lame again.
292 	 */
293 	int dnssec_lame_query;
294 
295 	/**
296 	 * This is flag that, if true, means that this event is
297 	 * waiting for a stub priming query.
298 	 */
299 	int wait_priming_stub;
300 
301 	/**
302 	 * This is a flag that, if true, means that this query is
303 	 * for (re)fetching glue from a zone. Since the address should
304 	 * have been glue, query again to the servers that should have
305 	 * been returning it as glue.
306 	 * The delegation point must be set to the one that should *not*
307 	 * be used when creating the state. A higher one will be attempted.
308 	 */
309 	int refetch_glue;
310 
311 	/** list of pending queries to authoritative servers. */
312 	struct outbound_list outlist;
313 };
314 
315 /**
316  * List of prepend items
317  */
318 struct iter_prep_list {
319 	/** next in list */
320 	struct iter_prep_list* next;
321 	/** rrset */
322 	struct ub_packed_rrset_key* rrset;
323 };
324 
325 /**
326  * Get the iterator function block.
327  * @return: function block with function pointers to iterator methods.
328  */
329 struct module_func_block* iter_get_funcblock(void);
330 
331 /**
332  * Get iterator state as a string
333  * @param state: to convert
334  * @return constant string that is printable.
335  */
336 const char* iter_state_to_string(enum iter_state state);
337 
338 /**
339  * See if iterator state is a response state
340  * @param s: to inspect
341  * @return true if response state.
342  */
343 int iter_state_is_responsestate(enum iter_state s);
344 
345 /** iterator init */
346 int iter_init(struct module_env* env, int id);
347 
348 /** iterator deinit */
349 void iter_deinit(struct module_env* env, int id);
350 
351 /** iterator operate on a query */
352 void iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
353 	struct outbound_entry* outbound);
354 
355 /**
356  * Return priming query results to interestes super querystates.
357  *
358  * Sets the delegation point and delegation message (not nonRD queries).
359  * This is a callback from walk_supers.
360  *
361  * @param qstate: query state that finished.
362  * @param id: module id.
363  * @param super: the qstate to inform.
364  */
365 void iter_inform_super(struct module_qstate* qstate, int id,
366 	struct module_qstate* super);
367 
368 /** iterator cleanup query state */
369 void iter_clear(struct module_qstate* qstate, int id);
370 
371 /** iterator alloc size routine */
372 size_t iter_get_mem(struct module_env* env, int id);
373 
374 #endif /* ITERATOR_ITERATOR_H */
375