xref: /openbsd-src/sbin/unwind/libunbound/iterator/iter_utils.c (revision 7037e34cdfd270b3989fb1829c7cd3439048bd3a)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * iterator/iter_utils.c - iterative resolver module utility functions.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file contains functions to assist the iterator module.
40ae8c6e27Sflorian  * Configuration options. Forward zones.
41ae8c6e27Sflorian  */
42ae8c6e27Sflorian #include "config.h"
43ae8c6e27Sflorian #include "iterator/iter_utils.h"
44ae8c6e27Sflorian #include "iterator/iterator.h"
45ae8c6e27Sflorian #include "iterator/iter_hints.h"
46ae8c6e27Sflorian #include "iterator/iter_fwd.h"
47ae8c6e27Sflorian #include "iterator/iter_donotq.h"
48ae8c6e27Sflorian #include "iterator/iter_delegpt.h"
49ae8c6e27Sflorian #include "iterator/iter_priv.h"
50ae8c6e27Sflorian #include "services/cache/infra.h"
51ae8c6e27Sflorian #include "services/cache/dns.h"
52ae8c6e27Sflorian #include "services/cache/rrset.h"
53411c5950Sflorian #include "services/outside_network.h"
54ae8c6e27Sflorian #include "util/net_help.h"
55ae8c6e27Sflorian #include "util/module.h"
56ae8c6e27Sflorian #include "util/log.h"
57ae8c6e27Sflorian #include "util/config_file.h"
58ae8c6e27Sflorian #include "util/regional.h"
59ae8c6e27Sflorian #include "util/data/msgparse.h"
60ae8c6e27Sflorian #include "util/data/dname.h"
61ae8c6e27Sflorian #include "util/random.h"
62ae8c6e27Sflorian #include "util/fptr_wlist.h"
63ae8c6e27Sflorian #include "validator/val_anchor.h"
64ae8c6e27Sflorian #include "validator/val_kcache.h"
65ae8c6e27Sflorian #include "validator/val_kentry.h"
66ae8c6e27Sflorian #include "validator/val_utils.h"
67ae8c6e27Sflorian #include "validator/val_sigcrypt.h"
68ae8c6e27Sflorian #include "sldns/sbuffer.h"
69ae8c6e27Sflorian #include "sldns/str2wire.h"
70ae8c6e27Sflorian 
71ae8c6e27Sflorian /** time when nameserver glue is said to be 'recent' */
72ae8c6e27Sflorian #define SUSPICION_RECENT_EXPIRY 86400
73ae8c6e27Sflorian 
74d500c338Sflorian /** if NAT64 is enabled and no NAT64 prefix is configured, first fall back to
75d500c338Sflorian  * DNS64 prefix.  If that is not configured, fall back to this default value.
76d500c338Sflorian  */
77d500c338Sflorian static const char DEFAULT_NAT64_PREFIX[] = "64:ff9b::/96";
78d500c338Sflorian 
79ae8c6e27Sflorian /** fillup fetch policy array */
80ae8c6e27Sflorian static void
81ae8c6e27Sflorian fetch_fill(struct iter_env* ie, const char* str)
82ae8c6e27Sflorian {
83ae8c6e27Sflorian 	char* s = (char*)str, *e;
84ae8c6e27Sflorian 	int i;
85ae8c6e27Sflorian 	for(i=0; i<ie->max_dependency_depth+1; i++) {
86ae8c6e27Sflorian 		ie->target_fetch_policy[i] = strtol(s, &e, 10);
87ae8c6e27Sflorian 		if(s == e)
88ae8c6e27Sflorian 			fatal_exit("cannot parse fetch policy number %s", s);
89ae8c6e27Sflorian 		s = e;
90ae8c6e27Sflorian 	}
91ae8c6e27Sflorian }
92ae8c6e27Sflorian 
93ae8c6e27Sflorian /** Read config string that represents the target fetch policy */
94ae8c6e27Sflorian static int
95ae8c6e27Sflorian read_fetch_policy(struct iter_env* ie, const char* str)
96ae8c6e27Sflorian {
97ae8c6e27Sflorian 	int count = cfg_count_numbers(str);
98ae8c6e27Sflorian 	if(count < 1) {
99ae8c6e27Sflorian 		log_err("Cannot parse target fetch policy: \"%s\"", str);
100ae8c6e27Sflorian 		return 0;
101ae8c6e27Sflorian 	}
102ae8c6e27Sflorian 	ie->max_dependency_depth = count - 1;
103ae8c6e27Sflorian 	ie->target_fetch_policy = (int*)calloc(
104ae8c6e27Sflorian 		(size_t)ie->max_dependency_depth+1, sizeof(int));
105ae8c6e27Sflorian 	if(!ie->target_fetch_policy) {
106ae8c6e27Sflorian 		log_err("alloc fetch policy: out of memory");
107ae8c6e27Sflorian 		return 0;
108ae8c6e27Sflorian 	}
109ae8c6e27Sflorian 	fetch_fill(ie, str);
110ae8c6e27Sflorian 	return 1;
111ae8c6e27Sflorian }
112ae8c6e27Sflorian 
113ae8c6e27Sflorian /** apply config caps whitelist items to name tree */
114ae8c6e27Sflorian static int
115ae8c6e27Sflorian caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg)
116ae8c6e27Sflorian {
117ae8c6e27Sflorian 	struct config_strlist* p;
118ae8c6e27Sflorian 	for(p=cfg->caps_whitelist; p; p=p->next) {
119ae8c6e27Sflorian 		struct name_tree_node* n;
120ae8c6e27Sflorian 		size_t len;
121ae8c6e27Sflorian 		uint8_t* nm = sldns_str2wire_dname(p->str, &len);
122ae8c6e27Sflorian 		if(!nm) {
123ae8c6e27Sflorian 			log_err("could not parse %s", p->str);
124ae8c6e27Sflorian 			return 0;
125ae8c6e27Sflorian 		}
126ae8c6e27Sflorian 		n = (struct name_tree_node*)calloc(1, sizeof(*n));
127ae8c6e27Sflorian 		if(!n) {
128ae8c6e27Sflorian 			log_err("out of memory");
129ae8c6e27Sflorian 			free(nm);
130ae8c6e27Sflorian 			return 0;
131ae8c6e27Sflorian 		}
132ae8c6e27Sflorian 		n->node.key = n;
133ae8c6e27Sflorian 		n->name = nm;
134ae8c6e27Sflorian 		n->len = len;
135ae8c6e27Sflorian 		n->labs = dname_count_labels(nm);
136ae8c6e27Sflorian 		n->dclass = LDNS_RR_CLASS_IN;
137ae8c6e27Sflorian 		if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) {
138ae8c6e27Sflorian 			/* duplicate element ignored, idempotent */
139ae8c6e27Sflorian 			free(n->name);
140ae8c6e27Sflorian 			free(n);
141ae8c6e27Sflorian 		}
142ae8c6e27Sflorian 	}
143ae8c6e27Sflorian 	name_tree_init_parents(ntree);
144ae8c6e27Sflorian 	return 1;
145ae8c6e27Sflorian }
146ae8c6e27Sflorian 
147ae8c6e27Sflorian int
148ae8c6e27Sflorian iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
149ae8c6e27Sflorian {
150d500c338Sflorian 	const char *nat64_prefix;
151ae8c6e27Sflorian 	int i;
152ae8c6e27Sflorian 	/* target fetch policy */
153ae8c6e27Sflorian 	if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
154ae8c6e27Sflorian 		return 0;
155ae8c6e27Sflorian 	for(i=0; i<iter_env->max_dependency_depth+1; i++)
156ae8c6e27Sflorian 		verbose(VERB_QUERY, "target fetch policy for level %d is %d",
157ae8c6e27Sflorian 			i, iter_env->target_fetch_policy[i]);
158ae8c6e27Sflorian 
159ae8c6e27Sflorian 	if(!iter_env->donotq)
160ae8c6e27Sflorian 		iter_env->donotq = donotq_create();
161ae8c6e27Sflorian 	if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
162ae8c6e27Sflorian 		log_err("Could not set donotqueryaddresses");
163ae8c6e27Sflorian 		return 0;
164ae8c6e27Sflorian 	}
165ae8c6e27Sflorian 	if(!iter_env->priv)
166ae8c6e27Sflorian 		iter_env->priv = priv_create();
167ae8c6e27Sflorian 	if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
168ae8c6e27Sflorian 		log_err("Could not set private addresses");
169ae8c6e27Sflorian 		return 0;
170ae8c6e27Sflorian 	}
171ae8c6e27Sflorian 	if(cfg->caps_whitelist) {
172ae8c6e27Sflorian 		if(!iter_env->caps_white)
173ae8c6e27Sflorian 			iter_env->caps_white = rbtree_create(name_tree_compare);
174ae8c6e27Sflorian 		if(!iter_env->caps_white || !caps_white_apply_cfg(
175ae8c6e27Sflorian 			iter_env->caps_white, cfg)) {
176ae8c6e27Sflorian 			log_err("Could not set capsforid whitelist");
177ae8c6e27Sflorian 			return 0;
178ae8c6e27Sflorian 		}
179ae8c6e27Sflorian 
180ae8c6e27Sflorian 	}
181d500c338Sflorian 
182d500c338Sflorian 	nat64_prefix = cfg->nat64_prefix;
183d500c338Sflorian 	if(!nat64_prefix)
184d500c338Sflorian 		nat64_prefix = cfg->dns64_prefix;
185d500c338Sflorian 	if(!nat64_prefix)
186d500c338Sflorian 		nat64_prefix = DEFAULT_NAT64_PREFIX;
187d500c338Sflorian 	if(!netblockstrtoaddr(nat64_prefix, 0, &iter_env->nat64_prefix_addr,
188d500c338Sflorian 		&iter_env->nat64_prefix_addrlen,
189d500c338Sflorian 		&iter_env->nat64_prefix_net)) {
190d500c338Sflorian 		log_err("cannot parse nat64-prefix netblock: %s", nat64_prefix);
191d500c338Sflorian 		return 0;
192d500c338Sflorian 	}
193d500c338Sflorian 	if(!addr_is_ip6(&iter_env->nat64_prefix_addr,
194d500c338Sflorian 		iter_env->nat64_prefix_addrlen)) {
195d500c338Sflorian 		log_err("nat64-prefix is not IPv6: %s", cfg->nat64_prefix);
196d500c338Sflorian 		return 0;
197d500c338Sflorian 	}
198d500c338Sflorian 	if(!prefixnet_is_nat64(iter_env->nat64_prefix_net)) {
199d500c338Sflorian 		log_err("nat64-prefix length it not 32, 40, 48, 56, 64 or 96: %s",
200d500c338Sflorian 			nat64_prefix);
201d500c338Sflorian 		return 0;
202d500c338Sflorian 	}
203d500c338Sflorian 
204ae8c6e27Sflorian 	iter_env->supports_ipv6 = cfg->do_ip6;
205ae8c6e27Sflorian 	iter_env->supports_ipv4 = cfg->do_ip4;
206d500c338Sflorian 	iter_env->use_nat64 = cfg->do_nat64;
207a1a7ba80Sflorian 	iter_env->outbound_msg_retry = cfg->outbound_msg_retry;
208d500c338Sflorian 	iter_env->max_sent_count = cfg->max_sent_count;
209d500c338Sflorian 	iter_env->max_query_restarts = cfg->max_query_restarts;
210ae8c6e27Sflorian 	return 1;
211ae8c6e27Sflorian }
212ae8c6e27Sflorian 
213ae8c6e27Sflorian /** filter out unsuitable targets
214ae8c6e27Sflorian  * @param iter_env: iterator environment with ipv6-support flag.
215ae8c6e27Sflorian  * @param env: module environment with infra cache.
216ae8c6e27Sflorian  * @param name: zone name
217ae8c6e27Sflorian  * @param namelen: length of name
218ae8c6e27Sflorian  * @param qtype: query type (host order).
219ae8c6e27Sflorian  * @param now: current time
220ae8c6e27Sflorian  * @param a: address in delegation point we are examining.
221ae8c6e27Sflorian  * @return an integer that signals the target suitability.
222ae8c6e27Sflorian  *	as follows:
223ae8c6e27Sflorian  *	-1: The address should be omitted from the list.
224ae8c6e27Sflorian  *	    Because:
225ae8c6e27Sflorian  *		o The address is bogus (DNSSEC validation failure).
226ae8c6e27Sflorian  *		o Listed as donotquery
227ae8c6e27Sflorian  *		o is ipv6 but no ipv6 support (in operating system).
228ae8c6e27Sflorian  *		o is ipv4 but no ipv4 support (in operating system).
229ae8c6e27Sflorian  *		o is lame
230ae8c6e27Sflorian  *	Otherwise, an rtt in milliseconds.
231ae8c6e27Sflorian  *	0 .. USEFUL_SERVER_TOP_TIMEOUT-1
232ae8c6e27Sflorian  *		The roundtrip time timeout estimate. less than 2 minutes.
233ae8c6e27Sflorian  *		Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
234ae8c6e27Sflorian  *		values 0 .. 49 are not used, unless that is changed.
235ae8c6e27Sflorian  *	USEFUL_SERVER_TOP_TIMEOUT
236ae8c6e27Sflorian  *		This value exactly is given for unresponsive blacklisted.
237ae8c6e27Sflorian  *	USEFUL_SERVER_TOP_TIMEOUT+1
238ae8c6e27Sflorian  *		For non-blacklisted servers: huge timeout, but has traffic.
239ae8c6e27Sflorian  *	USEFUL_SERVER_TOP_TIMEOUT*1 ..
240ae8c6e27Sflorian  *		parent-side lame servers get this penalty. A dispreferential
241ae8c6e27Sflorian  *		server. (lame in delegpt).
242ae8c6e27Sflorian  *	USEFUL_SERVER_TOP_TIMEOUT*2 ..
243ae8c6e27Sflorian  *		dnsseclame servers get penalty
244ae8c6e27Sflorian  *	USEFUL_SERVER_TOP_TIMEOUT*3 ..
245ae8c6e27Sflorian  *		recursion lame servers get penalty
246ae8c6e27Sflorian  *	UNKNOWN_SERVER_NICENESS
247ae8c6e27Sflorian  *		If no information is known about the server, this is
248ae8c6e27Sflorian  *		returned. 376 msec or so.
249ae8c6e27Sflorian  *	+BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
250ae8c6e27Sflorian  *
251ae8c6e27Sflorian  * When a final value is chosen that is dnsseclame ; dnsseclameness checking
252ae8c6e27Sflorian  * is turned off (so we do not discard the reply).
253ae8c6e27Sflorian  * When a final value is chosen that is recursionlame; RD bit is set on query.
254ae8c6e27Sflorian  * Because of the numbers this means recursionlame also have dnssec lameness
255ae8c6e27Sflorian  * checking turned off.
256ae8c6e27Sflorian  */
257ae8c6e27Sflorian static int
258ae8c6e27Sflorian iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
259ae8c6e27Sflorian 	uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
260ae8c6e27Sflorian 	struct delegpt_addr* a)
261ae8c6e27Sflorian {
262ae8c6e27Sflorian 	int rtt, lame, reclame, dnsseclame;
263ae8c6e27Sflorian 	if(a->bogus)
264ae8c6e27Sflorian 		return -1; /* address of server is bogus */
265ae8c6e27Sflorian 	if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
266ae8c6e27Sflorian 		log_addr(VERB_ALGO, "skip addr on the donotquery list",
267ae8c6e27Sflorian 			&a->addr, a->addrlen);
268ae8c6e27Sflorian 		return -1; /* server is on the donotquery list */
269ae8c6e27Sflorian 	}
270ae8c6e27Sflorian 	if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
271ae8c6e27Sflorian 		return -1; /* there is no ip6 available */
272ae8c6e27Sflorian 	}
273d500c338Sflorian 	if(!iter_env->supports_ipv4 && !iter_env->use_nat64 &&
274d500c338Sflorian 	   !addr_is_ip6(&a->addr, a->addrlen)) {
275ae8c6e27Sflorian 		return -1; /* there is no ip4 available */
276ae8c6e27Sflorian 	}
277ae8c6e27Sflorian 	/* check lameness - need zone , class info */
278ae8c6e27Sflorian 	if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
279ae8c6e27Sflorian 		name, namelen, qtype, &lame, &dnsseclame, &reclame,
280ae8c6e27Sflorian 		&rtt, now)) {
281ae8c6e27Sflorian 		log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
282*7037e34cSflorian 		verbose(VERB_ALGO, "   rtt=%d%s%s%s%s%s", rtt,
283ae8c6e27Sflorian 			lame?" LAME":"",
284ae8c6e27Sflorian 			dnsseclame?" DNSSEC_LAME":"",
285*7037e34cSflorian 			a->dnsseclame?" ADDR_DNSSEC_LAME":"",
286ae8c6e27Sflorian 			reclame?" REC_LAME":"",
287ae8c6e27Sflorian 			a->lame?" ADDR_LAME":"");
288ae8c6e27Sflorian 		if(lame)
289ae8c6e27Sflorian 			return -1; /* server is lame */
290ae8c6e27Sflorian 		else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
291ae8c6e27Sflorian 			/* server is unresponsive,
292ae8c6e27Sflorian 			 * we used to return TOP_TIMEOUT, but fairly useless,
293ae8c6e27Sflorian 			 * because if == TOP_TIMEOUT is dropped because
294ae8c6e27Sflorian 			 * blacklisted later, instead, remove it here, so
295ae8c6e27Sflorian 			 * other choices (that are not blacklisted) can be
296ae8c6e27Sflorian 			 * tried */
297ae8c6e27Sflorian 			return -1;
298ae8c6e27Sflorian 		/* select remainder from worst to best */
299ae8c6e27Sflorian 		else if(reclame)
300ae8c6e27Sflorian 			return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
301ae8c6e27Sflorian 		else if(dnsseclame || a->dnsseclame)
302ae8c6e27Sflorian 			return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
303ae8c6e27Sflorian 		else if(a->lame)
304ae8c6e27Sflorian 			return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
305ae8c6e27Sflorian 		else	return rtt;
306ae8c6e27Sflorian 	}
307ae8c6e27Sflorian 	/* no server information present */
308ae8c6e27Sflorian 	if(a->dnsseclame)
309ae8c6e27Sflorian 		return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
310ae8c6e27Sflorian 	else if(a->lame)
311ae8c6e27Sflorian 		return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
312ae8c6e27Sflorian 	return UNKNOWN_SERVER_NICENESS;
313ae8c6e27Sflorian }
314ae8c6e27Sflorian 
315ae8c6e27Sflorian /** lookup RTT information, and also store fastest rtt (if any) */
316ae8c6e27Sflorian static int
317ae8c6e27Sflorian iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
318ae8c6e27Sflorian 	uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
319ae8c6e27Sflorian 	struct delegpt* dp, int* best_rtt, struct sock_list* blacklist,
320ae8c6e27Sflorian 	size_t* num_suitable_results)
321ae8c6e27Sflorian {
322ae8c6e27Sflorian 	int got_it = 0;
323ae8c6e27Sflorian 	struct delegpt_addr* a;
324ae8c6e27Sflorian 	*num_suitable_results = 0;
325ae8c6e27Sflorian 
326ae8c6e27Sflorian 	if(dp->bogus)
327ae8c6e27Sflorian 		return 0; /* NS bogus, all bogus, nothing found */
328ae8c6e27Sflorian 	for(a=dp->result_list; a; a = a->next_result) {
329ae8c6e27Sflorian 		a->sel_rtt = iter_filter_unsuitable(iter_env, env,
330ae8c6e27Sflorian 			name, namelen, qtype, now, a);
331ae8c6e27Sflorian 		if(a->sel_rtt != -1) {
332ae8c6e27Sflorian 			if(sock_list_find(blacklist, &a->addr, a->addrlen))
333ae8c6e27Sflorian 				a->sel_rtt += BLACKLIST_PENALTY;
334ae8c6e27Sflorian 
335ae8c6e27Sflorian 			if(!got_it) {
336ae8c6e27Sflorian 				*best_rtt = a->sel_rtt;
337ae8c6e27Sflorian 				got_it = 1;
338ae8c6e27Sflorian 			} else if(a->sel_rtt < *best_rtt) {
339ae8c6e27Sflorian 				*best_rtt = a->sel_rtt;
340ae8c6e27Sflorian 			}
341ae8c6e27Sflorian 			(*num_suitable_results)++;
342ae8c6e27Sflorian 		}
343ae8c6e27Sflorian 	}
344ae8c6e27Sflorian 	return got_it;
345ae8c6e27Sflorian }
346ae8c6e27Sflorian 
347ae8c6e27Sflorian /** compare two rtts, return -1, 0 or 1 */
348ae8c6e27Sflorian static int
349ae8c6e27Sflorian rtt_compare(const void* x, const void* y)
350ae8c6e27Sflorian {
351ae8c6e27Sflorian 	if(*(int*)x == *(int*)y)
352ae8c6e27Sflorian 		return 0;
353ae8c6e27Sflorian 	if(*(int*)x > *(int*)y)
354ae8c6e27Sflorian 		return 1;
355ae8c6e27Sflorian 	return -1;
356ae8c6e27Sflorian }
357ae8c6e27Sflorian 
358ae8c6e27Sflorian /** get RTT for the Nth fastest server */
359ae8c6e27Sflorian static int
360ae8c6e27Sflorian nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n)
361ae8c6e27Sflorian {
362ae8c6e27Sflorian 	int rtt_band;
363ae8c6e27Sflorian 	size_t i;
364ae8c6e27Sflorian 	int* rtt_list, *rtt_index;
365ae8c6e27Sflorian 
366ae8c6e27Sflorian 	if(num_results < 1 || n >= num_results) {
367ae8c6e27Sflorian 		return -1;
368ae8c6e27Sflorian 	}
369ae8c6e27Sflorian 
370ae8c6e27Sflorian 	rtt_list = calloc(num_results, sizeof(int));
371ae8c6e27Sflorian 	if(!rtt_list) {
372ae8c6e27Sflorian 		log_err("malloc failure: allocating rtt_list");
373ae8c6e27Sflorian 		return -1;
374ae8c6e27Sflorian 	}
375ae8c6e27Sflorian 	rtt_index = rtt_list;
376ae8c6e27Sflorian 
377ae8c6e27Sflorian 	for(i=0; i<num_results && result_list; i++) {
378ae8c6e27Sflorian 		if(result_list->sel_rtt != -1) {
379ae8c6e27Sflorian 			*rtt_index = result_list->sel_rtt;
380ae8c6e27Sflorian 			rtt_index++;
381ae8c6e27Sflorian 		}
382ae8c6e27Sflorian 		result_list=result_list->next_result;
383ae8c6e27Sflorian 	}
384ae8c6e27Sflorian 	qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare);
385ae8c6e27Sflorian 
386ae8c6e27Sflorian 	log_assert(n > 0);
387ae8c6e27Sflorian 	rtt_band = rtt_list[n-1];
388ae8c6e27Sflorian 	free(rtt_list);
389ae8c6e27Sflorian 
390ae8c6e27Sflorian 	return rtt_band;
391ae8c6e27Sflorian }
392ae8c6e27Sflorian 
393ae8c6e27Sflorian /** filter the address list, putting best targets at front,
394ae8c6e27Sflorian  * returns number of best targets (or 0, no suitable targets) */
395ae8c6e27Sflorian static int
396ae8c6e27Sflorian iter_filter_order(struct iter_env* iter_env, struct module_env* env,
397ae8c6e27Sflorian 	uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
398ae8c6e27Sflorian 	struct delegpt* dp, int* selected_rtt, int open_target,
399ae8c6e27Sflorian 	struct sock_list* blacklist, time_t prefetch)
400ae8c6e27Sflorian {
401ae8c6e27Sflorian 	int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth;
4026d08cb1bSflorian 	int alllame = 0;
403ae8c6e27Sflorian 	size_t num_results;
404ae8c6e27Sflorian 	struct delegpt_addr* a, *n, *prev=NULL;
405ae8c6e27Sflorian 
406ae8c6e27Sflorian 	/* fillup sel_rtt and find best rtt in the bunch */
407ae8c6e27Sflorian 	got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
408ae8c6e27Sflorian 		&low_rtt, blacklist, &num_results);
409ae8c6e27Sflorian 	if(got_num == 0)
410ae8c6e27Sflorian 		return 0;
411ae8c6e27Sflorian 	if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
4126d08cb1bSflorian 		/* If all missing (or not fully resolved) targets are lame,
4136d08cb1bSflorian 		 * then use the remaining lame address. */
4146d08cb1bSflorian 		((delegpt_count_missing_targets(dp, &alllame) > 0 && !alllame) ||
4156d08cb1bSflorian 		open_target > 0)) {
416ae8c6e27Sflorian 		verbose(VERB_ALGO, "Bad choices, trying to get more choice");
417ae8c6e27Sflorian 		return 0; /* we want more choice. The best choice is a bad one.
418ae8c6e27Sflorian 			     return 0 to force the caller to fetch more */
419ae8c6e27Sflorian 	}
420ae8c6e27Sflorian 
421ae8c6e27Sflorian 	if(env->cfg->fast_server_permil != 0 && prefetch == 0 &&
422ae8c6e27Sflorian 		num_results > env->cfg->fast_server_num &&
423ae8c6e27Sflorian 		ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) {
424ae8c6e27Sflorian 		/* the query is not prefetch, but for a downstream client,
425ae8c6e27Sflorian 		 * there are more servers available then the fastest N we want
426ae8c6e27Sflorian 		 * to choose from. Limit our choice to the fastest servers. */
427ae8c6e27Sflorian 		nth = nth_rtt(dp->result_list, num_results,
428ae8c6e27Sflorian 			env->cfg->fast_server_num);
429ae8c6e27Sflorian 		if(nth > 0) {
430ae8c6e27Sflorian 			rtt_band = nth - low_rtt;
431ae8c6e27Sflorian 			if(rtt_band > RTT_BAND)
432ae8c6e27Sflorian 				rtt_band = RTT_BAND;
433ae8c6e27Sflorian 		}
434ae8c6e27Sflorian 	}
435ae8c6e27Sflorian 
436ae8c6e27Sflorian 	got_num = 0;
437ae8c6e27Sflorian 	a = dp->result_list;
438ae8c6e27Sflorian 	while(a) {
439ae8c6e27Sflorian 		/* skip unsuitable targets */
440ae8c6e27Sflorian 		if(a->sel_rtt == -1) {
441ae8c6e27Sflorian 			prev = a;
442ae8c6e27Sflorian 			a = a->next_result;
443ae8c6e27Sflorian 			continue;
444ae8c6e27Sflorian 		}
445ae8c6e27Sflorian 		/* classify the server address and determine what to do */
446ae8c6e27Sflorian 		swap_to_front = 0;
447ae8c6e27Sflorian 		if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) {
448ae8c6e27Sflorian 			got_num++;
449ae8c6e27Sflorian 			swap_to_front = 1;
450ae8c6e27Sflorian 		} else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) {
451ae8c6e27Sflorian 			got_num++;
452ae8c6e27Sflorian 			swap_to_front = 1;
453ae8c6e27Sflorian 		}
454ae8c6e27Sflorian 		/* swap to front if necessary, or move to next result */
455ae8c6e27Sflorian 		if(swap_to_front && prev) {
456ae8c6e27Sflorian 			n = a->next_result;
457ae8c6e27Sflorian 			prev->next_result = n;
458ae8c6e27Sflorian 			a->next_result = dp->result_list;
459ae8c6e27Sflorian 			dp->result_list = a;
460ae8c6e27Sflorian 			a = n;
461ae8c6e27Sflorian 		} else {
462ae8c6e27Sflorian 			prev = a;
463ae8c6e27Sflorian 			a = a->next_result;
464ae8c6e27Sflorian 		}
465ae8c6e27Sflorian 	}
466ae8c6e27Sflorian 	*selected_rtt = low_rtt;
467ae8c6e27Sflorian 
468ae8c6e27Sflorian 	if (env->cfg->prefer_ip6) {
469ae8c6e27Sflorian 		int got_num6 = 0;
470ae8c6e27Sflorian 		int low_rtt6 = 0;
471ae8c6e27Sflorian 		int i;
472ae8c6e27Sflorian 		int attempt = -1; /* filter to make sure addresses have
473ae8c6e27Sflorian 		  less attempts on them than the first, to force round
474ae8c6e27Sflorian 		  robin when all the IPv6 addresses fail */
475ae8c6e27Sflorian 		int num4ok = 0; /* number ip4 at low attempt count */
476ae8c6e27Sflorian 		int num4_lowrtt = 0;
477ae8c6e27Sflorian 		prev = NULL;
478ae8c6e27Sflorian 		a = dp->result_list;
479ae8c6e27Sflorian 		for(i = 0; i < got_num; i++) {
480411c5950Sflorian 			if(!a) break; /* robustness */
481ae8c6e27Sflorian 			swap_to_front = 0;
482ae8c6e27Sflorian 			if(a->addr.ss_family != AF_INET6 && attempt == -1) {
483ae8c6e27Sflorian 				/* if we only have ip4 at low attempt count,
484ae8c6e27Sflorian 				 * then ip6 is failing, and we need to
485ae8c6e27Sflorian 				 * select one of the remaining IPv4 addrs */
486ae8c6e27Sflorian 				attempt = a->attempts;
487ae8c6e27Sflorian 				num4ok++;
488ae8c6e27Sflorian 				num4_lowrtt = a->sel_rtt;
489ae8c6e27Sflorian 			} else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) {
490ae8c6e27Sflorian 				num4ok++;
491ae8c6e27Sflorian 				if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) {
492ae8c6e27Sflorian 					num4_lowrtt = a->sel_rtt;
493ae8c6e27Sflorian 				}
494ae8c6e27Sflorian 			}
495ae8c6e27Sflorian 			if(a->addr.ss_family == AF_INET6) {
496ae8c6e27Sflorian 				if(attempt == -1) {
497ae8c6e27Sflorian 					attempt = a->attempts;
498ae8c6e27Sflorian 				} else if(a->attempts > attempt) {
499ae8c6e27Sflorian 					break;
500ae8c6e27Sflorian 				}
501ae8c6e27Sflorian 				got_num6++;
502ae8c6e27Sflorian 				swap_to_front = 1;
503ae8c6e27Sflorian 				if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) {
504ae8c6e27Sflorian 					low_rtt6 = a->sel_rtt;
505ae8c6e27Sflorian 				}
506ae8c6e27Sflorian 			}
507ae8c6e27Sflorian 			/* swap to front if IPv6, or move to next result */
508ae8c6e27Sflorian 			if(swap_to_front && prev) {
509ae8c6e27Sflorian 				n = a->next_result;
510ae8c6e27Sflorian 				prev->next_result = n;
511ae8c6e27Sflorian 				a->next_result = dp->result_list;
512ae8c6e27Sflorian 				dp->result_list = a;
513ae8c6e27Sflorian 				a = n;
514ae8c6e27Sflorian 			} else {
515ae8c6e27Sflorian 				prev = a;
516ae8c6e27Sflorian 				a = a->next_result;
517ae8c6e27Sflorian 			}
518ae8c6e27Sflorian 		}
519ae8c6e27Sflorian 		if(got_num6 > 0) {
520ae8c6e27Sflorian 			got_num = got_num6;
521ae8c6e27Sflorian 			*selected_rtt = low_rtt6;
522ae8c6e27Sflorian 		} else if(num4ok > 0) {
523ae8c6e27Sflorian 			got_num = num4ok;
524ae8c6e27Sflorian 			*selected_rtt = num4_lowrtt;
525ae8c6e27Sflorian 		}
526e47fef9eSflorian 	} else if (env->cfg->prefer_ip4) {
527e47fef9eSflorian 		int got_num4 = 0;
528e47fef9eSflorian 		int low_rtt4 = 0;
529e47fef9eSflorian 		int i;
530e47fef9eSflorian 		int attempt = -1; /* filter to make sure addresses have
531e47fef9eSflorian 		  less attempts on them than the first, to force round
532e47fef9eSflorian 		  robin when all the IPv4 addresses fail */
533e47fef9eSflorian 		int num6ok = 0; /* number ip6 at low attempt count */
534e47fef9eSflorian 		int num6_lowrtt = 0;
535e47fef9eSflorian 		prev = NULL;
536e47fef9eSflorian 		a = dp->result_list;
537e47fef9eSflorian 		for(i = 0; i < got_num; i++) {
538411c5950Sflorian 			if(!a) break; /* robustness */
539e47fef9eSflorian 			swap_to_front = 0;
540e47fef9eSflorian 			if(a->addr.ss_family != AF_INET && attempt == -1) {
541e47fef9eSflorian 				/* if we only have ip6 at low attempt count,
542e47fef9eSflorian 				 * then ip4 is failing, and we need to
543e47fef9eSflorian 				 * select one of the remaining IPv6 addrs */
544e47fef9eSflorian 				attempt = a->attempts;
545e47fef9eSflorian 				num6ok++;
546e47fef9eSflorian 				num6_lowrtt = a->sel_rtt;
547e47fef9eSflorian 			} else if(a->addr.ss_family != AF_INET && attempt == a->attempts) {
548e47fef9eSflorian 				num6ok++;
549e47fef9eSflorian 				if(num6_lowrtt == 0 || a->sel_rtt < num6_lowrtt) {
550e47fef9eSflorian 					num6_lowrtt = a->sel_rtt;
551e47fef9eSflorian 				}
552e47fef9eSflorian 			}
553e47fef9eSflorian 			if(a->addr.ss_family == AF_INET) {
554e47fef9eSflorian 				if(attempt == -1) {
555e47fef9eSflorian 					attempt = a->attempts;
556e47fef9eSflorian 				} else if(a->attempts > attempt) {
557e47fef9eSflorian 					break;
558e47fef9eSflorian 				}
559e47fef9eSflorian 				got_num4++;
560e47fef9eSflorian 				swap_to_front = 1;
561e47fef9eSflorian 				if(low_rtt4 == 0 || a->sel_rtt < low_rtt4) {
562e47fef9eSflorian 					low_rtt4 = a->sel_rtt;
563e47fef9eSflorian 				}
564e47fef9eSflorian 			}
565e47fef9eSflorian 			/* swap to front if IPv4, or move to next result */
566e47fef9eSflorian 			if(swap_to_front && prev) {
567e47fef9eSflorian 				n = a->next_result;
568e47fef9eSflorian 				prev->next_result = n;
569e47fef9eSflorian 				a->next_result = dp->result_list;
570e47fef9eSflorian 				dp->result_list = a;
571e47fef9eSflorian 				a = n;
572e47fef9eSflorian 			} else {
573e47fef9eSflorian 				prev = a;
574e47fef9eSflorian 				a = a->next_result;
575e47fef9eSflorian 			}
576e47fef9eSflorian 		}
577e47fef9eSflorian 		if(got_num4 > 0) {
578e47fef9eSflorian 			got_num = got_num4;
579e47fef9eSflorian 			*selected_rtt = low_rtt4;
580e47fef9eSflorian 		} else if(num6ok > 0) {
581e47fef9eSflorian 			got_num = num6ok;
582e47fef9eSflorian 			*selected_rtt = num6_lowrtt;
583e47fef9eSflorian 		}
584ae8c6e27Sflorian 	}
585ae8c6e27Sflorian 	return got_num;
586ae8c6e27Sflorian }
587ae8c6e27Sflorian 
588ae8c6e27Sflorian struct delegpt_addr*
589ae8c6e27Sflorian iter_server_selection(struct iter_env* iter_env,
590ae8c6e27Sflorian 	struct module_env* env, struct delegpt* dp,
591ae8c6e27Sflorian 	uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
592ae8c6e27Sflorian 	int* chase_to_rd, int open_target, struct sock_list* blacklist,
593ae8c6e27Sflorian 	time_t prefetch)
594ae8c6e27Sflorian {
595ae8c6e27Sflorian 	int sel;
596ae8c6e27Sflorian 	int selrtt;
597ae8c6e27Sflorian 	struct delegpt_addr* a, *prev;
598ae8c6e27Sflorian 	int num = iter_filter_order(iter_env, env, name, namelen, qtype,
599ae8c6e27Sflorian 		*env->now, dp, &selrtt, open_target, blacklist, prefetch);
600ae8c6e27Sflorian 
601ae8c6e27Sflorian 	if(num == 0)
602ae8c6e27Sflorian 		return NULL;
603ae8c6e27Sflorian 	verbose(VERB_ALGO, "selrtt %d", selrtt);
604ae8c6e27Sflorian 	if(selrtt > BLACKLIST_PENALTY) {
605ae8c6e27Sflorian 		if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
606ae8c6e27Sflorian 			verbose(VERB_ALGO, "chase to "
607ae8c6e27Sflorian 				"blacklisted recursion lame server");
608ae8c6e27Sflorian 			*chase_to_rd = 1;
609ae8c6e27Sflorian 		}
610ae8c6e27Sflorian 		if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
611ae8c6e27Sflorian 			verbose(VERB_ALGO, "chase to "
612ae8c6e27Sflorian 				"blacklisted dnssec lame server");
613ae8c6e27Sflorian 			*dnssec_lame = 1;
614ae8c6e27Sflorian 		}
615ae8c6e27Sflorian 	} else {
616ae8c6e27Sflorian 		if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
617ae8c6e27Sflorian 			verbose(VERB_ALGO, "chase to recursion lame server");
618ae8c6e27Sflorian 			*chase_to_rd = 1;
619ae8c6e27Sflorian 		}
620ae8c6e27Sflorian 		if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
621ae8c6e27Sflorian 			verbose(VERB_ALGO, "chase to dnssec lame server");
622ae8c6e27Sflorian 			*dnssec_lame = 1;
623ae8c6e27Sflorian 		}
624ae8c6e27Sflorian 		if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
625ae8c6e27Sflorian 			verbose(VERB_ALGO, "chase to blacklisted lame server");
626ae8c6e27Sflorian 			return NULL;
627ae8c6e27Sflorian 		}
628ae8c6e27Sflorian 	}
629ae8c6e27Sflorian 
630ae8c6e27Sflorian 	if(num == 1) {
631ae8c6e27Sflorian 		a = dp->result_list;
632a1a7ba80Sflorian 		if(++a->attempts < iter_env->outbound_msg_retry)
633ae8c6e27Sflorian 			return a;
634ae8c6e27Sflorian 		dp->result_list = a->next_result;
635ae8c6e27Sflorian 		return a;
636ae8c6e27Sflorian 	}
637ae8c6e27Sflorian 
638ae8c6e27Sflorian 	/* randomly select a target from the list */
639ae8c6e27Sflorian 	log_assert(num > 1);
640ae8c6e27Sflorian 	/* grab secure random number, to pick unexpected server.
641ae8c6e27Sflorian 	 * also we need it to be threadsafe. */
642ae8c6e27Sflorian 	sel = ub_random_max(env->rnd, num);
643ae8c6e27Sflorian 	a = dp->result_list;
644ae8c6e27Sflorian 	prev = NULL;
645ae8c6e27Sflorian 	while(sel > 0 && a) {
646ae8c6e27Sflorian 		prev = a;
647ae8c6e27Sflorian 		a = a->next_result;
648ae8c6e27Sflorian 		sel--;
649ae8c6e27Sflorian 	}
650ae8c6e27Sflorian 	if(!a)  /* robustness */
651ae8c6e27Sflorian 		return NULL;
652a1a7ba80Sflorian 	if(++a->attempts < iter_env->outbound_msg_retry)
653ae8c6e27Sflorian 		return a;
654ae8c6e27Sflorian 	/* remove it from the delegation point result list */
655ae8c6e27Sflorian 	if(prev)
656ae8c6e27Sflorian 		prev->next_result = a->next_result;
657ae8c6e27Sflorian 	else	dp->result_list = a->next_result;
658ae8c6e27Sflorian 	return a;
659ae8c6e27Sflorian }
660ae8c6e27Sflorian 
661ae8c6e27Sflorian struct dns_msg*
662ae8c6e27Sflorian dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg,
663ae8c6e27Sflorian 	struct regional* region)
664ae8c6e27Sflorian {
665ae8c6e27Sflorian 	struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
666ae8c6e27Sflorian 		sizeof(struct dns_msg));
667ae8c6e27Sflorian 	if(!m)
668ae8c6e27Sflorian 		return NULL;
669ae8c6e27Sflorian 	memset(m, 0, sizeof(*m));
670ae8c6e27Sflorian 	if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
671ae8c6e27Sflorian 		log_err("malloc failure: allocating incoming dns_msg");
672ae8c6e27Sflorian 		return NULL;
673ae8c6e27Sflorian 	}
674ae8c6e27Sflorian 	return m;
675ae8c6e27Sflorian }
676ae8c6e27Sflorian 
677ae8c6e27Sflorian struct dns_msg*
678ae8c6e27Sflorian dns_copy_msg(struct dns_msg* from, struct regional* region)
679ae8c6e27Sflorian {
680ae8c6e27Sflorian 	struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
681ae8c6e27Sflorian 		sizeof(struct dns_msg));
682ae8c6e27Sflorian 	if(!m)
683ae8c6e27Sflorian 		return NULL;
684ae8c6e27Sflorian 	m->qinfo = from->qinfo;
685ae8c6e27Sflorian 	if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
686ae8c6e27Sflorian 		from->qinfo.qname_len)))
687ae8c6e27Sflorian 		return NULL;
688ae8c6e27Sflorian 	if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
689ae8c6e27Sflorian 		return NULL;
690ae8c6e27Sflorian 	return m;
691ae8c6e27Sflorian }
692ae8c6e27Sflorian 
693ae8c6e27Sflorian void
694ae8c6e27Sflorian iter_dns_store(struct module_env* env, struct query_info* msgqinf,
695ae8c6e27Sflorian 	struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
6966d08cb1bSflorian 	struct regional* region, uint16_t flags, time_t qstarttime)
697ae8c6e27Sflorian {
698ae8c6e27Sflorian 	if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
6996d08cb1bSflorian 		pside, region, flags, qstarttime))
700ae8c6e27Sflorian 		log_err("out of memory: cannot store data in cache");
701ae8c6e27Sflorian }
702ae8c6e27Sflorian 
703ae8c6e27Sflorian int
704ae8c6e27Sflorian iter_ns_probability(struct ub_randstate* rnd, int n, int m)
705ae8c6e27Sflorian {
706ae8c6e27Sflorian 	int sel;
707ae8c6e27Sflorian 	if(n == m) /* 100% chance */
708ae8c6e27Sflorian 		return 1;
709ae8c6e27Sflorian 	/* we do not need secure random numbers here, but
710ae8c6e27Sflorian 	 * we do need it to be threadsafe, so we use this */
711ae8c6e27Sflorian 	sel = ub_random_max(rnd, m);
712ae8c6e27Sflorian 	return (sel < n);
713ae8c6e27Sflorian }
714ae8c6e27Sflorian 
715ae8c6e27Sflorian /** detect dependency cycle for query and target */
716ae8c6e27Sflorian static int
717ae8c6e27Sflorian causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
718ae8c6e27Sflorian 	uint16_t t, uint16_t c)
719ae8c6e27Sflorian {
720ae8c6e27Sflorian 	struct query_info qinf;
721ae8c6e27Sflorian 	qinf.qname = name;
722ae8c6e27Sflorian 	qinf.qname_len = namelen;
723ae8c6e27Sflorian 	qinf.qtype = t;
724ae8c6e27Sflorian 	qinf.qclass = c;
725ae8c6e27Sflorian 	qinf.local_alias = NULL;
726ae8c6e27Sflorian 	fptr_ok(fptr_whitelist_modenv_detect_cycle(
727ae8c6e27Sflorian 		qstate->env->detect_cycle));
728ae8c6e27Sflorian 	return (*qstate->env->detect_cycle)(qstate, &qinf,
729ae8c6e27Sflorian 		(uint16_t)(BIT_RD|BIT_CD), qstate->is_priming,
730ae8c6e27Sflorian 		qstate->is_valrec);
731ae8c6e27Sflorian }
732ae8c6e27Sflorian 
733ae8c6e27Sflorian void
734ae8c6e27Sflorian iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
735ae8c6e27Sflorian {
736ae8c6e27Sflorian 	struct delegpt_ns* ns;
737ae8c6e27Sflorian 	for(ns = dp->nslist; ns; ns = ns->next) {
738ae8c6e27Sflorian 		if(ns->resolved)
739ae8c6e27Sflorian 			continue;
740ae8c6e27Sflorian 		/* see if this ns as target causes dependency cycle */
741ae8c6e27Sflorian 		if(causes_cycle(qstate, ns->name, ns->namelen,
742ae8c6e27Sflorian 			LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
743ae8c6e27Sflorian 		   causes_cycle(qstate, ns->name, ns->namelen,
744ae8c6e27Sflorian 			LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
745ae8c6e27Sflorian 			log_nametypeclass(VERB_QUERY, "skipping target due "
746ae8c6e27Sflorian 			 	"to dependency cycle (harden-glue: no may "
747ae8c6e27Sflorian 				"fix some of the cycles)",
748ae8c6e27Sflorian 				ns->name, LDNS_RR_TYPE_A,
749ae8c6e27Sflorian 				qstate->qinfo.qclass);
750ae8c6e27Sflorian 			ns->resolved = 1;
751ae8c6e27Sflorian 		}
752ae8c6e27Sflorian 	}
753ae8c6e27Sflorian }
754ae8c6e27Sflorian 
755ae8c6e27Sflorian void
756ae8c6e27Sflorian iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
757ae8c6e27Sflorian {
758ae8c6e27Sflorian 	struct delegpt_ns* ns;
759ae8c6e27Sflorian 	for(ns = dp->nslist; ns; ns = ns->next) {
760ae8c6e27Sflorian 		if(ns->done_pside4 && ns->done_pside6)
761ae8c6e27Sflorian 			continue;
762ae8c6e27Sflorian 		/* see if this ns as target causes dependency cycle */
763ae8c6e27Sflorian 		if(causes_cycle(qstate, ns->name, ns->namelen,
764ae8c6e27Sflorian 			LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
765ae8c6e27Sflorian 			log_nametypeclass(VERB_QUERY, "skipping target due "
766ae8c6e27Sflorian 			 	"to dependency cycle", ns->name,
767ae8c6e27Sflorian 				LDNS_RR_TYPE_A, qstate->qinfo.qclass);
768ae8c6e27Sflorian 			ns->done_pside4 = 1;
769ae8c6e27Sflorian 		}
770ae8c6e27Sflorian 		if(causes_cycle(qstate, ns->name, ns->namelen,
771ae8c6e27Sflorian 			LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
772ae8c6e27Sflorian 			log_nametypeclass(VERB_QUERY, "skipping target due "
773ae8c6e27Sflorian 			 	"to dependency cycle", ns->name,
774ae8c6e27Sflorian 				LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
775ae8c6e27Sflorian 			ns->done_pside6 = 1;
776ae8c6e27Sflorian 		}
777ae8c6e27Sflorian 	}
778ae8c6e27Sflorian }
779ae8c6e27Sflorian 
780ae8c6e27Sflorian int
781ae8c6e27Sflorian iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
782d500c338Sflorian 	struct delegpt* dp, int supports_ipv4, int supports_ipv6,
783d500c338Sflorian 	int use_nat64)
784ae8c6e27Sflorian {
785ae8c6e27Sflorian 	struct delegpt_ns* ns;
7867a05b9dfSflorian 	struct delegpt_addr* a;
787d500c338Sflorian 
788d500c338Sflorian 	if(supports_ipv6 && use_nat64)
789d500c338Sflorian 		supports_ipv4 = 1;
790d500c338Sflorian 
791ae8c6e27Sflorian 	/* check:
792ae8c6e27Sflorian 	 *      o RD qflag is on.
793ae8c6e27Sflorian 	 *      o no addresses are provided.
794ae8c6e27Sflorian 	 *      o all NS items are required glue.
795ae8c6e27Sflorian 	 * OR
796ae8c6e27Sflorian 	 *      o RD qflag is on.
797ae8c6e27Sflorian 	 *      o no addresses are provided.
798ae8c6e27Sflorian 	 *      o the query is for one of the nameservers in dp,
799ae8c6e27Sflorian 	 *        and that nameserver is a glue-name for this dp.
800ae8c6e27Sflorian 	 */
801ae8c6e27Sflorian 	if(!(qflags&BIT_RD))
802ae8c6e27Sflorian 		return 0;
8037a05b9dfSflorian 	/* either available or unused targets,
8047a05b9dfSflorian 	 * if they exist, the dp is not useless. */
8057a05b9dfSflorian 	for(a = dp->usable_list; a; a = a->next_usable) {
8067a05b9dfSflorian 		if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
807ae8c6e27Sflorian 			return 0;
8087a05b9dfSflorian 		else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
8097a05b9dfSflorian 			return 0;
8107a05b9dfSflorian 	}
8117a05b9dfSflorian 	for(a = dp->result_list; a; a = a->next_result) {
8127a05b9dfSflorian 		if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
8137a05b9dfSflorian 			return 0;
8147a05b9dfSflorian 		else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
8157a05b9dfSflorian 			return 0;
8167a05b9dfSflorian 	}
817ae8c6e27Sflorian 
818ae8c6e27Sflorian 	/* see if query is for one of the nameservers, which is glue */
8197a05b9dfSflorian 	if( ((qinfo->qtype == LDNS_RR_TYPE_A && supports_ipv4) ||
8207a05b9dfSflorian 		(qinfo->qtype == LDNS_RR_TYPE_AAAA && supports_ipv6)) &&
821ae8c6e27Sflorian 		dname_subdomain_c(qinfo->qname, dp->name) &&
822ae8c6e27Sflorian 		delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
823ae8c6e27Sflorian 		return 1;
824ae8c6e27Sflorian 
825ae8c6e27Sflorian 	for(ns = dp->nslist; ns; ns = ns->next) {
826ae8c6e27Sflorian 		if(ns->resolved) /* skip failed targets */
827ae8c6e27Sflorian 			continue;
828ae8c6e27Sflorian 		if(!dname_subdomain_c(ns->name, dp->name))
829ae8c6e27Sflorian 			return 0; /* one address is not required glue */
830ae8c6e27Sflorian 	}
831ae8c6e27Sflorian 	return 1;
832ae8c6e27Sflorian }
833ae8c6e27Sflorian 
834ae8c6e27Sflorian int
835ae8c6e27Sflorian iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo)
836ae8c6e27Sflorian {
837ae8c6e27Sflorian 	struct trust_anchor* a;
838ae8c6e27Sflorian 	if(!env || !env->anchors || !qinfo || !qinfo->qname)
839ae8c6e27Sflorian 		return 0;
840ae8c6e27Sflorian 	/* a trust anchor exists above the name? */
841ae8c6e27Sflorian 	if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len,
842ae8c6e27Sflorian 		qinfo->qclass))) {
843ae8c6e27Sflorian 		if(a->numDS == 0 && a->numDNSKEY == 0) {
844ae8c6e27Sflorian 			/* insecure trust point */
845ae8c6e27Sflorian 			lock_basic_unlock(&a->lock);
846ae8c6e27Sflorian 			return 0;
847ae8c6e27Sflorian 		}
848ae8c6e27Sflorian 		lock_basic_unlock(&a->lock);
849ae8c6e27Sflorian 		return 1;
850ae8c6e27Sflorian 	}
851ae8c6e27Sflorian 	/* no trust anchor above it. */
852ae8c6e27Sflorian 	return 0;
853ae8c6e27Sflorian }
854ae8c6e27Sflorian 
855ae8c6e27Sflorian int
856ae8c6e27Sflorian iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
857ae8c6e27Sflorian         struct dns_msg* msg, uint16_t dclass)
858ae8c6e27Sflorian {
859ae8c6e27Sflorian 	struct trust_anchor* a;
860ae8c6e27Sflorian 	/* information not available, !env->anchors can be common */
861ae8c6e27Sflorian 	if(!env || !env->anchors || !dp || !dp->name)
862ae8c6e27Sflorian 		return 0;
863ae8c6e27Sflorian 	/* a trust anchor exists with this name, RRSIGs expected */
864ae8c6e27Sflorian 	if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
865ae8c6e27Sflorian 		dclass))) {
866ae8c6e27Sflorian 		if(a->numDS == 0 && a->numDNSKEY == 0) {
867ae8c6e27Sflorian 			/* insecure trust point */
868ae8c6e27Sflorian 			lock_basic_unlock(&a->lock);
869ae8c6e27Sflorian 			return 0;
870ae8c6e27Sflorian 		}
871ae8c6e27Sflorian 		lock_basic_unlock(&a->lock);
872ae8c6e27Sflorian 		return 1;
873ae8c6e27Sflorian 	}
874ae8c6e27Sflorian 	/* see if DS rrset was given, in AUTH section */
875ae8c6e27Sflorian 	if(msg && msg->rep &&
876ae8c6e27Sflorian 		reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
877ae8c6e27Sflorian 		LDNS_RR_TYPE_DS, dclass))
878ae8c6e27Sflorian 		return 1;
879ae8c6e27Sflorian 	/* look in key cache */
880ae8c6e27Sflorian 	if(env->key_cache) {
881ae8c6e27Sflorian 		struct key_entry_key* kk = key_cache_obtain(env->key_cache,
882ae8c6e27Sflorian 			dp->name, dp->namelen, dclass, env->scratch, *env->now);
883ae8c6e27Sflorian 		if(kk) {
884ae8c6e27Sflorian 			if(query_dname_compare(kk->name, dp->name) == 0) {
885ae8c6e27Sflorian 			  if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
886ae8c6e27Sflorian 				regional_free_all(env->scratch);
887ae8c6e27Sflorian 				return 1;
888ae8c6e27Sflorian 			  } else if(key_entry_isnull(kk)) {
889ae8c6e27Sflorian 				regional_free_all(env->scratch);
890ae8c6e27Sflorian 				return 0;
891ae8c6e27Sflorian 			  }
892ae8c6e27Sflorian 			}
893ae8c6e27Sflorian 			regional_free_all(env->scratch);
894ae8c6e27Sflorian 		}
895ae8c6e27Sflorian 	}
896ae8c6e27Sflorian 	return 0;
897ae8c6e27Sflorian }
898ae8c6e27Sflorian 
899ae8c6e27Sflorian int
900ae8c6e27Sflorian iter_msg_has_dnssec(struct dns_msg* msg)
901ae8c6e27Sflorian {
902ae8c6e27Sflorian 	size_t i;
903ae8c6e27Sflorian 	if(!msg || !msg->rep)
904ae8c6e27Sflorian 		return 0;
905ae8c6e27Sflorian 	for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
906ae8c6e27Sflorian 		if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
907ae8c6e27Sflorian 			entry.data)->rrsig_count > 0)
908ae8c6e27Sflorian 			return 1;
909ae8c6e27Sflorian 	}
910ae8c6e27Sflorian 	/* empty message has no DNSSEC info, with DNSSEC the reply is
911ae8c6e27Sflorian 	 * not empty (NSEC) */
912ae8c6e27Sflorian 	return 0;
913ae8c6e27Sflorian }
914ae8c6e27Sflorian 
915ae8c6e27Sflorian int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
916ae8c6e27Sflorian         enum response_type type, uint16_t dclass)
917ae8c6e27Sflorian {
918ae8c6e27Sflorian 	if(!msg || !dp || !msg->rep || !dp->name)
919ae8c6e27Sflorian 		return 0;
920ae8c6e27Sflorian 	/* SOA RRset - always from reply zone */
921ae8c6e27Sflorian 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
922ae8c6e27Sflorian 		LDNS_RR_TYPE_SOA, dclass) ||
923ae8c6e27Sflorian 	   reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
924ae8c6e27Sflorian 		LDNS_RR_TYPE_SOA, dclass))
925ae8c6e27Sflorian 		return 1;
926ae8c6e27Sflorian 	if(type == RESPONSE_TYPE_REFERRAL) {
927ae8c6e27Sflorian 		size_t i;
928ae8c6e27Sflorian 		/* if it adds a single label, i.e. we expect .com,
929ae8c6e27Sflorian 		 * and referral to example.com. NS ... , then origin zone
930ae8c6e27Sflorian 		 * is .com. For a referral to sub.example.com. NS ... then
931ae8c6e27Sflorian 		 * we do not know, since example.com. may be in between. */
932ae8c6e27Sflorian 		for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
933ae8c6e27Sflorian 			i++) {
934ae8c6e27Sflorian 			struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
935ae8c6e27Sflorian 			if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
936ae8c6e27Sflorian 				ntohs(s->rk.rrset_class) == dclass) {
937ae8c6e27Sflorian 				int l = dname_count_labels(s->rk.dname);
938ae8c6e27Sflorian 				if(l == dp->namelabs + 1 &&
939ae8c6e27Sflorian 					dname_strict_subdomain(s->rk.dname,
940ae8c6e27Sflorian 					l, dp->name, dp->namelabs))
941ae8c6e27Sflorian 					return 1;
942ae8c6e27Sflorian 			}
943ae8c6e27Sflorian 		}
944ae8c6e27Sflorian 		return 0;
945ae8c6e27Sflorian 	}
946ae8c6e27Sflorian 	log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
947ae8c6e27Sflorian 	/* not a referral, and not lame delegation (upwards), so,
948ae8c6e27Sflorian 	 * any NS rrset must be from the zone itself */
949ae8c6e27Sflorian 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
950ae8c6e27Sflorian 		LDNS_RR_TYPE_NS, dclass) ||
951ae8c6e27Sflorian 	   reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
952ae8c6e27Sflorian 		LDNS_RR_TYPE_NS, dclass))
953ae8c6e27Sflorian 		return 1;
954ae8c6e27Sflorian 	/* a DNSKEY set is expected at the zone apex as well */
955ae8c6e27Sflorian 	/* this is for 'minimal responses' for DNSKEYs */
956ae8c6e27Sflorian 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
957ae8c6e27Sflorian 		LDNS_RR_TYPE_DNSKEY, dclass))
958ae8c6e27Sflorian 		return 1;
959ae8c6e27Sflorian 	return 0;
960ae8c6e27Sflorian }
961ae8c6e27Sflorian 
962ae8c6e27Sflorian /**
963ae8c6e27Sflorian  * check equality of two rrsets
964ae8c6e27Sflorian  * @param k1: rrset
965ae8c6e27Sflorian  * @param k2: rrset
966ae8c6e27Sflorian  * @return true if equal
967ae8c6e27Sflorian  */
968ae8c6e27Sflorian static int
969ae8c6e27Sflorian rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
970ae8c6e27Sflorian {
971ae8c6e27Sflorian 	struct packed_rrset_data* d1 = (struct packed_rrset_data*)
972ae8c6e27Sflorian 		k1->entry.data;
973ae8c6e27Sflorian 	struct packed_rrset_data* d2 = (struct packed_rrset_data*)
974ae8c6e27Sflorian 		k2->entry.data;
975ae8c6e27Sflorian 	size_t i, t;
976ae8c6e27Sflorian 	if(k1->rk.dname_len != k2->rk.dname_len ||
977ae8c6e27Sflorian 		k1->rk.flags != k2->rk.flags ||
978ae8c6e27Sflorian 		k1->rk.type != k2->rk.type ||
979ae8c6e27Sflorian 		k1->rk.rrset_class != k2->rk.rrset_class ||
980ae8c6e27Sflorian 		query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
981ae8c6e27Sflorian 		return 0;
982ae8c6e27Sflorian 	if(	/* do not check ttl: d1->ttl != d2->ttl || */
983ae8c6e27Sflorian 		d1->count != d2->count ||
984ae8c6e27Sflorian 		d1->rrsig_count != d2->rrsig_count ||
985ae8c6e27Sflorian 		d1->trust != d2->trust ||
986ae8c6e27Sflorian 		d1->security != d2->security)
987ae8c6e27Sflorian 		return 0;
988ae8c6e27Sflorian 	t = d1->count + d1->rrsig_count;
989ae8c6e27Sflorian 	for(i=0; i<t; i++) {
990ae8c6e27Sflorian 		if(d1->rr_len[i] != d2->rr_len[i] ||
991ae8c6e27Sflorian 			/* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/
992ae8c6e27Sflorian 			memcmp(d1->rr_data[i], d2->rr_data[i],
993ae8c6e27Sflorian 				d1->rr_len[i]) != 0)
994ae8c6e27Sflorian 			return 0;
995ae8c6e27Sflorian 	}
996ae8c6e27Sflorian 	return 1;
997ae8c6e27Sflorian }
998ae8c6e27Sflorian 
999988ebc2dSflorian /** compare rrsets and sort canonically.  Compares rrset name, type, class.
1000988ebc2dSflorian  * return 0 if equal, +1 if x > y, and -1 if x < y.
1001988ebc2dSflorian  */
1002988ebc2dSflorian static int
1003988ebc2dSflorian rrset_canonical_sort_cmp(const void* x, const void* y)
1004988ebc2dSflorian {
1005988ebc2dSflorian 	struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x;
1006988ebc2dSflorian 	struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y;
1007988ebc2dSflorian 	int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname);
1008988ebc2dSflorian 	if(r != 0)
1009988ebc2dSflorian 		return r;
1010988ebc2dSflorian 	if(rrx->rk.type != rry->rk.type) {
1011988ebc2dSflorian 		if(ntohs(rrx->rk.type) > ntohs(rry->rk.type))
1012988ebc2dSflorian 			return 1;
1013988ebc2dSflorian 		else	return -1;
1014988ebc2dSflorian 	}
1015988ebc2dSflorian 	if(rrx->rk.rrset_class != rry->rk.rrset_class) {
1016988ebc2dSflorian 		if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class))
1017988ebc2dSflorian 			return 1;
1018988ebc2dSflorian 		else	return -1;
1019988ebc2dSflorian 	}
1020988ebc2dSflorian 	return 0;
1021988ebc2dSflorian }
1022988ebc2dSflorian 
1023ae8c6e27Sflorian int
1024ae8c6e27Sflorian reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region)
1025ae8c6e27Sflorian {
1026ae8c6e27Sflorian 	size_t i;
1027988ebc2dSflorian 	struct ub_packed_rrset_key** sorted_p, **sorted_q;
1028ae8c6e27Sflorian 	if(p->flags != q->flags ||
1029ae8c6e27Sflorian 		p->qdcount != q->qdcount ||
1030ae8c6e27Sflorian 		/* do not check TTL, this may differ */
1031ae8c6e27Sflorian 		/*
1032ae8c6e27Sflorian 		p->ttl != q->ttl ||
1033ae8c6e27Sflorian 		p->prefetch_ttl != q->prefetch_ttl ||
1034ae8c6e27Sflorian 		*/
1035ae8c6e27Sflorian 		p->security != q->security ||
1036ae8c6e27Sflorian 		p->an_numrrsets != q->an_numrrsets ||
1037ae8c6e27Sflorian 		p->ns_numrrsets != q->ns_numrrsets ||
1038ae8c6e27Sflorian 		p->ar_numrrsets != q->ar_numrrsets ||
1039ae8c6e27Sflorian 		p->rrset_count != q->rrset_count)
1040ae8c6e27Sflorian 		return 0;
1041988ebc2dSflorian 	/* sort the rrsets in the authority and additional sections before
1042988ebc2dSflorian 	 * compare, the query and answer sections are ordered in the sequence
1043988ebc2dSflorian 	 * they should have (eg. one after the other for aliases). */
1044988ebc2dSflorian 	sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init(
1045988ebc2dSflorian 		region, p->rrsets, sizeof(*sorted_p)*p->rrset_count);
1046988ebc2dSflorian 	if(!sorted_p) return 0;
1047988ebc2dSflorian 	log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <=
1048988ebc2dSflorian 		p->rrset_count);
1049988ebc2dSflorian 	qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets,
1050988ebc2dSflorian 		sizeof(*sorted_p), rrset_canonical_sort_cmp);
1051988ebc2dSflorian 	qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets,
1052988ebc2dSflorian 		sizeof(*sorted_p), rrset_canonical_sort_cmp);
1053988ebc2dSflorian 
1054988ebc2dSflorian 	sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init(
1055988ebc2dSflorian 		region, q->rrsets, sizeof(*sorted_q)*q->rrset_count);
1056988ebc2dSflorian 	if(!sorted_q) {
1057ae8c6e27Sflorian 		regional_free_all(region);
1058ae8c6e27Sflorian 		return 0;
1059ae8c6e27Sflorian 	}
1060988ebc2dSflorian 	log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <=
1061988ebc2dSflorian 		q->rrset_count);
1062988ebc2dSflorian 	qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets,
1063988ebc2dSflorian 		sizeof(*sorted_q), rrset_canonical_sort_cmp);
1064988ebc2dSflorian 	qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets,
1065988ebc2dSflorian 		sizeof(*sorted_q), rrset_canonical_sort_cmp);
1066988ebc2dSflorian 
1067988ebc2dSflorian 	/* compare the rrsets */
1068988ebc2dSflorian 	for(i=0; i<p->rrset_count; i++) {
1069988ebc2dSflorian 		if(!rrset_equal(sorted_p[i], sorted_q[i])) {
1070988ebc2dSflorian 			if(!rrset_canonical_equal(region, sorted_p[i],
1071988ebc2dSflorian 				sorted_q[i])) {
1072ae8c6e27Sflorian 				regional_free_all(region);
1073988ebc2dSflorian 				return 0;
1074ae8c6e27Sflorian 			}
1075ae8c6e27Sflorian 		}
1076988ebc2dSflorian 	}
1077988ebc2dSflorian 	regional_free_all(region);
1078ae8c6e27Sflorian 	return 1;
1079ae8c6e27Sflorian }
1080ae8c6e27Sflorian 
1081ae8c6e27Sflorian void
1082ae8c6e27Sflorian caps_strip_reply(struct reply_info* rep)
1083ae8c6e27Sflorian {
1084ae8c6e27Sflorian 	size_t i;
1085ae8c6e27Sflorian 	if(!rep) return;
1086ae8c6e27Sflorian 	/* see if message is a referral, in which case the additional and
1087ae8c6e27Sflorian 	 * NS record cannot be removed */
1088ae8c6e27Sflorian 	/* referrals have the AA flag unset (strict check, not elsewhere in
1089ae8c6e27Sflorian 	 * unbound, but for 0x20 this is very convenient). */
1090ae8c6e27Sflorian 	if(!(rep->flags&BIT_AA))
1091ae8c6e27Sflorian 		return;
1092ae8c6e27Sflorian 	/* remove the additional section from the reply */
1093ae8c6e27Sflorian 	if(rep->ar_numrrsets != 0) {
1094ae8c6e27Sflorian 		verbose(VERB_ALGO, "caps fallback: removing additional section");
1095ae8c6e27Sflorian 		rep->rrset_count -= rep->ar_numrrsets;
1096ae8c6e27Sflorian 		rep->ar_numrrsets = 0;
1097ae8c6e27Sflorian 	}
1098ae8c6e27Sflorian 	/* is there an NS set in the authority section to remove? */
1099ae8c6e27Sflorian 	/* the failure case (Cisco firewalls) only has one rrset in authsec */
1100ae8c6e27Sflorian 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
1101ae8c6e27Sflorian 		struct ub_packed_rrset_key* s = rep->rrsets[i];
1102ae8c6e27Sflorian 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) {
1103ae8c6e27Sflorian 			/* remove NS rrset and break from loop (loop limits
1104ae8c6e27Sflorian 			 * have changed) */
1105ae8c6e27Sflorian 			/* move last rrset into this position (there is no
1106ae8c6e27Sflorian 			 * additional section any more) */
1107ae8c6e27Sflorian 			verbose(VERB_ALGO, "caps fallback: removing NS rrset");
1108ae8c6e27Sflorian 			if(i < rep->rrset_count-1)
1109ae8c6e27Sflorian 				rep->rrsets[i]=rep->rrsets[rep->rrset_count-1];
1110ae8c6e27Sflorian 			rep->rrset_count --;
1111ae8c6e27Sflorian 			rep->ns_numrrsets --;
1112ae8c6e27Sflorian 			break;
1113ae8c6e27Sflorian 		}
1114ae8c6e27Sflorian 	}
1115ae8c6e27Sflorian }
1116ae8c6e27Sflorian 
1117ae8c6e27Sflorian int caps_failed_rcode(struct reply_info* rep)
1118ae8c6e27Sflorian {
1119ae8c6e27Sflorian 	return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
1120ae8c6e27Sflorian 		FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN);
1121ae8c6e27Sflorian }
1122ae8c6e27Sflorian 
1123ae8c6e27Sflorian void
1124ae8c6e27Sflorian iter_store_parentside_rrset(struct module_env* env,
1125ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset)
1126ae8c6e27Sflorian {
1127ae8c6e27Sflorian 	struct rrset_ref ref;
1128ae8c6e27Sflorian 	rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
1129ae8c6e27Sflorian 	if(!rrset) {
1130ae8c6e27Sflorian 		log_err("malloc failure in store_parentside_rrset");
1131ae8c6e27Sflorian 		return;
1132ae8c6e27Sflorian 	}
1133ae8c6e27Sflorian 	rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
1134ae8c6e27Sflorian 	rrset->entry.hash = rrset_key_hash(&rrset->rk);
1135ae8c6e27Sflorian 	ref.key = rrset;
1136ae8c6e27Sflorian 	ref.id = rrset->id;
1137ae8c6e27Sflorian 	/* ignore ret: if it was in the cache, ref updated */
1138ae8c6e27Sflorian 	(void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
1139ae8c6e27Sflorian }
1140ae8c6e27Sflorian 
1141ae8c6e27Sflorian /** fetch NS record from reply, if any */
1142ae8c6e27Sflorian static struct ub_packed_rrset_key*
1143ae8c6e27Sflorian reply_get_NS_rrset(struct reply_info* rep)
1144ae8c6e27Sflorian {
1145ae8c6e27Sflorian 	size_t i;
1146ae8c6e27Sflorian 	for(i=0; i<rep->rrset_count; i++) {
1147ae8c6e27Sflorian 		if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
1148ae8c6e27Sflorian 			return rep->rrsets[i];
1149ae8c6e27Sflorian 		}
1150ae8c6e27Sflorian 	}
1151ae8c6e27Sflorian 	return NULL;
1152ae8c6e27Sflorian }
1153ae8c6e27Sflorian 
1154ae8c6e27Sflorian void
1155ae8c6e27Sflorian iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
1156ae8c6e27Sflorian {
1157ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1158ae8c6e27Sflorian 	if(rrset) {
1159ae8c6e27Sflorian 		log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
1160ae8c6e27Sflorian 		iter_store_parentside_rrset(env, rrset);
1161ae8c6e27Sflorian 	}
1162ae8c6e27Sflorian }
1163ae8c6e27Sflorian 
1164ae8c6e27Sflorian void iter_store_parentside_neg(struct module_env* env,
1165ae8c6e27Sflorian         struct query_info* qinfo, struct reply_info* rep)
1166ae8c6e27Sflorian {
1167ae8c6e27Sflorian 	/* TTL: NS from referral in iq->deleg_msg,
1168ae8c6e27Sflorian 	 *      or first RR from iq->response,
1169ae8c6e27Sflorian 	 *      or servfail5secs if !iq->response */
1170ae8c6e27Sflorian 	time_t ttl = NORR_TTL;
1171ae8c6e27Sflorian 	struct ub_packed_rrset_key* neg;
1172ae8c6e27Sflorian 	struct packed_rrset_data* newd;
1173ae8c6e27Sflorian 	if(rep) {
1174ae8c6e27Sflorian 		struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1175ae8c6e27Sflorian 		if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
1176ae8c6e27Sflorian 		if(rrset) ttl = ub_packed_rrset_ttl(rrset);
1177ae8c6e27Sflorian 	}
1178ae8c6e27Sflorian 	/* create empty rrset to store */
1179ae8c6e27Sflorian 	neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
1180ae8c6e27Sflorian 	                sizeof(struct ub_packed_rrset_key));
1181ae8c6e27Sflorian 	if(!neg) {
1182ae8c6e27Sflorian 		log_err("out of memory in store_parentside_neg");
1183ae8c6e27Sflorian 		return;
1184ae8c6e27Sflorian 	}
1185ae8c6e27Sflorian 	memset(&neg->entry, 0, sizeof(neg->entry));
1186ae8c6e27Sflorian 	neg->entry.key = neg;
1187ae8c6e27Sflorian 	neg->rk.type = htons(qinfo->qtype);
1188ae8c6e27Sflorian 	neg->rk.rrset_class = htons(qinfo->qclass);
1189ae8c6e27Sflorian 	neg->rk.flags = 0;
1190ae8c6e27Sflorian 	neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
1191ae8c6e27Sflorian 		qinfo->qname_len);
1192ae8c6e27Sflorian 	if(!neg->rk.dname) {
1193ae8c6e27Sflorian 		log_err("out of memory in store_parentside_neg");
1194ae8c6e27Sflorian 		return;
1195ae8c6e27Sflorian 	}
1196ae8c6e27Sflorian 	neg->rk.dname_len = qinfo->qname_len;
1197ae8c6e27Sflorian 	neg->entry.hash = rrset_key_hash(&neg->rk);
1198ae8c6e27Sflorian 	newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
1199ae8c6e27Sflorian 		sizeof(struct packed_rrset_data) + sizeof(size_t) +
1200ae8c6e27Sflorian 		sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t));
1201ae8c6e27Sflorian 	if(!newd) {
1202ae8c6e27Sflorian 		log_err("out of memory in store_parentside_neg");
1203ae8c6e27Sflorian 		return;
1204ae8c6e27Sflorian 	}
1205ae8c6e27Sflorian 	neg->entry.data = newd;
1206ae8c6e27Sflorian 	newd->ttl = ttl;
1207ae8c6e27Sflorian 	/* entry must have one RR, otherwise not valid in cache.
1208ae8c6e27Sflorian 	 * put in one RR with empty rdata: those are ignored as nameserver */
1209ae8c6e27Sflorian 	newd->count = 1;
1210ae8c6e27Sflorian 	newd->rrsig_count = 0;
1211ae8c6e27Sflorian 	newd->trust = rrset_trust_ans_noAA;
1212ae8c6e27Sflorian 	newd->rr_len = (size_t*)((uint8_t*)newd +
1213ae8c6e27Sflorian 		sizeof(struct packed_rrset_data));
1214ae8c6e27Sflorian 	newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
1215ae8c6e27Sflorian 	packed_rrset_ptr_fixup(newd);
1216ae8c6e27Sflorian 	newd->rr_ttl[0] = newd->ttl;
1217ae8c6e27Sflorian 	sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
1218ae8c6e27Sflorian 	/* store it */
1219ae8c6e27Sflorian 	log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
1220ae8c6e27Sflorian 	iter_store_parentside_rrset(env, neg);
1221ae8c6e27Sflorian }
1222ae8c6e27Sflorian 
1223ae8c6e27Sflorian int
1224ae8c6e27Sflorian iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
1225ae8c6e27Sflorian 	struct regional* region, struct query_info* qinfo)
1226ae8c6e27Sflorian {
1227ae8c6e27Sflorian 	struct ub_packed_rrset_key* akey;
1228ae8c6e27Sflorian 	akey = rrset_cache_lookup(env->rrset_cache, dp->name,
1229ae8c6e27Sflorian 		dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
1230ae8c6e27Sflorian 		PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1231ae8c6e27Sflorian 	if(akey) {
1232ae8c6e27Sflorian 		log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
1233ae8c6e27Sflorian 		dp->has_parent_side_NS = 1;
1234ae8c6e27Sflorian 		/* and mark the new names as lame */
1235ae8c6e27Sflorian 		if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
1236ae8c6e27Sflorian 			lock_rw_unlock(&akey->entry.lock);
1237ae8c6e27Sflorian 			return 0;
1238ae8c6e27Sflorian 		}
1239ae8c6e27Sflorian 		lock_rw_unlock(&akey->entry.lock);
1240ae8c6e27Sflorian 	}
1241ae8c6e27Sflorian 	return 1;
1242ae8c6e27Sflorian }
1243ae8c6e27Sflorian 
1244ae8c6e27Sflorian int iter_lookup_parent_glue_from_cache(struct module_env* env,
1245ae8c6e27Sflorian         struct delegpt* dp, struct regional* region, struct query_info* qinfo)
1246ae8c6e27Sflorian {
1247ae8c6e27Sflorian 	struct ub_packed_rrset_key* akey;
1248ae8c6e27Sflorian 	struct delegpt_ns* ns;
1249ae8c6e27Sflorian 	size_t num = delegpt_count_targets(dp);
1250ae8c6e27Sflorian 	for(ns = dp->nslist; ns; ns = ns->next) {
1251ab256815Sflorian 		if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE)
1252ab256815Sflorian 			continue;
1253ab256815Sflorian 		ns->cache_lookup_count++;
1254ae8c6e27Sflorian 		/* get cached parentside A */
1255ae8c6e27Sflorian 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1256ae8c6e27Sflorian 			ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
1257ae8c6e27Sflorian 			PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1258ae8c6e27Sflorian 		if(akey) {
1259ae8c6e27Sflorian 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
1260ae8c6e27Sflorian 			ns->done_pside4 = 1;
1261ae8c6e27Sflorian 			/* a negative-cache-element has no addresses it adds */
12625a7d75e6Ssthen 			if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL))
1263ae8c6e27Sflorian 				log_err("malloc failure in lookup_parent_glue");
1264ae8c6e27Sflorian 			lock_rw_unlock(&akey->entry.lock);
1265ae8c6e27Sflorian 		}
1266ae8c6e27Sflorian 		/* get cached parentside AAAA */
1267ae8c6e27Sflorian 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1268ae8c6e27Sflorian 			ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
1269ae8c6e27Sflorian 			PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1270ae8c6e27Sflorian 		if(akey) {
1271ae8c6e27Sflorian 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
1272ae8c6e27Sflorian 			ns->done_pside6 = 1;
1273ae8c6e27Sflorian 			/* a negative-cache-element has no addresses it adds */
12745a7d75e6Ssthen 			if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL))
1275ae8c6e27Sflorian 				log_err("malloc failure in lookup_parent_glue");
1276ae8c6e27Sflorian 			lock_rw_unlock(&akey->entry.lock);
1277ae8c6e27Sflorian 		}
1278ae8c6e27Sflorian 	}
1279ae8c6e27Sflorian 	/* see if new (but lame) addresses have become available */
1280ae8c6e27Sflorian 	return delegpt_count_targets(dp) != num;
1281ae8c6e27Sflorian }
1282ae8c6e27Sflorian 
1283ae8c6e27Sflorian int
1284ae8c6e27Sflorian iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
1285ae8c6e27Sflorian 	uint16_t* c)
1286ae8c6e27Sflorian {
1287ae8c6e27Sflorian 	uint16_t c1 = *c, c2 = *c;
1288096314feSflorian 	int r1, r2;
1289096314feSflorian 	int nolock = 1;
1290096314feSflorian 
1291096314feSflorian 	/* prelock both forwards and hints for atomic read. */
1292096314feSflorian 	lock_rw_rdlock(&fwd->lock);
1293096314feSflorian 	lock_rw_rdlock(&hints->lock);
1294096314feSflorian 	r1 = hints_next_root(hints, &c1, nolock);
1295096314feSflorian 	r2 = forwards_next_root(fwd, &c2, nolock);
1296096314feSflorian 	lock_rw_unlock(&fwd->lock);
1297096314feSflorian 	lock_rw_unlock(&hints->lock);
1298096314feSflorian 
1299ae8c6e27Sflorian 	if(!r1 && !r2) /* got none, end of list */
1300ae8c6e27Sflorian 		return 0;
1301ae8c6e27Sflorian 	else if(!r1) /* got one, return that */
1302ae8c6e27Sflorian 		*c = c2;
1303ae8c6e27Sflorian 	else if(!r2)
1304ae8c6e27Sflorian 		*c = c1;
1305ae8c6e27Sflorian 	else if(c1 < c2) /* got both take smallest */
1306ae8c6e27Sflorian 		*c = c1;
1307ae8c6e27Sflorian 	else	*c = c2;
1308ae8c6e27Sflorian 	return 1;
1309ae8c6e27Sflorian }
1310ae8c6e27Sflorian 
1311ae8c6e27Sflorian void
1312ae8c6e27Sflorian iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
1313ae8c6e27Sflorian {
1314ae8c6e27Sflorian 	/* Only the DS record for the delegation itself is expected.
1315ae8c6e27Sflorian 	 * We allow DS for everything between the bailiwick and the
1316ae8c6e27Sflorian 	 * zonecut, thus DS records must be at or above the zonecut.
1317ae8c6e27Sflorian 	 * And the DS records must be below the server authority zone.
1318ae8c6e27Sflorian 	 * The answer section is already scrubbed. */
1319ae8c6e27Sflorian 	size_t i = msg->rep->an_numrrsets;
1320ae8c6e27Sflorian 	while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
1321ae8c6e27Sflorian 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1322ae8c6e27Sflorian 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
1323ae8c6e27Sflorian 			(!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
1324ae8c6e27Sflorian 			|| query_dname_compare(z, s->rk.dname) == 0)) {
1325ae8c6e27Sflorian 			log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
1326ae8c6e27Sflorian 				s->rk.dname, ntohs(s->rk.type),
1327ae8c6e27Sflorian 				ntohs(s->rk.rrset_class));
1328ae8c6e27Sflorian 			memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
1329ae8c6e27Sflorian 				sizeof(struct ub_packed_rrset_key*) *
1330ae8c6e27Sflorian 				(msg->rep->rrset_count-i-1));
1331ae8c6e27Sflorian 			msg->rep->ns_numrrsets--;
1332ae8c6e27Sflorian 			msg->rep->rrset_count--;
1333ae8c6e27Sflorian 			/* stay at same i, but new record */
1334ae8c6e27Sflorian 			continue;
1335ae8c6e27Sflorian 		}
1336ae8c6e27Sflorian 		i++;
1337ae8c6e27Sflorian 	}
1338ae8c6e27Sflorian }
1339ae8c6e27Sflorian 
13409b465e50Sflorian void
13419b465e50Sflorian iter_scrub_nxdomain(struct dns_msg* msg)
13429b465e50Sflorian {
13439b465e50Sflorian 	if(msg->rep->an_numrrsets == 0)
13449b465e50Sflorian 		return;
13459b465e50Sflorian 
13469b465e50Sflorian 	memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets,
13479b465e50Sflorian 		sizeof(struct ub_packed_rrset_key*) *
13489b465e50Sflorian 		(msg->rep->rrset_count-msg->rep->an_numrrsets));
13499b465e50Sflorian 	msg->rep->rrset_count -= msg->rep->an_numrrsets;
13509b465e50Sflorian 	msg->rep->an_numrrsets = 0;
13519b465e50Sflorian }
13529b465e50Sflorian 
1353a1a7ba80Sflorian void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry)
1354ae8c6e27Sflorian {
1355ae8c6e27Sflorian 	struct delegpt_addr* a;
1356ae8c6e27Sflorian 	for(a=dp->target_list; a; a = a->next_target) {
1357a1a7ba80Sflorian 		if(a->attempts >= outbound_msg_retry) {
1358ae8c6e27Sflorian 			/* add back to result list */
1359d500c338Sflorian 			delegpt_add_to_result_list(dp, a);
1360ae8c6e27Sflorian 		}
1361ae8c6e27Sflorian 		if(a->attempts > d)
1362ae8c6e27Sflorian 			a->attempts -= d;
1363ae8c6e27Sflorian 		else a->attempts = 0;
1364ae8c6e27Sflorian 	}
1365ae8c6e27Sflorian }
1366ae8c6e27Sflorian 
1367a1a7ba80Sflorian void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old,
1368a1a7ba80Sflorian 	int outbound_msg_retry)
1369ae8c6e27Sflorian {
1370ae8c6e27Sflorian 	struct delegpt_addr* a, *o, *prev;
1371ae8c6e27Sflorian 	for(a=dp->target_list; a; a = a->next_target) {
1372ae8c6e27Sflorian 		o = delegpt_find_addr(old, &a->addr, a->addrlen);
1373ae8c6e27Sflorian 		if(o) {
1374ae8c6e27Sflorian 			log_addr(VERB_ALGO, "copy attempt count previous dp",
1375ae8c6e27Sflorian 				&a->addr, a->addrlen);
1376ae8c6e27Sflorian 			a->attempts = o->attempts;
1377ae8c6e27Sflorian 		}
1378ae8c6e27Sflorian 	}
1379ae8c6e27Sflorian 	prev = NULL;
1380ae8c6e27Sflorian 	a = dp->usable_list;
1381ae8c6e27Sflorian 	while(a) {
1382a1a7ba80Sflorian 		if(a->attempts >= outbound_msg_retry) {
1383ae8c6e27Sflorian 			log_addr(VERB_ALGO, "remove from usable list dp",
1384ae8c6e27Sflorian 				&a->addr, a->addrlen);
1385ae8c6e27Sflorian 			/* remove from result list */
1386ae8c6e27Sflorian 			if(prev)
1387ae8c6e27Sflorian 				prev->next_usable = a->next_usable;
1388ae8c6e27Sflorian 			else	dp->usable_list = a->next_usable;
1389ae8c6e27Sflorian 			/* prev stays the same */
1390ae8c6e27Sflorian 			a = a->next_usable;
1391ae8c6e27Sflorian 			continue;
1392ae8c6e27Sflorian 		}
1393ae8c6e27Sflorian 		prev = a;
1394ae8c6e27Sflorian 		a = a->next_usable;
1395ae8c6e27Sflorian 	}
1396ae8c6e27Sflorian }
1397ae8c6e27Sflorian 
1398ae8c6e27Sflorian int
1399ae8c6e27Sflorian iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
1400ae8c6e27Sflorian {
1401ae8c6e27Sflorian 	/* if for query example.com, there is example.com SOA or a subdomain
1402ae8c6e27Sflorian 	 * of example.com, then we are too low and need to fetch NS. */
1403ae8c6e27Sflorian 	size_t i;
1404ae8c6e27Sflorian 	/* if we have a DNAME or CNAME we are probably wrong */
1405ae8c6e27Sflorian 	/* if we have a qtype DS in the answer section, its fine */
1406ae8c6e27Sflorian 	for(i=0; i < msg->rep->an_numrrsets; i++) {
1407ae8c6e27Sflorian 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1408ae8c6e27Sflorian 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
1409ae8c6e27Sflorian 			ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
1410ae8c6e27Sflorian 			/* not the right answer, maybe too low, check the
1411ae8c6e27Sflorian 			 * RRSIG signer name (if there is any) for a hint
1412ae8c6e27Sflorian 			 * that it is from the dp zone anyway */
1413ae8c6e27Sflorian 			uint8_t* sname;
1414ae8c6e27Sflorian 			size_t slen;
1415ae8c6e27Sflorian 			val_find_rrset_signer(s, &sname, &slen);
1416ae8c6e27Sflorian 			if(sname && query_dname_compare(dp->name, sname)==0)
1417ae8c6e27Sflorian 				return 0; /* it is fine, from the right dp */
1418ae8c6e27Sflorian 			return 1;
1419ae8c6e27Sflorian 		}
1420ae8c6e27Sflorian 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
1421ae8c6e27Sflorian 			return 0; /* fine, we have a DS record */
1422ae8c6e27Sflorian 	}
1423ae8c6e27Sflorian 	for(i=msg->rep->an_numrrsets;
1424ae8c6e27Sflorian 		i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
1425ae8c6e27Sflorian 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1426ae8c6e27Sflorian 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1427ae8c6e27Sflorian 			if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
1428ae8c6e27Sflorian 				return 1; /* point is too low */
1429ae8c6e27Sflorian 			if(query_dname_compare(s->rk.dname, dp->name)==0)
1430ae8c6e27Sflorian 				return 0; /* right dp */
1431ae8c6e27Sflorian 		}
1432ae8c6e27Sflorian 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1433ae8c6e27Sflorian 			ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1434ae8c6e27Sflorian 			uint8_t* sname;
1435ae8c6e27Sflorian 			size_t slen;
1436ae8c6e27Sflorian 			val_find_rrset_signer(s, &sname, &slen);
1437ae8c6e27Sflorian 			if(sname && query_dname_compare(dp->name, sname)==0)
1438ae8c6e27Sflorian 				return 0; /* it is fine, from the right dp */
1439ae8c6e27Sflorian 			return 1;
1440ae8c6e27Sflorian 		}
1441ae8c6e27Sflorian 	}
1442ae8c6e27Sflorian 	/* we do not know */
1443ae8c6e27Sflorian 	return 1;
1444ae8c6e27Sflorian }
1445ae8c6e27Sflorian 
1446ae8c6e27Sflorian int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
1447ae8c6e27Sflorian {
1448ae8c6e27Sflorian 	/* no delegation point, do not see how we can go down,
1449ae8c6e27Sflorian 	 * robust check, it should really exist */
1450ae8c6e27Sflorian 	if(!dp) return 0;
1451ae8c6e27Sflorian 
1452ae8c6e27Sflorian 	/* see if dp equals the qname, then we cannot go down further */
1453ae8c6e27Sflorian 	if(query_dname_compare(qinfo->qname, dp->name) == 0)
1454ae8c6e27Sflorian 		return 0;
1455ae8c6e27Sflorian 	/* if dp is one label above the name we also cannot go down further */
1456ae8c6e27Sflorian 	if(dname_count_labels(qinfo->qname) == dp->namelabs+1)
1457ae8c6e27Sflorian 		return 0;
1458ae8c6e27Sflorian 	return 1;
1459ae8c6e27Sflorian }
1460ae8c6e27Sflorian 
1461ae8c6e27Sflorian int
1462411c5950Sflorian iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf,
1463096314feSflorian 	uint8_t** retdpname, size_t* retdpnamelen, uint8_t* dpname_storage,
1464096314feSflorian 	size_t dpname_storage_len)
1465ae8c6e27Sflorian {
1466ae8c6e27Sflorian 	struct iter_hints_stub *stub;
1467ae8c6e27Sflorian 	struct delegpt *dp;
1468096314feSflorian 	int nolock = 1;
1469ae8c6e27Sflorian 
1470ae8c6e27Sflorian 	/* Check for stub. */
1471096314feSflorian 	/* Lock both forwards and hints for atomic read. */
1472096314feSflorian 	lock_rw_rdlock(&qstate->env->fwds->lock);
1473096314feSflorian 	lock_rw_rdlock(&qstate->env->hints->lock);
1474ae8c6e27Sflorian 	stub = hints_lookup_stub(qstate->env->hints, qinf->qname,
1475096314feSflorian 	    qinf->qclass, NULL, nolock);
1476096314feSflorian 	dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass,
1477096314feSflorian 		nolock);
1478ae8c6e27Sflorian 
1479ae8c6e27Sflorian 	/* see if forward or stub is more pertinent */
1480ae8c6e27Sflorian 	if(stub && stub->dp && dp) {
1481ae8c6e27Sflorian 		if(dname_strict_subdomain(dp->name, dp->namelabs,
1482ae8c6e27Sflorian 			stub->dp->name, stub->dp->namelabs)) {
1483ae8c6e27Sflorian 			stub = NULL; /* ignore stub, forward is lower */
1484ae8c6e27Sflorian 		} else {
1485ae8c6e27Sflorian 			dp = NULL; /* ignore forward, stub is lower */
1486ae8c6e27Sflorian 		}
1487ae8c6e27Sflorian 	}
1488ae8c6e27Sflorian 
1489ae8c6e27Sflorian 	/* check stub */
1490ae8c6e27Sflorian 	if (stub != NULL && stub->dp != NULL) {
1491096314feSflorian 		int stub_no_cache = stub->dp->no_cache;
1492096314feSflorian 		lock_rw_unlock(&qstate->env->fwds->lock);
1493096314feSflorian 		if(stub_no_cache) {
1494ae8c6e27Sflorian 			char qname[255+1];
1495ae8c6e27Sflorian 			char dpname[255+1];
1496ae8c6e27Sflorian 			dname_str(qinf->qname, qname);
1497ae8c6e27Sflorian 			dname_str(stub->dp->name, dpname);
1498ae8c6e27Sflorian 			verbose(VERB_ALGO, "stub for %s %s has no_cache", qname, dpname);
1499ae8c6e27Sflorian 		}
1500411c5950Sflorian 		if(retdpname) {
1501096314feSflorian 			if(stub->dp->namelen > dpname_storage_len) {
1502096314feSflorian 				verbose(VERB_ALGO, "no cache stub dpname too long");
1503096314feSflorian 				lock_rw_unlock(&qstate->env->hints->lock);
1504096314feSflorian 				*retdpname = NULL;
1505096314feSflorian 				*retdpnamelen = 0;
1506096314feSflorian 				return stub_no_cache;
1507096314feSflorian 			}
1508096314feSflorian 			memmove(dpname_storage, stub->dp->name,
1509096314feSflorian 				stub->dp->namelen);
1510096314feSflorian 			*retdpname = dpname_storage;
1511411c5950Sflorian 			*retdpnamelen = stub->dp->namelen;
1512411c5950Sflorian 		}
1513096314feSflorian 		lock_rw_unlock(&qstate->env->hints->lock);
1514096314feSflorian 		return stub_no_cache;
1515ae8c6e27Sflorian 	}
1516ae8c6e27Sflorian 
1517ae8c6e27Sflorian 	/* Check for forward. */
1518ae8c6e27Sflorian 	if (dp) {
1519096314feSflorian 		int dp_no_cache = dp->no_cache;
1520096314feSflorian 		lock_rw_unlock(&qstate->env->hints->lock);
1521096314feSflorian 		if(dp_no_cache) {
1522ae8c6e27Sflorian 			char qname[255+1];
1523ae8c6e27Sflorian 			char dpname[255+1];
1524ae8c6e27Sflorian 			dname_str(qinf->qname, qname);
1525ae8c6e27Sflorian 			dname_str(dp->name, dpname);
1526ae8c6e27Sflorian 			verbose(VERB_ALGO, "forward for %s %s has no_cache", qname, dpname);
1527ae8c6e27Sflorian 		}
1528411c5950Sflorian 		if(retdpname) {
1529096314feSflorian 			if(dp->namelen > dpname_storage_len) {
1530096314feSflorian 				verbose(VERB_ALGO, "no cache dpname too long");
1531096314feSflorian 				lock_rw_unlock(&qstate->env->fwds->lock);
1532096314feSflorian 				*retdpname = NULL;
1533096314feSflorian 				*retdpnamelen = 0;
1534096314feSflorian 				return dp_no_cache;
1535096314feSflorian 			}
1536096314feSflorian 			memmove(dpname_storage, dp->name, dp->namelen);
1537096314feSflorian 			*retdpname = dpname_storage;
1538411c5950Sflorian 			*retdpnamelen = dp->namelen;
1539411c5950Sflorian 		}
1540096314feSflorian 		lock_rw_unlock(&qstate->env->fwds->lock);
1541096314feSflorian 		return dp_no_cache;
1542ae8c6e27Sflorian 	}
1543096314feSflorian 	lock_rw_unlock(&qstate->env->fwds->lock);
1544096314feSflorian 	lock_rw_unlock(&qstate->env->hints->lock);
1545411c5950Sflorian 	if(retdpname) {
1546411c5950Sflorian 		*retdpname = NULL;
1547411c5950Sflorian 		*retdpnamelen = 0;
1548411c5950Sflorian 	}
1549ae8c6e27Sflorian 	return 0;
1550ae8c6e27Sflorian }
1551411c5950Sflorian 
1552411c5950Sflorian void iterator_set_ip46_support(struct module_stack* mods,
1553411c5950Sflorian 	struct module_env* env, struct outside_network* outnet)
1554411c5950Sflorian {
1555411c5950Sflorian 	int m = modstack_find(mods, "iterator");
1556411c5950Sflorian 	struct iter_env* ie = NULL;
1557411c5950Sflorian 	if(m == -1)
1558411c5950Sflorian 		return;
1559411c5950Sflorian 	ie = (struct iter_env*)env->modinfo[m];
1560411c5950Sflorian 	if(outnet->pending == NULL)
1561411c5950Sflorian 		return; /* we are in testbound, no rbtree for UDP */
1562411c5950Sflorian 	if(outnet->num_ip4 == 0)
1563411c5950Sflorian 		ie->supports_ipv4 = 0;
1564411c5950Sflorian 	if(outnet->num_ip6 == 0)
1565411c5950Sflorian 		ie->supports_ipv6 = 0;
1566411c5950Sflorian }
1567