xref: /freebsd/contrib/unbound/iterator/iter_utils.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
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 
67 /** time when nameserver glue is said to be 'recent' */
68 #define SUSPICION_RECENT_EXPIRY 86400
69 /** penalty to validation failed blacklisted IPs */
70 #define BLACKLIST_PENALTY (USEFUL_SERVER_TOP_TIMEOUT*4)
71 
72 /** fillup fetch policy array */
73 static void
74 fetch_fill(struct iter_env* ie, const char* str)
75 {
76 	char* s = (char*)str, *e;
77 	int i;
78 	for(i=0; i<ie->max_dependency_depth+1; i++) {
79 		ie->target_fetch_policy[i] = strtol(s, &e, 10);
80 		if(s == e)
81 			fatal_exit("cannot parse fetch policy number %s", s);
82 		s = e;
83 	}
84 }
85 
86 /** Read config string that represents the target fetch policy */
87 static int
88 read_fetch_policy(struct iter_env* ie, const char* str)
89 {
90 	int count = cfg_count_numbers(str);
91 	if(count < 1) {
92 		log_err("Cannot parse target fetch policy: \"%s\"", str);
93 		return 0;
94 	}
95 	ie->max_dependency_depth = count - 1;
96 	ie->target_fetch_policy = (int*)calloc(
97 		(size_t)ie->max_dependency_depth+1, sizeof(int));
98 	if(!ie->target_fetch_policy) {
99 		log_err("alloc fetch policy: out of memory");
100 		return 0;
101 	}
102 	fetch_fill(ie, str);
103 	return 1;
104 }
105 
106 int
107 iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
108 {
109 	int i;
110 	/* target fetch policy */
111 	if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
112 		return 0;
113 	for(i=0; i<iter_env->max_dependency_depth+1; i++)
114 		verbose(VERB_QUERY, "target fetch policy for level %d is %d",
115 			i, iter_env->target_fetch_policy[i]);
116 
117 	if(!iter_env->donotq)
118 		iter_env->donotq = donotq_create();
119 	if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
120 		log_err("Could not set donotqueryaddresses");
121 		return 0;
122 	}
123 	if(!iter_env->priv)
124 		iter_env->priv = priv_create();
125 	if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
126 		log_err("Could not set private addresses");
127 		return 0;
128 	}
129 	iter_env->supports_ipv6 = cfg->do_ip6;
130 	iter_env->supports_ipv4 = cfg->do_ip4;
131 	return 1;
132 }
133 
134 /** filter out unsuitable targets
135  * @param iter_env: iterator environment with ipv6-support flag.
136  * @param env: module environment with infra cache.
137  * @param name: zone name
138  * @param namelen: length of name
139  * @param qtype: query type (host order).
140  * @param now: current time
141  * @param a: address in delegation point we are examining.
142  * @return an integer that signals the target suitability.
143  *	as follows:
144  *	-1: The address should be omitted from the list.
145  *	    Because:
146  *		o The address is bogus (DNSSEC validation failure).
147  *		o Listed as donotquery
148  *		o is ipv6 but no ipv6 support (in operating system).
149  *		o is ipv4 but no ipv4 support (in operating system).
150  *		o is lame
151  *	Otherwise, an rtt in milliseconds.
152  *	0 .. USEFUL_SERVER_TOP_TIMEOUT-1
153  *		The roundtrip time timeout estimate. less than 2 minutes.
154  *		Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
155  *		values 0 .. 49 are not used, unless that is changed.
156  *	USEFUL_SERVER_TOP_TIMEOUT
157  *		This value exactly is given for unresponsive blacklisted.
158  *	USEFUL_SERVER_TOP_TIMEOUT+1
159  *		For non-blacklisted servers: huge timeout, but has traffic.
160  *	USEFUL_SERVER_TOP_TIMEOUT*1 ..
161  *		parent-side lame servers get this penalty. A dispreferential
162  *		server. (lame in delegpt).
163  *	USEFUL_SERVER_TOP_TIMEOUT*2 ..
164  *		dnsseclame servers get penalty
165  *	USEFUL_SERVER_TOP_TIMEOUT*3 ..
166  *		recursion lame servers get penalty
167  *	UNKNOWN_SERVER_NICENESS
168  *		If no information is known about the server, this is
169  *		returned. 376 msec or so.
170  *	+BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
171  *
172  * When a final value is chosen that is dnsseclame ; dnsseclameness checking
173  * is turned off (so we do not discard the reply).
174  * When a final value is chosen that is recursionlame; RD bit is set on query.
175  * Because of the numbers this means recursionlame also have dnssec lameness
176  * checking turned off.
177  */
178 static int
179 iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
180 	uint8_t* name, size_t namelen, uint16_t qtype, uint32_t now,
181 	struct delegpt_addr* a)
182 {
183 	int rtt, lame, reclame, dnsseclame;
184 	if(a->bogus)
185 		return -1; /* address of server is bogus */
186 	if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
187 		log_addr(VERB_ALGO, "skip addr on the donotquery list",
188 			&a->addr, a->addrlen);
189 		return -1; /* server is on the donotquery list */
190 	}
191 	if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
192 		return -1; /* there is no ip6 available */
193 	}
194 	if(!iter_env->supports_ipv4 && !addr_is_ip6(&a->addr, a->addrlen)) {
195 		return -1; /* there is no ip4 available */
196 	}
197 	/* check lameness - need zone , class info */
198 	if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
199 		name, namelen, qtype, &lame, &dnsseclame, &reclame,
200 		&rtt, now)) {
201 		log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
202 		verbose(VERB_ALGO, "   rtt=%d%s%s%s%s", rtt,
203 			lame?" LAME":"",
204 			dnsseclame?" DNSSEC_LAME":"",
205 			reclame?" REC_LAME":"",
206 			a->lame?" ADDR_LAME":"");
207 		if(lame)
208 			return -1; /* server is lame */
209 		else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
210 			/* server is unresponsive,
211 			 * we used to return TOP_TIMOUT, but fairly useless,
212 			 * because if == TOP_TIMEOUT is dropped because
213 			 * blacklisted later, instead, remove it here, so
214 			 * other choices (that are not blacklisted) can be
215 			 * tried */
216 			return -1;
217 		/* select remainder from worst to best */
218 		else if(reclame)
219 			return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
220 		else if(dnsseclame )
221 			return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
222 		else if(a->lame)
223 			return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
224 		else	return rtt;
225 	}
226 	/* no server information present */
227 	if(a->lame)
228 		return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
229 	return UNKNOWN_SERVER_NICENESS;
230 }
231 
232 /** lookup RTT information, and also store fastest rtt (if any) */
233 static int
234 iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
235 	uint8_t* name, size_t namelen, uint16_t qtype, uint32_t now,
236 	struct delegpt* dp, int* best_rtt, struct sock_list* blacklist)
237 {
238 	int got_it = 0;
239 	struct delegpt_addr* a;
240 	if(dp->bogus)
241 		return 0; /* NS bogus, all bogus, nothing found */
242 	for(a=dp->result_list; a; a = a->next_result) {
243 		a->sel_rtt = iter_filter_unsuitable(iter_env, env,
244 			name, namelen, qtype, now, a);
245 		if(a->sel_rtt != -1) {
246 			if(sock_list_find(blacklist, &a->addr, a->addrlen))
247 				a->sel_rtt += BLACKLIST_PENALTY;
248 
249 			if(!got_it) {
250 				*best_rtt = a->sel_rtt;
251 				got_it = 1;
252 			} else if(a->sel_rtt < *best_rtt) {
253 				*best_rtt = a->sel_rtt;
254 			}
255 		}
256 	}
257 	return got_it;
258 }
259 
260 /** filter the addres list, putting best targets at front,
261  * returns number of best targets (or 0, no suitable targets) */
262 static int
263 iter_filter_order(struct iter_env* iter_env, struct module_env* env,
264 	uint8_t* name, size_t namelen, uint16_t qtype, uint32_t now,
265 	struct delegpt* dp, int* selected_rtt, int open_target,
266 	struct sock_list* blacklist)
267 {
268 	int got_num = 0, low_rtt = 0, swap_to_front;
269 	struct delegpt_addr* a, *n, *prev=NULL;
270 
271 	/* fillup sel_rtt and find best rtt in the bunch */
272 	got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
273 		&low_rtt, blacklist);
274 	if(got_num == 0)
275 		return 0;
276 	if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
277 		(delegpt_count_missing_targets(dp) > 0 || open_target > 0)) {
278 		verbose(VERB_ALGO, "Bad choices, trying to get more choice");
279 		return 0; /* we want more choice. The best choice is a bad one.
280 			     return 0 to force the caller to fetch more */
281 	}
282 
283 	got_num = 0;
284 	a = dp->result_list;
285 	while(a) {
286 		/* skip unsuitable targets */
287 		if(a->sel_rtt == -1) {
288 			prev = a;
289 			a = a->next_result;
290 			continue;
291 		}
292 		/* classify the server address and determine what to do */
293 		swap_to_front = 0;
294 		if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= RTT_BAND) {
295 			got_num++;
296 			swap_to_front = 1;
297 		} else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=RTT_BAND) {
298 			got_num++;
299 			swap_to_front = 1;
300 		}
301 		/* swap to front if necessary, or move to next result */
302 		if(swap_to_front && prev) {
303 			n = a->next_result;
304 			prev->next_result = n;
305 			a->next_result = dp->result_list;
306 			dp->result_list = a;
307 			a = n;
308 		} else {
309 			prev = a;
310 			a = a->next_result;
311 		}
312 	}
313 	*selected_rtt = low_rtt;
314 	return got_num;
315 }
316 
317 struct delegpt_addr*
318 iter_server_selection(struct iter_env* iter_env,
319 	struct module_env* env, struct delegpt* dp,
320 	uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
321 	int* chase_to_rd, int open_target, struct sock_list* blacklist)
322 {
323 	int sel;
324 	int selrtt;
325 	struct delegpt_addr* a, *prev;
326 	int num = iter_filter_order(iter_env, env, name, namelen, qtype,
327 		*env->now, dp, &selrtt, open_target, blacklist);
328 
329 	if(num == 0)
330 		return NULL;
331 	verbose(VERB_ALGO, "selrtt %d", selrtt);
332 	if(selrtt > BLACKLIST_PENALTY) {
333 		if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
334 			verbose(VERB_ALGO, "chase to "
335 				"blacklisted recursion lame server");
336 			*chase_to_rd = 1;
337 		}
338 		if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
339 			verbose(VERB_ALGO, "chase to "
340 				"blacklisted dnssec lame server");
341 			*dnssec_lame = 1;
342 		}
343 	} else {
344 		if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
345 			verbose(VERB_ALGO, "chase to recursion lame server");
346 			*chase_to_rd = 1;
347 		}
348 		if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
349 			verbose(VERB_ALGO, "chase to dnssec lame server");
350 			*dnssec_lame = 1;
351 		}
352 		if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
353 			verbose(VERB_ALGO, "chase to blacklisted lame server");
354 			return NULL;
355 		}
356 	}
357 
358 	if(num == 1) {
359 		a = dp->result_list;
360 		if(++a->attempts < OUTBOUND_MSG_RETRY)
361 			return a;
362 		dp->result_list = a->next_result;
363 		return a;
364 	}
365 
366 	/* randomly select a target from the list */
367 	log_assert(num > 1);
368 	/* grab secure random number, to pick unexpected server.
369 	 * also we need it to be threadsafe. */
370 	sel = ub_random_max(env->rnd, num);
371 	a = dp->result_list;
372 	prev = NULL;
373 	while(sel > 0 && a) {
374 		prev = a;
375 		a = a->next_result;
376 		sel--;
377 	}
378 	if(!a)  /* robustness */
379 		return NULL;
380 	if(++a->attempts < OUTBOUND_MSG_RETRY)
381 		return a;
382 	/* remove it from the delegation point result list */
383 	if(prev)
384 		prev->next_result = a->next_result;
385 	else	dp->result_list = a->next_result;
386 	return a;
387 }
388 
389 struct dns_msg*
390 dns_alloc_msg(ldns_buffer* pkt, struct msg_parse* msg,
391 	struct regional* region)
392 {
393 	struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
394 		sizeof(struct dns_msg));
395 	if(!m)
396 		return NULL;
397 	memset(m, 0, sizeof(*m));
398 	if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
399 		log_err("malloc failure: allocating incoming dns_msg");
400 		return NULL;
401 	}
402 	return m;
403 }
404 
405 struct dns_msg*
406 dns_copy_msg(struct dns_msg* from, struct regional* region)
407 {
408 	struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
409 		sizeof(struct dns_msg));
410 	if(!m)
411 		return NULL;
412 	m->qinfo = from->qinfo;
413 	if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
414 		from->qinfo.qname_len)))
415 		return NULL;
416 	if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
417 		return NULL;
418 	return m;
419 }
420 
421 void
422 iter_dns_store(struct module_env* env, struct query_info* msgqinf,
423 	struct reply_info* msgrep, int is_referral, uint32_t leeway, int pside,
424 	struct regional* region)
425 {
426 	if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
427 		pside, region))
428 		log_err("out of memory: cannot store data in cache");
429 }
430 
431 int
432 iter_ns_probability(struct ub_randstate* rnd, int n, int m)
433 {
434 	int sel;
435 	if(n == m) /* 100% chance */
436 		return 1;
437 	/* we do not need secure random numbers here, but
438 	 * we do need it to be threadsafe, so we use this */
439 	sel = ub_random_max(rnd, m);
440 	return (sel < n);
441 }
442 
443 /** detect dependency cycle for query and target */
444 static int
445 causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
446 	uint16_t t, uint16_t c)
447 {
448 	struct query_info qinf;
449 	qinf.qname = name;
450 	qinf.qname_len = namelen;
451 	qinf.qtype = t;
452 	qinf.qclass = c;
453 	fptr_ok(fptr_whitelist_modenv_detect_cycle(
454 		qstate->env->detect_cycle));
455 	return (*qstate->env->detect_cycle)(qstate, &qinf,
456 		(uint16_t)(BIT_RD|BIT_CD), qstate->is_priming);
457 }
458 
459 void
460 iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
461 {
462 	struct delegpt_ns* ns;
463 	for(ns = dp->nslist; ns; ns = ns->next) {
464 		if(ns->resolved)
465 			continue;
466 		/* see if this ns as target causes dependency cycle */
467 		if(causes_cycle(qstate, ns->name, ns->namelen,
468 			LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
469 		   causes_cycle(qstate, ns->name, ns->namelen,
470 			LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
471 			log_nametypeclass(VERB_QUERY, "skipping target due "
472 			 	"to dependency cycle (harden-glue: no may "
473 				"fix some of the cycles)",
474 				ns->name, LDNS_RR_TYPE_A,
475 				qstate->qinfo.qclass);
476 			ns->resolved = 1;
477 		}
478 	}
479 }
480 
481 void
482 iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
483 {
484 	struct delegpt_ns* ns;
485 	for(ns = dp->nslist; ns; ns = ns->next) {
486 		if(ns->done_pside4 && ns->done_pside6)
487 			continue;
488 		/* see if this ns as target causes dependency cycle */
489 		if(causes_cycle(qstate, ns->name, ns->namelen,
490 			LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
491 			log_nametypeclass(VERB_QUERY, "skipping target due "
492 			 	"to dependency cycle", ns->name,
493 				LDNS_RR_TYPE_A, qstate->qinfo.qclass);
494 			ns->done_pside4 = 1;
495 		}
496 		if(causes_cycle(qstate, ns->name, ns->namelen,
497 			LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
498 			log_nametypeclass(VERB_QUERY, "skipping target due "
499 			 	"to dependency cycle", ns->name,
500 				LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
501 			ns->done_pside6 = 1;
502 		}
503 	}
504 }
505 
506 int
507 iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
508 	struct delegpt* dp)
509 {
510 	struct delegpt_ns* ns;
511 	/* check:
512 	 *      o RD qflag is on.
513 	 *      o no addresses are provided.
514 	 *      o all NS items are required glue.
515 	 * OR
516 	 *      o RD qflag is on.
517 	 *      o no addresses are provided.
518 	 *      o the query is for one of the nameservers in dp,
519 	 *        and that nameserver is a glue-name for this dp.
520 	 */
521 	if(!(qflags&BIT_RD))
522 		return 0;
523 	/* either available or unused targets */
524 	if(dp->usable_list || dp->result_list)
525 		return 0;
526 
527 	/* see if query is for one of the nameservers, which is glue */
528 	if( (qinfo->qtype == LDNS_RR_TYPE_A ||
529 		qinfo->qtype == LDNS_RR_TYPE_AAAA) &&
530 		dname_subdomain_c(qinfo->qname, dp->name) &&
531 		delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
532 		return 1;
533 
534 	for(ns = dp->nslist; ns; ns = ns->next) {
535 		if(ns->resolved) /* skip failed targets */
536 			continue;
537 		if(!dname_subdomain_c(ns->name, dp->name))
538 			return 0; /* one address is not required glue */
539 	}
540 	return 1;
541 }
542 
543 int
544 iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
545         struct dns_msg* msg, uint16_t dclass)
546 {
547 	struct trust_anchor* a;
548 	/* information not available, !env->anchors can be common */
549 	if(!env || !env->anchors || !dp || !dp->name)
550 		return 0;
551 	/* a trust anchor exists with this name, RRSIGs expected */
552 	if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
553 		dclass))) {
554 		lock_basic_unlock(&a->lock);
555 		return 1;
556 	}
557 	/* see if DS rrset was given, in AUTH section */
558 	if(msg && msg->rep &&
559 		reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
560 		LDNS_RR_TYPE_DS, dclass))
561 		return 1;
562 	/* look in key cache */
563 	if(env->key_cache) {
564 		struct key_entry_key* kk = key_cache_obtain(env->key_cache,
565 			dp->name, dp->namelen, dclass, env->scratch, *env->now);
566 		if(kk) {
567 			if(query_dname_compare(kk->name, dp->name) == 0) {
568 			  if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
569 				regional_free_all(env->scratch);
570 				return 1;
571 			  } else if(key_entry_isnull(kk)) {
572 				regional_free_all(env->scratch);
573 				return 0;
574 			  }
575 			}
576 			regional_free_all(env->scratch);
577 		}
578 	}
579 	return 0;
580 }
581 
582 int
583 iter_msg_has_dnssec(struct dns_msg* msg)
584 {
585 	size_t i;
586 	if(!msg || !msg->rep)
587 		return 0;
588 	for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
589 		if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
590 			entry.data)->rrsig_count > 0)
591 			return 1;
592 	}
593 	/* empty message has no DNSSEC info, with DNSSEC the reply is
594 	 * not empty (NSEC) */
595 	return 0;
596 }
597 
598 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
599         enum response_type type, uint16_t dclass)
600 {
601 	if(!msg || !dp || !msg->rep || !dp->name)
602 		return 0;
603 	/* SOA RRset - always from reply zone */
604 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
605 		LDNS_RR_TYPE_SOA, dclass) ||
606 	   reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
607 		LDNS_RR_TYPE_SOA, dclass))
608 		return 1;
609 	if(type == RESPONSE_TYPE_REFERRAL) {
610 		size_t i;
611 		/* if it adds a single label, i.e. we expect .com,
612 		 * and referral to example.com. NS ... , then origin zone
613 		 * is .com. For a referral to sub.example.com. NS ... then
614 		 * we do not know, since example.com. may be in between. */
615 		for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
616 			i++) {
617 			struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
618 			if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
619 				ntohs(s->rk.rrset_class) == dclass) {
620 				int l = dname_count_labels(s->rk.dname);
621 				if(l == dp->namelabs + 1 &&
622 					dname_strict_subdomain(s->rk.dname,
623 					l, dp->name, dp->namelabs))
624 					return 1;
625 			}
626 		}
627 		return 0;
628 	}
629 	log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
630 	/* not a referral, and not lame delegation (upwards), so,
631 	 * any NS rrset must be from the zone itself */
632 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
633 		LDNS_RR_TYPE_NS, dclass) ||
634 	   reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
635 		LDNS_RR_TYPE_NS, dclass))
636 		return 1;
637 	/* a DNSKEY set is expected at the zone apex as well */
638 	/* this is for 'minimal responses' for DNSKEYs */
639 	if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
640 		LDNS_RR_TYPE_DNSKEY, dclass))
641 		return 1;
642 	return 0;
643 }
644 
645 /**
646  * check equality of two rrsets
647  * @param k1: rrset
648  * @param k2: rrset
649  * @return true if equal
650  */
651 static int
652 rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
653 {
654 	struct packed_rrset_data* d1 = (struct packed_rrset_data*)
655 		k1->entry.data;
656 	struct packed_rrset_data* d2 = (struct packed_rrset_data*)
657 		k2->entry.data;
658 	size_t i, t;
659 	if(k1->rk.dname_len != k2->rk.dname_len ||
660 		k1->rk.flags != k2->rk.flags ||
661 		k1->rk.type != k2->rk.type ||
662 		k1->rk.rrset_class != k2->rk.rrset_class ||
663 		query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
664 		return 0;
665 	if(d1->ttl != d2->ttl ||
666 		d1->count != d2->count ||
667 		d1->rrsig_count != d2->rrsig_count ||
668 		d1->trust != d2->trust ||
669 		d1->security != d2->security)
670 		return 0;
671 	t = d1->count + d1->rrsig_count;
672 	for(i=0; i<t; i++) {
673 		if(d1->rr_len[i] != d2->rr_len[i] ||
674 			d1->rr_ttl[i] != d2->rr_ttl[i] ||
675 			memcmp(d1->rr_data[i], d2->rr_data[i],
676 				d1->rr_len[i]) != 0)
677 			return 0;
678 	}
679 	return 1;
680 }
681 
682 int
683 reply_equal(struct reply_info* p, struct reply_info* q, ldns_buffer* scratch)
684 {
685 	size_t i;
686 	if(p->flags != q->flags ||
687 		p->qdcount != q->qdcount ||
688 		p->ttl != q->ttl ||
689 		p->prefetch_ttl != q->prefetch_ttl ||
690 		p->security != q->security ||
691 		p->an_numrrsets != q->an_numrrsets ||
692 		p->ns_numrrsets != q->ns_numrrsets ||
693 		p->ar_numrrsets != q->ar_numrrsets ||
694 		p->rrset_count != q->rrset_count)
695 		return 0;
696 	for(i=0; i<p->rrset_count; i++) {
697 		if(!rrset_equal(p->rrsets[i], q->rrsets[i])) {
698 			/* fallback procedure: try to sort and canonicalize */
699 			ldns_rr_list* pl, *ql;
700 			pl = packed_rrset_to_rr_list(p->rrsets[i], scratch);
701 			ql = packed_rrset_to_rr_list(q->rrsets[i], scratch);
702 			if(!pl || !ql) {
703 				ldns_rr_list_deep_free(pl);
704 				ldns_rr_list_deep_free(ql);
705 				return 0;
706 			}
707 			ldns_rr_list2canonical(pl);
708 			ldns_rr_list2canonical(ql);
709 			ldns_rr_list_sort(pl);
710 			ldns_rr_list_sort(ql);
711 			if(ldns_rr_list_compare(pl, ql) != 0) {
712 				ldns_rr_list_deep_free(pl);
713 				ldns_rr_list_deep_free(ql);
714 				return 0;
715 			}
716 			ldns_rr_list_deep_free(pl);
717 			ldns_rr_list_deep_free(ql);
718 			continue;
719 		}
720 	}
721 	return 1;
722 }
723 
724 void
725 iter_store_parentside_rrset(struct module_env* env,
726 	struct ub_packed_rrset_key* rrset)
727 {
728 	struct rrset_ref ref;
729 	rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
730 	if(!rrset) {
731 		log_err("malloc failure in store_parentside_rrset");
732 		return;
733 	}
734 	rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
735 	rrset->entry.hash = rrset_key_hash(&rrset->rk);
736 	ref.key = rrset;
737 	ref.id = rrset->id;
738 	/* ignore ret: if it was in the cache, ref updated */
739 	(void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
740 }
741 
742 /** fetch NS record from reply, if any */
743 static struct ub_packed_rrset_key*
744 reply_get_NS_rrset(struct reply_info* rep)
745 {
746 	size_t i;
747 	for(i=0; i<rep->rrset_count; i++) {
748 		if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
749 			return rep->rrsets[i];
750 		}
751 	}
752 	return NULL;
753 }
754 
755 void
756 iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
757 {
758 	struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
759 	if(rrset) {
760 		log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
761 		iter_store_parentside_rrset(env, rrset);
762 	}
763 }
764 
765 void iter_store_parentside_neg(struct module_env* env,
766         struct query_info* qinfo, struct reply_info* rep)
767 {
768 	/* TTL: NS from referral in iq->deleg_msg,
769 	 *      or first RR from iq->response,
770 	 *      or servfail5secs if !iq->response */
771 	uint32_t ttl = NORR_TTL;
772 	struct ub_packed_rrset_key* neg;
773 	struct packed_rrset_data* newd;
774 	if(rep) {
775 		struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
776 		if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
777 		if(rrset) ttl = ub_packed_rrset_ttl(rrset);
778 	}
779 	/* create empty rrset to store */
780 	neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
781 	                sizeof(struct ub_packed_rrset_key));
782 	if(!neg) {
783 		log_err("out of memory in store_parentside_neg");
784 		return;
785 	}
786 	memset(&neg->entry, 0, sizeof(neg->entry));
787 	neg->entry.key = neg;
788 	neg->rk.type = htons(qinfo->qtype);
789 	neg->rk.rrset_class = htons(qinfo->qclass);
790 	neg->rk.flags = 0;
791 	neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
792 		qinfo->qname_len);
793 	if(!neg->rk.dname) {
794 		log_err("out of memory in store_parentside_neg");
795 		return;
796 	}
797 	neg->rk.dname_len = qinfo->qname_len;
798 	neg->entry.hash = rrset_key_hash(&neg->rk);
799 	newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
800 		sizeof(struct packed_rrset_data) + sizeof(size_t) +
801 		sizeof(uint8_t*) + sizeof(uint32_t) + sizeof(uint16_t));
802 	if(!newd) {
803 		log_err("out of memory in store_parentside_neg");
804 		return;
805 	}
806 	neg->entry.data = newd;
807 	newd->ttl = ttl;
808 	/* entry must have one RR, otherwise not valid in cache.
809 	 * put in one RR with empty rdata: those are ignored as nameserver */
810 	newd->count = 1;
811 	newd->rrsig_count = 0;
812 	newd->trust = rrset_trust_ans_noAA;
813 	newd->rr_len = (size_t*)((uint8_t*)newd +
814 		sizeof(struct packed_rrset_data));
815 	newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
816 	packed_rrset_ptr_fixup(newd);
817 	newd->rr_ttl[0] = newd->ttl;
818 	ldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
819 	/* store it */
820 	log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
821 	iter_store_parentside_rrset(env, neg);
822 }
823 
824 int
825 iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
826 	struct regional* region, struct query_info* qinfo)
827 {
828 	struct ub_packed_rrset_key* akey;
829 	akey = rrset_cache_lookup(env->rrset_cache, dp->name,
830 		dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
831 		PACKED_RRSET_PARENT_SIDE, *env->now, 0);
832 	if(akey) {
833 		log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
834 		dp->has_parent_side_NS = 1;
835 		/* and mark the new names as lame */
836 		if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
837 			lock_rw_unlock(&akey->entry.lock);
838 			return 0;
839 		}
840 		lock_rw_unlock(&akey->entry.lock);
841 	}
842 	return 1;
843 }
844 
845 int iter_lookup_parent_glue_from_cache(struct module_env* env,
846         struct delegpt* dp, struct regional* region, struct query_info* qinfo)
847 {
848 	struct ub_packed_rrset_key* akey;
849 	struct delegpt_ns* ns;
850 	size_t num = delegpt_count_targets(dp);
851 	for(ns = dp->nslist; ns; ns = ns->next) {
852 		/* get cached parentside A */
853 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
854 			ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
855 			PACKED_RRSET_PARENT_SIDE, *env->now, 0);
856 		if(akey) {
857 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
858 			ns->done_pside4 = 1;
859 			/* a negative-cache-element has no addresses it adds */
860 			if(!delegpt_add_rrset_A(dp, region, akey, 1))
861 				log_err("malloc failure in lookup_parent_glue");
862 			lock_rw_unlock(&akey->entry.lock);
863 		}
864 		/* get cached parentside AAAA */
865 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
866 			ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
867 			PACKED_RRSET_PARENT_SIDE, *env->now, 0);
868 		if(akey) {
869 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
870 			ns->done_pside6 = 1;
871 			/* a negative-cache-element has no addresses it adds */
872 			if(!delegpt_add_rrset_AAAA(dp, region, akey, 1))
873 				log_err("malloc failure in lookup_parent_glue");
874 			lock_rw_unlock(&akey->entry.lock);
875 		}
876 	}
877 	/* see if new (but lame) addresses have become available */
878 	return delegpt_count_targets(dp) != num;
879 }
880 
881 int
882 iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
883 	uint16_t* c)
884 {
885 	uint16_t c1 = *c, c2 = *c;
886 	int r1 = hints_next_root(hints, &c1);
887 	int r2 = forwards_next_root(fwd, &c2);
888 	if(!r1 && !r2) /* got none, end of list */
889 		return 0;
890 	else if(!r1) /* got one, return that */
891 		*c = c2;
892 	else if(!r2)
893 		*c = c1;
894 	else if(c1 < c2) /* got both take smallest */
895 		*c = c1;
896 	else	*c = c2;
897 	return 1;
898 }
899 
900 void
901 iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
902 {
903 	/* Only the DS record for the delegation itself is expected.
904 	 * We allow DS for everything between the bailiwick and the
905 	 * zonecut, thus DS records must be at or above the zonecut.
906 	 * And the DS records must be below the server authority zone.
907 	 * The answer section is already scrubbed. */
908 	size_t i = msg->rep->an_numrrsets;
909 	while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
910 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
911 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
912 			(!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
913 			|| query_dname_compare(z, s->rk.dname) == 0)) {
914 			log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
915 				s->rk.dname, ntohs(s->rk.type),
916 				ntohs(s->rk.rrset_class));
917 			memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
918 				sizeof(struct ub_packed_rrset_key*) *
919 				(msg->rep->rrset_count-i-1));
920 			msg->rep->ns_numrrsets--;
921 			msg->rep->rrset_count--;
922 			/* stay at same i, but new record */
923 			continue;
924 		}
925 		i++;
926 	}
927 }
928 
929 void iter_dec_attempts(struct delegpt* dp, int d)
930 {
931 	struct delegpt_addr* a;
932 	for(a=dp->target_list; a; a = a->next_target) {
933 		if(a->attempts >= OUTBOUND_MSG_RETRY) {
934 			/* add back to result list */
935 			a->next_result = dp->result_list;
936 			dp->result_list = a;
937 		}
938 		if(a->attempts > d)
939 			a->attempts -= d;
940 		else a->attempts = 0;
941 	}
942 }
943 
944 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old)
945 {
946 	struct delegpt_addr* a, *o, *prev;
947 	for(a=dp->target_list; a; a = a->next_target) {
948 		o = delegpt_find_addr(old, &a->addr, a->addrlen);
949 		if(o) {
950 			log_addr(VERB_ALGO, "copy attempt count previous dp",
951 				&a->addr, a->addrlen);
952 			a->attempts = o->attempts;
953 		}
954 	}
955 	prev = NULL;
956 	a = dp->usable_list;
957 	while(a) {
958 		if(a->attempts >= OUTBOUND_MSG_RETRY) {
959 			log_addr(VERB_ALGO, "remove from usable list dp",
960 				&a->addr, a->addrlen);
961 			/* remove from result list */
962 			if(prev)
963 				prev->next_usable = a->next_usable;
964 			else	dp->usable_list = a->next_usable;
965 			/* prev stays the same */
966 			a = a->next_usable;
967 			continue;
968 		}
969 		prev = a;
970 		a = a->next_usable;
971 	}
972 }
973 
974 int
975 iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
976 {
977 	/* if for query example.com, there is example.com SOA or a subdomain
978 	 * of example.com, then we are too low and need to fetch NS. */
979 	size_t i;
980 	/* if we have a DNAME or CNAME we are probably wrong */
981 	/* if we have a qtype DS in the answer section, its fine */
982 	for(i=0; i < msg->rep->an_numrrsets; i++) {
983 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
984 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
985 			ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
986 			/* not the right answer, maybe too low, check the
987 			 * RRSIG signer name (if there is any) for a hint
988 			 * that it is from the dp zone anyway */
989 			uint8_t* sname;
990 			size_t slen;
991 			val_find_rrset_signer(s, &sname, &slen);
992 			if(sname && query_dname_compare(dp->name, sname)==0)
993 				return 0; /* it is fine, from the right dp */
994 			return 1;
995 		}
996 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
997 			return 0; /* fine, we have a DS record */
998 	}
999 	for(i=msg->rep->an_numrrsets;
1000 		i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
1001 		struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1002 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1003 			if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
1004 				return 1; /* point is too low */
1005 			if(query_dname_compare(s->rk.dname, dp->name)==0)
1006 				return 0; /* right dp */
1007 		}
1008 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1009 			ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1010 			uint8_t* sname;
1011 			size_t slen;
1012 			val_find_rrset_signer(s, &sname, &slen);
1013 			if(sname && query_dname_compare(dp->name, sname)==0)
1014 				return 0; /* it is fine, from the right dp */
1015 			return 1;
1016 		}
1017 	}
1018 	/* we do not know */
1019 	return 1;
1020 }
1021 
1022 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
1023 {
1024 	/* no delegation point, do not see how we can go down,
1025 	 * robust check, it should really exist */
1026 	if(!dp) return 0;
1027 
1028 	/* see if dp equals the qname, then we cannot go down further */
1029 	if(query_dname_compare(qinfo->qname, dp->name) == 0)
1030 		return 0;
1031 	/* if dp is one label above the name we also cannot go down further */
1032 	if(dname_count_labels(qinfo->qname) == dp->namelabs+1)
1033 		return 0;
1034 	return 1;
1035 }
1036