xref: /freebsd/contrib/unbound/iterator/iter_utils.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*
2  * iterator/iter_utils.h - iterative resolver module utility functions.
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 functions to assist the iterator module.
40  * Configuration options. Forward zones.
41  */
42 
43 #ifndef ITERATOR_ITER_UTILS_H
44 #define ITERATOR_ITER_UTILS_H
45 #include "iterator/iter_resptype.h"
46 struct sldns_buffer;
47 struct iter_env;
48 struct iter_hints;
49 struct iter_forwards;
50 struct config_file;
51 struct module_env;
52 struct delegpt_addr;
53 struct delegpt;
54 struct regional;
55 struct msg_parse;
56 struct ub_randstate;
57 struct query_info;
58 struct reply_info;
59 struct module_qstate;
60 struct sock_list;
61 struct ub_packed_rrset_key;
62 
63 /**
64  * Process config options and set iterator module state.
65  * Sets default values if no config is found.
66  * @param iter_env: iterator module state.
67  * @param cfg: config options.
68  * @return 0 on error.
69  */
70 int iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg);
71 
72 /**
73  * Select a valid, nice target to send query to.
74  * Sorting and removing unsuitable targets is combined.
75  *
76  * @param iter_env: iterator module global state, with ip6 enabled and
77  *	do-not-query-addresses.
78  * @param env: environment with infra cache (lameness, rtt info).
79  * @param dp: delegation point with result list.
80  * @param name: zone name (for lameness check).
81  * @param namelen: length of name.
82  * @param qtype: query type that we want to send.
83  * @param dnssec_lame: set to 1, if a known dnssec-lame server is selected
84  *	these are not preferred, but are used as a last resort.
85  * @param chase_to_rd: set to 1 if a known recursion lame server is selected
86  * 	these are not preferred, but are used as a last resort.
87  * @param open_target: number of currently outstanding target queries.
88  * 	If we wait for these, perhaps more server addresses become available.
89  * @param blacklist: the IP blacklist to use.
90  * @param prefetch: if not 0, prefetch is in use for this query.
91  * 	This means the query can have different timing, because prefetch is
92  * 	not waited upon by the downstream client, and thus a good time to
93  * 	perform exploration of other targets.
94  * @return best target or NULL if no target.
95  *	if not null, that target is removed from the result list in the dp.
96  */
97 struct delegpt_addr* iter_server_selection(struct iter_env* iter_env,
98 	struct module_env* env, struct delegpt* dp, uint8_t* name,
99 	size_t namelen, uint16_t qtype, int* dnssec_lame,
100 	int* chase_to_rd, int open_target, struct sock_list* blacklist,
101 	time_t prefetch);
102 
103 /**
104  * Allocate dns_msg from parsed msg, in regional.
105  * @param pkt: packet.
106  * @param msg: parsed message (cleaned and ready for regional allocation).
107  * @param regional: regional to use for allocation.
108  * @return newly allocated dns_msg, or NULL on memory error.
109  */
110 struct dns_msg* dns_alloc_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
111 	struct regional* regional);
112 
113 /**
114  * Copy a dns_msg to this regional.
115  * @param from: dns message, also in regional.
116  * @param regional: regional to use for allocation.
117  * @return newly allocated dns_msg, or NULL on memory error.
118  */
119 struct dns_msg* dns_copy_msg(struct dns_msg* from, struct regional* regional);
120 
121 /**
122  * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
123  * @param env: environment, with alloc structure and dns cache.
124  * @param qinf: query info, the query for which answer is stored.
125  * @param rep: reply in dns_msg from dns_alloc_msg for example.
126  * @param is_referral: If true, then the given message to be stored is a
127  *	referral. The cache implementation may use this as a hint.
128  * @param leeway: prefetch TTL leeway to expire old rrsets quicker.
129  * @param pside: true if dp is parentside, thus message is 'fresh' and NS
130  * 	can be prefetch-updates.
131  * @param region: to copy modified (cache is better) rrs back to.
132  * @param flags: with BIT_CD for dns64 AAAA translated queries.
133  * @return void, because we are not interested in alloc errors,
134  * 	the iterator and validator can operate on the results in their
135  * 	scratch space (the qstate.region) and are not dependent on the cache.
136  * 	It is useful to log the alloc failure (for the server operator),
137  * 	but the query resolution can continue without cache storage.
138  */
139 void iter_dns_store(struct module_env* env, struct query_info* qinf,
140 	struct reply_info* rep, int is_referral, time_t leeway, int pside,
141 	struct regional* region, uint16_t flags);
142 
143 /**
144  * Select randomly with n/m probability.
145  * For shuffle NS records for address fetching.
146  * @param rnd: random table
147  * @param n: probability.
148  * @param m: divisor for probability.
149  * @return true with n/m probability.
150  */
151 int iter_ns_probability(struct ub_randstate* rnd, int n, int m);
152 
153 /**
154  * Mark targets that result in a dependency cycle as done, so they
155  * will not get selected as targets.
156  * @param qstate: query state.
157  * @param dp: delegpt to mark ns in.
158  */
159 void iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp);
160 
161 /**
162  * Mark targets that result in a dependency cycle as done, so they
163  * will not get selected as targets.  For the parent-side lookups.
164  * @param qstate: query state.
165  * @param dp: delegpt to mark ns in.
166  */
167 void iter_mark_pside_cycle_targets(struct module_qstate* qstate,
168 	struct delegpt* dp);
169 
170 /**
171  * See if delegation is useful or offers immediately no targets for
172  * further recursion.
173  * @param qinfo: query name and type
174  * @param qflags: query flags with RD flag
175  * @param dp: delegpt to check.
176  * @return true if dp is useless.
177  */
178 int iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
179 	struct delegpt* dp);
180 
181 /**
182  * See if qname has DNSSEC needs.  This is true if there is a trust anchor above
183  * it.  Whether there is an insecure delegation to the data is unknown.
184  * @param env: environment with anchors.
185  * @param qinfo: query name and class.
186  * @return true if trust anchor above qname, false if no anchor or insecure
187  * point above qname.
188  */
189 int iter_qname_indicates_dnssec(struct module_env* env,
190 	struct query_info *qinfo);
191 
192 /**
193  * See if delegation is expected to have DNSSEC information (RRSIGs) in
194  * its answers, or not. Inspects delegation point (name), trust anchors,
195  * and delegation message (DS RRset) to determine this.
196  * @param env: module env with trust anchors.
197  * @param dp: delegation point.
198  * @param msg: delegation message, with DS if a secure referral.
199  * @param dclass: class of query.
200  * @return 1 if dnssec is expected, 0 if not or insecure point above qname.
201  */
202 int iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
203 	struct dns_msg* msg, uint16_t dclass);
204 
205 /**
206  * See if a message contains DNSSEC.
207  * This is examined by looking for RRSIGs. With DNSSEC a valid answer,
208  * nxdomain, nodata, referral or cname reply has RRSIGs in answer or auth
209  * sections, sigs on answer data, SOA, DS, or NSEC/NSEC3 records.
210  * @param msg: message to examine.
211  * @return true if DNSSEC information was found.
212  */
213 int iter_msg_has_dnssec(struct dns_msg* msg);
214 
215 /**
216  * See if a message is known to be from a certain zone.
217  * This looks for SOA or NS rrsets, for answers.
218  * For referrals, when one label is delegated, the zone is detected.
219  * Does not look at signatures.
220  * @param msg: the message to inspect.
221  * @param dp: delegation point with zone name to look for.
222  * @param type: type of message.
223  * @param dclass: class of query.
224  * @return true if message is certain to be from zone in dp->name.
225  *	false if not sure (empty msg), or not from the zone.
226  */
227 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
228 	enum response_type type, uint16_t dclass);
229 
230 /**
231  * Check if two replies are equal
232  * For fallback procedures
233  * @param p: reply one. The reply has rrset data pointers in region.
234  * 	Does not check rrset-IDs
235  * @param q: reply two
236  * @param region: scratch buffer.
237  * @return if one and two are equal.
238  */
239 int reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region);
240 
241 /**
242  * Remove unused bits from the reply if possible.
243  * So that caps-for-id (0x20) fallback is more likely to be successful.
244  * This removes like, the additional section, and NS record in the authority
245  * section if those records are gratuitous (not for a referral).
246  * @param rep: the reply to strip stuff out of.
247  */
248 void caps_strip_reply(struct reply_info* rep);
249 
250 /**
251  * see if reply has a 'useful' rcode for capsforid comparison, so
252  * not SERVFAIL or REFUSED, and thus NOERROR or NXDOMAIN.
253  * @param rep: reply to check.
254  * @return true if the rcode is a bad type of message.
255  */
256 int caps_failed_rcode(struct reply_info* rep);
257 
258 /**
259  * Store parent-side rrset in separate rrset cache entries for later
260  * last-resort * lookups in case the child-side versions of this information
261  * fails.
262  * @param env: environment with cache, time, ...
263  * @param rrset: the rrset to store (copied).
264  * Failure to store is logged, but otherwise ignored.
265  */
266 void iter_store_parentside_rrset(struct module_env* env,
267 	struct ub_packed_rrset_key* rrset);
268 
269 /**
270  * Store parent-side NS records from a referral message
271  * @param env: environment with cache, time, ...
272  * @param rep: response with NS rrset.
273  * Failure to store is logged, but otherwise ignored.
274  */
275 void iter_store_parentside_NS(struct module_env* env, struct reply_info* rep);
276 
277 /**
278  * Store parent-side negative element, the parentside rrset does not exist,
279  * creates an rrset with empty rdata in the rrset cache with PARENTSIDE flag.
280  * @param env: environment with cache, time, ...
281  * @param qinfo: the identity of the rrset that is missing.
282  * @param rep: delegation response or answer response, to glean TTL from.
283  * (malloc) failure is logged but otherwise ignored.
284  */
285 void iter_store_parentside_neg(struct module_env* env,
286 	struct query_info* qinfo, struct reply_info* rep);
287 
288 /**
289  * Add parent NS record if that exists in the cache.  This is both new
290  * information and acts like a timeout throttle on retries.
291  * @param env: query env with rrset cache and time.
292  * @param dp: delegation point to store result in.  Also this dp is used to
293  *	see which NS name is needed.
294  * @param region: region to alloc result in.
295  * @param qinfo: pertinent information, the qclass.
296  * @return false on malloc failure.
297  *	if true, the routine worked and if such cached information
298  *	existed dp->has_parent_side_NS is set true.
299  */
300 int iter_lookup_parent_NS_from_cache(struct module_env* env,
301 	struct delegpt* dp, struct regional* region, struct query_info* qinfo);
302 
303 /**
304  * Add parent-side glue if that exists in the cache.  This is both new
305  * information and acts like a timeout throttle on retries to fetch them.
306  * @param env: query env with rrset cache and time.
307  * @param dp: delegation point to store result in.  Also this dp is used to
308  *	see which NS name is needed.
309  * @param region: region to alloc result in.
310  * @param qinfo: pertinent information, the qclass.
311  * @return: true, it worked, no malloc failures, and new addresses (lame)
312  *	have been added, giving extra options as query targets.
313  */
314 int iter_lookup_parent_glue_from_cache(struct module_env* env,
315 	struct delegpt* dp, struct regional* region, struct query_info* qinfo);
316 
317 /**
318  * Lookup next root-hint or root-forward entry.
319  * @param hints: the hints.
320  * @param fwd: the forwards.
321  * @param c: the class to start searching at. 0 means find first one.
322  * @return false if no classes found, true if found and returned in c.
323  */
324 int iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
325 	uint16_t* c);
326 
327 /**
328  * Remove DS records that are inappropriate before they are cached.
329  * @param msg: the response to scrub.
330  * @param ns: RRSET that is the NS record for the referral.
331  * 	if NULL, then all DS records are removed from the authority section.
332  * @param z: zone name that the response is from.
333  */
334 void iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns,
335 	uint8_t* z);
336 
337 /**
338  * Prepare an NXDOMAIN message to be used for a subdomain answer by removing all
339  * RRs from the ANSWER section.
340  * @param msg: the response to scrub.
341  */
342 void iter_scrub_nxdomain(struct dns_msg* msg);
343 
344 /**
345  * Remove query attempts from all available ips. For 0x20.
346  * @param dp: delegpt.
347  * @param d: decrease.
348  */
349 void iter_dec_attempts(struct delegpt* dp, int d);
350 
351 /**
352  * Add retry counts from older delegpt to newer delegpt.
353  * Does not waste time on timeout'd (or other failing) addresses.
354  * @param dp: new delegationpoint.
355  * @param old: old delegationpoint.
356  */
357 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old);
358 
359 /**
360  * See if a DS response (type ANSWER) is too low: a nodata answer with
361  * a SOA record in the authority section at-or-below the qchase.qname.
362  * Also returns true if we are not sure (i.e. empty message, CNAME nosig).
363  * @param msg: the response.
364  * @param dp: the dp name is used to check if the RRSIG gives a clue that
365  * 	it was originated from the correct nameserver.
366  * @return true if too low.
367  */
368 int iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp);
369 
370 /**
371  * See if delegpt can go down a step to the qname or not
372  * @param qinfo: the query name looked up.
373  * @param dp: checked if the name can go lower to the qname
374  * @return true if can go down, false if that would not be possible.
375  * the current response seems to be the one and only, best possible, response.
376  */
377 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp);
378 
379 /**
380  * Lookup if no_cache is set in stub or fwd.
381  * @param qstate: query state with env with hints and fwds.
382  * @param qinf: query name to lookup for.
383  * @return true if no_cache is set in stub or fwd.
384  */
385 int iter_stub_fwd_no_cache(struct module_qstate *qstate,
386 	struct query_info *qinf);
387 
388 #endif /* ITERATOR_ITER_UTILS_H */
389