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