/src/unbound/iterator/iter_utils.c
Line | Count | Source |
1 | | /* |
2 | | * iterator/iter_utils.c - 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 | | #include "config.h" |
43 | | #include "iterator/iter_utils.h" |
44 | | #include "iterator/iterator.h" |
45 | | #include "iterator/iter_hints.h" |
46 | | #include "iterator/iter_fwd.h" |
47 | | #include "iterator/iter_donotq.h" |
48 | | #include "iterator/iter_delegpt.h" |
49 | | #include "iterator/iter_priv.h" |
50 | | #include "services/cache/infra.h" |
51 | | #include "services/cache/dns.h" |
52 | | #include "services/cache/rrset.h" |
53 | | #include "services/outside_network.h" |
54 | | #include "util/net_help.h" |
55 | | #include "util/module.h" |
56 | | #include "util/log.h" |
57 | | #include "util/config_file.h" |
58 | | #include "util/regional.h" |
59 | | #include "util/data/msgparse.h" |
60 | | #include "util/data/dname.h" |
61 | | #include "util/random.h" |
62 | | #include "util/fptr_wlist.h" |
63 | | #include "validator/val_anchor.h" |
64 | | #include "validator/val_kcache.h" |
65 | | #include "validator/val_kentry.h" |
66 | | #include "validator/val_utils.h" |
67 | | #include "validator/val_sigcrypt.h" |
68 | | #include "sldns/sbuffer.h" |
69 | | #include "sldns/str2wire.h" |
70 | | |
71 | | /** time when nameserver glue is said to be 'recent' */ |
72 | | #define SUSPICION_RECENT_EXPIRY 86400 |
73 | | |
74 | | /** if NAT64 is enabled and no NAT64 prefix is configured, first fall back to |
75 | | * DNS64 prefix. If that is not configured, fall back to this default value. |
76 | | */ |
77 | | static const char DEFAULT_NAT64_PREFIX[] = "64:ff9b::/96"; |
78 | | |
79 | | /** fillup fetch policy array */ |
80 | | static int |
81 | | fetch_fill(int* target_fetch_policy, int max_dependency_depth, const char* str) |
82 | 0 | { |
83 | 0 | char* s = (char*)str, *e; |
84 | 0 | int i; |
85 | 0 | for(i=0; i<max_dependency_depth+1; i++) { |
86 | 0 | target_fetch_policy[i] = strtol(s, &e, 10); |
87 | 0 | if(s == e) { |
88 | 0 | log_err("cannot parse fetch policy number %s", s); |
89 | 0 | return 0; |
90 | 0 | } |
91 | 0 | s = e; |
92 | 0 | } |
93 | 0 | return 1; |
94 | 0 | } |
95 | | |
96 | | /** Read config string that represents the target fetch policy */ |
97 | | int |
98 | | read_fetch_policy(int** target_fetch_policy, int* max_dependency_depth, |
99 | | const char* str) |
100 | 0 | { |
101 | 0 | int count = cfg_count_numbers(str); |
102 | 0 | if(count < 1) { |
103 | 0 | log_err("Cannot parse target fetch policy: \"%s\"", str); |
104 | 0 | return 0; |
105 | 0 | } |
106 | 0 | *max_dependency_depth = count - 1; |
107 | 0 | *target_fetch_policy = (int*)calloc( |
108 | 0 | (size_t)(*max_dependency_depth)+1, sizeof(int)); |
109 | 0 | if(!*target_fetch_policy) { |
110 | 0 | log_err("alloc fetch policy: out of memory"); |
111 | 0 | return 0; |
112 | 0 | } |
113 | 0 | if(!fetch_fill(*target_fetch_policy, *max_dependency_depth, str)) |
114 | 0 | return 0; |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | | struct rbtree_type* |
119 | | caps_white_create(void) |
120 | 0 | { |
121 | 0 | struct rbtree_type* caps_white = rbtree_create(name_tree_compare); |
122 | 0 | if(!caps_white) |
123 | 0 | log_err("out of memory"); |
124 | 0 | return caps_white; |
125 | 0 | } |
126 | | |
127 | | /** delete caps_whitelist element */ |
128 | | static void |
129 | | caps_free(struct rbnode_type* n, void* ATTR_UNUSED(d)) |
130 | 0 | { |
131 | 0 | if(n) { |
132 | 0 | free(((struct name_tree_node*)n)->name); |
133 | 0 | free(n); |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | void |
138 | | caps_white_delete(struct rbtree_type* caps_white) |
139 | 0 | { |
140 | 0 | if(!caps_white) |
141 | 0 | return; |
142 | 0 | traverse_postorder(caps_white, caps_free, NULL); |
143 | 0 | free(caps_white); |
144 | 0 | } |
145 | | |
146 | | int |
147 | | caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg) |
148 | 0 | { |
149 | 0 | struct config_strlist* p; |
150 | 0 | for(p=cfg->caps_whitelist; p; p=p->next) { |
151 | 0 | struct name_tree_node* n; |
152 | 0 | size_t len; |
153 | 0 | uint8_t* nm = sldns_str2wire_dname(p->str, &len); |
154 | 0 | if(!nm) { |
155 | 0 | log_err("could not parse %s", p->str); |
156 | 0 | return 0; |
157 | 0 | } |
158 | 0 | n = (struct name_tree_node*)calloc(1, sizeof(*n)); |
159 | 0 | if(!n) { |
160 | 0 | log_err("out of memory"); |
161 | 0 | free(nm); |
162 | 0 | return 0; |
163 | 0 | } |
164 | 0 | n->node.key = n; |
165 | 0 | n->name = nm; |
166 | 0 | n->len = len; |
167 | 0 | n->labs = dname_count_labels(nm); |
168 | 0 | n->dclass = LDNS_RR_CLASS_IN; |
169 | 0 | if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) { |
170 | | /* duplicate element ignored, idempotent */ |
171 | 0 | free(n->name); |
172 | 0 | free(n); |
173 | 0 | } |
174 | 0 | } |
175 | 0 | name_tree_init_parents(ntree); |
176 | 0 | return 1; |
177 | 0 | } |
178 | | |
179 | | int |
180 | | nat64_apply_cfg(struct iter_nat64* nat64, struct config_file* cfg) |
181 | 0 | { |
182 | 0 | const char *nat64_prefix; |
183 | |
|
184 | 0 | nat64_prefix = cfg->nat64_prefix; |
185 | 0 | if(!nat64_prefix) |
186 | 0 | nat64_prefix = cfg->dns64_prefix; |
187 | 0 | if(!nat64_prefix) |
188 | 0 | nat64_prefix = DEFAULT_NAT64_PREFIX; |
189 | 0 | if(!netblockstrtoaddr(nat64_prefix, 0, &nat64->nat64_prefix_addr, |
190 | 0 | &nat64->nat64_prefix_addrlen, &nat64->nat64_prefix_net)) { |
191 | 0 | log_err("cannot parse nat64-prefix netblock: %s", nat64_prefix); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 | if(!addr_is_ip6(&nat64->nat64_prefix_addr, |
195 | 0 | nat64->nat64_prefix_addrlen)) { |
196 | 0 | log_err("nat64-prefix is not IPv6: %s", cfg->nat64_prefix); |
197 | 0 | return 0; |
198 | 0 | } |
199 | 0 | if(!prefixnet_is_nat64(nat64->nat64_prefix_net)) { |
200 | 0 | log_err("nat64-prefix length it not 32, 40, 48, 56, 64 or 96: %s", |
201 | 0 | nat64_prefix); |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | nat64->use_nat64 = cfg->do_nat64; |
205 | 0 | return 1; |
206 | 0 | } |
207 | | |
208 | | int |
209 | | iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg) |
210 | 0 | { |
211 | 0 | int i; |
212 | | /* target fetch policy */ |
213 | 0 | if(!read_fetch_policy(&iter_env->target_fetch_policy, |
214 | 0 | &iter_env->max_dependency_depth, cfg->target_fetch_policy)) |
215 | 0 | return 0; |
216 | 0 | for(i=0; i<iter_env->max_dependency_depth+1; i++) |
217 | 0 | verbose(VERB_QUERY, "target fetch policy for level %d is %d", |
218 | 0 | i, iter_env->target_fetch_policy[i]); |
219 | |
|
220 | 0 | if(!iter_env->donotq) |
221 | 0 | iter_env->donotq = donotq_create(); |
222 | 0 | if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) { |
223 | 0 | log_err("Could not set donotqueryaddresses"); |
224 | 0 | return 0; |
225 | 0 | } |
226 | 0 | if(!iter_env->priv) |
227 | 0 | iter_env->priv = priv_create(); |
228 | 0 | if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) { |
229 | 0 | log_err("Could not set private addresses"); |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | if(cfg->caps_whitelist) { |
233 | 0 | if(!iter_env->caps_white) |
234 | 0 | iter_env->caps_white = caps_white_create(); |
235 | 0 | if(!iter_env->caps_white || !caps_white_apply_cfg( |
236 | 0 | iter_env->caps_white, cfg)) { |
237 | 0 | log_err("Could not set capsforid whitelist"); |
238 | 0 | return 0; |
239 | 0 | } |
240 | |
|
241 | 0 | } |
242 | | |
243 | 0 | if(!nat64_apply_cfg(&iter_env->nat64, cfg)) { |
244 | 0 | log_err("Could not setup nat64"); |
245 | 0 | return 0; |
246 | 0 | } |
247 | | |
248 | 0 | iter_env->supports_ipv6 = cfg->do_ip6; |
249 | 0 | iter_env->supports_ipv4 = cfg->do_ip4; |
250 | 0 | iter_env->outbound_msg_retry = cfg->outbound_msg_retry; |
251 | 0 | iter_env->max_sent_count = cfg->max_sent_count; |
252 | 0 | iter_env->max_query_restarts = cfg->max_query_restarts; |
253 | 0 | return 1; |
254 | 0 | } |
255 | | |
256 | | /** filter out unsuitable targets. |
257 | | * Applies NAT64 if needed as well by replacing the IPv4 with the synthesized |
258 | | * IPv6 address. |
259 | | * @param iter_env: iterator environment with ipv6-support flag. |
260 | | * @param env: module environment with infra cache. |
261 | | * @param name: zone name |
262 | | * @param namelen: length of name |
263 | | * @param qtype: query type (host order). |
264 | | * @param now: current time |
265 | | * @param a: address in delegation point we are examining. |
266 | | * @return an integer that signals the target suitability. |
267 | | * as follows: |
268 | | * -1: The address should be omitted from the list. |
269 | | * Because: |
270 | | * o The address is bogus (DNSSEC validation failure). |
271 | | * o Listed as donotquery |
272 | | * o is ipv6 but no ipv6 support (in operating system). |
273 | | * o is ipv4 but no ipv4 support (in operating system). |
274 | | * o is lame |
275 | | * Otherwise, an rtt in milliseconds. |
276 | | * 0 .. USEFUL_SERVER_TOP_TIMEOUT-1 |
277 | | * The roundtrip time timeout estimate. less than 2 minutes. |
278 | | * Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus |
279 | | * values 0 .. 49 are not used, unless that is changed. |
280 | | * USEFUL_SERVER_TOP_TIMEOUT |
281 | | * This value exactly is given for unresponsive blacklisted. |
282 | | * USEFUL_SERVER_TOP_TIMEOUT+1 |
283 | | * For non-blacklisted servers: huge timeout, but has traffic. |
284 | | * USEFUL_SERVER_TOP_TIMEOUT*1 .. |
285 | | * parent-side lame servers get this penalty. A dispreferential |
286 | | * server. (lame in delegpt). |
287 | | * USEFUL_SERVER_TOP_TIMEOUT*2 .. |
288 | | * dnsseclame servers get penalty |
289 | | * USEFUL_SERVER_TOP_TIMEOUT*3 .. |
290 | | * recursion lame servers get penalty |
291 | | * UNKNOWN_SERVER_NICENESS |
292 | | * If no information is known about the server, this is |
293 | | * returned. 376 msec or so. |
294 | | * +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs. |
295 | | * |
296 | | * When a final value is chosen that is dnsseclame ; dnsseclameness checking |
297 | | * is turned off (so we do not discard the reply). |
298 | | * When a final value is chosen that is recursionlame; RD bit is set on query. |
299 | | * Because of the numbers this means recursionlame also have dnssec lameness |
300 | | * checking turned off. |
301 | | */ |
302 | | static int |
303 | | iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env, |
304 | | uint8_t* name, size_t namelen, uint16_t qtype, time_t now, |
305 | | struct delegpt_addr* a) |
306 | 0 | { |
307 | 0 | int rtt, lame, reclame, dnsseclame; |
308 | 0 | if(a->bogus) |
309 | 0 | return -1; /* address of server is bogus */ |
310 | 0 | if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) { |
311 | 0 | if(iter_env->nat64.use_nat64 && |
312 | 0 | addr_is_ip6(&a->addr, a->addrlen) && |
313 | 0 | a->addrlen == iter_env->nat64.nat64_prefix_addrlen && |
314 | 0 | addr_in_common(&a->addr, 128, |
315 | 0 | &iter_env->nat64.nat64_prefix_addr, |
316 | 0 | iter_env->nat64.nat64_prefix_net, |
317 | 0 | iter_env->nat64.nat64_prefix_addrlen) == |
318 | 0 | iter_env->nat64.nat64_prefix_net) { |
319 | | /* The NAT64 is enabled, and address is IPv6, it is |
320 | | * in the NAT64 prefix. It is allowed. |
321 | | * So that in an IPv6-only cluster without internet |
322 | | * access, that makes the NAT64 translation continue |
323 | | * to work. The NAT64 prefix is allowed. */ |
324 | | /* Otherwise, after a timeout, the already NAT64 |
325 | | * translated address would be treated differently, |
326 | | * and that causes confusion. */ |
327 | 0 | log_addr(VERB_ALGO, "the addr is on the donotquery " |
328 | 0 | "list, but allowed because it is NAT64", |
329 | 0 | &a->addr, a->addrlen); |
330 | 0 | } else { |
331 | 0 | log_addr(VERB_ALGO, "skip addr on the donotquery list", |
332 | 0 | &a->addr, a->addrlen); |
333 | 0 | return -1; /* server is on the donotquery list */ |
334 | 0 | } |
335 | 0 | } |
336 | 0 | if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) { |
337 | 0 | return -1; /* there is no ip6 available */ |
338 | 0 | } |
339 | 0 | if(!iter_env->supports_ipv4 && !iter_env->nat64.use_nat64 && |
340 | 0 | !addr_is_ip6(&a->addr, a->addrlen)) { |
341 | 0 | return -1; /* there is no ip4 available */ |
342 | 0 | } |
343 | 0 | if(iter_env->nat64.use_nat64 && !addr_is_ip6(&a->addr, a->addrlen)) { |
344 | 0 | struct sockaddr_storage real_addr; |
345 | 0 | socklen_t real_addrlen; |
346 | 0 | addr_to_nat64(&a->addr, &iter_env->nat64.nat64_prefix_addr, |
347 | 0 | iter_env->nat64.nat64_prefix_addrlen, |
348 | 0 | iter_env->nat64.nat64_prefix_net, |
349 | 0 | &real_addr, &real_addrlen); |
350 | 0 | log_name_addr(VERB_QUERY, "NAT64 apply: from: ", |
351 | 0 | name, &a->addr, a->addrlen); |
352 | 0 | log_name_addr(VERB_QUERY, "NAT64 apply: to: ", |
353 | 0 | name, &real_addr, real_addrlen); |
354 | 0 | a->addr = real_addr; |
355 | 0 | a->addrlen = real_addrlen; |
356 | 0 | } |
357 | | /* check lameness - need zone , class info */ |
358 | 0 | if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen, |
359 | 0 | name, namelen, qtype, &lame, &dnsseclame, &reclame, |
360 | 0 | &rtt, now)) { |
361 | 0 | log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen); |
362 | 0 | verbose(VERB_ALGO, " rtt=%d%s%s%s%s%s", rtt, |
363 | 0 | lame?" LAME":"", |
364 | 0 | dnsseclame?" DNSSEC_LAME":"", |
365 | 0 | a->dnsseclame?" ADDR_DNSSEC_LAME":"", |
366 | 0 | reclame?" REC_LAME":"", |
367 | 0 | a->lame?" ADDR_LAME":""); |
368 | 0 | if(lame) |
369 | 0 | return -1; /* server is lame */ |
370 | 0 | else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT) |
371 | | /* server is unresponsive, |
372 | | * we used to return TOP_TIMEOUT, but fairly useless, |
373 | | * because if == TOP_TIMEOUT is dropped because |
374 | | * blacklisted later, instead, remove it here, so |
375 | | * other choices (that are not blacklisted) can be |
376 | | * tried */ |
377 | 0 | return -1; |
378 | | /* select remainder from worst to best */ |
379 | 0 | else if(reclame) |
380 | 0 | return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */ |
381 | 0 | else if(dnsseclame || a->dnsseclame) |
382 | 0 | return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */ |
383 | 0 | else if(a->lame) |
384 | 0 | return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */ |
385 | 0 | else return rtt; |
386 | 0 | } |
387 | | /* no server information present */ |
388 | 0 | if(a->dnsseclame) |
389 | 0 | return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */ |
390 | 0 | else if(a->lame) |
391 | 0 | return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */ |
392 | 0 | return UNKNOWN_SERVER_NICENESS; |
393 | 0 | } |
394 | | |
395 | | /** lookup RTT information, and also store fastest rtt (if any) */ |
396 | | static int |
397 | | iter_fill_rtt(struct iter_env* iter_env, struct module_env* env, |
398 | | uint8_t* name, size_t namelen, uint16_t qtype, time_t now, |
399 | | struct delegpt* dp, int* best_rtt, struct sock_list* blacklist, |
400 | | size_t* num_suitable_results) |
401 | 0 | { |
402 | 0 | int got_it = 0; |
403 | 0 | struct delegpt_addr* a; |
404 | 0 | *num_suitable_results = 0; |
405 | |
|
406 | 0 | if(dp->bogus) |
407 | 0 | return 0; /* NS bogus, all bogus, nothing found */ |
408 | 0 | for(a=dp->result_list; a; a = a->next_result) { |
409 | 0 | a->sel_rtt = iter_filter_unsuitable(iter_env, env, |
410 | 0 | name, namelen, qtype, now, a); |
411 | 0 | if(a->sel_rtt != -1) { |
412 | 0 | if(sock_list_find(blacklist, &a->addr, a->addrlen)) |
413 | 0 | a->sel_rtt += BLACKLIST_PENALTY; |
414 | |
|
415 | 0 | if(!got_it) { |
416 | 0 | *best_rtt = a->sel_rtt; |
417 | 0 | got_it = 1; |
418 | 0 | } else if(a->sel_rtt < *best_rtt) { |
419 | 0 | *best_rtt = a->sel_rtt; |
420 | 0 | } |
421 | 0 | (*num_suitable_results)++; |
422 | 0 | } |
423 | 0 | } |
424 | 0 | return got_it; |
425 | 0 | } |
426 | | |
427 | | /** compare two rtts, return -1, 0 or 1 */ |
428 | | static int |
429 | | rtt_compare(const void* x, const void* y) |
430 | 0 | { |
431 | 0 | if(*(int*)x == *(int*)y) |
432 | 0 | return 0; |
433 | 0 | if(*(int*)x > *(int*)y) |
434 | 0 | return 1; |
435 | 0 | return -1; |
436 | 0 | } |
437 | | |
438 | | /** get RTT for the Nth fastest server */ |
439 | | static int |
440 | | nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n) |
441 | 0 | { |
442 | 0 | int rtt_band; |
443 | 0 | size_t i; |
444 | 0 | int* rtt_list, *rtt_index; |
445 | |
|
446 | 0 | if(num_results < 1 || n >= num_results) { |
447 | 0 | return -1; |
448 | 0 | } |
449 | | |
450 | 0 | rtt_list = calloc(num_results, sizeof(int)); |
451 | 0 | if(!rtt_list) { |
452 | 0 | log_err("malloc failure: allocating rtt_list"); |
453 | 0 | return -1; |
454 | 0 | } |
455 | 0 | rtt_index = rtt_list; |
456 | |
|
457 | 0 | for(i=0; i<num_results && result_list; i++) { |
458 | 0 | if(result_list->sel_rtt != -1) { |
459 | 0 | *rtt_index = result_list->sel_rtt; |
460 | 0 | rtt_index++; |
461 | 0 | } |
462 | 0 | result_list=result_list->next_result; |
463 | 0 | } |
464 | 0 | qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare); |
465 | |
|
466 | 0 | log_assert(n > 0); |
467 | 0 | rtt_band = rtt_list[n-1]; |
468 | 0 | free(rtt_list); |
469 | |
|
470 | 0 | return rtt_band; |
471 | 0 | } |
472 | | |
473 | | /** filter the address list, putting best targets at front, |
474 | | * returns number of best targets (or 0, no suitable targets) */ |
475 | | static int |
476 | | iter_filter_order(struct iter_env* iter_env, struct module_env* env, |
477 | | uint8_t* name, size_t namelen, uint16_t qtype, time_t now, |
478 | | struct delegpt* dp, int* selected_rtt, int open_target, |
479 | | struct sock_list* blacklist, time_t prefetch) |
480 | 0 | { |
481 | 0 | int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth; |
482 | 0 | int alllame = 0; |
483 | 0 | size_t num_results; |
484 | 0 | struct delegpt_addr* a, *n, *prev=NULL; |
485 | | |
486 | | /* fillup sel_rtt and find best rtt in the bunch */ |
487 | 0 | got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp, |
488 | 0 | &low_rtt, blacklist, &num_results); |
489 | 0 | if(got_num == 0) |
490 | 0 | return 0; |
491 | 0 | if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT && |
492 | | /* If all missing (or not fully resolved) targets are lame, |
493 | | * then use the remaining lame address. */ |
494 | 0 | ((delegpt_count_missing_targets(dp, &alllame) > 0 && !alllame) || |
495 | 0 | open_target > 0)) { |
496 | 0 | verbose(VERB_ALGO, "Bad choices, trying to get more choice"); |
497 | 0 | return 0; /* we want more choice. The best choice is a bad one. |
498 | | return 0 to force the caller to fetch more */ |
499 | 0 | } |
500 | | |
501 | 0 | if(env->cfg->fast_server_permil != 0 && prefetch == 0 && |
502 | 0 | num_results > env->cfg->fast_server_num && |
503 | 0 | ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) { |
504 | | /* the query is not prefetch, but for a downstream client, |
505 | | * there are more servers available then the fastest N we want |
506 | | * to choose from. Limit our choice to the fastest servers. */ |
507 | 0 | nth = nth_rtt(dp->result_list, num_results, |
508 | 0 | env->cfg->fast_server_num); |
509 | 0 | if(nth > 0) { |
510 | 0 | rtt_band = nth - low_rtt; |
511 | 0 | if(rtt_band > RTT_BAND) |
512 | 0 | rtt_band = RTT_BAND; |
513 | 0 | } |
514 | 0 | } |
515 | |
|
516 | 0 | got_num = 0; |
517 | 0 | a = dp->result_list; |
518 | 0 | while(a) { |
519 | | /* skip unsuitable targets */ |
520 | 0 | if(a->sel_rtt == -1) { |
521 | 0 | prev = a; |
522 | 0 | a = a->next_result; |
523 | 0 | continue; |
524 | 0 | } |
525 | | /* classify the server address and determine what to do */ |
526 | 0 | swap_to_front = 0; |
527 | 0 | if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) { |
528 | 0 | got_num++; |
529 | 0 | swap_to_front = 1; |
530 | 0 | } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) { |
531 | 0 | got_num++; |
532 | 0 | swap_to_front = 1; |
533 | 0 | } |
534 | | /* swap to front if necessary, or move to next result */ |
535 | 0 | if(swap_to_front && prev) { |
536 | 0 | n = a->next_result; |
537 | 0 | prev->next_result = n; |
538 | 0 | a->next_result = dp->result_list; |
539 | 0 | dp->result_list = a; |
540 | 0 | a = n; |
541 | 0 | } else { |
542 | 0 | prev = a; |
543 | 0 | a = a->next_result; |
544 | 0 | } |
545 | 0 | } |
546 | 0 | *selected_rtt = low_rtt; |
547 | |
|
548 | 0 | if (env->cfg->prefer_ip6) { |
549 | 0 | int got_num6 = 0; |
550 | 0 | int low_rtt6 = 0; |
551 | 0 | int i; |
552 | 0 | int attempt = -1; /* filter to make sure addresses have |
553 | | less attempts on them than the first, to force round |
554 | | robin when all the IPv6 addresses fail */ |
555 | 0 | int num4ok = 0; /* number ip4 at low attempt count */ |
556 | 0 | int num4_lowrtt = 0; |
557 | 0 | prev = NULL; |
558 | 0 | a = dp->result_list; |
559 | 0 | for(i = 0; i < got_num; i++) { |
560 | 0 | if(!a) break; /* robustness */ |
561 | 0 | swap_to_front = 0; |
562 | 0 | if(a->addr.ss_family != AF_INET6 && attempt == -1) { |
563 | | /* if we only have ip4 at low attempt count, |
564 | | * then ip6 is failing, and we need to |
565 | | * select one of the remaining IPv4 addrs */ |
566 | 0 | attempt = a->attempts; |
567 | 0 | num4ok++; |
568 | 0 | num4_lowrtt = a->sel_rtt; |
569 | 0 | } else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) { |
570 | 0 | num4ok++; |
571 | 0 | if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) { |
572 | 0 | num4_lowrtt = a->sel_rtt; |
573 | 0 | } |
574 | 0 | } |
575 | 0 | if(a->addr.ss_family == AF_INET6) { |
576 | 0 | if(attempt == -1) { |
577 | 0 | attempt = a->attempts; |
578 | 0 | } else if(a->attempts > attempt) { |
579 | 0 | break; |
580 | 0 | } |
581 | 0 | got_num6++; |
582 | 0 | swap_to_front = 1; |
583 | 0 | if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) { |
584 | 0 | low_rtt6 = a->sel_rtt; |
585 | 0 | } |
586 | 0 | } |
587 | | /* swap to front if IPv6, or move to next result */ |
588 | 0 | if(swap_to_front && prev) { |
589 | 0 | n = a->next_result; |
590 | 0 | prev->next_result = n; |
591 | 0 | a->next_result = dp->result_list; |
592 | 0 | dp->result_list = a; |
593 | 0 | a = n; |
594 | 0 | } else { |
595 | 0 | prev = a; |
596 | 0 | a = a->next_result; |
597 | 0 | } |
598 | 0 | } |
599 | 0 | if(got_num6 > 0) { |
600 | 0 | got_num = got_num6; |
601 | 0 | *selected_rtt = low_rtt6; |
602 | 0 | } else if(num4ok > 0) { |
603 | 0 | got_num = num4ok; |
604 | 0 | *selected_rtt = num4_lowrtt; |
605 | 0 | } |
606 | 0 | } else if (env->cfg->prefer_ip4) { |
607 | 0 | int got_num4 = 0; |
608 | 0 | int low_rtt4 = 0; |
609 | 0 | int i; |
610 | 0 | int attempt = -1; /* filter to make sure addresses have |
611 | | less attempts on them than the first, to force round |
612 | | robin when all the IPv4 addresses fail */ |
613 | 0 | int num6ok = 0; /* number ip6 at low attempt count */ |
614 | 0 | int num6_lowrtt = 0; |
615 | 0 | prev = NULL; |
616 | 0 | a = dp->result_list; |
617 | 0 | for(i = 0; i < got_num; i++) { |
618 | 0 | if(!a) break; /* robustness */ |
619 | 0 | swap_to_front = 0; |
620 | 0 | if(a->addr.ss_family != AF_INET && attempt == -1) { |
621 | | /* if we only have ip6 at low attempt count, |
622 | | * then ip4 is failing, and we need to |
623 | | * select one of the remaining IPv6 addrs */ |
624 | 0 | attempt = a->attempts; |
625 | 0 | num6ok++; |
626 | 0 | num6_lowrtt = a->sel_rtt; |
627 | 0 | } else if(a->addr.ss_family != AF_INET && attempt == a->attempts) { |
628 | 0 | num6ok++; |
629 | 0 | if(num6_lowrtt == 0 || a->sel_rtt < num6_lowrtt) { |
630 | 0 | num6_lowrtt = a->sel_rtt; |
631 | 0 | } |
632 | 0 | } |
633 | 0 | if(a->addr.ss_family == AF_INET) { |
634 | 0 | if(attempt == -1) { |
635 | 0 | attempt = a->attempts; |
636 | 0 | } else if(a->attempts > attempt) { |
637 | 0 | break; |
638 | 0 | } |
639 | 0 | got_num4++; |
640 | 0 | swap_to_front = 1; |
641 | 0 | if(low_rtt4 == 0 || a->sel_rtt < low_rtt4) { |
642 | 0 | low_rtt4 = a->sel_rtt; |
643 | 0 | } |
644 | 0 | } |
645 | | /* swap to front if IPv4, or move to next result */ |
646 | 0 | if(swap_to_front && prev) { |
647 | 0 | n = a->next_result; |
648 | 0 | prev->next_result = n; |
649 | 0 | a->next_result = dp->result_list; |
650 | 0 | dp->result_list = a; |
651 | 0 | a = n; |
652 | 0 | } else { |
653 | 0 | prev = a; |
654 | 0 | a = a->next_result; |
655 | 0 | } |
656 | 0 | } |
657 | 0 | if(got_num4 > 0) { |
658 | 0 | got_num = got_num4; |
659 | 0 | *selected_rtt = low_rtt4; |
660 | 0 | } else if(num6ok > 0) { |
661 | 0 | got_num = num6ok; |
662 | 0 | *selected_rtt = num6_lowrtt; |
663 | 0 | } |
664 | 0 | } |
665 | 0 | return got_num; |
666 | 0 | } |
667 | | |
668 | | struct delegpt_addr* |
669 | | iter_server_selection(struct iter_env* iter_env, |
670 | | struct module_env* env, struct delegpt* dp, |
671 | | uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame, |
672 | | int* chase_to_rd, int open_target, struct sock_list* blacklist, |
673 | | time_t prefetch) |
674 | 0 | { |
675 | 0 | int sel; |
676 | 0 | int selrtt; |
677 | 0 | struct delegpt_addr* a, *prev; |
678 | 0 | int num = iter_filter_order(iter_env, env, name, namelen, qtype, |
679 | 0 | *env->now, dp, &selrtt, open_target, blacklist, prefetch); |
680 | |
|
681 | 0 | if(num == 0) |
682 | 0 | return NULL; |
683 | 0 | verbose(VERB_ALGO, "selrtt %d", selrtt); |
684 | 0 | if(selrtt > BLACKLIST_PENALTY) { |
685 | 0 | if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) { |
686 | 0 | verbose(VERB_ALGO, "chase to " |
687 | 0 | "blacklisted recursion lame server"); |
688 | 0 | *chase_to_rd = 1; |
689 | 0 | } |
690 | 0 | if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) { |
691 | 0 | verbose(VERB_ALGO, "chase to " |
692 | 0 | "blacklisted dnssec lame server"); |
693 | 0 | *dnssec_lame = 1; |
694 | 0 | } |
695 | 0 | } else { |
696 | 0 | if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) { |
697 | 0 | verbose(VERB_ALGO, "chase to recursion lame server"); |
698 | 0 | *chase_to_rd = 1; |
699 | 0 | } |
700 | 0 | if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) { |
701 | 0 | verbose(VERB_ALGO, "chase to dnssec lame server"); |
702 | 0 | *dnssec_lame = 1; |
703 | 0 | } |
704 | 0 | if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) { |
705 | 0 | verbose(VERB_ALGO, "chase to blacklisted lame server"); |
706 | 0 | return NULL; |
707 | 0 | } |
708 | 0 | } |
709 | | |
710 | 0 | if(num == 1) { |
711 | 0 | a = dp->result_list; |
712 | 0 | if(++a->attempts < iter_env->outbound_msg_retry) |
713 | 0 | return a; |
714 | 0 | dp->result_list = a->next_result; |
715 | 0 | return a; |
716 | 0 | } |
717 | | |
718 | | /* randomly select a target from the list */ |
719 | 0 | log_assert(num > 1); |
720 | | /* grab secure random number, to pick unexpected server. |
721 | | * also we need it to be threadsafe. */ |
722 | 0 | sel = ub_random_max(env->rnd, num); |
723 | 0 | a = dp->result_list; |
724 | 0 | prev = NULL; |
725 | 0 | while(sel > 0 && a) { |
726 | 0 | prev = a; |
727 | 0 | a = a->next_result; |
728 | 0 | sel--; |
729 | 0 | } |
730 | 0 | if(!a) /* robustness */ |
731 | 0 | return NULL; |
732 | 0 | if(++a->attempts < iter_env->outbound_msg_retry) |
733 | 0 | return a; |
734 | | /* remove it from the delegation point result list */ |
735 | 0 | if(prev) |
736 | 0 | prev->next_result = a->next_result; |
737 | 0 | else dp->result_list = a->next_result; |
738 | 0 | return a; |
739 | 0 | } |
740 | | |
741 | | struct dns_msg* |
742 | | dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg, |
743 | | struct regional* region) |
744 | 0 | { |
745 | 0 | struct dns_msg* m = (struct dns_msg*)regional_alloc(region, |
746 | 0 | sizeof(struct dns_msg)); |
747 | 0 | if(!m) |
748 | 0 | return NULL; |
749 | 0 | memset(m, 0, sizeof(*m)); |
750 | 0 | if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) { |
751 | 0 | log_err("malloc failure: allocating incoming dns_msg"); |
752 | 0 | return NULL; |
753 | 0 | } |
754 | 0 | return m; |
755 | 0 | } |
756 | | |
757 | | struct dns_msg* |
758 | | dns_copy_msg(struct dns_msg* from, struct regional* region) |
759 | 0 | { |
760 | 0 | struct dns_msg* m = (struct dns_msg*)regional_alloc(region, |
761 | 0 | sizeof(struct dns_msg)); |
762 | 0 | if(!m) |
763 | 0 | return NULL; |
764 | 0 | m->qinfo = from->qinfo; |
765 | 0 | if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname, |
766 | 0 | from->qinfo.qname_len))) |
767 | 0 | return NULL; |
768 | 0 | if(!(m->rep = reply_info_copy(from->rep, NULL, region))) |
769 | 0 | return NULL; |
770 | 0 | return m; |
771 | 0 | } |
772 | | |
773 | | void |
774 | | iter_dns_store(struct module_env* env, struct query_info* msgqinf, |
775 | | struct reply_info* msgrep, int is_referral, time_t leeway, int pside, |
776 | | struct regional* region, uint16_t flags, time_t qstarttime, |
777 | | int is_valrec) |
778 | 0 | { |
779 | 0 | if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway, |
780 | 0 | pside, region, flags, qstarttime, is_valrec)) |
781 | 0 | log_err("out of memory: cannot store data in cache"); |
782 | 0 | } |
783 | | |
784 | | int |
785 | | iter_ns_probability(struct ub_randstate* rnd, int n, int m) |
786 | 0 | { |
787 | 0 | int sel; |
788 | 0 | if(n == m) /* 100% chance */ |
789 | 0 | return 1; |
790 | | /* we do not need secure random numbers here, but |
791 | | * we do need it to be threadsafe, so we use this */ |
792 | 0 | sel = ub_random_max(rnd, m); |
793 | 0 | return (sel < n); |
794 | 0 | } |
795 | | |
796 | | /** detect dependency cycle for query and target */ |
797 | | static int |
798 | | causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen, |
799 | | uint16_t t, uint16_t c) |
800 | 0 | { |
801 | 0 | struct query_info qinf; |
802 | 0 | qinf.qname = name; |
803 | 0 | qinf.qname_len = namelen; |
804 | 0 | qinf.qtype = t; |
805 | 0 | qinf.qclass = c; |
806 | 0 | qinf.local_alias = NULL; |
807 | 0 | fptr_ok(fptr_whitelist_modenv_detect_cycle( |
808 | 0 | qstate->env->detect_cycle)); |
809 | 0 | return (*qstate->env->detect_cycle)(qstate, &qinf, |
810 | 0 | (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming, |
811 | 0 | qstate->is_valrec); |
812 | 0 | } |
813 | | |
814 | | void |
815 | | iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) |
816 | 0 | { |
817 | 0 | struct delegpt_ns* ns; |
818 | 0 | for(ns = dp->nslist; ns; ns = ns->next) { |
819 | 0 | if(ns->resolved) |
820 | 0 | continue; |
821 | | /* see if this ns as target causes dependency cycle */ |
822 | 0 | if(causes_cycle(qstate, ns->name, ns->namelen, |
823 | 0 | LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) || |
824 | 0 | causes_cycle(qstate, ns->name, ns->namelen, |
825 | 0 | LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { |
826 | 0 | log_nametypeclass(VERB_QUERY, "skipping target due " |
827 | 0 | "to dependency cycle (harden-glue: no may " |
828 | 0 | "fix some of the cycles)", |
829 | 0 | ns->name, LDNS_RR_TYPE_A, |
830 | 0 | qstate->qinfo.qclass); |
831 | 0 | ns->resolved = 1; |
832 | 0 | } |
833 | 0 | } |
834 | 0 | } |
835 | | |
836 | | void |
837 | | iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp) |
838 | 0 | { |
839 | 0 | struct delegpt_ns* ns; |
840 | 0 | for(ns = dp->nslist; ns; ns = ns->next) { |
841 | 0 | if(ns->done_pside4 && ns->done_pside6) |
842 | 0 | continue; |
843 | | /* see if this ns as target causes dependency cycle */ |
844 | 0 | if(causes_cycle(qstate, ns->name, ns->namelen, |
845 | 0 | LDNS_RR_TYPE_A, qstate->qinfo.qclass)) { |
846 | 0 | log_nametypeclass(VERB_QUERY, "skipping target due " |
847 | 0 | "to dependency cycle", ns->name, |
848 | 0 | LDNS_RR_TYPE_A, qstate->qinfo.qclass); |
849 | 0 | ns->done_pside4 = 1; |
850 | 0 | } |
851 | 0 | if(causes_cycle(qstate, ns->name, ns->namelen, |
852 | 0 | LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) { |
853 | 0 | log_nametypeclass(VERB_QUERY, "skipping target due " |
854 | 0 | "to dependency cycle", ns->name, |
855 | 0 | LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass); |
856 | 0 | ns->done_pside6 = 1; |
857 | 0 | } |
858 | 0 | } |
859 | 0 | } |
860 | | |
861 | | int |
862 | | iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags, |
863 | | struct delegpt* dp, int supports_ipv4, int supports_ipv6, |
864 | | int use_nat64) |
865 | 0 | { |
866 | 0 | struct delegpt_ns* ns; |
867 | 0 | struct delegpt_addr* a; |
868 | |
|
869 | 0 | if(supports_ipv6 && use_nat64) |
870 | 0 | supports_ipv4 = 1; |
871 | | |
872 | | /* check: |
873 | | * o RD qflag is on. |
874 | | * o no addresses are provided. |
875 | | * o all NS items are required glue. |
876 | | * OR |
877 | | * o RD qflag is on. |
878 | | * o no addresses are provided. |
879 | | * o the query is for one of the nameservers in dp, |
880 | | * and that nameserver is a glue-name for this dp. |
881 | | */ |
882 | 0 | if(!(qflags&BIT_RD)) |
883 | 0 | return 0; |
884 | | /* either available or unused targets, |
885 | | * if they exist, the dp is not useless. */ |
886 | 0 | for(a = dp->usable_list; a; a = a->next_usable) { |
887 | 0 | if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4) |
888 | 0 | return 0; |
889 | 0 | else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6) |
890 | 0 | return 0; |
891 | 0 | } |
892 | 0 | for(a = dp->result_list; a; a = a->next_result) { |
893 | 0 | if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4) |
894 | 0 | return 0; |
895 | 0 | else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6) |
896 | 0 | return 0; |
897 | 0 | } |
898 | | |
899 | | /* see if query is for one of the nameservers, which is glue */ |
900 | 0 | if( ((qinfo->qtype == LDNS_RR_TYPE_A && supports_ipv4) || |
901 | 0 | (qinfo->qtype == LDNS_RR_TYPE_AAAA && supports_ipv6)) && |
902 | 0 | dname_subdomain_c(qinfo->qname, dp->name) && |
903 | 0 | delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len)) |
904 | 0 | return 1; |
905 | | |
906 | 0 | for(ns = dp->nslist; ns; ns = ns->next) { |
907 | 0 | if(ns->resolved) /* skip failed targets */ |
908 | 0 | continue; |
909 | 0 | if(!dname_subdomain_c(ns->name, dp->name)) |
910 | 0 | return 0; /* one address is not required glue */ |
911 | 0 | } |
912 | 0 | return 1; |
913 | 0 | } |
914 | | |
915 | | int |
916 | | iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo) |
917 | 0 | { |
918 | 0 | struct trust_anchor* a; |
919 | 0 | if(!env || !env->anchors || !qinfo || !qinfo->qname) |
920 | 0 | return 0; |
921 | | /* a trust anchor exists above the name? */ |
922 | 0 | if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len, |
923 | 0 | qinfo->qclass))) { |
924 | 0 | if(a->numDS == 0 && a->numDNSKEY == 0) { |
925 | | /* insecure trust point */ |
926 | 0 | lock_basic_unlock(&a->lock); |
927 | 0 | return 0; |
928 | 0 | } |
929 | 0 | lock_basic_unlock(&a->lock); |
930 | 0 | return 1; |
931 | 0 | } |
932 | | /* no trust anchor above it. */ |
933 | 0 | return 0; |
934 | 0 | } |
935 | | |
936 | | int |
937 | | iter_indicates_dnssec(struct module_env* env, struct delegpt* dp, |
938 | | struct dns_msg* msg, uint16_t dclass) |
939 | 0 | { |
940 | 0 | struct trust_anchor* a; |
941 | | /* information not available, !env->anchors can be common */ |
942 | 0 | if(!env || !env->anchors || !dp || !dp->name) |
943 | 0 | return 0; |
944 | | /* a trust anchor exists with this name, RRSIGs expected */ |
945 | 0 | if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen, |
946 | 0 | dclass))) { |
947 | 0 | if(a->numDS == 0 && a->numDNSKEY == 0) { |
948 | | /* insecure trust point */ |
949 | 0 | lock_basic_unlock(&a->lock); |
950 | 0 | return 0; |
951 | 0 | } |
952 | 0 | lock_basic_unlock(&a->lock); |
953 | 0 | return 1; |
954 | 0 | } |
955 | | /* see if DS rrset was given, in AUTH section */ |
956 | 0 | if(msg && msg->rep && |
957 | 0 | reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, |
958 | 0 | LDNS_RR_TYPE_DS, dclass)) |
959 | 0 | return 1; |
960 | | /* look in key cache */ |
961 | 0 | if(env->key_cache) { |
962 | 0 | struct key_entry_key* kk = key_cache_obtain(env->key_cache, |
963 | 0 | dp->name, dp->namelen, dclass, env->scratch, *env->now); |
964 | 0 | if(kk) { |
965 | 0 | if(query_dname_compare(kk->name, dp->name) == 0) { |
966 | 0 | if(key_entry_isgood(kk) || key_entry_isbad(kk)) { |
967 | 0 | regional_free_all(env->scratch); |
968 | 0 | return 1; |
969 | 0 | } else if(key_entry_isnull(kk)) { |
970 | 0 | regional_free_all(env->scratch); |
971 | 0 | return 0; |
972 | 0 | } |
973 | 0 | } |
974 | 0 | regional_free_all(env->scratch); |
975 | 0 | } |
976 | 0 | } |
977 | 0 | return 0; |
978 | 0 | } |
979 | | |
980 | | int |
981 | | iter_msg_has_dnssec(struct dns_msg* msg) |
982 | 0 | { |
983 | 0 | size_t i; |
984 | 0 | if(!msg || !msg->rep) |
985 | 0 | return 0; |
986 | 0 | for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { |
987 | 0 | if(((struct packed_rrset_data*)msg->rep->rrsets[i]-> |
988 | 0 | entry.data)->rrsig_count > 0) |
989 | 0 | return 1; |
990 | 0 | } |
991 | | /* empty message has no DNSSEC info, with DNSSEC the reply is |
992 | | * not empty (NSEC) */ |
993 | 0 | return 0; |
994 | 0 | } |
995 | | |
996 | | int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp, |
997 | | enum response_type type, uint16_t dclass) |
998 | 0 | { |
999 | 0 | if(!msg || !dp || !msg->rep || !dp->name) |
1000 | 0 | return 0; |
1001 | | /* SOA RRset - always from reply zone */ |
1002 | 0 | if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, |
1003 | 0 | LDNS_RR_TYPE_SOA, dclass) || |
1004 | 0 | reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, |
1005 | 0 | LDNS_RR_TYPE_SOA, dclass)) |
1006 | 0 | return 1; |
1007 | 0 | if(type == RESPONSE_TYPE_REFERRAL) { |
1008 | 0 | size_t i; |
1009 | | /* if it adds a single label, i.e. we expect .com, |
1010 | | * and referral to example.com. NS ... , then origin zone |
1011 | | * is .com. For a referral to sub.example.com. NS ... then |
1012 | | * we do not know, since example.com. may be in between. */ |
1013 | 0 | for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets; |
1014 | 0 | i++) { |
1015 | 0 | struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; |
1016 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS && |
1017 | 0 | ntohs(s->rk.rrset_class) == dclass) { |
1018 | 0 | int l = dname_count_labels(s->rk.dname); |
1019 | 0 | if(l == dp->namelabs + 1 && |
1020 | 0 | dname_strict_subdomain(s->rk.dname, |
1021 | 0 | l, dp->name, dp->namelabs)) |
1022 | 0 | return 1; |
1023 | 0 | } |
1024 | 0 | } |
1025 | 0 | return 0; |
1026 | 0 | } |
1027 | 0 | log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME); |
1028 | | /* not a referral, and not lame delegation (upwards), so, |
1029 | | * any NS rrset must be from the zone itself */ |
1030 | 0 | if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, |
1031 | 0 | LDNS_RR_TYPE_NS, dclass) || |
1032 | 0 | reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen, |
1033 | 0 | LDNS_RR_TYPE_NS, dclass)) |
1034 | 0 | return 1; |
1035 | | /* a DNSKEY set is expected at the zone apex as well */ |
1036 | | /* this is for 'minimal responses' for DNSKEYs */ |
1037 | 0 | if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen, |
1038 | 0 | LDNS_RR_TYPE_DNSKEY, dclass)) |
1039 | 0 | return 1; |
1040 | 0 | return 0; |
1041 | 0 | } |
1042 | | |
1043 | | /** |
1044 | | * check equality of two rrsets |
1045 | | * @param k1: rrset |
1046 | | * @param k2: rrset |
1047 | | * @return true if equal |
1048 | | */ |
1049 | | static int |
1050 | | rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2) |
1051 | 0 | { |
1052 | 0 | struct packed_rrset_data* d1 = (struct packed_rrset_data*) |
1053 | 0 | k1->entry.data; |
1054 | 0 | struct packed_rrset_data* d2 = (struct packed_rrset_data*) |
1055 | 0 | k2->entry.data; |
1056 | 0 | size_t i, t; |
1057 | 0 | if(k1->rk.dname_len != k2->rk.dname_len || |
1058 | 0 | k1->rk.flags != k2->rk.flags || |
1059 | 0 | k1->rk.type != k2->rk.type || |
1060 | 0 | k1->rk.rrset_class != k2->rk.rrset_class || |
1061 | 0 | query_dname_compare(k1->rk.dname, k2->rk.dname) != 0) |
1062 | 0 | return 0; |
1063 | 0 | if( /* do not check ttl: d1->ttl != d2->ttl || */ |
1064 | 0 | d1->count != d2->count || |
1065 | 0 | d1->rrsig_count != d2->rrsig_count || |
1066 | 0 | d1->trust != d2->trust || |
1067 | 0 | d1->security != d2->security) |
1068 | 0 | return 0; |
1069 | 0 | t = d1->count + d1->rrsig_count; |
1070 | 0 | for(i=0; i<t; i++) { |
1071 | 0 | if(d1->rr_len[i] != d2->rr_len[i] || |
1072 | | /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/ |
1073 | 0 | memcmp(d1->rr_data[i], d2->rr_data[i], |
1074 | 0 | d1->rr_len[i]) != 0) |
1075 | 0 | return 0; |
1076 | 0 | } |
1077 | 0 | return 1; |
1078 | 0 | } |
1079 | | |
1080 | | /** compare rrsets and sort canonically. Compares rrset name, type, class. |
1081 | | * return 0 if equal, +1 if x > y, and -1 if x < y. |
1082 | | */ |
1083 | | static int |
1084 | | rrset_canonical_sort_cmp(const void* x, const void* y) |
1085 | 0 | { |
1086 | 0 | struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x; |
1087 | 0 | struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y; |
1088 | 0 | int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname); |
1089 | 0 | if(r != 0) |
1090 | 0 | return r; |
1091 | 0 | if(rrx->rk.type != rry->rk.type) { |
1092 | 0 | if(ntohs(rrx->rk.type) > ntohs(rry->rk.type)) |
1093 | 0 | return 1; |
1094 | 0 | else return -1; |
1095 | 0 | } |
1096 | 0 | if(rrx->rk.rrset_class != rry->rk.rrset_class) { |
1097 | 0 | if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class)) |
1098 | 0 | return 1; |
1099 | 0 | else return -1; |
1100 | 0 | } |
1101 | 0 | return 0; |
1102 | 0 | } |
1103 | | |
1104 | | int |
1105 | | reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region) |
1106 | 0 | { |
1107 | 0 | size_t i; |
1108 | 0 | struct ub_packed_rrset_key** sorted_p, **sorted_q; |
1109 | 0 | if(p->flags != q->flags || |
1110 | 0 | p->qdcount != q->qdcount || |
1111 | | /* do not check TTL, this may differ */ |
1112 | | /* |
1113 | | p->ttl != q->ttl || |
1114 | | p->prefetch_ttl != q->prefetch_ttl || |
1115 | | */ |
1116 | 0 | p->security != q->security || |
1117 | 0 | p->an_numrrsets != q->an_numrrsets || |
1118 | 0 | p->ns_numrrsets != q->ns_numrrsets || |
1119 | 0 | p->ar_numrrsets != q->ar_numrrsets || |
1120 | 0 | p->rrset_count != q->rrset_count) |
1121 | 0 | return 0; |
1122 | | /* sort the rrsets in the authority and additional sections before |
1123 | | * compare, the query and answer sections are ordered in the sequence |
1124 | | * they should have (eg. one after the other for aliases). */ |
1125 | 0 | sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init( |
1126 | 0 | region, p->rrsets, sizeof(*sorted_p)*p->rrset_count); |
1127 | 0 | if(!sorted_p) return 0; |
1128 | 0 | log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <= |
1129 | 0 | p->rrset_count); |
1130 | 0 | qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets, |
1131 | 0 | sizeof(*sorted_p), rrset_canonical_sort_cmp); |
1132 | 0 | qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets, |
1133 | 0 | sizeof(*sorted_p), rrset_canonical_sort_cmp); |
1134 | |
|
1135 | 0 | sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init( |
1136 | 0 | region, q->rrsets, sizeof(*sorted_q)*q->rrset_count); |
1137 | 0 | if(!sorted_q) { |
1138 | 0 | regional_free_all(region); |
1139 | 0 | return 0; |
1140 | 0 | } |
1141 | 0 | log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <= |
1142 | 0 | q->rrset_count); |
1143 | 0 | qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets, |
1144 | 0 | sizeof(*sorted_q), rrset_canonical_sort_cmp); |
1145 | 0 | qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets, |
1146 | 0 | sizeof(*sorted_q), rrset_canonical_sort_cmp); |
1147 | | |
1148 | | /* compare the rrsets */ |
1149 | 0 | for(i=0; i<p->rrset_count; i++) { |
1150 | 0 | if(!rrset_equal(sorted_p[i], sorted_q[i])) { |
1151 | 0 | if(!rrset_canonical_equal(region, sorted_p[i], |
1152 | 0 | sorted_q[i])) { |
1153 | 0 | regional_free_all(region); |
1154 | 0 | return 0; |
1155 | 0 | } |
1156 | 0 | } |
1157 | 0 | } |
1158 | 0 | regional_free_all(region); |
1159 | 0 | return 1; |
1160 | 0 | } |
1161 | | |
1162 | | void |
1163 | | caps_strip_reply(struct reply_info* rep) |
1164 | 0 | { |
1165 | 0 | size_t i; |
1166 | 0 | if(!rep) return; |
1167 | | /* see if message is a referral, in which case the additional and |
1168 | | * NS record cannot be removed */ |
1169 | | /* referrals have the AA flag unset (strict check, not elsewhere in |
1170 | | * unbound, but for 0x20 this is very convenient). */ |
1171 | 0 | if(!(rep->flags&BIT_AA)) |
1172 | 0 | return; |
1173 | | /* remove the additional section from the reply */ |
1174 | 0 | if(rep->ar_numrrsets != 0) { |
1175 | 0 | verbose(VERB_ALGO, "caps fallback: removing additional section"); |
1176 | 0 | rep->rrset_count -= rep->ar_numrrsets; |
1177 | 0 | rep->ar_numrrsets = 0; |
1178 | 0 | } |
1179 | | /* is there an NS set in the authority section to remove? */ |
1180 | | /* the failure case (Cisco firewalls) only has one rrset in authsec */ |
1181 | 0 | for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { |
1182 | 0 | struct ub_packed_rrset_key* s = rep->rrsets[i]; |
1183 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) { |
1184 | | /* remove NS rrset and break from loop (loop limits |
1185 | | * have changed) */ |
1186 | | /* move last rrset into this position (there is no |
1187 | | * additional section any more) */ |
1188 | 0 | verbose(VERB_ALGO, "caps fallback: removing NS rrset"); |
1189 | 0 | if(i < rep->rrset_count-1) |
1190 | 0 | rep->rrsets[i]=rep->rrsets[rep->rrset_count-1]; |
1191 | 0 | rep->rrset_count --; |
1192 | 0 | rep->ns_numrrsets --; |
1193 | 0 | break; |
1194 | 0 | } |
1195 | 0 | } |
1196 | 0 | } |
1197 | | |
1198 | | int caps_failed_rcode(struct reply_info* rep) |
1199 | 0 | { |
1200 | 0 | return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR || |
1201 | 0 | FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN); |
1202 | 0 | } |
1203 | | |
1204 | | void |
1205 | | iter_store_parentside_rrset(struct module_env* env, |
1206 | | struct ub_packed_rrset_key* rrset) |
1207 | 0 | { |
1208 | 0 | struct rrset_ref ref; |
1209 | 0 | rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now); |
1210 | 0 | if(!rrset) { |
1211 | 0 | log_err("malloc failure in store_parentside_rrset"); |
1212 | 0 | return; |
1213 | 0 | } |
1214 | 0 | rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE; |
1215 | 0 | rrset->entry.hash = rrset_key_hash(&rrset->rk); |
1216 | 0 | ref.key = rrset; |
1217 | 0 | ref.id = rrset->id; |
1218 | | /* ignore ret: if it was in the cache, ref updated */ |
1219 | 0 | (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now); |
1220 | 0 | } |
1221 | | |
1222 | | /** fetch NS record from reply, if any */ |
1223 | | static struct ub_packed_rrset_key* |
1224 | | reply_get_NS_rrset(struct reply_info* rep) |
1225 | 0 | { |
1226 | 0 | size_t i; |
1227 | 0 | for(i=0; i<rep->rrset_count; i++) { |
1228 | 0 | if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) { |
1229 | 0 | return rep->rrsets[i]; |
1230 | 0 | } |
1231 | 0 | } |
1232 | 0 | return NULL; |
1233 | 0 | } |
1234 | | |
1235 | | void |
1236 | | iter_store_parentside_NS(struct module_env* env, struct reply_info* rep) |
1237 | 0 | { |
1238 | 0 | struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); |
1239 | 0 | if(rrset) { |
1240 | 0 | log_rrset_key(VERB_ALGO, "store parent-side NS", rrset); |
1241 | 0 | iter_store_parentside_rrset(env, rrset); |
1242 | 0 | } |
1243 | 0 | } |
1244 | | |
1245 | | void iter_store_parentside_neg(struct module_env* env, |
1246 | | struct query_info* qinfo, struct reply_info* rep) |
1247 | 0 | { |
1248 | | /* TTL: NS from referral in iq->deleg_msg, |
1249 | | * or first RR from iq->response, |
1250 | | * or servfail5secs if !iq->response */ |
1251 | 0 | time_t ttl = NORR_TTL; |
1252 | 0 | struct ub_packed_rrset_key* neg; |
1253 | 0 | struct packed_rrset_data* newd; |
1254 | 0 | if(rep) { |
1255 | 0 | struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep); |
1256 | 0 | if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0]; |
1257 | 0 | if(rrset) ttl = ub_packed_rrset_ttl(rrset); |
1258 | 0 | } |
1259 | | /* create empty rrset to store */ |
1260 | 0 | neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch, |
1261 | 0 | sizeof(struct ub_packed_rrset_key)); |
1262 | 0 | if(!neg) { |
1263 | 0 | log_err("out of memory in store_parentside_neg"); |
1264 | 0 | return; |
1265 | 0 | } |
1266 | 0 | memset(&neg->entry, 0, sizeof(neg->entry)); |
1267 | 0 | neg->entry.key = neg; |
1268 | 0 | neg->rk.type = htons(qinfo->qtype); |
1269 | 0 | neg->rk.rrset_class = htons(qinfo->qclass); |
1270 | 0 | neg->rk.flags = 0; |
1271 | 0 | neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname, |
1272 | 0 | qinfo->qname_len); |
1273 | 0 | if(!neg->rk.dname) { |
1274 | 0 | log_err("out of memory in store_parentside_neg"); |
1275 | 0 | return; |
1276 | 0 | } |
1277 | 0 | neg->rk.dname_len = qinfo->qname_len; |
1278 | 0 | neg->entry.hash = rrset_key_hash(&neg->rk); |
1279 | 0 | newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch, |
1280 | 0 | sizeof(struct packed_rrset_data) + sizeof(size_t) + |
1281 | 0 | sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)); |
1282 | 0 | if(!newd) { |
1283 | 0 | log_err("out of memory in store_parentside_neg"); |
1284 | 0 | return; |
1285 | 0 | } |
1286 | 0 | neg->entry.data = newd; |
1287 | 0 | newd->ttl = ttl; |
1288 | | /* entry must have one RR, otherwise not valid in cache. |
1289 | | * put in one RR with empty rdata: those are ignored as nameserver */ |
1290 | 0 | newd->count = 1; |
1291 | 0 | newd->rrsig_count = 0; |
1292 | 0 | newd->trust = rrset_trust_ans_noAA; |
1293 | 0 | newd->rr_len = (size_t*)((uint8_t*)newd + |
1294 | 0 | sizeof(struct packed_rrset_data)); |
1295 | 0 | newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t); |
1296 | 0 | packed_rrset_ptr_fixup(newd); |
1297 | 0 | newd->rr_ttl[0] = newd->ttl; |
1298 | 0 | sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */); |
1299 | | /* store it */ |
1300 | 0 | log_rrset_key(VERB_ALGO, "store parent-side negative", neg); |
1301 | 0 | iter_store_parentside_rrset(env, neg); |
1302 | 0 | } |
1303 | | |
1304 | | int |
1305 | | iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp, |
1306 | | struct regional* region, struct query_info* qinfo) |
1307 | 0 | { |
1308 | 0 | struct ub_packed_rrset_key* akey; |
1309 | 0 | akey = rrset_cache_lookup(env->rrset_cache, dp->name, |
1310 | 0 | dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass, |
1311 | 0 | PACKED_RRSET_PARENT_SIDE, *env->now, 0); |
1312 | 0 | if(akey) { |
1313 | 0 | log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey); |
1314 | 0 | dp->has_parent_side_NS = 1; |
1315 | | /* and mark the new names as lame */ |
1316 | 0 | if(!delegpt_rrset_add_ns(dp, region, akey, 1)) { |
1317 | 0 | lock_rw_unlock(&akey->entry.lock); |
1318 | 0 | return 0; |
1319 | 0 | } |
1320 | 0 | lock_rw_unlock(&akey->entry.lock); |
1321 | 0 | } |
1322 | 0 | return 1; |
1323 | 0 | } |
1324 | | |
1325 | | int iter_lookup_parent_glue_from_cache(struct module_env* env, |
1326 | | struct delegpt* dp, struct regional* region, struct query_info* qinfo) |
1327 | 0 | { |
1328 | 0 | struct ub_packed_rrset_key* akey; |
1329 | 0 | struct delegpt_ns* ns; |
1330 | 0 | size_t num = delegpt_count_targets(dp); |
1331 | 0 | for(ns = dp->nslist; ns; ns = ns->next) { |
1332 | 0 | if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE) |
1333 | 0 | continue; |
1334 | 0 | ns->cache_lookup_count++; |
1335 | | /* get cached parentside A */ |
1336 | 0 | akey = rrset_cache_lookup(env->rrset_cache, ns->name, |
1337 | 0 | ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass, |
1338 | 0 | PACKED_RRSET_PARENT_SIDE, *env->now, 0); |
1339 | 0 | if(akey) { |
1340 | 0 | log_rrset_key(VERB_ALGO, "found parent-side", akey); |
1341 | 0 | ns->done_pside4 = 1; |
1342 | | /* a negative-cache-element has no addresses it adds */ |
1343 | 0 | if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL)) |
1344 | 0 | log_err("malloc failure in lookup_parent_glue"); |
1345 | 0 | lock_rw_unlock(&akey->entry.lock); |
1346 | 0 | } |
1347 | | /* get cached parentside AAAA */ |
1348 | 0 | akey = rrset_cache_lookup(env->rrset_cache, ns->name, |
1349 | 0 | ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass, |
1350 | 0 | PACKED_RRSET_PARENT_SIDE, *env->now, 0); |
1351 | 0 | if(akey) { |
1352 | 0 | log_rrset_key(VERB_ALGO, "found parent-side", akey); |
1353 | 0 | ns->done_pside6 = 1; |
1354 | | /* a negative-cache-element has no addresses it adds */ |
1355 | 0 | if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL)) |
1356 | 0 | log_err("malloc failure in lookup_parent_glue"); |
1357 | 0 | lock_rw_unlock(&akey->entry.lock); |
1358 | 0 | } |
1359 | 0 | } |
1360 | | /* see if new (but lame) addresses have become available */ |
1361 | 0 | return delegpt_count_targets(dp) != num; |
1362 | 0 | } |
1363 | | |
1364 | | int |
1365 | | iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd, |
1366 | | uint16_t* c) |
1367 | 0 | { |
1368 | 0 | uint16_t c1 = *c, c2 = *c; |
1369 | 0 | int r1, r2; |
1370 | 0 | int nolock = 1; |
1371 | | |
1372 | | /* prelock both forwards and hints for atomic read. */ |
1373 | 0 | lock_rw_rdlock(&fwd->lock); |
1374 | 0 | lock_rw_rdlock(&hints->lock); |
1375 | 0 | r1 = hints_next_root(hints, &c1, nolock); |
1376 | 0 | r2 = forwards_next_root(fwd, &c2, nolock); |
1377 | 0 | lock_rw_unlock(&fwd->lock); |
1378 | 0 | lock_rw_unlock(&hints->lock); |
1379 | |
|
1380 | 0 | if(!r1 && !r2) /* got none, end of list */ |
1381 | 0 | return 0; |
1382 | 0 | else if(!r1) /* got one, return that */ |
1383 | 0 | *c = c2; |
1384 | 0 | else if(!r2) |
1385 | 0 | *c = c1; |
1386 | 0 | else if(c1 < c2) /* got both take smallest */ |
1387 | 0 | *c = c1; |
1388 | 0 | else *c = c2; |
1389 | 0 | return 1; |
1390 | 0 | } |
1391 | | |
1392 | | void |
1393 | | iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z) |
1394 | 0 | { |
1395 | | /* Only the DS record for the delegation itself is expected. |
1396 | | * We allow DS for everything between the bailiwick and the |
1397 | | * zonecut, thus DS records must be at or above the zonecut. |
1398 | | * And the DS records must be below the server authority zone. |
1399 | | * The answer section is already scrubbed. */ |
1400 | 0 | size_t i = msg->rep->an_numrrsets; |
1401 | 0 | while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) { |
1402 | 0 | struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; |
1403 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS && |
1404 | 0 | (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname) |
1405 | 0 | || query_dname_compare(z, s->rk.dname) == 0)) { |
1406 | 0 | log_nametypeclass(VERB_ALGO, "removing irrelevant DS", |
1407 | 0 | s->rk.dname, ntohs(s->rk.type), |
1408 | 0 | ntohs(s->rk.rrset_class)); |
1409 | 0 | memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1, |
1410 | 0 | sizeof(struct ub_packed_rrset_key*) * |
1411 | 0 | (msg->rep->rrset_count-i-1)); |
1412 | 0 | msg->rep->ns_numrrsets--; |
1413 | 0 | msg->rep->rrset_count--; |
1414 | | /* stay at same i, but new record */ |
1415 | 0 | continue; |
1416 | 0 | } |
1417 | 0 | i++; |
1418 | 0 | } |
1419 | 0 | } |
1420 | | |
1421 | | void |
1422 | | iter_scrub_nxdomain(struct dns_msg* msg) |
1423 | 0 | { |
1424 | 0 | if(msg->rep->an_numrrsets == 0) |
1425 | 0 | return; |
1426 | | |
1427 | 0 | memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets, |
1428 | 0 | sizeof(struct ub_packed_rrset_key*) * |
1429 | 0 | (msg->rep->rrset_count-msg->rep->an_numrrsets)); |
1430 | 0 | msg->rep->rrset_count -= msg->rep->an_numrrsets; |
1431 | 0 | msg->rep->an_numrrsets = 0; |
1432 | 0 | } |
1433 | | |
1434 | | void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry) |
1435 | 0 | { |
1436 | 0 | struct delegpt_addr* a; |
1437 | 0 | for(a=dp->target_list; a; a = a->next_target) { |
1438 | 0 | if(a->attempts >= outbound_msg_retry) { |
1439 | | /* add back to result list */ |
1440 | 0 | delegpt_add_to_result_list(dp, a); |
1441 | 0 | } |
1442 | 0 | if(a->attempts > d) |
1443 | 0 | a->attempts -= d; |
1444 | 0 | else a->attempts = 0; |
1445 | 0 | } |
1446 | 0 | } |
1447 | | |
1448 | | void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old, |
1449 | | int outbound_msg_retry) |
1450 | 0 | { |
1451 | 0 | struct delegpt_addr* a, *o, *prev; |
1452 | 0 | for(a=dp->target_list; a; a = a->next_target) { |
1453 | 0 | o = delegpt_find_addr(old, &a->addr, a->addrlen); |
1454 | 0 | if(o) { |
1455 | 0 | log_addr(VERB_ALGO, "copy attempt count previous dp", |
1456 | 0 | &a->addr, a->addrlen); |
1457 | 0 | a->attempts = o->attempts; |
1458 | 0 | } |
1459 | 0 | } |
1460 | 0 | prev = NULL; |
1461 | 0 | a = dp->usable_list; |
1462 | 0 | while(a) { |
1463 | 0 | if(a->attempts >= outbound_msg_retry) { |
1464 | 0 | log_addr(VERB_ALGO, "remove from usable list dp", |
1465 | 0 | &a->addr, a->addrlen); |
1466 | | /* remove from result list */ |
1467 | 0 | if(prev) |
1468 | 0 | prev->next_usable = a->next_usable; |
1469 | 0 | else dp->usable_list = a->next_usable; |
1470 | | /* prev stays the same */ |
1471 | 0 | a = a->next_usable; |
1472 | 0 | continue; |
1473 | 0 | } |
1474 | 0 | prev = a; |
1475 | 0 | a = a->next_usable; |
1476 | 0 | } |
1477 | 0 | } |
1478 | | |
1479 | | int |
1480 | | iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp) |
1481 | 0 | { |
1482 | | /* if for query example.com, there is example.com SOA or a subdomain |
1483 | | * of example.com, then we are too low and need to fetch NS. */ |
1484 | 0 | size_t i; |
1485 | | /* if we have a DNAME or CNAME we are probably wrong */ |
1486 | | /* if we have a qtype DS in the answer section, its fine */ |
1487 | 0 | for(i=0; i < msg->rep->an_numrrsets; i++) { |
1488 | 0 | struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; |
1489 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME || |
1490 | 0 | ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) { |
1491 | | /* not the right answer, maybe too low, check the |
1492 | | * RRSIG signer name (if there is any) for a hint |
1493 | | * that it is from the dp zone anyway */ |
1494 | 0 | uint8_t* sname; |
1495 | 0 | size_t slen; |
1496 | 0 | val_find_rrset_signer(s, &sname, &slen); |
1497 | 0 | if(sname && query_dname_compare(dp->name, sname)==0) |
1498 | 0 | return 0; /* it is fine, from the right dp */ |
1499 | 0 | return 1; |
1500 | 0 | } |
1501 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS) |
1502 | 0 | return 0; /* fine, we have a DS record */ |
1503 | 0 | } |
1504 | 0 | for(i=msg->rep->an_numrrsets; |
1505 | 0 | i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) { |
1506 | 0 | struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; |
1507 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) { |
1508 | 0 | if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname)) |
1509 | 0 | return 1; /* point is too low */ |
1510 | 0 | if(query_dname_compare(s->rk.dname, dp->name)==0) |
1511 | 0 | return 0; /* right dp */ |
1512 | 0 | } |
1513 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC || |
1514 | 0 | ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { |
1515 | 0 | uint8_t* sname; |
1516 | 0 | size_t slen; |
1517 | 0 | val_find_rrset_signer(s, &sname, &slen); |
1518 | 0 | if(sname && query_dname_compare(dp->name, sname)==0) |
1519 | 0 | return 0; /* it is fine, from the right dp */ |
1520 | 0 | return 1; |
1521 | 0 | } |
1522 | 0 | } |
1523 | | /* we do not know */ |
1524 | 0 | return 1; |
1525 | 0 | } |
1526 | | |
1527 | | int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp) |
1528 | 0 | { |
1529 | | /* no delegation point, do not see how we can go down, |
1530 | | * robust check, it should really exist */ |
1531 | 0 | if(!dp) return 0; |
1532 | | |
1533 | | /* see if dp equals the qname, then we cannot go down further */ |
1534 | 0 | if(query_dname_compare(qinfo->qname, dp->name) == 0) |
1535 | 0 | return 0; |
1536 | | /* if dp is one label above the name we also cannot go down further */ |
1537 | 0 | if(dname_count_labels(qinfo->qname) == dp->namelabs+1) |
1538 | 0 | return 0; |
1539 | 0 | return 1; |
1540 | 0 | } |
1541 | | |
1542 | | int |
1543 | | iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf, |
1544 | | uint8_t** retdpname, size_t* retdpnamelen, uint8_t* dpname_storage, |
1545 | | size_t dpname_storage_len) |
1546 | 0 | { |
1547 | 0 | struct iter_hints_stub *stub; |
1548 | 0 | struct delegpt *dp; |
1549 | 0 | int nolock = 1; |
1550 | |
|
1551 | 0 | log_assert((retdpname && retdpnamelen |
1552 | 0 | && dpname_storage && dpname_storage_len > 0) || |
1553 | 0 | (retdpname == NULL && retdpnamelen == NULL |
1554 | 0 | && dpname_storage == NULL && dpname_storage_len == 0)); |
1555 | | |
1556 | | /* Check for stub. */ |
1557 | | /* Lock both forwards and hints for atomic read. */ |
1558 | 0 | lock_rw_rdlock(&qstate->env->fwds->lock); |
1559 | 0 | lock_rw_rdlock(&qstate->env->hints->lock); |
1560 | 0 | stub = hints_lookup_stub(qstate->env->hints, qinf->qname, |
1561 | 0 | qinf->qclass, NULL, nolock); |
1562 | 0 | dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass, |
1563 | 0 | nolock); |
1564 | | |
1565 | | /* see if forward or stub is more pertinent */ |
1566 | 0 | if(stub && stub->dp && dp) { |
1567 | 0 | if(dname_strict_subdomain(dp->name, dp->namelabs, |
1568 | 0 | stub->dp->name, stub->dp->namelabs)) { |
1569 | 0 | stub = NULL; /* ignore stub, forward is lower */ |
1570 | 0 | } else { |
1571 | 0 | dp = NULL; /* ignore forward, stub is lower */ |
1572 | 0 | } |
1573 | 0 | } |
1574 | | |
1575 | | /* check stub */ |
1576 | 0 | if (stub != NULL && stub->dp != NULL) { |
1577 | 0 | enum verbosity_value level = VERB_ALGO; |
1578 | 0 | int stub_no_cache = stub->dp->no_cache; |
1579 | 0 | lock_rw_unlock(&qstate->env->fwds->lock); |
1580 | 0 | if(verbosity >= level && stub_no_cache) { |
1581 | 0 | char qname[LDNS_MAX_DOMAINLEN]; |
1582 | 0 | char dpname[LDNS_MAX_DOMAINLEN]; |
1583 | 0 | dname_str(qinf->qname, qname); |
1584 | 0 | dname_str(stub->dp->name, dpname); |
1585 | 0 | verbose(level, "stub for %s %s has no_cache", qname, dpname); |
1586 | 0 | } |
1587 | 0 | if(retdpname) { |
1588 | 0 | if(stub->dp->namelen > dpname_storage_len) { |
1589 | 0 | verbose(VERB_ALGO, "no cache stub dpname too long"); |
1590 | 0 | lock_rw_unlock(&qstate->env->hints->lock); |
1591 | 0 | *retdpname = NULL; |
1592 | 0 | *retdpnamelen = 0; |
1593 | 0 | return stub_no_cache; |
1594 | 0 | } |
1595 | 0 | memmove(dpname_storage, stub->dp->name, |
1596 | 0 | stub->dp->namelen); |
1597 | 0 | *retdpname = dpname_storage; |
1598 | 0 | *retdpnamelen = stub->dp->namelen; |
1599 | 0 | } |
1600 | 0 | lock_rw_unlock(&qstate->env->hints->lock); |
1601 | 0 | return stub_no_cache; |
1602 | 0 | } |
1603 | | |
1604 | | /* Check for forward. */ |
1605 | 0 | if (dp) { |
1606 | 0 | enum verbosity_value level = VERB_ALGO; |
1607 | 0 | int dp_no_cache = dp->no_cache; |
1608 | 0 | lock_rw_unlock(&qstate->env->hints->lock); |
1609 | 0 | if(verbosity >= level && dp_no_cache) { |
1610 | 0 | char qname[LDNS_MAX_DOMAINLEN]; |
1611 | 0 | char dpname[LDNS_MAX_DOMAINLEN]; |
1612 | 0 | dname_str(qinf->qname, qname); |
1613 | 0 | dname_str(dp->name, dpname); |
1614 | 0 | verbose(level, "forward for %s %s has no_cache", qname, dpname); |
1615 | 0 | } |
1616 | 0 | if(retdpname) { |
1617 | 0 | if(dp->namelen > dpname_storage_len) { |
1618 | 0 | verbose(VERB_ALGO, "no cache dpname too long"); |
1619 | 0 | lock_rw_unlock(&qstate->env->fwds->lock); |
1620 | 0 | *retdpname = NULL; |
1621 | 0 | *retdpnamelen = 0; |
1622 | 0 | return dp_no_cache; |
1623 | 0 | } |
1624 | 0 | memmove(dpname_storage, dp->name, dp->namelen); |
1625 | 0 | *retdpname = dpname_storage; |
1626 | 0 | *retdpnamelen = dp->namelen; |
1627 | 0 | } |
1628 | 0 | lock_rw_unlock(&qstate->env->fwds->lock); |
1629 | 0 | return dp_no_cache; |
1630 | 0 | } |
1631 | 0 | lock_rw_unlock(&qstate->env->fwds->lock); |
1632 | 0 | lock_rw_unlock(&qstate->env->hints->lock); |
1633 | 0 | if(retdpname) { |
1634 | 0 | *retdpname = NULL; |
1635 | 0 | *retdpnamelen = 0; |
1636 | 0 | } |
1637 | 0 | return 0; |
1638 | 0 | } |
1639 | | |
1640 | | void iterator_set_ip46_support(struct module_stack* mods, |
1641 | | struct module_env* env, struct outside_network* outnet) |
1642 | 0 | { |
1643 | 0 | int m = modstack_find(mods, "iterator"); |
1644 | 0 | struct iter_env* ie = NULL; |
1645 | 0 | if(m == -1) |
1646 | 0 | return; |
1647 | 0 | ie = (struct iter_env*)env->modinfo[m]; |
1648 | 0 | if(outnet->pending == NULL) |
1649 | 0 | return; /* we are in testbound, no rbtree for UDP */ |
1650 | 0 | if(outnet->num_ip4 == 0) |
1651 | 0 | ie->supports_ipv4 = 0; |
1652 | 0 | if(outnet->num_ip6 == 0) |
1653 | 0 | ie->supports_ipv6 = 0; |
1654 | 0 | } |
1655 | | |
1656 | | void |
1657 | | limit_nsec_ttl(struct dns_msg* msg) |
1658 | 0 | { |
1659 | | /* Limit NSEC and NSEC3 TTL in response, RFC9077 */ |
1660 | 0 | size_t i; |
1661 | 0 | int found = 0; |
1662 | 0 | time_t soa_ttl = 0; |
1663 | | /* Limit the NSEC and NSEC3 TTL values to the SOA TTL and SOA minimum |
1664 | | * TTL. That has already been applied to the SOA record ttl. */ |
1665 | 0 | for(i=0; i<msg->rep->rrset_count; i++) { |
1666 | 0 | struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; |
1667 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) { |
1668 | 0 | struct packed_rrset_data* soadata = (struct packed_rrset_data*)s->entry.data; |
1669 | 0 | found = 1; |
1670 | 0 | soa_ttl = soadata->ttl; |
1671 | 0 | break; |
1672 | 0 | } |
1673 | 0 | } |
1674 | 0 | if(!found) |
1675 | 0 | return; |
1676 | 0 | for(i=0; i<msg->rep->rrset_count; i++) { |
1677 | 0 | struct ub_packed_rrset_key* s = msg->rep->rrsets[i]; |
1678 | 0 | if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC || |
1679 | 0 | ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { |
1680 | 0 | struct packed_rrset_data* data = (struct packed_rrset_data*)s->entry.data; |
1681 | | /* Limit the negative TTL. */ |
1682 | 0 | if(data->ttl > soa_ttl) { |
1683 | 0 | if(verbosity >= VERB_ALGO) { |
1684 | 0 | char buf[256]; |
1685 | 0 | snprintf(buf, sizeof(buf), |
1686 | 0 | "limiting TTL %d of %s record to the SOA TTL of %d for", |
1687 | 0 | (int)data->ttl, ((ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC)?"NSEC":"NSEC3"), (int)soa_ttl); |
1688 | 0 | log_nametypeclass(VERB_ALGO, buf, |
1689 | 0 | s->rk.dname, ntohs(s->rk.type), |
1690 | 0 | ntohs(s->rk.rrset_class)); |
1691 | 0 | } |
1692 | 0 | data->ttl = soa_ttl; |
1693 | 0 | } |
1694 | 0 | } |
1695 | 0 | } |
1696 | 0 | } |
1697 | | |
1698 | | void |
1699 | | iter_make_minimal(struct reply_info* rep) |
1700 | 0 | { |
1701 | 0 | size_t rem = rep->ns_numrrsets + rep->ar_numrrsets; |
1702 | 0 | rep->ns_numrrsets = 0; |
1703 | 0 | rep->ar_numrrsets = 0; |
1704 | 0 | rep->rrset_count -= rem; |
1705 | 0 | } |