2 * services/mesh.c - deal with mesh of query states and handle events for that.
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 functions to assist in dealing with a mesh of
40 * query states. This mesh is supposed to be thread-specific.
41 * It consists of query states (per qname, qtype, qclass) and connections
42 * between query states and the super and subquery states, and replies to
43 * send back to clients.
46 #include "services/mesh.h"
47 #include "services/outbound_list.h"
48 #include "services/cache/dns.h"
49 #include "services/cache/rrset.h"
51 #include "util/net_help.h"
52 #include "util/module.h"
53 #include "util/regional.h"
54 #include "util/data/msgencode.h"
55 #include "util/timehist.h"
56 #include "util/fptr_wlist.h"
57 #include "util/alloc.h"
58 #include "util/config_file.h"
59 #include "util/edns.h"
60 #include "sldns/sbuffer.h"
61 #include "sldns/wire2str.h"
62 #include "services/localzone.h"
63 #include "util/data/dname.h"
64 #include "respip/respip.h"
65 #include "services/listen_dnsport.h"
66 #include "util/timeval_func.h"
69 #include "edns-subnet/subnetmod.h"
70 #include "edns-subnet/edns-subnet.h"
74 * Compare two response-ip client info entries for the purpose of mesh state
75 * compare. It returns 0 if ci_a and ci_b are considered equal; otherwise
76 * 1 or -1 (they mean 'ci_a is larger/smaller than ci_b', respectively, but
77 * in practice it should be only used to mean they are different).
78 * We cannot share the mesh state for two queries if different response-ip
79 * actions can apply in the end, even if those queries are otherwise identical.
80 * For this purpose we compare tag lists and tag action lists; they should be
81 * identical to share the same state.
82 * For tag data, we don't look into the data content, as it can be
83 * expensive; unless tag data are not defined for both or they point to the
84 * exact same data in memory (i.e., they come from the same ACL entry), we
85 * consider these data different.
86 * Likewise, if the client info is associated with views, we don't look into
87 * the views. They are considered different unless they are exactly the same
88 * even if the views only differ in the names.
91 client_info_compare(const struct respip_client_info* ci_a,
92 const struct respip_client_info* ci_b)
102 if(ci_a->taglen != ci_b->taglen)
103 return (ci_a->taglen < ci_b->taglen) ? -1 : 1;
104 if(ci_a->taglist && !ci_b->taglist)
106 if(!ci_a->taglist && ci_b->taglist)
108 if(ci_a->taglist && ci_b->taglist) {
109 cmp = memcmp(ci_a->taglist, ci_b->taglist, ci_a->taglen);
113 if(ci_a->tag_actions_size != ci_b->tag_actions_size)
114 return (ci_a->tag_actions_size < ci_b->tag_actions_size) ?
116 if(ci_a->tag_actions && !ci_b->tag_actions)
118 if(!ci_a->tag_actions && ci_b->tag_actions)
120 if(ci_a->tag_actions && ci_b->tag_actions) {
121 cmp = memcmp(ci_a->tag_actions, ci_b->tag_actions,
122 ci_a->tag_actions_size);
126 if(ci_a->tag_datas != ci_b->tag_datas)
127 return ci_a->tag_datas < ci_b->tag_datas ? -1 : 1;
128 if(ci_a->view != ci_b->view)
129 return ci_a->view < ci_b->view ? -1 : 1;
130 /* For the unbound daemon these should be non-NULL and identical,
131 * but we check that just in case. */
132 if(ci_a->respip_set != ci_b->respip_set)
133 return ci_a->respip_set < ci_b->respip_set ? -1 : 1;
138 mesh_state_compare(const void* ap, const void* bp)
140 struct mesh_state* a = (struct mesh_state*)ap;
141 struct mesh_state* b = (struct mesh_state*)bp;
144 if(a->unique < b->unique)
146 if(a->unique > b->unique)
149 if(a->s.is_priming && !b->s.is_priming)
151 if(!a->s.is_priming && b->s.is_priming)
154 if(a->s.is_valrec && !b->s.is_valrec)
156 if(!a->s.is_valrec && b->s.is_valrec)
159 if((a->s.query_flags&BIT_RD) && !(b->s.query_flags&BIT_RD))
161 if(!(a->s.query_flags&BIT_RD) && (b->s.query_flags&BIT_RD))
164 if((a->s.query_flags&BIT_CD) && !(b->s.query_flags&BIT_CD))
166 if(!(a->s.query_flags&BIT_CD) && (b->s.query_flags&BIT_CD))
169 cmp = query_info_compare(&a->s.qinfo, &b->s.qinfo);
172 return client_info_compare(a->s.client_info, b->s.client_info);
176 mesh_state_ref_compare(const void* ap, const void* bp)
178 struct mesh_state_ref* a = (struct mesh_state_ref*)ap;
179 struct mesh_state_ref* b = (struct mesh_state_ref*)bp;
180 return mesh_state_compare(a->s, b->s);
184 mesh_create(struct module_stack* stack, struct module_env* env)
186 struct mesh_area* mesh = calloc(1, sizeof(struct mesh_area));
188 log_err("mesh area alloc: out of memory");
191 mesh->histogram = timehist_setup();
192 mesh->qbuf_bak = sldns_buffer_new(env->cfg->msg_buffer_size);
193 if(!mesh->histogram || !mesh->qbuf_bak) {
195 log_err("mesh area alloc: out of memory");
200 rbtree_init(&mesh->run, &mesh_state_compare);
201 rbtree_init(&mesh->all, &mesh_state_compare);
202 mesh->num_reply_addrs = 0;
203 mesh->num_reply_states = 0;
204 mesh->num_detached_states = 0;
205 mesh->num_forever_states = 0;
206 mesh->stats_jostled = 0;
207 mesh->stats_dropped = 0;
208 mesh->ans_expired = 0;
209 mesh->ans_cachedb = 0;
210 mesh->max_reply_states = env->cfg->num_queries_per_thread;
211 mesh->max_forever_states = (mesh->max_reply_states+1)/2;
213 mesh->jostle_max.tv_sec = (time_t)(env->cfg->jostle_time / 1000);
214 mesh->jostle_max.tv_usec = (time_t)((env->cfg->jostle_time % 1000)
220 /** help mesh delete delete mesh states */
222 mesh_delete_helper(rbnode_type* n)
224 struct mesh_state* mstate = (struct mesh_state*)n->key;
225 /* perform a full delete, not only 'cleanup' routine,
226 * because other callbacks expect a clean state in the mesh.
227 * For 're-entrant' calls */
228 mesh_state_delete(&mstate->s);
229 /* but because these delete the items from the tree, postorder
230 * traversal and rbtree rebalancing do not work together */
234 mesh_delete(struct mesh_area* mesh)
238 /* free all query states */
239 while(mesh->all.count)
240 mesh_delete_helper(mesh->all.root);
241 timehist_delete(mesh->histogram);
242 sldns_buffer_free(mesh->qbuf_bak);
247 mesh_delete_all(struct mesh_area* mesh)
249 /* free all query states */
250 while(mesh->all.count)
251 mesh_delete_helper(mesh->all.root);
252 mesh->stats_dropped += mesh->num_reply_addrs;
253 /* clear mesh area references */
254 rbtree_init(&mesh->run, &mesh_state_compare);
255 rbtree_init(&mesh->all, &mesh_state_compare);
256 mesh->num_reply_addrs = 0;
257 mesh->num_reply_states = 0;
258 mesh->num_detached_states = 0;
259 mesh->num_forever_states = 0;
260 mesh->forever_first = NULL;
261 mesh->forever_last = NULL;
262 mesh->jostle_first = NULL;
263 mesh->jostle_last = NULL;
266 int mesh_make_new_space(struct mesh_area* mesh, sldns_buffer* qbuf)
268 struct mesh_state* m = mesh->jostle_first;
269 /* free space is available */
270 if(mesh->num_reply_states < mesh->max_reply_states)
272 /* try to kick out a jostle-list item */
273 if(m && m->reply_list && m->list_select == mesh_jostle_list) {
276 timeval_subtract(&age, mesh->env->now_tv,
277 &m->reply_list->start_time);
278 if(timeval_smaller(&mesh->jostle_max, &age)) {
280 log_nametypeclass(VERB_ALGO, "query jostled out to "
281 "make space for a new one",
282 m->s.qinfo.qname, m->s.qinfo.qtype,
284 /* backup the query */
285 if(qbuf) sldns_buffer_copy(mesh->qbuf_bak, qbuf);
287 if(m->super_set.count > 0) {
288 verbose(VERB_ALGO, "notify supers of failure");
289 m->s.return_msg = NULL;
290 m->s.return_rcode = LDNS_RCODE_SERVFAIL;
291 mesh_walk_supers(mesh, m);
293 mesh->stats_jostled ++;
294 mesh_state_delete(&m->s);
295 /* restore the query - note that the qinfo ptr to
296 * the querybuffer is then correct again. */
297 if(qbuf) sldns_buffer_copy(qbuf, mesh->qbuf_bak);
301 /* no space for new item */
306 mesh_serve_expired_lookup(struct module_qstate* qstate,
307 struct query_info* lookup_qinfo)
310 struct lruhash_entry* e;
312 struct reply_info* data;
313 struct msgreply_entry* key;
314 time_t timenow = *qstate->env->now;
315 int must_validate = (!(qstate->query_flags&BIT_CD)
316 || qstate->env->cfg->ignore_cd) && qstate->env->need_to_validate;
318 h = query_info_hash(lookup_qinfo, qstate->query_flags);
319 e = slabhash_lookup(qstate->env->msg_cache, h, lookup_qinfo, 0);
322 key = (struct msgreply_entry*)e->key;
323 data = (struct reply_info*)e->data;
324 msg = tomsg(qstate->env, &key->key, data, qstate->region, timenow,
325 qstate->env->cfg->serve_expired, qstate->env->scratch);
329 /* Check CNAME chain (if any)
330 * This is part of tomsg above; no need to check now. */
332 /* Check security status of the cached answer.
333 * tomsg above has a subset of these checks, so we are leaving
335 * In case of bogus or revalidation we don't care to reply here. */
336 if(must_validate && (msg->rep->security == sec_status_bogus ||
337 msg->rep->security == sec_status_secure_sentinel_fail)) {
338 verbose(VERB_ALGO, "Serve expired: bogus answer found in cache");
340 } else if(msg->rep->security == sec_status_unchecked && must_validate) {
341 verbose(VERB_ALGO, "Serve expired: unchecked entry needs "
343 goto bail_out; /* need to validate cache entry first */
344 } else if(msg->rep->security == sec_status_secure &&
345 !reply_all_rrsets_secure(msg->rep) && must_validate) {
346 verbose(VERB_ALGO, "Serve expired: secure entry"
348 goto bail_out; /* rrset changed, re-verify */
351 lock_rw_unlock(&e->lock);
355 lock_rw_unlock(&e->lock);
360 /** Init the serve expired data structure */
362 mesh_serve_expired_init(struct mesh_state* mstate, int timeout)
366 /* Create serve_expired_data if not there yet */
367 if(!mstate->s.serve_expired_data) {
368 mstate->s.serve_expired_data = (struct serve_expired_data*)
370 mstate->s.region, sizeof(struct serve_expired_data));
371 if(!mstate->s.serve_expired_data)
375 /* Don't overwrite the function if already set */
376 mstate->s.serve_expired_data->get_cached_answer =
377 mstate->s.serve_expired_data->get_cached_answer?
378 mstate->s.serve_expired_data->get_cached_answer:
379 &mesh_serve_expired_lookup;
381 /* In case this timer already popped, start it again */
382 if(!mstate->s.serve_expired_data->timer) {
383 mstate->s.serve_expired_data->timer = comm_timer_create(
384 mstate->s.env->worker_base, mesh_serve_expired_callback, mstate);
385 if(!mstate->s.serve_expired_data->timer)
388 t.tv_sec = timeout/1000;
389 t.tv_usec = (timeout%1000)*1000;
391 comm_timer_set(mstate->s.serve_expired_data->timer, &t);
396 void mesh_new_client(struct mesh_area* mesh, struct query_info* qinfo,
397 struct respip_client_info* cinfo, uint16_t qflags,
398 struct edns_data* edns, struct comm_reply* rep, uint16_t qid,
401 struct mesh_state* s = NULL;
402 int unique = unique_mesh_state(edns->opt_list_in, mesh->env);
403 int was_detached = 0;
406 int timeout = mesh->env->cfg->serve_expired?
407 mesh->env->cfg->serve_expired_client_timeout:0;
408 struct sldns_buffer* r_buffer = rep->c->buffer;
409 if(rep->c->tcp_req_info) {
410 r_buffer = rep->c->tcp_req_info->spool_buffer;
413 s = mesh_area_find(mesh, cinfo, qinfo, qflags&(BIT_RD|BIT_CD), 0, 0);
414 /* does this create a new reply state? */
415 if(!s || s->list_select == mesh_no_list) {
416 if(!mesh_make_new_space(mesh, rep->c->buffer)) {
417 verbose(VERB_ALGO, "Too many queries. dropping "
419 comm_point_drop_reply(rep);
420 mesh->stats_dropped++;
423 /* for this new reply state, the reply address is free,
424 * so the limit of reply addresses does not stop reply states*/
426 /* protect our memory usage from storing reply addresses */
427 if(mesh->num_reply_addrs > mesh->max_reply_states*16) {
428 verbose(VERB_ALGO, "Too many requests queued. "
429 "dropping incoming query.");
430 comm_point_drop_reply(rep);
431 mesh->stats_dropped++;
435 /* see if it already exists, if not, create one */
438 struct rbnode_type* n;
440 s = mesh_state_create(mesh->env, qinfo, cinfo,
441 qflags&(BIT_RD|BIT_CD), 0, 0);
443 log_err("mesh_state_create: out of memory; SERVFAIL");
444 if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, NULL, NULL,
445 LDNS_RCODE_SERVFAIL, edns, rep, mesh->env->scratch, mesh->env->now_tv))
446 edns->opt_list_inplace_cb_out = NULL;
447 error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
448 qinfo, qid, qflags, edns);
449 comm_point_send_reply(rep);
452 /* set detached (it is now) */
453 mesh->num_detached_states++;
455 mesh_state_make_unique(s);
456 s->s.rpz_passthru = rpz_passthru;
457 /* copy the edns options we got from the front */
458 if(edns->opt_list_in) {
459 s->s.edns_opts_front_in = edns_opt_copy_region(edns->opt_list_in,
461 if(!s->s.edns_opts_front_in) {
462 log_err("edns_opt_copy_region: out of memory; SERVFAIL");
463 if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, NULL,
464 NULL, LDNS_RCODE_SERVFAIL, edns, rep, mesh->env->scratch, mesh->env->now_tv))
465 edns->opt_list_inplace_cb_out = NULL;
466 error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
467 qinfo, qid, qflags, edns);
468 comm_point_send_reply(rep);
469 mesh_state_delete(&s->s);
479 rbtree_insert(&mesh->all, &s->node);
480 log_assert(n != NULL);
483 if(!s->reply_list && !s->cb_list) {
485 if(s->super_set.count == 0) {
490 if(!mesh_state_add_reply(s, edns, rep, qid, qflags, qinfo)) {
491 log_err("mesh_new_client: out of memory; SERVFAIL");
494 if(rep->c->tcp_req_info) {
495 if(!tcp_req_info_add_meshstate(rep->c->tcp_req_info, mesh, s)) {
496 log_err("mesh_new_client: out of memory add tcpreqinfo");
501 http2_stream_add_meshstate(rep->c->h2_stream, mesh, s);
503 /* add serve expired timer if required and not already there */
504 if(timeout && !mesh_serve_expired_init(s, timeout)) {
505 log_err("mesh_new_client: out of memory initializing serve expired");
508 /* update statistics */
510 log_assert(mesh->num_detached_states > 0);
511 mesh->num_detached_states--;
514 mesh->num_reply_states ++;
516 mesh->num_reply_addrs++;
517 if(s->list_select == mesh_no_list) {
518 /* move to either the forever or the jostle_list */
519 if(mesh->num_forever_states < mesh->max_forever_states) {
520 mesh->num_forever_states ++;
521 mesh_list_insert(s, &mesh->forever_first,
522 &mesh->forever_last);
523 s->list_select = mesh_forever_list;
525 mesh_list_insert(s, &mesh->jostle_first,
527 s->list_select = mesh_jostle_list;
531 mesh_run(mesh, s, module_event_new, NULL);
535 if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, &s->s,
536 NULL, LDNS_RCODE_SERVFAIL, edns, rep, mesh->env->scratch, mesh->env->now_tv))
537 edns->opt_list_inplace_cb_out = NULL;
538 error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
539 qinfo, qid, qflags, edns);
540 comm_point_send_reply(rep);
542 mesh_state_delete(&s->s);
547 mesh_new_callback(struct mesh_area* mesh, struct query_info* qinfo,
548 uint16_t qflags, struct edns_data* edns, sldns_buffer* buf,
549 uint16_t qid, mesh_cb_func_type cb, void* cb_arg, int rpz_passthru)
551 struct mesh_state* s = NULL;
552 int unique = unique_mesh_state(edns->opt_list_in, mesh->env);
553 int timeout = mesh->env->cfg->serve_expired?
554 mesh->env->cfg->serve_expired_client_timeout:0;
555 int was_detached = 0;
559 s = mesh_area_find(mesh, NULL, qinfo, qflags&(BIT_RD|BIT_CD), 0, 0);
561 /* there are no limits on the number of callbacks */
563 /* see if it already exists, if not, create one */
566 struct rbnode_type* n;
568 s = mesh_state_create(mesh->env, qinfo, NULL,
569 qflags&(BIT_RD|BIT_CD), 0, 0);
573 /* set detached (it is now) */
574 mesh->num_detached_states++;
576 mesh_state_make_unique(s);
577 s->s.rpz_passthru = rpz_passthru;
578 if(edns->opt_list_in) {
579 s->s.edns_opts_front_in = edns_opt_copy_region(edns->opt_list_in,
581 if(!s->s.edns_opts_front_in) {
582 mesh_state_delete(&s->s);
591 rbtree_insert(&mesh->all, &s->node);
592 log_assert(n != NULL);
595 if(!s->reply_list && !s->cb_list) {
597 if(s->super_set.count == 0) {
602 if(!mesh_state_add_cb(s, edns, buf, cb, cb_arg, qid, qflags)) {
604 mesh_state_delete(&s->s);
607 /* add serve expired timer if not already there */
608 if(timeout && !mesh_serve_expired_init(s, timeout)) {
610 mesh_state_delete(&s->s);
613 /* update statistics */
615 log_assert(mesh->num_detached_states > 0);
616 mesh->num_detached_states--;
619 mesh->num_reply_states ++;
621 mesh->num_reply_addrs++;
623 mesh_run(mesh, s, module_event_new, NULL);
627 /* Internal backend routine of mesh_new_prefetch(). It takes one additional
628 * parameter, 'run', which controls whether to run the prefetch state
629 * immediately. When this function is called internally 'run' could be
630 * 0 (false), in which case the new state is only made runnable so it
631 * will not be run recursively on top of the current state. */
632 static void mesh_schedule_prefetch(struct mesh_area* mesh,
633 struct query_info* qinfo, uint16_t qflags, time_t leeway, int run,
636 struct mesh_state* s = mesh_area_find(mesh, NULL, qinfo,
637 qflags&(BIT_RD|BIT_CD), 0, 0);
639 struct rbnode_type* n;
641 /* already exists, and for a different purpose perhaps.
642 * if mesh_no_list, keep it that way. */
644 /* make it ignore the cache from now on */
646 sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
647 if(s->s.prefetch_leeway < leeway)
648 s->s.prefetch_leeway = leeway;
651 if(!mesh_make_new_space(mesh, NULL)) {
652 verbose(VERB_ALGO, "Too many queries. dropped prefetch.");
653 mesh->stats_dropped ++;
657 s = mesh_state_create(mesh->env, qinfo, NULL,
658 qflags&(BIT_RD|BIT_CD), 0, 0);
660 log_err("prefetch mesh_state_create: out of memory");
668 rbtree_insert(&mesh->all, &s->node);
669 log_assert(n != NULL);
670 /* set detached (it is now) */
671 mesh->num_detached_states++;
672 /* make it ignore the cache */
673 sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
674 s->s.prefetch_leeway = leeway;
676 if(s->list_select == mesh_no_list) {
677 /* move to either the forever or the jostle_list */
678 if(mesh->num_forever_states < mesh->max_forever_states) {
679 mesh->num_forever_states ++;
680 mesh_list_insert(s, &mesh->forever_first,
681 &mesh->forever_last);
682 s->list_select = mesh_forever_list;
684 mesh_list_insert(s, &mesh->jostle_first,
686 s->list_select = mesh_jostle_list;
689 s->s.rpz_passthru = rpz_passthru;
697 rbtree_insert(&mesh->run, &s->run_node);
698 log_assert(n != NULL);
702 mesh_run(mesh, s, module_event_new, NULL);
706 /* Same logic as mesh_schedule_prefetch but tailored to the subnet module logic
707 * like passing along the comm_reply info. This will be faked into an EDNS
708 * option for processing by the subnet module if the client has not already
709 * attached its own ECS data. */
710 static void mesh_schedule_prefetch_subnet(struct mesh_area* mesh,
711 struct query_info* qinfo, uint16_t qflags, time_t leeway, int run,
712 int rpz_passthru, struct sockaddr_storage* addr, struct edns_option* edns_list)
714 struct mesh_state* s = NULL;
715 struct edns_option* opt = NULL;
717 struct rbnode_type* n;
719 if(!mesh_make_new_space(mesh, NULL)) {
720 verbose(VERB_ALGO, "Too many queries. dropped prefetch.");
721 mesh->stats_dropped ++;
725 s = mesh_state_create(mesh->env, qinfo, NULL,
726 qflags&(BIT_RD|BIT_CD), 0, 0);
728 log_err("prefetch_subnet mesh_state_create: out of memory");
731 mesh_state_make_unique(s);
733 opt = edns_opt_list_find(edns_list, mesh->env->cfg->client_subnet_opcode);
735 /* Use the client's ECS data */
736 if(!edns_opt_list_append(&s->s.edns_opts_front_in, opt->opt_code,
737 opt->opt_len, opt->opt_data, s->s.region)) {
738 log_err("prefetch_subnet edns_opt_list_append: out of memory");
742 /* Store the client's address. Later in the subnet module,
743 * it is decided whether to include an ECS option or not.
745 s->s.client_addr = *addr;
752 rbtree_insert(&mesh->all, &s->node);
753 log_assert(n != NULL);
754 /* set detached (it is now) */
755 mesh->num_detached_states++;
756 /* make it ignore the cache */
757 sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
758 s->s.prefetch_leeway = leeway;
760 if(s->list_select == mesh_no_list) {
761 /* move to either the forever or the jostle_list */
762 if(mesh->num_forever_states < mesh->max_forever_states) {
763 mesh->num_forever_states ++;
764 mesh_list_insert(s, &mesh->forever_first,
765 &mesh->forever_last);
766 s->list_select = mesh_forever_list;
768 mesh_list_insert(s, &mesh->jostle_first,
770 s->list_select = mesh_jostle_list;
773 s->s.rpz_passthru = rpz_passthru;
781 rbtree_insert(&mesh->run, &s->run_node);
782 log_assert(n != NULL);
786 mesh_run(mesh, s, module_event_new, NULL);
788 #endif /* CLIENT_SUBNET */
790 void mesh_new_prefetch(struct mesh_area* mesh, struct query_info* qinfo,
791 uint16_t qflags, time_t leeway, int rpz_passthru,
792 struct sockaddr_storage* addr, struct edns_option* opt_list)
798 mesh_schedule_prefetch_subnet(mesh, qinfo, qflags, leeway, 1,
799 rpz_passthru, addr, opt_list);
802 mesh_schedule_prefetch(mesh, qinfo, qflags, leeway, 1,
806 void mesh_report_reply(struct mesh_area* mesh, struct outbound_entry* e,
807 struct comm_reply* reply, int what)
809 enum module_ev event = module_event_reply;
810 e->qstate->reply = reply;
811 if(what != NETEVENT_NOERROR) {
812 event = module_event_noreply;
813 if(what == NETEVENT_CAPSFAIL)
814 event = module_event_capsfail;
816 mesh_run(mesh, e->qstate->mesh_info, event, e);
820 mesh_state_create(struct module_env* env, struct query_info* qinfo,
821 struct respip_client_info* cinfo, uint16_t qflags, int prime,
824 struct regional* region = alloc_reg_obtain(env->alloc);
825 struct mesh_state* mstate;
829 mstate = (struct mesh_state*)regional_alloc(region,
830 sizeof(struct mesh_state));
832 alloc_reg_release(env->alloc, region);
835 memset(mstate, 0, sizeof(*mstate));
836 mstate->node = *RBTREE_NULL;
837 mstate->run_node = *RBTREE_NULL;
838 mstate->node.key = mstate;
839 mstate->run_node.key = mstate;
840 mstate->reply_list = NULL;
841 mstate->list_select = mesh_no_list;
842 mstate->replies_sent = 0;
843 rbtree_init(&mstate->super_set, &mesh_state_ref_compare);
844 rbtree_init(&mstate->sub_set, &mesh_state_ref_compare);
845 mstate->num_activated = 0;
846 mstate->unique = NULL;
847 /* init module qstate */
848 mstate->s.qinfo.qtype = qinfo->qtype;
849 mstate->s.qinfo.qclass = qinfo->qclass;
850 mstate->s.qinfo.local_alias = NULL;
851 mstate->s.qinfo.qname_len = qinfo->qname_len;
852 mstate->s.qinfo.qname = regional_alloc_init(region, qinfo->qname,
854 if(!mstate->s.qinfo.qname) {
855 alloc_reg_release(env->alloc, region);
859 mstate->s.client_info = regional_alloc_init(region, cinfo,
861 if(!mstate->s.client_info) {
862 alloc_reg_release(env->alloc, region);
866 /* remove all weird bits from qflags */
867 mstate->s.query_flags = (qflags & (BIT_RD|BIT_CD));
868 mstate->s.is_priming = prime;
869 mstate->s.is_valrec = valrec;
870 mstate->s.reply = NULL;
871 mstate->s.region = region;
872 mstate->s.curmod = 0;
873 mstate->s.return_msg = 0;
874 mstate->s.return_rcode = LDNS_RCODE_NOERROR;
876 mstate->s.mesh_info = mstate;
877 mstate->s.prefetch_leeway = 0;
878 mstate->s.serve_expired_data = NULL;
879 mstate->s.no_cache_lookup = 0;
880 mstate->s.no_cache_store = 0;
881 mstate->s.need_refetch = 0;
882 mstate->s.was_ratelimited = 0;
883 mstate->s.qstarttime = *env->now;
886 for(i=0; i<env->mesh->mods.num; i++) {
887 mstate->s.minfo[i] = NULL;
888 mstate->s.ext_state[i] = module_state_initial;
890 /* init edns option lists */
891 mstate->s.edns_opts_front_in = NULL;
892 mstate->s.edns_opts_back_out = NULL;
893 mstate->s.edns_opts_back_in = NULL;
894 mstate->s.edns_opts_front_out = NULL;
900 mesh_state_make_unique(struct mesh_state* mstate)
902 mstate->unique = mstate;
906 mesh_state_cleanup(struct mesh_state* mstate)
908 struct mesh_area* mesh;
912 mesh = mstate->s.env->mesh;
913 /* Stop and delete the serve expired timer */
914 if(mstate->s.serve_expired_data && mstate->s.serve_expired_data->timer) {
915 comm_timer_delete(mstate->s.serve_expired_data->timer);
916 mstate->s.serve_expired_data->timer = NULL;
918 /* drop unsent replies */
919 if(!mstate->replies_sent) {
920 struct mesh_reply* rep = mstate->reply_list;
922 /* in tcp_req_info, the mstates linked are removed, but
923 * the reply_list is now NULL, so the remove-from-empty-list
924 * takes no time and also it does not do the mesh accounting */
925 mstate->reply_list = NULL;
926 for(; rep; rep=rep->next) {
927 comm_point_drop_reply(&rep->query_reply);
928 log_assert(mesh->num_reply_addrs > 0);
929 mesh->num_reply_addrs--;
931 while((cb = mstate->cb_list)!=NULL) {
932 mstate->cb_list = cb->next;
933 fptr_ok(fptr_whitelist_mesh_cb(cb->cb));
934 (*cb->cb)(cb->cb_arg, LDNS_RCODE_SERVFAIL, NULL,
935 sec_status_unchecked, NULL, 0);
936 log_assert(mesh->num_reply_addrs > 0);
937 mesh->num_reply_addrs--;
941 /* de-init modules */
942 for(i=0; i<mesh->mods.num; i++) {
943 fptr_ok(fptr_whitelist_mod_clear(mesh->mods.mod[i]->clear));
944 (*mesh->mods.mod[i]->clear)(&mstate->s, i);
945 mstate->s.minfo[i] = NULL;
946 mstate->s.ext_state[i] = module_finished;
948 alloc_reg_release(mstate->s.env->alloc, mstate->s.region);
952 mesh_state_delete(struct module_qstate* qstate)
954 struct mesh_area* mesh;
955 struct mesh_state_ref* super, ref;
956 struct mesh_state* mstate;
959 mstate = qstate->mesh_info;
960 mesh = mstate->s.env->mesh;
961 mesh_detach_subs(&mstate->s);
962 if(mstate->list_select == mesh_forever_list) {
963 mesh->num_forever_states --;
964 mesh_list_remove(mstate, &mesh->forever_first,
965 &mesh->forever_last);
966 } else if(mstate->list_select == mesh_jostle_list) {
967 mesh_list_remove(mstate, &mesh->jostle_first,
970 if(!mstate->reply_list && !mstate->cb_list
971 && mstate->super_set.count == 0) {
972 log_assert(mesh->num_detached_states > 0);
973 mesh->num_detached_states--;
975 if(mstate->reply_list || mstate->cb_list) {
976 log_assert(mesh->num_reply_states > 0);
977 mesh->num_reply_states--;
981 RBTREE_FOR(super, struct mesh_state_ref*, &mstate->super_set) {
982 (void)rbtree_delete(&super->s->sub_set, &ref);
984 (void)rbtree_delete(&mesh->run, mstate);
985 (void)rbtree_delete(&mesh->all, mstate);
986 mesh_state_cleanup(mstate);
989 /** helper recursive rbtree find routine */
991 find_in_subsub(struct mesh_state* m, struct mesh_state* tofind, size_t *c)
993 struct mesh_state_ref* r;
994 if((*c)++ > MESH_MAX_SUBSUB)
996 RBTREE_FOR(r, struct mesh_state_ref*, &m->sub_set) {
997 if(r->s == tofind || find_in_subsub(r->s, tofind, c))
1003 /** find cycle for already looked up mesh_state */
1005 mesh_detect_cycle_found(struct module_qstate* qstate, struct mesh_state* dep_m)
1007 struct mesh_state* cyc_m = qstate->mesh_info;
1011 if(dep_m == cyc_m || find_in_subsub(dep_m, cyc_m, &counter)) {
1012 if(counter > MESH_MAX_SUBSUB)
1019 void mesh_detach_subs(struct module_qstate* qstate)
1021 struct mesh_area* mesh = qstate->env->mesh;
1022 struct mesh_state_ref* ref, lookup;
1023 #ifdef UNBOUND_DEBUG
1024 struct rbnode_type* n;
1026 lookup.node.key = &lookup;
1027 lookup.s = qstate->mesh_info;
1028 RBTREE_FOR(ref, struct mesh_state_ref*, &qstate->mesh_info->sub_set) {
1029 #ifdef UNBOUND_DEBUG
1034 rbtree_delete(&ref->s->super_set, &lookup);
1035 log_assert(n != NULL); /* must have been present */
1036 if(!ref->s->reply_list && !ref->s->cb_list
1037 && ref->s->super_set.count == 0) {
1038 mesh->num_detached_states++;
1039 log_assert(mesh->num_detached_states +
1040 mesh->num_reply_states <= mesh->all.count);
1043 rbtree_init(&qstate->mesh_info->sub_set, &mesh_state_ref_compare);
1046 int mesh_add_sub(struct module_qstate* qstate, struct query_info* qinfo,
1047 uint16_t qflags, int prime, int valrec, struct module_qstate** newq,
1048 struct mesh_state** sub)
1050 /* find it, if not, create it */
1051 struct mesh_area* mesh = qstate->env->mesh;
1052 *sub = mesh_area_find(mesh, NULL, qinfo, qflags,
1054 if(mesh_detect_cycle_found(qstate, *sub)) {
1055 verbose(VERB_ALGO, "attach failed, cycle detected");
1059 #ifdef UNBOUND_DEBUG
1060 struct rbnode_type* n;
1062 /* create a new one */
1063 *sub = mesh_state_create(qstate->env, qinfo, NULL, qflags, prime,
1066 log_err("mesh_attach_sub: out of memory");
1069 #ifdef UNBOUND_DEBUG
1074 rbtree_insert(&mesh->all, &(*sub)->node);
1075 log_assert(n != NULL);
1076 /* set detached (it is now) */
1077 mesh->num_detached_states++;
1078 /* set new query state to run */
1079 #ifdef UNBOUND_DEBUG
1084 rbtree_insert(&mesh->run, &(*sub)->run_node);
1085 log_assert(n != NULL);
1092 int mesh_attach_sub(struct module_qstate* qstate, struct query_info* qinfo,
1093 uint16_t qflags, int prime, int valrec, struct module_qstate** newq)
1095 struct mesh_area* mesh = qstate->env->mesh;
1096 struct mesh_state* sub = NULL;
1098 if(!mesh_add_sub(qstate, qinfo, qflags, prime, valrec, newq, &sub))
1100 was_detached = (sub->super_set.count == 0);
1101 if(!mesh_state_attachment(qstate->mesh_info, sub))
1103 /* if it was a duplicate attachment, the count was not zero before */
1104 if(!sub->reply_list && !sub->cb_list && was_detached &&
1105 sub->super_set.count == 1) {
1106 /* it used to be detached, before this one got added */
1107 log_assert(mesh->num_detached_states > 0);
1108 mesh->num_detached_states--;
1110 /* *newq will be run when inited after the current module stops */
1114 int mesh_state_attachment(struct mesh_state* super, struct mesh_state* sub)
1116 #ifdef UNBOUND_DEBUG
1117 struct rbnode_type* n;
1119 struct mesh_state_ref* subref; /* points to sub, inserted in super */
1120 struct mesh_state_ref* superref; /* points to super, inserted in sub */
1121 if( !(subref = regional_alloc(super->s.region,
1122 sizeof(struct mesh_state_ref))) ||
1123 !(superref = regional_alloc(sub->s.region,
1124 sizeof(struct mesh_state_ref))) ) {
1125 log_err("mesh_state_attachment: out of memory");
1128 superref->node.key = superref;
1129 superref->s = super;
1130 subref->node.key = subref;
1132 if(!rbtree_insert(&sub->super_set, &superref->node)) {
1133 /* this should not happen, iterator and validator do not
1134 * attach subqueries that are identical. */
1135 /* already attached, we are done, nothing todo.
1136 * since superref and subref already allocated in region,
1137 * we cannot free them */
1140 #ifdef UNBOUND_DEBUG
1145 rbtree_insert(&super->sub_set, &subref->node);
1146 log_assert(n != NULL); /* we checked above if statement, the reverse
1147 administration should not fail now, unless they are out of sync */
1152 * callback results to mesh cb entry
1153 * @param m: mesh state to send it for.
1154 * @param rcode: if not 0, error code.
1155 * @param rep: reply to send (or NULL if rcode is set).
1156 * @param r: callback entry
1157 * @param start_time: the time to pass to callback functions, it is 0 or
1158 * a value from one of the packets if the mesh state had packets.
1161 mesh_do_callback(struct mesh_state* m, int rcode, struct reply_info* rep,
1162 struct mesh_cb* r, struct timeval* start_time)
1165 char* reason = NULL;
1166 int was_ratelimited = m->s.was_ratelimited;
1167 /* bogus messages are not made into servfail, sec_status passed
1168 * to the callback function */
1169 if(rep && rep->security == sec_status_secure)
1172 if(!rep && rcode == LDNS_RCODE_NOERROR)
1173 rcode = LDNS_RCODE_SERVFAIL;
1174 if(!rcode && rep && (rep->security == sec_status_bogus ||
1175 rep->security == sec_status_secure_sentinel_fail)) {
1176 if(!(reason = errinf_to_str_bogus(&m->s)))
1177 rcode = LDNS_RCODE_SERVFAIL;
1179 /* send the reply */
1181 if(rcode == LDNS_RCODE_SERVFAIL) {
1182 if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1183 rep, rcode, &r->edns, NULL, m->s.region, start_time))
1184 r->edns.opt_list_inplace_cb_out = NULL;
1186 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep, rcode,
1187 &r->edns, NULL, m->s.region, start_time))
1188 r->edns.opt_list_inplace_cb_out = NULL;
1190 fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1191 (*r->cb)(r->cb_arg, rcode, r->buf, sec_status_unchecked, NULL,
1194 size_t udp_size = r->edns.udp_size;
1195 sldns_buffer_clear(r->buf);
1196 r->edns.edns_version = EDNS_ADVERTISED_VERSION;
1197 r->edns.udp_size = EDNS_ADVERTISED_SIZE;
1198 r->edns.ext_rcode = 0;
1199 r->edns.bits &= EDNS_DO;
1200 if(m->s.env->cfg->disable_edns_do && (r->edns.bits&EDNS_DO))
1201 r->edns.edns_present = 0;
1203 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep,
1204 LDNS_RCODE_NOERROR, &r->edns, NULL, m->s.region, start_time) ||
1205 !reply_info_answer_encode(&m->s.qinfo, rep, r->qid,
1206 r->qflags, r->buf, 0, 1,
1207 m->s.env->scratch, udp_size, &r->edns,
1208 (int)(r->edns.bits & EDNS_DO), secure))
1210 fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1211 (*r->cb)(r->cb_arg, LDNS_RCODE_SERVFAIL, r->buf,
1212 sec_status_unchecked, NULL, 0);
1214 fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1215 (*r->cb)(r->cb_arg, LDNS_RCODE_NOERROR, r->buf,
1216 (rep?rep->security:sec_status_unchecked),
1217 reason, was_ratelimited);
1221 log_assert(m->s.env->mesh->num_reply_addrs > 0);
1222 m->s.env->mesh->num_reply_addrs--;
1226 mesh_is_rpz_respip_tcponly_action(struct mesh_state const* m)
1228 struct respip_action_info const* respip_info = m->s.respip_action_info;
1229 return (respip_info == NULL
1231 : (respip_info->rpz_used
1232 && !respip_info->rpz_disabled
1233 && respip_info->action == respip_truncate))
1234 || m->s.tcp_required;
1238 mesh_is_udp(struct mesh_reply const* r)
1240 return r->query_reply.c->type == comm_udp;
1244 mesh_find_and_attach_ede_and_reason(struct mesh_state* m,
1245 struct reply_info* rep, struct mesh_reply* r)
1248 * During validation the EDE code can be received via two
1249 * code paths. One code path fills the reply_info EDE, and
1250 * the other fills it in the errinf_strlist. These paths
1251 * intersect at some points, but where is opaque due to
1252 * the complexity of the validator. At the time of writing
1253 * we make the choice to prefer the EDE from errinf_strlist
1254 * but a compelling reason to do otherwise is just as valid
1256 * The compelling reason is that with caching support, the value
1257 * in the reply_info is cached.
1258 * The reason members of the reply_info struct should be
1259 * updated as they are already cached. No reason to
1260 * try and find the EDE information in errinf anymore.
1262 if(rep->reason_bogus != LDNS_EDE_NONE) {
1263 edns_opt_list_append_ede(&r->edns.opt_list_out,
1264 m->s.region, rep->reason_bogus, rep->reason_bogus_str);
1269 * Send reply to mesh reply entry
1270 * @param m: mesh state to send it for.
1271 * @param rcode: if not 0, error code.
1272 * @param rep: reply to send (or NULL if rcode is set).
1273 * @param r: reply entry
1274 * @param r_buffer: buffer to use for reply entry.
1275 * @param prev: previous reply, already has its answer encoded in buffer.
1276 * @param prev_buffer: buffer for previous reply.
1279 mesh_send_reply(struct mesh_state* m, int rcode, struct reply_info* rep,
1280 struct mesh_reply* r, struct sldns_buffer* r_buffer,
1281 struct mesh_reply* prev, struct sldns_buffer* prev_buffer)
1283 struct timeval end_time;
1284 struct timeval duration;
1286 /* briefly set the replylist to null in case the
1287 * meshsendreply calls tcpreqinfo sendreply that
1288 * comm_point_drops because of size, and then the
1289 * null stops the mesh state remove and thus
1290 * reply_list modification and accounting */
1291 struct mesh_reply* rlist = m->reply_list;
1293 /* rpz: apply actions */
1294 rcode = mesh_is_udp(r) && mesh_is_rpz_respip_tcponly_action(m)
1295 ? (rcode|BIT_TC) : rcode;
1297 /* examine security status */
1298 if(m->s.env->need_to_validate && (!(r->qflags&BIT_CD) ||
1299 m->s.env->cfg->ignore_cd) && rep &&
1300 (rep->security <= sec_status_bogus ||
1301 rep->security == sec_status_secure_sentinel_fail)) {
1302 rcode = LDNS_RCODE_SERVFAIL;
1303 if(m->s.env->cfg->stat_extended)
1304 m->s.env->mesh->ans_bogus++;
1306 if(rep && rep->security == sec_status_secure)
1309 if(!rep && rcode == LDNS_RCODE_NOERROR)
1310 rcode = LDNS_RCODE_SERVFAIL;
1311 if(r->query_reply.c->use_h2) {
1312 r->query_reply.c->h2_stream = r->h2_stream;
1313 /* Mesh reply won't exist for long anymore. Make it impossible
1314 * for HTTP/2 stream to refer to mesh state, in case
1315 * connection gets cleanup before HTTP/2 stream close. */
1316 r->h2_stream->mesh_state = NULL;
1318 /* send the reply */
1319 /* We don't reuse the encoded answer if:
1320 * - either the previous or current response has a local alias. We could
1321 * compare the alias records and still reuse the previous answer if they
1322 * are the same, but that would be complicated and error prone for the
1323 * relatively minor case. So we err on the side of safety.
1324 * - there are registered callback functions for the given rcode, as these
1325 * need to be called for each reply. */
1326 if(((rcode != LDNS_RCODE_SERVFAIL &&
1327 !m->s.env->inplace_cb_lists[inplace_cb_reply]) ||
1328 (rcode == LDNS_RCODE_SERVFAIL &&
1329 !m->s.env->inplace_cb_lists[inplace_cb_reply_servfail])) &&
1330 prev && prev_buffer && prev->qflags == r->qflags &&
1331 !prev->local_alias && !r->local_alias &&
1332 prev->edns.edns_present == r->edns.edns_present &&
1333 prev->edns.bits == r->edns.bits &&
1334 prev->edns.udp_size == r->edns.udp_size &&
1335 edns_opt_list_compare(prev->edns.opt_list_out, r->edns.opt_list_out) == 0 &&
1336 edns_opt_list_compare(prev->edns.opt_list_inplace_cb_out, r->edns.opt_list_inplace_cb_out) == 0
1338 /* if the previous reply is identical to this one, fix ID */
1339 if(prev_buffer != r_buffer)
1340 sldns_buffer_copy(r_buffer, prev_buffer);
1341 sldns_buffer_write_at(r_buffer, 0, &r->qid, sizeof(uint16_t));
1342 sldns_buffer_write_at(r_buffer, 12, r->qname,
1343 m->s.qinfo.qname_len);
1344 m->reply_list = NULL;
1345 comm_point_send_reply(&r->query_reply);
1346 m->reply_list = rlist;
1348 m->s.qinfo.qname = r->qname;
1349 m->s.qinfo.local_alias = r->local_alias;
1350 if(rcode == LDNS_RCODE_SERVFAIL) {
1351 if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1352 rep, rcode, &r->edns, &r->query_reply, m->s.region, &r->start_time))
1353 r->edns.opt_list_inplace_cb_out = NULL;
1355 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep, rcode,
1356 &r->edns, &r->query_reply, m->s.region, &r->start_time))
1357 r->edns.opt_list_inplace_cb_out = NULL;
1359 /* Send along EDE EDNS0 option when SERVFAILing; usually
1360 * DNSSEC validation failures */
1361 /* Since we are SERVFAILing here, CD bit and rep->security
1362 * is already handled. */
1363 if(m->s.env->cfg->ede && rep) {
1364 mesh_find_and_attach_ede_and_reason(m, rep, r);
1366 error_encode(r_buffer, rcode, &m->s.qinfo, r->qid,
1367 r->qflags, &r->edns);
1368 m->reply_list = NULL;
1369 comm_point_send_reply(&r->query_reply);
1370 m->reply_list = rlist;
1372 size_t udp_size = r->edns.udp_size;
1373 r->edns.edns_version = EDNS_ADVERTISED_VERSION;
1374 r->edns.udp_size = EDNS_ADVERTISED_SIZE;
1375 r->edns.ext_rcode = 0;
1376 r->edns.bits &= EDNS_DO;
1377 if(m->s.env->cfg->disable_edns_do && (r->edns.bits&EDNS_DO))
1378 r->edns.edns_present = 0;
1379 m->s.qinfo.qname = r->qname;
1380 m->s.qinfo.local_alias = r->local_alias;
1382 /* Attach EDE without SERVFAIL if the validation failed.
1383 * Need to explicitly check for rep->security otherwise failed
1384 * validation paths may attach to a secure answer. */
1385 if(m->s.env->cfg->ede && rep &&
1386 (rep->security <= sec_status_bogus ||
1387 rep->security == sec_status_secure_sentinel_fail)) {
1388 mesh_find_and_attach_ede_and_reason(m, rep, r);
1391 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep,
1392 LDNS_RCODE_NOERROR, &r->edns, &r->query_reply, m->s.region, &r->start_time) ||
1393 !reply_info_answer_encode(&m->s.qinfo, rep, r->qid,
1394 r->qflags, r_buffer, 0, 1, m->s.env->scratch,
1395 udp_size, &r->edns, (int)(r->edns.bits & EDNS_DO),
1398 if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1399 rep, LDNS_RCODE_SERVFAIL, &r->edns, &r->query_reply, m->s.region, &r->start_time))
1400 r->edns.opt_list_inplace_cb_out = NULL;
1401 /* internal server error (probably malloc failure) so no
1402 * EDE (RFC8914) needed */
1403 error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
1404 &m->s.qinfo, r->qid, r->qflags, &r->edns);
1406 m->reply_list = NULL;
1407 comm_point_send_reply(&r->query_reply);
1408 m->reply_list = rlist;
1411 log_assert(m->s.env->mesh->num_reply_addrs > 0);
1412 m->s.env->mesh->num_reply_addrs--;
1413 end_time = *m->s.env->now_tv;
1414 timeval_subtract(&duration, &end_time, &r->start_time);
1415 verbose(VERB_ALGO, "query took " ARG_LL "d.%6.6d sec",
1416 (long long)duration.tv_sec, (int)duration.tv_usec);
1417 m->s.env->mesh->replies_sent++;
1418 timeval_add(&m->s.env->mesh->replies_sum_wait, &duration);
1419 timehist_insert(m->s.env->mesh->histogram, &duration);
1420 if(m->s.env->cfg->stat_extended) {
1421 uint16_t rc = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(
1423 if(secure) m->s.env->mesh->ans_secure++;
1424 m->s.env->mesh->ans_rcode[ rc ] ++;
1425 if(rc == 0 && LDNS_ANCOUNT(sldns_buffer_begin(r_buffer)) == 0)
1426 m->s.env->mesh->ans_nodata++;
1428 /* Log reply sent */
1429 if(m->s.env->cfg->log_replies) {
1430 log_reply_info(NO_VERBOSE, &m->s.qinfo,
1431 &r->query_reply.client_addr,
1432 r->query_reply.client_addrlen, duration, 0, r_buffer);
1436 void mesh_query_done(struct mesh_state* mstate)
1438 struct mesh_reply* r;
1439 struct mesh_reply* prev = NULL;
1440 struct sldns_buffer* prev_buffer = NULL;
1442 struct reply_info* rep = (mstate->s.return_msg?
1443 mstate->s.return_msg->rep:NULL);
1444 struct timeval tv = {0, 0};
1446 /* No need for the serve expired timer anymore; we are going to reply. */
1447 if(mstate->s.serve_expired_data) {
1448 comm_timer_delete(mstate->s.serve_expired_data->timer);
1449 mstate->s.serve_expired_data->timer = NULL;
1451 if(mstate->s.return_rcode == LDNS_RCODE_SERVFAIL ||
1452 (rep && FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_SERVFAIL)) {
1453 /* we are SERVFAILing; check for expired answer here */
1454 mesh_serve_expired_callback(mstate);
1455 if((mstate->reply_list || mstate->cb_list)
1456 && mstate->s.env->cfg->log_servfail
1457 && !mstate->s.env->cfg->val_log_squelch) {
1458 char* err = errinf_to_str_servfail(&mstate->s);
1464 for(r = mstate->reply_list; r; r = r->next) {
1468 /* if a response-ip address block has been stored the
1469 * information should be logged for each client. */
1470 if(mstate->s.respip_action_info &&
1471 mstate->s.respip_action_info->addrinfo) {
1472 respip_inform_print(mstate->s.respip_action_info,
1473 r->qname, mstate->s.qinfo.qtype,
1474 mstate->s.qinfo.qclass, r->local_alias,
1475 &r->query_reply.client_addr,
1476 r->query_reply.client_addrlen);
1479 /* if this query is determined to be dropped during the
1480 * mesh processing, this is the point to take that action. */
1481 if(mstate->s.is_drop) {
1482 /* briefly set the reply_list to NULL, so that the
1483 * tcp req info cleanup routine that calls the mesh
1484 * to deregister the meshstate for it is not done
1485 * because the list is NULL and also accounting is not
1486 * done there, but instead we do that here. */
1487 struct mesh_reply* reply_list = mstate->reply_list;
1488 mstate->reply_list = NULL;
1489 comm_point_drop_reply(&r->query_reply);
1490 mstate->reply_list = reply_list;
1492 struct sldns_buffer* r_buffer = r->query_reply.c->buffer;
1493 if(r->query_reply.c->tcp_req_info) {
1494 r_buffer = r->query_reply.c->tcp_req_info->spool_buffer;
1497 mesh_send_reply(mstate, mstate->s.return_rcode, rep,
1498 r, r_buffer, prev, prev_buffer);
1499 if(r->query_reply.c->tcp_req_info) {
1500 tcp_req_info_remove_mesh_state(r->query_reply.c->tcp_req_info, mstate);
1504 prev_buffer = r_buffer;
1507 /* Account for each reply sent. */
1508 if(i > 0 && mstate->s.respip_action_info &&
1509 mstate->s.respip_action_info->addrinfo &&
1510 mstate->s.env->cfg->stat_extended &&
1511 mstate->s.respip_action_info->rpz_used) {
1512 if(mstate->s.respip_action_info->rpz_disabled)
1513 mstate->s.env->mesh->rpz_action[RPZ_DISABLED_ACTION] += i;
1514 if(mstate->s.respip_action_info->rpz_cname_override)
1515 mstate->s.env->mesh->rpz_action[RPZ_CNAME_OVERRIDE_ACTION] += i;
1517 mstate->s.env->mesh->rpz_action[respip_action_to_rpz_action(
1518 mstate->s.respip_action_info->action)] += i;
1520 if(!mstate->s.is_drop && i > 0) {
1521 if(mstate->s.env->cfg->stat_extended
1522 && mstate->s.is_cachedb_answer) {
1523 mstate->s.env->mesh->ans_cachedb += i;
1527 /* Mesh area accounting */
1528 if(mstate->reply_list) {
1529 mstate->reply_list = NULL;
1530 if(!mstate->reply_list && !mstate->cb_list) {
1531 /* was a reply state, not anymore */
1532 log_assert(mstate->s.env->mesh->num_reply_states > 0);
1533 mstate->s.env->mesh->num_reply_states--;
1535 if(!mstate->reply_list && !mstate->cb_list &&
1536 mstate->super_set.count == 0)
1537 mstate->s.env->mesh->num_detached_states++;
1539 mstate->replies_sent = 1;
1541 while((c = mstate->cb_list) != NULL) {
1542 /* take this cb off the list; so that the list can be
1543 * changed, eg. by adds from the callback routine */
1544 if(!mstate->reply_list && mstate->cb_list && !c->next) {
1545 /* was a reply state, not anymore */
1546 log_assert(mstate->s.env->mesh->num_reply_states > 0);
1547 mstate->s.env->mesh->num_reply_states--;
1549 mstate->cb_list = c->next;
1550 if(!mstate->reply_list && !mstate->cb_list &&
1551 mstate->super_set.count == 0)
1552 mstate->s.env->mesh->num_detached_states++;
1553 mesh_do_callback(mstate, mstate->s.return_rcode, rep, c, &tv);
1557 void mesh_walk_supers(struct mesh_area* mesh, struct mesh_state* mstate)
1559 struct mesh_state_ref* ref;
1560 RBTREE_FOR(ref, struct mesh_state_ref*, &mstate->super_set)
1562 /* make super runnable */
1563 (void)rbtree_insert(&mesh->run, &ref->s->run_node);
1564 /* callback the function to inform super of result */
1565 fptr_ok(fptr_whitelist_mod_inform_super(
1566 mesh->mods.mod[ref->s->s.curmod]->inform_super));
1567 (*mesh->mods.mod[ref->s->s.curmod]->inform_super)(&mstate->s,
1568 ref->s->s.curmod, &ref->s->s);
1569 /* copy state that is always relevant to super */
1570 copy_state_to_super(&mstate->s, ref->s->s.curmod, &ref->s->s);
1574 struct mesh_state* mesh_area_find(struct mesh_area* mesh,
1575 struct respip_client_info* cinfo, struct query_info* qinfo,
1576 uint16_t qflags, int prime, int valrec)
1578 struct mesh_state key;
1579 struct mesh_state* result;
1581 key.node.key = &key;
1582 key.s.is_priming = prime;
1583 key.s.is_valrec = valrec;
1584 key.s.qinfo = *qinfo;
1585 key.s.query_flags = qflags;
1586 /* We are searching for a similar mesh state when we DO want to
1587 * aggregate the state. Thus unique is set to NULL. (default when we
1588 * desire aggregation).*/
1590 key.s.client_info = cinfo;
1592 result = (struct mesh_state*)rbtree_search(&mesh->all, &key);
1596 int mesh_state_add_cb(struct mesh_state* s, struct edns_data* edns,
1597 sldns_buffer* buf, mesh_cb_func_type cb, void* cb_arg,
1598 uint16_t qid, uint16_t qflags)
1600 struct mesh_cb* r = regional_alloc(s->s.region,
1601 sizeof(struct mesh_cb));
1605 log_assert(fptr_whitelist_mesh_cb(cb)); /* early failure ifmissing*/
1609 if(edns->opt_list_in && !(r->edns.opt_list_in =
1610 edns_opt_copy_region(edns->opt_list_in, s->s.region)))
1612 if(edns->opt_list_out && !(r->edns.opt_list_out =
1613 edns_opt_copy_region(edns->opt_list_out, s->s.region)))
1615 if(edns->opt_list_inplace_cb_out && !(r->edns.opt_list_inplace_cb_out =
1616 edns_opt_copy_region(edns->opt_list_inplace_cb_out, s->s.region)))
1620 r->next = s->cb_list;
1626 int mesh_state_add_reply(struct mesh_state* s, struct edns_data* edns,
1627 struct comm_reply* rep, uint16_t qid, uint16_t qflags,
1628 const struct query_info* qinfo)
1630 struct mesh_reply* r = regional_alloc(s->s.region,
1631 sizeof(struct mesh_reply));
1634 r->query_reply = *rep;
1636 if(edns->opt_list_in && !(r->edns.opt_list_in =
1637 edns_opt_copy_region(edns->opt_list_in, s->s.region)))
1639 if(edns->opt_list_out && !(r->edns.opt_list_out =
1640 edns_opt_copy_region(edns->opt_list_out, s->s.region)))
1642 if(edns->opt_list_inplace_cb_out && !(r->edns.opt_list_inplace_cb_out =
1643 edns_opt_copy_region(edns->opt_list_inplace_cb_out, s->s.region)))
1647 r->start_time = *s->s.env->now_tv;
1648 r->next = s->reply_list;
1649 r->qname = regional_alloc_init(s->s.region, qinfo->qname,
1650 s->s.qinfo.qname_len);
1654 r->h2_stream = rep->c->h2_stream;
1656 /* Data related to local alias stored in 'qinfo' (if any) is ephemeral
1657 * and can be different for different original queries (even if the
1658 * replaced query name is the same). So we need to make a deep copy
1659 * and store the copy for each reply info. */
1660 if(qinfo->local_alias) {
1661 struct packed_rrset_data* d;
1662 struct packed_rrset_data* dsrc;
1663 r->local_alias = regional_alloc_zero(s->s.region,
1664 sizeof(*qinfo->local_alias));
1667 r->local_alias->rrset = regional_alloc_init(s->s.region,
1668 qinfo->local_alias->rrset,
1669 sizeof(*qinfo->local_alias->rrset));
1670 if(!r->local_alias->rrset)
1672 dsrc = qinfo->local_alias->rrset->entry.data;
1674 /* In the current implementation, a local alias must be
1675 * a single CNAME RR (see worker_handle_request()). */
1676 log_assert(!qinfo->local_alias->next && dsrc->count == 1 &&
1677 qinfo->local_alias->rrset->rk.type ==
1678 htons(LDNS_RR_TYPE_CNAME));
1679 /* we should make a local copy for the owner name of
1681 r->local_alias->rrset->rk.dname_len =
1682 qinfo->local_alias->rrset->rk.dname_len;
1683 r->local_alias->rrset->rk.dname = regional_alloc_init(
1684 s->s.region, qinfo->local_alias->rrset->rk.dname,
1685 qinfo->local_alias->rrset->rk.dname_len);
1686 if(!r->local_alias->rrset->rk.dname)
1689 /* the rrset is not packed, like in the cache, but it is
1690 * individually allocated with an allocator from localzone. */
1691 d = regional_alloc_zero(s->s.region, sizeof(*d));
1694 r->local_alias->rrset->entry.data = d;
1695 if(!rrset_insert_rr(s->s.region, d, dsrc->rr_data[0],
1696 dsrc->rr_len[0], dsrc->rr_ttl[0], "CNAME local alias"))
1699 r->local_alias = NULL;
1705 /* Extract the query info and flags from 'mstate' into '*qinfop' and '*qflags'.
1706 * Since this is only used for internal refetch of otherwise-expired answer,
1707 * we simply ignore the rare failure mode when memory allocation fails. */
1709 mesh_copy_qinfo(struct mesh_state* mstate, struct query_info** qinfop,
1712 struct regional* region = mstate->s.env->scratch;
1713 struct query_info* qinfo;
1715 qinfo = regional_alloc_init(region, &mstate->s.qinfo, sizeof(*qinfo));
1718 qinfo->qname = regional_alloc_init(region, qinfo->qname,
1723 *qflags = mstate->s.query_flags;
1727 * Continue processing the mesh state at another module.
1728 * Handles module to modules transfer of control.
1729 * Handles module finished.
1730 * @param mesh: the mesh area.
1731 * @param mstate: currently active mesh state.
1732 * Deleted if finished, calls _done and _supers to
1733 * send replies to clients and inform other mesh states.
1734 * This in turn may create additional runnable mesh states.
1735 * @param s: state at which the current module exited.
1736 * @param ev: the event sent to the module.
1737 * returned is the event to send to the next module.
1738 * @return true if continue processing at the new module.
1739 * false if not continued processing is needed.
1742 mesh_continue(struct mesh_area* mesh, struct mesh_state* mstate,
1743 enum module_ext_state s, enum module_ev* ev)
1745 mstate->num_activated++;
1746 if(mstate->num_activated > MESH_MAX_ACTIVATION) {
1747 /* module is looping. Stop it. */
1748 log_err("internal error: looping module (%s) stopped",
1749 mesh->mods.mod[mstate->s.curmod]->name);
1750 log_query_info(NO_VERBOSE, "pass error for qstate",
1754 if(s == module_wait_module || s == module_restart_next) {
1755 /* start next module */
1757 if(mesh->mods.num == mstate->s.curmod) {
1758 log_err("Cannot pass to next module; at last module");
1759 log_query_info(VERB_QUERY, "pass error for qstate",
1762 return mesh_continue(mesh, mstate, module_error, ev);
1764 if(s == module_restart_next) {
1765 int curmod = mstate->s.curmod;
1766 for(; mstate->s.curmod < mesh->mods.num;
1767 mstate->s.curmod++) {
1768 fptr_ok(fptr_whitelist_mod_clear(
1769 mesh->mods.mod[mstate->s.curmod]->clear));
1770 (*mesh->mods.mod[mstate->s.curmod]->clear)
1771 (&mstate->s, mstate->s.curmod);
1772 mstate->s.minfo[mstate->s.curmod] = NULL;
1774 mstate->s.curmod = curmod;
1776 *ev = module_event_pass;
1779 if(s == module_wait_subquery && mstate->sub_set.count == 0) {
1780 log_err("module cannot wait for subquery, subquery list empty");
1781 log_query_info(VERB_QUERY, "pass error for qstate",
1785 if(s == module_error && mstate->s.return_rcode == LDNS_RCODE_NOERROR) {
1786 /* error is bad, handle pass back up below */
1787 mstate->s.return_rcode = LDNS_RCODE_SERVFAIL;
1789 if(s == module_error) {
1790 mesh_query_done(mstate);
1791 mesh_walk_supers(mesh, mstate);
1792 mesh_state_delete(&mstate->s);
1795 if(s == module_finished) {
1796 if(mstate->s.curmod == 0) {
1797 struct query_info* qinfo = NULL;
1798 struct edns_option* opt_list = NULL;
1799 struct sockaddr_storage addr;
1803 #ifdef CLIENT_SUBNET
1804 struct edns_option* ecs;
1805 if(mstate->s.need_refetch && mstate->reply_list &&
1806 modstack_find(&mesh->mods, "subnetcache") != -1 &&
1807 mstate->s.env->unique_mesh) {
1808 addr = mstate->reply_list->query_reply.client_addr;
1811 memset(&addr, 0, sizeof(addr));
1813 mesh_query_done(mstate);
1814 mesh_walk_supers(mesh, mstate);
1816 /* If the answer to the query needs to be refetched
1817 * from an external DNS server, we'll need to schedule
1818 * a prefetch after removing the current state, so
1819 * we need to make a copy of the query info here. */
1820 if(mstate->s.need_refetch) {
1821 mesh_copy_qinfo(mstate, &qinfo, &qflags);
1822 #ifdef CLIENT_SUBNET
1823 /* Make also a copy of the ecs option if any */
1824 if((ecs = edns_opt_list_find(
1825 mstate->s.edns_opts_front_in,
1826 mstate->s.env->cfg->client_subnet_opcode)) != NULL) {
1827 (void)edns_opt_list_append(&opt_list,
1828 ecs->opt_code, ecs->opt_len,
1830 mstate->s.env->scratch);
1833 rpz_p = mstate->s.rpz_passthru;
1837 mesh_state_delete(&mstate->s);
1838 mesh_new_prefetch(mesh, qinfo, qflags, 0,
1840 addr.ss_family!=AF_UNSPEC?&addr:NULL,
1843 mesh_state_delete(&mstate->s);
1847 /* pass along the locus of control */
1848 mstate->s.curmod --;
1849 *ev = module_event_moddone;
1855 void mesh_run(struct mesh_area* mesh, struct mesh_state* mstate,
1856 enum module_ev ev, struct outbound_entry* e)
1858 enum module_ext_state s;
1859 verbose(VERB_ALGO, "mesh_run: start");
1861 /* run the module */
1862 fptr_ok(fptr_whitelist_mod_operate(
1863 mesh->mods.mod[mstate->s.curmod]->operate));
1864 (*mesh->mods.mod[mstate->s.curmod]->operate)
1865 (&mstate->s, ev, mstate->s.curmod, e);
1867 /* examine results */
1868 mstate->s.reply = NULL;
1869 regional_free_all(mstate->s.env->scratch);
1870 s = mstate->s.ext_state[mstate->s.curmod];
1871 verbose(VERB_ALGO, "mesh_run: %s module exit state is %s",
1872 mesh->mods.mod[mstate->s.curmod]->name, strextstate(s));
1874 if(mesh_continue(mesh, mstate, s, &ev))
1877 /* run more modules */
1878 ev = module_event_pass;
1879 if(mesh->run.count > 0) {
1880 /* pop random element off the runnable tree */
1881 mstate = (struct mesh_state*)mesh->run.root->key;
1882 (void)rbtree_delete(&mesh->run, mstate);
1883 } else mstate = NULL;
1885 if(verbosity >= VERB_ALGO) {
1886 mesh_stats(mesh, "mesh_run: end");
1887 mesh_log_list(mesh);
1892 mesh_log_list(struct mesh_area* mesh)
1895 struct mesh_state* m;
1897 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1898 snprintf(buf, sizeof(buf), "%d%s%s%s%s%s%s mod%d %s%s",
1899 num++, (m->s.is_priming)?"p":"", /* prime */
1900 (m->s.is_valrec)?"v":"", /* prime */
1901 (m->s.query_flags&BIT_RD)?"RD":"",
1902 (m->s.query_flags&BIT_CD)?"CD":"",
1903 (m->super_set.count==0)?"d":"", /* detached */
1904 (m->sub_set.count!=0)?"c":"", /* children */
1905 m->s.curmod, (m->reply_list)?"rep":"", /*hasreply*/
1906 (m->cb_list)?"cb":"" /* callbacks */
1908 log_query_info(VERB_ALGO, buf, &m->s.qinfo);
1913 mesh_stats(struct mesh_area* mesh, const char* str)
1915 verbose(VERB_DETAIL, "%s %u recursion states (%u with reply, "
1916 "%u detached), %u waiting replies, %u recursion replies "
1917 "sent, %d replies dropped, %d states jostled out",
1918 str, (unsigned)mesh->all.count,
1919 (unsigned)mesh->num_reply_states,
1920 (unsigned)mesh->num_detached_states,
1921 (unsigned)mesh->num_reply_addrs,
1922 (unsigned)mesh->replies_sent,
1923 (unsigned)mesh->stats_dropped,
1924 (unsigned)mesh->stats_jostled);
1925 if(mesh->replies_sent > 0) {
1927 timeval_divide(&avg, &mesh->replies_sum_wait,
1928 mesh->replies_sent);
1929 log_info("average recursion processing time "
1930 ARG_LL "d.%6.6d sec",
1931 (long long)avg.tv_sec, (int)avg.tv_usec);
1932 log_info("histogram of recursion processing times");
1933 timehist_log(mesh->histogram, "recursions");
1938 mesh_stats_clear(struct mesh_area* mesh)
1942 mesh->replies_sent = 0;
1943 mesh->replies_sum_wait.tv_sec = 0;
1944 mesh->replies_sum_wait.tv_usec = 0;
1945 mesh->stats_jostled = 0;
1946 mesh->stats_dropped = 0;
1947 timehist_clear(mesh->histogram);
1948 mesh->ans_secure = 0;
1949 mesh->ans_bogus = 0;
1950 mesh->ans_expired = 0;
1951 mesh->ans_cachedb = 0;
1952 memset(&mesh->ans_rcode[0], 0, sizeof(size_t)*UB_STATS_RCODE_NUM);
1953 memset(&mesh->rpz_action[0], 0, sizeof(size_t)*UB_STATS_RPZ_ACTION_NUM);
1954 mesh->ans_nodata = 0;
1958 mesh_get_mem(struct mesh_area* mesh)
1960 struct mesh_state* m;
1961 size_t s = sizeof(*mesh) + sizeof(struct timehist) +
1962 sizeof(struct th_buck)*mesh->histogram->num +
1963 sizeof(sldns_buffer) + sldns_buffer_capacity(mesh->qbuf_bak);
1964 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1965 /* all, including m itself allocated in qstate region */
1966 s += regional_get_mem(m->s.region);
1972 mesh_detect_cycle(struct module_qstate* qstate, struct query_info* qinfo,
1973 uint16_t flags, int prime, int valrec)
1975 struct mesh_area* mesh = qstate->env->mesh;
1976 struct mesh_state* dep_m = NULL;
1977 dep_m = mesh_area_find(mesh, NULL, qinfo, flags, prime, valrec);
1978 return mesh_detect_cycle_found(qstate, dep_m);
1981 void mesh_list_insert(struct mesh_state* m, struct mesh_state** fp,
1982 struct mesh_state** lp)
1984 /* insert as last element */
1993 void mesh_list_remove(struct mesh_state* m, struct mesh_state** fp,
1994 struct mesh_state** lp)
1997 m->next->prev = m->prev;
2000 m->prev->next = m->next;
2004 void mesh_state_remove_reply(struct mesh_area* mesh, struct mesh_state* m,
2005 struct comm_point* cp)
2007 struct mesh_reply* n, *prev = NULL;
2009 /* when in mesh_cleanup, it sets the reply_list to NULL, so that
2010 * there is no accounting twice */
2011 if(!n) return; /* nothing to remove, also no accounting needed */
2013 if(n->query_reply.c == cp) {
2015 if(prev) prev->next = n->next;
2016 else m->reply_list = n->next;
2017 /* delete it, but allocated in m region */
2018 log_assert(mesh->num_reply_addrs > 0);
2019 mesh->num_reply_addrs--;
2028 /* it was not detached (because it had a reply list), could be now */
2029 if(!m->reply_list && !m->cb_list
2030 && m->super_set.count == 0) {
2031 mesh->num_detached_states++;
2033 /* if not replies any more in mstate, it is no longer a reply_state */
2034 if(!m->reply_list && !m->cb_list) {
2035 log_assert(mesh->num_reply_states > 0);
2036 mesh->num_reply_states--;
2042 apply_respip_action(struct module_qstate* qstate,
2043 const struct query_info* qinfo, struct respip_client_info* cinfo,
2044 struct respip_action_info* actinfo, struct reply_info* rep,
2045 struct ub_packed_rrset_key** alias_rrset,
2046 struct reply_info** encode_repp, struct auth_zones* az)
2048 if(qinfo->qtype != LDNS_RR_TYPE_A &&
2049 qinfo->qtype != LDNS_RR_TYPE_AAAA &&
2050 qinfo->qtype != LDNS_RR_TYPE_ANY)
2053 if(!respip_rewrite_reply(qinfo, cinfo, rep, encode_repp, actinfo,
2054 alias_rrset, 0, qstate->region, az, NULL))
2057 /* xxx_deny actions mean dropping the reply, unless the original reply
2058 * was redirected to response-ip data. */
2059 if((actinfo->action == respip_deny ||
2060 actinfo->action == respip_inform_deny) &&
2061 *encode_repp == rep)
2062 *encode_repp = NULL;
2068 mesh_serve_expired_callback(void* arg)
2070 struct mesh_state* mstate = (struct mesh_state*) arg;
2071 struct module_qstate* qstate = &mstate->s;
2072 struct mesh_reply* r;
2073 struct mesh_area* mesh = qstate->env->mesh;
2074 struct dns_msg* msg;
2076 struct mesh_reply* prev = NULL;
2077 struct sldns_buffer* prev_buffer = NULL;
2078 struct sldns_buffer* r_buffer = NULL;
2079 struct reply_info* partial_rep = NULL;
2080 struct ub_packed_rrset_key* alias_rrset = NULL;
2081 struct reply_info* encode_rep = NULL;
2082 struct respip_action_info actinfo;
2083 struct query_info* lookup_qinfo = &qstate->qinfo;
2084 struct query_info qinfo_tmp;
2085 struct timeval tv = {0, 0};
2086 int must_validate = (!(qstate->query_flags&BIT_CD)
2087 || qstate->env->cfg->ignore_cd) && qstate->env->need_to_validate;
2089 if(!qstate->serve_expired_data) return;
2090 verbose(VERB_ALGO, "Serve expired: Trying to reply with expired data");
2091 comm_timer_delete(qstate->serve_expired_data->timer);
2092 qstate->serve_expired_data->timer = NULL;
2093 /* If is_drop or no_cache_lookup (modules that handle their own cache e.g.,
2094 * subnetmod) ignore stale data from the main cache. */
2095 if(qstate->no_cache_lookup || qstate->is_drop) {
2097 "Serve expired: Not allowed to look into cache for stale");
2100 /* The following while is used instead of the `goto lookup_cache`
2101 * like in the worker. */
2103 fptr_ok(fptr_whitelist_serve_expired_lookup(
2104 qstate->serve_expired_data->get_cached_answer));
2105 msg = (*qstate->serve_expired_data->get_cached_answer)(qstate,
2109 /* Reset these in case we pass a second time from here. */
2110 encode_rep = msg->rep;
2111 memset(&actinfo, 0, sizeof(actinfo));
2112 actinfo.action = respip_none;
2114 if((mesh->use_response_ip || mesh->use_rpz) &&
2115 !partial_rep && !apply_respip_action(qstate, &qstate->qinfo,
2116 qstate->client_info, &actinfo, msg->rep, &alias_rrset, &encode_rep,
2117 qstate->env->auth_zones)) {
2119 } else if(partial_rep &&
2120 !respip_merge_cname(partial_rep, &qstate->qinfo, msg->rep,
2121 qstate->client_info, must_validate, &encode_rep, qstate->region,
2122 qstate->env->auth_zones)) {
2125 if(!encode_rep || alias_rrset) {
2130 /* A partial CNAME chain is found. */
2131 partial_rep = encode_rep;
2134 /* We've found a partial reply ending with an
2135 * alias. Replace the lookup qinfo for the
2136 * alias target and lookup the cache again to
2137 * (possibly) complete the reply. As we're
2138 * passing the "base" reply, there will be no
2139 * more alias chasing. */
2141 memset(&qinfo_tmp, 0, sizeof(qinfo_tmp));
2142 get_cname_target(alias_rrset, &qinfo_tmp.qname,
2143 &qinfo_tmp.qname_len);
2144 if(!qinfo_tmp.qname) {
2145 log_err("Serve expired: unexpected: invalid answer alias");
2148 qinfo_tmp.qtype = qstate->qinfo.qtype;
2149 qinfo_tmp.qclass = qstate->qinfo.qclass;
2150 lookup_qinfo = &qinfo_tmp;
2156 if(verbosity >= VERB_ALGO)
2157 log_dns_msg("Serve expired lookup", &qstate->qinfo, msg->rep);
2159 for(r = mstate->reply_list; r; r = r->next) {
2163 /* If address info is returned, it means the action should be an
2164 * 'inform' variant and the information should be logged. */
2165 if(actinfo.addrinfo) {
2166 respip_inform_print(&actinfo, r->qname,
2167 qstate->qinfo.qtype, qstate->qinfo.qclass,
2168 r->local_alias, &r->query_reply.client_addr,
2169 r->query_reply.client_addrlen);
2172 /* Add EDE Stale Answer (RCF8914). Ignore global ede as this is
2173 * warning instead of an error */
2174 if (r->edns.edns_present && qstate->env->cfg->ede_serve_expired &&
2175 qstate->env->cfg->ede) {
2176 edns_opt_list_append_ede(&r->edns.opt_list_out,
2177 mstate->s.region, LDNS_EDE_STALE_ANSWER, NULL);
2180 r_buffer = r->query_reply.c->buffer;
2181 if(r->query_reply.c->tcp_req_info)
2182 r_buffer = r->query_reply.c->tcp_req_info->spool_buffer;
2183 mesh_send_reply(mstate, LDNS_RCODE_NOERROR, msg->rep,
2184 r, r_buffer, prev, prev_buffer);
2185 if(r->query_reply.c->tcp_req_info)
2186 tcp_req_info_remove_mesh_state(r->query_reply.c->tcp_req_info, mstate);
2188 prev_buffer = r_buffer;
2190 /* Account for each reply sent. */
2192 mesh->ans_expired += i;
2193 if(actinfo.addrinfo && qstate->env->cfg->stat_extended &&
2195 if(actinfo.rpz_disabled)
2196 qstate->env->mesh->rpz_action[RPZ_DISABLED_ACTION] += i;
2197 if(actinfo.rpz_cname_override)
2198 qstate->env->mesh->rpz_action[RPZ_CNAME_OVERRIDE_ACTION] += i;
2200 qstate->env->mesh->rpz_action[
2201 respip_action_to_rpz_action(actinfo.action)] += i;
2205 /* Mesh area accounting */
2206 if(mstate->reply_list) {
2207 mstate->reply_list = NULL;
2208 if(!mstate->reply_list && !mstate->cb_list) {
2209 log_assert(mesh->num_reply_states > 0);
2210 mesh->num_reply_states--;
2211 if(mstate->super_set.count == 0) {
2212 mesh->num_detached_states++;
2217 while((c = mstate->cb_list) != NULL) {
2218 /* take this cb off the list; so that the list can be
2219 * changed, eg. by adds from the callback routine */
2220 if(!mstate->reply_list && mstate->cb_list && !c->next) {
2221 /* was a reply state, not anymore */
2222 log_assert(qstate->env->mesh->num_reply_states > 0);
2223 qstate->env->mesh->num_reply_states--;
2225 mstate->cb_list = c->next;
2226 if(!mstate->reply_list && !mstate->cb_list &&
2227 mstate->super_set.count == 0)
2228 qstate->env->mesh->num_detached_states++;
2229 mesh_do_callback(mstate, LDNS_RCODE_NOERROR, msg->rep, c, &tv);
2233 int mesh_jostle_exceeded(struct mesh_area* mesh)
2235 if(mesh->all.count < mesh->max_reply_states)