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