2 * iterator/iterator.c - iterative resolver DNS query response module
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains a module that performs recursive iterative DNS query
44 #include "iterator/iterator.h"
45 #include "iterator/iter_utils.h"
46 #include "iterator/iter_hints.h"
47 #include "iterator/iter_fwd.h"
48 #include "iterator/iter_donotq.h"
49 #include "iterator/iter_delegpt.h"
50 #include "iterator/iter_resptype.h"
51 #include "iterator/iter_scrub.h"
52 #include "iterator/iter_priv.h"
53 #include "validator/val_neg.h"
54 #include "services/cache/dns.h"
55 #include "services/cache/infra.h"
56 #include "util/module.h"
57 #include "util/netevent.h"
58 #include "util/net_help.h"
59 #include "util/regional.h"
60 #include "util/data/dname.h"
61 #include "util/data/msgencode.h"
62 #include "util/fptr_wlist.h"
63 #include "util/config_file.h"
64 #include "util/random.h"
65 #include "sldns/rrdef.h"
66 #include "sldns/wire2str.h"
67 #include "sldns/str2wire.h"
68 #include "sldns/parseutil.h"
69 #include "sldns/sbuffer.h"
72 iter_init(struct module_env* env, int id)
74 struct iter_env* iter_env = (struct iter_env*)calloc(1,
75 sizeof(struct iter_env));
77 log_err("malloc failure");
80 env->modinfo[id] = (void*)iter_env;
82 lock_basic_init(&iter_env->queries_ratelimit_lock);
83 lock_protect(&iter_env->queries_ratelimit_lock,
84 &iter_env->num_queries_ratelimited,
85 sizeof(iter_env->num_queries_ratelimited));
87 if(!iter_apply_cfg(iter_env, env->cfg)) {
88 log_err("iterator: could not apply configuration settings.");
95 /** delete caps_whitelist element */
97 caps_free(struct rbnode_type* n, void* ATTR_UNUSED(d))
100 free(((struct name_tree_node*)n)->name);
106 iter_deinit(struct module_env* env, int id)
108 struct iter_env* iter_env;
109 if(!env || !env->modinfo[id])
111 iter_env = (struct iter_env*)env->modinfo[id];
112 lock_basic_destroy(&iter_env->queries_ratelimit_lock);
113 free(iter_env->target_fetch_policy);
114 priv_delete(iter_env->priv);
115 donotq_delete(iter_env->donotq);
116 if(iter_env->caps_white) {
117 traverse_postorder(iter_env->caps_white, caps_free, NULL);
118 free(iter_env->caps_white);
121 env->modinfo[id] = NULL;
124 /** new query for iterator */
126 iter_new(struct module_qstate* qstate, int id)
128 struct iter_qstate* iq = (struct iter_qstate*)regional_alloc(
129 qstate->region, sizeof(struct iter_qstate));
130 qstate->minfo[id] = iq;
133 memset(iq, 0, sizeof(*iq));
134 iq->state = INIT_REQUEST_STATE;
135 iq->final_state = FINISHED_STATE;
136 iq->an_prepend_list = NULL;
137 iq->an_prepend_last = NULL;
138 iq->ns_prepend_list = NULL;
139 iq->ns_prepend_last = NULL;
142 iq->num_target_queries = 0;
143 iq->num_current_queries = 0;
144 iq->query_restart_count = 0;
145 iq->referral_count = 0;
147 iq->ratelimit_ok = 0;
148 iq->target_count = NULL;
149 iq->wait_priming_stub = 0;
150 iq->refetch_glue = 0;
151 iq->dnssec_expected = 0;
152 iq->dnssec_lame_query = 0;
153 iq->chase_flags = qstate->query_flags;
154 /* Start with the (current) qname. */
155 iq->qchase = qstate->qinfo;
156 outbound_list_init(&iq->outlist);
157 iq->minimise_count = 0;
158 iq->minimise_timeout_count = 0;
159 if (qstate->env->cfg->qname_minimisation)
160 iq->minimisation_state = INIT_MINIMISE_STATE;
162 iq->minimisation_state = DONOT_MINIMISE_STATE;
164 memset(&iq->qinfo_out, 0, sizeof(struct query_info));
169 * Transition to the next state. This can be used to advance a currently
170 * processing event. It cannot be used to reactivate a forEvent.
172 * @param iq: iterator query state
173 * @param nextstate The state to transition to.
174 * @return true. This is so this can be called as the return value for the
175 * actual process*State() methods. (Transitioning to the next state
176 * implies further processing).
179 next_state(struct iter_qstate* iq, enum iter_state nextstate)
181 /* If transitioning to a "response" state, make sure that there is a
183 if(iter_state_is_responsestate(nextstate)) {
184 if(iq->response == NULL) {
185 log_err("transitioning to response state sans "
189 iq->state = nextstate;
194 * Transition an event to its final state. Final states always either return
195 * a result up the module chain, or reactivate a dependent event. Which
196 * final state to transition to is set in the module state for the event when
197 * it was created, and depends on the original purpose of the event.
199 * The response is stored in the qstate->buf buffer.
201 * @param iq: iterator query state
202 * @return false. This is so this method can be used as the return value for
203 * the processState methods. (Transitioning to the final state
206 final_state(struct iter_qstate* iq)
208 return next_state(iq, iq->final_state);
212 * Callback routine to handle errors in parent query states
213 * @param qstate: query state that failed.
214 * @param id: module id.
215 * @param super: super state.
218 error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
220 struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
222 if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
223 qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
224 /* mark address as failed. */
225 struct delegpt_ns* dpns = NULL;
226 super_iq->num_target_queries--;
228 dpns = delegpt_find_ns(super_iq->dp,
229 qstate->qinfo.qname, qstate->qinfo.qname_len);
232 verbose(VERB_ALGO, "subq error, but not interested");
233 log_query_info(VERB_ALGO, "superq", &super->qinfo);
235 delegpt_log(VERB_ALGO, super_iq->dp);
239 /* see if the failure did get (parent-lame) info */
240 if(!cache_fill_missing(super->env, super_iq->qchase.qclass,
241 super->region, super_iq->dp))
242 log_err("out of memory adding missing");
244 dpns->resolved = 1; /* mark as failed */
246 if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
247 /* prime failed to get delegation */
250 /* evaluate targets again */
251 super_iq->state = QUERYTARGETS_STATE;
252 /* super becomes runnable, and will process this change */
256 * Return an error to the client
257 * @param qstate: our query state
258 * @param id: module id
259 * @param rcode: error code (DNS errcode).
260 * @return: 0 for use by caller, to make notation easy, like:
261 * return error_response(..).
264 error_response(struct module_qstate* qstate, int id, int rcode)
266 verbose(VERB_QUERY, "return error response %s",
267 sldns_lookup_by_id(sldns_rcodes, rcode)?
268 sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
269 qstate->return_rcode = rcode;
270 qstate->return_msg = NULL;
271 qstate->ext_state[id] = module_finished;
276 * Return an error to the client and cache the error code in the
277 * message cache (so per qname, qtype, qclass).
278 * @param qstate: our query state
279 * @param id: module id
280 * @param rcode: error code (DNS errcode).
281 * @return: 0 for use by caller, to make notation easy, like:
282 * return error_response(..).
285 error_response_cache(struct module_qstate* qstate, int id, int rcode)
287 if(!qstate->no_cache_store) {
289 struct reply_info err;
290 if(qstate->prefetch_leeway > NORR_TTL) {
291 verbose(VERB_ALGO, "error response for prefetch in cache");
292 /* attempt to adjust the cache entry prefetch */
293 if(dns_cache_prefetch_adjust(qstate->env, &qstate->qinfo,
294 NORR_TTL, qstate->query_flags))
295 return error_response(qstate, id, rcode);
296 /* if that fails (not in cache), fall through to store err */
298 if(qstate->env->cfg->serve_expired) {
299 /* if serving expired contents, and such content is
300 * already available, don't overwrite this servfail */
301 struct msgreply_entry* msg;
302 if((msg=msg_cache_lookup(qstate->env,
303 qstate->qinfo.qname, qstate->qinfo.qname_len,
304 qstate->qinfo.qtype, qstate->qinfo.qclass,
305 qstate->query_flags, 0, 0))
307 lock_rw_unlock(&msg->entry.lock);
308 return error_response(qstate, id, rcode);
310 /* serving expired contents, but nothing is cached
311 * at all, so the servfail cache entry is useful
312 * (stops waste of time on this servfail NORR_TTL) */
314 memset(&err, 0, sizeof(err));
315 err.flags = (uint16_t)(BIT_QR | BIT_RA);
316 FLAGS_SET_RCODE(err.flags, rcode);
319 err.prefetch_ttl = PREFETCH_TTL_CALC(err.ttl);
320 /* do not waste time trying to validate this servfail */
321 err.security = sec_status_indeterminate;
322 verbose(VERB_ALGO, "store error response in message cache");
323 iter_dns_store(qstate->env, &qstate->qinfo, &err, 0, 0, 0, NULL,
324 qstate->query_flags);
326 return error_response(qstate, id, rcode);
329 /** check if prepend item is duplicate item */
331 prepend_is_duplicate(struct ub_packed_rrset_key** sets, size_t to,
332 struct ub_packed_rrset_key* dup)
335 for(i=0; i<to; i++) {
336 if(sets[i]->rk.type == dup->rk.type &&
337 sets[i]->rk.rrset_class == dup->rk.rrset_class &&
338 sets[i]->rk.dname_len == dup->rk.dname_len &&
339 query_dname_compare(sets[i]->rk.dname, dup->rk.dname)
346 /** prepend the prepend list in the answer and authority section of dns_msg */
348 iter_prepend(struct iter_qstate* iq, struct dns_msg* msg,
349 struct regional* region)
351 struct iter_prep_list* p;
352 struct ub_packed_rrset_key** sets;
353 size_t num_an = 0, num_ns = 0;;
354 for(p = iq->an_prepend_list; p; p = p->next)
356 for(p = iq->ns_prepend_list; p; p = p->next)
358 if(num_an + num_ns == 0)
360 verbose(VERB_ALGO, "prepending %d rrsets", (int)num_an + (int)num_ns);
361 if(num_an > RR_COUNT_MAX || num_ns > RR_COUNT_MAX ||
362 msg->rep->rrset_count > RR_COUNT_MAX) return 0; /* overflow */
363 sets = regional_alloc(region, (num_an+num_ns+msg->rep->rrset_count) *
364 sizeof(struct ub_packed_rrset_key*));
369 for(p = iq->an_prepend_list; p; p = p->next) {
370 sets[num_an++] = p->rrset;
372 memcpy(sets+num_an, msg->rep->rrsets, msg->rep->an_numrrsets *
373 sizeof(struct ub_packed_rrset_key*));
376 for(p = iq->ns_prepend_list; p; p = p->next) {
377 if(prepend_is_duplicate(sets+msg->rep->an_numrrsets+num_an,
378 num_ns, p->rrset) || prepend_is_duplicate(
379 msg->rep->rrsets+msg->rep->an_numrrsets,
380 msg->rep->ns_numrrsets, p->rrset))
382 sets[msg->rep->an_numrrsets + num_an + num_ns++] = p->rrset;
384 memcpy(sets + num_an + msg->rep->an_numrrsets + num_ns,
385 msg->rep->rrsets + msg->rep->an_numrrsets,
386 (msg->rep->ns_numrrsets + msg->rep->ar_numrrsets) *
387 sizeof(struct ub_packed_rrset_key*));
389 /* NXDOMAIN rcode can stay if we prepended DNAME/CNAMEs, because
390 * this is what recursors should give. */
391 msg->rep->rrset_count += num_an + num_ns;
392 msg->rep->an_numrrsets += num_an;
393 msg->rep->ns_numrrsets += num_ns;
394 msg->rep->rrsets = sets;
399 * Find rrset in ANSWER prepend list.
400 * to avoid duplicate DNAMEs when a DNAME is traversed twice.
401 * @param iq: iterator query state.
402 * @param rrset: rrset to add.
403 * @return false if not found
406 iter_find_rrset_in_prepend_answer(struct iter_qstate* iq,
407 struct ub_packed_rrset_key* rrset)
409 struct iter_prep_list* p = iq->an_prepend_list;
411 if(ub_rrset_compare(p->rrset, rrset) == 0 &&
412 rrsetdata_equal((struct packed_rrset_data*)p->rrset
413 ->entry.data, (struct packed_rrset_data*)rrset
422 * Add rrset to ANSWER prepend list
423 * @param qstate: query state.
424 * @param iq: iterator query state.
425 * @param rrset: rrset to add.
426 * @return false on failure (malloc).
429 iter_add_prepend_answer(struct module_qstate* qstate, struct iter_qstate* iq,
430 struct ub_packed_rrset_key* rrset)
432 struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
433 qstate->region, sizeof(struct iter_prep_list));
439 if(iq->an_prepend_last)
440 iq->an_prepend_last->next = p;
441 else iq->an_prepend_list = p;
442 iq->an_prepend_last = p;
447 * Add rrset to AUTHORITY prepend list
448 * @param qstate: query state.
449 * @param iq: iterator query state.
450 * @param rrset: rrset to add.
451 * @return false on failure (malloc).
454 iter_add_prepend_auth(struct module_qstate* qstate, struct iter_qstate* iq,
455 struct ub_packed_rrset_key* rrset)
457 struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
458 qstate->region, sizeof(struct iter_prep_list));
464 if(iq->ns_prepend_last)
465 iq->ns_prepend_last->next = p;
466 else iq->ns_prepend_list = p;
467 iq->ns_prepend_last = p;
472 * Given a CNAME response (defined as a response containing a CNAME or DNAME
473 * that does not answer the request), process the response, modifying the
474 * state as necessary. This follows the CNAME/DNAME chain and returns the
477 * sets the new query name, after following the CNAME/DNAME chain.
478 * @param qstate: query state.
479 * @param iq: iterator query state.
480 * @param msg: the response.
481 * @param mname: returned target new query name.
482 * @param mname_len: length of mname.
483 * @return false on (malloc) error.
486 handle_cname_response(struct module_qstate* qstate, struct iter_qstate* iq,
487 struct dns_msg* msg, uint8_t** mname, size_t* mname_len)
490 /* Start with the (current) qname. */
491 *mname = iq->qchase.qname;
492 *mname_len = iq->qchase.qname_len;
494 /* Iterate over the ANSWER rrsets in order, looking for CNAMEs and
496 for(i=0; i<msg->rep->an_numrrsets; i++) {
497 struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
498 /* If there is a (relevant) DNAME, add it to the list.
499 * We always expect there to be CNAME that was generated
500 * by this DNAME following, so we don't process the DNAME
502 if(ntohs(r->rk.type) == LDNS_RR_TYPE_DNAME &&
503 dname_strict_subdomain_c(*mname, r->rk.dname) &&
504 !iter_find_rrset_in_prepend_answer(iq, r)) {
505 if(!iter_add_prepend_answer(qstate, iq, r))
510 if(ntohs(r->rk.type) == LDNS_RR_TYPE_CNAME &&
511 query_dname_compare(*mname, r->rk.dname) == 0 &&
512 !iter_find_rrset_in_prepend_answer(iq, r)) {
513 /* Add this relevant CNAME rrset to the prepend list.*/
514 if(!iter_add_prepend_answer(qstate, iq, r))
516 get_cname_target(r, mname, mname_len);
519 /* Other rrsets in the section are ignored. */
521 /* add authority rrsets to authority prepend, for wildcarded CNAMEs */
522 for(i=msg->rep->an_numrrsets; i<msg->rep->an_numrrsets +
523 msg->rep->ns_numrrsets; i++) {
524 struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
525 /* only add NSEC/NSEC3, as they may be needed for validation */
526 if(ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC ||
527 ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC3) {
528 if(!iter_add_prepend_auth(qstate, iq, r))
535 /** see if last resort is possible - does config allow queries to parent */
537 can_have_last_resort(struct module_env* env, uint8_t* nm, size_t nmlen,
540 struct delegpt* fwddp;
541 struct iter_hints_stub* stub;
542 int labs = dname_count_labels(nm);
543 /* do not process a last resort (the parent side) if a stub
544 * or forward is configured, because we do not want to go 'above'
545 * the configured servers */
546 if(!dname_is_root(nm) && (stub = (struct iter_hints_stub*)
547 name_tree_find(&env->hints->tree, nm, nmlen, labs, qclass)) &&
548 /* has_parent side is turned off for stub_first, where we
549 * are allowed to go to the parent */
550 stub->dp->has_parent_side_NS) {
553 if((fwddp = forwards_find(env->fwds, nm, qclass)) &&
554 /* has_parent_side is turned off for forward_first, where
555 * we are allowed to go to the parent */
556 fwddp->has_parent_side_NS) {
562 /** see if target name is caps-for-id whitelisted */
564 is_caps_whitelisted(struct iter_env* ie, struct iter_qstate* iq)
566 if(!ie->caps_white) return 0; /* no whitelist, or no capsforid */
567 return name_tree_lookup(ie->caps_white, iq->qchase.qname,
568 iq->qchase.qname_len, dname_count_labels(iq->qchase.qname),
569 iq->qchase.qclass) != NULL;
572 /** create target count structure for this query */
574 target_count_create(struct iter_qstate* iq)
576 if(!iq->target_count) {
577 iq->target_count = (int*)calloc(2, sizeof(int));
578 /* if calloc fails we simply do not track this number */
580 iq->target_count[0] = 1;
585 target_count_increase(struct iter_qstate* iq, int num)
587 target_count_create(iq);
589 iq->target_count[1] += num;
593 * Generate a subrequest.
594 * Generate a local request event. Local events are tied to this module, and
595 * have a corresponding (first tier) event that is waiting for this event to
596 * resolve to continue.
598 * @param qname The query name for this request.
599 * @param qnamelen length of qname
600 * @param qtype The query type for this request.
601 * @param qclass The query class for this request.
602 * @param qstate The event that is generating this event.
603 * @param id: module id.
604 * @param iq: The iterator state that is generating this event.
605 * @param initial_state The initial response state (normally this
606 * is QUERY_RESP_STATE, unless it is known that the request won't
607 * need iterative processing
608 * @param finalstate The final state for the response to this request.
609 * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
610 * not need initialisation.
611 * @param v: if true, validation is done on the subquery.
612 * @return false on error (malloc).
615 generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
616 uint16_t qclass, struct module_qstate* qstate, int id,
617 struct iter_qstate* iq, enum iter_state initial_state,
618 enum iter_state finalstate, struct module_qstate** subq_ret, int v)
620 struct module_qstate* subq = NULL;
621 struct iter_qstate* subiq = NULL;
622 uint16_t qflags = 0; /* OPCODE QUERY, no flags */
623 struct query_info qinf;
624 int prime = (finalstate == PRIME_RESP_STATE)?1:0;
627 qinf.qname_len = qnamelen;
629 qinf.qclass = qclass;
630 qinf.local_alias = NULL;
632 /* RD should be set only when sending the query back through the INIT
634 if(initial_state == INIT_REQUEST_STATE)
636 /* We set the CD flag so we can send this through the "head" of
637 * the resolution chain, which might have a validator. We are
638 * uninterested in validating things not on the direct resolution
645 /* attach subquery, lookup existing or make a new one */
646 fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
647 if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime, valrec,
653 /* initialise the new subquery */
655 subq->ext_state[id] = module_state_initial;
656 subq->minfo[id] = regional_alloc(subq->region,
657 sizeof(struct iter_qstate));
658 if(!subq->minfo[id]) {
659 log_err("init subq: out of memory");
660 fptr_ok(fptr_whitelist_modenv_kill_sub(
661 qstate->env->kill_sub));
662 (*qstate->env->kill_sub)(subq);
665 subiq = (struct iter_qstate*)subq->minfo[id];
666 memset(subiq, 0, sizeof(*subiq));
667 subiq->num_target_queries = 0;
668 target_count_create(iq);
669 subiq->target_count = iq->target_count;
671 iq->target_count[0] ++; /* extra reference */
672 subiq->num_current_queries = 0;
673 subiq->depth = iq->depth+1;
674 outbound_list_init(&subiq->outlist);
675 subiq->state = initial_state;
676 subiq->final_state = finalstate;
677 subiq->qchase = subq->qinfo;
678 subiq->chase_flags = subq->query_flags;
679 subiq->refetch_glue = 0;
680 if(qstate->env->cfg->qname_minimisation)
681 subiq->minimisation_state = INIT_MINIMISE_STATE;
683 subiq->minimisation_state = DONOT_MINIMISE_STATE;
684 memset(&subiq->qinfo_out, 0, sizeof(struct query_info));
690 * Generate and send a root priming request.
691 * @param qstate: the qtstate that triggered the need to prime.
692 * @param iq: iterator query state.
693 * @param id: module id.
694 * @param qclass: the class to prime.
695 * @return 0 on failure
698 prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
702 struct module_qstate* subq;
703 verbose(VERB_DETAIL, "priming . %s NS",
704 sldns_lookup_by_id(sldns_rr_classes, (int)qclass)?
705 sldns_lookup_by_id(sldns_rr_classes, (int)qclass)->name:"??");
706 dp = hints_lookup_root(qstate->env->hints, qclass);
708 verbose(VERB_ALGO, "Cannot prime due to lack of hints");
711 /* Priming requests start at the QUERYTARGETS state, skipping
712 * the normal INIT state logic (which would cause an infloop). */
713 if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS,
714 qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
716 verbose(VERB_ALGO, "could not prime root");
720 struct iter_qstate* subiq =
721 (struct iter_qstate*)subq->minfo[id];
722 /* Set the initial delegation point to the hint.
723 * copy dp, it is now part of the root prime query.
724 * dp was part of in the fixed hints structure. */
725 subiq->dp = delegpt_copy(dp, subq->region);
727 log_err("out of memory priming root, copydp");
728 fptr_ok(fptr_whitelist_modenv_kill_sub(
729 qstate->env->kill_sub));
730 (*qstate->env->kill_sub)(subq);
733 /* there should not be any target queries. */
734 subiq->num_target_queries = 0;
735 subiq->dnssec_expected = iter_indicates_dnssec(
736 qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
739 /* this module stops, our submodule starts, and does the query. */
740 qstate->ext_state[id] = module_wait_subquery;
745 * Generate and process a stub priming request. This method tests for the
746 * need to prime a stub zone, so it is safe to call for every request.
748 * @param qstate: the qtstate that triggered the need to prime.
749 * @param iq: iterator query state.
750 * @param id: module id.
751 * @param qname: request name.
752 * @param qclass: request class.
753 * @return true if a priming subrequest was made, false if not. The will only
754 * issue a priming request if it detects an unprimed stub.
755 * Uses value of 2 to signal during stub-prime in root-prime situation
756 * that a noprime-stub is available and resolution can continue.
759 prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
760 uint8_t* qname, uint16_t qclass)
762 /* Lookup the stub hint. This will return null if the stub doesn't
763 * need to be re-primed. */
764 struct iter_hints_stub* stub;
765 struct delegpt* stub_dp;
766 struct module_qstate* subq;
769 stub = hints_lookup_stub(qstate->env->hints, qname, qclass, iq->dp);
770 /* The stub (if there is one) does not need priming. */
775 /* is it a noprime stub (always use) */
778 if(iq->dp == NULL) r = 2;
779 /* copy the dp out of the fixed hints structure, so that
780 * it can be changed when servicing this query */
781 iq->dp = delegpt_copy(stub_dp, qstate->region);
783 log_err("out of memory priming stub");
784 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
785 return 1; /* return 1 to make module stop, with error */
787 log_nametypeclass(VERB_DETAIL, "use stub", stub_dp->name,
788 LDNS_RR_TYPE_NS, qclass);
792 /* Otherwise, we need to (re)prime the stub. */
793 log_nametypeclass(VERB_DETAIL, "priming stub", stub_dp->name,
794 LDNS_RR_TYPE_NS, qclass);
796 /* Stub priming events start at the QUERYTARGETS state to avoid the
797 * redundant INIT state processing. */
798 if(!generate_sub_request(stub_dp->name, stub_dp->namelen,
799 LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
800 QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0)) {
801 verbose(VERB_ALGO, "could not prime stub");
802 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
803 return 1; /* return 1 to make module stop, with error */
806 struct iter_qstate* subiq =
807 (struct iter_qstate*)subq->minfo[id];
809 /* Set the initial delegation point to the hint. */
810 /* make copy to avoid use of stub dp by different qs/threads */
811 subiq->dp = delegpt_copy(stub_dp, subq->region);
813 log_err("out of memory priming stub, copydp");
814 fptr_ok(fptr_whitelist_modenv_kill_sub(
815 qstate->env->kill_sub));
816 (*qstate->env->kill_sub)(subq);
817 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
818 return 1; /* return 1 to make module stop, with error */
820 /* there should not be any target queries -- although there
821 * wouldn't be anyway, since stub hints never have
822 * missing targets. */
823 subiq->num_target_queries = 0;
824 subiq->wait_priming_stub = 1;
825 subiq->dnssec_expected = iter_indicates_dnssec(
826 qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
829 /* this module stops, our submodule starts, and does the query. */
830 qstate->ext_state[id] = module_wait_subquery;
835 * Generate A and AAAA checks for glue that is in-zone for the referral
836 * we just got to obtain authoritative information on the addresses.
838 * @param qstate: the qtstate that triggered the need to prime.
839 * @param iq: iterator query state.
840 * @param id: module id.
843 generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
846 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
847 struct module_qstate* subq;
849 struct reply_info* rep = iq->response->rep;
850 struct ub_packed_rrset_key* s;
853 if(iq->depth == ie->max_dependency_depth)
855 /* walk through additional, and check if in-zone,
856 * only relevant A, AAAA are left after scrub anyway */
857 for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
859 /* check *ALL* addresses that are transmitted in additional*/
860 /* is it an address ? */
861 if( !(ntohs(s->rk.type)==LDNS_RR_TYPE_A ||
862 ntohs(s->rk.type)==LDNS_RR_TYPE_AAAA)) {
865 /* is this query the same as the A/AAAA check for it */
866 if(qstate->qinfo.qtype == ntohs(s->rk.type) &&
867 qstate->qinfo.qclass == ntohs(s->rk.rrset_class) &&
868 query_dname_compare(qstate->qinfo.qname,
870 (qstate->query_flags&BIT_RD) &&
871 !(qstate->query_flags&BIT_CD))
874 /* generate subrequest for it */
875 log_nametypeclass(VERB_ALGO, "schedule addr fetch",
876 s->rk.dname, ntohs(s->rk.type),
877 ntohs(s->rk.rrset_class));
878 if(!generate_sub_request(s->rk.dname, s->rk.dname_len,
879 ntohs(s->rk.type), ntohs(s->rk.rrset_class),
881 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
882 verbose(VERB_ALGO, "could not generate addr check");
885 /* ignore subq - not need for more init */
890 * Generate a NS check request to obtain authoritative information
893 * @param qstate: the qtstate that triggered the need to prime.
894 * @param iq: iterator query state.
895 * @param id: module id.
898 generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
900 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
901 struct module_qstate* subq;
904 if(iq->depth == ie->max_dependency_depth)
906 if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen,
909 /* is this query the same as the nscheck? */
910 if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS &&
911 query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
912 (qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
913 /* spawn off A, AAAA queries for in-zone glue to check */
914 generate_a_aaaa_check(qstate, iq, id);
918 log_nametypeclass(VERB_ALGO, "schedule ns fetch",
919 iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
920 if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
921 LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
922 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
923 verbose(VERB_ALGO, "could not generate ns check");
927 struct iter_qstate* subiq =
928 (struct iter_qstate*)subq->minfo[id];
930 /* make copy to avoid use of stub dp by different qs/threads */
931 /* refetch glue to start higher up the tree */
932 subiq->refetch_glue = 1;
933 subiq->dp = delegpt_copy(iq->dp, subq->region);
935 log_err("out of memory generating ns check, copydp");
936 fptr_ok(fptr_whitelist_modenv_kill_sub(
937 qstate->env->kill_sub));
938 (*qstate->env->kill_sub)(subq);
945 * Generate a DNSKEY prefetch query to get the DNSKEY for the DS record we
946 * just got in a referral (where we have dnssec_expected, thus have trust
947 * anchors above it). Note that right after calling this routine the
948 * iterator detached subqueries (because of following the referral), and thus
949 * the DNSKEY query becomes detached, its return stored in the cache for
950 * later lookup by the validator. This cache lookup by the validator avoids
951 * the roundtrip incurred by the DNSKEY query. The DNSKEY query is now
952 * performed at about the same time the original query is sent to the domain,
953 * thus the two answers are likely to be returned at about the same time,
954 * saving a roundtrip from the validated lookup.
956 * @param qstate: the qtstate that triggered the need to prime.
957 * @param iq: iterator query state.
958 * @param id: module id.
961 generate_dnskey_prefetch(struct module_qstate* qstate,
962 struct iter_qstate* iq, int id)
964 struct module_qstate* subq;
967 /* is this query the same as the prefetch? */
968 if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
969 query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
970 (qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
974 /* if the DNSKEY is in the cache this lookup will stop quickly */
975 log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch",
976 iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
977 if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
978 LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
979 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
980 /* we'll be slower, but it'll work */
981 verbose(VERB_ALGO, "could not generate dnskey prefetch");
985 struct iter_qstate* subiq =
986 (struct iter_qstate*)subq->minfo[id];
987 /* this qstate has the right delegation for the dnskey lookup*/
988 /* make copy to avoid use of stub dp by different qs/threads */
989 subiq->dp = delegpt_copy(iq->dp, subq->region);
990 /* if !subiq->dp, it'll start from the cache, no problem */
995 * See if the query needs forwarding.
997 * @param qstate: query state.
998 * @param iq: iterator query state.
999 * @return true if the request is forwarded, false if not.
1000 * If returns true but, iq->dp is NULL then a malloc failure occurred.
1003 forward_request(struct module_qstate* qstate, struct iter_qstate* iq)
1006 uint8_t* delname = iq->qchase.qname;
1007 size_t delnamelen = iq->qchase.qname_len;
1008 if(iq->refetch_glue) {
1009 delname = iq->dp->name;
1010 delnamelen = iq->dp->namelen;
1012 /* strip one label off of DS query to lookup higher for it */
1013 if( (iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue)
1014 && !dname_is_root(iq->qchase.qname))
1015 dname_remove_label(&delname, &delnamelen);
1016 dp = forwards_lookup(qstate->env->fwds, delname, iq->qchase.qclass);
1019 /* send recursion desired to forward addr */
1020 iq->chase_flags |= BIT_RD;
1021 iq->dp = delegpt_copy(dp, qstate->region);
1022 /* iq->dp checked by caller */
1023 verbose(VERB_ALGO, "forwarding request");
1028 * Process the initial part of the request handling. This state roughly
1029 * corresponds to resolver algorithms steps 1 (find answer in cache) and 2
1030 * (find the best servers to ask).
1032 * Note that all requests start here, and query restarts revisit this state.
1034 * This state either generates: 1) a response, from cache or error, 2) a
1035 * priming event, or 3) forwards the request to the next state (init2,
1038 * @param qstate: query state.
1039 * @param iq: iterator query state.
1040 * @param ie: iterator shared global environment.
1041 * @param id: module id.
1042 * @return true if the event needs more request processing immediately,
1046 processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
1047 struct iter_env* ie, int id)
1051 struct dns_msg* msg = NULL;
1053 log_query_info(VERB_DETAIL, "resolving", &qstate->qinfo);
1056 /* We enforce a maximum number of query restarts. This is primarily a
1057 * cheap way to prevent CNAME loops. */
1058 if(iq->query_restart_count > MAX_RESTART_COUNT) {
1059 verbose(VERB_QUERY, "request has exceeded the maximum number"
1060 " of query restarts with %d", iq->query_restart_count);
1061 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1064 /* We enforce a maximum recursion/dependency depth -- in general,
1065 * this is unnecessary for dependency loops (although it will
1066 * catch those), but it provides a sensible limit to the amount
1067 * of work required to answer a given query. */
1068 verbose(VERB_ALGO, "request has dependency depth of %d", iq->depth);
1069 if(iq->depth > ie->max_dependency_depth) {
1070 verbose(VERB_QUERY, "request has exceeded the maximum "
1071 "dependency depth with depth of %d", iq->depth);
1072 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1075 /* If the request is qclass=ANY, setup to generate each class */
1076 if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
1077 iq->qchase.qclass = 0;
1078 return next_state(iq, COLLECT_CLASS_STATE);
1082 * If we are restricted by a forward-zone or a stub-zone, we
1083 * can't re-fetch glue for this delegation point.
1084 * we won’t try to re-fetch glue if the iq->dp is null.
1086 if (iq->refetch_glue &&
1088 !can_have_last_resort(qstate->env,
1091 iq->qchase.qclass)) {
1092 iq->refetch_glue = 0;
1095 /* Resolver Algorithm Step 1 -- Look for the answer in local data. */
1097 /* This either results in a query restart (CNAME cache response), a
1098 * terminating response (ANSWER), or a cache miss (null). */
1100 if(qstate->blacklist) {
1101 /* if cache, or anything else, was blacklisted then
1102 * getting older results from cache is a bad idea, no cache */
1103 verbose(VERB_ALGO, "cache blacklisted, going to the network");
1105 } else if(!qstate->no_cache_lookup) {
1106 msg = dns_cache_lookup(qstate->env, iq->qchase.qname,
1107 iq->qchase.qname_len, iq->qchase.qtype,
1108 iq->qchase.qclass, qstate->query_flags,
1109 qstate->region, qstate->env->scratch);
1110 if(!msg && qstate->env->neg_cache) {
1111 /* lookup in negative cache; may result in
1112 * NOERROR/NODATA or NXDOMAIN answers that need validation */
1113 msg = val_neg_getmsg(qstate->env->neg_cache, &iq->qchase,
1114 qstate->region, qstate->env->rrset_cache,
1115 qstate->env->scratch_buffer,
1116 *qstate->env->now, 1/*add SOA*/, NULL);
1118 /* item taken from cache does not match our query name, thus
1119 * security needs to be re-examined later */
1120 if(msg && query_dname_compare(qstate->qinfo.qname,
1121 iq->qchase.qname) != 0)
1122 msg->rep->security = sec_status_unchecked;
1125 /* handle positive cache response */
1126 enum response_type type = response_type_from_cache(msg,
1128 if(verbosity >= VERB_ALGO) {
1129 log_dns_msg("msg from cache lookup", &msg->qinfo,
1131 verbose(VERB_ALGO, "msg ttl is %d, prefetch ttl %d",
1133 (int)msg->rep->prefetch_ttl);
1136 if(type == RESPONSE_TYPE_CNAME) {
1139 verbose(VERB_ALGO, "returning CNAME response from "
1141 if(!handle_cname_response(qstate, iq, msg,
1143 return error_response(qstate, id,
1144 LDNS_RCODE_SERVFAIL);
1145 iq->qchase.qname = sname;
1146 iq->qchase.qname_len = slen;
1147 /* This *is* a query restart, even if it is a cheap
1150 iq->refetch_glue = 0;
1151 iq->query_restart_count++;
1153 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1154 if(qstate->env->cfg->qname_minimisation)
1155 iq->minimisation_state = INIT_MINIMISE_STATE;
1156 return next_state(iq, INIT_REQUEST_STATE);
1159 /* if from cache, NULL, else insert 'cache IP' len=0 */
1160 if(qstate->reply_origin)
1161 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1162 /* it is an answer, response, to final state */
1163 verbose(VERB_ALGO, "returning answer from cache.");
1165 return final_state(iq);
1168 /* attempt to forward the request */
1169 if(forward_request(qstate, iq))
1172 log_err("alloc failure for forward dp");
1173 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1175 iq->refetch_glue = 0;
1176 iq->minimisation_state = DONOT_MINIMISE_STATE;
1177 /* the request has been forwarded.
1178 * forwarded requests need to be immediately sent to the
1179 * next state, QUERYTARGETS. */
1180 return next_state(iq, QUERYTARGETS_STATE);
1183 /* Resolver Algorithm Step 2 -- find the "best" servers. */
1185 /* first, adjust for DS queries. To avoid the grandparent problem,
1186 * we just look for the closest set of server to the parent of qname.
1187 * When re-fetching glue we also need to ask the parent.
1189 if(iq->refetch_glue) {
1191 log_err("internal or malloc fail: no dp for refetch");
1192 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1194 delname = iq->dp->name;
1195 delnamelen = iq->dp->namelen;
1197 delname = iq->qchase.qname;
1198 delnamelen = iq->qchase.qname_len;
1200 if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue ||
1201 (iq->qchase.qtype == LDNS_RR_TYPE_NS && qstate->prefetch_leeway)) {
1202 /* remove first label from delname, root goes to hints,
1203 * but only to fetch glue, not for qtype=DS. */
1204 /* also when prefetching an NS record, fetch it again from
1205 * its parent, just as if it expired, so that you do not
1206 * get stuck on an older nameserver that gives old NSrecords */
1207 if(dname_is_root(delname) && (iq->refetch_glue ||
1208 (iq->qchase.qtype == LDNS_RR_TYPE_NS &&
1209 qstate->prefetch_leeway)))
1210 delname = NULL; /* go to root priming */
1211 else dname_remove_label(&delname, &delnamelen);
1213 /* delname is the name to lookup a delegation for. If NULL rootprime */
1216 /* Lookup the delegation in the cache. If null, then the
1217 * cache needs to be primed for the qclass. */
1219 iq->dp = dns_cache_find_delegation(qstate->env, delname,
1220 delnamelen, iq->qchase.qtype, iq->qchase.qclass,
1221 qstate->region, &iq->deleg_msg,
1222 *qstate->env->now+qstate->prefetch_leeway);
1225 /* If the cache has returned nothing, then we have a
1226 * root priming situation. */
1227 if(iq->dp == NULL) {
1228 /* if there is a stub, then no root prime needed */
1229 int r = prime_stub(qstate, iq, id, delname,
1232 break; /* got noprime-stub-zone, continue */
1234 return 0; /* stub prime request made */
1235 if(forwards_lookup_root(qstate->env->fwds,
1236 iq->qchase.qclass)) {
1237 /* forward zone root, no root prime needed */
1238 /* fill in some dp - safety belt */
1239 iq->dp = hints_lookup_root(qstate->env->hints,
1242 log_err("internal error: no hints dp");
1243 return error_response(qstate, id,
1244 LDNS_RCODE_SERVFAIL);
1246 iq->dp = delegpt_copy(iq->dp, qstate->region);
1248 log_err("out of memory in safety belt");
1249 return error_response(qstate, id,
1250 LDNS_RCODE_SERVFAIL);
1252 return next_state(iq, INIT_REQUEST_2_STATE);
1254 /* Note that the result of this will set a new
1255 * DelegationPoint based on the result of priming. */
1256 if(!prime_root(qstate, iq, id, iq->qchase.qclass))
1257 return error_response(qstate, id,
1258 LDNS_RCODE_REFUSED);
1260 /* priming creates and sends a subordinate query, with
1261 * this query as the parent. So further processing for
1262 * this event will stop until reactivated by the
1263 * results of priming. */
1266 if(!iq->ratelimit_ok && qstate->prefetch_leeway)
1267 iq->ratelimit_ok = 1; /* allow prefetches, this keeps
1268 otherwise valid data in the cache */
1269 if(!iq->ratelimit_ok && infra_ratelimit_exceeded(
1270 qstate->env->infra_cache, iq->dp->name,
1271 iq->dp->namelen, *qstate->env->now)) {
1272 /* and increment the rate, so that the rate for time
1273 * now will also exceed the rate, keeping cache fresh */
1274 (void)infra_ratelimit_inc(qstate->env->infra_cache,
1275 iq->dp->name, iq->dp->namelen,
1277 /* see if we are passed through with slip factor */
1278 if(qstate->env->cfg->ratelimit_factor != 0 &&
1279 ub_random_max(qstate->env->rnd,
1280 qstate->env->cfg->ratelimit_factor) == 1) {
1281 iq->ratelimit_ok = 1;
1282 log_nametypeclass(VERB_ALGO, "ratelimit allowed through for "
1283 "delegation point", iq->dp->name,
1284 LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1286 lock_basic_lock(&ie->queries_ratelimit_lock);
1287 ie->num_queries_ratelimited++;
1288 lock_basic_unlock(&ie->queries_ratelimit_lock);
1289 log_nametypeclass(VERB_ALGO, "ratelimit exceeded with "
1290 "delegation point", iq->dp->name,
1291 LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1292 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1296 /* see if this dp not useless.
1298 * o all NS items are required glue.
1299 * or the query is for NS item that is required glue.
1300 * o no addresses are provided.
1302 * Instead, go up one level, and try to get even further
1303 * If the root was useless, use safety belt information.
1304 * Only check cache returns, because replies for servers
1305 * could be useless but lead to loops (bumping into the
1306 * same server reply) if useless-checked.
1308 if(iter_dp_is_useless(&qstate->qinfo, qstate->query_flags,
1310 if(dname_is_root(iq->dp->name)) {
1311 /* use safety belt */
1312 verbose(VERB_QUERY, "Cache has root NS but "
1313 "no addresses. Fallback to the safety belt.");
1314 iq->dp = hints_lookup_root(qstate->env->hints,
1316 /* note deleg_msg is from previous lookup,
1317 * but RD is on, so it is not used */
1319 log_err("internal error: no hints dp");
1320 return error_response(qstate, id,
1321 LDNS_RCODE_REFUSED);
1323 iq->dp = delegpt_copy(iq->dp, qstate->region);
1325 log_err("out of memory in safety belt");
1326 return error_response(qstate, id,
1327 LDNS_RCODE_SERVFAIL);
1332 "cache delegation was useless:");
1333 delegpt_log(VERB_ALGO, iq->dp);
1335 delname = iq->dp->name;
1336 delnamelen = iq->dp->namelen;
1337 dname_remove_label(&delname, &delnamelen);
1342 verbose(VERB_ALGO, "cache delegation returns delegpt");
1343 delegpt_log(VERB_ALGO, iq->dp);
1345 /* Otherwise, set the current delegation point and move on to the
1347 return next_state(iq, INIT_REQUEST_2_STATE);
1351 * Process the second part of the initial request handling. This state
1352 * basically exists so that queries that generate root priming events have
1353 * the same init processing as ones that do not. Request events that reach
1354 * this state must have a valid currentDelegationPoint set.
1356 * This part is primarily handling stub zone priming. Events that reach this
1357 * state must have a current delegation point.
1359 * @param qstate: query state.
1360 * @param iq: iterator query state.
1361 * @param id: module id.
1362 * @return true if the event needs more request processing immediately,
1366 processInitRequest2(struct module_qstate* qstate, struct iter_qstate* iq,
1371 log_query_info(VERB_QUERY, "resolving (init part 2): ",
1374 if(iq->refetch_glue) {
1376 log_err("internal or malloc fail: no dp for refetch");
1377 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1379 delname = iq->dp->name;
1380 delnamelen = iq->dp->namelen;
1382 delname = iq->qchase.qname;
1383 delnamelen = iq->qchase.qname_len;
1385 if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue) {
1386 if(!dname_is_root(delname))
1387 dname_remove_label(&delname, &delnamelen);
1388 iq->refetch_glue = 0; /* if CNAME causes restart, no refetch */
1390 /* Check to see if we need to prime a stub zone. */
1391 if(prime_stub(qstate, iq, id, delname, iq->qchase.qclass)) {
1392 /* A priming sub request was made */
1396 /* most events just get forwarded to the next state. */
1397 return next_state(iq, INIT_REQUEST_3_STATE);
1401 * Process the third part of the initial request handling. This state exists
1402 * as a separate state so that queries that generate stub priming events
1403 * will get the tail end of the init process but not repeat the stub priming
1406 * @param qstate: query state.
1407 * @param iq: iterator query state.
1408 * @param id: module id.
1409 * @return true, advancing the event to the QUERYTARGETS_STATE.
1412 processInitRequest3(struct module_qstate* qstate, struct iter_qstate* iq,
1415 log_query_info(VERB_QUERY, "resolving (init part 3): ",
1417 /* if the cache reply dp equals a validation anchor or msg has DS,
1418 * then DNSSEC RRSIGs are expected in the reply */
1419 iq->dnssec_expected = iter_indicates_dnssec(qstate->env, iq->dp,
1420 iq->deleg_msg, iq->qchase.qclass);
1422 /* If the RD flag wasn't set, then we just finish with the
1423 * cached referral as the response. */
1424 if(!(qstate->query_flags & BIT_RD) && iq->deleg_msg) {
1425 iq->response = iq->deleg_msg;
1426 if(verbosity >= VERB_ALGO && iq->response)
1427 log_dns_msg("no RD requested, using delegation msg",
1428 &iq->response->qinfo, iq->response->rep);
1429 if(qstate->reply_origin)
1430 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1431 return final_state(iq);
1433 /* After this point, unset the RD flag -- this query is going to
1434 * be sent to an auth. server. */
1435 iq->chase_flags &= ~BIT_RD;
1437 /* if dnssec expected, fetch key for the trust-anchor or cached-DS */
1438 if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
1439 !(qstate->query_flags&BIT_CD)) {
1440 generate_dnskey_prefetch(qstate, iq, id);
1441 fptr_ok(fptr_whitelist_modenv_detach_subs(
1442 qstate->env->detach_subs));
1443 (*qstate->env->detach_subs)(qstate);
1446 /* Jump to the next state. */
1447 return next_state(iq, QUERYTARGETS_STATE);
1451 * Given a basic query, generate a parent-side "target" query.
1452 * These are subordinate queries for missing delegation point target addresses,
1453 * for which only the parent of the delegation provides correct IP addresses.
1455 * @param qstate: query state.
1456 * @param iq: iterator query state.
1457 * @param id: module id.
1458 * @param name: target qname.
1459 * @param namelen: target qname length.
1460 * @param qtype: target qtype (either A or AAAA).
1461 * @param qclass: target qclass.
1462 * @return true on success, false on failure.
1465 generate_parentside_target_query(struct module_qstate* qstate,
1466 struct iter_qstate* iq, int id, uint8_t* name, size_t namelen,
1467 uint16_t qtype, uint16_t qclass)
1469 struct module_qstate* subq;
1470 if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1471 id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1474 struct iter_qstate* subiq =
1475 (struct iter_qstate*)subq->minfo[id];
1476 /* blacklist the cache - we want to fetch parent stuff */
1477 sock_list_insert(&subq->blacklist, NULL, 0, subq->region);
1478 subiq->query_for_pside_glue = 1;
1479 if(dname_subdomain_c(name, iq->dp->name)) {
1480 subiq->dp = delegpt_copy(iq->dp, subq->region);
1481 subiq->dnssec_expected = iter_indicates_dnssec(
1482 qstate->env, subiq->dp, NULL,
1483 subq->qinfo.qclass);
1484 subiq->refetch_glue = 1;
1486 subiq->dp = dns_cache_find_delegation(qstate->env,
1487 name, namelen, qtype, qclass, subq->region,
1489 *qstate->env->now+subq->prefetch_leeway);
1490 /* if no dp, then it's from root, refetch unneeded */
1492 subiq->dnssec_expected = iter_indicates_dnssec(
1493 qstate->env, subiq->dp, NULL,
1494 subq->qinfo.qclass);
1495 subiq->refetch_glue = 1;
1499 log_nametypeclass(VERB_QUERY, "new pside target", name, qtype, qclass);
1504 * Given a basic query, generate a "target" query. These are subordinate
1505 * queries for missing delegation point target addresses.
1507 * @param qstate: query state.
1508 * @param iq: iterator query state.
1509 * @param id: module id.
1510 * @param name: target qname.
1511 * @param namelen: target qname length.
1512 * @param qtype: target qtype (either A or AAAA).
1513 * @param qclass: target qclass.
1514 * @return true on success, false on failure.
1517 generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
1518 int id, uint8_t* name, size_t namelen, uint16_t qtype, uint16_t qclass)
1520 struct module_qstate* subq;
1521 if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1522 id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1524 log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
1529 * Given an event at a certain state, generate zero or more target queries
1530 * for it's current delegation point.
1532 * @param qstate: query state.
1533 * @param iq: iterator query state.
1534 * @param ie: iterator shared global environment.
1535 * @param id: module id.
1536 * @param maxtargets: The maximum number of targets to query for.
1537 * if it is negative, there is no maximum number of targets.
1538 * @param num: returns the number of queries generated and processed,
1539 * which may be zero if there were no missing targets.
1540 * @return false on error.
1543 query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
1544 struct iter_env* ie, int id, int maxtargets, int* num)
1546 int query_count = 0;
1547 struct delegpt_ns* ns;
1551 if(iq->depth == ie->max_dependency_depth)
1553 if(iq->depth > 0 && iq->target_count &&
1554 iq->target_count[1] > MAX_TARGET_COUNT) {
1555 char s[LDNS_MAX_DOMAINLEN+1];
1556 dname_str(qstate->qinfo.qname, s);
1557 verbose(VERB_QUERY, "request %s has exceeded the maximum "
1558 "number of glue fetches %d", s, iq->target_count[1]);
1562 iter_mark_cycle_targets(qstate, iq->dp);
1563 missing = (int)delegpt_count_missing_targets(iq->dp);
1564 log_assert(maxtargets != 0); /* that would not be useful */
1566 /* Generate target requests. Basically, any missing targets
1567 * are queried for here, regardless if it is necessary to do
1568 * so to continue processing. */
1569 if(maxtargets < 0 || maxtargets > missing)
1571 else toget = maxtargets;
1576 /* select 'toget' items from the total of 'missing' items */
1577 log_assert(toget <= missing);
1579 /* loop over missing targets */
1580 for(ns = iq->dp->nslist; ns; ns = ns->next) {
1584 /* randomly select this item with probability toget/missing */
1585 if(!iter_ns_probability(qstate->env->rnd, toget, missing)) {
1586 /* do not select this one, next; select toget number
1587 * of items from a list one less in size */
1592 if(ie->supports_ipv6 && !ns->got6) {
1593 /* Send the AAAA request. */
1594 if(!generate_target_query(qstate, iq, id,
1595 ns->name, ns->namelen,
1596 LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
1599 qstate->ext_state[id] = module_wait_subquery;
1604 /* Send the A request. */
1605 if(ie->supports_ipv4 && !ns->got4) {
1606 if(!generate_target_query(qstate, iq, id,
1607 ns->name, ns->namelen,
1608 LDNS_RR_TYPE_A, iq->qchase.qclass)) {
1611 qstate->ext_state[id] = module_wait_subquery;
1617 /* mark this target as in progress. */
1626 qstate->ext_state[id] = module_wait_subquery;
1632 * Called by processQueryTargets when it would like extra targets to query
1633 * but it seems to be out of options. At last resort some less appealing
1634 * options are explored. If there are no more options, the result is SERVFAIL
1636 * @param qstate: query state.
1637 * @param iq: iterator query state.
1638 * @param ie: iterator shared global environment.
1639 * @param id: module id.
1640 * @return true if the event requires more request processing immediately,
1644 processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
1645 struct iter_env* ie, int id)
1647 struct delegpt_ns* ns;
1648 int query_count = 0;
1649 verbose(VERB_ALGO, "No more query targets, attempting last resort");
1652 if(!can_have_last_resort(qstate->env, iq->dp->name, iq->dp->namelen,
1653 iq->qchase.qclass)) {
1654 /* fail -- no more targets, no more hope of targets, no hope
1656 verbose(VERB_QUERY, "configured stub or forward servers failed -- returning SERVFAIL");
1657 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1659 if(!iq->dp->has_parent_side_NS && dname_is_root(iq->dp->name)) {
1660 struct delegpt* p = hints_lookup_root(qstate->env->hints,
1663 struct delegpt_ns* ns;
1664 struct delegpt_addr* a;
1665 iq->chase_flags &= ~BIT_RD; /* go to authorities */
1666 for(ns = p->nslist; ns; ns=ns->next) {
1667 (void)delegpt_add_ns(iq->dp, qstate->region,
1668 ns->name, ns->lame);
1670 for(a = p->target_list; a; a=a->next_target) {
1671 (void)delegpt_add_addr(iq->dp, qstate->region,
1672 &a->addr, a->addrlen, a->bogus,
1676 iq->dp->has_parent_side_NS = 1;
1677 } else if(!iq->dp->has_parent_side_NS) {
1678 if(!iter_lookup_parent_NS_from_cache(qstate->env, iq->dp,
1679 qstate->region, &qstate->qinfo)
1680 || !iq->dp->has_parent_side_NS) {
1681 /* if: malloc failure in lookup go up to try */
1682 /* if: no parent NS in cache - go up one level */
1683 verbose(VERB_ALGO, "try to grab parent NS");
1684 iq->store_parent_NS = iq->dp;
1685 iq->chase_flags &= ~BIT_RD; /* go to authorities */
1686 iq->deleg_msg = NULL;
1687 iq->refetch_glue = 1;
1688 iq->query_restart_count++;
1690 if(qstate->env->cfg->qname_minimisation)
1691 iq->minimisation_state = INIT_MINIMISE_STATE;
1692 return next_state(iq, INIT_REQUEST_STATE);
1695 /* see if that makes new names available */
1696 if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
1697 qstate->region, iq->dp))
1698 log_err("out of memory in cache_fill_missing");
1699 if(iq->dp->usable_list) {
1700 verbose(VERB_ALGO, "try parent-side-name, w. glue from cache");
1701 return next_state(iq, QUERYTARGETS_STATE);
1703 /* try to fill out parent glue from cache */
1704 if(iter_lookup_parent_glue_from_cache(qstate->env, iq->dp,
1705 qstate->region, &qstate->qinfo)) {
1706 /* got parent stuff from cache, see if we can continue */
1707 verbose(VERB_ALGO, "try parent-side glue from cache");
1708 return next_state(iq, QUERYTARGETS_STATE);
1710 /* query for an extra name added by the parent-NS record */
1711 if(delegpt_count_missing_targets(iq->dp) > 0) {
1713 verbose(VERB_ALGO, "try parent-side target name");
1714 if(!query_for_targets(qstate, iq, ie, id, 1, &qs)) {
1715 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1717 iq->num_target_queries += qs;
1718 target_count_increase(iq, qs);
1720 qstate->ext_state[id] = module_wait_subquery;
1721 return 0; /* and wait for them */
1724 if(iq->depth == ie->max_dependency_depth) {
1725 verbose(VERB_QUERY, "maxdepth and need more nameservers, fail");
1726 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1728 if(iq->depth > 0 && iq->target_count &&
1729 iq->target_count[1] > MAX_TARGET_COUNT) {
1730 char s[LDNS_MAX_DOMAINLEN+1];
1731 dname_str(qstate->qinfo.qname, s);
1732 verbose(VERB_QUERY, "request %s has exceeded the maximum "
1733 "number of glue fetches %d", s, iq->target_count[1]);
1734 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1736 /* mark cycle targets for parent-side lookups */
1737 iter_mark_pside_cycle_targets(qstate, iq->dp);
1738 /* see if we can issue queries to get nameserver addresses */
1739 /* this lookup is not randomized, but sequential. */
1740 for(ns = iq->dp->nslist; ns; ns = ns->next) {
1741 /* if this nameserver is at a delegation point, but that
1742 * delegation point is a stub and we cannot go higher, skip*/
1743 if( ((ie->supports_ipv6 && !ns->done_pside6) ||
1744 (ie->supports_ipv4 && !ns->done_pside4)) &&
1745 !can_have_last_resort(qstate->env, ns->name, ns->namelen,
1746 iq->qchase.qclass)) {
1747 log_nametypeclass(VERB_ALGO, "cannot pside lookup ns "
1748 "because it is also a stub/forward,",
1749 ns->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1750 if(ie->supports_ipv6) ns->done_pside6 = 1;
1751 if(ie->supports_ipv4) ns->done_pside4 = 1;
1754 /* query for parent-side A and AAAA for nameservers */
1755 if(ie->supports_ipv6 && !ns->done_pside6) {
1756 /* Send the AAAA request. */
1757 if(!generate_parentside_target_query(qstate, iq, id,
1758 ns->name, ns->namelen,
1759 LDNS_RR_TYPE_AAAA, iq->qchase.qclass))
1760 return error_response(qstate, id,
1761 LDNS_RCODE_SERVFAIL);
1762 ns->done_pside6 = 1;
1765 if(ie->supports_ipv4 && !ns->done_pside4) {
1766 /* Send the A request. */
1767 if(!generate_parentside_target_query(qstate, iq, id,
1768 ns->name, ns->namelen,
1769 LDNS_RR_TYPE_A, iq->qchase.qclass))
1770 return error_response(qstate, id,
1771 LDNS_RCODE_SERVFAIL);
1772 ns->done_pside4 = 1;
1775 if(query_count != 0) { /* suspend to await results */
1776 verbose(VERB_ALGO, "try parent-side glue lookup");
1777 iq->num_target_queries += query_count;
1778 target_count_increase(iq, query_count);
1779 qstate->ext_state[id] = module_wait_subquery;
1784 /* if this was a parent-side glue query itself, then store that
1785 * failure in cache. */
1786 if(!qstate->no_cache_store && iq->query_for_pside_glue
1788 iter_store_parentside_neg(qstate->env, &qstate->qinfo,
1789 iq->deleg_msg?iq->deleg_msg->rep:
1790 (iq->response?iq->response->rep:NULL));
1792 verbose(VERB_QUERY, "out of query targets -- returning SERVFAIL");
1793 /* fail -- no more targets, no more hope of targets, no hope
1795 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1799 * Try to find the NS record set that will resolve a qtype DS query. Due
1800 * to grandparent/grandchild reasons we did not get a proper lookup right
1801 * away. We need to create type NS queries until we get the right parent
1802 * for this lookup. We remove labels from the query to find the right point.
1803 * If we end up at the old dp name, then there is no solution.
1805 * @param qstate: query state.
1806 * @param iq: iterator query state.
1807 * @param id: module id.
1808 * @return true if the event requires more immediate processing, false if
1809 * not. This is generally only true when forwarding the request to
1810 * the final state (i.e., on answer).
1813 processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
1815 struct module_qstate* subq = NULL;
1816 verbose(VERB_ALGO, "processDSNSFind");
1818 if(!iq->dsns_point) {
1820 iq->dsns_point = iq->qchase.qname;
1821 iq->dsns_point_len = iq->qchase.qname_len;
1823 /* robustcheck for internal error: we are not underneath the dp */
1824 if(!dname_subdomain_c(iq->dsns_point, iq->dp->name)) {
1825 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1828 /* go up one (more) step, until we hit the dp, if so, end */
1829 dname_remove_label(&iq->dsns_point, &iq->dsns_point_len);
1830 if(query_dname_compare(iq->dsns_point, iq->dp->name) == 0) {
1831 /* there was no inbetween nameserver, use the old delegation
1832 * point again. And this time, because dsns_point is nonNULL
1833 * we are going to accept the (bad) result */
1834 iq->state = QUERYTARGETS_STATE;
1837 iq->state = DSNS_FIND_STATE;
1839 /* spawn NS lookup (validation not needed, this is for DS lookup) */
1840 log_nametypeclass(VERB_ALGO, "fetch nameservers",
1841 iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1842 if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len,
1843 LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1844 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
1845 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1852 * This is the request event state where the request will be sent to one of
1853 * its current query targets. This state also handles issuing target lookup
1854 * queries for missing target IP addresses. Queries typically iterate on
1855 * this state, both when they are just trying different targets for a given
1856 * delegation point, and when they change delegation points. This state
1857 * roughly corresponds to RFC 1034 algorithm steps 3 and 4.
1859 * @param qstate: query state.
1860 * @param iq: iterator query state.
1861 * @param ie: iterator shared global environment.
1862 * @param id: module id.
1863 * @return true if the event requires more request processing immediately,
1864 * false if not. This state only returns true when it is generating
1865 * a SERVFAIL response because the query has hit a dead end.
1868 processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
1869 struct iter_env* ie, int id)
1872 struct delegpt_addr* target;
1873 struct outbound_entry* outq;
1875 /* NOTE: a request will encounter this state for each target it
1876 * needs to send a query to. That is, at least one per referral,
1877 * more if some targets timeout or return throwaway answers. */
1879 log_query_info(VERB_QUERY, "processQueryTargets:", &qstate->qinfo);
1880 verbose(VERB_ALGO, "processQueryTargets: targetqueries %d, "
1881 "currentqueries %d sentcount %d", iq->num_target_queries,
1882 iq->num_current_queries, iq->sent_count);
1884 /* Make sure that we haven't run away */
1885 /* FIXME: is this check even necessary? */
1886 if(iq->referral_count > MAX_REFERRAL_COUNT) {
1887 verbose(VERB_QUERY, "request has exceeded the maximum "
1888 "number of referrrals with %d", iq->referral_count);
1889 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1891 if(iq->sent_count > MAX_SENT_COUNT) {
1892 verbose(VERB_QUERY, "request has exceeded the maximum "
1893 "number of sends with %d", iq->sent_count);
1894 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1897 /* Make sure we have a delegation point, otherwise priming failed
1898 * or another failure occurred */
1900 verbose(VERB_QUERY, "Failed to get a delegation, giving up");
1901 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1903 if(!ie->supports_ipv6)
1904 delegpt_no_ipv6(iq->dp);
1905 if(!ie->supports_ipv4)
1906 delegpt_no_ipv4(iq->dp);
1907 delegpt_log(VERB_ALGO, iq->dp);
1909 if(iq->num_current_queries>0) {
1910 /* already busy answering a query, this restart is because
1911 * more delegpt addrs became available, wait for existing
1913 verbose(VERB_ALGO, "woke up, but wait for outstanding query");
1914 qstate->ext_state[id] = module_wait_reply;
1919 /* < not <=, because although the array is large enough for <=, the
1920 * generated query will immediately be discarded due to depth and
1921 * that servfail is cached, which is not good as opportunism goes. */
1922 if(iq->depth < ie->max_dependency_depth
1923 && iq->sent_count < TARGET_FETCH_STOP) {
1924 tf_policy = ie->target_fetch_policy[iq->depth];
1927 /* if in 0x20 fallback get as many targets as possible */
1928 if(iq->caps_fallback) {
1930 size_t naddr, nres, navail;
1931 if(!query_for_targets(qstate, iq, ie, id, -1, &extra)) {
1932 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1934 iq->num_target_queries += extra;
1935 target_count_increase(iq, extra);
1936 if(iq->num_target_queries > 0) {
1937 /* wait to get all targets, we want to try em */
1938 verbose(VERB_ALGO, "wait for all targets for fallback");
1939 qstate->ext_state[id] = module_wait_reply;
1942 /* did we do enough fallback queries already? */
1943 delegpt_count_addr(iq->dp, &naddr, &nres, &navail);
1944 /* the current caps_server is the number of fallbacks sent.
1945 * the original query is one that matched too, so we have
1946 * caps_server+1 number of matching queries now */
1947 if(iq->caps_server+1 >= naddr*3 ||
1948 iq->caps_server*2+2 >= MAX_SENT_COUNT) {
1949 /* *2 on sentcount check because ipv6 may fail */
1950 /* we're done, process the response */
1951 verbose(VERB_ALGO, "0x20 fallback had %d responses "
1952 "match for %d wanted, done.",
1953 (int)iq->caps_server+1, (int)naddr*3);
1954 iq->response = iq->caps_response;
1955 iq->caps_fallback = 0;
1956 iter_dec_attempts(iq->dp, 3); /* space for fallback */
1957 iq->num_current_queries++; /* RespState decrements it*/
1958 iq->referral_count++; /* make sure we don't loop */
1960 iq->state = QUERY_RESP_STATE;
1963 verbose(VERB_ALGO, "0x20 fallback number %d",
1964 (int)iq->caps_server);
1966 /* if there is a policy to fetch missing targets
1967 * opportunistically, do it. we rely on the fact that once a
1968 * query (or queries) for a missing name have been issued,
1969 * they will not show up again. */
1970 } else if(tf_policy != 0) {
1972 verbose(VERB_ALGO, "attempt to get extra %d targets",
1974 (void)query_for_targets(qstate, iq, ie, id, tf_policy, &extra);
1975 /* errors ignored, these targets are not strictly necessary for
1976 * this result, we do not have to reply with SERVFAIL */
1977 iq->num_target_queries += extra;
1978 target_count_increase(iq, extra);
1981 /* Add the current set of unused targets to our queue. */
1982 delegpt_add_unused_targets(iq->dp);
1984 /* Select the next usable target, filtering out unsuitable targets. */
1985 target = iter_server_selection(ie, qstate->env, iq->dp,
1986 iq->dp->name, iq->dp->namelen, iq->qchase.qtype,
1987 &iq->dnssec_lame_query, &iq->chase_to_rd,
1988 iq->num_target_queries, qstate->blacklist);
1990 /* If no usable target was selected... */
1992 /* Here we distinguish between three states: generate a new
1993 * target query, just wait, or quit (with a SERVFAIL).
1994 * We have the following information: number of active
1995 * target queries, number of active current queries,
1996 * the presence of missing targets at this delegation
1997 * point, and the given query target policy. */
1999 /* Check for the wait condition. If this is true, then
2000 * an action must be taken. */
2001 if(iq->num_target_queries==0 && iq->num_current_queries==0) {
2002 /* If there is nothing to wait for, then we need
2003 * to distinguish between generating (a) new target
2004 * query, or failing. */
2005 if(delegpt_count_missing_targets(iq->dp) > 0) {
2007 verbose(VERB_ALGO, "querying for next "
2009 if(!query_for_targets(qstate, iq, ie, id,
2011 return error_response(qstate, id,
2012 LDNS_RCODE_SERVFAIL);
2015 delegpt_count_missing_targets(iq->dp) == 0){
2016 /* it looked like there were missing
2017 * targets, but they did not turn up.
2018 * Try the bad choices again (if any),
2019 * when we get back here missing==0,
2020 * so this is not a loop. */
2023 iq->num_target_queries += qs;
2024 target_count_increase(iq, qs);
2026 /* Since a target query might have been made, we
2027 * need to check again. */
2028 if(iq->num_target_queries == 0) {
2029 /* if in capsforid fallback, instead of last
2030 * resort, we agree with the current reply
2031 * we have (if any) (our count of addrs bad)*/
2032 if(iq->caps_fallback && iq->caps_reply) {
2033 /* we're done, process the response */
2034 verbose(VERB_ALGO, "0x20 fallback had %d responses, "
2035 "but no more servers except "
2036 "last resort, done.",
2037 (int)iq->caps_server+1);
2038 iq->response = iq->caps_response;
2039 iq->caps_fallback = 0;
2040 iter_dec_attempts(iq->dp, 3); /* space for fallback */
2041 iq->num_current_queries++; /* RespState decrements it*/
2042 iq->referral_count++; /* make sure we don't loop */
2044 iq->state = QUERY_RESP_STATE;
2047 return processLastResort(qstate, iq, ie, id);
2051 /* otherwise, we have no current targets, so submerge
2052 * until one of the target or direct queries return. */
2053 if(iq->num_target_queries>0 && iq->num_current_queries>0) {
2054 verbose(VERB_ALGO, "no current targets -- waiting "
2055 "for %d targets to resolve or %d outstanding"
2056 " queries to respond", iq->num_target_queries,
2057 iq->num_current_queries);
2058 qstate->ext_state[id] = module_wait_reply;
2059 } else if(iq->num_target_queries>0) {
2060 verbose(VERB_ALGO, "no current targets -- waiting "
2061 "for %d targets to resolve.",
2062 iq->num_target_queries);
2063 qstate->ext_state[id] = module_wait_subquery;
2065 verbose(VERB_ALGO, "no current targets -- waiting "
2066 "for %d outstanding queries to respond.",
2067 iq->num_current_queries);
2068 qstate->ext_state[id] = module_wait_reply;
2073 /* if not forwarding, check ratelimits per delegationpoint name */
2074 if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
2075 if(!infra_ratelimit_inc(qstate->env->infra_cache, iq->dp->name,
2076 iq->dp->namelen, *qstate->env->now)) {
2077 lock_basic_lock(&ie->queries_ratelimit_lock);
2078 ie->num_queries_ratelimited++;
2079 lock_basic_unlock(&ie->queries_ratelimit_lock);
2080 verbose(VERB_ALGO, "query exceeded ratelimits");
2081 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2085 if(iq->minimisation_state == INIT_MINIMISE_STATE) {
2086 /* (Re)set qinfo_out to (new) delegation point, except when
2087 * qinfo_out is already a subdomain of dp. This happens when
2088 * increasing by more than one label at once (QNAMEs with more
2089 * than MAX_MINIMISE_COUNT labels). */
2090 if(!(iq->qinfo_out.qname_len
2091 && dname_subdomain_c(iq->qchase.qname,
2092 iq->qinfo_out.qname)
2093 && dname_subdomain_c(iq->qinfo_out.qname,
2095 iq->qinfo_out.qname = iq->dp->name;
2096 iq->qinfo_out.qname_len = iq->dp->namelen;
2097 iq->qinfo_out.qtype = LDNS_RR_TYPE_A;
2098 iq->qinfo_out.qclass = iq->qchase.qclass;
2099 iq->qinfo_out.local_alias = NULL;
2100 iq->minimise_count = 0;
2103 iq->minimisation_state = MINIMISE_STATE;
2105 if(iq->minimisation_state == MINIMISE_STATE) {
2106 int qchaselabs = dname_count_labels(iq->qchase.qname);
2107 int labdiff = qchaselabs -
2108 dname_count_labels(iq->qinfo_out.qname);
2110 iq->qinfo_out.qname = iq->qchase.qname;
2111 iq->qinfo_out.qname_len = iq->qchase.qname_len;
2112 iq->minimise_count++;
2113 iq->minimise_timeout_count = 0;
2115 iter_dec_attempts(iq->dp, 1);
2117 /* Limit number of iterations for QNAMEs with more
2118 * than MAX_MINIMISE_COUNT labels. Send first MINIMISE_ONE_LAB
2119 * labels of QNAME always individually.
2121 if(qchaselabs > MAX_MINIMISE_COUNT && labdiff > 1 &&
2122 iq->minimise_count > MINIMISE_ONE_LAB) {
2123 if(iq->minimise_count < MAX_MINIMISE_COUNT) {
2124 int multilabs = qchaselabs - 1 -
2126 int extralabs = multilabs /
2127 MINIMISE_MULTIPLE_LABS;
2129 if (MAX_MINIMISE_COUNT - iq->minimise_count >=
2130 multilabs % MINIMISE_MULTIPLE_LABS)
2131 /* Default behaviour is to add 1 label
2132 * every iteration. Therefore, decrement
2133 * the extralabs by 1 */
2135 if (extralabs < labdiff)
2136 labdiff -= extralabs;
2140 /* Last minimised iteration, send all labels with
2147 verbose(VERB_QUERY, "removing %d labels", labdiff-1);
2148 dname_remove_labels(&iq->qinfo_out.qname,
2149 &iq->qinfo_out.qname_len,
2152 if(labdiff < 1 || (labdiff < 2
2153 && (iq->qchase.qtype == LDNS_RR_TYPE_DS
2154 || iq->qchase.qtype == LDNS_RR_TYPE_A)))
2155 /* Stop minimising this query, resolve "as usual" */
2156 iq->minimisation_state = DONOT_MINIMISE_STATE;
2157 else if(!qstate->no_cache_lookup) {
2158 struct dns_msg* msg = dns_cache_lookup(qstate->env,
2159 iq->qinfo_out.qname, iq->qinfo_out.qname_len,
2160 iq->qinfo_out.qtype, iq->qinfo_out.qclass,
2161 qstate->query_flags, qstate->region,
2162 qstate->env->scratch);
2163 if(msg && msg->rep->an_numrrsets == 0
2164 && FLAGS_GET_RCODE(msg->rep->flags) ==
2166 /* no need to send query if it is already
2167 * cached as NOERROR/NODATA */
2171 if(iq->minimisation_state == SKIP_MINIMISE_STATE) {
2172 if(iq->minimise_timeout_count < MAX_MINIMISE_TIMEOUT_COUNT)
2173 /* Do not increment qname, continue incrementing next
2175 iq->minimisation_state = MINIMISE_STATE;
2176 else if(!qstate->env->cfg->qname_minimisation_strict)
2177 /* Too many time-outs detected for this QNAME and QTYPE.
2178 * We give up, disable QNAME minimisation. */
2179 iq->minimisation_state = DONOT_MINIMISE_STATE;
2181 if(iq->minimisation_state == DONOT_MINIMISE_STATE)
2182 iq->qinfo_out = iq->qchase;
2184 /* We have a valid target. */
2185 if(verbosity >= VERB_QUERY) {
2186 log_query_info(VERB_QUERY, "sending query:", &iq->qinfo_out);
2187 log_name_addr(VERB_QUERY, "sending to target:", iq->dp->name,
2188 &target->addr, target->addrlen);
2189 verbose(VERB_ALGO, "dnssec status: %s%s",
2190 iq->dnssec_expected?"expected": "not expected",
2191 iq->dnssec_lame_query?" but lame_query anyway": "");
2193 fptr_ok(fptr_whitelist_modenv_send_query(qstate->env->send_query));
2194 outq = (*qstate->env->send_query)(&iq->qinfo_out,
2195 iq->chase_flags | (iq->chase_to_rd?BIT_RD:0),
2196 /* unset CD if to forwarder(RD set) and not dnssec retry
2197 * (blacklist nonempty) and no trust-anchors are configured
2198 * above the qname or on the first attempt when dnssec is on */
2199 EDNS_DO| ((iq->chase_to_rd||(iq->chase_flags&BIT_RD)!=0)&&
2200 !qstate->blacklist&&(!iter_indicates_dnssec_fwd(qstate->env,
2201 &iq->qinfo_out)||target->attempts==1)?0:BIT_CD),
2202 iq->dnssec_expected, iq->caps_fallback || is_caps_whitelisted(
2203 ie, iq), &target->addr, target->addrlen,
2204 iq->dp->name, iq->dp->namelen,
2205 (iq->dp->ssl_upstream || qstate->env->cfg->ssl_upstream), qstate);
2207 log_addr(VERB_DETAIL, "error sending query to auth server",
2208 &target->addr, target->addrlen);
2209 if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok)
2210 infra_ratelimit_dec(qstate->env->infra_cache, iq->dp->name,
2211 iq->dp->namelen, *qstate->env->now);
2212 if(qstate->env->cfg->qname_minimisation)
2213 iq->minimisation_state = SKIP_MINIMISE_STATE;
2214 return next_state(iq, QUERYTARGETS_STATE);
2216 outbound_list_insert(&iq->outlist, outq);
2217 iq->num_current_queries++;
2219 qstate->ext_state[id] = module_wait_reply;
2224 /** find NS rrset in given list */
2225 static struct ub_packed_rrset_key*
2226 find_NS(struct reply_info* rep, size_t from, size_t to)
2229 for(i=from; i<to; i++) {
2230 if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
2231 return rep->rrsets[i];
2238 * Process the query response. All queries end up at this state first. This
2239 * process generally consists of analyzing the response and routing the
2240 * event to the next state (either bouncing it back to a request state, or
2241 * terminating the processing for this event).
2243 * @param qstate: query state.
2244 * @param iq: iterator query state.
2245 * @param id: module id.
2246 * @return true if the event requires more immediate processing, false if
2247 * not. This is generally only true when forwarding the request to
2248 * the final state (i.e., on answer).
2251 processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
2255 enum response_type type;
2256 iq->num_current_queries--;
2258 if(!inplace_cb_query_response_call(qstate->env, qstate, iq->response))
2259 log_err("unable to call query_response callback");
2261 if(iq->response == NULL) {
2262 /* Don't increment qname when QNAME minimisation is enabled */
2263 if(qstate->env->cfg->qname_minimisation) {
2264 iq->minimise_timeout_count++;
2265 iq->minimisation_state = SKIP_MINIMISE_STATE;
2267 iq->chase_to_rd = 0;
2268 iq->dnssec_lame_query = 0;
2269 verbose(VERB_ALGO, "query response was timeout");
2270 return next_state(iq, QUERYTARGETS_STATE);
2272 type = response_type_from_server(
2273 (int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2274 iq->response, &iq->qchase, iq->dp);
2275 iq->chase_to_rd = 0;
2276 if(type == RESPONSE_TYPE_REFERRAL && (iq->chase_flags&BIT_RD)) {
2277 /* When forwarding (RD bit is set), we handle referrals
2278 * differently. No queries should be sent elsewhere */
2279 type = RESPONSE_TYPE_ANSWER;
2281 if(!qstate->env->cfg->disable_dnssec_lame_check && iq->dnssec_expected
2282 && !iq->dnssec_lame_query &&
2283 !(iq->chase_flags&BIT_RD)
2284 && iq->sent_count < DNSSEC_LAME_DETECT_COUNT
2285 && type != RESPONSE_TYPE_LAME
2286 && type != RESPONSE_TYPE_REC_LAME
2287 && type != RESPONSE_TYPE_THROWAWAY
2288 && type != RESPONSE_TYPE_UNTYPED) {
2289 /* a possible answer, see if it is missing DNSSEC */
2290 /* but not when forwarding, so we dont mark fwder lame */
2291 if(!iter_msg_has_dnssec(iq->response)) {
2292 /* Mark this address as dnsseclame in this dp,
2293 * because that will make serverselection disprefer
2294 * it, but also, once it is the only final option,
2295 * use dnssec-lame-bypass if it needs to query there.*/
2297 struct delegpt_addr* a = delegpt_find_addr(
2298 iq->dp, &qstate->reply->addr,
2299 qstate->reply->addrlen);
2300 if(a) a->dnsseclame = 1;
2302 /* test the answer is from the zone we expected,
2303 * otherwise, (due to parent,child on same server), we
2304 * might mark the server,zone lame inappropriately */
2305 if(!iter_msg_from_zone(iq->response, iq->dp, type,
2307 qstate->reply = NULL;
2308 type = RESPONSE_TYPE_LAME;
2311 } else iq->dnssec_lame_query = 0;
2312 /* see if referral brings us close to the target */
2313 if(type == RESPONSE_TYPE_REFERRAL) {
2314 struct ub_packed_rrset_key* ns = find_NS(
2315 iq->response->rep, iq->response->rep->an_numrrsets,
2316 iq->response->rep->an_numrrsets
2317 + iq->response->rep->ns_numrrsets);
2318 if(!ns) ns = find_NS(iq->response->rep, 0,
2319 iq->response->rep->an_numrrsets);
2320 if(!ns || !dname_strict_subdomain_c(ns->rk.dname, iq->dp->name)
2321 || !dname_subdomain_c(iq->qchase.qname, ns->rk.dname)){
2322 verbose(VERB_ALGO, "bad referral, throwaway");
2323 type = RESPONSE_TYPE_THROWAWAY;
2325 iter_scrub_ds(iq->response, ns, iq->dp->name);
2326 } else iter_scrub_ds(iq->response, NULL, NULL);
2327 if(type == RESPONSE_TYPE_THROWAWAY &&
2328 FLAGS_GET_RCODE(iq->response->rep->flags) == LDNS_RCODE_YXDOMAIN) {
2329 /* YXDOMAIN is a permanent error, no need to retry */
2330 type = RESPONSE_TYPE_ANSWER;
2332 if(type == RESPONSE_TYPE_CNAME && iq->response->rep->an_numrrsets >= 1
2333 && ntohs(iq->response->rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_DNAME) {
2334 uint8_t* sname = NULL;
2335 size_t snamelen = 0;
2336 get_cname_target(iq->response->rep->rrsets[0], &sname,
2338 if(snamelen && dname_subdomain_c(sname, iq->response->rep->rrsets[0]->rk.dname)) {
2339 /* DNAME to a subdomain loop; do not recurse */
2340 type = RESPONSE_TYPE_ANSWER;
2344 /* handle each of the type cases */
2345 if(type == RESPONSE_TYPE_ANSWER) {
2346 /* ANSWER type responses terminate the query algorithm,
2347 * so they sent on their */
2348 if(verbosity >= VERB_DETAIL) {
2349 verbose(VERB_DETAIL, "query response was %s",
2350 FLAGS_GET_RCODE(iq->response->rep->flags)
2351 ==LDNS_RCODE_NXDOMAIN?"NXDOMAIN ANSWER":
2352 (iq->response->rep->an_numrrsets?"ANSWER":
2355 /* if qtype is DS, check we have the right level of answer,
2356 * like grandchild answer but we need the middle, reject it */
2357 if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2358 && !(iq->chase_flags&BIT_RD)
2359 && iter_ds_toolow(iq->response, iq->dp)
2360 && iter_dp_cangodown(&iq->qchase, iq->dp)) {
2361 /* close down outstanding requests to be discarded */
2362 outbound_list_clear(&iq->outlist);
2363 iq->num_current_queries = 0;
2364 fptr_ok(fptr_whitelist_modenv_detach_subs(
2365 qstate->env->detach_subs));
2366 (*qstate->env->detach_subs)(qstate);
2367 iq->num_target_queries = 0;
2368 return processDSNSFind(qstate, iq, id);
2370 if(!qstate->no_cache_store)
2371 iter_dns_store(qstate->env, &iq->response->qinfo,
2372 iq->response->rep, 0, qstate->prefetch_leeway,
2373 iq->dp&&iq->dp->has_parent_side_NS,
2374 qstate->region, qstate->query_flags);
2375 /* close down outstanding requests to be discarded */
2376 outbound_list_clear(&iq->outlist);
2377 iq->num_current_queries = 0;
2378 fptr_ok(fptr_whitelist_modenv_detach_subs(
2379 qstate->env->detach_subs));
2380 (*qstate->env->detach_subs)(qstate);
2381 iq->num_target_queries = 0;
2383 sock_list_insert(&qstate->reply_origin,
2384 &qstate->reply->addr, qstate->reply->addrlen,
2386 if(iq->minimisation_state != DONOT_MINIMISE_STATE) {
2387 if(FLAGS_GET_RCODE(iq->response->rep->flags) !=
2388 LDNS_RCODE_NOERROR) {
2389 if(qstate->env->cfg->qname_minimisation_strict)
2390 return final_state(iq);
2391 /* Best effort qname-minimisation.
2392 * Stop minimising and send full query when
2393 * RCODE is not NOERROR. */
2394 iq->minimisation_state = DONOT_MINIMISE_STATE;
2396 if(FLAGS_GET_RCODE(iq->response->rep->flags) ==
2397 LDNS_RCODE_NXDOMAIN) {
2398 /* Stop resolving when NXDOMAIN is DNSSEC
2399 * signed. Based on assumption that nameservers
2400 * serving signed zones do not return NXDOMAIN
2401 * for empty-non-terminals. */
2402 if(iq->dnssec_expected)
2403 return final_state(iq);
2404 /* Make subrequest to validate intermediate
2405 * NXDOMAIN if harden-below-nxdomain is
2407 if(qstate->env->cfg->harden_below_nxdomain) {
2408 struct module_qstate* subq = NULL;
2409 log_query_info(VERB_QUERY,
2410 "schedule NXDOMAIN validation:",
2411 &iq->response->qinfo);
2412 if(!generate_sub_request(
2413 iq->response->qinfo.qname,
2414 iq->response->qinfo.qname_len,
2415 iq->response->qinfo.qtype,
2416 iq->response->qinfo.qclass,
2419 FINISHED_STATE, &subq, 1))
2421 "could not validate NXDOMAIN "
2425 return next_state(iq, QUERYTARGETS_STATE);
2427 return final_state(iq);
2428 } else if(type == RESPONSE_TYPE_REFERRAL) {
2429 /* REFERRAL type responses get a reset of the
2430 * delegation point, and back to the QUERYTARGETS_STATE. */
2431 verbose(VERB_DETAIL, "query response was REFERRAL");
2433 if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
2434 /* we have a referral, no ratelimit, we can send
2435 * our queries to the given name */
2436 infra_ratelimit_dec(qstate->env->infra_cache,
2437 iq->dp->name, iq->dp->namelen,
2441 /* if hardened, only store referral if we asked for it */
2442 if(!qstate->no_cache_store &&
2443 (!qstate->env->cfg->harden_referral_path ||
2444 ( qstate->qinfo.qtype == LDNS_RR_TYPE_NS
2445 && (qstate->query_flags&BIT_RD)
2446 && !(qstate->query_flags&BIT_CD)
2447 /* we know that all other NS rrsets are scrubbed
2448 * away, thus on referral only one is left.
2449 * see if that equals the query name... */
2450 && ( /* auth section, but sometimes in answer section*/
2451 reply_find_rrset_section_ns(iq->response->rep,
2452 iq->qchase.qname, iq->qchase.qname_len,
2453 LDNS_RR_TYPE_NS, iq->qchase.qclass)
2454 || reply_find_rrset_section_an(iq->response->rep,
2455 iq->qchase.qname, iq->qchase.qname_len,
2456 LDNS_RR_TYPE_NS, iq->qchase.qclass)
2459 /* Store the referral under the current query */
2460 /* no prefetch-leeway, since its not the answer */
2461 iter_dns_store(qstate->env, &iq->response->qinfo,
2462 iq->response->rep, 1, 0, 0, NULL, 0);
2463 if(iq->store_parent_NS)
2464 iter_store_parentside_NS(qstate->env,
2466 if(qstate->env->neg_cache)
2467 val_neg_addreferral(qstate->env->neg_cache,
2468 iq->response->rep, iq->dp->name);
2470 /* store parent-side-in-zone-glue, if directly queried for */
2471 if(!qstate->no_cache_store && iq->query_for_pside_glue
2472 && !iq->pside_glue) {
2473 iq->pside_glue = reply_find_rrset(iq->response->rep,
2474 iq->qchase.qname, iq->qchase.qname_len,
2475 iq->qchase.qtype, iq->qchase.qclass);
2476 if(iq->pside_glue) {
2477 log_rrset_key(VERB_ALGO, "found parent-side "
2478 "glue", iq->pside_glue);
2479 iter_store_parentside_rrset(qstate->env,
2484 /* Reset the event state, setting the current delegation
2485 * point to the referral. */
2486 iq->deleg_msg = iq->response;
2487 iq->dp = delegpt_from_message(iq->response, qstate->region);
2488 if (qstate->env->cfg->qname_minimisation)
2489 iq->minimisation_state = INIT_MINIMISE_STATE;
2491 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2492 if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
2493 qstate->region, iq->dp))
2494 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2495 if(iq->store_parent_NS && query_dname_compare(iq->dp->name,
2496 iq->store_parent_NS->name) == 0)
2497 iter_merge_retry_counts(iq->dp, iq->store_parent_NS);
2498 delegpt_log(VERB_ALGO, iq->dp);
2499 /* Count this as a referral. */
2500 iq->referral_count++;
2502 /* see if the next dp is a trust anchor, or a DS was sent
2503 * along, indicating dnssec is expected for next zone */
2504 iq->dnssec_expected = iter_indicates_dnssec(qstate->env,
2505 iq->dp, iq->response, iq->qchase.qclass);
2506 /* if dnssec, validating then also fetch the key for the DS */
2507 if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
2508 !(qstate->query_flags&BIT_CD))
2509 generate_dnskey_prefetch(qstate, iq, id);
2511 /* spawn off NS and addr to auth servers for the NS we just
2512 * got in the referral. This gets authoritative answer
2513 * (answer section trust level) rrset.
2514 * right after, we detach the subs, answer goes to cache. */
2515 if(qstate->env->cfg->harden_referral_path)
2516 generate_ns_check(qstate, iq, id);
2518 /* stop current outstanding queries.
2519 * FIXME: should the outstanding queries be waited for and
2520 * handled? Say by a subquery that inherits the outbound_entry.
2522 outbound_list_clear(&iq->outlist);
2523 iq->num_current_queries = 0;
2524 fptr_ok(fptr_whitelist_modenv_detach_subs(
2525 qstate->env->detach_subs));
2526 (*qstate->env->detach_subs)(qstate);
2527 iq->num_target_queries = 0;
2528 verbose(VERB_ALGO, "cleared outbound list for next round");
2529 return next_state(iq, QUERYTARGETS_STATE);
2530 } else if(type == RESPONSE_TYPE_CNAME) {
2531 uint8_t* sname = NULL;
2532 size_t snamelen = 0;
2533 /* CNAME type responses get a query restart (i.e., get a
2534 * reset of the query state and go back to INIT_REQUEST_STATE).
2536 verbose(VERB_DETAIL, "query response was CNAME");
2537 if(verbosity >= VERB_ALGO)
2538 log_dns_msg("cname msg", &iq->response->qinfo,
2540 /* if qtype is DS, check we have the right level of answer,
2541 * like grandchild answer but we need the middle, reject it */
2542 if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2543 && !(iq->chase_flags&BIT_RD)
2544 && iter_ds_toolow(iq->response, iq->dp)
2545 && iter_dp_cangodown(&iq->qchase, iq->dp)) {
2546 outbound_list_clear(&iq->outlist);
2547 iq->num_current_queries = 0;
2548 fptr_ok(fptr_whitelist_modenv_detach_subs(
2549 qstate->env->detach_subs));
2550 (*qstate->env->detach_subs)(qstate);
2551 iq->num_target_queries = 0;
2552 return processDSNSFind(qstate, iq, id);
2554 /* Process the CNAME response. */
2555 if(!handle_cname_response(qstate, iq, iq->response,
2557 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2558 /* cache the CNAME response under the current query */
2559 /* NOTE : set referral=1, so that rrsets get stored but not
2560 * the partial query answer (CNAME only). */
2561 /* prefetchleeway applied because this updates answer parts */
2562 if(!qstate->no_cache_store)
2563 iter_dns_store(qstate->env, &iq->response->qinfo,
2564 iq->response->rep, 1, qstate->prefetch_leeway,
2565 iq->dp&&iq->dp->has_parent_side_NS, NULL,
2566 qstate->query_flags);
2567 /* set the current request's qname to the new value. */
2568 iq->qchase.qname = sname;
2569 iq->qchase.qname_len = snamelen;
2570 if (qstate->env->cfg->qname_minimisation)
2571 iq->minimisation_state = INIT_MINIMISE_STATE;
2572 /* Clear the query state, since this is a query restart. */
2573 iq->deleg_msg = NULL;
2575 iq->dsns_point = NULL;
2576 /* Note the query restart. */
2577 iq->query_restart_count++;
2580 /* stop current outstanding queries.
2581 * FIXME: should the outstanding queries be waited for and
2582 * handled? Say by a subquery that inherits the outbound_entry.
2584 outbound_list_clear(&iq->outlist);
2585 iq->num_current_queries = 0;
2586 fptr_ok(fptr_whitelist_modenv_detach_subs(
2587 qstate->env->detach_subs));
2588 (*qstate->env->detach_subs)(qstate);
2589 iq->num_target_queries = 0;
2591 sock_list_insert(&qstate->reply_origin,
2592 &qstate->reply->addr, qstate->reply->addrlen,
2594 verbose(VERB_ALGO, "cleared outbound list for query restart");
2595 /* go to INIT_REQUEST_STATE for new qname. */
2596 return next_state(iq, INIT_REQUEST_STATE);
2597 } else if(type == RESPONSE_TYPE_LAME) {
2598 /* Cache the LAMEness. */
2599 verbose(VERB_DETAIL, "query response was %sLAME",
2600 dnsseclame?"DNSSEC ":"");
2601 if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2602 log_err("mark lame: mismatch in qname and dpname");
2603 /* throwaway this reply below */
2604 } else if(qstate->reply) {
2605 /* need addr for lameness cache, but we may have
2606 * gotten this from cache, so test to be sure */
2607 if(!infra_set_lame(qstate->env->infra_cache,
2608 &qstate->reply->addr, qstate->reply->addrlen,
2609 iq->dp->name, iq->dp->namelen,
2610 *qstate->env->now, dnsseclame, 0,
2612 log_err("mark host lame: out of memory");
2614 } else if(type == RESPONSE_TYPE_REC_LAME) {
2615 /* Cache the LAMEness. */
2616 verbose(VERB_DETAIL, "query response REC_LAME: "
2617 "recursive but not authoritative server");
2618 if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2619 log_err("mark rec_lame: mismatch in qname and dpname");
2620 /* throwaway this reply below */
2621 } else if(qstate->reply) {
2622 /* need addr for lameness cache, but we may have
2623 * gotten this from cache, so test to be sure */
2624 verbose(VERB_DETAIL, "mark as REC_LAME");
2625 if(!infra_set_lame(qstate->env->infra_cache,
2626 &qstate->reply->addr, qstate->reply->addrlen,
2627 iq->dp->name, iq->dp->namelen,
2628 *qstate->env->now, 0, 1, iq->qchase.qtype))
2629 log_err("mark host lame: out of memory");
2631 } else if(type == RESPONSE_TYPE_THROWAWAY) {
2632 /* LAME and THROWAWAY responses are handled the same way.
2633 * In this case, the event is just sent directly back to
2634 * the QUERYTARGETS_STATE without resetting anything,
2635 * because, clearly, the next target must be tried. */
2636 verbose(VERB_DETAIL, "query response was THROWAWAY");
2638 log_warn("A query response came back with an unknown type: %d",
2642 /* LAME, THROWAWAY and "unknown" all end up here.
2643 * Recycle to the QUERYTARGETS state to hopefully try a
2644 * different target. */
2645 if (qstate->env->cfg->qname_minimisation &&
2646 !qstate->env->cfg->qname_minimisation_strict)
2647 iq->minimisation_state = DONOT_MINIMISE_STATE;
2648 return next_state(iq, QUERYTARGETS_STATE);
2652 * Return priming query results to interested super querystates.
2654 * Sets the delegation point and delegation message (not nonRD queries).
2655 * This is a callback from walk_supers.
2657 * @param qstate: priming query state that finished.
2658 * @param id: module id.
2659 * @param forq: the qstate for which priming has been done.
2662 prime_supers(struct module_qstate* qstate, int id, struct module_qstate* forq)
2664 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2665 struct delegpt* dp = NULL;
2667 log_assert(qstate->is_priming || foriq->wait_priming_stub);
2668 log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2669 /* Convert our response to a delegation point */
2670 dp = delegpt_from_message(qstate->return_msg, forq->region);
2672 /* if there is no convertable delegation point, then
2673 * the ANSWER type was (presumably) a negative answer. */
2674 verbose(VERB_ALGO, "prime response was not a positive "
2677 foriq->state = QUERYTARGETS_STATE;
2681 log_query_info(VERB_DETAIL, "priming successful for", &qstate->qinfo);
2682 delegpt_log(VERB_ALGO, dp);
2684 foriq->deleg_msg = dns_copy_msg(qstate->return_msg, forq->region);
2685 if(!foriq->deleg_msg) {
2686 log_err("copy prime response: out of memory");
2688 foriq->state = QUERYTARGETS_STATE;
2692 /* root priming responses go to init stage 2, priming stub
2693 * responses to to stage 3. */
2694 if(foriq->wait_priming_stub) {
2695 foriq->state = INIT_REQUEST_3_STATE;
2696 foriq->wait_priming_stub = 0;
2697 } else foriq->state = INIT_REQUEST_2_STATE;
2698 /* because we are finished, the parent will be reactivated */
2702 * This handles the response to a priming query. This is used to handle both
2703 * root and stub priming responses. This is basically the equivalent of the
2704 * QUERY_RESP_STATE, but will not handle CNAME responses and will treat
2705 * REFERRALs as ANSWERS. It will also update and reactivate the originating
2708 * @param qstate: query state.
2709 * @param id: module id.
2710 * @return true if the event needs more immediate processing, false if not.
2711 * This state always returns false.
2714 processPrimeResponse(struct module_qstate* qstate, int id)
2716 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2717 enum response_type type;
2718 iq->response->rep->flags &= ~(BIT_RD|BIT_RA); /* ignore rec-lame */
2719 type = response_type_from_server(
2720 (int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2721 iq->response, &iq->qchase, iq->dp);
2722 if(type == RESPONSE_TYPE_ANSWER) {
2723 qstate->return_rcode = LDNS_RCODE_NOERROR;
2724 qstate->return_msg = iq->response;
2726 qstate->return_rcode = LDNS_RCODE_SERVFAIL;
2727 qstate->return_msg = NULL;
2730 /* validate the root or stub after priming (if enabled).
2731 * This is the same query as the prime query, but with validation.
2732 * Now that we are primed, the additional queries that validation
2733 * may need can be resolved, such as DLV. */
2734 if(qstate->env->cfg->harden_referral_path) {
2735 struct module_qstate* subq = NULL;
2736 log_nametypeclass(VERB_ALGO, "schedule prime validation",
2737 qstate->qinfo.qname, qstate->qinfo.qtype,
2738 qstate->qinfo.qclass);
2739 if(!generate_sub_request(qstate->qinfo.qname,
2740 qstate->qinfo.qname_len, qstate->qinfo.qtype,
2741 qstate->qinfo.qclass, qstate, id, iq,
2742 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
2743 verbose(VERB_ALGO, "could not generate prime check");
2745 generate_a_aaaa_check(qstate, iq, id);
2748 /* This event is finished. */
2749 qstate->ext_state[id] = module_finished;
2754 * Do final processing on responses to target queries. Events reach this
2755 * state after the iterative resolution algorithm terminates. This state is
2756 * responsible for reactivating the original event, and housekeeping related
2757 * to received target responses (caching, updating the current delegation
2759 * Callback from walk_supers for every super state that is interested in
2760 * the results from this query.
2762 * @param qstate: query state.
2763 * @param id: module id.
2764 * @param forq: super query state.
2767 processTargetResponse(struct module_qstate* qstate, int id,
2768 struct module_qstate* forq)
2770 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2771 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2772 struct ub_packed_rrset_key* rrset;
2773 struct delegpt_ns* dpns;
2774 log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2776 foriq->state = QUERYTARGETS_STATE;
2777 log_query_info(VERB_ALGO, "processTargetResponse", &qstate->qinfo);
2778 log_query_info(VERB_ALGO, "processTargetResponse super", &forq->qinfo);
2780 /* Tell the originating event that this target query has finished
2781 * (regardless if it succeeded or not). */
2782 foriq->num_target_queries--;
2784 /* check to see if parent event is still interested (in orig name). */
2786 verbose(VERB_ALGO, "subq: parent not interested, was reset");
2787 return; /* not interested anymore */
2789 dpns = delegpt_find_ns(foriq->dp, qstate->qinfo.qname,
2790 qstate->qinfo.qname_len);
2792 /* If not interested, just stop processing this event */
2793 verbose(VERB_ALGO, "subq: parent not interested anymore");
2794 /* could be because parent was jostled out of the cache,
2795 and a new identical query arrived, that does not want it*/
2799 /* if iq->query_for_pside_glue then add the pside_glue (marked lame) */
2800 if(iq->pside_glue) {
2801 /* if the pside_glue is NULL, then it could not be found,
2802 * the done_pside is already set when created and a cache
2803 * entry created in processFinished so nothing to do here */
2804 log_rrset_key(VERB_ALGO, "add parentside glue to dp",
2806 if(!delegpt_add_rrset(foriq->dp, forq->region,
2808 log_err("out of memory adding pside glue");
2811 /* This response is relevant to the current query, so we
2812 * add (attempt to add, anyway) this target(s) and reactivate
2813 * the original event.
2814 * NOTE: we could only look for the AnswerRRset if the
2815 * response type was ANSWER. */
2816 rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
2818 /* if CNAMEs have been followed - add new NS to delegpt. */
2819 /* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
2820 if(!delegpt_find_ns(foriq->dp, rrset->rk.dname,
2821 rrset->rk.dname_len)) {
2822 /* if dpns->lame then set newcname ns lame too */
2823 if(!delegpt_add_ns(foriq->dp, forq->region,
2824 rrset->rk.dname, dpns->lame))
2825 log_err("out of memory adding cnamed-ns");
2827 /* if dpns->lame then set the address(es) lame too */
2828 if(!delegpt_add_rrset(foriq->dp, forq->region, rrset,
2830 log_err("out of memory adding targets");
2831 verbose(VERB_ALGO, "added target response");
2832 delegpt_log(VERB_ALGO, foriq->dp);
2834 verbose(VERB_ALGO, "iterator TargetResponse failed");
2835 dpns->resolved = 1; /* fail the target */
2840 * Process response for DS NS Find queries, that attempt to find the delegation
2841 * point where we ask the DS query from.
2843 * @param qstate: query state.
2844 * @param id: module id.
2845 * @param forq: super query state.
2848 processDSNSResponse(struct module_qstate* qstate, int id,
2849 struct module_qstate* forq)
2851 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2853 /* if the finished (iq->response) query has no NS set: continue
2854 * up to look for the right dp; nothing to change, do DPNSstate */
2855 if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2856 return; /* seek further */
2857 /* find the NS RRset (without allowing CNAMEs) */
2858 if(!reply_find_rrset(qstate->return_msg->rep, qstate->qinfo.qname,
2859 qstate->qinfo.qname_len, LDNS_RR_TYPE_NS,
2860 qstate->qinfo.qclass)){
2861 return; /* seek further */
2864 /* else, store as DP and continue at querytargets */
2865 foriq->state = QUERYTARGETS_STATE;
2866 foriq->dp = delegpt_from_message(qstate->return_msg, forq->region);
2868 log_err("out of memory in dsns dp alloc");
2869 return; /* dp==NULL in QUERYTARGETS makes SERVFAIL */
2871 /* success, go query the querytargets in the new dp (and go down) */
2875 * Process response for qclass=ANY queries for a particular class.
2876 * Append to result or error-exit.
2878 * @param qstate: query state.
2879 * @param id: module id.
2880 * @param forq: super query state.
2883 processClassResponse(struct module_qstate* qstate, int id,
2884 struct module_qstate* forq)
2886 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2887 struct dns_msg* from = qstate->return_msg;
2888 log_query_info(VERB_ALGO, "processClassResponse", &qstate->qinfo);
2889 log_query_info(VERB_ALGO, "processClassResponse super", &forq->qinfo);
2890 if(qstate->return_rcode != LDNS_RCODE_NOERROR) {
2891 /* cause servfail for qclass ANY query */
2892 foriq->response = NULL;
2893 foriq->state = FINISHED_STATE;
2897 if(!foriq->response) {
2898 /* allocate the response: copy RCODE, sec_state */
2899 foriq->response = dns_copy_msg(from, forq->region);
2900 if(!foriq->response) {
2901 log_err("malloc failed for qclass ANY response");
2902 foriq->state = FINISHED_STATE;
2905 foriq->response->qinfo.qclass = forq->qinfo.qclass;
2906 /* qclass ANY does not receive the AA flag on replies */
2907 foriq->response->rep->authoritative = 0;
2909 struct dns_msg* to = foriq->response;
2910 /* add _from_ this response _to_ existing collection */
2911 /* if there are records, copy RCODE */
2912 /* lower sec_state if this message is lower */
2913 if(from->rep->rrset_count != 0) {
2914 size_t n = from->rep->rrset_count+to->rep->rrset_count;
2915 struct ub_packed_rrset_key** dest, **d;
2916 /* copy appropriate rcode */
2917 to->rep->flags = from->rep->flags;
2919 if(from->rep->rrset_count > RR_COUNT_MAX ||
2920 to->rep->rrset_count > RR_COUNT_MAX) {
2921 log_err("malloc failed (too many rrsets) in collect ANY");
2922 foriq->state = FINISHED_STATE;
2923 return; /* integer overflow protection */
2925 dest = regional_alloc(forq->region, sizeof(dest[0])*n);
2927 log_err("malloc failed in collect ANY");
2928 foriq->state = FINISHED_STATE;
2933 memcpy(dest, to->rep->rrsets, to->rep->an_numrrsets
2935 dest += to->rep->an_numrrsets;
2936 memcpy(dest, from->rep->rrsets, from->rep->an_numrrsets
2938 dest += from->rep->an_numrrsets;
2940 memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets,
2941 to->rep->ns_numrrsets * sizeof(dest[0]));
2942 dest += to->rep->ns_numrrsets;
2943 memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets,
2944 from->rep->ns_numrrsets * sizeof(dest[0]));
2945 dest += from->rep->ns_numrrsets;
2947 memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets+
2948 to->rep->ns_numrrsets,
2949 to->rep->ar_numrrsets * sizeof(dest[0]));
2950 dest += to->rep->ar_numrrsets;
2951 memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets+
2952 from->rep->ns_numrrsets,
2953 from->rep->ar_numrrsets * sizeof(dest[0]));
2955 to->rep->rrsets = d;
2956 to->rep->an_numrrsets += from->rep->an_numrrsets;
2957 to->rep->ns_numrrsets += from->rep->ns_numrrsets;
2958 to->rep->ar_numrrsets += from->rep->ar_numrrsets;
2959 to->rep->rrset_count = n;
2961 if(from->rep->security < to->rep->security) /* lowest sec */
2962 to->rep->security = from->rep->security;
2963 if(from->rep->qdcount != 0) /* insert qd if appropriate */
2964 to->rep->qdcount = from->rep->qdcount;
2965 if(from->rep->ttl < to->rep->ttl) /* use smallest TTL */
2966 to->rep->ttl = from->rep->ttl;
2967 if(from->rep->prefetch_ttl < to->rep->prefetch_ttl)
2968 to->rep->prefetch_ttl = from->rep->prefetch_ttl;
2971 foriq->num_current_queries --;
2972 if(foriq->num_current_queries == 0)
2973 foriq->state = FINISHED_STATE;
2977 * Collect class ANY responses and make them into one response. This
2978 * state is started and it creates queries for all classes (that have
2979 * root hints). The answers are then collected.
2981 * @param qstate: query state.
2982 * @param id: module id.
2983 * @return true if the event needs more immediate processing, false if not.
2986 processCollectClass(struct module_qstate* qstate, int id)
2988 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2989 struct module_qstate* subq;
2990 /* If qchase.qclass == 0 then send out queries for all classes.
2991 * Otherwise, do nothing (wait for all answers to arrive and the
2992 * processClassResponse to put them together, and that moves us
2993 * towards the Finished state when done. */
2994 if(iq->qchase.qclass == 0) {
2996 iq->qchase.qclass = LDNS_RR_CLASS_ANY;
2997 while(iter_get_next_root(qstate->env->hints,
2998 qstate->env->fwds, &c)) {
2999 /* generate query for this class */
3000 log_nametypeclass(VERB_ALGO, "spawn collect query",
3001 qstate->qinfo.qname, qstate->qinfo.qtype, c);
3002 if(!generate_sub_request(qstate->qinfo.qname,
3003 qstate->qinfo.qname_len, qstate->qinfo.qtype,
3004 c, qstate, id, iq, INIT_REQUEST_STATE,
3005 FINISHED_STATE, &subq,
3006 (int)!(qstate->query_flags&BIT_CD))) {
3007 return error_response(qstate, id,
3008 LDNS_RCODE_SERVFAIL);
3010 /* ignore subq, no special init required */
3011 iq->num_current_queries ++;
3016 /* if no roots are configured at all, return */
3017 if(iq->num_current_queries == 0) {
3018 verbose(VERB_ALGO, "No root hints or fwds, giving up "
3020 return error_response(qstate, id, LDNS_RCODE_REFUSED);
3022 /* return false, wait for queries to return */
3024 /* if woke up here because of an answer, wait for more answers */
3029 * This handles the final state for first-tier responses (i.e., responses to
3030 * externally generated queries).
3032 * @param qstate: query state.
3033 * @param iq: iterator query state.
3034 * @param id: module id.
3035 * @return true if the event needs more processing, false if not. Since this
3036 * is the final state for an event, it always returns false.
3039 processFinished(struct module_qstate* qstate, struct iter_qstate* iq,
3042 log_query_info(VERB_QUERY, "finishing processing for",
3045 /* store negative cache element for parent side glue. */
3046 if(!qstate->no_cache_store && iq->query_for_pside_glue
3048 iter_store_parentside_neg(qstate->env, &qstate->qinfo,
3049 iq->deleg_msg?iq->deleg_msg->rep:
3050 (iq->response?iq->response->rep:NULL));
3052 verbose(VERB_ALGO, "No response is set, servfail");
3053 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3056 /* Make sure that the RA flag is set (since the presence of
3057 * this module means that recursion is available) */
3058 iq->response->rep->flags |= BIT_RA;
3060 /* Clear the AA flag */
3061 /* FIXME: does this action go here or in some other module? */
3062 iq->response->rep->flags &= ~BIT_AA;
3064 /* make sure QR flag is on */
3065 iq->response->rep->flags |= BIT_QR;
3067 /* we have finished processing this query */
3068 qstate->ext_state[id] = module_finished;
3070 /* TODO: we are using a private TTL, trim the response. */
3071 /* if (mPrivateTTL > 0){IterUtils.setPrivateTTL(resp, mPrivateTTL); } */
3073 /* prepend any items we have accumulated */
3074 if(iq->an_prepend_list || iq->ns_prepend_list) {
3075 if(!iter_prepend(iq, iq->response, qstate->region)) {
3076 log_err("prepend rrsets: out of memory");
3077 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3079 /* reset the query name back */
3080 iq->response->qinfo = qstate->qinfo;
3081 /* the security state depends on the combination */
3082 iq->response->rep->security = sec_status_unchecked;
3083 /* store message with the finished prepended items,
3084 * but only if we did recursion. The nonrecursion referral
3085 * from cache does not need to be stored in the msg cache. */
3086 if(!qstate->no_cache_store && qstate->query_flags&BIT_RD) {
3087 iter_dns_store(qstate->env, &qstate->qinfo,
3088 iq->response->rep, 0, qstate->prefetch_leeway,
3089 iq->dp&&iq->dp->has_parent_side_NS,
3090 qstate->region, qstate->query_flags);
3093 qstate->return_rcode = LDNS_RCODE_NOERROR;
3094 qstate->return_msg = iq->response;
3099 * Return priming query results to interested super querystates.
3101 * Sets the delegation point and delegation message (not nonRD queries).
3102 * This is a callback from walk_supers.
3104 * @param qstate: query state that finished.
3105 * @param id: module id.
3106 * @param super: the qstate to inform.
3109 iter_inform_super(struct module_qstate* qstate, int id,
3110 struct module_qstate* super)
3112 if(!qstate->is_priming && super->qinfo.qclass == LDNS_RR_CLASS_ANY)
3113 processClassResponse(qstate, id, super);
3114 else if(super->qinfo.qtype == LDNS_RR_TYPE_DS && ((struct iter_qstate*)
3115 super->minfo[id])->state == DSNS_FIND_STATE)
3116 processDSNSResponse(qstate, id, super);
3117 else if(qstate->return_rcode != LDNS_RCODE_NOERROR)
3118 error_supers(qstate, id, super);
3119 else if(qstate->is_priming)
3120 prime_supers(qstate, id, super);
3121 else processTargetResponse(qstate, id, super);
3125 * Handle iterator state.
3126 * Handle events. This is the real processing loop for events, responsible
3127 * for moving events through the various states. If a processing method
3128 * returns true, then it will be advanced to the next state. If false, then
3129 * processing will stop.
3131 * @param qstate: query state.
3132 * @param ie: iterator shared global environment.
3133 * @param iq: iterator query state.
3134 * @param id: module id.
3137 iter_handle(struct module_qstate* qstate, struct iter_qstate* iq,
3138 struct iter_env* ie, int id)
3142 verbose(VERB_ALGO, "iter_handle processing q with state %s",
3143 iter_state_to_string(iq->state));
3145 case INIT_REQUEST_STATE:
3146 cont = processInitRequest(qstate, iq, ie, id);
3148 case INIT_REQUEST_2_STATE:
3149 cont = processInitRequest2(qstate, iq, id);
3151 case INIT_REQUEST_3_STATE:
3152 cont = processInitRequest3(qstate, iq, id);
3154 case QUERYTARGETS_STATE:
3155 cont = processQueryTargets(qstate, iq, ie, id);
3157 case QUERY_RESP_STATE:
3158 cont = processQueryResponse(qstate, iq, id);
3160 case PRIME_RESP_STATE:
3161 cont = processPrimeResponse(qstate, id);
3163 case COLLECT_CLASS_STATE:
3164 cont = processCollectClass(qstate, id);
3166 case DSNS_FIND_STATE:
3167 cont = processDSNSFind(qstate, iq, id);
3169 case FINISHED_STATE:
3170 cont = processFinished(qstate, iq, id);
3173 log_warn("iterator: invalid state: %d",
3182 * This is the primary entry point for processing request events. Note that
3183 * this method should only be used by external modules.
3184 * @param qstate: query state.
3185 * @param ie: iterator shared global environment.
3186 * @param iq: iterator query state.
3187 * @param id: module id.
3190 process_request(struct module_qstate* qstate, struct iter_qstate* iq,
3191 struct iter_env* ie, int id)
3193 /* external requests start in the INIT state, and finish using the
3194 * FINISHED state. */
3195 iq->state = INIT_REQUEST_STATE;
3196 iq->final_state = FINISHED_STATE;
3197 verbose(VERB_ALGO, "process_request: new external request event");
3198 iter_handle(qstate, iq, ie, id);
3201 /** process authoritative server reply */
3203 process_response(struct module_qstate* qstate, struct iter_qstate* iq,
3204 struct iter_env* ie, int id, struct outbound_entry* outbound,
3205 enum module_ev event)
3207 struct msg_parse* prs;
3208 struct edns_data edns;
3211 verbose(VERB_ALGO, "process_response: new external response event");
3212 iq->response = NULL;
3213 iq->state = QUERY_RESP_STATE;
3214 if(event == module_event_noreply || event == module_event_error) {
3215 if(event == module_event_noreply && iq->sent_count >= 3 &&
3216 qstate->env->cfg->use_caps_bits_for_id &&
3217 !iq->caps_fallback) {
3218 /* start fallback */
3219 iq->caps_fallback = 1;
3220 iq->caps_server = 0;
3221 iq->caps_reply = NULL;
3222 iq->caps_response = NULL;
3223 iq->state = QUERYTARGETS_STATE;
3224 iq->num_current_queries--;
3225 /* need fresh attempts for the 0x20 fallback, if
3226 * that was the cause for the failure */
3227 iter_dec_attempts(iq->dp, 3);
3228 verbose(VERB_DETAIL, "Capsforid: timeouts, starting fallback");
3233 if( (event != module_event_reply && event != module_event_capsfail)
3234 || !qstate->reply) {
3235 log_err("Bad event combined with response");
3236 outbound_list_remove(&iq->outlist, outbound);
3237 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3242 prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
3243 sizeof(struct msg_parse));
3245 log_err("out of memory on incoming message");
3246 /* like packet got dropped */
3249 memset(prs, 0, sizeof(*prs));
3250 memset(&edns, 0, sizeof(edns));
3251 pkt = qstate->reply->c->buffer;
3252 sldns_buffer_set_position(pkt, 0);
3253 if(parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
3254 verbose(VERB_ALGO, "parse error on reply packet");
3257 /* edns is not examined, but removed from message to help cache */
3258 if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
3262 /* Copy the edns options we may got from the back end */
3264 qstate->edns_opts_back_in = edns_opt_copy_region(edns.opt_list,
3266 if(!qstate->edns_opts_back_in) {
3267 log_err("out of memory on incoming message");
3268 /* like packet got dropped */
3271 if(!inplace_cb_edns_back_parsed_call(qstate->env, qstate)) {
3272 log_err("unable to call edns_back_parsed callback");
3277 /* remove CD-bit, we asked for in case we handle validation ourself */
3278 prs->flags &= ~BIT_CD;
3280 /* normalize and sanitize: easy to delete items from linked lists */
3281 if(!scrub_message(pkt, prs, &iq->qinfo_out, iq->dp->name,
3282 qstate->env->scratch, qstate->env, ie)) {
3283 /* if 0x20 enabled, start fallback, but we have no message */
3284 if(event == module_event_capsfail && !iq->caps_fallback) {
3285 iq->caps_fallback = 1;
3286 iq->caps_server = 0;
3287 iq->caps_reply = NULL;
3288 iq->caps_response = NULL;
3289 iq->state = QUERYTARGETS_STATE;
3290 iq->num_current_queries--;
3291 verbose(VERB_DETAIL, "Capsforid: scrub failed, starting fallback with no response");
3296 /* allocate response dns_msg in region */
3297 iq->response = dns_alloc_msg(pkt, prs, qstate->region);
3300 log_query_info(VERB_DETAIL, "response for", &qstate->qinfo);
3301 log_name_addr(VERB_DETAIL, "reply from", iq->dp->name,
3302 &qstate->reply->addr, qstate->reply->addrlen);
3303 if(verbosity >= VERB_ALGO)
3304 log_dns_msg("incoming scrubbed packet:", &iq->response->qinfo,
3307 if(event == module_event_capsfail || iq->caps_fallback) {
3308 /* for fallback we care about main answer, not additionals */
3309 /* removing that makes comparison more likely to succeed */
3310 caps_strip_reply(iq->response->rep);
3311 if(!iq->caps_fallback) {
3312 /* start fallback */
3313 iq->caps_fallback = 1;
3314 iq->caps_server = 0;
3315 iq->caps_reply = iq->response->rep;
3316 iq->caps_response = iq->response;
3317 iq->state = QUERYTARGETS_STATE;
3318 iq->num_current_queries--;
3319 verbose(VERB_DETAIL, "Capsforid: starting fallback");
3322 /* check if reply is the same, otherwise, fail */
3323 if(!iq->caps_reply) {
3324 iq->caps_reply = iq->response->rep;
3325 iq->caps_response = iq->response;
3326 iq->caps_server = -1; /*become zero at ++,
3327 so that we start the full set of trials */
3328 } else if(caps_failed_rcode(iq->caps_reply) &&
3329 !caps_failed_rcode(iq->response->rep)) {
3330 /* prefer to upgrade to non-SERVFAIL */
3331 iq->caps_reply = iq->response->rep;
3332 iq->caps_response = iq->response;
3333 } else if(!caps_failed_rcode(iq->caps_reply) &&
3334 caps_failed_rcode(iq->response->rep)) {
3335 /* if we have non-SERVFAIL as answer then
3336 * we can ignore SERVFAILs for the equality
3338 /* no instructions here, skip other else */
3339 } else if(caps_failed_rcode(iq->caps_reply) &&
3340 caps_failed_rcode(iq->response->rep)) {
3341 /* failure is same as other failure in fallbk*/
3342 /* no instructions here, skip other else */
3343 } else if(!reply_equal(iq->response->rep, iq->caps_reply,
3344 qstate->env->scratch)) {
3345 verbose(VERB_DETAIL, "Capsforid fallback: "
3346 "getting different replies, failed");
3347 outbound_list_remove(&iq->outlist, outbound);
3348 (void)error_response(qstate, id,
3349 LDNS_RCODE_SERVFAIL);
3352 /* continue the fallback procedure at next server */
3354 iq->state = QUERYTARGETS_STATE;
3355 iq->num_current_queries--;
3356 verbose(VERB_DETAIL, "Capsforid: reply is equal. "
3357 "go to next fallback");
3361 iq->caps_fallback = 0; /* if we were in fallback, 0x20 is OK now */
3364 outbound_list_remove(&iq->outlist, outbound);
3365 iter_handle(qstate, iq, ie, id);
3369 iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
3370 struct outbound_entry* outbound)
3372 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
3373 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3374 verbose(VERB_QUERY, "iterator[module %d] operate: extstate:%s event:%s",
3375 id, strextstate(qstate->ext_state[id]), strmodulevent(event));
3376 if(iq) log_query_info(VERB_QUERY, "iterator operate: query",
3378 if(iq && qstate->qinfo.qname != iq->qchase.qname)
3379 log_query_info(VERB_QUERY, "iterator operate: chased to",
3382 /* perform iterator state machine */
3383 if((event == module_event_new || event == module_event_pass) &&
3385 if(!iter_new(qstate, id)) {
3386 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3389 iq = (struct iter_qstate*)qstate->minfo[id];
3390 process_request(qstate, iq, ie, id);
3393 if(iq && event == module_event_pass) {
3394 iter_handle(qstate, iq, ie, id);
3397 if(iq && outbound) {
3398 process_response(qstate, iq, ie, id, outbound, event);
3401 if(event == module_event_error) {
3402 verbose(VERB_ALGO, "got called with event error, giving up");
3403 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3407 log_err("bad event for iterator");
3408 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3412 iter_clear(struct module_qstate* qstate, int id)
3414 struct iter_qstate* iq;
3417 iq = (struct iter_qstate*)qstate->minfo[id];
3419 outbound_list_clear(&iq->outlist);
3420 if(iq->target_count && --iq->target_count[0] == 0)
3421 free(iq->target_count);
3422 iq->num_current_queries = 0;
3424 qstate->minfo[id] = NULL;
3428 iter_get_mem(struct module_env* env, int id)
3430 struct iter_env* ie = (struct iter_env*)env->modinfo[id];
3433 return sizeof(*ie) + sizeof(int)*((size_t)ie->max_dependency_depth+1)
3434 + donotq_get_mem(ie->donotq) + priv_get_mem(ie->priv);
3438 * The iterator function block
3440 static struct module_func_block iter_block = {
3442 &iter_init, &iter_deinit, &iter_operate, &iter_inform_super,
3443 &iter_clear, &iter_get_mem
3446 struct module_func_block*
3447 iter_get_funcblock(void)
3453 iter_state_to_string(enum iter_state state)
3457 case INIT_REQUEST_STATE :
3458 return "INIT REQUEST STATE";
3459 case INIT_REQUEST_2_STATE :
3460 return "INIT REQUEST STATE (stage 2)";
3461 case INIT_REQUEST_3_STATE:
3462 return "INIT REQUEST STATE (stage 3)";
3463 case QUERYTARGETS_STATE :
3464 return "QUERY TARGETS STATE";
3465 case PRIME_RESP_STATE :
3466 return "PRIME RESPONSE STATE";
3467 case COLLECT_CLASS_STATE :
3468 return "COLLECT CLASS STATE";
3469 case DSNS_FIND_STATE :
3470 return "DSNS FIND STATE";
3471 case QUERY_RESP_STATE :
3472 return "QUERY RESPONSE STATE";
3473 case FINISHED_STATE :
3474 return "FINISHED RESPONSE STATE";
3476 return "UNKNOWN ITER STATE";
3481 iter_state_is_responsestate(enum iter_state s)
3484 case INIT_REQUEST_STATE :
3485 case INIT_REQUEST_2_STATE :
3486 case INIT_REQUEST_3_STATE :
3487 case QUERYTARGETS_STATE :
3488 case COLLECT_CLASS_STATE :