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