2 * iterator/iter_utils.c - iterative resolver module utility functions.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions to assist the iterator module.
40 * Configuration options. Forward zones.
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 "util/net_help.h"
54 #include "util/module.h"
56 #include "util/config_file.h"
57 #include "util/regional.h"
58 #include "util/data/msgparse.h"
59 #include "util/data/dname.h"
60 #include "util/random.h"
61 #include "util/fptr_wlist.h"
62 #include "validator/val_anchor.h"
63 #include "validator/val_kcache.h"
64 #include "validator/val_kentry.h"
65 #include "validator/val_utils.h"
66 #include "validator/val_sigcrypt.h"
67 #include "sldns/sbuffer.h"
68 #include "sldns/str2wire.h"
70 /** time when nameserver glue is said to be 'recent' */
71 #define SUSPICION_RECENT_EXPIRY 86400
72 /** penalty to validation failed blacklisted IPs */
73 #define BLACKLIST_PENALTY (USEFUL_SERVER_TOP_TIMEOUT*4)
75 /** fillup fetch policy array */
77 fetch_fill(struct iter_env* ie, const char* str)
79 char* s = (char*)str, *e;
81 for(i=0; i<ie->max_dependency_depth+1; i++) {
82 ie->target_fetch_policy[i] = strtol(s, &e, 10);
84 fatal_exit("cannot parse fetch policy number %s", s);
89 /** Read config string that represents the target fetch policy */
91 read_fetch_policy(struct iter_env* ie, const char* str)
93 int count = cfg_count_numbers(str);
95 log_err("Cannot parse target fetch policy: \"%s\"", str);
98 ie->max_dependency_depth = count - 1;
99 ie->target_fetch_policy = (int*)calloc(
100 (size_t)ie->max_dependency_depth+1, sizeof(int));
101 if(!ie->target_fetch_policy) {
102 log_err("alloc fetch policy: out of memory");
109 /** apply config caps whitelist items to name tree */
111 caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg)
113 struct config_strlist* p;
114 for(p=cfg->caps_whitelist; p; p=p->next) {
115 struct name_tree_node* n;
117 uint8_t* nm = sldns_str2wire_dname(p->str, &len);
119 log_err("could not parse %s", p->str);
122 n = (struct name_tree_node*)calloc(1, sizeof(*n));
124 log_err("out of memory");
131 n->labs = dname_count_labels(nm);
132 n->dclass = LDNS_RR_CLASS_IN;
133 if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) {
134 /* duplicate element ignored, idempotent */
139 name_tree_init_parents(ntree);
144 iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
147 /* target fetch policy */
148 if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
150 for(i=0; i<iter_env->max_dependency_depth+1; i++)
151 verbose(VERB_QUERY, "target fetch policy for level %d is %d",
152 i, iter_env->target_fetch_policy[i]);
154 if(!iter_env->donotq)
155 iter_env->donotq = donotq_create();
156 if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
157 log_err("Could not set donotqueryaddresses");
161 iter_env->priv = priv_create();
162 if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
163 log_err("Could not set private addresses");
166 if(cfg->caps_whitelist) {
167 if(!iter_env->caps_white)
168 iter_env->caps_white = rbtree_create(name_tree_compare);
169 if(!iter_env->caps_white || !caps_white_apply_cfg(
170 iter_env->caps_white, cfg)) {
171 log_err("Could not set capsforid whitelist");
176 iter_env->supports_ipv6 = cfg->do_ip6;
177 iter_env->supports_ipv4 = cfg->do_ip4;
181 /** filter out unsuitable targets
182 * @param iter_env: iterator environment with ipv6-support flag.
183 * @param env: module environment with infra cache.
184 * @param name: zone name
185 * @param namelen: length of name
186 * @param qtype: query type (host order).
187 * @param now: current time
188 * @param a: address in delegation point we are examining.
189 * @return an integer that signals the target suitability.
191 * -1: The address should be omitted from the list.
193 * o The address is bogus (DNSSEC validation failure).
194 * o Listed as donotquery
195 * o is ipv6 but no ipv6 support (in operating system).
196 * o is ipv4 but no ipv4 support (in operating system).
198 * Otherwise, an rtt in milliseconds.
199 * 0 .. USEFUL_SERVER_TOP_TIMEOUT-1
200 * The roundtrip time timeout estimate. less than 2 minutes.
201 * Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
202 * values 0 .. 49 are not used, unless that is changed.
203 * USEFUL_SERVER_TOP_TIMEOUT
204 * This value exactly is given for unresponsive blacklisted.
205 * USEFUL_SERVER_TOP_TIMEOUT+1
206 * For non-blacklisted servers: huge timeout, but has traffic.
207 * USEFUL_SERVER_TOP_TIMEOUT*1 ..
208 * parent-side lame servers get this penalty. A dispreferential
209 * server. (lame in delegpt).
210 * USEFUL_SERVER_TOP_TIMEOUT*2 ..
211 * dnsseclame servers get penalty
212 * USEFUL_SERVER_TOP_TIMEOUT*3 ..
213 * recursion lame servers get penalty
214 * UNKNOWN_SERVER_NICENESS
215 * If no information is known about the server, this is
216 * returned. 376 msec or so.
217 * +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
219 * When a final value is chosen that is dnsseclame ; dnsseclameness checking
220 * is turned off (so we do not discard the reply).
221 * When a final value is chosen that is recursionlame; RD bit is set on query.
222 * Because of the numbers this means recursionlame also have dnssec lameness
223 * checking turned off.
226 iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
227 uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
228 struct delegpt_addr* a)
230 int rtt, lame, reclame, dnsseclame;
232 return -1; /* address of server is bogus */
233 if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
234 log_addr(VERB_ALGO, "skip addr on the donotquery list",
235 &a->addr, a->addrlen);
236 return -1; /* server is on the donotquery list */
238 if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
239 return -1; /* there is no ip6 available */
241 if(!iter_env->supports_ipv4 && !addr_is_ip6(&a->addr, a->addrlen)) {
242 return -1; /* there is no ip4 available */
244 /* check lameness - need zone , class info */
245 if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
246 name, namelen, qtype, &lame, &dnsseclame, &reclame,
248 log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
249 verbose(VERB_ALGO, " rtt=%d%s%s%s%s", rtt,
251 dnsseclame?" DNSSEC_LAME":"",
252 reclame?" REC_LAME":"",
253 a->lame?" ADDR_LAME":"");
255 return -1; /* server is lame */
256 else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
257 /* server is unresponsive,
258 * we used to return TOP_TIMEOUT, but fairly useless,
259 * because if == TOP_TIMEOUT is dropped because
260 * blacklisted later, instead, remove it here, so
261 * other choices (that are not blacklisted) can be
264 /* select remainder from worst to best */
266 return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
267 else if(dnsseclame || a->dnsseclame)
268 return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
270 return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
273 /* no server information present */
275 return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
277 return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
278 return UNKNOWN_SERVER_NICENESS;
281 /** lookup RTT information, and also store fastest rtt (if any) */
283 iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
284 uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
285 struct delegpt* dp, int* best_rtt, struct sock_list* blacklist,
286 size_t* num_suitable_results)
289 struct delegpt_addr* a;
290 *num_suitable_results = 0;
293 return 0; /* NS bogus, all bogus, nothing found */
294 for(a=dp->result_list; a; a = a->next_result) {
295 a->sel_rtt = iter_filter_unsuitable(iter_env, env,
296 name, namelen, qtype, now, a);
297 if(a->sel_rtt != -1) {
298 if(sock_list_find(blacklist, &a->addr, a->addrlen))
299 a->sel_rtt += BLACKLIST_PENALTY;
302 *best_rtt = a->sel_rtt;
304 } else if(a->sel_rtt < *best_rtt) {
305 *best_rtt = a->sel_rtt;
307 (*num_suitable_results)++;
313 /** compare two rtts, return -1, 0 or 1 */
315 rtt_compare(const void* x, const void* y)
317 if(*(int*)x == *(int*)y)
319 if(*(int*)x > *(int*)y)
324 /** get RTT for the Nth fastest server */
326 nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n)
330 int* rtt_list, *rtt_index;
332 if(num_results < 1 || n >= num_results) {
336 rtt_list = calloc(num_results, sizeof(int));
338 log_err("malloc failure: allocating rtt_list");
341 rtt_index = rtt_list;
343 for(i=0; i<num_results && result_list; i++) {
344 if(result_list->sel_rtt != -1) {
345 *rtt_index = result_list->sel_rtt;
348 result_list=result_list->next_result;
350 qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare);
353 rtt_band = rtt_list[n-1];
359 /** filter the address list, putting best targets at front,
360 * returns number of best targets (or 0, no suitable targets) */
362 iter_filter_order(struct iter_env* iter_env, struct module_env* env,
363 uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
364 struct delegpt* dp, int* selected_rtt, int open_target,
365 struct sock_list* blacklist, time_t prefetch)
367 int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth;
369 struct delegpt_addr* a, *n, *prev=NULL;
371 /* fillup sel_rtt and find best rtt in the bunch */
372 got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
373 &low_rtt, blacklist, &num_results);
376 if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
377 (delegpt_count_missing_targets(dp) > 0 || open_target > 0)) {
378 verbose(VERB_ALGO, "Bad choices, trying to get more choice");
379 return 0; /* we want more choice. The best choice is a bad one.
380 return 0 to force the caller to fetch more */
383 if(env->cfg->fast_server_permil != 0 && prefetch == 0 &&
384 num_results > env->cfg->fast_server_num &&
385 ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) {
386 /* the query is not prefetch, but for a downstream client,
387 * there are more servers available then the fastest N we want
388 * to choose from. Limit our choice to the fastest servers. */
389 nth = nth_rtt(dp->result_list, num_results,
390 env->cfg->fast_server_num);
392 rtt_band = nth - low_rtt;
393 if(rtt_band > RTT_BAND)
401 /* skip unsuitable targets */
402 if(a->sel_rtt == -1) {
407 /* classify the server address and determine what to do */
409 if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) {
412 } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) {
416 /* swap to front if necessary, or move to next result */
417 if(swap_to_front && prev) {
419 prev->next_result = n;
420 a->next_result = dp->result_list;
428 *selected_rtt = low_rtt;
430 if (env->cfg->prefer_ip6) {
434 int attempt = -1; /* filter to make sure addresses have
435 less attempts on them than the first, to force round
436 robin when all the IPv6 addresses fail */
437 int num4ok = 0; /* number ip4 at low attempt count */
441 for(i = 0; i < got_num; i++) {
443 if(a->addr.ss_family != AF_INET6 && attempt == -1) {
444 /* if we only have ip4 at low attempt count,
445 * then ip6 is failing, and we need to
446 * select one of the remaining IPv4 addrs */
447 attempt = a->attempts;
449 num4_lowrtt = a->sel_rtt;
450 } else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) {
452 if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) {
453 num4_lowrtt = a->sel_rtt;
456 if(a->addr.ss_family == AF_INET6) {
458 attempt = a->attempts;
459 } else if(a->attempts > attempt) {
464 if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) {
465 low_rtt6 = a->sel_rtt;
468 /* swap to front if IPv6, or move to next result */
469 if(swap_to_front && prev) {
471 prev->next_result = n;
472 a->next_result = dp->result_list;
482 *selected_rtt = low_rtt6;
483 } else if(num4ok > 0) {
485 *selected_rtt = num4_lowrtt;
492 iter_server_selection(struct iter_env* iter_env,
493 struct module_env* env, struct delegpt* dp,
494 uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
495 int* chase_to_rd, int open_target, struct sock_list* blacklist,
500 struct delegpt_addr* a, *prev;
501 int num = iter_filter_order(iter_env, env, name, namelen, qtype,
502 *env->now, dp, &selrtt, open_target, blacklist, prefetch);
506 verbose(VERB_ALGO, "selrtt %d", selrtt);
507 if(selrtt > BLACKLIST_PENALTY) {
508 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
509 verbose(VERB_ALGO, "chase to "
510 "blacklisted recursion lame server");
513 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
514 verbose(VERB_ALGO, "chase to "
515 "blacklisted dnssec lame server");
519 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
520 verbose(VERB_ALGO, "chase to recursion lame server");
523 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
524 verbose(VERB_ALGO, "chase to dnssec lame server");
527 if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
528 verbose(VERB_ALGO, "chase to blacklisted lame server");
535 if(++a->attempts < OUTBOUND_MSG_RETRY)
537 dp->result_list = a->next_result;
541 /* randomly select a target from the list */
543 /* grab secure random number, to pick unexpected server.
544 * also we need it to be threadsafe. */
545 sel = ub_random_max(env->rnd, num);
548 while(sel > 0 && a) {
553 if(!a) /* robustness */
555 if(++a->attempts < OUTBOUND_MSG_RETRY)
557 /* remove it from the delegation point result list */
559 prev->next_result = a->next_result;
560 else dp->result_list = a->next_result;
565 dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg,
566 struct regional* region)
568 struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
569 sizeof(struct dns_msg));
572 memset(m, 0, sizeof(*m));
573 if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
574 log_err("malloc failure: allocating incoming dns_msg");
581 dns_copy_msg(struct dns_msg* from, struct regional* region)
583 struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
584 sizeof(struct dns_msg));
587 m->qinfo = from->qinfo;
588 if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
589 from->qinfo.qname_len)))
591 if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
597 iter_dns_store(struct module_env* env, struct query_info* msgqinf,
598 struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
599 struct regional* region, uint16_t flags)
601 if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
602 pside, region, flags))
603 log_err("out of memory: cannot store data in cache");
607 iter_ns_probability(struct ub_randstate* rnd, int n, int m)
610 if(n == m) /* 100% chance */
612 /* we do not need secure random numbers here, but
613 * we do need it to be threadsafe, so we use this */
614 sel = ub_random_max(rnd, m);
618 /** detect dependency cycle for query and target */
620 causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
621 uint16_t t, uint16_t c)
623 struct query_info qinf;
625 qinf.qname_len = namelen;
628 qinf.local_alias = NULL;
629 fptr_ok(fptr_whitelist_modenv_detect_cycle(
630 qstate->env->detect_cycle));
631 return (*qstate->env->detect_cycle)(qstate, &qinf,
632 (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming,
637 iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
639 struct delegpt_ns* ns;
640 for(ns = dp->nslist; ns; ns = ns->next) {
643 /* see if this ns as target causes dependency cycle */
644 if(causes_cycle(qstate, ns->name, ns->namelen,
645 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
646 causes_cycle(qstate, ns->name, ns->namelen,
647 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
648 log_nametypeclass(VERB_QUERY, "skipping target due "
649 "to dependency cycle (harden-glue: no may "
650 "fix some of the cycles)",
651 ns->name, LDNS_RR_TYPE_A,
652 qstate->qinfo.qclass);
659 iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
661 struct delegpt_ns* ns;
662 for(ns = dp->nslist; ns; ns = ns->next) {
663 if(ns->done_pside4 && ns->done_pside6)
665 /* see if this ns as target causes dependency cycle */
666 if(causes_cycle(qstate, ns->name, ns->namelen,
667 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
668 log_nametypeclass(VERB_QUERY, "skipping target due "
669 "to dependency cycle", ns->name,
670 LDNS_RR_TYPE_A, qstate->qinfo.qclass);
673 if(causes_cycle(qstate, ns->name, ns->namelen,
674 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
675 log_nametypeclass(VERB_QUERY, "skipping target due "
676 "to dependency cycle", ns->name,
677 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
684 iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
687 struct delegpt_ns* ns;
690 * o no addresses are provided.
691 * o all NS items are required glue.
694 * o no addresses are provided.
695 * o the query is for one of the nameservers in dp,
696 * and that nameserver is a glue-name for this dp.
700 /* either available or unused targets */
701 if(dp->usable_list || dp->result_list)
704 /* see if query is for one of the nameservers, which is glue */
705 if( (qinfo->qtype == LDNS_RR_TYPE_A ||
706 qinfo->qtype == LDNS_RR_TYPE_AAAA) &&
707 dname_subdomain_c(qinfo->qname, dp->name) &&
708 delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
711 for(ns = dp->nslist; ns; ns = ns->next) {
712 if(ns->resolved) /* skip failed targets */
714 if(!dname_subdomain_c(ns->name, dp->name))
715 return 0; /* one address is not required glue */
721 iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo)
723 struct trust_anchor* a;
724 if(!env || !env->anchors || !qinfo || !qinfo->qname)
726 /* a trust anchor exists above the name? */
727 if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len,
729 if(a->numDS == 0 && a->numDNSKEY == 0) {
730 /* insecure trust point */
731 lock_basic_unlock(&a->lock);
734 lock_basic_unlock(&a->lock);
737 /* no trust anchor above it. */
742 iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
743 struct dns_msg* msg, uint16_t dclass)
745 struct trust_anchor* a;
746 /* information not available, !env->anchors can be common */
747 if(!env || !env->anchors || !dp || !dp->name)
749 /* a trust anchor exists with this name, RRSIGs expected */
750 if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
752 if(a->numDS == 0 && a->numDNSKEY == 0) {
753 /* insecure trust point */
754 lock_basic_unlock(&a->lock);
757 lock_basic_unlock(&a->lock);
760 /* see if DS rrset was given, in AUTH section */
761 if(msg && msg->rep &&
762 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
763 LDNS_RR_TYPE_DS, dclass))
765 /* look in key cache */
767 struct key_entry_key* kk = key_cache_obtain(env->key_cache,
768 dp->name, dp->namelen, dclass, env->scratch, *env->now);
770 if(query_dname_compare(kk->name, dp->name) == 0) {
771 if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
772 regional_free_all(env->scratch);
774 } else if(key_entry_isnull(kk)) {
775 regional_free_all(env->scratch);
779 regional_free_all(env->scratch);
786 iter_msg_has_dnssec(struct dns_msg* msg)
789 if(!msg || !msg->rep)
791 for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
792 if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
793 entry.data)->rrsig_count > 0)
796 /* empty message has no DNSSEC info, with DNSSEC the reply is
797 * not empty (NSEC) */
801 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
802 enum response_type type, uint16_t dclass)
804 if(!msg || !dp || !msg->rep || !dp->name)
806 /* SOA RRset - always from reply zone */
807 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
808 LDNS_RR_TYPE_SOA, dclass) ||
809 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
810 LDNS_RR_TYPE_SOA, dclass))
812 if(type == RESPONSE_TYPE_REFERRAL) {
814 /* if it adds a single label, i.e. we expect .com,
815 * and referral to example.com. NS ... , then origin zone
816 * is .com. For a referral to sub.example.com. NS ... then
817 * we do not know, since example.com. may be in between. */
818 for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
820 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
821 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
822 ntohs(s->rk.rrset_class) == dclass) {
823 int l = dname_count_labels(s->rk.dname);
824 if(l == dp->namelabs + 1 &&
825 dname_strict_subdomain(s->rk.dname,
826 l, dp->name, dp->namelabs))
832 log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
833 /* not a referral, and not lame delegation (upwards), so,
834 * any NS rrset must be from the zone itself */
835 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
836 LDNS_RR_TYPE_NS, dclass) ||
837 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
838 LDNS_RR_TYPE_NS, dclass))
840 /* a DNSKEY set is expected at the zone apex as well */
841 /* this is for 'minimal responses' for DNSKEYs */
842 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
843 LDNS_RR_TYPE_DNSKEY, dclass))
849 * check equality of two rrsets
852 * @return true if equal
855 rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
857 struct packed_rrset_data* d1 = (struct packed_rrset_data*)
859 struct packed_rrset_data* d2 = (struct packed_rrset_data*)
862 if(k1->rk.dname_len != k2->rk.dname_len ||
863 k1->rk.flags != k2->rk.flags ||
864 k1->rk.type != k2->rk.type ||
865 k1->rk.rrset_class != k2->rk.rrset_class ||
866 query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
868 if( /* do not check ttl: d1->ttl != d2->ttl || */
869 d1->count != d2->count ||
870 d1->rrsig_count != d2->rrsig_count ||
871 d1->trust != d2->trust ||
872 d1->security != d2->security)
874 t = d1->count + d1->rrsig_count;
876 if(d1->rr_len[i] != d2->rr_len[i] ||
877 /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/
878 memcmp(d1->rr_data[i], d2->rr_data[i],
885 /** compare rrsets and sort canonically. Compares rrset name, type, class.
886 * return 0 if equal, +1 if x > y, and -1 if x < y.
889 rrset_canonical_sort_cmp(const void* x, const void* y)
891 struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x;
892 struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y;
893 int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname);
896 if(rrx->rk.type != rry->rk.type) {
897 if(ntohs(rrx->rk.type) > ntohs(rry->rk.type))
901 if(rrx->rk.rrset_class != rry->rk.rrset_class) {
902 if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class))
910 reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region)
913 struct ub_packed_rrset_key** sorted_p, **sorted_q;
914 if(p->flags != q->flags ||
915 p->qdcount != q->qdcount ||
916 /* do not check TTL, this may differ */
919 p->prefetch_ttl != q->prefetch_ttl ||
921 p->security != q->security ||
922 p->an_numrrsets != q->an_numrrsets ||
923 p->ns_numrrsets != q->ns_numrrsets ||
924 p->ar_numrrsets != q->ar_numrrsets ||
925 p->rrset_count != q->rrset_count)
927 /* sort the rrsets in the authority and additional sections before
928 * compare, the query and answer sections are ordered in the sequence
929 * they should have (eg. one after the other for aliases). */
930 sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init(
931 region, p->rrsets, sizeof(*sorted_p)*p->rrset_count);
932 if(!sorted_p) return 0;
933 log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <=
935 qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets,
936 sizeof(*sorted_p), rrset_canonical_sort_cmp);
937 qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets,
938 sizeof(*sorted_p), rrset_canonical_sort_cmp);
940 sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init(
941 region, q->rrsets, sizeof(*sorted_q)*q->rrset_count);
943 regional_free_all(region);
946 log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <=
948 qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets,
949 sizeof(*sorted_q), rrset_canonical_sort_cmp);
950 qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets,
951 sizeof(*sorted_q), rrset_canonical_sort_cmp);
953 /* compare the rrsets */
954 for(i=0; i<p->rrset_count; i++) {
955 if(!rrset_equal(sorted_p[i], sorted_q[i])) {
956 if(!rrset_canonical_equal(region, sorted_p[i],
958 regional_free_all(region);
963 regional_free_all(region);
968 caps_strip_reply(struct reply_info* rep)
972 /* see if message is a referral, in which case the additional and
973 * NS record cannot be removed */
974 /* referrals have the AA flag unset (strict check, not elsewhere in
975 * unbound, but for 0x20 this is very convenient). */
976 if(!(rep->flags&BIT_AA))
978 /* remove the additional section from the reply */
979 if(rep->ar_numrrsets != 0) {
980 verbose(VERB_ALGO, "caps fallback: removing additional section");
981 rep->rrset_count -= rep->ar_numrrsets;
982 rep->ar_numrrsets = 0;
984 /* is there an NS set in the authority section to remove? */
985 /* the failure case (Cisco firewalls) only has one rrset in authsec */
986 for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
987 struct ub_packed_rrset_key* s = rep->rrsets[i];
988 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) {
989 /* remove NS rrset and break from loop (loop limits
991 /* move last rrset into this position (there is no
992 * additional section any more) */
993 verbose(VERB_ALGO, "caps fallback: removing NS rrset");
994 if(i < rep->rrset_count-1)
995 rep->rrsets[i]=rep->rrsets[rep->rrset_count-1];
997 rep->ns_numrrsets --;
1003 int caps_failed_rcode(struct reply_info* rep)
1005 return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
1006 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN);
1010 iter_store_parentside_rrset(struct module_env* env,
1011 struct ub_packed_rrset_key* rrset)
1013 struct rrset_ref ref;
1014 rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
1016 log_err("malloc failure in store_parentside_rrset");
1019 rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
1020 rrset->entry.hash = rrset_key_hash(&rrset->rk);
1023 /* ignore ret: if it was in the cache, ref updated */
1024 (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
1027 /** fetch NS record from reply, if any */
1028 static struct ub_packed_rrset_key*
1029 reply_get_NS_rrset(struct reply_info* rep)
1032 for(i=0; i<rep->rrset_count; i++) {
1033 if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
1034 return rep->rrsets[i];
1041 iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
1043 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1045 log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
1046 iter_store_parentside_rrset(env, rrset);
1050 void iter_store_parentside_neg(struct module_env* env,
1051 struct query_info* qinfo, struct reply_info* rep)
1053 /* TTL: NS from referral in iq->deleg_msg,
1054 * or first RR from iq->response,
1055 * or servfail5secs if !iq->response */
1056 time_t ttl = NORR_TTL;
1057 struct ub_packed_rrset_key* neg;
1058 struct packed_rrset_data* newd;
1060 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1061 if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
1062 if(rrset) ttl = ub_packed_rrset_ttl(rrset);
1064 /* create empty rrset to store */
1065 neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
1066 sizeof(struct ub_packed_rrset_key));
1068 log_err("out of memory in store_parentside_neg");
1071 memset(&neg->entry, 0, sizeof(neg->entry));
1072 neg->entry.key = neg;
1073 neg->rk.type = htons(qinfo->qtype);
1074 neg->rk.rrset_class = htons(qinfo->qclass);
1076 neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
1078 if(!neg->rk.dname) {
1079 log_err("out of memory in store_parentside_neg");
1082 neg->rk.dname_len = qinfo->qname_len;
1083 neg->entry.hash = rrset_key_hash(&neg->rk);
1084 newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
1085 sizeof(struct packed_rrset_data) + sizeof(size_t) +
1086 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t));
1088 log_err("out of memory in store_parentside_neg");
1091 neg->entry.data = newd;
1093 /* entry must have one RR, otherwise not valid in cache.
1094 * put in one RR with empty rdata: those are ignored as nameserver */
1096 newd->rrsig_count = 0;
1097 newd->trust = rrset_trust_ans_noAA;
1098 newd->rr_len = (size_t*)((uint8_t*)newd +
1099 sizeof(struct packed_rrset_data));
1100 newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
1101 packed_rrset_ptr_fixup(newd);
1102 newd->rr_ttl[0] = newd->ttl;
1103 sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
1105 log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
1106 iter_store_parentside_rrset(env, neg);
1110 iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
1111 struct regional* region, struct query_info* qinfo)
1113 struct ub_packed_rrset_key* akey;
1114 akey = rrset_cache_lookup(env->rrset_cache, dp->name,
1115 dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
1116 PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1118 log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
1119 dp->has_parent_side_NS = 1;
1120 /* and mark the new names as lame */
1121 if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
1122 lock_rw_unlock(&akey->entry.lock);
1125 lock_rw_unlock(&akey->entry.lock);
1130 int iter_lookup_parent_glue_from_cache(struct module_env* env,
1131 struct delegpt* dp, struct regional* region, struct query_info* qinfo)
1133 struct ub_packed_rrset_key* akey;
1134 struct delegpt_ns* ns;
1135 size_t num = delegpt_count_targets(dp);
1136 for(ns = dp->nslist; ns; ns = ns->next) {
1137 /* get cached parentside A */
1138 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1139 ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
1140 PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1142 log_rrset_key(VERB_ALGO, "found parent-side", akey);
1143 ns->done_pside4 = 1;
1144 /* a negative-cache-element has no addresses it adds */
1145 if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL))
1146 log_err("malloc failure in lookup_parent_glue");
1147 lock_rw_unlock(&akey->entry.lock);
1149 /* get cached parentside AAAA */
1150 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1151 ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
1152 PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1154 log_rrset_key(VERB_ALGO, "found parent-side", akey);
1155 ns->done_pside6 = 1;
1156 /* a negative-cache-element has no addresses it adds */
1157 if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL))
1158 log_err("malloc failure in lookup_parent_glue");
1159 lock_rw_unlock(&akey->entry.lock);
1162 /* see if new (but lame) addresses have become available */
1163 return delegpt_count_targets(dp) != num;
1167 iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
1170 uint16_t c1 = *c, c2 = *c;
1171 int r1 = hints_next_root(hints, &c1);
1172 int r2 = forwards_next_root(fwd, &c2);
1173 if(!r1 && !r2) /* got none, end of list */
1175 else if(!r1) /* got one, return that */
1179 else if(c1 < c2) /* got both take smallest */
1186 iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
1188 /* Only the DS record for the delegation itself is expected.
1189 * We allow DS for everything between the bailiwick and the
1190 * zonecut, thus DS records must be at or above the zonecut.
1191 * And the DS records must be below the server authority zone.
1192 * The answer section is already scrubbed. */
1193 size_t i = msg->rep->an_numrrsets;
1194 while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
1195 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1196 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
1197 (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
1198 || query_dname_compare(z, s->rk.dname) == 0)) {
1199 log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
1200 s->rk.dname, ntohs(s->rk.type),
1201 ntohs(s->rk.rrset_class));
1202 memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
1203 sizeof(struct ub_packed_rrset_key*) *
1204 (msg->rep->rrset_count-i-1));
1205 msg->rep->ns_numrrsets--;
1206 msg->rep->rrset_count--;
1207 /* stay at same i, but new record */
1215 iter_scrub_nxdomain(struct dns_msg* msg)
1217 if(msg->rep->an_numrrsets == 0)
1220 memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets,
1221 sizeof(struct ub_packed_rrset_key*) *
1222 (msg->rep->rrset_count-msg->rep->an_numrrsets));
1223 msg->rep->rrset_count -= msg->rep->an_numrrsets;
1224 msg->rep->an_numrrsets = 0;
1227 void iter_dec_attempts(struct delegpt* dp, int d)
1229 struct delegpt_addr* a;
1230 for(a=dp->target_list; a; a = a->next_target) {
1231 if(a->attempts >= OUTBOUND_MSG_RETRY) {
1232 /* add back to result list */
1233 a->next_result = dp->result_list;
1234 dp->result_list = a;
1238 else a->attempts = 0;
1242 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old)
1244 struct delegpt_addr* a, *o, *prev;
1245 for(a=dp->target_list; a; a = a->next_target) {
1246 o = delegpt_find_addr(old, &a->addr, a->addrlen);
1248 log_addr(VERB_ALGO, "copy attempt count previous dp",
1249 &a->addr, a->addrlen);
1250 a->attempts = o->attempts;
1254 a = dp->usable_list;
1256 if(a->attempts >= OUTBOUND_MSG_RETRY) {
1257 log_addr(VERB_ALGO, "remove from usable list dp",
1258 &a->addr, a->addrlen);
1259 /* remove from result list */
1261 prev->next_usable = a->next_usable;
1262 else dp->usable_list = a->next_usable;
1263 /* prev stays the same */
1273 iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
1275 /* if for query example.com, there is example.com SOA or a subdomain
1276 * of example.com, then we are too low and need to fetch NS. */
1278 /* if we have a DNAME or CNAME we are probably wrong */
1279 /* if we have a qtype DS in the answer section, its fine */
1280 for(i=0; i < msg->rep->an_numrrsets; i++) {
1281 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1282 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
1283 ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
1284 /* not the right answer, maybe too low, check the
1285 * RRSIG signer name (if there is any) for a hint
1286 * that it is from the dp zone anyway */
1289 val_find_rrset_signer(s, &sname, &slen);
1290 if(sname && query_dname_compare(dp->name, sname)==0)
1291 return 0; /* it is fine, from the right dp */
1294 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
1295 return 0; /* fine, we have a DS record */
1297 for(i=msg->rep->an_numrrsets;
1298 i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
1299 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1300 if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1301 if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
1302 return 1; /* point is too low */
1303 if(query_dname_compare(s->rk.dname, dp->name)==0)
1304 return 0; /* right dp */
1306 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1307 ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1310 val_find_rrset_signer(s, &sname, &slen);
1311 if(sname && query_dname_compare(dp->name, sname)==0)
1312 return 0; /* it is fine, from the right dp */
1316 /* we do not know */
1320 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
1322 /* no delegation point, do not see how we can go down,
1323 * robust check, it should really exist */
1326 /* see if dp equals the qname, then we cannot go down further */
1327 if(query_dname_compare(qinfo->qname, dp->name) == 0)
1329 /* if dp is one label above the name we also cannot go down further */
1330 if(dname_count_labels(qinfo->qname) == dp->namelabs+1)
1336 iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf)
1338 struct iter_hints_stub *stub;
1341 /* Check for stub. */
1342 stub = hints_lookup_stub(qstate->env->hints, qinf->qname,
1343 qinf->qclass, NULL);
1344 dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass);
1346 /* see if forward or stub is more pertinent */
1347 if(stub && stub->dp && dp) {
1348 if(dname_strict_subdomain(dp->name, dp->namelabs,
1349 stub->dp->name, stub->dp->namelabs)) {
1350 stub = NULL; /* ignore stub, forward is lower */
1352 dp = NULL; /* ignore forward, stub is lower */
1357 if (stub != NULL && stub->dp != NULL) {
1358 if(stub->dp->no_cache) {
1361 dname_str(qinf->qname, qname);
1362 dname_str(stub->dp->name, dpname);
1363 verbose(VERB_ALGO, "stub for %s %s has no_cache", qname, dpname);
1365 return (stub->dp->no_cache);
1368 /* Check for forward. */
1373 dname_str(qinf->qname, qname);
1374 dname_str(dp->name, dpname);
1375 verbose(VERB_ALGO, "forward for %s %s has no_cache", qname, dpname);
1377 return (dp->no_cache);