xref: /freebsd/contrib/unbound/iterator/iterator.c (revision 865f46b255599c4a645e84a4cbb5ea7abdc0e207)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * iterator/iterator.c - iterative resolver DNS query response module
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
398a384985SDag-Erling Smørgrav  * This file contains a module that performs recursive iterative DNS query
40b7579f77SDag-Erling Smørgrav  * processing.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #include "config.h"
44b7579f77SDag-Erling Smørgrav #include "iterator/iterator.h"
45b7579f77SDag-Erling Smørgrav #include "iterator/iter_utils.h"
46b7579f77SDag-Erling Smørgrav #include "iterator/iter_hints.h"
47b7579f77SDag-Erling Smørgrav #include "iterator/iter_fwd.h"
48b7579f77SDag-Erling Smørgrav #include "iterator/iter_donotq.h"
49b7579f77SDag-Erling Smørgrav #include "iterator/iter_delegpt.h"
50b7579f77SDag-Erling Smørgrav #include "iterator/iter_resptype.h"
51b7579f77SDag-Erling Smørgrav #include "iterator/iter_scrub.h"
52b7579f77SDag-Erling Smørgrav #include "iterator/iter_priv.h"
53b7579f77SDag-Erling Smørgrav #include "validator/val_neg.h"
54b7579f77SDag-Erling Smørgrav #include "services/cache/dns.h"
55b7579f77SDag-Erling Smørgrav #include "services/cache/infra.h"
5657bddd21SDag-Erling Smørgrav #include "services/authzone.h"
57b7579f77SDag-Erling Smørgrav #include "util/module.h"
58b7579f77SDag-Erling Smørgrav #include "util/netevent.h"
59b7579f77SDag-Erling Smørgrav #include "util/net_help.h"
60b7579f77SDag-Erling Smørgrav #include "util/regional.h"
61b7579f77SDag-Erling Smørgrav #include "util/data/dname.h"
62b7579f77SDag-Erling Smørgrav #include "util/data/msgencode.h"
63b7579f77SDag-Erling Smørgrav #include "util/fptr_wlist.h"
64b7579f77SDag-Erling Smørgrav #include "util/config_file.h"
6509a3aaf3SDag-Erling Smørgrav #include "util/random.h"
6609a3aaf3SDag-Erling Smørgrav #include "sldns/rrdef.h"
6709a3aaf3SDag-Erling Smørgrav #include "sldns/wire2str.h"
6805ab2901SDag-Erling Smørgrav #include "sldns/str2wire.h"
6909a3aaf3SDag-Erling Smørgrav #include "sldns/parseutil.h"
7009a3aaf3SDag-Erling Smørgrav #include "sldns/sbuffer.h"
71b7579f77SDag-Erling Smørgrav 
72e86b9096SDag-Erling Smørgrav /* in msec */
73e86b9096SDag-Erling Smørgrav int UNKNOWN_SERVER_NICENESS = 376;
74790c6b24SCy Schubert /* in msec */
75790c6b24SCy Schubert int USEFUL_SERVER_TOP_TIMEOUT = 120000;
76790c6b24SCy Schubert /* Equals USEFUL_SERVER_TOP_TIMEOUT*4 */
77790c6b24SCy Schubert int BLACKLIST_PENALTY = (120000*4);
78e86b9096SDag-Erling Smørgrav 
79091e9e46SCy Schubert static void target_count_increase_nx(struct iter_qstate* iq, int num);
80091e9e46SCy Schubert 
81b7579f77SDag-Erling Smørgrav int
82b7579f77SDag-Erling Smørgrav iter_init(struct module_env* env, int id)
83b7579f77SDag-Erling Smørgrav {
84b7579f77SDag-Erling Smørgrav 	struct iter_env* iter_env = (struct iter_env*)calloc(1,
85b7579f77SDag-Erling Smørgrav 		sizeof(struct iter_env));
86b7579f77SDag-Erling Smørgrav 	if(!iter_env) {
87b7579f77SDag-Erling Smørgrav 		log_err("malloc failure");
88b7579f77SDag-Erling Smørgrav 		return 0;
89b7579f77SDag-Erling Smørgrav 	}
90b7579f77SDag-Erling Smørgrav 	env->modinfo[id] = (void*)iter_env;
91971980c3SDag-Erling Smørgrav 
92971980c3SDag-Erling Smørgrav 	lock_basic_init(&iter_env->queries_ratelimit_lock);
93971980c3SDag-Erling Smørgrav 	lock_protect(&iter_env->queries_ratelimit_lock,
94971980c3SDag-Erling Smørgrav 			&iter_env->num_queries_ratelimited,
95971980c3SDag-Erling Smørgrav 		sizeof(iter_env->num_queries_ratelimited));
96971980c3SDag-Erling Smørgrav 
97b7579f77SDag-Erling Smørgrav 	if(!iter_apply_cfg(iter_env, env->cfg)) {
98b7579f77SDag-Erling Smørgrav 		log_err("iterator: could not apply configuration settings.");
99b7579f77SDag-Erling Smørgrav 		return 0;
100b7579f77SDag-Erling Smørgrav 	}
10105ab2901SDag-Erling Smørgrav 
102b7579f77SDag-Erling Smørgrav 	return 1;
103b7579f77SDag-Erling Smørgrav }
104b7579f77SDag-Erling Smørgrav 
10509a3aaf3SDag-Erling Smørgrav /** delete caps_whitelist element */
10609a3aaf3SDag-Erling Smørgrav static void
1073005e0a3SDag-Erling Smørgrav caps_free(struct rbnode_type* n, void* ATTR_UNUSED(d))
10809a3aaf3SDag-Erling Smørgrav {
10909a3aaf3SDag-Erling Smørgrav 	if(n) {
11009a3aaf3SDag-Erling Smørgrav 		free(((struct name_tree_node*)n)->name);
11109a3aaf3SDag-Erling Smørgrav 		free(n);
11209a3aaf3SDag-Erling Smørgrav 	}
11309a3aaf3SDag-Erling Smørgrav }
11409a3aaf3SDag-Erling Smørgrav 
115b7579f77SDag-Erling Smørgrav void
116b7579f77SDag-Erling Smørgrav iter_deinit(struct module_env* env, int id)
117b7579f77SDag-Erling Smørgrav {
118b7579f77SDag-Erling Smørgrav 	struct iter_env* iter_env;
119b7579f77SDag-Erling Smørgrav 	if(!env || !env->modinfo[id])
120b7579f77SDag-Erling Smørgrav 		return;
121b7579f77SDag-Erling Smørgrav 	iter_env = (struct iter_env*)env->modinfo[id];
122971980c3SDag-Erling Smørgrav 	lock_basic_destroy(&iter_env->queries_ratelimit_lock);
123b7579f77SDag-Erling Smørgrav 	free(iter_env->target_fetch_policy);
124b7579f77SDag-Erling Smørgrav 	priv_delete(iter_env->priv);
125b7579f77SDag-Erling Smørgrav 	donotq_delete(iter_env->donotq);
12609a3aaf3SDag-Erling Smørgrav 	if(iter_env->caps_white) {
12709a3aaf3SDag-Erling Smørgrav 		traverse_postorder(iter_env->caps_white, caps_free, NULL);
12809a3aaf3SDag-Erling Smørgrav 		free(iter_env->caps_white);
12909a3aaf3SDag-Erling Smørgrav 	}
130b7579f77SDag-Erling Smørgrav 	free(iter_env);
131b7579f77SDag-Erling Smørgrav 	env->modinfo[id] = NULL;
132b7579f77SDag-Erling Smørgrav }
133b7579f77SDag-Erling Smørgrav 
134b7579f77SDag-Erling Smørgrav /** new query for iterator */
135b7579f77SDag-Erling Smørgrav static int
136b7579f77SDag-Erling Smørgrav iter_new(struct module_qstate* qstate, int id)
137b7579f77SDag-Erling Smørgrav {
138b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq = (struct iter_qstate*)regional_alloc(
139b7579f77SDag-Erling Smørgrav 		qstate->region, sizeof(struct iter_qstate));
140b7579f77SDag-Erling Smørgrav 	qstate->minfo[id] = iq;
141b7579f77SDag-Erling Smørgrav 	if(!iq)
142b7579f77SDag-Erling Smørgrav 		return 0;
143b7579f77SDag-Erling Smørgrav 	memset(iq, 0, sizeof(*iq));
144b7579f77SDag-Erling Smørgrav 	iq->state = INIT_REQUEST_STATE;
145b7579f77SDag-Erling Smørgrav 	iq->final_state = FINISHED_STATE;
146b7579f77SDag-Erling Smørgrav 	iq->an_prepend_list = NULL;
147b7579f77SDag-Erling Smørgrav 	iq->an_prepend_last = NULL;
148b7579f77SDag-Erling Smørgrav 	iq->ns_prepend_list = NULL;
149b7579f77SDag-Erling Smørgrav 	iq->ns_prepend_last = NULL;
150b7579f77SDag-Erling Smørgrav 	iq->dp = NULL;
151b7579f77SDag-Erling Smørgrav 	iq->depth = 0;
152b7579f77SDag-Erling Smørgrav 	iq->num_target_queries = 0;
153b7579f77SDag-Erling Smørgrav 	iq->num_current_queries = 0;
154b7579f77SDag-Erling Smørgrav 	iq->query_restart_count = 0;
155b7579f77SDag-Erling Smørgrav 	iq->referral_count = 0;
156b7579f77SDag-Erling Smørgrav 	iq->sent_count = 0;
15709a3aaf3SDag-Erling Smørgrav 	iq->ratelimit_ok = 0;
15852df462fSXin LI 	iq->target_count = NULL;
159091e9e46SCy Schubert 	iq->dp_target_count = 0;
160b7579f77SDag-Erling Smørgrav 	iq->wait_priming_stub = 0;
161b7579f77SDag-Erling Smørgrav 	iq->refetch_glue = 0;
162b7579f77SDag-Erling Smørgrav 	iq->dnssec_expected = 0;
163b7579f77SDag-Erling Smørgrav 	iq->dnssec_lame_query = 0;
164b7579f77SDag-Erling Smørgrav 	iq->chase_flags = qstate->query_flags;
165b7579f77SDag-Erling Smørgrav 	/* Start with the (current) qname. */
166b7579f77SDag-Erling Smørgrav 	iq->qchase = qstate->qinfo;
167b7579f77SDag-Erling Smørgrav 	outbound_list_init(&iq->outlist);
168e2d15004SDag-Erling Smørgrav 	iq->minimise_count = 0;
16925039b37SCy Schubert 	iq->timeout_count = 0;
17005ab2901SDag-Erling Smørgrav 	if (qstate->env->cfg->qname_minimisation)
17105ab2901SDag-Erling Smørgrav 		iq->minimisation_state = INIT_MINIMISE_STATE;
17205ab2901SDag-Erling Smørgrav 	else
17305ab2901SDag-Erling Smørgrav 		iq->minimisation_state = DONOT_MINIMISE_STATE;
17405ab2901SDag-Erling Smørgrav 
17505ab2901SDag-Erling Smørgrav 	memset(&iq->qinfo_out, 0, sizeof(struct query_info));
176b7579f77SDag-Erling Smørgrav 	return 1;
177b7579f77SDag-Erling Smørgrav }
178b7579f77SDag-Erling Smørgrav 
179b7579f77SDag-Erling Smørgrav /**
180b7579f77SDag-Erling Smørgrav  * Transition to the next state. This can be used to advance a currently
181b7579f77SDag-Erling Smørgrav  * processing event. It cannot be used to reactivate a forEvent.
182b7579f77SDag-Erling Smørgrav  *
183b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state
184b7579f77SDag-Erling Smørgrav  * @param nextstate The state to transition to.
185b7579f77SDag-Erling Smørgrav  * @return true. This is so this can be called as the return value for the
186b7579f77SDag-Erling Smørgrav  *         actual process*State() methods. (Transitioning to the next state
187b7579f77SDag-Erling Smørgrav  *         implies further processing).
188b7579f77SDag-Erling Smørgrav  */
189b7579f77SDag-Erling Smørgrav static int
190b7579f77SDag-Erling Smørgrav next_state(struct iter_qstate* iq, enum iter_state nextstate)
191b7579f77SDag-Erling Smørgrav {
192b7579f77SDag-Erling Smørgrav 	/* If transitioning to a "response" state, make sure that there is a
193b7579f77SDag-Erling Smørgrav 	 * response */
194b7579f77SDag-Erling Smørgrav 	if(iter_state_is_responsestate(nextstate)) {
195b7579f77SDag-Erling Smørgrav 		if(iq->response == NULL) {
196b7579f77SDag-Erling Smørgrav 			log_err("transitioning to response state sans "
197b7579f77SDag-Erling Smørgrav 				"response.");
198b7579f77SDag-Erling Smørgrav 		}
199b7579f77SDag-Erling Smørgrav 	}
200b7579f77SDag-Erling Smørgrav 	iq->state = nextstate;
201b7579f77SDag-Erling Smørgrav 	return 1;
202b7579f77SDag-Erling Smørgrav }
203b7579f77SDag-Erling Smørgrav 
204b7579f77SDag-Erling Smørgrav /**
205b7579f77SDag-Erling Smørgrav  * Transition an event to its final state. Final states always either return
206b7579f77SDag-Erling Smørgrav  * a result up the module chain, or reactivate a dependent event. Which
20705ab2901SDag-Erling Smørgrav  * final state to transition to is set in the module state for the event when
208b7579f77SDag-Erling Smørgrav  * it was created, and depends on the original purpose of the event.
209b7579f77SDag-Erling Smørgrav  *
210b7579f77SDag-Erling Smørgrav  * The response is stored in the qstate->buf buffer.
211b7579f77SDag-Erling Smørgrav  *
212b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state
213b7579f77SDag-Erling Smørgrav  * @return false. This is so this method can be used as the return value for
214b7579f77SDag-Erling Smørgrav  *         the processState methods. (Transitioning to the final state
215b7579f77SDag-Erling Smørgrav  */
216b7579f77SDag-Erling Smørgrav static int
217b7579f77SDag-Erling Smørgrav final_state(struct iter_qstate* iq)
218b7579f77SDag-Erling Smørgrav {
219b7579f77SDag-Erling Smørgrav 	return next_state(iq, iq->final_state);
220b7579f77SDag-Erling Smørgrav }
221b7579f77SDag-Erling Smørgrav 
222b7579f77SDag-Erling Smørgrav /**
223b7579f77SDag-Erling Smørgrav  * Callback routine to handle errors in parent query states
224b7579f77SDag-Erling Smørgrav  * @param qstate: query state that failed.
225b7579f77SDag-Erling Smørgrav  * @param id: module id.
226b7579f77SDag-Erling Smørgrav  * @param super: super state.
227b7579f77SDag-Erling Smørgrav  */
228b7579f77SDag-Erling Smørgrav static void
229b7579f77SDag-Erling Smørgrav error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
230b7579f77SDag-Erling Smørgrav {
231091e9e46SCy Schubert 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
232b7579f77SDag-Erling Smørgrav 	struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
233b7579f77SDag-Erling Smørgrav 
234b7579f77SDag-Erling Smørgrav 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
235b7579f77SDag-Erling Smørgrav 		qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
236b7579f77SDag-Erling Smørgrav 		/* mark address as failed. */
237b7579f77SDag-Erling Smørgrav 		struct delegpt_ns* dpns = NULL;
238b5663de9SDag-Erling Smørgrav 		super_iq->num_target_queries--;
239b7579f77SDag-Erling Smørgrav 		if(super_iq->dp)
240b7579f77SDag-Erling Smørgrav 			dpns = delegpt_find_ns(super_iq->dp,
241b7579f77SDag-Erling Smørgrav 				qstate->qinfo.qname, qstate->qinfo.qname_len);
242b7579f77SDag-Erling Smørgrav 		if(!dpns) {
243b7579f77SDag-Erling Smørgrav 			/* not interested */
2444c75e3aaSDag-Erling Smørgrav 			/* this can happen, for eg. qname minimisation asked
2454c75e3aaSDag-Erling Smørgrav 			 * for an NXDOMAIN to be validated, and used qtype
2464c75e3aaSDag-Erling Smørgrav 			 * A for that, and the error of that, the name, is
2474c75e3aaSDag-Erling Smørgrav 			 * not listed in super_iq->dp */
248b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "subq error, but not interested");
249b7579f77SDag-Erling Smørgrav 			log_query_info(VERB_ALGO, "superq", &super->qinfo);
250b7579f77SDag-Erling Smørgrav 			return;
251b7579f77SDag-Erling Smørgrav 		} else {
252b7579f77SDag-Erling Smørgrav 			/* see if the failure did get (parent-lame) info */
253bc892140SDag-Erling Smørgrav 			if(!cache_fill_missing(super->env, super_iq->qchase.qclass,
254bc892140SDag-Erling Smørgrav 				super->region, super_iq->dp))
255b7579f77SDag-Erling Smørgrav 				log_err("out of memory adding missing");
256b7579f77SDag-Erling Smørgrav 		}
257091e9e46SCy Schubert 		delegpt_mark_neg(dpns, qstate->qinfo.qtype);
258091e9e46SCy Schubert 		if((dpns->got4 == 2 || !ie->supports_ipv4) &&
2590a92a9fcSCy Schubert 			(dpns->got6 == 2 || !ie->supports_ipv6)) {
260*865f46b2SCy Schubert 			dpns->resolved = 1; /* mark as failed */
261091e9e46SCy Schubert 			target_count_increase_nx(super_iq, 1);
262b7579f77SDag-Erling Smørgrav 		}
2630a92a9fcSCy Schubert 	}
264b7579f77SDag-Erling Smørgrav 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
265b7579f77SDag-Erling Smørgrav 		/* prime failed to get delegation */
266b7579f77SDag-Erling Smørgrav 		super_iq->dp = NULL;
267b7579f77SDag-Erling Smørgrav 	}
268b7579f77SDag-Erling Smørgrav 	/* evaluate targets again */
269b7579f77SDag-Erling Smørgrav 	super_iq->state = QUERYTARGETS_STATE;
270b7579f77SDag-Erling Smørgrav 	/* super becomes runnable, and will process this change */
271b7579f77SDag-Erling Smørgrav }
272b7579f77SDag-Erling Smørgrav 
273b7579f77SDag-Erling Smørgrav /**
274b7579f77SDag-Erling Smørgrav  * Return an error to the client
275b7579f77SDag-Erling Smørgrav  * @param qstate: our query state
276b7579f77SDag-Erling Smørgrav  * @param id: module id
277b7579f77SDag-Erling Smørgrav  * @param rcode: error code (DNS errcode).
278b7579f77SDag-Erling Smørgrav  * @return: 0 for use by caller, to make notation easy, like:
279b7579f77SDag-Erling Smørgrav  * 	return error_response(..).
280b7579f77SDag-Erling Smørgrav  */
281b7579f77SDag-Erling Smørgrav static int
282b7579f77SDag-Erling Smørgrav error_response(struct module_qstate* qstate, int id, int rcode)
283b7579f77SDag-Erling Smørgrav {
284b7579f77SDag-Erling Smørgrav 	verbose(VERB_QUERY, "return error response %s",
28517d15b25SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)?
28617d15b25SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
287b7579f77SDag-Erling Smørgrav 	qstate->return_rcode = rcode;
288b7579f77SDag-Erling Smørgrav 	qstate->return_msg = NULL;
289b7579f77SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
290b7579f77SDag-Erling Smørgrav 	return 0;
291b7579f77SDag-Erling Smørgrav }
292b7579f77SDag-Erling Smørgrav 
293b7579f77SDag-Erling Smørgrav /**
294b7579f77SDag-Erling Smørgrav  * Return an error to the client and cache the error code in the
295b7579f77SDag-Erling Smørgrav  * message cache (so per qname, qtype, qclass).
296b7579f77SDag-Erling Smørgrav  * @param qstate: our query state
297b7579f77SDag-Erling Smørgrav  * @param id: module id
298b7579f77SDag-Erling Smørgrav  * @param rcode: error code (DNS errcode).
299b7579f77SDag-Erling Smørgrav  * @return: 0 for use by caller, to make notation easy, like:
300b7579f77SDag-Erling Smørgrav  * 	return error_response(..).
301b7579f77SDag-Erling Smørgrav  */
302b7579f77SDag-Erling Smørgrav static int
303b7579f77SDag-Erling Smørgrav error_response_cache(struct module_qstate* qstate, int id, int rcode)
304b7579f77SDag-Erling Smørgrav {
305bc892140SDag-Erling Smørgrav 	if(!qstate->no_cache_store) {
306b7579f77SDag-Erling Smørgrav 		/* store in cache */
307b7579f77SDag-Erling Smørgrav 		struct reply_info err;
308ff825849SDag-Erling Smørgrav 		if(qstate->prefetch_leeway > NORR_TTL) {
309ff825849SDag-Erling Smørgrav 			verbose(VERB_ALGO, "error response for prefetch in cache");
310ff825849SDag-Erling Smørgrav 			/* attempt to adjust the cache entry prefetch */
311ff825849SDag-Erling Smørgrav 			if(dns_cache_prefetch_adjust(qstate->env, &qstate->qinfo,
312ff825849SDag-Erling Smørgrav 				NORR_TTL, qstate->query_flags))
313ff825849SDag-Erling Smørgrav 				return error_response(qstate, id, rcode);
314ff825849SDag-Erling Smørgrav 			/* if that fails (not in cache), fall through to store err */
315ff825849SDag-Erling Smørgrav 		}
316c7f4d7adSDag-Erling Smørgrav 		if(qstate->env->cfg->serve_expired) {
317c7f4d7adSDag-Erling Smørgrav 			/* if serving expired contents, and such content is
318c7f4d7adSDag-Erling Smørgrav 			 * already available, don't overwrite this servfail */
319c7f4d7adSDag-Erling Smørgrav 			struct msgreply_entry* msg;
320c7f4d7adSDag-Erling Smørgrav 			if((msg=msg_cache_lookup(qstate->env,
321c7f4d7adSDag-Erling Smørgrav 				qstate->qinfo.qname, qstate->qinfo.qname_len,
322c7f4d7adSDag-Erling Smørgrav 				qstate->qinfo.qtype, qstate->qinfo.qclass,
3234c75e3aaSDag-Erling Smørgrav 				qstate->query_flags, 0,
3244c75e3aaSDag-Erling Smørgrav 				qstate->env->cfg->serve_expired_ttl_reset))
325c7f4d7adSDag-Erling Smørgrav 				!= NULL) {
3264c75e3aaSDag-Erling Smørgrav 				if(qstate->env->cfg->serve_expired_ttl_reset) {
3274c75e3aaSDag-Erling Smørgrav 					struct reply_info* rep =
3284c75e3aaSDag-Erling Smørgrav 						(struct reply_info*)msg->entry.data;
3294c75e3aaSDag-Erling Smørgrav 					if(rep && *qstate->env->now +
3304c75e3aaSDag-Erling Smørgrav 						qstate->env->cfg->serve_expired_ttl  >
3314c75e3aaSDag-Erling Smørgrav 						rep->serve_expired_ttl) {
3324c75e3aaSDag-Erling Smørgrav 						rep->serve_expired_ttl =
3334c75e3aaSDag-Erling Smørgrav 							*qstate->env->now +
3344c75e3aaSDag-Erling Smørgrav 							qstate->env->cfg->serve_expired_ttl;
3354c75e3aaSDag-Erling Smørgrav 					}
3364c75e3aaSDag-Erling Smørgrav 				}
337c7f4d7adSDag-Erling Smørgrav 				lock_rw_unlock(&msg->entry.lock);
338c7f4d7adSDag-Erling Smørgrav 				return error_response(qstate, id, rcode);
339c7f4d7adSDag-Erling Smørgrav 			}
340c7f4d7adSDag-Erling Smørgrav 			/* serving expired contents, but nothing is cached
341c7f4d7adSDag-Erling Smørgrav 			 * at all, so the servfail cache entry is useful
342c7f4d7adSDag-Erling Smørgrav 			 * (stops waste of time on this servfail NORR_TTL) */
343e86b9096SDag-Erling Smørgrav 		} else {
344e86b9096SDag-Erling Smørgrav 			/* don't overwrite existing (non-expired) data in
345e86b9096SDag-Erling Smørgrav 			 * cache with a servfail */
346e86b9096SDag-Erling Smørgrav 			struct msgreply_entry* msg;
347e86b9096SDag-Erling Smørgrav 			if((msg=msg_cache_lookup(qstate->env,
348e86b9096SDag-Erling Smørgrav 				qstate->qinfo.qname, qstate->qinfo.qname_len,
349e86b9096SDag-Erling Smørgrav 				qstate->qinfo.qtype, qstate->qinfo.qclass,
350e86b9096SDag-Erling Smørgrav 				qstate->query_flags, *qstate->env->now, 0))
351e86b9096SDag-Erling Smørgrav 				!= NULL) {
352e86b9096SDag-Erling Smørgrav 				struct reply_info* rep = (struct reply_info*)
353e86b9096SDag-Erling Smørgrav 					msg->entry.data;
354e86b9096SDag-Erling Smørgrav 				if(FLAGS_GET_RCODE(rep->flags) ==
355e86b9096SDag-Erling Smørgrav 					LDNS_RCODE_NOERROR ||
356e86b9096SDag-Erling Smørgrav 					FLAGS_GET_RCODE(rep->flags) ==
357e86b9096SDag-Erling Smørgrav 					LDNS_RCODE_NXDOMAIN) {
358e86b9096SDag-Erling Smørgrav 					/* we have a good entry,
359e86b9096SDag-Erling Smørgrav 					 * don't overwrite */
360e86b9096SDag-Erling Smørgrav 					lock_rw_unlock(&msg->entry.lock);
361e86b9096SDag-Erling Smørgrav 					return error_response(qstate, id, rcode);
362e86b9096SDag-Erling Smørgrav 				}
363e86b9096SDag-Erling Smørgrav 				lock_rw_unlock(&msg->entry.lock);
364e86b9096SDag-Erling Smørgrav 			}
365e86b9096SDag-Erling Smørgrav 
366c7f4d7adSDag-Erling Smørgrav 		}
367b7579f77SDag-Erling Smørgrav 		memset(&err, 0, sizeof(err));
368b7579f77SDag-Erling Smørgrav 		err.flags = (uint16_t)(BIT_QR | BIT_RA);
369b7579f77SDag-Erling Smørgrav 		FLAGS_SET_RCODE(err.flags, rcode);
370b7579f77SDag-Erling Smørgrav 		err.qdcount = 1;
371b7579f77SDag-Erling Smørgrav 		err.ttl = NORR_TTL;
372b7579f77SDag-Erling Smørgrav 		err.prefetch_ttl = PREFETCH_TTL_CALC(err.ttl);
3734c75e3aaSDag-Erling Smørgrav 		err.serve_expired_ttl = NORR_TTL;
374b7579f77SDag-Erling Smørgrav 		/* do not waste time trying to validate this servfail */
375b7579f77SDag-Erling Smørgrav 		err.security = sec_status_indeterminate;
376b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "store error response in message cache");
377ff825849SDag-Erling Smørgrav 		iter_dns_store(qstate->env, &qstate->qinfo, &err, 0, 0, 0, NULL,
378790c6b24SCy Schubert 			qstate->query_flags, qstate->qstarttime);
379bc892140SDag-Erling Smørgrav 	}
380b7579f77SDag-Erling Smørgrav 	return error_response(qstate, id, rcode);
381b7579f77SDag-Erling Smørgrav }
382b7579f77SDag-Erling Smørgrav 
383b7579f77SDag-Erling Smørgrav /** check if prepend item is duplicate item */
384b7579f77SDag-Erling Smørgrav static int
385b7579f77SDag-Erling Smørgrav prepend_is_duplicate(struct ub_packed_rrset_key** sets, size_t to,
386b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* dup)
387b7579f77SDag-Erling Smørgrav {
388b7579f77SDag-Erling Smørgrav 	size_t i;
389b7579f77SDag-Erling Smørgrav 	for(i=0; i<to; i++) {
390b7579f77SDag-Erling Smørgrav 		if(sets[i]->rk.type == dup->rk.type &&
391b7579f77SDag-Erling Smørgrav 			sets[i]->rk.rrset_class == dup->rk.rrset_class &&
392b7579f77SDag-Erling Smørgrav 			sets[i]->rk.dname_len == dup->rk.dname_len &&
393b7579f77SDag-Erling Smørgrav 			query_dname_compare(sets[i]->rk.dname, dup->rk.dname)
394b7579f77SDag-Erling Smørgrav 			== 0)
395b7579f77SDag-Erling Smørgrav 			return 1;
396b7579f77SDag-Erling Smørgrav 	}
397b7579f77SDag-Erling Smørgrav 	return 0;
398b7579f77SDag-Erling Smørgrav }
399b7579f77SDag-Erling Smørgrav 
400b7579f77SDag-Erling Smørgrav /** prepend the prepend list in the answer and authority section of dns_msg */
401b7579f77SDag-Erling Smørgrav static int
402b7579f77SDag-Erling Smørgrav iter_prepend(struct iter_qstate* iq, struct dns_msg* msg,
403b7579f77SDag-Erling Smørgrav 	struct regional* region)
404b7579f77SDag-Erling Smørgrav {
405b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* p;
406b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key** sets;
407b7579f77SDag-Erling Smørgrav 	size_t num_an = 0, num_ns = 0;;
408b7579f77SDag-Erling Smørgrav 	for(p = iq->an_prepend_list; p; p = p->next)
409b7579f77SDag-Erling Smørgrav 		num_an++;
410b7579f77SDag-Erling Smørgrav 	for(p = iq->ns_prepend_list; p; p = p->next)
411b7579f77SDag-Erling Smørgrav 		num_ns++;
412b7579f77SDag-Erling Smørgrav 	if(num_an + num_ns == 0)
413b7579f77SDag-Erling Smørgrav 		return 1;
414b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "prepending %d rrsets", (int)num_an + (int)num_ns);
41509a3aaf3SDag-Erling Smørgrav 	if(num_an > RR_COUNT_MAX || num_ns > RR_COUNT_MAX ||
41609a3aaf3SDag-Erling Smørgrav 		msg->rep->rrset_count > RR_COUNT_MAX) return 0; /* overflow */
417b7579f77SDag-Erling Smørgrav 	sets = regional_alloc(region, (num_an+num_ns+msg->rep->rrset_count) *
418b7579f77SDag-Erling Smørgrav 		sizeof(struct ub_packed_rrset_key*));
419b7579f77SDag-Erling Smørgrav 	if(!sets)
420b7579f77SDag-Erling Smørgrav 		return 0;
421b7579f77SDag-Erling Smørgrav 	/* ANSWER section */
422b7579f77SDag-Erling Smørgrav 	num_an = 0;
423b7579f77SDag-Erling Smørgrav 	for(p = iq->an_prepend_list; p; p = p->next) {
424b7579f77SDag-Erling Smørgrav 		sets[num_an++] = p->rrset;
4250eefd307SCy Schubert 		if(ub_packed_rrset_ttl(p->rrset) < msg->rep->ttl)
4260eefd307SCy Schubert 			msg->rep->ttl = ub_packed_rrset_ttl(p->rrset);
427b7579f77SDag-Erling Smørgrav 	}
428b7579f77SDag-Erling Smørgrav 	memcpy(sets+num_an, msg->rep->rrsets, msg->rep->an_numrrsets *
429b7579f77SDag-Erling Smørgrav 		sizeof(struct ub_packed_rrset_key*));
430b7579f77SDag-Erling Smørgrav 	/* AUTH section */
431b7579f77SDag-Erling Smørgrav 	num_ns = 0;
432b7579f77SDag-Erling Smørgrav 	for(p = iq->ns_prepend_list; p; p = p->next) {
433b7579f77SDag-Erling Smørgrav 		if(prepend_is_duplicate(sets+msg->rep->an_numrrsets+num_an,
434b7579f77SDag-Erling Smørgrav 			num_ns, p->rrset) || prepend_is_duplicate(
435b7579f77SDag-Erling Smørgrav 			msg->rep->rrsets+msg->rep->an_numrrsets,
436b7579f77SDag-Erling Smørgrav 			msg->rep->ns_numrrsets, p->rrset))
437b7579f77SDag-Erling Smørgrav 			continue;
438b7579f77SDag-Erling Smørgrav 		sets[msg->rep->an_numrrsets + num_an + num_ns++] = p->rrset;
4390eefd307SCy Schubert 		if(ub_packed_rrset_ttl(p->rrset) < msg->rep->ttl)
4400eefd307SCy Schubert 			msg->rep->ttl = ub_packed_rrset_ttl(p->rrset);
441b7579f77SDag-Erling Smørgrav 	}
442b7579f77SDag-Erling Smørgrav 	memcpy(sets + num_an + msg->rep->an_numrrsets + num_ns,
443b7579f77SDag-Erling Smørgrav 		msg->rep->rrsets + msg->rep->an_numrrsets,
444b7579f77SDag-Erling Smørgrav 		(msg->rep->ns_numrrsets + msg->rep->ar_numrrsets) *
445b7579f77SDag-Erling Smørgrav 		sizeof(struct ub_packed_rrset_key*));
446b7579f77SDag-Erling Smørgrav 
447b7579f77SDag-Erling Smørgrav 	/* NXDOMAIN rcode can stay if we prepended DNAME/CNAMEs, because
448b7579f77SDag-Erling Smørgrav 	 * this is what recursors should give. */
449b7579f77SDag-Erling Smørgrav 	msg->rep->rrset_count += num_an + num_ns;
450b7579f77SDag-Erling Smørgrav 	msg->rep->an_numrrsets += num_an;
451b7579f77SDag-Erling Smørgrav 	msg->rep->ns_numrrsets += num_ns;
452b7579f77SDag-Erling Smørgrav 	msg->rep->rrsets = sets;
453b7579f77SDag-Erling Smørgrav 	return 1;
454b7579f77SDag-Erling Smørgrav }
455b7579f77SDag-Erling Smørgrav 
456b7579f77SDag-Erling Smørgrav /**
45765b390aaSDag-Erling Smørgrav  * Find rrset in ANSWER prepend list.
45865b390aaSDag-Erling Smørgrav  * to avoid duplicate DNAMEs when a DNAME is traversed twice.
45965b390aaSDag-Erling Smørgrav  * @param iq: iterator query state.
46065b390aaSDag-Erling Smørgrav  * @param rrset: rrset to add.
46165b390aaSDag-Erling Smørgrav  * @return false if not found
46265b390aaSDag-Erling Smørgrav  */
46365b390aaSDag-Erling Smørgrav static int
46465b390aaSDag-Erling Smørgrav iter_find_rrset_in_prepend_answer(struct iter_qstate* iq,
46565b390aaSDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset)
46665b390aaSDag-Erling Smørgrav {
46765b390aaSDag-Erling Smørgrav 	struct iter_prep_list* p = iq->an_prepend_list;
46865b390aaSDag-Erling Smørgrav 	while(p) {
46965b390aaSDag-Erling Smørgrav 		if(ub_rrset_compare(p->rrset, rrset) == 0 &&
47065b390aaSDag-Erling Smørgrav 			rrsetdata_equal((struct packed_rrset_data*)p->rrset
47165b390aaSDag-Erling Smørgrav 			->entry.data, (struct packed_rrset_data*)rrset
47265b390aaSDag-Erling Smørgrav 			->entry.data))
47365b390aaSDag-Erling Smørgrav 			return 1;
47465b390aaSDag-Erling Smørgrav 		p = p->next;
47565b390aaSDag-Erling Smørgrav 	}
47665b390aaSDag-Erling Smørgrav 	return 0;
47765b390aaSDag-Erling Smørgrav }
47865b390aaSDag-Erling Smørgrav 
47965b390aaSDag-Erling Smørgrav /**
480b7579f77SDag-Erling Smørgrav  * Add rrset to ANSWER prepend list
481b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
482b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
483b7579f77SDag-Erling Smørgrav  * @param rrset: rrset to add.
484b7579f77SDag-Erling Smørgrav  * @return false on failure (malloc).
485b7579f77SDag-Erling Smørgrav  */
486b7579f77SDag-Erling Smørgrav static int
487b7579f77SDag-Erling Smørgrav iter_add_prepend_answer(struct module_qstate* qstate, struct iter_qstate* iq,
488b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset)
489b7579f77SDag-Erling Smørgrav {
490b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
491b7579f77SDag-Erling Smørgrav 		qstate->region, sizeof(struct iter_prep_list));
492b7579f77SDag-Erling Smørgrav 	if(!p)
493b7579f77SDag-Erling Smørgrav 		return 0;
494b7579f77SDag-Erling Smørgrav 	p->rrset = rrset;
495b7579f77SDag-Erling Smørgrav 	p->next = NULL;
496b7579f77SDag-Erling Smørgrav 	/* add at end */
497b7579f77SDag-Erling Smørgrav 	if(iq->an_prepend_last)
498b7579f77SDag-Erling Smørgrav 		iq->an_prepend_last->next = p;
499b7579f77SDag-Erling Smørgrav 	else	iq->an_prepend_list = p;
500b7579f77SDag-Erling Smørgrav 	iq->an_prepend_last = p;
501b7579f77SDag-Erling Smørgrav 	return 1;
502b7579f77SDag-Erling Smørgrav }
503b7579f77SDag-Erling Smørgrav 
504b7579f77SDag-Erling Smørgrav /**
505b7579f77SDag-Erling Smørgrav  * Add rrset to AUTHORITY prepend list
506b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
507b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
508b7579f77SDag-Erling Smørgrav  * @param rrset: rrset to add.
509b7579f77SDag-Erling Smørgrav  * @return false on failure (malloc).
510b7579f77SDag-Erling Smørgrav  */
511b7579f77SDag-Erling Smørgrav static int
512b7579f77SDag-Erling Smørgrav iter_add_prepend_auth(struct module_qstate* qstate, struct iter_qstate* iq,
513b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset)
514b7579f77SDag-Erling Smørgrav {
515b7579f77SDag-Erling Smørgrav 	struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
516b7579f77SDag-Erling Smørgrav 		qstate->region, sizeof(struct iter_prep_list));
517b7579f77SDag-Erling Smørgrav 	if(!p)
518b7579f77SDag-Erling Smørgrav 		return 0;
519b7579f77SDag-Erling Smørgrav 	p->rrset = rrset;
520b7579f77SDag-Erling Smørgrav 	p->next = NULL;
521b7579f77SDag-Erling Smørgrav 	/* add at end */
522b7579f77SDag-Erling Smørgrav 	if(iq->ns_prepend_last)
523b7579f77SDag-Erling Smørgrav 		iq->ns_prepend_last->next = p;
524b7579f77SDag-Erling Smørgrav 	else	iq->ns_prepend_list = p;
525b7579f77SDag-Erling Smørgrav 	iq->ns_prepend_last = p;
526b7579f77SDag-Erling Smørgrav 	return 1;
527b7579f77SDag-Erling Smørgrav }
528b7579f77SDag-Erling Smørgrav 
529b7579f77SDag-Erling Smørgrav /**
530b7579f77SDag-Erling Smørgrav  * Given a CNAME response (defined as a response containing a CNAME or DNAME
531b7579f77SDag-Erling Smørgrav  * that does not answer the request), process the response, modifying the
532b7579f77SDag-Erling Smørgrav  * state as necessary. This follows the CNAME/DNAME chain and returns the
533b7579f77SDag-Erling Smørgrav  * final query name.
534b7579f77SDag-Erling Smørgrav  *
535b7579f77SDag-Erling Smørgrav  * sets the new query name, after following the CNAME/DNAME chain.
536b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
537b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
538b7579f77SDag-Erling Smørgrav  * @param msg: the response.
539b7579f77SDag-Erling Smørgrav  * @param mname: returned target new query name.
540b7579f77SDag-Erling Smørgrav  * @param mname_len: length of mname.
541b7579f77SDag-Erling Smørgrav  * @return false on (malloc) error.
542b7579f77SDag-Erling Smørgrav  */
543b7579f77SDag-Erling Smørgrav static int
544b7579f77SDag-Erling Smørgrav handle_cname_response(struct module_qstate* qstate, struct iter_qstate* iq,
545b7579f77SDag-Erling Smørgrav         struct dns_msg* msg, uint8_t** mname, size_t* mname_len)
546b7579f77SDag-Erling Smørgrav {
547b7579f77SDag-Erling Smørgrav 	size_t i;
548b7579f77SDag-Erling Smørgrav 	/* Start with the (current) qname. */
549b7579f77SDag-Erling Smørgrav 	*mname = iq->qchase.qname;
550b7579f77SDag-Erling Smørgrav 	*mname_len = iq->qchase.qname_len;
551b7579f77SDag-Erling Smørgrav 
552b7579f77SDag-Erling Smørgrav 	/* Iterate over the ANSWER rrsets in order, looking for CNAMEs and
553b7579f77SDag-Erling Smørgrav 	 * DNAMES. */
554b7579f77SDag-Erling Smørgrav 	for(i=0; i<msg->rep->an_numrrsets; i++) {
555b7579f77SDag-Erling Smørgrav 		struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
556b7579f77SDag-Erling Smørgrav 		/* If there is a (relevant) DNAME, add it to the list.
557b7579f77SDag-Erling Smørgrav 		 * We always expect there to be CNAME that was generated
558b7579f77SDag-Erling Smørgrav 		 * by this DNAME following, so we don't process the DNAME
559b7579f77SDag-Erling Smørgrav 		 * directly.  */
560b7579f77SDag-Erling Smørgrav 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_DNAME &&
56165b390aaSDag-Erling Smørgrav 			dname_strict_subdomain_c(*mname, r->rk.dname) &&
56265b390aaSDag-Erling Smørgrav 			!iter_find_rrset_in_prepend_answer(iq, r)) {
563b7579f77SDag-Erling Smørgrav 			if(!iter_add_prepend_answer(qstate, iq, r))
564b7579f77SDag-Erling Smørgrav 				return 0;
565b7579f77SDag-Erling Smørgrav 			continue;
566b7579f77SDag-Erling Smørgrav 		}
567b7579f77SDag-Erling Smørgrav 
568b7579f77SDag-Erling Smørgrav 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_CNAME &&
56965b390aaSDag-Erling Smørgrav 			query_dname_compare(*mname, r->rk.dname) == 0 &&
57065b390aaSDag-Erling Smørgrav 			!iter_find_rrset_in_prepend_answer(iq, r)) {
571b7579f77SDag-Erling Smørgrav 			/* Add this relevant CNAME rrset to the prepend list.*/
572b7579f77SDag-Erling Smørgrav 			if(!iter_add_prepend_answer(qstate, iq, r))
573b7579f77SDag-Erling Smørgrav 				return 0;
574b7579f77SDag-Erling Smørgrav 			get_cname_target(r, mname, mname_len);
575b7579f77SDag-Erling Smørgrav 		}
576b7579f77SDag-Erling Smørgrav 
577b7579f77SDag-Erling Smørgrav 		/* Other rrsets in the section are ignored. */
578b7579f77SDag-Erling Smørgrav 	}
579b7579f77SDag-Erling Smørgrav 	/* add authority rrsets to authority prepend, for wildcarded CNAMEs */
580b7579f77SDag-Erling Smørgrav 	for(i=msg->rep->an_numrrsets; i<msg->rep->an_numrrsets +
581b7579f77SDag-Erling Smørgrav 		msg->rep->ns_numrrsets; i++) {
582b7579f77SDag-Erling Smørgrav 		struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
583b7579f77SDag-Erling Smørgrav 		/* only add NSEC/NSEC3, as they may be needed for validation */
584b7579f77SDag-Erling Smørgrav 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC ||
585b7579f77SDag-Erling Smørgrav 			ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC3) {
586b7579f77SDag-Erling Smørgrav 			if(!iter_add_prepend_auth(qstate, iq, r))
587b7579f77SDag-Erling Smørgrav 				return 0;
588b7579f77SDag-Erling Smørgrav 		}
589b7579f77SDag-Erling Smørgrav 	}
590b7579f77SDag-Erling Smørgrav 	return 1;
591b7579f77SDag-Erling Smørgrav }
592b7579f77SDag-Erling Smørgrav 
5935469a995SCy Schubert /** add response specific error information for log servfail */
5945469a995SCy Schubert static void
5955469a995SCy Schubert errinf_reply(struct module_qstate* qstate, struct iter_qstate* iq)
5965469a995SCy Schubert {
5975469a995SCy Schubert 	if(qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail)
5985469a995SCy Schubert 		return;
599*865f46b2SCy Schubert 	if((qstate->reply && qstate->reply->remote_addrlen != 0) ||
600*865f46b2SCy Schubert 		(iq->fail_reply && iq->fail_reply->remote_addrlen != 0)) {
6015469a995SCy Schubert 		char from[256], frm[512];
602*865f46b2SCy Schubert 		if(qstate->reply && qstate->reply->remote_addrlen != 0)
603*865f46b2SCy Schubert 			addr_to_str(&qstate->reply->remote_addr,
604*865f46b2SCy Schubert 				qstate->reply->remote_addrlen, from,
605*865f46b2SCy Schubert 				sizeof(from));
6065469a995SCy Schubert 		else
607*865f46b2SCy Schubert 			addr_to_str(&iq->fail_reply->remote_addr,
608*865f46b2SCy Schubert 				iq->fail_reply->remote_addrlen, from,
609*865f46b2SCy Schubert 				sizeof(from));
6105469a995SCy Schubert 		snprintf(frm, sizeof(frm), "from %s", from);
6115469a995SCy Schubert 		errinf(qstate, frm);
6125469a995SCy Schubert 	}
6135469a995SCy Schubert 	if(iq->scrub_failures || iq->parse_failures) {
6145469a995SCy Schubert 		if(iq->scrub_failures)
6155469a995SCy Schubert 			errinf(qstate, "upstream response failed scrub");
6165469a995SCy Schubert 		if(iq->parse_failures)
6175469a995SCy Schubert 			errinf(qstate, "could not parse upstream response");
6185469a995SCy Schubert 	} else if(iq->response == NULL && iq->timeout_count != 0) {
6195469a995SCy Schubert 		errinf(qstate, "upstream server timeout");
6205469a995SCy Schubert 	} else if(iq->response == NULL) {
6215469a995SCy Schubert 		errinf(qstate, "no server to query");
6225469a995SCy Schubert 		if(iq->dp) {
6235469a995SCy Schubert 			if(iq->dp->target_list == NULL)
6245469a995SCy Schubert 				errinf(qstate, "no addresses for nameservers");
6255469a995SCy Schubert 			else	errinf(qstate, "nameserver addresses not usable");
6265469a995SCy Schubert 			if(iq->dp->nslist == NULL)
6275469a995SCy Schubert 				errinf(qstate, "have no nameserver names");
6285469a995SCy Schubert 			if(iq->dp->bogus)
6295469a995SCy Schubert 				errinf(qstate, "NS record was dnssec bogus");
6305469a995SCy Schubert 		}
6315469a995SCy Schubert 	}
6325469a995SCy Schubert 	if(iq->response && iq->response->rep) {
6335469a995SCy Schubert 		if(FLAGS_GET_RCODE(iq->response->rep->flags) != 0) {
6345469a995SCy Schubert 			char rcode[256], rc[32];
6355469a995SCy Schubert 			(void)sldns_wire2str_rcode_buf(
6365469a995SCy Schubert 				FLAGS_GET_RCODE(iq->response->rep->flags),
6375469a995SCy Schubert 				rc, sizeof(rc));
6385469a995SCy Schubert 			snprintf(rcode, sizeof(rcode), "got %s", rc);
6395469a995SCy Schubert 			errinf(qstate, rcode);
6405469a995SCy Schubert 		} else {
6415469a995SCy Schubert 			/* rcode NOERROR */
6425469a995SCy Schubert 			if(iq->response->rep->an_numrrsets == 0) {
6435469a995SCy Schubert 				errinf(qstate, "nodata answer");
6445469a995SCy Schubert 			}
6455469a995SCy Schubert 		}
6465469a995SCy Schubert 	}
6475469a995SCy Schubert }
6485469a995SCy Schubert 
649c7f4d7adSDag-Erling Smørgrav /** see if last resort is possible - does config allow queries to parent */
650c7f4d7adSDag-Erling Smørgrav static int
651c7f4d7adSDag-Erling Smørgrav can_have_last_resort(struct module_env* env, uint8_t* nm, size_t nmlen,
6527da0adf7SDag-Erling Smørgrav 	uint16_t qclass, struct delegpt** retdp)
653c7f4d7adSDag-Erling Smørgrav {
654c7f4d7adSDag-Erling Smørgrav 	struct delegpt* fwddp;
655c7f4d7adSDag-Erling Smørgrav 	struct iter_hints_stub* stub;
656c7f4d7adSDag-Erling Smørgrav 	int labs = dname_count_labels(nm);
657c7f4d7adSDag-Erling Smørgrav 	/* do not process a last resort (the parent side) if a stub
658c7f4d7adSDag-Erling Smørgrav 	 * or forward is configured, because we do not want to go 'above'
659c7f4d7adSDag-Erling Smørgrav 	 * the configured servers */
660c7f4d7adSDag-Erling Smørgrav 	if(!dname_is_root(nm) && (stub = (struct iter_hints_stub*)
661c7f4d7adSDag-Erling Smørgrav 		name_tree_find(&env->hints->tree, nm, nmlen, labs, qclass)) &&
662c7f4d7adSDag-Erling Smørgrav 		/* has_parent side is turned off for stub_first, where we
663c7f4d7adSDag-Erling Smørgrav 		 * are allowed to go to the parent */
664c7f4d7adSDag-Erling Smørgrav 		stub->dp->has_parent_side_NS) {
6657da0adf7SDag-Erling Smørgrav 		if(retdp) *retdp = stub->dp;
666c7f4d7adSDag-Erling Smørgrav 		return 0;
667c7f4d7adSDag-Erling Smørgrav 	}
668c7f4d7adSDag-Erling Smørgrav 	if((fwddp = forwards_find(env->fwds, nm, qclass)) &&
669c7f4d7adSDag-Erling Smørgrav 		/* has_parent_side is turned off for forward_first, where
670c7f4d7adSDag-Erling Smørgrav 		 * we are allowed to go to the parent */
671c7f4d7adSDag-Erling Smørgrav 		fwddp->has_parent_side_NS) {
6727da0adf7SDag-Erling Smørgrav 		if(retdp) *retdp = fwddp;
673c7f4d7adSDag-Erling Smørgrav 		return 0;
674c7f4d7adSDag-Erling Smørgrav 	}
675c7f4d7adSDag-Erling Smørgrav 	return 1;
676c7f4d7adSDag-Erling Smørgrav }
677c7f4d7adSDag-Erling Smørgrav 
67809a3aaf3SDag-Erling Smørgrav /** see if target name is caps-for-id whitelisted */
67909a3aaf3SDag-Erling Smørgrav static int
68009a3aaf3SDag-Erling Smørgrav is_caps_whitelisted(struct iter_env* ie, struct iter_qstate* iq)
68109a3aaf3SDag-Erling Smørgrav {
68209a3aaf3SDag-Erling Smørgrav 	if(!ie->caps_white) return 0; /* no whitelist, or no capsforid */
68309a3aaf3SDag-Erling Smørgrav 	return name_tree_lookup(ie->caps_white, iq->qchase.qname,
68409a3aaf3SDag-Erling Smørgrav 		iq->qchase.qname_len, dname_count_labels(iq->qchase.qname),
68509a3aaf3SDag-Erling Smørgrav 		iq->qchase.qclass) != NULL;
68609a3aaf3SDag-Erling Smørgrav }
68709a3aaf3SDag-Erling Smørgrav 
6880a92a9fcSCy Schubert /**
6890a92a9fcSCy Schubert  * Create target count structure for this query. This is always explicitly
6900a92a9fcSCy Schubert  * created for the parent query.
6910a92a9fcSCy Schubert  */
69252df462fSXin LI static void
69352df462fSXin LI target_count_create(struct iter_qstate* iq)
69452df462fSXin LI {
69552df462fSXin LI 	if(!iq->target_count) {
6960a92a9fcSCy Schubert 		iq->target_count = (int*)calloc(TARGET_COUNT_MAX, sizeof(int));
69752df462fSXin LI 		/* if calloc fails we simply do not track this number */
6980a92a9fcSCy Schubert 		if(iq->target_count) {
6990a92a9fcSCy Schubert 			iq->target_count[TARGET_COUNT_REF] = 1;
7000a92a9fcSCy Schubert 			iq->nxns_dp = (uint8_t**)calloc(1, sizeof(uint8_t*));
7010a92a9fcSCy Schubert 		}
70252df462fSXin LI 	}
70352df462fSXin LI }
70452df462fSXin LI 
70552df462fSXin LI static void
70652df462fSXin LI target_count_increase(struct iter_qstate* iq, int num)
70752df462fSXin LI {
70852df462fSXin LI 	target_count_create(iq);
70952df462fSXin LI 	if(iq->target_count)
7100a92a9fcSCy Schubert 		iq->target_count[TARGET_COUNT_QUERIES] += num;
711091e9e46SCy Schubert 	iq->dp_target_count++;
712091e9e46SCy Schubert }
713091e9e46SCy Schubert 
714091e9e46SCy Schubert static void
715091e9e46SCy Schubert target_count_increase_nx(struct iter_qstate* iq, int num)
716091e9e46SCy Schubert {
717091e9e46SCy Schubert 	target_count_create(iq);
718091e9e46SCy Schubert 	if(iq->target_count)
7190a92a9fcSCy Schubert 		iq->target_count[TARGET_COUNT_NX] += num;
72052df462fSXin LI }
72152df462fSXin LI 
722b7579f77SDag-Erling Smørgrav /**
723b7579f77SDag-Erling Smørgrav  * Generate a subrequest.
724b7579f77SDag-Erling Smørgrav  * Generate a local request event. Local events are tied to this module, and
72505ab2901SDag-Erling Smørgrav  * have a corresponding (first tier) event that is waiting for this event to
726b7579f77SDag-Erling Smørgrav  * resolve to continue.
727b7579f77SDag-Erling Smørgrav  *
728b7579f77SDag-Erling Smørgrav  * @param qname The query name for this request.
729b7579f77SDag-Erling Smørgrav  * @param qnamelen length of qname
730b7579f77SDag-Erling Smørgrav  * @param qtype The query type for this request.
731b7579f77SDag-Erling Smørgrav  * @param qclass The query class for this request.
732b7579f77SDag-Erling Smørgrav  * @param qstate The event that is generating this event.
733b7579f77SDag-Erling Smørgrav  * @param id: module id.
734b7579f77SDag-Erling Smørgrav  * @param iq: The iterator state that is generating this event.
735b7579f77SDag-Erling Smørgrav  * @param initial_state The initial response state (normally this
736b7579f77SDag-Erling Smørgrav  *          is QUERY_RESP_STATE, unless it is known that the request won't
737b7579f77SDag-Erling Smørgrav  *          need iterative processing
738b7579f77SDag-Erling Smørgrav  * @param finalstate The final state for the response to this request.
739b7579f77SDag-Erling Smørgrav  * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
740b7579f77SDag-Erling Smørgrav  * 	not need initialisation.
741b7579f77SDag-Erling Smørgrav  * @param v: if true, validation is done on the subquery.
742091e9e46SCy Schubert  * @param detached: true if this qstate should not attach to the subquery
743b7579f77SDag-Erling Smørgrav  * @return false on error (malloc).
744b7579f77SDag-Erling Smørgrav  */
745b7579f77SDag-Erling Smørgrav static int
746b7579f77SDag-Erling Smørgrav generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
747b7579f77SDag-Erling Smørgrav 	uint16_t qclass, struct module_qstate* qstate, int id,
748b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq, enum iter_state initial_state,
749091e9e46SCy Schubert 	enum iter_state finalstate, struct module_qstate** subq_ret, int v,
750091e9e46SCy Schubert 	int detached)
751b7579f77SDag-Erling Smørgrav {
752b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq = NULL;
753b7579f77SDag-Erling Smørgrav 	struct iter_qstate* subiq = NULL;
754b7579f77SDag-Erling Smørgrav 	uint16_t qflags = 0; /* OPCODE QUERY, no flags */
755b7579f77SDag-Erling Smørgrav 	struct query_info qinf;
756b7579f77SDag-Erling Smørgrav 	int prime = (finalstate == PRIME_RESP_STATE)?1:0;
757ff825849SDag-Erling Smørgrav 	int valrec = 0;
758b7579f77SDag-Erling Smørgrav 	qinf.qname = qname;
759b7579f77SDag-Erling Smørgrav 	qinf.qname_len = qnamelen;
760b7579f77SDag-Erling Smørgrav 	qinf.qtype = qtype;
761b7579f77SDag-Erling Smørgrav 	qinf.qclass = qclass;
762bc892140SDag-Erling Smørgrav 	qinf.local_alias = NULL;
763b7579f77SDag-Erling Smørgrav 
764b7579f77SDag-Erling Smørgrav 	/* RD should be set only when sending the query back through the INIT
765b7579f77SDag-Erling Smørgrav 	 * state. */
766b7579f77SDag-Erling Smørgrav 	if(initial_state == INIT_REQUEST_STATE)
767b7579f77SDag-Erling Smørgrav 		qflags |= BIT_RD;
768b7579f77SDag-Erling Smørgrav 	/* We set the CD flag so we can send this through the "head" of
769b7579f77SDag-Erling Smørgrav 	 * the resolution chain, which might have a validator. We are
770b7579f77SDag-Erling Smørgrav 	 * uninterested in validating things not on the direct resolution
771b7579f77SDag-Erling Smørgrav 	 * path.  */
772ff825849SDag-Erling Smørgrav 	if(!v) {
773b7579f77SDag-Erling Smørgrav 		qflags |= BIT_CD;
774ff825849SDag-Erling Smørgrav 		valrec = 1;
775ff825849SDag-Erling Smørgrav 	}
776b7579f77SDag-Erling Smørgrav 
777091e9e46SCy Schubert 	if(detached) {
778091e9e46SCy Schubert 		struct mesh_state* sub = NULL;
779091e9e46SCy Schubert 		fptr_ok(fptr_whitelist_modenv_add_sub(
780091e9e46SCy Schubert 			qstate->env->add_sub));
781091e9e46SCy Schubert 		if(!(*qstate->env->add_sub)(qstate, &qinf,
782091e9e46SCy Schubert 			qflags, prime, valrec, &subq, &sub)){
783b7579f77SDag-Erling Smørgrav 			return 0;
784b7579f77SDag-Erling Smørgrav 		}
785091e9e46SCy Schubert 	}
786091e9e46SCy Schubert 	else {
787091e9e46SCy Schubert 		/* attach subquery, lookup existing or make a new one */
788091e9e46SCy Schubert 		fptr_ok(fptr_whitelist_modenv_attach_sub(
789091e9e46SCy Schubert 			qstate->env->attach_sub));
790091e9e46SCy Schubert 		if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime,
791091e9e46SCy Schubert 			valrec, &subq)) {
792091e9e46SCy Schubert 			return 0;
793091e9e46SCy Schubert 		}
794091e9e46SCy Schubert 	}
795b7579f77SDag-Erling Smørgrav 	*subq_ret = subq;
796b7579f77SDag-Erling Smørgrav 	if(subq) {
797b7579f77SDag-Erling Smørgrav 		/* initialise the new subquery */
798b7579f77SDag-Erling Smørgrav 		subq->curmod = id;
799b7579f77SDag-Erling Smørgrav 		subq->ext_state[id] = module_state_initial;
800b7579f77SDag-Erling Smørgrav 		subq->minfo[id] = regional_alloc(subq->region,
801b7579f77SDag-Erling Smørgrav 			sizeof(struct iter_qstate));
802b7579f77SDag-Erling Smørgrav 		if(!subq->minfo[id]) {
803b7579f77SDag-Erling Smørgrav 			log_err("init subq: out of memory");
804b7579f77SDag-Erling Smørgrav 			fptr_ok(fptr_whitelist_modenv_kill_sub(
805b7579f77SDag-Erling Smørgrav 				qstate->env->kill_sub));
806b7579f77SDag-Erling Smørgrav 			(*qstate->env->kill_sub)(subq);
807b7579f77SDag-Erling Smørgrav 			return 0;
808b7579f77SDag-Erling Smørgrav 		}
809b7579f77SDag-Erling Smørgrav 		subiq = (struct iter_qstate*)subq->minfo[id];
810b7579f77SDag-Erling Smørgrav 		memset(subiq, 0, sizeof(*subiq));
811b7579f77SDag-Erling Smørgrav 		subiq->num_target_queries = 0;
81252df462fSXin LI 		target_count_create(iq);
81352df462fSXin LI 		subiq->target_count = iq->target_count;
8140a92a9fcSCy Schubert 		if(iq->target_count) {
8150a92a9fcSCy Schubert 			iq->target_count[TARGET_COUNT_REF] ++; /* extra reference */
8160a92a9fcSCy Schubert 			subiq->nxns_dp = iq->nxns_dp;
8170a92a9fcSCy Schubert 		}
818091e9e46SCy Schubert 		subiq->dp_target_count = 0;
819b7579f77SDag-Erling Smørgrav 		subiq->num_current_queries = 0;
820b7579f77SDag-Erling Smørgrav 		subiq->depth = iq->depth+1;
821b7579f77SDag-Erling Smørgrav 		outbound_list_init(&subiq->outlist);
822b7579f77SDag-Erling Smørgrav 		subiq->state = initial_state;
823b7579f77SDag-Erling Smørgrav 		subiq->final_state = finalstate;
824b7579f77SDag-Erling Smørgrav 		subiq->qchase = subq->qinfo;
825b7579f77SDag-Erling Smørgrav 		subiq->chase_flags = subq->query_flags;
826b7579f77SDag-Erling Smørgrav 		subiq->refetch_glue = 0;
82705ab2901SDag-Erling Smørgrav 		if(qstate->env->cfg->qname_minimisation)
82805ab2901SDag-Erling Smørgrav 			subiq->minimisation_state = INIT_MINIMISE_STATE;
82905ab2901SDag-Erling Smørgrav 		else
83005ab2901SDag-Erling Smørgrav 			subiq->minimisation_state = DONOT_MINIMISE_STATE;
83105ab2901SDag-Erling Smørgrav 		memset(&subiq->qinfo_out, 0, sizeof(struct query_info));
832b7579f77SDag-Erling Smørgrav 	}
833b7579f77SDag-Erling Smørgrav 	return 1;
834b7579f77SDag-Erling Smørgrav }
835b7579f77SDag-Erling Smørgrav 
836b7579f77SDag-Erling Smørgrav /**
837b7579f77SDag-Erling Smørgrav  * Generate and send a root priming request.
838b7579f77SDag-Erling Smørgrav  * @param qstate: the qtstate that triggered the need to prime.
839b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
840b7579f77SDag-Erling Smørgrav  * @param id: module id.
841b7579f77SDag-Erling Smørgrav  * @param qclass: the class to prime.
842b7579f77SDag-Erling Smørgrav  * @return 0 on failure
843b7579f77SDag-Erling Smørgrav  */
844b7579f77SDag-Erling Smørgrav static int
845b7579f77SDag-Erling Smørgrav prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
846b7579f77SDag-Erling Smørgrav 	uint16_t qclass)
847b7579f77SDag-Erling Smørgrav {
848b7579f77SDag-Erling Smørgrav 	struct delegpt* dp;
849b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
850b7579f77SDag-Erling Smørgrav 	verbose(VERB_DETAIL, "priming . %s NS",
85117d15b25SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rr_classes, (int)qclass)?
85217d15b25SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rr_classes, (int)qclass)->name:"??");
853b7579f77SDag-Erling Smørgrav 	dp = hints_lookup_root(qstate->env->hints, qclass);
854b7579f77SDag-Erling Smørgrav 	if(!dp) {
855b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "Cannot prime due to lack of hints");
856b7579f77SDag-Erling Smørgrav 		return 0;
857b7579f77SDag-Erling Smørgrav 	}
858b7579f77SDag-Erling Smørgrav 	/* Priming requests start at the QUERYTARGETS state, skipping
859b7579f77SDag-Erling Smørgrav 	 * the normal INIT state logic (which would cause an infloop). */
860b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS,
861b7579f77SDag-Erling Smørgrav 		qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
862091e9e46SCy Schubert 		&subq, 0, 0)) {
863b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "could not prime root");
864b7579f77SDag-Erling Smørgrav 		return 0;
865b7579f77SDag-Erling Smørgrav 	}
866b7579f77SDag-Erling Smørgrav 	if(subq) {
867b7579f77SDag-Erling Smørgrav 		struct iter_qstate* subiq =
868b7579f77SDag-Erling Smørgrav 			(struct iter_qstate*)subq->minfo[id];
869b7579f77SDag-Erling Smørgrav 		/* Set the initial delegation point to the hint.
870b7579f77SDag-Erling Smørgrav 		 * copy dp, it is now part of the root prime query.
871b7579f77SDag-Erling Smørgrav 		 * dp was part of in the fixed hints structure. */
872b7579f77SDag-Erling Smørgrav 		subiq->dp = delegpt_copy(dp, subq->region);
873b7579f77SDag-Erling Smørgrav 		if(!subiq->dp) {
874b7579f77SDag-Erling Smørgrav 			log_err("out of memory priming root, copydp");
875b7579f77SDag-Erling Smørgrav 			fptr_ok(fptr_whitelist_modenv_kill_sub(
876b7579f77SDag-Erling Smørgrav 				qstate->env->kill_sub));
877b7579f77SDag-Erling Smørgrav 			(*qstate->env->kill_sub)(subq);
878b7579f77SDag-Erling Smørgrav 			return 0;
879b7579f77SDag-Erling Smørgrav 		}
880b7579f77SDag-Erling Smørgrav 		/* there should not be any target queries. */
881b7579f77SDag-Erling Smørgrav 		subiq->num_target_queries = 0;
882b7579f77SDag-Erling Smørgrav 		subiq->dnssec_expected = iter_indicates_dnssec(
883b7579f77SDag-Erling Smørgrav 			qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
884b7579f77SDag-Erling Smørgrav 	}
885b7579f77SDag-Erling Smørgrav 
886b7579f77SDag-Erling Smørgrav 	/* this module stops, our submodule starts, and does the query. */
887b7579f77SDag-Erling Smørgrav 	qstate->ext_state[id] = module_wait_subquery;
888b7579f77SDag-Erling Smørgrav 	return 1;
889b7579f77SDag-Erling Smørgrav }
890b7579f77SDag-Erling Smørgrav 
891b7579f77SDag-Erling Smørgrav /**
892b7579f77SDag-Erling Smørgrav  * Generate and process a stub priming request. This method tests for the
893b7579f77SDag-Erling Smørgrav  * need to prime a stub zone, so it is safe to call for every request.
894b7579f77SDag-Erling Smørgrav  *
895b7579f77SDag-Erling Smørgrav  * @param qstate: the qtstate that triggered the need to prime.
896b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
897b7579f77SDag-Erling Smørgrav  * @param id: module id.
898b7579f77SDag-Erling Smørgrav  * @param qname: request name.
899b7579f77SDag-Erling Smørgrav  * @param qclass: request class.
900b7579f77SDag-Erling Smørgrav  * @return true if a priming subrequest was made, false if not. The will only
901b7579f77SDag-Erling Smørgrav  *         issue a priming request if it detects an unprimed stub.
902b7579f77SDag-Erling Smørgrav  *         Uses value of 2 to signal during stub-prime in root-prime situation
903b7579f77SDag-Erling Smørgrav  *         that a noprime-stub is available and resolution can continue.
904b7579f77SDag-Erling Smørgrav  */
905b7579f77SDag-Erling Smørgrav static int
906b7579f77SDag-Erling Smørgrav prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
907b7579f77SDag-Erling Smørgrav 	uint8_t* qname, uint16_t qclass)
908b7579f77SDag-Erling Smørgrav {
909b7579f77SDag-Erling Smørgrav 	/* Lookup the stub hint. This will return null if the stub doesn't
910b7579f77SDag-Erling Smørgrav 	 * need to be re-primed. */
911b7579f77SDag-Erling Smørgrav 	struct iter_hints_stub* stub;
912b7579f77SDag-Erling Smørgrav 	struct delegpt* stub_dp;
913b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
914b7579f77SDag-Erling Smørgrav 
915b7579f77SDag-Erling Smørgrav 	if(!qname) return 0;
916b7579f77SDag-Erling Smørgrav 	stub = hints_lookup_stub(qstate->env->hints, qname, qclass, iq->dp);
917b7579f77SDag-Erling Smørgrav 	/* The stub (if there is one) does not need priming. */
918b7579f77SDag-Erling Smørgrav 	if(!stub)
919b7579f77SDag-Erling Smørgrav 		return 0;
920b7579f77SDag-Erling Smørgrav 	stub_dp = stub->dp;
92157bddd21SDag-Erling Smørgrav 	/* if we have an auth_zone dp, and stub is equal, don't prime stub
92257bddd21SDag-Erling Smørgrav 	 * yet, unless we want to fallback and avoid the auth_zone */
92357bddd21SDag-Erling Smørgrav 	if(!iq->auth_zone_avoid && iq->dp && iq->dp->auth_dp &&
92457bddd21SDag-Erling Smørgrav 		query_dname_compare(iq->dp->name, stub_dp->name) == 0)
92557bddd21SDag-Erling Smørgrav 		return 0;
926b7579f77SDag-Erling Smørgrav 
927b7579f77SDag-Erling Smørgrav 	/* is it a noprime stub (always use) */
928b7579f77SDag-Erling Smørgrav 	if(stub->noprime) {
929b7579f77SDag-Erling Smørgrav 		int r = 0;
930b7579f77SDag-Erling Smørgrav 		if(iq->dp == NULL) r = 2;
931b7579f77SDag-Erling Smørgrav 		/* copy the dp out of the fixed hints structure, so that
932b7579f77SDag-Erling Smørgrav 		 * it can be changed when servicing this query */
933b7579f77SDag-Erling Smørgrav 		iq->dp = delegpt_copy(stub_dp, qstate->region);
934b7579f77SDag-Erling Smørgrav 		if(!iq->dp) {
935b7579f77SDag-Erling Smørgrav 			log_err("out of memory priming stub");
9364c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, priming stub");
937b7579f77SDag-Erling Smørgrav 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
938b7579f77SDag-Erling Smørgrav 			return 1; /* return 1 to make module stop, with error */
939b7579f77SDag-Erling Smørgrav 		}
940b7579f77SDag-Erling Smørgrav 		log_nametypeclass(VERB_DETAIL, "use stub", stub_dp->name,
941b7579f77SDag-Erling Smørgrav 			LDNS_RR_TYPE_NS, qclass);
942b7579f77SDag-Erling Smørgrav 		return r;
943b7579f77SDag-Erling Smørgrav 	}
944b7579f77SDag-Erling Smørgrav 
945b7579f77SDag-Erling Smørgrav 	/* Otherwise, we need to (re)prime the stub. */
946b7579f77SDag-Erling Smørgrav 	log_nametypeclass(VERB_DETAIL, "priming stub", stub_dp->name,
947b7579f77SDag-Erling Smørgrav 		LDNS_RR_TYPE_NS, qclass);
948b7579f77SDag-Erling Smørgrav 
949b7579f77SDag-Erling Smørgrav 	/* Stub priming events start at the QUERYTARGETS state to avoid the
950b7579f77SDag-Erling Smørgrav 	 * redundant INIT state processing. */
951b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request(stub_dp->name, stub_dp->namelen,
952b7579f77SDag-Erling Smørgrav 		LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
953091e9e46SCy Schubert 		QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0, 0)) {
954b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "could not prime stub");
9554c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "could not generate lookup for stub prime");
956b7579f77SDag-Erling Smørgrav 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
957b7579f77SDag-Erling Smørgrav 		return 1; /* return 1 to make module stop, with error */
958b7579f77SDag-Erling Smørgrav 	}
959b7579f77SDag-Erling Smørgrav 	if(subq) {
960b7579f77SDag-Erling Smørgrav 		struct iter_qstate* subiq =
961b7579f77SDag-Erling Smørgrav 			(struct iter_qstate*)subq->minfo[id];
962b7579f77SDag-Erling Smørgrav 
963b7579f77SDag-Erling Smørgrav 		/* Set the initial delegation point to the hint. */
964b7579f77SDag-Erling Smørgrav 		/* make copy to avoid use of stub dp by different qs/threads */
965b7579f77SDag-Erling Smørgrav 		subiq->dp = delegpt_copy(stub_dp, subq->region);
966b7579f77SDag-Erling Smørgrav 		if(!subiq->dp) {
967b7579f77SDag-Erling Smørgrav 			log_err("out of memory priming stub, copydp");
968b7579f77SDag-Erling Smørgrav 			fptr_ok(fptr_whitelist_modenv_kill_sub(
969b7579f77SDag-Erling Smørgrav 				qstate->env->kill_sub));
970b7579f77SDag-Erling Smørgrav 			(*qstate->env->kill_sub)(subq);
9714c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, in stub prime");
972b7579f77SDag-Erling Smørgrav 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
973b7579f77SDag-Erling Smørgrav 			return 1; /* return 1 to make module stop, with error */
974b7579f77SDag-Erling Smørgrav 		}
975b7579f77SDag-Erling Smørgrav 		/* there should not be any target queries -- although there
976b7579f77SDag-Erling Smørgrav 		 * wouldn't be anyway, since stub hints never have
977b7579f77SDag-Erling Smørgrav 		 * missing targets. */
978b7579f77SDag-Erling Smørgrav 		subiq->num_target_queries = 0;
979b7579f77SDag-Erling Smørgrav 		subiq->wait_priming_stub = 1;
980b7579f77SDag-Erling Smørgrav 		subiq->dnssec_expected = iter_indicates_dnssec(
981b7579f77SDag-Erling Smørgrav 			qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
982b7579f77SDag-Erling Smørgrav 	}
983b7579f77SDag-Erling Smørgrav 
984b7579f77SDag-Erling Smørgrav 	/* this module stops, our submodule starts, and does the query. */
985b7579f77SDag-Erling Smørgrav 	qstate->ext_state[id] = module_wait_subquery;
986b7579f77SDag-Erling Smørgrav 	return 1;
987b7579f77SDag-Erling Smørgrav }
988b7579f77SDag-Erling Smørgrav 
989b7579f77SDag-Erling Smørgrav /**
99057bddd21SDag-Erling Smørgrav  * Generate a delegation point for an auth zone (unless cached dp is better)
99157bddd21SDag-Erling Smørgrav  * false on alloc failure.
99257bddd21SDag-Erling Smørgrav  */
99357bddd21SDag-Erling Smørgrav static int
99457bddd21SDag-Erling Smørgrav auth_zone_delegpt(struct module_qstate* qstate, struct iter_qstate* iq,
99557bddd21SDag-Erling Smørgrav 	uint8_t* delname, size_t delnamelen)
99657bddd21SDag-Erling Smørgrav {
99757bddd21SDag-Erling Smørgrav 	struct auth_zone* z;
99857bddd21SDag-Erling Smørgrav 	if(iq->auth_zone_avoid)
99957bddd21SDag-Erling Smørgrav 		return 1;
100057bddd21SDag-Erling Smørgrav 	if(!delname) {
100157bddd21SDag-Erling Smørgrav 		delname = iq->qchase.qname;
100257bddd21SDag-Erling Smørgrav 		delnamelen = iq->qchase.qname_len;
100357bddd21SDag-Erling Smørgrav 	}
100457bddd21SDag-Erling Smørgrav 	lock_rw_rdlock(&qstate->env->auth_zones->lock);
100557bddd21SDag-Erling Smørgrav 	z = auth_zones_find_zone(qstate->env->auth_zones, delname, delnamelen,
100657bddd21SDag-Erling Smørgrav 		qstate->qinfo.qclass);
100757bddd21SDag-Erling Smørgrav 	if(!z) {
100857bddd21SDag-Erling Smørgrav 		lock_rw_unlock(&qstate->env->auth_zones->lock);
100957bddd21SDag-Erling Smørgrav 		return 1;
101057bddd21SDag-Erling Smørgrav 	}
101157bddd21SDag-Erling Smørgrav 	lock_rw_rdlock(&z->lock);
101257bddd21SDag-Erling Smørgrav 	lock_rw_unlock(&qstate->env->auth_zones->lock);
101357bddd21SDag-Erling Smørgrav 	if(z->for_upstream) {
101457bddd21SDag-Erling Smørgrav 		if(iq->dp && query_dname_compare(z->name, iq->dp->name) == 0
101557bddd21SDag-Erling Smørgrav 			&& iq->dp->auth_dp && qstate->blacklist &&
101657bddd21SDag-Erling Smørgrav 			z->fallback_enabled) {
101757bddd21SDag-Erling Smørgrav 			/* cache is blacklisted and fallback, and we
101857bddd21SDag-Erling Smørgrav 			 * already have an auth_zone dp */
101957bddd21SDag-Erling Smørgrav 			if(verbosity>=VERB_ALGO) {
102057bddd21SDag-Erling Smørgrav 				char buf[255+1];
102157bddd21SDag-Erling Smørgrav 				dname_str(z->name, buf);
102257bddd21SDag-Erling Smørgrav 				verbose(VERB_ALGO, "auth_zone %s "
102357bddd21SDag-Erling Smørgrav 				  "fallback because cache blacklisted",
102457bddd21SDag-Erling Smørgrav 				  buf);
102557bddd21SDag-Erling Smørgrav 			}
102657bddd21SDag-Erling Smørgrav 			lock_rw_unlock(&z->lock);
102757bddd21SDag-Erling Smørgrav 			iq->dp = NULL;
102857bddd21SDag-Erling Smørgrav 			return 1;
102957bddd21SDag-Erling Smørgrav 		}
103057bddd21SDag-Erling Smørgrav 		if(iq->dp==NULL || dname_subdomain_c(z->name, iq->dp->name)) {
103157bddd21SDag-Erling Smørgrav 			struct delegpt* dp;
103257bddd21SDag-Erling Smørgrav 			if(qstate->blacklist && z->fallback_enabled) {
103357bddd21SDag-Erling Smørgrav 				/* cache is blacklisted because of a DNSSEC
103457bddd21SDag-Erling Smørgrav 				 * validation failure, and the zone allows
103557bddd21SDag-Erling Smørgrav 				 * fallback to the internet, query there. */
103657bddd21SDag-Erling Smørgrav 				if(verbosity>=VERB_ALGO) {
103757bddd21SDag-Erling Smørgrav 					char buf[255+1];
103857bddd21SDag-Erling Smørgrav 					dname_str(z->name, buf);
103957bddd21SDag-Erling Smørgrav 					verbose(VERB_ALGO, "auth_zone %s "
104057bddd21SDag-Erling Smørgrav 					  "fallback because cache blacklisted",
104157bddd21SDag-Erling Smørgrav 					  buf);
104257bddd21SDag-Erling Smørgrav 				}
104357bddd21SDag-Erling Smørgrav 				lock_rw_unlock(&z->lock);
104457bddd21SDag-Erling Smørgrav 				return 1;
104557bddd21SDag-Erling Smørgrav 			}
104657bddd21SDag-Erling Smørgrav 			dp = (struct delegpt*)regional_alloc_zero(
104757bddd21SDag-Erling Smørgrav 				qstate->region, sizeof(*dp));
104857bddd21SDag-Erling Smørgrav 			if(!dp) {
104957bddd21SDag-Erling Smørgrav 				log_err("alloc failure");
105057bddd21SDag-Erling Smørgrav 				if(z->fallback_enabled) {
105157bddd21SDag-Erling Smørgrav 					lock_rw_unlock(&z->lock);
105257bddd21SDag-Erling Smørgrav 					return 1; /* just fallback */
105357bddd21SDag-Erling Smørgrav 				}
105457bddd21SDag-Erling Smørgrav 				lock_rw_unlock(&z->lock);
10554c75e3aaSDag-Erling Smørgrav 				errinf(qstate, "malloc failure");
105657bddd21SDag-Erling Smørgrav 				return 0;
105757bddd21SDag-Erling Smørgrav 			}
105857bddd21SDag-Erling Smørgrav 			dp->name = regional_alloc_init(qstate->region,
105957bddd21SDag-Erling Smørgrav 				z->name, z->namelen);
106057bddd21SDag-Erling Smørgrav 			if(!dp->name) {
106157bddd21SDag-Erling Smørgrav 				log_err("alloc failure");
106257bddd21SDag-Erling Smørgrav 				if(z->fallback_enabled) {
106357bddd21SDag-Erling Smørgrav 					lock_rw_unlock(&z->lock);
106457bddd21SDag-Erling Smørgrav 					return 1; /* just fallback */
106557bddd21SDag-Erling Smørgrav 				}
106657bddd21SDag-Erling Smørgrav 				lock_rw_unlock(&z->lock);
10674c75e3aaSDag-Erling Smørgrav 				errinf(qstate, "malloc failure");
106857bddd21SDag-Erling Smørgrav 				return 0;
106957bddd21SDag-Erling Smørgrav 			}
107057bddd21SDag-Erling Smørgrav 			dp->namelen = z->namelen;
107157bddd21SDag-Erling Smørgrav 			dp->namelabs = z->namelabs;
107257bddd21SDag-Erling Smørgrav 			dp->auth_dp = 1;
107357bddd21SDag-Erling Smørgrav 			iq->dp = dp;
107457bddd21SDag-Erling Smørgrav 		}
107557bddd21SDag-Erling Smørgrav 	}
107657bddd21SDag-Erling Smørgrav 
107757bddd21SDag-Erling Smørgrav 	lock_rw_unlock(&z->lock);
107857bddd21SDag-Erling Smørgrav 	return 1;
107957bddd21SDag-Erling Smørgrav }
108057bddd21SDag-Erling Smørgrav 
108157bddd21SDag-Erling Smørgrav /**
1082b7579f77SDag-Erling Smørgrav  * Generate A and AAAA checks for glue that is in-zone for the referral
10838a384985SDag-Erling Smørgrav  * we just got to obtain authoritative information on the addresses.
1084b7579f77SDag-Erling Smørgrav  *
1085b7579f77SDag-Erling Smørgrav  * @param qstate: the qtstate that triggered the need to prime.
1086b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1087b7579f77SDag-Erling Smørgrav  * @param id: module id.
1088b7579f77SDag-Erling Smørgrav  */
1089b7579f77SDag-Erling Smørgrav static void
1090b7579f77SDag-Erling Smørgrav generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
1091b7579f77SDag-Erling Smørgrav 	int id)
1092b7579f77SDag-Erling Smørgrav {
1093b7579f77SDag-Erling Smørgrav 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
1094b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
1095b7579f77SDag-Erling Smørgrav 	size_t i;
1096b7579f77SDag-Erling Smørgrav 	struct reply_info* rep = iq->response->rep;
1097b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* s;
1098b7579f77SDag-Erling Smørgrav 	log_assert(iq->dp);
1099b7579f77SDag-Erling Smørgrav 
1100b7579f77SDag-Erling Smørgrav 	if(iq->depth == ie->max_dependency_depth)
1101b7579f77SDag-Erling Smørgrav 		return;
1102b7579f77SDag-Erling Smørgrav 	/* walk through additional, and check if in-zone,
1103b7579f77SDag-Erling Smørgrav 	 * only relevant A, AAAA are left after scrub anyway */
1104b7579f77SDag-Erling Smørgrav 	for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
1105b7579f77SDag-Erling Smørgrav 		s = rep->rrsets[i];
1106b7579f77SDag-Erling Smørgrav 		/* check *ALL* addresses that are transmitted in additional*/
1107b7579f77SDag-Erling Smørgrav 		/* is it an address ? */
1108b7579f77SDag-Erling Smørgrav 		if( !(ntohs(s->rk.type)==LDNS_RR_TYPE_A ||
1109b7579f77SDag-Erling Smørgrav 			ntohs(s->rk.type)==LDNS_RR_TYPE_AAAA)) {
1110b7579f77SDag-Erling Smørgrav 			continue;
1111b7579f77SDag-Erling Smørgrav 		}
1112b7579f77SDag-Erling Smørgrav 		/* is this query the same as the A/AAAA check for it */
1113b7579f77SDag-Erling Smørgrav 		if(qstate->qinfo.qtype == ntohs(s->rk.type) &&
1114b7579f77SDag-Erling Smørgrav 			qstate->qinfo.qclass == ntohs(s->rk.rrset_class) &&
1115b7579f77SDag-Erling Smørgrav 			query_dname_compare(qstate->qinfo.qname,
1116b7579f77SDag-Erling Smørgrav 				s->rk.dname)==0 &&
1117b7579f77SDag-Erling Smørgrav 			(qstate->query_flags&BIT_RD) &&
1118b7579f77SDag-Erling Smørgrav 			!(qstate->query_flags&BIT_CD))
1119b7579f77SDag-Erling Smørgrav 			continue;
1120b7579f77SDag-Erling Smørgrav 
1121b7579f77SDag-Erling Smørgrav 		/* generate subrequest for it */
1122b7579f77SDag-Erling Smørgrav 		log_nametypeclass(VERB_ALGO, "schedule addr fetch",
1123b7579f77SDag-Erling Smørgrav 			s->rk.dname, ntohs(s->rk.type),
1124b7579f77SDag-Erling Smørgrav 			ntohs(s->rk.rrset_class));
1125b7579f77SDag-Erling Smørgrav 		if(!generate_sub_request(s->rk.dname, s->rk.dname_len,
1126b7579f77SDag-Erling Smørgrav 			ntohs(s->rk.type), ntohs(s->rk.rrset_class),
1127b7579f77SDag-Erling Smørgrav 			qstate, id, iq,
1128091e9e46SCy Schubert 			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1, 0)) {
1129b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "could not generate addr check");
1130b7579f77SDag-Erling Smørgrav 			return;
1131b7579f77SDag-Erling Smørgrav 		}
1132b7579f77SDag-Erling Smørgrav 		/* ignore subq - not need for more init */
1133b7579f77SDag-Erling Smørgrav 	}
1134b7579f77SDag-Erling Smørgrav }
1135b7579f77SDag-Erling Smørgrav 
1136b7579f77SDag-Erling Smørgrav /**
1137b7579f77SDag-Erling Smørgrav  * Generate a NS check request to obtain authoritative information
1138b7579f77SDag-Erling Smørgrav  * on an NS rrset.
1139b7579f77SDag-Erling Smørgrav  *
1140b7579f77SDag-Erling Smørgrav  * @param qstate: the qtstate that triggered the need to prime.
1141b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1142b7579f77SDag-Erling Smørgrav  * @param id: module id.
1143b7579f77SDag-Erling Smørgrav  */
1144b7579f77SDag-Erling Smørgrav static void
1145b7579f77SDag-Erling Smørgrav generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
1146b7579f77SDag-Erling Smørgrav {
1147b7579f77SDag-Erling Smørgrav 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
1148b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
1149b7579f77SDag-Erling Smørgrav 	log_assert(iq->dp);
1150b7579f77SDag-Erling Smørgrav 
1151b7579f77SDag-Erling Smørgrav 	if(iq->depth == ie->max_dependency_depth)
1152b7579f77SDag-Erling Smørgrav 		return;
1153c7f4d7adSDag-Erling Smørgrav 	if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen,
11547da0adf7SDag-Erling Smørgrav 		iq->qchase.qclass, NULL))
1155c7f4d7adSDag-Erling Smørgrav 		return;
1156b7579f77SDag-Erling Smørgrav 	/* is this query the same as the nscheck? */
1157b7579f77SDag-Erling Smørgrav 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS &&
1158b7579f77SDag-Erling Smørgrav 		query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
1159b7579f77SDag-Erling Smørgrav 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
1160b7579f77SDag-Erling Smørgrav 		/* spawn off A, AAAA queries for in-zone glue to check */
1161b7579f77SDag-Erling Smørgrav 		generate_a_aaaa_check(qstate, iq, id);
1162b7579f77SDag-Erling Smørgrav 		return;
1163b7579f77SDag-Erling Smørgrav 	}
116457bddd21SDag-Erling Smørgrav 	/* no need to get the NS record for DS, it is above the zonecut */
116557bddd21SDag-Erling Smørgrav 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_DS)
116657bddd21SDag-Erling Smørgrav 		return;
1167b7579f77SDag-Erling Smørgrav 
1168b7579f77SDag-Erling Smørgrav 	log_nametypeclass(VERB_ALGO, "schedule ns fetch",
1169b7579f77SDag-Erling Smørgrav 		iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1170b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
1171b7579f77SDag-Erling Smørgrav 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1172091e9e46SCy Schubert 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1, 0)) {
1173b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "could not generate ns check");
1174b7579f77SDag-Erling Smørgrav 		return;
1175b7579f77SDag-Erling Smørgrav 	}
1176b7579f77SDag-Erling Smørgrav 	if(subq) {
1177b7579f77SDag-Erling Smørgrav 		struct iter_qstate* subiq =
1178b7579f77SDag-Erling Smørgrav 			(struct iter_qstate*)subq->minfo[id];
1179b7579f77SDag-Erling Smørgrav 
1180b7579f77SDag-Erling Smørgrav 		/* make copy to avoid use of stub dp by different qs/threads */
1181b7579f77SDag-Erling Smørgrav 		/* refetch glue to start higher up the tree */
1182b7579f77SDag-Erling Smørgrav 		subiq->refetch_glue = 1;
1183b7579f77SDag-Erling Smørgrav 		subiq->dp = delegpt_copy(iq->dp, subq->region);
1184b7579f77SDag-Erling Smørgrav 		if(!subiq->dp) {
1185b7579f77SDag-Erling Smørgrav 			log_err("out of memory generating ns check, copydp");
1186b7579f77SDag-Erling Smørgrav 			fptr_ok(fptr_whitelist_modenv_kill_sub(
1187b7579f77SDag-Erling Smørgrav 				qstate->env->kill_sub));
1188b7579f77SDag-Erling Smørgrav 			(*qstate->env->kill_sub)(subq);
1189b7579f77SDag-Erling Smørgrav 			return;
1190b7579f77SDag-Erling Smørgrav 		}
1191b7579f77SDag-Erling Smørgrav 	}
1192b7579f77SDag-Erling Smørgrav }
1193b7579f77SDag-Erling Smørgrav 
1194b7579f77SDag-Erling Smørgrav /**
1195b7579f77SDag-Erling Smørgrav  * Generate a DNSKEY prefetch query to get the DNSKEY for the DS record we
1196b7579f77SDag-Erling Smørgrav  * just got in a referral (where we have dnssec_expected, thus have trust
1197b7579f77SDag-Erling Smørgrav  * anchors above it).  Note that right after calling this routine the
1198b7579f77SDag-Erling Smørgrav  * iterator detached subqueries (because of following the referral), and thus
1199b7579f77SDag-Erling Smørgrav  * the DNSKEY query becomes detached, its return stored in the cache for
1200b7579f77SDag-Erling Smørgrav  * later lookup by the validator.  This cache lookup by the validator avoids
1201b7579f77SDag-Erling Smørgrav  * the roundtrip incurred by the DNSKEY query.  The DNSKEY query is now
1202b7579f77SDag-Erling Smørgrav  * performed at about the same time the original query is sent to the domain,
1203b7579f77SDag-Erling Smørgrav  * thus the two answers are likely to be returned at about the same time,
1204b7579f77SDag-Erling Smørgrav  * saving a roundtrip from the validated lookup.
1205b7579f77SDag-Erling Smørgrav  *
1206b7579f77SDag-Erling Smørgrav  * @param qstate: the qtstate that triggered the need to prime.
1207b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1208b7579f77SDag-Erling Smørgrav  * @param id: module id.
1209b7579f77SDag-Erling Smørgrav  */
1210b7579f77SDag-Erling Smørgrav static void
1211b7579f77SDag-Erling Smørgrav generate_dnskey_prefetch(struct module_qstate* qstate,
1212b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq, int id)
1213b7579f77SDag-Erling Smørgrav {
1214b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
1215b7579f77SDag-Erling Smørgrav 	log_assert(iq->dp);
1216b7579f77SDag-Erling Smørgrav 
1217b7579f77SDag-Erling Smørgrav 	/* is this query the same as the prefetch? */
1218b7579f77SDag-Erling Smørgrav 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
1219b7579f77SDag-Erling Smørgrav 		query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
1220b7579f77SDag-Erling Smørgrav 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
1221b7579f77SDag-Erling Smørgrav 		return;
1222b7579f77SDag-Erling Smørgrav 	}
12234f5c8956SCy Schubert 	/* we do not generate this prefetch when the query list is full,
12244f5c8956SCy Schubert 	 * the query is fetched, if needed, when the validator wants it.
12254f5c8956SCy Schubert 	 * At that time the validator waits for it, after spawning it.
12264f5c8956SCy Schubert 	 * This means there is one state that uses cpu and a socket, the
12274f5c8956SCy Schubert 	 * spawned while this one waits, and not several at the same time,
12284f5c8956SCy Schubert 	 * if we had created the lookup here. And this helps to keep
12294f5c8956SCy Schubert 	 * the total load down, but the query still succeeds to resolve. */
12304f5c8956SCy Schubert 	if(mesh_jostle_exceeded(qstate->env->mesh))
12314f5c8956SCy Schubert 		return;
1232b7579f77SDag-Erling Smørgrav 
1233b7579f77SDag-Erling Smørgrav 	/* if the DNSKEY is in the cache this lookup will stop quickly */
1234b7579f77SDag-Erling Smørgrav 	log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch",
1235b7579f77SDag-Erling Smørgrav 		iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
1236b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
1237b7579f77SDag-Erling Smørgrav 		LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
1238091e9e46SCy Schubert 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0)) {
1239b7579f77SDag-Erling Smørgrav 		/* we'll be slower, but it'll work */
1240b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "could not generate dnskey prefetch");
1241b7579f77SDag-Erling Smørgrav 		return;
1242b7579f77SDag-Erling Smørgrav 	}
1243b7579f77SDag-Erling Smørgrav 	if(subq) {
1244b7579f77SDag-Erling Smørgrav 		struct iter_qstate* subiq =
1245b7579f77SDag-Erling Smørgrav 			(struct iter_qstate*)subq->minfo[id];
1246b7579f77SDag-Erling Smørgrav 		/* this qstate has the right delegation for the dnskey lookup*/
1247b7579f77SDag-Erling Smørgrav 		/* make copy to avoid use of stub dp by different qs/threads */
1248b7579f77SDag-Erling Smørgrav 		subiq->dp = delegpt_copy(iq->dp, subq->region);
1249b7579f77SDag-Erling Smørgrav 		/* if !subiq->dp, it'll start from the cache, no problem */
1250b7579f77SDag-Erling Smørgrav 	}
1251b7579f77SDag-Erling Smørgrav }
1252b7579f77SDag-Erling Smørgrav 
1253b7579f77SDag-Erling Smørgrav /**
1254b7579f77SDag-Erling Smørgrav  * See if the query needs forwarding.
1255b7579f77SDag-Erling Smørgrav  *
1256b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1257b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1258b7579f77SDag-Erling Smørgrav  * @return true if the request is forwarded, false if not.
1259b7579f77SDag-Erling Smørgrav  * 	If returns true but, iq->dp is NULL then a malloc failure occurred.
1260b7579f77SDag-Erling Smørgrav  */
1261b7579f77SDag-Erling Smørgrav static int
1262b7579f77SDag-Erling Smørgrav forward_request(struct module_qstate* qstate, struct iter_qstate* iq)
1263b7579f77SDag-Erling Smørgrav {
1264b7579f77SDag-Erling Smørgrav 	struct delegpt* dp;
1265b7579f77SDag-Erling Smørgrav 	uint8_t* delname = iq->qchase.qname;
1266b7579f77SDag-Erling Smørgrav 	size_t delnamelen = iq->qchase.qname_len;
1267a755b6f6SDag-Erling Smørgrav 	if(iq->refetch_glue && iq->dp) {
1268b7579f77SDag-Erling Smørgrav 		delname = iq->dp->name;
1269b7579f77SDag-Erling Smørgrav 		delnamelen = iq->dp->namelen;
1270b7579f77SDag-Erling Smørgrav 	}
1271b7579f77SDag-Erling Smørgrav 	/* strip one label off of DS query to lookup higher for it */
1272b7579f77SDag-Erling Smørgrav 	if( (iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue)
1273b7579f77SDag-Erling Smørgrav 		&& !dname_is_root(iq->qchase.qname))
1274b7579f77SDag-Erling Smørgrav 		dname_remove_label(&delname, &delnamelen);
1275b7579f77SDag-Erling Smørgrav 	dp = forwards_lookup(qstate->env->fwds, delname, iq->qchase.qclass);
1276b7579f77SDag-Erling Smørgrav 	if(!dp)
1277b7579f77SDag-Erling Smørgrav 		return 0;
1278b7579f77SDag-Erling Smørgrav 	/* send recursion desired to forward addr */
1279b7579f77SDag-Erling Smørgrav 	iq->chase_flags |= BIT_RD;
1280b7579f77SDag-Erling Smørgrav 	iq->dp = delegpt_copy(dp, qstate->region);
1281b7579f77SDag-Erling Smørgrav 	/* iq->dp checked by caller */
1282b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "forwarding request");
1283b7579f77SDag-Erling Smørgrav 	return 1;
1284b7579f77SDag-Erling Smørgrav }
1285b7579f77SDag-Erling Smørgrav 
1286b7579f77SDag-Erling Smørgrav /**
1287b7579f77SDag-Erling Smørgrav  * Process the initial part of the request handling. This state roughly
1288b7579f77SDag-Erling Smørgrav  * corresponds to resolver algorithms steps 1 (find answer in cache) and 2
1289b7579f77SDag-Erling Smørgrav  * (find the best servers to ask).
1290b7579f77SDag-Erling Smørgrav  *
1291b7579f77SDag-Erling Smørgrav  * Note that all requests start here, and query restarts revisit this state.
1292b7579f77SDag-Erling Smørgrav  *
1293b7579f77SDag-Erling Smørgrav  * This state either generates: 1) a response, from cache or error, 2) a
1294b7579f77SDag-Erling Smørgrav  * priming event, or 3) forwards the request to the next state (init2,
1295b7579f77SDag-Erling Smørgrav  * generally).
1296b7579f77SDag-Erling Smørgrav  *
1297b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1298b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1299b7579f77SDag-Erling Smørgrav  * @param ie: iterator shared global environment.
1300b7579f77SDag-Erling Smørgrav  * @param id: module id.
1301b7579f77SDag-Erling Smørgrav  * @return true if the event needs more request processing immediately,
1302b7579f77SDag-Erling Smørgrav  *         false if not.
1303b7579f77SDag-Erling Smørgrav  */
1304b7579f77SDag-Erling Smørgrav static int
1305b7579f77SDag-Erling Smørgrav processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
1306b7579f77SDag-Erling Smørgrav 	struct iter_env* ie, int id)
1307b7579f77SDag-Erling Smørgrav {
13085469a995SCy Schubert 	uint8_t* delname, *dpname=NULL;
13095469a995SCy Schubert 	size_t delnamelen, dpnamelen=0;
1310bc892140SDag-Erling Smørgrav 	struct dns_msg* msg = NULL;
1311b7579f77SDag-Erling Smørgrav 
1312b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_DETAIL, "resolving", &qstate->qinfo);
1313b7579f77SDag-Erling Smørgrav 	/* check effort */
1314b7579f77SDag-Erling Smørgrav 
1315b7579f77SDag-Erling Smørgrav 	/* We enforce a maximum number of query restarts. This is primarily a
1316b7579f77SDag-Erling Smørgrav 	 * cheap way to prevent CNAME loops. */
1317b7579f77SDag-Erling Smørgrav 	if(iq->query_restart_count > MAX_RESTART_COUNT) {
1318b7579f77SDag-Erling Smørgrav 		verbose(VERB_QUERY, "request has exceeded the maximum number"
1319b7579f77SDag-Erling Smørgrav 			" of query restarts with %d", iq->query_restart_count);
13204c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "request has exceeded the maximum number "
13214c75e3aaSDag-Erling Smørgrav 			"restarts (eg. indirections)");
13224c75e3aaSDag-Erling Smørgrav 		if(iq->qchase.qname)
13234c75e3aaSDag-Erling Smørgrav 			errinf_dname(qstate, "stop at", iq->qchase.qname);
1324b7579f77SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1325b7579f77SDag-Erling Smørgrav 	}
1326b7579f77SDag-Erling Smørgrav 
1327b7579f77SDag-Erling Smørgrav 	/* We enforce a maximum recursion/dependency depth -- in general,
1328b7579f77SDag-Erling Smørgrav 	 * this is unnecessary for dependency loops (although it will
1329b7579f77SDag-Erling Smørgrav 	 * catch those), but it provides a sensible limit to the amount
1330b7579f77SDag-Erling Smørgrav 	 * of work required to answer a given query. */
1331b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "request has dependency depth of %d", iq->depth);
1332b7579f77SDag-Erling Smørgrav 	if(iq->depth > ie->max_dependency_depth) {
1333b7579f77SDag-Erling Smørgrav 		verbose(VERB_QUERY, "request has exceeded the maximum "
1334b7579f77SDag-Erling Smørgrav 			"dependency depth with depth of %d", iq->depth);
13354c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "request has exceeded the maximum dependency "
13364c75e3aaSDag-Erling Smørgrav 			"depth (eg. nameserver lookup recursion)");
1337b7579f77SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1338b7579f77SDag-Erling Smørgrav 	}
1339b7579f77SDag-Erling Smørgrav 
1340b7579f77SDag-Erling Smørgrav 	/* If the request is qclass=ANY, setup to generate each class */
1341b7579f77SDag-Erling Smørgrav 	if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
1342b7579f77SDag-Erling Smørgrav 		iq->qchase.qclass = 0;
1343b7579f77SDag-Erling Smørgrav 		return next_state(iq, COLLECT_CLASS_STATE);
1344b7579f77SDag-Erling Smørgrav 	}
1345b7579f77SDag-Erling Smørgrav 
1346c7f4d7adSDag-Erling Smørgrav 	/*
1347c7f4d7adSDag-Erling Smørgrav 	 * If we are restricted by a forward-zone or a stub-zone, we
1348c7f4d7adSDag-Erling Smørgrav 	 * can't re-fetch glue for this delegation point.
1349c7f4d7adSDag-Erling Smørgrav 	 * we won’t try to re-fetch glue if the iq->dp is null.
1350c7f4d7adSDag-Erling Smørgrav 	 */
1351c7f4d7adSDag-Erling Smørgrav 	if (iq->refetch_glue &&
1352c7f4d7adSDag-Erling Smørgrav 	        iq->dp &&
13537da0adf7SDag-Erling Smørgrav 	        !can_have_last_resort(qstate->env, iq->dp->name,
13547da0adf7SDag-Erling Smørgrav 	             iq->dp->namelen, iq->qchase.qclass, NULL)) {
1355c7f4d7adSDag-Erling Smørgrav 	    iq->refetch_glue = 0;
1356c7f4d7adSDag-Erling Smørgrav 	}
1357c7f4d7adSDag-Erling Smørgrav 
1358b7579f77SDag-Erling Smørgrav 	/* Resolver Algorithm Step 1 -- Look for the answer in local data. */
1359b7579f77SDag-Erling Smørgrav 
1360b7579f77SDag-Erling Smørgrav 	/* This either results in a query restart (CNAME cache response), a
1361b7579f77SDag-Erling Smørgrav 	 * terminating response (ANSWER), or a cache miss (null). */
1362b7579f77SDag-Erling Smørgrav 
13635469a995SCy Schubert 	if (iter_stub_fwd_no_cache(qstate, &iq->qchase, &dpname, &dpnamelen)) {
13644c75e3aaSDag-Erling Smørgrav 		/* Asked to not query cache. */
13654c75e3aaSDag-Erling Smørgrav 		verbose(VERB_ALGO, "no-cache set, going to the network");
13664c75e3aaSDag-Erling Smørgrav 		qstate->no_cache_lookup = 1;
13674c75e3aaSDag-Erling Smørgrav 		qstate->no_cache_store = 1;
13684c75e3aaSDag-Erling Smørgrav 		msg = NULL;
13694c75e3aaSDag-Erling Smørgrav 	} else if(qstate->blacklist) {
1370b7579f77SDag-Erling Smørgrav 		/* if cache, or anything else, was blacklisted then
1371b7579f77SDag-Erling Smørgrav 		 * getting older results from cache is a bad idea, no cache */
1372b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "cache blacklisted, going to the network");
1373b7579f77SDag-Erling Smørgrav 		msg = NULL;
1374bc892140SDag-Erling Smørgrav 	} else if(!qstate->no_cache_lookup) {
1375b7579f77SDag-Erling Smørgrav 		msg = dns_cache_lookup(qstate->env, iq->qchase.qname,
1376b7579f77SDag-Erling Smørgrav 			iq->qchase.qname_len, iq->qchase.qtype,
1377ff825849SDag-Erling Smørgrav 			iq->qchase.qclass, qstate->query_flags,
13785469a995SCy Schubert 			qstate->region, qstate->env->scratch, 0, dpname,
13795469a995SCy Schubert 			dpnamelen);
13800fb34990SDag-Erling Smørgrav 		if(!msg && qstate->env->neg_cache &&
13810fb34990SDag-Erling Smørgrav 			iter_qname_indicates_dnssec(qstate->env, &iq->qchase)) {
1382b7579f77SDag-Erling Smørgrav 			/* lookup in negative cache; may result in
1383b7579f77SDag-Erling Smørgrav 			 * NOERROR/NODATA or NXDOMAIN answers that need validation */
1384b7579f77SDag-Erling Smørgrav 			msg = val_neg_getmsg(qstate->env->neg_cache, &iq->qchase,
1385b7579f77SDag-Erling Smørgrav 				qstate->region, qstate->env->rrset_cache,
1386b7579f77SDag-Erling Smørgrav 				qstate->env->scratch_buffer,
138757bddd21SDag-Erling Smørgrav 				*qstate->env->now, 1/*add SOA*/, NULL,
138857bddd21SDag-Erling Smørgrav 				qstate->env->cfg);
1389b7579f77SDag-Erling Smørgrav 		}
1390b7579f77SDag-Erling Smørgrav 		/* item taken from cache does not match our query name, thus
1391b7579f77SDag-Erling Smørgrav 		 * security needs to be re-examined later */
1392b7579f77SDag-Erling Smørgrav 		if(msg && query_dname_compare(qstate->qinfo.qname,
1393b7579f77SDag-Erling Smørgrav 			iq->qchase.qname) != 0)
1394b7579f77SDag-Erling Smørgrav 			msg->rep->security = sec_status_unchecked;
1395b7579f77SDag-Erling Smørgrav 	}
1396b7579f77SDag-Erling Smørgrav 	if(msg) {
1397b7579f77SDag-Erling Smørgrav 		/* handle positive cache response */
1398b7579f77SDag-Erling Smørgrav 		enum response_type type = response_type_from_cache(msg,
1399b7579f77SDag-Erling Smørgrav 			&iq->qchase);
1400b7579f77SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO) {
1401b7579f77SDag-Erling Smørgrav 			log_dns_msg("msg from cache lookup", &msg->qinfo,
1402b7579f77SDag-Erling Smørgrav 				msg->rep);
1403b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "msg ttl is %d, prefetch ttl %d",
1404b7579f77SDag-Erling Smørgrav 				(int)msg->rep->ttl,
1405b7579f77SDag-Erling Smørgrav 				(int)msg->rep->prefetch_ttl);
1406b7579f77SDag-Erling Smørgrav 		}
1407b7579f77SDag-Erling Smørgrav 
1408b7579f77SDag-Erling Smørgrav 		if(type == RESPONSE_TYPE_CNAME) {
1409b7579f77SDag-Erling Smørgrav 			uint8_t* sname = 0;
1410b7579f77SDag-Erling Smørgrav 			size_t slen = 0;
1411b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "returning CNAME response from "
1412b7579f77SDag-Erling Smørgrav 				"cache");
1413b7579f77SDag-Erling Smørgrav 			if(!handle_cname_response(qstate, iq, msg,
14144c75e3aaSDag-Erling Smørgrav 				&sname, &slen)) {
14154c75e3aaSDag-Erling Smørgrav 				errinf(qstate, "failed to prepend CNAME "
14164c75e3aaSDag-Erling Smørgrav 					"components, malloc failure");
1417b7579f77SDag-Erling Smørgrav 				return error_response(qstate, id,
1418b7579f77SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
14194c75e3aaSDag-Erling Smørgrav 			}
1420b7579f77SDag-Erling Smørgrav 			iq->qchase.qname = sname;
1421b7579f77SDag-Erling Smørgrav 			iq->qchase.qname_len = slen;
1422b7579f77SDag-Erling Smørgrav 			/* This *is* a query restart, even if it is a cheap
1423b7579f77SDag-Erling Smørgrav 			 * one. */
1424b7579f77SDag-Erling Smørgrav 			iq->dp = NULL;
1425b7579f77SDag-Erling Smørgrav 			iq->refetch_glue = 0;
1426b7579f77SDag-Erling Smørgrav 			iq->query_restart_count++;
1427b7579f77SDag-Erling Smørgrav 			iq->sent_count = 0;
1428091e9e46SCy Schubert 			iq->dp_target_count = 0;
1429b7579f77SDag-Erling Smørgrav 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
143005ab2901SDag-Erling Smørgrav 			if(qstate->env->cfg->qname_minimisation)
143105ab2901SDag-Erling Smørgrav 				iq->minimisation_state = INIT_MINIMISE_STATE;
1432b7579f77SDag-Erling Smørgrav 			return next_state(iq, INIT_REQUEST_STATE);
1433b7579f77SDag-Erling Smørgrav 		}
1434b7579f77SDag-Erling Smørgrav 
1435b7579f77SDag-Erling Smørgrav 		/* if from cache, NULL, else insert 'cache IP' len=0 */
1436b7579f77SDag-Erling Smørgrav 		if(qstate->reply_origin)
1437b7579f77SDag-Erling Smørgrav 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
14384c75e3aaSDag-Erling Smørgrav 		if(FLAGS_GET_RCODE(msg->rep->flags) == LDNS_RCODE_SERVFAIL)
14394c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "SERVFAIL in cache");
1440b7579f77SDag-Erling Smørgrav 		/* it is an answer, response, to final state */
1441b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "returning answer from cache.");
1442b7579f77SDag-Erling Smørgrav 		iq->response = msg;
1443b7579f77SDag-Erling Smørgrav 		return final_state(iq);
1444b7579f77SDag-Erling Smørgrav 	}
1445b7579f77SDag-Erling Smørgrav 
1446b7579f77SDag-Erling Smørgrav 	/* attempt to forward the request */
1447b7579f77SDag-Erling Smørgrav 	if(forward_request(qstate, iq))
1448b7579f77SDag-Erling Smørgrav 	{
1449b7579f77SDag-Erling Smørgrav 		if(!iq->dp) {
1450b7579f77SDag-Erling Smørgrav 			log_err("alloc failure for forward dp");
14514c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure for forward zone");
1452b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1453b7579f77SDag-Erling Smørgrav 		}
1454b7579f77SDag-Erling Smørgrav 		iq->refetch_glue = 0;
145505ab2901SDag-Erling Smørgrav 		iq->minimisation_state = DONOT_MINIMISE_STATE;
1456b7579f77SDag-Erling Smørgrav 		/* the request has been forwarded.
1457b7579f77SDag-Erling Smørgrav 		 * forwarded requests need to be immediately sent to the
1458b7579f77SDag-Erling Smørgrav 		 * next state, QUERYTARGETS. */
1459b7579f77SDag-Erling Smørgrav 		return next_state(iq, QUERYTARGETS_STATE);
1460b7579f77SDag-Erling Smørgrav 	}
1461b7579f77SDag-Erling Smørgrav 
1462b7579f77SDag-Erling Smørgrav 	/* Resolver Algorithm Step 2 -- find the "best" servers. */
1463b7579f77SDag-Erling Smørgrav 
1464b7579f77SDag-Erling Smørgrav 	/* first, adjust for DS queries. To avoid the grandparent problem,
1465b7579f77SDag-Erling Smørgrav 	 * we just look for the closest set of server to the parent of qname.
1466b7579f77SDag-Erling Smørgrav 	 * When re-fetching glue we also need to ask the parent.
1467b7579f77SDag-Erling Smørgrav 	 */
1468b7579f77SDag-Erling Smørgrav 	if(iq->refetch_glue) {
1469b7579f77SDag-Erling Smørgrav 		if(!iq->dp) {
1470b7579f77SDag-Erling Smørgrav 			log_err("internal or malloc fail: no dp for refetch");
14714c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, for delegation info");
1472b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1473b7579f77SDag-Erling Smørgrav 		}
1474b7579f77SDag-Erling Smørgrav 		delname = iq->dp->name;
1475b7579f77SDag-Erling Smørgrav 		delnamelen = iq->dp->namelen;
1476b7579f77SDag-Erling Smørgrav 	} else {
1477b7579f77SDag-Erling Smørgrav 		delname = iq->qchase.qname;
1478b7579f77SDag-Erling Smørgrav 		delnamelen = iq->qchase.qname_len;
1479b7579f77SDag-Erling Smørgrav 	}
1480b7579f77SDag-Erling Smørgrav 	if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue ||
14810fb34990SDag-Erling Smørgrav 	   (iq->qchase.qtype == LDNS_RR_TYPE_NS && qstate->prefetch_leeway
14827da0adf7SDag-Erling Smørgrav 	   && can_have_last_resort(qstate->env, delname, delnamelen, iq->qchase.qclass, NULL))) {
1483b7579f77SDag-Erling Smørgrav 		/* remove first label from delname, root goes to hints,
1484b7579f77SDag-Erling Smørgrav 		 * but only to fetch glue, not for qtype=DS. */
1485b7579f77SDag-Erling Smørgrav 		/* also when prefetching an NS record, fetch it again from
1486b7579f77SDag-Erling Smørgrav 		 * its parent, just as if it expired, so that you do not
1487b7579f77SDag-Erling Smørgrav 		 * get stuck on an older nameserver that gives old NSrecords */
1488b7579f77SDag-Erling Smørgrav 		if(dname_is_root(delname) && (iq->refetch_glue ||
1489b7579f77SDag-Erling Smørgrav 			(iq->qchase.qtype == LDNS_RR_TYPE_NS &&
1490b7579f77SDag-Erling Smørgrav 			qstate->prefetch_leeway)))
1491b7579f77SDag-Erling Smørgrav 			delname = NULL; /* go to root priming */
1492b7579f77SDag-Erling Smørgrav 		else 	dname_remove_label(&delname, &delnamelen);
1493b7579f77SDag-Erling Smørgrav 	}
1494b7579f77SDag-Erling Smørgrav 	/* delname is the name to lookup a delegation for. If NULL rootprime */
1495b7579f77SDag-Erling Smørgrav 	while(1) {
1496b7579f77SDag-Erling Smørgrav 
1497b7579f77SDag-Erling Smørgrav 		/* Lookup the delegation in the cache. If null, then the
1498b7579f77SDag-Erling Smørgrav 		 * cache needs to be primed for the qclass. */
1499b7579f77SDag-Erling Smørgrav 		if(delname)
1500b7579f77SDag-Erling Smørgrav 		     iq->dp = dns_cache_find_delegation(qstate->env, delname,
1501b7579f77SDag-Erling Smørgrav 			delnamelen, iq->qchase.qtype, iq->qchase.qclass,
1502b7579f77SDag-Erling Smørgrav 			qstate->region, &iq->deleg_msg,
1503790c6b24SCy Schubert 			*qstate->env->now+qstate->prefetch_leeway, 1,
1504790c6b24SCy Schubert 			dpname, dpnamelen);
1505b7579f77SDag-Erling Smørgrav 		else iq->dp = NULL;
1506b7579f77SDag-Erling Smørgrav 
1507b7579f77SDag-Erling Smørgrav 		/* If the cache has returned nothing, then we have a
1508b7579f77SDag-Erling Smørgrav 		 * root priming situation. */
1509b7579f77SDag-Erling Smørgrav 		if(iq->dp == NULL) {
151057bddd21SDag-Erling Smørgrav 			int r;
151157bddd21SDag-Erling Smørgrav 			/* if under auth zone, no prime needed */
151257bddd21SDag-Erling Smørgrav 			if(!auth_zone_delegpt(qstate, iq, delname, delnamelen))
151357bddd21SDag-Erling Smørgrav 				return error_response(qstate, id,
151457bddd21SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
151557bddd21SDag-Erling Smørgrav 			if(iq->dp) /* use auth zone dp */
151657bddd21SDag-Erling Smørgrav 				return next_state(iq, INIT_REQUEST_2_STATE);
1517b7579f77SDag-Erling Smørgrav 			/* if there is a stub, then no root prime needed */
151857bddd21SDag-Erling Smørgrav 			r = prime_stub(qstate, iq, id, delname,
1519b7579f77SDag-Erling Smørgrav 				iq->qchase.qclass);
1520b7579f77SDag-Erling Smørgrav 			if(r == 2)
1521b7579f77SDag-Erling Smørgrav 				break; /* got noprime-stub-zone, continue */
1522b7579f77SDag-Erling Smørgrav 			else if(r)
1523b7579f77SDag-Erling Smørgrav 				return 0; /* stub prime request made */
1524b7579f77SDag-Erling Smørgrav 			if(forwards_lookup_root(qstate->env->fwds,
1525b7579f77SDag-Erling Smørgrav 				iq->qchase.qclass)) {
1526b7579f77SDag-Erling Smørgrav 				/* forward zone root, no root prime needed */
1527b7579f77SDag-Erling Smørgrav 				/* fill in some dp - safety belt */
1528b7579f77SDag-Erling Smørgrav 				iq->dp = hints_lookup_root(qstate->env->hints,
1529b7579f77SDag-Erling Smørgrav 					iq->qchase.qclass);
1530b7579f77SDag-Erling Smørgrav 				if(!iq->dp) {
1531b7579f77SDag-Erling Smørgrav 					log_err("internal error: no hints dp");
15324c75e3aaSDag-Erling Smørgrav 					errinf(qstate, "no hints for this class");
1533b7579f77SDag-Erling Smørgrav 					return error_response(qstate, id,
1534b7579f77SDag-Erling Smørgrav 						LDNS_RCODE_SERVFAIL);
1535b7579f77SDag-Erling Smørgrav 				}
1536b7579f77SDag-Erling Smørgrav 				iq->dp = delegpt_copy(iq->dp, qstate->region);
1537b7579f77SDag-Erling Smørgrav 				if(!iq->dp) {
1538b7579f77SDag-Erling Smørgrav 					log_err("out of memory in safety belt");
15394c75e3aaSDag-Erling Smørgrav 					errinf(qstate, "malloc failure, in safety belt");
1540b7579f77SDag-Erling Smørgrav 					return error_response(qstate, id,
1541b7579f77SDag-Erling Smørgrav 						LDNS_RCODE_SERVFAIL);
1542b7579f77SDag-Erling Smørgrav 				}
1543b7579f77SDag-Erling Smørgrav 				return next_state(iq, INIT_REQUEST_2_STATE);
1544b7579f77SDag-Erling Smørgrav 			}
1545b7579f77SDag-Erling Smørgrav 			/* Note that the result of this will set a new
1546b7579f77SDag-Erling Smørgrav 			 * DelegationPoint based on the result of priming. */
1547b7579f77SDag-Erling Smørgrav 			if(!prime_root(qstate, iq, id, iq->qchase.qclass))
1548b7579f77SDag-Erling Smørgrav 				return error_response(qstate, id,
1549b7579f77SDag-Erling Smørgrav 					LDNS_RCODE_REFUSED);
1550b7579f77SDag-Erling Smørgrav 
1551b7579f77SDag-Erling Smørgrav 			/* priming creates and sends a subordinate query, with
1552b7579f77SDag-Erling Smørgrav 			 * this query as the parent. So further processing for
1553b7579f77SDag-Erling Smørgrav 			 * this event will stop until reactivated by the
1554b7579f77SDag-Erling Smørgrav 			 * results of priming. */
1555b7579f77SDag-Erling Smørgrav 			return 0;
1556b7579f77SDag-Erling Smørgrav 		}
155709a3aaf3SDag-Erling Smørgrav 		if(!iq->ratelimit_ok && qstate->prefetch_leeway)
155809a3aaf3SDag-Erling Smørgrav 			iq->ratelimit_ok = 1; /* allow prefetches, this keeps
155909a3aaf3SDag-Erling Smørgrav 			otherwise valid data in the cache */
1560b7579f77SDag-Erling Smørgrav 
1561b7579f77SDag-Erling Smørgrav 		/* see if this dp not useless.
1562b7579f77SDag-Erling Smørgrav 		 * It is useless if:
1563b7579f77SDag-Erling Smørgrav 		 *	o all NS items are required glue.
1564b7579f77SDag-Erling Smørgrav 		 *	  or the query is for NS item that is required glue.
1565b7579f77SDag-Erling Smørgrav 		 *	o no addresses are provided.
1566b7579f77SDag-Erling Smørgrav 		 *	o RD qflag is on.
1567b7579f77SDag-Erling Smørgrav 		 * Instead, go up one level, and try to get even further
1568b7579f77SDag-Erling Smørgrav 		 * If the root was useless, use safety belt information.
1569b7579f77SDag-Erling Smørgrav 		 * Only check cache returns, because replies for servers
1570b7579f77SDag-Erling Smørgrav 		 * could be useless but lead to loops (bumping into the
1571b7579f77SDag-Erling Smørgrav 		 * same server reply) if useless-checked.
1572b7579f77SDag-Erling Smørgrav 		 */
1573b7579f77SDag-Erling Smørgrav 		if(iter_dp_is_useless(&qstate->qinfo, qstate->query_flags,
1574a39a5a69SCy Schubert 			iq->dp, ie->supports_ipv4, ie->supports_ipv6)) {
15757da0adf7SDag-Erling Smørgrav 			struct delegpt* retdp = NULL;
15767da0adf7SDag-Erling Smørgrav 			if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen, iq->qchase.qclass, &retdp)) {
15777da0adf7SDag-Erling Smørgrav 				if(retdp) {
15787da0adf7SDag-Erling Smørgrav 					verbose(VERB_QUERY, "cache has stub "
15797da0adf7SDag-Erling Smørgrav 						"or fwd but no addresses, "
15807da0adf7SDag-Erling Smørgrav 						"fallback to config");
15817da0adf7SDag-Erling Smørgrav 					iq->dp = delegpt_copy(retdp,
15827da0adf7SDag-Erling Smørgrav 						qstate->region);
15837da0adf7SDag-Erling Smørgrav 					if(!iq->dp) {
15847da0adf7SDag-Erling Smørgrav 						log_err("out of memory in "
15857da0adf7SDag-Erling Smørgrav 							"stub/fwd fallback");
15864c75e3aaSDag-Erling Smørgrav 						errinf(qstate, "malloc failure, for fallback to config");
15877da0adf7SDag-Erling Smørgrav 						return error_response(qstate,
15887da0adf7SDag-Erling Smørgrav 						    id, LDNS_RCODE_SERVFAIL);
15897da0adf7SDag-Erling Smørgrav 					}
15907da0adf7SDag-Erling Smørgrav 					break;
15917da0adf7SDag-Erling Smørgrav 				}
15920fb34990SDag-Erling Smørgrav 				verbose(VERB_ALGO, "useless dp "
15930fb34990SDag-Erling Smørgrav 					"but cannot go up, servfail");
15947da0adf7SDag-Erling Smørgrav 				delegpt_log(VERB_ALGO, iq->dp);
15954c75e3aaSDag-Erling Smørgrav 				errinf(qstate, "no useful nameservers, "
15964c75e3aaSDag-Erling Smørgrav 					"and cannot go up");
15974c75e3aaSDag-Erling Smørgrav 				errinf_dname(qstate, "for zone", iq->dp->name);
15980fb34990SDag-Erling Smørgrav 				return error_response(qstate, id,
15990fb34990SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
16000fb34990SDag-Erling Smørgrav 			}
1601b7579f77SDag-Erling Smørgrav 			if(dname_is_root(iq->dp->name)) {
1602b7579f77SDag-Erling Smørgrav 				/* use safety belt */
1603b7579f77SDag-Erling Smørgrav 				verbose(VERB_QUERY, "Cache has root NS but "
1604b7579f77SDag-Erling Smørgrav 				"no addresses. Fallback to the safety belt.");
1605b7579f77SDag-Erling Smørgrav 				iq->dp = hints_lookup_root(qstate->env->hints,
1606b7579f77SDag-Erling Smørgrav 					iq->qchase.qclass);
1607b7579f77SDag-Erling Smørgrav 				/* note deleg_msg is from previous lookup,
1608b7579f77SDag-Erling Smørgrav 				 * but RD is on, so it is not used */
1609b7579f77SDag-Erling Smørgrav 				if(!iq->dp) {
1610b7579f77SDag-Erling Smørgrav 					log_err("internal error: no hints dp");
1611b7579f77SDag-Erling Smørgrav 					return error_response(qstate, id,
1612b7579f77SDag-Erling Smørgrav 						LDNS_RCODE_REFUSED);
1613b7579f77SDag-Erling Smørgrav 				}
1614b7579f77SDag-Erling Smørgrav 				iq->dp = delegpt_copy(iq->dp, qstate->region);
1615b7579f77SDag-Erling Smørgrav 				if(!iq->dp) {
1616b7579f77SDag-Erling Smørgrav 					log_err("out of memory in safety belt");
16174c75e3aaSDag-Erling Smørgrav 					errinf(qstate, "malloc failure, in safety belt, for root");
1618b7579f77SDag-Erling Smørgrav 					return error_response(qstate, id,
1619b7579f77SDag-Erling Smørgrav 						LDNS_RCODE_SERVFAIL);
1620b7579f77SDag-Erling Smørgrav 				}
1621b7579f77SDag-Erling Smørgrav 				break;
1622b7579f77SDag-Erling Smørgrav 			} else {
1623b7579f77SDag-Erling Smørgrav 				verbose(VERB_ALGO,
1624b7579f77SDag-Erling Smørgrav 					"cache delegation was useless:");
1625b7579f77SDag-Erling Smørgrav 				delegpt_log(VERB_ALGO, iq->dp);
1626b7579f77SDag-Erling Smørgrav 				/* go up */
1627b7579f77SDag-Erling Smørgrav 				delname = iq->dp->name;
1628b7579f77SDag-Erling Smørgrav 				delnamelen = iq->dp->namelen;
1629b7579f77SDag-Erling Smørgrav 				dname_remove_label(&delname, &delnamelen);
1630b7579f77SDag-Erling Smørgrav 			}
1631b7579f77SDag-Erling Smørgrav 		} else break;
1632b7579f77SDag-Erling Smørgrav 	}
1633b7579f77SDag-Erling Smørgrav 
1634b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "cache delegation returns delegpt");
1635b7579f77SDag-Erling Smørgrav 	delegpt_log(VERB_ALGO, iq->dp);
1636b7579f77SDag-Erling Smørgrav 
1637b7579f77SDag-Erling Smørgrav 	/* Otherwise, set the current delegation point and move on to the
1638b7579f77SDag-Erling Smørgrav 	 * next state. */
1639b7579f77SDag-Erling Smørgrav 	return next_state(iq, INIT_REQUEST_2_STATE);
1640b7579f77SDag-Erling Smørgrav }
1641b7579f77SDag-Erling Smørgrav 
1642b7579f77SDag-Erling Smørgrav /**
1643b7579f77SDag-Erling Smørgrav  * Process the second part of the initial request handling. This state
1644b7579f77SDag-Erling Smørgrav  * basically exists so that queries that generate root priming events have
1645b7579f77SDag-Erling Smørgrav  * the same init processing as ones that do not. Request events that reach
1646b7579f77SDag-Erling Smørgrav  * this state must have a valid currentDelegationPoint set.
1647b7579f77SDag-Erling Smørgrav  *
16488a384985SDag-Erling Smørgrav  * This part is primarily handling stub zone priming. Events that reach this
1649b7579f77SDag-Erling Smørgrav  * state must have a current delegation point.
1650b7579f77SDag-Erling Smørgrav  *
1651b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1652b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1653b7579f77SDag-Erling Smørgrav  * @param id: module id.
1654b7579f77SDag-Erling Smørgrav  * @return true if the event needs more request processing immediately,
1655b7579f77SDag-Erling Smørgrav  *         false if not.
1656b7579f77SDag-Erling Smørgrav  */
1657b7579f77SDag-Erling Smørgrav static int
1658b7579f77SDag-Erling Smørgrav processInitRequest2(struct module_qstate* qstate, struct iter_qstate* iq,
1659b7579f77SDag-Erling Smørgrav 	int id)
1660b7579f77SDag-Erling Smørgrav {
1661b7579f77SDag-Erling Smørgrav 	uint8_t* delname;
1662b7579f77SDag-Erling Smørgrav 	size_t delnamelen;
1663b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_QUERY, "resolving (init part 2): ",
1664b7579f77SDag-Erling Smørgrav 		&qstate->qinfo);
1665b7579f77SDag-Erling Smørgrav 
166657bddd21SDag-Erling Smørgrav 	delname = iq->qchase.qname;
166757bddd21SDag-Erling Smørgrav 	delnamelen = iq->qchase.qname_len;
1668b7579f77SDag-Erling Smørgrav 	if(iq->refetch_glue) {
166957bddd21SDag-Erling Smørgrav 		struct iter_hints_stub* stub;
1670b7579f77SDag-Erling Smørgrav 		if(!iq->dp) {
1671b7579f77SDag-Erling Smørgrav 			log_err("internal or malloc fail: no dp for refetch");
16724c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, no delegation info");
1673b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1674b7579f77SDag-Erling Smørgrav 		}
167557bddd21SDag-Erling Smørgrav 		/* Do not send queries above stub, do not set delname to dp if
167657bddd21SDag-Erling Smørgrav 		 * this is above stub without stub-first. */
167757bddd21SDag-Erling Smørgrav 		stub = hints_lookup_stub(
167857bddd21SDag-Erling Smørgrav 			qstate->env->hints, iq->qchase.qname, iq->qchase.qclass,
167957bddd21SDag-Erling Smørgrav 			iq->dp);
168057bddd21SDag-Erling Smørgrav 		if(!stub || !stub->dp->has_parent_side_NS ||
168157bddd21SDag-Erling Smørgrav 			dname_subdomain_c(iq->dp->name, stub->dp->name)) {
1682b7579f77SDag-Erling Smørgrav 			delname = iq->dp->name;
1683b7579f77SDag-Erling Smørgrav 			delnamelen = iq->dp->namelen;
168457bddd21SDag-Erling Smørgrav 		}
1685b7579f77SDag-Erling Smørgrav 	}
1686b7579f77SDag-Erling Smørgrav 	if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue) {
1687b7579f77SDag-Erling Smørgrav 		if(!dname_is_root(delname))
1688b7579f77SDag-Erling Smørgrav 			dname_remove_label(&delname, &delnamelen);
1689b7579f77SDag-Erling Smørgrav 		iq->refetch_glue = 0; /* if CNAME causes restart, no refetch */
1690b7579f77SDag-Erling Smørgrav 	}
169157bddd21SDag-Erling Smørgrav 
169257bddd21SDag-Erling Smørgrav 	/* see if we have an auth zone to answer from, improves dp from cache
169357bddd21SDag-Erling Smørgrav 	 * (if any dp from cache) with auth zone dp, if that is lower */
169457bddd21SDag-Erling Smørgrav 	if(!auth_zone_delegpt(qstate, iq, delname, delnamelen))
169557bddd21SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
169657bddd21SDag-Erling Smørgrav 
1697b7579f77SDag-Erling Smørgrav 	/* Check to see if we need to prime a stub zone. */
1698b7579f77SDag-Erling Smørgrav 	if(prime_stub(qstate, iq, id, delname, iq->qchase.qclass)) {
1699b7579f77SDag-Erling Smørgrav 		/* A priming sub request was made */
1700b7579f77SDag-Erling Smørgrav 		return 0;
1701b7579f77SDag-Erling Smørgrav 	}
1702b7579f77SDag-Erling Smørgrav 
1703b7579f77SDag-Erling Smørgrav 	/* most events just get forwarded to the next state. */
1704b7579f77SDag-Erling Smørgrav 	return next_state(iq, INIT_REQUEST_3_STATE);
1705b7579f77SDag-Erling Smørgrav }
1706b7579f77SDag-Erling Smørgrav 
1707b7579f77SDag-Erling Smørgrav /**
1708b7579f77SDag-Erling Smørgrav  * Process the third part of the initial request handling. This state exists
1709b7579f77SDag-Erling Smørgrav  * as a separate state so that queries that generate stub priming events
1710b7579f77SDag-Erling Smørgrav  * will get the tail end of the init process but not repeat the stub priming
1711b7579f77SDag-Erling Smørgrav  * check.
1712b7579f77SDag-Erling Smørgrav  *
1713b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1714b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1715b7579f77SDag-Erling Smørgrav  * @param id: module id.
1716b7579f77SDag-Erling Smørgrav  * @return true, advancing the event to the QUERYTARGETS_STATE.
1717b7579f77SDag-Erling Smørgrav  */
1718b7579f77SDag-Erling Smørgrav static int
1719b7579f77SDag-Erling Smørgrav processInitRequest3(struct module_qstate* qstate, struct iter_qstate* iq,
1720b7579f77SDag-Erling Smørgrav 	int id)
1721b7579f77SDag-Erling Smørgrav {
1722b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_QUERY, "resolving (init part 3): ",
1723b7579f77SDag-Erling Smørgrav 		&qstate->qinfo);
1724b7579f77SDag-Erling Smørgrav 	/* if the cache reply dp equals a validation anchor or msg has DS,
1725b7579f77SDag-Erling Smørgrav 	 * then DNSSEC RRSIGs are expected in the reply */
1726b7579f77SDag-Erling Smørgrav 	iq->dnssec_expected = iter_indicates_dnssec(qstate->env, iq->dp,
1727b7579f77SDag-Erling Smørgrav 		iq->deleg_msg, iq->qchase.qclass);
1728b7579f77SDag-Erling Smørgrav 
1729b7579f77SDag-Erling Smørgrav 	/* If the RD flag wasn't set, then we just finish with the
1730b7579f77SDag-Erling Smørgrav 	 * cached referral as the response. */
173165b390aaSDag-Erling Smørgrav 	if(!(qstate->query_flags & BIT_RD) && iq->deleg_msg) {
1732b7579f77SDag-Erling Smørgrav 		iq->response = iq->deleg_msg;
173317d15b25SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO && iq->response)
1734b7579f77SDag-Erling Smørgrav 			log_dns_msg("no RD requested, using delegation msg",
1735b7579f77SDag-Erling Smørgrav 				&iq->response->qinfo, iq->response->rep);
1736b7579f77SDag-Erling Smørgrav 		if(qstate->reply_origin)
1737b7579f77SDag-Erling Smørgrav 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1738b7579f77SDag-Erling Smørgrav 		return final_state(iq);
1739b7579f77SDag-Erling Smørgrav 	}
1740b7579f77SDag-Erling Smørgrav 	/* After this point, unset the RD flag -- this query is going to
1741b7579f77SDag-Erling Smørgrav 	 * be sent to an auth. server. */
1742b7579f77SDag-Erling Smørgrav 	iq->chase_flags &= ~BIT_RD;
1743b7579f77SDag-Erling Smørgrav 
1744b7579f77SDag-Erling Smørgrav 	/* if dnssec expected, fetch key for the trust-anchor or cached-DS */
1745b7579f77SDag-Erling Smørgrav 	if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
1746b7579f77SDag-Erling Smørgrav 		!(qstate->query_flags&BIT_CD)) {
1747b7579f77SDag-Erling Smørgrav 		generate_dnskey_prefetch(qstate, iq, id);
1748b7579f77SDag-Erling Smørgrav 		fptr_ok(fptr_whitelist_modenv_detach_subs(
1749b7579f77SDag-Erling Smørgrav 			qstate->env->detach_subs));
1750b7579f77SDag-Erling Smørgrav 		(*qstate->env->detach_subs)(qstate);
1751b7579f77SDag-Erling Smørgrav 	}
1752b7579f77SDag-Erling Smørgrav 
1753b7579f77SDag-Erling Smørgrav 	/* Jump to the next state. */
1754b7579f77SDag-Erling Smørgrav 	return next_state(iq, QUERYTARGETS_STATE);
1755b7579f77SDag-Erling Smørgrav }
1756b7579f77SDag-Erling Smørgrav 
1757b7579f77SDag-Erling Smørgrav /**
1758b7579f77SDag-Erling Smørgrav  * Given a basic query, generate a parent-side "target" query.
1759b7579f77SDag-Erling Smørgrav  * These are subordinate queries for missing delegation point target addresses,
1760b7579f77SDag-Erling Smørgrav  * for which only the parent of the delegation provides correct IP addresses.
1761b7579f77SDag-Erling Smørgrav  *
1762b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1763b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1764b7579f77SDag-Erling Smørgrav  * @param id: module id.
1765b7579f77SDag-Erling Smørgrav  * @param name: target qname.
1766b7579f77SDag-Erling Smørgrav  * @param namelen: target qname length.
1767b7579f77SDag-Erling Smørgrav  * @param qtype: target qtype (either A or AAAA).
1768b7579f77SDag-Erling Smørgrav  * @param qclass: target qclass.
1769b7579f77SDag-Erling Smørgrav  * @return true on success, false on failure.
1770b7579f77SDag-Erling Smørgrav  */
1771b7579f77SDag-Erling Smørgrav static int
1772b7579f77SDag-Erling Smørgrav generate_parentside_target_query(struct module_qstate* qstate,
1773b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq, int id, uint8_t* name, size_t namelen,
1774b7579f77SDag-Erling Smørgrav 	uint16_t qtype, uint16_t qclass)
1775b7579f77SDag-Erling Smørgrav {
1776b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
1777b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1778091e9e46SCy Schubert 		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0))
1779b7579f77SDag-Erling Smørgrav 		return 0;
1780b7579f77SDag-Erling Smørgrav 	if(subq) {
1781b7579f77SDag-Erling Smørgrav 		struct iter_qstate* subiq =
1782b7579f77SDag-Erling Smørgrav 			(struct iter_qstate*)subq->minfo[id];
1783b7579f77SDag-Erling Smørgrav 		/* blacklist the cache - we want to fetch parent stuff */
1784b7579f77SDag-Erling Smørgrav 		sock_list_insert(&subq->blacklist, NULL, 0, subq->region);
1785b7579f77SDag-Erling Smørgrav 		subiq->query_for_pside_glue = 1;
1786b7579f77SDag-Erling Smørgrav 		if(dname_subdomain_c(name, iq->dp->name)) {
1787b7579f77SDag-Erling Smørgrav 			subiq->dp = delegpt_copy(iq->dp, subq->region);
1788b7579f77SDag-Erling Smørgrav 			subiq->dnssec_expected = iter_indicates_dnssec(
1789b7579f77SDag-Erling Smørgrav 				qstate->env, subiq->dp, NULL,
1790b7579f77SDag-Erling Smørgrav 				subq->qinfo.qclass);
1791b7579f77SDag-Erling Smørgrav 			subiq->refetch_glue = 1;
1792b7579f77SDag-Erling Smørgrav 		} else {
1793b7579f77SDag-Erling Smørgrav 			subiq->dp = dns_cache_find_delegation(qstate->env,
1794b7579f77SDag-Erling Smørgrav 				name, namelen, qtype, qclass, subq->region,
1795b7579f77SDag-Erling Smørgrav 				&subiq->deleg_msg,
1796790c6b24SCy Schubert 				*qstate->env->now+subq->prefetch_leeway,
1797790c6b24SCy Schubert 				1, NULL, 0);
1798b7579f77SDag-Erling Smørgrav 			/* if no dp, then it's from root, refetch unneeded */
1799b7579f77SDag-Erling Smørgrav 			if(subiq->dp) {
1800b7579f77SDag-Erling Smørgrav 				subiq->dnssec_expected = iter_indicates_dnssec(
1801b7579f77SDag-Erling Smørgrav 					qstate->env, subiq->dp, NULL,
1802b7579f77SDag-Erling Smørgrav 					subq->qinfo.qclass);
1803b7579f77SDag-Erling Smørgrav 				subiq->refetch_glue = 1;
1804b7579f77SDag-Erling Smørgrav 			}
1805b7579f77SDag-Erling Smørgrav 		}
1806b7579f77SDag-Erling Smørgrav 	}
1807b7579f77SDag-Erling Smørgrav 	log_nametypeclass(VERB_QUERY, "new pside target", name, qtype, qclass);
1808b7579f77SDag-Erling Smørgrav 	return 1;
1809b7579f77SDag-Erling Smørgrav }
1810b7579f77SDag-Erling Smørgrav 
1811b7579f77SDag-Erling Smørgrav /**
1812b7579f77SDag-Erling Smørgrav  * Given a basic query, generate a "target" query. These are subordinate
1813b7579f77SDag-Erling Smørgrav  * queries for missing delegation point target addresses.
1814b7579f77SDag-Erling Smørgrav  *
1815b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1816b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1817b7579f77SDag-Erling Smørgrav  * @param id: module id.
1818b7579f77SDag-Erling Smørgrav  * @param name: target qname.
1819b7579f77SDag-Erling Smørgrav  * @param namelen: target qname length.
1820b7579f77SDag-Erling Smørgrav  * @param qtype: target qtype (either A or AAAA).
1821b7579f77SDag-Erling Smørgrav  * @param qclass: target qclass.
1822b7579f77SDag-Erling Smørgrav  * @return true on success, false on failure.
1823b7579f77SDag-Erling Smørgrav  */
1824b7579f77SDag-Erling Smørgrav static int
1825b7579f77SDag-Erling Smørgrav generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
1826b7579f77SDag-Erling Smørgrav         int id, uint8_t* name, size_t namelen, uint16_t qtype, uint16_t qclass)
1827b7579f77SDag-Erling Smørgrav {
1828b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
1829b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1830091e9e46SCy Schubert 		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0))
1831b7579f77SDag-Erling Smørgrav 		return 0;
1832b7579f77SDag-Erling Smørgrav 	log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
1833b7579f77SDag-Erling Smørgrav 	return 1;
1834b7579f77SDag-Erling Smørgrav }
1835b7579f77SDag-Erling Smørgrav 
1836b7579f77SDag-Erling Smørgrav /**
1837b7579f77SDag-Erling Smørgrav  * Given an event at a certain state, generate zero or more target queries
1838b7579f77SDag-Erling Smørgrav  * for it's current delegation point.
1839b7579f77SDag-Erling Smørgrav  *
1840b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1841b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1842b7579f77SDag-Erling Smørgrav  * @param ie: iterator shared global environment.
1843b7579f77SDag-Erling Smørgrav  * @param id: module id.
1844b7579f77SDag-Erling Smørgrav  * @param maxtargets: The maximum number of targets to query for.
1845b7579f77SDag-Erling Smørgrav  *	if it is negative, there is no maximum number of targets.
1846b7579f77SDag-Erling Smørgrav  * @param num: returns the number of queries generated and processed,
1847b7579f77SDag-Erling Smørgrav  *	which may be zero if there were no missing targets.
1848b7579f77SDag-Erling Smørgrav  * @return false on error.
1849b7579f77SDag-Erling Smørgrav  */
1850b7579f77SDag-Erling Smørgrav static int
1851b7579f77SDag-Erling Smørgrav query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
1852b7579f77SDag-Erling Smørgrav         struct iter_env* ie, int id, int maxtargets, int* num)
1853b7579f77SDag-Erling Smørgrav {
1854b7579f77SDag-Erling Smørgrav 	int query_count = 0;
1855b7579f77SDag-Erling Smørgrav 	struct delegpt_ns* ns;
1856b7579f77SDag-Erling Smørgrav 	int missing;
1857b7579f77SDag-Erling Smørgrav 	int toget = 0;
1858b7579f77SDag-Erling Smørgrav 
1859a39a5a69SCy Schubert 	iter_mark_cycle_targets(qstate, iq->dp);
18600a92a9fcSCy Schubert 	missing = (int)delegpt_count_missing_targets(iq->dp, NULL);
1861a39a5a69SCy Schubert 	log_assert(maxtargets != 0); /* that would not be useful */
1862a39a5a69SCy Schubert 
1863a39a5a69SCy Schubert 	/* Generate target requests. Basically, any missing targets
1864a39a5a69SCy Schubert 	 * are queried for here, regardless if it is necessary to do
1865a39a5a69SCy Schubert 	 * so to continue processing. */
1866a39a5a69SCy Schubert 	if(maxtargets < 0 || maxtargets > missing)
1867a39a5a69SCy Schubert 		toget = missing;
1868a39a5a69SCy Schubert 	else	toget = maxtargets;
1869a39a5a69SCy Schubert 	if(toget == 0) {
1870a39a5a69SCy Schubert 		*num = 0;
1871a39a5a69SCy Schubert 		return 1;
1872a39a5a69SCy Schubert 	}
1873a39a5a69SCy Schubert 
1874a39a5a69SCy Schubert 	/* now that we are sure that a target query is going to be made,
1875a39a5a69SCy Schubert 	 * check the limits. */
1876b7579f77SDag-Erling Smørgrav 	if(iq->depth == ie->max_dependency_depth)
1877b7579f77SDag-Erling Smørgrav 		return 0;
187852df462fSXin LI 	if(iq->depth > 0 && iq->target_count &&
18790a92a9fcSCy Schubert 		iq->target_count[TARGET_COUNT_QUERIES] > MAX_TARGET_COUNT) {
18806480faa8SDag-Erling Smørgrav 		char s[LDNS_MAX_DOMAINLEN+1];
18816480faa8SDag-Erling Smørgrav 		dname_str(qstate->qinfo.qname, s);
18826480faa8SDag-Erling Smørgrav 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
18830a92a9fcSCy Schubert 			"number of glue fetches %d", s,
18840a92a9fcSCy Schubert 			iq->target_count[TARGET_COUNT_QUERIES]);
188552df462fSXin LI 		return 0;
188652df462fSXin LI 	}
1887091e9e46SCy Schubert 	if(iq->dp_target_count > MAX_DP_TARGET_COUNT) {
1888091e9e46SCy Schubert 		char s[LDNS_MAX_DOMAINLEN+1];
1889091e9e46SCy Schubert 		dname_str(qstate->qinfo.qname, s);
1890091e9e46SCy Schubert 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
1891091e9e46SCy Schubert 			"number of glue fetches %d to a single delegation point",
1892091e9e46SCy Schubert 			s, iq->dp_target_count);
1893091e9e46SCy Schubert 		return 0;
1894091e9e46SCy Schubert 	}
1895b7579f77SDag-Erling Smørgrav 
1896b7579f77SDag-Erling Smørgrav 	/* select 'toget' items from the total of 'missing' items */
1897b7579f77SDag-Erling Smørgrav 	log_assert(toget <= missing);
1898b7579f77SDag-Erling Smørgrav 
1899b7579f77SDag-Erling Smørgrav 	/* loop over missing targets */
1900b7579f77SDag-Erling Smørgrav 	for(ns = iq->dp->nslist; ns; ns = ns->next) {
1901b7579f77SDag-Erling Smørgrav 		if(ns->resolved)
1902b7579f77SDag-Erling Smørgrav 			continue;
1903b7579f77SDag-Erling Smørgrav 
1904b7579f77SDag-Erling Smørgrav 		/* randomly select this item with probability toget/missing */
1905b7579f77SDag-Erling Smørgrav 		if(!iter_ns_probability(qstate->env->rnd, toget, missing)) {
1906b7579f77SDag-Erling Smørgrav 			/* do not select this one, next; select toget number
1907b7579f77SDag-Erling Smørgrav 			 * of items from a list one less in size */
1908b7579f77SDag-Erling Smørgrav 			missing --;
1909b7579f77SDag-Erling Smørgrav 			continue;
1910b7579f77SDag-Erling Smørgrav 		}
1911b7579f77SDag-Erling Smørgrav 
19120a92a9fcSCy Schubert 		if(ie->supports_ipv6 &&
19130a92a9fcSCy Schubert 			((ns->lame && !ns->done_pside6) ||
19140a92a9fcSCy Schubert 			(!ns->lame && !ns->got6))) {
1915b7579f77SDag-Erling Smørgrav 			/* Send the AAAA request. */
1916b7579f77SDag-Erling Smørgrav 			if(!generate_target_query(qstate, iq, id,
1917b7579f77SDag-Erling Smørgrav 				ns->name, ns->namelen,
1918b7579f77SDag-Erling Smørgrav 				LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
1919b7579f77SDag-Erling Smørgrav 				*num = query_count;
1920b7579f77SDag-Erling Smørgrav 				if(query_count > 0)
1921b7579f77SDag-Erling Smørgrav 					qstate->ext_state[id] = module_wait_subquery;
1922b7579f77SDag-Erling Smørgrav 				return 0;
1923b7579f77SDag-Erling Smørgrav 			}
1924b7579f77SDag-Erling Smørgrav 			query_count++;
19254f5c8956SCy Schubert 			/* If the mesh query list is full, exit the loop here.
19264f5c8956SCy Schubert 			 * This makes the routine spawn one query at a time,
19274f5c8956SCy Schubert 			 * and this means there is no query state load
19284f5c8956SCy Schubert 			 * increase, because the spawned state uses cpu and a
19294f5c8956SCy Schubert 			 * socket while this state waits for that spawned
19304f5c8956SCy Schubert 			 * state. Next time we can look up further targets */
19314f5c8956SCy Schubert 			if(mesh_jostle_exceeded(qstate->env->mesh))
19324f5c8956SCy Schubert 				break;
1933b7579f77SDag-Erling Smørgrav 		}
1934b7579f77SDag-Erling Smørgrav 		/* Send the A request. */
19350a92a9fcSCy Schubert 		if(ie->supports_ipv4 &&
19360a92a9fcSCy Schubert 			((ns->lame && !ns->done_pside4) ||
19370a92a9fcSCy Schubert 			(!ns->lame && !ns->got4))) {
1938b7579f77SDag-Erling Smørgrav 			if(!generate_target_query(qstate, iq, id,
1939b7579f77SDag-Erling Smørgrav 				ns->name, ns->namelen,
1940b7579f77SDag-Erling Smørgrav 				LDNS_RR_TYPE_A, iq->qchase.qclass)) {
1941b7579f77SDag-Erling Smørgrav 				*num = query_count;
1942b7579f77SDag-Erling Smørgrav 				if(query_count > 0)
1943b7579f77SDag-Erling Smørgrav 					qstate->ext_state[id] = module_wait_subquery;
1944b7579f77SDag-Erling Smørgrav 				return 0;
1945b7579f77SDag-Erling Smørgrav 			}
1946b7579f77SDag-Erling Smørgrav 			query_count++;
19474f5c8956SCy Schubert 			/* If the mesh query list is full, exit the loop. */
19484f5c8956SCy Schubert 			if(mesh_jostle_exceeded(qstate->env->mesh))
19494f5c8956SCy Schubert 				break;
1950b7579f77SDag-Erling Smørgrav 		}
1951b7579f77SDag-Erling Smørgrav 
1952b7579f77SDag-Erling Smørgrav 		/* mark this target as in progress. */
1953b7579f77SDag-Erling Smørgrav 		ns->resolved = 1;
1954b7579f77SDag-Erling Smørgrav 		missing--;
1955b7579f77SDag-Erling Smørgrav 		toget--;
1956b7579f77SDag-Erling Smørgrav 		if(toget == 0)
1957b7579f77SDag-Erling Smørgrav 			break;
1958b7579f77SDag-Erling Smørgrav 	}
1959b7579f77SDag-Erling Smørgrav 	*num = query_count;
1960b7579f77SDag-Erling Smørgrav 	if(query_count > 0)
1961b7579f77SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_subquery;
1962b7579f77SDag-Erling Smørgrav 
1963b7579f77SDag-Erling Smørgrav 	return 1;
1964b7579f77SDag-Erling Smørgrav }
1965b7579f77SDag-Erling Smørgrav 
1966b7579f77SDag-Erling Smørgrav /**
1967b7579f77SDag-Erling Smørgrav  * Called by processQueryTargets when it would like extra targets to query
1968b7579f77SDag-Erling Smørgrav  * but it seems to be out of options.  At last resort some less appealing
1969b7579f77SDag-Erling Smørgrav  * options are explored.  If there are no more options, the result is SERVFAIL
1970b7579f77SDag-Erling Smørgrav  *
1971b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
1972b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
1973b7579f77SDag-Erling Smørgrav  * @param ie: iterator shared global environment.
1974b7579f77SDag-Erling Smørgrav  * @param id: module id.
1975b7579f77SDag-Erling Smørgrav  * @return true if the event requires more request processing immediately,
1976b7579f77SDag-Erling Smørgrav  *         false if not.
1977b7579f77SDag-Erling Smørgrav  */
1978b7579f77SDag-Erling Smørgrav static int
1979b7579f77SDag-Erling Smørgrav processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
1980b7579f77SDag-Erling Smørgrav 	struct iter_env* ie, int id)
1981b7579f77SDag-Erling Smørgrav {
1982b7579f77SDag-Erling Smørgrav 	struct delegpt_ns* ns;
1983b7579f77SDag-Erling Smørgrav 	int query_count = 0;
1984b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "No more query targets, attempting last resort");
1985b7579f77SDag-Erling Smørgrav 	log_assert(iq->dp);
1986b7579f77SDag-Erling Smørgrav 
1987c7f4d7adSDag-Erling Smørgrav 	if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen,
19887da0adf7SDag-Erling Smørgrav 		iq->qchase.qclass, NULL)) {
198917d15b25SDag-Erling Smørgrav 		/* fail -- no more targets, no more hope of targets, no hope
199017d15b25SDag-Erling Smørgrav 		 * of a response. */
19914c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "all the configured stub or forward servers failed,");
19924c75e3aaSDag-Erling Smørgrav 		errinf_dname(qstate, "at zone", iq->dp->name);
19935469a995SCy Schubert 		errinf_reply(qstate, iq);
1994c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_QUERY, "configured stub or forward servers failed -- returning SERVFAIL");
199517d15b25SDag-Erling Smørgrav 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
199617d15b25SDag-Erling Smørgrav 	}
19978ed2b524SDag-Erling Smørgrav 	if(!iq->dp->has_parent_side_NS && dname_is_root(iq->dp->name)) {
19988ed2b524SDag-Erling Smørgrav 		struct delegpt* p = hints_lookup_root(qstate->env->hints,
19998ed2b524SDag-Erling Smørgrav 			iq->qchase.qclass);
20008ed2b524SDag-Erling Smørgrav 		if(p) {
20018ed2b524SDag-Erling Smørgrav 			struct delegpt_addr* a;
20028ed2b524SDag-Erling Smørgrav 			iq->chase_flags &= ~BIT_RD; /* go to authorities */
20038ed2b524SDag-Erling Smørgrav 			for(ns = p->nslist; ns; ns=ns->next) {
20048ed2b524SDag-Erling Smørgrav 				(void)delegpt_add_ns(iq->dp, qstate->region,
20059cf5bc93SCy Schubert 					ns->name, ns->lame, ns->tls_auth_name,
20069cf5bc93SCy Schubert 					ns->port);
20078ed2b524SDag-Erling Smørgrav 			}
20088ed2b524SDag-Erling Smørgrav 			for(a = p->target_list; a; a=a->next_target) {
20098ed2b524SDag-Erling Smørgrav 				(void)delegpt_add_addr(iq->dp, qstate->region,
20108ed2b524SDag-Erling Smørgrav 					&a->addr, a->addrlen, a->bogus,
20119cf5bc93SCy Schubert 					a->lame, a->tls_auth_name, -1, NULL);
20128ed2b524SDag-Erling Smørgrav 			}
20138ed2b524SDag-Erling Smørgrav 		}
20148ed2b524SDag-Erling Smørgrav 		iq->dp->has_parent_side_NS = 1;
20158ed2b524SDag-Erling Smørgrav 	} else if(!iq->dp->has_parent_side_NS) {
2016b7579f77SDag-Erling Smørgrav 		if(!iter_lookup_parent_NS_from_cache(qstate->env, iq->dp,
2017b7579f77SDag-Erling Smørgrav 			qstate->region, &qstate->qinfo)
2018b7579f77SDag-Erling Smørgrav 			|| !iq->dp->has_parent_side_NS) {
2019b7579f77SDag-Erling Smørgrav 			/* if: malloc failure in lookup go up to try */
2020b7579f77SDag-Erling Smørgrav 			/* if: no parent NS in cache - go up one level */
2021b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "try to grab parent NS");
2022b7579f77SDag-Erling Smørgrav 			iq->store_parent_NS = iq->dp;
20238ed2b524SDag-Erling Smørgrav 			iq->chase_flags &= ~BIT_RD; /* go to authorities */
2024b7579f77SDag-Erling Smørgrav 			iq->deleg_msg = NULL;
2025b7579f77SDag-Erling Smørgrav 			iq->refetch_glue = 1;
2026b7579f77SDag-Erling Smørgrav 			iq->query_restart_count++;
2027b7579f77SDag-Erling Smørgrav 			iq->sent_count = 0;
2028091e9e46SCy Schubert 			iq->dp_target_count = 0;
202905ab2901SDag-Erling Smørgrav 			if(qstate->env->cfg->qname_minimisation)
203005ab2901SDag-Erling Smørgrav 				iq->minimisation_state = INIT_MINIMISE_STATE;
2031b7579f77SDag-Erling Smørgrav 			return next_state(iq, INIT_REQUEST_STATE);
2032b7579f77SDag-Erling Smørgrav 		}
2033b7579f77SDag-Erling Smørgrav 	}
2034b7579f77SDag-Erling Smørgrav 	/* see if that makes new names available */
2035b7579f77SDag-Erling Smørgrav 	if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
2036b7579f77SDag-Erling Smørgrav 		qstate->region, iq->dp))
2037b7579f77SDag-Erling Smørgrav 		log_err("out of memory in cache_fill_missing");
2038b7579f77SDag-Erling Smørgrav 	if(iq->dp->usable_list) {
2039b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "try parent-side-name, w. glue from cache");
2040b7579f77SDag-Erling Smørgrav 		return next_state(iq, QUERYTARGETS_STATE);
2041b7579f77SDag-Erling Smørgrav 	}
2042b7579f77SDag-Erling Smørgrav 	/* try to fill out parent glue from cache */
2043b7579f77SDag-Erling Smørgrav 	if(iter_lookup_parent_glue_from_cache(qstate->env, iq->dp,
2044b7579f77SDag-Erling Smørgrav 		qstate->region, &qstate->qinfo)) {
2045b7579f77SDag-Erling Smørgrav 		/* got parent stuff from cache, see if we can continue */
2046b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "try parent-side glue from cache");
2047b7579f77SDag-Erling Smørgrav 		return next_state(iq, QUERYTARGETS_STATE);
2048b7579f77SDag-Erling Smørgrav 	}
2049b7579f77SDag-Erling Smørgrav 	/* query for an extra name added by the parent-NS record */
20500a92a9fcSCy Schubert 	if(delegpt_count_missing_targets(iq->dp, NULL) > 0) {
2051b7579f77SDag-Erling Smørgrav 		int qs = 0;
2052b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "try parent-side target name");
2053b7579f77SDag-Erling Smørgrav 		if(!query_for_targets(qstate, iq, ie, id, 1, &qs)) {
20544c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "could not fetch nameserver");
20554c75e3aaSDag-Erling Smørgrav 			errinf_dname(qstate, "at zone", iq->dp->name);
2056b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2057b7579f77SDag-Erling Smørgrav 		}
2058b7579f77SDag-Erling Smørgrav 		iq->num_target_queries += qs;
205952df462fSXin LI 		target_count_increase(iq, qs);
2060b7579f77SDag-Erling Smørgrav 		if(qs != 0) {
2061b7579f77SDag-Erling Smørgrav 			qstate->ext_state[id] = module_wait_subquery;
2062b7579f77SDag-Erling Smørgrav 			return 0; /* and wait for them */
2063b7579f77SDag-Erling Smørgrav 		}
2064b7579f77SDag-Erling Smørgrav 	}
2065b7579f77SDag-Erling Smørgrav 	if(iq->depth == ie->max_dependency_depth) {
2066b7579f77SDag-Erling Smørgrav 		verbose(VERB_QUERY, "maxdepth and need more nameservers, fail");
20674c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "cannot fetch more nameservers because at max dependency depth");
2068b7579f77SDag-Erling Smørgrav 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
2069b7579f77SDag-Erling Smørgrav 	}
207052df462fSXin LI 	if(iq->depth > 0 && iq->target_count &&
20710a92a9fcSCy Schubert 		iq->target_count[TARGET_COUNT_QUERIES] > MAX_TARGET_COUNT) {
20726480faa8SDag-Erling Smørgrav 		char s[LDNS_MAX_DOMAINLEN+1];
20736480faa8SDag-Erling Smørgrav 		dname_str(qstate->qinfo.qname, s);
20746480faa8SDag-Erling Smørgrav 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
20750a92a9fcSCy Schubert 			"number of glue fetches %d", s,
20760a92a9fcSCy Schubert 			iq->target_count[TARGET_COUNT_QUERIES]);
20774c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "exceeded the maximum number of glue fetches");
207852df462fSXin LI 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
207952df462fSXin LI 	}
2080b7579f77SDag-Erling Smørgrav 	/* mark cycle targets for parent-side lookups */
2081b7579f77SDag-Erling Smørgrav 	iter_mark_pside_cycle_targets(qstate, iq->dp);
2082b7579f77SDag-Erling Smørgrav 	/* see if we can issue queries to get nameserver addresses */
2083b7579f77SDag-Erling Smørgrav 	/* this lookup is not randomized, but sequential. */
2084b7579f77SDag-Erling Smørgrav 	for(ns = iq->dp->nslist; ns; ns = ns->next) {
2085c7f4d7adSDag-Erling Smørgrav 		/* if this nameserver is at a delegation point, but that
2086c7f4d7adSDag-Erling Smørgrav 		 * delegation point is a stub and we cannot go higher, skip*/
2087c7f4d7adSDag-Erling Smørgrav 		if( ((ie->supports_ipv6 && !ns->done_pside6) ||
2088c7f4d7adSDag-Erling Smørgrav 		    (ie->supports_ipv4 && !ns->done_pside4)) &&
2089c7f4d7adSDag-Erling Smørgrav 		    !can_have_last_resort(qstate->env, ns->name, ns->namelen,
20907da0adf7SDag-Erling Smørgrav 			iq->qchase.qclass, NULL)) {
2091c7f4d7adSDag-Erling Smørgrav 			log_nametypeclass(VERB_ALGO, "cannot pside lookup ns "
2092c7f4d7adSDag-Erling Smørgrav 				"because it is also a stub/forward,",
2093c7f4d7adSDag-Erling Smørgrav 				ns->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
2094c7f4d7adSDag-Erling Smørgrav 			if(ie->supports_ipv6) ns->done_pside6 = 1;
2095c7f4d7adSDag-Erling Smørgrav 			if(ie->supports_ipv4) ns->done_pside4 = 1;
2096c7f4d7adSDag-Erling Smørgrav 			continue;
2097c7f4d7adSDag-Erling Smørgrav 		}
2098b7579f77SDag-Erling Smørgrav 		/* query for parent-side A and AAAA for nameservers */
2099b7579f77SDag-Erling Smørgrav 		if(ie->supports_ipv6 && !ns->done_pside6) {
2100b7579f77SDag-Erling Smørgrav 			/* Send the AAAA request. */
2101b7579f77SDag-Erling Smørgrav 			if(!generate_parentside_target_query(qstate, iq, id,
2102b7579f77SDag-Erling Smørgrav 				ns->name, ns->namelen,
21034c75e3aaSDag-Erling Smørgrav 				LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
21044c75e3aaSDag-Erling Smørgrav 				errinf_dname(qstate, "could not generate nameserver AAAA lookup for", ns->name);
2105b7579f77SDag-Erling Smørgrav 				return error_response(qstate, id,
2106b7579f77SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
21074c75e3aaSDag-Erling Smørgrav 			}
2108b7579f77SDag-Erling Smørgrav 			ns->done_pside6 = 1;
2109b7579f77SDag-Erling Smørgrav 			query_count++;
21104f5c8956SCy Schubert 			if(mesh_jostle_exceeded(qstate->env->mesh)) {
21114f5c8956SCy Schubert 				/* Wait for the lookup; do not spawn multiple
21124f5c8956SCy Schubert 				 * lookups at a time. */
21134f5c8956SCy Schubert 				verbose(VERB_ALGO, "try parent-side glue lookup");
21144f5c8956SCy Schubert 				iq->num_target_queries += query_count;
21154f5c8956SCy Schubert 				target_count_increase(iq, query_count);
21164f5c8956SCy Schubert 				qstate->ext_state[id] = module_wait_subquery;
21174f5c8956SCy Schubert 				return 0;
21184f5c8956SCy Schubert 			}
2119b7579f77SDag-Erling Smørgrav 		}
2120b7579f77SDag-Erling Smørgrav 		if(ie->supports_ipv4 && !ns->done_pside4) {
2121b7579f77SDag-Erling Smørgrav 			/* Send the A request. */
2122b7579f77SDag-Erling Smørgrav 			if(!generate_parentside_target_query(qstate, iq, id,
2123b7579f77SDag-Erling Smørgrav 				ns->name, ns->namelen,
21244c75e3aaSDag-Erling Smørgrav 				LDNS_RR_TYPE_A, iq->qchase.qclass)) {
21254c75e3aaSDag-Erling Smørgrav 				errinf_dname(qstate, "could not generate nameserver A lookup for", ns->name);
2126b7579f77SDag-Erling Smørgrav 				return error_response(qstate, id,
2127b7579f77SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
21284c75e3aaSDag-Erling Smørgrav 			}
2129b7579f77SDag-Erling Smørgrav 			ns->done_pside4 = 1;
2130b7579f77SDag-Erling Smørgrav 			query_count++;
2131b7579f77SDag-Erling Smørgrav 		}
2132b7579f77SDag-Erling Smørgrav 		if(query_count != 0) { /* suspend to await results */
2133b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "try parent-side glue lookup");
2134b7579f77SDag-Erling Smørgrav 			iq->num_target_queries += query_count;
213552df462fSXin LI 			target_count_increase(iq, query_count);
2136b7579f77SDag-Erling Smørgrav 			qstate->ext_state[id] = module_wait_subquery;
2137b7579f77SDag-Erling Smørgrav 			return 0;
2138b7579f77SDag-Erling Smørgrav 		}
2139b7579f77SDag-Erling Smørgrav 	}
2140b7579f77SDag-Erling Smørgrav 
2141b7579f77SDag-Erling Smørgrav 	/* if this was a parent-side glue query itself, then store that
2142b7579f77SDag-Erling Smørgrav 	 * failure in cache. */
2143bc892140SDag-Erling Smørgrav 	if(!qstate->no_cache_store && iq->query_for_pside_glue
2144bc892140SDag-Erling Smørgrav 		&& !iq->pside_glue)
2145b7579f77SDag-Erling Smørgrav 			iter_store_parentside_neg(qstate->env, &qstate->qinfo,
2146b7579f77SDag-Erling Smørgrav 				iq->deleg_msg?iq->deleg_msg->rep:
2147b7579f77SDag-Erling Smørgrav 				(iq->response?iq->response->rep:NULL));
2148b7579f77SDag-Erling Smørgrav 
21494c75e3aaSDag-Erling Smørgrav 	errinf(qstate, "all servers for this domain failed,");
21504c75e3aaSDag-Erling Smørgrav 	errinf_dname(qstate, "at zone", iq->dp->name);
21515469a995SCy Schubert 	errinf_reply(qstate, iq);
2152b7579f77SDag-Erling Smørgrav 	verbose(VERB_QUERY, "out of query targets -- returning SERVFAIL");
2153b7579f77SDag-Erling Smørgrav 	/* fail -- no more targets, no more hope of targets, no hope
2154b7579f77SDag-Erling Smørgrav 	 * of a response. */
2155b7579f77SDag-Erling Smørgrav 	return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
2156b7579f77SDag-Erling Smørgrav }
2157b7579f77SDag-Erling Smørgrav 
2158b7579f77SDag-Erling Smørgrav /**
2159b7579f77SDag-Erling Smørgrav  * Try to find the NS record set that will resolve a qtype DS query. Due
2160b7579f77SDag-Erling Smørgrav  * to grandparent/grandchild reasons we did not get a proper lookup right
2161b7579f77SDag-Erling Smørgrav  * away.  We need to create type NS queries until we get the right parent
2162b7579f77SDag-Erling Smørgrav  * for this lookup.  We remove labels from the query to find the right point.
2163b7579f77SDag-Erling Smørgrav  * If we end up at the old dp name, then there is no solution.
2164b7579f77SDag-Erling Smørgrav  *
2165b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
2166b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
2167b7579f77SDag-Erling Smørgrav  * @param id: module id.
2168b7579f77SDag-Erling Smørgrav  * @return true if the event requires more immediate processing, false if
2169b7579f77SDag-Erling Smørgrav  *         not. This is generally only true when forwarding the request to
2170b7579f77SDag-Erling Smørgrav  *         the final state (i.e., on answer).
2171b7579f77SDag-Erling Smørgrav  */
2172b7579f77SDag-Erling Smørgrav static int
21738ed2b524SDag-Erling Smørgrav processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
2174b7579f77SDag-Erling Smørgrav {
2175b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq = NULL;
2176b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "processDSNSFind");
2177b7579f77SDag-Erling Smørgrav 
2178b7579f77SDag-Erling Smørgrav 	if(!iq->dsns_point) {
2179b7579f77SDag-Erling Smørgrav 		/* initialize */
2180b7579f77SDag-Erling Smørgrav 		iq->dsns_point = iq->qchase.qname;
2181b7579f77SDag-Erling Smørgrav 		iq->dsns_point_len = iq->qchase.qname_len;
2182b7579f77SDag-Erling Smørgrav 	}
2183b7579f77SDag-Erling Smørgrav 	/* robustcheck for internal error: we are not underneath the dp */
2184b7579f77SDag-Erling Smørgrav 	if(!dname_subdomain_c(iq->dsns_point, iq->dp->name)) {
21854c75e3aaSDag-Erling Smørgrav 		errinf_dname(qstate, "for DS query parent-child nameserver search the query is not under the zone", iq->dp->name);
2186b7579f77SDag-Erling Smørgrav 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
2187b7579f77SDag-Erling Smørgrav 	}
2188b7579f77SDag-Erling Smørgrav 
2189b7579f77SDag-Erling Smørgrav 	/* go up one (more) step, until we hit the dp, if so, end */
2190b7579f77SDag-Erling Smørgrav 	dname_remove_label(&iq->dsns_point, &iq->dsns_point_len);
2191b7579f77SDag-Erling Smørgrav 	if(query_dname_compare(iq->dsns_point, iq->dp->name) == 0) {
2192b7579f77SDag-Erling Smørgrav 		/* there was no inbetween nameserver, use the old delegation
2193b7579f77SDag-Erling Smørgrav 		 * point again.  And this time, because dsns_point is nonNULL
2194b7579f77SDag-Erling Smørgrav 		 * we are going to accept the (bad) result */
2195b7579f77SDag-Erling Smørgrav 		iq->state = QUERYTARGETS_STATE;
2196b7579f77SDag-Erling Smørgrav 		return 1;
2197b7579f77SDag-Erling Smørgrav 	}
2198b7579f77SDag-Erling Smørgrav 	iq->state = DSNS_FIND_STATE;
2199b7579f77SDag-Erling Smørgrav 
2200b7579f77SDag-Erling Smørgrav 	/* spawn NS lookup (validation not needed, this is for DS lookup) */
2201b7579f77SDag-Erling Smørgrav 	log_nametypeclass(VERB_ALGO, "fetch nameservers",
2202b7579f77SDag-Erling Smørgrav 		iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
2203b7579f77SDag-Erling Smørgrav 	if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len,
2204b7579f77SDag-Erling Smørgrav 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
2205091e9e46SCy Schubert 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0)) {
22064c75e3aaSDag-Erling Smørgrav 		errinf_dname(qstate, "for DS query parent-child nameserver search, could not generate NS lookup for", iq->dsns_point);
2207b7579f77SDag-Erling Smørgrav 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
2208b7579f77SDag-Erling Smørgrav 	}
2209b7579f77SDag-Erling Smørgrav 
2210b7579f77SDag-Erling Smørgrav 	return 0;
2211b7579f77SDag-Erling Smørgrav }
2212b7579f77SDag-Erling Smørgrav 
2213b7579f77SDag-Erling Smørgrav /**
22140a92a9fcSCy Schubert  * Check if we wait responses for sent queries and update the iterator's
22150a92a9fcSCy Schubert  * external state.
22160a92a9fcSCy Schubert  */
22170a92a9fcSCy Schubert static void
22180a92a9fcSCy Schubert check_waiting_queries(struct iter_qstate* iq, struct module_qstate* qstate,
22190a92a9fcSCy Schubert 	int id)
22200a92a9fcSCy Schubert {
22210a92a9fcSCy Schubert 	if(iq->num_target_queries>0 && iq->num_current_queries>0) {
22220a92a9fcSCy Schubert 		verbose(VERB_ALGO, "waiting for %d targets to "
22230a92a9fcSCy Schubert 			"resolve or %d outstanding queries to "
22240a92a9fcSCy Schubert 			"respond", iq->num_target_queries,
22250a92a9fcSCy Schubert 			iq->num_current_queries);
22260a92a9fcSCy Schubert 		qstate->ext_state[id] = module_wait_reply;
22270a92a9fcSCy Schubert 	} else if(iq->num_target_queries>0) {
22280a92a9fcSCy Schubert 		verbose(VERB_ALGO, "waiting for %d targets to "
22290a92a9fcSCy Schubert 			"resolve", iq->num_target_queries);
22300a92a9fcSCy Schubert 		qstate->ext_state[id] = module_wait_subquery;
22310a92a9fcSCy Schubert 	} else {
22320a92a9fcSCy Schubert 		verbose(VERB_ALGO, "waiting for %d "
22330a92a9fcSCy Schubert 			"outstanding queries to respond",
22340a92a9fcSCy Schubert 			iq->num_current_queries);
22350a92a9fcSCy Schubert 		qstate->ext_state[id] = module_wait_reply;
22360a92a9fcSCy Schubert 	}
22370a92a9fcSCy Schubert }
22380a92a9fcSCy Schubert 
22390a92a9fcSCy Schubert /**
2240b7579f77SDag-Erling Smørgrav  * This is the request event state where the request will be sent to one of
2241b7579f77SDag-Erling Smørgrav  * its current query targets. This state also handles issuing target lookup
2242b7579f77SDag-Erling Smørgrav  * queries for missing target IP addresses. Queries typically iterate on
2243b7579f77SDag-Erling Smørgrav  * this state, both when they are just trying different targets for a given
2244b7579f77SDag-Erling Smørgrav  * delegation point, and when they change delegation points. This state
2245b7579f77SDag-Erling Smørgrav  * roughly corresponds to RFC 1034 algorithm steps 3 and 4.
2246b7579f77SDag-Erling Smørgrav  *
2247b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
2248b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
2249b7579f77SDag-Erling Smørgrav  * @param ie: iterator shared global environment.
2250b7579f77SDag-Erling Smørgrav  * @param id: module id.
2251b7579f77SDag-Erling Smørgrav  * @return true if the event requires more request processing immediately,
2252b7579f77SDag-Erling Smørgrav  *         false if not. This state only returns true when it is generating
2253b7579f77SDag-Erling Smørgrav  *         a SERVFAIL response because the query has hit a dead end.
2254b7579f77SDag-Erling Smørgrav  */
2255b7579f77SDag-Erling Smørgrav static int
2256b7579f77SDag-Erling Smørgrav processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
2257b7579f77SDag-Erling Smørgrav 	struct iter_env* ie, int id)
2258b7579f77SDag-Erling Smørgrav {
2259b7579f77SDag-Erling Smørgrav 	int tf_policy;
2260b7579f77SDag-Erling Smørgrav 	struct delegpt_addr* target;
2261b7579f77SDag-Erling Smørgrav 	struct outbound_entry* outq;
226257bddd21SDag-Erling Smørgrav 	int auth_fallback = 0;
2263e86b9096SDag-Erling Smørgrav 	uint8_t* qout_orig = NULL;
2264e86b9096SDag-Erling Smørgrav 	size_t qout_orig_len = 0;
22659cf5bc93SCy Schubert 	int sq_check_ratelimit = 1;
22669cf5bc93SCy Schubert 	int sq_was_ratelimited = 0;
2267*865f46b2SCy Schubert 	int can_do_promisc = 0;
2268b7579f77SDag-Erling Smørgrav 
2269b7579f77SDag-Erling Smørgrav 	/* NOTE: a request will encounter this state for each target it
2270b7579f77SDag-Erling Smørgrav 	 * needs to send a query to. That is, at least one per referral,
2271b7579f77SDag-Erling Smørgrav 	 * more if some targets timeout or return throwaway answers. */
2272b7579f77SDag-Erling Smørgrav 
2273b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_QUERY, "processQueryTargets:", &qstate->qinfo);
2274b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "processQueryTargets: targetqueries %d, "
2275b7579f77SDag-Erling Smørgrav 		"currentqueries %d sentcount %d", iq->num_target_queries,
2276b7579f77SDag-Erling Smørgrav 		iq->num_current_queries, iq->sent_count);
2277b7579f77SDag-Erling Smørgrav 
2278b7579f77SDag-Erling Smørgrav 	/* Make sure that we haven't run away */
2279b7579f77SDag-Erling Smørgrav 	/* FIXME: is this check even necessary? */
2280b7579f77SDag-Erling Smørgrav 	if(iq->referral_count > MAX_REFERRAL_COUNT) {
2281b7579f77SDag-Erling Smørgrav 		verbose(VERB_QUERY, "request has exceeded the maximum "
2282b7579f77SDag-Erling Smørgrav 			"number of referrrals with %d", iq->referral_count);
22834c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "exceeded the maximum of referrals");
2284b7579f77SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2285b7579f77SDag-Erling Smørgrav 	}
2286b7579f77SDag-Erling Smørgrav 	if(iq->sent_count > MAX_SENT_COUNT) {
2287b7579f77SDag-Erling Smørgrav 		verbose(VERB_QUERY, "request has exceeded the maximum "
2288b7579f77SDag-Erling Smørgrav 			"number of sends with %d", iq->sent_count);
22894c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "exceeded the maximum number of sends");
2290b7579f77SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2291b7579f77SDag-Erling Smørgrav 	}
22920a92a9fcSCy Schubert 
22930a92a9fcSCy Schubert 	/* Check if we reached MAX_TARGET_NX limit without a fallback activation. */
22940a92a9fcSCy Schubert 	if(iq->target_count && !*iq->nxns_dp &&
22950a92a9fcSCy Schubert 		iq->target_count[TARGET_COUNT_NX] > MAX_TARGET_NX) {
22960a92a9fcSCy Schubert 		struct delegpt_ns* ns;
22970a92a9fcSCy Schubert 		/* If we can wait for resolution, do so. */
22980a92a9fcSCy Schubert 		if(iq->num_target_queries>0 || iq->num_current_queries>0) {
22990a92a9fcSCy Schubert 			check_waiting_queries(iq, qstate, id);
23000a92a9fcSCy Schubert 			return 0;
23010a92a9fcSCy Schubert 		}
23020a92a9fcSCy Schubert 		verbose(VERB_ALGO, "request has exceeded the maximum "
23030a92a9fcSCy Schubert 			"number of nxdomain nameserver lookups (%d) with %d",
23040a92a9fcSCy Schubert 			MAX_TARGET_NX, iq->target_count[TARGET_COUNT_NX]);
23050a92a9fcSCy Schubert 		/* Check for dp because we require one below */
23060a92a9fcSCy Schubert 		if(!iq->dp) {
23070a92a9fcSCy Schubert 			verbose(VERB_QUERY, "Failed to get a delegation, "
23080a92a9fcSCy Schubert 				"giving up");
23090a92a9fcSCy Schubert 			errinf(qstate, "failed to get a delegation (eg. prime "
23100a92a9fcSCy Schubert 				"failure)");
23110a92a9fcSCy Schubert 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
23120a92a9fcSCy Schubert 		}
23130a92a9fcSCy Schubert 		/* We reached the limit but we already have parent side
23140a92a9fcSCy Schubert 		 * information; stop resolution */
23150a92a9fcSCy Schubert 		if(iq->dp->has_parent_side_NS) {
23160a92a9fcSCy Schubert 			verbose(VERB_ALGO, "parent-side information is "
23170a92a9fcSCy Schubert 				"already present for the delegation point, no "
23180a92a9fcSCy Schubert 				"fallback possible");
2319091e9e46SCy Schubert 			errinf(qstate, "exceeded the maximum nameserver nxdomains");
2320091e9e46SCy Schubert 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2321091e9e46SCy Schubert 		}
23220a92a9fcSCy Schubert 		verbose(VERB_ALGO, "initiating parent-side fallback for "
23230a92a9fcSCy Schubert 			"nxdomain nameserver lookups");
23240a92a9fcSCy Schubert 		/* Mark all the current NSes as resolved to allow for parent
23250a92a9fcSCy Schubert 		 * fallback */
23260a92a9fcSCy Schubert 		for(ns=iq->dp->nslist; ns; ns=ns->next) {
23270a92a9fcSCy Schubert 			ns->resolved = 1;
23280a92a9fcSCy Schubert 		}
23290a92a9fcSCy Schubert 		/* Note the delegation point that triggered the NXNS fallback;
23300a92a9fcSCy Schubert 		 * no reason for shared queries to keep trying there.
23310a92a9fcSCy Schubert 		 * This also marks the fallback activation. */
23320a92a9fcSCy Schubert 		*iq->nxns_dp = malloc(iq->dp->namelen);
23330a92a9fcSCy Schubert 		if(!*iq->nxns_dp) {
23340a92a9fcSCy Schubert 			verbose(VERB_ALGO, "out of memory while initiating "
23350a92a9fcSCy Schubert 				"fallback");
23360a92a9fcSCy Schubert 			errinf(qstate, "exceeded the maximum nameserver "
23370a92a9fcSCy Schubert 				"nxdomains (malloc)");
23380a92a9fcSCy Schubert 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
23390a92a9fcSCy Schubert 		}
23400a92a9fcSCy Schubert 		memcpy(*iq->nxns_dp, iq->dp->name, iq->dp->namelen);
23410a92a9fcSCy Schubert 	} else if(iq->target_count && *iq->nxns_dp) {
23420a92a9fcSCy Schubert 		/* Handle the NXNS fallback case. */
23430a92a9fcSCy Schubert 		/* If we can wait for resolution, do so. */
23440a92a9fcSCy Schubert 		if(iq->num_target_queries>0 || iq->num_current_queries>0) {
23450a92a9fcSCy Schubert 			check_waiting_queries(iq, qstate, id);
23460a92a9fcSCy Schubert 			return 0;
23470a92a9fcSCy Schubert 		}
23480a92a9fcSCy Schubert 		/* Check for dp because we require one below */
23490a92a9fcSCy Schubert 		if(!iq->dp) {
23500a92a9fcSCy Schubert 			verbose(VERB_QUERY, "Failed to get a delegation, "
23510a92a9fcSCy Schubert 				"giving up");
23520a92a9fcSCy Schubert 			errinf(qstate, "failed to get a delegation (eg. prime "
23530a92a9fcSCy Schubert 				"failure)");
23540a92a9fcSCy Schubert 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
23550a92a9fcSCy Schubert 		}
23560a92a9fcSCy Schubert 
23570a92a9fcSCy Schubert 		if(iq->target_count[TARGET_COUNT_NX] > MAX_TARGET_NX_FALLBACK) {
23580a92a9fcSCy Schubert 			verbose(VERB_ALGO, "request has exceeded the maximum "
23590a92a9fcSCy Schubert 				"number of fallback nxdomain nameserver "
23600a92a9fcSCy Schubert 				"lookups (%d) with %d", MAX_TARGET_NX_FALLBACK,
23610a92a9fcSCy Schubert 				iq->target_count[TARGET_COUNT_NX]);
23620a92a9fcSCy Schubert 			errinf(qstate, "exceeded the maximum nameserver nxdomains");
23630a92a9fcSCy Schubert 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
23640a92a9fcSCy Schubert 		}
23650a92a9fcSCy Schubert 
23660a92a9fcSCy Schubert 		if(!iq->dp->has_parent_side_NS) {
23670a92a9fcSCy Schubert 			struct delegpt_ns* ns;
23680a92a9fcSCy Schubert 			if(!dname_canonical_compare(*iq->nxns_dp, iq->dp->name)) {
23690a92a9fcSCy Schubert 				verbose(VERB_ALGO, "this delegation point "
23700a92a9fcSCy Schubert 					"initiated the fallback, marking the "
23710a92a9fcSCy Schubert 					"nslist as resolved");
23720a92a9fcSCy Schubert 				for(ns=iq->dp->nslist; ns; ns=ns->next) {
23730a92a9fcSCy Schubert 					ns->resolved = 1;
23740a92a9fcSCy Schubert 				}
23750a92a9fcSCy Schubert 			}
23760a92a9fcSCy Schubert 		}
23770a92a9fcSCy Schubert 	}
2378b7579f77SDag-Erling Smørgrav 
2379b7579f77SDag-Erling Smørgrav 	/* Make sure we have a delegation point, otherwise priming failed
2380b7579f77SDag-Erling Smørgrav 	 * or another failure occurred */
2381b7579f77SDag-Erling Smørgrav 	if(!iq->dp) {
2382b7579f77SDag-Erling Smørgrav 		verbose(VERB_QUERY, "Failed to get a delegation, giving up");
23834c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "failed to get a delegation (eg. prime failure)");
2384b7579f77SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2385b7579f77SDag-Erling Smørgrav 	}
2386b7579f77SDag-Erling Smørgrav 	if(!ie->supports_ipv6)
2387b7579f77SDag-Erling Smørgrav 		delegpt_no_ipv6(iq->dp);
2388b7579f77SDag-Erling Smørgrav 	if(!ie->supports_ipv4)
2389b7579f77SDag-Erling Smørgrav 		delegpt_no_ipv4(iq->dp);
2390b7579f77SDag-Erling Smørgrav 	delegpt_log(VERB_ALGO, iq->dp);
2391b7579f77SDag-Erling Smørgrav 
2392b7579f77SDag-Erling Smørgrav 	if(iq->num_current_queries>0) {
2393b7579f77SDag-Erling Smørgrav 		/* already busy answering a query, this restart is because
2394b7579f77SDag-Erling Smørgrav 		 * more delegpt addrs became available, wait for existing
2395b7579f77SDag-Erling Smørgrav 		 * query. */
2396b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "woke up, but wait for outstanding query");
2397b7579f77SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_reply;
2398b7579f77SDag-Erling Smørgrav 		return 0;
2399b7579f77SDag-Erling Smørgrav 	}
2400b7579f77SDag-Erling Smørgrav 
2401a755b6f6SDag-Erling Smørgrav 	if(iq->minimisation_state == INIT_MINIMISE_STATE
2402a755b6f6SDag-Erling Smørgrav 		&& !(iq->chase_flags & BIT_RD)) {
240357bddd21SDag-Erling Smørgrav 		/* (Re)set qinfo_out to (new) delegation point, except when
240457bddd21SDag-Erling Smørgrav 		 * qinfo_out is already a subdomain of dp. This happens when
240557bddd21SDag-Erling Smørgrav 		 * increasing by more than one label at once (QNAMEs with more
240657bddd21SDag-Erling Smørgrav 		 * than MAX_MINIMISE_COUNT labels). */
240757bddd21SDag-Erling Smørgrav 		if(!(iq->qinfo_out.qname_len
240857bddd21SDag-Erling Smørgrav 			&& dname_subdomain_c(iq->qchase.qname,
240957bddd21SDag-Erling Smørgrav 				iq->qinfo_out.qname)
241057bddd21SDag-Erling Smørgrav 			&& dname_subdomain_c(iq->qinfo_out.qname,
241157bddd21SDag-Erling Smørgrav 				iq->dp->name))) {
241257bddd21SDag-Erling Smørgrav 			iq->qinfo_out.qname = iq->dp->name;
241357bddd21SDag-Erling Smørgrav 			iq->qinfo_out.qname_len = iq->dp->namelen;
241457bddd21SDag-Erling Smørgrav 			iq->qinfo_out.qtype = LDNS_RR_TYPE_A;
241557bddd21SDag-Erling Smørgrav 			iq->qinfo_out.qclass = iq->qchase.qclass;
241657bddd21SDag-Erling Smørgrav 			iq->qinfo_out.local_alias = NULL;
241757bddd21SDag-Erling Smørgrav 			iq->minimise_count = 0;
241857bddd21SDag-Erling Smørgrav 		}
241957bddd21SDag-Erling Smørgrav 
242057bddd21SDag-Erling Smørgrav 		iq->minimisation_state = MINIMISE_STATE;
242157bddd21SDag-Erling Smørgrav 	}
242257bddd21SDag-Erling Smørgrav 	if(iq->minimisation_state == MINIMISE_STATE) {
242357bddd21SDag-Erling Smørgrav 		int qchaselabs = dname_count_labels(iq->qchase.qname);
242457bddd21SDag-Erling Smørgrav 		int labdiff = qchaselabs -
242557bddd21SDag-Erling Smørgrav 			dname_count_labels(iq->qinfo_out.qname);
242657bddd21SDag-Erling Smørgrav 
2427e86b9096SDag-Erling Smørgrav 		qout_orig = iq->qinfo_out.qname;
2428e86b9096SDag-Erling Smørgrav 		qout_orig_len = iq->qinfo_out.qname_len;
242957bddd21SDag-Erling Smørgrav 		iq->qinfo_out.qname = iq->qchase.qname;
243057bddd21SDag-Erling Smørgrav 		iq->qinfo_out.qname_len = iq->qchase.qname_len;
243157bddd21SDag-Erling Smørgrav 		iq->minimise_count++;
243225039b37SCy Schubert 		iq->timeout_count = 0;
243357bddd21SDag-Erling Smørgrav 
243424e36522SCy Schubert 		iter_dec_attempts(iq->dp, 1, ie->outbound_msg_retry);
243557bddd21SDag-Erling Smørgrav 
243657bddd21SDag-Erling Smørgrav 		/* Limit number of iterations for QNAMEs with more
243757bddd21SDag-Erling Smørgrav 		 * than MAX_MINIMISE_COUNT labels. Send first MINIMISE_ONE_LAB
243857bddd21SDag-Erling Smørgrav 		 * labels of QNAME always individually.
243957bddd21SDag-Erling Smørgrav 		 */
244057bddd21SDag-Erling Smørgrav 		if(qchaselabs > MAX_MINIMISE_COUNT && labdiff > 1 &&
244157bddd21SDag-Erling Smørgrav 			iq->minimise_count > MINIMISE_ONE_LAB) {
244257bddd21SDag-Erling Smørgrav 			if(iq->minimise_count < MAX_MINIMISE_COUNT) {
244357bddd21SDag-Erling Smørgrav 				int multilabs = qchaselabs - 1 -
244457bddd21SDag-Erling Smørgrav 					MINIMISE_ONE_LAB;
244557bddd21SDag-Erling Smørgrav 				int extralabs = multilabs /
244657bddd21SDag-Erling Smørgrav 					MINIMISE_MULTIPLE_LABS;
244757bddd21SDag-Erling Smørgrav 
244857bddd21SDag-Erling Smørgrav 				if (MAX_MINIMISE_COUNT - iq->minimise_count >=
244957bddd21SDag-Erling Smørgrav 					multilabs % MINIMISE_MULTIPLE_LABS)
245057bddd21SDag-Erling Smørgrav 					/* Default behaviour is to add 1 label
245157bddd21SDag-Erling Smørgrav 					 * every iteration. Therefore, decrement
245257bddd21SDag-Erling Smørgrav 					 * the extralabs by 1 */
245357bddd21SDag-Erling Smørgrav 					extralabs--;
245457bddd21SDag-Erling Smørgrav 				if (extralabs < labdiff)
245557bddd21SDag-Erling Smørgrav 					labdiff -= extralabs;
245657bddd21SDag-Erling Smørgrav 				else
245757bddd21SDag-Erling Smørgrav 					labdiff = 1;
245857bddd21SDag-Erling Smørgrav 			}
245957bddd21SDag-Erling Smørgrav 			/* Last minimised iteration, send all labels with
246057bddd21SDag-Erling Smørgrav 			 * QTYPE=NS */
246157bddd21SDag-Erling Smørgrav 			else
246257bddd21SDag-Erling Smørgrav 				labdiff = 1;
246357bddd21SDag-Erling Smørgrav 		}
246457bddd21SDag-Erling Smørgrav 
246557bddd21SDag-Erling Smørgrav 		if(labdiff > 1) {
246657bddd21SDag-Erling Smørgrav 			verbose(VERB_QUERY, "removing %d labels", labdiff-1);
246757bddd21SDag-Erling Smørgrav 			dname_remove_labels(&iq->qinfo_out.qname,
246857bddd21SDag-Erling Smørgrav 				&iq->qinfo_out.qname_len,
246957bddd21SDag-Erling Smørgrav 				labdiff-1);
247057bddd21SDag-Erling Smørgrav 		}
247157bddd21SDag-Erling Smørgrav 		if(labdiff < 1 || (labdiff < 2
247257bddd21SDag-Erling Smørgrav 			&& (iq->qchase.qtype == LDNS_RR_TYPE_DS
247357bddd21SDag-Erling Smørgrav 			|| iq->qchase.qtype == LDNS_RR_TYPE_A)))
247457bddd21SDag-Erling Smørgrav 			/* Stop minimising this query, resolve "as usual" */
247557bddd21SDag-Erling Smørgrav 			iq->minimisation_state = DONOT_MINIMISE_STATE;
247657bddd21SDag-Erling Smørgrav 		else if(!qstate->no_cache_lookup) {
247757bddd21SDag-Erling Smørgrav 			struct dns_msg* msg = dns_cache_lookup(qstate->env,
247857bddd21SDag-Erling Smørgrav 				iq->qinfo_out.qname, iq->qinfo_out.qname_len,
247957bddd21SDag-Erling Smørgrav 				iq->qinfo_out.qtype, iq->qinfo_out.qclass,
248057bddd21SDag-Erling Smørgrav 				qstate->query_flags, qstate->region,
24815469a995SCy Schubert 				qstate->env->scratch, 0, iq->dp->name,
24825469a995SCy Schubert 				iq->dp->namelen);
2483091e9e46SCy Schubert 			if(msg && FLAGS_GET_RCODE(msg->rep->flags) ==
248457bddd21SDag-Erling Smørgrav 				LDNS_RCODE_NOERROR)
248557bddd21SDag-Erling Smørgrav 				/* no need to send query if it is already
2486091e9e46SCy Schubert 				 * cached as NOERROR */
248757bddd21SDag-Erling Smørgrav 				return 1;
2488091e9e46SCy Schubert 			if(msg && FLAGS_GET_RCODE(msg->rep->flags) ==
2489091e9e46SCy Schubert 				LDNS_RCODE_NXDOMAIN &&
2490091e9e46SCy Schubert 				qstate->env->need_to_validate &&
2491091e9e46SCy Schubert 				qstate->env->cfg->harden_below_nxdomain) {
2492091e9e46SCy Schubert 				if(msg->rep->security == sec_status_secure) {
2493091e9e46SCy Schubert 					iq->response = msg;
2494091e9e46SCy Schubert 					return final_state(iq);
2495091e9e46SCy Schubert 				}
2496091e9e46SCy Schubert 				if(msg->rep->security == sec_status_unchecked) {
2497091e9e46SCy Schubert 					struct module_qstate* subq = NULL;
2498091e9e46SCy Schubert 					if(!generate_sub_request(
2499091e9e46SCy Schubert 						iq->qinfo_out.qname,
2500091e9e46SCy Schubert 						iq->qinfo_out.qname_len,
2501091e9e46SCy Schubert 						iq->qinfo_out.qtype,
2502091e9e46SCy Schubert 						iq->qinfo_out.qclass,
2503091e9e46SCy Schubert 						qstate, id, iq,
2504091e9e46SCy Schubert 						INIT_REQUEST_STATE,
2505091e9e46SCy Schubert 						FINISHED_STATE, &subq, 1, 1))
2506091e9e46SCy Schubert 						verbose(VERB_ALGO,
2507091e9e46SCy Schubert 						"could not validate NXDOMAIN "
2508091e9e46SCy Schubert 						"response");
2509091e9e46SCy Schubert 				}
2510091e9e46SCy Schubert 			}
2511091e9e46SCy Schubert 			if(msg && FLAGS_GET_RCODE(msg->rep->flags) ==
2512091e9e46SCy Schubert 				LDNS_RCODE_NXDOMAIN) {
2513091e9e46SCy Schubert 				/* return and add a label in the next
2514091e9e46SCy Schubert 				 * minimisation iteration.
2515091e9e46SCy Schubert 				 */
2516091e9e46SCy Schubert 				return 1;
2517091e9e46SCy Schubert 			}
251857bddd21SDag-Erling Smørgrav 		}
251957bddd21SDag-Erling Smørgrav 	}
252057bddd21SDag-Erling Smørgrav 	if(iq->minimisation_state == SKIP_MINIMISE_STATE) {
252125039b37SCy Schubert 		if(iq->timeout_count < MAX_MINIMISE_TIMEOUT_COUNT)
252257bddd21SDag-Erling Smørgrav 			/* Do not increment qname, continue incrementing next
252357bddd21SDag-Erling Smørgrav 			 * iteration */
252457bddd21SDag-Erling Smørgrav 			iq->minimisation_state = MINIMISE_STATE;
252557bddd21SDag-Erling Smørgrav 		else if(!qstate->env->cfg->qname_minimisation_strict)
252657bddd21SDag-Erling Smørgrav 			/* Too many time-outs detected for this QNAME and QTYPE.
252757bddd21SDag-Erling Smørgrav 			 * We give up, disable QNAME minimisation. */
252857bddd21SDag-Erling Smørgrav 			iq->minimisation_state = DONOT_MINIMISE_STATE;
252957bddd21SDag-Erling Smørgrav 	}
253057bddd21SDag-Erling Smørgrav 	if(iq->minimisation_state == DONOT_MINIMISE_STATE)
253157bddd21SDag-Erling Smørgrav 		iq->qinfo_out = iq->qchase;
253257bddd21SDag-Erling Smørgrav 
253357bddd21SDag-Erling Smørgrav 	/* now find an answer to this query */
253457bddd21SDag-Erling Smørgrav 	/* see if authority zones have an answer */
253557bddd21SDag-Erling Smørgrav 	/* now we know the dp, we can check the auth zone for locally hosted
253657bddd21SDag-Erling Smørgrav 	 * contents */
253757bddd21SDag-Erling Smørgrav 	if(!iq->auth_zone_avoid && qstate->blacklist) {
253857bddd21SDag-Erling Smørgrav 		if(auth_zones_can_fallback(qstate->env->auth_zones,
253957bddd21SDag-Erling Smørgrav 			iq->dp->name, iq->dp->namelen, iq->qinfo_out.qclass)) {
254057bddd21SDag-Erling Smørgrav 			/* if cache is blacklisted and this zone allows us
254157bddd21SDag-Erling Smørgrav 			 * to fallback to the internet, then do so, and
254257bddd21SDag-Erling Smørgrav 			 * fetch results from the internet servers */
254357bddd21SDag-Erling Smørgrav 			iq->auth_zone_avoid = 1;
254457bddd21SDag-Erling Smørgrav 		}
254557bddd21SDag-Erling Smørgrav 	}
254657bddd21SDag-Erling Smørgrav 	if(iq->auth_zone_avoid) {
254757bddd21SDag-Erling Smørgrav 		iq->auth_zone_avoid = 0;
254857bddd21SDag-Erling Smørgrav 		auth_fallback = 1;
254957bddd21SDag-Erling Smørgrav 	} else if(auth_zones_lookup(qstate->env->auth_zones, &iq->qinfo_out,
255057bddd21SDag-Erling Smørgrav 		qstate->region, &iq->response, &auth_fallback, iq->dp->name,
255157bddd21SDag-Erling Smørgrav 		iq->dp->namelen)) {
255257bddd21SDag-Erling Smørgrav 		/* use this as a response to be processed by the iterator */
255357bddd21SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO) {
255457bddd21SDag-Erling Smørgrav 			log_dns_msg("msg from auth zone",
255557bddd21SDag-Erling Smørgrav 				&iq->response->qinfo, iq->response->rep);
255657bddd21SDag-Erling Smørgrav 		}
25570fb34990SDag-Erling Smørgrav 		if((iq->chase_flags&BIT_RD) && !(iq->response->rep->flags&BIT_AA)) {
25580fb34990SDag-Erling Smørgrav 			verbose(VERB_ALGO, "forwarder, ignoring referral from auth zone");
25590fb34990SDag-Erling Smørgrav 		} else {
25600fb34990SDag-Erling Smørgrav 			lock_rw_wrlock(&qstate->env->auth_zones->lock);
25610fb34990SDag-Erling Smørgrav 			qstate->env->auth_zones->num_query_up++;
25620fb34990SDag-Erling Smørgrav 			lock_rw_unlock(&qstate->env->auth_zones->lock);
256357bddd21SDag-Erling Smørgrav 			iq->num_current_queries++;
256457bddd21SDag-Erling Smørgrav 			iq->chase_to_rd = 0;
256557bddd21SDag-Erling Smørgrav 			iq->dnssec_lame_query = 0;
256657bddd21SDag-Erling Smørgrav 			iq->auth_zone_response = 1;
256757bddd21SDag-Erling Smørgrav 			return next_state(iq, QUERY_RESP_STATE);
256857bddd21SDag-Erling Smørgrav 		}
25690fb34990SDag-Erling Smørgrav 	}
257057bddd21SDag-Erling Smørgrav 	iq->auth_zone_response = 0;
257157bddd21SDag-Erling Smørgrav 	if(auth_fallback == 0) {
257257bddd21SDag-Erling Smørgrav 		/* like we got servfail from the auth zone lookup, and
257357bddd21SDag-Erling Smørgrav 		 * no internet fallback */
257457bddd21SDag-Erling Smørgrav 		verbose(VERB_ALGO, "auth zone lookup failed, no fallback,"
257557bddd21SDag-Erling Smørgrav 			" servfail");
25764c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "auth zone lookup failed, fallback is off");
257757bddd21SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
257857bddd21SDag-Erling Smørgrav 	}
2579e86b9096SDag-Erling Smørgrav 	if(iq->dp->auth_dp) {
258057bddd21SDag-Erling Smørgrav 		/* we wanted to fallback, but had no delegpt, only the
258157bddd21SDag-Erling Smørgrav 		 * auth zone generated delegpt, create an actual one */
258257bddd21SDag-Erling Smørgrav 		iq->auth_zone_avoid = 1;
258357bddd21SDag-Erling Smørgrav 		return next_state(iq, INIT_REQUEST_STATE);
258457bddd21SDag-Erling Smørgrav 	}
258557bddd21SDag-Erling Smørgrav 	/* but mostly, fallback==1 (like, when no such auth zone exists)
258657bddd21SDag-Erling Smørgrav 	 * and we continue with lookups */
258757bddd21SDag-Erling Smørgrav 
2588b7579f77SDag-Erling Smørgrav 	tf_policy = 0;
2589b7579f77SDag-Erling Smørgrav 	/* < not <=, because although the array is large enough for <=, the
2590b7579f77SDag-Erling Smørgrav 	 * generated query will immediately be discarded due to depth and
2591b7579f77SDag-Erling Smørgrav 	 * that servfail is cached, which is not good as opportunism goes. */
2592b7579f77SDag-Erling Smørgrav 	if(iq->depth < ie->max_dependency_depth
2593091e9e46SCy Schubert 		&& iq->num_target_queries == 0
25940a92a9fcSCy Schubert 		&& (!iq->target_count || iq->target_count[TARGET_COUNT_NX]==0)
2595*865f46b2SCy Schubert 		&& iq->sent_count < TARGET_FETCH_STOP) {
2596*865f46b2SCy Schubert 		can_do_promisc = 1;
2597*865f46b2SCy Schubert 	}
2598*865f46b2SCy Schubert 	/* if the mesh query list is full, then do not waste cpu and sockets to
2599*865f46b2SCy Schubert 	 * fetch promiscuous targets. They can be looked up when needed. */
2600*865f46b2SCy Schubert 	if(can_do_promisc && !mesh_jostle_exceeded(qstate->env->mesh)) {
2601b7579f77SDag-Erling Smørgrav 		tf_policy = ie->target_fetch_policy[iq->depth];
2602b7579f77SDag-Erling Smørgrav 	}
2603b7579f77SDag-Erling Smørgrav 
2604b7579f77SDag-Erling Smørgrav 	/* if in 0x20 fallback get as many targets as possible */
2605b7579f77SDag-Erling Smørgrav 	if(iq->caps_fallback) {
2606b7579f77SDag-Erling Smørgrav 		int extra = 0;
2607b7579f77SDag-Erling Smørgrav 		size_t naddr, nres, navail;
2608b7579f77SDag-Erling Smørgrav 		if(!query_for_targets(qstate, iq, ie, id, -1, &extra)) {
26094c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "could not fetch nameservers for 0x20 fallback");
2610b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2611b7579f77SDag-Erling Smørgrav 		}
2612b7579f77SDag-Erling Smørgrav 		iq->num_target_queries += extra;
261352df462fSXin LI 		target_count_increase(iq, extra);
2614b7579f77SDag-Erling Smørgrav 		if(iq->num_target_queries > 0) {
2615b7579f77SDag-Erling Smørgrav 			/* wait to get all targets, we want to try em */
2616b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "wait for all targets for fallback");
2617b7579f77SDag-Erling Smørgrav 			qstate->ext_state[id] = module_wait_reply;
2618e86b9096SDag-Erling Smørgrav 			/* undo qname minimise step because we'll get back here
2619e86b9096SDag-Erling Smørgrav 			 * to do it again */
2620e86b9096SDag-Erling Smørgrav 			if(qout_orig && iq->minimise_count > 0) {
2621e86b9096SDag-Erling Smørgrav 				iq->minimise_count--;
2622e86b9096SDag-Erling Smørgrav 				iq->qinfo_out.qname = qout_orig;
2623e86b9096SDag-Erling Smørgrav 				iq->qinfo_out.qname_len = qout_orig_len;
2624e86b9096SDag-Erling Smørgrav 			}
2625b7579f77SDag-Erling Smørgrav 			return 0;
2626b7579f77SDag-Erling Smørgrav 		}
2627b7579f77SDag-Erling Smørgrav 		/* did we do enough fallback queries already? */
2628b7579f77SDag-Erling Smørgrav 		delegpt_count_addr(iq->dp, &naddr, &nres, &navail);
2629b7579f77SDag-Erling Smørgrav 		/* the current caps_server is the number of fallbacks sent.
2630b7579f77SDag-Erling Smørgrav 		 * the original query is one that matched too, so we have
2631b7579f77SDag-Erling Smørgrav 		 * caps_server+1 number of matching queries now */
2632b7579f77SDag-Erling Smørgrav 		if(iq->caps_server+1 >= naddr*3 ||
263309a3aaf3SDag-Erling Smørgrav 			iq->caps_server*2+2 >= MAX_SENT_COUNT) {
263409a3aaf3SDag-Erling Smørgrav 			/* *2 on sentcount check because ipv6 may fail */
2635b7579f77SDag-Erling Smørgrav 			/* we're done, process the response */
2636b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "0x20 fallback had %d responses "
2637b7579f77SDag-Erling Smørgrav 				"match for %d wanted, done.",
2638b7579f77SDag-Erling Smørgrav 				(int)iq->caps_server+1, (int)naddr*3);
263909a3aaf3SDag-Erling Smørgrav 			iq->response = iq->caps_response;
2640b7579f77SDag-Erling Smørgrav 			iq->caps_fallback = 0;
264124e36522SCy Schubert 			iter_dec_attempts(iq->dp, 3, ie->outbound_msg_retry); /* space for fallback */
2642b7579f77SDag-Erling Smørgrav 			iq->num_current_queries++; /* RespState decrements it*/
2643b7579f77SDag-Erling Smørgrav 			iq->referral_count++; /* make sure we don't loop */
2644b7579f77SDag-Erling Smørgrav 			iq->sent_count = 0;
2645091e9e46SCy Schubert 			iq->dp_target_count = 0;
2646b7579f77SDag-Erling Smørgrav 			iq->state = QUERY_RESP_STATE;
2647b7579f77SDag-Erling Smørgrav 			return 1;
2648b7579f77SDag-Erling Smørgrav 		}
2649b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "0x20 fallback number %d",
2650b7579f77SDag-Erling Smørgrav 			(int)iq->caps_server);
2651b7579f77SDag-Erling Smørgrav 
2652b7579f77SDag-Erling Smørgrav 	/* if there is a policy to fetch missing targets
2653b7579f77SDag-Erling Smørgrav 	 * opportunistically, do it. we rely on the fact that once a
2654b7579f77SDag-Erling Smørgrav 	 * query (or queries) for a missing name have been issued,
2655b7579f77SDag-Erling Smørgrav 	 * they will not show up again. */
2656b7579f77SDag-Erling Smørgrav 	} else if(tf_policy != 0) {
2657b7579f77SDag-Erling Smørgrav 		int extra = 0;
2658b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "attempt to get extra %d targets",
2659b7579f77SDag-Erling Smørgrav 			tf_policy);
2660b7579f77SDag-Erling Smørgrav 		(void)query_for_targets(qstate, iq, ie, id, tf_policy, &extra);
2661b7579f77SDag-Erling Smørgrav 		/* errors ignored, these targets are not strictly necessary for
2662b7579f77SDag-Erling Smørgrav 		 * this result, we do not have to reply with SERVFAIL */
2663b7579f77SDag-Erling Smørgrav 		iq->num_target_queries += extra;
266452df462fSXin LI 		target_count_increase(iq, extra);
2665b7579f77SDag-Erling Smørgrav 	}
2666b7579f77SDag-Erling Smørgrav 
2667b7579f77SDag-Erling Smørgrav 	/* Add the current set of unused targets to our queue. */
2668b7579f77SDag-Erling Smørgrav 	delegpt_add_unused_targets(iq->dp);
2669b7579f77SDag-Erling Smørgrav 
267024e36522SCy Schubert 	if(qstate->env->auth_zones) {
267124e36522SCy Schubert 		/* apply rpz triggers at query time */
267224e36522SCy Schubert 		struct dns_msg* forged_response = rpz_callback_from_iterator_module(qstate, iq);
267324e36522SCy Schubert 		if(forged_response != NULL) {
267424e36522SCy Schubert 			qstate->ext_state[id] = module_finished;
26759cf5bc93SCy Schubert 			qstate->return_rcode = LDNS_RCODE_NOERROR;
267624e36522SCy Schubert 			qstate->return_msg = forged_response;
267724e36522SCy Schubert 			iq->response = forged_response;
267824e36522SCy Schubert 			next_state(iq, FINISHED_STATE);
267924e36522SCy Schubert 			if(!iter_prepend(iq, qstate->return_msg, qstate->region)) {
2680a39a5a69SCy Schubert 				log_err("rpz: prepend rrsets: out of memory");
268124e36522SCy Schubert 				return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
268224e36522SCy Schubert 			}
268324e36522SCy Schubert 			return 0;
268424e36522SCy Schubert 		}
268524e36522SCy Schubert 	}
268624e36522SCy Schubert 
2687b7579f77SDag-Erling Smørgrav 	/* Select the next usable target, filtering out unsuitable targets. */
2688b7579f77SDag-Erling Smørgrav 	target = iter_server_selection(ie, qstate->env, iq->dp,
2689b7579f77SDag-Erling Smørgrav 		iq->dp->name, iq->dp->namelen, iq->qchase.qtype,
2690b7579f77SDag-Erling Smørgrav 		&iq->dnssec_lame_query, &iq->chase_to_rd,
26910fb34990SDag-Erling Smørgrav 		iq->num_target_queries, qstate->blacklist,
26920fb34990SDag-Erling Smørgrav 		qstate->prefetch_leeway);
2693b7579f77SDag-Erling Smørgrav 
2694b7579f77SDag-Erling Smørgrav 	/* If no usable target was selected... */
2695b7579f77SDag-Erling Smørgrav 	if(!target) {
2696b7579f77SDag-Erling Smørgrav 		/* Here we distinguish between three states: generate a new
2697b7579f77SDag-Erling Smørgrav 		 * target query, just wait, or quit (with a SERVFAIL).
2698b7579f77SDag-Erling Smørgrav 		 * We have the following information: number of active
2699b7579f77SDag-Erling Smørgrav 		 * target queries, number of active current queries,
2700b7579f77SDag-Erling Smørgrav 		 * the presence of missing targets at this delegation
2701b7579f77SDag-Erling Smørgrav 		 * point, and the given query target policy. */
2702b7579f77SDag-Erling Smørgrav 
2703b7579f77SDag-Erling Smørgrav 		/* Check for the wait condition. If this is true, then
2704b7579f77SDag-Erling Smørgrav 		 * an action must be taken. */
2705b7579f77SDag-Erling Smørgrav 		if(iq->num_target_queries==0 && iq->num_current_queries==0) {
2706b7579f77SDag-Erling Smørgrav 			/* If there is nothing to wait for, then we need
2707b7579f77SDag-Erling Smørgrav 			 * to distinguish between generating (a) new target
2708b7579f77SDag-Erling Smørgrav 			 * query, or failing. */
27090a92a9fcSCy Schubert 			if(delegpt_count_missing_targets(iq->dp, NULL) > 0) {
2710b7579f77SDag-Erling Smørgrav 				int qs = 0;
2711b7579f77SDag-Erling Smørgrav 				verbose(VERB_ALGO, "querying for next "
2712b7579f77SDag-Erling Smørgrav 					"missing target");
2713b7579f77SDag-Erling Smørgrav 				if(!query_for_targets(qstate, iq, ie, id,
2714b7579f77SDag-Erling Smørgrav 					1, &qs)) {
27154c75e3aaSDag-Erling Smørgrav 					errinf(qstate, "could not fetch nameserver");
27164c75e3aaSDag-Erling Smørgrav 					errinf_dname(qstate, "at zone", iq->dp->name);
2717b7579f77SDag-Erling Smørgrav 					return error_response(qstate, id,
2718b7579f77SDag-Erling Smørgrav 						LDNS_RCODE_SERVFAIL);
2719b7579f77SDag-Erling Smørgrav 				}
2720b7579f77SDag-Erling Smørgrav 				if(qs == 0 &&
27210a92a9fcSCy Schubert 				   delegpt_count_missing_targets(iq->dp, NULL) == 0){
2722b7579f77SDag-Erling Smørgrav 					/* it looked like there were missing
2723b7579f77SDag-Erling Smørgrav 					 * targets, but they did not turn up.
2724b7579f77SDag-Erling Smørgrav 					 * Try the bad choices again (if any),
2725b7579f77SDag-Erling Smørgrav 					 * when we get back here missing==0,
2726b7579f77SDag-Erling Smørgrav 					 * so this is not a loop. */
2727b7579f77SDag-Erling Smørgrav 					return 1;
2728b7579f77SDag-Erling Smørgrav 				}
2729b7579f77SDag-Erling Smørgrav 				iq->num_target_queries += qs;
273052df462fSXin LI 				target_count_increase(iq, qs);
2731b7579f77SDag-Erling Smørgrav 			}
2732b7579f77SDag-Erling Smørgrav 			/* Since a target query might have been made, we
2733b7579f77SDag-Erling Smørgrav 			 * need to check again. */
2734b7579f77SDag-Erling Smørgrav 			if(iq->num_target_queries == 0) {
273509a3aaf3SDag-Erling Smørgrav 				/* if in capsforid fallback, instead of last
273609a3aaf3SDag-Erling Smørgrav 				 * resort, we agree with the current reply
273709a3aaf3SDag-Erling Smørgrav 				 * we have (if any) (our count of addrs bad)*/
273809a3aaf3SDag-Erling Smørgrav 				if(iq->caps_fallback && iq->caps_reply) {
273909a3aaf3SDag-Erling Smørgrav 					/* we're done, process the response */
274009a3aaf3SDag-Erling Smørgrav 					verbose(VERB_ALGO, "0x20 fallback had %d responses, "
274109a3aaf3SDag-Erling Smørgrav 						"but no more servers except "
274209a3aaf3SDag-Erling Smørgrav 						"last resort, done.",
274309a3aaf3SDag-Erling Smørgrav 						(int)iq->caps_server+1);
274409a3aaf3SDag-Erling Smørgrav 					iq->response = iq->caps_response;
274509a3aaf3SDag-Erling Smørgrav 					iq->caps_fallback = 0;
274624e36522SCy Schubert 					iter_dec_attempts(iq->dp, 3, ie->outbound_msg_retry); /* space for fallback */
274709a3aaf3SDag-Erling Smørgrav 					iq->num_current_queries++; /* RespState decrements it*/
274809a3aaf3SDag-Erling Smørgrav 					iq->referral_count++; /* make sure we don't loop */
274909a3aaf3SDag-Erling Smørgrav 					iq->sent_count = 0;
2750091e9e46SCy Schubert 					iq->dp_target_count = 0;
275109a3aaf3SDag-Erling Smørgrav 					iq->state = QUERY_RESP_STATE;
275209a3aaf3SDag-Erling Smørgrav 					return 1;
275309a3aaf3SDag-Erling Smørgrav 				}
2754b7579f77SDag-Erling Smørgrav 				return processLastResort(qstate, iq, ie, id);
2755b7579f77SDag-Erling Smørgrav 			}
2756b7579f77SDag-Erling Smørgrav 		}
2757b7579f77SDag-Erling Smørgrav 
2758b7579f77SDag-Erling Smørgrav 		/* otherwise, we have no current targets, so submerge
2759b7579f77SDag-Erling Smørgrav 		 * until one of the target or direct queries return. */
27600a92a9fcSCy Schubert 		verbose(VERB_ALGO, "no current targets");
27610a92a9fcSCy Schubert 		check_waiting_queries(iq, qstate, id);
2762e86b9096SDag-Erling Smørgrav 		/* undo qname minimise step because we'll get back here
2763e86b9096SDag-Erling Smørgrav 		 * to do it again */
2764e86b9096SDag-Erling Smørgrav 		if(qout_orig && iq->minimise_count > 0) {
2765e86b9096SDag-Erling Smørgrav 			iq->minimise_count--;
2766e86b9096SDag-Erling Smørgrav 			iq->qinfo_out.qname = qout_orig;
2767e86b9096SDag-Erling Smørgrav 			iq->qinfo_out.qname_len = qout_orig_len;
2768e86b9096SDag-Erling Smørgrav 		}
2769b7579f77SDag-Erling Smørgrav 		return 0;
2770b7579f77SDag-Erling Smørgrav 	}
2771b7579f77SDag-Erling Smørgrav 
2772*865f46b2SCy Schubert 	/* We have a target. We could have created promiscuous target
2773*865f46b2SCy Schubert 	 * queries but we are currently under pressure (mesh_jostle_exceeded).
2774*865f46b2SCy Schubert 	 * If we are configured to allow promiscuous target queries and haven't
2775*865f46b2SCy Schubert 	 * gone out to the network for a target query for this delegation, then
2776*865f46b2SCy Schubert 	 * it is possible to slip in a promiscuous one with a 1/10 chance. */
2777*865f46b2SCy Schubert 	if(can_do_promisc && tf_policy == 0 && iq->depth == 0
2778*865f46b2SCy Schubert 		&& iq->depth < ie->max_dependency_depth
2779*865f46b2SCy Schubert 		&& ie->target_fetch_policy[iq->depth] != 0
2780*865f46b2SCy Schubert 		&& iq->dp_target_count == 0
2781*865f46b2SCy Schubert 		&& !ub_random_max(qstate->env->rnd, 10)) {
2782*865f46b2SCy Schubert 		int extra = 0;
2783*865f46b2SCy Schubert 		verbose(VERB_ALGO, "available target exists in cache but "
2784*865f46b2SCy Schubert 			"attempt to get extra 1 target");
2785*865f46b2SCy Schubert 		(void)query_for_targets(qstate, iq, ie, id, 1, &extra);
2786*865f46b2SCy Schubert 		/* errors ignored, these targets are not strictly necessary for
2787*865f46b2SCy Schubert 		* this result, we do not have to reply with SERVFAIL */
2788*865f46b2SCy Schubert 		if(extra > 0) {
2789*865f46b2SCy Schubert 			iq->num_target_queries += extra;
2790*865f46b2SCy Schubert 			target_count_increase(iq, extra);
2791*865f46b2SCy Schubert 			check_waiting_queries(iq, qstate, id);
2792*865f46b2SCy Schubert 			/* undo qname minimise step because we'll get back here
2793*865f46b2SCy Schubert 			 * to do it again */
2794*865f46b2SCy Schubert 			if(qout_orig && iq->minimise_count > 0) {
2795*865f46b2SCy Schubert 				iq->minimise_count--;
2796*865f46b2SCy Schubert 				iq->qinfo_out.qname = qout_orig;
2797*865f46b2SCy Schubert 				iq->qinfo_out.qname_len = qout_orig_len;
2798*865f46b2SCy Schubert 			}
2799*865f46b2SCy Schubert 			return 0;
2800*865f46b2SCy Schubert 		}
2801*865f46b2SCy Schubert 	}
2802*865f46b2SCy Schubert 
28039cf5bc93SCy Schubert 	/* Do not check ratelimit for forwarding queries or if we already got a
28049cf5bc93SCy Schubert 	 * pass. */
28059cf5bc93SCy Schubert 	sq_check_ratelimit = (!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok);
2806b7579f77SDag-Erling Smørgrav 	/* We have a valid target. */
2807b7579f77SDag-Erling Smørgrav 	if(verbosity >= VERB_QUERY) {
280805ab2901SDag-Erling Smørgrav 		log_query_info(VERB_QUERY, "sending query:", &iq->qinfo_out);
2809b7579f77SDag-Erling Smørgrav 		log_name_addr(VERB_QUERY, "sending to target:", iq->dp->name,
2810b7579f77SDag-Erling Smørgrav 			&target->addr, target->addrlen);
2811b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "dnssec status: %s%s",
2812b7579f77SDag-Erling Smørgrav 			iq->dnssec_expected?"expected": "not expected",
2813b7579f77SDag-Erling Smørgrav 			iq->dnssec_lame_query?" but lame_query anyway": "");
2814b7579f77SDag-Erling Smørgrav 	}
2815b7579f77SDag-Erling Smørgrav 	fptr_ok(fptr_whitelist_modenv_send_query(qstate->env->send_query));
2816bc892140SDag-Erling Smørgrav 	outq = (*qstate->env->send_query)(&iq->qinfo_out,
2817e2d15004SDag-Erling Smørgrav 		iq->chase_flags | (iq->chase_to_rd?BIT_RD:0),
2818e2d15004SDag-Erling Smørgrav 		/* unset CD if to forwarder(RD set) and not dnssec retry
2819e2d15004SDag-Erling Smørgrav 		 * (blacklist nonempty) and no trust-anchors are configured
2820e2d15004SDag-Erling Smørgrav 		 * above the qname or on the first attempt when dnssec is on */
2821e2d15004SDag-Erling Smørgrav 		EDNS_DO| ((iq->chase_to_rd||(iq->chase_flags&BIT_RD)!=0)&&
28220fb34990SDag-Erling Smørgrav 		!qstate->blacklist&&(!iter_qname_indicates_dnssec(qstate->env,
2823e2d15004SDag-Erling Smørgrav 		&iq->qinfo_out)||target->attempts==1)?0:BIT_CD),
282409a3aaf3SDag-Erling Smørgrav 		iq->dnssec_expected, iq->caps_fallback || is_caps_whitelisted(
28259cf5bc93SCy Schubert 		ie, iq), sq_check_ratelimit, &target->addr, target->addrlen,
2826bc892140SDag-Erling Smørgrav 		iq->dp->name, iq->dp->namelen,
282724e36522SCy Schubert 		(iq->dp->tcp_upstream || qstate->env->cfg->tcp_upstream),
28280fb34990SDag-Erling Smørgrav 		(iq->dp->ssl_upstream || qstate->env->cfg->ssl_upstream),
28299cf5bc93SCy Schubert 		target->tls_auth_name, qstate, &sq_was_ratelimited);
2830b7579f77SDag-Erling Smørgrav 	if(!outq) {
28319cf5bc93SCy Schubert 		if(sq_was_ratelimited) {
28329cf5bc93SCy Schubert 			lock_basic_lock(&ie->queries_ratelimit_lock);
28339cf5bc93SCy Schubert 			ie->num_queries_ratelimited++;
28349cf5bc93SCy Schubert 			lock_basic_unlock(&ie->queries_ratelimit_lock);
28359cf5bc93SCy Schubert 			verbose(VERB_ALGO, "query exceeded ratelimits");
28369cf5bc93SCy Schubert 			qstate->was_ratelimited = 1;
28379cf5bc93SCy Schubert 			errinf_dname(qstate, "exceeded ratelimit for zone",
28389cf5bc93SCy Schubert 				iq->dp->name);
28399cf5bc93SCy Schubert 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
28409cf5bc93SCy Schubert 		}
28415469a995SCy Schubert 		log_addr(VERB_QUERY, "error sending query to auth server",
2842b7579f77SDag-Erling Smørgrav 			&target->addr, target->addrlen);
2843971980c3SDag-Erling Smørgrav 		if(qstate->env->cfg->qname_minimisation)
2844971980c3SDag-Erling Smørgrav 			iq->minimisation_state = SKIP_MINIMISE_STATE;
2845b7579f77SDag-Erling Smørgrav 		return next_state(iq, QUERYTARGETS_STATE);
2846b7579f77SDag-Erling Smørgrav 	}
2847b7579f77SDag-Erling Smørgrav 	outbound_list_insert(&iq->outlist, outq);
2848b7579f77SDag-Erling Smørgrav 	iq->num_current_queries++;
2849b7579f77SDag-Erling Smørgrav 	iq->sent_count++;
2850b7579f77SDag-Erling Smørgrav 	qstate->ext_state[id] = module_wait_reply;
2851b7579f77SDag-Erling Smørgrav 
2852b7579f77SDag-Erling Smørgrav 	return 0;
2853b7579f77SDag-Erling Smørgrav }
2854b7579f77SDag-Erling Smørgrav 
2855b7579f77SDag-Erling Smørgrav /** find NS rrset in given list */
2856b7579f77SDag-Erling Smørgrav static struct ub_packed_rrset_key*
2857b7579f77SDag-Erling Smørgrav find_NS(struct reply_info* rep, size_t from, size_t to)
2858b7579f77SDag-Erling Smørgrav {
2859b7579f77SDag-Erling Smørgrav 	size_t i;
2860b7579f77SDag-Erling Smørgrav 	for(i=from; i<to; i++) {
2861b7579f77SDag-Erling Smørgrav 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
2862b7579f77SDag-Erling Smørgrav 			return rep->rrsets[i];
2863b7579f77SDag-Erling Smørgrav 	}
2864b7579f77SDag-Erling Smørgrav 	return NULL;
2865b7579f77SDag-Erling Smørgrav }
2866b7579f77SDag-Erling Smørgrav 
2867b7579f77SDag-Erling Smørgrav 
2868b7579f77SDag-Erling Smørgrav /**
2869b7579f77SDag-Erling Smørgrav  * Process the query response. All queries end up at this state first. This
2870b7579f77SDag-Erling Smørgrav  * process generally consists of analyzing the response and routing the
2871b7579f77SDag-Erling Smørgrav  * event to the next state (either bouncing it back to a request state, or
2872b7579f77SDag-Erling Smørgrav  * terminating the processing for this event).
2873b7579f77SDag-Erling Smørgrav  *
2874b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
2875b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
287624e36522SCy Schubert  * @param ie: iterator shared global environment.
2877b7579f77SDag-Erling Smørgrav  * @param id: module id.
2878b7579f77SDag-Erling Smørgrav  * @return true if the event requires more immediate processing, false if
2879b7579f77SDag-Erling Smørgrav  *         not. This is generally only true when forwarding the request to
2880b7579f77SDag-Erling Smørgrav  *         the final state (i.e., on answer).
2881b7579f77SDag-Erling Smørgrav  */
2882b7579f77SDag-Erling Smørgrav static int
2883b7579f77SDag-Erling Smørgrav processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
288424e36522SCy Schubert 	struct iter_env* ie, int id)
2885b7579f77SDag-Erling Smørgrav {
2886b7579f77SDag-Erling Smørgrav 	int dnsseclame = 0;
2887b7579f77SDag-Erling Smørgrav 	enum response_type type;
288824e36522SCy Schubert 
2889b7579f77SDag-Erling Smørgrav 	iq->num_current_queries--;
289065b390aaSDag-Erling Smørgrav 
289165b390aaSDag-Erling Smørgrav 	if(!inplace_cb_query_response_call(qstate->env, qstate, iq->response))
289265b390aaSDag-Erling Smørgrav 		log_err("unable to call query_response callback");
289365b390aaSDag-Erling Smørgrav 
2894b7579f77SDag-Erling Smørgrav 	if(iq->response == NULL) {
289505ab2901SDag-Erling Smørgrav 		/* Don't increment qname when QNAME minimisation is enabled */
2896971980c3SDag-Erling Smørgrav 		if(qstate->env->cfg->qname_minimisation) {
289705ab2901SDag-Erling Smørgrav 			iq->minimisation_state = SKIP_MINIMISE_STATE;
2898971980c3SDag-Erling Smørgrav 		}
289925039b37SCy Schubert 		iq->timeout_count++;
2900b7579f77SDag-Erling Smørgrav 		iq->chase_to_rd = 0;
2901b7579f77SDag-Erling Smørgrav 		iq->dnssec_lame_query = 0;
2902b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "query response was timeout");
2903b7579f77SDag-Erling Smørgrav 		return next_state(iq, QUERYTARGETS_STATE);
2904b7579f77SDag-Erling Smørgrav 	}
290525039b37SCy Schubert 	iq->timeout_count = 0;
2906b7579f77SDag-Erling Smørgrav 	type = response_type_from_server(
2907b7579f77SDag-Erling Smørgrav 		(int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
29080fb34990SDag-Erling Smørgrav 		iq->response, &iq->qinfo_out, iq->dp);
2909b7579f77SDag-Erling Smørgrav 	iq->chase_to_rd = 0;
2910*865f46b2SCy Schubert 	/* remove TC flag, if this is erroneously set by TCP upstream */
2911*865f46b2SCy Schubert 	iq->response->rep->flags &= ~BIT_TC;
29120fb34990SDag-Erling Smørgrav 	if(type == RESPONSE_TYPE_REFERRAL && (iq->chase_flags&BIT_RD) &&
29130fb34990SDag-Erling Smørgrav 		!iq->auth_zone_response) {
2914b7579f77SDag-Erling Smørgrav 		/* When forwarding (RD bit is set), we handle referrals
2915b7579f77SDag-Erling Smørgrav 		 * differently. No queries should be sent elsewhere */
2916b7579f77SDag-Erling Smørgrav 		type = RESPONSE_TYPE_ANSWER;
2917b7579f77SDag-Erling Smørgrav 	}
2918e2d15004SDag-Erling Smørgrav 	if(!qstate->env->cfg->disable_dnssec_lame_check && iq->dnssec_expected
2919e2d15004SDag-Erling Smørgrav                 && !iq->dnssec_lame_query &&
2920b7579f77SDag-Erling Smørgrav 		!(iq->chase_flags&BIT_RD)
2921e2d15004SDag-Erling Smørgrav 		&& iq->sent_count < DNSSEC_LAME_DETECT_COUNT
2922b7579f77SDag-Erling Smørgrav 		&& type != RESPONSE_TYPE_LAME
2923b7579f77SDag-Erling Smørgrav 		&& type != RESPONSE_TYPE_REC_LAME
2924b7579f77SDag-Erling Smørgrav 		&& type != RESPONSE_TYPE_THROWAWAY
2925b7579f77SDag-Erling Smørgrav 		&& type != RESPONSE_TYPE_UNTYPED) {
2926b7579f77SDag-Erling Smørgrav 		/* a possible answer, see if it is missing DNSSEC */
2927b7579f77SDag-Erling Smørgrav 		/* but not when forwarding, so we dont mark fwder lame */
292817d15b25SDag-Erling Smørgrav 		if(!iter_msg_has_dnssec(iq->response)) {
292917d15b25SDag-Erling Smørgrav 			/* Mark this address as dnsseclame in this dp,
293017d15b25SDag-Erling Smørgrav 			 * because that will make serverselection disprefer
293117d15b25SDag-Erling Smørgrav 			 * it, but also, once it is the only final option,
293217d15b25SDag-Erling Smørgrav 			 * use dnssec-lame-bypass if it needs to query there.*/
293317d15b25SDag-Erling Smørgrav 			if(qstate->reply) {
293417d15b25SDag-Erling Smørgrav 				struct delegpt_addr* a = delegpt_find_addr(
2935*865f46b2SCy Schubert 					iq->dp, &qstate->reply->remote_addr,
2936*865f46b2SCy Schubert 					qstate->reply->remote_addrlen);
293717d15b25SDag-Erling Smørgrav 				if(a) a->dnsseclame = 1;
293817d15b25SDag-Erling Smørgrav 			}
293917d15b25SDag-Erling Smørgrav 			/* test the answer is from the zone we expected,
2940b7579f77SDag-Erling Smørgrav 		 	 * otherwise, (due to parent,child on same server), we
2941b7579f77SDag-Erling Smørgrav 		 	 * might mark the server,zone lame inappropriately */
294217d15b25SDag-Erling Smørgrav 			if(!iter_msg_from_zone(iq->response, iq->dp, type,
294317d15b25SDag-Erling Smørgrav 				iq->qchase.qclass))
294417d15b25SDag-Erling Smørgrav 				qstate->reply = NULL;
2945b7579f77SDag-Erling Smørgrav 			type = RESPONSE_TYPE_LAME;
2946b7579f77SDag-Erling Smørgrav 			dnsseclame = 1;
2947b7579f77SDag-Erling Smørgrav 		}
2948b7579f77SDag-Erling Smørgrav 	} else iq->dnssec_lame_query = 0;
2949b7579f77SDag-Erling Smørgrav 	/* see if referral brings us close to the target */
2950b7579f77SDag-Erling Smørgrav 	if(type == RESPONSE_TYPE_REFERRAL) {
2951b7579f77SDag-Erling Smørgrav 		struct ub_packed_rrset_key* ns = find_NS(
2952b7579f77SDag-Erling Smørgrav 			iq->response->rep, iq->response->rep->an_numrrsets,
2953b7579f77SDag-Erling Smørgrav 			iq->response->rep->an_numrrsets
2954b7579f77SDag-Erling Smørgrav 			+ iq->response->rep->ns_numrrsets);
2955b7579f77SDag-Erling Smørgrav 		if(!ns) ns = find_NS(iq->response->rep, 0,
2956b7579f77SDag-Erling Smørgrav 				iq->response->rep->an_numrrsets);
2957b7579f77SDag-Erling Smørgrav 		if(!ns || !dname_strict_subdomain_c(ns->rk.dname, iq->dp->name)
2958b7579f77SDag-Erling Smørgrav 			|| !dname_subdomain_c(iq->qchase.qname, ns->rk.dname)){
2959b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "bad referral, throwaway");
2960b7579f77SDag-Erling Smørgrav 			type = RESPONSE_TYPE_THROWAWAY;
2961b7579f77SDag-Erling Smørgrav 		} else
2962b7579f77SDag-Erling Smørgrav 			iter_scrub_ds(iq->response, ns, iq->dp->name);
2963b7579f77SDag-Erling Smørgrav 	} else iter_scrub_ds(iq->response, NULL, NULL);
296465b390aaSDag-Erling Smørgrav 	if(type == RESPONSE_TYPE_THROWAWAY &&
296565b390aaSDag-Erling Smørgrav 		FLAGS_GET_RCODE(iq->response->rep->flags) == LDNS_RCODE_YXDOMAIN) {
296665b390aaSDag-Erling Smørgrav 		/* YXDOMAIN is a permanent error, no need to retry */
296765b390aaSDag-Erling Smørgrav 		type = RESPONSE_TYPE_ANSWER;
296865b390aaSDag-Erling Smørgrav 	}
296965b390aaSDag-Erling Smørgrav 	if(type == RESPONSE_TYPE_CNAME && iq->response->rep->an_numrrsets >= 1
297065b390aaSDag-Erling Smørgrav 		&& ntohs(iq->response->rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_DNAME) {
297165b390aaSDag-Erling Smørgrav 		uint8_t* sname = NULL;
297265b390aaSDag-Erling Smørgrav 		size_t snamelen = 0;
297365b390aaSDag-Erling Smørgrav 		get_cname_target(iq->response->rep->rrsets[0], &sname,
297465b390aaSDag-Erling Smørgrav 			&snamelen);
297565b390aaSDag-Erling Smørgrav 		if(snamelen && dname_subdomain_c(sname, iq->response->rep->rrsets[0]->rk.dname)) {
297665b390aaSDag-Erling Smørgrav 			/* DNAME to a subdomain loop; do not recurse */
297765b390aaSDag-Erling Smørgrav 			type = RESPONSE_TYPE_ANSWER;
297865b390aaSDag-Erling Smørgrav 		}
29794c75e3aaSDag-Erling Smørgrav 	} else if(type == RESPONSE_TYPE_CNAME &&
29804c75e3aaSDag-Erling Smørgrav 		iq->qchase.qtype == LDNS_RR_TYPE_CNAME &&
29814c75e3aaSDag-Erling Smørgrav 		iq->minimisation_state == MINIMISE_STATE &&
29824c75e3aaSDag-Erling Smørgrav 		query_dname_compare(iq->qchase.qname, iq->qinfo_out.qname) == 0) {
29834c75e3aaSDag-Erling Smørgrav 		/* The minimised query for full QTYPE and hidden QTYPE can be
29844c75e3aaSDag-Erling Smørgrav 		 * classified as CNAME response type, even when the original
29854c75e3aaSDag-Erling Smørgrav 		 * QTYPE=CNAME. This should be treated as answer response type.
29864c75e3aaSDag-Erling Smørgrav 		 */
29874c75e3aaSDag-Erling Smørgrav 		type = RESPONSE_TYPE_ANSWER;
298865b390aaSDag-Erling Smørgrav 	}
2989b7579f77SDag-Erling Smørgrav 
2990b7579f77SDag-Erling Smørgrav 	/* handle each of the type cases */
2991b7579f77SDag-Erling Smørgrav 	if(type == RESPONSE_TYPE_ANSWER) {
2992b7579f77SDag-Erling Smørgrav 		/* ANSWER type responses terminate the query algorithm,
2993b7579f77SDag-Erling Smørgrav 		 * so they sent on their */
2994b7579f77SDag-Erling Smørgrav 		if(verbosity >= VERB_DETAIL) {
2995b7579f77SDag-Erling Smørgrav 			verbose(VERB_DETAIL, "query response was %s",
2996b7579f77SDag-Erling Smørgrav 				FLAGS_GET_RCODE(iq->response->rep->flags)
2997b7579f77SDag-Erling Smørgrav 				==LDNS_RCODE_NXDOMAIN?"NXDOMAIN ANSWER":
2998b7579f77SDag-Erling Smørgrav 				(iq->response->rep->an_numrrsets?"ANSWER":
2999b7579f77SDag-Erling Smørgrav 				"nodata ANSWER"));
3000b7579f77SDag-Erling Smørgrav 		}
3001b7579f77SDag-Erling Smørgrav 		/* if qtype is DS, check we have the right level of answer,
3002b7579f77SDag-Erling Smørgrav 		 * like grandchild answer but we need the middle, reject it */
3003b7579f77SDag-Erling Smørgrav 		if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
3004b7579f77SDag-Erling Smørgrav 			&& !(iq->chase_flags&BIT_RD)
3005b7579f77SDag-Erling Smørgrav 			&& iter_ds_toolow(iq->response, iq->dp)
30068ed2b524SDag-Erling Smørgrav 			&& iter_dp_cangodown(&iq->qchase, iq->dp)) {
30078ed2b524SDag-Erling Smørgrav 			/* close down outstanding requests to be discarded */
30088ed2b524SDag-Erling Smørgrav 			outbound_list_clear(&iq->outlist);
30098ed2b524SDag-Erling Smørgrav 			iq->num_current_queries = 0;
30108ed2b524SDag-Erling Smørgrav 			fptr_ok(fptr_whitelist_modenv_detach_subs(
30118ed2b524SDag-Erling Smørgrav 				qstate->env->detach_subs));
30128ed2b524SDag-Erling Smørgrav 			(*qstate->env->detach_subs)(qstate);
30138ed2b524SDag-Erling Smørgrav 			iq->num_target_queries = 0;
3014b7579f77SDag-Erling Smørgrav 			return processDSNSFind(qstate, iq, id);
30158ed2b524SDag-Erling Smørgrav 		}
3016bc892140SDag-Erling Smørgrav 		if(!qstate->no_cache_store)
30178ed2b524SDag-Erling Smørgrav 			iter_dns_store(qstate->env, &iq->response->qinfo,
3018a39a5a69SCy Schubert 				iq->response->rep,
3019a39a5a69SCy Schubert 				iq->qchase.qtype != iq->response->qinfo.qtype,
3020a39a5a69SCy Schubert 				qstate->prefetch_leeway,
3021b7579f77SDag-Erling Smørgrav 				iq->dp&&iq->dp->has_parent_side_NS,
3022790c6b24SCy Schubert 				qstate->region, qstate->query_flags,
3023790c6b24SCy Schubert 				qstate->qstarttime);
3024b7579f77SDag-Erling Smørgrav 		/* close down outstanding requests to be discarded */
3025b7579f77SDag-Erling Smørgrav 		outbound_list_clear(&iq->outlist);
3026b7579f77SDag-Erling Smørgrav 		iq->num_current_queries = 0;
3027b7579f77SDag-Erling Smørgrav 		fptr_ok(fptr_whitelist_modenv_detach_subs(
3028b7579f77SDag-Erling Smørgrav 			qstate->env->detach_subs));
3029b7579f77SDag-Erling Smørgrav 		(*qstate->env->detach_subs)(qstate);
3030b7579f77SDag-Erling Smørgrav 		iq->num_target_queries = 0;
3031b7579f77SDag-Erling Smørgrav 		if(qstate->reply)
3032b7579f77SDag-Erling Smørgrav 			sock_list_insert(&qstate->reply_origin,
3033*865f46b2SCy Schubert 				&qstate->reply->remote_addr,
3034*865f46b2SCy Schubert 				qstate->reply->remote_addrlen, qstate->region);
3035a755b6f6SDag-Erling Smørgrav 		if(iq->minimisation_state != DONOT_MINIMISE_STATE
3036a755b6f6SDag-Erling Smørgrav 			&& !(iq->chase_flags & BIT_RD)) {
303705ab2901SDag-Erling Smørgrav 			if(FLAGS_GET_RCODE(iq->response->rep->flags) !=
3038bc892140SDag-Erling Smørgrav 				LDNS_RCODE_NOERROR) {
3039e86b9096SDag-Erling Smørgrav 				if(qstate->env->cfg->qname_minimisation_strict) {
3040e86b9096SDag-Erling Smørgrav 					if(FLAGS_GET_RCODE(iq->response->rep->flags) ==
3041e86b9096SDag-Erling Smørgrav 						LDNS_RCODE_NXDOMAIN) {
3042e86b9096SDag-Erling Smørgrav 						iter_scrub_nxdomain(iq->response);
3043bc892140SDag-Erling Smørgrav 						return final_state(iq);
3044e86b9096SDag-Erling Smørgrav 					}
3045e86b9096SDag-Erling Smørgrav 					return error_response(qstate, id,
3046e86b9096SDag-Erling Smørgrav 						LDNS_RCODE_SERVFAIL);
3047e86b9096SDag-Erling Smørgrav 				}
3048bc892140SDag-Erling Smørgrav 				/* Best effort qname-minimisation.
3049bc892140SDag-Erling Smørgrav 				 * Stop minimising and send full query when
3050bc892140SDag-Erling Smørgrav 				 * RCODE is not NOERROR. */
305105ab2901SDag-Erling Smørgrav 				iq->minimisation_state = DONOT_MINIMISE_STATE;
3052bc892140SDag-Erling Smørgrav 			}
3053e2d15004SDag-Erling Smørgrav 			if(FLAGS_GET_RCODE(iq->response->rep->flags) ==
3054e2d15004SDag-Erling Smørgrav 				LDNS_RCODE_NXDOMAIN) {
3055e2d15004SDag-Erling Smørgrav 				/* Stop resolving when NXDOMAIN is DNSSEC
30568a384985SDag-Erling Smørgrav 				 * signed. Based on assumption that nameservers
3057e2d15004SDag-Erling Smørgrav 				 * serving signed zones do not return NXDOMAIN
3058e2d15004SDag-Erling Smørgrav 				 * for empty-non-terminals. */
3059e2d15004SDag-Erling Smørgrav 				if(iq->dnssec_expected)
3060e2d15004SDag-Erling Smørgrav 					return final_state(iq);
3061e2d15004SDag-Erling Smørgrav 				/* Make subrequest to validate intermediate
3062e2d15004SDag-Erling Smørgrav 				 * NXDOMAIN if harden-below-nxdomain is
3063e2d15004SDag-Erling Smørgrav 				 * enabled. */
3064091e9e46SCy Schubert 				if(qstate->env->cfg->harden_below_nxdomain &&
3065091e9e46SCy Schubert 					qstate->env->need_to_validate) {
3066e2d15004SDag-Erling Smørgrav 					struct module_qstate* subq = NULL;
3067e2d15004SDag-Erling Smørgrav 					log_query_info(VERB_QUERY,
3068e2d15004SDag-Erling Smørgrav 						"schedule NXDOMAIN validation:",
3069e2d15004SDag-Erling Smørgrav 						&iq->response->qinfo);
3070e2d15004SDag-Erling Smørgrav 					if(!generate_sub_request(
3071e2d15004SDag-Erling Smørgrav 						iq->response->qinfo.qname,
3072e2d15004SDag-Erling Smørgrav 						iq->response->qinfo.qname_len,
3073e2d15004SDag-Erling Smørgrav 						iq->response->qinfo.qtype,
3074e2d15004SDag-Erling Smørgrav 						iq->response->qinfo.qclass,
3075e2d15004SDag-Erling Smørgrav 						qstate, id, iq,
3076e2d15004SDag-Erling Smørgrav 						INIT_REQUEST_STATE,
3077091e9e46SCy Schubert 						FINISHED_STATE, &subq, 1, 1))
3078e2d15004SDag-Erling Smørgrav 						verbose(VERB_ALGO,
3079e2d15004SDag-Erling Smørgrav 						"could not validate NXDOMAIN "
3080e2d15004SDag-Erling Smørgrav 						"response");
3081e2d15004SDag-Erling Smørgrav 				}
3082e2d15004SDag-Erling Smørgrav 			}
308305ab2901SDag-Erling Smørgrav 			return next_state(iq, QUERYTARGETS_STATE);
308405ab2901SDag-Erling Smørgrav 		}
3085b7579f77SDag-Erling Smørgrav 		return final_state(iq);
3086b7579f77SDag-Erling Smørgrav 	} else if(type == RESPONSE_TYPE_REFERRAL) {
3087b7579f77SDag-Erling Smørgrav 		/* REFERRAL type responses get a reset of the
3088b7579f77SDag-Erling Smørgrav 		 * delegation point, and back to the QUERYTARGETS_STATE. */
3089b7579f77SDag-Erling Smørgrav 		verbose(VERB_DETAIL, "query response was REFERRAL");
3090b7579f77SDag-Erling Smørgrav 
3091b7579f77SDag-Erling Smørgrav 		/* if hardened, only store referral if we asked for it */
3092bc892140SDag-Erling Smørgrav 		if(!qstate->no_cache_store &&
3093bc892140SDag-Erling Smørgrav 		(!qstate->env->cfg->harden_referral_path ||
3094b7579f77SDag-Erling Smørgrav 		    (  qstate->qinfo.qtype == LDNS_RR_TYPE_NS
3095b7579f77SDag-Erling Smørgrav 			&& (qstate->query_flags&BIT_RD)
3096b7579f77SDag-Erling Smørgrav 			&& !(qstate->query_flags&BIT_CD)
3097b7579f77SDag-Erling Smørgrav 			   /* we know that all other NS rrsets are scrubbed
3098b7579f77SDag-Erling Smørgrav 			    * away, thus on referral only one is left.
3099b7579f77SDag-Erling Smørgrav 			    * see if that equals the query name... */
3100b7579f77SDag-Erling Smørgrav 			&& ( /* auth section, but sometimes in answer section*/
3101b7579f77SDag-Erling Smørgrav 			  reply_find_rrset_section_ns(iq->response->rep,
3102b7579f77SDag-Erling Smørgrav 				iq->qchase.qname, iq->qchase.qname_len,
3103b7579f77SDag-Erling Smørgrav 				LDNS_RR_TYPE_NS, iq->qchase.qclass)
3104b7579f77SDag-Erling Smørgrav 			  || reply_find_rrset_section_an(iq->response->rep,
3105b7579f77SDag-Erling Smørgrav 				iq->qchase.qname, iq->qchase.qname_len,
3106b7579f77SDag-Erling Smørgrav 				LDNS_RR_TYPE_NS, iq->qchase.qclass)
3107b7579f77SDag-Erling Smørgrav 			  )
3108bc892140SDag-Erling Smørgrav 		    ))) {
3109b7579f77SDag-Erling Smørgrav 			/* Store the referral under the current query */
3110b7579f77SDag-Erling Smørgrav 			/* no prefetch-leeway, since its not the answer */
31118ed2b524SDag-Erling Smørgrav 			iter_dns_store(qstate->env, &iq->response->qinfo,
3112790c6b24SCy Schubert 				iq->response->rep, 1, 0, 0, NULL, 0,
3113790c6b24SCy Schubert 				qstate->qstarttime);
3114b7579f77SDag-Erling Smørgrav 			if(iq->store_parent_NS)
3115b7579f77SDag-Erling Smørgrav 				iter_store_parentside_NS(qstate->env,
3116b7579f77SDag-Erling Smørgrav 					iq->response->rep);
3117b7579f77SDag-Erling Smørgrav 			if(qstate->env->neg_cache)
3118b7579f77SDag-Erling Smørgrav 				val_neg_addreferral(qstate->env->neg_cache,
3119b7579f77SDag-Erling Smørgrav 					iq->response->rep, iq->dp->name);
3120b7579f77SDag-Erling Smørgrav 		}
3121b7579f77SDag-Erling Smørgrav 		/* store parent-side-in-zone-glue, if directly queried for */
3122bc892140SDag-Erling Smørgrav 		if(!qstate->no_cache_store && iq->query_for_pside_glue
3123bc892140SDag-Erling Smørgrav 			&& !iq->pside_glue) {
3124b7579f77SDag-Erling Smørgrav 				iq->pside_glue = reply_find_rrset(iq->response->rep,
3125b7579f77SDag-Erling Smørgrav 					iq->qchase.qname, iq->qchase.qname_len,
3126b7579f77SDag-Erling Smørgrav 					iq->qchase.qtype, iq->qchase.qclass);
3127b7579f77SDag-Erling Smørgrav 				if(iq->pside_glue) {
3128b7579f77SDag-Erling Smørgrav 					log_rrset_key(VERB_ALGO, "found parent-side "
3129b7579f77SDag-Erling Smørgrav 						"glue", iq->pside_glue);
3130b7579f77SDag-Erling Smørgrav 					iter_store_parentside_rrset(qstate->env,
3131b7579f77SDag-Erling Smørgrav 						iq->pside_glue);
3132b7579f77SDag-Erling Smørgrav 				}
3133b7579f77SDag-Erling Smørgrav 		}
3134b7579f77SDag-Erling Smørgrav 
3135b7579f77SDag-Erling Smørgrav 		/* Reset the event state, setting the current delegation
3136b7579f77SDag-Erling Smørgrav 		 * point to the referral. */
3137b7579f77SDag-Erling Smørgrav 		iq->deleg_msg = iq->response;
3138b7579f77SDag-Erling Smørgrav 		iq->dp = delegpt_from_message(iq->response, qstate->region);
313905ab2901SDag-Erling Smørgrav 		if (qstate->env->cfg->qname_minimisation)
314005ab2901SDag-Erling Smørgrav 			iq->minimisation_state = INIT_MINIMISE_STATE;
31414c75e3aaSDag-Erling Smørgrav 		if(!iq->dp) {
31424c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, for delegation point");
3143b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
31444c75e3aaSDag-Erling Smørgrav 		}
3145b7579f77SDag-Erling Smørgrav 		if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
31464c75e3aaSDag-Erling Smørgrav 			qstate->region, iq->dp)) {
31474c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, copy extra info into delegation point");
3148b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
31494c75e3aaSDag-Erling Smørgrav 		}
3150b7579f77SDag-Erling Smørgrav 		if(iq->store_parent_NS && query_dname_compare(iq->dp->name,
3151b7579f77SDag-Erling Smørgrav 			iq->store_parent_NS->name) == 0)
315224e36522SCy Schubert 			iter_merge_retry_counts(iq->dp, iq->store_parent_NS,
315324e36522SCy Schubert 				ie->outbound_msg_retry);
3154b7579f77SDag-Erling Smørgrav 		delegpt_log(VERB_ALGO, iq->dp);
3155b7579f77SDag-Erling Smørgrav 		/* Count this as a referral. */
3156b7579f77SDag-Erling Smørgrav 		iq->referral_count++;
3157b7579f77SDag-Erling Smørgrav 		iq->sent_count = 0;
3158091e9e46SCy Schubert 		iq->dp_target_count = 0;
3159b7579f77SDag-Erling Smørgrav 		/* see if the next dp is a trust anchor, or a DS was sent
3160b7579f77SDag-Erling Smørgrav 		 * along, indicating dnssec is expected for next zone */
3161b7579f77SDag-Erling Smørgrav 		iq->dnssec_expected = iter_indicates_dnssec(qstate->env,
3162b7579f77SDag-Erling Smørgrav 			iq->dp, iq->response, iq->qchase.qclass);
3163b7579f77SDag-Erling Smørgrav 		/* if dnssec, validating then also fetch the key for the DS */
3164b7579f77SDag-Erling Smørgrav 		if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
3165b7579f77SDag-Erling Smørgrav 			!(qstate->query_flags&BIT_CD))
3166b7579f77SDag-Erling Smørgrav 			generate_dnskey_prefetch(qstate, iq, id);
3167b7579f77SDag-Erling Smørgrav 
3168b7579f77SDag-Erling Smørgrav 		/* spawn off NS and addr to auth servers for the NS we just
3169b7579f77SDag-Erling Smørgrav 		 * got in the referral. This gets authoritative answer
3170b7579f77SDag-Erling Smørgrav 		 * (answer section trust level) rrset.
3171b7579f77SDag-Erling Smørgrav 		 * right after, we detach the subs, answer goes to cache. */
3172b7579f77SDag-Erling Smørgrav 		if(qstate->env->cfg->harden_referral_path)
3173b7579f77SDag-Erling Smørgrav 			generate_ns_check(qstate, iq, id);
3174b7579f77SDag-Erling Smørgrav 
3175b7579f77SDag-Erling Smørgrav 		/* stop current outstanding queries.
3176b7579f77SDag-Erling Smørgrav 		 * FIXME: should the outstanding queries be waited for and
3177b7579f77SDag-Erling Smørgrav 		 * handled? Say by a subquery that inherits the outbound_entry.
3178b7579f77SDag-Erling Smørgrav 		 */
3179b7579f77SDag-Erling Smørgrav 		outbound_list_clear(&iq->outlist);
3180b7579f77SDag-Erling Smørgrav 		iq->num_current_queries = 0;
3181b7579f77SDag-Erling Smørgrav 		fptr_ok(fptr_whitelist_modenv_detach_subs(
3182b7579f77SDag-Erling Smørgrav 			qstate->env->detach_subs));
3183b7579f77SDag-Erling Smørgrav 		(*qstate->env->detach_subs)(qstate);
3184b7579f77SDag-Erling Smørgrav 		iq->num_target_queries = 0;
31855469a995SCy Schubert 		iq->response = NULL;
31865469a995SCy Schubert 		iq->fail_reply = NULL;
3187b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "cleared outbound list for next round");
3188b7579f77SDag-Erling Smørgrav 		return next_state(iq, QUERYTARGETS_STATE);
3189b7579f77SDag-Erling Smørgrav 	} else if(type == RESPONSE_TYPE_CNAME) {
3190b7579f77SDag-Erling Smørgrav 		uint8_t* sname = NULL;
3191b7579f77SDag-Erling Smørgrav 		size_t snamelen = 0;
3192b7579f77SDag-Erling Smørgrav 		/* CNAME type responses get a query restart (i.e., get a
3193b7579f77SDag-Erling Smørgrav 		 * reset of the query state and go back to INIT_REQUEST_STATE).
3194b7579f77SDag-Erling Smørgrav 		 */
3195b7579f77SDag-Erling Smørgrav 		verbose(VERB_DETAIL, "query response was CNAME");
3196b7579f77SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO)
3197b7579f77SDag-Erling Smørgrav 			log_dns_msg("cname msg", &iq->response->qinfo,
3198b7579f77SDag-Erling Smørgrav 				iq->response->rep);
3199b7579f77SDag-Erling Smørgrav 		/* if qtype is DS, check we have the right level of answer,
3200b7579f77SDag-Erling Smørgrav 		 * like grandchild answer but we need the middle, reject it */
3201b7579f77SDag-Erling Smørgrav 		if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
3202b7579f77SDag-Erling Smørgrav 			&& !(iq->chase_flags&BIT_RD)
3203b7579f77SDag-Erling Smørgrav 			&& iter_ds_toolow(iq->response, iq->dp)
32048ed2b524SDag-Erling Smørgrav 			&& iter_dp_cangodown(&iq->qchase, iq->dp)) {
32058ed2b524SDag-Erling Smørgrav 			outbound_list_clear(&iq->outlist);
32068ed2b524SDag-Erling Smørgrav 			iq->num_current_queries = 0;
32078ed2b524SDag-Erling Smørgrav 			fptr_ok(fptr_whitelist_modenv_detach_subs(
32088ed2b524SDag-Erling Smørgrav 				qstate->env->detach_subs));
32098ed2b524SDag-Erling Smørgrav 			(*qstate->env->detach_subs)(qstate);
32108ed2b524SDag-Erling Smørgrav 			iq->num_target_queries = 0;
3211b7579f77SDag-Erling Smørgrav 			return processDSNSFind(qstate, iq, id);
32128ed2b524SDag-Erling Smørgrav 		}
3213b7579f77SDag-Erling Smørgrav 		/* Process the CNAME response. */
3214b7579f77SDag-Erling Smørgrav 		if(!handle_cname_response(qstate, iq, iq->response,
32154c75e3aaSDag-Erling Smørgrav 			&sname, &snamelen)) {
32164c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, CNAME info");
3217b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
32184c75e3aaSDag-Erling Smørgrav 		}
3219b7579f77SDag-Erling Smørgrav 		/* cache the CNAME response under the current query */
3220b7579f77SDag-Erling Smørgrav 		/* NOTE : set referral=1, so that rrsets get stored but not
3221b7579f77SDag-Erling Smørgrav 		 * the partial query answer (CNAME only). */
3222b7579f77SDag-Erling Smørgrav 		/* prefetchleeway applied because this updates answer parts */
3223bc892140SDag-Erling Smørgrav 		if(!qstate->no_cache_store)
32248ed2b524SDag-Erling Smørgrav 			iter_dns_store(qstate->env, &iq->response->qinfo,
3225b7579f77SDag-Erling Smørgrav 				iq->response->rep, 1, qstate->prefetch_leeway,
3226ff825849SDag-Erling Smørgrav 				iq->dp&&iq->dp->has_parent_side_NS, NULL,
3227790c6b24SCy Schubert 				qstate->query_flags, qstate->qstarttime);
3228b7579f77SDag-Erling Smørgrav 		/* set the current request's qname to the new value. */
3229b7579f77SDag-Erling Smørgrav 		iq->qchase.qname = sname;
3230b7579f77SDag-Erling Smørgrav 		iq->qchase.qname_len = snamelen;
323124e36522SCy Schubert 		if(qstate->env->auth_zones) {
323224e36522SCy Schubert 			/* apply rpz qname triggers after cname */
323324e36522SCy Schubert 			struct dns_msg* forged_response =
323424e36522SCy Schubert 				rpz_callback_from_iterator_cname(qstate, iq);
323524e36522SCy Schubert 			while(forged_response && reply_find_rrset_section_an(
323624e36522SCy Schubert 				forged_response->rep, iq->qchase.qname,
323724e36522SCy Schubert 				iq->qchase.qname_len, LDNS_RR_TYPE_CNAME,
323824e36522SCy Schubert 				iq->qchase.qclass)) {
323924e36522SCy Schubert 				/* another cname to follow */
324024e36522SCy Schubert 				if(!handle_cname_response(qstate, iq, forged_response,
324124e36522SCy Schubert 					&sname, &snamelen)) {
324224e36522SCy Schubert 					errinf(qstate, "malloc failure, CNAME info");
324324e36522SCy Schubert 					return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
324424e36522SCy Schubert 				}
324524e36522SCy Schubert 				iq->qchase.qname = sname;
324624e36522SCy Schubert 				iq->qchase.qname_len = snamelen;
324724e36522SCy Schubert 				forged_response =
324824e36522SCy Schubert 					rpz_callback_from_iterator_cname(qstate, iq);
324924e36522SCy Schubert 			}
325024e36522SCy Schubert 			if(forged_response != NULL) {
325124e36522SCy Schubert 				qstate->ext_state[id] = module_finished;
32529cf5bc93SCy Schubert 				qstate->return_rcode = LDNS_RCODE_NOERROR;
325324e36522SCy Schubert 				qstate->return_msg = forged_response;
325424e36522SCy Schubert 				iq->response = forged_response;
325524e36522SCy Schubert 				next_state(iq, FINISHED_STATE);
325624e36522SCy Schubert 				if(!iter_prepend(iq, qstate->return_msg, qstate->region)) {
3257a39a5a69SCy Schubert 					log_err("rpz: after cname, prepend rrsets: out of memory");
325824e36522SCy Schubert 					return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
325924e36522SCy Schubert 				}
326024e36522SCy Schubert 				qstate->return_msg->qinfo = qstate->qinfo;
326124e36522SCy Schubert 				return 0;
326224e36522SCy Schubert 			}
326324e36522SCy Schubert 		}
3264b7579f77SDag-Erling Smørgrav 		/* Clear the query state, since this is a query restart. */
3265b7579f77SDag-Erling Smørgrav 		iq->deleg_msg = NULL;
3266b7579f77SDag-Erling Smørgrav 		iq->dp = NULL;
3267b7579f77SDag-Erling Smørgrav 		iq->dsns_point = NULL;
326857bddd21SDag-Erling Smørgrav 		iq->auth_zone_response = 0;
3269b7579f77SDag-Erling Smørgrav 		iq->sent_count = 0;
3270091e9e46SCy Schubert 		iq->dp_target_count = 0;
32717da0adf7SDag-Erling Smørgrav 		if(iq->minimisation_state != MINIMISE_STATE)
32727da0adf7SDag-Erling Smørgrav 			/* Only count as query restart when it is not an extra
32737da0adf7SDag-Erling Smørgrav 			 * query as result of qname minimisation. */
32747da0adf7SDag-Erling Smørgrav 			iq->query_restart_count++;
32757da0adf7SDag-Erling Smørgrav 		if(qstate->env->cfg->qname_minimisation)
32767da0adf7SDag-Erling Smørgrav 			iq->minimisation_state = INIT_MINIMISE_STATE;
3277b7579f77SDag-Erling Smørgrav 
3278b7579f77SDag-Erling Smørgrav 		/* stop current outstanding queries.
3279b7579f77SDag-Erling Smørgrav 		 * FIXME: should the outstanding queries be waited for and
3280b7579f77SDag-Erling Smørgrav 		 * handled? Say by a subquery that inherits the outbound_entry.
3281b7579f77SDag-Erling Smørgrav 		 */
3282b7579f77SDag-Erling Smørgrav 		outbound_list_clear(&iq->outlist);
3283b7579f77SDag-Erling Smørgrav 		iq->num_current_queries = 0;
3284b7579f77SDag-Erling Smørgrav 		fptr_ok(fptr_whitelist_modenv_detach_subs(
3285b7579f77SDag-Erling Smørgrav 			qstate->env->detach_subs));
3286b7579f77SDag-Erling Smørgrav 		(*qstate->env->detach_subs)(qstate);
3287b7579f77SDag-Erling Smørgrav 		iq->num_target_queries = 0;
3288b7579f77SDag-Erling Smørgrav 		if(qstate->reply)
3289b7579f77SDag-Erling Smørgrav 			sock_list_insert(&qstate->reply_origin,
3290*865f46b2SCy Schubert 				&qstate->reply->remote_addr,
3291*865f46b2SCy Schubert 				qstate->reply->remote_addrlen, qstate->region);
3292b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "cleared outbound list for query restart");
3293b7579f77SDag-Erling Smørgrav 		/* go to INIT_REQUEST_STATE for new qname. */
3294b7579f77SDag-Erling Smørgrav 		return next_state(iq, INIT_REQUEST_STATE);
3295b7579f77SDag-Erling Smørgrav 	} else if(type == RESPONSE_TYPE_LAME) {
3296b7579f77SDag-Erling Smørgrav 		/* Cache the LAMEness. */
3297b7579f77SDag-Erling Smørgrav 		verbose(VERB_DETAIL, "query response was %sLAME",
3298b7579f77SDag-Erling Smørgrav 			dnsseclame?"DNSSEC ":"");
3299b7579f77SDag-Erling Smørgrav 		if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
3300b7579f77SDag-Erling Smørgrav 			log_err("mark lame: mismatch in qname and dpname");
3301b7579f77SDag-Erling Smørgrav 			/* throwaway this reply below */
3302b7579f77SDag-Erling Smørgrav 		} else if(qstate->reply) {
3303b7579f77SDag-Erling Smørgrav 			/* need addr for lameness cache, but we may have
3304b7579f77SDag-Erling Smørgrav 			 * gotten this from cache, so test to be sure */
3305b7579f77SDag-Erling Smørgrav 			if(!infra_set_lame(qstate->env->infra_cache,
3306*865f46b2SCy Schubert 				&qstate->reply->remote_addr,
3307*865f46b2SCy Schubert 				qstate->reply->remote_addrlen,
3308b7579f77SDag-Erling Smørgrav 				iq->dp->name, iq->dp->namelen,
3309b7579f77SDag-Erling Smørgrav 				*qstate->env->now, dnsseclame, 0,
3310b7579f77SDag-Erling Smørgrav 				iq->qchase.qtype))
3311b7579f77SDag-Erling Smørgrav 				log_err("mark host lame: out of memory");
331217d15b25SDag-Erling Smørgrav 		}
3313b7579f77SDag-Erling Smørgrav 	} else if(type == RESPONSE_TYPE_REC_LAME) {
3314b7579f77SDag-Erling Smørgrav 		/* Cache the LAMEness. */
3315b7579f77SDag-Erling Smørgrav 		verbose(VERB_DETAIL, "query response REC_LAME: "
3316b7579f77SDag-Erling Smørgrav 			"recursive but not authoritative server");
3317b7579f77SDag-Erling Smørgrav 		if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
3318b7579f77SDag-Erling Smørgrav 			log_err("mark rec_lame: mismatch in qname and dpname");
3319b7579f77SDag-Erling Smørgrav 			/* throwaway this reply below */
3320b7579f77SDag-Erling Smørgrav 		} else if(qstate->reply) {
3321b7579f77SDag-Erling Smørgrav 			/* need addr for lameness cache, but we may have
3322b7579f77SDag-Erling Smørgrav 			 * gotten this from cache, so test to be sure */
3323b7579f77SDag-Erling Smørgrav 			verbose(VERB_DETAIL, "mark as REC_LAME");
3324b7579f77SDag-Erling Smørgrav 			if(!infra_set_lame(qstate->env->infra_cache,
3325*865f46b2SCy Schubert 				&qstate->reply->remote_addr,
3326*865f46b2SCy Schubert 				qstate->reply->remote_addrlen,
3327b7579f77SDag-Erling Smørgrav 				iq->dp->name, iq->dp->namelen,
3328b7579f77SDag-Erling Smørgrav 				*qstate->env->now, 0, 1, iq->qchase.qtype))
3329b7579f77SDag-Erling Smørgrav 				log_err("mark host lame: out of memory");
3330b7579f77SDag-Erling Smørgrav 		}
3331b7579f77SDag-Erling Smørgrav 	} else if(type == RESPONSE_TYPE_THROWAWAY) {
3332b7579f77SDag-Erling Smørgrav 		/* LAME and THROWAWAY responses are handled the same way.
3333b7579f77SDag-Erling Smørgrav 		 * In this case, the event is just sent directly back to
3334b7579f77SDag-Erling Smørgrav 		 * the QUERYTARGETS_STATE without resetting anything,
3335b7579f77SDag-Erling Smørgrav 		 * because, clearly, the next target must be tried. */
3336b7579f77SDag-Erling Smørgrav 		verbose(VERB_DETAIL, "query response was THROWAWAY");
3337b7579f77SDag-Erling Smørgrav 	} else {
3338b7579f77SDag-Erling Smørgrav 		log_warn("A query response came back with an unknown type: %d",
3339b7579f77SDag-Erling Smørgrav 			(int)type);
3340b7579f77SDag-Erling Smørgrav 	}
3341b7579f77SDag-Erling Smørgrav 
3342b7579f77SDag-Erling Smørgrav 	/* LAME, THROWAWAY and "unknown" all end up here.
3343b7579f77SDag-Erling Smørgrav 	 * Recycle to the QUERYTARGETS state to hopefully try a
3344b7579f77SDag-Erling Smørgrav 	 * different target. */
3345bc892140SDag-Erling Smørgrav 	if (qstate->env->cfg->qname_minimisation &&
3346bc892140SDag-Erling Smørgrav 		!qstate->env->cfg->qname_minimisation_strict)
334705ab2901SDag-Erling Smørgrav 		iq->minimisation_state = DONOT_MINIMISE_STATE;
334857bddd21SDag-Erling Smørgrav 	if(iq->auth_zone_response) {
334957bddd21SDag-Erling Smørgrav 		/* can we fallback? */
335057bddd21SDag-Erling Smørgrav 		iq->auth_zone_response = 0;
335157bddd21SDag-Erling Smørgrav 		if(!auth_zones_can_fallback(qstate->env->auth_zones,
335257bddd21SDag-Erling Smørgrav 			iq->dp->name, iq->dp->namelen, qstate->qinfo.qclass)) {
335357bddd21SDag-Erling Smørgrav 			verbose(VERB_ALGO, "auth zone response bad, and no"
335457bddd21SDag-Erling Smørgrav 				" fallback possible, servfail");
3355a755b6f6SDag-Erling Smørgrav 			errinf_dname(qstate, "response is bad, no fallback, "
33564c75e3aaSDag-Erling Smørgrav 				"for auth zone", iq->dp->name);
335757bddd21SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
335857bddd21SDag-Erling Smørgrav 		}
335957bddd21SDag-Erling Smørgrav 		verbose(VERB_ALGO, "auth zone response was bad, "
336057bddd21SDag-Erling Smørgrav 			"fallback enabled");
336157bddd21SDag-Erling Smørgrav 		iq->auth_zone_avoid = 1;
336257bddd21SDag-Erling Smørgrav 		if(iq->dp->auth_dp) {
336357bddd21SDag-Erling Smørgrav 			/* we are using a dp for the auth zone, with no
336457bddd21SDag-Erling Smørgrav 			 * nameservers, get one first */
336557bddd21SDag-Erling Smørgrav 			iq->dp = NULL;
336657bddd21SDag-Erling Smørgrav 			return next_state(iq, INIT_REQUEST_STATE);
336757bddd21SDag-Erling Smørgrav 		}
336857bddd21SDag-Erling Smørgrav 	}
3369b7579f77SDag-Erling Smørgrav 	return next_state(iq, QUERYTARGETS_STATE);
3370b7579f77SDag-Erling Smørgrav }
3371b7579f77SDag-Erling Smørgrav 
3372b7579f77SDag-Erling Smørgrav /**
3373ff825849SDag-Erling Smørgrav  * Return priming query results to interested super querystates.
3374b7579f77SDag-Erling Smørgrav  *
3375b7579f77SDag-Erling Smørgrav  * Sets the delegation point and delegation message (not nonRD queries).
3376b7579f77SDag-Erling Smørgrav  * This is a callback from walk_supers.
3377b7579f77SDag-Erling Smørgrav  *
3378b7579f77SDag-Erling Smørgrav  * @param qstate: priming query state that finished.
3379b7579f77SDag-Erling Smørgrav  * @param id: module id.
3380b7579f77SDag-Erling Smørgrav  * @param forq: the qstate for which priming has been done.
3381b7579f77SDag-Erling Smørgrav  */
3382b7579f77SDag-Erling Smørgrav static void
3383b7579f77SDag-Erling Smørgrav prime_supers(struct module_qstate* qstate, int id, struct module_qstate* forq)
3384b7579f77SDag-Erling Smørgrav {
3385b7579f77SDag-Erling Smørgrav 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
3386b7579f77SDag-Erling Smørgrav 	struct delegpt* dp = NULL;
3387b7579f77SDag-Erling Smørgrav 
3388b7579f77SDag-Erling Smørgrav 	log_assert(qstate->is_priming || foriq->wait_priming_stub);
3389b7579f77SDag-Erling Smørgrav 	log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
3390b7579f77SDag-Erling Smørgrav 	/* Convert our response to a delegation point */
3391b7579f77SDag-Erling Smørgrav 	dp = delegpt_from_message(qstate->return_msg, forq->region);
3392b7579f77SDag-Erling Smørgrav 	if(!dp) {
339324e36522SCy Schubert 		/* if there is no convertible delegation point, then
3394b7579f77SDag-Erling Smørgrav 		 * the ANSWER type was (presumably) a negative answer. */
3395b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "prime response was not a positive "
3396b7579f77SDag-Erling Smørgrav 			"ANSWER; failing");
3397b7579f77SDag-Erling Smørgrav 		foriq->dp = NULL;
3398b7579f77SDag-Erling Smørgrav 		foriq->state = QUERYTARGETS_STATE;
3399b7579f77SDag-Erling Smørgrav 		return;
3400b7579f77SDag-Erling Smørgrav 	}
3401b7579f77SDag-Erling Smørgrav 
3402b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_DETAIL, "priming successful for", &qstate->qinfo);
3403b7579f77SDag-Erling Smørgrav 	delegpt_log(VERB_ALGO, dp);
3404b7579f77SDag-Erling Smørgrav 	foriq->dp = dp;
3405b7579f77SDag-Erling Smørgrav 	foriq->deleg_msg = dns_copy_msg(qstate->return_msg, forq->region);
3406b7579f77SDag-Erling Smørgrav 	if(!foriq->deleg_msg) {
3407b7579f77SDag-Erling Smørgrav 		log_err("copy prime response: out of memory");
3408b7579f77SDag-Erling Smørgrav 		foriq->dp = NULL;
3409b7579f77SDag-Erling Smørgrav 		foriq->state = QUERYTARGETS_STATE;
3410b7579f77SDag-Erling Smørgrav 		return;
3411b7579f77SDag-Erling Smørgrav 	}
3412b7579f77SDag-Erling Smørgrav 
3413b7579f77SDag-Erling Smørgrav 	/* root priming responses go to init stage 2, priming stub
3414b7579f77SDag-Erling Smørgrav 	 * responses to to stage 3. */
3415b7579f77SDag-Erling Smørgrav 	if(foriq->wait_priming_stub) {
3416b7579f77SDag-Erling Smørgrav 		foriq->state = INIT_REQUEST_3_STATE;
3417b7579f77SDag-Erling Smørgrav 		foriq->wait_priming_stub = 0;
3418b7579f77SDag-Erling Smørgrav 	} else	foriq->state = INIT_REQUEST_2_STATE;
3419b7579f77SDag-Erling Smørgrav 	/* because we are finished, the parent will be reactivated */
3420b7579f77SDag-Erling Smørgrav }
3421b7579f77SDag-Erling Smørgrav 
3422b7579f77SDag-Erling Smørgrav /**
3423b7579f77SDag-Erling Smørgrav  * This handles the response to a priming query. This is used to handle both
3424b7579f77SDag-Erling Smørgrav  * root and stub priming responses. This is basically the equivalent of the
3425b7579f77SDag-Erling Smørgrav  * QUERY_RESP_STATE, but will not handle CNAME responses and will treat
3426b7579f77SDag-Erling Smørgrav  * REFERRALs as ANSWERS. It will also update and reactivate the originating
3427b7579f77SDag-Erling Smørgrav  * event.
3428b7579f77SDag-Erling Smørgrav  *
3429b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3430b7579f77SDag-Erling Smørgrav  * @param id: module id.
3431b7579f77SDag-Erling Smørgrav  * @return true if the event needs more immediate processing, false if not.
3432b7579f77SDag-Erling Smørgrav  *         This state always returns false.
3433b7579f77SDag-Erling Smørgrav  */
3434b7579f77SDag-Erling Smørgrav static int
3435b7579f77SDag-Erling Smørgrav processPrimeResponse(struct module_qstate* qstate, int id)
3436b7579f77SDag-Erling Smørgrav {
3437b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3438b7579f77SDag-Erling Smørgrav 	enum response_type type;
3439b7579f77SDag-Erling Smørgrav 	iq->response->rep->flags &= ~(BIT_RD|BIT_RA); /* ignore rec-lame */
3440b7579f77SDag-Erling Smørgrav 	type = response_type_from_server(
3441b7579f77SDag-Erling Smørgrav 		(int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
3442b7579f77SDag-Erling Smørgrav 		iq->response, &iq->qchase, iq->dp);
3443b7579f77SDag-Erling Smørgrav 	if(type == RESPONSE_TYPE_ANSWER) {
3444b7579f77SDag-Erling Smørgrav 		qstate->return_rcode = LDNS_RCODE_NOERROR;
3445b7579f77SDag-Erling Smørgrav 		qstate->return_msg = iq->response;
3446b7579f77SDag-Erling Smørgrav 	} else {
34474c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "prime response did not get an answer");
34484c75e3aaSDag-Erling Smørgrav 		errinf_dname(qstate, "for", qstate->qinfo.qname);
3449b7579f77SDag-Erling Smørgrav 		qstate->return_rcode = LDNS_RCODE_SERVFAIL;
3450b7579f77SDag-Erling Smørgrav 		qstate->return_msg = NULL;
3451b7579f77SDag-Erling Smørgrav 	}
3452b7579f77SDag-Erling Smørgrav 
3453b7579f77SDag-Erling Smørgrav 	/* validate the root or stub after priming (if enabled).
3454b7579f77SDag-Erling Smørgrav 	 * This is the same query as the prime query, but with validation.
3455b7579f77SDag-Erling Smørgrav 	 * Now that we are primed, the additional queries that validation
3456c0caa2e2SCy Schubert 	 * may need can be resolved. */
3457b7579f77SDag-Erling Smørgrav 	if(qstate->env->cfg->harden_referral_path) {
3458b7579f77SDag-Erling Smørgrav 		struct module_qstate* subq = NULL;
3459b7579f77SDag-Erling Smørgrav 		log_nametypeclass(VERB_ALGO, "schedule prime validation",
3460b7579f77SDag-Erling Smørgrav 			qstate->qinfo.qname, qstate->qinfo.qtype,
3461b7579f77SDag-Erling Smørgrav 			qstate->qinfo.qclass);
3462b7579f77SDag-Erling Smørgrav 		if(!generate_sub_request(qstate->qinfo.qname,
3463b7579f77SDag-Erling Smørgrav 			qstate->qinfo.qname_len, qstate->qinfo.qtype,
3464b7579f77SDag-Erling Smørgrav 			qstate->qinfo.qclass, qstate, id, iq,
3465091e9e46SCy Schubert 			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1, 0)) {
3466b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "could not generate prime check");
3467b7579f77SDag-Erling Smørgrav 		}
3468b7579f77SDag-Erling Smørgrav 		generate_a_aaaa_check(qstate, iq, id);
3469b7579f77SDag-Erling Smørgrav 	}
3470b7579f77SDag-Erling Smørgrav 
3471b7579f77SDag-Erling Smørgrav 	/* This event is finished. */
3472b7579f77SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
3473b7579f77SDag-Erling Smørgrav 	return 0;
3474b7579f77SDag-Erling Smørgrav }
3475b7579f77SDag-Erling Smørgrav 
3476b7579f77SDag-Erling Smørgrav /**
3477b7579f77SDag-Erling Smørgrav  * Do final processing on responses to target queries. Events reach this
3478b7579f77SDag-Erling Smørgrav  * state after the iterative resolution algorithm terminates. This state is
34798a384985SDag-Erling Smørgrav  * responsible for reactivating the original event, and housekeeping related
3480b7579f77SDag-Erling Smørgrav  * to received target responses (caching, updating the current delegation
3481b7579f77SDag-Erling Smørgrav  * point, etc).
3482b7579f77SDag-Erling Smørgrav  * Callback from walk_supers for every super state that is interested in
3483b7579f77SDag-Erling Smørgrav  * the results from this query.
3484b7579f77SDag-Erling Smørgrav  *
3485b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3486b7579f77SDag-Erling Smørgrav  * @param id: module id.
3487b7579f77SDag-Erling Smørgrav  * @param forq: super query state.
3488b7579f77SDag-Erling Smørgrav  */
3489b7579f77SDag-Erling Smørgrav static void
3490b7579f77SDag-Erling Smørgrav processTargetResponse(struct module_qstate* qstate, int id,
3491b7579f77SDag-Erling Smørgrav 	struct module_qstate* forq)
3492b7579f77SDag-Erling Smørgrav {
3493091e9e46SCy Schubert 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
3494b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3495b7579f77SDag-Erling Smørgrav 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
3496b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset;
3497b7579f77SDag-Erling Smørgrav 	struct delegpt_ns* dpns;
3498b7579f77SDag-Erling Smørgrav 	log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
3499b7579f77SDag-Erling Smørgrav 
3500b7579f77SDag-Erling Smørgrav 	foriq->state = QUERYTARGETS_STATE;
3501b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_ALGO, "processTargetResponse", &qstate->qinfo);
3502b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_ALGO, "processTargetResponse super", &forq->qinfo);
3503b7579f77SDag-Erling Smørgrav 
3504b5663de9SDag-Erling Smørgrav 	/* Tell the originating event that this target query has finished
3505b5663de9SDag-Erling Smørgrav 	 * (regardless if it succeeded or not). */
3506b5663de9SDag-Erling Smørgrav 	foriq->num_target_queries--;
3507b5663de9SDag-Erling Smørgrav 
3508b7579f77SDag-Erling Smørgrav 	/* check to see if parent event is still interested (in orig name).  */
3509b7579f77SDag-Erling Smørgrav 	if(!foriq->dp) {
3510b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "subq: parent not interested, was reset");
3511b7579f77SDag-Erling Smørgrav 		return; /* not interested anymore */
3512b7579f77SDag-Erling Smørgrav 	}
3513b7579f77SDag-Erling Smørgrav 	dpns = delegpt_find_ns(foriq->dp, qstate->qinfo.qname,
3514b7579f77SDag-Erling Smørgrav 			qstate->qinfo.qname_len);
3515b7579f77SDag-Erling Smørgrav 	if(!dpns) {
3516b7579f77SDag-Erling Smørgrav 		/* If not interested, just stop processing this event */
3517b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "subq: parent not interested anymore");
3518b7579f77SDag-Erling Smørgrav 		/* could be because parent was jostled out of the cache,
3519b7579f77SDag-Erling Smørgrav 		   and a new identical query arrived, that does not want it*/
3520b7579f77SDag-Erling Smørgrav 		return;
3521b7579f77SDag-Erling Smørgrav 	}
3522b7579f77SDag-Erling Smørgrav 
3523b7579f77SDag-Erling Smørgrav 	/* if iq->query_for_pside_glue then add the pside_glue (marked lame) */
3524b7579f77SDag-Erling Smørgrav 	if(iq->pside_glue) {
3525b7579f77SDag-Erling Smørgrav 		/* if the pside_glue is NULL, then it could not be found,
3526b7579f77SDag-Erling Smørgrav 		 * the done_pside is already set when created and a cache
3527b7579f77SDag-Erling Smørgrav 		 * entry created in processFinished so nothing to do here */
3528b7579f77SDag-Erling Smørgrav 		log_rrset_key(VERB_ALGO, "add parentside glue to dp",
3529b7579f77SDag-Erling Smørgrav 			iq->pside_glue);
3530b7579f77SDag-Erling Smørgrav 		if(!delegpt_add_rrset(foriq->dp, forq->region,
3531091e9e46SCy Schubert 			iq->pside_glue, 1, NULL))
3532b7579f77SDag-Erling Smørgrav 			log_err("out of memory adding pside glue");
3533b7579f77SDag-Erling Smørgrav 	}
3534b7579f77SDag-Erling Smørgrav 
3535b7579f77SDag-Erling Smørgrav 	/* This response is relevant to the current query, so we
3536b7579f77SDag-Erling Smørgrav 	 * add (attempt to add, anyway) this target(s) and reactivate
3537b7579f77SDag-Erling Smørgrav 	 * the original event.
3538b7579f77SDag-Erling Smørgrav 	 * NOTE: we could only look for the AnswerRRset if the
3539b7579f77SDag-Erling Smørgrav 	 * response type was ANSWER. */
3540b7579f77SDag-Erling Smørgrav 	rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
3541b7579f77SDag-Erling Smørgrav 	if(rrset) {
3542091e9e46SCy Schubert 		int additions = 0;
3543b7579f77SDag-Erling Smørgrav 		/* if CNAMEs have been followed - add new NS to delegpt. */
3544b7579f77SDag-Erling Smørgrav 		/* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
3545b7579f77SDag-Erling Smørgrav 		if(!delegpt_find_ns(foriq->dp, rrset->rk.dname,
3546b7579f77SDag-Erling Smørgrav 			rrset->rk.dname_len)) {
3547b7579f77SDag-Erling Smørgrav 			/* if dpns->lame then set newcname ns lame too */
3548b7579f77SDag-Erling Smørgrav 			if(!delegpt_add_ns(foriq->dp, forq->region,
35499cf5bc93SCy Schubert 				rrset->rk.dname, dpns->lame, dpns->tls_auth_name,
35509cf5bc93SCy Schubert 				dpns->port))
3551b7579f77SDag-Erling Smørgrav 				log_err("out of memory adding cnamed-ns");
3552b7579f77SDag-Erling Smørgrav 		}
3553b7579f77SDag-Erling Smørgrav 		/* if dpns->lame then set the address(es) lame too */
3554b7579f77SDag-Erling Smørgrav 		if(!delegpt_add_rrset(foriq->dp, forq->region, rrset,
3555091e9e46SCy Schubert 			dpns->lame, &additions))
3556b7579f77SDag-Erling Smørgrav 			log_err("out of memory adding targets");
3557091e9e46SCy Schubert 		if(!additions) {
3558091e9e46SCy Schubert 			/* no new addresses, increase the nxns counter, like
3559091e9e46SCy Schubert 			 * this could be a list of wildcards with no new
3560091e9e46SCy Schubert 			 * addresses */
3561091e9e46SCy Schubert 			target_count_increase_nx(foriq, 1);
3562091e9e46SCy Schubert 		}
3563b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "added target response");
3564b7579f77SDag-Erling Smørgrav 		delegpt_log(VERB_ALGO, foriq->dp);
3565b7579f77SDag-Erling Smørgrav 	} else {
3566b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "iterator TargetResponse failed");
3567091e9e46SCy Schubert 		delegpt_mark_neg(dpns, qstate->qinfo.qtype);
35688cee2ebaSCy Schubert 		if((dpns->got4 == 2 || !ie->supports_ipv4) &&
3569*865f46b2SCy Schubert 			(dpns->got6 == 2 || !ie->supports_ipv6)) {
3570*865f46b2SCy Schubert 			dpns->resolved = 1; /* fail the target */
35710a92a9fcSCy Schubert 			/* do not count cached answers */
3572*865f46b2SCy Schubert 			if(qstate->reply_origin && qstate->reply_origin->len != 0) {
3573091e9e46SCy Schubert 				target_count_increase_nx(foriq, 1);
3574b7579f77SDag-Erling Smørgrav 			}
3575b7579f77SDag-Erling Smørgrav 		}
35760a92a9fcSCy Schubert 	}
3577*865f46b2SCy Schubert }
3578b7579f77SDag-Erling Smørgrav 
3579b7579f77SDag-Erling Smørgrav /**
3580b7579f77SDag-Erling Smørgrav  * Process response for DS NS Find queries, that attempt to find the delegation
3581b7579f77SDag-Erling Smørgrav  * point where we ask the DS query from.
3582b7579f77SDag-Erling Smørgrav  *
3583b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3584b7579f77SDag-Erling Smørgrav  * @param id: module id.
3585b7579f77SDag-Erling Smørgrav  * @param forq: super query state.
3586b7579f77SDag-Erling Smørgrav  */
3587b7579f77SDag-Erling Smørgrav static void
3588b7579f77SDag-Erling Smørgrav processDSNSResponse(struct module_qstate* qstate, int id,
3589b7579f77SDag-Erling Smørgrav 	struct module_qstate* forq)
3590b7579f77SDag-Erling Smørgrav {
3591b7579f77SDag-Erling Smørgrav 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
3592b7579f77SDag-Erling Smørgrav 
3593b7579f77SDag-Erling Smørgrav 	/* if the finished (iq->response) query has no NS set: continue
3594b7579f77SDag-Erling Smørgrav 	 * up to look for the right dp; nothing to change, do DPNSstate */
3595b7579f77SDag-Erling Smørgrav 	if(qstate->return_rcode != LDNS_RCODE_NOERROR)
3596b7579f77SDag-Erling Smørgrav 		return; /* seek further */
3597b7579f77SDag-Erling Smørgrav 	/* find the NS RRset (without allowing CNAMEs) */
3598b7579f77SDag-Erling Smørgrav 	if(!reply_find_rrset(qstate->return_msg->rep, qstate->qinfo.qname,
3599b7579f77SDag-Erling Smørgrav 		qstate->qinfo.qname_len, LDNS_RR_TYPE_NS,
3600b7579f77SDag-Erling Smørgrav 		qstate->qinfo.qclass)){
3601b7579f77SDag-Erling Smørgrav 		return; /* seek further */
3602b7579f77SDag-Erling Smørgrav 	}
3603b7579f77SDag-Erling Smørgrav 
3604b7579f77SDag-Erling Smørgrav 	/* else, store as DP and continue at querytargets */
3605b7579f77SDag-Erling Smørgrav 	foriq->state = QUERYTARGETS_STATE;
3606b7579f77SDag-Erling Smørgrav 	foriq->dp = delegpt_from_message(qstate->return_msg, forq->region);
3607b7579f77SDag-Erling Smørgrav 	if(!foriq->dp) {
3608b7579f77SDag-Erling Smørgrav 		log_err("out of memory in dsns dp alloc");
36094c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "malloc failure, in DS search");
3610b7579f77SDag-Erling Smørgrav 		return; /* dp==NULL in QUERYTARGETS makes SERVFAIL */
3611b7579f77SDag-Erling Smørgrav 	}
3612b7579f77SDag-Erling Smørgrav 	/* success, go query the querytargets in the new dp (and go down) */
3613b7579f77SDag-Erling Smørgrav }
3614b7579f77SDag-Erling Smørgrav 
3615b7579f77SDag-Erling Smørgrav /**
3616b7579f77SDag-Erling Smørgrav  * Process response for qclass=ANY queries for a particular class.
3617b7579f77SDag-Erling Smørgrav  * Append to result or error-exit.
3618b7579f77SDag-Erling Smørgrav  *
3619b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3620b7579f77SDag-Erling Smørgrav  * @param id: module id.
3621b7579f77SDag-Erling Smørgrav  * @param forq: super query state.
3622b7579f77SDag-Erling Smørgrav  */
3623b7579f77SDag-Erling Smørgrav static void
3624b7579f77SDag-Erling Smørgrav processClassResponse(struct module_qstate* qstate, int id,
3625b7579f77SDag-Erling Smørgrav 	struct module_qstate* forq)
3626b7579f77SDag-Erling Smørgrav {
3627b7579f77SDag-Erling Smørgrav 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
3628b7579f77SDag-Erling Smørgrav 	struct dns_msg* from = qstate->return_msg;
3629b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_ALGO, "processClassResponse", &qstate->qinfo);
3630b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_ALGO, "processClassResponse super", &forq->qinfo);
3631b7579f77SDag-Erling Smørgrav 	if(qstate->return_rcode != LDNS_RCODE_NOERROR) {
3632b7579f77SDag-Erling Smørgrav 		/* cause servfail for qclass ANY query */
3633b7579f77SDag-Erling Smørgrav 		foriq->response = NULL;
3634b7579f77SDag-Erling Smørgrav 		foriq->state = FINISHED_STATE;
3635b7579f77SDag-Erling Smørgrav 		return;
3636b7579f77SDag-Erling Smørgrav 	}
3637b7579f77SDag-Erling Smørgrav 	/* append result */
3638b7579f77SDag-Erling Smørgrav 	if(!foriq->response) {
3639b7579f77SDag-Erling Smørgrav 		/* allocate the response: copy RCODE, sec_state */
3640b7579f77SDag-Erling Smørgrav 		foriq->response = dns_copy_msg(from, forq->region);
3641b7579f77SDag-Erling Smørgrav 		if(!foriq->response) {
3642b7579f77SDag-Erling Smørgrav 			log_err("malloc failed for qclass ANY response");
3643b7579f77SDag-Erling Smørgrav 			foriq->state = FINISHED_STATE;
3644b7579f77SDag-Erling Smørgrav 			return;
3645b7579f77SDag-Erling Smørgrav 		}
3646b7579f77SDag-Erling Smørgrav 		foriq->response->qinfo.qclass = forq->qinfo.qclass;
3647b7579f77SDag-Erling Smørgrav 		/* qclass ANY does not receive the AA flag on replies */
3648b7579f77SDag-Erling Smørgrav 		foriq->response->rep->authoritative = 0;
3649b7579f77SDag-Erling Smørgrav 	} else {
3650b7579f77SDag-Erling Smørgrav 		struct dns_msg* to = foriq->response;
3651b7579f77SDag-Erling Smørgrav 		/* add _from_ this response _to_ existing collection */
3652b7579f77SDag-Erling Smørgrav 		/* if there are records, copy RCODE */
3653b7579f77SDag-Erling Smørgrav 		/* lower sec_state if this message is lower */
3654b7579f77SDag-Erling Smørgrav 		if(from->rep->rrset_count != 0) {
3655b7579f77SDag-Erling Smørgrav 			size_t n = from->rep->rrset_count+to->rep->rrset_count;
3656b7579f77SDag-Erling Smørgrav 			struct ub_packed_rrset_key** dest, **d;
3657b7579f77SDag-Erling Smørgrav 			/* copy appropriate rcode */
3658b7579f77SDag-Erling Smørgrav 			to->rep->flags = from->rep->flags;
3659b7579f77SDag-Erling Smørgrav 			/* copy rrsets */
366009a3aaf3SDag-Erling Smørgrav 			if(from->rep->rrset_count > RR_COUNT_MAX ||
366109a3aaf3SDag-Erling Smørgrav 				to->rep->rrset_count > RR_COUNT_MAX) {
366209a3aaf3SDag-Erling Smørgrav 				log_err("malloc failed (too many rrsets) in collect ANY");
366309a3aaf3SDag-Erling Smørgrav 				foriq->state = FINISHED_STATE;
366409a3aaf3SDag-Erling Smørgrav 				return; /* integer overflow protection */
366509a3aaf3SDag-Erling Smørgrav 			}
3666b7579f77SDag-Erling Smørgrav 			dest = regional_alloc(forq->region, sizeof(dest[0])*n);
3667b7579f77SDag-Erling Smørgrav 			if(!dest) {
3668b7579f77SDag-Erling Smørgrav 				log_err("malloc failed in collect ANY");
3669b7579f77SDag-Erling Smørgrav 				foriq->state = FINISHED_STATE;
3670b7579f77SDag-Erling Smørgrav 				return;
3671b7579f77SDag-Erling Smørgrav 			}
3672b7579f77SDag-Erling Smørgrav 			d = dest;
3673b7579f77SDag-Erling Smørgrav 			/* copy AN */
3674b7579f77SDag-Erling Smørgrav 			memcpy(dest, to->rep->rrsets, to->rep->an_numrrsets
3675b7579f77SDag-Erling Smørgrav 				* sizeof(dest[0]));
3676b7579f77SDag-Erling Smørgrav 			dest += to->rep->an_numrrsets;
3677b7579f77SDag-Erling Smørgrav 			memcpy(dest, from->rep->rrsets, from->rep->an_numrrsets
3678b7579f77SDag-Erling Smørgrav 				* sizeof(dest[0]));
3679b7579f77SDag-Erling Smørgrav 			dest += from->rep->an_numrrsets;
3680b7579f77SDag-Erling Smørgrav 			/* copy NS */
3681b7579f77SDag-Erling Smørgrav 			memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets,
3682b7579f77SDag-Erling Smørgrav 				to->rep->ns_numrrsets * sizeof(dest[0]));
3683b7579f77SDag-Erling Smørgrav 			dest += to->rep->ns_numrrsets;
3684b7579f77SDag-Erling Smørgrav 			memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets,
3685b7579f77SDag-Erling Smørgrav 				from->rep->ns_numrrsets * sizeof(dest[0]));
3686b7579f77SDag-Erling Smørgrav 			dest += from->rep->ns_numrrsets;
3687b7579f77SDag-Erling Smørgrav 			/* copy AR */
3688b7579f77SDag-Erling Smørgrav 			memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets+
3689b7579f77SDag-Erling Smørgrav 				to->rep->ns_numrrsets,
3690b7579f77SDag-Erling Smørgrav 				to->rep->ar_numrrsets * sizeof(dest[0]));
3691b7579f77SDag-Erling Smørgrav 			dest += to->rep->ar_numrrsets;
3692b7579f77SDag-Erling Smørgrav 			memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets+
3693b7579f77SDag-Erling Smørgrav 				from->rep->ns_numrrsets,
3694b7579f77SDag-Erling Smørgrav 				from->rep->ar_numrrsets * sizeof(dest[0]));
3695b7579f77SDag-Erling Smørgrav 			/* update counts */
3696b7579f77SDag-Erling Smørgrav 			to->rep->rrsets = d;
3697b7579f77SDag-Erling Smørgrav 			to->rep->an_numrrsets += from->rep->an_numrrsets;
3698b7579f77SDag-Erling Smørgrav 			to->rep->ns_numrrsets += from->rep->ns_numrrsets;
3699b7579f77SDag-Erling Smørgrav 			to->rep->ar_numrrsets += from->rep->ar_numrrsets;
3700b7579f77SDag-Erling Smørgrav 			to->rep->rrset_count = n;
3701b7579f77SDag-Erling Smørgrav 		}
3702b7579f77SDag-Erling Smørgrav 		if(from->rep->security < to->rep->security) /* lowest sec */
3703b7579f77SDag-Erling Smørgrav 			to->rep->security = from->rep->security;
3704b7579f77SDag-Erling Smørgrav 		if(from->rep->qdcount != 0) /* insert qd if appropriate */
3705b7579f77SDag-Erling Smørgrav 			to->rep->qdcount = from->rep->qdcount;
3706b7579f77SDag-Erling Smørgrav 		if(from->rep->ttl < to->rep->ttl) /* use smallest TTL */
3707b7579f77SDag-Erling Smørgrav 			to->rep->ttl = from->rep->ttl;
3708b7579f77SDag-Erling Smørgrav 		if(from->rep->prefetch_ttl < to->rep->prefetch_ttl)
3709b7579f77SDag-Erling Smørgrav 			to->rep->prefetch_ttl = from->rep->prefetch_ttl;
37104c75e3aaSDag-Erling Smørgrav 		if(from->rep->serve_expired_ttl < to->rep->serve_expired_ttl)
37114c75e3aaSDag-Erling Smørgrav 			to->rep->serve_expired_ttl = from->rep->serve_expired_ttl;
3712b7579f77SDag-Erling Smørgrav 	}
3713b7579f77SDag-Erling Smørgrav 	/* are we done? */
3714b7579f77SDag-Erling Smørgrav 	foriq->num_current_queries --;
3715b7579f77SDag-Erling Smørgrav 	if(foriq->num_current_queries == 0)
3716b7579f77SDag-Erling Smørgrav 		foriq->state = FINISHED_STATE;
3717b7579f77SDag-Erling Smørgrav }
3718b7579f77SDag-Erling Smørgrav 
3719b7579f77SDag-Erling Smørgrav /**
3720b7579f77SDag-Erling Smørgrav  * Collect class ANY responses and make them into one response.  This
3721b7579f77SDag-Erling Smørgrav  * state is started and it creates queries for all classes (that have
3722b7579f77SDag-Erling Smørgrav  * root hints).  The answers are then collected.
3723b7579f77SDag-Erling Smørgrav  *
3724b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3725b7579f77SDag-Erling Smørgrav  * @param id: module id.
3726b7579f77SDag-Erling Smørgrav  * @return true if the event needs more immediate processing, false if not.
3727b7579f77SDag-Erling Smørgrav  */
3728b7579f77SDag-Erling Smørgrav static int
3729b7579f77SDag-Erling Smørgrav processCollectClass(struct module_qstate* qstate, int id)
3730b7579f77SDag-Erling Smørgrav {
3731b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3732b7579f77SDag-Erling Smørgrav 	struct module_qstate* subq;
3733b7579f77SDag-Erling Smørgrav 	/* If qchase.qclass == 0 then send out queries for all classes.
3734b7579f77SDag-Erling Smørgrav 	 * Otherwise, do nothing (wait for all answers to arrive and the
3735b7579f77SDag-Erling Smørgrav 	 * processClassResponse to put them together, and that moves us
3736b7579f77SDag-Erling Smørgrav 	 * towards the Finished state when done. */
3737b7579f77SDag-Erling Smørgrav 	if(iq->qchase.qclass == 0) {
3738b7579f77SDag-Erling Smørgrav 		uint16_t c = 0;
3739b7579f77SDag-Erling Smørgrav 		iq->qchase.qclass = LDNS_RR_CLASS_ANY;
3740b7579f77SDag-Erling Smørgrav 		while(iter_get_next_root(qstate->env->hints,
3741b7579f77SDag-Erling Smørgrav 			qstate->env->fwds, &c)) {
3742b7579f77SDag-Erling Smørgrav 			/* generate query for this class */
3743b7579f77SDag-Erling Smørgrav 			log_nametypeclass(VERB_ALGO, "spawn collect query",
3744b7579f77SDag-Erling Smørgrav 				qstate->qinfo.qname, qstate->qinfo.qtype, c);
3745b7579f77SDag-Erling Smørgrav 			if(!generate_sub_request(qstate->qinfo.qname,
3746b7579f77SDag-Erling Smørgrav 				qstate->qinfo.qname_len, qstate->qinfo.qtype,
3747b7579f77SDag-Erling Smørgrav 				c, qstate, id, iq, INIT_REQUEST_STATE,
3748b7579f77SDag-Erling Smørgrav 				FINISHED_STATE, &subq,
3749091e9e46SCy Schubert 				(int)!(qstate->query_flags&BIT_CD), 0)) {
37504c75e3aaSDag-Erling Smørgrav 				errinf(qstate, "could not generate class ANY"
37514c75e3aaSDag-Erling Smørgrav 					" lookup query");
3752b7579f77SDag-Erling Smørgrav 				return error_response(qstate, id,
3753b7579f77SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
3754b7579f77SDag-Erling Smørgrav 			}
3755b7579f77SDag-Erling Smørgrav 			/* ignore subq, no special init required */
3756b7579f77SDag-Erling Smørgrav 			iq->num_current_queries ++;
3757b7579f77SDag-Erling Smørgrav 			if(c == 0xffff)
3758b7579f77SDag-Erling Smørgrav 				break;
3759b7579f77SDag-Erling Smørgrav 			else c++;
3760b7579f77SDag-Erling Smørgrav 		}
3761b7579f77SDag-Erling Smørgrav 		/* if no roots are configured at all, return */
3762b7579f77SDag-Erling Smørgrav 		if(iq->num_current_queries == 0) {
3763b7579f77SDag-Erling Smørgrav 			verbose(VERB_ALGO, "No root hints or fwds, giving up "
3764b7579f77SDag-Erling Smørgrav 				"on qclass ANY");
3765b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_REFUSED);
3766b7579f77SDag-Erling Smørgrav 		}
3767b7579f77SDag-Erling Smørgrav 		/* return false, wait for queries to return */
3768b7579f77SDag-Erling Smørgrav 	}
3769b7579f77SDag-Erling Smørgrav 	/* if woke up here because of an answer, wait for more answers */
3770b7579f77SDag-Erling Smørgrav 	return 0;
3771b7579f77SDag-Erling Smørgrav }
3772b7579f77SDag-Erling Smørgrav 
3773b7579f77SDag-Erling Smørgrav /**
3774b7579f77SDag-Erling Smørgrav  * This handles the final state for first-tier responses (i.e., responses to
3775b7579f77SDag-Erling Smørgrav  * externally generated queries).
3776b7579f77SDag-Erling Smørgrav  *
3777b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3778b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
3779b7579f77SDag-Erling Smørgrav  * @param id: module id.
3780b7579f77SDag-Erling Smørgrav  * @return true if the event needs more processing, false if not. Since this
3781b7579f77SDag-Erling Smørgrav  *         is the final state for an event, it always returns false.
3782b7579f77SDag-Erling Smørgrav  */
3783b7579f77SDag-Erling Smørgrav static int
3784b7579f77SDag-Erling Smørgrav processFinished(struct module_qstate* qstate, struct iter_qstate* iq,
3785b7579f77SDag-Erling Smørgrav 	int id)
3786b7579f77SDag-Erling Smørgrav {
3787b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_QUERY, "finishing processing for",
3788b7579f77SDag-Erling Smørgrav 		&qstate->qinfo);
3789b7579f77SDag-Erling Smørgrav 
3790b7579f77SDag-Erling Smørgrav 	/* store negative cache element for parent side glue. */
3791bc892140SDag-Erling Smørgrav 	if(!qstate->no_cache_store && iq->query_for_pside_glue
3792bc892140SDag-Erling Smørgrav 		&& !iq->pside_glue)
3793b7579f77SDag-Erling Smørgrav 			iter_store_parentside_neg(qstate->env, &qstate->qinfo,
3794b7579f77SDag-Erling Smørgrav 				iq->deleg_msg?iq->deleg_msg->rep:
3795b7579f77SDag-Erling Smørgrav 				(iq->response?iq->response->rep:NULL));
3796b7579f77SDag-Erling Smørgrav 	if(!iq->response) {
3797b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "No response is set, servfail");
37984c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "(no response found at query finish)");
3799b7579f77SDag-Erling Smørgrav 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3800b7579f77SDag-Erling Smørgrav 	}
3801b7579f77SDag-Erling Smørgrav 
3802b7579f77SDag-Erling Smørgrav 	/* Make sure that the RA flag is set (since the presence of
3803b7579f77SDag-Erling Smørgrav 	 * this module means that recursion is available) */
3804b7579f77SDag-Erling Smørgrav 	iq->response->rep->flags |= BIT_RA;
3805b7579f77SDag-Erling Smørgrav 
3806b7579f77SDag-Erling Smørgrav 	/* Clear the AA flag */
3807b7579f77SDag-Erling Smørgrav 	/* FIXME: does this action go here or in some other module? */
3808b7579f77SDag-Erling Smørgrav 	iq->response->rep->flags &= ~BIT_AA;
3809b7579f77SDag-Erling Smørgrav 
3810b7579f77SDag-Erling Smørgrav 	/* make sure QR flag is on */
3811b7579f77SDag-Erling Smørgrav 	iq->response->rep->flags |= BIT_QR;
3812b7579f77SDag-Erling Smørgrav 
3813b7579f77SDag-Erling Smørgrav 	/* we have finished processing this query */
3814b7579f77SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
3815b7579f77SDag-Erling Smørgrav 
3816b7579f77SDag-Erling Smørgrav 	/* TODO:  we are using a private TTL, trim the response. */
3817b7579f77SDag-Erling Smørgrav 	/* if (mPrivateTTL > 0){IterUtils.setPrivateTTL(resp, mPrivateTTL); } */
3818b7579f77SDag-Erling Smørgrav 
3819b7579f77SDag-Erling Smørgrav 	/* prepend any items we have accumulated */
3820b7579f77SDag-Erling Smørgrav 	if(iq->an_prepend_list || iq->ns_prepend_list) {
3821b7579f77SDag-Erling Smørgrav 		if(!iter_prepend(iq, iq->response, qstate->region)) {
3822b7579f77SDag-Erling Smørgrav 			log_err("prepend rrsets: out of memory");
3823b7579f77SDag-Erling Smørgrav 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3824b7579f77SDag-Erling Smørgrav 		}
3825b7579f77SDag-Erling Smørgrav 		/* reset the query name back */
3826b7579f77SDag-Erling Smørgrav 		iq->response->qinfo = qstate->qinfo;
3827b7579f77SDag-Erling Smørgrav 		/* the security state depends on the combination */
3828b7579f77SDag-Erling Smørgrav 		iq->response->rep->security = sec_status_unchecked;
3829b7579f77SDag-Erling Smørgrav 		/* store message with the finished prepended items,
3830b7579f77SDag-Erling Smørgrav 		 * but only if we did recursion. The nonrecursion referral
3831b7579f77SDag-Erling Smørgrav 		 * from cache does not need to be stored in the msg cache. */
3832bc892140SDag-Erling Smørgrav 		if(!qstate->no_cache_store && qstate->query_flags&BIT_RD) {
38338ed2b524SDag-Erling Smørgrav 			iter_dns_store(qstate->env, &qstate->qinfo,
3834b7579f77SDag-Erling Smørgrav 				iq->response->rep, 0, qstate->prefetch_leeway,
3835b7579f77SDag-Erling Smørgrav 				iq->dp&&iq->dp->has_parent_side_NS,
3836790c6b24SCy Schubert 				qstate->region, qstate->query_flags,
3837790c6b24SCy Schubert 				qstate->qstarttime);
3838b7579f77SDag-Erling Smørgrav 		}
3839b7579f77SDag-Erling Smørgrav 	}
3840b7579f77SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
3841b7579f77SDag-Erling Smørgrav 	qstate->return_msg = iq->response;
3842b7579f77SDag-Erling Smørgrav 	return 0;
3843b7579f77SDag-Erling Smørgrav }
3844b7579f77SDag-Erling Smørgrav 
3845b7579f77SDag-Erling Smørgrav /*
38468a384985SDag-Erling Smørgrav  * Return priming query results to interested super querystates.
3847b7579f77SDag-Erling Smørgrav  *
3848b7579f77SDag-Erling Smørgrav  * Sets the delegation point and delegation message (not nonRD queries).
3849b7579f77SDag-Erling Smørgrav  * This is a callback from walk_supers.
3850b7579f77SDag-Erling Smørgrav  *
3851b7579f77SDag-Erling Smørgrav  * @param qstate: query state that finished.
3852b7579f77SDag-Erling Smørgrav  * @param id: module id.
3853b7579f77SDag-Erling Smørgrav  * @param super: the qstate to inform.
3854b7579f77SDag-Erling Smørgrav  */
3855b7579f77SDag-Erling Smørgrav void
3856b7579f77SDag-Erling Smørgrav iter_inform_super(struct module_qstate* qstate, int id,
3857b7579f77SDag-Erling Smørgrav 	struct module_qstate* super)
3858b7579f77SDag-Erling Smørgrav {
3859b7579f77SDag-Erling Smørgrav 	if(!qstate->is_priming && super->qinfo.qclass == LDNS_RR_CLASS_ANY)
3860b7579f77SDag-Erling Smørgrav 		processClassResponse(qstate, id, super);
3861b7579f77SDag-Erling Smørgrav 	else if(super->qinfo.qtype == LDNS_RR_TYPE_DS && ((struct iter_qstate*)
3862b7579f77SDag-Erling Smørgrav 		super->minfo[id])->state == DSNS_FIND_STATE)
3863b7579f77SDag-Erling Smørgrav 		processDSNSResponse(qstate, id, super);
3864b7579f77SDag-Erling Smørgrav 	else if(qstate->return_rcode != LDNS_RCODE_NOERROR)
3865b7579f77SDag-Erling Smørgrav 		error_supers(qstate, id, super);
3866b7579f77SDag-Erling Smørgrav 	else if(qstate->is_priming)
3867b7579f77SDag-Erling Smørgrav 		prime_supers(qstate, id, super);
3868b7579f77SDag-Erling Smørgrav 	else	processTargetResponse(qstate, id, super);
3869b7579f77SDag-Erling Smørgrav }
3870b7579f77SDag-Erling Smørgrav 
3871b7579f77SDag-Erling Smørgrav /**
3872b7579f77SDag-Erling Smørgrav  * Handle iterator state.
3873b7579f77SDag-Erling Smørgrav  * Handle events. This is the real processing loop for events, responsible
3874b7579f77SDag-Erling Smørgrav  * for moving events through the various states. If a processing method
3875b7579f77SDag-Erling Smørgrav  * returns true, then it will be advanced to the next state. If false, then
3876b7579f77SDag-Erling Smørgrav  * processing will stop.
3877b7579f77SDag-Erling Smørgrav  *
3878b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3879b7579f77SDag-Erling Smørgrav  * @param ie: iterator shared global environment.
3880b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
3881b7579f77SDag-Erling Smørgrav  * @param id: module id.
3882b7579f77SDag-Erling Smørgrav  */
3883b7579f77SDag-Erling Smørgrav static void
3884b7579f77SDag-Erling Smørgrav iter_handle(struct module_qstate* qstate, struct iter_qstate* iq,
3885b7579f77SDag-Erling Smørgrav 	struct iter_env* ie, int id)
3886b7579f77SDag-Erling Smørgrav {
3887b7579f77SDag-Erling Smørgrav 	int cont = 1;
3888b7579f77SDag-Erling Smørgrav 	while(cont) {
3889b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "iter_handle processing q with state %s",
3890b7579f77SDag-Erling Smørgrav 			iter_state_to_string(iq->state));
3891b7579f77SDag-Erling Smørgrav 		switch(iq->state) {
3892b7579f77SDag-Erling Smørgrav 			case INIT_REQUEST_STATE:
3893b7579f77SDag-Erling Smørgrav 				cont = processInitRequest(qstate, iq, ie, id);
3894b7579f77SDag-Erling Smørgrav 				break;
3895b7579f77SDag-Erling Smørgrav 			case INIT_REQUEST_2_STATE:
3896b7579f77SDag-Erling Smørgrav 				cont = processInitRequest2(qstate, iq, id);
3897b7579f77SDag-Erling Smørgrav 				break;
3898b7579f77SDag-Erling Smørgrav 			case INIT_REQUEST_3_STATE:
3899b7579f77SDag-Erling Smørgrav 				cont = processInitRequest3(qstate, iq, id);
3900b7579f77SDag-Erling Smørgrav 				break;
3901b7579f77SDag-Erling Smørgrav 			case QUERYTARGETS_STATE:
3902b7579f77SDag-Erling Smørgrav 				cont = processQueryTargets(qstate, iq, ie, id);
3903b7579f77SDag-Erling Smørgrav 				break;
3904b7579f77SDag-Erling Smørgrav 			case QUERY_RESP_STATE:
390524e36522SCy Schubert 				cont = processQueryResponse(qstate, iq, ie, id);
3906b7579f77SDag-Erling Smørgrav 				break;
3907b7579f77SDag-Erling Smørgrav 			case PRIME_RESP_STATE:
3908b7579f77SDag-Erling Smørgrav 				cont = processPrimeResponse(qstate, id);
3909b7579f77SDag-Erling Smørgrav 				break;
3910b7579f77SDag-Erling Smørgrav 			case COLLECT_CLASS_STATE:
3911b7579f77SDag-Erling Smørgrav 				cont = processCollectClass(qstate, id);
3912b7579f77SDag-Erling Smørgrav 				break;
3913b7579f77SDag-Erling Smørgrav 			case DSNS_FIND_STATE:
3914b7579f77SDag-Erling Smørgrav 				cont = processDSNSFind(qstate, iq, id);
3915b7579f77SDag-Erling Smørgrav 				break;
3916b7579f77SDag-Erling Smørgrav 			case FINISHED_STATE:
3917b7579f77SDag-Erling Smørgrav 				cont = processFinished(qstate, iq, id);
3918b7579f77SDag-Erling Smørgrav 				break;
3919b7579f77SDag-Erling Smørgrav 			default:
3920b7579f77SDag-Erling Smørgrav 				log_warn("iterator: invalid state: %d",
3921b7579f77SDag-Erling Smørgrav 					iq->state);
3922b7579f77SDag-Erling Smørgrav 				cont = 0;
3923b7579f77SDag-Erling Smørgrav 				break;
3924b7579f77SDag-Erling Smørgrav 		}
3925b7579f77SDag-Erling Smørgrav 	}
3926b7579f77SDag-Erling Smørgrav }
3927b7579f77SDag-Erling Smørgrav 
3928b7579f77SDag-Erling Smørgrav /**
3929b7579f77SDag-Erling Smørgrav  * This is the primary entry point for processing request events. Note that
3930b7579f77SDag-Erling Smørgrav  * this method should only be used by external modules.
3931b7579f77SDag-Erling Smørgrav  * @param qstate: query state.
3932b7579f77SDag-Erling Smørgrav  * @param ie: iterator shared global environment.
3933b7579f77SDag-Erling Smørgrav  * @param iq: iterator query state.
3934b7579f77SDag-Erling Smørgrav  * @param id: module id.
3935b7579f77SDag-Erling Smørgrav  */
3936b7579f77SDag-Erling Smørgrav static void
3937b7579f77SDag-Erling Smørgrav process_request(struct module_qstate* qstate, struct iter_qstate* iq,
3938b7579f77SDag-Erling Smørgrav 	struct iter_env* ie, int id)
3939b7579f77SDag-Erling Smørgrav {
3940b7579f77SDag-Erling Smørgrav 	/* external requests start in the INIT state, and finish using the
3941b7579f77SDag-Erling Smørgrav 	 * FINISHED state. */
3942b7579f77SDag-Erling Smørgrav 	iq->state = INIT_REQUEST_STATE;
3943b7579f77SDag-Erling Smørgrav 	iq->final_state = FINISHED_STATE;
3944b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "process_request: new external request event");
3945b7579f77SDag-Erling Smørgrav 	iter_handle(qstate, iq, ie, id);
3946b7579f77SDag-Erling Smørgrav }
3947b7579f77SDag-Erling Smørgrav 
3948b7579f77SDag-Erling Smørgrav /** process authoritative server reply */
3949b7579f77SDag-Erling Smørgrav static void
3950b7579f77SDag-Erling Smørgrav process_response(struct module_qstate* qstate, struct iter_qstate* iq,
3951b7579f77SDag-Erling Smørgrav 	struct iter_env* ie, int id, struct outbound_entry* outbound,
3952b7579f77SDag-Erling Smørgrav 	enum module_ev event)
3953b7579f77SDag-Erling Smørgrav {
3954b7579f77SDag-Erling Smørgrav 	struct msg_parse* prs;
3955b7579f77SDag-Erling Smørgrav 	struct edns_data edns;
395617d15b25SDag-Erling Smørgrav 	sldns_buffer* pkt;
3957b7579f77SDag-Erling Smørgrav 
3958b7579f77SDag-Erling Smørgrav 	verbose(VERB_ALGO, "process_response: new external response event");
3959b7579f77SDag-Erling Smørgrav 	iq->response = NULL;
3960b7579f77SDag-Erling Smørgrav 	iq->state = QUERY_RESP_STATE;
3961b7579f77SDag-Erling Smørgrav 	if(event == module_event_noreply || event == module_event_error) {
396225039b37SCy Schubert 		if(event == module_event_noreply && iq->timeout_count >= 3 &&
3963ff825849SDag-Erling Smørgrav 			qstate->env->cfg->use_caps_bits_for_id &&
3964e86b9096SDag-Erling Smørgrav 			!iq->caps_fallback && !is_caps_whitelisted(ie, iq)) {
3965ff825849SDag-Erling Smørgrav 			/* start fallback */
3966ff825849SDag-Erling Smørgrav 			iq->caps_fallback = 1;
3967ff825849SDag-Erling Smørgrav 			iq->caps_server = 0;
3968ff825849SDag-Erling Smørgrav 			iq->caps_reply = NULL;
396909a3aaf3SDag-Erling Smørgrav 			iq->caps_response = NULL;
39704c75e3aaSDag-Erling Smørgrav 			iq->caps_minimisation_state = DONOT_MINIMISE_STATE;
3971ff825849SDag-Erling Smørgrav 			iq->state = QUERYTARGETS_STATE;
3972ff825849SDag-Erling Smørgrav 			iq->num_current_queries--;
3973ff825849SDag-Erling Smørgrav 			/* need fresh attempts for the 0x20 fallback, if
3974ff825849SDag-Erling Smørgrav 			 * that was the cause for the failure */
397524e36522SCy Schubert 			iter_dec_attempts(iq->dp, 3, ie->outbound_msg_retry);
3976ff825849SDag-Erling Smørgrav 			verbose(VERB_DETAIL, "Capsforid: timeouts, starting fallback");
3977ff825849SDag-Erling Smørgrav 			goto handle_it;
3978ff825849SDag-Erling Smørgrav 		}
3979b7579f77SDag-Erling Smørgrav 		goto handle_it;
3980b7579f77SDag-Erling Smørgrav 	}
3981b7579f77SDag-Erling Smørgrav 	if( (event != module_event_reply && event != module_event_capsfail)
3982b7579f77SDag-Erling Smørgrav 		|| !qstate->reply) {
3983b7579f77SDag-Erling Smørgrav 		log_err("Bad event combined with response");
3984b7579f77SDag-Erling Smørgrav 		outbound_list_remove(&iq->outlist, outbound);
39854c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "module iterator received wrong internal event with a response message");
3986b7579f77SDag-Erling Smørgrav 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3987b7579f77SDag-Erling Smørgrav 		return;
3988b7579f77SDag-Erling Smørgrav 	}
3989b7579f77SDag-Erling Smørgrav 
3990b7579f77SDag-Erling Smørgrav 	/* parse message */
39915469a995SCy Schubert 	iq->fail_reply = qstate->reply;
3992b7579f77SDag-Erling Smørgrav 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
3993b7579f77SDag-Erling Smørgrav 		sizeof(struct msg_parse));
3994b7579f77SDag-Erling Smørgrav 	if(!prs) {
3995b7579f77SDag-Erling Smørgrav 		log_err("out of memory on incoming message");
3996b7579f77SDag-Erling Smørgrav 		/* like packet got dropped */
3997b7579f77SDag-Erling Smørgrav 		goto handle_it;
3998b7579f77SDag-Erling Smørgrav 	}
3999b7579f77SDag-Erling Smørgrav 	memset(prs, 0, sizeof(*prs));
4000b7579f77SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
4001b7579f77SDag-Erling Smørgrav 	pkt = qstate->reply->c->buffer;
400217d15b25SDag-Erling Smørgrav 	sldns_buffer_set_position(pkt, 0);
4003b7579f77SDag-Erling Smørgrav 	if(parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
4004b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "parse error on reply packet");
40055469a995SCy Schubert 		iq->parse_failures++;
4006b7579f77SDag-Erling Smørgrav 		goto handle_it;
4007b7579f77SDag-Erling Smørgrav 	}
4008b7579f77SDag-Erling Smørgrav 	/* edns is not examined, but removed from message to help cache */
400924e36522SCy Schubert 	if(parse_extract_edns_from_response_msg(prs, &edns, qstate->env->scratch) !=
40105469a995SCy Schubert 		LDNS_RCODE_NOERROR) {
40115469a995SCy Schubert 		iq->parse_failures++;
4012b7579f77SDag-Erling Smørgrav 		goto handle_it;
40135469a995SCy Schubert 	}
4014bc892140SDag-Erling Smørgrav 
4015bc892140SDag-Erling Smørgrav 	/* Copy the edns options we may got from the back end */
401624e36522SCy Schubert 	if(edns.opt_list_in) {
401724e36522SCy Schubert 		qstate->edns_opts_back_in = edns_opt_copy_region(edns.opt_list_in,
4018bc892140SDag-Erling Smørgrav 			qstate->region);
4019bc892140SDag-Erling Smørgrav 		if(!qstate->edns_opts_back_in) {
4020bc892140SDag-Erling Smørgrav 			log_err("out of memory on incoming message");
4021bc892140SDag-Erling Smørgrav 			/* like packet got dropped */
4022bc892140SDag-Erling Smørgrav 			goto handle_it;
4023bc892140SDag-Erling Smørgrav 		}
402465b390aaSDag-Erling Smørgrav 		if(!inplace_cb_edns_back_parsed_call(qstate->env, qstate)) {
402565b390aaSDag-Erling Smørgrav 			log_err("unable to call edns_back_parsed callback");
402665b390aaSDag-Erling Smørgrav 			goto handle_it;
402765b390aaSDag-Erling Smørgrav 		}
4028bc892140SDag-Erling Smørgrav 	}
4029bc892140SDag-Erling Smørgrav 
4030b7579f77SDag-Erling Smørgrav 	/* remove CD-bit, we asked for in case we handle validation ourself */
4031b7579f77SDag-Erling Smørgrav 	prs->flags &= ~BIT_CD;
4032b7579f77SDag-Erling Smørgrav 
4033b7579f77SDag-Erling Smørgrav 	/* normalize and sanitize: easy to delete items from linked lists */
403405ab2901SDag-Erling Smørgrav 	if(!scrub_message(pkt, prs, &iq->qinfo_out, iq->dp->name,
403509a3aaf3SDag-Erling Smørgrav 		qstate->env->scratch, qstate->env, ie)) {
403609a3aaf3SDag-Erling Smørgrav 		/* if 0x20 enabled, start fallback, but we have no message */
403709a3aaf3SDag-Erling Smørgrav 		if(event == module_event_capsfail && !iq->caps_fallback) {
403809a3aaf3SDag-Erling Smørgrav 			iq->caps_fallback = 1;
403909a3aaf3SDag-Erling Smørgrav 			iq->caps_server = 0;
404009a3aaf3SDag-Erling Smørgrav 			iq->caps_reply = NULL;
404109a3aaf3SDag-Erling Smørgrav 			iq->caps_response = NULL;
40424c75e3aaSDag-Erling Smørgrav 			iq->caps_minimisation_state = DONOT_MINIMISE_STATE;
404309a3aaf3SDag-Erling Smørgrav 			iq->state = QUERYTARGETS_STATE;
404409a3aaf3SDag-Erling Smørgrav 			iq->num_current_queries--;
404509a3aaf3SDag-Erling Smørgrav 			verbose(VERB_DETAIL, "Capsforid: scrub failed, starting fallback with no response");
404609a3aaf3SDag-Erling Smørgrav 		}
40475469a995SCy Schubert 		iq->scrub_failures++;
4048b7579f77SDag-Erling Smørgrav 		goto handle_it;
404909a3aaf3SDag-Erling Smørgrav 	}
4050b7579f77SDag-Erling Smørgrav 
4051b7579f77SDag-Erling Smørgrav 	/* allocate response dns_msg in region */
4052b7579f77SDag-Erling Smørgrav 	iq->response = dns_alloc_msg(pkt, prs, qstate->region);
4053b7579f77SDag-Erling Smørgrav 	if(!iq->response)
4054b7579f77SDag-Erling Smørgrav 		goto handle_it;
4055b7579f77SDag-Erling Smørgrav 	log_query_info(VERB_DETAIL, "response for", &qstate->qinfo);
4056b7579f77SDag-Erling Smørgrav 	log_name_addr(VERB_DETAIL, "reply from", iq->dp->name,
4057*865f46b2SCy Schubert 		&qstate->reply->remote_addr, qstate->reply->remote_addrlen);
4058b7579f77SDag-Erling Smørgrav 	if(verbosity >= VERB_ALGO)
4059b7579f77SDag-Erling Smørgrav 		log_dns_msg("incoming scrubbed packet:", &iq->response->qinfo,
4060b7579f77SDag-Erling Smørgrav 			iq->response->rep);
4061b7579f77SDag-Erling Smørgrav 
4062ff825849SDag-Erling Smørgrav 	if(event == module_event_capsfail || iq->caps_fallback) {
40634c75e3aaSDag-Erling Smørgrav 		if(qstate->env->cfg->qname_minimisation &&
40644c75e3aaSDag-Erling Smørgrav 			iq->minimisation_state != DONOT_MINIMISE_STATE) {
40654c75e3aaSDag-Erling Smørgrav 			/* Skip QNAME minimisation for next query, since that
40664c75e3aaSDag-Erling Smørgrav 			 * one has to match the current query. */
40674c75e3aaSDag-Erling Smørgrav 			iq->minimisation_state = SKIP_MINIMISE_STATE;
40684c75e3aaSDag-Erling Smørgrav 		}
40696480faa8SDag-Erling Smørgrav 		/* for fallback we care about main answer, not additionals */
40706480faa8SDag-Erling Smørgrav 		/* removing that makes comparison more likely to succeed */
40716480faa8SDag-Erling Smørgrav 		caps_strip_reply(iq->response->rep);
40724c75e3aaSDag-Erling Smørgrav 
40734c75e3aaSDag-Erling Smørgrav 		if(iq->caps_fallback &&
40744c75e3aaSDag-Erling Smørgrav 			iq->caps_minimisation_state != iq->minimisation_state) {
40754c75e3aaSDag-Erling Smørgrav 			/* QNAME minimisation state has changed, restart caps
40764c75e3aaSDag-Erling Smørgrav 			 * fallback. */
40774c75e3aaSDag-Erling Smørgrav 			iq->caps_fallback = 0;
40784c75e3aaSDag-Erling Smørgrav 		}
40794c75e3aaSDag-Erling Smørgrav 
4080b7579f77SDag-Erling Smørgrav 		if(!iq->caps_fallback) {
4081b7579f77SDag-Erling Smørgrav 			/* start fallback */
4082b7579f77SDag-Erling Smørgrav 			iq->caps_fallback = 1;
4083b7579f77SDag-Erling Smørgrav 			iq->caps_server = 0;
4084b7579f77SDag-Erling Smørgrav 			iq->caps_reply = iq->response->rep;
408509a3aaf3SDag-Erling Smørgrav 			iq->caps_response = iq->response;
40864c75e3aaSDag-Erling Smørgrav 			iq->caps_minimisation_state = iq->minimisation_state;
4087b7579f77SDag-Erling Smørgrav 			iq->state = QUERYTARGETS_STATE;
4088b7579f77SDag-Erling Smørgrav 			iq->num_current_queries--;
4089b7579f77SDag-Erling Smørgrav 			verbose(VERB_DETAIL, "Capsforid: starting fallback");
4090b7579f77SDag-Erling Smørgrav 			goto handle_it;
4091b7579f77SDag-Erling Smørgrav 		} else {
4092b7579f77SDag-Erling Smørgrav 			/* check if reply is the same, otherwise, fail */
4093ff825849SDag-Erling Smørgrav 			if(!iq->caps_reply) {
4094ff825849SDag-Erling Smørgrav 				iq->caps_reply = iq->response->rep;
409509a3aaf3SDag-Erling Smørgrav 				iq->caps_response = iq->response;
4096ff825849SDag-Erling Smørgrav 				iq->caps_server = -1; /*become zero at ++,
4097ff825849SDag-Erling Smørgrav 				so that we start the full set of trials */
409809a3aaf3SDag-Erling Smørgrav 			} else if(caps_failed_rcode(iq->caps_reply) &&
409909a3aaf3SDag-Erling Smørgrav 				!caps_failed_rcode(iq->response->rep)) {
410009a3aaf3SDag-Erling Smørgrav 				/* prefer to upgrade to non-SERVFAIL */
410109a3aaf3SDag-Erling Smørgrav 				iq->caps_reply = iq->response->rep;
410209a3aaf3SDag-Erling Smørgrav 				iq->caps_response = iq->response;
410309a3aaf3SDag-Erling Smørgrav 			} else if(!caps_failed_rcode(iq->caps_reply) &&
410409a3aaf3SDag-Erling Smørgrav 				caps_failed_rcode(iq->response->rep)) {
410509a3aaf3SDag-Erling Smørgrav 				/* if we have non-SERVFAIL as answer then
410609a3aaf3SDag-Erling Smørgrav 				 * we can ignore SERVFAILs for the equality
410709a3aaf3SDag-Erling Smørgrav 				 * comparison */
410809a3aaf3SDag-Erling Smørgrav 				/* no instructions here, skip other else */
410909a3aaf3SDag-Erling Smørgrav 			} else if(caps_failed_rcode(iq->caps_reply) &&
411009a3aaf3SDag-Erling Smørgrav 				caps_failed_rcode(iq->response->rep)) {
411109a3aaf3SDag-Erling Smørgrav 				/* failure is same as other failure in fallbk*/
411209a3aaf3SDag-Erling Smørgrav 				/* no instructions here, skip other else */
4113ff825849SDag-Erling Smørgrav 			} else if(!reply_equal(iq->response->rep, iq->caps_reply,
411417d15b25SDag-Erling Smørgrav 				qstate->env->scratch)) {
4115b7579f77SDag-Erling Smørgrav 				verbose(VERB_DETAIL, "Capsforid fallback: "
4116b7579f77SDag-Erling Smørgrav 					"getting different replies, failed");
4117b7579f77SDag-Erling Smørgrav 				outbound_list_remove(&iq->outlist, outbound);
41184c75e3aaSDag-Erling Smørgrav 				errinf(qstate, "0x20 failed, then got different replies in fallback");
4119b7579f77SDag-Erling Smørgrav 				(void)error_response(qstate, id,
4120b7579f77SDag-Erling Smørgrav 					LDNS_RCODE_SERVFAIL);
4121b7579f77SDag-Erling Smørgrav 				return;
4122b7579f77SDag-Erling Smørgrav 			}
4123b7579f77SDag-Erling Smørgrav 			/* continue the fallback procedure at next server */
4124b7579f77SDag-Erling Smørgrav 			iq->caps_server++;
4125b7579f77SDag-Erling Smørgrav 			iq->state = QUERYTARGETS_STATE;
4126b7579f77SDag-Erling Smørgrav 			iq->num_current_queries--;
4127b7579f77SDag-Erling Smørgrav 			verbose(VERB_DETAIL, "Capsforid: reply is equal. "
4128b7579f77SDag-Erling Smørgrav 				"go to next fallback");
4129b7579f77SDag-Erling Smørgrav 			goto handle_it;
4130b7579f77SDag-Erling Smørgrav 		}
4131b7579f77SDag-Erling Smørgrav 	}
4132b7579f77SDag-Erling Smørgrav 	iq->caps_fallback = 0; /* if we were in fallback, 0x20 is OK now */
4133b7579f77SDag-Erling Smørgrav 
4134b7579f77SDag-Erling Smørgrav handle_it:
4135b7579f77SDag-Erling Smørgrav 	outbound_list_remove(&iq->outlist, outbound);
4136b7579f77SDag-Erling Smørgrav 	iter_handle(qstate, iq, ie, id);
4137b7579f77SDag-Erling Smørgrav }
4138b7579f77SDag-Erling Smørgrav 
4139b7579f77SDag-Erling Smørgrav void
4140b7579f77SDag-Erling Smørgrav iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
4141b7579f77SDag-Erling Smørgrav 	struct outbound_entry* outbound)
4142b7579f77SDag-Erling Smørgrav {
4143b7579f77SDag-Erling Smørgrav 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
4144b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
4145b7579f77SDag-Erling Smørgrav 	verbose(VERB_QUERY, "iterator[module %d] operate: extstate:%s event:%s",
4146b7579f77SDag-Erling Smørgrav 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
4147b7579f77SDag-Erling Smørgrav 	if(iq) log_query_info(VERB_QUERY, "iterator operate: query",
4148b7579f77SDag-Erling Smørgrav 		&qstate->qinfo);
4149b7579f77SDag-Erling Smørgrav 	if(iq && qstate->qinfo.qname != iq->qchase.qname)
4150b7579f77SDag-Erling Smørgrav 		log_query_info(VERB_QUERY, "iterator operate: chased to",
4151b7579f77SDag-Erling Smørgrav 			&iq->qchase);
4152b7579f77SDag-Erling Smørgrav 
4153b7579f77SDag-Erling Smørgrav 	/* perform iterator state machine */
4154b7579f77SDag-Erling Smørgrav 	if((event == module_event_new || event == module_event_pass) &&
4155b7579f77SDag-Erling Smørgrav 		iq == NULL) {
4156b7579f77SDag-Erling Smørgrav 		if(!iter_new(qstate, id)) {
41574c75e3aaSDag-Erling Smørgrav 			errinf(qstate, "malloc failure, new iterator module allocation");
4158b7579f77SDag-Erling Smørgrav 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
4159b7579f77SDag-Erling Smørgrav 			return;
4160b7579f77SDag-Erling Smørgrav 		}
4161b7579f77SDag-Erling Smørgrav 		iq = (struct iter_qstate*)qstate->minfo[id];
4162b7579f77SDag-Erling Smørgrav 		process_request(qstate, iq, ie, id);
4163b7579f77SDag-Erling Smørgrav 		return;
4164b7579f77SDag-Erling Smørgrav 	}
4165b7579f77SDag-Erling Smørgrav 	if(iq && event == module_event_pass) {
4166b7579f77SDag-Erling Smørgrav 		iter_handle(qstate, iq, ie, id);
4167b7579f77SDag-Erling Smørgrav 		return;
4168b7579f77SDag-Erling Smørgrav 	}
4169b7579f77SDag-Erling Smørgrav 	if(iq && outbound) {
4170b7579f77SDag-Erling Smørgrav 		process_response(qstate, iq, ie, id, outbound, event);
4171b7579f77SDag-Erling Smørgrav 		return;
4172b7579f77SDag-Erling Smørgrav 	}
4173b7579f77SDag-Erling Smørgrav 	if(event == module_event_error) {
4174b7579f77SDag-Erling Smørgrav 		verbose(VERB_ALGO, "got called with event error, giving up");
41754c75e3aaSDag-Erling Smørgrav 		errinf(qstate, "iterator module got the error event");
4176b7579f77SDag-Erling Smørgrav 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
4177b7579f77SDag-Erling Smørgrav 		return;
4178b7579f77SDag-Erling Smørgrav 	}
4179b7579f77SDag-Erling Smørgrav 
4180b7579f77SDag-Erling Smørgrav 	log_err("bad event for iterator");
41814c75e3aaSDag-Erling Smørgrav 	errinf(qstate, "iterator module received wrong event");
4182b7579f77SDag-Erling Smørgrav 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
4183b7579f77SDag-Erling Smørgrav }
4184b7579f77SDag-Erling Smørgrav 
4185b7579f77SDag-Erling Smørgrav void
4186b7579f77SDag-Erling Smørgrav iter_clear(struct module_qstate* qstate, int id)
4187b7579f77SDag-Erling Smørgrav {
4188b7579f77SDag-Erling Smørgrav 	struct iter_qstate* iq;
4189b7579f77SDag-Erling Smørgrav 	if(!qstate)
4190b7579f77SDag-Erling Smørgrav 		return;
4191b7579f77SDag-Erling Smørgrav 	iq = (struct iter_qstate*)qstate->minfo[id];
4192b7579f77SDag-Erling Smørgrav 	if(iq) {
4193b7579f77SDag-Erling Smørgrav 		outbound_list_clear(&iq->outlist);
41940a92a9fcSCy Schubert 		if(iq->target_count && --iq->target_count[TARGET_COUNT_REF] == 0) {
419552df462fSXin LI 			free(iq->target_count);
41960a92a9fcSCy Schubert 			if(*iq->nxns_dp) free(*iq->nxns_dp);
41970a92a9fcSCy Schubert 			free(iq->nxns_dp);
41980a92a9fcSCy Schubert 		}
4199b7579f77SDag-Erling Smørgrav 		iq->num_current_queries = 0;
4200b7579f77SDag-Erling Smørgrav 	}
4201b7579f77SDag-Erling Smørgrav 	qstate->minfo[id] = NULL;
4202b7579f77SDag-Erling Smørgrav }
4203b7579f77SDag-Erling Smørgrav 
4204b7579f77SDag-Erling Smørgrav size_t
4205b7579f77SDag-Erling Smørgrav iter_get_mem(struct module_env* env, int id)
4206b7579f77SDag-Erling Smørgrav {
4207b7579f77SDag-Erling Smørgrav 	struct iter_env* ie = (struct iter_env*)env->modinfo[id];
4208b7579f77SDag-Erling Smørgrav 	if(!ie)
4209b7579f77SDag-Erling Smørgrav 		return 0;
4210b7579f77SDag-Erling Smørgrav 	return sizeof(*ie) + sizeof(int)*((size_t)ie->max_dependency_depth+1)
4211b7579f77SDag-Erling Smørgrav 		+ donotq_get_mem(ie->donotq) + priv_get_mem(ie->priv);
4212b7579f77SDag-Erling Smørgrav }
4213b7579f77SDag-Erling Smørgrav 
4214b7579f77SDag-Erling Smørgrav /**
4215b7579f77SDag-Erling Smørgrav  * The iterator function block
4216b7579f77SDag-Erling Smørgrav  */
4217b7579f77SDag-Erling Smørgrav static struct module_func_block iter_block = {
4218b7579f77SDag-Erling Smørgrav 	"iterator",
4219b7579f77SDag-Erling Smørgrav 	&iter_init, &iter_deinit, &iter_operate, &iter_inform_super,
4220b7579f77SDag-Erling Smørgrav 	&iter_clear, &iter_get_mem
4221b7579f77SDag-Erling Smørgrav };
4222b7579f77SDag-Erling Smørgrav 
4223b7579f77SDag-Erling Smørgrav struct module_func_block*
4224b7579f77SDag-Erling Smørgrav iter_get_funcblock(void)
4225b7579f77SDag-Erling Smørgrav {
4226b7579f77SDag-Erling Smørgrav 	return &iter_block;
4227b7579f77SDag-Erling Smørgrav }
4228b7579f77SDag-Erling Smørgrav 
4229b7579f77SDag-Erling Smørgrav const char*
4230b7579f77SDag-Erling Smørgrav iter_state_to_string(enum iter_state state)
4231b7579f77SDag-Erling Smørgrav {
4232b7579f77SDag-Erling Smørgrav 	switch (state)
4233b7579f77SDag-Erling Smørgrav 	{
4234b7579f77SDag-Erling Smørgrav 	case INIT_REQUEST_STATE :
4235b7579f77SDag-Erling Smørgrav 		return "INIT REQUEST STATE";
4236b7579f77SDag-Erling Smørgrav 	case INIT_REQUEST_2_STATE :
4237b7579f77SDag-Erling Smørgrav 		return "INIT REQUEST STATE (stage 2)";
4238b7579f77SDag-Erling Smørgrav 	case INIT_REQUEST_3_STATE:
4239b7579f77SDag-Erling Smørgrav 		return "INIT REQUEST STATE (stage 3)";
4240b7579f77SDag-Erling Smørgrav 	case QUERYTARGETS_STATE :
4241b7579f77SDag-Erling Smørgrav 		return "QUERY TARGETS STATE";
4242b7579f77SDag-Erling Smørgrav 	case PRIME_RESP_STATE :
4243b7579f77SDag-Erling Smørgrav 		return "PRIME RESPONSE STATE";
4244b7579f77SDag-Erling Smørgrav 	case COLLECT_CLASS_STATE :
4245b7579f77SDag-Erling Smørgrav 		return "COLLECT CLASS STATE";
4246b7579f77SDag-Erling Smørgrav 	case DSNS_FIND_STATE :
4247b7579f77SDag-Erling Smørgrav 		return "DSNS FIND STATE";
4248b7579f77SDag-Erling Smørgrav 	case QUERY_RESP_STATE :
4249b7579f77SDag-Erling Smørgrav 		return "QUERY RESPONSE STATE";
4250b7579f77SDag-Erling Smørgrav 	case FINISHED_STATE :
4251b7579f77SDag-Erling Smørgrav 		return "FINISHED RESPONSE STATE";
4252b7579f77SDag-Erling Smørgrav 	default :
4253b7579f77SDag-Erling Smørgrav 		return "UNKNOWN ITER STATE";
4254b7579f77SDag-Erling Smørgrav 	}
4255b7579f77SDag-Erling Smørgrav }
4256b7579f77SDag-Erling Smørgrav 
4257b7579f77SDag-Erling Smørgrav int
4258b7579f77SDag-Erling Smørgrav iter_state_is_responsestate(enum iter_state s)
4259b7579f77SDag-Erling Smørgrav {
4260b7579f77SDag-Erling Smørgrav 	switch(s) {
4261b7579f77SDag-Erling Smørgrav 		case INIT_REQUEST_STATE :
4262b7579f77SDag-Erling Smørgrav 		case INIT_REQUEST_2_STATE :
4263b7579f77SDag-Erling Smørgrav 		case INIT_REQUEST_3_STATE :
4264b7579f77SDag-Erling Smørgrav 		case QUERYTARGETS_STATE :
4265b7579f77SDag-Erling Smørgrav 		case COLLECT_CLASS_STATE :
4266b7579f77SDag-Erling Smørgrav 			return 0;
4267b7579f77SDag-Erling Smørgrav 		default:
4268b7579f77SDag-Erling Smørgrav 			break;
4269b7579f77SDag-Erling Smørgrav 	}
4270b7579f77SDag-Erling Smørgrav 	return 1;
4271b7579f77SDag-Erling Smørgrav }
4272