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