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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
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"
67 /** time when nameserver glue is said to be 'recent' */
68 #define SUSPICION_RECENT_EXPIRY 86400
69 /** penalty to validation failed blacklisted IPs */
70 #define BLACKLIST_PENALTY (USEFUL_SERVER_TOP_TIMEOUT*4)
72 /** fillup fetch policy array */
74 fetch_fill(struct iter_env* ie, const char* str)
76 char* s = (char*)str, *e;
78 for(i=0; i<ie->max_dependency_depth+1; i++) {
79 ie->target_fetch_policy[i] = strtol(s, &e, 10);
81 fatal_exit("cannot parse fetch policy number %s", s);
86 /** Read config string that represents the target fetch policy */
88 read_fetch_policy(struct iter_env* ie, const char* str)
90 int count = cfg_count_numbers(str);
92 log_err("Cannot parse target fetch policy: \"%s\"", str);
95 ie->max_dependency_depth = count - 1;
96 ie->target_fetch_policy = (int*)calloc(
97 (size_t)ie->max_dependency_depth+1, sizeof(int));
98 if(!ie->target_fetch_policy) {
99 log_err("alloc fetch policy: out of memory");
107 iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
110 /* target fetch policy */
111 if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
113 for(i=0; i<iter_env->max_dependency_depth+1; i++)
114 verbose(VERB_QUERY, "target fetch policy for level %d is %d",
115 i, iter_env->target_fetch_policy[i]);
117 if(!iter_env->donotq)
118 iter_env->donotq = donotq_create();
119 if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
120 log_err("Could not set donotqueryaddresses");
124 iter_env->priv = priv_create();
125 if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
126 log_err("Could not set private addresses");
129 iter_env->supports_ipv6 = cfg->do_ip6;
130 iter_env->supports_ipv4 = cfg->do_ip4;
134 /** filter out unsuitable targets
135 * @param iter_env: iterator environment with ipv6-support flag.
136 * @param env: module environment with infra cache.
137 * @param name: zone name
138 * @param namelen: length of name
139 * @param qtype: query type (host order).
140 * @param now: current time
141 * @param a: address in delegation point we are examining.
142 * @return an integer that signals the target suitability.
144 * -1: The address should be omitted from the list.
146 * o The address is bogus (DNSSEC validation failure).
147 * o Listed as donotquery
148 * o is ipv6 but no ipv6 support (in operating system).
149 * o is ipv4 but no ipv4 support (in operating system).
151 * Otherwise, an rtt in milliseconds.
152 * 0 .. USEFUL_SERVER_TOP_TIMEOUT-1
153 * The roundtrip time timeout estimate. less than 2 minutes.
154 * Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
155 * values 0 .. 49 are not used, unless that is changed.
156 * USEFUL_SERVER_TOP_TIMEOUT
157 * This value exactly is given for unresponsive blacklisted.
158 * USEFUL_SERVER_TOP_TIMEOUT+1
159 * For non-blacklisted servers: huge timeout, but has traffic.
160 * USEFUL_SERVER_TOP_TIMEOUT*1 ..
161 * parent-side lame servers get this penalty. A dispreferential
162 * server. (lame in delegpt).
163 * USEFUL_SERVER_TOP_TIMEOUT*2 ..
164 * dnsseclame servers get penalty
165 * USEFUL_SERVER_TOP_TIMEOUT*3 ..
166 * recursion lame servers get penalty
167 * UNKNOWN_SERVER_NICENESS
168 * If no information is known about the server, this is
169 * returned. 376 msec or so.
170 * +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
172 * When a final value is chosen that is dnsseclame ; dnsseclameness checking
173 * is turned off (so we do not discard the reply).
174 * When a final value is chosen that is recursionlame; RD bit is set on query.
175 * Because of the numbers this means recursionlame also have dnssec lameness
176 * checking turned off.
179 iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
180 uint8_t* name, size_t namelen, uint16_t qtype, uint32_t now,
181 struct delegpt_addr* a)
183 int rtt, lame, reclame, dnsseclame;
185 return -1; /* address of server is bogus */
186 if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
187 log_addr(VERB_ALGO, "skip addr on the donotquery list",
188 &a->addr, a->addrlen);
189 return -1; /* server is on the donotquery list */
191 if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
192 return -1; /* there is no ip6 available */
194 if(!iter_env->supports_ipv4 && !addr_is_ip6(&a->addr, a->addrlen)) {
195 return -1; /* there is no ip4 available */
197 /* check lameness - need zone , class info */
198 if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
199 name, namelen, qtype, &lame, &dnsseclame, &reclame,
201 log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
202 verbose(VERB_ALGO, " rtt=%d%s%s%s%s", rtt,
204 dnsseclame?" DNSSEC_LAME":"",
205 reclame?" REC_LAME":"",
206 a->lame?" ADDR_LAME":"");
208 return -1; /* server is lame */
209 else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
210 /* server is unresponsive,
211 * we used to return TOP_TIMOUT, but fairly useless,
212 * because if == TOP_TIMEOUT is dropped because
213 * blacklisted later, instead, remove it here, so
214 * other choices (that are not blacklisted) can be
217 /* select remainder from worst to best */
219 return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
221 return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
223 return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
226 /* no server information present */
228 return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
229 return UNKNOWN_SERVER_NICENESS;
232 /** lookup RTT information, and also store fastest rtt (if any) */
234 iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
235 uint8_t* name, size_t namelen, uint16_t qtype, uint32_t now,
236 struct delegpt* dp, int* best_rtt, struct sock_list* blacklist)
239 struct delegpt_addr* a;
241 return 0; /* NS bogus, all bogus, nothing found */
242 for(a=dp->result_list; a; a = a->next_result) {
243 a->sel_rtt = iter_filter_unsuitable(iter_env, env,
244 name, namelen, qtype, now, a);
245 if(a->sel_rtt != -1) {
246 if(sock_list_find(blacklist, &a->addr, a->addrlen))
247 a->sel_rtt += BLACKLIST_PENALTY;
250 *best_rtt = a->sel_rtt;
252 } else if(a->sel_rtt < *best_rtt) {
253 *best_rtt = a->sel_rtt;
260 /** filter the addres list, putting best targets at front,
261 * returns number of best targets (or 0, no suitable targets) */
263 iter_filter_order(struct iter_env* iter_env, struct module_env* env,
264 uint8_t* name, size_t namelen, uint16_t qtype, uint32_t now,
265 struct delegpt* dp, int* selected_rtt, int open_target,
266 struct sock_list* blacklist)
268 int got_num = 0, low_rtt = 0, swap_to_front;
269 struct delegpt_addr* a, *n, *prev=NULL;
271 /* fillup sel_rtt and find best rtt in the bunch */
272 got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
273 &low_rtt, blacklist);
276 if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
277 (delegpt_count_missing_targets(dp) > 0 || open_target > 0)) {
278 verbose(VERB_ALGO, "Bad choices, trying to get more choice");
279 return 0; /* we want more choice. The best choice is a bad one.
280 return 0 to force the caller to fetch more */
286 /* skip unsuitable targets */
287 if(a->sel_rtt == -1) {
292 /* classify the server address and determine what to do */
294 if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= RTT_BAND) {
297 } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=RTT_BAND) {
301 /* swap to front if necessary, or move to next result */
302 if(swap_to_front && prev) {
304 prev->next_result = n;
305 a->next_result = dp->result_list;
313 *selected_rtt = low_rtt;
318 iter_server_selection(struct iter_env* iter_env,
319 struct module_env* env, struct delegpt* dp,
320 uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
321 int* chase_to_rd, int open_target, struct sock_list* blacklist)
325 struct delegpt_addr* a, *prev;
326 int num = iter_filter_order(iter_env, env, name, namelen, qtype,
327 *env->now, dp, &selrtt, open_target, blacklist);
331 verbose(VERB_ALGO, "selrtt %d", selrtt);
332 if(selrtt > BLACKLIST_PENALTY) {
333 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
334 verbose(VERB_ALGO, "chase to "
335 "blacklisted recursion lame server");
338 if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
339 verbose(VERB_ALGO, "chase to "
340 "blacklisted dnssec lame server");
344 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
345 verbose(VERB_ALGO, "chase to recursion lame server");
348 if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
349 verbose(VERB_ALGO, "chase to dnssec lame server");
352 if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
353 verbose(VERB_ALGO, "chase to blacklisted lame server");
360 if(++a->attempts < OUTBOUND_MSG_RETRY)
362 dp->result_list = a->next_result;
366 /* randomly select a target from the list */
368 /* grab secure random number, to pick unexpected server.
369 * also we need it to be threadsafe. */
370 sel = ub_random_max(env->rnd, num);
373 while(sel > 0 && a) {
378 if(!a) /* robustness */
380 if(++a->attempts < OUTBOUND_MSG_RETRY)
382 /* remove it from the delegation point result list */
384 prev->next_result = a->next_result;
385 else dp->result_list = a->next_result;
390 dns_alloc_msg(ldns_buffer* pkt, struct msg_parse* msg,
391 struct regional* region)
393 struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
394 sizeof(struct dns_msg));
397 memset(m, 0, sizeof(*m));
398 if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
399 log_err("malloc failure: allocating incoming dns_msg");
406 dns_copy_msg(struct dns_msg* from, struct regional* region)
408 struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
409 sizeof(struct dns_msg));
412 m->qinfo = from->qinfo;
413 if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
414 from->qinfo.qname_len)))
416 if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
422 iter_dns_store(struct module_env* env, struct query_info* msgqinf,
423 struct reply_info* msgrep, int is_referral, uint32_t leeway, int pside,
424 struct regional* region)
426 if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
428 log_err("out of memory: cannot store data in cache");
432 iter_ns_probability(struct ub_randstate* rnd, int n, int m)
435 if(n == m) /* 100% chance */
437 /* we do not need secure random numbers here, but
438 * we do need it to be threadsafe, so we use this */
439 sel = ub_random_max(rnd, m);
443 /** detect dependency cycle for query and target */
445 causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
446 uint16_t t, uint16_t c)
448 struct query_info qinf;
450 qinf.qname_len = namelen;
453 fptr_ok(fptr_whitelist_modenv_detect_cycle(
454 qstate->env->detect_cycle));
455 return (*qstate->env->detect_cycle)(qstate, &qinf,
456 (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming);
460 iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
462 struct delegpt_ns* ns;
463 for(ns = dp->nslist; ns; ns = ns->next) {
466 /* see if this ns as target causes dependency cycle */
467 if(causes_cycle(qstate, ns->name, ns->namelen,
468 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
469 causes_cycle(qstate, ns->name, ns->namelen,
470 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
471 log_nametypeclass(VERB_QUERY, "skipping target due "
472 "to dependency cycle (harden-glue: no may "
473 "fix some of the cycles)",
474 ns->name, LDNS_RR_TYPE_A,
475 qstate->qinfo.qclass);
482 iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
484 struct delegpt_ns* ns;
485 for(ns = dp->nslist; ns; ns = ns->next) {
486 if(ns->done_pside4 && ns->done_pside6)
488 /* see if this ns as target causes dependency cycle */
489 if(causes_cycle(qstate, ns->name, ns->namelen,
490 LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
491 log_nametypeclass(VERB_QUERY, "skipping target due "
492 "to dependency cycle", ns->name,
493 LDNS_RR_TYPE_A, qstate->qinfo.qclass);
496 if(causes_cycle(qstate, ns->name, ns->namelen,
497 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
498 log_nametypeclass(VERB_QUERY, "skipping target due "
499 "to dependency cycle", ns->name,
500 LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
507 iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
510 struct delegpt_ns* ns;
513 * o no addresses are provided.
514 * o all NS items are required glue.
517 * o no addresses are provided.
518 * o the query is for one of the nameservers in dp,
519 * and that nameserver is a glue-name for this dp.
523 /* either available or unused targets */
524 if(dp->usable_list || dp->result_list)
527 /* see if query is for one of the nameservers, which is glue */
528 if( (qinfo->qtype == LDNS_RR_TYPE_A ||
529 qinfo->qtype == LDNS_RR_TYPE_AAAA) &&
530 dname_subdomain_c(qinfo->qname, dp->name) &&
531 delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
534 for(ns = dp->nslist; ns; ns = ns->next) {
535 if(ns->resolved) /* skip failed targets */
537 if(!dname_subdomain_c(ns->name, dp->name))
538 return 0; /* one address is not required glue */
544 iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
545 struct dns_msg* msg, uint16_t dclass)
547 struct trust_anchor* a;
548 /* information not available, !env->anchors can be common */
549 if(!env || !env->anchors || !dp || !dp->name)
551 /* a trust anchor exists with this name, RRSIGs expected */
552 if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
554 lock_basic_unlock(&a->lock);
557 /* see if DS rrset was given, in AUTH section */
558 if(msg && msg->rep &&
559 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
560 LDNS_RR_TYPE_DS, dclass))
562 /* look in key cache */
564 struct key_entry_key* kk = key_cache_obtain(env->key_cache,
565 dp->name, dp->namelen, dclass, env->scratch, *env->now);
567 if(query_dname_compare(kk->name, dp->name) == 0) {
568 if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
569 regional_free_all(env->scratch);
571 } else if(key_entry_isnull(kk)) {
572 regional_free_all(env->scratch);
576 regional_free_all(env->scratch);
583 iter_msg_has_dnssec(struct dns_msg* msg)
586 if(!msg || !msg->rep)
588 for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
589 if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
590 entry.data)->rrsig_count > 0)
593 /* empty message has no DNSSEC info, with DNSSEC the reply is
594 * not empty (NSEC) */
598 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
599 enum response_type type, uint16_t dclass)
601 if(!msg || !dp || !msg->rep || !dp->name)
603 /* SOA RRset - always from reply zone */
604 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
605 LDNS_RR_TYPE_SOA, dclass) ||
606 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
607 LDNS_RR_TYPE_SOA, dclass))
609 if(type == RESPONSE_TYPE_REFERRAL) {
611 /* if it adds a single label, i.e. we expect .com,
612 * and referral to example.com. NS ... , then origin zone
613 * is .com. For a referral to sub.example.com. NS ... then
614 * we do not know, since example.com. may be in between. */
615 for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
617 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
618 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
619 ntohs(s->rk.rrset_class) == dclass) {
620 int l = dname_count_labels(s->rk.dname);
621 if(l == dp->namelabs + 1 &&
622 dname_strict_subdomain(s->rk.dname,
623 l, dp->name, dp->namelabs))
629 log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
630 /* not a referral, and not lame delegation (upwards), so,
631 * any NS rrset must be from the zone itself */
632 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
633 LDNS_RR_TYPE_NS, dclass) ||
634 reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
635 LDNS_RR_TYPE_NS, dclass))
637 /* a DNSKEY set is expected at the zone apex as well */
638 /* this is for 'minimal responses' for DNSKEYs */
639 if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
640 LDNS_RR_TYPE_DNSKEY, dclass))
646 * check equality of two rrsets
649 * @return true if equal
652 rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
654 struct packed_rrset_data* d1 = (struct packed_rrset_data*)
656 struct packed_rrset_data* d2 = (struct packed_rrset_data*)
659 if(k1->rk.dname_len != k2->rk.dname_len ||
660 k1->rk.flags != k2->rk.flags ||
661 k1->rk.type != k2->rk.type ||
662 k1->rk.rrset_class != k2->rk.rrset_class ||
663 query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
665 if(d1->ttl != d2->ttl ||
666 d1->count != d2->count ||
667 d1->rrsig_count != d2->rrsig_count ||
668 d1->trust != d2->trust ||
669 d1->security != d2->security)
671 t = d1->count + d1->rrsig_count;
673 if(d1->rr_len[i] != d2->rr_len[i] ||
674 d1->rr_ttl[i] != d2->rr_ttl[i] ||
675 memcmp(d1->rr_data[i], d2->rr_data[i],
683 reply_equal(struct reply_info* p, struct reply_info* q, ldns_buffer* scratch)
686 if(p->flags != q->flags ||
687 p->qdcount != q->qdcount ||
689 p->prefetch_ttl != q->prefetch_ttl ||
690 p->security != q->security ||
691 p->an_numrrsets != q->an_numrrsets ||
692 p->ns_numrrsets != q->ns_numrrsets ||
693 p->ar_numrrsets != q->ar_numrrsets ||
694 p->rrset_count != q->rrset_count)
696 for(i=0; i<p->rrset_count; i++) {
697 if(!rrset_equal(p->rrsets[i], q->rrsets[i])) {
698 /* fallback procedure: try to sort and canonicalize */
699 ldns_rr_list* pl, *ql;
700 pl = packed_rrset_to_rr_list(p->rrsets[i], scratch);
701 ql = packed_rrset_to_rr_list(q->rrsets[i], scratch);
703 ldns_rr_list_deep_free(pl);
704 ldns_rr_list_deep_free(ql);
707 ldns_rr_list2canonical(pl);
708 ldns_rr_list2canonical(ql);
709 ldns_rr_list_sort(pl);
710 ldns_rr_list_sort(ql);
711 if(ldns_rr_list_compare(pl, ql) != 0) {
712 ldns_rr_list_deep_free(pl);
713 ldns_rr_list_deep_free(ql);
716 ldns_rr_list_deep_free(pl);
717 ldns_rr_list_deep_free(ql);
725 iter_store_parentside_rrset(struct module_env* env,
726 struct ub_packed_rrset_key* rrset)
728 struct rrset_ref ref;
729 rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
731 log_err("malloc failure in store_parentside_rrset");
734 rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
735 rrset->entry.hash = rrset_key_hash(&rrset->rk);
738 /* ignore ret: if it was in the cache, ref updated */
739 (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
742 /** fetch NS record from reply, if any */
743 static struct ub_packed_rrset_key*
744 reply_get_NS_rrset(struct reply_info* rep)
747 for(i=0; i<rep->rrset_count; i++) {
748 if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
749 return rep->rrsets[i];
756 iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
758 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
760 log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
761 iter_store_parentside_rrset(env, rrset);
765 void iter_store_parentside_neg(struct module_env* env,
766 struct query_info* qinfo, struct reply_info* rep)
768 /* TTL: NS from referral in iq->deleg_msg,
769 * or first RR from iq->response,
770 * or servfail5secs if !iq->response */
771 uint32_t ttl = NORR_TTL;
772 struct ub_packed_rrset_key* neg;
773 struct packed_rrset_data* newd;
775 struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
776 if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
777 if(rrset) ttl = ub_packed_rrset_ttl(rrset);
779 /* create empty rrset to store */
780 neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
781 sizeof(struct ub_packed_rrset_key));
783 log_err("out of memory in store_parentside_neg");
786 memset(&neg->entry, 0, sizeof(neg->entry));
787 neg->entry.key = neg;
788 neg->rk.type = htons(qinfo->qtype);
789 neg->rk.rrset_class = htons(qinfo->qclass);
791 neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
794 log_err("out of memory in store_parentside_neg");
797 neg->rk.dname_len = qinfo->qname_len;
798 neg->entry.hash = rrset_key_hash(&neg->rk);
799 newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
800 sizeof(struct packed_rrset_data) + sizeof(size_t) +
801 sizeof(uint8_t*) + sizeof(uint32_t) + sizeof(uint16_t));
803 log_err("out of memory in store_parentside_neg");
806 neg->entry.data = newd;
808 /* entry must have one RR, otherwise not valid in cache.
809 * put in one RR with empty rdata: those are ignored as nameserver */
811 newd->rrsig_count = 0;
812 newd->trust = rrset_trust_ans_noAA;
813 newd->rr_len = (size_t*)((uint8_t*)newd +
814 sizeof(struct packed_rrset_data));
815 newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
816 packed_rrset_ptr_fixup(newd);
817 newd->rr_ttl[0] = newd->ttl;
818 ldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
820 log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
821 iter_store_parentside_rrset(env, neg);
825 iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
826 struct regional* region, struct query_info* qinfo)
828 struct ub_packed_rrset_key* akey;
829 akey = rrset_cache_lookup(env->rrset_cache, dp->name,
830 dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
831 PACKED_RRSET_PARENT_SIDE, *env->now, 0);
833 log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
834 dp->has_parent_side_NS = 1;
835 /* and mark the new names as lame */
836 if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
837 lock_rw_unlock(&akey->entry.lock);
840 lock_rw_unlock(&akey->entry.lock);
845 int iter_lookup_parent_glue_from_cache(struct module_env* env,
846 struct delegpt* dp, struct regional* region, struct query_info* qinfo)
848 struct ub_packed_rrset_key* akey;
849 struct delegpt_ns* ns;
850 size_t num = delegpt_count_targets(dp);
851 for(ns = dp->nslist; ns; ns = ns->next) {
852 /* get cached parentside A */
853 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
854 ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
855 PACKED_RRSET_PARENT_SIDE, *env->now, 0);
857 log_rrset_key(VERB_ALGO, "found parent-side", akey);
859 /* a negative-cache-element has no addresses it adds */
860 if(!delegpt_add_rrset_A(dp, region, akey, 1))
861 log_err("malloc failure in lookup_parent_glue");
862 lock_rw_unlock(&akey->entry.lock);
864 /* get cached parentside AAAA */
865 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
866 ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
867 PACKED_RRSET_PARENT_SIDE, *env->now, 0);
869 log_rrset_key(VERB_ALGO, "found parent-side", akey);
871 /* a negative-cache-element has no addresses it adds */
872 if(!delegpt_add_rrset_AAAA(dp, region, akey, 1))
873 log_err("malloc failure in lookup_parent_glue");
874 lock_rw_unlock(&akey->entry.lock);
877 /* see if new (but lame) addresses have become available */
878 return delegpt_count_targets(dp) != num;
882 iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
885 uint16_t c1 = *c, c2 = *c;
886 int r1 = hints_next_root(hints, &c1);
887 int r2 = forwards_next_root(fwd, &c2);
888 if(!r1 && !r2) /* got none, end of list */
890 else if(!r1) /* got one, return that */
894 else if(c1 < c2) /* got both take smallest */
901 iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
903 /* Only the DS record for the delegation itself is expected.
904 * We allow DS for everything between the bailiwick and the
905 * zonecut, thus DS records must be at or above the zonecut.
906 * And the DS records must be below the server authority zone.
907 * The answer section is already scrubbed. */
908 size_t i = msg->rep->an_numrrsets;
909 while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
910 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
911 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
912 (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
913 || query_dname_compare(z, s->rk.dname) == 0)) {
914 log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
915 s->rk.dname, ntohs(s->rk.type),
916 ntohs(s->rk.rrset_class));
917 memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
918 sizeof(struct ub_packed_rrset_key*) *
919 (msg->rep->rrset_count-i-1));
920 msg->rep->ns_numrrsets--;
921 msg->rep->rrset_count--;
922 /* stay at same i, but new record */
929 void iter_dec_attempts(struct delegpt* dp, int d)
931 struct delegpt_addr* a;
932 for(a=dp->target_list; a; a = a->next_target) {
933 if(a->attempts >= OUTBOUND_MSG_RETRY) {
934 /* add back to result list */
935 a->next_result = dp->result_list;
940 else a->attempts = 0;
944 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old)
946 struct delegpt_addr* a, *o, *prev;
947 for(a=dp->target_list; a; a = a->next_target) {
948 o = delegpt_find_addr(old, &a->addr, a->addrlen);
950 log_addr(VERB_ALGO, "copy attempt count previous dp",
951 &a->addr, a->addrlen);
952 a->attempts = o->attempts;
958 if(a->attempts >= OUTBOUND_MSG_RETRY) {
959 log_addr(VERB_ALGO, "remove from usable list dp",
960 &a->addr, a->addrlen);
961 /* remove from result list */
963 prev->next_usable = a->next_usable;
964 else dp->usable_list = a->next_usable;
965 /* prev stays the same */
975 iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
977 /* if for query example.com, there is example.com SOA or a subdomain
978 * of example.com, then we are too low and need to fetch NS. */
980 /* if we have a DNAME or CNAME we are probably wrong */
981 /* if we have a qtype DS in the answer section, its fine */
982 for(i=0; i < msg->rep->an_numrrsets; i++) {
983 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
984 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
985 ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
986 /* not the right answer, maybe too low, check the
987 * RRSIG signer name (if there is any) for a hint
988 * that it is from the dp zone anyway */
991 val_find_rrset_signer(s, &sname, &slen);
992 if(sname && query_dname_compare(dp->name, sname)==0)
993 return 0; /* it is fine, from the right dp */
996 if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
997 return 0; /* fine, we have a DS record */
999 for(i=msg->rep->an_numrrsets;
1000 i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
1001 struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1002 if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1003 if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
1004 return 1; /* point is too low */
1005 if(query_dname_compare(s->rk.dname, dp->name)==0)
1006 return 0; /* right dp */
1008 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1009 ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1012 val_find_rrset_signer(s, &sname, &slen);
1013 if(sname && query_dname_compare(dp->name, sname)==0)
1014 return 0; /* it is fine, from the right dp */
1018 /* we do not know */
1022 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
1024 /* no delegation point, do not see how we can go down,
1025 * robust check, it should really exist */
1028 /* see if dp equals the qname, then we cannot go down further */
1029 if(query_dname_compare(qinfo->qname, dp->name) == 0)
1031 /* if dp is one label above the name we also cannot go down further */
1032 if(dname_count_labels(qinfo->qname) == dp->namelabs+1)