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 "ldns/rrdef.h"
65 #include "ldns/wire2str.h"
66 #include "ldns/parseutil.h"
67 #include "ldns/sbuffer.h"
70 iter_init(struct module_env* env, int id)
72 struct iter_env* iter_env = (struct iter_env*)calloc(1,
73 sizeof(struct iter_env));
75 log_err("malloc failure");
78 env->modinfo[id] = (void*)iter_env;
79 if(!iter_apply_cfg(iter_env, env->cfg)) {
80 log_err("iterator: could not apply configuration settings.");
87 iter_deinit(struct module_env* env, int id)
89 struct iter_env* iter_env;
90 if(!env || !env->modinfo[id])
92 iter_env = (struct iter_env*)env->modinfo[id];
93 free(iter_env->target_fetch_policy);
94 priv_delete(iter_env->priv);
95 donotq_delete(iter_env->donotq);
97 env->modinfo[id] = NULL;
100 /** new query for iterator */
102 iter_new(struct module_qstate* qstate, int id)
104 struct iter_qstate* iq = (struct iter_qstate*)regional_alloc(
105 qstate->region, sizeof(struct iter_qstate));
106 qstate->minfo[id] = iq;
109 memset(iq, 0, sizeof(*iq));
110 iq->state = INIT_REQUEST_STATE;
111 iq->final_state = FINISHED_STATE;
112 iq->an_prepend_list = NULL;
113 iq->an_prepend_last = NULL;
114 iq->ns_prepend_list = NULL;
115 iq->ns_prepend_last = NULL;
118 iq->num_target_queries = 0;
119 iq->num_current_queries = 0;
120 iq->query_restart_count = 0;
121 iq->referral_count = 0;
123 iq->target_count = NULL;
124 iq->wait_priming_stub = 0;
125 iq->refetch_glue = 0;
126 iq->dnssec_expected = 0;
127 iq->dnssec_lame_query = 0;
128 iq->chase_flags = qstate->query_flags;
129 /* Start with the (current) qname. */
130 iq->qchase = qstate->qinfo;
131 outbound_list_init(&iq->outlist);
136 * Transition to the next state. This can be used to advance a currently
137 * processing event. It cannot be used to reactivate a forEvent.
139 * @param iq: iterator query state
140 * @param nextstate The state to transition to.
141 * @return true. This is so this can be called as the return value for the
142 * actual process*State() methods. (Transitioning to the next state
143 * implies further processing).
146 next_state(struct iter_qstate* iq, enum iter_state nextstate)
148 /* If transitioning to a "response" state, make sure that there is a
150 if(iter_state_is_responsestate(nextstate)) {
151 if(iq->response == NULL) {
152 log_err("transitioning to response state sans "
156 iq->state = nextstate;
161 * Transition an event to its final state. Final states always either return
162 * a result up the module chain, or reactivate a dependent event. Which
163 * final state to transtion to is set in the module state for the event when
164 * it was created, and depends on the original purpose of the event.
166 * The response is stored in the qstate->buf buffer.
168 * @param iq: iterator query state
169 * @return false. This is so this method can be used as the return value for
170 * the processState methods. (Transitioning to the final state
173 final_state(struct iter_qstate* iq)
175 return next_state(iq, iq->final_state);
179 * Callback routine to handle errors in parent query states
180 * @param qstate: query state that failed.
181 * @param id: module id.
182 * @param super: super state.
185 error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
187 struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
189 if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
190 qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
191 /* mark address as failed. */
192 struct delegpt_ns* dpns = NULL;
194 dpns = delegpt_find_ns(super_iq->dp,
195 qstate->qinfo.qname, qstate->qinfo.qname_len);
198 verbose(VERB_ALGO, "subq error, but not interested");
199 log_query_info(VERB_ALGO, "superq", &super->qinfo);
201 delegpt_log(VERB_ALGO, super_iq->dp);
205 /* see if the failure did get (parent-lame) info */
206 if(!cache_fill_missing(super->env,
207 super_iq->qchase.qclass, super->region,
209 log_err("out of memory adding missing");
211 dpns->resolved = 1; /* mark as failed */
212 super_iq->num_target_queries--;
214 if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
215 /* prime failed to get delegation */
218 /* evaluate targets again */
219 super_iq->state = QUERYTARGETS_STATE;
220 /* super becomes runnable, and will process this change */
224 * Return an error to the client
225 * @param qstate: our query state
226 * @param id: module id
227 * @param rcode: error code (DNS errcode).
228 * @return: 0 for use by caller, to make notation easy, like:
229 * return error_response(..).
232 error_response(struct module_qstate* qstate, int id, int rcode)
234 verbose(VERB_QUERY, "return error response %s",
235 sldns_lookup_by_id(sldns_rcodes, rcode)?
236 sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
237 qstate->return_rcode = rcode;
238 qstate->return_msg = NULL;
239 qstate->ext_state[id] = module_finished;
244 * Return an error to the client and cache the error code in the
245 * message cache (so per qname, qtype, qclass).
246 * @param qstate: our query state
247 * @param id: module id
248 * @param rcode: error code (DNS errcode).
249 * @return: 0 for use by caller, to make notation easy, like:
250 * return error_response(..).
253 error_response_cache(struct module_qstate* qstate, int id, int rcode)
256 struct reply_info err;
257 if(qstate->prefetch_leeway > NORR_TTL) {
258 verbose(VERB_ALGO, "error response for prefetch in cache");
259 /* attempt to adjust the cache entry prefetch */
260 if(dns_cache_prefetch_adjust(qstate->env, &qstate->qinfo,
261 NORR_TTL, qstate->query_flags))
262 return error_response(qstate, id, rcode);
263 /* if that fails (not in cache), fall through to store err */
265 memset(&err, 0, sizeof(err));
266 err.flags = (uint16_t)(BIT_QR | BIT_RA);
267 FLAGS_SET_RCODE(err.flags, rcode);
270 err.prefetch_ttl = PREFETCH_TTL_CALC(err.ttl);
271 /* do not waste time trying to validate this servfail */
272 err.security = sec_status_indeterminate;
273 verbose(VERB_ALGO, "store error response in message cache");
274 iter_dns_store(qstate->env, &qstate->qinfo, &err, 0, 0, 0, NULL,
275 qstate->query_flags);
276 return error_response(qstate, id, rcode);
279 /** check if prepend item is duplicate item */
281 prepend_is_duplicate(struct ub_packed_rrset_key** sets, size_t to,
282 struct ub_packed_rrset_key* dup)
285 for(i=0; i<to; i++) {
286 if(sets[i]->rk.type == dup->rk.type &&
287 sets[i]->rk.rrset_class == dup->rk.rrset_class &&
288 sets[i]->rk.dname_len == dup->rk.dname_len &&
289 query_dname_compare(sets[i]->rk.dname, dup->rk.dname)
296 /** prepend the prepend list in the answer and authority section of dns_msg */
298 iter_prepend(struct iter_qstate* iq, struct dns_msg* msg,
299 struct regional* region)
301 struct iter_prep_list* p;
302 struct ub_packed_rrset_key** sets;
303 size_t num_an = 0, num_ns = 0;;
304 for(p = iq->an_prepend_list; p; p = p->next)
306 for(p = iq->ns_prepend_list; p; p = p->next)
308 if(num_an + num_ns == 0)
310 verbose(VERB_ALGO, "prepending %d rrsets", (int)num_an + (int)num_ns);
311 sets = regional_alloc(region, (num_an+num_ns+msg->rep->rrset_count) *
312 sizeof(struct ub_packed_rrset_key*));
317 for(p = iq->an_prepend_list; p; p = p->next) {
318 sets[num_an++] = p->rrset;
320 memcpy(sets+num_an, msg->rep->rrsets, msg->rep->an_numrrsets *
321 sizeof(struct ub_packed_rrset_key*));
324 for(p = iq->ns_prepend_list; p; p = p->next) {
325 if(prepend_is_duplicate(sets+msg->rep->an_numrrsets+num_an,
326 num_ns, p->rrset) || prepend_is_duplicate(
327 msg->rep->rrsets+msg->rep->an_numrrsets,
328 msg->rep->ns_numrrsets, p->rrset))
330 sets[msg->rep->an_numrrsets + num_an + num_ns++] = p->rrset;
332 memcpy(sets + num_an + msg->rep->an_numrrsets + num_ns,
333 msg->rep->rrsets + msg->rep->an_numrrsets,
334 (msg->rep->ns_numrrsets + msg->rep->ar_numrrsets) *
335 sizeof(struct ub_packed_rrset_key*));
337 /* NXDOMAIN rcode can stay if we prepended DNAME/CNAMEs, because
338 * this is what recursors should give. */
339 msg->rep->rrset_count += num_an + num_ns;
340 msg->rep->an_numrrsets += num_an;
341 msg->rep->ns_numrrsets += num_ns;
342 msg->rep->rrsets = sets;
347 * Add rrset to ANSWER prepend list
348 * @param qstate: query state.
349 * @param iq: iterator query state.
350 * @param rrset: rrset to add.
351 * @return false on failure (malloc).
354 iter_add_prepend_answer(struct module_qstate* qstate, struct iter_qstate* iq,
355 struct ub_packed_rrset_key* rrset)
357 struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
358 qstate->region, sizeof(struct iter_prep_list));
364 if(iq->an_prepend_last)
365 iq->an_prepend_last->next = p;
366 else iq->an_prepend_list = p;
367 iq->an_prepend_last = p;
372 * Add rrset to AUTHORITY prepend list
373 * @param qstate: query state.
374 * @param iq: iterator query state.
375 * @param rrset: rrset to add.
376 * @return false on failure (malloc).
379 iter_add_prepend_auth(struct module_qstate* qstate, struct iter_qstate* iq,
380 struct ub_packed_rrset_key* rrset)
382 struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
383 qstate->region, sizeof(struct iter_prep_list));
389 if(iq->ns_prepend_last)
390 iq->ns_prepend_last->next = p;
391 else iq->ns_prepend_list = p;
392 iq->ns_prepend_last = p;
397 * Given a CNAME response (defined as a response containing a CNAME or DNAME
398 * that does not answer the request), process the response, modifying the
399 * state as necessary. This follows the CNAME/DNAME chain and returns the
402 * sets the new query name, after following the CNAME/DNAME chain.
403 * @param qstate: query state.
404 * @param iq: iterator query state.
405 * @param msg: the response.
406 * @param mname: returned target new query name.
407 * @param mname_len: length of mname.
408 * @return false on (malloc) error.
411 handle_cname_response(struct module_qstate* qstate, struct iter_qstate* iq,
412 struct dns_msg* msg, uint8_t** mname, size_t* mname_len)
415 /* Start with the (current) qname. */
416 *mname = iq->qchase.qname;
417 *mname_len = iq->qchase.qname_len;
419 /* Iterate over the ANSWER rrsets in order, looking for CNAMEs and
421 for(i=0; i<msg->rep->an_numrrsets; i++) {
422 struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
423 /* If there is a (relevant) DNAME, add it to the list.
424 * We always expect there to be CNAME that was generated
425 * by this DNAME following, so we don't process the DNAME
427 if(ntohs(r->rk.type) == LDNS_RR_TYPE_DNAME &&
428 dname_strict_subdomain_c(*mname, r->rk.dname)) {
429 if(!iter_add_prepend_answer(qstate, iq, r))
434 if(ntohs(r->rk.type) == LDNS_RR_TYPE_CNAME &&
435 query_dname_compare(*mname, r->rk.dname) == 0) {
436 /* Add this relevant CNAME rrset to the prepend list.*/
437 if(!iter_add_prepend_answer(qstate, iq, r))
439 get_cname_target(r, mname, mname_len);
442 /* Other rrsets in the section are ignored. */
444 /* add authority rrsets to authority prepend, for wildcarded CNAMEs */
445 for(i=msg->rep->an_numrrsets; i<msg->rep->an_numrrsets +
446 msg->rep->ns_numrrsets; i++) {
447 struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
448 /* only add NSEC/NSEC3, as they may be needed for validation */
449 if(ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC ||
450 ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC3) {
451 if(!iter_add_prepend_auth(qstate, iq, r))
458 /** create target count structure for this query */
460 target_count_create(struct iter_qstate* iq)
462 if(!iq->target_count) {
463 iq->target_count = (int*)calloc(2, sizeof(int));
464 /* if calloc fails we simply do not track this number */
466 iq->target_count[0] = 1;
471 target_count_increase(struct iter_qstate* iq, int num)
473 target_count_create(iq);
475 iq->target_count[1] += num;
479 * Generate a subrequest.
480 * Generate a local request event. Local events are tied to this module, and
481 * have a correponding (first tier) event that is waiting for this event to
482 * resolve to continue.
484 * @param qname The query name for this request.
485 * @param qnamelen length of qname
486 * @param qtype The query type for this request.
487 * @param qclass The query class for this request.
488 * @param qstate The event that is generating this event.
489 * @param id: module id.
490 * @param iq: The iterator state that is generating this event.
491 * @param initial_state The initial response state (normally this
492 * is QUERY_RESP_STATE, unless it is known that the request won't
493 * need iterative processing
494 * @param finalstate The final state for the response to this request.
495 * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
496 * not need initialisation.
497 * @param v: if true, validation is done on the subquery.
498 * @return false on error (malloc).
501 generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
502 uint16_t qclass, struct module_qstate* qstate, int id,
503 struct iter_qstate* iq, enum iter_state initial_state,
504 enum iter_state finalstate, struct module_qstate** subq_ret, int v)
506 struct module_qstate* subq = NULL;
507 struct iter_qstate* subiq = NULL;
508 uint16_t qflags = 0; /* OPCODE QUERY, no flags */
509 struct query_info qinf;
510 int prime = (finalstate == PRIME_RESP_STATE)?1:0;
513 qinf.qname_len = qnamelen;
515 qinf.qclass = qclass;
517 /* RD should be set only when sending the query back through the INIT
519 if(initial_state == INIT_REQUEST_STATE)
521 /* We set the CD flag so we can send this through the "head" of
522 * the resolution chain, which might have a validator. We are
523 * uninterested in validating things not on the direct resolution
530 /* attach subquery, lookup existing or make a new one */
531 fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
532 if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime, valrec,
538 /* initialise the new subquery */
540 subq->ext_state[id] = module_state_initial;
541 subq->minfo[id] = regional_alloc(subq->region,
542 sizeof(struct iter_qstate));
543 if(!subq->minfo[id]) {
544 log_err("init subq: out of memory");
545 fptr_ok(fptr_whitelist_modenv_kill_sub(
546 qstate->env->kill_sub));
547 (*qstate->env->kill_sub)(subq);
550 subiq = (struct iter_qstate*)subq->minfo[id];
551 memset(subiq, 0, sizeof(*subiq));
552 subiq->num_target_queries = 0;
553 target_count_create(iq);
554 subiq->target_count = iq->target_count;
556 iq->target_count[0] ++; /* extra reference */
557 subiq->num_current_queries = 0;
558 subiq->depth = iq->depth+1;
559 outbound_list_init(&subiq->outlist);
560 subiq->state = initial_state;
561 subiq->final_state = finalstate;
562 subiq->qchase = subq->qinfo;
563 subiq->chase_flags = subq->query_flags;
564 subiq->refetch_glue = 0;
570 * Generate and send a root priming request.
571 * @param qstate: the qtstate that triggered the need to prime.
572 * @param iq: iterator query state.
573 * @param id: module id.
574 * @param qclass: the class to prime.
575 * @return 0 on failure
578 prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
582 struct module_qstate* subq;
583 verbose(VERB_DETAIL, "priming . %s NS",
584 sldns_lookup_by_id(sldns_rr_classes, (int)qclass)?
585 sldns_lookup_by_id(sldns_rr_classes, (int)qclass)->name:"??");
586 dp = hints_lookup_root(qstate->env->hints, qclass);
588 verbose(VERB_ALGO, "Cannot prime due to lack of hints");
591 /* Priming requests start at the QUERYTARGETS state, skipping
592 * the normal INIT state logic (which would cause an infloop). */
593 if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS,
594 qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
596 verbose(VERB_ALGO, "could not prime root");
600 struct iter_qstate* subiq =
601 (struct iter_qstate*)subq->minfo[id];
602 /* Set the initial delegation point to the hint.
603 * copy dp, it is now part of the root prime query.
604 * dp was part of in the fixed hints structure. */
605 subiq->dp = delegpt_copy(dp, subq->region);
607 log_err("out of memory priming root, copydp");
608 fptr_ok(fptr_whitelist_modenv_kill_sub(
609 qstate->env->kill_sub));
610 (*qstate->env->kill_sub)(subq);
613 /* there should not be any target queries. */
614 subiq->num_target_queries = 0;
615 subiq->dnssec_expected = iter_indicates_dnssec(
616 qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
619 /* this module stops, our submodule starts, and does the query. */
620 qstate->ext_state[id] = module_wait_subquery;
625 * Generate and process a stub priming request. This method tests for the
626 * need to prime a stub zone, so it is safe to call for every request.
628 * @param qstate: the qtstate that triggered the need to prime.
629 * @param iq: iterator query state.
630 * @param id: module id.
631 * @param qname: request name.
632 * @param qclass: request class.
633 * @return true if a priming subrequest was made, false if not. The will only
634 * issue a priming request if it detects an unprimed stub.
635 * Uses value of 2 to signal during stub-prime in root-prime situation
636 * that a noprime-stub is available and resolution can continue.
639 prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
640 uint8_t* qname, uint16_t qclass)
642 /* Lookup the stub hint. This will return null if the stub doesn't
643 * need to be re-primed. */
644 struct iter_hints_stub* stub;
645 struct delegpt* stub_dp;
646 struct module_qstate* subq;
649 stub = hints_lookup_stub(qstate->env->hints, qname, qclass, iq->dp);
650 /* The stub (if there is one) does not need priming. */
655 /* is it a noprime stub (always use) */
658 if(iq->dp == NULL) r = 2;
659 /* copy the dp out of the fixed hints structure, so that
660 * it can be changed when servicing this query */
661 iq->dp = delegpt_copy(stub_dp, qstate->region);
663 log_err("out of memory priming stub");
664 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
665 return 1; /* return 1 to make module stop, with error */
667 log_nametypeclass(VERB_DETAIL, "use stub", stub_dp->name,
668 LDNS_RR_TYPE_NS, qclass);
672 /* Otherwise, we need to (re)prime the stub. */
673 log_nametypeclass(VERB_DETAIL, "priming stub", stub_dp->name,
674 LDNS_RR_TYPE_NS, qclass);
676 /* Stub priming events start at the QUERYTARGETS state to avoid the
677 * redundant INIT state processing. */
678 if(!generate_sub_request(stub_dp->name, stub_dp->namelen,
679 LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
680 QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0)) {
681 verbose(VERB_ALGO, "could not prime stub");
682 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
683 return 1; /* return 1 to make module stop, with error */
686 struct iter_qstate* subiq =
687 (struct iter_qstate*)subq->minfo[id];
689 /* Set the initial delegation point to the hint. */
690 /* make copy to avoid use of stub dp by different qs/threads */
691 subiq->dp = delegpt_copy(stub_dp, subq->region);
693 log_err("out of memory priming stub, copydp");
694 fptr_ok(fptr_whitelist_modenv_kill_sub(
695 qstate->env->kill_sub));
696 (*qstate->env->kill_sub)(subq);
697 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
698 return 1; /* return 1 to make module stop, with error */
700 /* there should not be any target queries -- although there
701 * wouldn't be anyway, since stub hints never have
702 * missing targets. */
703 subiq->num_target_queries = 0;
704 subiq->wait_priming_stub = 1;
705 subiq->dnssec_expected = iter_indicates_dnssec(
706 qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
709 /* this module stops, our submodule starts, and does the query. */
710 qstate->ext_state[id] = module_wait_subquery;
715 * Generate A and AAAA checks for glue that is in-zone for the referral
716 * we just got to obtain authoritative information on the adresses.
718 * @param qstate: the qtstate that triggered the need to prime.
719 * @param iq: iterator query state.
720 * @param id: module id.
723 generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
726 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
727 struct module_qstate* subq;
729 struct reply_info* rep = iq->response->rep;
730 struct ub_packed_rrset_key* s;
733 if(iq->depth == ie->max_dependency_depth)
735 /* walk through additional, and check if in-zone,
736 * only relevant A, AAAA are left after scrub anyway */
737 for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
739 /* check *ALL* addresses that are transmitted in additional*/
740 /* is it an address ? */
741 if( !(ntohs(s->rk.type)==LDNS_RR_TYPE_A ||
742 ntohs(s->rk.type)==LDNS_RR_TYPE_AAAA)) {
745 /* is this query the same as the A/AAAA check for it */
746 if(qstate->qinfo.qtype == ntohs(s->rk.type) &&
747 qstate->qinfo.qclass == ntohs(s->rk.rrset_class) &&
748 query_dname_compare(qstate->qinfo.qname,
750 (qstate->query_flags&BIT_RD) &&
751 !(qstate->query_flags&BIT_CD))
754 /* generate subrequest for it */
755 log_nametypeclass(VERB_ALGO, "schedule addr fetch",
756 s->rk.dname, ntohs(s->rk.type),
757 ntohs(s->rk.rrset_class));
758 if(!generate_sub_request(s->rk.dname, s->rk.dname_len,
759 ntohs(s->rk.type), ntohs(s->rk.rrset_class),
761 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
762 verbose(VERB_ALGO, "could not generate addr check");
765 /* ignore subq - not need for more init */
770 * Generate a NS check request to obtain authoritative information
773 * @param qstate: the qtstate that triggered the need to prime.
774 * @param iq: iterator query state.
775 * @param id: module id.
778 generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
780 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
781 struct module_qstate* subq;
784 if(iq->depth == ie->max_dependency_depth)
786 /* is this query the same as the nscheck? */
787 if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS &&
788 query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
789 (qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
790 /* spawn off A, AAAA queries for in-zone glue to check */
791 generate_a_aaaa_check(qstate, iq, id);
795 log_nametypeclass(VERB_ALGO, "schedule ns fetch",
796 iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
797 if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
798 LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
799 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
800 verbose(VERB_ALGO, "could not generate ns check");
804 struct iter_qstate* subiq =
805 (struct iter_qstate*)subq->minfo[id];
807 /* make copy to avoid use of stub dp by different qs/threads */
808 /* refetch glue to start higher up the tree */
809 subiq->refetch_glue = 1;
810 subiq->dp = delegpt_copy(iq->dp, subq->region);
812 log_err("out of memory generating ns check, copydp");
813 fptr_ok(fptr_whitelist_modenv_kill_sub(
814 qstate->env->kill_sub));
815 (*qstate->env->kill_sub)(subq);
822 * Generate a DNSKEY prefetch query to get the DNSKEY for the DS record we
823 * just got in a referral (where we have dnssec_expected, thus have trust
824 * anchors above it). Note that right after calling this routine the
825 * iterator detached subqueries (because of following the referral), and thus
826 * the DNSKEY query becomes detached, its return stored in the cache for
827 * later lookup by the validator. This cache lookup by the validator avoids
828 * the roundtrip incurred by the DNSKEY query. The DNSKEY query is now
829 * performed at about the same time the original query is sent to the domain,
830 * thus the two answers are likely to be returned at about the same time,
831 * saving a roundtrip from the validated lookup.
833 * @param qstate: the qtstate that triggered the need to prime.
834 * @param iq: iterator query state.
835 * @param id: module id.
838 generate_dnskey_prefetch(struct module_qstate* qstate,
839 struct iter_qstate* iq, int id)
841 struct module_qstate* subq;
844 /* is this query the same as the prefetch? */
845 if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
846 query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
847 (qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
851 /* if the DNSKEY is in the cache this lookup will stop quickly */
852 log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch",
853 iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
854 if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
855 LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
856 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
857 /* we'll be slower, but it'll work */
858 verbose(VERB_ALGO, "could not generate dnskey prefetch");
862 struct iter_qstate* subiq =
863 (struct iter_qstate*)subq->minfo[id];
864 /* this qstate has the right delegation for the dnskey lookup*/
865 /* make copy to avoid use of stub dp by different qs/threads */
866 subiq->dp = delegpt_copy(iq->dp, subq->region);
867 /* if !subiq->dp, it'll start from the cache, no problem */
872 * See if the query needs forwarding.
874 * @param qstate: query state.
875 * @param iq: iterator query state.
876 * @return true if the request is forwarded, false if not.
877 * If returns true but, iq->dp is NULL then a malloc failure occurred.
880 forward_request(struct module_qstate* qstate, struct iter_qstate* iq)
883 uint8_t* delname = iq->qchase.qname;
884 size_t delnamelen = iq->qchase.qname_len;
885 if(iq->refetch_glue) {
886 delname = iq->dp->name;
887 delnamelen = iq->dp->namelen;
889 /* strip one label off of DS query to lookup higher for it */
890 if( (iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue)
891 && !dname_is_root(iq->qchase.qname))
892 dname_remove_label(&delname, &delnamelen);
893 dp = forwards_lookup(qstate->env->fwds, delname, iq->qchase.qclass);
896 /* send recursion desired to forward addr */
897 iq->chase_flags |= BIT_RD;
898 iq->dp = delegpt_copy(dp, qstate->region);
899 /* iq->dp checked by caller */
900 verbose(VERB_ALGO, "forwarding request");
905 * Process the initial part of the request handling. This state roughly
906 * corresponds to resolver algorithms steps 1 (find answer in cache) and 2
907 * (find the best servers to ask).
909 * Note that all requests start here, and query restarts revisit this state.
911 * This state either generates: 1) a response, from cache or error, 2) a
912 * priming event, or 3) forwards the request to the next state (init2,
915 * @param qstate: query state.
916 * @param iq: iterator query state.
917 * @param ie: iterator shared global environment.
918 * @param id: module id.
919 * @return true if the event needs more request processing immediately,
923 processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
924 struct iter_env* ie, int id)
930 log_query_info(VERB_DETAIL, "resolving", &qstate->qinfo);
933 /* We enforce a maximum number of query restarts. This is primarily a
934 * cheap way to prevent CNAME loops. */
935 if(iq->query_restart_count > MAX_RESTART_COUNT) {
936 verbose(VERB_QUERY, "request has exceeded the maximum number"
937 " of query restarts with %d", iq->query_restart_count);
938 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
941 /* We enforce a maximum recursion/dependency depth -- in general,
942 * this is unnecessary for dependency loops (although it will
943 * catch those), but it provides a sensible limit to the amount
944 * of work required to answer a given query. */
945 verbose(VERB_ALGO, "request has dependency depth of %d", iq->depth);
946 if(iq->depth > ie->max_dependency_depth) {
947 verbose(VERB_QUERY, "request has exceeded the maximum "
948 "dependency depth with depth of %d", iq->depth);
949 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
952 /* If the request is qclass=ANY, setup to generate each class */
953 if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
954 iq->qchase.qclass = 0;
955 return next_state(iq, COLLECT_CLASS_STATE);
958 /* Resolver Algorithm Step 1 -- Look for the answer in local data. */
960 /* This either results in a query restart (CNAME cache response), a
961 * terminating response (ANSWER), or a cache miss (null). */
963 if(qstate->blacklist) {
964 /* if cache, or anything else, was blacklisted then
965 * getting older results from cache is a bad idea, no cache */
966 verbose(VERB_ALGO, "cache blacklisted, going to the network");
969 msg = dns_cache_lookup(qstate->env, iq->qchase.qname,
970 iq->qchase.qname_len, iq->qchase.qtype,
971 iq->qchase.qclass, qstate->query_flags,
972 qstate->region, qstate->env->scratch);
973 if(!msg && qstate->env->neg_cache) {
974 /* lookup in negative cache; may result in
975 * NOERROR/NODATA or NXDOMAIN answers that need validation */
976 msg = val_neg_getmsg(qstate->env->neg_cache, &iq->qchase,
977 qstate->region, qstate->env->rrset_cache,
978 qstate->env->scratch_buffer,
979 *qstate->env->now, 1/*add SOA*/, NULL);
981 /* item taken from cache does not match our query name, thus
982 * security needs to be re-examined later */
983 if(msg && query_dname_compare(qstate->qinfo.qname,
984 iq->qchase.qname) != 0)
985 msg->rep->security = sec_status_unchecked;
988 /* handle positive cache response */
989 enum response_type type = response_type_from_cache(msg,
991 if(verbosity >= VERB_ALGO) {
992 log_dns_msg("msg from cache lookup", &msg->qinfo,
994 verbose(VERB_ALGO, "msg ttl is %d, prefetch ttl %d",
996 (int)msg->rep->prefetch_ttl);
999 if(type == RESPONSE_TYPE_CNAME) {
1002 verbose(VERB_ALGO, "returning CNAME response from "
1004 if(!handle_cname_response(qstate, iq, msg,
1006 return error_response(qstate, id,
1007 LDNS_RCODE_SERVFAIL);
1008 iq->qchase.qname = sname;
1009 iq->qchase.qname_len = slen;
1010 /* This *is* a query restart, even if it is a cheap
1013 iq->refetch_glue = 0;
1014 iq->query_restart_count++;
1016 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1017 return next_state(iq, INIT_REQUEST_STATE);
1020 /* if from cache, NULL, else insert 'cache IP' len=0 */
1021 if(qstate->reply_origin)
1022 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1023 /* it is an answer, response, to final state */
1024 verbose(VERB_ALGO, "returning answer from cache.");
1026 return final_state(iq);
1029 /* attempt to forward the request */
1030 if(forward_request(qstate, iq))
1033 log_err("alloc failure for forward dp");
1034 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1036 iq->refetch_glue = 0;
1037 /* the request has been forwarded.
1038 * forwarded requests need to be immediately sent to the
1039 * next state, QUERYTARGETS. */
1040 return next_state(iq, QUERYTARGETS_STATE);
1043 /* Resolver Algorithm Step 2 -- find the "best" servers. */
1045 /* first, adjust for DS queries. To avoid the grandparent problem,
1046 * we just look for the closest set of server to the parent of qname.
1047 * When re-fetching glue we also need to ask the parent.
1049 if(iq->refetch_glue) {
1051 log_err("internal or malloc fail: no dp for refetch");
1052 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1054 delname = iq->dp->name;
1055 delnamelen = iq->dp->namelen;
1057 delname = iq->qchase.qname;
1058 delnamelen = iq->qchase.qname_len;
1060 if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue ||
1061 (iq->qchase.qtype == LDNS_RR_TYPE_NS && qstate->prefetch_leeway)) {
1062 /* remove first label from delname, root goes to hints,
1063 * but only to fetch glue, not for qtype=DS. */
1064 /* also when prefetching an NS record, fetch it again from
1065 * its parent, just as if it expired, so that you do not
1066 * get stuck on an older nameserver that gives old NSrecords */
1067 if(dname_is_root(delname) && (iq->refetch_glue ||
1068 (iq->qchase.qtype == LDNS_RR_TYPE_NS &&
1069 qstate->prefetch_leeway)))
1070 delname = NULL; /* go to root priming */
1071 else dname_remove_label(&delname, &delnamelen);
1073 /* delname is the name to lookup a delegation for. If NULL rootprime */
1076 /* Lookup the delegation in the cache. If null, then the
1077 * cache needs to be primed for the qclass. */
1079 iq->dp = dns_cache_find_delegation(qstate->env, delname,
1080 delnamelen, iq->qchase.qtype, iq->qchase.qclass,
1081 qstate->region, &iq->deleg_msg,
1082 *qstate->env->now+qstate->prefetch_leeway);
1085 /* If the cache has returned nothing, then we have a
1086 * root priming situation. */
1087 if(iq->dp == NULL) {
1088 /* if there is a stub, then no root prime needed */
1089 int r = prime_stub(qstate, iq, id, delname,
1092 break; /* got noprime-stub-zone, continue */
1094 return 0; /* stub prime request made */
1095 if(forwards_lookup_root(qstate->env->fwds,
1096 iq->qchase.qclass)) {
1097 /* forward zone root, no root prime needed */
1098 /* fill in some dp - safety belt */
1099 iq->dp = hints_lookup_root(qstate->env->hints,
1102 log_err("internal error: no hints dp");
1103 return error_response(qstate, id,
1104 LDNS_RCODE_SERVFAIL);
1106 iq->dp = delegpt_copy(iq->dp, qstate->region);
1108 log_err("out of memory in safety belt");
1109 return error_response(qstate, id,
1110 LDNS_RCODE_SERVFAIL);
1112 return next_state(iq, INIT_REQUEST_2_STATE);
1114 /* Note that the result of this will set a new
1115 * DelegationPoint based on the result of priming. */
1116 if(!prime_root(qstate, iq, id, iq->qchase.qclass))
1117 return error_response(qstate, id,
1118 LDNS_RCODE_REFUSED);
1120 /* priming creates and sends a subordinate query, with
1121 * this query as the parent. So further processing for
1122 * this event will stop until reactivated by the
1123 * results of priming. */
1127 /* see if this dp not useless.
1129 * o all NS items are required glue.
1130 * or the query is for NS item that is required glue.
1131 * o no addresses are provided.
1133 * Instead, go up one level, and try to get even further
1134 * If the root was useless, use safety belt information.
1135 * Only check cache returns, because replies for servers
1136 * could be useless but lead to loops (bumping into the
1137 * same server reply) if useless-checked.
1139 if(iter_dp_is_useless(&qstate->qinfo, qstate->query_flags,
1141 if(dname_is_root(iq->dp->name)) {
1142 /* use safety belt */
1143 verbose(VERB_QUERY, "Cache has root NS but "
1144 "no addresses. Fallback to the safety belt.");
1145 iq->dp = hints_lookup_root(qstate->env->hints,
1147 /* note deleg_msg is from previous lookup,
1148 * but RD is on, so it is not used */
1150 log_err("internal error: no hints dp");
1151 return error_response(qstate, id,
1152 LDNS_RCODE_REFUSED);
1154 iq->dp = delegpt_copy(iq->dp, qstate->region);
1156 log_err("out of memory in safety belt");
1157 return error_response(qstate, id,
1158 LDNS_RCODE_SERVFAIL);
1163 "cache delegation was useless:");
1164 delegpt_log(VERB_ALGO, iq->dp);
1166 delname = iq->dp->name;
1167 delnamelen = iq->dp->namelen;
1168 dname_remove_label(&delname, &delnamelen);
1173 verbose(VERB_ALGO, "cache delegation returns delegpt");
1174 delegpt_log(VERB_ALGO, iq->dp);
1176 /* Otherwise, set the current delegation point and move on to the
1178 return next_state(iq, INIT_REQUEST_2_STATE);
1182 * Process the second part of the initial request handling. This state
1183 * basically exists so that queries that generate root priming events have
1184 * the same init processing as ones that do not. Request events that reach
1185 * this state must have a valid currentDelegationPoint set.
1187 * This part is primarly handling stub zone priming. Events that reach this
1188 * state must have a current delegation point.
1190 * @param qstate: query state.
1191 * @param iq: iterator query state.
1192 * @param id: module id.
1193 * @return true if the event needs more request processing immediately,
1197 processInitRequest2(struct module_qstate* qstate, struct iter_qstate* iq,
1202 log_query_info(VERB_QUERY, "resolving (init part 2): ",
1205 if(iq->refetch_glue) {
1207 log_err("internal or malloc fail: no dp for refetch");
1208 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1210 delname = iq->dp->name;
1211 delnamelen = iq->dp->namelen;
1213 delname = iq->qchase.qname;
1214 delnamelen = iq->qchase.qname_len;
1216 if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue) {
1217 if(!dname_is_root(delname))
1218 dname_remove_label(&delname, &delnamelen);
1219 iq->refetch_glue = 0; /* if CNAME causes restart, no refetch */
1221 /* Check to see if we need to prime a stub zone. */
1222 if(prime_stub(qstate, iq, id, delname, iq->qchase.qclass)) {
1223 /* A priming sub request was made */
1227 /* most events just get forwarded to the next state. */
1228 return next_state(iq, INIT_REQUEST_3_STATE);
1232 * Process the third part of the initial request handling. This state exists
1233 * as a separate state so that queries that generate stub priming events
1234 * will get the tail end of the init process but not repeat the stub priming
1237 * @param qstate: query state.
1238 * @param iq: iterator query state.
1239 * @param id: module id.
1240 * @return true, advancing the event to the QUERYTARGETS_STATE.
1243 processInitRequest3(struct module_qstate* qstate, struct iter_qstate* iq,
1246 log_query_info(VERB_QUERY, "resolving (init part 3): ",
1248 /* if the cache reply dp equals a validation anchor or msg has DS,
1249 * then DNSSEC RRSIGs are expected in the reply */
1250 iq->dnssec_expected = iter_indicates_dnssec(qstate->env, iq->dp,
1251 iq->deleg_msg, iq->qchase.qclass);
1253 /* If the RD flag wasn't set, then we just finish with the
1254 * cached referral as the response. */
1255 if(!(qstate->query_flags & BIT_RD)) {
1256 iq->response = iq->deleg_msg;
1257 if(verbosity >= VERB_ALGO && iq->response)
1258 log_dns_msg("no RD requested, using delegation msg",
1259 &iq->response->qinfo, iq->response->rep);
1260 if(qstate->reply_origin)
1261 sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1262 return final_state(iq);
1264 /* After this point, unset the RD flag -- this query is going to
1265 * be sent to an auth. server. */
1266 iq->chase_flags &= ~BIT_RD;
1268 /* if dnssec expected, fetch key for the trust-anchor or cached-DS */
1269 if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
1270 !(qstate->query_flags&BIT_CD)) {
1271 generate_dnskey_prefetch(qstate, iq, id);
1272 fptr_ok(fptr_whitelist_modenv_detach_subs(
1273 qstate->env->detach_subs));
1274 (*qstate->env->detach_subs)(qstate);
1277 /* Jump to the next state. */
1278 return next_state(iq, QUERYTARGETS_STATE);
1282 * Given a basic query, generate a parent-side "target" query.
1283 * These are subordinate queries for missing delegation point target addresses,
1284 * for which only the parent of the delegation provides correct IP addresses.
1286 * @param qstate: query state.
1287 * @param iq: iterator query state.
1288 * @param id: module id.
1289 * @param name: target qname.
1290 * @param namelen: target qname length.
1291 * @param qtype: target qtype (either A or AAAA).
1292 * @param qclass: target qclass.
1293 * @return true on success, false on failure.
1296 generate_parentside_target_query(struct module_qstate* qstate,
1297 struct iter_qstate* iq, int id, uint8_t* name, size_t namelen,
1298 uint16_t qtype, uint16_t qclass)
1300 struct module_qstate* subq;
1301 if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1302 id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1305 struct iter_qstate* subiq =
1306 (struct iter_qstate*)subq->minfo[id];
1307 /* blacklist the cache - we want to fetch parent stuff */
1308 sock_list_insert(&subq->blacklist, NULL, 0, subq->region);
1309 subiq->query_for_pside_glue = 1;
1310 if(dname_subdomain_c(name, iq->dp->name)) {
1311 subiq->dp = delegpt_copy(iq->dp, subq->region);
1312 subiq->dnssec_expected = iter_indicates_dnssec(
1313 qstate->env, subiq->dp, NULL,
1314 subq->qinfo.qclass);
1315 subiq->refetch_glue = 1;
1317 subiq->dp = dns_cache_find_delegation(qstate->env,
1318 name, namelen, qtype, qclass, subq->region,
1320 *qstate->env->now+subq->prefetch_leeway);
1321 /* if no dp, then it's from root, refetch unneeded */
1323 subiq->dnssec_expected = iter_indicates_dnssec(
1324 qstate->env, subiq->dp, NULL,
1325 subq->qinfo.qclass);
1326 subiq->refetch_glue = 1;
1330 log_nametypeclass(VERB_QUERY, "new pside target", name, qtype, qclass);
1335 * Given a basic query, generate a "target" query. These are subordinate
1336 * queries for missing delegation point target addresses.
1338 * @param qstate: query state.
1339 * @param iq: iterator query state.
1340 * @param id: module id.
1341 * @param name: target qname.
1342 * @param namelen: target qname length.
1343 * @param qtype: target qtype (either A or AAAA).
1344 * @param qclass: target qclass.
1345 * @return true on success, false on failure.
1348 generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
1349 int id, uint8_t* name, size_t namelen, uint16_t qtype, uint16_t qclass)
1351 struct module_qstate* subq;
1352 if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1353 id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1355 log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
1360 * Given an event at a certain state, generate zero or more target queries
1361 * for it's current delegation point.
1363 * @param qstate: query state.
1364 * @param iq: iterator query state.
1365 * @param ie: iterator shared global environment.
1366 * @param id: module id.
1367 * @param maxtargets: The maximum number of targets to query for.
1368 * if it is negative, there is no maximum number of targets.
1369 * @param num: returns the number of queries generated and processed,
1370 * which may be zero if there were no missing targets.
1371 * @return false on error.
1374 query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
1375 struct iter_env* ie, int id, int maxtargets, int* num)
1377 int query_count = 0;
1378 struct delegpt_ns* ns;
1382 if(iq->depth == ie->max_dependency_depth)
1384 if(iq->depth > 0 && iq->target_count &&
1385 iq->target_count[1] > MAX_TARGET_COUNT) {
1386 char s[LDNS_MAX_DOMAINLEN+1];
1387 dname_str(qstate->qinfo.qname, s);
1388 verbose(VERB_QUERY, "request %s has exceeded the maximum "
1389 "number of glue fetches %d", s, iq->target_count[1]);
1393 iter_mark_cycle_targets(qstate, iq->dp);
1394 missing = (int)delegpt_count_missing_targets(iq->dp);
1395 log_assert(maxtargets != 0); /* that would not be useful */
1397 /* Generate target requests. Basically, any missing targets
1398 * are queried for here, regardless if it is necessary to do
1399 * so to continue processing. */
1400 if(maxtargets < 0 || maxtargets > missing)
1402 else toget = maxtargets;
1407 /* select 'toget' items from the total of 'missing' items */
1408 log_assert(toget <= missing);
1410 /* loop over missing targets */
1411 for(ns = iq->dp->nslist; ns; ns = ns->next) {
1415 /* randomly select this item with probability toget/missing */
1416 if(!iter_ns_probability(qstate->env->rnd, toget, missing)) {
1417 /* do not select this one, next; select toget number
1418 * of items from a list one less in size */
1423 if(ie->supports_ipv6 && !ns->got6) {
1424 /* Send the AAAA request. */
1425 if(!generate_target_query(qstate, iq, id,
1426 ns->name, ns->namelen,
1427 LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
1430 qstate->ext_state[id] = module_wait_subquery;
1435 /* Send the A request. */
1436 if(ie->supports_ipv4 && !ns->got4) {
1437 if(!generate_target_query(qstate, iq, id,
1438 ns->name, ns->namelen,
1439 LDNS_RR_TYPE_A, iq->qchase.qclass)) {
1442 qstate->ext_state[id] = module_wait_subquery;
1448 /* mark this target as in progress. */
1457 qstate->ext_state[id] = module_wait_subquery;
1462 /** see if last resort is possible - does config allow queries to parent */
1464 can_have_last_resort(struct module_env* env, struct delegpt* dp,
1465 struct iter_qstate* iq)
1467 struct delegpt* fwddp;
1468 struct iter_hints_stub* stub;
1469 /* do not process a last resort (the parent side) if a stub
1470 * or forward is configured, because we do not want to go 'above'
1471 * the configured servers */
1472 if(!dname_is_root(dp->name) && (stub = (struct iter_hints_stub*)
1473 name_tree_find(&env->hints->tree, dp->name, dp->namelen,
1474 dp->namelabs, iq->qchase.qclass)) &&
1475 /* has_parent side is turned off for stub_first, where we
1476 * are allowed to go to the parent */
1477 stub->dp->has_parent_side_NS) {
1478 verbose(VERB_QUERY, "configured stub servers failed -- returning SERVFAIL");
1481 if((fwddp = forwards_find(env->fwds, dp->name, iq->qchase.qclass)) &&
1482 /* has_parent_side is turned off for forward_first, where
1483 * we are allowed to go to the parent */
1484 fwddp->has_parent_side_NS) {
1485 verbose(VERB_QUERY, "configured forward servers failed -- returning SERVFAIL");
1492 * Called by processQueryTargets when it would like extra targets to query
1493 * but it seems to be out of options. At last resort some less appealing
1494 * options are explored. If there are no more options, the result is SERVFAIL
1496 * @param qstate: query state.
1497 * @param iq: iterator query state.
1498 * @param ie: iterator shared global environment.
1499 * @param id: module id.
1500 * @return true if the event requires more request processing immediately,
1504 processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
1505 struct iter_env* ie, int id)
1507 struct delegpt_ns* ns;
1508 int query_count = 0;
1509 verbose(VERB_ALGO, "No more query targets, attempting last resort");
1512 if(!can_have_last_resort(qstate->env, iq->dp, iq)) {
1513 /* fail -- no more targets, no more hope of targets, no hope
1515 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1517 if(!iq->dp->has_parent_side_NS && dname_is_root(iq->dp->name)) {
1518 struct delegpt* p = hints_lookup_root(qstate->env->hints,
1521 struct delegpt_ns* ns;
1522 struct delegpt_addr* a;
1523 iq->chase_flags &= ~BIT_RD; /* go to authorities */
1524 for(ns = p->nslist; ns; ns=ns->next) {
1525 (void)delegpt_add_ns(iq->dp, qstate->region,
1526 ns->name, ns->lame);
1528 for(a = p->target_list; a; a=a->next_target) {
1529 (void)delegpt_add_addr(iq->dp, qstate->region,
1530 &a->addr, a->addrlen, a->bogus,
1534 iq->dp->has_parent_side_NS = 1;
1535 } else if(!iq->dp->has_parent_side_NS) {
1536 if(!iter_lookup_parent_NS_from_cache(qstate->env, iq->dp,
1537 qstate->region, &qstate->qinfo)
1538 || !iq->dp->has_parent_side_NS) {
1539 /* if: malloc failure in lookup go up to try */
1540 /* if: no parent NS in cache - go up one level */
1541 verbose(VERB_ALGO, "try to grab parent NS");
1542 iq->store_parent_NS = iq->dp;
1543 iq->chase_flags &= ~BIT_RD; /* go to authorities */
1544 iq->deleg_msg = NULL;
1545 iq->refetch_glue = 1;
1546 iq->query_restart_count++;
1548 return next_state(iq, INIT_REQUEST_STATE);
1551 /* see if that makes new names available */
1552 if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
1553 qstate->region, iq->dp))
1554 log_err("out of memory in cache_fill_missing");
1555 if(iq->dp->usable_list) {
1556 verbose(VERB_ALGO, "try parent-side-name, w. glue from cache");
1557 return next_state(iq, QUERYTARGETS_STATE);
1559 /* try to fill out parent glue from cache */
1560 if(iter_lookup_parent_glue_from_cache(qstate->env, iq->dp,
1561 qstate->region, &qstate->qinfo)) {
1562 /* got parent stuff from cache, see if we can continue */
1563 verbose(VERB_ALGO, "try parent-side glue from cache");
1564 return next_state(iq, QUERYTARGETS_STATE);
1566 /* query for an extra name added by the parent-NS record */
1567 if(delegpt_count_missing_targets(iq->dp) > 0) {
1569 verbose(VERB_ALGO, "try parent-side target name");
1570 if(!query_for_targets(qstate, iq, ie, id, 1, &qs)) {
1571 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1573 iq->num_target_queries += qs;
1574 target_count_increase(iq, qs);
1576 qstate->ext_state[id] = module_wait_subquery;
1577 return 0; /* and wait for them */
1580 if(iq->depth == ie->max_dependency_depth) {
1581 verbose(VERB_QUERY, "maxdepth and need more nameservers, fail");
1582 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1584 if(iq->depth > 0 && iq->target_count &&
1585 iq->target_count[1] > MAX_TARGET_COUNT) {
1586 char s[LDNS_MAX_DOMAINLEN+1];
1587 dname_str(qstate->qinfo.qname, s);
1588 verbose(VERB_QUERY, "request %s has exceeded the maximum "
1589 "number of glue fetches %d", s, iq->target_count[1]);
1590 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1592 /* mark cycle targets for parent-side lookups */
1593 iter_mark_pside_cycle_targets(qstate, iq->dp);
1594 /* see if we can issue queries to get nameserver addresses */
1595 /* this lookup is not randomized, but sequential. */
1596 for(ns = iq->dp->nslist; ns; ns = ns->next) {
1597 /* query for parent-side A and AAAA for nameservers */
1598 if(ie->supports_ipv6 && !ns->done_pside6) {
1599 /* Send the AAAA request. */
1600 if(!generate_parentside_target_query(qstate, iq, id,
1601 ns->name, ns->namelen,
1602 LDNS_RR_TYPE_AAAA, iq->qchase.qclass))
1603 return error_response(qstate, id,
1604 LDNS_RCODE_SERVFAIL);
1605 ns->done_pside6 = 1;
1608 if(ie->supports_ipv4 && !ns->done_pside4) {
1609 /* Send the A request. */
1610 if(!generate_parentside_target_query(qstate, iq, id,
1611 ns->name, ns->namelen,
1612 LDNS_RR_TYPE_A, iq->qchase.qclass))
1613 return error_response(qstate, id,
1614 LDNS_RCODE_SERVFAIL);
1615 ns->done_pside4 = 1;
1618 if(query_count != 0) { /* suspend to await results */
1619 verbose(VERB_ALGO, "try parent-side glue lookup");
1620 iq->num_target_queries += query_count;
1621 target_count_increase(iq, query_count);
1622 qstate->ext_state[id] = module_wait_subquery;
1627 /* if this was a parent-side glue query itself, then store that
1628 * failure in cache. */
1629 if(iq->query_for_pside_glue && !iq->pside_glue)
1630 iter_store_parentside_neg(qstate->env, &qstate->qinfo,
1631 iq->deleg_msg?iq->deleg_msg->rep:
1632 (iq->response?iq->response->rep:NULL));
1634 verbose(VERB_QUERY, "out of query targets -- returning SERVFAIL");
1635 /* fail -- no more targets, no more hope of targets, no hope
1637 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1641 * Try to find the NS record set that will resolve a qtype DS query. Due
1642 * to grandparent/grandchild reasons we did not get a proper lookup right
1643 * away. We need to create type NS queries until we get the right parent
1644 * for this lookup. We remove labels from the query to find the right point.
1645 * If we end up at the old dp name, then there is no solution.
1647 * @param qstate: query state.
1648 * @param iq: iterator query state.
1649 * @param id: module id.
1650 * @return true if the event requires more immediate processing, false if
1651 * not. This is generally only true when forwarding the request to
1652 * the final state (i.e., on answer).
1655 processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
1657 struct module_qstate* subq = NULL;
1658 verbose(VERB_ALGO, "processDSNSFind");
1660 if(!iq->dsns_point) {
1662 iq->dsns_point = iq->qchase.qname;
1663 iq->dsns_point_len = iq->qchase.qname_len;
1665 /* robustcheck for internal error: we are not underneath the dp */
1666 if(!dname_subdomain_c(iq->dsns_point, iq->dp->name)) {
1667 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1670 /* go up one (more) step, until we hit the dp, if so, end */
1671 dname_remove_label(&iq->dsns_point, &iq->dsns_point_len);
1672 if(query_dname_compare(iq->dsns_point, iq->dp->name) == 0) {
1673 /* there was no inbetween nameserver, use the old delegation
1674 * point again. And this time, because dsns_point is nonNULL
1675 * we are going to accept the (bad) result */
1676 iq->state = QUERYTARGETS_STATE;
1679 iq->state = DSNS_FIND_STATE;
1681 /* spawn NS lookup (validation not needed, this is for DS lookup) */
1682 log_nametypeclass(VERB_ALGO, "fetch nameservers",
1683 iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1684 if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len,
1685 LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1686 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
1687 return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1694 * This is the request event state where the request will be sent to one of
1695 * its current query targets. This state also handles issuing target lookup
1696 * queries for missing target IP addresses. Queries typically iterate on
1697 * this state, both when they are just trying different targets for a given
1698 * delegation point, and when they change delegation points. This state
1699 * roughly corresponds to RFC 1034 algorithm steps 3 and 4.
1701 * @param qstate: query state.
1702 * @param iq: iterator query state.
1703 * @param ie: iterator shared global environment.
1704 * @param id: module id.
1705 * @return true if the event requires more request processing immediately,
1706 * false if not. This state only returns true when it is generating
1707 * a SERVFAIL response because the query has hit a dead end.
1710 processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
1711 struct iter_env* ie, int id)
1714 struct delegpt_addr* target;
1715 struct outbound_entry* outq;
1717 /* NOTE: a request will encounter this state for each target it
1718 * needs to send a query to. That is, at least one per referral,
1719 * more if some targets timeout or return throwaway answers. */
1721 log_query_info(VERB_QUERY, "processQueryTargets:", &qstate->qinfo);
1722 verbose(VERB_ALGO, "processQueryTargets: targetqueries %d, "
1723 "currentqueries %d sentcount %d", iq->num_target_queries,
1724 iq->num_current_queries, iq->sent_count);
1726 /* Make sure that we haven't run away */
1727 /* FIXME: is this check even necessary? */
1728 if(iq->referral_count > MAX_REFERRAL_COUNT) {
1729 verbose(VERB_QUERY, "request has exceeded the maximum "
1730 "number of referrrals with %d", iq->referral_count);
1731 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1733 if(iq->sent_count > MAX_SENT_COUNT) {
1734 verbose(VERB_QUERY, "request has exceeded the maximum "
1735 "number of sends with %d", iq->sent_count);
1736 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1739 /* Make sure we have a delegation point, otherwise priming failed
1740 * or another failure occurred */
1742 verbose(VERB_QUERY, "Failed to get a delegation, giving up");
1743 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1745 if(!ie->supports_ipv6)
1746 delegpt_no_ipv6(iq->dp);
1747 if(!ie->supports_ipv4)
1748 delegpt_no_ipv4(iq->dp);
1749 delegpt_log(VERB_ALGO, iq->dp);
1751 if(iq->num_current_queries>0) {
1752 /* already busy answering a query, this restart is because
1753 * more delegpt addrs became available, wait for existing
1755 verbose(VERB_ALGO, "woke up, but wait for outstanding query");
1756 qstate->ext_state[id] = module_wait_reply;
1761 /* < not <=, because although the array is large enough for <=, the
1762 * generated query will immediately be discarded due to depth and
1763 * that servfail is cached, which is not good as opportunism goes. */
1764 if(iq->depth < ie->max_dependency_depth
1765 && iq->sent_count < TARGET_FETCH_STOP) {
1766 tf_policy = ie->target_fetch_policy[iq->depth];
1769 /* if in 0x20 fallback get as many targets as possible */
1770 if(iq->caps_fallback) {
1772 size_t naddr, nres, navail;
1773 if(!query_for_targets(qstate, iq, ie, id, -1, &extra)) {
1774 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1776 iq->num_target_queries += extra;
1777 target_count_increase(iq, extra);
1778 if(iq->num_target_queries > 0) {
1779 /* wait to get all targets, we want to try em */
1780 verbose(VERB_ALGO, "wait for all targets for fallback");
1781 qstate->ext_state[id] = module_wait_reply;
1784 /* did we do enough fallback queries already? */
1785 delegpt_count_addr(iq->dp, &naddr, &nres, &navail);
1786 /* the current caps_server is the number of fallbacks sent.
1787 * the original query is one that matched too, so we have
1788 * caps_server+1 number of matching queries now */
1789 if(iq->caps_server+1 >= naddr*3 ||
1790 iq->caps_server+1 >= MAX_SENT_COUNT) {
1791 /* we're done, process the response */
1792 verbose(VERB_ALGO, "0x20 fallback had %d responses "
1793 "match for %d wanted, done.",
1794 (int)iq->caps_server+1, (int)naddr*3);
1795 iq->caps_fallback = 0;
1796 iter_dec_attempts(iq->dp, 3); /* space for fallback */
1797 iq->num_current_queries++; /* RespState decrements it*/
1798 iq->referral_count++; /* make sure we don't loop */
1800 iq->state = QUERY_RESP_STATE;
1803 verbose(VERB_ALGO, "0x20 fallback number %d",
1804 (int)iq->caps_server);
1806 /* if there is a policy to fetch missing targets
1807 * opportunistically, do it. we rely on the fact that once a
1808 * query (or queries) for a missing name have been issued,
1809 * they will not show up again. */
1810 } else if(tf_policy != 0) {
1812 verbose(VERB_ALGO, "attempt to get extra %d targets",
1814 (void)query_for_targets(qstate, iq, ie, id, tf_policy, &extra);
1815 /* errors ignored, these targets are not strictly necessary for
1816 * this result, we do not have to reply with SERVFAIL */
1817 iq->num_target_queries += extra;
1818 target_count_increase(iq, extra);
1821 /* Add the current set of unused targets to our queue. */
1822 delegpt_add_unused_targets(iq->dp);
1824 /* Select the next usable target, filtering out unsuitable targets. */
1825 target = iter_server_selection(ie, qstate->env, iq->dp,
1826 iq->dp->name, iq->dp->namelen, iq->qchase.qtype,
1827 &iq->dnssec_lame_query, &iq->chase_to_rd,
1828 iq->num_target_queries, qstate->blacklist);
1830 /* If no usable target was selected... */
1832 /* Here we distinguish between three states: generate a new
1833 * target query, just wait, or quit (with a SERVFAIL).
1834 * We have the following information: number of active
1835 * target queries, number of active current queries,
1836 * the presence of missing targets at this delegation
1837 * point, and the given query target policy. */
1839 /* Check for the wait condition. If this is true, then
1840 * an action must be taken. */
1841 if(iq->num_target_queries==0 && iq->num_current_queries==0) {
1842 /* If there is nothing to wait for, then we need
1843 * to distinguish between generating (a) new target
1844 * query, or failing. */
1845 if(delegpt_count_missing_targets(iq->dp) > 0) {
1847 verbose(VERB_ALGO, "querying for next "
1849 if(!query_for_targets(qstate, iq, ie, id,
1851 return error_response(qstate, id,
1852 LDNS_RCODE_SERVFAIL);
1855 delegpt_count_missing_targets(iq->dp) == 0){
1856 /* it looked like there were missing
1857 * targets, but they did not turn up.
1858 * Try the bad choices again (if any),
1859 * when we get back here missing==0,
1860 * so this is not a loop. */
1863 iq->num_target_queries += qs;
1864 target_count_increase(iq, qs);
1866 /* Since a target query might have been made, we
1867 * need to check again. */
1868 if(iq->num_target_queries == 0) {
1869 return processLastResort(qstate, iq, ie, id);
1873 /* otherwise, we have no current targets, so submerge
1874 * until one of the target or direct queries return. */
1875 if(iq->num_target_queries>0 && iq->num_current_queries>0) {
1876 verbose(VERB_ALGO, "no current targets -- waiting "
1877 "for %d targets to resolve or %d outstanding"
1878 " queries to respond", iq->num_target_queries,
1879 iq->num_current_queries);
1880 qstate->ext_state[id] = module_wait_reply;
1881 } else if(iq->num_target_queries>0) {
1882 verbose(VERB_ALGO, "no current targets -- waiting "
1883 "for %d targets to resolve.",
1884 iq->num_target_queries);
1885 qstate->ext_state[id] = module_wait_subquery;
1887 verbose(VERB_ALGO, "no current targets -- waiting "
1888 "for %d outstanding queries to respond.",
1889 iq->num_current_queries);
1890 qstate->ext_state[id] = module_wait_reply;
1895 /* We have a valid target. */
1896 if(verbosity >= VERB_QUERY) {
1897 log_query_info(VERB_QUERY, "sending query:", &iq->qchase);
1898 log_name_addr(VERB_QUERY, "sending to target:", iq->dp->name,
1899 &target->addr, target->addrlen);
1900 verbose(VERB_ALGO, "dnssec status: %s%s",
1901 iq->dnssec_expected?"expected": "not expected",
1902 iq->dnssec_lame_query?" but lame_query anyway": "");
1904 fptr_ok(fptr_whitelist_modenv_send_query(qstate->env->send_query));
1905 outq = (*qstate->env->send_query)(
1906 iq->qchase.qname, iq->qchase.qname_len,
1907 iq->qchase.qtype, iq->qchase.qclass,
1908 iq->chase_flags | (iq->chase_to_rd?BIT_RD:0), EDNS_DO|BIT_CD,
1909 iq->dnssec_expected, iq->caps_fallback, &target->addr,
1910 target->addrlen, iq->dp->name, iq->dp->namelen, qstate);
1912 log_addr(VERB_DETAIL, "error sending query to auth server",
1913 &target->addr, target->addrlen);
1914 return next_state(iq, QUERYTARGETS_STATE);
1916 outbound_list_insert(&iq->outlist, outq);
1917 iq->num_current_queries++;
1919 qstate->ext_state[id] = module_wait_reply;
1924 /** find NS rrset in given list */
1925 static struct ub_packed_rrset_key*
1926 find_NS(struct reply_info* rep, size_t from, size_t to)
1929 for(i=from; i<to; i++) {
1930 if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
1931 return rep->rrsets[i];
1938 * Process the query response. All queries end up at this state first. This
1939 * process generally consists of analyzing the response and routing the
1940 * event to the next state (either bouncing it back to a request state, or
1941 * terminating the processing for this event).
1943 * @param qstate: query state.
1944 * @param iq: iterator query state.
1945 * @param id: module id.
1946 * @return true if the event requires more immediate processing, false if
1947 * not. This is generally only true when forwarding the request to
1948 * the final state (i.e., on answer).
1951 processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
1955 enum response_type type;
1956 iq->num_current_queries--;
1957 if(iq->response == NULL) {
1958 iq->chase_to_rd = 0;
1959 iq->dnssec_lame_query = 0;
1960 verbose(VERB_ALGO, "query response was timeout");
1961 return next_state(iq, QUERYTARGETS_STATE);
1963 type = response_type_from_server(
1964 (int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
1965 iq->response, &iq->qchase, iq->dp);
1966 iq->chase_to_rd = 0;
1967 if(type == RESPONSE_TYPE_REFERRAL && (iq->chase_flags&BIT_RD)) {
1968 /* When forwarding (RD bit is set), we handle referrals
1969 * differently. No queries should be sent elsewhere */
1970 type = RESPONSE_TYPE_ANSWER;
1972 if(iq->dnssec_expected && !iq->dnssec_lame_query &&
1973 !(iq->chase_flags&BIT_RD)
1974 && type != RESPONSE_TYPE_LAME
1975 && type != RESPONSE_TYPE_REC_LAME
1976 && type != RESPONSE_TYPE_THROWAWAY
1977 && type != RESPONSE_TYPE_UNTYPED) {
1978 /* a possible answer, see if it is missing DNSSEC */
1979 /* but not when forwarding, so we dont mark fwder lame */
1980 if(!iter_msg_has_dnssec(iq->response)) {
1981 /* Mark this address as dnsseclame in this dp,
1982 * because that will make serverselection disprefer
1983 * it, but also, once it is the only final option,
1984 * use dnssec-lame-bypass if it needs to query there.*/
1986 struct delegpt_addr* a = delegpt_find_addr(
1987 iq->dp, &qstate->reply->addr,
1988 qstate->reply->addrlen);
1989 if(a) a->dnsseclame = 1;
1991 /* test the answer is from the zone we expected,
1992 * otherwise, (due to parent,child on same server), we
1993 * might mark the server,zone lame inappropriately */
1994 if(!iter_msg_from_zone(iq->response, iq->dp, type,
1996 qstate->reply = NULL;
1997 type = RESPONSE_TYPE_LAME;
2000 } else iq->dnssec_lame_query = 0;
2001 /* see if referral brings us close to the target */
2002 if(type == RESPONSE_TYPE_REFERRAL) {
2003 struct ub_packed_rrset_key* ns = find_NS(
2004 iq->response->rep, iq->response->rep->an_numrrsets,
2005 iq->response->rep->an_numrrsets
2006 + iq->response->rep->ns_numrrsets);
2007 if(!ns) ns = find_NS(iq->response->rep, 0,
2008 iq->response->rep->an_numrrsets);
2009 if(!ns || !dname_strict_subdomain_c(ns->rk.dname, iq->dp->name)
2010 || !dname_subdomain_c(iq->qchase.qname, ns->rk.dname)){
2011 verbose(VERB_ALGO, "bad referral, throwaway");
2012 type = RESPONSE_TYPE_THROWAWAY;
2014 iter_scrub_ds(iq->response, ns, iq->dp->name);
2015 } else iter_scrub_ds(iq->response, NULL, NULL);
2017 /* handle each of the type cases */
2018 if(type == RESPONSE_TYPE_ANSWER) {
2019 /* ANSWER type responses terminate the query algorithm,
2020 * so they sent on their */
2021 if(verbosity >= VERB_DETAIL) {
2022 verbose(VERB_DETAIL, "query response was %s",
2023 FLAGS_GET_RCODE(iq->response->rep->flags)
2024 ==LDNS_RCODE_NXDOMAIN?"NXDOMAIN ANSWER":
2025 (iq->response->rep->an_numrrsets?"ANSWER":
2028 /* if qtype is DS, check we have the right level of answer,
2029 * like grandchild answer but we need the middle, reject it */
2030 if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2031 && !(iq->chase_flags&BIT_RD)
2032 && iter_ds_toolow(iq->response, iq->dp)
2033 && iter_dp_cangodown(&iq->qchase, iq->dp)) {
2034 /* close down outstanding requests to be discarded */
2035 outbound_list_clear(&iq->outlist);
2036 iq->num_current_queries = 0;
2037 fptr_ok(fptr_whitelist_modenv_detach_subs(
2038 qstate->env->detach_subs));
2039 (*qstate->env->detach_subs)(qstate);
2040 iq->num_target_queries = 0;
2041 return processDSNSFind(qstate, iq, id);
2043 iter_dns_store(qstate->env, &iq->response->qinfo,
2044 iq->response->rep, 0, qstate->prefetch_leeway,
2045 iq->dp&&iq->dp->has_parent_side_NS,
2046 qstate->region, qstate->query_flags);
2047 /* close down outstanding requests to be discarded */
2048 outbound_list_clear(&iq->outlist);
2049 iq->num_current_queries = 0;
2050 fptr_ok(fptr_whitelist_modenv_detach_subs(
2051 qstate->env->detach_subs));
2052 (*qstate->env->detach_subs)(qstate);
2053 iq->num_target_queries = 0;
2055 sock_list_insert(&qstate->reply_origin,
2056 &qstate->reply->addr, qstate->reply->addrlen,
2058 return final_state(iq);
2059 } else if(type == RESPONSE_TYPE_REFERRAL) {
2060 /* REFERRAL type responses get a reset of the
2061 * delegation point, and back to the QUERYTARGETS_STATE. */
2062 verbose(VERB_DETAIL, "query response was REFERRAL");
2064 /* if hardened, only store referral if we asked for it */
2065 if(!qstate->env->cfg->harden_referral_path ||
2066 ( qstate->qinfo.qtype == LDNS_RR_TYPE_NS
2067 && (qstate->query_flags&BIT_RD)
2068 && !(qstate->query_flags&BIT_CD)
2069 /* we know that all other NS rrsets are scrubbed
2070 * away, thus on referral only one is left.
2071 * see if that equals the query name... */
2072 && ( /* auth section, but sometimes in answer section*/
2073 reply_find_rrset_section_ns(iq->response->rep,
2074 iq->qchase.qname, iq->qchase.qname_len,
2075 LDNS_RR_TYPE_NS, iq->qchase.qclass)
2076 || reply_find_rrset_section_an(iq->response->rep,
2077 iq->qchase.qname, iq->qchase.qname_len,
2078 LDNS_RR_TYPE_NS, iq->qchase.qclass)
2081 /* Store the referral under the current query */
2082 /* no prefetch-leeway, since its not the answer */
2083 iter_dns_store(qstate->env, &iq->response->qinfo,
2084 iq->response->rep, 1, 0, 0, NULL, 0);
2085 if(iq->store_parent_NS)
2086 iter_store_parentside_NS(qstate->env,
2088 if(qstate->env->neg_cache)
2089 val_neg_addreferral(qstate->env->neg_cache,
2090 iq->response->rep, iq->dp->name);
2092 /* store parent-side-in-zone-glue, if directly queried for */
2093 if(iq->query_for_pside_glue && !iq->pside_glue) {
2094 iq->pside_glue = reply_find_rrset(iq->response->rep,
2095 iq->qchase.qname, iq->qchase.qname_len,
2096 iq->qchase.qtype, iq->qchase.qclass);
2097 if(iq->pside_glue) {
2098 log_rrset_key(VERB_ALGO, "found parent-side "
2099 "glue", iq->pside_glue);
2100 iter_store_parentside_rrset(qstate->env,
2105 /* Reset the event state, setting the current delegation
2106 * point to the referral. */
2107 iq->deleg_msg = iq->response;
2108 iq->dp = delegpt_from_message(iq->response, qstate->region);
2110 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2111 if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
2112 qstate->region, iq->dp))
2113 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2114 if(iq->store_parent_NS && query_dname_compare(iq->dp->name,
2115 iq->store_parent_NS->name) == 0)
2116 iter_merge_retry_counts(iq->dp, iq->store_parent_NS);
2117 delegpt_log(VERB_ALGO, iq->dp);
2118 /* Count this as a referral. */
2119 iq->referral_count++;
2121 /* see if the next dp is a trust anchor, or a DS was sent
2122 * along, indicating dnssec is expected for next zone */
2123 iq->dnssec_expected = iter_indicates_dnssec(qstate->env,
2124 iq->dp, iq->response, iq->qchase.qclass);
2125 /* if dnssec, validating then also fetch the key for the DS */
2126 if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
2127 !(qstate->query_flags&BIT_CD))
2128 generate_dnskey_prefetch(qstate, iq, id);
2130 /* spawn off NS and addr to auth servers for the NS we just
2131 * got in the referral. This gets authoritative answer
2132 * (answer section trust level) rrset.
2133 * right after, we detach the subs, answer goes to cache. */
2134 if(qstate->env->cfg->harden_referral_path)
2135 generate_ns_check(qstate, iq, id);
2137 /* stop current outstanding queries.
2138 * FIXME: should the outstanding queries be waited for and
2139 * handled? Say by a subquery that inherits the outbound_entry.
2141 outbound_list_clear(&iq->outlist);
2142 iq->num_current_queries = 0;
2143 fptr_ok(fptr_whitelist_modenv_detach_subs(
2144 qstate->env->detach_subs));
2145 (*qstate->env->detach_subs)(qstate);
2146 iq->num_target_queries = 0;
2147 verbose(VERB_ALGO, "cleared outbound list for next round");
2148 return next_state(iq, QUERYTARGETS_STATE);
2149 } else if(type == RESPONSE_TYPE_CNAME) {
2150 uint8_t* sname = NULL;
2151 size_t snamelen = 0;
2152 /* CNAME type responses get a query restart (i.e., get a
2153 * reset of the query state and go back to INIT_REQUEST_STATE).
2155 verbose(VERB_DETAIL, "query response was CNAME");
2156 if(verbosity >= VERB_ALGO)
2157 log_dns_msg("cname msg", &iq->response->qinfo,
2159 /* if qtype is DS, check we have the right level of answer,
2160 * like grandchild answer but we need the middle, reject it */
2161 if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2162 && !(iq->chase_flags&BIT_RD)
2163 && iter_ds_toolow(iq->response, iq->dp)
2164 && iter_dp_cangodown(&iq->qchase, iq->dp)) {
2165 outbound_list_clear(&iq->outlist);
2166 iq->num_current_queries = 0;
2167 fptr_ok(fptr_whitelist_modenv_detach_subs(
2168 qstate->env->detach_subs));
2169 (*qstate->env->detach_subs)(qstate);
2170 iq->num_target_queries = 0;
2171 return processDSNSFind(qstate, iq, id);
2173 /* Process the CNAME response. */
2174 if(!handle_cname_response(qstate, iq, iq->response,
2176 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2177 /* cache the CNAME response under the current query */
2178 /* NOTE : set referral=1, so that rrsets get stored but not
2179 * the partial query answer (CNAME only). */
2180 /* prefetchleeway applied because this updates answer parts */
2181 iter_dns_store(qstate->env, &iq->response->qinfo,
2182 iq->response->rep, 1, qstate->prefetch_leeway,
2183 iq->dp&&iq->dp->has_parent_side_NS, NULL,
2184 qstate->query_flags);
2185 /* set the current request's qname to the new value. */
2186 iq->qchase.qname = sname;
2187 iq->qchase.qname_len = snamelen;
2188 /* Clear the query state, since this is a query restart. */
2189 iq->deleg_msg = NULL;
2191 iq->dsns_point = NULL;
2192 /* Note the query restart. */
2193 iq->query_restart_count++;
2196 /* stop current outstanding queries.
2197 * FIXME: should the outstanding queries be waited for and
2198 * handled? Say by a subquery that inherits the outbound_entry.
2200 outbound_list_clear(&iq->outlist);
2201 iq->num_current_queries = 0;
2202 fptr_ok(fptr_whitelist_modenv_detach_subs(
2203 qstate->env->detach_subs));
2204 (*qstate->env->detach_subs)(qstate);
2205 iq->num_target_queries = 0;
2207 sock_list_insert(&qstate->reply_origin,
2208 &qstate->reply->addr, qstate->reply->addrlen,
2210 verbose(VERB_ALGO, "cleared outbound list for query restart");
2211 /* go to INIT_REQUEST_STATE for new qname. */
2212 return next_state(iq, INIT_REQUEST_STATE);
2213 } else if(type == RESPONSE_TYPE_LAME) {
2214 /* Cache the LAMEness. */
2215 verbose(VERB_DETAIL, "query response was %sLAME",
2216 dnsseclame?"DNSSEC ":"");
2217 if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2218 log_err("mark lame: mismatch in qname and dpname");
2219 /* throwaway this reply below */
2220 } else if(qstate->reply) {
2221 /* need addr for lameness cache, but we may have
2222 * gotten this from cache, so test to be sure */
2223 if(!infra_set_lame(qstate->env->infra_cache,
2224 &qstate->reply->addr, qstate->reply->addrlen,
2225 iq->dp->name, iq->dp->namelen,
2226 *qstate->env->now, dnsseclame, 0,
2228 log_err("mark host lame: out of memory");
2230 } else if(type == RESPONSE_TYPE_REC_LAME) {
2231 /* Cache the LAMEness. */
2232 verbose(VERB_DETAIL, "query response REC_LAME: "
2233 "recursive but not authoritative server");
2234 if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2235 log_err("mark rec_lame: mismatch in qname and dpname");
2236 /* throwaway this reply below */
2237 } else if(qstate->reply) {
2238 /* need addr for lameness cache, but we may have
2239 * gotten this from cache, so test to be sure */
2240 verbose(VERB_DETAIL, "mark as REC_LAME");
2241 if(!infra_set_lame(qstate->env->infra_cache,
2242 &qstate->reply->addr, qstate->reply->addrlen,
2243 iq->dp->name, iq->dp->namelen,
2244 *qstate->env->now, 0, 1, iq->qchase.qtype))
2245 log_err("mark host lame: out of memory");
2247 } else if(type == RESPONSE_TYPE_THROWAWAY) {
2248 /* LAME and THROWAWAY responses are handled the same way.
2249 * In this case, the event is just sent directly back to
2250 * the QUERYTARGETS_STATE without resetting anything,
2251 * because, clearly, the next target must be tried. */
2252 verbose(VERB_DETAIL, "query response was THROWAWAY");
2254 log_warn("A query response came back with an unknown type: %d",
2258 /* LAME, THROWAWAY and "unknown" all end up here.
2259 * Recycle to the QUERYTARGETS state to hopefully try a
2260 * different target. */
2261 return next_state(iq, QUERYTARGETS_STATE);
2265 * Return priming query results to interested super querystates.
2267 * Sets the delegation point and delegation message (not nonRD queries).
2268 * This is a callback from walk_supers.
2270 * @param qstate: priming query state that finished.
2271 * @param id: module id.
2272 * @param forq: the qstate for which priming has been done.
2275 prime_supers(struct module_qstate* qstate, int id, struct module_qstate* forq)
2277 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2278 struct delegpt* dp = NULL;
2280 log_assert(qstate->is_priming || foriq->wait_priming_stub);
2281 log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2282 /* Convert our response to a delegation point */
2283 dp = delegpt_from_message(qstate->return_msg, forq->region);
2285 /* if there is no convertable delegation point, then
2286 * the ANSWER type was (presumably) a negative answer. */
2287 verbose(VERB_ALGO, "prime response was not a positive "
2290 foriq->state = QUERYTARGETS_STATE;
2294 log_query_info(VERB_DETAIL, "priming successful for", &qstate->qinfo);
2295 delegpt_log(VERB_ALGO, dp);
2297 foriq->deleg_msg = dns_copy_msg(qstate->return_msg, forq->region);
2298 if(!foriq->deleg_msg) {
2299 log_err("copy prime response: out of memory");
2301 foriq->state = QUERYTARGETS_STATE;
2305 /* root priming responses go to init stage 2, priming stub
2306 * responses to to stage 3. */
2307 if(foriq->wait_priming_stub) {
2308 foriq->state = INIT_REQUEST_3_STATE;
2309 foriq->wait_priming_stub = 0;
2310 } else foriq->state = INIT_REQUEST_2_STATE;
2311 /* because we are finished, the parent will be reactivated */
2315 * This handles the response to a priming query. This is used to handle both
2316 * root and stub priming responses. This is basically the equivalent of the
2317 * QUERY_RESP_STATE, but will not handle CNAME responses and will treat
2318 * REFERRALs as ANSWERS. It will also update and reactivate the originating
2321 * @param qstate: query state.
2322 * @param id: module id.
2323 * @return true if the event needs more immediate processing, false if not.
2324 * This state always returns false.
2327 processPrimeResponse(struct module_qstate* qstate, int id)
2329 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2330 enum response_type type;
2331 iq->response->rep->flags &= ~(BIT_RD|BIT_RA); /* ignore rec-lame */
2332 type = response_type_from_server(
2333 (int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2334 iq->response, &iq->qchase, iq->dp);
2335 if(type == RESPONSE_TYPE_ANSWER) {
2336 qstate->return_rcode = LDNS_RCODE_NOERROR;
2337 qstate->return_msg = iq->response;
2339 qstate->return_rcode = LDNS_RCODE_SERVFAIL;
2340 qstate->return_msg = NULL;
2343 /* validate the root or stub after priming (if enabled).
2344 * This is the same query as the prime query, but with validation.
2345 * Now that we are primed, the additional queries that validation
2346 * may need can be resolved, such as DLV. */
2347 if(qstate->env->cfg->harden_referral_path) {
2348 struct module_qstate* subq = NULL;
2349 log_nametypeclass(VERB_ALGO, "schedule prime validation",
2350 qstate->qinfo.qname, qstate->qinfo.qtype,
2351 qstate->qinfo.qclass);
2352 if(!generate_sub_request(qstate->qinfo.qname,
2353 qstate->qinfo.qname_len, qstate->qinfo.qtype,
2354 qstate->qinfo.qclass, qstate, id, iq,
2355 INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
2356 verbose(VERB_ALGO, "could not generate prime check");
2358 generate_a_aaaa_check(qstate, iq, id);
2361 /* This event is finished. */
2362 qstate->ext_state[id] = module_finished;
2367 * Do final processing on responses to target queries. Events reach this
2368 * state after the iterative resolution algorithm terminates. This state is
2369 * responsible for reactiving the original event, and housekeeping related
2370 * to received target responses (caching, updating the current delegation
2372 * Callback from walk_supers for every super state that is interested in
2373 * the results from this query.
2375 * @param qstate: query state.
2376 * @param id: module id.
2377 * @param forq: super query state.
2380 processTargetResponse(struct module_qstate* qstate, int id,
2381 struct module_qstate* forq)
2383 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2384 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2385 struct ub_packed_rrset_key* rrset;
2386 struct delegpt_ns* dpns;
2387 log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2389 foriq->state = QUERYTARGETS_STATE;
2390 log_query_info(VERB_ALGO, "processTargetResponse", &qstate->qinfo);
2391 log_query_info(VERB_ALGO, "processTargetResponse super", &forq->qinfo);
2393 /* check to see if parent event is still interested (in orig name). */
2395 verbose(VERB_ALGO, "subq: parent not interested, was reset");
2396 return; /* not interested anymore */
2398 dpns = delegpt_find_ns(foriq->dp, qstate->qinfo.qname,
2399 qstate->qinfo.qname_len);
2401 /* If not interested, just stop processing this event */
2402 verbose(VERB_ALGO, "subq: parent not interested anymore");
2403 /* could be because parent was jostled out of the cache,
2404 and a new identical query arrived, that does not want it*/
2408 /* Tell the originating event that this target query has finished
2409 * (regardless if it succeeded or not). */
2410 foriq->num_target_queries--;
2412 /* if iq->query_for_pside_glue then add the pside_glue (marked lame) */
2413 if(iq->pside_glue) {
2414 /* if the pside_glue is NULL, then it could not be found,
2415 * the done_pside is already set when created and a cache
2416 * entry created in processFinished so nothing to do here */
2417 log_rrset_key(VERB_ALGO, "add parentside glue to dp",
2419 if(!delegpt_add_rrset(foriq->dp, forq->region,
2421 log_err("out of memory adding pside glue");
2424 /* This response is relevant to the current query, so we
2425 * add (attempt to add, anyway) this target(s) and reactivate
2426 * the original event.
2427 * NOTE: we could only look for the AnswerRRset if the
2428 * response type was ANSWER. */
2429 rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
2431 /* if CNAMEs have been followed - add new NS to delegpt. */
2432 /* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
2433 if(!delegpt_find_ns(foriq->dp, rrset->rk.dname,
2434 rrset->rk.dname_len)) {
2435 /* if dpns->lame then set newcname ns lame too */
2436 if(!delegpt_add_ns(foriq->dp, forq->region,
2437 rrset->rk.dname, dpns->lame))
2438 log_err("out of memory adding cnamed-ns");
2440 /* if dpns->lame then set the address(es) lame too */
2441 if(!delegpt_add_rrset(foriq->dp, forq->region, rrset,
2443 log_err("out of memory adding targets");
2444 verbose(VERB_ALGO, "added target response");
2445 delegpt_log(VERB_ALGO, foriq->dp);
2447 verbose(VERB_ALGO, "iterator TargetResponse failed");
2448 dpns->resolved = 1; /* fail the target */
2453 * Process response for DS NS Find queries, that attempt to find the delegation
2454 * point where we ask the DS query from.
2456 * @param qstate: query state.
2457 * @param id: module id.
2458 * @param forq: super query state.
2461 processDSNSResponse(struct module_qstate* qstate, int id,
2462 struct module_qstate* forq)
2464 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2466 /* if the finished (iq->response) query has no NS set: continue
2467 * up to look for the right dp; nothing to change, do DPNSstate */
2468 if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2469 return; /* seek further */
2470 /* find the NS RRset (without allowing CNAMEs) */
2471 if(!reply_find_rrset(qstate->return_msg->rep, qstate->qinfo.qname,
2472 qstate->qinfo.qname_len, LDNS_RR_TYPE_NS,
2473 qstate->qinfo.qclass)){
2474 return; /* seek further */
2477 /* else, store as DP and continue at querytargets */
2478 foriq->state = QUERYTARGETS_STATE;
2479 foriq->dp = delegpt_from_message(qstate->return_msg, forq->region);
2481 log_err("out of memory in dsns dp alloc");
2482 return; /* dp==NULL in QUERYTARGETS makes SERVFAIL */
2484 /* success, go query the querytargets in the new dp (and go down) */
2488 * Process response for qclass=ANY queries for a particular class.
2489 * Append to result or error-exit.
2491 * @param qstate: query state.
2492 * @param id: module id.
2493 * @param forq: super query state.
2496 processClassResponse(struct module_qstate* qstate, int id,
2497 struct module_qstate* forq)
2499 struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2500 struct dns_msg* from = qstate->return_msg;
2501 log_query_info(VERB_ALGO, "processClassResponse", &qstate->qinfo);
2502 log_query_info(VERB_ALGO, "processClassResponse super", &forq->qinfo);
2503 if(qstate->return_rcode != LDNS_RCODE_NOERROR) {
2504 /* cause servfail for qclass ANY query */
2505 foriq->response = NULL;
2506 foriq->state = FINISHED_STATE;
2510 if(!foriq->response) {
2511 /* allocate the response: copy RCODE, sec_state */
2512 foriq->response = dns_copy_msg(from, forq->region);
2513 if(!foriq->response) {
2514 log_err("malloc failed for qclass ANY response");
2515 foriq->state = FINISHED_STATE;
2518 foriq->response->qinfo.qclass = forq->qinfo.qclass;
2519 /* qclass ANY does not receive the AA flag on replies */
2520 foriq->response->rep->authoritative = 0;
2522 struct dns_msg* to = foriq->response;
2523 /* add _from_ this response _to_ existing collection */
2524 /* if there are records, copy RCODE */
2525 /* lower sec_state if this message is lower */
2526 if(from->rep->rrset_count != 0) {
2527 size_t n = from->rep->rrset_count+to->rep->rrset_count;
2528 struct ub_packed_rrset_key** dest, **d;
2529 /* copy appropriate rcode */
2530 to->rep->flags = from->rep->flags;
2532 dest = regional_alloc(forq->region, sizeof(dest[0])*n);
2534 log_err("malloc failed in collect ANY");
2535 foriq->state = FINISHED_STATE;
2540 memcpy(dest, to->rep->rrsets, to->rep->an_numrrsets
2542 dest += to->rep->an_numrrsets;
2543 memcpy(dest, from->rep->rrsets, from->rep->an_numrrsets
2545 dest += from->rep->an_numrrsets;
2547 memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets,
2548 to->rep->ns_numrrsets * sizeof(dest[0]));
2549 dest += to->rep->ns_numrrsets;
2550 memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets,
2551 from->rep->ns_numrrsets * sizeof(dest[0]));
2552 dest += from->rep->ns_numrrsets;
2554 memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets+
2555 to->rep->ns_numrrsets,
2556 to->rep->ar_numrrsets * sizeof(dest[0]));
2557 dest += to->rep->ar_numrrsets;
2558 memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets+
2559 from->rep->ns_numrrsets,
2560 from->rep->ar_numrrsets * sizeof(dest[0]));
2562 to->rep->rrsets = d;
2563 to->rep->an_numrrsets += from->rep->an_numrrsets;
2564 to->rep->ns_numrrsets += from->rep->ns_numrrsets;
2565 to->rep->ar_numrrsets += from->rep->ar_numrrsets;
2566 to->rep->rrset_count = n;
2568 if(from->rep->security < to->rep->security) /* lowest sec */
2569 to->rep->security = from->rep->security;
2570 if(from->rep->qdcount != 0) /* insert qd if appropriate */
2571 to->rep->qdcount = from->rep->qdcount;
2572 if(from->rep->ttl < to->rep->ttl) /* use smallest TTL */
2573 to->rep->ttl = from->rep->ttl;
2574 if(from->rep->prefetch_ttl < to->rep->prefetch_ttl)
2575 to->rep->prefetch_ttl = from->rep->prefetch_ttl;
2578 foriq->num_current_queries --;
2579 if(foriq->num_current_queries == 0)
2580 foriq->state = FINISHED_STATE;
2584 * Collect class ANY responses and make them into one response. This
2585 * state is started and it creates queries for all classes (that have
2586 * root hints). The answers are then collected.
2588 * @param qstate: query state.
2589 * @param id: module id.
2590 * @return true if the event needs more immediate processing, false if not.
2593 processCollectClass(struct module_qstate* qstate, int id)
2595 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2596 struct module_qstate* subq;
2597 /* If qchase.qclass == 0 then send out queries for all classes.
2598 * Otherwise, do nothing (wait for all answers to arrive and the
2599 * processClassResponse to put them together, and that moves us
2600 * towards the Finished state when done. */
2601 if(iq->qchase.qclass == 0) {
2603 iq->qchase.qclass = LDNS_RR_CLASS_ANY;
2604 while(iter_get_next_root(qstate->env->hints,
2605 qstate->env->fwds, &c)) {
2606 /* generate query for this class */
2607 log_nametypeclass(VERB_ALGO, "spawn collect query",
2608 qstate->qinfo.qname, qstate->qinfo.qtype, c);
2609 if(!generate_sub_request(qstate->qinfo.qname,
2610 qstate->qinfo.qname_len, qstate->qinfo.qtype,
2611 c, qstate, id, iq, INIT_REQUEST_STATE,
2612 FINISHED_STATE, &subq,
2613 (int)!(qstate->query_flags&BIT_CD))) {
2614 return error_response(qstate, id,
2615 LDNS_RCODE_SERVFAIL);
2617 /* ignore subq, no special init required */
2618 iq->num_current_queries ++;
2623 /* if no roots are configured at all, return */
2624 if(iq->num_current_queries == 0) {
2625 verbose(VERB_ALGO, "No root hints or fwds, giving up "
2627 return error_response(qstate, id, LDNS_RCODE_REFUSED);
2629 /* return false, wait for queries to return */
2631 /* if woke up here because of an answer, wait for more answers */
2636 * This handles the final state for first-tier responses (i.e., responses to
2637 * externally generated queries).
2639 * @param qstate: query state.
2640 * @param iq: iterator query state.
2641 * @param id: module id.
2642 * @return true if the event needs more processing, false if not. Since this
2643 * is the final state for an event, it always returns false.
2646 processFinished(struct module_qstate* qstate, struct iter_qstate* iq,
2649 log_query_info(VERB_QUERY, "finishing processing for",
2652 /* store negative cache element for parent side glue. */
2653 if(iq->query_for_pside_glue && !iq->pside_glue)
2654 iter_store_parentside_neg(qstate->env, &qstate->qinfo,
2655 iq->deleg_msg?iq->deleg_msg->rep:
2656 (iq->response?iq->response->rep:NULL));
2658 verbose(VERB_ALGO, "No response is set, servfail");
2659 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2662 /* Make sure that the RA flag is set (since the presence of
2663 * this module means that recursion is available) */
2664 iq->response->rep->flags |= BIT_RA;
2666 /* Clear the AA flag */
2667 /* FIXME: does this action go here or in some other module? */
2668 iq->response->rep->flags &= ~BIT_AA;
2670 /* make sure QR flag is on */
2671 iq->response->rep->flags |= BIT_QR;
2673 /* we have finished processing this query */
2674 qstate->ext_state[id] = module_finished;
2676 /* TODO: we are using a private TTL, trim the response. */
2677 /* if (mPrivateTTL > 0){IterUtils.setPrivateTTL(resp, mPrivateTTL); } */
2679 /* prepend any items we have accumulated */
2680 if(iq->an_prepend_list || iq->ns_prepend_list) {
2681 if(!iter_prepend(iq, iq->response, qstate->region)) {
2682 log_err("prepend rrsets: out of memory");
2683 return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2685 /* reset the query name back */
2686 iq->response->qinfo = qstate->qinfo;
2687 /* the security state depends on the combination */
2688 iq->response->rep->security = sec_status_unchecked;
2689 /* store message with the finished prepended items,
2690 * but only if we did recursion. The nonrecursion referral
2691 * from cache does not need to be stored in the msg cache. */
2692 if(qstate->query_flags&BIT_RD) {
2693 iter_dns_store(qstate->env, &qstate->qinfo,
2694 iq->response->rep, 0, qstate->prefetch_leeway,
2695 iq->dp&&iq->dp->has_parent_side_NS,
2696 qstate->region, qstate->query_flags);
2699 qstate->return_rcode = LDNS_RCODE_NOERROR;
2700 qstate->return_msg = iq->response;
2705 * Return priming query results to interestes super querystates.
2707 * Sets the delegation point and delegation message (not nonRD queries).
2708 * This is a callback from walk_supers.
2710 * @param qstate: query state that finished.
2711 * @param id: module id.
2712 * @param super: the qstate to inform.
2715 iter_inform_super(struct module_qstate* qstate, int id,
2716 struct module_qstate* super)
2718 if(!qstate->is_priming && super->qinfo.qclass == LDNS_RR_CLASS_ANY)
2719 processClassResponse(qstate, id, super);
2720 else if(super->qinfo.qtype == LDNS_RR_TYPE_DS && ((struct iter_qstate*)
2721 super->minfo[id])->state == DSNS_FIND_STATE)
2722 processDSNSResponse(qstate, id, super);
2723 else if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2724 error_supers(qstate, id, super);
2725 else if(qstate->is_priming)
2726 prime_supers(qstate, id, super);
2727 else processTargetResponse(qstate, id, super);
2731 * Handle iterator state.
2732 * Handle events. This is the real processing loop for events, responsible
2733 * for moving events through the various states. If a processing method
2734 * returns true, then it will be advanced to the next state. If false, then
2735 * processing will stop.
2737 * @param qstate: query state.
2738 * @param ie: iterator shared global environment.
2739 * @param iq: iterator query state.
2740 * @param id: module id.
2743 iter_handle(struct module_qstate* qstate, struct iter_qstate* iq,
2744 struct iter_env* ie, int id)
2748 verbose(VERB_ALGO, "iter_handle processing q with state %s",
2749 iter_state_to_string(iq->state));
2751 case INIT_REQUEST_STATE:
2752 cont = processInitRequest(qstate, iq, ie, id);
2754 case INIT_REQUEST_2_STATE:
2755 cont = processInitRequest2(qstate, iq, id);
2757 case INIT_REQUEST_3_STATE:
2758 cont = processInitRequest3(qstate, iq, id);
2760 case QUERYTARGETS_STATE:
2761 cont = processQueryTargets(qstate, iq, ie, id);
2763 case QUERY_RESP_STATE:
2764 cont = processQueryResponse(qstate, iq, id);
2766 case PRIME_RESP_STATE:
2767 cont = processPrimeResponse(qstate, id);
2769 case COLLECT_CLASS_STATE:
2770 cont = processCollectClass(qstate, id);
2772 case DSNS_FIND_STATE:
2773 cont = processDSNSFind(qstate, iq, id);
2775 case FINISHED_STATE:
2776 cont = processFinished(qstate, iq, id);
2779 log_warn("iterator: invalid state: %d",
2788 * This is the primary entry point for processing request events. Note that
2789 * this method should only be used by external modules.
2790 * @param qstate: query state.
2791 * @param ie: iterator shared global environment.
2792 * @param iq: iterator query state.
2793 * @param id: module id.
2796 process_request(struct module_qstate* qstate, struct iter_qstate* iq,
2797 struct iter_env* ie, int id)
2799 /* external requests start in the INIT state, and finish using the
2800 * FINISHED state. */
2801 iq->state = INIT_REQUEST_STATE;
2802 iq->final_state = FINISHED_STATE;
2803 verbose(VERB_ALGO, "process_request: new external request event");
2804 iter_handle(qstate, iq, ie, id);
2807 /** process authoritative server reply */
2809 process_response(struct module_qstate* qstate, struct iter_qstate* iq,
2810 struct iter_env* ie, int id, struct outbound_entry* outbound,
2811 enum module_ev event)
2813 struct msg_parse* prs;
2814 struct edns_data edns;
2817 verbose(VERB_ALGO, "process_response: new external response event");
2818 iq->response = NULL;
2819 iq->state = QUERY_RESP_STATE;
2820 if(event == module_event_noreply || event == module_event_error) {
2821 if(event == module_event_noreply && iq->sent_count >= 3 &&
2822 qstate->env->cfg->use_caps_bits_for_id &&
2823 !iq->caps_fallback) {
2824 /* start fallback */
2825 iq->caps_fallback = 1;
2826 iq->caps_server = 0;
2827 iq->caps_reply = NULL;
2828 iq->state = QUERYTARGETS_STATE;
2829 iq->num_current_queries--;
2830 /* need fresh attempts for the 0x20 fallback, if
2831 * that was the cause for the failure */
2832 iter_dec_attempts(iq->dp, 3);
2833 verbose(VERB_DETAIL, "Capsforid: timeouts, starting fallback");
2838 if( (event != module_event_reply && event != module_event_capsfail)
2839 || !qstate->reply) {
2840 log_err("Bad event combined with response");
2841 outbound_list_remove(&iq->outlist, outbound);
2842 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2847 prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
2848 sizeof(struct msg_parse));
2850 log_err("out of memory on incoming message");
2851 /* like packet got dropped */
2854 memset(prs, 0, sizeof(*prs));
2855 memset(&edns, 0, sizeof(edns));
2856 pkt = qstate->reply->c->buffer;
2857 sldns_buffer_set_position(pkt, 0);
2858 if(parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
2859 verbose(VERB_ALGO, "parse error on reply packet");
2862 /* edns is not examined, but removed from message to help cache */
2863 if(parse_extract_edns(prs, &edns) != LDNS_RCODE_NOERROR)
2865 /* remove CD-bit, we asked for in case we handle validation ourself */
2866 prs->flags &= ~BIT_CD;
2868 /* normalize and sanitize: easy to delete items from linked lists */
2869 if(!scrub_message(pkt, prs, &iq->qchase, iq->dp->name,
2870 qstate->env->scratch, qstate->env, ie))
2873 /* allocate response dns_msg in region */
2874 iq->response = dns_alloc_msg(pkt, prs, qstate->region);
2877 log_query_info(VERB_DETAIL, "response for", &qstate->qinfo);
2878 log_name_addr(VERB_DETAIL, "reply from", iq->dp->name,
2879 &qstate->reply->addr, qstate->reply->addrlen);
2880 if(verbosity >= VERB_ALGO)
2881 log_dns_msg("incoming scrubbed packet:", &iq->response->qinfo,
2884 if(event == module_event_capsfail || iq->caps_fallback) {
2885 /* for fallback we care about main answer, not additionals */
2886 /* removing that makes comparison more likely to succeed */
2887 caps_strip_reply(iq->response->rep);
2888 if(!iq->caps_fallback) {
2889 /* start fallback */
2890 iq->caps_fallback = 1;
2891 iq->caps_server = 0;
2892 iq->caps_reply = iq->response->rep;
2893 iq->state = QUERYTARGETS_STATE;
2894 iq->num_current_queries--;
2895 verbose(VERB_DETAIL, "Capsforid: starting fallback");
2898 /* check if reply is the same, otherwise, fail */
2899 if(!iq->caps_reply) {
2900 iq->caps_reply = iq->response->rep;
2901 iq->caps_server = -1; /*become zero at ++,
2902 so that we start the full set of trials */
2903 } else if(!reply_equal(iq->response->rep, iq->caps_reply,
2904 qstate->env->scratch)) {
2905 verbose(VERB_DETAIL, "Capsforid fallback: "
2906 "getting different replies, failed");
2907 outbound_list_remove(&iq->outlist, outbound);
2908 (void)error_response(qstate, id,
2909 LDNS_RCODE_SERVFAIL);
2912 /* continue the fallback procedure at next server */
2914 iq->state = QUERYTARGETS_STATE;
2915 iq->num_current_queries--;
2916 verbose(VERB_DETAIL, "Capsforid: reply is equal. "
2917 "go to next fallback");
2921 iq->caps_fallback = 0; /* if we were in fallback, 0x20 is OK now */
2924 outbound_list_remove(&iq->outlist, outbound);
2925 iter_handle(qstate, iq, ie, id);
2929 iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
2930 struct outbound_entry* outbound)
2932 struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
2933 struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2934 verbose(VERB_QUERY, "iterator[module %d] operate: extstate:%s event:%s",
2935 id, strextstate(qstate->ext_state[id]), strmodulevent(event));
2936 if(iq) log_query_info(VERB_QUERY, "iterator operate: query",
2938 if(iq && qstate->qinfo.qname != iq->qchase.qname)
2939 log_query_info(VERB_QUERY, "iterator operate: chased to",
2942 /* perform iterator state machine */
2943 if((event == module_event_new || event == module_event_pass) &&
2945 if(!iter_new(qstate, id)) {
2946 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2949 iq = (struct iter_qstate*)qstate->minfo[id];
2950 process_request(qstate, iq, ie, id);
2953 if(iq && event == module_event_pass) {
2954 iter_handle(qstate, iq, ie, id);
2957 if(iq && outbound) {
2958 process_response(qstate, iq, ie, id, outbound, event);
2961 if(event == module_event_error) {
2962 verbose(VERB_ALGO, "got called with event error, giving up");
2963 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2967 log_err("bad event for iterator");
2968 (void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2972 iter_clear(struct module_qstate* qstate, int id)
2974 struct iter_qstate* iq;
2977 iq = (struct iter_qstate*)qstate->minfo[id];
2979 outbound_list_clear(&iq->outlist);
2980 if(iq->target_count && --iq->target_count[0] == 0)
2981 free(iq->target_count);
2982 iq->num_current_queries = 0;
2984 qstate->minfo[id] = NULL;
2988 iter_get_mem(struct module_env* env, int id)
2990 struct iter_env* ie = (struct iter_env*)env->modinfo[id];
2993 return sizeof(*ie) + sizeof(int)*((size_t)ie->max_dependency_depth+1)
2994 + donotq_get_mem(ie->donotq) + priv_get_mem(ie->priv);
2998 * The iterator function block
3000 static struct module_func_block iter_block = {
3002 &iter_init, &iter_deinit, &iter_operate, &iter_inform_super,
3003 &iter_clear, &iter_get_mem
3006 struct module_func_block*
3007 iter_get_funcblock(void)
3013 iter_state_to_string(enum iter_state state)
3017 case INIT_REQUEST_STATE :
3018 return "INIT REQUEST STATE";
3019 case INIT_REQUEST_2_STATE :
3020 return "INIT REQUEST STATE (stage 2)";
3021 case INIT_REQUEST_3_STATE:
3022 return "INIT REQUEST STATE (stage 3)";
3023 case QUERYTARGETS_STATE :
3024 return "QUERY TARGETS STATE";
3025 case PRIME_RESP_STATE :
3026 return "PRIME RESPONSE STATE";
3027 case COLLECT_CLASS_STATE :
3028 return "COLLECT CLASS STATE";
3029 case DSNS_FIND_STATE :
3030 return "DSNS FIND STATE";
3031 case QUERY_RESP_STATE :
3032 return "QUERY RESPONSE STATE";
3033 case FINISHED_STATE :
3034 return "FINISHED RESPONSE STATE";
3036 return "UNKNOWN ITER STATE";
3041 iter_state_is_responsestate(enum iter_state s)
3044 case INIT_REQUEST_STATE :
3045 case INIT_REQUEST_2_STATE :
3046 case INIT_REQUEST_3_STATE :
3047 case QUERYTARGETS_STATE :
3048 case COLLECT_CLASS_STATE :