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