xref: /freebsd/contrib/unbound/iterator/iterator.c (revision 3d32dc633c5e21bf15dd0d968734efe72776afdc)
1 /*
2  * iterator/iterator.c - iterative resolver DNS query response module
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 a module that performs recursive iterative DNS query
40  * processing.
41  */
42 
43 #include "config.h"
44 #include "iterator/iterator.h"
45 #include "iterator/iter_utils.h"
46 #include "iterator/iter_hints.h"
47 #include "iterator/iter_fwd.h"
48 #include "iterator/iter_donotq.h"
49 #include "iterator/iter_delegpt.h"
50 #include "iterator/iter_resptype.h"
51 #include "iterator/iter_scrub.h"
52 #include "iterator/iter_priv.h"
53 #include "validator/val_neg.h"
54 #include "services/cache/dns.h"
55 #include "services/cache/infra.h"
56 #include "services/authzone.h"
57 #include "util/module.h"
58 #include "util/netevent.h"
59 #include "util/net_help.h"
60 #include "util/regional.h"
61 #include "util/data/dname.h"
62 #include "util/data/msgencode.h"
63 #include "util/fptr_wlist.h"
64 #include "util/config_file.h"
65 #include "util/random.h"
66 #include "sldns/rrdef.h"
67 #include "sldns/wire2str.h"
68 #include "sldns/str2wire.h"
69 #include "sldns/parseutil.h"
70 #include "sldns/sbuffer.h"
71 
72 int
73 iter_init(struct module_env* env, int id)
74 {
75 	struct iter_env* iter_env = (struct iter_env*)calloc(1,
76 		sizeof(struct iter_env));
77 	if(!iter_env) {
78 		log_err("malloc failure");
79 		return 0;
80 	}
81 	env->modinfo[id] = (void*)iter_env;
82 
83 	lock_basic_init(&iter_env->queries_ratelimit_lock);
84 	lock_protect(&iter_env->queries_ratelimit_lock,
85 			&iter_env->num_queries_ratelimited,
86 		sizeof(iter_env->num_queries_ratelimited));
87 
88 	if(!iter_apply_cfg(iter_env, env->cfg)) {
89 		log_err("iterator: could not apply configuration settings.");
90 		return 0;
91 	}
92 
93 	return 1;
94 }
95 
96 /** delete caps_whitelist element */
97 static void
98 caps_free(struct rbnode_type* n, void* ATTR_UNUSED(d))
99 {
100 	if(n) {
101 		free(((struct name_tree_node*)n)->name);
102 		free(n);
103 	}
104 }
105 
106 void
107 iter_deinit(struct module_env* env, int id)
108 {
109 	struct iter_env* iter_env;
110 	if(!env || !env->modinfo[id])
111 		return;
112 	iter_env = (struct iter_env*)env->modinfo[id];
113 	lock_basic_destroy(&iter_env->queries_ratelimit_lock);
114 	free(iter_env->target_fetch_policy);
115 	priv_delete(iter_env->priv);
116 	donotq_delete(iter_env->donotq);
117 	if(iter_env->caps_white) {
118 		traverse_postorder(iter_env->caps_white, caps_free, NULL);
119 		free(iter_env->caps_white);
120 	}
121 	free(iter_env);
122 	env->modinfo[id] = NULL;
123 }
124 
125 /** new query for iterator */
126 static int
127 iter_new(struct module_qstate* qstate, int id)
128 {
129 	struct iter_qstate* iq = (struct iter_qstate*)regional_alloc(
130 		qstate->region, sizeof(struct iter_qstate));
131 	qstate->minfo[id] = iq;
132 	if(!iq)
133 		return 0;
134 	memset(iq, 0, sizeof(*iq));
135 	iq->state = INIT_REQUEST_STATE;
136 	iq->final_state = FINISHED_STATE;
137 	iq->an_prepend_list = NULL;
138 	iq->an_prepend_last = NULL;
139 	iq->ns_prepend_list = NULL;
140 	iq->ns_prepend_last = NULL;
141 	iq->dp = NULL;
142 	iq->depth = 0;
143 	iq->num_target_queries = 0;
144 	iq->num_current_queries = 0;
145 	iq->query_restart_count = 0;
146 	iq->referral_count = 0;
147 	iq->sent_count = 0;
148 	iq->ratelimit_ok = 0;
149 	iq->target_count = NULL;
150 	iq->wait_priming_stub = 0;
151 	iq->refetch_glue = 0;
152 	iq->dnssec_expected = 0;
153 	iq->dnssec_lame_query = 0;
154 	iq->chase_flags = qstate->query_flags;
155 	/* Start with the (current) qname. */
156 	iq->qchase = qstate->qinfo;
157 	outbound_list_init(&iq->outlist);
158 	iq->minimise_count = 0;
159 	iq->minimise_timeout_count = 0;
160 	if (qstate->env->cfg->qname_minimisation)
161 		iq->minimisation_state = INIT_MINIMISE_STATE;
162 	else
163 		iq->minimisation_state = DONOT_MINIMISE_STATE;
164 
165 	memset(&iq->qinfo_out, 0, sizeof(struct query_info));
166 	return 1;
167 }
168 
169 /**
170  * Transition to the next state. This can be used to advance a currently
171  * processing event. It cannot be used to reactivate a forEvent.
172  *
173  * @param iq: iterator query state
174  * @param nextstate The state to transition to.
175  * @return true. This is so this can be called as the return value for the
176  *         actual process*State() methods. (Transitioning to the next state
177  *         implies further processing).
178  */
179 static int
180 next_state(struct iter_qstate* iq, enum iter_state nextstate)
181 {
182 	/* If transitioning to a "response" state, make sure that there is a
183 	 * response */
184 	if(iter_state_is_responsestate(nextstate)) {
185 		if(iq->response == NULL) {
186 			log_err("transitioning to response state sans "
187 				"response.");
188 		}
189 	}
190 	iq->state = nextstate;
191 	return 1;
192 }
193 
194 /**
195  * Transition an event to its final state. Final states always either return
196  * a result up the module chain, or reactivate a dependent event. Which
197  * final state to transition to is set in the module state for the event when
198  * it was created, and depends on the original purpose of the event.
199  *
200  * The response is stored in the qstate->buf buffer.
201  *
202  * @param iq: iterator query state
203  * @return false. This is so this method can be used as the return value for
204  *         the processState methods. (Transitioning to the final state
205  */
206 static int
207 final_state(struct iter_qstate* iq)
208 {
209 	return next_state(iq, iq->final_state);
210 }
211 
212 /**
213  * Callback routine to handle errors in parent query states
214  * @param qstate: query state that failed.
215  * @param id: module id.
216  * @param super: super state.
217  */
218 static void
219 error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
220 {
221 	struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
222 
223 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
224 		qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
225 		/* mark address as failed. */
226 		struct delegpt_ns* dpns = NULL;
227 		super_iq->num_target_queries--;
228 		if(super_iq->dp)
229 			dpns = delegpt_find_ns(super_iq->dp,
230 				qstate->qinfo.qname, qstate->qinfo.qname_len);
231 		if(!dpns) {
232 			/* not interested */
233 			verbose(VERB_ALGO, "subq error, but not interested");
234 			log_query_info(VERB_ALGO, "superq", &super->qinfo);
235 			if(super_iq->dp)
236 				delegpt_log(VERB_ALGO, super_iq->dp);
237 			log_assert(0);
238 			return;
239 		} else {
240 			/* see if the failure did get (parent-lame) info */
241 			if(!cache_fill_missing(super->env, super_iq->qchase.qclass,
242 				super->region, super_iq->dp))
243 				log_err("out of memory adding missing");
244 		}
245 		dpns->resolved = 1; /* mark as failed */
246 	}
247 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
248 		/* prime failed to get delegation */
249 		super_iq->dp = NULL;
250 	}
251 	/* evaluate targets again */
252 	super_iq->state = QUERYTARGETS_STATE;
253 	/* super becomes runnable, and will process this change */
254 }
255 
256 /**
257  * Return an error to the client
258  * @param qstate: our query state
259  * @param id: module id
260  * @param rcode: error code (DNS errcode).
261  * @return: 0 for use by caller, to make notation easy, like:
262  * 	return error_response(..).
263  */
264 static int
265 error_response(struct module_qstate* qstate, int id, int rcode)
266 {
267 	verbose(VERB_QUERY, "return error response %s",
268 		sldns_lookup_by_id(sldns_rcodes, rcode)?
269 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
270 	qstate->return_rcode = rcode;
271 	qstate->return_msg = NULL;
272 	qstate->ext_state[id] = module_finished;
273 	return 0;
274 }
275 
276 /**
277  * Return an error to the client and cache the error code in the
278  * message cache (so per qname, qtype, qclass).
279  * @param qstate: our query state
280  * @param id: module id
281  * @param rcode: error code (DNS errcode).
282  * @return: 0 for use by caller, to make notation easy, like:
283  * 	return error_response(..).
284  */
285 static int
286 error_response_cache(struct module_qstate* qstate, int id, int rcode)
287 {
288 	if(!qstate->no_cache_store) {
289 		/* store in cache */
290 		struct reply_info err;
291 		if(qstate->prefetch_leeway > NORR_TTL) {
292 			verbose(VERB_ALGO, "error response for prefetch in cache");
293 			/* attempt to adjust the cache entry prefetch */
294 			if(dns_cache_prefetch_adjust(qstate->env, &qstate->qinfo,
295 				NORR_TTL, qstate->query_flags))
296 				return error_response(qstate, id, rcode);
297 			/* if that fails (not in cache), fall through to store err */
298 		}
299 		if(qstate->env->cfg->serve_expired) {
300 			/* if serving expired contents, and such content is
301 			 * already available, don't overwrite this servfail */
302 			struct msgreply_entry* msg;
303 			if((msg=msg_cache_lookup(qstate->env,
304 				qstate->qinfo.qname, qstate->qinfo.qname_len,
305 				qstate->qinfo.qtype, qstate->qinfo.qclass,
306 				qstate->query_flags, 0, 0))
307 				!= NULL) {
308 				lock_rw_unlock(&msg->entry.lock);
309 				return error_response(qstate, id, rcode);
310 			}
311 			/* serving expired contents, but nothing is cached
312 			 * at all, so the servfail cache entry is useful
313 			 * (stops waste of time on this servfail NORR_TTL) */
314 		}
315 		memset(&err, 0, sizeof(err));
316 		err.flags = (uint16_t)(BIT_QR | BIT_RA);
317 		FLAGS_SET_RCODE(err.flags, rcode);
318 		err.qdcount = 1;
319 		err.ttl = NORR_TTL;
320 		err.prefetch_ttl = PREFETCH_TTL_CALC(err.ttl);
321 		/* do not waste time trying to validate this servfail */
322 		err.security = sec_status_indeterminate;
323 		verbose(VERB_ALGO, "store error response in message cache");
324 		iter_dns_store(qstate->env, &qstate->qinfo, &err, 0, 0, 0, NULL,
325 			qstate->query_flags);
326 	}
327 	return error_response(qstate, id, rcode);
328 }
329 
330 /** check if prepend item is duplicate item */
331 static int
332 prepend_is_duplicate(struct ub_packed_rrset_key** sets, size_t to,
333 	struct ub_packed_rrset_key* dup)
334 {
335 	size_t i;
336 	for(i=0; i<to; i++) {
337 		if(sets[i]->rk.type == dup->rk.type &&
338 			sets[i]->rk.rrset_class == dup->rk.rrset_class &&
339 			sets[i]->rk.dname_len == dup->rk.dname_len &&
340 			query_dname_compare(sets[i]->rk.dname, dup->rk.dname)
341 			== 0)
342 			return 1;
343 	}
344 	return 0;
345 }
346 
347 /** prepend the prepend list in the answer and authority section of dns_msg */
348 static int
349 iter_prepend(struct iter_qstate* iq, struct dns_msg* msg,
350 	struct regional* region)
351 {
352 	struct iter_prep_list* p;
353 	struct ub_packed_rrset_key** sets;
354 	size_t num_an = 0, num_ns = 0;;
355 	for(p = iq->an_prepend_list; p; p = p->next)
356 		num_an++;
357 	for(p = iq->ns_prepend_list; p; p = p->next)
358 		num_ns++;
359 	if(num_an + num_ns == 0)
360 		return 1;
361 	verbose(VERB_ALGO, "prepending %d rrsets", (int)num_an + (int)num_ns);
362 	if(num_an > RR_COUNT_MAX || num_ns > RR_COUNT_MAX ||
363 		msg->rep->rrset_count > RR_COUNT_MAX) return 0; /* overflow */
364 	sets = regional_alloc(region, (num_an+num_ns+msg->rep->rrset_count) *
365 		sizeof(struct ub_packed_rrset_key*));
366 	if(!sets)
367 		return 0;
368 	/* ANSWER section */
369 	num_an = 0;
370 	for(p = iq->an_prepend_list; p; p = p->next) {
371 		sets[num_an++] = p->rrset;
372 	}
373 	memcpy(sets+num_an, msg->rep->rrsets, msg->rep->an_numrrsets *
374 		sizeof(struct ub_packed_rrset_key*));
375 	/* AUTH section */
376 	num_ns = 0;
377 	for(p = iq->ns_prepend_list; p; p = p->next) {
378 		if(prepend_is_duplicate(sets+msg->rep->an_numrrsets+num_an,
379 			num_ns, p->rrset) || prepend_is_duplicate(
380 			msg->rep->rrsets+msg->rep->an_numrrsets,
381 			msg->rep->ns_numrrsets, p->rrset))
382 			continue;
383 		sets[msg->rep->an_numrrsets + num_an + num_ns++] = p->rrset;
384 	}
385 	memcpy(sets + num_an + msg->rep->an_numrrsets + num_ns,
386 		msg->rep->rrsets + msg->rep->an_numrrsets,
387 		(msg->rep->ns_numrrsets + msg->rep->ar_numrrsets) *
388 		sizeof(struct ub_packed_rrset_key*));
389 
390 	/* NXDOMAIN rcode can stay if we prepended DNAME/CNAMEs, because
391 	 * this is what recursors should give. */
392 	msg->rep->rrset_count += num_an + num_ns;
393 	msg->rep->an_numrrsets += num_an;
394 	msg->rep->ns_numrrsets += num_ns;
395 	msg->rep->rrsets = sets;
396 	return 1;
397 }
398 
399 /**
400  * Find rrset in ANSWER prepend list.
401  * to avoid duplicate DNAMEs when a DNAME is traversed twice.
402  * @param iq: iterator query state.
403  * @param rrset: rrset to add.
404  * @return false if not found
405  */
406 static int
407 iter_find_rrset_in_prepend_answer(struct iter_qstate* iq,
408 	struct ub_packed_rrset_key* rrset)
409 {
410 	struct iter_prep_list* p = iq->an_prepend_list;
411 	while(p) {
412 		if(ub_rrset_compare(p->rrset, rrset) == 0 &&
413 			rrsetdata_equal((struct packed_rrset_data*)p->rrset
414 			->entry.data, (struct packed_rrset_data*)rrset
415 			->entry.data))
416 			return 1;
417 		p = p->next;
418 	}
419 	return 0;
420 }
421 
422 /**
423  * Add rrset to ANSWER prepend list
424  * @param qstate: query state.
425  * @param iq: iterator query state.
426  * @param rrset: rrset to add.
427  * @return false on failure (malloc).
428  */
429 static int
430 iter_add_prepend_answer(struct module_qstate* qstate, struct iter_qstate* iq,
431 	struct ub_packed_rrset_key* rrset)
432 {
433 	struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
434 		qstate->region, sizeof(struct iter_prep_list));
435 	if(!p)
436 		return 0;
437 	p->rrset = rrset;
438 	p->next = NULL;
439 	/* add at end */
440 	if(iq->an_prepend_last)
441 		iq->an_prepend_last->next = p;
442 	else	iq->an_prepend_list = p;
443 	iq->an_prepend_last = p;
444 	return 1;
445 }
446 
447 /**
448  * Add rrset to AUTHORITY prepend list
449  * @param qstate: query state.
450  * @param iq: iterator query state.
451  * @param rrset: rrset to add.
452  * @return false on failure (malloc).
453  */
454 static int
455 iter_add_prepend_auth(struct module_qstate* qstate, struct iter_qstate* iq,
456 	struct ub_packed_rrset_key* rrset)
457 {
458 	struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
459 		qstate->region, sizeof(struct iter_prep_list));
460 	if(!p)
461 		return 0;
462 	p->rrset = rrset;
463 	p->next = NULL;
464 	/* add at end */
465 	if(iq->ns_prepend_last)
466 		iq->ns_prepend_last->next = p;
467 	else	iq->ns_prepend_list = p;
468 	iq->ns_prepend_last = p;
469 	return 1;
470 }
471 
472 /**
473  * Given a CNAME response (defined as a response containing a CNAME or DNAME
474  * that does not answer the request), process the response, modifying the
475  * state as necessary. This follows the CNAME/DNAME chain and returns the
476  * final query name.
477  *
478  * sets the new query name, after following the CNAME/DNAME chain.
479  * @param qstate: query state.
480  * @param iq: iterator query state.
481  * @param msg: the response.
482  * @param mname: returned target new query name.
483  * @param mname_len: length of mname.
484  * @return false on (malloc) error.
485  */
486 static int
487 handle_cname_response(struct module_qstate* qstate, struct iter_qstate* iq,
488         struct dns_msg* msg, uint8_t** mname, size_t* mname_len)
489 {
490 	size_t i;
491 	/* Start with the (current) qname. */
492 	*mname = iq->qchase.qname;
493 	*mname_len = iq->qchase.qname_len;
494 
495 	/* Iterate over the ANSWER rrsets in order, looking for CNAMEs and
496 	 * DNAMES. */
497 	for(i=0; i<msg->rep->an_numrrsets; i++) {
498 		struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
499 		/* If there is a (relevant) DNAME, add it to the list.
500 		 * We always expect there to be CNAME that was generated
501 		 * by this DNAME following, so we don't process the DNAME
502 		 * directly.  */
503 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_DNAME &&
504 			dname_strict_subdomain_c(*mname, r->rk.dname) &&
505 			!iter_find_rrset_in_prepend_answer(iq, r)) {
506 			if(!iter_add_prepend_answer(qstate, iq, r))
507 				return 0;
508 			continue;
509 		}
510 
511 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_CNAME &&
512 			query_dname_compare(*mname, r->rk.dname) == 0 &&
513 			!iter_find_rrset_in_prepend_answer(iq, r)) {
514 			/* Add this relevant CNAME rrset to the prepend list.*/
515 			if(!iter_add_prepend_answer(qstate, iq, r))
516 				return 0;
517 			get_cname_target(r, mname, mname_len);
518 		}
519 
520 		/* Other rrsets in the section are ignored. */
521 	}
522 	/* add authority rrsets to authority prepend, for wildcarded CNAMEs */
523 	for(i=msg->rep->an_numrrsets; i<msg->rep->an_numrrsets +
524 		msg->rep->ns_numrrsets; i++) {
525 		struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
526 		/* only add NSEC/NSEC3, as they may be needed for validation */
527 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC ||
528 			ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC3) {
529 			if(!iter_add_prepend_auth(qstate, iq, r))
530 				return 0;
531 		}
532 	}
533 	return 1;
534 }
535 
536 /** see if last resort is possible - does config allow queries to parent */
537 static int
538 can_have_last_resort(struct module_env* env, uint8_t* nm, size_t nmlen,
539 	uint16_t qclass, struct delegpt** retdp)
540 {
541 	struct delegpt* fwddp;
542 	struct iter_hints_stub* stub;
543 	int labs = dname_count_labels(nm);
544 	/* do not process a last resort (the parent side) if a stub
545 	 * or forward is configured, because we do not want to go 'above'
546 	 * the configured servers */
547 	if(!dname_is_root(nm) && (stub = (struct iter_hints_stub*)
548 		name_tree_find(&env->hints->tree, nm, nmlen, labs, qclass)) &&
549 		/* has_parent side is turned off for stub_first, where we
550 		 * are allowed to go to the parent */
551 		stub->dp->has_parent_side_NS) {
552 		if(retdp) *retdp = stub->dp;
553 		return 0;
554 	}
555 	if((fwddp = forwards_find(env->fwds, nm, qclass)) &&
556 		/* has_parent_side is turned off for forward_first, where
557 		 * we are allowed to go to the parent */
558 		fwddp->has_parent_side_NS) {
559 		if(retdp) *retdp = fwddp;
560 		return 0;
561 	}
562 	return 1;
563 }
564 
565 /** see if target name is caps-for-id whitelisted */
566 static int
567 is_caps_whitelisted(struct iter_env* ie, struct iter_qstate* iq)
568 {
569 	if(!ie->caps_white) return 0; /* no whitelist, or no capsforid */
570 	return name_tree_lookup(ie->caps_white, iq->qchase.qname,
571 		iq->qchase.qname_len, dname_count_labels(iq->qchase.qname),
572 		iq->qchase.qclass) != NULL;
573 }
574 
575 /** create target count structure for this query */
576 static void
577 target_count_create(struct iter_qstate* iq)
578 {
579 	if(!iq->target_count) {
580 		iq->target_count = (int*)calloc(2, sizeof(int));
581 		/* if calloc fails we simply do not track this number */
582 		if(iq->target_count)
583 			iq->target_count[0] = 1;
584 	}
585 }
586 
587 static void
588 target_count_increase(struct iter_qstate* iq, int num)
589 {
590 	target_count_create(iq);
591 	if(iq->target_count)
592 		iq->target_count[1] += num;
593 }
594 
595 /**
596  * Generate a subrequest.
597  * Generate a local request event. Local events are tied to this module, and
598  * have a corresponding (first tier) event that is waiting for this event to
599  * resolve to continue.
600  *
601  * @param qname The query name for this request.
602  * @param qnamelen length of qname
603  * @param qtype The query type for this request.
604  * @param qclass The query class for this request.
605  * @param qstate The event that is generating this event.
606  * @param id: module id.
607  * @param iq: The iterator state that is generating this event.
608  * @param initial_state The initial response state (normally this
609  *          is QUERY_RESP_STATE, unless it is known that the request won't
610  *          need iterative processing
611  * @param finalstate The final state for the response to this request.
612  * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
613  * 	not need initialisation.
614  * @param v: if true, validation is done on the subquery.
615  * @return false on error (malloc).
616  */
617 static int
618 generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
619 	uint16_t qclass, struct module_qstate* qstate, int id,
620 	struct iter_qstate* iq, enum iter_state initial_state,
621 	enum iter_state finalstate, struct module_qstate** subq_ret, int v)
622 {
623 	struct module_qstate* subq = NULL;
624 	struct iter_qstate* subiq = NULL;
625 	uint16_t qflags = 0; /* OPCODE QUERY, no flags */
626 	struct query_info qinf;
627 	int prime = (finalstate == PRIME_RESP_STATE)?1:0;
628 	int valrec = 0;
629 	qinf.qname = qname;
630 	qinf.qname_len = qnamelen;
631 	qinf.qtype = qtype;
632 	qinf.qclass = qclass;
633 	qinf.local_alias = NULL;
634 
635 	/* RD should be set only when sending the query back through the INIT
636 	 * state. */
637 	if(initial_state == INIT_REQUEST_STATE)
638 		qflags |= BIT_RD;
639 	/* We set the CD flag so we can send this through the "head" of
640 	 * the resolution chain, which might have a validator. We are
641 	 * uninterested in validating things not on the direct resolution
642 	 * path.  */
643 	if(!v) {
644 		qflags |= BIT_CD;
645 		valrec = 1;
646 	}
647 
648 	/* attach subquery, lookup existing or make a new one */
649 	fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
650 	if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime, valrec,
651 		&subq)) {
652 		return 0;
653 	}
654 	*subq_ret = subq;
655 	if(subq) {
656 		/* initialise the new subquery */
657 		subq->curmod = id;
658 		subq->ext_state[id] = module_state_initial;
659 		subq->minfo[id] = regional_alloc(subq->region,
660 			sizeof(struct iter_qstate));
661 		if(!subq->minfo[id]) {
662 			log_err("init subq: out of memory");
663 			fptr_ok(fptr_whitelist_modenv_kill_sub(
664 				qstate->env->kill_sub));
665 			(*qstate->env->kill_sub)(subq);
666 			return 0;
667 		}
668 		subiq = (struct iter_qstate*)subq->minfo[id];
669 		memset(subiq, 0, sizeof(*subiq));
670 		subiq->num_target_queries = 0;
671 		target_count_create(iq);
672 		subiq->target_count = iq->target_count;
673 		if(iq->target_count)
674 			iq->target_count[0] ++; /* extra reference */
675 		subiq->num_current_queries = 0;
676 		subiq->depth = iq->depth+1;
677 		outbound_list_init(&subiq->outlist);
678 		subiq->state = initial_state;
679 		subiq->final_state = finalstate;
680 		subiq->qchase = subq->qinfo;
681 		subiq->chase_flags = subq->query_flags;
682 		subiq->refetch_glue = 0;
683 		if(qstate->env->cfg->qname_minimisation)
684 			subiq->minimisation_state = INIT_MINIMISE_STATE;
685 		else
686 			subiq->minimisation_state = DONOT_MINIMISE_STATE;
687 		memset(&subiq->qinfo_out, 0, sizeof(struct query_info));
688 	}
689 	return 1;
690 }
691 
692 /**
693  * Generate and send a root priming request.
694  * @param qstate: the qtstate that triggered the need to prime.
695  * @param iq: iterator query state.
696  * @param id: module id.
697  * @param qclass: the class to prime.
698  * @return 0 on failure
699  */
700 static int
701 prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
702 	uint16_t qclass)
703 {
704 	struct delegpt* dp;
705 	struct module_qstate* subq;
706 	verbose(VERB_DETAIL, "priming . %s NS",
707 		sldns_lookup_by_id(sldns_rr_classes, (int)qclass)?
708 		sldns_lookup_by_id(sldns_rr_classes, (int)qclass)->name:"??");
709 	dp = hints_lookup_root(qstate->env->hints, qclass);
710 	if(!dp) {
711 		verbose(VERB_ALGO, "Cannot prime due to lack of hints");
712 		return 0;
713 	}
714 	/* Priming requests start at the QUERYTARGETS state, skipping
715 	 * the normal INIT state logic (which would cause an infloop). */
716 	if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS,
717 		qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
718 		&subq, 0)) {
719 		verbose(VERB_ALGO, "could not prime root");
720 		return 0;
721 	}
722 	if(subq) {
723 		struct iter_qstate* subiq =
724 			(struct iter_qstate*)subq->minfo[id];
725 		/* Set the initial delegation point to the hint.
726 		 * copy dp, it is now part of the root prime query.
727 		 * dp was part of in the fixed hints structure. */
728 		subiq->dp = delegpt_copy(dp, subq->region);
729 		if(!subiq->dp) {
730 			log_err("out of memory priming root, copydp");
731 			fptr_ok(fptr_whitelist_modenv_kill_sub(
732 				qstate->env->kill_sub));
733 			(*qstate->env->kill_sub)(subq);
734 			return 0;
735 		}
736 		/* there should not be any target queries. */
737 		subiq->num_target_queries = 0;
738 		subiq->dnssec_expected = iter_indicates_dnssec(
739 			qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
740 	}
741 
742 	/* this module stops, our submodule starts, and does the query. */
743 	qstate->ext_state[id] = module_wait_subquery;
744 	return 1;
745 }
746 
747 /**
748  * Generate and process a stub priming request. This method tests for the
749  * need to prime a stub zone, so it is safe to call for every request.
750  *
751  * @param qstate: the qtstate that triggered the need to prime.
752  * @param iq: iterator query state.
753  * @param id: module id.
754  * @param qname: request name.
755  * @param qclass: request class.
756  * @return true if a priming subrequest was made, false if not. The will only
757  *         issue a priming request if it detects an unprimed stub.
758  *         Uses value of 2 to signal during stub-prime in root-prime situation
759  *         that a noprime-stub is available and resolution can continue.
760  */
761 static int
762 prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
763 	uint8_t* qname, uint16_t qclass)
764 {
765 	/* Lookup the stub hint. This will return null if the stub doesn't
766 	 * need to be re-primed. */
767 	struct iter_hints_stub* stub;
768 	struct delegpt* stub_dp;
769 	struct module_qstate* subq;
770 
771 	if(!qname) return 0;
772 	stub = hints_lookup_stub(qstate->env->hints, qname, qclass, iq->dp);
773 	/* The stub (if there is one) does not need priming. */
774 	if(!stub)
775 		return 0;
776 	stub_dp = stub->dp;
777 	/* if we have an auth_zone dp, and stub is equal, don't prime stub
778 	 * yet, unless we want to fallback and avoid the auth_zone */
779 	if(!iq->auth_zone_avoid && iq->dp && iq->dp->auth_dp &&
780 		query_dname_compare(iq->dp->name, stub_dp->name) == 0)
781 		return 0;
782 
783 	/* is it a noprime stub (always use) */
784 	if(stub->noprime) {
785 		int r = 0;
786 		if(iq->dp == NULL) r = 2;
787 		/* copy the dp out of the fixed hints structure, so that
788 		 * it can be changed when servicing this query */
789 		iq->dp = delegpt_copy(stub_dp, qstate->region);
790 		if(!iq->dp) {
791 			log_err("out of memory priming stub");
792 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
793 			return 1; /* return 1 to make module stop, with error */
794 		}
795 		log_nametypeclass(VERB_DETAIL, "use stub", stub_dp->name,
796 			LDNS_RR_TYPE_NS, qclass);
797 		return r;
798 	}
799 
800 	/* Otherwise, we need to (re)prime the stub. */
801 	log_nametypeclass(VERB_DETAIL, "priming stub", stub_dp->name,
802 		LDNS_RR_TYPE_NS, qclass);
803 
804 	/* Stub priming events start at the QUERYTARGETS state to avoid the
805 	 * redundant INIT state processing. */
806 	if(!generate_sub_request(stub_dp->name, stub_dp->namelen,
807 		LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
808 		QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0)) {
809 		verbose(VERB_ALGO, "could not prime stub");
810 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
811 		return 1; /* return 1 to make module stop, with error */
812 	}
813 	if(subq) {
814 		struct iter_qstate* subiq =
815 			(struct iter_qstate*)subq->minfo[id];
816 
817 		/* Set the initial delegation point to the hint. */
818 		/* make copy to avoid use of stub dp by different qs/threads */
819 		subiq->dp = delegpt_copy(stub_dp, subq->region);
820 		if(!subiq->dp) {
821 			log_err("out of memory priming stub, copydp");
822 			fptr_ok(fptr_whitelist_modenv_kill_sub(
823 				qstate->env->kill_sub));
824 			(*qstate->env->kill_sub)(subq);
825 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
826 			return 1; /* return 1 to make module stop, with error */
827 		}
828 		/* there should not be any target queries -- although there
829 		 * wouldn't be anyway, since stub hints never have
830 		 * missing targets. */
831 		subiq->num_target_queries = 0;
832 		subiq->wait_priming_stub = 1;
833 		subiq->dnssec_expected = iter_indicates_dnssec(
834 			qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
835 	}
836 
837 	/* this module stops, our submodule starts, and does the query. */
838 	qstate->ext_state[id] = module_wait_subquery;
839 	return 1;
840 }
841 
842 /**
843  * Generate a delegation point for an auth zone (unless cached dp is better)
844  * false on alloc failure.
845  */
846 static int
847 auth_zone_delegpt(struct module_qstate* qstate, struct iter_qstate* iq,
848 	uint8_t* delname, size_t delnamelen)
849 {
850 	struct auth_zone* z;
851 	if(iq->auth_zone_avoid)
852 		return 1;
853 	if(!delname) {
854 		delname = iq->qchase.qname;
855 		delnamelen = iq->qchase.qname_len;
856 	}
857 	lock_rw_rdlock(&qstate->env->auth_zones->lock);
858 	z = auth_zones_find_zone(qstate->env->auth_zones, delname, delnamelen,
859 		qstate->qinfo.qclass);
860 	if(!z) {
861 		lock_rw_unlock(&qstate->env->auth_zones->lock);
862 		return 1;
863 	}
864 	lock_rw_rdlock(&z->lock);
865 	lock_rw_unlock(&qstate->env->auth_zones->lock);
866 	if(z->for_upstream) {
867 		if(iq->dp && query_dname_compare(z->name, iq->dp->name) == 0
868 			&& iq->dp->auth_dp && qstate->blacklist &&
869 			z->fallback_enabled) {
870 			/* cache is blacklisted and fallback, and we
871 			 * already have an auth_zone dp */
872 			if(verbosity>=VERB_ALGO) {
873 				char buf[255+1];
874 				dname_str(z->name, buf);
875 				verbose(VERB_ALGO, "auth_zone %s "
876 				  "fallback because cache blacklisted",
877 				  buf);
878 			}
879 			lock_rw_unlock(&z->lock);
880 			iq->dp = NULL;
881 			return 1;
882 		}
883 		if(iq->dp==NULL || dname_subdomain_c(z->name, iq->dp->name)) {
884 			struct delegpt* dp;
885 			if(qstate->blacklist && z->fallback_enabled) {
886 				/* cache is blacklisted because of a DNSSEC
887 				 * validation failure, and the zone allows
888 				 * fallback to the internet, query there. */
889 				if(verbosity>=VERB_ALGO) {
890 					char buf[255+1];
891 					dname_str(z->name, buf);
892 					verbose(VERB_ALGO, "auth_zone %s "
893 					  "fallback because cache blacklisted",
894 					  buf);
895 				}
896 				lock_rw_unlock(&z->lock);
897 				return 1;
898 			}
899 			dp = (struct delegpt*)regional_alloc_zero(
900 				qstate->region, sizeof(*dp));
901 			if(!dp) {
902 				log_err("alloc failure");
903 				if(z->fallback_enabled) {
904 					lock_rw_unlock(&z->lock);
905 					return 1; /* just fallback */
906 				}
907 				lock_rw_unlock(&z->lock);
908 				return 0;
909 			}
910 			dp->name = regional_alloc_init(qstate->region,
911 				z->name, z->namelen);
912 			if(!dp->name) {
913 				log_err("alloc failure");
914 				if(z->fallback_enabled) {
915 					lock_rw_unlock(&z->lock);
916 					return 1; /* just fallback */
917 				}
918 				lock_rw_unlock(&z->lock);
919 				return 0;
920 			}
921 			dp->namelen = z->namelen;
922 			dp->namelabs = z->namelabs;
923 			dp->auth_dp = 1;
924 			iq->dp = dp;
925 		}
926 	}
927 
928 	lock_rw_unlock(&z->lock);
929 	return 1;
930 }
931 
932 /**
933  * Generate A and AAAA checks for glue that is in-zone for the referral
934  * we just got to obtain authoritative information on the addresses.
935  *
936  * @param qstate: the qtstate that triggered the need to prime.
937  * @param iq: iterator query state.
938  * @param id: module id.
939  */
940 static void
941 generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
942 	int id)
943 {
944 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
945 	struct module_qstate* subq;
946 	size_t i;
947 	struct reply_info* rep = iq->response->rep;
948 	struct ub_packed_rrset_key* s;
949 	log_assert(iq->dp);
950 
951 	if(iq->depth == ie->max_dependency_depth)
952 		return;
953 	/* walk through additional, and check if in-zone,
954 	 * only relevant A, AAAA are left after scrub anyway */
955 	for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
956 		s = rep->rrsets[i];
957 		/* check *ALL* addresses that are transmitted in additional*/
958 		/* is it an address ? */
959 		if( !(ntohs(s->rk.type)==LDNS_RR_TYPE_A ||
960 			ntohs(s->rk.type)==LDNS_RR_TYPE_AAAA)) {
961 			continue;
962 		}
963 		/* is this query the same as the A/AAAA check for it */
964 		if(qstate->qinfo.qtype == ntohs(s->rk.type) &&
965 			qstate->qinfo.qclass == ntohs(s->rk.rrset_class) &&
966 			query_dname_compare(qstate->qinfo.qname,
967 				s->rk.dname)==0 &&
968 			(qstate->query_flags&BIT_RD) &&
969 			!(qstate->query_flags&BIT_CD))
970 			continue;
971 
972 		/* generate subrequest for it */
973 		log_nametypeclass(VERB_ALGO, "schedule addr fetch",
974 			s->rk.dname, ntohs(s->rk.type),
975 			ntohs(s->rk.rrset_class));
976 		if(!generate_sub_request(s->rk.dname, s->rk.dname_len,
977 			ntohs(s->rk.type), ntohs(s->rk.rrset_class),
978 			qstate, id, iq,
979 			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
980 			verbose(VERB_ALGO, "could not generate addr check");
981 			return;
982 		}
983 		/* ignore subq - not need for more init */
984 	}
985 }
986 
987 /**
988  * Generate a NS check request to obtain authoritative information
989  * on an NS rrset.
990  *
991  * @param qstate: the qtstate that triggered the need to prime.
992  * @param iq: iterator query state.
993  * @param id: module id.
994  */
995 static void
996 generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
997 {
998 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
999 	struct module_qstate* subq;
1000 	log_assert(iq->dp);
1001 
1002 	if(iq->depth == ie->max_dependency_depth)
1003 		return;
1004 	if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen,
1005 		iq->qchase.qclass, NULL))
1006 		return;
1007 	/* is this query the same as the nscheck? */
1008 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS &&
1009 		query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
1010 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
1011 		/* spawn off A, AAAA queries for in-zone glue to check */
1012 		generate_a_aaaa_check(qstate, iq, id);
1013 		return;
1014 	}
1015 	/* no need to get the NS record for DS, it is above the zonecut */
1016 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_DS)
1017 		return;
1018 
1019 	log_nametypeclass(VERB_ALGO, "schedule ns fetch",
1020 		iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1021 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
1022 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1023 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
1024 		verbose(VERB_ALGO, "could not generate ns check");
1025 		return;
1026 	}
1027 	if(subq) {
1028 		struct iter_qstate* subiq =
1029 			(struct iter_qstate*)subq->minfo[id];
1030 
1031 		/* make copy to avoid use of stub dp by different qs/threads */
1032 		/* refetch glue to start higher up the tree */
1033 		subiq->refetch_glue = 1;
1034 		subiq->dp = delegpt_copy(iq->dp, subq->region);
1035 		if(!subiq->dp) {
1036 			log_err("out of memory generating ns check, copydp");
1037 			fptr_ok(fptr_whitelist_modenv_kill_sub(
1038 				qstate->env->kill_sub));
1039 			(*qstate->env->kill_sub)(subq);
1040 			return;
1041 		}
1042 	}
1043 }
1044 
1045 /**
1046  * Generate a DNSKEY prefetch query to get the DNSKEY for the DS record we
1047  * just got in a referral (where we have dnssec_expected, thus have trust
1048  * anchors above it).  Note that right after calling this routine the
1049  * iterator detached subqueries (because of following the referral), and thus
1050  * the DNSKEY query becomes detached, its return stored in the cache for
1051  * later lookup by the validator.  This cache lookup by the validator avoids
1052  * the roundtrip incurred by the DNSKEY query.  The DNSKEY query is now
1053  * performed at about the same time the original query is sent to the domain,
1054  * thus the two answers are likely to be returned at about the same time,
1055  * saving a roundtrip from the validated lookup.
1056  *
1057  * @param qstate: the qtstate that triggered the need to prime.
1058  * @param iq: iterator query state.
1059  * @param id: module id.
1060  */
1061 static void
1062 generate_dnskey_prefetch(struct module_qstate* qstate,
1063 	struct iter_qstate* iq, int id)
1064 {
1065 	struct module_qstate* subq;
1066 	log_assert(iq->dp);
1067 
1068 	/* is this query the same as the prefetch? */
1069 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
1070 		query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
1071 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
1072 		return;
1073 	}
1074 
1075 	/* if the DNSKEY is in the cache this lookup will stop quickly */
1076 	log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch",
1077 		iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
1078 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
1079 		LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
1080 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
1081 		/* we'll be slower, but it'll work */
1082 		verbose(VERB_ALGO, "could not generate dnskey prefetch");
1083 		return;
1084 	}
1085 	if(subq) {
1086 		struct iter_qstate* subiq =
1087 			(struct iter_qstate*)subq->minfo[id];
1088 		/* this qstate has the right delegation for the dnskey lookup*/
1089 		/* make copy to avoid use of stub dp by different qs/threads */
1090 		subiq->dp = delegpt_copy(iq->dp, subq->region);
1091 		/* if !subiq->dp, it'll start from the cache, no problem */
1092 	}
1093 }
1094 
1095 /**
1096  * See if the query needs forwarding.
1097  *
1098  * @param qstate: query state.
1099  * @param iq: iterator query state.
1100  * @return true if the request is forwarded, false if not.
1101  * 	If returns true but, iq->dp is NULL then a malloc failure occurred.
1102  */
1103 static int
1104 forward_request(struct module_qstate* qstate, struct iter_qstate* iq)
1105 {
1106 	struct delegpt* dp;
1107 	uint8_t* delname = iq->qchase.qname;
1108 	size_t delnamelen = iq->qchase.qname_len;
1109 	if(iq->refetch_glue) {
1110 		delname = iq->dp->name;
1111 		delnamelen = iq->dp->namelen;
1112 	}
1113 	/* strip one label off of DS query to lookup higher for it */
1114 	if( (iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue)
1115 		&& !dname_is_root(iq->qchase.qname))
1116 		dname_remove_label(&delname, &delnamelen);
1117 	dp = forwards_lookup(qstate->env->fwds, delname, iq->qchase.qclass);
1118 	if(!dp)
1119 		return 0;
1120 	/* send recursion desired to forward addr */
1121 	iq->chase_flags |= BIT_RD;
1122 	iq->dp = delegpt_copy(dp, qstate->region);
1123 	/* iq->dp checked by caller */
1124 	verbose(VERB_ALGO, "forwarding request");
1125 	return 1;
1126 }
1127 
1128 /**
1129  * Process the initial part of the request handling. This state roughly
1130  * corresponds to resolver algorithms steps 1 (find answer in cache) and 2
1131  * (find the best servers to ask).
1132  *
1133  * Note that all requests start here, and query restarts revisit this state.
1134  *
1135  * This state either generates: 1) a response, from cache or error, 2) a
1136  * priming event, or 3) forwards the request to the next state (init2,
1137  * generally).
1138  *
1139  * @param qstate: query state.
1140  * @param iq: iterator query state.
1141  * @param ie: iterator shared global environment.
1142  * @param id: module id.
1143  * @return true if the event needs more request processing immediately,
1144  *         false if not.
1145  */
1146 static int
1147 processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
1148 	struct iter_env* ie, int id)
1149 {
1150 	uint8_t* delname;
1151 	size_t delnamelen;
1152 	struct dns_msg* msg = NULL;
1153 
1154 	log_query_info(VERB_DETAIL, "resolving", &qstate->qinfo);
1155 	/* check effort */
1156 
1157 	/* We enforce a maximum number of query restarts. This is primarily a
1158 	 * cheap way to prevent CNAME loops. */
1159 	if(iq->query_restart_count > MAX_RESTART_COUNT) {
1160 		verbose(VERB_QUERY, "request has exceeded the maximum number"
1161 			" of query restarts with %d", iq->query_restart_count);
1162 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1163 	}
1164 
1165 	/* We enforce a maximum recursion/dependency depth -- in general,
1166 	 * this is unnecessary for dependency loops (although it will
1167 	 * catch those), but it provides a sensible limit to the amount
1168 	 * of work required to answer a given query. */
1169 	verbose(VERB_ALGO, "request has dependency depth of %d", iq->depth);
1170 	if(iq->depth > ie->max_dependency_depth) {
1171 		verbose(VERB_QUERY, "request has exceeded the maximum "
1172 			"dependency depth with depth of %d", iq->depth);
1173 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1174 	}
1175 
1176 	/* If the request is qclass=ANY, setup to generate each class */
1177 	if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
1178 		iq->qchase.qclass = 0;
1179 		return next_state(iq, COLLECT_CLASS_STATE);
1180 	}
1181 
1182 	/*
1183 	 * If we are restricted by a forward-zone or a stub-zone, we
1184 	 * can't re-fetch glue for this delegation point.
1185 	 * we won’t try to re-fetch glue if the iq->dp is null.
1186 	 */
1187 	if (iq->refetch_glue &&
1188 	        iq->dp &&
1189 	        !can_have_last_resort(qstate->env, iq->dp->name,
1190 	             iq->dp->namelen, iq->qchase.qclass, NULL)) {
1191 	    iq->refetch_glue = 0;
1192 	}
1193 
1194 	/* Resolver Algorithm Step 1 -- Look for the answer in local data. */
1195 
1196 	/* This either results in a query restart (CNAME cache response), a
1197 	 * terminating response (ANSWER), or a cache miss (null). */
1198 
1199 	if(qstate->blacklist) {
1200 		/* if cache, or anything else, was blacklisted then
1201 		 * getting older results from cache is a bad idea, no cache */
1202 		verbose(VERB_ALGO, "cache blacklisted, going to the network");
1203 		msg = NULL;
1204 	} else if(!qstate->no_cache_lookup) {
1205 		msg = dns_cache_lookup(qstate->env, iq->qchase.qname,
1206 			iq->qchase.qname_len, iq->qchase.qtype,
1207 			iq->qchase.qclass, qstate->query_flags,
1208 			qstate->region, qstate->env->scratch, 0);
1209 		if(!msg && qstate->env->neg_cache &&
1210 			iter_qname_indicates_dnssec(qstate->env, &iq->qchase)) {
1211 			/* lookup in negative cache; may result in
1212 			 * NOERROR/NODATA or NXDOMAIN answers that need validation */
1213 			msg = val_neg_getmsg(qstate->env->neg_cache, &iq->qchase,
1214 				qstate->region, qstate->env->rrset_cache,
1215 				qstate->env->scratch_buffer,
1216 				*qstate->env->now, 1/*add SOA*/, NULL,
1217 				qstate->env->cfg);
1218 		}
1219 		/* item taken from cache does not match our query name, thus
1220 		 * security needs to be re-examined later */
1221 		if(msg && query_dname_compare(qstate->qinfo.qname,
1222 			iq->qchase.qname) != 0)
1223 			msg->rep->security = sec_status_unchecked;
1224 	}
1225 	if(msg) {
1226 		/* handle positive cache response */
1227 		enum response_type type = response_type_from_cache(msg,
1228 			&iq->qchase);
1229 		if(verbosity >= VERB_ALGO) {
1230 			log_dns_msg("msg from cache lookup", &msg->qinfo,
1231 				msg->rep);
1232 			verbose(VERB_ALGO, "msg ttl is %d, prefetch ttl %d",
1233 				(int)msg->rep->ttl,
1234 				(int)msg->rep->prefetch_ttl);
1235 		}
1236 
1237 		if(type == RESPONSE_TYPE_CNAME) {
1238 			uint8_t* sname = 0;
1239 			size_t slen = 0;
1240 			verbose(VERB_ALGO, "returning CNAME response from "
1241 				"cache");
1242 			if(!handle_cname_response(qstate, iq, msg,
1243 				&sname, &slen))
1244 				return error_response(qstate, id,
1245 					LDNS_RCODE_SERVFAIL);
1246 			iq->qchase.qname = sname;
1247 			iq->qchase.qname_len = slen;
1248 			/* This *is* a query restart, even if it is a cheap
1249 			 * one. */
1250 			iq->dp = NULL;
1251 			iq->refetch_glue = 0;
1252 			iq->query_restart_count++;
1253 			iq->sent_count = 0;
1254 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1255 			if(qstate->env->cfg->qname_minimisation)
1256 				iq->minimisation_state = INIT_MINIMISE_STATE;
1257 			return next_state(iq, INIT_REQUEST_STATE);
1258 		}
1259 
1260 		/* if from cache, NULL, else insert 'cache IP' len=0 */
1261 		if(qstate->reply_origin)
1262 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1263 		/* it is an answer, response, to final state */
1264 		verbose(VERB_ALGO, "returning answer from cache.");
1265 		iq->response = msg;
1266 		return final_state(iq);
1267 	}
1268 
1269 	/* attempt to forward the request */
1270 	if(forward_request(qstate, iq))
1271 	{
1272 		if(!iq->dp) {
1273 			log_err("alloc failure for forward dp");
1274 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1275 		}
1276 		iq->refetch_glue = 0;
1277 		iq->minimisation_state = DONOT_MINIMISE_STATE;
1278 		/* the request has been forwarded.
1279 		 * forwarded requests need to be immediately sent to the
1280 		 * next state, QUERYTARGETS. */
1281 		return next_state(iq, QUERYTARGETS_STATE);
1282 	}
1283 
1284 	/* Resolver Algorithm Step 2 -- find the "best" servers. */
1285 
1286 	/* first, adjust for DS queries. To avoid the grandparent problem,
1287 	 * we just look for the closest set of server to the parent of qname.
1288 	 * When re-fetching glue we also need to ask the parent.
1289 	 */
1290 	if(iq->refetch_glue) {
1291 		if(!iq->dp) {
1292 			log_err("internal or malloc fail: no dp for refetch");
1293 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1294 		}
1295 		delname = iq->dp->name;
1296 		delnamelen = iq->dp->namelen;
1297 	} else {
1298 		delname = iq->qchase.qname;
1299 		delnamelen = iq->qchase.qname_len;
1300 	}
1301 	if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue ||
1302 	   (iq->qchase.qtype == LDNS_RR_TYPE_NS && qstate->prefetch_leeway
1303 	   && can_have_last_resort(qstate->env, delname, delnamelen, iq->qchase.qclass, NULL))) {
1304 		/* remove first label from delname, root goes to hints,
1305 		 * but only to fetch glue, not for qtype=DS. */
1306 		/* also when prefetching an NS record, fetch it again from
1307 		 * its parent, just as if it expired, so that you do not
1308 		 * get stuck on an older nameserver that gives old NSrecords */
1309 		if(dname_is_root(delname) && (iq->refetch_glue ||
1310 			(iq->qchase.qtype == LDNS_RR_TYPE_NS &&
1311 			qstate->prefetch_leeway)))
1312 			delname = NULL; /* go to root priming */
1313 		else 	dname_remove_label(&delname, &delnamelen);
1314 	}
1315 	/* delname is the name to lookup a delegation for. If NULL rootprime */
1316 	while(1) {
1317 
1318 		/* Lookup the delegation in the cache. If null, then the
1319 		 * cache needs to be primed for the qclass. */
1320 		if(delname)
1321 		     iq->dp = dns_cache_find_delegation(qstate->env, delname,
1322 			delnamelen, iq->qchase.qtype, iq->qchase.qclass,
1323 			qstate->region, &iq->deleg_msg,
1324 			*qstate->env->now+qstate->prefetch_leeway);
1325 		else iq->dp = NULL;
1326 
1327 		/* If the cache has returned nothing, then we have a
1328 		 * root priming situation. */
1329 		if(iq->dp == NULL) {
1330 			int r;
1331 			/* if under auth zone, no prime needed */
1332 			if(!auth_zone_delegpt(qstate, iq, delname, delnamelen))
1333 				return error_response(qstate, id,
1334 					LDNS_RCODE_SERVFAIL);
1335 			if(iq->dp) /* use auth zone dp */
1336 				return next_state(iq, INIT_REQUEST_2_STATE);
1337 			/* if there is a stub, then no root prime needed */
1338 			r = prime_stub(qstate, iq, id, delname,
1339 				iq->qchase.qclass);
1340 			if(r == 2)
1341 				break; /* got noprime-stub-zone, continue */
1342 			else if(r)
1343 				return 0; /* stub prime request made */
1344 			if(forwards_lookup_root(qstate->env->fwds,
1345 				iq->qchase.qclass)) {
1346 				/* forward zone root, no root prime needed */
1347 				/* fill in some dp - safety belt */
1348 				iq->dp = hints_lookup_root(qstate->env->hints,
1349 					iq->qchase.qclass);
1350 				if(!iq->dp) {
1351 					log_err("internal error: no hints dp");
1352 					return error_response(qstate, id,
1353 						LDNS_RCODE_SERVFAIL);
1354 				}
1355 				iq->dp = delegpt_copy(iq->dp, qstate->region);
1356 				if(!iq->dp) {
1357 					log_err("out of memory in safety belt");
1358 					return error_response(qstate, id,
1359 						LDNS_RCODE_SERVFAIL);
1360 				}
1361 				return next_state(iq, INIT_REQUEST_2_STATE);
1362 			}
1363 			/* Note that the result of this will set a new
1364 			 * DelegationPoint based on the result of priming. */
1365 			if(!prime_root(qstate, iq, id, iq->qchase.qclass))
1366 				return error_response(qstate, id,
1367 					LDNS_RCODE_REFUSED);
1368 
1369 			/* priming creates and sends a subordinate query, with
1370 			 * this query as the parent. So further processing for
1371 			 * this event will stop until reactivated by the
1372 			 * results of priming. */
1373 			return 0;
1374 		}
1375 		if(!iq->ratelimit_ok && qstate->prefetch_leeway)
1376 			iq->ratelimit_ok = 1; /* allow prefetches, this keeps
1377 			otherwise valid data in the cache */
1378 		if(!iq->ratelimit_ok && infra_ratelimit_exceeded(
1379 			qstate->env->infra_cache, iq->dp->name,
1380 			iq->dp->namelen, *qstate->env->now)) {
1381 			/* and increment the rate, so that the rate for time
1382 			 * now will also exceed the rate, keeping cache fresh */
1383 			(void)infra_ratelimit_inc(qstate->env->infra_cache,
1384 				iq->dp->name, iq->dp->namelen,
1385 				*qstate->env->now);
1386 			/* see if we are passed through with slip factor */
1387 			if(qstate->env->cfg->ratelimit_factor != 0 &&
1388 				ub_random_max(qstate->env->rnd,
1389 				    qstate->env->cfg->ratelimit_factor) == 1) {
1390 				iq->ratelimit_ok = 1;
1391 				log_nametypeclass(VERB_ALGO, "ratelimit allowed through for "
1392 					"delegation point", iq->dp->name,
1393 					LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1394 			} else {
1395 				lock_basic_lock(&ie->queries_ratelimit_lock);
1396 				ie->num_queries_ratelimited++;
1397 				lock_basic_unlock(&ie->queries_ratelimit_lock);
1398 				log_nametypeclass(VERB_ALGO, "ratelimit exceeded with "
1399 					"delegation point", iq->dp->name,
1400 					LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1401 				return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1402 			}
1403 		}
1404 
1405 		/* see if this dp not useless.
1406 		 * It is useless if:
1407 		 *	o all NS items are required glue.
1408 		 *	  or the query is for NS item that is required glue.
1409 		 *	o no addresses are provided.
1410 		 *	o RD qflag is on.
1411 		 * Instead, go up one level, and try to get even further
1412 		 * If the root was useless, use safety belt information.
1413 		 * Only check cache returns, because replies for servers
1414 		 * could be useless but lead to loops (bumping into the
1415 		 * same server reply) if useless-checked.
1416 		 */
1417 		if(iter_dp_is_useless(&qstate->qinfo, qstate->query_flags,
1418 			iq->dp)) {
1419 			struct delegpt* retdp = NULL;
1420 			if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen, iq->qchase.qclass, &retdp)) {
1421 				if(retdp) {
1422 					verbose(VERB_QUERY, "cache has stub "
1423 						"or fwd but no addresses, "
1424 						"fallback to config");
1425 					iq->dp = delegpt_copy(retdp,
1426 						qstate->region);
1427 					if(!iq->dp) {
1428 						log_err("out of memory in "
1429 							"stub/fwd fallback");
1430 						return error_response(qstate,
1431 						    id, LDNS_RCODE_SERVFAIL);
1432 					}
1433 					break;
1434 				}
1435 				verbose(VERB_ALGO, "useless dp "
1436 					"but cannot go up, servfail");
1437 				delegpt_log(VERB_ALGO, iq->dp);
1438 				return error_response(qstate, id,
1439 					LDNS_RCODE_SERVFAIL);
1440 			}
1441 			if(dname_is_root(iq->dp->name)) {
1442 				/* use safety belt */
1443 				verbose(VERB_QUERY, "Cache has root NS but "
1444 				"no addresses. Fallback to the safety belt.");
1445 				iq->dp = hints_lookup_root(qstate->env->hints,
1446 					iq->qchase.qclass);
1447 				/* note deleg_msg is from previous lookup,
1448 				 * but RD is on, so it is not used */
1449 				if(!iq->dp) {
1450 					log_err("internal error: no hints dp");
1451 					return error_response(qstate, id,
1452 						LDNS_RCODE_REFUSED);
1453 				}
1454 				iq->dp = delegpt_copy(iq->dp, qstate->region);
1455 				if(!iq->dp) {
1456 					log_err("out of memory in safety belt");
1457 					return error_response(qstate, id,
1458 						LDNS_RCODE_SERVFAIL);
1459 				}
1460 				break;
1461 			} else {
1462 				verbose(VERB_ALGO,
1463 					"cache delegation was useless:");
1464 				delegpt_log(VERB_ALGO, iq->dp);
1465 				/* go up */
1466 				delname = iq->dp->name;
1467 				delnamelen = iq->dp->namelen;
1468 				dname_remove_label(&delname, &delnamelen);
1469 			}
1470 		} else break;
1471 	}
1472 
1473 	verbose(VERB_ALGO, "cache delegation returns delegpt");
1474 	delegpt_log(VERB_ALGO, iq->dp);
1475 
1476 	/* Otherwise, set the current delegation point and move on to the
1477 	 * next state. */
1478 	return next_state(iq, INIT_REQUEST_2_STATE);
1479 }
1480 
1481 /**
1482  * Process the second part of the initial request handling. This state
1483  * basically exists so that queries that generate root priming events have
1484  * the same init processing as ones that do not. Request events that reach
1485  * this state must have a valid currentDelegationPoint set.
1486  *
1487  * This part is primarily handling stub zone priming. Events that reach this
1488  * state must have a current delegation point.
1489  *
1490  * @param qstate: query state.
1491  * @param iq: iterator query state.
1492  * @param id: module id.
1493  * @return true if the event needs more request processing immediately,
1494  *         false if not.
1495  */
1496 static int
1497 processInitRequest2(struct module_qstate* qstate, struct iter_qstate* iq,
1498 	int id)
1499 {
1500 	uint8_t* delname;
1501 	size_t delnamelen;
1502 	log_query_info(VERB_QUERY, "resolving (init part 2): ",
1503 		&qstate->qinfo);
1504 
1505 	delname = iq->qchase.qname;
1506 	delnamelen = iq->qchase.qname_len;
1507 	if(iq->refetch_glue) {
1508 		struct iter_hints_stub* stub;
1509 		if(!iq->dp) {
1510 			log_err("internal or malloc fail: no dp for refetch");
1511 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1512 		}
1513 		/* Do not send queries above stub, do not set delname to dp if
1514 		 * this is above stub without stub-first. */
1515 		stub = hints_lookup_stub(
1516 			qstate->env->hints, iq->qchase.qname, iq->qchase.qclass,
1517 			iq->dp);
1518 		if(!stub || !stub->dp->has_parent_side_NS ||
1519 			dname_subdomain_c(iq->dp->name, stub->dp->name)) {
1520 			delname = iq->dp->name;
1521 			delnamelen = iq->dp->namelen;
1522 		}
1523 	}
1524 	if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue) {
1525 		if(!dname_is_root(delname))
1526 			dname_remove_label(&delname, &delnamelen);
1527 		iq->refetch_glue = 0; /* if CNAME causes restart, no refetch */
1528 	}
1529 
1530 	/* see if we have an auth zone to answer from, improves dp from cache
1531 	 * (if any dp from cache) with auth zone dp, if that is lower */
1532 	if(!auth_zone_delegpt(qstate, iq, delname, delnamelen))
1533 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1534 
1535 	/* Check to see if we need to prime a stub zone. */
1536 	if(prime_stub(qstate, iq, id, delname, iq->qchase.qclass)) {
1537 		/* A priming sub request was made */
1538 		return 0;
1539 	}
1540 
1541 	/* most events just get forwarded to the next state. */
1542 	return next_state(iq, INIT_REQUEST_3_STATE);
1543 }
1544 
1545 /**
1546  * Process the third part of the initial request handling. This state exists
1547  * as a separate state so that queries that generate stub priming events
1548  * will get the tail end of the init process but not repeat the stub priming
1549  * check.
1550  *
1551  * @param qstate: query state.
1552  * @param iq: iterator query state.
1553  * @param id: module id.
1554  * @return true, advancing the event to the QUERYTARGETS_STATE.
1555  */
1556 static int
1557 processInitRequest3(struct module_qstate* qstate, struct iter_qstate* iq,
1558 	int id)
1559 {
1560 	log_query_info(VERB_QUERY, "resolving (init part 3): ",
1561 		&qstate->qinfo);
1562 	/* if the cache reply dp equals a validation anchor or msg has DS,
1563 	 * then DNSSEC RRSIGs are expected in the reply */
1564 	iq->dnssec_expected = iter_indicates_dnssec(qstate->env, iq->dp,
1565 		iq->deleg_msg, iq->qchase.qclass);
1566 
1567 	/* If the RD flag wasn't set, then we just finish with the
1568 	 * cached referral as the response. */
1569 	if(!(qstate->query_flags & BIT_RD) && iq->deleg_msg) {
1570 		iq->response = iq->deleg_msg;
1571 		if(verbosity >= VERB_ALGO && iq->response)
1572 			log_dns_msg("no RD requested, using delegation msg",
1573 				&iq->response->qinfo, iq->response->rep);
1574 		if(qstate->reply_origin)
1575 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1576 		return final_state(iq);
1577 	}
1578 	/* After this point, unset the RD flag -- this query is going to
1579 	 * be sent to an auth. server. */
1580 	iq->chase_flags &= ~BIT_RD;
1581 
1582 	/* if dnssec expected, fetch key for the trust-anchor or cached-DS */
1583 	if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
1584 		!(qstate->query_flags&BIT_CD)) {
1585 		generate_dnskey_prefetch(qstate, iq, id);
1586 		fptr_ok(fptr_whitelist_modenv_detach_subs(
1587 			qstate->env->detach_subs));
1588 		(*qstate->env->detach_subs)(qstate);
1589 	}
1590 
1591 	/* Jump to the next state. */
1592 	return next_state(iq, QUERYTARGETS_STATE);
1593 }
1594 
1595 /**
1596  * Given a basic query, generate a parent-side "target" query.
1597  * These are subordinate queries for missing delegation point target addresses,
1598  * for which only the parent of the delegation provides correct IP addresses.
1599  *
1600  * @param qstate: query state.
1601  * @param iq: iterator query state.
1602  * @param id: module id.
1603  * @param name: target qname.
1604  * @param namelen: target qname length.
1605  * @param qtype: target qtype (either A or AAAA).
1606  * @param qclass: target qclass.
1607  * @return true on success, false on failure.
1608  */
1609 static int
1610 generate_parentside_target_query(struct module_qstate* qstate,
1611 	struct iter_qstate* iq, int id, uint8_t* name, size_t namelen,
1612 	uint16_t qtype, uint16_t qclass)
1613 {
1614 	struct module_qstate* subq;
1615 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1616 		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1617 		return 0;
1618 	if(subq) {
1619 		struct iter_qstate* subiq =
1620 			(struct iter_qstate*)subq->minfo[id];
1621 		/* blacklist the cache - we want to fetch parent stuff */
1622 		sock_list_insert(&subq->blacklist, NULL, 0, subq->region);
1623 		subiq->query_for_pside_glue = 1;
1624 		if(dname_subdomain_c(name, iq->dp->name)) {
1625 			subiq->dp = delegpt_copy(iq->dp, subq->region);
1626 			subiq->dnssec_expected = iter_indicates_dnssec(
1627 				qstate->env, subiq->dp, NULL,
1628 				subq->qinfo.qclass);
1629 			subiq->refetch_glue = 1;
1630 		} else {
1631 			subiq->dp = dns_cache_find_delegation(qstate->env,
1632 				name, namelen, qtype, qclass, subq->region,
1633 				&subiq->deleg_msg,
1634 				*qstate->env->now+subq->prefetch_leeway);
1635 			/* if no dp, then it's from root, refetch unneeded */
1636 			if(subiq->dp) {
1637 				subiq->dnssec_expected = iter_indicates_dnssec(
1638 					qstate->env, subiq->dp, NULL,
1639 					subq->qinfo.qclass);
1640 				subiq->refetch_glue = 1;
1641 			}
1642 		}
1643 	}
1644 	log_nametypeclass(VERB_QUERY, "new pside target", name, qtype, qclass);
1645 	return 1;
1646 }
1647 
1648 /**
1649  * Given a basic query, generate a "target" query. These are subordinate
1650  * queries for missing delegation point target addresses.
1651  *
1652  * @param qstate: query state.
1653  * @param iq: iterator query state.
1654  * @param id: module id.
1655  * @param name: target qname.
1656  * @param namelen: target qname length.
1657  * @param qtype: target qtype (either A or AAAA).
1658  * @param qclass: target qclass.
1659  * @return true on success, false on failure.
1660  */
1661 static int
1662 generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
1663         int id, uint8_t* name, size_t namelen, uint16_t qtype, uint16_t qclass)
1664 {
1665 	struct module_qstate* subq;
1666 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1667 		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1668 		return 0;
1669 	log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
1670 	return 1;
1671 }
1672 
1673 /**
1674  * Given an event at a certain state, generate zero or more target queries
1675  * for it's current delegation point.
1676  *
1677  * @param qstate: query state.
1678  * @param iq: iterator query state.
1679  * @param ie: iterator shared global environment.
1680  * @param id: module id.
1681  * @param maxtargets: The maximum number of targets to query for.
1682  *	if it is negative, there is no maximum number of targets.
1683  * @param num: returns the number of queries generated and processed,
1684  *	which may be zero if there were no missing targets.
1685  * @return false on error.
1686  */
1687 static int
1688 query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
1689         struct iter_env* ie, int id, int maxtargets, int* num)
1690 {
1691 	int query_count = 0;
1692 	struct delegpt_ns* ns;
1693 	int missing;
1694 	int toget = 0;
1695 
1696 	if(iq->depth == ie->max_dependency_depth)
1697 		return 0;
1698 	if(iq->depth > 0 && iq->target_count &&
1699 		iq->target_count[1] > MAX_TARGET_COUNT) {
1700 		char s[LDNS_MAX_DOMAINLEN+1];
1701 		dname_str(qstate->qinfo.qname, s);
1702 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
1703 			"number of glue fetches %d", s, iq->target_count[1]);
1704 		return 0;
1705 	}
1706 
1707 	iter_mark_cycle_targets(qstate, iq->dp);
1708 	missing = (int)delegpt_count_missing_targets(iq->dp);
1709 	log_assert(maxtargets != 0); /* that would not be useful */
1710 
1711 	/* Generate target requests. Basically, any missing targets
1712 	 * are queried for here, regardless if it is necessary to do
1713 	 * so to continue processing. */
1714 	if(maxtargets < 0 || maxtargets > missing)
1715 		toget = missing;
1716 	else	toget = maxtargets;
1717 	if(toget == 0) {
1718 		*num = 0;
1719 		return 1;
1720 	}
1721 	/* select 'toget' items from the total of 'missing' items */
1722 	log_assert(toget <= missing);
1723 
1724 	/* loop over missing targets */
1725 	for(ns = iq->dp->nslist; ns; ns = ns->next) {
1726 		if(ns->resolved)
1727 			continue;
1728 
1729 		/* randomly select this item with probability toget/missing */
1730 		if(!iter_ns_probability(qstate->env->rnd, toget, missing)) {
1731 			/* do not select this one, next; select toget number
1732 			 * of items from a list one less in size */
1733 			missing --;
1734 			continue;
1735 		}
1736 
1737 		if(ie->supports_ipv6 && !ns->got6) {
1738 			/* Send the AAAA request. */
1739 			if(!generate_target_query(qstate, iq, id,
1740 				ns->name, ns->namelen,
1741 				LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
1742 				*num = query_count;
1743 				if(query_count > 0)
1744 					qstate->ext_state[id] = module_wait_subquery;
1745 				return 0;
1746 			}
1747 			query_count++;
1748 		}
1749 		/* Send the A request. */
1750 		if(ie->supports_ipv4 && !ns->got4) {
1751 			if(!generate_target_query(qstate, iq, id,
1752 				ns->name, ns->namelen,
1753 				LDNS_RR_TYPE_A, iq->qchase.qclass)) {
1754 				*num = query_count;
1755 				if(query_count > 0)
1756 					qstate->ext_state[id] = module_wait_subquery;
1757 				return 0;
1758 			}
1759 			query_count++;
1760 		}
1761 
1762 		/* mark this target as in progress. */
1763 		ns->resolved = 1;
1764 		missing--;
1765 		toget--;
1766 		if(toget == 0)
1767 			break;
1768 	}
1769 	*num = query_count;
1770 	if(query_count > 0)
1771 		qstate->ext_state[id] = module_wait_subquery;
1772 
1773 	return 1;
1774 }
1775 
1776 /**
1777  * Called by processQueryTargets when it would like extra targets to query
1778  * but it seems to be out of options.  At last resort some less appealing
1779  * options are explored.  If there are no more options, the result is SERVFAIL
1780  *
1781  * @param qstate: query state.
1782  * @param iq: iterator query state.
1783  * @param ie: iterator shared global environment.
1784  * @param id: module id.
1785  * @return true if the event requires more request processing immediately,
1786  *         false if not.
1787  */
1788 static int
1789 processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
1790 	struct iter_env* ie, int id)
1791 {
1792 	struct delegpt_ns* ns;
1793 	int query_count = 0;
1794 	verbose(VERB_ALGO, "No more query targets, attempting last resort");
1795 	log_assert(iq->dp);
1796 
1797 	if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen,
1798 		iq->qchase.qclass, NULL)) {
1799 		/* fail -- no more targets, no more hope of targets, no hope
1800 		 * of a response. */
1801 		verbose(VERB_QUERY, "configured stub or forward servers failed -- returning SERVFAIL");
1802 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1803 	}
1804 	if(!iq->dp->has_parent_side_NS && dname_is_root(iq->dp->name)) {
1805 		struct delegpt* p = hints_lookup_root(qstate->env->hints,
1806 			iq->qchase.qclass);
1807 		if(p) {
1808 			struct delegpt_ns* ns;
1809 			struct delegpt_addr* a;
1810 			iq->chase_flags &= ~BIT_RD; /* go to authorities */
1811 			for(ns = p->nslist; ns; ns=ns->next) {
1812 				(void)delegpt_add_ns(iq->dp, qstate->region,
1813 					ns->name, ns->lame);
1814 			}
1815 			for(a = p->target_list; a; a=a->next_target) {
1816 				(void)delegpt_add_addr(iq->dp, qstate->region,
1817 					&a->addr, a->addrlen, a->bogus,
1818 					a->lame, a->tls_auth_name);
1819 			}
1820 		}
1821 		iq->dp->has_parent_side_NS = 1;
1822 	} else if(!iq->dp->has_parent_side_NS) {
1823 		if(!iter_lookup_parent_NS_from_cache(qstate->env, iq->dp,
1824 			qstate->region, &qstate->qinfo)
1825 			|| !iq->dp->has_parent_side_NS) {
1826 			/* if: malloc failure in lookup go up to try */
1827 			/* if: no parent NS in cache - go up one level */
1828 			verbose(VERB_ALGO, "try to grab parent NS");
1829 			iq->store_parent_NS = iq->dp;
1830 			iq->chase_flags &= ~BIT_RD; /* go to authorities */
1831 			iq->deleg_msg = NULL;
1832 			iq->refetch_glue = 1;
1833 			iq->query_restart_count++;
1834 			iq->sent_count = 0;
1835 			if(qstate->env->cfg->qname_minimisation)
1836 				iq->minimisation_state = INIT_MINIMISE_STATE;
1837 			return next_state(iq, INIT_REQUEST_STATE);
1838 		}
1839 	}
1840 	/* see if that makes new names available */
1841 	if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
1842 		qstate->region, iq->dp))
1843 		log_err("out of memory in cache_fill_missing");
1844 	if(iq->dp->usable_list) {
1845 		verbose(VERB_ALGO, "try parent-side-name, w. glue from cache");
1846 		return next_state(iq, QUERYTARGETS_STATE);
1847 	}
1848 	/* try to fill out parent glue from cache */
1849 	if(iter_lookup_parent_glue_from_cache(qstate->env, iq->dp,
1850 		qstate->region, &qstate->qinfo)) {
1851 		/* got parent stuff from cache, see if we can continue */
1852 		verbose(VERB_ALGO, "try parent-side glue from cache");
1853 		return next_state(iq, QUERYTARGETS_STATE);
1854 	}
1855 	/* query for an extra name added by the parent-NS record */
1856 	if(delegpt_count_missing_targets(iq->dp) > 0) {
1857 		int qs = 0;
1858 		verbose(VERB_ALGO, "try parent-side target name");
1859 		if(!query_for_targets(qstate, iq, ie, id, 1, &qs)) {
1860 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1861 		}
1862 		iq->num_target_queries += qs;
1863 		target_count_increase(iq, qs);
1864 		if(qs != 0) {
1865 			qstate->ext_state[id] = module_wait_subquery;
1866 			return 0; /* and wait for them */
1867 		}
1868 	}
1869 	if(iq->depth == ie->max_dependency_depth) {
1870 		verbose(VERB_QUERY, "maxdepth and need more nameservers, fail");
1871 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1872 	}
1873 	if(iq->depth > 0 && iq->target_count &&
1874 		iq->target_count[1] > MAX_TARGET_COUNT) {
1875 		char s[LDNS_MAX_DOMAINLEN+1];
1876 		dname_str(qstate->qinfo.qname, s);
1877 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
1878 			"number of glue fetches %d", s, iq->target_count[1]);
1879 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1880 	}
1881 	/* mark cycle targets for parent-side lookups */
1882 	iter_mark_pside_cycle_targets(qstate, iq->dp);
1883 	/* see if we can issue queries to get nameserver addresses */
1884 	/* this lookup is not randomized, but sequential. */
1885 	for(ns = iq->dp->nslist; ns; ns = ns->next) {
1886 		/* if this nameserver is at a delegation point, but that
1887 		 * delegation point is a stub and we cannot go higher, skip*/
1888 		if( ((ie->supports_ipv6 && !ns->done_pside6) ||
1889 		    (ie->supports_ipv4 && !ns->done_pside4)) &&
1890 		    !can_have_last_resort(qstate->env, ns->name, ns->namelen,
1891 			iq->qchase.qclass, NULL)) {
1892 			log_nametypeclass(VERB_ALGO, "cannot pside lookup ns "
1893 				"because it is also a stub/forward,",
1894 				ns->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1895 			if(ie->supports_ipv6) ns->done_pside6 = 1;
1896 			if(ie->supports_ipv4) ns->done_pside4 = 1;
1897 			continue;
1898 		}
1899 		/* query for parent-side A and AAAA for nameservers */
1900 		if(ie->supports_ipv6 && !ns->done_pside6) {
1901 			/* Send the AAAA request. */
1902 			if(!generate_parentside_target_query(qstate, iq, id,
1903 				ns->name, ns->namelen,
1904 				LDNS_RR_TYPE_AAAA, iq->qchase.qclass))
1905 				return error_response(qstate, id,
1906 					LDNS_RCODE_SERVFAIL);
1907 			ns->done_pside6 = 1;
1908 			query_count++;
1909 		}
1910 		if(ie->supports_ipv4 && !ns->done_pside4) {
1911 			/* Send the A request. */
1912 			if(!generate_parentside_target_query(qstate, iq, id,
1913 				ns->name, ns->namelen,
1914 				LDNS_RR_TYPE_A, iq->qchase.qclass))
1915 				return error_response(qstate, id,
1916 					LDNS_RCODE_SERVFAIL);
1917 			ns->done_pside4 = 1;
1918 			query_count++;
1919 		}
1920 		if(query_count != 0) { /* suspend to await results */
1921 			verbose(VERB_ALGO, "try parent-side glue lookup");
1922 			iq->num_target_queries += query_count;
1923 			target_count_increase(iq, query_count);
1924 			qstate->ext_state[id] = module_wait_subquery;
1925 			return 0;
1926 		}
1927 	}
1928 
1929 	/* if this was a parent-side glue query itself, then store that
1930 	 * failure in cache. */
1931 	if(!qstate->no_cache_store && iq->query_for_pside_glue
1932 		&& !iq->pside_glue)
1933 			iter_store_parentside_neg(qstate->env, &qstate->qinfo,
1934 				iq->deleg_msg?iq->deleg_msg->rep:
1935 				(iq->response?iq->response->rep:NULL));
1936 
1937 	verbose(VERB_QUERY, "out of query targets -- returning SERVFAIL");
1938 	/* fail -- no more targets, no more hope of targets, no hope
1939 	 * of a response. */
1940 	return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1941 }
1942 
1943 /**
1944  * Try to find the NS record set that will resolve a qtype DS query. Due
1945  * to grandparent/grandchild reasons we did not get a proper lookup right
1946  * away.  We need to create type NS queries until we get the right parent
1947  * for this lookup.  We remove labels from the query to find the right point.
1948  * If we end up at the old dp name, then there is no solution.
1949  *
1950  * @param qstate: query state.
1951  * @param iq: iterator query state.
1952  * @param id: module id.
1953  * @return true if the event requires more immediate processing, false if
1954  *         not. This is generally only true when forwarding the request to
1955  *         the final state (i.e., on answer).
1956  */
1957 static int
1958 processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
1959 {
1960 	struct module_qstate* subq = NULL;
1961 	verbose(VERB_ALGO, "processDSNSFind");
1962 
1963 	if(!iq->dsns_point) {
1964 		/* initialize */
1965 		iq->dsns_point = iq->qchase.qname;
1966 		iq->dsns_point_len = iq->qchase.qname_len;
1967 	}
1968 	/* robustcheck for internal error: we are not underneath the dp */
1969 	if(!dname_subdomain_c(iq->dsns_point, iq->dp->name)) {
1970 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1971 	}
1972 
1973 	/* go up one (more) step, until we hit the dp, if so, end */
1974 	dname_remove_label(&iq->dsns_point, &iq->dsns_point_len);
1975 	if(query_dname_compare(iq->dsns_point, iq->dp->name) == 0) {
1976 		/* there was no inbetween nameserver, use the old delegation
1977 		 * point again.  And this time, because dsns_point is nonNULL
1978 		 * we are going to accept the (bad) result */
1979 		iq->state = QUERYTARGETS_STATE;
1980 		return 1;
1981 	}
1982 	iq->state = DSNS_FIND_STATE;
1983 
1984 	/* spawn NS lookup (validation not needed, this is for DS lookup) */
1985 	log_nametypeclass(VERB_ALGO, "fetch nameservers",
1986 		iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1987 	if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len,
1988 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1989 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
1990 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1991 	}
1992 
1993 	return 0;
1994 }
1995 
1996 /**
1997  * This is the request event state where the request will be sent to one of
1998  * its current query targets. This state also handles issuing target lookup
1999  * queries for missing target IP addresses. Queries typically iterate on
2000  * this state, both when they are just trying different targets for a given
2001  * delegation point, and when they change delegation points. This state
2002  * roughly corresponds to RFC 1034 algorithm steps 3 and 4.
2003  *
2004  * @param qstate: query state.
2005  * @param iq: iterator query state.
2006  * @param ie: iterator shared global environment.
2007  * @param id: module id.
2008  * @return true if the event requires more request processing immediately,
2009  *         false if not. This state only returns true when it is generating
2010  *         a SERVFAIL response because the query has hit a dead end.
2011  */
2012 static int
2013 processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
2014 	struct iter_env* ie, int id)
2015 {
2016 	int tf_policy;
2017 	struct delegpt_addr* target;
2018 	struct outbound_entry* outq;
2019 	int auth_fallback = 0;
2020 
2021 	/* NOTE: a request will encounter this state for each target it
2022 	 * needs to send a query to. That is, at least one per referral,
2023 	 * more if some targets timeout or return throwaway answers. */
2024 
2025 	log_query_info(VERB_QUERY, "processQueryTargets:", &qstate->qinfo);
2026 	verbose(VERB_ALGO, "processQueryTargets: targetqueries %d, "
2027 		"currentqueries %d sentcount %d", iq->num_target_queries,
2028 		iq->num_current_queries, iq->sent_count);
2029 
2030 	/* Make sure that we haven't run away */
2031 	/* FIXME: is this check even necessary? */
2032 	if(iq->referral_count > MAX_REFERRAL_COUNT) {
2033 		verbose(VERB_QUERY, "request has exceeded the maximum "
2034 			"number of referrrals with %d", iq->referral_count);
2035 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2036 	}
2037 	if(iq->sent_count > MAX_SENT_COUNT) {
2038 		verbose(VERB_QUERY, "request has exceeded the maximum "
2039 			"number of sends with %d", iq->sent_count);
2040 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2041 	}
2042 
2043 	/* Make sure we have a delegation point, otherwise priming failed
2044 	 * or another failure occurred */
2045 	if(!iq->dp) {
2046 		verbose(VERB_QUERY, "Failed to get a delegation, giving up");
2047 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2048 	}
2049 	if(!ie->supports_ipv6)
2050 		delegpt_no_ipv6(iq->dp);
2051 	if(!ie->supports_ipv4)
2052 		delegpt_no_ipv4(iq->dp);
2053 	delegpt_log(VERB_ALGO, iq->dp);
2054 
2055 	if(iq->num_current_queries>0) {
2056 		/* already busy answering a query, this restart is because
2057 		 * more delegpt addrs became available, wait for existing
2058 		 * query. */
2059 		verbose(VERB_ALGO, "woke up, but wait for outstanding query");
2060 		qstate->ext_state[id] = module_wait_reply;
2061 		return 0;
2062 	}
2063 
2064 	if(iq->minimisation_state == INIT_MINIMISE_STATE) {
2065 		/* (Re)set qinfo_out to (new) delegation point, except when
2066 		 * qinfo_out is already a subdomain of dp. This happens when
2067 		 * increasing by more than one label at once (QNAMEs with more
2068 		 * than MAX_MINIMISE_COUNT labels). */
2069 		if(!(iq->qinfo_out.qname_len
2070 			&& dname_subdomain_c(iq->qchase.qname,
2071 				iq->qinfo_out.qname)
2072 			&& dname_subdomain_c(iq->qinfo_out.qname,
2073 				iq->dp->name))) {
2074 			iq->qinfo_out.qname = iq->dp->name;
2075 			iq->qinfo_out.qname_len = iq->dp->namelen;
2076 			iq->qinfo_out.qtype = LDNS_RR_TYPE_A;
2077 			iq->qinfo_out.qclass = iq->qchase.qclass;
2078 			iq->qinfo_out.local_alias = NULL;
2079 			iq->minimise_count = 0;
2080 		}
2081 
2082 		iq->minimisation_state = MINIMISE_STATE;
2083 	}
2084 	if(iq->minimisation_state == MINIMISE_STATE) {
2085 		int qchaselabs = dname_count_labels(iq->qchase.qname);
2086 		int labdiff = qchaselabs -
2087 			dname_count_labels(iq->qinfo_out.qname);
2088 
2089 		iq->qinfo_out.qname = iq->qchase.qname;
2090 		iq->qinfo_out.qname_len = iq->qchase.qname_len;
2091 		iq->minimise_count++;
2092 		iq->minimise_timeout_count = 0;
2093 
2094 		iter_dec_attempts(iq->dp, 1);
2095 
2096 		/* Limit number of iterations for QNAMEs with more
2097 		 * than MAX_MINIMISE_COUNT labels. Send first MINIMISE_ONE_LAB
2098 		 * labels of QNAME always individually.
2099 		 */
2100 		if(qchaselabs > MAX_MINIMISE_COUNT && labdiff > 1 &&
2101 			iq->minimise_count > MINIMISE_ONE_LAB) {
2102 			if(iq->minimise_count < MAX_MINIMISE_COUNT) {
2103 				int multilabs = qchaselabs - 1 -
2104 					MINIMISE_ONE_LAB;
2105 				int extralabs = multilabs /
2106 					MINIMISE_MULTIPLE_LABS;
2107 
2108 				if (MAX_MINIMISE_COUNT - iq->minimise_count >=
2109 					multilabs % MINIMISE_MULTIPLE_LABS)
2110 					/* Default behaviour is to add 1 label
2111 					 * every iteration. Therefore, decrement
2112 					 * the extralabs by 1 */
2113 					extralabs--;
2114 				if (extralabs < labdiff)
2115 					labdiff -= extralabs;
2116 				else
2117 					labdiff = 1;
2118 			}
2119 			/* Last minimised iteration, send all labels with
2120 			 * QTYPE=NS */
2121 			else
2122 				labdiff = 1;
2123 		}
2124 
2125 		if(labdiff > 1) {
2126 			verbose(VERB_QUERY, "removing %d labels", labdiff-1);
2127 			dname_remove_labels(&iq->qinfo_out.qname,
2128 				&iq->qinfo_out.qname_len,
2129 				labdiff-1);
2130 		}
2131 		if(labdiff < 1 || (labdiff < 2
2132 			&& (iq->qchase.qtype == LDNS_RR_TYPE_DS
2133 			|| iq->qchase.qtype == LDNS_RR_TYPE_A)))
2134 			/* Stop minimising this query, resolve "as usual" */
2135 			iq->minimisation_state = DONOT_MINIMISE_STATE;
2136 		else if(!qstate->no_cache_lookup) {
2137 			struct dns_msg* msg = dns_cache_lookup(qstate->env,
2138 				iq->qinfo_out.qname, iq->qinfo_out.qname_len,
2139 				iq->qinfo_out.qtype, iq->qinfo_out.qclass,
2140 				qstate->query_flags, qstate->region,
2141 				qstate->env->scratch, 0);
2142 			if(msg && msg->rep->an_numrrsets == 0
2143 				&& FLAGS_GET_RCODE(msg->rep->flags) ==
2144 				LDNS_RCODE_NOERROR)
2145 				/* no need to send query if it is already
2146 				 * cached as NOERROR/NODATA */
2147 				return 1;
2148 		}
2149 	}
2150 	if(iq->minimisation_state == SKIP_MINIMISE_STATE) {
2151 		if(iq->minimise_timeout_count < MAX_MINIMISE_TIMEOUT_COUNT)
2152 			/* Do not increment qname, continue incrementing next
2153 			 * iteration */
2154 			iq->minimisation_state = MINIMISE_STATE;
2155 		else if(!qstate->env->cfg->qname_minimisation_strict)
2156 			/* Too many time-outs detected for this QNAME and QTYPE.
2157 			 * We give up, disable QNAME minimisation. */
2158 			iq->minimisation_state = DONOT_MINIMISE_STATE;
2159 	}
2160 	if(iq->minimisation_state == DONOT_MINIMISE_STATE)
2161 		iq->qinfo_out = iq->qchase;
2162 
2163 	/* now find an answer to this query */
2164 	/* see if authority zones have an answer */
2165 	/* now we know the dp, we can check the auth zone for locally hosted
2166 	 * contents */
2167 	if(!iq->auth_zone_avoid && qstate->blacklist) {
2168 		if(auth_zones_can_fallback(qstate->env->auth_zones,
2169 			iq->dp->name, iq->dp->namelen, iq->qinfo_out.qclass)) {
2170 			/* if cache is blacklisted and this zone allows us
2171 			 * to fallback to the internet, then do so, and
2172 			 * fetch results from the internet servers */
2173 			iq->auth_zone_avoid = 1;
2174 		}
2175 	}
2176 	if(iq->auth_zone_avoid) {
2177 		iq->auth_zone_avoid = 0;
2178 		auth_fallback = 1;
2179 	} else if(auth_zones_lookup(qstate->env->auth_zones, &iq->qinfo_out,
2180 		qstate->region, &iq->response, &auth_fallback, iq->dp->name,
2181 		iq->dp->namelen)) {
2182 		/* use this as a response to be processed by the iterator */
2183 		if(verbosity >= VERB_ALGO) {
2184 			log_dns_msg("msg from auth zone",
2185 				&iq->response->qinfo, iq->response->rep);
2186 		}
2187 		if((iq->chase_flags&BIT_RD) && !(iq->response->rep->flags&BIT_AA)) {
2188 			verbose(VERB_ALGO, "forwarder, ignoring referral from auth zone");
2189 		} else {
2190 			lock_rw_wrlock(&qstate->env->auth_zones->lock);
2191 			qstate->env->auth_zones->num_query_up++;
2192 			lock_rw_unlock(&qstate->env->auth_zones->lock);
2193 			iq->num_current_queries++;
2194 			iq->chase_to_rd = 0;
2195 			iq->dnssec_lame_query = 0;
2196 			iq->auth_zone_response = 1;
2197 			return next_state(iq, QUERY_RESP_STATE);
2198 		}
2199 	}
2200 	iq->auth_zone_response = 0;
2201 	if(auth_fallback == 0) {
2202 		/* like we got servfail from the auth zone lookup, and
2203 		 * no internet fallback */
2204 		verbose(VERB_ALGO, "auth zone lookup failed, no fallback,"
2205 			" servfail");
2206 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2207 	}
2208 	if(iq->dp && iq->dp->auth_dp) {
2209 		/* we wanted to fallback, but had no delegpt, only the
2210 		 * auth zone generated delegpt, create an actual one */
2211 		iq->auth_zone_avoid = 1;
2212 		return next_state(iq, INIT_REQUEST_STATE);
2213 	}
2214 	/* but mostly, fallback==1 (like, when no such auth zone exists)
2215 	 * and we continue with lookups */
2216 
2217 	tf_policy = 0;
2218 	/* < not <=, because although the array is large enough for <=, the
2219 	 * generated query will immediately be discarded due to depth and
2220 	 * that servfail is cached, which is not good as opportunism goes. */
2221 	if(iq->depth < ie->max_dependency_depth
2222 		&& iq->sent_count < TARGET_FETCH_STOP) {
2223 		tf_policy = ie->target_fetch_policy[iq->depth];
2224 	}
2225 
2226 	/* if in 0x20 fallback get as many targets as possible */
2227 	if(iq->caps_fallback) {
2228 		int extra = 0;
2229 		size_t naddr, nres, navail;
2230 		if(!query_for_targets(qstate, iq, ie, id, -1, &extra)) {
2231 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2232 		}
2233 		iq->num_target_queries += extra;
2234 		target_count_increase(iq, extra);
2235 		if(iq->num_target_queries > 0) {
2236 			/* wait to get all targets, we want to try em */
2237 			verbose(VERB_ALGO, "wait for all targets for fallback");
2238 			qstate->ext_state[id] = module_wait_reply;
2239 			return 0;
2240 		}
2241 		/* did we do enough fallback queries already? */
2242 		delegpt_count_addr(iq->dp, &naddr, &nres, &navail);
2243 		/* the current caps_server is the number of fallbacks sent.
2244 		 * the original query is one that matched too, so we have
2245 		 * caps_server+1 number of matching queries now */
2246 		if(iq->caps_server+1 >= naddr*3 ||
2247 			iq->caps_server*2+2 >= MAX_SENT_COUNT) {
2248 			/* *2 on sentcount check because ipv6 may fail */
2249 			/* we're done, process the response */
2250 			verbose(VERB_ALGO, "0x20 fallback had %d responses "
2251 				"match for %d wanted, done.",
2252 				(int)iq->caps_server+1, (int)naddr*3);
2253 			iq->response = iq->caps_response;
2254 			iq->caps_fallback = 0;
2255 			iter_dec_attempts(iq->dp, 3); /* space for fallback */
2256 			iq->num_current_queries++; /* RespState decrements it*/
2257 			iq->referral_count++; /* make sure we don't loop */
2258 			iq->sent_count = 0;
2259 			iq->state = QUERY_RESP_STATE;
2260 			return 1;
2261 		}
2262 		verbose(VERB_ALGO, "0x20 fallback number %d",
2263 			(int)iq->caps_server);
2264 
2265 	/* if there is a policy to fetch missing targets
2266 	 * opportunistically, do it. we rely on the fact that once a
2267 	 * query (or queries) for a missing name have been issued,
2268 	 * they will not show up again. */
2269 	} else if(tf_policy != 0) {
2270 		int extra = 0;
2271 		verbose(VERB_ALGO, "attempt to get extra %d targets",
2272 			tf_policy);
2273 		(void)query_for_targets(qstate, iq, ie, id, tf_policy, &extra);
2274 		/* errors ignored, these targets are not strictly necessary for
2275 		 * this result, we do not have to reply with SERVFAIL */
2276 		iq->num_target_queries += extra;
2277 		target_count_increase(iq, extra);
2278 	}
2279 
2280 	/* Add the current set of unused targets to our queue. */
2281 	delegpt_add_unused_targets(iq->dp);
2282 
2283 	/* Select the next usable target, filtering out unsuitable targets. */
2284 	target = iter_server_selection(ie, qstate->env, iq->dp,
2285 		iq->dp->name, iq->dp->namelen, iq->qchase.qtype,
2286 		&iq->dnssec_lame_query, &iq->chase_to_rd,
2287 		iq->num_target_queries, qstate->blacklist,
2288 		qstate->prefetch_leeway);
2289 
2290 	/* If no usable target was selected... */
2291 	if(!target) {
2292 		/* Here we distinguish between three states: generate a new
2293 		 * target query, just wait, or quit (with a SERVFAIL).
2294 		 * We have the following information: number of active
2295 		 * target queries, number of active current queries,
2296 		 * the presence of missing targets at this delegation
2297 		 * point, and the given query target policy. */
2298 
2299 		/* Check for the wait condition. If this is true, then
2300 		 * an action must be taken. */
2301 		if(iq->num_target_queries==0 && iq->num_current_queries==0) {
2302 			/* If there is nothing to wait for, then we need
2303 			 * to distinguish between generating (a) new target
2304 			 * query, or failing. */
2305 			if(delegpt_count_missing_targets(iq->dp) > 0) {
2306 				int qs = 0;
2307 				verbose(VERB_ALGO, "querying for next "
2308 					"missing target");
2309 				if(!query_for_targets(qstate, iq, ie, id,
2310 					1, &qs)) {
2311 					return error_response(qstate, id,
2312 						LDNS_RCODE_SERVFAIL);
2313 				}
2314 				if(qs == 0 &&
2315 				   delegpt_count_missing_targets(iq->dp) == 0){
2316 					/* it looked like there were missing
2317 					 * targets, but they did not turn up.
2318 					 * Try the bad choices again (if any),
2319 					 * when we get back here missing==0,
2320 					 * so this is not a loop. */
2321 					return 1;
2322 				}
2323 				iq->num_target_queries += qs;
2324 				target_count_increase(iq, qs);
2325 			}
2326 			/* Since a target query might have been made, we
2327 			 * need to check again. */
2328 			if(iq->num_target_queries == 0) {
2329 				/* if in capsforid fallback, instead of last
2330 				 * resort, we agree with the current reply
2331 				 * we have (if any) (our count of addrs bad)*/
2332 				if(iq->caps_fallback && iq->caps_reply) {
2333 					/* we're done, process the response */
2334 					verbose(VERB_ALGO, "0x20 fallback had %d responses, "
2335 						"but no more servers except "
2336 						"last resort, done.",
2337 						(int)iq->caps_server+1);
2338 					iq->response = iq->caps_response;
2339 					iq->caps_fallback = 0;
2340 					iter_dec_attempts(iq->dp, 3); /* space for fallback */
2341 					iq->num_current_queries++; /* RespState decrements it*/
2342 					iq->referral_count++; /* make sure we don't loop */
2343 					iq->sent_count = 0;
2344 					iq->state = QUERY_RESP_STATE;
2345 					return 1;
2346 				}
2347 				return processLastResort(qstate, iq, ie, id);
2348 			}
2349 		}
2350 
2351 		/* otherwise, we have no current targets, so submerge
2352 		 * until one of the target or direct queries return. */
2353 		if(iq->num_target_queries>0 && iq->num_current_queries>0) {
2354 			verbose(VERB_ALGO, "no current targets -- waiting "
2355 				"for %d targets to resolve or %d outstanding"
2356 				" queries to respond", iq->num_target_queries,
2357 				iq->num_current_queries);
2358 			qstate->ext_state[id] = module_wait_reply;
2359 		} else if(iq->num_target_queries>0) {
2360 			verbose(VERB_ALGO, "no current targets -- waiting "
2361 				"for %d targets to resolve.",
2362 				iq->num_target_queries);
2363 			qstate->ext_state[id] = module_wait_subquery;
2364 		} else {
2365 			verbose(VERB_ALGO, "no current targets -- waiting "
2366 				"for %d outstanding queries to respond.",
2367 				iq->num_current_queries);
2368 			qstate->ext_state[id] = module_wait_reply;
2369 		}
2370 		return 0;
2371 	}
2372 
2373 	/* if not forwarding, check ratelimits per delegationpoint name */
2374 	if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
2375 		if(!infra_ratelimit_inc(qstate->env->infra_cache, iq->dp->name,
2376 			iq->dp->namelen, *qstate->env->now)) {
2377 			lock_basic_lock(&ie->queries_ratelimit_lock);
2378 			ie->num_queries_ratelimited++;
2379 			lock_basic_unlock(&ie->queries_ratelimit_lock);
2380 			verbose(VERB_ALGO, "query exceeded ratelimits");
2381 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2382 		}
2383 	}
2384 
2385 	/* We have a valid target. */
2386 	if(verbosity >= VERB_QUERY) {
2387 		log_query_info(VERB_QUERY, "sending query:", &iq->qinfo_out);
2388 		log_name_addr(VERB_QUERY, "sending to target:", iq->dp->name,
2389 			&target->addr, target->addrlen);
2390 		verbose(VERB_ALGO, "dnssec status: %s%s",
2391 			iq->dnssec_expected?"expected": "not expected",
2392 			iq->dnssec_lame_query?" but lame_query anyway": "");
2393 	}
2394 	fptr_ok(fptr_whitelist_modenv_send_query(qstate->env->send_query));
2395 	outq = (*qstate->env->send_query)(&iq->qinfo_out,
2396 		iq->chase_flags | (iq->chase_to_rd?BIT_RD:0),
2397 		/* unset CD if to forwarder(RD set) and not dnssec retry
2398 		 * (blacklist nonempty) and no trust-anchors are configured
2399 		 * above the qname or on the first attempt when dnssec is on */
2400 		EDNS_DO| ((iq->chase_to_rd||(iq->chase_flags&BIT_RD)!=0)&&
2401 		!qstate->blacklist&&(!iter_qname_indicates_dnssec(qstate->env,
2402 		&iq->qinfo_out)||target->attempts==1)?0:BIT_CD),
2403 		iq->dnssec_expected, iq->caps_fallback || is_caps_whitelisted(
2404 		ie, iq), &target->addr, target->addrlen,
2405 		iq->dp->name, iq->dp->namelen,
2406 		(iq->dp->ssl_upstream || qstate->env->cfg->ssl_upstream),
2407 		target->tls_auth_name, qstate);
2408 	if(!outq) {
2409 		log_addr(VERB_DETAIL, "error sending query to auth server",
2410 			&target->addr, target->addrlen);
2411 		if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok)
2412 		    infra_ratelimit_dec(qstate->env->infra_cache, iq->dp->name,
2413 			iq->dp->namelen, *qstate->env->now);
2414 		if(qstate->env->cfg->qname_minimisation)
2415 			iq->minimisation_state = SKIP_MINIMISE_STATE;
2416 		return next_state(iq, QUERYTARGETS_STATE);
2417 	}
2418 	outbound_list_insert(&iq->outlist, outq);
2419 	iq->num_current_queries++;
2420 	iq->sent_count++;
2421 	qstate->ext_state[id] = module_wait_reply;
2422 
2423 	return 0;
2424 }
2425 
2426 /** find NS rrset in given list */
2427 static struct ub_packed_rrset_key*
2428 find_NS(struct reply_info* rep, size_t from, size_t to)
2429 {
2430 	size_t i;
2431 	for(i=from; i<to; i++) {
2432 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
2433 			return rep->rrsets[i];
2434 	}
2435 	return NULL;
2436 }
2437 
2438 
2439 /**
2440  * Process the query response. All queries end up at this state first. This
2441  * process generally consists of analyzing the response and routing the
2442  * event to the next state (either bouncing it back to a request state, or
2443  * terminating the processing for this event).
2444  *
2445  * @param qstate: query state.
2446  * @param iq: iterator query state.
2447  * @param id: module id.
2448  * @return true if the event requires more immediate processing, false if
2449  *         not. This is generally only true when forwarding the request to
2450  *         the final state (i.e., on answer).
2451  */
2452 static int
2453 processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
2454 	int id)
2455 {
2456 	int dnsseclame = 0;
2457 	enum response_type type;
2458 	iq->num_current_queries--;
2459 
2460 	if(!inplace_cb_query_response_call(qstate->env, qstate, iq->response))
2461 		log_err("unable to call query_response callback");
2462 
2463 	if(iq->response == NULL) {
2464 		/* Don't increment qname when QNAME minimisation is enabled */
2465 		if(qstate->env->cfg->qname_minimisation) {
2466 			iq->minimise_timeout_count++;
2467 			iq->minimisation_state = SKIP_MINIMISE_STATE;
2468 		}
2469 		iq->chase_to_rd = 0;
2470 		iq->dnssec_lame_query = 0;
2471 		verbose(VERB_ALGO, "query response was timeout");
2472 		return next_state(iq, QUERYTARGETS_STATE);
2473 	}
2474 	type = response_type_from_server(
2475 		(int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2476 		iq->response, &iq->qinfo_out, iq->dp);
2477 	iq->chase_to_rd = 0;
2478 	if(type == RESPONSE_TYPE_REFERRAL && (iq->chase_flags&BIT_RD) &&
2479 		!iq->auth_zone_response) {
2480 		/* When forwarding (RD bit is set), we handle referrals
2481 		 * differently. No queries should be sent elsewhere */
2482 		type = RESPONSE_TYPE_ANSWER;
2483 	}
2484 	if(!qstate->env->cfg->disable_dnssec_lame_check && iq->dnssec_expected
2485                 && !iq->dnssec_lame_query &&
2486 		!(iq->chase_flags&BIT_RD)
2487 		&& iq->sent_count < DNSSEC_LAME_DETECT_COUNT
2488 		&& type != RESPONSE_TYPE_LAME
2489 		&& type != RESPONSE_TYPE_REC_LAME
2490 		&& type != RESPONSE_TYPE_THROWAWAY
2491 		&& type != RESPONSE_TYPE_UNTYPED) {
2492 		/* a possible answer, see if it is missing DNSSEC */
2493 		/* but not when forwarding, so we dont mark fwder lame */
2494 		if(!iter_msg_has_dnssec(iq->response)) {
2495 			/* Mark this address as dnsseclame in this dp,
2496 			 * because that will make serverselection disprefer
2497 			 * it, but also, once it is the only final option,
2498 			 * use dnssec-lame-bypass if it needs to query there.*/
2499 			if(qstate->reply) {
2500 				struct delegpt_addr* a = delegpt_find_addr(
2501 					iq->dp, &qstate->reply->addr,
2502 					qstate->reply->addrlen);
2503 				if(a) a->dnsseclame = 1;
2504 			}
2505 			/* test the answer is from the zone we expected,
2506 		 	 * otherwise, (due to parent,child on same server), we
2507 		 	 * might mark the server,zone lame inappropriately */
2508 			if(!iter_msg_from_zone(iq->response, iq->dp, type,
2509 				iq->qchase.qclass))
2510 				qstate->reply = NULL;
2511 			type = RESPONSE_TYPE_LAME;
2512 			dnsseclame = 1;
2513 		}
2514 	} else iq->dnssec_lame_query = 0;
2515 	/* see if referral brings us close to the target */
2516 	if(type == RESPONSE_TYPE_REFERRAL) {
2517 		struct ub_packed_rrset_key* ns = find_NS(
2518 			iq->response->rep, iq->response->rep->an_numrrsets,
2519 			iq->response->rep->an_numrrsets
2520 			+ iq->response->rep->ns_numrrsets);
2521 		if(!ns) ns = find_NS(iq->response->rep, 0,
2522 				iq->response->rep->an_numrrsets);
2523 		if(!ns || !dname_strict_subdomain_c(ns->rk.dname, iq->dp->name)
2524 			|| !dname_subdomain_c(iq->qchase.qname, ns->rk.dname)){
2525 			verbose(VERB_ALGO, "bad referral, throwaway");
2526 			type = RESPONSE_TYPE_THROWAWAY;
2527 		} else
2528 			iter_scrub_ds(iq->response, ns, iq->dp->name);
2529 	} else iter_scrub_ds(iq->response, NULL, NULL);
2530 	if(type == RESPONSE_TYPE_THROWAWAY &&
2531 		FLAGS_GET_RCODE(iq->response->rep->flags) == LDNS_RCODE_YXDOMAIN) {
2532 		/* YXDOMAIN is a permanent error, no need to retry */
2533 		type = RESPONSE_TYPE_ANSWER;
2534 	}
2535 	if(type == RESPONSE_TYPE_CNAME && iq->response->rep->an_numrrsets >= 1
2536 		&& ntohs(iq->response->rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_DNAME) {
2537 		uint8_t* sname = NULL;
2538 		size_t snamelen = 0;
2539 		get_cname_target(iq->response->rep->rrsets[0], &sname,
2540 			&snamelen);
2541 		if(snamelen && dname_subdomain_c(sname, iq->response->rep->rrsets[0]->rk.dname)) {
2542 			/* DNAME to a subdomain loop; do not recurse */
2543 			type = RESPONSE_TYPE_ANSWER;
2544 		}
2545 	}
2546 
2547 	/* handle each of the type cases */
2548 	if(type == RESPONSE_TYPE_ANSWER) {
2549 		/* ANSWER type responses terminate the query algorithm,
2550 		 * so they sent on their */
2551 		if(verbosity >= VERB_DETAIL) {
2552 			verbose(VERB_DETAIL, "query response was %s",
2553 				FLAGS_GET_RCODE(iq->response->rep->flags)
2554 				==LDNS_RCODE_NXDOMAIN?"NXDOMAIN ANSWER":
2555 				(iq->response->rep->an_numrrsets?"ANSWER":
2556 				"nodata ANSWER"));
2557 		}
2558 		/* if qtype is DS, check we have the right level of answer,
2559 		 * like grandchild answer but we need the middle, reject it */
2560 		if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2561 			&& !(iq->chase_flags&BIT_RD)
2562 			&& iter_ds_toolow(iq->response, iq->dp)
2563 			&& iter_dp_cangodown(&iq->qchase, iq->dp)) {
2564 			/* close down outstanding requests to be discarded */
2565 			outbound_list_clear(&iq->outlist);
2566 			iq->num_current_queries = 0;
2567 			fptr_ok(fptr_whitelist_modenv_detach_subs(
2568 				qstate->env->detach_subs));
2569 			(*qstate->env->detach_subs)(qstate);
2570 			iq->num_target_queries = 0;
2571 			return processDSNSFind(qstate, iq, id);
2572 		}
2573 		if(!qstate->no_cache_store)
2574 			iter_dns_store(qstate->env, &iq->response->qinfo,
2575 				iq->response->rep, 0, qstate->prefetch_leeway,
2576 				iq->dp&&iq->dp->has_parent_side_NS,
2577 				qstate->region, qstate->query_flags);
2578 		/* close down outstanding requests to be discarded */
2579 		outbound_list_clear(&iq->outlist);
2580 		iq->num_current_queries = 0;
2581 		fptr_ok(fptr_whitelist_modenv_detach_subs(
2582 			qstate->env->detach_subs));
2583 		(*qstate->env->detach_subs)(qstate);
2584 		iq->num_target_queries = 0;
2585 		if(qstate->reply)
2586 			sock_list_insert(&qstate->reply_origin,
2587 				&qstate->reply->addr, qstate->reply->addrlen,
2588 				qstate->region);
2589 		if(iq->minimisation_state != DONOT_MINIMISE_STATE) {
2590 			if(FLAGS_GET_RCODE(iq->response->rep->flags) !=
2591 				LDNS_RCODE_NOERROR) {
2592 				if(qstate->env->cfg->qname_minimisation_strict)
2593 					return final_state(iq);
2594 				/* Best effort qname-minimisation.
2595 				 * Stop minimising and send full query when
2596 				 * RCODE is not NOERROR. */
2597 				iq->minimisation_state = DONOT_MINIMISE_STATE;
2598 			}
2599 			if(FLAGS_GET_RCODE(iq->response->rep->flags) ==
2600 				LDNS_RCODE_NXDOMAIN) {
2601 				/* Stop resolving when NXDOMAIN is DNSSEC
2602 				 * signed. Based on assumption that nameservers
2603 				 * serving signed zones do not return NXDOMAIN
2604 				 * for empty-non-terminals. */
2605 				if(iq->dnssec_expected)
2606 					return final_state(iq);
2607 				/* Make subrequest to validate intermediate
2608 				 * NXDOMAIN if harden-below-nxdomain is
2609 				 * enabled. */
2610 				if(qstate->env->cfg->harden_below_nxdomain) {
2611 					struct module_qstate* subq = NULL;
2612 					log_query_info(VERB_QUERY,
2613 						"schedule NXDOMAIN validation:",
2614 						&iq->response->qinfo);
2615 					if(!generate_sub_request(
2616 						iq->response->qinfo.qname,
2617 						iq->response->qinfo.qname_len,
2618 						iq->response->qinfo.qtype,
2619 						iq->response->qinfo.qclass,
2620 						qstate, id, iq,
2621 						INIT_REQUEST_STATE,
2622 						FINISHED_STATE, &subq, 1))
2623 						verbose(VERB_ALGO,
2624 						"could not validate NXDOMAIN "
2625 						"response");
2626 				}
2627 			}
2628 			return next_state(iq, QUERYTARGETS_STATE);
2629 		}
2630 		return final_state(iq);
2631 	} else if(type == RESPONSE_TYPE_REFERRAL) {
2632 		/* REFERRAL type responses get a reset of the
2633 		 * delegation point, and back to the QUERYTARGETS_STATE. */
2634 		verbose(VERB_DETAIL, "query response was REFERRAL");
2635 
2636 		if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
2637 			/* we have a referral, no ratelimit, we can send
2638 			 * our queries to the given name */
2639 			infra_ratelimit_dec(qstate->env->infra_cache,
2640 				iq->dp->name, iq->dp->namelen,
2641 				*qstate->env->now);
2642 		}
2643 
2644 		/* if hardened, only store referral if we asked for it */
2645 		if(!qstate->no_cache_store &&
2646 		(!qstate->env->cfg->harden_referral_path ||
2647 		    (  qstate->qinfo.qtype == LDNS_RR_TYPE_NS
2648 			&& (qstate->query_flags&BIT_RD)
2649 			&& !(qstate->query_flags&BIT_CD)
2650 			   /* we know that all other NS rrsets are scrubbed
2651 			    * away, thus on referral only one is left.
2652 			    * see if that equals the query name... */
2653 			&& ( /* auth section, but sometimes in answer section*/
2654 			  reply_find_rrset_section_ns(iq->response->rep,
2655 				iq->qchase.qname, iq->qchase.qname_len,
2656 				LDNS_RR_TYPE_NS, iq->qchase.qclass)
2657 			  || reply_find_rrset_section_an(iq->response->rep,
2658 				iq->qchase.qname, iq->qchase.qname_len,
2659 				LDNS_RR_TYPE_NS, iq->qchase.qclass)
2660 			  )
2661 		    ))) {
2662 			/* Store the referral under the current query */
2663 			/* no prefetch-leeway, since its not the answer */
2664 			iter_dns_store(qstate->env, &iq->response->qinfo,
2665 				iq->response->rep, 1, 0, 0, NULL, 0);
2666 			if(iq->store_parent_NS)
2667 				iter_store_parentside_NS(qstate->env,
2668 					iq->response->rep);
2669 			if(qstate->env->neg_cache)
2670 				val_neg_addreferral(qstate->env->neg_cache,
2671 					iq->response->rep, iq->dp->name);
2672 		}
2673 		/* store parent-side-in-zone-glue, if directly queried for */
2674 		if(!qstate->no_cache_store && iq->query_for_pside_glue
2675 			&& !iq->pside_glue) {
2676 				iq->pside_glue = reply_find_rrset(iq->response->rep,
2677 					iq->qchase.qname, iq->qchase.qname_len,
2678 					iq->qchase.qtype, iq->qchase.qclass);
2679 				if(iq->pside_glue) {
2680 					log_rrset_key(VERB_ALGO, "found parent-side "
2681 						"glue", iq->pside_glue);
2682 					iter_store_parentside_rrset(qstate->env,
2683 						iq->pside_glue);
2684 				}
2685 		}
2686 
2687 		/* Reset the event state, setting the current delegation
2688 		 * point to the referral. */
2689 		iq->deleg_msg = iq->response;
2690 		iq->dp = delegpt_from_message(iq->response, qstate->region);
2691 		if (qstate->env->cfg->qname_minimisation)
2692 			iq->minimisation_state = INIT_MINIMISE_STATE;
2693 		if(!iq->dp)
2694 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2695 		if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
2696 			qstate->region, iq->dp))
2697 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2698 		if(iq->store_parent_NS && query_dname_compare(iq->dp->name,
2699 			iq->store_parent_NS->name) == 0)
2700 			iter_merge_retry_counts(iq->dp, iq->store_parent_NS);
2701 		delegpt_log(VERB_ALGO, iq->dp);
2702 		/* Count this as a referral. */
2703 		iq->referral_count++;
2704 		iq->sent_count = 0;
2705 		/* see if the next dp is a trust anchor, or a DS was sent
2706 		 * along, indicating dnssec is expected for next zone */
2707 		iq->dnssec_expected = iter_indicates_dnssec(qstate->env,
2708 			iq->dp, iq->response, iq->qchase.qclass);
2709 		/* if dnssec, validating then also fetch the key for the DS */
2710 		if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
2711 			!(qstate->query_flags&BIT_CD))
2712 			generate_dnskey_prefetch(qstate, iq, id);
2713 
2714 		/* spawn off NS and addr to auth servers for the NS we just
2715 		 * got in the referral. This gets authoritative answer
2716 		 * (answer section trust level) rrset.
2717 		 * right after, we detach the subs, answer goes to cache. */
2718 		if(qstate->env->cfg->harden_referral_path)
2719 			generate_ns_check(qstate, iq, id);
2720 
2721 		/* stop current outstanding queries.
2722 		 * FIXME: should the outstanding queries be waited for and
2723 		 * handled? Say by a subquery that inherits the outbound_entry.
2724 		 */
2725 		outbound_list_clear(&iq->outlist);
2726 		iq->num_current_queries = 0;
2727 		fptr_ok(fptr_whitelist_modenv_detach_subs(
2728 			qstate->env->detach_subs));
2729 		(*qstate->env->detach_subs)(qstate);
2730 		iq->num_target_queries = 0;
2731 		verbose(VERB_ALGO, "cleared outbound list for next round");
2732 		return next_state(iq, QUERYTARGETS_STATE);
2733 	} else if(type == RESPONSE_TYPE_CNAME) {
2734 		uint8_t* sname = NULL;
2735 		size_t snamelen = 0;
2736 		/* CNAME type responses get a query restart (i.e., get a
2737 		 * reset of the query state and go back to INIT_REQUEST_STATE).
2738 		 */
2739 		verbose(VERB_DETAIL, "query response was CNAME");
2740 		if(verbosity >= VERB_ALGO)
2741 			log_dns_msg("cname msg", &iq->response->qinfo,
2742 				iq->response->rep);
2743 		/* if qtype is DS, check we have the right level of answer,
2744 		 * like grandchild answer but we need the middle, reject it */
2745 		if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2746 			&& !(iq->chase_flags&BIT_RD)
2747 			&& iter_ds_toolow(iq->response, iq->dp)
2748 			&& iter_dp_cangodown(&iq->qchase, iq->dp)) {
2749 			outbound_list_clear(&iq->outlist);
2750 			iq->num_current_queries = 0;
2751 			fptr_ok(fptr_whitelist_modenv_detach_subs(
2752 				qstate->env->detach_subs));
2753 			(*qstate->env->detach_subs)(qstate);
2754 			iq->num_target_queries = 0;
2755 			return processDSNSFind(qstate, iq, id);
2756 		}
2757 		/* Process the CNAME response. */
2758 		if(!handle_cname_response(qstate, iq, iq->response,
2759 			&sname, &snamelen))
2760 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2761 		/* cache the CNAME response under the current query */
2762 		/* NOTE : set referral=1, so that rrsets get stored but not
2763 		 * the partial query answer (CNAME only). */
2764 		/* prefetchleeway applied because this updates answer parts */
2765 		if(!qstate->no_cache_store)
2766 			iter_dns_store(qstate->env, &iq->response->qinfo,
2767 				iq->response->rep, 1, qstate->prefetch_leeway,
2768 				iq->dp&&iq->dp->has_parent_side_NS, NULL,
2769 				qstate->query_flags);
2770 		/* set the current request's qname to the new value. */
2771 		iq->qchase.qname = sname;
2772 		iq->qchase.qname_len = snamelen;
2773 		/* Clear the query state, since this is a query restart. */
2774 		iq->deleg_msg = NULL;
2775 		iq->dp = NULL;
2776 		iq->dsns_point = NULL;
2777 		iq->auth_zone_response = 0;
2778 		iq->sent_count = 0;
2779 		if(iq->minimisation_state != MINIMISE_STATE)
2780 			/* Only count as query restart when it is not an extra
2781 			 * query as result of qname minimisation. */
2782 			iq->query_restart_count++;
2783 		if(qstate->env->cfg->qname_minimisation)
2784 			iq->minimisation_state = INIT_MINIMISE_STATE;
2785 
2786 		/* stop current outstanding queries.
2787 		 * FIXME: should the outstanding queries be waited for and
2788 		 * handled? Say by a subquery that inherits the outbound_entry.
2789 		 */
2790 		outbound_list_clear(&iq->outlist);
2791 		iq->num_current_queries = 0;
2792 		fptr_ok(fptr_whitelist_modenv_detach_subs(
2793 			qstate->env->detach_subs));
2794 		(*qstate->env->detach_subs)(qstate);
2795 		iq->num_target_queries = 0;
2796 		if(qstate->reply)
2797 			sock_list_insert(&qstate->reply_origin,
2798 				&qstate->reply->addr, qstate->reply->addrlen,
2799 				qstate->region);
2800 		verbose(VERB_ALGO, "cleared outbound list for query restart");
2801 		/* go to INIT_REQUEST_STATE for new qname. */
2802 		return next_state(iq, INIT_REQUEST_STATE);
2803 	} else if(type == RESPONSE_TYPE_LAME) {
2804 		/* Cache the LAMEness. */
2805 		verbose(VERB_DETAIL, "query response was %sLAME",
2806 			dnsseclame?"DNSSEC ":"");
2807 		if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2808 			log_err("mark lame: mismatch in qname and dpname");
2809 			/* throwaway this reply below */
2810 		} else if(qstate->reply) {
2811 			/* need addr for lameness cache, but we may have
2812 			 * gotten this from cache, so test to be sure */
2813 			if(!infra_set_lame(qstate->env->infra_cache,
2814 				&qstate->reply->addr, qstate->reply->addrlen,
2815 				iq->dp->name, iq->dp->namelen,
2816 				*qstate->env->now, dnsseclame, 0,
2817 				iq->qchase.qtype))
2818 				log_err("mark host lame: out of memory");
2819 		}
2820 	} else if(type == RESPONSE_TYPE_REC_LAME) {
2821 		/* Cache the LAMEness. */
2822 		verbose(VERB_DETAIL, "query response REC_LAME: "
2823 			"recursive but not authoritative server");
2824 		if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2825 			log_err("mark rec_lame: mismatch in qname and dpname");
2826 			/* throwaway this reply below */
2827 		} else if(qstate->reply) {
2828 			/* need addr for lameness cache, but we may have
2829 			 * gotten this from cache, so test to be sure */
2830 			verbose(VERB_DETAIL, "mark as REC_LAME");
2831 			if(!infra_set_lame(qstate->env->infra_cache,
2832 				&qstate->reply->addr, qstate->reply->addrlen,
2833 				iq->dp->name, iq->dp->namelen,
2834 				*qstate->env->now, 0, 1, iq->qchase.qtype))
2835 				log_err("mark host lame: out of memory");
2836 		}
2837 	} else if(type == RESPONSE_TYPE_THROWAWAY) {
2838 		/* LAME and THROWAWAY responses are handled the same way.
2839 		 * In this case, the event is just sent directly back to
2840 		 * the QUERYTARGETS_STATE without resetting anything,
2841 		 * because, clearly, the next target must be tried. */
2842 		verbose(VERB_DETAIL, "query response was THROWAWAY");
2843 	} else {
2844 		log_warn("A query response came back with an unknown type: %d",
2845 			(int)type);
2846 	}
2847 
2848 	/* LAME, THROWAWAY and "unknown" all end up here.
2849 	 * Recycle to the QUERYTARGETS state to hopefully try a
2850 	 * different target. */
2851 	if (qstate->env->cfg->qname_minimisation &&
2852 		!qstate->env->cfg->qname_minimisation_strict)
2853 		iq->minimisation_state = DONOT_MINIMISE_STATE;
2854 	if(iq->auth_zone_response) {
2855 		/* can we fallback? */
2856 		iq->auth_zone_response = 0;
2857 		if(!auth_zones_can_fallback(qstate->env->auth_zones,
2858 			iq->dp->name, iq->dp->namelen, qstate->qinfo.qclass)) {
2859 			verbose(VERB_ALGO, "auth zone response bad, and no"
2860 				" fallback possible, servfail");
2861 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2862 		}
2863 		verbose(VERB_ALGO, "auth zone response was bad, "
2864 			"fallback enabled");
2865 		iq->auth_zone_avoid = 1;
2866 		if(iq->dp->auth_dp) {
2867 			/* we are using a dp for the auth zone, with no
2868 			 * nameservers, get one first */
2869 			iq->dp = NULL;
2870 			return next_state(iq, INIT_REQUEST_STATE);
2871 		}
2872 	}
2873 	return next_state(iq, QUERYTARGETS_STATE);
2874 }
2875 
2876 /**
2877  * Return priming query results to interested super querystates.
2878  *
2879  * Sets the delegation point and delegation message (not nonRD queries).
2880  * This is a callback from walk_supers.
2881  *
2882  * @param qstate: priming query state that finished.
2883  * @param id: module id.
2884  * @param forq: the qstate for which priming has been done.
2885  */
2886 static void
2887 prime_supers(struct module_qstate* qstate, int id, struct module_qstate* forq)
2888 {
2889 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2890 	struct delegpt* dp = NULL;
2891 
2892 	log_assert(qstate->is_priming || foriq->wait_priming_stub);
2893 	log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2894 	/* Convert our response to a delegation point */
2895 	dp = delegpt_from_message(qstate->return_msg, forq->region);
2896 	if(!dp) {
2897 		/* if there is no convertable delegation point, then
2898 		 * the ANSWER type was (presumably) a negative answer. */
2899 		verbose(VERB_ALGO, "prime response was not a positive "
2900 			"ANSWER; failing");
2901 		foriq->dp = NULL;
2902 		foriq->state = QUERYTARGETS_STATE;
2903 		return;
2904 	}
2905 
2906 	log_query_info(VERB_DETAIL, "priming successful for", &qstate->qinfo);
2907 	delegpt_log(VERB_ALGO, dp);
2908 	foriq->dp = dp;
2909 	foriq->deleg_msg = dns_copy_msg(qstate->return_msg, forq->region);
2910 	if(!foriq->deleg_msg) {
2911 		log_err("copy prime response: out of memory");
2912 		foriq->dp = NULL;
2913 		foriq->state = QUERYTARGETS_STATE;
2914 		return;
2915 	}
2916 
2917 	/* root priming responses go to init stage 2, priming stub
2918 	 * responses to to stage 3. */
2919 	if(foriq->wait_priming_stub) {
2920 		foriq->state = INIT_REQUEST_3_STATE;
2921 		foriq->wait_priming_stub = 0;
2922 	} else	foriq->state = INIT_REQUEST_2_STATE;
2923 	/* because we are finished, the parent will be reactivated */
2924 }
2925 
2926 /**
2927  * This handles the response to a priming query. This is used to handle both
2928  * root and stub priming responses. This is basically the equivalent of the
2929  * QUERY_RESP_STATE, but will not handle CNAME responses and will treat
2930  * REFERRALs as ANSWERS. It will also update and reactivate the originating
2931  * event.
2932  *
2933  * @param qstate: query state.
2934  * @param id: module id.
2935  * @return true if the event needs more immediate processing, false if not.
2936  *         This state always returns false.
2937  */
2938 static int
2939 processPrimeResponse(struct module_qstate* qstate, int id)
2940 {
2941 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2942 	enum response_type type;
2943 	iq->response->rep->flags &= ~(BIT_RD|BIT_RA); /* ignore rec-lame */
2944 	type = response_type_from_server(
2945 		(int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2946 		iq->response, &iq->qchase, iq->dp);
2947 	if(type == RESPONSE_TYPE_ANSWER) {
2948 		qstate->return_rcode = LDNS_RCODE_NOERROR;
2949 		qstate->return_msg = iq->response;
2950 	} else {
2951 		qstate->return_rcode = LDNS_RCODE_SERVFAIL;
2952 		qstate->return_msg = NULL;
2953 	}
2954 
2955 	/* validate the root or stub after priming (if enabled).
2956 	 * This is the same query as the prime query, but with validation.
2957 	 * Now that we are primed, the additional queries that validation
2958 	 * may need can be resolved, such as DLV. */
2959 	if(qstate->env->cfg->harden_referral_path) {
2960 		struct module_qstate* subq = NULL;
2961 		log_nametypeclass(VERB_ALGO, "schedule prime validation",
2962 			qstate->qinfo.qname, qstate->qinfo.qtype,
2963 			qstate->qinfo.qclass);
2964 		if(!generate_sub_request(qstate->qinfo.qname,
2965 			qstate->qinfo.qname_len, qstate->qinfo.qtype,
2966 			qstate->qinfo.qclass, qstate, id, iq,
2967 			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
2968 			verbose(VERB_ALGO, "could not generate prime check");
2969 		}
2970 		generate_a_aaaa_check(qstate, iq, id);
2971 	}
2972 
2973 	/* This event is finished. */
2974 	qstate->ext_state[id] = module_finished;
2975 	return 0;
2976 }
2977 
2978 /**
2979  * Do final processing on responses to target queries. Events reach this
2980  * state after the iterative resolution algorithm terminates. This state is
2981  * responsible for reactivating the original event, and housekeeping related
2982  * to received target responses (caching, updating the current delegation
2983  * point, etc).
2984  * Callback from walk_supers for every super state that is interested in
2985  * the results from this query.
2986  *
2987  * @param qstate: query state.
2988  * @param id: module id.
2989  * @param forq: super query state.
2990  */
2991 static void
2992 processTargetResponse(struct module_qstate* qstate, int id,
2993 	struct module_qstate* forq)
2994 {
2995 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2996 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2997 	struct ub_packed_rrset_key* rrset;
2998 	struct delegpt_ns* dpns;
2999 	log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
3000 
3001 	foriq->state = QUERYTARGETS_STATE;
3002 	log_query_info(VERB_ALGO, "processTargetResponse", &qstate->qinfo);
3003 	log_query_info(VERB_ALGO, "processTargetResponse super", &forq->qinfo);
3004 
3005 	/* Tell the originating event that this target query has finished
3006 	 * (regardless if it succeeded or not). */
3007 	foriq->num_target_queries--;
3008 
3009 	/* check to see if parent event is still interested (in orig name).  */
3010 	if(!foriq->dp) {
3011 		verbose(VERB_ALGO, "subq: parent not interested, was reset");
3012 		return; /* not interested anymore */
3013 	}
3014 	dpns = delegpt_find_ns(foriq->dp, qstate->qinfo.qname,
3015 			qstate->qinfo.qname_len);
3016 	if(!dpns) {
3017 		/* If not interested, just stop processing this event */
3018 		verbose(VERB_ALGO, "subq: parent not interested anymore");
3019 		/* could be because parent was jostled out of the cache,
3020 		   and a new identical query arrived, that does not want it*/
3021 		return;
3022 	}
3023 
3024 	/* if iq->query_for_pside_glue then add the pside_glue (marked lame) */
3025 	if(iq->pside_glue) {
3026 		/* if the pside_glue is NULL, then it could not be found,
3027 		 * the done_pside is already set when created and a cache
3028 		 * entry created in processFinished so nothing to do here */
3029 		log_rrset_key(VERB_ALGO, "add parentside glue to dp",
3030 			iq->pside_glue);
3031 		if(!delegpt_add_rrset(foriq->dp, forq->region,
3032 			iq->pside_glue, 1))
3033 			log_err("out of memory adding pside glue");
3034 	}
3035 
3036 	/* This response is relevant to the current query, so we
3037 	 * add (attempt to add, anyway) this target(s) and reactivate
3038 	 * the original event.
3039 	 * NOTE: we could only look for the AnswerRRset if the
3040 	 * response type was ANSWER. */
3041 	rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
3042 	if(rrset) {
3043 		/* if CNAMEs have been followed - add new NS to delegpt. */
3044 		/* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
3045 		if(!delegpt_find_ns(foriq->dp, rrset->rk.dname,
3046 			rrset->rk.dname_len)) {
3047 			/* if dpns->lame then set newcname ns lame too */
3048 			if(!delegpt_add_ns(foriq->dp, forq->region,
3049 				rrset->rk.dname, dpns->lame))
3050 				log_err("out of memory adding cnamed-ns");
3051 		}
3052 		/* if dpns->lame then set the address(es) lame too */
3053 		if(!delegpt_add_rrset(foriq->dp, forq->region, rrset,
3054 			dpns->lame))
3055 			log_err("out of memory adding targets");
3056 		verbose(VERB_ALGO, "added target response");
3057 		delegpt_log(VERB_ALGO, foriq->dp);
3058 	} else {
3059 		verbose(VERB_ALGO, "iterator TargetResponse failed");
3060 		dpns->resolved = 1; /* fail the target */
3061 	}
3062 }
3063 
3064 /**
3065  * Process response for DS NS Find queries, that attempt to find the delegation
3066  * point where we ask the DS query from.
3067  *
3068  * @param qstate: query state.
3069  * @param id: module id.
3070  * @param forq: super query state.
3071  */
3072 static void
3073 processDSNSResponse(struct module_qstate* qstate, int id,
3074 	struct module_qstate* forq)
3075 {
3076 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
3077 
3078 	/* if the finished (iq->response) query has no NS set: continue
3079 	 * up to look for the right dp; nothing to change, do DPNSstate */
3080 	if(qstate->return_rcode != LDNS_RCODE_NOERROR)
3081 		return; /* seek further */
3082 	/* find the NS RRset (without allowing CNAMEs) */
3083 	if(!reply_find_rrset(qstate->return_msg->rep, qstate->qinfo.qname,
3084 		qstate->qinfo.qname_len, LDNS_RR_TYPE_NS,
3085 		qstate->qinfo.qclass)){
3086 		return; /* seek further */
3087 	}
3088 
3089 	/* else, store as DP and continue at querytargets */
3090 	foriq->state = QUERYTARGETS_STATE;
3091 	foriq->dp = delegpt_from_message(qstate->return_msg, forq->region);
3092 	if(!foriq->dp) {
3093 		log_err("out of memory in dsns dp alloc");
3094 		return; /* dp==NULL in QUERYTARGETS makes SERVFAIL */
3095 	}
3096 	/* success, go query the querytargets in the new dp (and go down) */
3097 }
3098 
3099 /**
3100  * Process response for qclass=ANY queries for a particular class.
3101  * Append to result or error-exit.
3102  *
3103  * @param qstate: query state.
3104  * @param id: module id.
3105  * @param forq: super query state.
3106  */
3107 static void
3108 processClassResponse(struct module_qstate* qstate, int id,
3109 	struct module_qstate* forq)
3110 {
3111 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
3112 	struct dns_msg* from = qstate->return_msg;
3113 	log_query_info(VERB_ALGO, "processClassResponse", &qstate->qinfo);
3114 	log_query_info(VERB_ALGO, "processClassResponse super", &forq->qinfo);
3115 	if(qstate->return_rcode != LDNS_RCODE_NOERROR) {
3116 		/* cause servfail for qclass ANY query */
3117 		foriq->response = NULL;
3118 		foriq->state = FINISHED_STATE;
3119 		return;
3120 	}
3121 	/* append result */
3122 	if(!foriq->response) {
3123 		/* allocate the response: copy RCODE, sec_state */
3124 		foriq->response = dns_copy_msg(from, forq->region);
3125 		if(!foriq->response) {
3126 			log_err("malloc failed for qclass ANY response");
3127 			foriq->state = FINISHED_STATE;
3128 			return;
3129 		}
3130 		foriq->response->qinfo.qclass = forq->qinfo.qclass;
3131 		/* qclass ANY does not receive the AA flag on replies */
3132 		foriq->response->rep->authoritative = 0;
3133 	} else {
3134 		struct dns_msg* to = foriq->response;
3135 		/* add _from_ this response _to_ existing collection */
3136 		/* if there are records, copy RCODE */
3137 		/* lower sec_state if this message is lower */
3138 		if(from->rep->rrset_count != 0) {
3139 			size_t n = from->rep->rrset_count+to->rep->rrset_count;
3140 			struct ub_packed_rrset_key** dest, **d;
3141 			/* copy appropriate rcode */
3142 			to->rep->flags = from->rep->flags;
3143 			/* copy rrsets */
3144 			if(from->rep->rrset_count > RR_COUNT_MAX ||
3145 				to->rep->rrset_count > RR_COUNT_MAX) {
3146 				log_err("malloc failed (too many rrsets) in collect ANY");
3147 				foriq->state = FINISHED_STATE;
3148 				return; /* integer overflow protection */
3149 			}
3150 			dest = regional_alloc(forq->region, sizeof(dest[0])*n);
3151 			if(!dest) {
3152 				log_err("malloc failed in collect ANY");
3153 				foriq->state = FINISHED_STATE;
3154 				return;
3155 			}
3156 			d = dest;
3157 			/* copy AN */
3158 			memcpy(dest, to->rep->rrsets, to->rep->an_numrrsets
3159 				* sizeof(dest[0]));
3160 			dest += to->rep->an_numrrsets;
3161 			memcpy(dest, from->rep->rrsets, from->rep->an_numrrsets
3162 				* sizeof(dest[0]));
3163 			dest += from->rep->an_numrrsets;
3164 			/* copy NS */
3165 			memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets,
3166 				to->rep->ns_numrrsets * sizeof(dest[0]));
3167 			dest += to->rep->ns_numrrsets;
3168 			memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets,
3169 				from->rep->ns_numrrsets * sizeof(dest[0]));
3170 			dest += from->rep->ns_numrrsets;
3171 			/* copy AR */
3172 			memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets+
3173 				to->rep->ns_numrrsets,
3174 				to->rep->ar_numrrsets * sizeof(dest[0]));
3175 			dest += to->rep->ar_numrrsets;
3176 			memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets+
3177 				from->rep->ns_numrrsets,
3178 				from->rep->ar_numrrsets * sizeof(dest[0]));
3179 			/* update counts */
3180 			to->rep->rrsets = d;
3181 			to->rep->an_numrrsets += from->rep->an_numrrsets;
3182 			to->rep->ns_numrrsets += from->rep->ns_numrrsets;
3183 			to->rep->ar_numrrsets += from->rep->ar_numrrsets;
3184 			to->rep->rrset_count = n;
3185 		}
3186 		if(from->rep->security < to->rep->security) /* lowest sec */
3187 			to->rep->security = from->rep->security;
3188 		if(from->rep->qdcount != 0) /* insert qd if appropriate */
3189 			to->rep->qdcount = from->rep->qdcount;
3190 		if(from->rep->ttl < to->rep->ttl) /* use smallest TTL */
3191 			to->rep->ttl = from->rep->ttl;
3192 		if(from->rep->prefetch_ttl < to->rep->prefetch_ttl)
3193 			to->rep->prefetch_ttl = from->rep->prefetch_ttl;
3194 	}
3195 	/* are we done? */
3196 	foriq->num_current_queries --;
3197 	if(foriq->num_current_queries == 0)
3198 		foriq->state = FINISHED_STATE;
3199 }
3200 
3201 /**
3202  * Collect class ANY responses and make them into one response.  This
3203  * state is started and it creates queries for all classes (that have
3204  * root hints).  The answers are then collected.
3205  *
3206  * @param qstate: query state.
3207  * @param id: module id.
3208  * @return true if the event needs more immediate processing, false if not.
3209  */
3210 static int
3211 processCollectClass(struct module_qstate* qstate, int id)
3212 {
3213 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3214 	struct module_qstate* subq;
3215 	/* If qchase.qclass == 0 then send out queries for all classes.
3216 	 * Otherwise, do nothing (wait for all answers to arrive and the
3217 	 * processClassResponse to put them together, and that moves us
3218 	 * towards the Finished state when done. */
3219 	if(iq->qchase.qclass == 0) {
3220 		uint16_t c = 0;
3221 		iq->qchase.qclass = LDNS_RR_CLASS_ANY;
3222 		while(iter_get_next_root(qstate->env->hints,
3223 			qstate->env->fwds, &c)) {
3224 			/* generate query for this class */
3225 			log_nametypeclass(VERB_ALGO, "spawn collect query",
3226 				qstate->qinfo.qname, qstate->qinfo.qtype, c);
3227 			if(!generate_sub_request(qstate->qinfo.qname,
3228 				qstate->qinfo.qname_len, qstate->qinfo.qtype,
3229 				c, qstate, id, iq, INIT_REQUEST_STATE,
3230 				FINISHED_STATE, &subq,
3231 				(int)!(qstate->query_flags&BIT_CD))) {
3232 				return error_response(qstate, id,
3233 					LDNS_RCODE_SERVFAIL);
3234 			}
3235 			/* ignore subq, no special init required */
3236 			iq->num_current_queries ++;
3237 			if(c == 0xffff)
3238 				break;
3239 			else c++;
3240 		}
3241 		/* if no roots are configured at all, return */
3242 		if(iq->num_current_queries == 0) {
3243 			verbose(VERB_ALGO, "No root hints or fwds, giving up "
3244 				"on qclass ANY");
3245 			return error_response(qstate, id, LDNS_RCODE_REFUSED);
3246 		}
3247 		/* return false, wait for queries to return */
3248 	}
3249 	/* if woke up here because of an answer, wait for more answers */
3250 	return 0;
3251 }
3252 
3253 /**
3254  * This handles the final state for first-tier responses (i.e., responses to
3255  * externally generated queries).
3256  *
3257  * @param qstate: query state.
3258  * @param iq: iterator query state.
3259  * @param id: module id.
3260  * @return true if the event needs more processing, false if not. Since this
3261  *         is the final state for an event, it always returns false.
3262  */
3263 static int
3264 processFinished(struct module_qstate* qstate, struct iter_qstate* iq,
3265 	int id)
3266 {
3267 	log_query_info(VERB_QUERY, "finishing processing for",
3268 		&qstate->qinfo);
3269 
3270 	/* store negative cache element for parent side glue. */
3271 	if(!qstate->no_cache_store && iq->query_for_pside_glue
3272 		&& !iq->pside_glue)
3273 			iter_store_parentside_neg(qstate->env, &qstate->qinfo,
3274 				iq->deleg_msg?iq->deleg_msg->rep:
3275 				(iq->response?iq->response->rep:NULL));
3276 	if(!iq->response) {
3277 		verbose(VERB_ALGO, "No response is set, servfail");
3278 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3279 	}
3280 
3281 	/* Make sure that the RA flag is set (since the presence of
3282 	 * this module means that recursion is available) */
3283 	iq->response->rep->flags |= BIT_RA;
3284 
3285 	/* Clear the AA flag */
3286 	/* FIXME: does this action go here or in some other module? */
3287 	iq->response->rep->flags &= ~BIT_AA;
3288 
3289 	/* make sure QR flag is on */
3290 	iq->response->rep->flags |= BIT_QR;
3291 
3292 	/* we have finished processing this query */
3293 	qstate->ext_state[id] = module_finished;
3294 
3295 	/* TODO:  we are using a private TTL, trim the response. */
3296 	/* if (mPrivateTTL > 0){IterUtils.setPrivateTTL(resp, mPrivateTTL); } */
3297 
3298 	/* prepend any items we have accumulated */
3299 	if(iq->an_prepend_list || iq->ns_prepend_list) {
3300 		if(!iter_prepend(iq, iq->response, qstate->region)) {
3301 			log_err("prepend rrsets: out of memory");
3302 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3303 		}
3304 		/* reset the query name back */
3305 		iq->response->qinfo = qstate->qinfo;
3306 		/* the security state depends on the combination */
3307 		iq->response->rep->security = sec_status_unchecked;
3308 		/* store message with the finished prepended items,
3309 		 * but only if we did recursion. The nonrecursion referral
3310 		 * from cache does not need to be stored in the msg cache. */
3311 		if(!qstate->no_cache_store && qstate->query_flags&BIT_RD) {
3312 			iter_dns_store(qstate->env, &qstate->qinfo,
3313 				iq->response->rep, 0, qstate->prefetch_leeway,
3314 				iq->dp&&iq->dp->has_parent_side_NS,
3315 				qstate->region, qstate->query_flags);
3316 		}
3317 	}
3318 	qstate->return_rcode = LDNS_RCODE_NOERROR;
3319 	qstate->return_msg = iq->response;
3320 	return 0;
3321 }
3322 
3323 /*
3324  * Return priming query results to interested super querystates.
3325  *
3326  * Sets the delegation point and delegation message (not nonRD queries).
3327  * This is a callback from walk_supers.
3328  *
3329  * @param qstate: query state that finished.
3330  * @param id: module id.
3331  * @param super: the qstate to inform.
3332  */
3333 void
3334 iter_inform_super(struct module_qstate* qstate, int id,
3335 	struct module_qstate* super)
3336 {
3337 	if(!qstate->is_priming && super->qinfo.qclass == LDNS_RR_CLASS_ANY)
3338 		processClassResponse(qstate, id, super);
3339 	else if(super->qinfo.qtype == LDNS_RR_TYPE_DS && ((struct iter_qstate*)
3340 		super->minfo[id])->state == DSNS_FIND_STATE)
3341 		processDSNSResponse(qstate, id, super);
3342 	else if(qstate->return_rcode != LDNS_RCODE_NOERROR)
3343 		error_supers(qstate, id, super);
3344 	else if(qstate->is_priming)
3345 		prime_supers(qstate, id, super);
3346 	else	processTargetResponse(qstate, id, super);
3347 }
3348 
3349 /**
3350  * Handle iterator state.
3351  * Handle events. This is the real processing loop for events, responsible
3352  * for moving events through the various states. If a processing method
3353  * returns true, then it will be advanced to the next state. If false, then
3354  * processing will stop.
3355  *
3356  * @param qstate: query state.
3357  * @param ie: iterator shared global environment.
3358  * @param iq: iterator query state.
3359  * @param id: module id.
3360  */
3361 static void
3362 iter_handle(struct module_qstate* qstate, struct iter_qstate* iq,
3363 	struct iter_env* ie, int id)
3364 {
3365 	int cont = 1;
3366 	while(cont) {
3367 		verbose(VERB_ALGO, "iter_handle processing q with state %s",
3368 			iter_state_to_string(iq->state));
3369 		switch(iq->state) {
3370 			case INIT_REQUEST_STATE:
3371 				cont = processInitRequest(qstate, iq, ie, id);
3372 				break;
3373 			case INIT_REQUEST_2_STATE:
3374 				cont = processInitRequest2(qstate, iq, id);
3375 				break;
3376 			case INIT_REQUEST_3_STATE:
3377 				cont = processInitRequest3(qstate, iq, id);
3378 				break;
3379 			case QUERYTARGETS_STATE:
3380 				cont = processQueryTargets(qstate, iq, ie, id);
3381 				break;
3382 			case QUERY_RESP_STATE:
3383 				cont = processQueryResponse(qstate, iq, id);
3384 				break;
3385 			case PRIME_RESP_STATE:
3386 				cont = processPrimeResponse(qstate, id);
3387 				break;
3388 			case COLLECT_CLASS_STATE:
3389 				cont = processCollectClass(qstate, id);
3390 				break;
3391 			case DSNS_FIND_STATE:
3392 				cont = processDSNSFind(qstate, iq, id);
3393 				break;
3394 			case FINISHED_STATE:
3395 				cont = processFinished(qstate, iq, id);
3396 				break;
3397 			default:
3398 				log_warn("iterator: invalid state: %d",
3399 					iq->state);
3400 				cont = 0;
3401 				break;
3402 		}
3403 	}
3404 }
3405 
3406 /**
3407  * This is the primary entry point for processing request events. Note that
3408  * this method should only be used by external modules.
3409  * @param qstate: query state.
3410  * @param ie: iterator shared global environment.
3411  * @param iq: iterator query state.
3412  * @param id: module id.
3413  */
3414 static void
3415 process_request(struct module_qstate* qstate, struct iter_qstate* iq,
3416 	struct iter_env* ie, int id)
3417 {
3418 	/* external requests start in the INIT state, and finish using the
3419 	 * FINISHED state. */
3420 	iq->state = INIT_REQUEST_STATE;
3421 	iq->final_state = FINISHED_STATE;
3422 	verbose(VERB_ALGO, "process_request: new external request event");
3423 	iter_handle(qstate, iq, ie, id);
3424 }
3425 
3426 /** process authoritative server reply */
3427 static void
3428 process_response(struct module_qstate* qstate, struct iter_qstate* iq,
3429 	struct iter_env* ie, int id, struct outbound_entry* outbound,
3430 	enum module_ev event)
3431 {
3432 	struct msg_parse* prs;
3433 	struct edns_data edns;
3434 	sldns_buffer* pkt;
3435 
3436 	verbose(VERB_ALGO, "process_response: new external response event");
3437 	iq->response = NULL;
3438 	iq->state = QUERY_RESP_STATE;
3439 	if(event == module_event_noreply || event == module_event_error) {
3440 		if(event == module_event_noreply && iq->sent_count >= 3 &&
3441 			qstate->env->cfg->use_caps_bits_for_id &&
3442 			!iq->caps_fallback) {
3443 			/* start fallback */
3444 			iq->caps_fallback = 1;
3445 			iq->caps_server = 0;
3446 			iq->caps_reply = NULL;
3447 			iq->caps_response = NULL;
3448 			iq->state = QUERYTARGETS_STATE;
3449 			iq->num_current_queries--;
3450 			/* need fresh attempts for the 0x20 fallback, if
3451 			 * that was the cause for the failure */
3452 			iter_dec_attempts(iq->dp, 3);
3453 			verbose(VERB_DETAIL, "Capsforid: timeouts, starting fallback");
3454 			goto handle_it;
3455 		}
3456 		goto handle_it;
3457 	}
3458 	if( (event != module_event_reply && event != module_event_capsfail)
3459 		|| !qstate->reply) {
3460 		log_err("Bad event combined with response");
3461 		outbound_list_remove(&iq->outlist, outbound);
3462 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3463 		return;
3464 	}
3465 
3466 	/* parse message */
3467 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
3468 		sizeof(struct msg_parse));
3469 	if(!prs) {
3470 		log_err("out of memory on incoming message");
3471 		/* like packet got dropped */
3472 		goto handle_it;
3473 	}
3474 	memset(prs, 0, sizeof(*prs));
3475 	memset(&edns, 0, sizeof(edns));
3476 	pkt = qstate->reply->c->buffer;
3477 	sldns_buffer_set_position(pkt, 0);
3478 	if(parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
3479 		verbose(VERB_ALGO, "parse error on reply packet");
3480 		goto handle_it;
3481 	}
3482 	/* edns is not examined, but removed from message to help cache */
3483 	if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
3484 		LDNS_RCODE_NOERROR)
3485 		goto handle_it;
3486 
3487 	/* Copy the edns options we may got from the back end */
3488 	if(edns.opt_list) {
3489 		qstate->edns_opts_back_in = edns_opt_copy_region(edns.opt_list,
3490 			qstate->region);
3491 		if(!qstate->edns_opts_back_in) {
3492 			log_err("out of memory on incoming message");
3493 			/* like packet got dropped */
3494 			goto handle_it;
3495 		}
3496 		if(!inplace_cb_edns_back_parsed_call(qstate->env, qstate)) {
3497 			log_err("unable to call edns_back_parsed callback");
3498 			goto handle_it;
3499 		}
3500 	}
3501 
3502 	/* remove CD-bit, we asked for in case we handle validation ourself */
3503 	prs->flags &= ~BIT_CD;
3504 
3505 	/* normalize and sanitize: easy to delete items from linked lists */
3506 	if(!scrub_message(pkt, prs, &iq->qinfo_out, iq->dp->name,
3507 		qstate->env->scratch, qstate->env, ie)) {
3508 		/* if 0x20 enabled, start fallback, but we have no message */
3509 		if(event == module_event_capsfail && !iq->caps_fallback) {
3510 			iq->caps_fallback = 1;
3511 			iq->caps_server = 0;
3512 			iq->caps_reply = NULL;
3513 			iq->caps_response = NULL;
3514 			iq->state = QUERYTARGETS_STATE;
3515 			iq->num_current_queries--;
3516 			verbose(VERB_DETAIL, "Capsforid: scrub failed, starting fallback with no response");
3517 		}
3518 		goto handle_it;
3519 	}
3520 
3521 	/* allocate response dns_msg in region */
3522 	iq->response = dns_alloc_msg(pkt, prs, qstate->region);
3523 	if(!iq->response)
3524 		goto handle_it;
3525 	log_query_info(VERB_DETAIL, "response for", &qstate->qinfo);
3526 	log_name_addr(VERB_DETAIL, "reply from", iq->dp->name,
3527 		&qstate->reply->addr, qstate->reply->addrlen);
3528 	if(verbosity >= VERB_ALGO)
3529 		log_dns_msg("incoming scrubbed packet:", &iq->response->qinfo,
3530 			iq->response->rep);
3531 
3532 	if(event == module_event_capsfail || iq->caps_fallback) {
3533 		/* for fallback we care about main answer, not additionals */
3534 		/* removing that makes comparison more likely to succeed */
3535 		caps_strip_reply(iq->response->rep);
3536 		if(!iq->caps_fallback) {
3537 			/* start fallback */
3538 			iq->caps_fallback = 1;
3539 			iq->caps_server = 0;
3540 			iq->caps_reply = iq->response->rep;
3541 			iq->caps_response = iq->response;
3542 			iq->state = QUERYTARGETS_STATE;
3543 			iq->num_current_queries--;
3544 			verbose(VERB_DETAIL, "Capsforid: starting fallback");
3545 			goto handle_it;
3546 		} else {
3547 			/* check if reply is the same, otherwise, fail */
3548 			if(!iq->caps_reply) {
3549 				iq->caps_reply = iq->response->rep;
3550 				iq->caps_response = iq->response;
3551 				iq->caps_server = -1; /*become zero at ++,
3552 				so that we start the full set of trials */
3553 			} else if(caps_failed_rcode(iq->caps_reply) &&
3554 				!caps_failed_rcode(iq->response->rep)) {
3555 				/* prefer to upgrade to non-SERVFAIL */
3556 				iq->caps_reply = iq->response->rep;
3557 				iq->caps_response = iq->response;
3558 			} else if(!caps_failed_rcode(iq->caps_reply) &&
3559 				caps_failed_rcode(iq->response->rep)) {
3560 				/* if we have non-SERVFAIL as answer then
3561 				 * we can ignore SERVFAILs for the equality
3562 				 * comparison */
3563 				/* no instructions here, skip other else */
3564 			} else if(caps_failed_rcode(iq->caps_reply) &&
3565 				caps_failed_rcode(iq->response->rep)) {
3566 				/* failure is same as other failure in fallbk*/
3567 				/* no instructions here, skip other else */
3568 			} else if(!reply_equal(iq->response->rep, iq->caps_reply,
3569 				qstate->env->scratch)) {
3570 				verbose(VERB_DETAIL, "Capsforid fallback: "
3571 					"getting different replies, failed");
3572 				outbound_list_remove(&iq->outlist, outbound);
3573 				(void)error_response(qstate, id,
3574 					LDNS_RCODE_SERVFAIL);
3575 				return;
3576 			}
3577 			/* continue the fallback procedure at next server */
3578 			iq->caps_server++;
3579 			iq->state = QUERYTARGETS_STATE;
3580 			iq->num_current_queries--;
3581 			verbose(VERB_DETAIL, "Capsforid: reply is equal. "
3582 				"go to next fallback");
3583 			goto handle_it;
3584 		}
3585 	}
3586 	iq->caps_fallback = 0; /* if we were in fallback, 0x20 is OK now */
3587 
3588 handle_it:
3589 	outbound_list_remove(&iq->outlist, outbound);
3590 	iter_handle(qstate, iq, ie, id);
3591 }
3592 
3593 void
3594 iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
3595 	struct outbound_entry* outbound)
3596 {
3597 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
3598 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3599 	verbose(VERB_QUERY, "iterator[module %d] operate: extstate:%s event:%s",
3600 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
3601 	if(iq) log_query_info(VERB_QUERY, "iterator operate: query",
3602 		&qstate->qinfo);
3603 	if(iq && qstate->qinfo.qname != iq->qchase.qname)
3604 		log_query_info(VERB_QUERY, "iterator operate: chased to",
3605 			&iq->qchase);
3606 
3607 	/* perform iterator state machine */
3608 	if((event == module_event_new || event == module_event_pass) &&
3609 		iq == NULL) {
3610 		if(!iter_new(qstate, id)) {
3611 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3612 			return;
3613 		}
3614 		iq = (struct iter_qstate*)qstate->minfo[id];
3615 		process_request(qstate, iq, ie, id);
3616 		return;
3617 	}
3618 	if(iq && event == module_event_pass) {
3619 		iter_handle(qstate, iq, ie, id);
3620 		return;
3621 	}
3622 	if(iq && outbound) {
3623 		process_response(qstate, iq, ie, id, outbound, event);
3624 		return;
3625 	}
3626 	if(event == module_event_error) {
3627 		verbose(VERB_ALGO, "got called with event error, giving up");
3628 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3629 		return;
3630 	}
3631 
3632 	log_err("bad event for iterator");
3633 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3634 }
3635 
3636 void
3637 iter_clear(struct module_qstate* qstate, int id)
3638 {
3639 	struct iter_qstate* iq;
3640 	if(!qstate)
3641 		return;
3642 	iq = (struct iter_qstate*)qstate->minfo[id];
3643 	if(iq) {
3644 		outbound_list_clear(&iq->outlist);
3645 		if(iq->target_count && --iq->target_count[0] == 0)
3646 			free(iq->target_count);
3647 		iq->num_current_queries = 0;
3648 	}
3649 	qstate->minfo[id] = NULL;
3650 }
3651 
3652 size_t
3653 iter_get_mem(struct module_env* env, int id)
3654 {
3655 	struct iter_env* ie = (struct iter_env*)env->modinfo[id];
3656 	if(!ie)
3657 		return 0;
3658 	return sizeof(*ie) + sizeof(int)*((size_t)ie->max_dependency_depth+1)
3659 		+ donotq_get_mem(ie->donotq) + priv_get_mem(ie->priv);
3660 }
3661 
3662 /**
3663  * The iterator function block
3664  */
3665 static struct module_func_block iter_block = {
3666 	"iterator",
3667 	&iter_init, &iter_deinit, &iter_operate, &iter_inform_super,
3668 	&iter_clear, &iter_get_mem
3669 };
3670 
3671 struct module_func_block*
3672 iter_get_funcblock(void)
3673 {
3674 	return &iter_block;
3675 }
3676 
3677 const char*
3678 iter_state_to_string(enum iter_state state)
3679 {
3680 	switch (state)
3681 	{
3682 	case INIT_REQUEST_STATE :
3683 		return "INIT REQUEST STATE";
3684 	case INIT_REQUEST_2_STATE :
3685 		return "INIT REQUEST STATE (stage 2)";
3686 	case INIT_REQUEST_3_STATE:
3687 		return "INIT REQUEST STATE (stage 3)";
3688 	case QUERYTARGETS_STATE :
3689 		return "QUERY TARGETS STATE";
3690 	case PRIME_RESP_STATE :
3691 		return "PRIME RESPONSE STATE";
3692 	case COLLECT_CLASS_STATE :
3693 		return "COLLECT CLASS STATE";
3694 	case DSNS_FIND_STATE :
3695 		return "DSNS FIND STATE";
3696 	case QUERY_RESP_STATE :
3697 		return "QUERY RESPONSE STATE";
3698 	case FINISHED_STATE :
3699 		return "FINISHED RESPONSE STATE";
3700 	default :
3701 		return "UNKNOWN ITER STATE";
3702 	}
3703 }
3704 
3705 int
3706 iter_state_is_responsestate(enum iter_state s)
3707 {
3708 	switch(s) {
3709 		case INIT_REQUEST_STATE :
3710 		case INIT_REQUEST_2_STATE :
3711 		case INIT_REQUEST_3_STATE :
3712 		case QUERYTARGETS_STATE :
3713 		case COLLECT_CLASS_STATE :
3714 			return 0;
3715 		default:
3716 			break;
3717 	}
3718 	return 1;
3719 }
3720