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