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