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