xref: /freebsd/contrib/unbound/services/mesh.c (revision 90e29718cffcec987769ccbe39308357202c46d5)
1 /*
2  * services/mesh.c - deal with mesh of query states and handle events for that.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains 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.
44  */
45 #include "config.h"
46 #include "services/mesh.h"
47 #include "services/outbound_list.h"
48 #include "services/cache/dns.h"
49 #include "services/cache/rrset.h"
50 #include "util/log.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 
67 #ifdef CLIENT_SUBNET
68 #include "edns-subnet/subnetmod.h"
69 #include "edns-subnet/edns-subnet.h"
70 #endif
71 
72 /** subtract timers and the values do not overflow or become negative */
73 static void
74 timeval_subtract(struct timeval* d, const struct timeval* end, const struct timeval* start)
75 {
76 #ifndef S_SPLINT_S
77 	time_t end_usec = end->tv_usec;
78 	d->tv_sec = end->tv_sec - start->tv_sec;
79 	if(end_usec < start->tv_usec) {
80 		end_usec += 1000000;
81 		d->tv_sec--;
82 	}
83 	d->tv_usec = end_usec - start->tv_usec;
84 #endif
85 }
86 
87 /** add timers and the values do not overflow or become negative */
88 static void
89 timeval_add(struct timeval* d, const struct timeval* add)
90 {
91 #ifndef S_SPLINT_S
92 	d->tv_sec += add->tv_sec;
93 	d->tv_usec += add->tv_usec;
94 	if(d->tv_usec >= 1000000 ) {
95 		d->tv_usec -= 1000000;
96 		d->tv_sec++;
97 	}
98 #endif
99 }
100 
101 /** divide sum of timers to get average */
102 static void
103 timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d)
104 {
105 #ifndef S_SPLINT_S
106 	size_t leftover;
107 	if(d <= 0) {
108 		avg->tv_sec = 0;
109 		avg->tv_usec = 0;
110 		return;
111 	}
112 	avg->tv_sec = sum->tv_sec / d;
113 	avg->tv_usec = sum->tv_usec / d;
114 	/* handle fraction from seconds divide */
115 	leftover = sum->tv_sec - avg->tv_sec*d;
116 	if(leftover <= 0)
117 		leftover = 0;
118 	avg->tv_usec += (((long long)leftover)*((long long)1000000))/d;
119 	if(avg->tv_sec < 0)
120 		avg->tv_sec = 0;
121 	if(avg->tv_usec < 0)
122 		avg->tv_usec = 0;
123 #endif
124 }
125 
126 /** histogram compare of time values */
127 static int
128 timeval_smaller(const struct timeval* x, const struct timeval* y)
129 {
130 #ifndef S_SPLINT_S
131 	if(x->tv_sec < y->tv_sec)
132 		return 1;
133 	else if(x->tv_sec == y->tv_sec) {
134 		if(x->tv_usec <= y->tv_usec)
135 			return 1;
136 		else	return 0;
137 	}
138 	else	return 0;
139 #endif
140 }
141 
142 /**
143  * Compare two response-ip client info entries for the purpose of mesh state
144  * compare.  It returns 0 if ci_a and ci_b are considered equal; otherwise
145  * 1 or -1 (they mean 'ci_a is larger/smaller than ci_b', respectively, but
146  * in practice it should be only used to mean they are different).
147  * We cannot share the mesh state for two queries if different response-ip
148  * actions can apply in the end, even if those queries are otherwise identical.
149  * For this purpose we compare tag lists and tag action lists; they should be
150  * identical to share the same state.
151  * For tag data, we don't look into the data content, as it can be
152  * expensive; unless tag data are not defined for both or they point to the
153  * exact same data in memory (i.e., they come from the same ACL entry), we
154  * consider these data different.
155  * Likewise, if the client info is associated with views, we don't look into
156  * the views.  They are considered different unless they are exactly the same
157  * even if the views only differ in the names.
158  */
159 static int
160 client_info_compare(const struct respip_client_info* ci_a,
161 	const struct respip_client_info* ci_b)
162 {
163 	int cmp;
164 
165 	if(!ci_a && !ci_b)
166 		return 0;
167 	if(ci_a && !ci_b)
168 		return -1;
169 	if(!ci_a && ci_b)
170 		return 1;
171 	if(ci_a->taglen != ci_b->taglen)
172 		return (ci_a->taglen < ci_b->taglen) ? -1 : 1;
173 	if(ci_a->taglist && !ci_b->taglist)
174 		return -1;
175 	if(!ci_a->taglist && ci_b->taglist)
176 		return 1;
177 	if(ci_a->taglist && ci_b->taglist) {
178 		cmp = memcmp(ci_a->taglist, ci_b->taglist, ci_a->taglen);
179 		if(cmp != 0)
180 			return cmp;
181 	}
182 	if(ci_a->tag_actions_size != ci_b->tag_actions_size)
183 		return (ci_a->tag_actions_size < ci_b->tag_actions_size) ?
184 			-1 : 1;
185 	if(ci_a->tag_actions && !ci_b->tag_actions)
186 		return -1;
187 	if(!ci_a->tag_actions && ci_b->tag_actions)
188 		return 1;
189 	if(ci_a->tag_actions && ci_b->tag_actions) {
190 		cmp = memcmp(ci_a->tag_actions, ci_b->tag_actions,
191 			ci_a->tag_actions_size);
192 		if(cmp != 0)
193 			return cmp;
194 	}
195 	if(ci_a->tag_datas != ci_b->tag_datas)
196 		return ci_a->tag_datas < ci_b->tag_datas ? -1 : 1;
197 	if(ci_a->view != ci_b->view)
198 		return ci_a->view < ci_b->view ? -1 : 1;
199 	/* For the unbound daemon these should be non-NULL and identical,
200 	 * but we check that just in case. */
201 	if(ci_a->respip_set != ci_b->respip_set)
202 		return ci_a->respip_set < ci_b->respip_set ? -1 : 1;
203 	return 0;
204 }
205 
206 int
207 mesh_state_compare(const void* ap, const void* bp)
208 {
209 	struct mesh_state* a = (struct mesh_state*)ap;
210 	struct mesh_state* b = (struct mesh_state*)bp;
211 	int cmp;
212 
213 	if(a->unique < b->unique)
214 		return -1;
215 	if(a->unique > b->unique)
216 		return 1;
217 
218 	if(a->s.is_priming && !b->s.is_priming)
219 		return -1;
220 	if(!a->s.is_priming && b->s.is_priming)
221 		return 1;
222 
223 	if(a->s.is_valrec && !b->s.is_valrec)
224 		return -1;
225 	if(!a->s.is_valrec && b->s.is_valrec)
226 		return 1;
227 
228 	if((a->s.query_flags&BIT_RD) && !(b->s.query_flags&BIT_RD))
229 		return -1;
230 	if(!(a->s.query_flags&BIT_RD) && (b->s.query_flags&BIT_RD))
231 		return 1;
232 
233 	if((a->s.query_flags&BIT_CD) && !(b->s.query_flags&BIT_CD))
234 		return -1;
235 	if(!(a->s.query_flags&BIT_CD) && (b->s.query_flags&BIT_CD))
236 		return 1;
237 
238 	cmp = query_info_compare(&a->s.qinfo, &b->s.qinfo);
239 	if(cmp != 0)
240 		return cmp;
241 	return client_info_compare(a->s.client_info, b->s.client_info);
242 }
243 
244 int
245 mesh_state_ref_compare(const void* ap, const void* bp)
246 {
247 	struct mesh_state_ref* a = (struct mesh_state_ref*)ap;
248 	struct mesh_state_ref* b = (struct mesh_state_ref*)bp;
249 	return mesh_state_compare(a->s, b->s);
250 }
251 
252 struct mesh_area*
253 mesh_create(struct module_stack* stack, struct module_env* env)
254 {
255 	struct mesh_area* mesh = calloc(1, sizeof(struct mesh_area));
256 	if(!mesh) {
257 		log_err("mesh area alloc: out of memory");
258 		return NULL;
259 	}
260 	mesh->histogram = timehist_setup();
261 	mesh->qbuf_bak = sldns_buffer_new(env->cfg->msg_buffer_size);
262 	if(!mesh->histogram || !mesh->qbuf_bak) {
263 		free(mesh);
264 		log_err("mesh area alloc: out of memory");
265 		return NULL;
266 	}
267 	mesh->mods = *stack;
268 	mesh->env = env;
269 	rbtree_init(&mesh->run, &mesh_state_compare);
270 	rbtree_init(&mesh->all, &mesh_state_compare);
271 	mesh->num_reply_addrs = 0;
272 	mesh->num_reply_states = 0;
273 	mesh->num_detached_states = 0;
274 	mesh->num_forever_states = 0;
275 	mesh->stats_jostled = 0;
276 	mesh->stats_dropped = 0;
277 	mesh->ans_expired = 0;
278 	mesh->max_reply_states = env->cfg->num_queries_per_thread;
279 	mesh->max_forever_states = (mesh->max_reply_states+1)/2;
280 #ifndef S_SPLINT_S
281 	mesh->jostle_max.tv_sec = (time_t)(env->cfg->jostle_time / 1000);
282 	mesh->jostle_max.tv_usec = (time_t)((env->cfg->jostle_time % 1000)
283 		*1000);
284 #endif
285 	return mesh;
286 }
287 
288 /** help mesh delete delete mesh states */
289 static void
290 mesh_delete_helper(rbnode_type* n)
291 {
292 	struct mesh_state* mstate = (struct mesh_state*)n->key;
293 	/* perform a full delete, not only 'cleanup' routine,
294 	 * because other callbacks expect a clean state in the mesh.
295 	 * For 're-entrant' calls */
296 	mesh_state_delete(&mstate->s);
297 	/* but because these delete the items from the tree, postorder
298 	 * traversal and rbtree rebalancing do not work together */
299 }
300 
301 void
302 mesh_delete(struct mesh_area* mesh)
303 {
304 	if(!mesh)
305 		return;
306 	/* free all query states */
307 	while(mesh->all.count)
308 		mesh_delete_helper(mesh->all.root);
309 	timehist_delete(mesh->histogram);
310 	sldns_buffer_free(mesh->qbuf_bak);
311 	free(mesh);
312 }
313 
314 void
315 mesh_delete_all(struct mesh_area* mesh)
316 {
317 	/* free all query states */
318 	while(mesh->all.count)
319 		mesh_delete_helper(mesh->all.root);
320 	mesh->stats_dropped += mesh->num_reply_addrs;
321 	/* clear mesh area references */
322 	rbtree_init(&mesh->run, &mesh_state_compare);
323 	rbtree_init(&mesh->all, &mesh_state_compare);
324 	mesh->num_reply_addrs = 0;
325 	mesh->num_reply_states = 0;
326 	mesh->num_detached_states = 0;
327 	mesh->num_forever_states = 0;
328 	mesh->forever_first = NULL;
329 	mesh->forever_last = NULL;
330 	mesh->jostle_first = NULL;
331 	mesh->jostle_last = NULL;
332 }
333 
334 int mesh_make_new_space(struct mesh_area* mesh, sldns_buffer* qbuf)
335 {
336 	struct mesh_state* m = mesh->jostle_first;
337 	/* free space is available */
338 	if(mesh->num_reply_states < mesh->max_reply_states)
339 		return 1;
340 	/* try to kick out a jostle-list item */
341 	if(m && m->reply_list && m->list_select == mesh_jostle_list) {
342 		/* how old is it? */
343 		struct timeval age;
344 		timeval_subtract(&age, mesh->env->now_tv,
345 			&m->reply_list->start_time);
346 		if(timeval_smaller(&mesh->jostle_max, &age)) {
347 			/* its a goner */
348 			log_nametypeclass(VERB_ALGO, "query jostled out to "
349 				"make space for a new one",
350 				m->s.qinfo.qname, m->s.qinfo.qtype,
351 				m->s.qinfo.qclass);
352 			/* backup the query */
353 			if(qbuf) sldns_buffer_copy(mesh->qbuf_bak, qbuf);
354 			/* notify supers */
355 			if(m->super_set.count > 0) {
356 				verbose(VERB_ALGO, "notify supers of failure");
357 				m->s.return_msg = NULL;
358 				m->s.return_rcode = LDNS_RCODE_SERVFAIL;
359 				mesh_walk_supers(mesh, m);
360 			}
361 			mesh->stats_jostled ++;
362 			mesh_state_delete(&m->s);
363 			/* restore the query - note that the qinfo ptr to
364 			 * the querybuffer is then correct again. */
365 			if(qbuf) sldns_buffer_copy(qbuf, mesh->qbuf_bak);
366 			return 1;
367 		}
368 	}
369 	/* no space for new item */
370 	return 0;
371 }
372 
373 struct dns_msg*
374 mesh_serve_expired_lookup(struct module_qstate* qstate,
375 	struct query_info* lookup_qinfo)
376 {
377 	hashvalue_type h;
378 	struct lruhash_entry* e;
379 	struct dns_msg* msg;
380 	struct reply_info* data;
381 	struct msgreply_entry* key;
382 	time_t timenow = *qstate->env->now;
383 	int must_validate = (!(qstate->query_flags&BIT_CD)
384 		|| qstate->env->cfg->ignore_cd) && qstate->env->need_to_validate;
385 	/* Lookup cache */
386 	h = query_info_hash(lookup_qinfo, qstate->query_flags);
387 	e = slabhash_lookup(qstate->env->msg_cache, h, lookup_qinfo, 0);
388 	if(!e) return NULL;
389 
390 	key = (struct msgreply_entry*)e->key;
391 	data = (struct reply_info*)e->data;
392 	msg = tomsg(qstate->env, &key->key, data, qstate->region, timenow,
393 		qstate->env->cfg->serve_expired, qstate->env->scratch);
394 	if(!msg)
395 		goto bail_out;
396 
397 	/* Check CNAME chain (if any)
398 	 * This is part of tomsg above; no need to check now. */
399 
400 	/* Check security status of the cached answer.
401 	 * tomsg above has a subset of these checks, so we are leaving
402 	 * these as is.
403 	 * In case of bogus or revalidation we don't care to reply here. */
404 	if(must_validate && (msg->rep->security == sec_status_bogus ||
405 		msg->rep->security == sec_status_secure_sentinel_fail)) {
406 		verbose(VERB_ALGO, "Serve expired: bogus answer found in cache");
407 		goto bail_out;
408 	} else if(msg->rep->security == sec_status_unchecked && must_validate) {
409 		verbose(VERB_ALGO, "Serve expired: unchecked entry needs "
410 			"validation");
411 		goto bail_out; /* need to validate cache entry first */
412 	} else if(msg->rep->security == sec_status_secure &&
413 		!reply_all_rrsets_secure(msg->rep) && must_validate) {
414 			verbose(VERB_ALGO, "Serve expired: secure entry"
415 				" changed status");
416 			goto bail_out; /* rrset changed, re-verify */
417 	}
418 
419 	lock_rw_unlock(&e->lock);
420 	return msg;
421 
422 bail_out:
423 	lock_rw_unlock(&e->lock);
424 	return NULL;
425 }
426 
427 
428 /** Init the serve expired data structure */
429 static int
430 mesh_serve_expired_init(struct mesh_state* mstate, int timeout)
431 {
432 	struct timeval t;
433 
434 	/* Create serve_expired_data if not there yet */
435 	if(!mstate->s.serve_expired_data) {
436 		mstate->s.serve_expired_data = (struct serve_expired_data*)
437 			regional_alloc_zero(
438 				mstate->s.region, sizeof(struct serve_expired_data));
439 		if(!mstate->s.serve_expired_data)
440 			return 0;
441 	}
442 
443 	/* Don't overwrite the function if already set */
444 	mstate->s.serve_expired_data->get_cached_answer =
445 		mstate->s.serve_expired_data->get_cached_answer?
446 		mstate->s.serve_expired_data->get_cached_answer:
447 		&mesh_serve_expired_lookup;
448 
449 	/* In case this timer already popped, start it again */
450 	if(!mstate->s.serve_expired_data->timer) {
451 		mstate->s.serve_expired_data->timer = comm_timer_create(
452 			mstate->s.env->worker_base, mesh_serve_expired_callback, mstate);
453 		if(!mstate->s.serve_expired_data->timer)
454 			return 0;
455 #ifndef S_SPLINT_S
456 		t.tv_sec = timeout/1000;
457 		t.tv_usec = (timeout%1000)*1000;
458 #endif
459 		comm_timer_set(mstate->s.serve_expired_data->timer, &t);
460 	}
461 	return 1;
462 }
463 
464 void mesh_new_client(struct mesh_area* mesh, struct query_info* qinfo,
465 	struct respip_client_info* cinfo, uint16_t qflags,
466 	struct edns_data* edns, struct comm_reply* rep, uint16_t qid,
467 	int rpz_passthru)
468 {
469 	struct mesh_state* s = NULL;
470 	int unique = unique_mesh_state(edns->opt_list_in, mesh->env);
471 	int was_detached = 0;
472 	int was_noreply = 0;
473 	int added = 0;
474 	int timeout = mesh->env->cfg->serve_expired?
475 		mesh->env->cfg->serve_expired_client_timeout:0;
476 	struct sldns_buffer* r_buffer = rep->c->buffer;
477 	if(rep->c->tcp_req_info) {
478 		r_buffer = rep->c->tcp_req_info->spool_buffer;
479 	}
480 	if(!unique)
481 		s = mesh_area_find(mesh, cinfo, qinfo, qflags&(BIT_RD|BIT_CD), 0, 0);
482 	/* does this create a new reply state? */
483 	if(!s || s->list_select == mesh_no_list) {
484 		if(!mesh_make_new_space(mesh, rep->c->buffer)) {
485 			verbose(VERB_ALGO, "Too many queries. dropping "
486 				"incoming query.");
487 			comm_point_drop_reply(rep);
488 			mesh->stats_dropped++;
489 			return;
490 		}
491 		/* for this new reply state, the reply address is free,
492 		 * so the limit of reply addresses does not stop reply states*/
493 	} else {
494 		/* protect our memory usage from storing reply addresses */
495 		if(mesh->num_reply_addrs > mesh->max_reply_states*16) {
496 			verbose(VERB_ALGO, "Too many requests queued. "
497 				"dropping incoming query.");
498 			comm_point_drop_reply(rep);
499 			mesh->stats_dropped++;
500 			return;
501 		}
502 	}
503 	/* see if it already exists, if not, create one */
504 	if(!s) {
505 #ifdef UNBOUND_DEBUG
506 		struct rbnode_type* n;
507 #endif
508 		s = mesh_state_create(mesh->env, qinfo, cinfo,
509 			qflags&(BIT_RD|BIT_CD), 0, 0);
510 		if(!s) {
511 			log_err("mesh_state_create: out of memory; SERVFAIL");
512 			if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, NULL, NULL,
513 				LDNS_RCODE_SERVFAIL, edns, rep, mesh->env->scratch, mesh->env->now_tv))
514 					edns->opt_list_inplace_cb_out = NULL;
515 			error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
516 				qinfo, qid, qflags, edns);
517 			comm_point_send_reply(rep);
518 			return;
519 		}
520 		if(unique)
521 			mesh_state_make_unique(s);
522 		s->s.rpz_passthru = rpz_passthru;
523 		/* copy the edns options we got from the front */
524 		if(edns->opt_list_in) {
525 			s->s.edns_opts_front_in = edns_opt_copy_region(edns->opt_list_in,
526 				s->s.region);
527 			if(!s->s.edns_opts_front_in) {
528 				log_err("mesh_state_create: out of memory; SERVFAIL");
529 				if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, NULL,
530 					NULL, LDNS_RCODE_SERVFAIL, edns, rep, mesh->env->scratch, mesh->env->now_tv))
531 						edns->opt_list_inplace_cb_out = NULL;
532 				error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
533 					qinfo, qid, qflags, edns);
534 				comm_point_send_reply(rep);
535 				return;
536 			}
537 		}
538 
539 #ifdef UNBOUND_DEBUG
540 		n =
541 #else
542 		(void)
543 #endif
544 		rbtree_insert(&mesh->all, &s->node);
545 		log_assert(n != NULL);
546 		/* set detached (it is now) */
547 		mesh->num_detached_states++;
548 		added = 1;
549 	}
550 	if(!s->reply_list && !s->cb_list) {
551 		was_noreply = 1;
552 		if(s->super_set.count == 0) {
553 			was_detached = 1;
554 		}
555 	}
556 	/* add reply to s */
557 	if(!mesh_state_add_reply(s, edns, rep, qid, qflags, qinfo)) {
558 		log_err("mesh_new_client: out of memory; SERVFAIL");
559 		goto servfail_mem;
560 	}
561 	if(rep->c->tcp_req_info) {
562 		if(!tcp_req_info_add_meshstate(rep->c->tcp_req_info, mesh, s)) {
563 			log_err("mesh_new_client: out of memory add tcpreqinfo");
564 			goto servfail_mem;
565 		}
566 	}
567 	if(rep->c->use_h2) {
568 		http2_stream_add_meshstate(rep->c->h2_stream, mesh, s);
569 	}
570 	/* add serve expired timer if required and not already there */
571 	if(timeout && !mesh_serve_expired_init(s, timeout)) {
572 		log_err("mesh_new_client: out of memory initializing serve expired");
573 		goto servfail_mem;
574 	}
575 	/* update statistics */
576 	if(was_detached) {
577 		log_assert(mesh->num_detached_states > 0);
578 		mesh->num_detached_states--;
579 	}
580 	if(was_noreply) {
581 		mesh->num_reply_states ++;
582 	}
583 	mesh->num_reply_addrs++;
584 	if(s->list_select == mesh_no_list) {
585 		/* move to either the forever or the jostle_list */
586 		if(mesh->num_forever_states < mesh->max_forever_states) {
587 			mesh->num_forever_states ++;
588 			mesh_list_insert(s, &mesh->forever_first,
589 				&mesh->forever_last);
590 			s->list_select = mesh_forever_list;
591 		} else {
592 			mesh_list_insert(s, &mesh->jostle_first,
593 				&mesh->jostle_last);
594 			s->list_select = mesh_jostle_list;
595 		}
596 	}
597 	if(added)
598 		mesh_run(mesh, s, module_event_new, NULL);
599 	return;
600 
601 servfail_mem:
602 	if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, &s->s,
603 		NULL, LDNS_RCODE_SERVFAIL, edns, rep, mesh->env->scratch, mesh->env->now_tv))
604 			edns->opt_list_inplace_cb_out = NULL;
605 	error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
606 		qinfo, qid, qflags, edns);
607 	comm_point_send_reply(rep);
608 	if(added)
609 		mesh_state_delete(&s->s);
610 	return;
611 }
612 
613 int
614 mesh_new_callback(struct mesh_area* mesh, struct query_info* qinfo,
615 	uint16_t qflags, struct edns_data* edns, sldns_buffer* buf,
616 	uint16_t qid, mesh_cb_func_type cb, void* cb_arg, int rpz_passthru)
617 {
618 	struct mesh_state* s = NULL;
619 	int unique = unique_mesh_state(edns->opt_list_in, mesh->env);
620 	int timeout = mesh->env->cfg->serve_expired?
621 		mesh->env->cfg->serve_expired_client_timeout:0;
622 	int was_detached = 0;
623 	int was_noreply = 0;
624 	int added = 0;
625 	if(!unique)
626 		s = mesh_area_find(mesh, NULL, qinfo, qflags&(BIT_RD|BIT_CD), 0, 0);
627 
628 	/* there are no limits on the number of callbacks */
629 
630 	/* see if it already exists, if not, create one */
631 	if(!s) {
632 #ifdef UNBOUND_DEBUG
633 		struct rbnode_type* n;
634 #endif
635 		s = mesh_state_create(mesh->env, qinfo, NULL,
636 			qflags&(BIT_RD|BIT_CD), 0, 0);
637 		if(!s) {
638 			return 0;
639 		}
640 		if(unique)
641 			mesh_state_make_unique(s);
642 		s->s.rpz_passthru = rpz_passthru;
643 		if(edns->opt_list_in) {
644 			s->s.edns_opts_front_in = edns_opt_copy_region(edns->opt_list_in,
645 				s->s.region);
646 			if(!s->s.edns_opts_front_in) {
647 				return 0;
648 			}
649 		}
650 #ifdef UNBOUND_DEBUG
651 		n =
652 #else
653 		(void)
654 #endif
655 		rbtree_insert(&mesh->all, &s->node);
656 		log_assert(n != NULL);
657 		/* set detached (it is now) */
658 		mesh->num_detached_states++;
659 		added = 1;
660 	}
661 	if(!s->reply_list && !s->cb_list) {
662 		was_noreply = 1;
663 		if(s->super_set.count == 0) {
664 			was_detached = 1;
665 		}
666 	}
667 	/* add reply to s */
668 	if(!mesh_state_add_cb(s, edns, buf, cb, cb_arg, qid, qflags)) {
669 		if(added)
670 			mesh_state_delete(&s->s);
671 		return 0;
672 	}
673 	/* add serve expired timer if not already there */
674 	if(timeout && !mesh_serve_expired_init(s, timeout)) {
675 		return 0;
676 	}
677 	/* update statistics */
678 	if(was_detached) {
679 		log_assert(mesh->num_detached_states > 0);
680 		mesh->num_detached_states--;
681 	}
682 	if(was_noreply) {
683 		mesh->num_reply_states ++;
684 	}
685 	mesh->num_reply_addrs++;
686 	if(added)
687 		mesh_run(mesh, s, module_event_new, NULL);
688 	return 1;
689 }
690 
691 /* Internal backend routine of mesh_new_prefetch().  It takes one additional
692  * parameter, 'run', which controls whether to run the prefetch state
693  * immediately.  When this function is called internally 'run' could be
694  * 0 (false), in which case the new state is only made runnable so it
695  * will not be run recursively on top of the current state. */
696 static void mesh_schedule_prefetch(struct mesh_area* mesh,
697 	struct query_info* qinfo, uint16_t qflags, time_t leeway, int run,
698 	int rpz_passthru)
699 {
700 	struct mesh_state* s = mesh_area_find(mesh, NULL, qinfo,
701 		qflags&(BIT_RD|BIT_CD), 0, 0);
702 #ifdef UNBOUND_DEBUG
703 	struct rbnode_type* n;
704 #endif
705 	/* already exists, and for a different purpose perhaps.
706 	 * if mesh_no_list, keep it that way. */
707 	if(s) {
708 		/* make it ignore the cache from now on */
709 		if(!s->s.blacklist)
710 			sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
711 		if(s->s.prefetch_leeway < leeway)
712 			s->s.prefetch_leeway = leeway;
713 		return;
714 	}
715 	if(!mesh_make_new_space(mesh, NULL)) {
716 		verbose(VERB_ALGO, "Too many queries. dropped prefetch.");
717 		mesh->stats_dropped ++;
718 		return;
719 	}
720 
721 	s = mesh_state_create(mesh->env, qinfo, NULL,
722 		qflags&(BIT_RD|BIT_CD), 0, 0);
723 	if(!s) {
724 		log_err("prefetch mesh_state_create: out of memory");
725 		return;
726 	}
727 #ifdef UNBOUND_DEBUG
728 	n =
729 #else
730 	(void)
731 #endif
732 	rbtree_insert(&mesh->all, &s->node);
733 	log_assert(n != NULL);
734 	/* set detached (it is now) */
735 	mesh->num_detached_states++;
736 	/* make it ignore the cache */
737 	sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
738 	s->s.prefetch_leeway = leeway;
739 
740 	if(s->list_select == mesh_no_list) {
741 		/* move to either the forever or the jostle_list */
742 		if(mesh->num_forever_states < mesh->max_forever_states) {
743 			mesh->num_forever_states ++;
744 			mesh_list_insert(s, &mesh->forever_first,
745 				&mesh->forever_last);
746 			s->list_select = mesh_forever_list;
747 		} else {
748 			mesh_list_insert(s, &mesh->jostle_first,
749 				&mesh->jostle_last);
750 			s->list_select = mesh_jostle_list;
751 		}
752 	}
753 	s->s.rpz_passthru = rpz_passthru;
754 
755 	if(!run) {
756 #ifdef UNBOUND_DEBUG
757 		n =
758 #else
759 		(void)
760 #endif
761 		rbtree_insert(&mesh->run, &s->run_node);
762 		log_assert(n != NULL);
763 		return;
764 	}
765 
766 	mesh_run(mesh, s, module_event_new, NULL);
767 }
768 
769 #ifdef CLIENT_SUBNET
770 /* Same logic as mesh_schedule_prefetch but tailored to the subnet module logic
771  * like passing along the comm_reply info. This will be faked into an EDNS
772  * option for processing by the subnet module if the client has not already
773  * attached its own ECS data. */
774 static void mesh_schedule_prefetch_subnet(struct mesh_area* mesh,
775 	struct query_info* qinfo, uint16_t qflags, time_t leeway, int run,
776 	int rpz_passthru, struct comm_reply* rep, struct edns_option* edns_list)
777 {
778 	struct mesh_state* s = NULL;
779 	struct edns_option* opt = NULL;
780 #ifdef UNBOUND_DEBUG
781 	struct rbnode_type* n;
782 #endif
783 	if(!mesh_make_new_space(mesh, NULL)) {
784 		verbose(VERB_ALGO, "Too many queries. dropped prefetch.");
785 		mesh->stats_dropped ++;
786 		return;
787 	}
788 
789 	s = mesh_state_create(mesh->env, qinfo, NULL,
790 		qflags&(BIT_RD|BIT_CD), 0, 0);
791 	if(!s) {
792 		log_err("prefetch_subnet mesh_state_create: out of memory");
793 		return;
794 	}
795 	mesh_state_make_unique(s);
796 
797 	opt = edns_opt_list_find(edns_list, mesh->env->cfg->client_subnet_opcode);
798 	if(opt) {
799 		/* Use the client's ECS data */
800 		if(!edns_opt_list_append(&s->s.edns_opts_front_in, opt->opt_code,
801 			opt->opt_len, opt->opt_data, s->s.region)) {
802 			log_err("prefetch_subnet edns_opt_list_append: out of memory");
803 			return;
804 		}
805 	} else {
806 		/* Fake the ECS data from the client's IP */
807 		struct ecs_data ecs;
808 		memset(&ecs, 0, sizeof(ecs));
809 		subnet_option_from_ss(&rep->addr, &ecs, mesh->env->cfg);
810 		if(ecs.subnet_validdata == 0) {
811 			log_err("prefetch_subnet subnet_option_from_ss: invalid data");
812 			return;
813 		}
814 		subnet_ecs_opt_list_append(&ecs, &s->s.edns_opts_front_in,
815 			&s->s, s->s.region);
816 		if(!s->s.edns_opts_front_in) {
817 			log_err("prefetch_subnet subnet_ecs_opt_list_append: out of memory");
818 			return;
819 		}
820 	}
821 #ifdef UNBOUND_DEBUG
822 	n =
823 #else
824 	(void)
825 #endif
826 	rbtree_insert(&mesh->all, &s->node);
827 	log_assert(n != NULL);
828 	/* set detached (it is now) */
829 	mesh->num_detached_states++;
830 	/* make it ignore the cache */
831 	sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
832 	s->s.prefetch_leeway = leeway;
833 
834 	if(s->list_select == mesh_no_list) {
835 		/* move to either the forever or the jostle_list */
836 		if(mesh->num_forever_states < mesh->max_forever_states) {
837 			mesh->num_forever_states ++;
838 			mesh_list_insert(s, &mesh->forever_first,
839 				&mesh->forever_last);
840 			s->list_select = mesh_forever_list;
841 		} else {
842 			mesh_list_insert(s, &mesh->jostle_first,
843 				&mesh->jostle_last);
844 			s->list_select = mesh_jostle_list;
845 		}
846 	}
847 	s->s.rpz_passthru = rpz_passthru;
848 
849 	if(!run) {
850 #ifdef UNBOUND_DEBUG
851 		n =
852 #else
853 		(void)
854 #endif
855 		rbtree_insert(&mesh->run, &s->run_node);
856 		log_assert(n != NULL);
857 		return;
858 	}
859 
860 	mesh_run(mesh, s, module_event_new, NULL);
861 }
862 #endif /* CLIENT_SUBNET */
863 
864 void mesh_new_prefetch(struct mesh_area* mesh, struct query_info* qinfo,
865 	uint16_t qflags, time_t leeway, int rpz_passthru,
866 	struct comm_reply* rep, struct edns_option* opt_list)
867 {
868 	(void)opt_list;
869 	(void)rep;
870 #ifdef CLIENT_SUBNET
871 	if(rep)
872 		mesh_schedule_prefetch_subnet(mesh, qinfo, qflags, leeway, 1,
873 			rpz_passthru, rep, opt_list);
874 	else
875 #endif
876 		mesh_schedule_prefetch(mesh, qinfo, qflags, leeway, 1,
877 			rpz_passthru);
878 }
879 
880 void mesh_report_reply(struct mesh_area* mesh, struct outbound_entry* e,
881         struct comm_reply* reply, int what)
882 {
883 	enum module_ev event = module_event_reply;
884 	e->qstate->reply = reply;
885 	if(what != NETEVENT_NOERROR) {
886 		event = module_event_noreply;
887 		if(what == NETEVENT_CAPSFAIL)
888 			event = module_event_capsfail;
889 	}
890 	mesh_run(mesh, e->qstate->mesh_info, event, e);
891 }
892 
893 struct mesh_state*
894 mesh_state_create(struct module_env* env, struct query_info* qinfo,
895 	struct respip_client_info* cinfo, uint16_t qflags, int prime,
896 	int valrec)
897 {
898 	struct regional* region = alloc_reg_obtain(env->alloc);
899 	struct mesh_state* mstate;
900 	int i;
901 	if(!region)
902 		return NULL;
903 	mstate = (struct mesh_state*)regional_alloc(region,
904 		sizeof(struct mesh_state));
905 	if(!mstate) {
906 		alloc_reg_release(env->alloc, region);
907 		return NULL;
908 	}
909 	memset(mstate, 0, sizeof(*mstate));
910 	mstate->node = *RBTREE_NULL;
911 	mstate->run_node = *RBTREE_NULL;
912 	mstate->node.key = mstate;
913 	mstate->run_node.key = mstate;
914 	mstate->reply_list = NULL;
915 	mstate->list_select = mesh_no_list;
916 	mstate->replies_sent = 0;
917 	rbtree_init(&mstate->super_set, &mesh_state_ref_compare);
918 	rbtree_init(&mstate->sub_set, &mesh_state_ref_compare);
919 	mstate->num_activated = 0;
920 	mstate->unique = NULL;
921 	/* init module qstate */
922 	mstate->s.qinfo.qtype = qinfo->qtype;
923 	mstate->s.qinfo.qclass = qinfo->qclass;
924 	mstate->s.qinfo.local_alias = NULL;
925 	mstate->s.qinfo.qname_len = qinfo->qname_len;
926 	mstate->s.qinfo.qname = regional_alloc_init(region, qinfo->qname,
927 		qinfo->qname_len);
928 	if(!mstate->s.qinfo.qname) {
929 		alloc_reg_release(env->alloc, region);
930 		return NULL;
931 	}
932 	if(cinfo) {
933 		mstate->s.client_info = regional_alloc_init(region, cinfo,
934 			sizeof(*cinfo));
935 		if(!mstate->s.client_info) {
936 			alloc_reg_release(env->alloc, region);
937 			return NULL;
938 		}
939 	}
940 	/* remove all weird bits from qflags */
941 	mstate->s.query_flags = (qflags & (BIT_RD|BIT_CD));
942 	mstate->s.is_priming = prime;
943 	mstate->s.is_valrec = valrec;
944 	mstate->s.reply = NULL;
945 	mstate->s.region = region;
946 	mstate->s.curmod = 0;
947 	mstate->s.return_msg = 0;
948 	mstate->s.return_rcode = LDNS_RCODE_NOERROR;
949 	mstate->s.env = env;
950 	mstate->s.mesh_info = mstate;
951 	mstate->s.prefetch_leeway = 0;
952 	mstate->s.serve_expired_data = NULL;
953 	mstate->s.no_cache_lookup = 0;
954 	mstate->s.no_cache_store = 0;
955 	mstate->s.need_refetch = 0;
956 	mstate->s.was_ratelimited = 0;
957 
958 	/* init modules */
959 	for(i=0; i<env->mesh->mods.num; i++) {
960 		mstate->s.minfo[i] = NULL;
961 		mstate->s.ext_state[i] = module_state_initial;
962 	}
963 	/* init edns option lists */
964 	mstate->s.edns_opts_front_in = NULL;
965 	mstate->s.edns_opts_back_out = NULL;
966 	mstate->s.edns_opts_back_in = NULL;
967 	mstate->s.edns_opts_front_out = NULL;
968 
969 	return mstate;
970 }
971 
972 int
973 mesh_state_is_unique(struct mesh_state* mstate)
974 {
975 	return mstate->unique != NULL;
976 }
977 
978 void
979 mesh_state_make_unique(struct mesh_state* mstate)
980 {
981 	mstate->unique = mstate;
982 }
983 
984 void
985 mesh_state_cleanup(struct mesh_state* mstate)
986 {
987 	struct mesh_area* mesh;
988 	int i;
989 	if(!mstate)
990 		return;
991 	mesh = mstate->s.env->mesh;
992 	/* Stop and delete the serve expired timer */
993 	if(mstate->s.serve_expired_data && mstate->s.serve_expired_data->timer) {
994 		comm_timer_delete(mstate->s.serve_expired_data->timer);
995 		mstate->s.serve_expired_data->timer = NULL;
996 	}
997 	/* drop unsent replies */
998 	if(!mstate->replies_sent) {
999 		struct mesh_reply* rep = mstate->reply_list;
1000 		struct mesh_cb* cb;
1001 		/* in tcp_req_info, the mstates linked are removed, but
1002 		 * the reply_list is now NULL, so the remove-from-empty-list
1003 		 * takes no time and also it does not do the mesh accounting */
1004 		mstate->reply_list = NULL;
1005 		for(; rep; rep=rep->next) {
1006 			comm_point_drop_reply(&rep->query_reply);
1007 			log_assert(mesh->num_reply_addrs > 0);
1008 			mesh->num_reply_addrs--;
1009 		}
1010 		while((cb = mstate->cb_list)!=NULL) {
1011 			mstate->cb_list = cb->next;
1012 			fptr_ok(fptr_whitelist_mesh_cb(cb->cb));
1013 			(*cb->cb)(cb->cb_arg, LDNS_RCODE_SERVFAIL, NULL,
1014 				sec_status_unchecked, NULL, 0);
1015 			log_assert(mesh->num_reply_addrs > 0);
1016 			mesh->num_reply_addrs--;
1017 		}
1018 	}
1019 
1020 	/* de-init modules */
1021 	for(i=0; i<mesh->mods.num; i++) {
1022 		fptr_ok(fptr_whitelist_mod_clear(mesh->mods.mod[i]->clear));
1023 		(*mesh->mods.mod[i]->clear)(&mstate->s, i);
1024 		mstate->s.minfo[i] = NULL;
1025 		mstate->s.ext_state[i] = module_finished;
1026 	}
1027 	alloc_reg_release(mstate->s.env->alloc, mstate->s.region);
1028 }
1029 
1030 void
1031 mesh_state_delete(struct module_qstate* qstate)
1032 {
1033 	struct mesh_area* mesh;
1034 	struct mesh_state_ref* super, ref;
1035 	struct mesh_state* mstate;
1036 	if(!qstate)
1037 		return;
1038 	mstate = qstate->mesh_info;
1039 	mesh = mstate->s.env->mesh;
1040 	mesh_detach_subs(&mstate->s);
1041 	if(mstate->list_select == mesh_forever_list) {
1042 		mesh->num_forever_states --;
1043 		mesh_list_remove(mstate, &mesh->forever_first,
1044 			&mesh->forever_last);
1045 	} else if(mstate->list_select == mesh_jostle_list) {
1046 		mesh_list_remove(mstate, &mesh->jostle_first,
1047 			&mesh->jostle_last);
1048 	}
1049 	if(!mstate->reply_list && !mstate->cb_list
1050 		&& mstate->super_set.count == 0) {
1051 		log_assert(mesh->num_detached_states > 0);
1052 		mesh->num_detached_states--;
1053 	}
1054 	if(mstate->reply_list || mstate->cb_list) {
1055 		log_assert(mesh->num_reply_states > 0);
1056 		mesh->num_reply_states--;
1057 	}
1058 	ref.node.key = &ref;
1059 	ref.s = mstate;
1060 	RBTREE_FOR(super, struct mesh_state_ref*, &mstate->super_set) {
1061 		(void)rbtree_delete(&super->s->sub_set, &ref);
1062 	}
1063 	(void)rbtree_delete(&mesh->run, mstate);
1064 	(void)rbtree_delete(&mesh->all, mstate);
1065 	mesh_state_cleanup(mstate);
1066 }
1067 
1068 /** helper recursive rbtree find routine */
1069 static int
1070 find_in_subsub(struct mesh_state* m, struct mesh_state* tofind, size_t *c)
1071 {
1072 	struct mesh_state_ref* r;
1073 	if((*c)++ > MESH_MAX_SUBSUB)
1074 		return 1;
1075 	RBTREE_FOR(r, struct mesh_state_ref*, &m->sub_set) {
1076 		if(r->s == tofind || find_in_subsub(r->s, tofind, c))
1077 			return 1;
1078 	}
1079 	return 0;
1080 }
1081 
1082 /** find cycle for already looked up mesh_state */
1083 static int
1084 mesh_detect_cycle_found(struct module_qstate* qstate, struct mesh_state* dep_m)
1085 {
1086 	struct mesh_state* cyc_m = qstate->mesh_info;
1087 	size_t counter = 0;
1088 	if(!dep_m)
1089 		return 0;
1090 	if(dep_m == cyc_m || find_in_subsub(dep_m, cyc_m, &counter)) {
1091 		if(counter > MESH_MAX_SUBSUB)
1092 			return 2;
1093 		return 1;
1094 	}
1095 	return 0;
1096 }
1097 
1098 void mesh_detach_subs(struct module_qstate* qstate)
1099 {
1100 	struct mesh_area* mesh = qstate->env->mesh;
1101 	struct mesh_state_ref* ref, lookup;
1102 #ifdef UNBOUND_DEBUG
1103 	struct rbnode_type* n;
1104 #endif
1105 	lookup.node.key = &lookup;
1106 	lookup.s = qstate->mesh_info;
1107 	RBTREE_FOR(ref, struct mesh_state_ref*, &qstate->mesh_info->sub_set) {
1108 #ifdef UNBOUND_DEBUG
1109 		n =
1110 #else
1111 		(void)
1112 #endif
1113 		rbtree_delete(&ref->s->super_set, &lookup);
1114 		log_assert(n != NULL); /* must have been present */
1115 		if(!ref->s->reply_list && !ref->s->cb_list
1116 			&& ref->s->super_set.count == 0) {
1117 			mesh->num_detached_states++;
1118 			log_assert(mesh->num_detached_states +
1119 				mesh->num_reply_states <= mesh->all.count);
1120 		}
1121 	}
1122 	rbtree_init(&qstate->mesh_info->sub_set, &mesh_state_ref_compare);
1123 }
1124 
1125 int mesh_add_sub(struct module_qstate* qstate, struct query_info* qinfo,
1126         uint16_t qflags, int prime, int valrec, struct module_qstate** newq,
1127 	struct mesh_state** sub)
1128 {
1129 	/* find it, if not, create it */
1130 	struct mesh_area* mesh = qstate->env->mesh;
1131 	*sub = mesh_area_find(mesh, NULL, qinfo, qflags,
1132 		prime, valrec);
1133 	if(mesh_detect_cycle_found(qstate, *sub)) {
1134 		verbose(VERB_ALGO, "attach failed, cycle detected");
1135 		return 0;
1136 	}
1137 	if(!*sub) {
1138 #ifdef UNBOUND_DEBUG
1139 		struct rbnode_type* n;
1140 #endif
1141 		/* create a new one */
1142 		*sub = mesh_state_create(qstate->env, qinfo, NULL, qflags, prime,
1143 			valrec);
1144 		if(!*sub) {
1145 			log_err("mesh_attach_sub: out of memory");
1146 			return 0;
1147 		}
1148 #ifdef UNBOUND_DEBUG
1149 		n =
1150 #else
1151 		(void)
1152 #endif
1153 		rbtree_insert(&mesh->all, &(*sub)->node);
1154 		log_assert(n != NULL);
1155 		/* set detached (it is now) */
1156 		mesh->num_detached_states++;
1157 		/* set new query state to run */
1158 #ifdef UNBOUND_DEBUG
1159 		n =
1160 #else
1161 		(void)
1162 #endif
1163 		rbtree_insert(&mesh->run, &(*sub)->run_node);
1164 		log_assert(n != NULL);
1165 		*newq = &(*sub)->s;
1166 	} else
1167 		*newq = NULL;
1168 	return 1;
1169 }
1170 
1171 int mesh_attach_sub(struct module_qstate* qstate, struct query_info* qinfo,
1172         uint16_t qflags, int prime, int valrec, struct module_qstate** newq)
1173 {
1174 	struct mesh_area* mesh = qstate->env->mesh;
1175 	struct mesh_state* sub = NULL;
1176 	int was_detached;
1177 	if(!mesh_add_sub(qstate, qinfo, qflags, prime, valrec, newq, &sub))
1178 		return 0;
1179 	was_detached = (sub->super_set.count == 0);
1180 	if(!mesh_state_attachment(qstate->mesh_info, sub))
1181 		return 0;
1182 	/* if it was a duplicate  attachment, the count was not zero before */
1183 	if(!sub->reply_list && !sub->cb_list && was_detached &&
1184 		sub->super_set.count == 1) {
1185 		/* it used to be detached, before this one got added */
1186 		log_assert(mesh->num_detached_states > 0);
1187 		mesh->num_detached_states--;
1188 	}
1189 	/* *newq will be run when inited after the current module stops */
1190 	return 1;
1191 }
1192 
1193 int mesh_state_attachment(struct mesh_state* super, struct mesh_state* sub)
1194 {
1195 #ifdef UNBOUND_DEBUG
1196 	struct rbnode_type* n;
1197 #endif
1198 	struct mesh_state_ref* subref; /* points to sub, inserted in super */
1199 	struct mesh_state_ref* superref; /* points to super, inserted in sub */
1200 	if( !(subref = regional_alloc(super->s.region,
1201 		sizeof(struct mesh_state_ref))) ||
1202 		!(superref = regional_alloc(sub->s.region,
1203 		sizeof(struct mesh_state_ref))) ) {
1204 		log_err("mesh_state_attachment: out of memory");
1205 		return 0;
1206 	}
1207 	superref->node.key = superref;
1208 	superref->s = super;
1209 	subref->node.key = subref;
1210 	subref->s = sub;
1211 	if(!rbtree_insert(&sub->super_set, &superref->node)) {
1212 		/* this should not happen, iterator and validator do not
1213 		 * attach subqueries that are identical. */
1214 		/* already attached, we are done, nothing todo.
1215 		 * since superref and subref already allocated in region,
1216 		 * we cannot free them */
1217 		return 1;
1218 	}
1219 #ifdef UNBOUND_DEBUG
1220 	n =
1221 #else
1222 	(void)
1223 #endif
1224 	rbtree_insert(&super->sub_set, &subref->node);
1225 	log_assert(n != NULL); /* we checked above if statement, the reverse
1226 	  administration should not fail now, unless they are out of sync */
1227 	return 1;
1228 }
1229 
1230 /**
1231  * callback results to mesh cb entry
1232  * @param m: mesh state to send it for.
1233  * @param rcode: if not 0, error code.
1234  * @param rep: reply to send (or NULL if rcode is set).
1235  * @param r: callback entry
1236  * @param start_time: the time to pass to callback functions, it is 0 or
1237  * 	a value from one of the packets if the mesh state had packets.
1238  */
1239 static void
1240 mesh_do_callback(struct mesh_state* m, int rcode, struct reply_info* rep,
1241 	struct mesh_cb* r, struct timeval* start_time)
1242 {
1243 	int secure;
1244 	char* reason = NULL;
1245 	int was_ratelimited = m->s.was_ratelimited;
1246 	/* bogus messages are not made into servfail, sec_status passed
1247 	 * to the callback function */
1248 	if(rep && rep->security == sec_status_secure)
1249 		secure = 1;
1250 	else	secure = 0;
1251 	if(!rep && rcode == LDNS_RCODE_NOERROR)
1252 		rcode = LDNS_RCODE_SERVFAIL;
1253 	if(!rcode && (rep->security == sec_status_bogus ||
1254 		rep->security == sec_status_secure_sentinel_fail)) {
1255 		if(!(reason = errinf_to_str_bogus(&m->s)))
1256 			rcode = LDNS_RCODE_SERVFAIL;
1257 	}
1258 	/* send the reply */
1259 	if(rcode) {
1260 		if(rcode == LDNS_RCODE_SERVFAIL) {
1261 			if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1262 				rep, rcode, &r->edns, NULL, m->s.region, start_time))
1263 					r->edns.opt_list_inplace_cb_out = NULL;
1264 		} else {
1265 			if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep, rcode,
1266 				&r->edns, NULL, m->s.region, start_time))
1267 					r->edns.opt_list_inplace_cb_out = NULL;
1268 		}
1269 		fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1270 		(*r->cb)(r->cb_arg, rcode, r->buf, sec_status_unchecked, NULL,
1271 			was_ratelimited);
1272 	} else {
1273 		size_t udp_size = r->edns.udp_size;
1274 		sldns_buffer_clear(r->buf);
1275 		r->edns.edns_version = EDNS_ADVERTISED_VERSION;
1276 		r->edns.udp_size = EDNS_ADVERTISED_SIZE;
1277 		r->edns.ext_rcode = 0;
1278 		r->edns.bits &= EDNS_DO;
1279 
1280 		if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep,
1281 			LDNS_RCODE_NOERROR, &r->edns, NULL, m->s.region, start_time) ||
1282 			!reply_info_answer_encode(&m->s.qinfo, rep, r->qid,
1283 			r->qflags, r->buf, 0, 1,
1284 			m->s.env->scratch, udp_size, &r->edns,
1285 			(int)(r->edns.bits & EDNS_DO), secure))
1286 		{
1287 			fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1288 			(*r->cb)(r->cb_arg, LDNS_RCODE_SERVFAIL, r->buf,
1289 				sec_status_unchecked, NULL, 0);
1290 		} else {
1291 			fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1292 			(*r->cb)(r->cb_arg, LDNS_RCODE_NOERROR, r->buf,
1293 				rep->security, reason, was_ratelimited);
1294 		}
1295 	}
1296 	free(reason);
1297 	log_assert(m->s.env->mesh->num_reply_addrs > 0);
1298 	m->s.env->mesh->num_reply_addrs--;
1299 }
1300 
1301 static inline int
1302 mesh_is_rpz_respip_tcponly_action(struct mesh_state const* m)
1303 {
1304 	struct respip_action_info const* respip_info = m->s.respip_action_info;
1305 	return respip_info == NULL
1306 			? 0
1307 			: (respip_info->rpz_used
1308 			&& !respip_info->rpz_disabled
1309 			&& respip_info->action == respip_truncate);
1310 }
1311 
1312 static inline int
1313 mesh_is_udp(struct mesh_reply const* r) {
1314 	return r->query_reply.c->type == comm_udp;
1315 }
1316 
1317 /**
1318  * Send reply to mesh reply entry
1319  * @param m: mesh state to send it for.
1320  * @param rcode: if not 0, error code.
1321  * @param rep: reply to send (or NULL if rcode is set).
1322  * @param r: reply entry
1323  * @param r_buffer: buffer to use for reply entry.
1324  * @param prev: previous reply, already has its answer encoded in buffer.
1325  * @param prev_buffer: buffer for previous reply.
1326  */
1327 static void
1328 mesh_send_reply(struct mesh_state* m, int rcode, struct reply_info* rep,
1329 	struct mesh_reply* r, struct sldns_buffer* r_buffer,
1330 	struct mesh_reply* prev, struct sldns_buffer* prev_buffer)
1331 {
1332 	struct timeval end_time;
1333 	struct timeval duration;
1334 	int secure;
1335 	/* briefly set the replylist to null in case the
1336 	 * meshsendreply calls tcpreqinfo sendreply that
1337 	 * comm_point_drops because of size, and then the
1338 	 * null stops the mesh state remove and thus
1339 	 * reply_list modification and accounting */
1340 	struct mesh_reply* rlist = m->reply_list;
1341 
1342 	/* rpz: apply actions */
1343 	rcode = mesh_is_udp(r) && mesh_is_rpz_respip_tcponly_action(m)
1344 			? (rcode|BIT_TC) : rcode;
1345 
1346 	/* examine security status */
1347 	if(m->s.env->need_to_validate && (!(r->qflags&BIT_CD) ||
1348 		m->s.env->cfg->ignore_cd) && rep &&
1349 		(rep->security <= sec_status_bogus ||
1350 		rep->security == sec_status_secure_sentinel_fail)) {
1351 		rcode = LDNS_RCODE_SERVFAIL;
1352 		if(m->s.env->cfg->stat_extended)
1353 			m->s.env->mesh->ans_bogus++;
1354 	}
1355 	if(rep && rep->security == sec_status_secure)
1356 		secure = 1;
1357 	else	secure = 0;
1358 	if(!rep && rcode == LDNS_RCODE_NOERROR)
1359 		rcode = LDNS_RCODE_SERVFAIL;
1360 	if(r->query_reply.c->use_h2) {
1361 		r->query_reply.c->h2_stream = r->h2_stream;
1362 		/* Mesh reply won't exist for long anymore. Make it impossible
1363 		 * for HTTP/2 stream to refer to mesh state, in case
1364 		 * connection gets cleanup before HTTP/2 stream close. */
1365 		r->h2_stream->mesh_state = NULL;
1366 	}
1367 	/* send the reply */
1368 	/* We don't reuse the encoded answer if:
1369 	 * - either the previous or current response has a local alias.  We could
1370 	 *   compare the alias records and still reuse the previous answer if they
1371 	 *   are the same, but that would be complicated and error prone for the
1372 	 *   relatively minor case. So we err on the side of safety.
1373 	 * - there are registered callback functions for the given rcode, as these
1374 	 *   need to be called for each reply. */
1375 	if(((rcode != LDNS_RCODE_SERVFAIL &&
1376 			!m->s.env->inplace_cb_lists[inplace_cb_reply]) ||
1377 		(rcode == LDNS_RCODE_SERVFAIL &&
1378 			!m->s.env->inplace_cb_lists[inplace_cb_reply_servfail])) &&
1379 		prev && prev_buffer && prev->qflags == r->qflags &&
1380 		!prev->local_alias && !r->local_alias &&
1381 		prev->edns.edns_present == r->edns.edns_present &&
1382 		prev->edns.bits == r->edns.bits &&
1383 		prev->edns.udp_size == r->edns.udp_size &&
1384 		edns_opt_list_compare(prev->edns.opt_list_out, r->edns.opt_list_out) == 0 &&
1385 		edns_opt_list_compare(prev->edns.opt_list_inplace_cb_out, r->edns.opt_list_inplace_cb_out) == 0
1386 		) {
1387 		/* if the previous reply is identical to this one, fix ID */
1388 		if(prev_buffer != r_buffer)
1389 			sldns_buffer_copy(r_buffer, prev_buffer);
1390 		sldns_buffer_write_at(r_buffer, 0, &r->qid, sizeof(uint16_t));
1391 		sldns_buffer_write_at(r_buffer, 12, r->qname,
1392 			m->s.qinfo.qname_len);
1393 		m->reply_list = NULL;
1394 		comm_point_send_reply(&r->query_reply);
1395 		m->reply_list = rlist;
1396 	} else if(rcode) {
1397 		m->s.qinfo.qname = r->qname;
1398 		m->s.qinfo.local_alias = r->local_alias;
1399 		if(rcode == LDNS_RCODE_SERVFAIL) {
1400 			if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1401 				rep, rcode, &r->edns, &r->query_reply, m->s.region, &r->start_time))
1402 					r->edns.opt_list_inplace_cb_out = NULL;
1403 		} else {
1404 			if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep, rcode,
1405 				&r->edns, &r->query_reply, m->s.region, &r->start_time))
1406 					r->edns.opt_list_inplace_cb_out = NULL;
1407 		}
1408 		/* Send along EDE BOGUS EDNS0 option when answer is bogus */
1409 		if(m->s.env->cfg->ede && rcode == LDNS_RCODE_SERVFAIL &&
1410 			m->s.env->need_to_validate && (!(r->qflags&BIT_CD) ||
1411 			m->s.env->cfg->ignore_cd) && rep &&
1412 			(rep->security <= sec_status_bogus ||
1413 			rep->security == sec_status_secure_sentinel_fail)) {
1414 			char *reason = m->s.env->cfg->val_log_level >= 2
1415 				? errinf_to_str_bogus(&m->s) : NULL;
1416 
1417 			/* During validation the EDE code can be received via two
1418 			 * code paths. One code path fills the reply_info EDE, and
1419 			 * the other fills it in the errinf_strlist. These paths
1420 			 * intersect at some points, but where is opaque due to
1421 			 * the complexity of the validator. At the time of writing
1422 			 * we make the choice to prefer the EDE from errinf_strlist
1423 			 * but a compelling reason to do otherwise is just as valid
1424 			 */
1425 			sldns_ede_code reason_bogus = errinf_to_reason_bogus(&m->s);
1426 			if ((reason_bogus == LDNS_EDE_DNSSEC_BOGUS &&
1427 				rep->reason_bogus != LDNS_EDE_NONE) ||
1428 				reason_bogus == LDNS_EDE_NONE) {
1429 					reason_bogus = rep->reason_bogus;
1430 			}
1431 
1432 			if(reason_bogus != LDNS_EDE_NONE) {
1433 				edns_opt_list_append_ede(&r->edns.opt_list_out,
1434 					m->s.region, reason_bogus, reason);
1435 			}
1436 			free(reason);
1437 		}
1438 		error_encode(r_buffer, rcode, &m->s.qinfo, r->qid,
1439 			r->qflags, &r->edns);
1440 		m->reply_list = NULL;
1441 		comm_point_send_reply(&r->query_reply);
1442 		m->reply_list = rlist;
1443 	} else {
1444 		size_t udp_size = r->edns.udp_size;
1445 		r->edns.edns_version = EDNS_ADVERTISED_VERSION;
1446 		r->edns.udp_size = EDNS_ADVERTISED_SIZE;
1447 		r->edns.ext_rcode = 0;
1448 		r->edns.bits &= EDNS_DO;
1449 		m->s.qinfo.qname = r->qname;
1450 		m->s.qinfo.local_alias = r->local_alias;
1451 		if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep,
1452 			LDNS_RCODE_NOERROR, &r->edns, &r->query_reply, m->s.region, &r->start_time) ||
1453 			!reply_info_answer_encode(&m->s.qinfo, rep, r->qid,
1454 			r->qflags, r_buffer, 0, 1, m->s.env->scratch,
1455 			udp_size, &r->edns, (int)(r->edns.bits & EDNS_DO),
1456 			secure))
1457 		{
1458 			if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1459 			rep, LDNS_RCODE_SERVFAIL, &r->edns, &r->query_reply, m->s.region, &r->start_time))
1460 				r->edns.opt_list_inplace_cb_out = NULL;
1461 			/* internal server error (probably malloc failure) so no
1462 			 * EDE (RFC8914) needed */
1463 			error_encode(r_buffer, LDNS_RCODE_SERVFAIL,
1464 				&m->s.qinfo, r->qid, r->qflags, &r->edns);
1465 		}
1466 		m->reply_list = NULL;
1467 		comm_point_send_reply(&r->query_reply);
1468 		m->reply_list = rlist;
1469 	}
1470 	/* account */
1471 	log_assert(m->s.env->mesh->num_reply_addrs > 0);
1472 	m->s.env->mesh->num_reply_addrs--;
1473 	end_time = *m->s.env->now_tv;
1474 	timeval_subtract(&duration, &end_time, &r->start_time);
1475 	verbose(VERB_ALGO, "query took " ARG_LL "d.%6.6d sec",
1476 		(long long)duration.tv_sec, (int)duration.tv_usec);
1477 	m->s.env->mesh->replies_sent++;
1478 	timeval_add(&m->s.env->mesh->replies_sum_wait, &duration);
1479 	timehist_insert(m->s.env->mesh->histogram, &duration);
1480 	if(m->s.env->cfg->stat_extended) {
1481 		uint16_t rc = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(
1482 			r_buffer, 2));
1483 		if(secure) m->s.env->mesh->ans_secure++;
1484 		m->s.env->mesh->ans_rcode[ rc ] ++;
1485 		if(rc == 0 && LDNS_ANCOUNT(sldns_buffer_begin(r_buffer)) == 0)
1486 			m->s.env->mesh->ans_nodata++;
1487 	}
1488 	/* Log reply sent */
1489 	if(m->s.env->cfg->log_replies) {
1490 		log_reply_info(NO_VERBOSE, &m->s.qinfo, &r->query_reply.addr,
1491 			r->query_reply.addrlen, duration, 0, r_buffer);
1492 	}
1493 }
1494 
1495 void mesh_query_done(struct mesh_state* mstate)
1496 {
1497 	struct mesh_reply* r;
1498 	struct mesh_reply* prev = NULL;
1499 	struct sldns_buffer* prev_buffer = NULL;
1500 	struct mesh_cb* c;
1501 	struct reply_info* rep = (mstate->s.return_msg?
1502 		mstate->s.return_msg->rep:NULL);
1503 	struct timeval tv = {0, 0};
1504 	/* No need for the serve expired timer anymore; we are going to reply. */
1505 	if(mstate->s.serve_expired_data) {
1506 		comm_timer_delete(mstate->s.serve_expired_data->timer);
1507 		mstate->s.serve_expired_data->timer = NULL;
1508 	}
1509 	if(mstate->s.return_rcode == LDNS_RCODE_SERVFAIL ||
1510 		(rep && FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_SERVFAIL)) {
1511 		/* we are SERVFAILing; check for expired answer here */
1512 		mesh_serve_expired_callback(mstate);
1513 		if((mstate->reply_list || mstate->cb_list)
1514 		&& mstate->s.env->cfg->log_servfail
1515 		&& !mstate->s.env->cfg->val_log_squelch) {
1516 			char* err = errinf_to_str_servfail(&mstate->s);
1517 			if(err)
1518 				log_err("%s", err);
1519 			free(err);
1520 		}
1521 	}
1522 	for(r = mstate->reply_list; r; r = r->next) {
1523 		tv = r->start_time;
1524 
1525 		/* if a response-ip address block has been stored the
1526 		 *  information should be logged for each client. */
1527 		if(mstate->s.respip_action_info &&
1528 			mstate->s.respip_action_info->addrinfo) {
1529 			respip_inform_print(mstate->s.respip_action_info,
1530 				r->qname, mstate->s.qinfo.qtype,
1531 				mstate->s.qinfo.qclass, r->local_alias,
1532 				&r->query_reply);
1533 			if(mstate->s.env->cfg->stat_extended &&
1534 				mstate->s.respip_action_info->rpz_used) {
1535 				if(mstate->s.respip_action_info->rpz_disabled)
1536 					mstate->s.env->mesh->rpz_action[RPZ_DISABLED_ACTION]++;
1537 				if(mstate->s.respip_action_info->rpz_cname_override)
1538 					mstate->s.env->mesh->rpz_action[RPZ_CNAME_OVERRIDE_ACTION]++;
1539 				else
1540 					mstate->s.env->mesh->rpz_action[respip_action_to_rpz_action(
1541 						mstate->s.respip_action_info->action)]++;
1542 			}
1543 		}
1544 
1545 		/* if this query is determined to be dropped during the
1546 		 * mesh processing, this is the point to take that action. */
1547 		if(mstate->s.is_drop) {
1548 			/* briefly set the reply_list to NULL, so that the
1549 			 * tcp req info cleanup routine that calls the mesh
1550 			 * to deregister the meshstate for it is not done
1551 			 * because the list is NULL and also accounting is not
1552 			 * done there, but instead we do that here. */
1553 			struct mesh_reply* reply_list = mstate->reply_list;
1554 			mstate->reply_list = NULL;
1555 			comm_point_drop_reply(&r->query_reply);
1556 			mstate->reply_list = reply_list;
1557 		} else {
1558 			struct sldns_buffer* r_buffer = r->query_reply.c->buffer;
1559 			if(r->query_reply.c->tcp_req_info) {
1560 				r_buffer = r->query_reply.c->tcp_req_info->spool_buffer;
1561 				prev_buffer = NULL;
1562 			}
1563 			mesh_send_reply(mstate, mstate->s.return_rcode, rep,
1564 				r, r_buffer, prev, prev_buffer);
1565 			if(r->query_reply.c->tcp_req_info) {
1566 				tcp_req_info_remove_mesh_state(r->query_reply.c->tcp_req_info, mstate);
1567 				r_buffer = NULL;
1568 			}
1569 			prev = r;
1570 			prev_buffer = r_buffer;
1571 		}
1572 	}
1573 	if(mstate->reply_list) {
1574 		mstate->reply_list = NULL;
1575 		if(!mstate->reply_list && !mstate->cb_list) {
1576 			/* was a reply state, not anymore */
1577 			log_assert(mstate->s.env->mesh->num_reply_states > 0);
1578 			mstate->s.env->mesh->num_reply_states--;
1579 		}
1580 		if(!mstate->reply_list && !mstate->cb_list &&
1581 			mstate->super_set.count == 0)
1582 			mstate->s.env->mesh->num_detached_states++;
1583 	}
1584 	mstate->replies_sent = 1;
1585 	while((c = mstate->cb_list) != NULL) {
1586 		/* take this cb off the list; so that the list can be
1587 		 * changed, eg. by adds from the callback routine */
1588 		if(!mstate->reply_list && mstate->cb_list && !c->next) {
1589 			/* was a reply state, not anymore */
1590 			log_assert(mstate->s.env->mesh->num_reply_states > 0);
1591 			mstate->s.env->mesh->num_reply_states--;
1592 		}
1593 		mstate->cb_list = c->next;
1594 		if(!mstate->reply_list && !mstate->cb_list &&
1595 			mstate->super_set.count == 0)
1596 			mstate->s.env->mesh->num_detached_states++;
1597 		mesh_do_callback(mstate, mstate->s.return_rcode, rep, c, &tv);
1598 	}
1599 }
1600 
1601 void mesh_walk_supers(struct mesh_area* mesh, struct mesh_state* mstate)
1602 {
1603 	struct mesh_state_ref* ref;
1604 	RBTREE_FOR(ref, struct mesh_state_ref*, &mstate->super_set)
1605 	{
1606 		/* make super runnable */
1607 		(void)rbtree_insert(&mesh->run, &ref->s->run_node);
1608 		/* callback the function to inform super of result */
1609 		fptr_ok(fptr_whitelist_mod_inform_super(
1610 			mesh->mods.mod[ref->s->s.curmod]->inform_super));
1611 		(*mesh->mods.mod[ref->s->s.curmod]->inform_super)(&mstate->s,
1612 			ref->s->s.curmod, &ref->s->s);
1613 		/* copy state that is always relevant to super */
1614 		copy_state_to_super(&mstate->s, ref->s->s.curmod, &ref->s->s);
1615 	}
1616 }
1617 
1618 struct mesh_state* mesh_area_find(struct mesh_area* mesh,
1619 	struct respip_client_info* cinfo, struct query_info* qinfo,
1620 	uint16_t qflags, int prime, int valrec)
1621 {
1622 	struct mesh_state key;
1623 	struct mesh_state* result;
1624 
1625 	key.node.key = &key;
1626 	key.s.is_priming = prime;
1627 	key.s.is_valrec = valrec;
1628 	key.s.qinfo = *qinfo;
1629 	key.s.query_flags = qflags;
1630 	/* We are searching for a similar mesh state when we DO want to
1631 	 * aggregate the state. Thus unique is set to NULL. (default when we
1632 	 * desire aggregation).*/
1633 	key.unique = NULL;
1634 	key.s.client_info = cinfo;
1635 
1636 	result = (struct mesh_state*)rbtree_search(&mesh->all, &key);
1637 	return result;
1638 }
1639 
1640 int mesh_state_add_cb(struct mesh_state* s, struct edns_data* edns,
1641         sldns_buffer* buf, mesh_cb_func_type cb, void* cb_arg,
1642 	uint16_t qid, uint16_t qflags)
1643 {
1644 	struct mesh_cb* r = regional_alloc(s->s.region,
1645 		sizeof(struct mesh_cb));
1646 	if(!r)
1647 		return 0;
1648 	r->buf = buf;
1649 	log_assert(fptr_whitelist_mesh_cb(cb)); /* early failure ifmissing*/
1650 	r->cb = cb;
1651 	r->cb_arg = cb_arg;
1652 	r->edns = *edns;
1653 	if(edns->opt_list_in && !(r->edns.opt_list_in =
1654 			edns_opt_copy_region(edns->opt_list_in, s->s.region)))
1655 		return 0;
1656 	if(edns->opt_list_out && !(r->edns.opt_list_out =
1657 			edns_opt_copy_region(edns->opt_list_out, s->s.region)))
1658 		return 0;
1659 	if(edns->opt_list_inplace_cb_out && !(r->edns.opt_list_inplace_cb_out =
1660 			edns_opt_copy_region(edns->opt_list_inplace_cb_out, s->s.region)))
1661 		return 0;
1662 	r->qid = qid;
1663 	r->qflags = qflags;
1664 	r->next = s->cb_list;
1665 	s->cb_list = r;
1666 	return 1;
1667 
1668 }
1669 
1670 int mesh_state_add_reply(struct mesh_state* s, struct edns_data* edns,
1671         struct comm_reply* rep, uint16_t qid, uint16_t qflags,
1672         const struct query_info* qinfo)
1673 {
1674 	struct mesh_reply* r = regional_alloc(s->s.region,
1675 		sizeof(struct mesh_reply));
1676 	if(!r)
1677 		return 0;
1678 	r->query_reply = *rep;
1679 	r->edns = *edns;
1680 	if(edns->opt_list_in && !(r->edns.opt_list_in =
1681 			edns_opt_copy_region(edns->opt_list_in, s->s.region)))
1682 		return 0;
1683 	if(edns->opt_list_out && !(r->edns.opt_list_out =
1684 			edns_opt_copy_region(edns->opt_list_out, s->s.region)))
1685 		return 0;
1686 	if(edns->opt_list_inplace_cb_out && !(r->edns.opt_list_inplace_cb_out =
1687 			edns_opt_copy_region(edns->opt_list_inplace_cb_out, s->s.region)))
1688 		return 0;
1689 	r->qid = qid;
1690 	r->qflags = qflags;
1691 	r->start_time = *s->s.env->now_tv;
1692 	r->next = s->reply_list;
1693 	r->qname = regional_alloc_init(s->s.region, qinfo->qname,
1694 		s->s.qinfo.qname_len);
1695 	if(!r->qname)
1696 		return 0;
1697 	if(rep->c->use_h2)
1698 		r->h2_stream = rep->c->h2_stream;
1699 
1700 	/* Data related to local alias stored in 'qinfo' (if any) is ephemeral
1701 	 * and can be different for different original queries (even if the
1702 	 * replaced query name is the same).  So we need to make a deep copy
1703 	 * and store the copy for each reply info. */
1704 	if(qinfo->local_alias) {
1705 		struct packed_rrset_data* d;
1706 		struct packed_rrset_data* dsrc;
1707 		r->local_alias = regional_alloc_zero(s->s.region,
1708 			sizeof(*qinfo->local_alias));
1709 		if(!r->local_alias)
1710 			return 0;
1711 		r->local_alias->rrset = regional_alloc_init(s->s.region,
1712 			qinfo->local_alias->rrset,
1713 			sizeof(*qinfo->local_alias->rrset));
1714 		if(!r->local_alias->rrset)
1715 			return 0;
1716 		dsrc = qinfo->local_alias->rrset->entry.data;
1717 
1718 		/* In the current implementation, a local alias must be
1719 		 * a single CNAME RR (see worker_handle_request()). */
1720 		log_assert(!qinfo->local_alias->next && dsrc->count == 1 &&
1721 			qinfo->local_alias->rrset->rk.type ==
1722 			htons(LDNS_RR_TYPE_CNAME));
1723 		/* we should make a local copy for the owner name of
1724 		 * the RRset */
1725 		r->local_alias->rrset->rk.dname_len =
1726 			qinfo->local_alias->rrset->rk.dname_len;
1727 		r->local_alias->rrset->rk.dname = regional_alloc_init(
1728 			s->s.region, qinfo->local_alias->rrset->rk.dname,
1729 			qinfo->local_alias->rrset->rk.dname_len);
1730 		if(!r->local_alias->rrset->rk.dname)
1731 			return 0;
1732 
1733 		/* the rrset is not packed, like in the cache, but it is
1734 		 * individually allocated with an allocator from localzone. */
1735 		d = regional_alloc_zero(s->s.region, sizeof(*d));
1736 		if(!d)
1737 			return 0;
1738 		r->local_alias->rrset->entry.data = d;
1739 		if(!rrset_insert_rr(s->s.region, d, dsrc->rr_data[0],
1740 			dsrc->rr_len[0], dsrc->rr_ttl[0], "CNAME local alias"))
1741 			return 0;
1742 	} else
1743 		r->local_alias = NULL;
1744 
1745 	s->reply_list = r;
1746 	return 1;
1747 }
1748 
1749 /* Extract the query info and flags from 'mstate' into '*qinfop' and '*qflags'.
1750  * Since this is only used for internal refetch of otherwise-expired answer,
1751  * we simply ignore the rare failure mode when memory allocation fails. */
1752 static void
1753 mesh_copy_qinfo(struct mesh_state* mstate, struct query_info** qinfop,
1754 	uint16_t* qflags)
1755 {
1756 	struct regional* region = mstate->s.env->scratch;
1757 	struct query_info* qinfo;
1758 
1759 	qinfo = regional_alloc_init(region, &mstate->s.qinfo, sizeof(*qinfo));
1760 	if(!qinfo)
1761 		return;
1762 	qinfo->qname = regional_alloc_init(region, qinfo->qname,
1763 		qinfo->qname_len);
1764 	if(!qinfo->qname)
1765 		return;
1766 	*qinfop = qinfo;
1767 	*qflags = mstate->s.query_flags;
1768 }
1769 
1770 /**
1771  * Continue processing the mesh state at another module.
1772  * Handles module to modules transfer of control.
1773  * Handles module finished.
1774  * @param mesh: the mesh area.
1775  * @param mstate: currently active mesh state.
1776  * 	Deleted if finished, calls _done and _supers to
1777  * 	send replies to clients and inform other mesh states.
1778  * 	This in turn may create additional runnable mesh states.
1779  * @param s: state at which the current module exited.
1780  * @param ev: the event sent to the module.
1781  * 	returned is the event to send to the next module.
1782  * @return true if continue processing at the new module.
1783  * 	false if not continued processing is needed.
1784  */
1785 static int
1786 mesh_continue(struct mesh_area* mesh, struct mesh_state* mstate,
1787 	enum module_ext_state s, enum module_ev* ev)
1788 {
1789 	mstate->num_activated++;
1790 	if(mstate->num_activated > MESH_MAX_ACTIVATION) {
1791 		/* module is looping. Stop it. */
1792 		log_err("internal error: looping module (%s) stopped",
1793 			mesh->mods.mod[mstate->s.curmod]->name);
1794 		log_query_info(NO_VERBOSE, "pass error for qstate",
1795 			&mstate->s.qinfo);
1796 		s = module_error;
1797 	}
1798 	if(s == module_wait_module || s == module_restart_next) {
1799 		/* start next module */
1800 		mstate->s.curmod++;
1801 		if(mesh->mods.num == mstate->s.curmod) {
1802 			log_err("Cannot pass to next module; at last module");
1803 			log_query_info(VERB_QUERY, "pass error for qstate",
1804 				&mstate->s.qinfo);
1805 			mstate->s.curmod--;
1806 			return mesh_continue(mesh, mstate, module_error, ev);
1807 		}
1808 		if(s == module_restart_next) {
1809 			int curmod = mstate->s.curmod;
1810 			for(; mstate->s.curmod < mesh->mods.num;
1811 				mstate->s.curmod++) {
1812 				fptr_ok(fptr_whitelist_mod_clear(
1813 					mesh->mods.mod[mstate->s.curmod]->clear));
1814 				(*mesh->mods.mod[mstate->s.curmod]->clear)
1815 					(&mstate->s, mstate->s.curmod);
1816 				mstate->s.minfo[mstate->s.curmod] = NULL;
1817 			}
1818 			mstate->s.curmod = curmod;
1819 		}
1820 		*ev = module_event_pass;
1821 		return 1;
1822 	}
1823 	if(s == module_wait_subquery && mstate->sub_set.count == 0) {
1824 		log_err("module cannot wait for subquery, subquery list empty");
1825 		log_query_info(VERB_QUERY, "pass error for qstate",
1826 			&mstate->s.qinfo);
1827 		s = module_error;
1828 	}
1829 	if(s == module_error && mstate->s.return_rcode == LDNS_RCODE_NOERROR) {
1830 		/* error is bad, handle pass back up below */
1831 		mstate->s.return_rcode = LDNS_RCODE_SERVFAIL;
1832 	}
1833 	if(s == module_error) {
1834 		mesh_query_done(mstate);
1835 		mesh_walk_supers(mesh, mstate);
1836 		mesh_state_delete(&mstate->s);
1837 		return 0;
1838 	}
1839 	if(s == module_finished) {
1840 		if(mstate->s.curmod == 0) {
1841 			struct query_info* qinfo = NULL;
1842 			uint16_t qflags;
1843 			int rpz_p = 0;
1844 
1845 			mesh_query_done(mstate);
1846 			mesh_walk_supers(mesh, mstate);
1847 
1848 			/* If the answer to the query needs to be refetched
1849 			 * from an external DNS server, we'll need to schedule
1850 			 * a prefetch after removing the current state, so
1851 			 * we need to make a copy of the query info here. */
1852 			if(mstate->s.need_refetch) {
1853 				mesh_copy_qinfo(mstate, &qinfo, &qflags);
1854 				rpz_p = mstate->s.rpz_passthru;
1855 			}
1856 
1857 			mesh_state_delete(&mstate->s);
1858 			if(qinfo) {
1859 				mesh_schedule_prefetch(mesh, qinfo, qflags,
1860 					0, 1, rpz_p);
1861 			}
1862 			return 0;
1863 		}
1864 		/* pass along the locus of control */
1865 		mstate->s.curmod --;
1866 		*ev = module_event_moddone;
1867 		return 1;
1868 	}
1869 	return 0;
1870 }
1871 
1872 void mesh_run(struct mesh_area* mesh, struct mesh_state* mstate,
1873 	enum module_ev ev, struct outbound_entry* e)
1874 {
1875 	enum module_ext_state s;
1876 	verbose(VERB_ALGO, "mesh_run: start");
1877 	while(mstate) {
1878 		/* run the module */
1879 		fptr_ok(fptr_whitelist_mod_operate(
1880 			mesh->mods.mod[mstate->s.curmod]->operate));
1881 		(*mesh->mods.mod[mstate->s.curmod]->operate)
1882 			(&mstate->s, ev, mstate->s.curmod, e);
1883 
1884 		/* examine results */
1885 		mstate->s.reply = NULL;
1886 		regional_free_all(mstate->s.env->scratch);
1887 		s = mstate->s.ext_state[mstate->s.curmod];
1888 		verbose(VERB_ALGO, "mesh_run: %s module exit state is %s",
1889 			mesh->mods.mod[mstate->s.curmod]->name, strextstate(s));
1890 		e = NULL;
1891 		if(mesh_continue(mesh, mstate, s, &ev))
1892 			continue;
1893 
1894 		/* run more modules */
1895 		ev = module_event_pass;
1896 		if(mesh->run.count > 0) {
1897 			/* pop random element off the runnable tree */
1898 			mstate = (struct mesh_state*)mesh->run.root->key;
1899 			(void)rbtree_delete(&mesh->run, mstate);
1900 		} else mstate = NULL;
1901 	}
1902 	if(verbosity >= VERB_ALGO) {
1903 		mesh_stats(mesh, "mesh_run: end");
1904 		mesh_log_list(mesh);
1905 	}
1906 }
1907 
1908 void
1909 mesh_log_list(struct mesh_area* mesh)
1910 {
1911 	char buf[30];
1912 	struct mesh_state* m;
1913 	int num = 0;
1914 	RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1915 		snprintf(buf, sizeof(buf), "%d%s%s%s%s%s%s mod%d %s%s",
1916 			num++, (m->s.is_priming)?"p":"",  /* prime */
1917 			(m->s.is_valrec)?"v":"",  /* prime */
1918 			(m->s.query_flags&BIT_RD)?"RD":"",
1919 			(m->s.query_flags&BIT_CD)?"CD":"",
1920 			(m->super_set.count==0)?"d":"", /* detached */
1921 			(m->sub_set.count!=0)?"c":"",  /* children */
1922 			m->s.curmod, (m->reply_list)?"rep":"", /*hasreply*/
1923 			(m->cb_list)?"cb":"" /* callbacks */
1924 			);
1925 		log_query_info(VERB_ALGO, buf, &m->s.qinfo);
1926 	}
1927 }
1928 
1929 void
1930 mesh_stats(struct mesh_area* mesh, const char* str)
1931 {
1932 	verbose(VERB_DETAIL, "%s %u recursion states (%u with reply, "
1933 		"%u detached), %u waiting replies, %u recursion replies "
1934 		"sent, %d replies dropped, %d states jostled out",
1935 		str, (unsigned)mesh->all.count,
1936 		(unsigned)mesh->num_reply_states,
1937 		(unsigned)mesh->num_detached_states,
1938 		(unsigned)mesh->num_reply_addrs,
1939 		(unsigned)mesh->replies_sent,
1940 		(unsigned)mesh->stats_dropped,
1941 		(unsigned)mesh->stats_jostled);
1942 	if(mesh->replies_sent > 0) {
1943 		struct timeval avg;
1944 		timeval_divide(&avg, &mesh->replies_sum_wait,
1945 			mesh->replies_sent);
1946 		log_info("average recursion processing time "
1947 			ARG_LL "d.%6.6d sec",
1948 			(long long)avg.tv_sec, (int)avg.tv_usec);
1949 		log_info("histogram of recursion processing times");
1950 		timehist_log(mesh->histogram, "recursions");
1951 	}
1952 }
1953 
1954 void
1955 mesh_stats_clear(struct mesh_area* mesh)
1956 {
1957 	if(!mesh)
1958 		return;
1959 	mesh->replies_sent = 0;
1960 	mesh->replies_sum_wait.tv_sec = 0;
1961 	mesh->replies_sum_wait.tv_usec = 0;
1962 	mesh->stats_jostled = 0;
1963 	mesh->stats_dropped = 0;
1964 	timehist_clear(mesh->histogram);
1965 	mesh->ans_secure = 0;
1966 	mesh->ans_bogus = 0;
1967 	mesh->ans_expired = 0;
1968 	memset(&mesh->ans_rcode[0], 0, sizeof(size_t)*UB_STATS_RCODE_NUM);
1969 	memset(&mesh->rpz_action[0], 0, sizeof(size_t)*UB_STATS_RPZ_ACTION_NUM);
1970 	mesh->ans_nodata = 0;
1971 }
1972 
1973 size_t
1974 mesh_get_mem(struct mesh_area* mesh)
1975 {
1976 	struct mesh_state* m;
1977 	size_t s = sizeof(*mesh) + sizeof(struct timehist) +
1978 		sizeof(struct th_buck)*mesh->histogram->num +
1979 		sizeof(sldns_buffer) + sldns_buffer_capacity(mesh->qbuf_bak);
1980 	RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1981 		/* all, including m itself allocated in qstate region */
1982 		s += regional_get_mem(m->s.region);
1983 	}
1984 	return s;
1985 }
1986 
1987 int
1988 mesh_detect_cycle(struct module_qstate* qstate, struct query_info* qinfo,
1989 	uint16_t flags, int prime, int valrec)
1990 {
1991 	struct mesh_area* mesh = qstate->env->mesh;
1992 	struct mesh_state* dep_m = NULL;
1993 	dep_m = mesh_area_find(mesh, NULL, qinfo, flags, prime, valrec);
1994 	return mesh_detect_cycle_found(qstate, dep_m);
1995 }
1996 
1997 void mesh_list_insert(struct mesh_state* m, struct mesh_state** fp,
1998         struct mesh_state** lp)
1999 {
2000 	/* insert as last element */
2001 	m->prev = *lp;
2002 	m->next = NULL;
2003 	if(*lp)
2004 		(*lp)->next = m;
2005 	else	*fp = m;
2006 	*lp = m;
2007 }
2008 
2009 void mesh_list_remove(struct mesh_state* m, struct mesh_state** fp,
2010         struct mesh_state** lp)
2011 {
2012 	if(m->next)
2013 		m->next->prev = m->prev;
2014 	else	*lp = m->prev;
2015 	if(m->prev)
2016 		m->prev->next = m->next;
2017 	else	*fp = m->next;
2018 }
2019 
2020 void mesh_state_remove_reply(struct mesh_area* mesh, struct mesh_state* m,
2021 	struct comm_point* cp)
2022 {
2023 	struct mesh_reply* n, *prev = NULL;
2024 	n = m->reply_list;
2025 	/* when in mesh_cleanup, it sets the reply_list to NULL, so that
2026 	 * there is no accounting twice */
2027 	if(!n) return; /* nothing to remove, also no accounting needed */
2028 	while(n) {
2029 		if(n->query_reply.c == cp) {
2030 			/* unlink it */
2031 			if(prev) prev->next = n->next;
2032 			else m->reply_list = n->next;
2033 			/* delete it, but allocated in m region */
2034 			log_assert(mesh->num_reply_addrs > 0);
2035 			mesh->num_reply_addrs--;
2036 
2037 			/* prev = prev; */
2038 			n = n->next;
2039 			continue;
2040 		}
2041 		prev = n;
2042 		n = n->next;
2043 	}
2044 	/* it was not detached (because it had a reply list), could be now */
2045 	if(!m->reply_list && !m->cb_list
2046 		&& m->super_set.count == 0) {
2047 		mesh->num_detached_states++;
2048 	}
2049 	/* if not replies any more in mstate, it is no longer a reply_state */
2050 	if(!m->reply_list && !m->cb_list) {
2051 		log_assert(mesh->num_reply_states > 0);
2052 		mesh->num_reply_states--;
2053 	}
2054 }
2055 
2056 
2057 static int
2058 apply_respip_action(struct module_qstate* qstate,
2059 	const struct query_info* qinfo, struct respip_client_info* cinfo,
2060 	struct respip_action_info* actinfo, struct reply_info* rep,
2061 	struct ub_packed_rrset_key** alias_rrset,
2062 	struct reply_info** encode_repp, struct auth_zones* az)
2063 {
2064 	if(qinfo->qtype != LDNS_RR_TYPE_A &&
2065 		qinfo->qtype != LDNS_RR_TYPE_AAAA &&
2066 		qinfo->qtype != LDNS_RR_TYPE_ANY)
2067 		return 1;
2068 
2069 	if(!respip_rewrite_reply(qinfo, cinfo, rep, encode_repp, actinfo,
2070 		alias_rrset, 0, qstate->region, az, NULL))
2071 		return 0;
2072 
2073 	/* xxx_deny actions mean dropping the reply, unless the original reply
2074 	 * was redirected to response-ip data. */
2075 	if((actinfo->action == respip_deny ||
2076 		actinfo->action == respip_inform_deny) &&
2077 		*encode_repp == rep)
2078 		*encode_repp = NULL;
2079 
2080 	return 1;
2081 }
2082 
2083 void
2084 mesh_serve_expired_callback(void* arg)
2085 {
2086 	struct mesh_state* mstate = (struct mesh_state*) arg;
2087 	struct module_qstate* qstate = &mstate->s;
2088 	struct mesh_reply* r;
2089 	struct mesh_area* mesh = qstate->env->mesh;
2090 	struct dns_msg* msg;
2091 	struct mesh_cb* c;
2092 	struct mesh_reply* prev = NULL;
2093 	struct sldns_buffer* prev_buffer = NULL;
2094 	struct sldns_buffer* r_buffer = NULL;
2095 	struct reply_info* partial_rep = NULL;
2096 	struct ub_packed_rrset_key* alias_rrset = NULL;
2097 	struct reply_info* encode_rep = NULL;
2098 	struct respip_action_info actinfo;
2099 	struct query_info* lookup_qinfo = &qstate->qinfo;
2100 	struct query_info qinfo_tmp;
2101 	struct timeval tv = {0, 0};
2102 	int must_validate = (!(qstate->query_flags&BIT_CD)
2103 		|| qstate->env->cfg->ignore_cd) && qstate->env->need_to_validate;
2104 	if(!qstate->serve_expired_data) return;
2105 	verbose(VERB_ALGO, "Serve expired: Trying to reply with expired data");
2106 	comm_timer_delete(qstate->serve_expired_data->timer);
2107 	qstate->serve_expired_data->timer = NULL;
2108 	/* If is_drop or no_cache_lookup (modules that handle their own cache e.g.,
2109 	 * subnetmod) ignore stale data from the main cache. */
2110 	if(qstate->no_cache_lookup || qstate->is_drop) {
2111 		verbose(VERB_ALGO,
2112 			"Serve expired: Not allowed to look into cache for stale");
2113 		return;
2114 	}
2115 	/* The following while is used instead of the `goto lookup_cache`
2116 	 * like in the worker. */
2117 	while(1) {
2118 		fptr_ok(fptr_whitelist_serve_expired_lookup(
2119 			qstate->serve_expired_data->get_cached_answer));
2120 		msg = (*qstate->serve_expired_data->get_cached_answer)(qstate,
2121 			lookup_qinfo);
2122 		if(!msg)
2123 			return;
2124 		/* Reset these in case we pass a second time from here. */
2125 		encode_rep = msg->rep;
2126 		memset(&actinfo, 0, sizeof(actinfo));
2127 		actinfo.action = respip_none;
2128 		alias_rrset = NULL;
2129 		if((mesh->use_response_ip || mesh->use_rpz) &&
2130 			!partial_rep && !apply_respip_action(qstate, &qstate->qinfo,
2131 			qstate->client_info, &actinfo, msg->rep, &alias_rrset, &encode_rep,
2132 			qstate->env->auth_zones)) {
2133 			return;
2134 		} else if(partial_rep &&
2135 			!respip_merge_cname(partial_rep, &qstate->qinfo, msg->rep,
2136 			qstate->client_info, must_validate, &encode_rep, qstate->region,
2137 			qstate->env->auth_zones)) {
2138 			return;
2139 		}
2140 		if(!encode_rep || alias_rrset) {
2141 			if(!encode_rep) {
2142 				/* Needs drop */
2143 				return;
2144 			} else {
2145 				/* A partial CNAME chain is found. */
2146 				partial_rep = encode_rep;
2147 			}
2148 		}
2149 		/* We've found a partial reply ending with an
2150 		* alias.  Replace the lookup qinfo for the
2151 		* alias target and lookup the cache again to
2152 		* (possibly) complete the reply.  As we're
2153 		* passing the "base" reply, there will be no
2154 		* more alias chasing. */
2155 		if(partial_rep) {
2156 			memset(&qinfo_tmp, 0, sizeof(qinfo_tmp));
2157 			get_cname_target(alias_rrset, &qinfo_tmp.qname,
2158 				&qinfo_tmp.qname_len);
2159 			if(!qinfo_tmp.qname) {
2160 				log_err("Serve expired: unexpected: invalid answer alias");
2161 				return;
2162 			}
2163 			qinfo_tmp.qtype = qstate->qinfo.qtype;
2164 			qinfo_tmp.qclass = qstate->qinfo.qclass;
2165 			lookup_qinfo = &qinfo_tmp;
2166 			continue;
2167 		}
2168 		break;
2169 	}
2170 
2171 	if(verbosity >= VERB_ALGO)
2172 		log_dns_msg("Serve expired lookup", &qstate->qinfo, msg->rep);
2173 
2174 	for(r = mstate->reply_list; r; r = r->next) {
2175 		tv = r->start_time;
2176 
2177 		/* If address info is returned, it means the action should be an
2178 		* 'inform' variant and the information should be logged. */
2179 		if(actinfo.addrinfo) {
2180 			respip_inform_print(&actinfo, r->qname,
2181 				qstate->qinfo.qtype, qstate->qinfo.qclass,
2182 				r->local_alias, &r->query_reply);
2183 
2184 			if(qstate->env->cfg->stat_extended && actinfo.rpz_used) {
2185 				if(actinfo.rpz_disabled)
2186 					qstate->env->mesh->rpz_action[RPZ_DISABLED_ACTION]++;
2187 				if(actinfo.rpz_cname_override)
2188 					qstate->env->mesh->rpz_action[RPZ_CNAME_OVERRIDE_ACTION]++;
2189 				else
2190 					qstate->env->mesh->rpz_action[
2191 						respip_action_to_rpz_action(actinfo.action)]++;
2192 			}
2193 		}
2194 
2195 		/* Add EDE Stale Answer (RCF8914). Ignore global ede as this is
2196 		 * warning instead of an error */
2197 		if (r->edns.edns_present && qstate->env->cfg->ede_serve_expired &&
2198 			qstate->env->cfg->ede) {
2199 			edns_opt_list_append_ede(&r->edns.opt_list_out,
2200 				mstate->s.region, LDNS_EDE_STALE_ANSWER, NULL);
2201 		}
2202 
2203 		r_buffer = r->query_reply.c->buffer;
2204 		if(r->query_reply.c->tcp_req_info)
2205 			r_buffer = r->query_reply.c->tcp_req_info->spool_buffer;
2206 		mesh_send_reply(mstate, LDNS_RCODE_NOERROR, msg->rep,
2207 			r, r_buffer, prev, prev_buffer);
2208 		if(r->query_reply.c->tcp_req_info)
2209 			tcp_req_info_remove_mesh_state(r->query_reply.c->tcp_req_info, mstate);
2210 		prev = r;
2211 		prev_buffer = r_buffer;
2212 
2213 		/* Account for each reply sent. */
2214 		mesh->ans_expired++;
2215 
2216 	}
2217 	if(mstate->reply_list) {
2218 		mstate->reply_list = NULL;
2219 		if(!mstate->reply_list && !mstate->cb_list) {
2220 			log_assert(mesh->num_reply_states > 0);
2221 			mesh->num_reply_states--;
2222 			if(mstate->super_set.count == 0) {
2223 				mesh->num_detached_states++;
2224 			}
2225 		}
2226 	}
2227 	while((c = mstate->cb_list) != NULL) {
2228 		/* take this cb off the list; so that the list can be
2229 		 * changed, eg. by adds from the callback routine */
2230 		if(!mstate->reply_list && mstate->cb_list && !c->next) {
2231 			/* was a reply state, not anymore */
2232 			log_assert(qstate->env->mesh->num_reply_states > 0);
2233 			qstate->env->mesh->num_reply_states--;
2234 		}
2235 		mstate->cb_list = c->next;
2236 		if(!mstate->reply_list && !mstate->cb_list &&
2237 			mstate->super_set.count == 0)
2238 			qstate->env->mesh->num_detached_states++;
2239 		mesh_do_callback(mstate, LDNS_RCODE_NOERROR, msg->rep, c, &tv);
2240 	}
2241 }
2242