xref: /openbsd-src/usr.sbin/unbound/iterator/iter_utils.c (revision 98bc733b08604094f4138174a0ee0bb9faaca4bd)
1933707f3Ssthen /*
2933707f3Ssthen  * iterator/iter_utils.c - iterative resolver module utility functions.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains functions to assist the iterator module.
40933707f3Ssthen  * Configuration options. Forward zones.
41933707f3Ssthen  */
42933707f3Ssthen #include "config.h"
43933707f3Ssthen #include "iterator/iter_utils.h"
44933707f3Ssthen #include "iterator/iterator.h"
45933707f3Ssthen #include "iterator/iter_hints.h"
46933707f3Ssthen #include "iterator/iter_fwd.h"
47933707f3Ssthen #include "iterator/iter_donotq.h"
48933707f3Ssthen #include "iterator/iter_delegpt.h"
49933707f3Ssthen #include "iterator/iter_priv.h"
50933707f3Ssthen #include "services/cache/infra.h"
51933707f3Ssthen #include "services/cache/dns.h"
52933707f3Ssthen #include "services/cache/rrset.h"
53191f22c6Ssthen #include "services/outside_network.h"
54933707f3Ssthen #include "util/net_help.h"
55933707f3Ssthen #include "util/module.h"
56933707f3Ssthen #include "util/log.h"
57933707f3Ssthen #include "util/config_file.h"
58933707f3Ssthen #include "util/regional.h"
59933707f3Ssthen #include "util/data/msgparse.h"
60933707f3Ssthen #include "util/data/dname.h"
61933707f3Ssthen #include "util/random.h"
62933707f3Ssthen #include "util/fptr_wlist.h"
63933707f3Ssthen #include "validator/val_anchor.h"
64933707f3Ssthen #include "validator/val_kcache.h"
65933707f3Ssthen #include "validator/val_kentry.h"
66d8d14d0cSsthen #include "validator/val_utils.h"
675d76a658Ssthen #include "validator/val_sigcrypt.h"
68a58bff56Ssthen #include "sldns/sbuffer.h"
69a58bff56Ssthen #include "sldns/str2wire.h"
70933707f3Ssthen 
71933707f3Ssthen /** time when nameserver glue is said to be 'recent' */
72933707f3Ssthen #define SUSPICION_RECENT_EXPIRY 86400
73933707f3Ssthen 
748b7325afSsthen /** if NAT64 is enabled and no NAT64 prefix is configured, first fall back to
758b7325afSsthen  * DNS64 prefix.  If that is not configured, fall back to this default value.
768b7325afSsthen  */
778b7325afSsthen static const char DEFAULT_NAT64_PREFIX[] = "64:ff9b::/96";
788b7325afSsthen 
79933707f3Ssthen /** fillup fetch policy array */
80933707f3Ssthen static void
81933707f3Ssthen fetch_fill(struct iter_env* ie, const char* str)
82933707f3Ssthen {
83933707f3Ssthen 	char* s = (char*)str, *e;
84933707f3Ssthen 	int i;
85933707f3Ssthen 	for(i=0; i<ie->max_dependency_depth+1; i++) {
86933707f3Ssthen 		ie->target_fetch_policy[i] = strtol(s, &e, 10);
87933707f3Ssthen 		if(s == e)
88933707f3Ssthen 			fatal_exit("cannot parse fetch policy number %s", s);
89933707f3Ssthen 		s = e;
90933707f3Ssthen 	}
91933707f3Ssthen }
92933707f3Ssthen 
93933707f3Ssthen /** Read config string that represents the target fetch policy */
94933707f3Ssthen static int
95933707f3Ssthen read_fetch_policy(struct iter_env* ie, const char* str)
96933707f3Ssthen {
97933707f3Ssthen 	int count = cfg_count_numbers(str);
98933707f3Ssthen 	if(count < 1) {
99933707f3Ssthen 		log_err("Cannot parse target fetch policy: \"%s\"", str);
100933707f3Ssthen 		return 0;
101933707f3Ssthen 	}
102933707f3Ssthen 	ie->max_dependency_depth = count - 1;
103933707f3Ssthen 	ie->target_fetch_policy = (int*)calloc(
104933707f3Ssthen 		(size_t)ie->max_dependency_depth+1, sizeof(int));
105933707f3Ssthen 	if(!ie->target_fetch_policy) {
106933707f3Ssthen 		log_err("alloc fetch policy: out of memory");
107933707f3Ssthen 		return 0;
108933707f3Ssthen 	}
109933707f3Ssthen 	fetch_fill(ie, str);
110933707f3Ssthen 	return 1;
111933707f3Ssthen }
112933707f3Ssthen 
113a58bff56Ssthen /** apply config caps whitelist items to name tree */
114a58bff56Ssthen static int
11577079be7Ssthen caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg)
116a58bff56Ssthen {
117a58bff56Ssthen 	struct config_strlist* p;
118a58bff56Ssthen 	for(p=cfg->caps_whitelist; p; p=p->next) {
119a58bff56Ssthen 		struct name_tree_node* n;
120a58bff56Ssthen 		size_t len;
121a58bff56Ssthen 		uint8_t* nm = sldns_str2wire_dname(p->str, &len);
122a58bff56Ssthen 		if(!nm) {
123a58bff56Ssthen 			log_err("could not parse %s", p->str);
124a58bff56Ssthen 			return 0;
125a58bff56Ssthen 		}
126a58bff56Ssthen 		n = (struct name_tree_node*)calloc(1, sizeof(*n));
127a58bff56Ssthen 		if(!n) {
128a58bff56Ssthen 			log_err("out of memory");
129a58bff56Ssthen 			free(nm);
130a58bff56Ssthen 			return 0;
131a58bff56Ssthen 		}
132a58bff56Ssthen 		n->node.key = n;
133a58bff56Ssthen 		n->name = nm;
134a58bff56Ssthen 		n->len = len;
135a58bff56Ssthen 		n->labs = dname_count_labels(nm);
136a58bff56Ssthen 		n->dclass = LDNS_RR_CLASS_IN;
137a58bff56Ssthen 		if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) {
138a58bff56Ssthen 			/* duplicate element ignored, idempotent */
139a58bff56Ssthen 			free(n->name);
140a58bff56Ssthen 			free(n);
141a58bff56Ssthen 		}
142a58bff56Ssthen 	}
143a58bff56Ssthen 	name_tree_init_parents(ntree);
144a58bff56Ssthen 	return 1;
145a58bff56Ssthen }
146a58bff56Ssthen 
147933707f3Ssthen int
148933707f3Ssthen iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
149933707f3Ssthen {
1508b7325afSsthen 	const char *nat64_prefix;
151933707f3Ssthen 	int i;
152933707f3Ssthen 	/* target fetch policy */
153933707f3Ssthen 	if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
154933707f3Ssthen 		return 0;
155933707f3Ssthen 	for(i=0; i<iter_env->max_dependency_depth+1; i++)
156933707f3Ssthen 		verbose(VERB_QUERY, "target fetch policy for level %d is %d",
157933707f3Ssthen 			i, iter_env->target_fetch_policy[i]);
158933707f3Ssthen 
159933707f3Ssthen 	if(!iter_env->donotq)
160933707f3Ssthen 		iter_env->donotq = donotq_create();
161933707f3Ssthen 	if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
162933707f3Ssthen 		log_err("Could not set donotqueryaddresses");
163933707f3Ssthen 		return 0;
164933707f3Ssthen 	}
165933707f3Ssthen 	if(!iter_env->priv)
166933707f3Ssthen 		iter_env->priv = priv_create();
167933707f3Ssthen 	if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
168933707f3Ssthen 		log_err("Could not set private addresses");
169933707f3Ssthen 		return 0;
170933707f3Ssthen 	}
171a58bff56Ssthen 	if(cfg->caps_whitelist) {
172a58bff56Ssthen 		if(!iter_env->caps_white)
173a58bff56Ssthen 			iter_env->caps_white = rbtree_create(name_tree_compare);
174a58bff56Ssthen 		if(!iter_env->caps_white || !caps_white_apply_cfg(
175a58bff56Ssthen 			iter_env->caps_white, cfg)) {
176a58bff56Ssthen 			log_err("Could not set capsforid whitelist");
177a58bff56Ssthen 			return 0;
178a58bff56Ssthen 		}
179a58bff56Ssthen 
180a58bff56Ssthen 	}
1818b7325afSsthen 
1828b7325afSsthen 	nat64_prefix = cfg->nat64_prefix;
1838b7325afSsthen 	if(!nat64_prefix)
1848b7325afSsthen 		nat64_prefix = cfg->dns64_prefix;
1858b7325afSsthen 	if(!nat64_prefix)
1868b7325afSsthen 		nat64_prefix = DEFAULT_NAT64_PREFIX;
1878b7325afSsthen 	if(!netblockstrtoaddr(nat64_prefix, 0, &iter_env->nat64_prefix_addr,
1888b7325afSsthen 		&iter_env->nat64_prefix_addrlen,
1898b7325afSsthen 		&iter_env->nat64_prefix_net)) {
1908b7325afSsthen 		log_err("cannot parse nat64-prefix netblock: %s", nat64_prefix);
1918b7325afSsthen 		return 0;
1928b7325afSsthen 	}
1938b7325afSsthen 	if(!addr_is_ip6(&iter_env->nat64_prefix_addr,
1948b7325afSsthen 		iter_env->nat64_prefix_addrlen)) {
1958b7325afSsthen 		log_err("nat64-prefix is not IPv6: %s", cfg->nat64_prefix);
1968b7325afSsthen 		return 0;
1978b7325afSsthen 	}
1988b7325afSsthen 	if(!prefixnet_is_nat64(iter_env->nat64_prefix_net)) {
1998b7325afSsthen 		log_err("nat64-prefix length it not 32, 40, 48, 56, 64 or 96: %s",
2008b7325afSsthen 			nat64_prefix);
2018b7325afSsthen 		return 0;
2028b7325afSsthen 	}
2038b7325afSsthen 
204933707f3Ssthen 	iter_env->supports_ipv6 = cfg->do_ip6;
205933707f3Ssthen 	iter_env->supports_ipv4 = cfg->do_ip4;
2068b7325afSsthen 	iter_env->use_nat64 = cfg->do_nat64;
207e21c60efSsthen 	iter_env->outbound_msg_retry = cfg->outbound_msg_retry;
2088b7325afSsthen 	iter_env->max_sent_count = cfg->max_sent_count;
2098b7325afSsthen 	iter_env->max_query_restarts = cfg->max_query_restarts;
210933707f3Ssthen 	return 1;
211933707f3Ssthen }
212933707f3Ssthen 
213933707f3Ssthen /** filter out unsuitable targets
214933707f3Ssthen  * @param iter_env: iterator environment with ipv6-support flag.
215933707f3Ssthen  * @param env: module environment with infra cache.
216933707f3Ssthen  * @param name: zone name
217933707f3Ssthen  * @param namelen: length of name
218933707f3Ssthen  * @param qtype: query type (host order).
219933707f3Ssthen  * @param now: current time
220933707f3Ssthen  * @param a: address in delegation point we are examining.
221933707f3Ssthen  * @return an integer that signals the target suitability.
222933707f3Ssthen  *	as follows:
223933707f3Ssthen  *	-1: The address should be omitted from the list.
224933707f3Ssthen  *	    Because:
225933707f3Ssthen  *		o The address is bogus (DNSSEC validation failure).
226933707f3Ssthen  *		o Listed as donotquery
227933707f3Ssthen  *		o is ipv6 but no ipv6 support (in operating system).
228933707f3Ssthen  *		o is ipv4 but no ipv4 support (in operating system).
229933707f3Ssthen  *		o is lame
230933707f3Ssthen  *	Otherwise, an rtt in milliseconds.
231933707f3Ssthen  *	0 .. USEFUL_SERVER_TOP_TIMEOUT-1
232933707f3Ssthen  *		The roundtrip time timeout estimate. less than 2 minutes.
233933707f3Ssthen  *		Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
234933707f3Ssthen  *		values 0 .. 49 are not used, unless that is changed.
235933707f3Ssthen  *	USEFUL_SERVER_TOP_TIMEOUT
236933707f3Ssthen  *		This value exactly is given for unresponsive blacklisted.
237933707f3Ssthen  *	USEFUL_SERVER_TOP_TIMEOUT+1
238933707f3Ssthen  *		For non-blacklisted servers: huge timeout, but has traffic.
239933707f3Ssthen  *	USEFUL_SERVER_TOP_TIMEOUT*1 ..
240933707f3Ssthen  *		parent-side lame servers get this penalty. A dispreferential
241933707f3Ssthen  *		server. (lame in delegpt).
242933707f3Ssthen  *	USEFUL_SERVER_TOP_TIMEOUT*2 ..
243933707f3Ssthen  *		dnsseclame servers get penalty
244933707f3Ssthen  *	USEFUL_SERVER_TOP_TIMEOUT*3 ..
245933707f3Ssthen  *		recursion lame servers get penalty
246933707f3Ssthen  *	UNKNOWN_SERVER_NICENESS
247933707f3Ssthen  *		If no information is known about the server, this is
248933707f3Ssthen  *		returned. 376 msec or so.
249933707f3Ssthen  *	+BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
250933707f3Ssthen  *
251933707f3Ssthen  * When a final value is chosen that is dnsseclame ; dnsseclameness checking
252933707f3Ssthen  * is turned off (so we do not discard the reply).
253933707f3Ssthen  * When a final value is chosen that is recursionlame; RD bit is set on query.
254933707f3Ssthen  * Because of the numbers this means recursionlame also have dnssec lameness
255933707f3Ssthen  * checking turned off.
256933707f3Ssthen  */
257933707f3Ssthen static int
258933707f3Ssthen iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
259229e174cSsthen 	uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
260933707f3Ssthen 	struct delegpt_addr* a)
261933707f3Ssthen {
262933707f3Ssthen 	int rtt, lame, reclame, dnsseclame;
263933707f3Ssthen 	if(a->bogus)
264933707f3Ssthen 		return -1; /* address of server is bogus */
265933707f3Ssthen 	if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
266933707f3Ssthen 		log_addr(VERB_ALGO, "skip addr on the donotquery list",
267933707f3Ssthen 			&a->addr, a->addrlen);
268933707f3Ssthen 		return -1; /* server is on the donotquery list */
269933707f3Ssthen 	}
270933707f3Ssthen 	if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
271933707f3Ssthen 		return -1; /* there is no ip6 available */
272933707f3Ssthen 	}
2738b7325afSsthen 	if(!iter_env->supports_ipv4 && !iter_env->use_nat64 &&
2748b7325afSsthen 	   !addr_is_ip6(&a->addr, a->addrlen)) {
275933707f3Ssthen 		return -1; /* there is no ip4 available */
276933707f3Ssthen 	}
277933707f3Ssthen 	/* check lameness - need zone , class info */
278933707f3Ssthen 	if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
279933707f3Ssthen 		name, namelen, qtype, &lame, &dnsseclame, &reclame,
280933707f3Ssthen 		&rtt, now)) {
281933707f3Ssthen 		log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
282*98bc733bSsthen 		verbose(VERB_ALGO, "   rtt=%d%s%s%s%s%s", rtt,
283933707f3Ssthen 			lame?" LAME":"",
284933707f3Ssthen 			dnsseclame?" DNSSEC_LAME":"",
285*98bc733bSsthen 			a->dnsseclame?" ADDR_DNSSEC_LAME":"",
286933707f3Ssthen 			reclame?" REC_LAME":"",
287933707f3Ssthen 			a->lame?" ADDR_LAME":"");
288933707f3Ssthen 		if(lame)
289933707f3Ssthen 			return -1; /* server is lame */
290933707f3Ssthen 		else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
291933707f3Ssthen 			/* server is unresponsive,
2924bfc71b0Ssthen 			 * we used to return TOP_TIMEOUT, but fairly useless,
293933707f3Ssthen 			 * because if == TOP_TIMEOUT is dropped because
294933707f3Ssthen 			 * blacklisted later, instead, remove it here, so
295933707f3Ssthen 			 * other choices (that are not blacklisted) can be
296933707f3Ssthen 			 * tried */
297933707f3Ssthen 			return -1;
298933707f3Ssthen 		/* select remainder from worst to best */
299933707f3Ssthen 		else if(reclame)
300933707f3Ssthen 			return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
301229e174cSsthen 		else if(dnsseclame || a->dnsseclame)
302933707f3Ssthen 			return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
303933707f3Ssthen 		else if(a->lame)
304933707f3Ssthen 			return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
305933707f3Ssthen 		else	return rtt;
306933707f3Ssthen 	}
307933707f3Ssthen 	/* no server information present */
308229e174cSsthen 	if(a->dnsseclame)
309229e174cSsthen 		return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
310229e174cSsthen 	else if(a->lame)
311933707f3Ssthen 		return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
312933707f3Ssthen 	return UNKNOWN_SERVER_NICENESS;
313933707f3Ssthen }
314933707f3Ssthen 
315933707f3Ssthen /** lookup RTT information, and also store fastest rtt (if any) */
316933707f3Ssthen static int
317933707f3Ssthen iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
318229e174cSsthen 	uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
3193150e5f6Ssthen 	struct delegpt* dp, int* best_rtt, struct sock_list* blacklist,
3203150e5f6Ssthen 	size_t* num_suitable_results)
321933707f3Ssthen {
322933707f3Ssthen 	int got_it = 0;
323933707f3Ssthen 	struct delegpt_addr* a;
3243150e5f6Ssthen 	*num_suitable_results = 0;
3253150e5f6Ssthen 
326933707f3Ssthen 	if(dp->bogus)
327933707f3Ssthen 		return 0; /* NS bogus, all bogus, nothing found */
328933707f3Ssthen 	for(a=dp->result_list; a; a = a->next_result) {
329933707f3Ssthen 		a->sel_rtt = iter_filter_unsuitable(iter_env, env,
330933707f3Ssthen 			name, namelen, qtype, now, a);
331933707f3Ssthen 		if(a->sel_rtt != -1) {
332933707f3Ssthen 			if(sock_list_find(blacklist, &a->addr, a->addrlen))
333933707f3Ssthen 				a->sel_rtt += BLACKLIST_PENALTY;
334933707f3Ssthen 
335933707f3Ssthen 			if(!got_it) {
336933707f3Ssthen 				*best_rtt = a->sel_rtt;
337933707f3Ssthen 				got_it = 1;
338933707f3Ssthen 			} else if(a->sel_rtt < *best_rtt) {
339933707f3Ssthen 				*best_rtt = a->sel_rtt;
340933707f3Ssthen 			}
3413150e5f6Ssthen 			(*num_suitable_results)++;
342933707f3Ssthen 		}
343933707f3Ssthen 	}
344933707f3Ssthen 	return got_it;
345933707f3Ssthen }
346933707f3Ssthen 
3473150e5f6Ssthen /** compare two rtts, return -1, 0 or 1 */
3483150e5f6Ssthen static int
3493150e5f6Ssthen rtt_compare(const void* x, const void* y)
3503150e5f6Ssthen {
3513150e5f6Ssthen 	if(*(int*)x == *(int*)y)
3523150e5f6Ssthen 		return 0;
3533150e5f6Ssthen 	if(*(int*)x > *(int*)y)
3543150e5f6Ssthen 		return 1;
3553150e5f6Ssthen 	return -1;
3563150e5f6Ssthen }
3573150e5f6Ssthen 
3583150e5f6Ssthen /** get RTT for the Nth fastest server */
3593150e5f6Ssthen static int
3603150e5f6Ssthen nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n)
3613150e5f6Ssthen {
3623150e5f6Ssthen 	int rtt_band;
3633150e5f6Ssthen 	size_t i;
3643150e5f6Ssthen 	int* rtt_list, *rtt_index;
3653150e5f6Ssthen 
3663150e5f6Ssthen 	if(num_results < 1 || n >= num_results) {
3673150e5f6Ssthen 		return -1;
3683150e5f6Ssthen 	}
3693150e5f6Ssthen 
3703150e5f6Ssthen 	rtt_list = calloc(num_results, sizeof(int));
3713150e5f6Ssthen 	if(!rtt_list) {
3723150e5f6Ssthen 		log_err("malloc failure: allocating rtt_list");
3733150e5f6Ssthen 		return -1;
3743150e5f6Ssthen 	}
3753150e5f6Ssthen 	rtt_index = rtt_list;
3763150e5f6Ssthen 
3773150e5f6Ssthen 	for(i=0; i<num_results && result_list; i++) {
3783150e5f6Ssthen 		if(result_list->sel_rtt != -1) {
3793150e5f6Ssthen 			*rtt_index = result_list->sel_rtt;
3803150e5f6Ssthen 			rtt_index++;
3813150e5f6Ssthen 		}
3823150e5f6Ssthen 		result_list=result_list->next_result;
3833150e5f6Ssthen 	}
3843150e5f6Ssthen 	qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare);
3853150e5f6Ssthen 
3863150e5f6Ssthen 	log_assert(n > 0);
3873150e5f6Ssthen 	rtt_band = rtt_list[n-1];
3883150e5f6Ssthen 	free(rtt_list);
3893150e5f6Ssthen 
3903150e5f6Ssthen 	return rtt_band;
3913150e5f6Ssthen }
3923150e5f6Ssthen 
3934bfc71b0Ssthen /** filter the address list, putting best targets at front,
394933707f3Ssthen  * returns number of best targets (or 0, no suitable targets) */
395933707f3Ssthen static int
396933707f3Ssthen iter_filter_order(struct iter_env* iter_env, struct module_env* env,
397229e174cSsthen 	uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
398933707f3Ssthen 	struct delegpt* dp, int* selected_rtt, int open_target,
39920237c55Ssthen 	struct sock_list* blacklist, time_t prefetch)
400933707f3Ssthen {
4013150e5f6Ssthen 	int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth;
402d1e2768aSsthen 	int alllame = 0;
4033150e5f6Ssthen 	size_t num_results;
404933707f3Ssthen 	struct delegpt_addr* a, *n, *prev=NULL;
405933707f3Ssthen 
406933707f3Ssthen 	/* fillup sel_rtt and find best rtt in the bunch */
407933707f3Ssthen 	got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
4083150e5f6Ssthen 		&low_rtt, blacklist, &num_results);
409933707f3Ssthen 	if(got_num == 0)
410933707f3Ssthen 		return 0;
411933707f3Ssthen 	if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
412d1e2768aSsthen 		/* If all missing (or not fully resolved) targets are lame,
413d1e2768aSsthen 		 * then use the remaining lame address. */
414d1e2768aSsthen 		((delegpt_count_missing_targets(dp, &alllame) > 0 && !alllame) ||
415d1e2768aSsthen 		open_target > 0)) {
416933707f3Ssthen 		verbose(VERB_ALGO, "Bad choices, trying to get more choice");
417933707f3Ssthen 		return 0; /* we want more choice. The best choice is a bad one.
418933707f3Ssthen 			     return 0 to force the caller to fetch more */
419933707f3Ssthen 	}
420933707f3Ssthen 
4213150e5f6Ssthen 	if(env->cfg->fast_server_permil != 0 && prefetch == 0 &&
4223150e5f6Ssthen 		num_results > env->cfg->fast_server_num &&
4233150e5f6Ssthen 		ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) {
42420237c55Ssthen 		/* the query is not prefetch, but for a downstream client,
4253150e5f6Ssthen 		 * there are more servers available then the fastest N we want
4263150e5f6Ssthen 		 * to choose from. Limit our choice to the fastest servers. */
4273150e5f6Ssthen 		nth = nth_rtt(dp->result_list, num_results,
4283150e5f6Ssthen 			env->cfg->fast_server_num);
4293150e5f6Ssthen 		if(nth > 0) {
4303150e5f6Ssthen 			rtt_band = nth - low_rtt;
4313150e5f6Ssthen 			if(rtt_band > RTT_BAND)
4323150e5f6Ssthen 				rtt_band = RTT_BAND;
4333150e5f6Ssthen 		}
43420237c55Ssthen 	}
43520237c55Ssthen 
436933707f3Ssthen 	got_num = 0;
437933707f3Ssthen 	a = dp->result_list;
438933707f3Ssthen 	while(a) {
439933707f3Ssthen 		/* skip unsuitable targets */
440933707f3Ssthen 		if(a->sel_rtt == -1) {
441933707f3Ssthen 			prev = a;
442933707f3Ssthen 			a = a->next_result;
443933707f3Ssthen 			continue;
444933707f3Ssthen 		}
445933707f3Ssthen 		/* classify the server address and determine what to do */
446933707f3Ssthen 		swap_to_front = 0;
44720237c55Ssthen 		if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) {
448933707f3Ssthen 			got_num++;
449933707f3Ssthen 			swap_to_front = 1;
45020237c55Ssthen 		} else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) {
451933707f3Ssthen 			got_num++;
452933707f3Ssthen 			swap_to_front = 1;
453933707f3Ssthen 		}
454933707f3Ssthen 		/* swap to front if necessary, or move to next result */
455933707f3Ssthen 		if(swap_to_front && prev) {
456933707f3Ssthen 			n = a->next_result;
457933707f3Ssthen 			prev->next_result = n;
458933707f3Ssthen 			a->next_result = dp->result_list;
459933707f3Ssthen 			dp->result_list = a;
460933707f3Ssthen 			a = n;
461933707f3Ssthen 		} else {
462933707f3Ssthen 			prev = a;
463933707f3Ssthen 			a = a->next_result;
464933707f3Ssthen 		}
465933707f3Ssthen 	}
466933707f3Ssthen 	*selected_rtt = low_rtt;
46777079be7Ssthen 
46877079be7Ssthen 	if (env->cfg->prefer_ip6) {
46977079be7Ssthen 		int got_num6 = 0;
47077079be7Ssthen 		int low_rtt6 = 0;
47177079be7Ssthen 		int i;
4722308e98cSsthen 		int attempt = -1; /* filter to make sure addresses have
4732308e98cSsthen 		  less attempts on them than the first, to force round
4742308e98cSsthen 		  robin when all the IPv6 addresses fail */
4752308e98cSsthen 		int num4ok = 0; /* number ip4 at low attempt count */
4762308e98cSsthen 		int num4_lowrtt = 0;
47777079be7Ssthen 		prev = NULL;
47877079be7Ssthen 		a = dp->result_list;
47977079be7Ssthen 		for(i = 0; i < got_num; i++) {
480191f22c6Ssthen 			if(!a) break; /* robustness */
48177079be7Ssthen 			swap_to_front = 0;
4822308e98cSsthen 			if(a->addr.ss_family != AF_INET6 && attempt == -1) {
4832308e98cSsthen 				/* if we only have ip4 at low attempt count,
4842308e98cSsthen 				 * then ip6 is failing, and we need to
4852308e98cSsthen 				 * select one of the remaining IPv4 addrs */
4862308e98cSsthen 				attempt = a->attempts;
4872308e98cSsthen 				num4ok++;
4882308e98cSsthen 				num4_lowrtt = a->sel_rtt;
4892308e98cSsthen 			} else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) {
4902308e98cSsthen 				num4ok++;
4912308e98cSsthen 				if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) {
4922308e98cSsthen 					num4_lowrtt = a->sel_rtt;
4932308e98cSsthen 				}
4942308e98cSsthen 			}
49577079be7Ssthen 			if(a->addr.ss_family == AF_INET6) {
4962308e98cSsthen 				if(attempt == -1) {
4972308e98cSsthen 					attempt = a->attempts;
4982308e98cSsthen 				} else if(a->attempts > attempt) {
4992308e98cSsthen 					break;
5002308e98cSsthen 				}
50177079be7Ssthen 				got_num6++;
50277079be7Ssthen 				swap_to_front = 1;
50377079be7Ssthen 				if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) {
50477079be7Ssthen 					low_rtt6 = a->sel_rtt;
50577079be7Ssthen 				}
50677079be7Ssthen 			}
50777079be7Ssthen 			/* swap to front if IPv6, or move to next result */
50877079be7Ssthen 			if(swap_to_front && prev) {
50977079be7Ssthen 				n = a->next_result;
51077079be7Ssthen 				prev->next_result = n;
51177079be7Ssthen 				a->next_result = dp->result_list;
51277079be7Ssthen 				dp->result_list = a;
51377079be7Ssthen 				a = n;
51477079be7Ssthen 			} else {
51577079be7Ssthen 				prev = a;
51677079be7Ssthen 				a = a->next_result;
51777079be7Ssthen 			}
51877079be7Ssthen 		}
51977079be7Ssthen 		if(got_num6 > 0) {
52077079be7Ssthen 			got_num = got_num6;
52177079be7Ssthen 			*selected_rtt = low_rtt6;
5222308e98cSsthen 		} else if(num4ok > 0) {
5232308e98cSsthen 			got_num = num4ok;
5242308e98cSsthen 			*selected_rtt = num4_lowrtt;
52577079be7Ssthen 		}
526a3167c07Ssthen 	} else if (env->cfg->prefer_ip4) {
527a3167c07Ssthen 		int got_num4 = 0;
528a3167c07Ssthen 		int low_rtt4 = 0;
529a3167c07Ssthen 		int i;
530a3167c07Ssthen 		int attempt = -1; /* filter to make sure addresses have
531a3167c07Ssthen 		  less attempts on them than the first, to force round
532a3167c07Ssthen 		  robin when all the IPv4 addresses fail */
533a3167c07Ssthen 		int num6ok = 0; /* number ip6 at low attempt count */
534a3167c07Ssthen 		int num6_lowrtt = 0;
535a3167c07Ssthen 		prev = NULL;
536a3167c07Ssthen 		a = dp->result_list;
537a3167c07Ssthen 		for(i = 0; i < got_num; i++) {
538191f22c6Ssthen 			if(!a) break; /* robustness */
539a3167c07Ssthen 			swap_to_front = 0;
540a3167c07Ssthen 			if(a->addr.ss_family != AF_INET && attempt == -1) {
541a3167c07Ssthen 				/* if we only have ip6 at low attempt count,
542a3167c07Ssthen 				 * then ip4 is failing, and we need to
543a3167c07Ssthen 				 * select one of the remaining IPv6 addrs */
544a3167c07Ssthen 				attempt = a->attempts;
545a3167c07Ssthen 				num6ok++;
546a3167c07Ssthen 				num6_lowrtt = a->sel_rtt;
547a3167c07Ssthen 			} else if(a->addr.ss_family != AF_INET && attempt == a->attempts) {
548a3167c07Ssthen 				num6ok++;
549a3167c07Ssthen 				if(num6_lowrtt == 0 || a->sel_rtt < num6_lowrtt) {
550a3167c07Ssthen 					num6_lowrtt = a->sel_rtt;
551a3167c07Ssthen 				}
552a3167c07Ssthen 			}
553a3167c07Ssthen 			if(a->addr.ss_family == AF_INET) {
554a3167c07Ssthen 				if(attempt == -1) {
555a3167c07Ssthen 					attempt = a->attempts;
556a3167c07Ssthen 				} else if(a->attempts > attempt) {
557a3167c07Ssthen 					break;
558a3167c07Ssthen 				}
559a3167c07Ssthen 				got_num4++;
560a3167c07Ssthen 				swap_to_front = 1;
561a3167c07Ssthen 				if(low_rtt4 == 0 || a->sel_rtt < low_rtt4) {
562a3167c07Ssthen 					low_rtt4 = a->sel_rtt;
563a3167c07Ssthen 				}
564a3167c07Ssthen 			}
565a3167c07Ssthen 			/* swap to front if IPv4, or move to next result */
566a3167c07Ssthen 			if(swap_to_front && prev) {
567a3167c07Ssthen 				n = a->next_result;
568a3167c07Ssthen 				prev->next_result = n;
569a3167c07Ssthen 				a->next_result = dp->result_list;
570a3167c07Ssthen 				dp->result_list = a;
571a3167c07Ssthen 				a = n;
572a3167c07Ssthen 			} else {
573a3167c07Ssthen 				prev = a;
574a3167c07Ssthen 				a = a->next_result;
575a3167c07Ssthen 			}
576a3167c07Ssthen 		}
577a3167c07Ssthen 		if(got_num4 > 0) {
578a3167c07Ssthen 			got_num = got_num4;
579a3167c07Ssthen 			*selected_rtt = low_rtt4;
580a3167c07Ssthen 		} else if(num6ok > 0) {
581a3167c07Ssthen 			got_num = num6ok;
582a3167c07Ssthen 			*selected_rtt = num6_lowrtt;
583a3167c07Ssthen 		}
58477079be7Ssthen 	}
585933707f3Ssthen 	return got_num;
586933707f3Ssthen }
587933707f3Ssthen 
588933707f3Ssthen struct delegpt_addr*
589933707f3Ssthen iter_server_selection(struct iter_env* iter_env,
590933707f3Ssthen 	struct module_env* env, struct delegpt* dp,
591933707f3Ssthen 	uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
59220237c55Ssthen 	int* chase_to_rd, int open_target, struct sock_list* blacklist,
59320237c55Ssthen 	time_t prefetch)
594933707f3Ssthen {
595933707f3Ssthen 	int sel;
596933707f3Ssthen 	int selrtt;
597933707f3Ssthen 	struct delegpt_addr* a, *prev;
598933707f3Ssthen 	int num = iter_filter_order(iter_env, env, name, namelen, qtype,
59920237c55Ssthen 		*env->now, dp, &selrtt, open_target, blacklist, prefetch);
600933707f3Ssthen 
601933707f3Ssthen 	if(num == 0)
602933707f3Ssthen 		return NULL;
603933707f3Ssthen 	verbose(VERB_ALGO, "selrtt %d", selrtt);
604933707f3Ssthen 	if(selrtt > BLACKLIST_PENALTY) {
605933707f3Ssthen 		if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
606933707f3Ssthen 			verbose(VERB_ALGO, "chase to "
607933707f3Ssthen 				"blacklisted recursion lame server");
608933707f3Ssthen 			*chase_to_rd = 1;
609933707f3Ssthen 		}
610933707f3Ssthen 		if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
611933707f3Ssthen 			verbose(VERB_ALGO, "chase to "
612933707f3Ssthen 				"blacklisted dnssec lame server");
613933707f3Ssthen 			*dnssec_lame = 1;
614933707f3Ssthen 		}
615933707f3Ssthen 	} else {
616933707f3Ssthen 		if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
617933707f3Ssthen 			verbose(VERB_ALGO, "chase to recursion lame server");
618933707f3Ssthen 			*chase_to_rd = 1;
619933707f3Ssthen 		}
620933707f3Ssthen 		if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
621933707f3Ssthen 			verbose(VERB_ALGO, "chase to dnssec lame server");
622933707f3Ssthen 			*dnssec_lame = 1;
623933707f3Ssthen 		}
624933707f3Ssthen 		if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
625933707f3Ssthen 			verbose(VERB_ALGO, "chase to blacklisted lame server");
626933707f3Ssthen 			return NULL;
627933707f3Ssthen 		}
628933707f3Ssthen 	}
629933707f3Ssthen 
630933707f3Ssthen 	if(num == 1) {
631933707f3Ssthen 		a = dp->result_list;
632e21c60efSsthen 		if(++a->attempts < iter_env->outbound_msg_retry)
633933707f3Ssthen 			return a;
634933707f3Ssthen 		dp->result_list = a->next_result;
635933707f3Ssthen 		return a;
636933707f3Ssthen 	}
637933707f3Ssthen 
638933707f3Ssthen 	/* randomly select a target from the list */
639933707f3Ssthen 	log_assert(num > 1);
640933707f3Ssthen 	/* grab secure random number, to pick unexpected server.
641933707f3Ssthen 	 * also we need it to be threadsafe. */
642933707f3Ssthen 	sel = ub_random_max(env->rnd, num);
643933707f3Ssthen 	a = dp->result_list;
644933707f3Ssthen 	prev = NULL;
645933707f3Ssthen 	while(sel > 0 && a) {
646933707f3Ssthen 		prev = a;
647933707f3Ssthen 		a = a->next_result;
648933707f3Ssthen 		sel--;
649933707f3Ssthen 	}
650933707f3Ssthen 	if(!a)  /* robustness */
651933707f3Ssthen 		return NULL;
652e21c60efSsthen 	if(++a->attempts < iter_env->outbound_msg_retry)
653933707f3Ssthen 		return a;
654933707f3Ssthen 	/* remove it from the delegation point result list */
655933707f3Ssthen 	if(prev)
656933707f3Ssthen 		prev->next_result = a->next_result;
657933707f3Ssthen 	else	dp->result_list = a->next_result;
658933707f3Ssthen 	return a;
659933707f3Ssthen }
660933707f3Ssthen 
661933707f3Ssthen struct dns_msg*
6625d76a658Ssthen dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg,
663933707f3Ssthen 	struct regional* region)
664933707f3Ssthen {
665933707f3Ssthen 	struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
666933707f3Ssthen 		sizeof(struct dns_msg));
667933707f3Ssthen 	if(!m)
668933707f3Ssthen 		return NULL;
669933707f3Ssthen 	memset(m, 0, sizeof(*m));
670933707f3Ssthen 	if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
671933707f3Ssthen 		log_err("malloc failure: allocating incoming dns_msg");
672933707f3Ssthen 		return NULL;
673933707f3Ssthen 	}
674933707f3Ssthen 	return m;
675933707f3Ssthen }
676933707f3Ssthen 
677933707f3Ssthen struct dns_msg*
678933707f3Ssthen dns_copy_msg(struct dns_msg* from, struct regional* region)
679933707f3Ssthen {
680933707f3Ssthen 	struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
681933707f3Ssthen 		sizeof(struct dns_msg));
682933707f3Ssthen 	if(!m)
683933707f3Ssthen 		return NULL;
684933707f3Ssthen 	m->qinfo = from->qinfo;
685933707f3Ssthen 	if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
686933707f3Ssthen 		from->qinfo.qname_len)))
687933707f3Ssthen 		return NULL;
688933707f3Ssthen 	if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
689933707f3Ssthen 		return NULL;
690933707f3Ssthen 	return m;
691933707f3Ssthen }
692933707f3Ssthen 
6933dcb24b8Ssthen void
694933707f3Ssthen iter_dns_store(struct module_env* env, struct query_info* msgqinf,
695229e174cSsthen 	struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
696d1e2768aSsthen 	struct regional* region, uint16_t flags, time_t qstarttime)
697933707f3Ssthen {
6983dcb24b8Ssthen 	if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
699d1e2768aSsthen 		pside, region, flags, qstarttime))
7003dcb24b8Ssthen 		log_err("out of memory: cannot store data in cache");
701933707f3Ssthen }
702933707f3Ssthen 
703933707f3Ssthen int
704933707f3Ssthen iter_ns_probability(struct ub_randstate* rnd, int n, int m)
705933707f3Ssthen {
706933707f3Ssthen 	int sel;
707933707f3Ssthen 	if(n == m) /* 100% chance */
708933707f3Ssthen 		return 1;
709933707f3Ssthen 	/* we do not need secure random numbers here, but
710933707f3Ssthen 	 * we do need it to be threadsafe, so we use this */
711933707f3Ssthen 	sel = ub_random_max(rnd, m);
712933707f3Ssthen 	return (sel < n);
713933707f3Ssthen }
714933707f3Ssthen 
715933707f3Ssthen /** detect dependency cycle for query and target */
716933707f3Ssthen static int
717933707f3Ssthen causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
718933707f3Ssthen 	uint16_t t, uint16_t c)
719933707f3Ssthen {
720933707f3Ssthen 	struct query_info qinf;
721933707f3Ssthen 	qinf.qname = name;
722933707f3Ssthen 	qinf.qname_len = namelen;
723933707f3Ssthen 	qinf.qtype = t;
724933707f3Ssthen 	qinf.qclass = c;
72577079be7Ssthen 	qinf.local_alias = NULL;
726933707f3Ssthen 	fptr_ok(fptr_whitelist_modenv_detect_cycle(
727933707f3Ssthen 		qstate->env->detect_cycle));
728933707f3Ssthen 	return (*qstate->env->detect_cycle)(qstate, &qinf,
72957dceb2aSbrad 		(uint16_t)(BIT_RD|BIT_CD), qstate->is_priming,
73057dceb2aSbrad 		qstate->is_valrec);
731933707f3Ssthen }
732933707f3Ssthen 
733933707f3Ssthen void
734933707f3Ssthen iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
735933707f3Ssthen {
736933707f3Ssthen 	struct delegpt_ns* ns;
737933707f3Ssthen 	for(ns = dp->nslist; ns; ns = ns->next) {
738933707f3Ssthen 		if(ns->resolved)
739933707f3Ssthen 			continue;
740933707f3Ssthen 		/* see if this ns as target causes dependency cycle */
741933707f3Ssthen 		if(causes_cycle(qstate, ns->name, ns->namelen,
742933707f3Ssthen 			LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
743933707f3Ssthen 		   causes_cycle(qstate, ns->name, ns->namelen,
744933707f3Ssthen 			LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
745933707f3Ssthen 			log_nametypeclass(VERB_QUERY, "skipping target due "
746933707f3Ssthen 			 	"to dependency cycle (harden-glue: no may "
747933707f3Ssthen 				"fix some of the cycles)",
748933707f3Ssthen 				ns->name, LDNS_RR_TYPE_A,
749933707f3Ssthen 				qstate->qinfo.qclass);
750933707f3Ssthen 			ns->resolved = 1;
751933707f3Ssthen 		}
752933707f3Ssthen 	}
753933707f3Ssthen }
754933707f3Ssthen 
755933707f3Ssthen void
756933707f3Ssthen iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
757933707f3Ssthen {
758933707f3Ssthen 	struct delegpt_ns* ns;
759933707f3Ssthen 	for(ns = dp->nslist; ns; ns = ns->next) {
760933707f3Ssthen 		if(ns->done_pside4 && ns->done_pside6)
761933707f3Ssthen 			continue;
762933707f3Ssthen 		/* see if this ns as target causes dependency cycle */
763933707f3Ssthen 		if(causes_cycle(qstate, ns->name, ns->namelen,
764933707f3Ssthen 			LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
765933707f3Ssthen 			log_nametypeclass(VERB_QUERY, "skipping target due "
766933707f3Ssthen 			 	"to dependency cycle", ns->name,
767933707f3Ssthen 				LDNS_RR_TYPE_A, qstate->qinfo.qclass);
768933707f3Ssthen 			ns->done_pside4 = 1;
769933707f3Ssthen 		}
770933707f3Ssthen 		if(causes_cycle(qstate, ns->name, ns->namelen,
771933707f3Ssthen 			LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
772933707f3Ssthen 			log_nametypeclass(VERB_QUERY, "skipping target due "
773933707f3Ssthen 			 	"to dependency cycle", ns->name,
774933707f3Ssthen 				LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
775933707f3Ssthen 			ns->done_pside6 = 1;
776933707f3Ssthen 		}
777933707f3Ssthen 	}
778933707f3Ssthen }
779933707f3Ssthen 
780933707f3Ssthen int
781933707f3Ssthen iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
7828b7325afSsthen 	struct delegpt* dp, int supports_ipv4, int supports_ipv6,
7838b7325afSsthen 	int use_nat64)
784933707f3Ssthen {
785933707f3Ssthen 	struct delegpt_ns* ns;
7860bdb4f62Ssthen 	struct delegpt_addr* a;
7878b7325afSsthen 
7888b7325afSsthen 	if(supports_ipv6 && use_nat64)
7898b7325afSsthen 		supports_ipv4 = 1;
7908b7325afSsthen 
791933707f3Ssthen 	/* check:
792933707f3Ssthen 	 *      o RD qflag is on.
793933707f3Ssthen 	 *      o no addresses are provided.
794933707f3Ssthen 	 *      o all NS items are required glue.
795933707f3Ssthen 	 * OR
796933707f3Ssthen 	 *      o RD qflag is on.
797933707f3Ssthen 	 *      o no addresses are provided.
798933707f3Ssthen 	 *      o the query is for one of the nameservers in dp,
799933707f3Ssthen 	 *        and that nameserver is a glue-name for this dp.
800933707f3Ssthen 	 */
801933707f3Ssthen 	if(!(qflags&BIT_RD))
802933707f3Ssthen 		return 0;
8030bdb4f62Ssthen 	/* either available or unused targets,
8040bdb4f62Ssthen 	 * if they exist, the dp is not useless. */
8050bdb4f62Ssthen 	for(a = dp->usable_list; a; a = a->next_usable) {
8060bdb4f62Ssthen 		if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
807933707f3Ssthen 			return 0;
8080bdb4f62Ssthen 		else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
8090bdb4f62Ssthen 			return 0;
8100bdb4f62Ssthen 	}
8110bdb4f62Ssthen 	for(a = dp->result_list; a; a = a->next_result) {
8120bdb4f62Ssthen 		if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
8130bdb4f62Ssthen 			return 0;
8140bdb4f62Ssthen 		else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
8150bdb4f62Ssthen 			return 0;
8160bdb4f62Ssthen 	}
817933707f3Ssthen 
818933707f3Ssthen 	/* see if query is for one of the nameservers, which is glue */
8190bdb4f62Ssthen 	if( ((qinfo->qtype == LDNS_RR_TYPE_A && supports_ipv4) ||
8200bdb4f62Ssthen 		(qinfo->qtype == LDNS_RR_TYPE_AAAA && supports_ipv6)) &&
821933707f3Ssthen 		dname_subdomain_c(qinfo->qname, dp->name) &&
822933707f3Ssthen 		delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
823933707f3Ssthen 		return 1;
824933707f3Ssthen 
825933707f3Ssthen 	for(ns = dp->nslist; ns; ns = ns->next) {
826933707f3Ssthen 		if(ns->resolved) /* skip failed targets */
827933707f3Ssthen 			continue;
828933707f3Ssthen 		if(!dname_subdomain_c(ns->name, dp->name))
829933707f3Ssthen 			return 0; /* one address is not required glue */
830933707f3Ssthen 	}
831933707f3Ssthen 	return 1;
832933707f3Ssthen }
833933707f3Ssthen 
834933707f3Ssthen int
83520237c55Ssthen iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo)
8362ee382b6Ssthen {
8372ee382b6Ssthen 	struct trust_anchor* a;
8382ee382b6Ssthen 	if(!env || !env->anchors || !qinfo || !qinfo->qname)
8392ee382b6Ssthen 		return 0;
8402ee382b6Ssthen 	/* a trust anchor exists above the name? */
8412ee382b6Ssthen 	if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len,
8422ee382b6Ssthen 		qinfo->qclass))) {
8432ee382b6Ssthen 		if(a->numDS == 0 && a->numDNSKEY == 0) {
8442ee382b6Ssthen 			/* insecure trust point */
8452ee382b6Ssthen 			lock_basic_unlock(&a->lock);
8462ee382b6Ssthen 			return 0;
8472ee382b6Ssthen 		}
8482ee382b6Ssthen 		lock_basic_unlock(&a->lock);
8492ee382b6Ssthen 		return 1;
8502ee382b6Ssthen 	}
8512ee382b6Ssthen 	/* no trust anchor above it. */
8522ee382b6Ssthen 	return 0;
8532ee382b6Ssthen }
8542ee382b6Ssthen 
8552ee382b6Ssthen int
856933707f3Ssthen iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
857933707f3Ssthen         struct dns_msg* msg, uint16_t dclass)
858933707f3Ssthen {
859933707f3Ssthen 	struct trust_anchor* a;
860933707f3Ssthen 	/* information not available, !env->anchors can be common */
861933707f3Ssthen 	if(!env || !env->anchors || !dp || !dp->name)
862933707f3Ssthen 		return 0;
863933707f3Ssthen 	/* a trust anchor exists with this name, RRSIGs expected */
864933707f3Ssthen 	if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
865933707f3Ssthen 		dclass))) {
866bdfc4d55Sflorian 		if(a->numDS == 0 && a->numDNSKEY == 0) {
867bdfc4d55Sflorian 			/* insecure trust point */
868bdfc4d55Sflorian 			lock_basic_unlock(&a->lock);
869bdfc4d55Sflorian 			return 0;
870bdfc4d55Sflorian 		}
871933707f3Ssthen 		lock_basic_unlock(&a->lock);
872933707f3Ssthen 		return 1;
873933707f3Ssthen 	}
874933707f3Ssthen 	/* see if DS rrset was given, in AUTH section */
875933707f3Ssthen 	if(msg && msg->rep &&
876933707f3Ssthen 		reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
877933707f3Ssthen 		LDNS_RR_TYPE_DS, dclass))
878933707f3Ssthen 		return 1;
879933707f3Ssthen 	/* look in key cache */
880933707f3Ssthen 	if(env->key_cache) {
881933707f3Ssthen 		struct key_entry_key* kk = key_cache_obtain(env->key_cache,
882933707f3Ssthen 			dp->name, dp->namelen, dclass, env->scratch, *env->now);
883933707f3Ssthen 		if(kk) {
884933707f3Ssthen 			if(query_dname_compare(kk->name, dp->name) == 0) {
885933707f3Ssthen 			  if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
886933707f3Ssthen 				regional_free_all(env->scratch);
887933707f3Ssthen 				return 1;
888933707f3Ssthen 			  } else if(key_entry_isnull(kk)) {
889933707f3Ssthen 				regional_free_all(env->scratch);
890933707f3Ssthen 				return 0;
891933707f3Ssthen 			  }
892933707f3Ssthen 			}
893933707f3Ssthen 			regional_free_all(env->scratch);
894933707f3Ssthen 		}
895933707f3Ssthen 	}
896933707f3Ssthen 	return 0;
897933707f3Ssthen }
898933707f3Ssthen 
899933707f3Ssthen int
900933707f3Ssthen iter_msg_has_dnssec(struct dns_msg* msg)
901933707f3Ssthen {
902933707f3Ssthen 	size_t i;
903933707f3Ssthen 	if(!msg || !msg->rep)
904933707f3Ssthen 		return 0;
905933707f3Ssthen 	for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
906933707f3Ssthen 		if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
907933707f3Ssthen 			entry.data)->rrsig_count > 0)
908933707f3Ssthen 			return 1;
909933707f3Ssthen 	}
910933707f3Ssthen 	/* empty message has no DNSSEC info, with DNSSEC the reply is
911933707f3Ssthen 	 * not empty (NSEC) */
912933707f3Ssthen 	return 0;
913933707f3Ssthen }
914933707f3Ssthen 
915933707f3Ssthen int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
916933707f3Ssthen         enum response_type type, uint16_t dclass)
917933707f3Ssthen {
918933707f3Ssthen 	if(!msg || !dp || !msg->rep || !dp->name)
919933707f3Ssthen 		return 0;
920933707f3Ssthen 	/* SOA RRset - always from reply zone */
921933707f3Ssthen 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
922933707f3Ssthen 		LDNS_RR_TYPE_SOA, dclass) ||
923933707f3Ssthen 	   reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
924933707f3Ssthen 		LDNS_RR_TYPE_SOA, dclass))
925933707f3Ssthen 		return 1;
926933707f3Ssthen 	if(type == RESPONSE_TYPE_REFERRAL) {
927933707f3Ssthen 		size_t i;
928933707f3Ssthen 		/* if it adds a single label, i.e. we expect .com,
929933707f3Ssthen 		 * and referral to example.com. NS ... , then origin zone
930933707f3Ssthen 		 * is .com. For a referral to sub.example.com. NS ... then
931933707f3Ssthen 		 * we do not know, since example.com. may be in between. */
932933707f3Ssthen 		for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
933933707f3Ssthen 			i++) {
934933707f3Ssthen 			struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
935933707f3Ssthen 			if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
936933707f3Ssthen 				ntohs(s->rk.rrset_class) == dclass) {
937933707f3Ssthen 				int l = dname_count_labels(s->rk.dname);
938933707f3Ssthen 				if(l == dp->namelabs + 1 &&
939933707f3Ssthen 					dname_strict_subdomain(s->rk.dname,
940933707f3Ssthen 					l, dp->name, dp->namelabs))
941933707f3Ssthen 					return 1;
942933707f3Ssthen 			}
943933707f3Ssthen 		}
944933707f3Ssthen 		return 0;
945933707f3Ssthen 	}
946933707f3Ssthen 	log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
947933707f3Ssthen 	/* not a referral, and not lame delegation (upwards), so,
948933707f3Ssthen 	 * any NS rrset must be from the zone itself */
949933707f3Ssthen 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
950933707f3Ssthen 		LDNS_RR_TYPE_NS, dclass) ||
951933707f3Ssthen 	   reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
952933707f3Ssthen 		LDNS_RR_TYPE_NS, dclass))
953933707f3Ssthen 		return 1;
954933707f3Ssthen 	/* a DNSKEY set is expected at the zone apex as well */
955933707f3Ssthen 	/* this is for 'minimal responses' for DNSKEYs */
956933707f3Ssthen 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
957933707f3Ssthen 		LDNS_RR_TYPE_DNSKEY, dclass))
958933707f3Ssthen 		return 1;
959933707f3Ssthen 	return 0;
960933707f3Ssthen }
961933707f3Ssthen 
962933707f3Ssthen /**
963933707f3Ssthen  * check equality of two rrsets
964933707f3Ssthen  * @param k1: rrset
965933707f3Ssthen  * @param k2: rrset
966933707f3Ssthen  * @return true if equal
967933707f3Ssthen  */
968933707f3Ssthen static int
969933707f3Ssthen rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
970933707f3Ssthen {
971933707f3Ssthen 	struct packed_rrset_data* d1 = (struct packed_rrset_data*)
972933707f3Ssthen 		k1->entry.data;
973933707f3Ssthen 	struct packed_rrset_data* d2 = (struct packed_rrset_data*)
974933707f3Ssthen 		k2->entry.data;
975933707f3Ssthen 	size_t i, t;
976933707f3Ssthen 	if(k1->rk.dname_len != k2->rk.dname_len ||
977933707f3Ssthen 		k1->rk.flags != k2->rk.flags ||
978933707f3Ssthen 		k1->rk.type != k2->rk.type ||
979933707f3Ssthen 		k1->rk.rrset_class != k2->rk.rrset_class ||
980933707f3Ssthen 		query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
981933707f3Ssthen 		return 0;
98298f3ca02Sbrad 	if(	/* do not check ttl: d1->ttl != d2->ttl || */
983933707f3Ssthen 		d1->count != d2->count ||
984933707f3Ssthen 		d1->rrsig_count != d2->rrsig_count ||
985933707f3Ssthen 		d1->trust != d2->trust ||
986933707f3Ssthen 		d1->security != d2->security)
987933707f3Ssthen 		return 0;
988933707f3Ssthen 	t = d1->count + d1->rrsig_count;
989933707f3Ssthen 	for(i=0; i<t; i++) {
990933707f3Ssthen 		if(d1->rr_len[i] != d2->rr_len[i] ||
99198f3ca02Sbrad 			/* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/
992933707f3Ssthen 			memcmp(d1->rr_data[i], d2->rr_data[i],
993933707f3Ssthen 				d1->rr_len[i]) != 0)
994933707f3Ssthen 			return 0;
995933707f3Ssthen 	}
996933707f3Ssthen 	return 1;
997933707f3Ssthen }
998933707f3Ssthen 
999c3b38330Ssthen /** compare rrsets and sort canonically.  Compares rrset name, type, class.
1000c3b38330Ssthen  * return 0 if equal, +1 if x > y, and -1 if x < y.
1001c3b38330Ssthen  */
1002c3b38330Ssthen static int
1003c3b38330Ssthen rrset_canonical_sort_cmp(const void* x, const void* y)
1004c3b38330Ssthen {
1005c3b38330Ssthen 	struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x;
1006c3b38330Ssthen 	struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y;
1007c3b38330Ssthen 	int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname);
1008c3b38330Ssthen 	if(r != 0)
1009c3b38330Ssthen 		return r;
1010c3b38330Ssthen 	if(rrx->rk.type != rry->rk.type) {
1011c3b38330Ssthen 		if(ntohs(rrx->rk.type) > ntohs(rry->rk.type))
1012c3b38330Ssthen 			return 1;
1013c3b38330Ssthen 		else	return -1;
1014c3b38330Ssthen 	}
1015c3b38330Ssthen 	if(rrx->rk.rrset_class != rry->rk.rrset_class) {
1016c3b38330Ssthen 		if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class))
1017c3b38330Ssthen 			return 1;
1018c3b38330Ssthen 		else	return -1;
1019c3b38330Ssthen 	}
1020c3b38330Ssthen 	return 0;
1021c3b38330Ssthen }
1022c3b38330Ssthen 
1023933707f3Ssthen int
10245d76a658Ssthen reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region)
1025933707f3Ssthen {
1026933707f3Ssthen 	size_t i;
1027c3b38330Ssthen 	struct ub_packed_rrset_key** sorted_p, **sorted_q;
1028933707f3Ssthen 	if(p->flags != q->flags ||
1029933707f3Ssthen 		p->qdcount != q->qdcount ||
103098f3ca02Sbrad 		/* do not check TTL, this may differ */
103198f3ca02Sbrad 		/*
1032933707f3Ssthen 		p->ttl != q->ttl ||
1033933707f3Ssthen 		p->prefetch_ttl != q->prefetch_ttl ||
103498f3ca02Sbrad 		*/
1035933707f3Ssthen 		p->security != q->security ||
1036933707f3Ssthen 		p->an_numrrsets != q->an_numrrsets ||
1037933707f3Ssthen 		p->ns_numrrsets != q->ns_numrrsets ||
1038933707f3Ssthen 		p->ar_numrrsets != q->ar_numrrsets ||
1039933707f3Ssthen 		p->rrset_count != q->rrset_count)
1040933707f3Ssthen 		return 0;
1041c3b38330Ssthen 	/* sort the rrsets in the authority and additional sections before
1042c3b38330Ssthen 	 * compare, the query and answer sections are ordered in the sequence
1043c3b38330Ssthen 	 * they should have (eg. one after the other for aliases). */
1044c3b38330Ssthen 	sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init(
1045c3b38330Ssthen 		region, p->rrsets, sizeof(*sorted_p)*p->rrset_count);
1046c3b38330Ssthen 	if(!sorted_p) return 0;
1047c3b38330Ssthen 	log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <=
1048c3b38330Ssthen 		p->rrset_count);
1049c3b38330Ssthen 	qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets,
1050c3b38330Ssthen 		sizeof(*sorted_p), rrset_canonical_sort_cmp);
1051c3b38330Ssthen 	qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets,
1052c3b38330Ssthen 		sizeof(*sorted_p), rrset_canonical_sort_cmp);
1053c3b38330Ssthen 
1054c3b38330Ssthen 	sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init(
1055c3b38330Ssthen 		region, q->rrsets, sizeof(*sorted_q)*q->rrset_count);
1056c3b38330Ssthen 	if(!sorted_q) {
10575d76a658Ssthen 		regional_free_all(region);
1058933707f3Ssthen 		return 0;
1059933707f3Ssthen 	}
1060c3b38330Ssthen 	log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <=
1061c3b38330Ssthen 		q->rrset_count);
1062c3b38330Ssthen 	qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets,
1063c3b38330Ssthen 		sizeof(*sorted_q), rrset_canonical_sort_cmp);
1064c3b38330Ssthen 	qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets,
1065c3b38330Ssthen 		sizeof(*sorted_q), rrset_canonical_sort_cmp);
1066c3b38330Ssthen 
1067c3b38330Ssthen 	/* compare the rrsets */
1068c3b38330Ssthen 	for(i=0; i<p->rrset_count; i++) {
1069c3b38330Ssthen 		if(!rrset_equal(sorted_p[i], sorted_q[i])) {
1070c3b38330Ssthen 			if(!rrset_canonical_equal(region, sorted_p[i],
1071c3b38330Ssthen 				sorted_q[i])) {
10725d76a658Ssthen 				regional_free_all(region);
1073c3b38330Ssthen 				return 0;
1074933707f3Ssthen 			}
1075933707f3Ssthen 		}
1076c3b38330Ssthen 	}
1077c3b38330Ssthen 	regional_free_all(region);
1078933707f3Ssthen 	return 1;
1079933707f3Ssthen }
1080933707f3Ssthen 
1081933707f3Ssthen void
108235fa0a9cSbrad caps_strip_reply(struct reply_info* rep)
108335fa0a9cSbrad {
108435fa0a9cSbrad 	size_t i;
108535fa0a9cSbrad 	if(!rep) return;
108635fa0a9cSbrad 	/* see if message is a referral, in which case the additional and
108735fa0a9cSbrad 	 * NS record cannot be removed */
108835fa0a9cSbrad 	/* referrals have the AA flag unset (strict check, not elsewhere in
108935fa0a9cSbrad 	 * unbound, but for 0x20 this is very convenient). */
109035fa0a9cSbrad 	if(!(rep->flags&BIT_AA))
109135fa0a9cSbrad 		return;
109235fa0a9cSbrad 	/* remove the additional section from the reply */
109335fa0a9cSbrad 	if(rep->ar_numrrsets != 0) {
109435fa0a9cSbrad 		verbose(VERB_ALGO, "caps fallback: removing additional section");
109535fa0a9cSbrad 		rep->rrset_count -= rep->ar_numrrsets;
109635fa0a9cSbrad 		rep->ar_numrrsets = 0;
109735fa0a9cSbrad 	}
109835fa0a9cSbrad 	/* is there an NS set in the authority section to remove? */
109935fa0a9cSbrad 	/* the failure case (Cisco firewalls) only has one rrset in authsec */
110035fa0a9cSbrad 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
110135fa0a9cSbrad 		struct ub_packed_rrset_key* s = rep->rrsets[i];
110235fa0a9cSbrad 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) {
110335fa0a9cSbrad 			/* remove NS rrset and break from loop (loop limits
110435fa0a9cSbrad 			 * have changed) */
110535fa0a9cSbrad 			/* move last rrset into this position (there is no
110635fa0a9cSbrad 			 * additional section any more) */
110735fa0a9cSbrad 			verbose(VERB_ALGO, "caps fallback: removing NS rrset");
110835fa0a9cSbrad 			if(i < rep->rrset_count-1)
110935fa0a9cSbrad 				rep->rrsets[i]=rep->rrsets[rep->rrset_count-1];
111035fa0a9cSbrad 			rep->rrset_count --;
111135fa0a9cSbrad 			rep->ns_numrrsets --;
111235fa0a9cSbrad 			break;
111335fa0a9cSbrad 		}
111435fa0a9cSbrad 	}
111535fa0a9cSbrad }
111635fa0a9cSbrad 
1117a58bff56Ssthen int caps_failed_rcode(struct reply_info* rep)
1118a58bff56Ssthen {
1119a58bff56Ssthen 	return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
1120a58bff56Ssthen 		FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN);
1121a58bff56Ssthen }
1122a58bff56Ssthen 
112335fa0a9cSbrad void
1124933707f3Ssthen iter_store_parentside_rrset(struct module_env* env,
1125933707f3Ssthen 	struct ub_packed_rrset_key* rrset)
1126933707f3Ssthen {
1127933707f3Ssthen 	struct rrset_ref ref;
1128933707f3Ssthen 	rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
1129933707f3Ssthen 	if(!rrset) {
1130933707f3Ssthen 		log_err("malloc failure in store_parentside_rrset");
1131933707f3Ssthen 		return;
1132933707f3Ssthen 	}
1133933707f3Ssthen 	rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
1134933707f3Ssthen 	rrset->entry.hash = rrset_key_hash(&rrset->rk);
1135933707f3Ssthen 	ref.key = rrset;
1136933707f3Ssthen 	ref.id = rrset->id;
1137933707f3Ssthen 	/* ignore ret: if it was in the cache, ref updated */
1138933707f3Ssthen 	(void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
1139933707f3Ssthen }
1140933707f3Ssthen 
1141933707f3Ssthen /** fetch NS record from reply, if any */
1142933707f3Ssthen static struct ub_packed_rrset_key*
1143933707f3Ssthen reply_get_NS_rrset(struct reply_info* rep)
1144933707f3Ssthen {
1145933707f3Ssthen 	size_t i;
1146933707f3Ssthen 	for(i=0; i<rep->rrset_count; i++) {
1147933707f3Ssthen 		if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
1148933707f3Ssthen 			return rep->rrsets[i];
1149933707f3Ssthen 		}
1150933707f3Ssthen 	}
1151933707f3Ssthen 	return NULL;
1152933707f3Ssthen }
1153933707f3Ssthen 
1154933707f3Ssthen void
1155933707f3Ssthen iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
1156933707f3Ssthen {
1157933707f3Ssthen 	struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1158933707f3Ssthen 	if(rrset) {
1159933707f3Ssthen 		log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
1160933707f3Ssthen 		iter_store_parentside_rrset(env, rrset);
1161933707f3Ssthen 	}
1162933707f3Ssthen }
1163933707f3Ssthen 
1164933707f3Ssthen void iter_store_parentside_neg(struct module_env* env,
1165933707f3Ssthen         struct query_info* qinfo, struct reply_info* rep)
1166933707f3Ssthen {
1167933707f3Ssthen 	/* TTL: NS from referral in iq->deleg_msg,
1168933707f3Ssthen 	 *      or first RR from iq->response,
1169933707f3Ssthen 	 *      or servfail5secs if !iq->response */
1170229e174cSsthen 	time_t ttl = NORR_TTL;
1171933707f3Ssthen 	struct ub_packed_rrset_key* neg;
1172933707f3Ssthen 	struct packed_rrset_data* newd;
1173933707f3Ssthen 	if(rep) {
1174933707f3Ssthen 		struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1175933707f3Ssthen 		if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
1176933707f3Ssthen 		if(rrset) ttl = ub_packed_rrset_ttl(rrset);
1177933707f3Ssthen 	}
1178933707f3Ssthen 	/* create empty rrset to store */
1179933707f3Ssthen 	neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
1180933707f3Ssthen 	                sizeof(struct ub_packed_rrset_key));
1181933707f3Ssthen 	if(!neg) {
1182933707f3Ssthen 		log_err("out of memory in store_parentside_neg");
1183933707f3Ssthen 		return;
1184933707f3Ssthen 	}
1185933707f3Ssthen 	memset(&neg->entry, 0, sizeof(neg->entry));
1186933707f3Ssthen 	neg->entry.key = neg;
1187933707f3Ssthen 	neg->rk.type = htons(qinfo->qtype);
1188933707f3Ssthen 	neg->rk.rrset_class = htons(qinfo->qclass);
1189933707f3Ssthen 	neg->rk.flags = 0;
1190933707f3Ssthen 	neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
1191933707f3Ssthen 		qinfo->qname_len);
1192933707f3Ssthen 	if(!neg->rk.dname) {
1193933707f3Ssthen 		log_err("out of memory in store_parentside_neg");
1194933707f3Ssthen 		return;
1195933707f3Ssthen 	}
1196933707f3Ssthen 	neg->rk.dname_len = qinfo->qname_len;
1197933707f3Ssthen 	neg->entry.hash = rrset_key_hash(&neg->rk);
1198933707f3Ssthen 	newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
1199933707f3Ssthen 		sizeof(struct packed_rrset_data) + sizeof(size_t) +
1200229e174cSsthen 		sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t));
1201933707f3Ssthen 	if(!newd) {
1202933707f3Ssthen 		log_err("out of memory in store_parentside_neg");
1203933707f3Ssthen 		return;
1204933707f3Ssthen 	}
1205933707f3Ssthen 	neg->entry.data = newd;
1206933707f3Ssthen 	newd->ttl = ttl;
1207933707f3Ssthen 	/* entry must have one RR, otherwise not valid in cache.
1208933707f3Ssthen 	 * put in one RR with empty rdata: those are ignored as nameserver */
1209933707f3Ssthen 	newd->count = 1;
1210933707f3Ssthen 	newd->rrsig_count = 0;
1211933707f3Ssthen 	newd->trust = rrset_trust_ans_noAA;
1212933707f3Ssthen 	newd->rr_len = (size_t*)((uint8_t*)newd +
1213933707f3Ssthen 		sizeof(struct packed_rrset_data));
1214933707f3Ssthen 	newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
1215933707f3Ssthen 	packed_rrset_ptr_fixup(newd);
1216933707f3Ssthen 	newd->rr_ttl[0] = newd->ttl;
12175d76a658Ssthen 	sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
1218933707f3Ssthen 	/* store it */
1219933707f3Ssthen 	log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
1220933707f3Ssthen 	iter_store_parentside_rrset(env, neg);
1221933707f3Ssthen }
1222933707f3Ssthen 
1223933707f3Ssthen int
1224933707f3Ssthen iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
1225933707f3Ssthen 	struct regional* region, struct query_info* qinfo)
1226933707f3Ssthen {
1227933707f3Ssthen 	struct ub_packed_rrset_key* akey;
1228933707f3Ssthen 	akey = rrset_cache_lookup(env->rrset_cache, dp->name,
1229933707f3Ssthen 		dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
1230933707f3Ssthen 		PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1231933707f3Ssthen 	if(akey) {
1232933707f3Ssthen 		log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
1233933707f3Ssthen 		dp->has_parent_side_NS = 1;
1234933707f3Ssthen 		/* and mark the new names as lame */
1235933707f3Ssthen 		if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
1236933707f3Ssthen 			lock_rw_unlock(&akey->entry.lock);
1237933707f3Ssthen 			return 0;
1238933707f3Ssthen 		}
1239933707f3Ssthen 		lock_rw_unlock(&akey->entry.lock);
1240933707f3Ssthen 	}
1241933707f3Ssthen 	return 1;
1242933707f3Ssthen }
1243933707f3Ssthen 
1244933707f3Ssthen int iter_lookup_parent_glue_from_cache(struct module_env* env,
1245933707f3Ssthen         struct delegpt* dp, struct regional* region, struct query_info* qinfo)
1246933707f3Ssthen {
1247933707f3Ssthen 	struct ub_packed_rrset_key* akey;
1248933707f3Ssthen 	struct delegpt_ns* ns;
1249933707f3Ssthen 	size_t num = delegpt_count_targets(dp);
1250933707f3Ssthen 	for(ns = dp->nslist; ns; ns = ns->next) {
12517dd170e2Ssthen 		if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE)
12527dd170e2Ssthen 			continue;
12537dd170e2Ssthen 		ns->cache_lookup_count++;
1254933707f3Ssthen 		/* get cached parentside A */
1255933707f3Ssthen 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1256933707f3Ssthen 			ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
1257933707f3Ssthen 			PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1258933707f3Ssthen 		if(akey) {
1259933707f3Ssthen 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
1260933707f3Ssthen 			ns->done_pside4 = 1;
1261933707f3Ssthen 			/* a negative-cache-element has no addresses it adds */
126206a13c09Ssthen 			if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL))
1263933707f3Ssthen 				log_err("malloc failure in lookup_parent_glue");
1264933707f3Ssthen 			lock_rw_unlock(&akey->entry.lock);
1265933707f3Ssthen 		}
1266933707f3Ssthen 		/* get cached parentside AAAA */
1267933707f3Ssthen 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1268933707f3Ssthen 			ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
1269933707f3Ssthen 			PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1270933707f3Ssthen 		if(akey) {
1271933707f3Ssthen 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
1272933707f3Ssthen 			ns->done_pside6 = 1;
1273933707f3Ssthen 			/* a negative-cache-element has no addresses it adds */
127406a13c09Ssthen 			if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL))
1275933707f3Ssthen 				log_err("malloc failure in lookup_parent_glue");
1276933707f3Ssthen 			lock_rw_unlock(&akey->entry.lock);
1277933707f3Ssthen 		}
1278933707f3Ssthen 	}
1279933707f3Ssthen 	/* see if new (but lame) addresses have become available */
1280933707f3Ssthen 	return delegpt_count_targets(dp) != num;
1281933707f3Ssthen }
1282933707f3Ssthen 
1283933707f3Ssthen int
1284933707f3Ssthen iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
1285933707f3Ssthen 	uint16_t* c)
1286933707f3Ssthen {
1287933707f3Ssthen 	uint16_t c1 = *c, c2 = *c;
12882bdc0ed1Ssthen 	int r1, r2;
12892bdc0ed1Ssthen 	int nolock = 1;
12902bdc0ed1Ssthen 
12912bdc0ed1Ssthen 	/* prelock both forwards and hints for atomic read. */
12922bdc0ed1Ssthen 	lock_rw_rdlock(&fwd->lock);
12932bdc0ed1Ssthen 	lock_rw_rdlock(&hints->lock);
12942bdc0ed1Ssthen 	r1 = hints_next_root(hints, &c1, nolock);
12952bdc0ed1Ssthen 	r2 = forwards_next_root(fwd, &c2, nolock);
12962bdc0ed1Ssthen 	lock_rw_unlock(&fwd->lock);
12972bdc0ed1Ssthen 	lock_rw_unlock(&hints->lock);
12982bdc0ed1Ssthen 
1299933707f3Ssthen 	if(!r1 && !r2) /* got none, end of list */
1300933707f3Ssthen 		return 0;
1301933707f3Ssthen 	else if(!r1) /* got one, return that */
1302933707f3Ssthen 		*c = c2;
1303933707f3Ssthen 	else if(!r2)
1304933707f3Ssthen 		*c = c1;
1305933707f3Ssthen 	else if(c1 < c2) /* got both take smallest */
1306933707f3Ssthen 		*c = c1;
1307933707f3Ssthen 	else	*c = c2;
1308933707f3Ssthen 	return 1;
1309933707f3Ssthen }
1310933707f3Ssthen 
1311933707f3Ssthen void
1312933707f3Ssthen iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
1313933707f3Ssthen {
1314933707f3Ssthen 	/* Only the DS record for the delegation itself is expected.
1315933707f3Ssthen 	 * We allow DS for everything between the bailiwick and the
1316933707f3Ssthen 	 * zonecut, thus DS records must be at or above the zonecut.
1317933707f3Ssthen 	 * And the DS records must be below the server authority zone.
1318933707f3Ssthen 	 * The answer section is already scrubbed. */
1319933707f3Ssthen 	size_t i = msg->rep->an_numrrsets;
1320933707f3Ssthen 	while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
1321933707f3Ssthen 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1322933707f3Ssthen 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
1323933707f3Ssthen 			(!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
1324933707f3Ssthen 			|| query_dname_compare(z, s->rk.dname) == 0)) {
1325933707f3Ssthen 			log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
1326933707f3Ssthen 				s->rk.dname, ntohs(s->rk.type),
1327933707f3Ssthen 				ntohs(s->rk.rrset_class));
1328933707f3Ssthen 			memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
1329933707f3Ssthen 				sizeof(struct ub_packed_rrset_key*) *
1330933707f3Ssthen 				(msg->rep->rrset_count-i-1));
1331933707f3Ssthen 			msg->rep->ns_numrrsets--;
1332933707f3Ssthen 			msg->rep->rrset_count--;
1333933707f3Ssthen 			/* stay at same i, but new record */
1334933707f3Ssthen 			continue;
1335933707f3Ssthen 		}
1336933707f3Ssthen 		i++;
1337933707f3Ssthen 	}
1338933707f3Ssthen }
1339933707f3Ssthen 
1340550cf4a9Ssthen void
1341550cf4a9Ssthen iter_scrub_nxdomain(struct dns_msg* msg)
1342550cf4a9Ssthen {
1343550cf4a9Ssthen 	if(msg->rep->an_numrrsets == 0)
1344550cf4a9Ssthen 		return;
1345550cf4a9Ssthen 
1346550cf4a9Ssthen 	memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets,
1347550cf4a9Ssthen 		sizeof(struct ub_packed_rrset_key*) *
1348550cf4a9Ssthen 		(msg->rep->rrset_count-msg->rep->an_numrrsets));
1349550cf4a9Ssthen 	msg->rep->rrset_count -= msg->rep->an_numrrsets;
1350550cf4a9Ssthen 	msg->rep->an_numrrsets = 0;
1351550cf4a9Ssthen }
1352550cf4a9Ssthen 
1353e21c60efSsthen void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry)
1354933707f3Ssthen {
1355933707f3Ssthen 	struct delegpt_addr* a;
1356933707f3Ssthen 	for(a=dp->target_list; a; a = a->next_target) {
1357e21c60efSsthen 		if(a->attempts >= outbound_msg_retry) {
1358933707f3Ssthen 			/* add back to result list */
13598b7325afSsthen 			delegpt_add_to_result_list(dp, a);
1360933707f3Ssthen 		}
1361933707f3Ssthen 		if(a->attempts > d)
1362933707f3Ssthen 			a->attempts -= d;
1363933707f3Ssthen 		else a->attempts = 0;
1364933707f3Ssthen 	}
1365933707f3Ssthen }
1366933707f3Ssthen 
1367e21c60efSsthen void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old,
1368e21c60efSsthen 	int outbound_msg_retry)
1369933707f3Ssthen {
1370933707f3Ssthen 	struct delegpt_addr* a, *o, *prev;
1371933707f3Ssthen 	for(a=dp->target_list; a; a = a->next_target) {
1372933707f3Ssthen 		o = delegpt_find_addr(old, &a->addr, a->addrlen);
1373933707f3Ssthen 		if(o) {
1374933707f3Ssthen 			log_addr(VERB_ALGO, "copy attempt count previous dp",
1375933707f3Ssthen 				&a->addr, a->addrlen);
1376933707f3Ssthen 			a->attempts = o->attempts;
1377933707f3Ssthen 		}
1378933707f3Ssthen 	}
1379933707f3Ssthen 	prev = NULL;
1380933707f3Ssthen 	a = dp->usable_list;
1381933707f3Ssthen 	while(a) {
1382e21c60efSsthen 		if(a->attempts >= outbound_msg_retry) {
1383933707f3Ssthen 			log_addr(VERB_ALGO, "remove from usable list dp",
1384933707f3Ssthen 				&a->addr, a->addrlen);
1385933707f3Ssthen 			/* remove from result list */
1386933707f3Ssthen 			if(prev)
1387933707f3Ssthen 				prev->next_usable = a->next_usable;
1388933707f3Ssthen 			else	dp->usable_list = a->next_usable;
1389933707f3Ssthen 			/* prev stays the same */
1390933707f3Ssthen 			a = a->next_usable;
1391933707f3Ssthen 			continue;
1392933707f3Ssthen 		}
1393933707f3Ssthen 		prev = a;
1394933707f3Ssthen 		a = a->next_usable;
1395933707f3Ssthen 	}
1396933707f3Ssthen }
1397d8d14d0cSsthen 
1398d8d14d0cSsthen int
1399d8d14d0cSsthen iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
1400d8d14d0cSsthen {
1401d8d14d0cSsthen 	/* if for query example.com, there is example.com SOA or a subdomain
1402d8d14d0cSsthen 	 * of example.com, then we are too low and need to fetch NS. */
1403d8d14d0cSsthen 	size_t i;
1404d8d14d0cSsthen 	/* if we have a DNAME or CNAME we are probably wrong */
1405d8d14d0cSsthen 	/* if we have a qtype DS in the answer section, its fine */
1406d8d14d0cSsthen 	for(i=0; i < msg->rep->an_numrrsets; i++) {
1407d8d14d0cSsthen 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1408d8d14d0cSsthen 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
1409d8d14d0cSsthen 			ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
1410d8d14d0cSsthen 			/* not the right answer, maybe too low, check the
1411d8d14d0cSsthen 			 * RRSIG signer name (if there is any) for a hint
1412d8d14d0cSsthen 			 * that it is from the dp zone anyway */
1413d8d14d0cSsthen 			uint8_t* sname;
1414d8d14d0cSsthen 			size_t slen;
1415d8d14d0cSsthen 			val_find_rrset_signer(s, &sname, &slen);
1416d8d14d0cSsthen 			if(sname && query_dname_compare(dp->name, sname)==0)
1417d8d14d0cSsthen 				return 0; /* it is fine, from the right dp */
1418d8d14d0cSsthen 			return 1;
1419d8d14d0cSsthen 		}
1420d8d14d0cSsthen 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
1421d8d14d0cSsthen 			return 0; /* fine, we have a DS record */
1422d8d14d0cSsthen 	}
1423d8d14d0cSsthen 	for(i=msg->rep->an_numrrsets;
1424d8d14d0cSsthen 		i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
1425d8d14d0cSsthen 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1426d8d14d0cSsthen 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1427d8d14d0cSsthen 			if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
1428d8d14d0cSsthen 				return 1; /* point is too low */
1429d8d14d0cSsthen 			if(query_dname_compare(s->rk.dname, dp->name)==0)
1430d8d14d0cSsthen 				return 0; /* right dp */
1431d8d14d0cSsthen 		}
1432d8d14d0cSsthen 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1433d8d14d0cSsthen 			ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1434d8d14d0cSsthen 			uint8_t* sname;
1435d8d14d0cSsthen 			size_t slen;
1436d8d14d0cSsthen 			val_find_rrset_signer(s, &sname, &slen);
1437d8d14d0cSsthen 			if(sname && query_dname_compare(dp->name, sname)==0)
1438d8d14d0cSsthen 				return 0; /* it is fine, from the right dp */
1439d8d14d0cSsthen 			return 1;
1440d8d14d0cSsthen 		}
1441d8d14d0cSsthen 	}
1442d8d14d0cSsthen 	/* we do not know */
1443d8d14d0cSsthen 	return 1;
1444d8d14d0cSsthen }
1445d8d14d0cSsthen 
1446d8d14d0cSsthen int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
1447d8d14d0cSsthen {
1448d8d14d0cSsthen 	/* no delegation point, do not see how we can go down,
1449d8d14d0cSsthen 	 * robust check, it should really exist */
1450d8d14d0cSsthen 	if(!dp) return 0;
1451d8d14d0cSsthen 
1452d8d14d0cSsthen 	/* see if dp equals the qname, then we cannot go down further */
1453d8d14d0cSsthen 	if(query_dname_compare(qinfo->qname, dp->name) == 0)
1454d8d14d0cSsthen 		return 0;
1455d8d14d0cSsthen 	/* if dp is one label above the name we also cannot go down further */
1456d8d14d0cSsthen 	if(dname_count_labels(qinfo->qname) == dp->namelabs+1)
1457d8d14d0cSsthen 		return 0;
1458d8d14d0cSsthen 	return 1;
1459d8d14d0cSsthen }
14603150e5f6Ssthen 
14613150e5f6Ssthen int
1462191f22c6Ssthen iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf,
14632bdc0ed1Ssthen 	uint8_t** retdpname, size_t* retdpnamelen, uint8_t* dpname_storage,
14642bdc0ed1Ssthen 	size_t dpname_storage_len)
14653150e5f6Ssthen {
14663150e5f6Ssthen 	struct iter_hints_stub *stub;
14673150e5f6Ssthen 	struct delegpt *dp;
14682bdc0ed1Ssthen 	int nolock = 1;
14693150e5f6Ssthen 
14703150e5f6Ssthen 	/* Check for stub. */
14712bdc0ed1Ssthen 	/* Lock both forwards and hints for atomic read. */
14722bdc0ed1Ssthen 	lock_rw_rdlock(&qstate->env->fwds->lock);
14732bdc0ed1Ssthen 	lock_rw_rdlock(&qstate->env->hints->lock);
14743150e5f6Ssthen 	stub = hints_lookup_stub(qstate->env->hints, qinf->qname,
14752bdc0ed1Ssthen 	    qinf->qclass, NULL, nolock);
14762bdc0ed1Ssthen 	dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass,
14772bdc0ed1Ssthen 		nolock);
14783150e5f6Ssthen 
14793150e5f6Ssthen 	/* see if forward or stub is more pertinent */
14803150e5f6Ssthen 	if(stub && stub->dp && dp) {
14813150e5f6Ssthen 		if(dname_strict_subdomain(dp->name, dp->namelabs,
14823150e5f6Ssthen 			stub->dp->name, stub->dp->namelabs)) {
14833150e5f6Ssthen 			stub = NULL; /* ignore stub, forward is lower */
14843150e5f6Ssthen 		} else {
14853150e5f6Ssthen 			dp = NULL; /* ignore forward, stub is lower */
14863150e5f6Ssthen 		}
14873150e5f6Ssthen 	}
14883150e5f6Ssthen 
14893150e5f6Ssthen 	/* check stub */
14903150e5f6Ssthen 	if (stub != NULL && stub->dp != NULL) {
14912bdc0ed1Ssthen 		int stub_no_cache = stub->dp->no_cache;
14922bdc0ed1Ssthen 		lock_rw_unlock(&qstate->env->fwds->lock);
14932bdc0ed1Ssthen 		if(stub_no_cache) {
14943150e5f6Ssthen 			char qname[255+1];
14953150e5f6Ssthen 			char dpname[255+1];
14963150e5f6Ssthen 			dname_str(qinf->qname, qname);
14973150e5f6Ssthen 			dname_str(stub->dp->name, dpname);
14983150e5f6Ssthen 			verbose(VERB_ALGO, "stub for %s %s has no_cache", qname, dpname);
14993150e5f6Ssthen 		}
1500191f22c6Ssthen 		if(retdpname) {
15012bdc0ed1Ssthen 			if(stub->dp->namelen > dpname_storage_len) {
15022bdc0ed1Ssthen 				verbose(VERB_ALGO, "no cache stub dpname too long");
15032bdc0ed1Ssthen 				lock_rw_unlock(&qstate->env->hints->lock);
15042bdc0ed1Ssthen 				*retdpname = NULL;
15052bdc0ed1Ssthen 				*retdpnamelen = 0;
15062bdc0ed1Ssthen 				return stub_no_cache;
15072bdc0ed1Ssthen 			}
15082bdc0ed1Ssthen 			memmove(dpname_storage, stub->dp->name,
15092bdc0ed1Ssthen 				stub->dp->namelen);
15102bdc0ed1Ssthen 			*retdpname = dpname_storage;
1511191f22c6Ssthen 			*retdpnamelen = stub->dp->namelen;
1512191f22c6Ssthen 		}
15132bdc0ed1Ssthen 		lock_rw_unlock(&qstate->env->hints->lock);
15142bdc0ed1Ssthen 		return stub_no_cache;
15153150e5f6Ssthen 	}
15163150e5f6Ssthen 
15173150e5f6Ssthen 	/* Check for forward. */
15183150e5f6Ssthen 	if (dp) {
15192bdc0ed1Ssthen 		int dp_no_cache = dp->no_cache;
15202bdc0ed1Ssthen 		lock_rw_unlock(&qstate->env->hints->lock);
15212bdc0ed1Ssthen 		if(dp_no_cache) {
15223150e5f6Ssthen 			char qname[255+1];
15233150e5f6Ssthen 			char dpname[255+1];
15243150e5f6Ssthen 			dname_str(qinf->qname, qname);
15253150e5f6Ssthen 			dname_str(dp->name, dpname);
15263150e5f6Ssthen 			verbose(VERB_ALGO, "forward for %s %s has no_cache", qname, dpname);
15273150e5f6Ssthen 		}
1528191f22c6Ssthen 		if(retdpname) {
15292bdc0ed1Ssthen 			if(dp->namelen > dpname_storage_len) {
15302bdc0ed1Ssthen 				verbose(VERB_ALGO, "no cache dpname too long");
15312bdc0ed1Ssthen 				lock_rw_unlock(&qstate->env->fwds->lock);
15322bdc0ed1Ssthen 				*retdpname = NULL;
15332bdc0ed1Ssthen 				*retdpnamelen = 0;
15342bdc0ed1Ssthen 				return dp_no_cache;
15352bdc0ed1Ssthen 			}
15362bdc0ed1Ssthen 			memmove(dpname_storage, dp->name, dp->namelen);
15372bdc0ed1Ssthen 			*retdpname = dpname_storage;
1538191f22c6Ssthen 			*retdpnamelen = dp->namelen;
1539191f22c6Ssthen 		}
15402bdc0ed1Ssthen 		lock_rw_unlock(&qstate->env->fwds->lock);
15412bdc0ed1Ssthen 		return dp_no_cache;
15423150e5f6Ssthen 	}
15432bdc0ed1Ssthen 	lock_rw_unlock(&qstate->env->fwds->lock);
15442bdc0ed1Ssthen 	lock_rw_unlock(&qstate->env->hints->lock);
1545191f22c6Ssthen 	if(retdpname) {
1546191f22c6Ssthen 		*retdpname = NULL;
1547191f22c6Ssthen 		*retdpnamelen = 0;
1548191f22c6Ssthen 	}
15493150e5f6Ssthen 	return 0;
15503150e5f6Ssthen }
1551191f22c6Ssthen 
1552191f22c6Ssthen void iterator_set_ip46_support(struct module_stack* mods,
1553191f22c6Ssthen 	struct module_env* env, struct outside_network* outnet)
1554191f22c6Ssthen {
1555191f22c6Ssthen 	int m = modstack_find(mods, "iterator");
1556191f22c6Ssthen 	struct iter_env* ie = NULL;
1557191f22c6Ssthen 	if(m == -1)
1558191f22c6Ssthen 		return;
1559191f22c6Ssthen 	ie = (struct iter_env*)env->modinfo[m];
1560191f22c6Ssthen 	if(outnet->pending == NULL)
1561191f22c6Ssthen 		return; /* we are in testbound, no rbtree for UDP */
1562191f22c6Ssthen 	if(outnet->num_ip4 == 0)
1563191f22c6Ssthen 		ie->supports_ipv4 = 0;
1564191f22c6Ssthen 	if(outnet->num_ip6 == 0)
1565191f22c6Ssthen 		ie->supports_ipv6 = 0;
1566191f22c6Ssthen }
1567