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 recusive 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/parseutil.h"
68 #include "sldns/sbuffer.h"
71 iter_init(struct module_env* env, int id)
73 struct iter_env* iter_env = (struct iter_env*)calloc(1,
74 sizeof(struct iter_env));
76 log_err("malloc failure");
79 env->modinfo[id] = (void*)iter_env;
80 if(!iter_apply_cfg(iter_env, env->cfg)) {
81 log_err("iterator: could not apply configuration settings.");
87 /** delete caps_whitelist element */
89 caps_free(struct rbnode_t* n, void* ATTR_UNUSED(d))
92 free(((struct name_tree_node*)n)->name);
98 iter_deinit(struct module_env* env, int id)
100 struct iter_env* iter_env;
101 if(!env || !env->modinfo[id])
103 iter_env = (struct iter_env*)env->modinfo[id];
104 free(iter_env->target_fetch_policy);
105 priv_delete(iter_env->priv);
106 donotq_delete(iter_env->donotq);
107 if(iter_env->caps_white) {
108 traverse_postorder(iter_env->caps_white, caps_free, NULL);
109 free(iter_env->caps_white);
112 env->modinfo[id] = NULL;
115 /** new query for iterator */
117 iter_new(struct module_qstate* qstate, int id)
119 struct iter_qstate* iq = (struct iter_qstate*)regional_alloc(
120 qstate->region, sizeof(struct iter_qstate));
121 qstate->minfo[id] = iq;
124 memset(iq, 0, sizeof(*iq));
125 iq->state = INIT_REQUEST_STATE;
126 iq->final_state = FINISHED_STATE;
127 iq->an_prepend_list = NULL;
128 iq->an_prepend_last = NULL;
129 iq->ns_prepend_list = NULL;
130 iq->ns_prepend_last = NULL;
133 iq->num_target_queries = 0;
134 iq->num_current_queries = 0;
135 iq->query_restart_count = 0;
136 iq->referral_count = 0;
138 iq->ratelimit_ok = 0;
139 iq->target_count = NULL;
140 iq->wait_priming_stub = 0;
141 iq->refetch_glue = 0;
142 iq->dnssec_expected = 0;
143 iq->dnssec_lame_query = 0;
144 iq->chase_flags = qstate->query_flags;
145 /* Start with the (current) qname. */
146 iq->qchase = qstate->qinfo;
147 outbound_list_init(&iq->outlist);
152 * Transition to the next state. This can be used to advance a currently
153 * processing event. It cannot be used to reactivate a forEvent.
155 * @param iq: iterator query state
156 * @param nextstate The state to transition to.
157 * @return true. This is so this can be called as the return value for the
158 * actual process*State() methods. (Transitioning to the next state
159 * implies further processing).
162 next_state(struct iter_qstate* iq, enum iter_state nextstate)
164 /* If transitioning to a "response" state, make sure that there is a
166 if(iter_state_is_responsestate(nextstate)) {
167 if(iq->response == NULL) {
168 log_err("transitioning to response state sans "
172 iq->state = nextstate;
177 * Transition an event to its final state. Final states always either return
178 * a result up the module chain, or reactivate a dependent event. Which
179 * final state to transtion to is set in the module state for the event when
180 * it was created, and depends on the original purpose of the event.
182 * The response is stored in the qstate->buf buffer.
184 * @param iq: iterator query state
185 * @return false. This is so this method can be used as the return value for
186 * the processState methods. (Transitioning to the final state
189 final_state(struct iter_qstate* iq)
191 return next_state(iq, iq->final_state);
195 * Callback routine to handle errors in parent query states
196 * @param qstate: query state that failed.
197 * @param id: module id.
198 * @param super: super state.
201 error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
203 struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
205 if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
206 qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
207 /* mark address as failed. */
208 struct delegpt_ns* dpns = NULL;
210 dpns = delegpt_find_ns(super_iq->dp,
211 qstate->qinfo.qname, qstate->qinfo.qname_len);
214 verbose(VERB_ALGO, "subq error, but not interested");
215 log_query_info(VERB_ALGO, "superq", &super->qinfo);
217 delegpt_log(VERB_ALGO, super_iq->dp);
221 /* see if the failure did get (parent-lame) info */
222 if(!cache_fill_missing(super->env,
223 super_iq->qchase.qclass, super->region,
225 log_err("out of memory adding missing");
227 dpns->resolved = 1; /* mark as failed */
228 super_iq->num_target_queries--;
230 if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
231 /* prime failed to get delegation */
234 /* evaluate targets again */
235 super_iq->state = QUERYTARGETS_STATE;
236 /* super becomes runnable, and will process this change */
240 * Return an error to the client
241 * @param qstate: our query state
242 * @param id: module id
243 * @param rcode: error code (DNS errcode).
244 * @return: 0 for use by caller, to make notation easy, like:
245 * return error_response(..).
248 error_response(struct module_qstate* qstate, int id, int rcode)
250 verbose(VERB_QUERY, "return error response %s",
251 sldns_lookup_by_id(sldns_rcodes, rcode)?
252 sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
253 qstate->return_rcode = rcode;
254 qstate->return_msg = NULL;
255 qstate->ext_state[id] = module_finished;
260 * Return an error to the client and cache the error code in the
261 * message cache (so per qname, qtype, qclass).
262 * @param qstate: our query state
263 * @param id: module id
264 * @param rcode: error code (DNS errcode).
265 * @return: 0 for use by caller, to make notation easy, like:
266 * return error_response(..).
269 error_response_cache(struct module_qstate* qstate, int id, int rcode)
272 struct reply_info err;
273 if(qstate->prefetch_leeway > NORR_TTL) {
274 verbose(VERB_ALGO, "error response for prefetch in cache");
275 /* attempt to adjust the cache entry prefetch */
276 if(dns_cache_prefetch_adjust(qstate->env, &qstate->qinfo,
277 NORR_TTL, qstate->query_flags))
278 return error_response(qstate, id, rcode);
279 /* if that fails (not in cache), fall through to store err */
281 memset(&err, 0, sizeof(err));
282 err.flags = (uint16_t)(BIT_QR | BIT_RA);
283 FLAGS_SET_RCODE(err.flags, rcode);
286 err.prefetch_ttl = PREFETCH_TTL_CALC(err.ttl);
287 /* do not waste time trying to validate this servfail */
288 err.security = sec_status_indeterminate;
289 verbose(VERB_ALGO, "store error response in message cache");
290 iter_dns_store(qstate->env, &qstate->qinfo, &err, 0, 0, 0, NULL,
291 qstate->query_flags);
292 return error_response(qstate, id, rcode);
295 /** check if prepend item is duplicate item */
297 prepend_is_duplicate(struct ub_packed_rrset_key** sets, size_t to,
298 struct ub_packed_rrset_key* dup)
301 for(i=0; i<to; i++) {
302 if(sets[i]->rk.type == dup->rk.type &&
303 sets[i]->rk.rrset_class == dup->rk.rrset_class &&
304 sets[i]->rk.dname_len == dup->rk.dname_len &&
305 query_dname_compare(sets[i]->rk.dname, dup->rk.dname)
312 /** prepend the prepend list in the answer and authority section of dns_msg */
314 iter_prepend(struct iter_qstate* iq, struct dns_msg* msg,
315 struct regional* region)
317 struct iter_prep_list* p;
318 struct ub_packed_rrset_key** sets;
319 size_t num_an = 0, num_ns = 0;;
320 for(p = iq->an_prepend_list; p; p = p->next)
322 for(p = iq->ns_prepend_list; p; p = p->next)
324 if(num_an + num_ns == 0)
326 verbose(VERB_ALGO, "prepending %d rrsets", (int)num_an + (int)num_ns);
327 if(num_an > RR_COUNT_MAX || num_ns > RR_COUNT_MAX ||
328 msg->rep->rrset_count > RR_COUNT_MAX) return 0; /* overflow */
329 sets = regional_alloc(region, (num_an+num_ns+msg->rep->rrset_count) *
330 sizeof(struct ub_packed_rrset_key*));
335 for(p = iq->an_prepend_list; p; p = p->next) {
336 sets[num_an++] = p->rrset;
338 memcpy(sets+num_an, msg->rep->rrsets, msg->rep->an_numrrsets *
339 sizeof(struct ub_packed_rrset_key*));
342 for(p = iq->ns_prepend_list; p; p = p->next) {
343 if(prepend_is_duplicate(sets+msg->rep->an_numrrsets+num_an,
344 num_ns, p->rrset) || prepend_is_duplicate(
345 msg->rep->rrsets+msg->rep->an_numrrsets,
346 msg->rep->ns_numrrsets, p->rrset))
348 sets[msg->rep->an_numrrsets + num_an + num_ns++] = p->rrset;
350 memcpy(sets + num_an + msg->rep->an_numrrsets + num_ns,
351 msg->rep->rrsets + msg->rep->an_numrrsets,
352 (msg->rep->ns_numrrsets + msg->rep->ar_numrrsets) *
353 sizeof(struct ub_packed_rrset_key*));
355 /* NXDOMAIN rcode can stay if we prepended DNAME/CNAMEs, because
356 * this is what recursors should give. */
357 msg->rep->rrset_count += num_an + num_ns;
358 msg->rep->an_numrrsets += num_an;
359 msg->rep->ns_numrrsets += num_ns;
360 msg->rep->rrsets = sets;
365 * Add rrset to ANSWER prepend list
366 * @param qstate: query state.
367 * @param iq: iterator query state.
368 * @param rrset: rrset to add.
369 * @return false on failure (malloc).
372 iter_add_prepend_answer(struct module_qstate* qstate, struct iter_qstate* iq,
373 struct ub_packed_rrset_key* rrset)
375 struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
376 qstate->region, sizeof(struct iter_prep_list));
382 if(iq->an_prepend_last)
383 iq->an_prepend_last->next = p;
384 else iq->an_prepend_list = p;
385 iq->an_prepend_last = p;
390 * Add rrset to AUTHORITY prepend list
391 * @param qstate: query state.
392 * @param iq: iterator query state.
393 * @param rrset: rrset to add.
394 * @return false on failure (malloc).
397 iter_add_prepend_auth(struct module_qstate* qstate, struct iter_qstate* iq,
398 struct ub_packed_rrset_key* rrset)
400 struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
401 qstate->region, sizeof(struct iter_prep_list));
407 if(iq->ns_prepend_last)
408 iq->ns_prepend_last->next = p;
409 else iq->ns_prepend_list = p;
410 iq->ns_prepend_last = p;
415 * Given a CNAME response (defined as a response containing a CNAME or DNAME
416 * that does not answer the request), process the response, modifying the
417 * state as necessary. This follows the CNAME/DNAME chain and returns the
420 * sets the new query name, after following the CNAME/DNAME chain.
421 * @param qstate: query state.
422 * @param iq: iterator query state.
423 * @param msg: the response.
424 * @param mname: returned target new query name.
425 * @param mname_len: length of mname.
426 * @return false on (malloc) error.
429 handle_cname_response(struct module_qstate* qstate, struct iter_qstate* iq,
430 struct dns_msg* msg, uint8_t** mname, size_t* mname_len)
433 /* Start with the (current) qname. */
434 *mname = iq->qchase.qname;
435 *mname_len = iq->qchase.qname_len;
437 /* Iterate over the ANSWER rrsets in order, looking for CNAMEs and
439 for(i=0; i<msg->rep->an_numrrsets; i++) {
440 struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
441 /* If there is a (relevant) DNAME, add it to the list.
442 * We always expect there to be CNAME that was generated
443 * by this DNAME following, so we don't process the DNAME
445 if(ntohs(r->rk.type) == LDNS_RR_TYPE_DNAME &&
446 dname_strict_subdomain_c(*mname, r->rk.dname)) {
447 if(!iter_add_prepend_answer(qstate, iq, r))
452 if(ntohs(r->rk.type) == LDNS_RR_TYPE_CNAME &&
453 query_dname_compare(*mname, r->rk.dname) == 0) {
454 /* Add this relevant CNAME rrset to the prepend list.*/
455 if(!iter_add_prepend_answer(qstate, iq, r))
457 get_cname_target(r, mname, mname_len);
460 /* Other rrsets in the section are ignored. */
462 /* add authority rrsets to authority prepend, for wildcarded CNAMEs */
463 for(i=msg->rep->an_numrrsets; i<msg->rep->an_numrrsets +
464 msg->rep->ns_numrrsets; i++) {
465 struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
466 /* only add NSEC/NSEC3, as they may be needed for validation */
467 if(ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC ||
468 ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC3) {
469 if(!iter_add_prepend_auth(qstate, iq, r))
476 /** see if target name is caps-for-id whitelisted */
478 is_caps_whitelisted(struct iter_env* ie, struct iter_qstate* iq)
480 if(!ie->caps_white) return 0; /* no whitelist, or no capsforid */
481 return name_tree_lookup(ie->caps_white, iq->qchase.qname,
482 iq->qchase.qname_len, dname_count_labels(iq->qchase.qname),
483 iq->qchase.qclass) != NULL;
486 /** create target count structure for this query */
488 target_count_create(struct iter_qstate* iq)
490 if(!iq->target_count) {
491 iq->target_count = (int*)calloc(2, sizeof(int));
492 /* if calloc fails we simply do not track this number */
494 iq->target_count[0] = 1;
499 target_count_increase(struct iter_qstate* iq, int num)
501 target_count_create(iq);
503 iq->target_count[1] += num;
507 * Generate a subrequest.
508 * Generate a local request event. Local events are tied to this module, and
509 * have a correponding (first tier) event that is waiting for this event to
510 * resolve to continue.
512 * @param qname The query name for this request.
513 * @param qnamelen length of qname
514 * @param qtype The query type for this request.
515 * @param qclass The query class for this request.
516 * @param qstate The event that is generating this event.
517 * @param id: module id.
518 * @param iq: The iterator state that is generating this event.
519 * @param initial_state The initial response state (normally this
520 * is QUERY_RESP_STATE, unless it is known that the request won't
521 * need iterative processing
522 * @param finalstate The final state for the response to this request.
523 * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
524 * not need initialisation.
525 * @param v: if true, validation is done on the subquery.
526 * @return false on error (malloc).
529 generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
530 uint16_t qclass, struct module_qstate* qstate, int id,
531 struct iter_qstate* iq, enum iter_state initial_state,
532 enum iter_state finalstate, struct module_qstate** subq_ret, int v)
534 struct module_qstate* subq = NULL;
535 struct iter_qstate* subiq = NULL;
536 uint16_t qflags = 0; /* OPCODE QUERY, no flags */
537 struct query_info qinf;
538 int prime = (finalstate == PRIME_RESP_STATE)?1:0;
541 qinf.qname_len = qnamelen;
543 qinf.qclass = qclass;
545 /* RD should be set only when sending the query back through the INIT
547 if(initial_state == INIT_REQUEST_STATE)
549 /* We set the CD flag so we can send this through the "head" of
550 * the resolution chain, which might have a validator. We are
551 * uninterested in validating things not on the direct resolution
558 /* attach subquery, lookup existing or make a new one */
559 fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
560 if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime, valrec,
566 /* initialise the new subquery */
568 subq->ext_state[id] = module_state_initial;
569 subq->minfo[id] = regional_alloc(subq->region,
570 sizeof(struct iter_qstate));
571 if(!subq->minfo[id]) {
572 log_err("init subq: out of memory");
573 fptr_ok(fptr_whitelist_modenv_kill_sub(
574 qstate->env->kill_sub));
575 (*qstate->env->kill_sub)(subq);
578 subiq = (struct iter_qstate*)subq->minfo[id];
579 memset(subiq, 0, sizeof(*subiq));
580 subiq->num_target_queries = 0;
581 target_count_create(iq);
582 subiq->target_count = iq->target_count;
584 iq->target_count[0] ++; /* extra reference */
585 subiq->num_current_queries = 0;
586 subiq->depth = iq->depth+1;
587 outbound_list_init(&subiq->outlist);
588 subiq->state = initial_state;
589 subiq->final_state = finalstate;
590 subiq->qchase = subq->qinfo;
591 subiq->chase_flags = subq->query_flags;
592 subiq->refetch_glue = 0;
598 * Generate and send a root priming request.
599 * @param qstate: the qtstate that triggered the need to prime.
600 * @param iq: iterator query state.
601 * @param id: module id.
602 * @param qclass: the class to prime.
603 * @return 0 on failure
606 prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
610 struct module_qstate* subq;
611 verbose(VERB_DETAIL, "priming . %s NS",
612 sldns_lookup_by_id(sldns_rr_classes, (int)qclass)?
613 sldns_lookup_by_id(sldns_rr_classes, (int)qclass)->name:"??");
614 dp = hints_lookup_root(qstate->env->hints, qclass);
616 verbose(VERB_ALGO, "Cannot prime due to lack of hints");
619 /* Priming requests start at the QUERYTARGETS state, skipping
620 * the normal INIT state logic (which would cause an infloop). */
621 if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS,
622 qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
624 verbose(VERB_ALGO, "could not prime root");
628 struct iter_qstate* subiq =
629 (struct iter_qstate*)subq->minfo[id];
630 /* Set the initial delegation point to the hint.
631 * copy dp, it is now part of the root prime query.
632 * dp was part of in the fixed hints structure. */
633 subiq->dp = delegpt_copy(dp, subq->region);
635 log_err("out of memory priming root, copydp");
636 fptr_ok(fptr_whitelist_modenv_kill_sub(
637 qstate->env->kill_sub));
638 (*qstate->env->kill_sub)(subq);
641 /* there should not be any target queries. */
642 subiq->num_target_queries = 0;
643 subiq->dnssec_expected = iter_indicates_dnssec(
644 qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
647 /* this module stops, our submodule starts, and does the query. */
648 qstate->ext_state[id] = module_wait_subquery;
653 * Generate and process a stub priming request. This method tests for the
654 * need to prime a stub zone, so it is safe to call for every request.
656 * @param qstate: the qtstate that triggered the need to prime.
657 * @param iq: iterator query state.
658 * @param id: module id.
659 * @param qname: request name.
660 * @param qclass: request class.
661 * @return true if a priming subrequest was made, false if not. The will only
662 * issue a priming request if it detects an unprimed stub.
663 * Uses value of 2 to signal during stub-prime in root-prime situation
664 * that a noprime-stub is available and resolution can continue.
667 prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
668 uint8_t* qname, uint16_t qclass)
670 /* Lookup the stub hint. This will return null if the stub doesn't
671 * need to be re-primed. */
672 struct iter_hints_stub* stub;
673 struct delegpt* stub_dp;
674 struct module_qstate* subq;
677 stub = hints_lookup_stub(qstate->env->hints, qname, qclass, iq->dp);
678 /* The stub (if there is one) does not need priming. */
683 /* is it a noprime stub (always use) */
686 if(iq->dp == NULL) r = 2;
687 /* copy the dp out of the fixed hints structure, so that
688 * it can be changed when servicing this query */
689 iq->dp = delegpt_copy(stub_dp, qstate->region);
691 log_err("out of memory priming stub");
692 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
693 return 1; /* return 1 to make module stop, with error */
695 log_nametypeclass(VERB_DETAIL, "use stub", stub_dp->name,
696 LDNS_RR_TYPE_NS, qclass);
700 /* Otherwise, we need to (re)prime the stub. */
701 log_nametypeclass(VERB_DETAIL, "priming stub", stub_dp->name,
702 LDNS_RR_TYPE_NS, qclass);
704 /* Stub priming events start at the QUERYTARGETS state to avoid the
705 * redundant INIT state processing. */
706 if(!generate_sub_request(stub_dp->name, stub_dp->namelen,
707 LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
708 QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0)) {
709 verbose(VERB_ALGO, "could not prime stub");
710 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
711 return 1; /* return 1 to make module stop, with error */
714 struct iter_qstate* subiq =
715 (struct iter_qstate*)subq->minfo[id];
717 /* Set the initial delegation point to the hint. */
718 /* make copy to avoid use of stub dp by different qs/threads */
719 subiq->dp = delegpt_copy(stub_dp, subq->region);
721 log_err("out of memory priming stub, copydp");
722 fptr_ok(fptr_whitelist_modenv_kill_sub(
723 qstate->env->kill_sub));
724 (*qstate->env->kill_sub)(subq);
725 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
726 return 1; /* return 1 to make module stop, with error */
728 /* there should not be any target queries -- although there
729 * wouldn't be anyway, since stub hints never have
730 * missing targets. */
731 subiq->num_target_queries = 0;
732 subiq->wait_priming_stub = 1;
733 subiq->dnssec_expected = iter_indicates_dnssec(
734 qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
737 /* this module stops, our submodule starts, and does the query. */
738 qstate->ext_state[id] = module_wait_subquery;
743 * Generate A and AAAA checks for glue that is in-zone for the referral
744 * we just got to obtain authoritative information on the adresses.
746 * @param qstate: the qtstate that triggered the need to prime.
747 * @param iq: iterator query state.
748 * @param id: module id.
751 generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
754 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
755 struct module_qstate* subq;
757 struct reply_info* rep = iq->response->rep;
758 struct ub_packed_rrset_key* s;
761 if(iq->depth == ie->max_dependency_depth)
763 /* walk through additional, and check if in-zone,
764 * only relevant A, AAAA are left after scrub anyway */
765 for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
767 /* check *ALL* addresses that are transmitted in additional*/
768 /* is it an address ? */
769 if( !(ntohs(s->rk.type)==LDNS_RR_TYPE_A ||
770 ntohs(s->rk.type)==LDNS_RR_TYPE_AAAA)) {
773 /* is this query the same as the A/AAAA check for it */
774 if(qstate->qinfo.qtype == ntohs(s->rk.type) &&
775 qstate->qinfo.qclass == ntohs(s->rk.rrset_class) &&
776 query_dname_compare(qstate->qinfo.qname,
778 (qstate->query_flags&BIT_RD) &&
779 !(qstate->query_flags&BIT_CD))
782 /* generate subrequest for it */
783 log_nametypeclass(VERB_ALGO, "schedule addr fetch",
784 s->rk.dname, ntohs(s->rk.type),
785 ntohs(s->rk.rrset_class));
786 if(!generate_sub_request(s->rk.dname, s->rk.dname_len,
787 ntohs(s->rk.type), ntohs(s->rk.rrset_class),
789 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
790 verbose(VERB_ALGO, "could not generate addr check");
793 /* ignore subq - not need for more init */
798 * Generate a NS check request to obtain authoritative information
801 * @param qstate: the qtstate that triggered the need to prime.
802 * @param iq: iterator query state.
803 * @param id: module id.
806 generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
808 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
809 struct module_qstate* subq;
812 if(iq->depth == ie->max_dependency_depth)
814 /* is this query the same as the nscheck? */
815 if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS &&
816 query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
817 (qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
818 /* spawn off A, AAAA queries for in-zone glue to check */
819 generate_a_aaaa_check(qstate, iq, id);
823 log_nametypeclass(VERB_ALGO, "schedule ns fetch",
824 iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
825 if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
826 LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
827 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
828 verbose(VERB_ALGO, "could not generate ns check");
832 struct iter_qstate* subiq =
833 (struct iter_qstate*)subq->minfo[id];
835 /* make copy to avoid use of stub dp by different qs/threads */
836 /* refetch glue to start higher up the tree */
837 subiq->refetch_glue = 1;
838 subiq->dp = delegpt_copy(iq->dp, subq->region);
840 log_err("out of memory generating ns check, copydp");
841 fptr_ok(fptr_whitelist_modenv_kill_sub(
842 qstate->env->kill_sub));
843 (*qstate->env->kill_sub)(subq);
850 * Generate a DNSKEY prefetch query to get the DNSKEY for the DS record we
851 * just got in a referral (where we have dnssec_expected, thus have trust
852 * anchors above it). Note that right after calling this routine the
853 * iterator detached subqueries (because of following the referral), and thus
854 * the DNSKEY query becomes detached, its return stored in the cache for
855 * later lookup by the validator. This cache lookup by the validator avoids
856 * the roundtrip incurred by the DNSKEY query. The DNSKEY query is now
857 * performed at about the same time the original query is sent to the domain,
858 * thus the two answers are likely to be returned at about the same time,
859 * saving a roundtrip from the validated lookup.
861 * @param qstate: the qtstate that triggered the need to prime.
862 * @param iq: iterator query state.
863 * @param id: module id.
866 generate_dnskey_prefetch(struct module_qstate* qstate,
867 struct iter_qstate* iq, int id)
869 struct module_qstate* subq;
872 /* is this query the same as the prefetch? */
873 if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
874 query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
875 (qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
879 /* if the DNSKEY is in the cache this lookup will stop quickly */
880 log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch",
881 iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
882 if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
883 LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
884 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
885 /* we'll be slower, but it'll work */
886 verbose(VERB_ALGO, "could not generate dnskey prefetch");
890 struct iter_qstate* subiq =
891 (struct iter_qstate*)subq->minfo[id];
892 /* this qstate has the right delegation for the dnskey lookup*/
893 /* make copy to avoid use of stub dp by different qs/threads */
894 subiq->dp = delegpt_copy(iq->dp, subq->region);
895 /* if !subiq->dp, it'll start from the cache, no problem */
900 * See if the query needs forwarding.
902 * @param qstate: query state.
903 * @param iq: iterator query state.
904 * @return true if the request is forwarded, false if not.
905 * If returns true but, iq->dp is NULL then a malloc failure occurred.
908 forward_request(struct module_qstate* qstate, struct iter_qstate* iq)
911 uint8_t* delname = iq->qchase.qname;
912 size_t delnamelen = iq->qchase.qname_len;
913 if(iq->refetch_glue) {
914 delname = iq->dp->name;
915 delnamelen = iq->dp->namelen;
917 /* strip one label off of DS query to lookup higher for it */
918 if( (iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue)
919 && !dname_is_root(iq->qchase.qname))
920 dname_remove_label(&delname, &delnamelen);
921 dp = forwards_lookup(qstate->env->fwds, delname, iq->qchase.qclass);
924 /* send recursion desired to forward addr */
925 iq->chase_flags |= BIT_RD;
926 iq->dp = delegpt_copy(dp, qstate->region);
927 /* iq->dp checked by caller */
928 verbose(VERB_ALGO, "forwarding request");
933 * Process the initial part of the request handling. This state roughly
934 * corresponds to resolver algorithms steps 1 (find answer in cache) and 2
935 * (find the best servers to ask).
937 * Note that all requests start here, and query restarts revisit this state.
939 * This state either generates: 1) a response, from cache or error, 2) a
940 * priming event, or 3) forwards the request to the next state (init2,
943 * @param qstate: query state.
944 * @param iq: iterator query state.
945 * @param ie: iterator shared global environment.
946 * @param id: module id.
947 * @return true if the event needs more request processing immediately,
951 processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
952 struct iter_env* ie, int id)
958 log_query_info(VERB_DETAIL, "resolving", &qstate->qinfo);
961 /* We enforce a maximum number of query restarts. This is primarily a
962 * cheap way to prevent CNAME loops. */
963 if(iq->query_restart_count > MAX_RESTART_COUNT) {
964 verbose(VERB_QUERY, "request has exceeded the maximum number"
965 " of query restarts with %d", iq->query_restart_count);
966 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
969 /* We enforce a maximum recursion/dependency depth -- in general,
970 * this is unnecessary for dependency loops (although it will
971 * catch those), but it provides a sensible limit to the amount
972 * of work required to answer a given query. */
973 verbose(VERB_ALGO, "request has dependency depth of %d", iq->depth);
974 if(iq->depth > ie->max_dependency_depth) {
975 verbose(VERB_QUERY, "request has exceeded the maximum "
976 "dependency depth with depth of %d", iq->depth);
977 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
980 /* If the request is qclass=ANY, setup to generate each class */
981 if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
982 iq->qchase.qclass = 0;
983 return next_state(iq, COLLECT_CLASS_STATE);
986 /* Resolver Algorithm Step 1 -- Look for the answer in local data. */
988 /* This either results in a query restart (CNAME cache response), a
989 * terminating response (ANSWER), or a cache miss (null). */
991 if(qstate->blacklist) {
992 /* if cache, or anything else, was blacklisted then
993 * getting older results from cache is a bad idea, no cache */
994 verbose(VERB_ALGO, "cache blacklisted, going to the network");
997 msg = dns_cache_lookup(qstate->env, iq->qchase.qname,
998 iq->qchase.qname_len, iq->qchase.qtype,
999 iq->qchase.qclass, qstate->query_flags,
1000 qstate->region, qstate->env->scratch);
1001 if(!msg && qstate->env->neg_cache) {
1002 /* lookup in negative cache; may result in
1003 * NOERROR/NODATA or NXDOMAIN answers that need validation */
1004 msg = val_neg_getmsg(qstate->env->neg_cache, &iq->qchase,
1005 qstate->region, qstate->env->rrset_cache,
1006 qstate->env->scratch_buffer,
1007 *qstate->env->now, 1/*add SOA*/, NULL);
1009 /* item taken from cache does not match our query name, thus
1010 * security needs to be re-examined later */
1011 if(msg && query_dname_compare(qstate->qinfo.qname,
1012 iq->qchase.qname) != 0)
1013 msg->rep->security = sec_status_unchecked;
1016 /* handle positive cache response */
1017 enum response_type type = response_type_from_cache(msg,
1019 if(verbosity >= VERB_ALGO) {
1020 log_dns_msg("msg from cache lookup", &msg->qinfo,
1022 verbose(VERB_ALGO, "msg ttl is %d, prefetch ttl %d",
1024 (int)msg->rep->prefetch_ttl);
1027 if(type == RESPONSE_TYPE_CNAME) {
1030 verbose(VERB_ALGO, "returning CNAME response from "
1032 if(!handle_cname_response(qstate, iq, msg,
1034 return error_response(qstate, id,
1035 LDNS_RCODE_SERVFAIL);
1036 iq->qchase.qname = sname;
1037 iq->qchase.qname_len = slen;
1038 /* This *is* a query restart, even if it is a cheap
1041 iq->refetch_glue = 0;
1042 iq->query_restart_count++;
1044 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1045 return next_state(iq, INIT_REQUEST_STATE);
1048 /* if from cache, NULL, else insert 'cache IP' len=0 */
1049 if(qstate->reply_origin)
1050 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1051 /* it is an answer, response, to final state */
1052 verbose(VERB_ALGO, "returning answer from cache.");
1054 return final_state(iq);
1057 /* attempt to forward the request */
1058 if(forward_request(qstate, iq))
1061 log_err("alloc failure for forward dp");
1062 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1064 iq->refetch_glue = 0;
1065 /* the request has been forwarded.
1066 * forwarded requests need to be immediately sent to the
1067 * next state, QUERYTARGETS. */
1068 return next_state(iq, QUERYTARGETS_STATE);
1071 /* Resolver Algorithm Step 2 -- find the "best" servers. */
1073 /* first, adjust for DS queries. To avoid the grandparent problem,
1074 * we just look for the closest set of server to the parent of qname.
1075 * When re-fetching glue we also need to ask the parent.
1077 if(iq->refetch_glue) {
1079 log_err("internal or malloc fail: no dp for refetch");
1080 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1082 delname = iq->dp->name;
1083 delnamelen = iq->dp->namelen;
1085 delname = iq->qchase.qname;
1086 delnamelen = iq->qchase.qname_len;
1088 if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue ||
1089 (iq->qchase.qtype == LDNS_RR_TYPE_NS && qstate->prefetch_leeway)) {
1090 /* remove first label from delname, root goes to hints,
1091 * but only to fetch glue, not for qtype=DS. */
1092 /* also when prefetching an NS record, fetch it again from
1093 * its parent, just as if it expired, so that you do not
1094 * get stuck on an older nameserver that gives old NSrecords */
1095 if(dname_is_root(delname) && (iq->refetch_glue ||
1096 (iq->qchase.qtype == LDNS_RR_TYPE_NS &&
1097 qstate->prefetch_leeway)))
1098 delname = NULL; /* go to root priming */
1099 else dname_remove_label(&delname, &delnamelen);
1101 /* delname is the name to lookup a delegation for. If NULL rootprime */
1104 /* Lookup the delegation in the cache. If null, then the
1105 * cache needs to be primed for the qclass. */
1107 iq->dp = dns_cache_find_delegation(qstate->env, delname,
1108 delnamelen, iq->qchase.qtype, iq->qchase.qclass,
1109 qstate->region, &iq->deleg_msg,
1110 *qstate->env->now+qstate->prefetch_leeway);
1113 /* If the cache has returned nothing, then we have a
1114 * root priming situation. */
1115 if(iq->dp == NULL) {
1116 /* if there is a stub, then no root prime needed */
1117 int r = prime_stub(qstate, iq, id, delname,
1120 break; /* got noprime-stub-zone, continue */
1122 return 0; /* stub prime request made */
1123 if(forwards_lookup_root(qstate->env->fwds,
1124 iq->qchase.qclass)) {
1125 /* forward zone root, no root prime needed */
1126 /* fill in some dp - safety belt */
1127 iq->dp = hints_lookup_root(qstate->env->hints,
1130 log_err("internal error: no hints dp");
1131 return error_response(qstate, id,
1132 LDNS_RCODE_SERVFAIL);
1134 iq->dp = delegpt_copy(iq->dp, qstate->region);
1136 log_err("out of memory in safety belt");
1137 return error_response(qstate, id,
1138 LDNS_RCODE_SERVFAIL);
1140 return next_state(iq, INIT_REQUEST_2_STATE);
1142 /* Note that the result of this will set a new
1143 * DelegationPoint based on the result of priming. */
1144 if(!prime_root(qstate, iq, id, iq->qchase.qclass))
1145 return error_response(qstate, id,
1146 LDNS_RCODE_REFUSED);
1148 /* priming creates and sends a subordinate query, with
1149 * this query as the parent. So further processing for
1150 * this event will stop until reactivated by the
1151 * results of priming. */
1154 if(!iq->ratelimit_ok && qstate->prefetch_leeway)
1155 iq->ratelimit_ok = 1; /* allow prefetches, this keeps
1156 otherwise valid data in the cache */
1157 if(!iq->ratelimit_ok && infra_ratelimit_exceeded(
1158 qstate->env->infra_cache, iq->dp->name,
1159 iq->dp->namelen, *qstate->env->now)) {
1160 /* and increment the rate, so that the rate for time
1161 * now will also exceed the rate, keeping cache fresh */
1162 (void)infra_ratelimit_inc(qstate->env->infra_cache,
1163 iq->dp->name, iq->dp->namelen,
1165 /* see if we are passed through with slip factor */
1166 if(qstate->env->cfg->ratelimit_factor != 0 &&
1167 ub_random_max(qstate->env->rnd,
1168 qstate->env->cfg->ratelimit_factor) == 1) {
1169 iq->ratelimit_ok = 1;
1170 log_nametypeclass(VERB_ALGO, "ratelimit allowed through for "
1171 "delegation point", iq->dp->name,
1172 LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1174 log_nametypeclass(VERB_ALGO, "ratelimit exceeded with "
1175 "delegation point", iq->dp->name,
1176 LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1177 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1181 /* see if this dp not useless.
1183 * o all NS items are required glue.
1184 * or the query is for NS item that is required glue.
1185 * o no addresses are provided.
1187 * Instead, go up one level, and try to get even further
1188 * If the root was useless, use safety belt information.
1189 * Only check cache returns, because replies for servers
1190 * could be useless but lead to loops (bumping into the
1191 * same server reply) if useless-checked.
1193 if(iter_dp_is_useless(&qstate->qinfo, qstate->query_flags,
1195 if(dname_is_root(iq->dp->name)) {
1196 /* use safety belt */
1197 verbose(VERB_QUERY, "Cache has root NS but "
1198 "no addresses. Fallback to the safety belt.");
1199 iq->dp = hints_lookup_root(qstate->env->hints,
1201 /* note deleg_msg is from previous lookup,
1202 * but RD is on, so it is not used */
1204 log_err("internal error: no hints dp");
1205 return error_response(qstate, id,
1206 LDNS_RCODE_REFUSED);
1208 iq->dp = delegpt_copy(iq->dp, qstate->region);
1210 log_err("out of memory in safety belt");
1211 return error_response(qstate, id,
1212 LDNS_RCODE_SERVFAIL);
1217 "cache delegation was useless:");
1218 delegpt_log(VERB_ALGO, iq->dp);
1220 delname = iq->dp->name;
1221 delnamelen = iq->dp->namelen;
1222 dname_remove_label(&delname, &delnamelen);
1227 verbose(VERB_ALGO, "cache delegation returns delegpt");
1228 delegpt_log(VERB_ALGO, iq->dp);
1230 /* Otherwise, set the current delegation point and move on to the
1232 return next_state(iq, INIT_REQUEST_2_STATE);
1236 * Process the second part of the initial request handling. This state
1237 * basically exists so that queries that generate root priming events have
1238 * the same init processing as ones that do not. Request events that reach
1239 * this state must have a valid currentDelegationPoint set.
1241 * This part is primarly handling stub zone priming. Events that reach this
1242 * state must have a current delegation point.
1244 * @param qstate: query state.
1245 * @param iq: iterator query state.
1246 * @param id: module id.
1247 * @return true if the event needs more request processing immediately,
1251 processInitRequest2(struct module_qstate* qstate, struct iter_qstate* iq,
1256 log_query_info(VERB_QUERY, "resolving (init part 2): ",
1259 if(iq->refetch_glue) {
1261 log_err("internal or malloc fail: no dp for refetch");
1262 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1264 delname = iq->dp->name;
1265 delnamelen = iq->dp->namelen;
1267 delname = iq->qchase.qname;
1268 delnamelen = iq->qchase.qname_len;
1270 if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue) {
1271 if(!dname_is_root(delname))
1272 dname_remove_label(&delname, &delnamelen);
1273 iq->refetch_glue = 0; /* if CNAME causes restart, no refetch */
1275 /* Check to see if we need to prime a stub zone. */
1276 if(prime_stub(qstate, iq, id, delname, iq->qchase.qclass)) {
1277 /* A priming sub request was made */
1281 /* most events just get forwarded to the next state. */
1282 return next_state(iq, INIT_REQUEST_3_STATE);
1286 * Process the third part of the initial request handling. This state exists
1287 * as a separate state so that queries that generate stub priming events
1288 * will get the tail end of the init process but not repeat the stub priming
1291 * @param qstate: query state.
1292 * @param iq: iterator query state.
1293 * @param id: module id.
1294 * @return true, advancing the event to the QUERYTARGETS_STATE.
1297 processInitRequest3(struct module_qstate* qstate, struct iter_qstate* iq,
1300 log_query_info(VERB_QUERY, "resolving (init part 3): ",
1302 /* if the cache reply dp equals a validation anchor or msg has DS,
1303 * then DNSSEC RRSIGs are expected in the reply */
1304 iq->dnssec_expected = iter_indicates_dnssec(qstate->env, iq->dp,
1305 iq->deleg_msg, iq->qchase.qclass);
1307 /* If the RD flag wasn't set, then we just finish with the
1308 * cached referral as the response. */
1309 if(!(qstate->query_flags & BIT_RD)) {
1310 iq->response = iq->deleg_msg;
1311 if(verbosity >= VERB_ALGO && iq->response)
1312 log_dns_msg("no RD requested, using delegation msg",
1313 &iq->response->qinfo, iq->response->rep);
1314 if(qstate->reply_origin)
1315 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1316 return final_state(iq);
1318 /* After this point, unset the RD flag -- this query is going to
1319 * be sent to an auth. server. */
1320 iq->chase_flags &= ~BIT_RD;
1322 /* if dnssec expected, fetch key for the trust-anchor or cached-DS */
1323 if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
1324 !(qstate->query_flags&BIT_CD)) {
1325 generate_dnskey_prefetch(qstate, iq, id);
1326 fptr_ok(fptr_whitelist_modenv_detach_subs(
1327 qstate->env->detach_subs));
1328 (*qstate->env->detach_subs)(qstate);
1331 /* Jump to the next state. */
1332 return next_state(iq, QUERYTARGETS_STATE);
1336 * Given a basic query, generate a parent-side "target" query.
1337 * These are subordinate queries for missing delegation point target addresses,
1338 * for which only the parent of the delegation provides correct IP addresses.
1340 * @param qstate: query state.
1341 * @param iq: iterator query state.
1342 * @param id: module id.
1343 * @param name: target qname.
1344 * @param namelen: target qname length.
1345 * @param qtype: target qtype (either A or AAAA).
1346 * @param qclass: target qclass.
1347 * @return true on success, false on failure.
1350 generate_parentside_target_query(struct module_qstate* qstate,
1351 struct iter_qstate* iq, int id, uint8_t* name, size_t namelen,
1352 uint16_t qtype, uint16_t qclass)
1354 struct module_qstate* subq;
1355 if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1356 id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1359 struct iter_qstate* subiq =
1360 (struct iter_qstate*)subq->minfo[id];
1361 /* blacklist the cache - we want to fetch parent stuff */
1362 sock_list_insert(&subq->blacklist, NULL, 0, subq->region);
1363 subiq->query_for_pside_glue = 1;
1364 if(dname_subdomain_c(name, iq->dp->name)) {
1365 subiq->dp = delegpt_copy(iq->dp, subq->region);
1366 subiq->dnssec_expected = iter_indicates_dnssec(
1367 qstate->env, subiq->dp, NULL,
1368 subq->qinfo.qclass);
1369 subiq->refetch_glue = 1;
1371 subiq->dp = dns_cache_find_delegation(qstate->env,
1372 name, namelen, qtype, qclass, subq->region,
1374 *qstate->env->now+subq->prefetch_leeway);
1375 /* if no dp, then it's from root, refetch unneeded */
1377 subiq->dnssec_expected = iter_indicates_dnssec(
1378 qstate->env, subiq->dp, NULL,
1379 subq->qinfo.qclass);
1380 subiq->refetch_glue = 1;
1384 log_nametypeclass(VERB_QUERY, "new pside target", name, qtype, qclass);
1389 * Given a basic query, generate a "target" query. These are subordinate
1390 * queries for missing delegation point target addresses.
1392 * @param qstate: query state.
1393 * @param iq: iterator query state.
1394 * @param id: module id.
1395 * @param name: target qname.
1396 * @param namelen: target qname length.
1397 * @param qtype: target qtype (either A or AAAA).
1398 * @param qclass: target qclass.
1399 * @return true on success, false on failure.
1402 generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
1403 int id, uint8_t* name, size_t namelen, uint16_t qtype, uint16_t qclass)
1405 struct module_qstate* subq;
1406 if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1407 id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1409 log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
1414 * Given an event at a certain state, generate zero or more target queries
1415 * for it's current delegation point.
1417 * @param qstate: query state.
1418 * @param iq: iterator query state.
1419 * @param ie: iterator shared global environment.
1420 * @param id: module id.
1421 * @param maxtargets: The maximum number of targets to query for.
1422 * if it is negative, there is no maximum number of targets.
1423 * @param num: returns the number of queries generated and processed,
1424 * which may be zero if there were no missing targets.
1425 * @return false on error.
1428 query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
1429 struct iter_env* ie, int id, int maxtargets, int* num)
1431 int query_count = 0;
1432 struct delegpt_ns* ns;
1436 if(iq->depth == ie->max_dependency_depth)
1438 if(iq->depth > 0 && iq->target_count &&
1439 iq->target_count[1] > MAX_TARGET_COUNT) {
1440 char s[LDNS_MAX_DOMAINLEN+1];
1441 dname_str(qstate->qinfo.qname, s);
1442 verbose(VERB_QUERY, "request %s has exceeded the maximum "
1443 "number of glue fetches %d", s, iq->target_count[1]);
1447 iter_mark_cycle_targets(qstate, iq->dp);
1448 missing = (int)delegpt_count_missing_targets(iq->dp);
1449 log_assert(maxtargets != 0); /* that would not be useful */
1451 /* Generate target requests. Basically, any missing targets
1452 * are queried for here, regardless if it is necessary to do
1453 * so to continue processing. */
1454 if(maxtargets < 0 || maxtargets > missing)
1456 else toget = maxtargets;
1461 /* select 'toget' items from the total of 'missing' items */
1462 log_assert(toget <= missing);
1464 /* loop over missing targets */
1465 for(ns = iq->dp->nslist; ns; ns = ns->next) {
1469 /* randomly select this item with probability toget/missing */
1470 if(!iter_ns_probability(qstate->env->rnd, toget, missing)) {
1471 /* do not select this one, next; select toget number
1472 * of items from a list one less in size */
1477 if(ie->supports_ipv6 && !ns->got6) {
1478 /* Send the AAAA request. */
1479 if(!generate_target_query(qstate, iq, id,
1480 ns->name, ns->namelen,
1481 LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
1484 qstate->ext_state[id] = module_wait_subquery;
1489 /* Send the A request. */
1490 if(ie->supports_ipv4 && !ns->got4) {
1491 if(!generate_target_query(qstate, iq, id,
1492 ns->name, ns->namelen,
1493 LDNS_RR_TYPE_A, iq->qchase.qclass)) {
1496 qstate->ext_state[id] = module_wait_subquery;
1502 /* mark this target as in progress. */
1511 qstate->ext_state[id] = module_wait_subquery;
1516 /** see if last resort is possible - does config allow queries to parent */
1518 can_have_last_resort(struct module_env* env, struct delegpt* dp,
1519 struct iter_qstate* iq)
1521 struct delegpt* fwddp;
1522 struct iter_hints_stub* stub;
1523 /* do not process a last resort (the parent side) if a stub
1524 * or forward is configured, because we do not want to go 'above'
1525 * the configured servers */
1526 if(!dname_is_root(dp->name) && (stub = (struct iter_hints_stub*)
1527 name_tree_find(&env->hints->tree, dp->name, dp->namelen,
1528 dp->namelabs, iq->qchase.qclass)) &&
1529 /* has_parent side is turned off for stub_first, where we
1530 * are allowed to go to the parent */
1531 stub->dp->has_parent_side_NS) {
1532 verbose(VERB_QUERY, "configured stub servers failed -- returning SERVFAIL");
1535 if((fwddp = forwards_find(env->fwds, dp->name, iq->qchase.qclass)) &&
1536 /* has_parent_side is turned off for forward_first, where
1537 * we are allowed to go to the parent */
1538 fwddp->has_parent_side_NS) {
1539 verbose(VERB_QUERY, "configured forward servers failed -- returning SERVFAIL");
1546 * Called by processQueryTargets when it would like extra targets to query
1547 * but it seems to be out of options. At last resort some less appealing
1548 * options are explored. If there are no more options, the result is SERVFAIL
1550 * @param qstate: query state.
1551 * @param iq: iterator query state.
1552 * @param ie: iterator shared global environment.
1553 * @param id: module id.
1554 * @return true if the event requires more request processing immediately,
1558 processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
1559 struct iter_env* ie, int id)
1561 struct delegpt_ns* ns;
1562 int query_count = 0;
1563 verbose(VERB_ALGO, "No more query targets, attempting last resort");
1566 if(!can_have_last_resort(qstate->env, iq->dp, iq)) {
1567 /* fail -- no more targets, no more hope of targets, no hope
1569 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1571 if(!iq->dp->has_parent_side_NS && dname_is_root(iq->dp->name)) {
1572 struct delegpt* p = hints_lookup_root(qstate->env->hints,
1575 struct delegpt_ns* ns;
1576 struct delegpt_addr* a;
1577 iq->chase_flags &= ~BIT_RD; /* go to authorities */
1578 for(ns = p->nslist; ns; ns=ns->next) {
1579 (void)delegpt_add_ns(iq->dp, qstate->region,
1580 ns->name, ns->lame);
1582 for(a = p->target_list; a; a=a->next_target) {
1583 (void)delegpt_add_addr(iq->dp, qstate->region,
1584 &a->addr, a->addrlen, a->bogus,
1588 iq->dp->has_parent_side_NS = 1;
1589 } else if(!iq->dp->has_parent_side_NS) {
1590 if(!iter_lookup_parent_NS_from_cache(qstate->env, iq->dp,
1591 qstate->region, &qstate->qinfo)
1592 || !iq->dp->has_parent_side_NS) {
1593 /* if: malloc failure in lookup go up to try */
1594 /* if: no parent NS in cache - go up one level */
1595 verbose(VERB_ALGO, "try to grab parent NS");
1596 iq->store_parent_NS = iq->dp;
1597 iq->chase_flags &= ~BIT_RD; /* go to authorities */
1598 iq->deleg_msg = NULL;
1599 iq->refetch_glue = 1;
1600 iq->query_restart_count++;
1602 return next_state(iq, INIT_REQUEST_STATE);
1605 /* see if that makes new names available */
1606 if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
1607 qstate->region, iq->dp))
1608 log_err("out of memory in cache_fill_missing");
1609 if(iq->dp->usable_list) {
1610 verbose(VERB_ALGO, "try parent-side-name, w. glue from cache");
1611 return next_state(iq, QUERYTARGETS_STATE);
1613 /* try to fill out parent glue from cache */
1614 if(iter_lookup_parent_glue_from_cache(qstate->env, iq->dp,
1615 qstate->region, &qstate->qinfo)) {
1616 /* got parent stuff from cache, see if we can continue */
1617 verbose(VERB_ALGO, "try parent-side glue from cache");
1618 return next_state(iq, QUERYTARGETS_STATE);
1620 /* query for an extra name added by the parent-NS record */
1621 if(delegpt_count_missing_targets(iq->dp) > 0) {
1623 verbose(VERB_ALGO, "try parent-side target name");
1624 if(!query_for_targets(qstate, iq, ie, id, 1, &qs)) {
1625 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1627 iq->num_target_queries += qs;
1628 target_count_increase(iq, qs);
1630 qstate->ext_state[id] = module_wait_subquery;
1631 return 0; /* and wait for them */
1634 if(iq->depth == ie->max_dependency_depth) {
1635 verbose(VERB_QUERY, "maxdepth and need more nameservers, fail");
1636 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1638 if(iq->depth > 0 && iq->target_count &&
1639 iq->target_count[1] > MAX_TARGET_COUNT) {
1640 char s[LDNS_MAX_DOMAINLEN+1];
1641 dname_str(qstate->qinfo.qname, s);
1642 verbose(VERB_QUERY, "request %s has exceeded the maximum "
1643 "number of glue fetches %d", s, iq->target_count[1]);
1644 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1646 /* mark cycle targets for parent-side lookups */
1647 iter_mark_pside_cycle_targets(qstate, iq->dp);
1648 /* see if we can issue queries to get nameserver addresses */
1649 /* this lookup is not randomized, but sequential. */
1650 for(ns = iq->dp->nslist; ns; ns = ns->next) {
1651 /* query for parent-side A and AAAA for nameservers */
1652 if(ie->supports_ipv6 && !ns->done_pside6) {
1653 /* Send the AAAA request. */
1654 if(!generate_parentside_target_query(qstate, iq, id,
1655 ns->name, ns->namelen,
1656 LDNS_RR_TYPE_AAAA, iq->qchase.qclass))
1657 return error_response(qstate, id,
1658 LDNS_RCODE_SERVFAIL);
1659 ns->done_pside6 = 1;
1662 if(ie->supports_ipv4 && !ns->done_pside4) {
1663 /* Send the A request. */
1664 if(!generate_parentside_target_query(qstate, iq, id,
1665 ns->name, ns->namelen,
1666 LDNS_RR_TYPE_A, iq->qchase.qclass))
1667 return error_response(qstate, id,
1668 LDNS_RCODE_SERVFAIL);
1669 ns->done_pside4 = 1;
1672 if(query_count != 0) { /* suspend to await results */
1673 verbose(VERB_ALGO, "try parent-side glue lookup");
1674 iq->num_target_queries += query_count;
1675 target_count_increase(iq, query_count);
1676 qstate->ext_state[id] = module_wait_subquery;
1681 /* if this was a parent-side glue query itself, then store that
1682 * failure in cache. */
1683 if(iq->query_for_pside_glue && !iq->pside_glue)
1684 iter_store_parentside_neg(qstate->env, &qstate->qinfo,
1685 iq->deleg_msg?iq->deleg_msg->rep:
1686 (iq->response?iq->response->rep:NULL));
1688 verbose(VERB_QUERY, "out of query targets -- returning SERVFAIL");
1689 /* fail -- no more targets, no more hope of targets, no hope
1691 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1695 * Try to find the NS record set that will resolve a qtype DS query. Due
1696 * to grandparent/grandchild reasons we did not get a proper lookup right
1697 * away. We need to create type NS queries until we get the right parent
1698 * for this lookup. We remove labels from the query to find the right point.
1699 * If we end up at the old dp name, then there is no solution.
1701 * @param qstate: query state.
1702 * @param iq: iterator query state.
1703 * @param id: module id.
1704 * @return true if the event requires more immediate processing, false if
1705 * not. This is generally only true when forwarding the request to
1706 * the final state (i.e., on answer).
1709 processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
1711 struct module_qstate* subq = NULL;
1712 verbose(VERB_ALGO, "processDSNSFind");
1714 if(!iq->dsns_point) {
1716 iq->dsns_point = iq->qchase.qname;
1717 iq->dsns_point_len = iq->qchase.qname_len;
1719 /* robustcheck for internal error: we are not underneath the dp */
1720 if(!dname_subdomain_c(iq->dsns_point, iq->dp->name)) {
1721 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1724 /* go up one (more) step, until we hit the dp, if so, end */
1725 dname_remove_label(&iq->dsns_point, &iq->dsns_point_len);
1726 if(query_dname_compare(iq->dsns_point, iq->dp->name) == 0) {
1727 /* there was no inbetween nameserver, use the old delegation
1728 * point again. And this time, because dsns_point is nonNULL
1729 * we are going to accept the (bad) result */
1730 iq->state = QUERYTARGETS_STATE;
1733 iq->state = DSNS_FIND_STATE;
1735 /* spawn NS lookup (validation not needed, this is for DS lookup) */
1736 log_nametypeclass(VERB_ALGO, "fetch nameservers",
1737 iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1738 if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len,
1739 LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1740 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
1741 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1748 * This is the request event state where the request will be sent to one of
1749 * its current query targets. This state also handles issuing target lookup
1750 * queries for missing target IP addresses. Queries typically iterate on
1751 * this state, both when they are just trying different targets for a given
1752 * delegation point, and when they change delegation points. This state
1753 * roughly corresponds to RFC 1034 algorithm steps 3 and 4.
1755 * @param qstate: query state.
1756 * @param iq: iterator query state.
1757 * @param ie: iterator shared global environment.
1758 * @param id: module id.
1759 * @return true if the event requires more request processing immediately,
1760 * false if not. This state only returns true when it is generating
1761 * a SERVFAIL response because the query has hit a dead end.
1764 processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
1765 struct iter_env* ie, int id)
1768 struct delegpt_addr* target;
1769 struct outbound_entry* outq;
1771 /* NOTE: a request will encounter this state for each target it
1772 * needs to send a query to. That is, at least one per referral,
1773 * more if some targets timeout or return throwaway answers. */
1775 log_query_info(VERB_QUERY, "processQueryTargets:", &qstate->qinfo);
1776 verbose(VERB_ALGO, "processQueryTargets: targetqueries %d, "
1777 "currentqueries %d sentcount %d", iq->num_target_queries,
1778 iq->num_current_queries, iq->sent_count);
1780 /* Make sure that we haven't run away */
1781 /* FIXME: is this check even necessary? */
1782 if(iq->referral_count > MAX_REFERRAL_COUNT) {
1783 verbose(VERB_QUERY, "request has exceeded the maximum "
1784 "number of referrrals with %d", iq->referral_count);
1785 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1787 if(iq->sent_count > MAX_SENT_COUNT) {
1788 verbose(VERB_QUERY, "request has exceeded the maximum "
1789 "number of sends with %d", iq->sent_count);
1790 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1793 /* Make sure we have a delegation point, otherwise priming failed
1794 * or another failure occurred */
1796 verbose(VERB_QUERY, "Failed to get a delegation, giving up");
1797 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1799 if(!ie->supports_ipv6)
1800 delegpt_no_ipv6(iq->dp);
1801 if(!ie->supports_ipv4)
1802 delegpt_no_ipv4(iq->dp);
1803 delegpt_log(VERB_ALGO, iq->dp);
1805 if(iq->num_current_queries>0) {
1806 /* already busy answering a query, this restart is because
1807 * more delegpt addrs became available, wait for existing
1809 verbose(VERB_ALGO, "woke up, but wait for outstanding query");
1810 qstate->ext_state[id] = module_wait_reply;
1815 /* < not <=, because although the array is large enough for <=, the
1816 * generated query will immediately be discarded due to depth and
1817 * that servfail is cached, which is not good as opportunism goes. */
1818 if(iq->depth < ie->max_dependency_depth
1819 && iq->sent_count < TARGET_FETCH_STOP) {
1820 tf_policy = ie->target_fetch_policy[iq->depth];
1823 /* if in 0x20 fallback get as many targets as possible */
1824 if(iq->caps_fallback) {
1826 size_t naddr, nres, navail;
1827 if(!query_for_targets(qstate, iq, ie, id, -1, &extra)) {
1828 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1830 iq->num_target_queries += extra;
1831 target_count_increase(iq, extra);
1832 if(iq->num_target_queries > 0) {
1833 /* wait to get all targets, we want to try em */
1834 verbose(VERB_ALGO, "wait for all targets for fallback");
1835 qstate->ext_state[id] = module_wait_reply;
1838 /* did we do enough fallback queries already? */
1839 delegpt_count_addr(iq->dp, &naddr, &nres, &navail);
1840 /* the current caps_server is the number of fallbacks sent.
1841 * the original query is one that matched too, so we have
1842 * caps_server+1 number of matching queries now */
1843 if(iq->caps_server+1 >= naddr*3 ||
1844 iq->caps_server*2+2 >= MAX_SENT_COUNT) {
1845 /* *2 on sentcount check because ipv6 may fail */
1846 /* we're done, process the response */
1847 verbose(VERB_ALGO, "0x20 fallback had %d responses "
1848 "match for %d wanted, done.",
1849 (int)iq->caps_server+1, (int)naddr*3);
1850 iq->response = iq->caps_response;
1851 iq->caps_fallback = 0;
1852 iter_dec_attempts(iq->dp, 3); /* space for fallback */
1853 iq->num_current_queries++; /* RespState decrements it*/
1854 iq->referral_count++; /* make sure we don't loop */
1856 iq->state = QUERY_RESP_STATE;
1859 verbose(VERB_ALGO, "0x20 fallback number %d",
1860 (int)iq->caps_server);
1862 /* if there is a policy to fetch missing targets
1863 * opportunistically, do it. we rely on the fact that once a
1864 * query (or queries) for a missing name have been issued,
1865 * they will not show up again. */
1866 } else if(tf_policy != 0) {
1868 verbose(VERB_ALGO, "attempt to get extra %d targets",
1870 (void)query_for_targets(qstate, iq, ie, id, tf_policy, &extra);
1871 /* errors ignored, these targets are not strictly necessary for
1872 * this result, we do not have to reply with SERVFAIL */
1873 iq->num_target_queries += extra;
1874 target_count_increase(iq, extra);
1877 /* Add the current set of unused targets to our queue. */
1878 delegpt_add_unused_targets(iq->dp);
1880 /* Select the next usable target, filtering out unsuitable targets. */
1881 target = iter_server_selection(ie, qstate->env, iq->dp,
1882 iq->dp->name, iq->dp->namelen, iq->qchase.qtype,
1883 &iq->dnssec_lame_query, &iq->chase_to_rd,
1884 iq->num_target_queries, qstate->blacklist);
1886 /* If no usable target was selected... */
1888 /* Here we distinguish between three states: generate a new
1889 * target query, just wait, or quit (with a SERVFAIL).
1890 * We have the following information: number of active
1891 * target queries, number of active current queries,
1892 * the presence of missing targets at this delegation
1893 * point, and the given query target policy. */
1895 /* Check for the wait condition. If this is true, then
1896 * an action must be taken. */
1897 if(iq->num_target_queries==0 && iq->num_current_queries==0) {
1898 /* If there is nothing to wait for, then we need
1899 * to distinguish between generating (a) new target
1900 * query, or failing. */
1901 if(delegpt_count_missing_targets(iq->dp) > 0) {
1903 verbose(VERB_ALGO, "querying for next "
1905 if(!query_for_targets(qstate, iq, ie, id,
1907 return error_response(qstate, id,
1908 LDNS_RCODE_SERVFAIL);
1911 delegpt_count_missing_targets(iq->dp) == 0){
1912 /* it looked like there were missing
1913 * targets, but they did not turn up.
1914 * Try the bad choices again (if any),
1915 * when we get back here missing==0,
1916 * so this is not a loop. */
1919 iq->num_target_queries += qs;
1920 target_count_increase(iq, qs);
1922 /* Since a target query might have been made, we
1923 * need to check again. */
1924 if(iq->num_target_queries == 0) {
1925 /* if in capsforid fallback, instead of last
1926 * resort, we agree with the current reply
1927 * we have (if any) (our count of addrs bad)*/
1928 if(iq->caps_fallback && iq->caps_reply) {
1929 /* we're done, process the response */
1930 verbose(VERB_ALGO, "0x20 fallback had %d responses, "
1931 "but no more servers except "
1932 "last resort, done.",
1933 (int)iq->caps_server+1);
1934 iq->response = iq->caps_response;
1935 iq->caps_fallback = 0;
1936 iter_dec_attempts(iq->dp, 3); /* space for fallback */
1937 iq->num_current_queries++; /* RespState decrements it*/
1938 iq->referral_count++; /* make sure we don't loop */
1940 iq->state = QUERY_RESP_STATE;
1943 return processLastResort(qstate, iq, ie, id);
1947 /* otherwise, we have no current targets, so submerge
1948 * until one of the target or direct queries return. */
1949 if(iq->num_target_queries>0 && iq->num_current_queries>0) {
1950 verbose(VERB_ALGO, "no current targets -- waiting "
1951 "for %d targets to resolve or %d outstanding"
1952 " queries to respond", iq->num_target_queries,
1953 iq->num_current_queries);
1954 qstate->ext_state[id] = module_wait_reply;
1955 } else if(iq->num_target_queries>0) {
1956 verbose(VERB_ALGO, "no current targets -- waiting "
1957 "for %d targets to resolve.",
1958 iq->num_target_queries);
1959 qstate->ext_state[id] = module_wait_subquery;
1961 verbose(VERB_ALGO, "no current targets -- waiting "
1962 "for %d outstanding queries to respond.",
1963 iq->num_current_queries);
1964 qstate->ext_state[id] = module_wait_reply;
1969 /* if not forwarding, check ratelimits per delegationpoint name */
1970 if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
1971 if(!infra_ratelimit_inc(qstate->env->infra_cache, iq->dp->name,
1972 iq->dp->namelen, *qstate->env->now)) {
1973 verbose(VERB_ALGO, "query exceeded ratelimits");
1974 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1978 /* We have a valid target. */
1979 if(verbosity >= VERB_QUERY) {
1980 log_query_info(VERB_QUERY, "sending query:", &iq->qchase);
1981 log_name_addr(VERB_QUERY, "sending to target:", iq->dp->name,
1982 &target->addr, target->addrlen);
1983 verbose(VERB_ALGO, "dnssec status: %s%s",
1984 iq->dnssec_expected?"expected": "not expected",
1985 iq->dnssec_lame_query?" but lame_query anyway": "");
1987 fptr_ok(fptr_whitelist_modenv_send_query(qstate->env->send_query));
1988 outq = (*qstate->env->send_query)(
1989 iq->qchase.qname, iq->qchase.qname_len,
1990 iq->qchase.qtype, iq->qchase.qclass,
1991 iq->chase_flags | (iq->chase_to_rd?BIT_RD:0), EDNS_DO|BIT_CD,
1992 iq->dnssec_expected, iq->caps_fallback || is_caps_whitelisted(
1993 ie, iq), &target->addr, target->addrlen, iq->dp->name,
1994 iq->dp->namelen, qstate);
1996 log_addr(VERB_DETAIL, "error sending query to auth server",
1997 &target->addr, target->addrlen);
1998 if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok)
1999 infra_ratelimit_dec(qstate->env->infra_cache, iq->dp->name,
2000 iq->dp->namelen, *qstate->env->now);
2001 return next_state(iq, QUERYTARGETS_STATE);
2003 outbound_list_insert(&iq->outlist, outq);
2004 iq->num_current_queries++;
2006 qstate->ext_state[id] = module_wait_reply;
2011 /** find NS rrset in given list */
2012 static struct ub_packed_rrset_key*
2013 find_NS(struct reply_info* rep, size_t from, size_t to)
2016 for(i=from; i<to; i++) {
2017 if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
2018 return rep->rrsets[i];
2025 * Process the query response. All queries end up at this state first. This
2026 * process generally consists of analyzing the response and routing the
2027 * event to the next state (either bouncing it back to a request state, or
2028 * terminating the processing for this event).
2030 * @param qstate: query state.
2031 * @param iq: iterator query state.
2032 * @param id: module id.
2033 * @return true if the event requires more immediate processing, false if
2034 * not. This is generally only true when forwarding the request to
2035 * the final state (i.e., on answer).
2038 processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
2042 enum response_type type;
2043 iq->num_current_queries--;
2044 if(iq->response == NULL) {
2045 iq->chase_to_rd = 0;
2046 iq->dnssec_lame_query = 0;
2047 verbose(VERB_ALGO, "query response was timeout");
2048 return next_state(iq, QUERYTARGETS_STATE);
2050 type = response_type_from_server(
2051 (int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2052 iq->response, &iq->qchase, iq->dp);
2053 iq->chase_to_rd = 0;
2054 if(type == RESPONSE_TYPE_REFERRAL && (iq->chase_flags&BIT_RD)) {
2055 /* When forwarding (RD bit is set), we handle referrals
2056 * differently. No queries should be sent elsewhere */
2057 type = RESPONSE_TYPE_ANSWER;
2059 if(iq->dnssec_expected && !iq->dnssec_lame_query &&
2060 !(iq->chase_flags&BIT_RD)
2061 && type != RESPONSE_TYPE_LAME
2062 && type != RESPONSE_TYPE_REC_LAME
2063 && type != RESPONSE_TYPE_THROWAWAY
2064 && type != RESPONSE_TYPE_UNTYPED) {
2065 /* a possible answer, see if it is missing DNSSEC */
2066 /* but not when forwarding, so we dont mark fwder lame */
2067 if(!iter_msg_has_dnssec(iq->response)) {
2068 /* Mark this address as dnsseclame in this dp,
2069 * because that will make serverselection disprefer
2070 * it, but also, once it is the only final option,
2071 * use dnssec-lame-bypass if it needs to query there.*/
2073 struct delegpt_addr* a = delegpt_find_addr(
2074 iq->dp, &qstate->reply->addr,
2075 qstate->reply->addrlen);
2076 if(a) a->dnsseclame = 1;
2078 /* test the answer is from the zone we expected,
2079 * otherwise, (due to parent,child on same server), we
2080 * might mark the server,zone lame inappropriately */
2081 if(!iter_msg_from_zone(iq->response, iq->dp, type,
2083 qstate->reply = NULL;
2084 type = RESPONSE_TYPE_LAME;
2087 } else iq->dnssec_lame_query = 0;
2088 /* see if referral brings us close to the target */
2089 if(type == RESPONSE_TYPE_REFERRAL) {
2090 struct ub_packed_rrset_key* ns = find_NS(
2091 iq->response->rep, iq->response->rep->an_numrrsets,
2092 iq->response->rep->an_numrrsets
2093 + iq->response->rep->ns_numrrsets);
2094 if(!ns) ns = find_NS(iq->response->rep, 0,
2095 iq->response->rep->an_numrrsets);
2096 if(!ns || !dname_strict_subdomain_c(ns->rk.dname, iq->dp->name)
2097 || !dname_subdomain_c(iq->qchase.qname, ns->rk.dname)){
2098 verbose(VERB_ALGO, "bad referral, throwaway");
2099 type = RESPONSE_TYPE_THROWAWAY;
2101 iter_scrub_ds(iq->response, ns, iq->dp->name);
2102 } else iter_scrub_ds(iq->response, NULL, NULL);
2104 /* handle each of the type cases */
2105 if(type == RESPONSE_TYPE_ANSWER) {
2106 /* ANSWER type responses terminate the query algorithm,
2107 * so they sent on their */
2108 if(verbosity >= VERB_DETAIL) {
2109 verbose(VERB_DETAIL, "query response was %s",
2110 FLAGS_GET_RCODE(iq->response->rep->flags)
2111 ==LDNS_RCODE_NXDOMAIN?"NXDOMAIN ANSWER":
2112 (iq->response->rep->an_numrrsets?"ANSWER":
2115 /* if qtype is DS, check we have the right level of answer,
2116 * like grandchild answer but we need the middle, reject it */
2117 if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2118 && !(iq->chase_flags&BIT_RD)
2119 && iter_ds_toolow(iq->response, iq->dp)
2120 && iter_dp_cangodown(&iq->qchase, iq->dp)) {
2121 /* close down outstanding requests to be discarded */
2122 outbound_list_clear(&iq->outlist);
2123 iq->num_current_queries = 0;
2124 fptr_ok(fptr_whitelist_modenv_detach_subs(
2125 qstate->env->detach_subs));
2126 (*qstate->env->detach_subs)(qstate);
2127 iq->num_target_queries = 0;
2128 return processDSNSFind(qstate, iq, id);
2130 iter_dns_store(qstate->env, &iq->response->qinfo,
2131 iq->response->rep, 0, qstate->prefetch_leeway,
2132 iq->dp&&iq->dp->has_parent_side_NS,
2133 qstate->region, qstate->query_flags);
2134 /* close down outstanding requests to be discarded */
2135 outbound_list_clear(&iq->outlist);
2136 iq->num_current_queries = 0;
2137 fptr_ok(fptr_whitelist_modenv_detach_subs(
2138 qstate->env->detach_subs));
2139 (*qstate->env->detach_subs)(qstate);
2140 iq->num_target_queries = 0;
2142 sock_list_insert(&qstate->reply_origin,
2143 &qstate->reply->addr, qstate->reply->addrlen,
2145 return final_state(iq);
2146 } else if(type == RESPONSE_TYPE_REFERRAL) {
2147 /* REFERRAL type responses get a reset of the
2148 * delegation point, and back to the QUERYTARGETS_STATE. */
2149 verbose(VERB_DETAIL, "query response was REFERRAL");
2151 if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
2152 /* we have a referral, no ratelimit, we can send
2153 * our queries to the given name */
2154 infra_ratelimit_dec(qstate->env->infra_cache,
2155 iq->dp->name, iq->dp->namelen,
2159 /* if hardened, only store referral if we asked for it */
2160 if(!qstate->env->cfg->harden_referral_path ||
2161 ( qstate->qinfo.qtype == LDNS_RR_TYPE_NS
2162 && (qstate->query_flags&BIT_RD)
2163 && !(qstate->query_flags&BIT_CD)
2164 /* we know that all other NS rrsets are scrubbed
2165 * away, thus on referral only one is left.
2166 * see if that equals the query name... */
2167 && ( /* auth section, but sometimes in answer section*/
2168 reply_find_rrset_section_ns(iq->response->rep,
2169 iq->qchase.qname, iq->qchase.qname_len,
2170 LDNS_RR_TYPE_NS, iq->qchase.qclass)
2171 || reply_find_rrset_section_an(iq->response->rep,
2172 iq->qchase.qname, iq->qchase.qname_len,
2173 LDNS_RR_TYPE_NS, iq->qchase.qclass)
2176 /* Store the referral under the current query */
2177 /* no prefetch-leeway, since its not the answer */
2178 iter_dns_store(qstate->env, &iq->response->qinfo,
2179 iq->response->rep, 1, 0, 0, NULL, 0);
2180 if(iq->store_parent_NS)
2181 iter_store_parentside_NS(qstate->env,
2183 if(qstate->env->neg_cache)
2184 val_neg_addreferral(qstate->env->neg_cache,
2185 iq->response->rep, iq->dp->name);
2187 /* store parent-side-in-zone-glue, if directly queried for */
2188 if(iq->query_for_pside_glue && !iq->pside_glue) {
2189 iq->pside_glue = reply_find_rrset(iq->response->rep,
2190 iq->qchase.qname, iq->qchase.qname_len,
2191 iq->qchase.qtype, iq->qchase.qclass);
2192 if(iq->pside_glue) {
2193 log_rrset_key(VERB_ALGO, "found parent-side "
2194 "glue", iq->pside_glue);
2195 iter_store_parentside_rrset(qstate->env,
2200 /* Reset the event state, setting the current delegation
2201 * point to the referral. */
2202 iq->deleg_msg = iq->response;
2203 iq->dp = delegpt_from_message(iq->response, qstate->region);
2205 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2206 if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
2207 qstate->region, iq->dp))
2208 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2209 if(iq->store_parent_NS && query_dname_compare(iq->dp->name,
2210 iq->store_parent_NS->name) == 0)
2211 iter_merge_retry_counts(iq->dp, iq->store_parent_NS);
2212 delegpt_log(VERB_ALGO, iq->dp);
2213 /* Count this as a referral. */
2214 iq->referral_count++;
2216 /* see if the next dp is a trust anchor, or a DS was sent
2217 * along, indicating dnssec is expected for next zone */
2218 iq->dnssec_expected = iter_indicates_dnssec(qstate->env,
2219 iq->dp, iq->response, iq->qchase.qclass);
2220 /* if dnssec, validating then also fetch the key for the DS */
2221 if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
2222 !(qstate->query_flags&BIT_CD))
2223 generate_dnskey_prefetch(qstate, iq, id);
2225 /* spawn off NS and addr to auth servers for the NS we just
2226 * got in the referral. This gets authoritative answer
2227 * (answer section trust level) rrset.
2228 * right after, we detach the subs, answer goes to cache. */
2229 if(qstate->env->cfg->harden_referral_path)
2230 generate_ns_check(qstate, iq, id);
2232 /* stop current outstanding queries.
2233 * FIXME: should the outstanding queries be waited for and
2234 * handled? Say by a subquery that inherits the outbound_entry.
2236 outbound_list_clear(&iq->outlist);
2237 iq->num_current_queries = 0;
2238 fptr_ok(fptr_whitelist_modenv_detach_subs(
2239 qstate->env->detach_subs));
2240 (*qstate->env->detach_subs)(qstate);
2241 iq->num_target_queries = 0;
2242 verbose(VERB_ALGO, "cleared outbound list for next round");
2243 return next_state(iq, QUERYTARGETS_STATE);
2244 } else if(type == RESPONSE_TYPE_CNAME) {
2245 uint8_t* sname = NULL;
2246 size_t snamelen = 0;
2247 /* CNAME type responses get a query restart (i.e., get a
2248 * reset of the query state and go back to INIT_REQUEST_STATE).
2250 verbose(VERB_DETAIL, "query response was CNAME");
2251 if(verbosity >= VERB_ALGO)
2252 log_dns_msg("cname msg", &iq->response->qinfo,
2254 /* if qtype is DS, check we have the right level of answer,
2255 * like grandchild answer but we need the middle, reject it */
2256 if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2257 && !(iq->chase_flags&BIT_RD)
2258 && iter_ds_toolow(iq->response, iq->dp)
2259 && iter_dp_cangodown(&iq->qchase, iq->dp)) {
2260 outbound_list_clear(&iq->outlist);
2261 iq->num_current_queries = 0;
2262 fptr_ok(fptr_whitelist_modenv_detach_subs(
2263 qstate->env->detach_subs));
2264 (*qstate->env->detach_subs)(qstate);
2265 iq->num_target_queries = 0;
2266 return processDSNSFind(qstate, iq, id);
2268 /* Process the CNAME response. */
2269 if(!handle_cname_response(qstate, iq, iq->response,
2271 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2272 /* cache the CNAME response under the current query */
2273 /* NOTE : set referral=1, so that rrsets get stored but not
2274 * the partial query answer (CNAME only). */
2275 /* prefetchleeway applied because this updates answer parts */
2276 iter_dns_store(qstate->env, &iq->response->qinfo,
2277 iq->response->rep, 1, qstate->prefetch_leeway,
2278 iq->dp&&iq->dp->has_parent_side_NS, NULL,
2279 qstate->query_flags);
2280 /* set the current request's qname to the new value. */
2281 iq->qchase.qname = sname;
2282 iq->qchase.qname_len = snamelen;
2283 /* Clear the query state, since this is a query restart. */
2284 iq->deleg_msg = NULL;
2286 iq->dsns_point = NULL;
2287 /* Note the query restart. */
2288 iq->query_restart_count++;
2291 /* stop current outstanding queries.
2292 * FIXME: should the outstanding queries be waited for and
2293 * handled? Say by a subquery that inherits the outbound_entry.
2295 outbound_list_clear(&iq->outlist);
2296 iq->num_current_queries = 0;
2297 fptr_ok(fptr_whitelist_modenv_detach_subs(
2298 qstate->env->detach_subs));
2299 (*qstate->env->detach_subs)(qstate);
2300 iq->num_target_queries = 0;
2302 sock_list_insert(&qstate->reply_origin,
2303 &qstate->reply->addr, qstate->reply->addrlen,
2305 verbose(VERB_ALGO, "cleared outbound list for query restart");
2306 /* go to INIT_REQUEST_STATE for new qname. */
2307 return next_state(iq, INIT_REQUEST_STATE);
2308 } else if(type == RESPONSE_TYPE_LAME) {
2309 /* Cache the LAMEness. */
2310 verbose(VERB_DETAIL, "query response was %sLAME",
2311 dnsseclame?"DNSSEC ":"");
2312 if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2313 log_err("mark lame: mismatch in qname and dpname");
2314 /* throwaway this reply below */
2315 } else if(qstate->reply) {
2316 /* need addr for lameness cache, but we may have
2317 * gotten this from cache, so test to be sure */
2318 if(!infra_set_lame(qstate->env->infra_cache,
2319 &qstate->reply->addr, qstate->reply->addrlen,
2320 iq->dp->name, iq->dp->namelen,
2321 *qstate->env->now, dnsseclame, 0,
2323 log_err("mark host lame: out of memory");
2325 } else if(type == RESPONSE_TYPE_REC_LAME) {
2326 /* Cache the LAMEness. */
2327 verbose(VERB_DETAIL, "query response REC_LAME: "
2328 "recursive but not authoritative server");
2329 if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2330 log_err("mark rec_lame: mismatch in qname and dpname");
2331 /* throwaway this reply below */
2332 } else if(qstate->reply) {
2333 /* need addr for lameness cache, but we may have
2334 * gotten this from cache, so test to be sure */
2335 verbose(VERB_DETAIL, "mark as REC_LAME");
2336 if(!infra_set_lame(qstate->env->infra_cache,
2337 &qstate->reply->addr, qstate->reply->addrlen,
2338 iq->dp->name, iq->dp->namelen,
2339 *qstate->env->now, 0, 1, iq->qchase.qtype))
2340 log_err("mark host lame: out of memory");
2342 } else if(type == RESPONSE_TYPE_THROWAWAY) {
2343 /* LAME and THROWAWAY responses are handled the same way.
2344 * In this case, the event is just sent directly back to
2345 * the QUERYTARGETS_STATE without resetting anything,
2346 * because, clearly, the next target must be tried. */
2347 verbose(VERB_DETAIL, "query response was THROWAWAY");
2349 log_warn("A query response came back with an unknown type: %d",
2353 /* LAME, THROWAWAY and "unknown" all end up here.
2354 * Recycle to the QUERYTARGETS state to hopefully try a
2355 * different target. */
2356 return next_state(iq, QUERYTARGETS_STATE);
2360 * Return priming query results to interested super querystates.
2362 * Sets the delegation point and delegation message (not nonRD queries).
2363 * This is a callback from walk_supers.
2365 * @param qstate: priming query state that finished.
2366 * @param id: module id.
2367 * @param forq: the qstate for which priming has been done.
2370 prime_supers(struct module_qstate* qstate, int id, struct module_qstate* forq)
2372 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2373 struct delegpt* dp = NULL;
2375 log_assert(qstate->is_priming || foriq->wait_priming_stub);
2376 log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2377 /* Convert our response to a delegation point */
2378 dp = delegpt_from_message(qstate->return_msg, forq->region);
2380 /* if there is no convertable delegation point, then
2381 * the ANSWER type was (presumably) a negative answer. */
2382 verbose(VERB_ALGO, "prime response was not a positive "
2385 foriq->state = QUERYTARGETS_STATE;
2389 log_query_info(VERB_DETAIL, "priming successful for", &qstate->qinfo);
2390 delegpt_log(VERB_ALGO, dp);
2392 foriq->deleg_msg = dns_copy_msg(qstate->return_msg, forq->region);
2393 if(!foriq->deleg_msg) {
2394 log_err("copy prime response: out of memory");
2396 foriq->state = QUERYTARGETS_STATE;
2400 /* root priming responses go to init stage 2, priming stub
2401 * responses to to stage 3. */
2402 if(foriq->wait_priming_stub) {
2403 foriq->state = INIT_REQUEST_3_STATE;
2404 foriq->wait_priming_stub = 0;
2405 } else foriq->state = INIT_REQUEST_2_STATE;
2406 /* because we are finished, the parent will be reactivated */
2410 * This handles the response to a priming query. This is used to handle both
2411 * root and stub priming responses. This is basically the equivalent of the
2412 * QUERY_RESP_STATE, but will not handle CNAME responses and will treat
2413 * REFERRALs as ANSWERS. It will also update and reactivate the originating
2416 * @param qstate: query state.
2417 * @param id: module id.
2418 * @return true if the event needs more immediate processing, false if not.
2419 * This state always returns false.
2422 processPrimeResponse(struct module_qstate* qstate, int id)
2424 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2425 enum response_type type;
2426 iq->response->rep->flags &= ~(BIT_RD|BIT_RA); /* ignore rec-lame */
2427 type = response_type_from_server(
2428 (int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2429 iq->response, &iq->qchase, iq->dp);
2430 if(type == RESPONSE_TYPE_ANSWER) {
2431 qstate->return_rcode = LDNS_RCODE_NOERROR;
2432 qstate->return_msg = iq->response;
2434 qstate->return_rcode = LDNS_RCODE_SERVFAIL;
2435 qstate->return_msg = NULL;
2438 /* validate the root or stub after priming (if enabled).
2439 * This is the same query as the prime query, but with validation.
2440 * Now that we are primed, the additional queries that validation
2441 * may need can be resolved, such as DLV. */
2442 if(qstate->env->cfg->harden_referral_path) {
2443 struct module_qstate* subq = NULL;
2444 log_nametypeclass(VERB_ALGO, "schedule prime validation",
2445 qstate->qinfo.qname, qstate->qinfo.qtype,
2446 qstate->qinfo.qclass);
2447 if(!generate_sub_request(qstate->qinfo.qname,
2448 qstate->qinfo.qname_len, qstate->qinfo.qtype,
2449 qstate->qinfo.qclass, qstate, id, iq,
2450 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
2451 verbose(VERB_ALGO, "could not generate prime check");
2453 generate_a_aaaa_check(qstate, iq, id);
2456 /* This event is finished. */
2457 qstate->ext_state[id] = module_finished;
2462 * Do final processing on responses to target queries. Events reach this
2463 * state after the iterative resolution algorithm terminates. This state is
2464 * responsible for reactiving the original event, and housekeeping related
2465 * to received target responses (caching, updating the current delegation
2467 * Callback from walk_supers for every super state that is interested in
2468 * the results from this query.
2470 * @param qstate: query state.
2471 * @param id: module id.
2472 * @param forq: super query state.
2475 processTargetResponse(struct module_qstate* qstate, int id,
2476 struct module_qstate* forq)
2478 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2479 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2480 struct ub_packed_rrset_key* rrset;
2481 struct delegpt_ns* dpns;
2482 log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2484 foriq->state = QUERYTARGETS_STATE;
2485 log_query_info(VERB_ALGO, "processTargetResponse", &qstate->qinfo);
2486 log_query_info(VERB_ALGO, "processTargetResponse super", &forq->qinfo);
2488 /* check to see if parent event is still interested (in orig name). */
2490 verbose(VERB_ALGO, "subq: parent not interested, was reset");
2491 return; /* not interested anymore */
2493 dpns = delegpt_find_ns(foriq->dp, qstate->qinfo.qname,
2494 qstate->qinfo.qname_len);
2496 /* If not interested, just stop processing this event */
2497 verbose(VERB_ALGO, "subq: parent not interested anymore");
2498 /* could be because parent was jostled out of the cache,
2499 and a new identical query arrived, that does not want it*/
2503 /* Tell the originating event that this target query has finished
2504 * (regardless if it succeeded or not). */
2505 foriq->num_target_queries--;
2507 /* if iq->query_for_pside_glue then add the pside_glue (marked lame) */
2508 if(iq->pside_glue) {
2509 /* if the pside_glue is NULL, then it could not be found,
2510 * the done_pside is already set when created and a cache
2511 * entry created in processFinished so nothing to do here */
2512 log_rrset_key(VERB_ALGO, "add parentside glue to dp",
2514 if(!delegpt_add_rrset(foriq->dp, forq->region,
2516 log_err("out of memory adding pside glue");
2519 /* This response is relevant to the current query, so we
2520 * add (attempt to add, anyway) this target(s) and reactivate
2521 * the original event.
2522 * NOTE: we could only look for the AnswerRRset if the
2523 * response type was ANSWER. */
2524 rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
2526 /* if CNAMEs have been followed - add new NS to delegpt. */
2527 /* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
2528 if(!delegpt_find_ns(foriq->dp, rrset->rk.dname,
2529 rrset->rk.dname_len)) {
2530 /* if dpns->lame then set newcname ns lame too */
2531 if(!delegpt_add_ns(foriq->dp, forq->region,
2532 rrset->rk.dname, dpns->lame))
2533 log_err("out of memory adding cnamed-ns");
2535 /* if dpns->lame then set the address(es) lame too */
2536 if(!delegpt_add_rrset(foriq->dp, forq->region, rrset,
2538 log_err("out of memory adding targets");
2539 verbose(VERB_ALGO, "added target response");
2540 delegpt_log(VERB_ALGO, foriq->dp);
2542 verbose(VERB_ALGO, "iterator TargetResponse failed");
2543 dpns->resolved = 1; /* fail the target */
2548 * Process response for DS NS Find queries, that attempt to find the delegation
2549 * point where we ask the DS query from.
2551 * @param qstate: query state.
2552 * @param id: module id.
2553 * @param forq: super query state.
2556 processDSNSResponse(struct module_qstate* qstate, int id,
2557 struct module_qstate* forq)
2559 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2561 /* if the finished (iq->response) query has no NS set: continue
2562 * up to look for the right dp; nothing to change, do DPNSstate */
2563 if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2564 return; /* seek further */
2565 /* find the NS RRset (without allowing CNAMEs) */
2566 if(!reply_find_rrset(qstate->return_msg->rep, qstate->qinfo.qname,
2567 qstate->qinfo.qname_len, LDNS_RR_TYPE_NS,
2568 qstate->qinfo.qclass)){
2569 return; /* seek further */
2572 /* else, store as DP and continue at querytargets */
2573 foriq->state = QUERYTARGETS_STATE;
2574 foriq->dp = delegpt_from_message(qstate->return_msg, forq->region);
2576 log_err("out of memory in dsns dp alloc");
2577 return; /* dp==NULL in QUERYTARGETS makes SERVFAIL */
2579 /* success, go query the querytargets in the new dp (and go down) */
2583 * Process response for qclass=ANY queries for a particular class.
2584 * Append to result or error-exit.
2586 * @param qstate: query state.
2587 * @param id: module id.
2588 * @param forq: super query state.
2591 processClassResponse(struct module_qstate* qstate, int id,
2592 struct module_qstate* forq)
2594 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2595 struct dns_msg* from = qstate->return_msg;
2596 log_query_info(VERB_ALGO, "processClassResponse", &qstate->qinfo);
2597 log_query_info(VERB_ALGO, "processClassResponse super", &forq->qinfo);
2598 if(qstate->return_rcode != LDNS_RCODE_NOERROR) {
2599 /* cause servfail for qclass ANY query */
2600 foriq->response = NULL;
2601 foriq->state = FINISHED_STATE;
2605 if(!foriq->response) {
2606 /* allocate the response: copy RCODE, sec_state */
2607 foriq->response = dns_copy_msg(from, forq->region);
2608 if(!foriq->response) {
2609 log_err("malloc failed for qclass ANY response");
2610 foriq->state = FINISHED_STATE;
2613 foriq->response->qinfo.qclass = forq->qinfo.qclass;
2614 /* qclass ANY does not receive the AA flag on replies */
2615 foriq->response->rep->authoritative = 0;
2617 struct dns_msg* to = foriq->response;
2618 /* add _from_ this response _to_ existing collection */
2619 /* if there are records, copy RCODE */
2620 /* lower sec_state if this message is lower */
2621 if(from->rep->rrset_count != 0) {
2622 size_t n = from->rep->rrset_count+to->rep->rrset_count;
2623 struct ub_packed_rrset_key** dest, **d;
2624 /* copy appropriate rcode */
2625 to->rep->flags = from->rep->flags;
2627 if(from->rep->rrset_count > RR_COUNT_MAX ||
2628 to->rep->rrset_count > RR_COUNT_MAX) {
2629 log_err("malloc failed (too many rrsets) in collect ANY");
2630 foriq->state = FINISHED_STATE;
2631 return; /* integer overflow protection */
2633 dest = regional_alloc(forq->region, sizeof(dest[0])*n);
2635 log_err("malloc failed in collect ANY");
2636 foriq->state = FINISHED_STATE;
2641 memcpy(dest, to->rep->rrsets, to->rep->an_numrrsets
2643 dest += to->rep->an_numrrsets;
2644 memcpy(dest, from->rep->rrsets, from->rep->an_numrrsets
2646 dest += from->rep->an_numrrsets;
2648 memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets,
2649 to->rep->ns_numrrsets * sizeof(dest[0]));
2650 dest += to->rep->ns_numrrsets;
2651 memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets,
2652 from->rep->ns_numrrsets * sizeof(dest[0]));
2653 dest += from->rep->ns_numrrsets;
2655 memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets+
2656 to->rep->ns_numrrsets,
2657 to->rep->ar_numrrsets * sizeof(dest[0]));
2658 dest += to->rep->ar_numrrsets;
2659 memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets+
2660 from->rep->ns_numrrsets,
2661 from->rep->ar_numrrsets * sizeof(dest[0]));
2663 to->rep->rrsets = d;
2664 to->rep->an_numrrsets += from->rep->an_numrrsets;
2665 to->rep->ns_numrrsets += from->rep->ns_numrrsets;
2666 to->rep->ar_numrrsets += from->rep->ar_numrrsets;
2667 to->rep->rrset_count = n;
2669 if(from->rep->security < to->rep->security) /* lowest sec */
2670 to->rep->security = from->rep->security;
2671 if(from->rep->qdcount != 0) /* insert qd if appropriate */
2672 to->rep->qdcount = from->rep->qdcount;
2673 if(from->rep->ttl < to->rep->ttl) /* use smallest TTL */
2674 to->rep->ttl = from->rep->ttl;
2675 if(from->rep->prefetch_ttl < to->rep->prefetch_ttl)
2676 to->rep->prefetch_ttl = from->rep->prefetch_ttl;
2679 foriq->num_current_queries --;
2680 if(foriq->num_current_queries == 0)
2681 foriq->state = FINISHED_STATE;
2685 * Collect class ANY responses and make them into one response. This
2686 * state is started and it creates queries for all classes (that have
2687 * root hints). The answers are then collected.
2689 * @param qstate: query state.
2690 * @param id: module id.
2691 * @return true if the event needs more immediate processing, false if not.
2694 processCollectClass(struct module_qstate* qstate, int id)
2696 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2697 struct module_qstate* subq;
2698 /* If qchase.qclass == 0 then send out queries for all classes.
2699 * Otherwise, do nothing (wait for all answers to arrive and the
2700 * processClassResponse to put them together, and that moves us
2701 * towards the Finished state when done. */
2702 if(iq->qchase.qclass == 0) {
2704 iq->qchase.qclass = LDNS_RR_CLASS_ANY;
2705 while(iter_get_next_root(qstate->env->hints,
2706 qstate->env->fwds, &c)) {
2707 /* generate query for this class */
2708 log_nametypeclass(VERB_ALGO, "spawn collect query",
2709 qstate->qinfo.qname, qstate->qinfo.qtype, c);
2710 if(!generate_sub_request(qstate->qinfo.qname,
2711 qstate->qinfo.qname_len, qstate->qinfo.qtype,
2712 c, qstate, id, iq, INIT_REQUEST_STATE,
2713 FINISHED_STATE, &subq,
2714 (int)!(qstate->query_flags&BIT_CD))) {
2715 return error_response(qstate, id,
2716 LDNS_RCODE_SERVFAIL);
2718 /* ignore subq, no special init required */
2719 iq->num_current_queries ++;
2724 /* if no roots are configured at all, return */
2725 if(iq->num_current_queries == 0) {
2726 verbose(VERB_ALGO, "No root hints or fwds, giving up "
2728 return error_response(qstate, id, LDNS_RCODE_REFUSED);
2730 /* return false, wait for queries to return */
2732 /* if woke up here because of an answer, wait for more answers */
2737 * This handles the final state for first-tier responses (i.e., responses to
2738 * externally generated queries).
2740 * @param qstate: query state.
2741 * @param iq: iterator query state.
2742 * @param id: module id.
2743 * @return true if the event needs more processing, false if not. Since this
2744 * is the final state for an event, it always returns false.
2747 processFinished(struct module_qstate* qstate, struct iter_qstate* iq,
2750 log_query_info(VERB_QUERY, "finishing processing for",
2753 /* store negative cache element for parent side glue. */
2754 if(iq->query_for_pside_glue && !iq->pside_glue)
2755 iter_store_parentside_neg(qstate->env, &qstate->qinfo,
2756 iq->deleg_msg?iq->deleg_msg->rep:
2757 (iq->response?iq->response->rep:NULL));
2759 verbose(VERB_ALGO, "No response is set, servfail");
2760 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2763 /* Make sure that the RA flag is set (since the presence of
2764 * this module means that recursion is available) */
2765 iq->response->rep->flags |= BIT_RA;
2767 /* Clear the AA flag */
2768 /* FIXME: does this action go here or in some other module? */
2769 iq->response->rep->flags &= ~BIT_AA;
2771 /* make sure QR flag is on */
2772 iq->response->rep->flags |= BIT_QR;
2774 /* we have finished processing this query */
2775 qstate->ext_state[id] = module_finished;
2777 /* TODO: we are using a private TTL, trim the response. */
2778 /* if (mPrivateTTL > 0){IterUtils.setPrivateTTL(resp, mPrivateTTL); } */
2780 /* prepend any items we have accumulated */
2781 if(iq->an_prepend_list || iq->ns_prepend_list) {
2782 if(!iter_prepend(iq, iq->response, qstate->region)) {
2783 log_err("prepend rrsets: out of memory");
2784 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2786 /* reset the query name back */
2787 iq->response->qinfo = qstate->qinfo;
2788 /* the security state depends on the combination */
2789 iq->response->rep->security = sec_status_unchecked;
2790 /* store message with the finished prepended items,
2791 * but only if we did recursion. The nonrecursion referral
2792 * from cache does not need to be stored in the msg cache. */
2793 if(qstate->query_flags&BIT_RD) {
2794 iter_dns_store(qstate->env, &qstate->qinfo,
2795 iq->response->rep, 0, qstate->prefetch_leeway,
2796 iq->dp&&iq->dp->has_parent_side_NS,
2797 qstate->region, qstate->query_flags);
2800 qstate->return_rcode = LDNS_RCODE_NOERROR;
2801 qstate->return_msg = iq->response;
2806 * Return priming query results to interestes super querystates.
2808 * Sets the delegation point and delegation message (not nonRD queries).
2809 * This is a callback from walk_supers.
2811 * @param qstate: query state that finished.
2812 * @param id: module id.
2813 * @param super: the qstate to inform.
2816 iter_inform_super(struct module_qstate* qstate, int id,
2817 struct module_qstate* super)
2819 if(!qstate->is_priming && super->qinfo.qclass == LDNS_RR_CLASS_ANY)
2820 processClassResponse(qstate, id, super);
2821 else if(super->qinfo.qtype == LDNS_RR_TYPE_DS && ((struct iter_qstate*)
2822 super->minfo[id])->state == DSNS_FIND_STATE)
2823 processDSNSResponse(qstate, id, super);
2824 else if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2825 error_supers(qstate, id, super);
2826 else if(qstate->is_priming)
2827 prime_supers(qstate, id, super);
2828 else processTargetResponse(qstate, id, super);
2832 * Handle iterator state.
2833 * Handle events. This is the real processing loop for events, responsible
2834 * for moving events through the various states. If a processing method
2835 * returns true, then it will be advanced to the next state. If false, then
2836 * processing will stop.
2838 * @param qstate: query state.
2839 * @param ie: iterator shared global environment.
2840 * @param iq: iterator query state.
2841 * @param id: module id.
2844 iter_handle(struct module_qstate* qstate, struct iter_qstate* iq,
2845 struct iter_env* ie, int id)
2849 verbose(VERB_ALGO, "iter_handle processing q with state %s",
2850 iter_state_to_string(iq->state));
2852 case INIT_REQUEST_STATE:
2853 cont = processInitRequest(qstate, iq, ie, id);
2855 case INIT_REQUEST_2_STATE:
2856 cont = processInitRequest2(qstate, iq, id);
2858 case INIT_REQUEST_3_STATE:
2859 cont = processInitRequest3(qstate, iq, id);
2861 case QUERYTARGETS_STATE:
2862 cont = processQueryTargets(qstate, iq, ie, id);
2864 case QUERY_RESP_STATE:
2865 cont = processQueryResponse(qstate, iq, id);
2867 case PRIME_RESP_STATE:
2868 cont = processPrimeResponse(qstate, id);
2870 case COLLECT_CLASS_STATE:
2871 cont = processCollectClass(qstate, id);
2873 case DSNS_FIND_STATE:
2874 cont = processDSNSFind(qstate, iq, id);
2876 case FINISHED_STATE:
2877 cont = processFinished(qstate, iq, id);
2880 log_warn("iterator: invalid state: %d",
2889 * This is the primary entry point for processing request events. Note that
2890 * this method should only be used by external modules.
2891 * @param qstate: query state.
2892 * @param ie: iterator shared global environment.
2893 * @param iq: iterator query state.
2894 * @param id: module id.
2897 process_request(struct module_qstate* qstate, struct iter_qstate* iq,
2898 struct iter_env* ie, int id)
2900 /* external requests start in the INIT state, and finish using the
2901 * FINISHED state. */
2902 iq->state = INIT_REQUEST_STATE;
2903 iq->final_state = FINISHED_STATE;
2904 verbose(VERB_ALGO, "process_request: new external request event");
2905 iter_handle(qstate, iq, ie, id);
2908 /** process authoritative server reply */
2910 process_response(struct module_qstate* qstate, struct iter_qstate* iq,
2911 struct iter_env* ie, int id, struct outbound_entry* outbound,
2912 enum module_ev event)
2914 struct msg_parse* prs;
2915 struct edns_data edns;
2918 verbose(VERB_ALGO, "process_response: new external response event");
2919 iq->response = NULL;
2920 iq->state = QUERY_RESP_STATE;
2921 if(event == module_event_noreply || event == module_event_error) {
2922 if(event == module_event_noreply && iq->sent_count >= 3 &&
2923 qstate->env->cfg->use_caps_bits_for_id &&
2924 !iq->caps_fallback) {
2925 /* start fallback */
2926 iq->caps_fallback = 1;
2927 iq->caps_server = 0;
2928 iq->caps_reply = NULL;
2929 iq->caps_response = NULL;
2930 iq->state = QUERYTARGETS_STATE;
2931 iq->num_current_queries--;
2932 /* need fresh attempts for the 0x20 fallback, if
2933 * that was the cause for the failure */
2934 iter_dec_attempts(iq->dp, 3);
2935 verbose(VERB_DETAIL, "Capsforid: timeouts, starting fallback");
2940 if( (event != module_event_reply && event != module_event_capsfail)
2941 || !qstate->reply) {
2942 log_err("Bad event combined with response");
2943 outbound_list_remove(&iq->outlist, outbound);
2944 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2949 prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
2950 sizeof(struct msg_parse));
2952 log_err("out of memory on incoming message");
2953 /* like packet got dropped */
2956 memset(prs, 0, sizeof(*prs));
2957 memset(&edns, 0, sizeof(edns));
2958 pkt = qstate->reply->c->buffer;
2959 sldns_buffer_set_position(pkt, 0);
2960 if(parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
2961 verbose(VERB_ALGO, "parse error on reply packet");
2964 /* edns is not examined, but removed from message to help cache */
2965 if(parse_extract_edns(prs, &edns) != LDNS_RCODE_NOERROR)
2967 /* remove CD-bit, we asked for in case we handle validation ourself */
2968 prs->flags &= ~BIT_CD;
2970 /* normalize and sanitize: easy to delete items from linked lists */
2971 if(!scrub_message(pkt, prs, &iq->qchase, iq->dp->name,
2972 qstate->env->scratch, qstate->env, ie)) {
2973 /* if 0x20 enabled, start fallback, but we have no message */
2974 if(event == module_event_capsfail && !iq->caps_fallback) {
2975 iq->caps_fallback = 1;
2976 iq->caps_server = 0;
2977 iq->caps_reply = NULL;
2978 iq->caps_response = NULL;
2979 iq->state = QUERYTARGETS_STATE;
2980 iq->num_current_queries--;
2981 verbose(VERB_DETAIL, "Capsforid: scrub failed, starting fallback with no response");
2986 /* allocate response dns_msg in region */
2987 iq->response = dns_alloc_msg(pkt, prs, qstate->region);
2990 log_query_info(VERB_DETAIL, "response for", &qstate->qinfo);
2991 log_name_addr(VERB_DETAIL, "reply from", iq->dp->name,
2992 &qstate->reply->addr, qstate->reply->addrlen);
2993 if(verbosity >= VERB_ALGO)
2994 log_dns_msg("incoming scrubbed packet:", &iq->response->qinfo,
2997 if(event == module_event_capsfail || iq->caps_fallback) {
2998 /* for fallback we care about main answer, not additionals */
2999 /* removing that makes comparison more likely to succeed */
3000 caps_strip_reply(iq->response->rep);
3001 if(!iq->caps_fallback) {
3002 /* start fallback */
3003 iq->caps_fallback = 1;
3004 iq->caps_server = 0;
3005 iq->caps_reply = iq->response->rep;
3006 iq->caps_response = iq->response;
3007 iq->state = QUERYTARGETS_STATE;
3008 iq->num_current_queries--;
3009 verbose(VERB_DETAIL, "Capsforid: starting fallback");
3012 /* check if reply is the same, otherwise, fail */
3013 if(!iq->caps_reply) {
3014 iq->caps_reply = iq->response->rep;
3015 iq->caps_response = iq->response;
3016 iq->caps_server = -1; /*become zero at ++,
3017 so that we start the full set of trials */
3018 } else if(caps_failed_rcode(iq->caps_reply) &&
3019 !caps_failed_rcode(iq->response->rep)) {
3020 /* prefer to upgrade to non-SERVFAIL */
3021 iq->caps_reply = iq->response->rep;
3022 iq->caps_response = iq->response;
3023 } else if(!caps_failed_rcode(iq->caps_reply) &&
3024 caps_failed_rcode(iq->response->rep)) {
3025 /* if we have non-SERVFAIL as answer then
3026 * we can ignore SERVFAILs for the equality
3028 /* no instructions here, skip other else */
3029 } else if(caps_failed_rcode(iq->caps_reply) &&
3030 caps_failed_rcode(iq->response->rep)) {
3031 /* failure is same as other failure in fallbk*/
3032 /* no instructions here, skip other else */
3033 } else if(!reply_equal(iq->response->rep, iq->caps_reply,
3034 qstate->env->scratch)) {
3035 verbose(VERB_DETAIL, "Capsforid fallback: "
3036 "getting different replies, failed");
3037 outbound_list_remove(&iq->outlist, outbound);
3038 (void)error_response(qstate, id,
3039 LDNS_RCODE_SERVFAIL);
3042 /* continue the fallback procedure at next server */
3044 iq->state = QUERYTARGETS_STATE;
3045 iq->num_current_queries--;
3046 verbose(VERB_DETAIL, "Capsforid: reply is equal. "
3047 "go to next fallback");
3051 iq->caps_fallback = 0; /* if we were in fallback, 0x20 is OK now */
3054 outbound_list_remove(&iq->outlist, outbound);
3055 iter_handle(qstate, iq, ie, id);
3059 iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
3060 struct outbound_entry* outbound)
3062 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
3063 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3064 verbose(VERB_QUERY, "iterator[module %d] operate: extstate:%s event:%s",
3065 id, strextstate(qstate->ext_state[id]), strmodulevent(event));
3066 if(iq) log_query_info(VERB_QUERY, "iterator operate: query",
3068 if(iq && qstate->qinfo.qname != iq->qchase.qname)
3069 log_query_info(VERB_QUERY, "iterator operate: chased to",
3072 /* perform iterator state machine */
3073 if((event == module_event_new || event == module_event_pass) &&
3075 if(!iter_new(qstate, id)) {
3076 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3079 iq = (struct iter_qstate*)qstate->minfo[id];
3080 process_request(qstate, iq, ie, id);
3083 if(iq && event == module_event_pass) {
3084 iter_handle(qstate, iq, ie, id);
3087 if(iq && outbound) {
3088 process_response(qstate, iq, ie, id, outbound, event);
3091 if(event == module_event_error) {
3092 verbose(VERB_ALGO, "got called with event error, giving up");
3093 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3097 log_err("bad event for iterator");
3098 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3102 iter_clear(struct module_qstate* qstate, int id)
3104 struct iter_qstate* iq;
3107 iq = (struct iter_qstate*)qstate->minfo[id];
3109 outbound_list_clear(&iq->outlist);
3110 if(iq->target_count && --iq->target_count[0] == 0)
3111 free(iq->target_count);
3112 iq->num_current_queries = 0;
3114 qstate->minfo[id] = NULL;
3118 iter_get_mem(struct module_env* env, int id)
3120 struct iter_env* ie = (struct iter_env*)env->modinfo[id];
3123 return sizeof(*ie) + sizeof(int)*((size_t)ie->max_dependency_depth+1)
3124 + donotq_get_mem(ie->donotq) + priv_get_mem(ie->priv);
3128 * The iterator function block
3130 static struct module_func_block iter_block = {
3132 &iter_init, &iter_deinit, &iter_operate, &iter_inform_super,
3133 &iter_clear, &iter_get_mem
3136 struct module_func_block*
3137 iter_get_funcblock(void)
3143 iter_state_to_string(enum iter_state state)
3147 case INIT_REQUEST_STATE :
3148 return "INIT REQUEST STATE";
3149 case INIT_REQUEST_2_STATE :
3150 return "INIT REQUEST STATE (stage 2)";
3151 case INIT_REQUEST_3_STATE:
3152 return "INIT REQUEST STATE (stage 3)";
3153 case QUERYTARGETS_STATE :
3154 return "QUERY TARGETS STATE";
3155 case PRIME_RESP_STATE :
3156 return "PRIME RESPONSE STATE";
3157 case COLLECT_CLASS_STATE :
3158 return "COLLECT CLASS STATE";
3159 case DSNS_FIND_STATE :
3160 return "DSNS FIND STATE";
3161 case QUERY_RESP_STATE :
3162 return "QUERY RESPONSE STATE";
3163 case FINISHED_STATE :
3164 return "FINISHED RESPONSE STATE";
3166 return "UNKNOWN ITER STATE";
3171 iter_state_is_responsestate(enum iter_state s)
3174 case INIT_REQUEST_STATE :
3175 case INIT_REQUEST_2_STATE :
3176 case INIT_REQUEST_3_STATE :
3177 case QUERYTARGETS_STATE :
3178 case COLLECT_CLASS_STATE :