1 /*
2 * daemon/worker.c - worker that handles a pending list of requests.
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 implements the worker that handles callbacks on events, for
40 * pending requests.
41 */
42 #include "config.h"
43 #include "util/log.h"
44 #include "util/net_help.h"
45 #include "util/random.h"
46 #include "daemon/worker.h"
47 #include "daemon/daemon.h"
48 #include "daemon/remote.h"
49 #include "daemon/acl_list.h"
50 #include "util/netevent.h"
51 #include "util/config_file.h"
52 #include "util/module.h"
53 #include "util/regional.h"
54 #include "util/storage/slabhash.h"
55 #include "services/listen_dnsport.h"
56 #include "services/outside_network.h"
57 #include "services/outbound_list.h"
58 #include "services/cache/rrset.h"
59 #include "services/cache/infra.h"
60 #include "services/cache/dns.h"
61 #include "services/authzone.h"
62 #include "services/mesh.h"
63 #include "services/localzone.h"
64 #include "services/rpz.h"
65 #include "util/data/msgparse.h"
66 #include "util/data/msgencode.h"
67 #include "util/data/dname.h"
68 #include "util/fptr_wlist.h"
69 #include "util/proxy_protocol.h"
70 #include "util/tube.h"
71 #include "util/edns.h"
72 #include "util/timeval_func.h"
73 #include "iterator/iter_fwd.h"
74 #include "iterator/iter_hints.h"
75 #include "iterator/iter_utils.h"
76 #include "validator/autotrust.h"
77 #include "validator/val_anchor.h"
78 #include "respip/respip.h"
79 #include "libunbound/context.h"
80 #include "libunbound/libworker.h"
81 #include "sldns/sbuffer.h"
82 #include "sldns/wire2str.h"
83 #include "util/shm_side/shm_main.h"
84 #include "dnscrypt/dnscrypt.h"
85 #include "dnstap/dtstream.h"
86
87 #ifdef HAVE_SYS_TYPES_H
88 # include <sys/types.h>
89 #endif
90 #ifdef HAVE_NETDB_H
91 #include <netdb.h>
92 #endif
93 #include <signal.h>
94 #ifdef UB_ON_WINDOWS
95 #include "winrc/win_svc.h"
96 #endif
97
98 /** Size of an UDP datagram */
99 #define NORMAL_UDP_SIZE 512 /* bytes */
100 /** ratelimit for error responses */
101 #define ERROR_RATELIMIT 100 /* qps */
102
103 /**
104 * seconds to add to prefetch leeway. This is a TTL that expires old rrsets
105 * earlier than they should in order to put the new update into the cache.
106 * This additional value is to make sure that if not all TTLs are equal in
107 * the message to be updated(and replaced), that rrsets with up to this much
108 * extra TTL are also replaced. This means that the resulting new message
109 * will have (most likely) this TTL at least, avoiding very small 'split
110 * second' TTLs due to operators choosing relative primes for TTLs (or so).
111 * Also has to be at least one to break ties (and overwrite cached entry).
112 */
113 #define PREFETCH_EXPIRY_ADD 60
114
115 /** Report on memory usage by this thread and global */
116 static void
worker_mem_report(struct worker * ATTR_UNUSED (worker),struct serviced_query * ATTR_UNUSED (cur_serv))117 worker_mem_report(struct worker* ATTR_UNUSED(worker),
118 struct serviced_query* ATTR_UNUSED(cur_serv))
119 {
120 #ifdef UNBOUND_ALLOC_STATS
121 /* measure memory leakage */
122 extern size_t unbound_mem_alloc, unbound_mem_freed;
123 /* debug func in validator module */
124 size_t total, front, back, mesh, msg, rrset, infra, ac, superac;
125 size_t me, iter, val, anch;
126 int i;
127 #ifdef CLIENT_SUBNET
128 size_t subnet = 0;
129 #endif /* CLIENT_SUBNET */
130 if(verbosity < VERB_ALGO)
131 return;
132 front = listen_get_mem(worker->front);
133 back = outnet_get_mem(worker->back);
134 msg = slabhash_get_mem(worker->env.msg_cache);
135 rrset = slabhash_get_mem(&worker->env.rrset_cache->table);
136 infra = infra_get_mem(worker->env.infra_cache);
137 mesh = mesh_get_mem(worker->env.mesh);
138 ac = alloc_get_mem(worker->alloc);
139 superac = alloc_get_mem(&worker->daemon->superalloc);
140 anch = anchors_get_mem(worker->env.anchors);
141 iter = 0;
142 val = 0;
143 for(i=0; i<worker->env.mesh->mods.num; i++) {
144 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
145 mods.mod[i]->get_mem));
146 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
147 val += (*worker->env.mesh->mods.mod[i]->get_mem)
148 (&worker->env, i);
149 #ifdef CLIENT_SUBNET
150 else if(strcmp(worker->env.mesh->mods.mod[i]->name,
151 "subnetcache")==0)
152 subnet += (*worker->env.mesh->mods.mod[i]->get_mem)
153 (&worker->env, i);
154 #endif /* CLIENT_SUBNET */
155 else iter += (*worker->env.mesh->mods.mod[i]->get_mem)
156 (&worker->env, i);
157 }
158 me = sizeof(*worker) + sizeof(*worker->base) + sizeof(*worker->comsig)
159 + comm_point_get_mem(worker->cmd_com)
160 + sizeof(worker->rndstate)
161 + regional_get_mem(worker->scratchpad)
162 + sizeof(*worker->env.scratch_buffer)
163 + sldns_buffer_capacity(worker->env.scratch_buffer);
164 if(worker->daemon->env->fwds)
165 log_info("forwards=%u", (unsigned)forwards_get_mem(worker->env.fwds));
166 if(worker->daemon->env->hints)
167 log_info("hints=%u", (unsigned)hints_get_mem(worker->env.hints));
168 if(worker->thread_num == 0)
169 me += acl_list_get_mem(worker->daemon->acl);
170 if(cur_serv) {
171 me += serviced_get_mem(cur_serv);
172 }
173 total = front+back+mesh+msg+rrset+infra+iter+val+ac+superac+me;
174 #ifdef CLIENT_SUBNET
175 total += subnet;
176 log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
177 "rrset=%u infra=%u iter=%u val=%u subnet=%u anchors=%u "
178 "alloccache=%u globalalloccache=%u me=%u",
179 (unsigned)total, (unsigned)front, (unsigned)back,
180 (unsigned)mesh, (unsigned)msg, (unsigned)rrset, (unsigned)infra,
181 (unsigned)iter, (unsigned)val,
182 (unsigned)subnet, (unsigned)anch, (unsigned)ac,
183 (unsigned)superac, (unsigned)me);
184 #else /* no CLIENT_SUBNET */
185 log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
186 "rrset=%u infra=%u iter=%u val=%u anchors=%u "
187 "alloccache=%u globalalloccache=%u me=%u",
188 (unsigned)total, (unsigned)front, (unsigned)back,
189 (unsigned)mesh, (unsigned)msg, (unsigned)rrset,
190 (unsigned)infra, (unsigned)iter, (unsigned)val, (unsigned)anch,
191 (unsigned)ac, (unsigned)superac, (unsigned)me);
192 #endif /* CLIENT_SUBNET */
193 log_info("Total heap memory estimate: %u total-alloc: %u "
194 "total-free: %u", (unsigned)total,
195 (unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
196 #else /* no UNBOUND_ALLOC_STATS */
197 size_t val = 0;
198 #ifdef CLIENT_SUBNET
199 size_t subnet = 0;
200 #endif /* CLIENT_SUBNET */
201 int i;
202 if(verbosity < VERB_QUERY)
203 return;
204 for(i=0; i<worker->env.mesh->mods.num; i++) {
205 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
206 mods.mod[i]->get_mem));
207 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
208 val += (*worker->env.mesh->mods.mod[i]->get_mem)
209 (&worker->env, i);
210 #ifdef CLIENT_SUBNET
211 else if(strcmp(worker->env.mesh->mods.mod[i]->name,
212 "subnetcache")==0)
213 subnet += (*worker->env.mesh->mods.mod[i]->get_mem)
214 (&worker->env, i);
215 #endif /* CLIENT_SUBNET */
216 }
217 #ifdef CLIENT_SUBNET
218 verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u "
219 "subnet=%u",
220 (unsigned)slabhash_get_mem(worker->env.msg_cache),
221 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
222 (unsigned)infra_get_mem(worker->env.infra_cache),
223 (unsigned)val, (unsigned)subnet);
224 #else /* no CLIENT_SUBNET */
225 verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u",
226 (unsigned)slabhash_get_mem(worker->env.msg_cache),
227 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
228 (unsigned)infra_get_mem(worker->env.infra_cache),
229 (unsigned)val);
230 #endif /* CLIENT_SUBNET */
231 #endif /* UNBOUND_ALLOC_STATS */
232 }
233
234 void
worker_send_cmd(struct worker * worker,enum worker_commands cmd)235 worker_send_cmd(struct worker* worker, enum worker_commands cmd)
236 {
237 uint32_t c = (uint32_t)htonl(cmd);
238 if(!tube_write_msg(worker->cmd, (uint8_t*)&c, sizeof(c), 0)) {
239 log_err("worker send cmd %d failed", (int)cmd);
240 }
241 }
242
243 int
worker_handle_service_reply(struct comm_point * c,void * arg,int error,struct comm_reply * reply_info)244 worker_handle_service_reply(struct comm_point* c, void* arg, int error,
245 struct comm_reply* reply_info)
246 {
247 struct outbound_entry* e = (struct outbound_entry*)arg;
248 struct worker* worker = e->qstate->env->worker;
249 struct serviced_query *sq = e->qsent;
250
251 verbose(VERB_ALGO, "worker svcd callback for qstate %p", e->qstate);
252 if(error != 0) {
253 mesh_report_reply(worker->env.mesh, e, reply_info, error);
254 worker_mem_report(worker, sq);
255 return 0;
256 }
257 /* sanity check. */
258 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
259 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
260 LDNS_PACKET_QUERY
261 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
262 /* error becomes timeout for the module as if this reply
263 * never arrived. */
264 verbose(VERB_ALGO, "worker: bad reply handled as timeout");
265 mesh_report_reply(worker->env.mesh, e, reply_info,
266 NETEVENT_TIMEOUT);
267 worker_mem_report(worker, sq);
268 return 0;
269 }
270 mesh_report_reply(worker->env.mesh, e, reply_info, NETEVENT_NOERROR);
271 worker_mem_report(worker, sq);
272 return 0;
273 }
274
275 /** ratelimit error replies
276 * @param worker: the worker struct with ratelimit counter
277 * @param err: error code that would be wanted.
278 * @return value of err if okay, or -1 if it should be discarded instead.
279 */
280 static int
worker_err_ratelimit(struct worker * worker,int err)281 worker_err_ratelimit(struct worker* worker, int err)
282 {
283 if(worker->err_limit_time == *worker->env.now) {
284 /* see if limit is exceeded for this second */
285 if(worker->err_limit_count++ > ERROR_RATELIMIT)
286 return -1;
287 } else {
288 /* new second, new limits */
289 worker->err_limit_time = *worker->env.now;
290 worker->err_limit_count = 1;
291 }
292 return err;
293 }
294
295 /**
296 * Structure holding the result of the worker_check_request function.
297 * Based on configuration it could be called up to four times; ideally should
298 * be called once.
299 */
300 struct check_request_result {
301 int checked;
302 int value;
303 };
304 /** check request sanity.
305 * @param pkt: the wire packet to examine for sanity.
306 * @param worker: parameters for checking.
307 * @param out: struct to update with the result.
308 */
309 static void
worker_check_request(sldns_buffer * pkt,struct worker * worker,struct check_request_result * out)310 worker_check_request(sldns_buffer* pkt, struct worker* worker,
311 struct check_request_result* out)
312 {
313 if(out->checked) return;
314 out->checked = 1;
315 if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
316 verbose(VERB_QUERY, "request too short, discarded");
317 out->value = -1;
318 return;
319 }
320 if(sldns_buffer_limit(pkt) > NORMAL_UDP_SIZE &&
321 worker->daemon->cfg->harden_large_queries) {
322 verbose(VERB_QUERY, "request too large, discarded");
323 out->value = -1;
324 return;
325 }
326 if(LDNS_QR_WIRE(sldns_buffer_begin(pkt))) {
327 verbose(VERB_QUERY, "request has QR bit on, discarded");
328 out->value = -1;
329 return;
330 }
331 if(LDNS_TC_WIRE(sldns_buffer_begin(pkt))) {
332 LDNS_TC_CLR(sldns_buffer_begin(pkt));
333 verbose(VERB_QUERY, "request bad, has TC bit on");
334 out->value = worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
335 return;
336 }
337 if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY &&
338 LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_NOTIFY) {
339 verbose(VERB_QUERY, "request unknown opcode %d",
340 LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)));
341 out->value = worker_err_ratelimit(worker, LDNS_RCODE_NOTIMPL);
342 return;
343 }
344 if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1) {
345 verbose(VERB_QUERY, "request wrong nr qd=%d",
346 LDNS_QDCOUNT(sldns_buffer_begin(pkt)));
347 out->value = worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
348 return;
349 }
350 if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0 &&
351 (LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 1 ||
352 LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_NOTIFY)) {
353 verbose(VERB_QUERY, "request wrong nr an=%d",
354 LDNS_ANCOUNT(sldns_buffer_begin(pkt)));
355 out->value = worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
356 return;
357 }
358 if(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) {
359 verbose(VERB_QUERY, "request wrong nr ns=%d",
360 LDNS_NSCOUNT(sldns_buffer_begin(pkt)));
361 out->value = worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
362 return;
363 }
364 if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) {
365 verbose(VERB_QUERY, "request wrong nr ar=%d",
366 LDNS_ARCOUNT(sldns_buffer_begin(pkt)));
367 out->value = worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
368 return;
369 }
370 out->value = 0;
371 return;
372 }
373
374 void
worker_handle_control_cmd(struct tube * ATTR_UNUSED (tube),uint8_t * msg,size_t len,int error,void * arg)375 worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), uint8_t* msg,
376 size_t len, int error, void* arg)
377 {
378 struct worker* worker = (struct worker*)arg;
379 enum worker_commands cmd;
380 if(error != NETEVENT_NOERROR) {
381 free(msg);
382 if(error == NETEVENT_CLOSED)
383 comm_base_exit(worker->base);
384 else log_info("control event: %d", error);
385 return;
386 }
387 if(len != sizeof(uint32_t)) {
388 fatal_exit("bad control msg length %d", (int)len);
389 }
390 cmd = sldns_read_uint32(msg);
391 free(msg);
392 switch(cmd) {
393 case worker_cmd_quit:
394 verbose(VERB_ALGO, "got control cmd quit");
395 comm_base_exit(worker->base);
396 break;
397 case worker_cmd_stats:
398 verbose(VERB_ALGO, "got control cmd stats");
399 server_stats_reply(worker, 1);
400 break;
401 case worker_cmd_stats_noreset:
402 verbose(VERB_ALGO, "got control cmd stats_noreset");
403 server_stats_reply(worker, 0);
404 break;
405 case worker_cmd_remote:
406 verbose(VERB_ALGO, "got control cmd remote");
407 daemon_remote_exec(worker);
408 break;
409 default:
410 log_err("bad command %d", (int)cmd);
411 break;
412 }
413 }
414
415 /** check if a delegation is secure */
416 static enum sec_status
check_delegation_secure(struct reply_info * rep)417 check_delegation_secure(struct reply_info *rep)
418 {
419 /* return smallest security status */
420 size_t i;
421 enum sec_status sec = sec_status_secure;
422 enum sec_status s;
423 size_t num = rep->an_numrrsets + rep->ns_numrrsets;
424 /* check if answer and authority are OK */
425 for(i=0; i<num; i++) {
426 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
427 ->security;
428 if(s < sec)
429 sec = s;
430 }
431 /* in additional, only unchecked triggers revalidation */
432 for(i=num; i<rep->rrset_count; i++) {
433 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
434 ->security;
435 if(s == sec_status_unchecked)
436 return s;
437 }
438 return sec;
439 }
440
441 /** remove nonsecure from a delegation referral additional section */
442 static void
deleg_remove_nonsecure_additional(struct reply_info * rep)443 deleg_remove_nonsecure_additional(struct reply_info* rep)
444 {
445 /* we can simply edit it, since we are working in the scratch region */
446 size_t i;
447 enum sec_status s;
448
449 for(i = rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
450 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
451 ->security;
452 if(s != sec_status_secure) {
453 memmove(rep->rrsets+i, rep->rrsets+i+1,
454 sizeof(struct ub_packed_rrset_key*)*
455 (rep->rrset_count - i - 1));
456 rep->ar_numrrsets--;
457 rep->rrset_count--;
458 i--;
459 }
460 }
461 }
462
463 /** answer nonrecursive query from the cache */
464 static int
answer_norec_from_cache(struct worker * worker,struct query_info * qinfo,uint16_t id,uint16_t flags,struct comm_reply * repinfo,struct edns_data * edns)465 answer_norec_from_cache(struct worker* worker, struct query_info* qinfo,
466 uint16_t id, uint16_t flags, struct comm_reply* repinfo,
467 struct edns_data* edns)
468 {
469 /* for a nonrecursive query return either:
470 * o an error (servfail; we try to avoid this)
471 * o a delegation (closest we have; this routine tries that)
472 * o the answer (checked by answer_from_cache)
473 *
474 * So, grab a delegation from the rrset cache.
475 * Then check if it needs validation, if so, this routine fails,
476 * so that iterator can prime and validator can verify rrsets.
477 */
478 uint16_t udpsize = edns->udp_size;
479 int secure = 0;
480 time_t timenow = *worker->env.now;
481 int has_cd_bit = (flags&BIT_CD);
482 int must_validate = (!has_cd_bit || worker->env.cfg->ignore_cd)
483 && worker->env.need_to_validate;
484 struct dns_msg *msg = NULL;
485 struct delegpt *dp;
486
487 dp = dns_cache_find_delegation(&worker->env, qinfo->qname,
488 qinfo->qname_len, qinfo->qtype, qinfo->qclass,
489 worker->scratchpad, &msg, timenow, 0, NULL, 0);
490 if(!dp) { /* no delegation, need to reprime */
491 return 0;
492 }
493 /* In case we have a local alias, copy it into the delegation message.
494 * Shallow copy should be fine, as we'll be done with msg in this
495 * function. */
496 msg->qinfo.local_alias = qinfo->local_alias;
497 if(must_validate) {
498 switch(check_delegation_secure(msg->rep)) {
499 case sec_status_unchecked:
500 /* some rrsets have not been verified yet, go and
501 * let validator do that */
502 return 0;
503 case sec_status_bogus:
504 case sec_status_secure_sentinel_fail:
505 /* some rrsets are bogus, reply servfail */
506 edns->edns_version = EDNS_ADVERTISED_VERSION;
507 edns->udp_size = EDNS_ADVERTISED_SIZE;
508 edns->ext_rcode = 0;
509 edns->bits &= EDNS_DO;
510 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL,
511 msg->rep, LDNS_RCODE_SERVFAIL, edns, repinfo, worker->scratchpad,
512 worker->env.now_tv))
513 return 0;
514 /* Attach the cached EDE (RFC8914) */
515 if(worker->env.cfg->ede &&
516 msg->rep->reason_bogus != LDNS_EDE_NONE) {
517 edns_opt_list_append_ede(&edns->opt_list_out,
518 worker->scratchpad, msg->rep->reason_bogus,
519 msg->rep->reason_bogus_str);
520 }
521 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
522 &msg->qinfo, id, flags, edns);
523 if(worker->stats.extended) {
524 worker->stats.ans_bogus++;
525 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
526 }
527 return 1;
528 case sec_status_secure:
529 /* all rrsets are secure */
530 /* remove non-secure rrsets from the add. section*/
531 if(worker->env.cfg->val_clean_additional)
532 deleg_remove_nonsecure_additional(msg->rep);
533 secure = 1;
534 break;
535 case sec_status_indeterminate:
536 case sec_status_insecure:
537 default:
538 /* not secure */
539 secure = 0;
540 break;
541 }
542 }
543 /* return this delegation from the cache */
544 edns->edns_version = EDNS_ADVERTISED_VERSION;
545 edns->udp_size = EDNS_ADVERTISED_SIZE;
546 edns->ext_rcode = 0;
547 edns->bits &= EDNS_DO;
548 if(worker->env.cfg->disable_edns_do && (edns->bits & EDNS_DO))
549 edns->edns_present = 0;
550 if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, msg->rep,
551 (int)(flags&LDNS_RCODE_MASK), edns, repinfo, worker->scratchpad,
552 worker->env.now_tv))
553 return 0;
554 msg->rep->flags |= BIT_QR|BIT_RA;
555 /* Attach the cached EDE (RFC8914) if CD bit is set and the answer is
556 * bogus. */
557 if(worker->env.cfg->ede && has_cd_bit &&
558 (check_delegation_secure(msg->rep) == sec_status_bogus ||
559 check_delegation_secure(msg->rep) == sec_status_secure_sentinel_fail) &&
560 msg->rep->reason_bogus != LDNS_EDE_NONE) {
561 edns_opt_list_append_ede(&edns->opt_list_out,
562 worker->scratchpad, msg->rep->reason_bogus,
563 msg->rep->reason_bogus_str);
564 }
565 if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags,
566 repinfo->c->buffer, 0, 1, worker->scratchpad,
567 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
568 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
569 LDNS_RCODE_SERVFAIL, edns, repinfo, worker->scratchpad,
570 worker->env.now_tv))
571 edns->opt_list_inplace_cb_out = NULL;
572 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
573 &msg->qinfo, id, flags, edns);
574 }
575 if(worker->stats.extended) {
576 if(secure) worker->stats.ans_secure++;
577 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
578 }
579 return 1;
580 }
581
582 /** Apply, if applicable, a response IP action to a cached answer.
583 * If the answer is rewritten as a result of an action, '*encode_repp' will
584 * point to the reply info containing the modified answer. '*encode_repp' will
585 * be intact otherwise.
586 * It returns 1 on success, 0 otherwise. */
587 static int
apply_respip_action(struct worker * worker,const struct query_info * qinfo,struct respip_client_info * cinfo,struct reply_info * rep,struct sockaddr_storage * addr,socklen_t addrlen,struct ub_packed_rrset_key ** alias_rrset,struct reply_info ** encode_repp,struct auth_zones * az)588 apply_respip_action(struct worker* worker, const struct query_info* qinfo,
589 struct respip_client_info* cinfo, struct reply_info* rep,
590 struct sockaddr_storage* addr, socklen_t addrlen,
591 struct ub_packed_rrset_key** alias_rrset,
592 struct reply_info** encode_repp, struct auth_zones* az)
593 {
594 struct respip_action_info actinfo = {0, 0, 0, 0, NULL, 0, NULL};
595 actinfo.action = respip_none;
596
597 if(qinfo->qtype != LDNS_RR_TYPE_A &&
598 qinfo->qtype != LDNS_RR_TYPE_AAAA &&
599 qinfo->qtype != LDNS_RR_TYPE_ANY)
600 return 1;
601
602 if(!respip_rewrite_reply(qinfo, cinfo, rep, encode_repp, &actinfo,
603 alias_rrset, 0, worker->scratchpad, az, NULL))
604 return 0;
605
606 /* xxx_deny actions mean dropping the reply, unless the original reply
607 * was redirected to response-ip data. */
608 if(actinfo.action == respip_always_deny ||
609 ((actinfo.action == respip_deny ||
610 actinfo.action == respip_inform_deny) &&
611 *encode_repp == rep))
612 *encode_repp = NULL;
613
614 /* If address info is returned, it means the action should be an
615 * 'inform' variant and the information should be logged. */
616 if(actinfo.addrinfo) {
617 respip_inform_print(&actinfo, qinfo->qname,
618 qinfo->qtype, qinfo->qclass, qinfo->local_alias,
619 addr, addrlen);
620
621 if(worker->stats.extended && actinfo.rpz_used) {
622 if(actinfo.rpz_disabled)
623 worker->stats.rpz_action[RPZ_DISABLED_ACTION]++;
624 if(actinfo.rpz_cname_override)
625 worker->stats.rpz_action[RPZ_CNAME_OVERRIDE_ACTION]++;
626 else
627 worker->stats.rpz_action[
628 respip_action_to_rpz_action(actinfo.action)]++;
629 }
630 }
631
632 return 1;
633 }
634
635 /** answer query from the cache.
636 * Normally, the answer message will be built in repinfo->c->buffer; if the
637 * answer is supposed to be suppressed or the answer is supposed to be an
638 * incomplete CNAME chain, the buffer is explicitly cleared to signal the
639 * caller as such. In the latter case *partial_rep will point to the incomplete
640 * reply, and this function is (possibly) supposed to be called again with that
641 * *partial_rep value to complete the chain. In addition, if the query should
642 * be completely dropped, '*need_drop' will be set to 1. */
643 static int
answer_from_cache(struct worker * worker,struct query_info * qinfo,struct respip_client_info * cinfo,int * need_drop,int * is_expired_answer,int * is_secure_answer,struct ub_packed_rrset_key ** alias_rrset,struct reply_info ** partial_repp,struct reply_info * rep,uint16_t id,uint16_t flags,struct comm_reply * repinfo,struct edns_data * edns)644 answer_from_cache(struct worker* worker, struct query_info* qinfo,
645 struct respip_client_info* cinfo, int* need_drop, int* is_expired_answer,
646 int* is_secure_answer, struct ub_packed_rrset_key** alias_rrset,
647 struct reply_info** partial_repp,
648 struct reply_info* rep, uint16_t id, uint16_t flags,
649 struct comm_reply* repinfo, struct edns_data* edns)
650 {
651 time_t timenow = *worker->env.now;
652 uint16_t udpsize = edns->udp_size;
653 struct reply_info* encode_rep = rep;
654 struct reply_info* partial_rep = *partial_repp;
655 int has_cd_bit = (flags&BIT_CD);
656 int must_validate = (!has_cd_bit || worker->env.cfg->ignore_cd)
657 && worker->env.need_to_validate;
658 *partial_repp = NULL; /* avoid accidental further pass */
659
660 /* Check TTL */
661 if(rep->ttl < timenow) {
662 /* Check if we need to serve expired now */
663 if(worker->env.cfg->serve_expired &&
664 /* if serve-expired-client-timeout is set, serve
665 * an expired record without attempting recursion
666 * if the serve_expired_norec_ttl is set for the record
667 * as we know that recursion is currently failing. */
668 (!worker->env.cfg->serve_expired_client_timeout ||
669 timenow < rep->serve_expired_norec_ttl)
670 #ifdef USE_CACHEDB
671 && !(worker->env.cachedb_enabled &&
672 worker->env.cfg->cachedb_check_when_serve_expired)
673 #endif
674 ) {
675 if(!reply_info_can_answer_expired(rep, timenow))
676 return 0;
677 if(!rrset_array_lock(rep->ref, rep->rrset_count, 0))
678 return 0;
679 *is_expired_answer = 1;
680 } else {
681 /* the rrsets may have been updated in the meantime.
682 * we will refetch the message format from the
683 * authoritative server
684 */
685 return 0;
686 }
687 } else {
688 if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
689 return 0;
690 }
691 /* locked and ids and ttls are OK. */
692
693 /* check CNAME chain (if any) */
694 if(rep->an_numrrsets > 0 && (rep->rrsets[0]->rk.type ==
695 htons(LDNS_RR_TYPE_CNAME) || rep->rrsets[0]->rk.type ==
696 htons(LDNS_RR_TYPE_DNAME))) {
697 if(!reply_check_cname_chain(qinfo, rep)) {
698 /* cname chain invalid, redo iterator steps */
699 verbose(VERB_ALGO, "Cache reply: cname chain broken");
700 goto bail_out;
701 }
702 }
703 /* check security status of the cached answer */
704 if(must_validate && (rep->security == sec_status_bogus ||
705 rep->security == sec_status_secure_sentinel_fail)) {
706 /* BAD cached */
707 edns->edns_version = EDNS_ADVERTISED_VERSION;
708 edns->udp_size = EDNS_ADVERTISED_SIZE;
709 edns->ext_rcode = 0;
710 edns->bits &= EDNS_DO;
711 if(worker->env.cfg->disable_edns_do && (edns->bits & EDNS_DO))
712 edns->edns_present = 0;
713 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, rep,
714 LDNS_RCODE_SERVFAIL, edns, repinfo, worker->scratchpad,
715 worker->env.now_tv))
716 goto bail_out;
717 /* Attach the cached EDE (RFC8914) */
718 if(worker->env.cfg->ede && rep->reason_bogus != LDNS_EDE_NONE) {
719 edns_opt_list_append_ede(&edns->opt_list_out,
720 worker->scratchpad, rep->reason_bogus,
721 rep->reason_bogus_str);
722 }
723 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
724 qinfo, id, flags, edns);
725 rrset_array_unlock_touch(worker->env.rrset_cache,
726 worker->scratchpad, rep->ref, rep->rrset_count);
727 if(worker->stats.extended) {
728 worker->stats.ans_bogus ++;
729 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL] ++;
730 }
731 return 1;
732 } else if(rep->security == sec_status_unchecked && must_validate) {
733 verbose(VERB_ALGO, "Cache reply: unchecked entry needs "
734 "validation");
735 goto bail_out; /* need to validate cache entry first */
736 } else if(rep->security == sec_status_secure) {
737 if(reply_all_rrsets_secure(rep)) {
738 *is_secure_answer = 1;
739 } else {
740 if(must_validate) {
741 verbose(VERB_ALGO, "Cache reply: secure entry"
742 " changed status");
743 goto bail_out; /* rrset changed, re-verify */
744 }
745 *is_secure_answer = 0;
746 }
747 } else *is_secure_answer = 0;
748
749 edns->edns_version = EDNS_ADVERTISED_VERSION;
750 edns->udp_size = EDNS_ADVERTISED_SIZE;
751 edns->ext_rcode = 0;
752 edns->bits &= EDNS_DO;
753 if(worker->env.cfg->disable_edns_do && (edns->bits & EDNS_DO))
754 edns->edns_present = 0;
755 *alias_rrset = NULL; /* avoid confusion if caller set it to non-NULL */
756 if((worker->daemon->use_response_ip || worker->daemon->use_rpz) &&
757 !partial_rep && !apply_respip_action(worker, qinfo, cinfo, rep,
758 &repinfo->client_addr, repinfo->client_addrlen, alias_rrset,
759 &encode_rep, worker->env.auth_zones)) {
760 goto bail_out;
761 } else if(partial_rep &&
762 !respip_merge_cname(partial_rep, qinfo, rep, cinfo,
763 must_validate, &encode_rep, worker->scratchpad,
764 worker->env.auth_zones)) {
765 goto bail_out;
766 }
767 if(encode_rep != rep) {
768 /* if rewritten, it can't be considered "secure" */
769 *is_secure_answer = 0;
770 }
771 if(!encode_rep || *alias_rrset) {
772 if(!encode_rep)
773 *need_drop = 1;
774 else {
775 /* If a partial CNAME chain is found, we first need to
776 * make a copy of the reply in the scratchpad so we
777 * can release the locks and lookup the cache again. */
778 *partial_repp = reply_info_copy(encode_rep, NULL,
779 worker->scratchpad);
780 if(!*partial_repp)
781 goto bail_out;
782 }
783 } else {
784 if(*is_expired_answer == 1 &&
785 worker->env.cfg->ede_serve_expired && worker->env.cfg->ede) {
786 EDNS_OPT_LIST_APPEND_EDE(&edns->opt_list_out,
787 worker->scratchpad, LDNS_EDE_STALE_ANSWER, "");
788 }
789 /* Attach the cached EDE (RFC8914) if CD bit is set and the
790 * answer is bogus. */
791 if(*is_secure_answer == 0 &&
792 worker->env.cfg->ede && has_cd_bit &&
793 encode_rep->reason_bogus != LDNS_EDE_NONE) {
794 edns_opt_list_append_ede(&edns->opt_list_out,
795 worker->scratchpad, encode_rep->reason_bogus,
796 encode_rep->reason_bogus_str);
797 }
798 if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, encode_rep,
799 (int)(flags&LDNS_RCODE_MASK), edns, repinfo, worker->scratchpad,
800 worker->env.now_tv))
801 goto bail_out;
802 if(!reply_info_answer_encode(qinfo, encode_rep, id, flags,
803 repinfo->c->buffer, timenow, 1, worker->scratchpad,
804 udpsize, edns, (int)(edns->bits & EDNS_DO),
805 *is_secure_answer)) {
806 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo,
807 NULL, NULL, LDNS_RCODE_SERVFAIL, edns, repinfo,
808 worker->scratchpad, worker->env.now_tv))
809 edns->opt_list_inplace_cb_out = NULL;
810 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
811 qinfo, id, flags, edns);
812 }
813 }
814 /* cannot send the reply right now, because blocking network syscall
815 * is bad while holding locks. */
816 rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
817 rep->ref, rep->rrset_count);
818 /* go and return this buffer to the client */
819 return 1;
820
821 bail_out:
822 rrset_array_unlock_touch(worker->env.rrset_cache,
823 worker->scratchpad, rep->ref, rep->rrset_count);
824 return 0;
825 }
826
827 /** Reply to client and perform prefetch to keep cache up to date. */
828 static void
reply_and_prefetch(struct worker * worker,struct query_info * qinfo,uint16_t flags,struct comm_reply * repinfo,time_t leeway,int noreply,int rpz_passthru,struct edns_option * opt_list)829 reply_and_prefetch(struct worker* worker, struct query_info* qinfo,
830 uint16_t flags, struct comm_reply* repinfo, time_t leeway, int noreply,
831 int rpz_passthru, struct edns_option* opt_list)
832 {
833 (void)opt_list;
834 /* first send answer to client to keep its latency
835 * as small as a cachereply */
836 if(!noreply) {
837 if(repinfo->c->tcp_req_info) {
838 sldns_buffer_copy(
839 repinfo->c->tcp_req_info->spool_buffer,
840 repinfo->c->buffer);
841 }
842 comm_point_send_reply(repinfo);
843 }
844 server_stats_prefetch(&worker->stats, worker);
845 #ifdef CLIENT_SUBNET
846 /* Check if the subnet module is enabled. In that case pass over the
847 * comm_reply information for ECS generation later. The mesh states are
848 * unique when subnet is enabled. */
849 if(modstack_find(&worker->env.mesh->mods, "subnetcache") != -1
850 && worker->env.unique_mesh) {
851 mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
852 PREFETCH_EXPIRY_ADD, rpz_passthru,
853 &repinfo->client_addr, opt_list);
854 return;
855 }
856 #endif
857 /* create the prefetch in the mesh as a normal lookup without
858 * client addrs waiting, which has the cache blacklisted (to bypass
859 * the cache and go to the network for the data). */
860 /* this (potentially) runs the mesh for the new query */
861 mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
862 PREFETCH_EXPIRY_ADD, rpz_passthru, NULL, NULL);
863 }
864
865 /**
866 * Fill CH class answer into buffer. Keeps query.
867 * @param pkt: buffer
868 * @param str: string to put into text record (<255).
869 * array of strings, every string becomes a text record.
870 * @param num: number of strings in array.
871 * @param edns: edns reply information.
872 * @param worker: worker with scratch region.
873 * @param repinfo: reply information for a communication point.
874 */
875 static void
chaos_replystr(sldns_buffer * pkt,char ** str,int num,struct edns_data * edns,struct worker * worker,struct comm_reply * repinfo)876 chaos_replystr(sldns_buffer* pkt, char** str, int num, struct edns_data* edns,
877 struct worker* worker, struct comm_reply* repinfo)
878 {
879 int i;
880 unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
881 unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
882 size_t udpsize = edns->udp_size;
883 edns->edns_version = EDNS_ADVERTISED_VERSION;
884 edns->udp_size = EDNS_ADVERTISED_SIZE;
885 edns->bits &= EDNS_DO;
886 if(!inplace_cb_reply_local_call(&worker->env, NULL, NULL, NULL,
887 LDNS_RCODE_NOERROR, edns, repinfo, worker->scratchpad,
888 worker->env.now_tv))
889 edns->opt_list_inplace_cb_out = NULL;
890 sldns_buffer_clear(pkt);
891 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
892 sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
893 if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
894 if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
895 sldns_buffer_write_u16(pkt, 1); /* qdcount */
896 sldns_buffer_write_u16(pkt, (uint16_t)num); /* ancount */
897 sldns_buffer_write_u16(pkt, 0); /* nscount */
898 sldns_buffer_write_u16(pkt, 0); /* arcount */
899 (void)query_dname_len(pkt); /* skip qname */
900 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
901 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
902 for(i=0; i<num; i++) {
903 size_t len = strlen(str[i]);
904 if(len>255) len=255; /* cap size of TXT record */
905 if(sldns_buffer_position(pkt)+2+2+2+4+2+1+len+
906 calc_edns_field_size(edns) > udpsize) {
907 sldns_buffer_write_u16_at(pkt, 6, i); /* ANCOUNT */
908 LDNS_TC_SET(sldns_buffer_begin(pkt));
909 break;
910 }
911 sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
912 sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
913 sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
914 sldns_buffer_write_u32(pkt, 0); /* TTL */
915 sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
916 sldns_buffer_write_u8(pkt, len);
917 sldns_buffer_write(pkt, str[i], len);
918 }
919 sldns_buffer_flip(pkt);
920 if(sldns_buffer_capacity(pkt) >=
921 sldns_buffer_limit(pkt)+calc_edns_field_size(edns))
922 attach_edns_record(pkt, edns);
923 }
924
925 /** Reply with one string */
926 static void
chaos_replyonestr(sldns_buffer * pkt,const char * str,struct edns_data * edns,struct worker * worker,struct comm_reply * repinfo)927 chaos_replyonestr(sldns_buffer* pkt, const char* str, struct edns_data* edns,
928 struct worker* worker, struct comm_reply* repinfo)
929 {
930 chaos_replystr(pkt, (char**)&str, 1, edns, worker, repinfo);
931 }
932
933 /**
934 * Create CH class trustanchor answer.
935 * @param pkt: buffer
936 * @param edns: edns reply information.
937 * @param w: worker with scratch region.
938 * @param repinfo: reply information for a communication point.
939 */
940 static void
chaos_trustanchor(sldns_buffer * pkt,struct edns_data * edns,struct worker * w,struct comm_reply * repinfo)941 chaos_trustanchor(sldns_buffer* pkt, struct edns_data* edns, struct worker* w,
942 struct comm_reply* repinfo)
943 {
944 #define TA_RESPONSE_MAX_TXT 16 /* max number of TXT records */
945 #define TA_RESPONSE_MAX_TAGS 32 /* max number of tags printed per zone */
946 char* str_array[TA_RESPONSE_MAX_TXT];
947 uint16_t tags[TA_RESPONSE_MAX_TAGS];
948 int num = 0;
949 struct trust_anchor* ta;
950
951 if(!w->env.need_to_validate) {
952 /* no validator module, reply no trustanchors */
953 chaos_replystr(pkt, NULL, 0, edns, w, repinfo);
954 return;
955 }
956
957 /* fill the string with contents */
958 lock_basic_lock(&w->env.anchors->lock);
959 RBTREE_FOR(ta, struct trust_anchor*, w->env.anchors->tree) {
960 char* str;
961 size_t i, numtag, str_len = 255;
962 if(num == TA_RESPONSE_MAX_TXT) continue;
963 str = (char*)regional_alloc(w->scratchpad, str_len);
964 if(!str) continue;
965 lock_basic_lock(&ta->lock);
966 numtag = anchor_list_keytags(ta, tags, TA_RESPONSE_MAX_TAGS);
967 if(numtag == 0) {
968 /* empty, insecure point */
969 lock_basic_unlock(&ta->lock);
970 continue;
971 }
972 str_array[num] = str;
973 num++;
974
975 /* spool name of anchor */
976 (void)sldns_wire2str_dname_buf(ta->name, ta->namelen, str, str_len);
977 str_len -= strlen(str); str += strlen(str);
978 /* spool tags */
979 for(i=0; i<numtag; i++) {
980 snprintf(str, str_len, " %u", (unsigned)tags[i]);
981 str_len -= strlen(str); str += strlen(str);
982 }
983 lock_basic_unlock(&ta->lock);
984 }
985 lock_basic_unlock(&w->env.anchors->lock);
986
987 chaos_replystr(pkt, str_array, num, edns, w, repinfo);
988 regional_free_all(w->scratchpad);
989 }
990
991 /**
992 * Answer CH class queries.
993 * @param w: worker
994 * @param qinfo: query info. Pointer into packet buffer.
995 * @param edns: edns info from query.
996 * @param repinfo: reply information for a communication point.
997 * @param pkt: packet buffer.
998 * @return: true if a reply is to be sent.
999 */
1000 static int
answer_chaos(struct worker * w,struct query_info * qinfo,struct edns_data * edns,struct comm_reply * repinfo,sldns_buffer * pkt)1001 answer_chaos(struct worker* w, struct query_info* qinfo,
1002 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* pkt)
1003 {
1004 struct config_file* cfg = w->env.cfg;
1005 if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
1006 return 0;
1007 if(query_dname_compare(qinfo->qname,
1008 (uint8_t*)"\002id\006server") == 0 ||
1009 query_dname_compare(qinfo->qname,
1010 (uint8_t*)"\010hostname\004bind") == 0)
1011 {
1012 if(cfg->hide_identity)
1013 return 0;
1014 if(cfg->identity==NULL || cfg->identity[0]==0) {
1015 char buf[MAXHOSTNAMELEN+1];
1016 if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
1017 buf[MAXHOSTNAMELEN] = 0;
1018 chaos_replyonestr(pkt, buf, edns, w, repinfo);
1019 } else {
1020 log_err("gethostname: %s", strerror(errno));
1021 chaos_replyonestr(pkt, "no hostname", edns, w, repinfo);
1022 }
1023 }
1024 else chaos_replyonestr(pkt, cfg->identity, edns, w, repinfo);
1025 return 1;
1026 }
1027 if(query_dname_compare(qinfo->qname,
1028 (uint8_t*)"\007version\006server") == 0 ||
1029 query_dname_compare(qinfo->qname,
1030 (uint8_t*)"\007version\004bind") == 0)
1031 {
1032 if(cfg->hide_version)
1033 return 0;
1034 if(cfg->version==NULL || cfg->version[0]==0)
1035 chaos_replyonestr(pkt, PACKAGE_STRING, edns, w, repinfo);
1036 else chaos_replyonestr(pkt, cfg->version, edns, w, repinfo);
1037 return 1;
1038 }
1039 if(query_dname_compare(qinfo->qname,
1040 (uint8_t*)"\013trustanchor\007unbound") == 0)
1041 {
1042 if(cfg->hide_trustanchor)
1043 return 0;
1044 chaos_trustanchor(pkt, edns, w, repinfo);
1045 return 1;
1046 }
1047
1048 return 0;
1049 }
1050
1051 /**
1052 * Answer notify queries. These are notifies for authoritative zones,
1053 * the reply is an ack that the notify has been received. We need to check
1054 * access permission here.
1055 * @param w: worker
1056 * @param qinfo: query info. Pointer into packet buffer.
1057 * @param edns: edns info from query.
1058 * @param addr: client address.
1059 * @param addrlen: client address length.
1060 * @param pkt: packet buffer.
1061 */
1062 static void
answer_notify(struct worker * w,struct query_info * qinfo,struct edns_data * edns,sldns_buffer * pkt,struct sockaddr_storage * addr,socklen_t addrlen)1063 answer_notify(struct worker* w, struct query_info* qinfo,
1064 struct edns_data* edns, sldns_buffer* pkt,
1065 struct sockaddr_storage* addr, socklen_t addrlen)
1066 {
1067 int refused = 0;
1068 int rcode = LDNS_RCODE_NOERROR;
1069 uint32_t serial = 0;
1070 int has_serial;
1071 if(!w->env.auth_zones) return;
1072 has_serial = auth_zone_parse_notify_serial(pkt, &serial);
1073 if(auth_zones_notify(w->env.auth_zones, &w->env, qinfo->qname,
1074 qinfo->qname_len, qinfo->qclass, addr,
1075 addrlen, has_serial, serial, &refused)) {
1076 rcode = LDNS_RCODE_NOERROR;
1077 } else {
1078 if(refused)
1079 rcode = LDNS_RCODE_REFUSED;
1080 else rcode = LDNS_RCODE_SERVFAIL;
1081 }
1082
1083 if(verbosity >= VERB_DETAIL) {
1084 char buf[380];
1085 char zname[255+1];
1086 char sr[25];
1087 dname_str(qinfo->qname, zname);
1088 sr[0]=0;
1089 if(has_serial)
1090 snprintf(sr, sizeof(sr), "serial %u ",
1091 (unsigned)serial);
1092 if(rcode == LDNS_RCODE_REFUSED)
1093 snprintf(buf, sizeof(buf),
1094 "refused NOTIFY %sfor %s from", sr, zname);
1095 else if(rcode == LDNS_RCODE_SERVFAIL)
1096 snprintf(buf, sizeof(buf),
1097 "servfail for NOTIFY %sfor %s from", sr, zname);
1098 else snprintf(buf, sizeof(buf),
1099 "received NOTIFY %sfor %s from", sr, zname);
1100 log_addr(VERB_DETAIL, buf, addr, addrlen);
1101 }
1102 edns->edns_version = EDNS_ADVERTISED_VERSION;
1103 edns->udp_size = EDNS_ADVERTISED_SIZE;
1104 edns->ext_rcode = 0;
1105 edns->bits &= EDNS_DO;
1106 error_encode(pkt, rcode, qinfo,
1107 *(uint16_t*)(void *)sldns_buffer_begin(pkt),
1108 sldns_buffer_read_u16_at(pkt, 2), edns);
1109 LDNS_OPCODE_SET(sldns_buffer_begin(pkt), LDNS_PACKET_NOTIFY);
1110 }
1111
1112 static int
deny_refuse(struct comm_point * c,enum acl_access acl,enum acl_access deny,enum acl_access refuse,struct worker * worker,struct comm_reply * repinfo,struct acl_addr * acladdr,int ede,struct check_request_result * check_result)1113 deny_refuse(struct comm_point* c, enum acl_access acl,
1114 enum acl_access deny, enum acl_access refuse,
1115 struct worker* worker, struct comm_reply* repinfo,
1116 struct acl_addr* acladdr, int ede,
1117 struct check_request_result* check_result)
1118 {
1119 if(acl == deny) {
1120 if(verbosity >= VERB_ALGO) {
1121 log_acl_action("dropped", &repinfo->client_addr,
1122 repinfo->client_addrlen, acl, acladdr);
1123 log_buf(VERB_ALGO, "dropped", c->buffer);
1124 }
1125 comm_point_drop_reply(repinfo);
1126 if(worker->stats.extended)
1127 worker->stats.unwanted_queries++;
1128 return 0;
1129 } else if(acl == refuse) {
1130 size_t opt_rr_mark;
1131
1132 if(verbosity >= VERB_ALGO) {
1133 log_acl_action("refused", &repinfo->client_addr,
1134 repinfo->client_addrlen, acl, acladdr);
1135 log_buf(VERB_ALGO, "refuse", c->buffer);
1136 }
1137
1138 if(worker->stats.extended)
1139 worker->stats.unwanted_queries++;
1140 worker_check_request(c->buffer, worker, check_result);
1141 if(check_result->value != 0) {
1142 if(check_result->value != -1) {
1143 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1144 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1145 check_result->value);
1146 return 1;
1147 }
1148 comm_point_drop_reply(repinfo);
1149 return 0;
1150 }
1151 /* worker_check_request() above guarantees that the buffer contains at
1152 * least a header and that qdcount == 1
1153 */
1154 log_assert(sldns_buffer_limit(c->buffer) >= LDNS_HEADER_SIZE
1155 && LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) == 1);
1156
1157 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE); /* skip header */
1158
1159 /* check additional section is present and that we respond with EDEs */
1160 if(LDNS_ARCOUNT(sldns_buffer_begin(c->buffer)) != 1
1161 || !ede) {
1162 LDNS_QDCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1163 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1164 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1165 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1166 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1167 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1168 LDNS_RCODE_REFUSED);
1169 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1170 sldns_buffer_flip(c->buffer);
1171 return 1;
1172 }
1173
1174 if (!query_dname_len(c->buffer)) {
1175 LDNS_QDCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1176 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1177 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1178 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1179 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1180 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1181 LDNS_RCODE_FORMERR);
1182 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1183 sldns_buffer_flip(c->buffer);
1184 return 1;
1185 }
1186 /* space available for query type and class? */
1187 if (sldns_buffer_remaining(c->buffer) < 2 * sizeof(uint16_t)) {
1188 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1189 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1190 LDNS_RCODE_FORMERR);
1191 LDNS_QDCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1192 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1193 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1194 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1195 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1196 sldns_buffer_flip(c->buffer);
1197 return 1;
1198 }
1199 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1200 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1201 LDNS_RCODE_REFUSED);
1202
1203 sldns_buffer_skip(c->buffer, (ssize_t)sizeof(uint16_t)); /* skip qtype */
1204
1205 sldns_buffer_skip(c->buffer, (ssize_t)sizeof(uint16_t)); /* skip qclass */
1206
1207 /* The OPT RR to be returned should come directly after
1208 * the query, so mark this spot.
1209 */
1210 opt_rr_mark = sldns_buffer_position(c->buffer);
1211
1212 /* Skip through the RR records */
1213 if(LDNS_ANCOUNT(sldns_buffer_begin(c->buffer)) != 0 ||
1214 LDNS_NSCOUNT(sldns_buffer_begin(c->buffer)) != 0) {
1215 if(!skip_pkt_rrs(c->buffer,
1216 ((int)LDNS_ANCOUNT(sldns_buffer_begin(c->buffer)))+
1217 ((int)LDNS_NSCOUNT(sldns_buffer_begin(c->buffer))))) {
1218 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1219 LDNS_RCODE_FORMERR);
1220 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1221 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1222 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1223 sldns_buffer_set_position(c->buffer, opt_rr_mark);
1224 sldns_buffer_flip(c->buffer);
1225 return 1;
1226 }
1227 }
1228 /* Do we have a valid OPT RR here? If not return REFUSED (could be a valid TSIG or something so no FORMERR) */
1229 /* domain name must be the root of length 1. */
1230 if(sldns_buffer_remaining(c->buffer) < 1 || *sldns_buffer_current(c->buffer) != 0) {
1231 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1232 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1233 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1234 sldns_buffer_set_position(c->buffer, opt_rr_mark);
1235 sldns_buffer_flip(c->buffer);
1236 return 1;
1237 } else {
1238 sldns_buffer_skip(c->buffer, 1); /* skip root label */
1239 }
1240 if(sldns_buffer_remaining(c->buffer) < 2 ||
1241 sldns_buffer_read_u16(c->buffer) != LDNS_RR_TYPE_OPT) {
1242 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1243 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1244 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1245 sldns_buffer_set_position(c->buffer, opt_rr_mark);
1246 sldns_buffer_flip(c->buffer);
1247 return 1;
1248 }
1249 /* Write OPT RR directly after the query,
1250 * so without the (possibly skipped) Answer and NS RRs
1251 */
1252 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1253 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1254 sldns_buffer_clear(c->buffer); /* reset write limit */
1255 sldns_buffer_set_position(c->buffer, opt_rr_mark);
1256
1257 /* Check if OPT record can be written
1258 * 17 == root label (1) + RR type (2) + UDP Size (2)
1259 * + Fields (4) + rdata len (2) + EDE Option code (2)
1260 * + EDE Option length (2) + EDE info-code (2)
1261 */
1262 if (sldns_buffer_available(c->buffer, 17) == 0) {
1263 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0);
1264 sldns_buffer_flip(c->buffer);
1265 return 1;
1266 }
1267
1268 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 1);
1269
1270 /* root label */
1271 sldns_buffer_write_u8(c->buffer, 0);
1272 sldns_buffer_write_u16(c->buffer, LDNS_RR_TYPE_OPT);
1273 sldns_buffer_write_u16(c->buffer, EDNS_ADVERTISED_SIZE);
1274
1275 /* write OPT Record TTL Field */
1276 sldns_buffer_write_u32(c->buffer, 0);
1277
1278 /* write rdata len: EDE option + length + info-code */
1279 sldns_buffer_write_u16(c->buffer, 6);
1280
1281 /* write OPTIONS; add EDE option code */
1282 sldns_buffer_write_u16(c->buffer, LDNS_EDNS_EDE);
1283
1284 /* write single EDE option length (for just 1 info-code) */
1285 sldns_buffer_write_u16(c->buffer, 2);
1286
1287 /* write single EDE info-code */
1288 sldns_buffer_write_u16(c->buffer, LDNS_EDE_PROHIBITED);
1289
1290 sldns_buffer_flip(c->buffer);
1291
1292 verbose(VERB_ALGO, "attached EDE code: %d", LDNS_EDE_PROHIBITED);
1293
1294 return 1;
1295
1296 }
1297
1298 return -1;
1299 }
1300
1301 static int
deny_refuse_all(struct comm_point * c,enum acl_access * acl,struct worker * worker,struct comm_reply * repinfo,struct acl_addr ** acladdr,int ede,int check_proxy,struct check_request_result * check_result)1302 deny_refuse_all(struct comm_point* c, enum acl_access* acl,
1303 struct worker* worker, struct comm_reply* repinfo,
1304 struct acl_addr** acladdr, int ede, int check_proxy,
1305 struct check_request_result* check_result)
1306 {
1307 if(check_proxy) {
1308 *acladdr = acl_addr_lookup(worker->daemon->acl,
1309 &repinfo->remote_addr, repinfo->remote_addrlen);
1310 } else {
1311 *acladdr = acl_addr_lookup(worker->daemon->acl,
1312 &repinfo->client_addr, repinfo->client_addrlen);
1313 }
1314 /* If there is no ACL based on client IP use the interface ACL. */
1315 if(!(*acladdr) && c->socket) {
1316 *acladdr = c->socket->acl;
1317 }
1318 *acl = acl_get_control(*acladdr);
1319 return deny_refuse(c, *acl, acl_deny, acl_refuse, worker, repinfo,
1320 *acladdr, ede, check_result);
1321 }
1322
1323 static int
deny_refuse_non_local(struct comm_point * c,enum acl_access acl,struct worker * worker,struct comm_reply * repinfo,struct acl_addr * acladdr,int ede,struct check_request_result * check_result)1324 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
1325 struct worker* worker, struct comm_reply* repinfo,
1326 struct acl_addr* acladdr, int ede,
1327 struct check_request_result* check_result)
1328 {
1329 return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local,
1330 worker, repinfo, acladdr, ede, check_result);
1331 }
1332
1333 /* Check if the query is blocked by source IP rate limiting.
1334 * Returns 1 if it passes the check, 0 otherwise. */
1335 static int
check_ip_ratelimit(struct worker * worker,struct sockaddr_storage * addr,socklen_t addrlen,int has_cookie,sldns_buffer * pkt)1336 check_ip_ratelimit(struct worker* worker, struct sockaddr_storage* addr,
1337 socklen_t addrlen, int has_cookie, sldns_buffer* pkt)
1338 {
1339 if(!infra_ip_ratelimit_inc(worker->env.infra_cache, addr, addrlen,
1340 *worker->env.now, has_cookie,
1341 worker->env.cfg->ip_ratelimit_backoff, pkt)) {
1342 /* See if we can pass through with slip factor */
1343 if(!has_cookie && worker->env.cfg->ip_ratelimit_factor != 0 &&
1344 ub_random_max(worker->env.rnd,
1345 worker->env.cfg->ip_ratelimit_factor) == 0) {
1346 char addrbuf[128];
1347 addr_to_str(addr, addrlen, addrbuf, sizeof(addrbuf));
1348 verbose(VERB_QUERY, "ip_ratelimit allowed through for "
1349 "ip address %s because of slip in "
1350 "ip_ratelimit_factor", addrbuf);
1351 return 1;
1352 }
1353 return 0;
1354 }
1355 return 1;
1356 }
1357
1358 int
worker_handle_request(struct comm_point * c,void * arg,int error,struct comm_reply * repinfo)1359 worker_handle_request(struct comm_point* c, void* arg, int error,
1360 struct comm_reply* repinfo)
1361 {
1362 struct worker* worker = (struct worker*)arg;
1363 int ret;
1364 hashvalue_type h;
1365 struct lruhash_entry* e;
1366 struct query_info qinfo;
1367 struct edns_data edns;
1368 struct edns_option* original_edns_list = NULL;
1369 enum acl_access acl;
1370 struct acl_addr* acladdr;
1371 int pre_edns_ip_ratelimit = 1;
1372 int rc = 0;
1373 int need_drop = 0;
1374 int is_expired_answer = 0;
1375 int is_secure_answer = 0;
1376 int rpz_passthru = 0;
1377 long long wait_queue_time = 0;
1378 /* We might have to chase a CNAME chain internally, in which case
1379 * we'll have up to two replies and combine them to build a complete
1380 * answer. These variables control this case. */
1381 struct ub_packed_rrset_key* alias_rrset = NULL;
1382 struct reply_info* partial_rep = NULL;
1383 struct query_info* lookup_qinfo = &qinfo;
1384 struct query_info qinfo_tmp; /* placeholder for lookup_qinfo */
1385 struct respip_client_info* cinfo = NULL, cinfo_tmp;
1386 struct timeval wait_time;
1387 struct check_request_result check_result = {0,0};
1388 memset(&qinfo, 0, sizeof(qinfo));
1389
1390 if((error != NETEVENT_NOERROR && error != NETEVENT_DONE)|| !repinfo) {
1391 /* some bad tcp query DNS formats give these error calls */
1392 verbose(VERB_ALGO, "handle request called with err=%d", error);
1393 return 0;
1394 }
1395
1396 if (worker->env.cfg->sock_queue_timeout && timeval_isset(&c->recv_tv)) {
1397 timeval_subtract(&wait_time, worker->env.now_tv, &c->recv_tv);
1398 wait_queue_time = wait_time.tv_sec * 1000000 + wait_time.tv_usec;
1399 if (worker->stats.max_query_time_us < wait_queue_time)
1400 worker->stats.max_query_time_us = wait_queue_time;
1401 if(wait_queue_time >
1402 (long long)(worker->env.cfg->sock_queue_timeout * 1000000)) {
1403 /* count and drop queries that were sitting in the socket queue too long */
1404 worker->stats.num_queries_timed_out++;
1405 return 0;
1406 }
1407 }
1408
1409 #ifdef USE_DNSCRYPT
1410 repinfo->max_udp_size = worker->daemon->cfg->max_udp_size;
1411 if(!dnsc_handle_curved_request(worker->daemon->dnscenv, repinfo)) {
1412 worker->stats.num_query_dnscrypt_crypted_malformed++;
1413 return 0;
1414 }
1415 if(c->dnscrypt && !repinfo->is_dnscrypted) {
1416 char buf[LDNS_MAX_DOMAINLEN+1];
1417 /* Check if this is unencrypted and asking for certs */
1418 worker_check_request(c->buffer, worker, &check_result);
1419 if(check_result.value != 0) {
1420 verbose(VERB_ALGO,
1421 "dnscrypt: worker check request: bad query.");
1422 log_addr(VERB_CLIENT,"from",&repinfo->client_addr,
1423 repinfo->client_addrlen);
1424 comm_point_drop_reply(repinfo);
1425 return 0;
1426 }
1427 if(!query_info_parse(&qinfo, c->buffer)) {
1428 verbose(VERB_ALGO,
1429 "dnscrypt: worker parse request: formerror.");
1430 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1431 repinfo->client_addrlen);
1432 comm_point_drop_reply(repinfo);
1433 return 0;
1434 }
1435 dname_str(qinfo.qname, buf);
1436 if(!(qinfo.qtype == LDNS_RR_TYPE_TXT &&
1437 strcasecmp(buf,
1438 worker->daemon->dnscenv->provider_name) == 0)) {
1439 verbose(VERB_ALGO,
1440 "dnscrypt: not TXT \"%s\". Received: %s \"%s\"",
1441 worker->daemon->dnscenv->provider_name,
1442 sldns_rr_descript(qinfo.qtype)->_name,
1443 buf);
1444 comm_point_drop_reply(repinfo);
1445 worker->stats.num_query_dnscrypt_cleartext++;
1446 return 0;
1447 }
1448 worker->stats.num_query_dnscrypt_cert++;
1449 sldns_buffer_rewind(c->buffer);
1450 } else if(c->dnscrypt && repinfo->is_dnscrypted) {
1451 worker->stats.num_query_dnscrypt_crypted++;
1452 }
1453 #endif
1454 #ifdef USE_DNSTAP
1455 /*
1456 * sending src (client)/dst (local service) addresses over DNSTAP from incoming request handler
1457 */
1458 if(worker->dtenv.log_client_query_messages) {
1459 log_addr(VERB_ALGO, "request from client", &repinfo->client_addr, repinfo->client_addrlen);
1460 log_addr(VERB_ALGO, "to local addr", (void*)repinfo->c->socket->addr, repinfo->c->socket->addrlen);
1461 dt_msg_send_client_query(&worker->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr, c->type, c->ssl, c->buffer,
1462 ((worker->env.cfg->sock_queue_timeout && timeval_isset(&c->recv_tv))?&c->recv_tv:NULL));
1463 }
1464 #endif
1465 /* Check deny/refuse ACLs */
1466 if(repinfo->is_proxied) {
1467 if((ret=deny_refuse_all(c, &acl, worker, repinfo, &acladdr,
1468 worker->env.cfg->ede, 1, &check_result)) != -1) {
1469 if(ret == 1)
1470 goto send_reply;
1471 return ret;
1472 }
1473 }
1474 if((ret=deny_refuse_all(c, &acl, worker, repinfo, &acladdr,
1475 worker->env.cfg->ede, 0, &check_result)) != -1) {
1476 if(ret == 1)
1477 goto send_reply;
1478 return ret;
1479 }
1480
1481 worker_check_request(c->buffer, worker, &check_result);
1482 if(check_result.value != 0) {
1483 verbose(VERB_ALGO, "worker check request: bad query.");
1484 log_addr(VERB_CLIENT,"from",&repinfo->client_addr, repinfo->client_addrlen);
1485 if(check_result.value != -1) {
1486 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1487 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1488 check_result.value);
1489 return 1;
1490 }
1491 comm_point_drop_reply(repinfo);
1492 return 0;
1493 }
1494
1495 worker->stats.num_queries++;
1496 pre_edns_ip_ratelimit = !worker->env.cfg->do_answer_cookie
1497 || sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE
1498 || LDNS_ARCOUNT(sldns_buffer_begin(c->buffer)) == 0;
1499
1500 /* If the IP rate limiting check needs extra EDNS information (e.g.,
1501 * DNS Cookies) postpone the check until after EDNS is parsed. */
1502 if(pre_edns_ip_ratelimit) {
1503 /* NOTE: we always check the repinfo->client_address.
1504 * IP ratelimiting is implicitly disabled for proxies. */
1505 if(!check_ip_ratelimit(worker, &repinfo->client_addr,
1506 repinfo->client_addrlen, 0, c->buffer)) {
1507 worker->stats.num_queries_ip_ratelimited++;
1508 comm_point_drop_reply(repinfo);
1509 return 0;
1510 }
1511 }
1512
1513 if(!query_info_parse(&qinfo, c->buffer)) {
1514 verbose(VERB_ALGO, "worker parse request: formerror.");
1515 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1516 repinfo->client_addrlen);
1517 memset(&qinfo, 0, sizeof(qinfo)); /* zero qinfo.qname */
1518 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1519 comm_point_drop_reply(repinfo);
1520 return 0;
1521 }
1522 sldns_buffer_rewind(c->buffer);
1523 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1524 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1525 LDNS_RCODE_FORMERR);
1526 goto send_reply;
1527 }
1528 if(worker->env.cfg->log_queries) {
1529 char ip[128];
1530 addr_to_str(&repinfo->client_addr, repinfo->client_addrlen, ip, sizeof(ip));
1531 log_query_in(ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
1532 }
1533 if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
1534 qinfo.qtype == LDNS_RR_TYPE_IXFR) {
1535 verbose(VERB_ALGO, "worker request: refused zone transfer.");
1536 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1537 repinfo->client_addrlen);
1538 sldns_buffer_rewind(c->buffer);
1539 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1540 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1541 LDNS_RCODE_REFUSED);
1542 if(worker->stats.extended) {
1543 worker->stats.qtype[qinfo.qtype]++;
1544 }
1545 goto send_reply;
1546 }
1547 if(qinfo.qtype == LDNS_RR_TYPE_OPT ||
1548 qinfo.qtype == LDNS_RR_TYPE_TSIG ||
1549 qinfo.qtype == LDNS_RR_TYPE_TKEY ||
1550 qinfo.qtype == LDNS_RR_TYPE_MAILA ||
1551 qinfo.qtype == LDNS_RR_TYPE_MAILB ||
1552 (qinfo.qtype >= 128 && qinfo.qtype <= 248)) {
1553 verbose(VERB_ALGO, "worker request: formerror for meta-type.");
1554 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1555 repinfo->client_addrlen);
1556 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1557 comm_point_drop_reply(repinfo);
1558 return 0;
1559 }
1560 sldns_buffer_rewind(c->buffer);
1561 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1562 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1563 LDNS_RCODE_FORMERR);
1564 if(worker->stats.extended) {
1565 worker->stats.qtype[qinfo.qtype]++;
1566 }
1567 goto send_reply;
1568 }
1569 if((ret=parse_edns_from_query_pkt(
1570 c->buffer, &edns, worker->env.cfg, c, repinfo,
1571 (worker->env.now ? *worker->env.now : time(NULL)),
1572 worker->scratchpad,
1573 worker->daemon->cookie_secrets)) != 0) {
1574 struct edns_data reply_edns;
1575 verbose(VERB_ALGO, "worker parse edns: formerror.");
1576 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1577 repinfo->client_addrlen);
1578 memset(&reply_edns, 0, sizeof(reply_edns));
1579 reply_edns.edns_present = 1;
1580 error_encode(c->buffer, ret, &qinfo,
1581 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1582 sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
1583 regional_free_all(worker->scratchpad);
1584 goto send_reply;
1585 }
1586 if(edns.edns_present) {
1587 if(edns.edns_version != 0) {
1588 edns.opt_list_in = NULL;
1589 edns.opt_list_out = NULL;
1590 edns.opt_list_inplace_cb_out = NULL;
1591 verbose(VERB_ALGO, "query with bad edns version.");
1592 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1593 repinfo->client_addrlen);
1594 extended_error_encode(c->buffer, EDNS_RCODE_BADVERS, &qinfo,
1595 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1596 sldns_buffer_read_u16_at(c->buffer, 2), 0, &edns);
1597 regional_free_all(worker->scratchpad);
1598 goto send_reply;
1599 }
1600 if(edns.udp_size < NORMAL_UDP_SIZE &&
1601 worker->daemon->cfg->harden_short_bufsize) {
1602 verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
1603 (int)edns.udp_size);
1604 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1605 repinfo->client_addrlen);
1606 edns.udp_size = NORMAL_UDP_SIZE;
1607 }
1608 }
1609
1610 /* Get stats for cookies */
1611 server_stats_downstream_cookie(&worker->stats, &edns);
1612
1613 /* If the IP rate limiting check was postponed, check now. */
1614 if(!pre_edns_ip_ratelimit) {
1615 /* NOTE: we always check the repinfo->client_address.
1616 * IP ratelimiting is implicitly disabled for proxies. */
1617 if(!check_ip_ratelimit(worker, &repinfo->client_addr,
1618 repinfo->client_addrlen, edns.cookie_valid,
1619 c->buffer)) {
1620 worker->stats.num_queries_ip_ratelimited++;
1621 comm_point_drop_reply(repinfo);
1622 return 0;
1623 }
1624 }
1625
1626 /* "if, else if" sequence below deals with downstream DNS Cookies */
1627 if(acl != acl_allow_cookie)
1628 ; /* pass; No cookie downstream processing whatsoever */
1629
1630 else if(edns.cookie_valid)
1631 ; /* pass; Valid cookie is good! */
1632
1633 else if(c->type != comm_udp)
1634 ; /* pass; Stateful transport */
1635
1636 else if(edns.cookie_present) {
1637 /* Cookie present, but not valid: Cookie was bad! */
1638 extended_error_encode(c->buffer,
1639 LDNS_EXT_RCODE_BADCOOKIE, &qinfo,
1640 *(uint16_t*)(void *)
1641 sldns_buffer_begin(c->buffer),
1642 sldns_buffer_read_u16_at(c->buffer, 2),
1643 0, &edns);
1644 regional_free_all(worker->scratchpad);
1645 goto send_reply;
1646 } else {
1647 /* Cookie required, but no cookie present on UDP */
1648 verbose(VERB_ALGO, "worker request: "
1649 "need cookie or stateful transport");
1650 log_addr(VERB_ALGO, "from",&repinfo->remote_addr
1651 , repinfo->remote_addrlen);
1652 EDNS_OPT_LIST_APPEND_EDE(&edns.opt_list_out,
1653 worker->scratchpad, LDNS_EDE_OTHER,
1654 "DNS Cookie needed for UDP replies");
1655 error_encode(c->buffer,
1656 (LDNS_RCODE_REFUSED|BIT_TC), &qinfo,
1657 *(uint16_t*)(void *)
1658 sldns_buffer_begin(c->buffer),
1659 sldns_buffer_read_u16_at(c->buffer, 2),
1660 &edns);
1661 regional_free_all(worker->scratchpad);
1662 goto send_reply;
1663 }
1664
1665 if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
1666 c->type == comm_udp) {
1667 verbose(VERB_QUERY,
1668 "worker request: max UDP reply size modified"
1669 " (%d to max-udp-size)", (int)edns.udp_size);
1670 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1671 repinfo->client_addrlen);
1672 edns.udp_size = worker->daemon->cfg->max_udp_size;
1673 }
1674 if(edns.udp_size < LDNS_HEADER_SIZE) {
1675 verbose(VERB_ALGO, "worker request: edns is too small.");
1676 log_addr(VERB_CLIENT, "from", &repinfo->client_addr,
1677 repinfo->client_addrlen);
1678 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1679 LDNS_TC_SET(sldns_buffer_begin(c->buffer));
1680 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1681 LDNS_RCODE_SERVFAIL);
1682 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1683 sldns_buffer_write_at(c->buffer, 4,
1684 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1685 sldns_buffer_flip(c->buffer);
1686 regional_free_all(worker->scratchpad);
1687 goto send_reply;
1688 }
1689 if(worker->stats.extended)
1690 server_stats_insquery(&worker->stats, c, qinfo.qtype,
1691 qinfo.qclass, &edns, repinfo);
1692 if(c->type != comm_udp)
1693 edns.udp_size = 65535; /* max size for TCP replies */
1694 if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
1695 &edns, repinfo, c->buffer)) {
1696 regional_free_all(worker->scratchpad);
1697 goto send_reply;
1698 }
1699 if(LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1700 LDNS_PACKET_NOTIFY) {
1701 answer_notify(worker, &qinfo, &edns, c->buffer,
1702 &repinfo->client_addr, repinfo->client_addrlen);
1703 regional_free_all(worker->scratchpad);
1704 goto send_reply;
1705 }
1706 if(local_zones_answer(worker->daemon->local_zones, &worker->env, &qinfo,
1707 &edns, c->buffer, worker->scratchpad, repinfo, acladdr->taglist,
1708 acladdr->taglen, acladdr->tag_actions,
1709 acladdr->tag_actions_size, acladdr->tag_datas,
1710 acladdr->tag_datas_size, worker->daemon->cfg->tagname,
1711 worker->daemon->cfg->num_tags, acladdr->view)) {
1712 regional_free_all(worker->scratchpad);
1713 if(sldns_buffer_limit(c->buffer) == 0) {
1714 comm_point_drop_reply(repinfo);
1715 return 0;
1716 }
1717 goto send_reply;
1718 }
1719 if(worker->env.auth_zones &&
1720 rpz_callback_from_worker_request(worker->env.auth_zones,
1721 &worker->env, &qinfo, &edns, c->buffer, worker->scratchpad,
1722 repinfo, acladdr->taglist, acladdr->taglen, &worker->stats,
1723 &rpz_passthru)) {
1724 regional_free_all(worker->scratchpad);
1725 if(sldns_buffer_limit(c->buffer) == 0) {
1726 comm_point_drop_reply(repinfo);
1727 return 0;
1728 }
1729 goto send_reply;
1730 }
1731 if(worker->env.auth_zones &&
1732 auth_zones_answer(worker->env.auth_zones, &worker->env,
1733 &qinfo, &edns, repinfo, c->buffer, worker->scratchpad)) {
1734 regional_free_all(worker->scratchpad);
1735 if(sldns_buffer_limit(c->buffer) == 0) {
1736 comm_point_drop_reply(repinfo);
1737 return 0;
1738 }
1739 /* set RA for everyone that can have recursion (based on
1740 * access control list) */
1741 if(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer)) &&
1742 acl != acl_deny_non_local && acl != acl_refuse_non_local)
1743 LDNS_RA_SET(sldns_buffer_begin(c->buffer));
1744 goto send_reply;
1745 }
1746
1747 /* We've looked in our local zones. If the answer isn't there, we
1748 * might need to bail out based on ACLs now. */
1749 if((ret=deny_refuse_non_local(c, acl, worker, repinfo, acladdr,
1750 worker->env.cfg->ede, &check_result)) != -1)
1751 {
1752 regional_free_all(worker->scratchpad);
1753 if(ret == 1)
1754 goto send_reply;
1755 return ret;
1756 }
1757
1758 /* If this request does not have the recursion bit set, verify
1759 * ACLs allow the recursion bit to be treated as set. */
1760 if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
1761 acl == acl_allow_setrd ) {
1762 LDNS_RD_SET(sldns_buffer_begin(c->buffer));
1763 }
1764
1765 /* If this request does not have the recursion bit set, verify
1766 * ACLs allow the snooping. */
1767 if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
1768 acl != acl_allow_snoop ) {
1769 if(worker->env.cfg->ede) {
1770 EDNS_OPT_LIST_APPEND_EDE(&edns.opt_list_out,
1771 worker->scratchpad, LDNS_EDE_NOT_AUTHORITATIVE, "");
1772 }
1773 error_encode(c->buffer, LDNS_RCODE_REFUSED, &qinfo,
1774 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1775 sldns_buffer_read_u16_at(c->buffer, 2), &edns);
1776 regional_free_all(worker->scratchpad);
1777 log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
1778 &repinfo->client_addr, repinfo->client_addrlen);
1779
1780 goto send_reply;
1781 }
1782
1783 /* If we've found a local alias, replace the qname with the alias
1784 * target before resolving it. */
1785 if(qinfo.local_alias) {
1786 struct ub_packed_rrset_key* rrset = qinfo.local_alias->rrset;
1787 struct packed_rrset_data* d = rrset->entry.data;
1788
1789 /* Sanity check: our current implementation only supports
1790 * a single CNAME RRset as a local alias. */
1791 if(qinfo.local_alias->next ||
1792 rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) ||
1793 d->count != 1) {
1794 log_err("assumption failure: unexpected local alias");
1795 regional_free_all(worker->scratchpad);
1796 return 0; /* drop it */
1797 }
1798 qinfo.qname = d->rr_data[0] + 2;
1799 qinfo.qname_len = d->rr_len[0] - 2;
1800 }
1801
1802 /* If we may apply IP-based actions to the answer, build the client
1803 * information. As this can be expensive, skip it if there is
1804 * absolutely no possibility of it. */
1805 if((worker->daemon->use_response_ip || worker->daemon->use_rpz) &&
1806 (qinfo.qtype == LDNS_RR_TYPE_A ||
1807 qinfo.qtype == LDNS_RR_TYPE_AAAA ||
1808 qinfo.qtype == LDNS_RR_TYPE_ANY)) {
1809 cinfo_tmp.taglist = acladdr->taglist;
1810 cinfo_tmp.taglen = acladdr->taglen;
1811 cinfo_tmp.tag_actions = acladdr->tag_actions;
1812 cinfo_tmp.tag_actions_size = acladdr->tag_actions_size;
1813 cinfo_tmp.tag_datas = acladdr->tag_datas;
1814 cinfo_tmp.tag_datas_size = acladdr->tag_datas_size;
1815 cinfo_tmp.view = acladdr->view;
1816 cinfo_tmp.respip_set = worker->daemon->respip_set;
1817 cinfo = &cinfo_tmp;
1818 }
1819
1820 /* Keep the original edns list around. The pointer could change if there is
1821 * a cached answer (through the inplace callback function there).
1822 * No need to actually copy the contents as they shouldn't change.
1823 * Used while prefetching and subnet is enabled. */
1824 original_edns_list = edns.opt_list_in;
1825 lookup_cache:
1826 /* Lookup the cache. In case we chase an intermediate CNAME chain
1827 * this is a two-pass operation, and lookup_qinfo is different for
1828 * each pass. We should still pass the original qinfo to
1829 * answer_from_cache(), however, since it's used to build the reply. */
1830 if(!edns_bypass_cache_stage(edns.opt_list_in, &worker->env)) {
1831 is_expired_answer = 0;
1832 is_secure_answer = 0;
1833 h = query_info_hash(lookup_qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
1834 if((e=slabhash_lookup(worker->env.msg_cache, h, lookup_qinfo, 0))) {
1835 struct reply_info* rep = (struct reply_info*)e->data;
1836 /* answer from cache - we have acquired a readlock on it */
1837 if(answer_from_cache(worker, &qinfo, cinfo, &need_drop,
1838 &is_expired_answer, &is_secure_answer,
1839 &alias_rrset, &partial_rep, rep,
1840 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1841 sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1842 &edns)) {
1843 /* prefetch it if the prefetch TTL expired.
1844 * Note that if there is more than one pass
1845 * its qname must be that used for cache
1846 * lookup. */
1847 if((worker->env.cfg->prefetch &&
1848 *worker->env.now >= rep->prefetch_ttl) ||
1849 (worker->env.cfg->serve_expired &&
1850 *worker->env.now > rep->ttl)) {
1851
1852 time_t leeway = rep->ttl - *worker->env.now;
1853 if(rep->ttl < *worker->env.now)
1854 leeway = 0;
1855 lock_rw_unlock(&e->lock);
1856
1857 reply_and_prefetch(worker, lookup_qinfo,
1858 sldns_buffer_read_u16_at(c->buffer, 2),
1859 repinfo, leeway,
1860 (partial_rep || need_drop),
1861 rpz_passthru,
1862 original_edns_list);
1863 if(!partial_rep) {
1864 rc = 0;
1865 regional_free_all(worker->scratchpad);
1866 goto send_reply_rc;
1867 }
1868 } else if(!partial_rep) {
1869 lock_rw_unlock(&e->lock);
1870 regional_free_all(worker->scratchpad);
1871 goto send_reply;
1872 } else {
1873 /* Note that we've already released the
1874 * lock if we're here after prefetch. */
1875 lock_rw_unlock(&e->lock);
1876 }
1877 /* We've found a partial reply ending with an
1878 * alias. Replace the lookup qinfo for the
1879 * alias target and lookup the cache again to
1880 * (possibly) complete the reply. As we're
1881 * passing the "base" reply, there will be no
1882 * more alias chasing. */
1883 memset(&qinfo_tmp, 0, sizeof(qinfo_tmp));
1884 get_cname_target(alias_rrset, &qinfo_tmp.qname,
1885 &qinfo_tmp.qname_len);
1886 if(!qinfo_tmp.qname) {
1887 log_err("unexpected: invalid answer alias");
1888 regional_free_all(worker->scratchpad);
1889 return 0; /* drop query */
1890 }
1891 qinfo_tmp.qtype = qinfo.qtype;
1892 qinfo_tmp.qclass = qinfo.qclass;
1893 lookup_qinfo = &qinfo_tmp;
1894 goto lookup_cache;
1895 }
1896 verbose(VERB_ALGO, "answer from the cache failed");
1897 lock_rw_unlock(&e->lock);
1898 }
1899
1900 if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
1901 if(answer_norec_from_cache(worker, &qinfo,
1902 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1903 sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1904 &edns)) {
1905 regional_free_all(worker->scratchpad);
1906 goto send_reply;
1907 }
1908 verbose(VERB_ALGO, "answer norec from cache -- "
1909 "need to validate or not primed");
1910 }
1911 }
1912 sldns_buffer_rewind(c->buffer);
1913 server_stats_querymiss(&worker->stats, worker);
1914
1915 if(verbosity >= VERB_CLIENT) {
1916 if(c->type == comm_udp)
1917 log_addr(VERB_CLIENT, "udp request from",
1918 &repinfo->client_addr, repinfo->client_addrlen);
1919 else log_addr(VERB_CLIENT, "tcp request from",
1920 &repinfo->client_addr, repinfo->client_addrlen);
1921 }
1922
1923 /* grab a work request structure for this new request */
1924 mesh_new_client(worker->env.mesh, &qinfo, cinfo,
1925 sldns_buffer_read_u16_at(c->buffer, 2),
1926 &edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1927 rpz_passthru);
1928 regional_free_all(worker->scratchpad);
1929 worker_mem_report(worker, NULL);
1930 return 0;
1931
1932 send_reply:
1933 rc = 1;
1934 send_reply_rc:
1935 if(need_drop) {
1936 comm_point_drop_reply(repinfo);
1937 return 0;
1938 }
1939 if(is_expired_answer) {
1940 worker->stats.ans_expired++;
1941 }
1942 server_stats_insrcode(&worker->stats, c->buffer);
1943 if(worker->stats.extended) {
1944 if(is_secure_answer) worker->stats.ans_secure++;
1945 }
1946 #ifdef USE_DNSTAP
1947 /*
1948 * sending src (client)/dst (local service) addresses over DNSTAP from send_reply code label (when we serviced local zone for ex.)
1949 */
1950 if(worker->dtenv.log_client_response_messages && rc !=0) {
1951 log_addr(VERB_ALGO, "from local addr", (void*)repinfo->c->socket->addr, repinfo->c->socket->addrlen);
1952 log_addr(VERB_ALGO, "response to client", &repinfo->client_addr, repinfo->client_addrlen);
1953 dt_msg_send_client_response(&worker->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr, c->type, c->ssl, c->buffer);
1954 }
1955 #endif
1956 if(worker->env.cfg->log_replies)
1957 {
1958 struct timeval tv;
1959 memset(&tv, 0, sizeof(tv));
1960 if(qinfo.local_alias && qinfo.local_alias->rrset &&
1961 qinfo.local_alias->rrset->rk.dname) {
1962 /* log original qname, before the local alias was
1963 * used to resolve that CNAME to something else */
1964 qinfo.qname = qinfo.local_alias->rrset->rk.dname;
1965 log_reply_info(NO_VERBOSE, &qinfo,
1966 &repinfo->client_addr, repinfo->client_addrlen,
1967 tv, 1, c->buffer,
1968 (worker->env.cfg->log_destaddr?(void*)repinfo->c->socket->addr:NULL),
1969 c->type);
1970 } else {
1971 log_reply_info(NO_VERBOSE, &qinfo,
1972 &repinfo->client_addr, repinfo->client_addrlen,
1973 tv, 1, c->buffer,
1974 (worker->env.cfg->log_destaddr?(void*)repinfo->c->socket->addr:NULL),
1975 c->type);
1976 }
1977 }
1978 #ifdef USE_DNSCRYPT
1979 if(!dnsc_handle_uncurved_request(repinfo)) {
1980 return 0;
1981 }
1982 #endif
1983 return rc;
1984 }
1985
1986 void
worker_sighandler(int sig,void * arg)1987 worker_sighandler(int sig, void* arg)
1988 {
1989 /* note that log, print, syscalls here give race conditions.
1990 * And cause hangups if the log-lock is held by the application. */
1991 struct worker* worker = (struct worker*)arg;
1992 switch(sig) {
1993 #ifdef SIGHUP
1994 case SIGHUP:
1995 comm_base_exit(worker->base);
1996 break;
1997 #endif
1998 #ifdef SIGBREAK
1999 case SIGBREAK:
2000 #endif
2001 case SIGINT:
2002 worker->need_to_exit = 1;
2003 comm_base_exit(worker->base);
2004 break;
2005 #ifdef SIGQUIT
2006 case SIGQUIT:
2007 worker->need_to_exit = 1;
2008 comm_base_exit(worker->base);
2009 break;
2010 #endif
2011 case SIGTERM:
2012 worker->need_to_exit = 1;
2013 comm_base_exit(worker->base);
2014 break;
2015 default:
2016 /* unknown signal, ignored */
2017 break;
2018 }
2019 }
2020
2021 /** restart statistics timer for worker, if enabled */
2022 static void
worker_restart_timer(struct worker * worker)2023 worker_restart_timer(struct worker* worker)
2024 {
2025 if(worker->env.cfg->stat_interval > 0) {
2026 struct timeval tv;
2027 #ifndef S_SPLINT_S
2028 tv.tv_sec = worker->env.cfg->stat_interval;
2029 tv.tv_usec = 0;
2030 #endif
2031 comm_timer_set(worker->stat_timer, &tv);
2032 }
2033 }
2034
worker_stat_timer_cb(void * arg)2035 void worker_stat_timer_cb(void* arg)
2036 {
2037 struct worker* worker = (struct worker*)arg;
2038 server_stats_log(&worker->stats, worker, worker->thread_num);
2039 mesh_stats(worker->env.mesh, "mesh has");
2040 worker_mem_report(worker, NULL);
2041 /* SHM is enabled, process data to SHM */
2042 if (worker->daemon->cfg->shm_enable) {
2043 shm_main_run(worker);
2044 }
2045 if(!worker->daemon->cfg->stat_cumulative) {
2046 worker_stats_clear(worker);
2047 }
2048 /* start next timer */
2049 worker_restart_timer(worker);
2050 }
2051
worker_probe_timer_cb(void * arg)2052 void worker_probe_timer_cb(void* arg)
2053 {
2054 struct worker* worker = (struct worker*)arg;
2055 struct timeval tv;
2056 #ifndef S_SPLINT_S
2057 tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
2058 tv.tv_usec = 0;
2059 #endif
2060 if(tv.tv_sec != 0)
2061 comm_timer_set(worker->env.probe_timer, &tv);
2062 }
2063
2064 struct worker*
worker_create(struct daemon * daemon,int id,int * ports,int n)2065 worker_create(struct daemon* daemon, int id, int* ports, int n)
2066 {
2067 unsigned int seed;
2068 struct worker* worker = (struct worker*)calloc(1,
2069 sizeof(struct worker));
2070 if(!worker)
2071 return NULL;
2072 worker->numports = n;
2073 worker->ports = (int*)memdup(ports, sizeof(int)*n);
2074 if(!worker->ports) {
2075 free(worker);
2076 return NULL;
2077 }
2078 worker->daemon = daemon;
2079 worker->thread_num = id;
2080 if(!(worker->cmd = tube_create())) {
2081 free(worker->ports);
2082 free(worker);
2083 return NULL;
2084 }
2085 /* create random state here to avoid locking trouble in RAND_bytes */
2086 if(!(worker->rndstate = ub_initstate(daemon->rand))) {
2087 log_err("could not init random numbers.");
2088 tube_delete(worker->cmd);
2089 free(worker->ports);
2090 free(worker);
2091 return NULL;
2092 }
2093 explicit_bzero(&seed, sizeof(seed));
2094 return worker;
2095 }
2096
2097 int
worker_init(struct worker * worker,struct config_file * cfg,struct listen_port * ports,int do_sigs)2098 worker_init(struct worker* worker, struct config_file *cfg,
2099 struct listen_port* ports, int do_sigs)
2100 {
2101 #ifdef USE_DNSTAP
2102 struct dt_env* dtenv = &worker->dtenv;
2103 #else
2104 void* dtenv = NULL;
2105 #endif
2106 #ifdef HAVE_GETTID
2107 worker->thread_tid = gettid();
2108 #endif
2109 worker->need_to_exit = 0;
2110 worker->base = comm_base_create(do_sigs);
2111 if(!worker->base) {
2112 log_err("could not create event handling base");
2113 worker_delete(worker);
2114 return 0;
2115 }
2116 comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
2117 &worker_start_accept, worker);
2118 if(do_sigs) {
2119 #ifdef SIGHUP
2120 ub_thread_sig_unblock(SIGHUP);
2121 #endif
2122 #ifdef SIGBREAK
2123 ub_thread_sig_unblock(SIGBREAK);
2124 #endif
2125 ub_thread_sig_unblock(SIGINT);
2126 #ifdef SIGQUIT
2127 ub_thread_sig_unblock(SIGQUIT);
2128 #endif
2129 ub_thread_sig_unblock(SIGTERM);
2130 #ifndef LIBEVENT_SIGNAL_PROBLEM
2131 worker->comsig = comm_signal_create(worker->base,
2132 worker_sighandler, worker);
2133 if(!worker->comsig
2134 #ifdef SIGHUP
2135 || !comm_signal_bind(worker->comsig, SIGHUP)
2136 #endif
2137 #ifdef SIGQUIT
2138 || !comm_signal_bind(worker->comsig, SIGQUIT)
2139 #endif
2140 || !comm_signal_bind(worker->comsig, SIGTERM)
2141 #ifdef SIGBREAK
2142 || !comm_signal_bind(worker->comsig, SIGBREAK)
2143 #endif
2144 || !comm_signal_bind(worker->comsig, SIGINT)) {
2145 log_err("could not create signal handlers");
2146 worker_delete(worker);
2147 return 0;
2148 }
2149 #endif /* LIBEVENT_SIGNAL_PROBLEM */
2150 if(!daemon_remote_open_accept(worker->daemon->rc,
2151 worker->daemon->rc_ports, worker)) {
2152 worker_delete(worker);
2153 return 0;
2154 }
2155 #ifdef UB_ON_WINDOWS
2156 wsvc_setup_worker(worker);
2157 #endif /* UB_ON_WINDOWS */
2158 } else { /* !do_sigs */
2159 worker->comsig = NULL;
2160 }
2161 #ifdef USE_DNSTAP
2162 if(cfg->dnstap) {
2163 log_assert(worker->daemon->dtenv != NULL);
2164 memcpy(&worker->dtenv, worker->daemon->dtenv, sizeof(struct dt_env));
2165 if(!dt_init(&worker->dtenv, worker->base))
2166 fatal_exit("dt_init failed");
2167 }
2168 #endif
2169 worker->front = listen_create(worker->base, ports,
2170 cfg->msg_buffer_size, (int)cfg->incoming_num_tcp,
2171 cfg->do_tcp_keepalive
2172 ? cfg->tcp_keepalive_timeout
2173 : cfg->tcp_idle_timeout,
2174 cfg->harden_large_queries, cfg->http_max_streams,
2175 cfg->http_endpoint, cfg->http_notls_downstream,
2176 worker->daemon->tcl, worker->daemon->listen_sslctx,
2177 dtenv, worker->daemon->doq_table, worker->env.rnd,
2178 cfg->ssl_service_key, cfg->ssl_service_pem, cfg,
2179 worker_handle_request, worker);
2180 if(!worker->front) {
2181 log_err("could not create listening sockets");
2182 worker_delete(worker);
2183 return 0;
2184 }
2185 worker->back = outside_network_create(worker->base,
2186 cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports,
2187 cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
2188 cfg->do_tcp?cfg->outgoing_num_tcp:0, cfg->ip_dscp,
2189 worker->daemon->env->infra_cache, worker->rndstate,
2190 cfg->use_caps_bits_for_id, worker->ports, worker->numports,
2191 cfg->unwanted_threshold, cfg->outgoing_tcp_mss,
2192 &worker_alloc_cleanup, worker,
2193 cfg->do_udp || cfg->udp_upstream_without_downstream,
2194 worker->daemon->connect_sslctx, cfg->delay_close,
2195 cfg->tls_use_sni, dtenv, cfg->udp_connect,
2196 cfg->max_reuse_tcp_queries, cfg->tcp_reuse_timeout,
2197 cfg->tcp_auth_query_timeout);
2198 if(!worker->back) {
2199 log_err("could not create outgoing sockets");
2200 worker_delete(worker);
2201 return 0;
2202 }
2203 iterator_set_ip46_support(&worker->daemon->mods, worker->daemon->env,
2204 worker->back);
2205 /* start listening to commands */
2206 if(!tube_setup_bg_listen(worker->cmd, worker->base,
2207 &worker_handle_control_cmd, worker)) {
2208 log_err("could not create control compt.");
2209 worker_delete(worker);
2210 return 0;
2211 }
2212 worker->stat_timer = comm_timer_create(worker->base,
2213 worker_stat_timer_cb, worker);
2214 if(!worker->stat_timer) {
2215 log_err("could not create statistics timer");
2216 }
2217
2218 /* we use the msg_buffer_size as a good estimate for what the
2219 * user wants for memory usage sizes */
2220 worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
2221 if(!worker->scratchpad) {
2222 log_err("malloc failure");
2223 worker_delete(worker);
2224 return 0;
2225 }
2226
2227 server_stats_init(&worker->stats, cfg);
2228 worker->alloc = worker->daemon->worker_allocs[worker->thread_num];
2229 alloc_set_id_cleanup(worker->alloc, &worker_alloc_cleanup, worker);
2230 worker->env = *worker->daemon->env;
2231 comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
2232 worker->env.worker = worker;
2233 worker->env.worker_base = worker->base;
2234 worker->env.send_query = &worker_send_query;
2235 worker->env.alloc = worker->alloc;
2236 worker->env.outnet = worker->back;
2237 worker->env.rnd = worker->rndstate;
2238 /* If case prefetch is triggered, the corresponding mesh will clear
2239 * the scratchpad for the module env in the middle of request handling.
2240 * It would be prone to a use-after-free kind of bug, so we avoid
2241 * sharing it with worker's own scratchpad at the cost of having
2242 * one more pad per worker. */
2243 worker->env.scratch = regional_create_custom(cfg->msg_buffer_size);
2244 if(!worker->env.scratch) {
2245 log_err("malloc failure");
2246 worker_delete(worker);
2247 return 0;
2248 }
2249 worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
2250 if(!worker->env.mesh) {
2251 log_err("malloc failure");
2252 worker_delete(worker);
2253 return 0;
2254 }
2255 /* Pass on daemon variables that we would need in the mesh area */
2256 worker->env.mesh->use_response_ip = worker->daemon->use_response_ip;
2257 worker->env.mesh->use_rpz = worker->daemon->use_rpz;
2258
2259 worker->env.detach_subs = &mesh_detach_subs;
2260 worker->env.attach_sub = &mesh_attach_sub;
2261 worker->env.add_sub = &mesh_add_sub;
2262 worker->env.kill_sub = &mesh_state_delete;
2263 worker->env.detect_cycle = &mesh_detect_cycle;
2264 worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
2265 if(!worker->env.scratch_buffer) {
2266 log_err("malloc failure");
2267 worker_delete(worker);
2268 return 0;
2269 }
2270 /* one probe timer per process -- if we have 5011 anchors */
2271 if(autr_get_num_anchors(worker->env.anchors) > 0
2272 #ifndef THREADS_DISABLED
2273 && worker->thread_num == 0
2274 #endif
2275 ) {
2276 struct timeval tv;
2277 tv.tv_sec = 0;
2278 tv.tv_usec = 0;
2279 worker->env.probe_timer = comm_timer_create(worker->base,
2280 worker_probe_timer_cb, worker);
2281 if(!worker->env.probe_timer) {
2282 log_err("could not create 5011-probe timer");
2283 } else {
2284 /* let timer fire, then it can reset itself */
2285 comm_timer_set(worker->env.probe_timer, &tv);
2286 }
2287 }
2288 /* zone transfer tasks, setup once per process, if any */
2289 if(worker->env.auth_zones
2290 #ifndef THREADS_DISABLED
2291 && worker->thread_num == 0
2292 #endif
2293 ) {
2294 auth_xfer_pickup_initial(worker->env.auth_zones, &worker->env);
2295 auth_zones_pickup_zonemd_verify(worker->env.auth_zones,
2296 &worker->env);
2297 }
2298 #ifdef USE_DNSTAP
2299 if(worker->daemon->cfg->dnstap
2300 #ifndef THREADS_DISABLED
2301 && worker->thread_num == 0
2302 #endif
2303 ) {
2304 if(!dt_io_thread_start(dtenv->dtio, comm_base_internal(
2305 worker->base), worker->daemon->num)) {
2306 log_err("could not start dnstap io thread");
2307 worker_delete(worker);
2308 return 0;
2309 }
2310 }
2311 #endif /* USE_DNSTAP */
2312 worker_mem_report(worker, NULL);
2313 /* if statistics enabled start timer */
2314 if(worker->env.cfg->stat_interval > 0) {
2315 verbose(VERB_ALGO, "set statistics interval %d secs",
2316 worker->env.cfg->stat_interval);
2317 worker_restart_timer(worker);
2318 }
2319 pp_init(&sldns_write_uint16, &sldns_write_uint32);
2320 return 1;
2321 }
2322
2323 void
worker_work(struct worker * worker)2324 worker_work(struct worker* worker)
2325 {
2326 comm_base_dispatch(worker->base);
2327 }
2328
2329 void
worker_delete(struct worker * worker)2330 worker_delete(struct worker* worker)
2331 {
2332 if(!worker)
2333 return;
2334 if(worker->env.mesh && verbosity >= VERB_OPS) {
2335 server_stats_log(&worker->stats, worker, worker->thread_num);
2336 mesh_stats(worker->env.mesh, "mesh has");
2337 worker_mem_report(worker, NULL);
2338 }
2339 outside_network_quit_prepare(worker->back);
2340 mesh_delete(worker->env.mesh);
2341 sldns_buffer_free(worker->env.scratch_buffer);
2342 listen_delete(worker->front);
2343 outside_network_delete(worker->back);
2344 comm_signal_delete(worker->comsig);
2345 tube_delete(worker->cmd);
2346 comm_timer_delete(worker->stat_timer);
2347 comm_timer_delete(worker->env.probe_timer);
2348 free(worker->ports);
2349 if(worker->thread_num == 0) {
2350 #ifdef UB_ON_WINDOWS
2351 wsvc_desetup_worker(worker);
2352 #endif /* UB_ON_WINDOWS */
2353 }
2354 #ifdef USE_DNSTAP
2355 if(worker->daemon->cfg->dnstap
2356 #ifndef THREADS_DISABLED
2357 && worker->thread_num == 0
2358 #endif
2359 ) {
2360 dt_io_thread_stop(worker->dtenv.dtio);
2361 }
2362 dt_deinit(&worker->dtenv);
2363 #endif /* USE_DNSTAP */
2364 comm_base_delete(worker->base);
2365 ub_randfree(worker->rndstate);
2366 /* don't touch worker->alloc, as it's maintained in daemon */
2367 regional_destroy(worker->env.scratch);
2368 regional_destroy(worker->scratchpad);
2369 free(worker);
2370 }
2371
2372 struct outbound_entry*
worker_send_query(struct query_info * qinfo,uint16_t flags,int dnssec,int want_dnssec,int nocaps,int check_ratelimit,struct sockaddr_storage * addr,socklen_t addrlen,uint8_t * zone,size_t zonelen,int tcp_upstream,int ssl_upstream,char * tls_auth_name,struct module_qstate * q,int * was_ratelimited)2373 worker_send_query(struct query_info* qinfo, uint16_t flags, int dnssec,
2374 int want_dnssec, int nocaps, int check_ratelimit,
2375 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
2376 size_t zonelen, int tcp_upstream, int ssl_upstream, char* tls_auth_name,
2377 struct module_qstate* q, int* was_ratelimited)
2378 {
2379 struct worker* worker = q->env->worker;
2380 struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
2381 q->region, sizeof(*e));
2382 if(!e)
2383 return NULL;
2384 e->qstate = q;
2385 e->qsent = outnet_serviced_query(worker->back, qinfo, flags, dnssec,
2386 want_dnssec, nocaps, check_ratelimit, tcp_upstream,
2387 ssl_upstream, tls_auth_name, addr, addrlen, zone, zonelen, q,
2388 worker_handle_service_reply, e, worker->back->udp_buff, q->env,
2389 was_ratelimited);
2390 if(!e->qsent) {
2391 return NULL;
2392 }
2393 return e;
2394 }
2395
2396 void
worker_alloc_cleanup(void * arg)2397 worker_alloc_cleanup(void* arg)
2398 {
2399 struct worker* worker = (struct worker*)arg;
2400 slabhash_clear(&worker->env.rrset_cache->table);
2401 slabhash_clear(worker->env.msg_cache);
2402 }
2403
worker_stats_clear(struct worker * worker)2404 void worker_stats_clear(struct worker* worker)
2405 {
2406 server_stats_init(&worker->stats, worker->env.cfg);
2407 mesh_stats_clear(worker->env.mesh);
2408 worker->back->unwanted_replies = 0;
2409 worker->back->num_tcp_outgoing = 0;
2410 worker->back->num_udp_outgoing = 0;
2411 }
2412
worker_start_accept(void * arg)2413 void worker_start_accept(void* arg)
2414 {
2415 struct worker* worker = (struct worker*)arg;
2416 listen_start_accept(worker->front);
2417 if(worker->thread_num == 0)
2418 daemon_remote_start_accept(worker->daemon->rc);
2419 }
2420
worker_stop_accept(void * arg)2421 void worker_stop_accept(void* arg)
2422 {
2423 struct worker* worker = (struct worker*)arg;
2424 listen_stop_accept(worker->front);
2425 if(worker->thread_num == 0)
2426 daemon_remote_stop_accept(worker->daemon->rc);
2427 }
2428
2429 /* --- fake callbacks for fptr_wlist to work --- */
libworker_send_query(struct query_info * ATTR_UNUSED (qinfo),uint16_t ATTR_UNUSED (flags),int ATTR_UNUSED (dnssec),int ATTR_UNUSED (want_dnssec),int ATTR_UNUSED (nocaps),int ATTR_UNUSED (check_ratelimit),struct sockaddr_storage * ATTR_UNUSED (addr),socklen_t ATTR_UNUSED (addrlen),uint8_t * ATTR_UNUSED (zone),size_t ATTR_UNUSED (zonelen),int ATTR_UNUSED (tcp_upstream),int ATTR_UNUSED (ssl_upstream),char * ATTR_UNUSED (tls_auth_name),struct module_qstate * ATTR_UNUSED (q),int * ATTR_UNUSED (was_ratelimited))2430 struct outbound_entry* libworker_send_query(
2431 struct query_info* ATTR_UNUSED(qinfo),
2432 uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec),
2433 int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
2434 int ATTR_UNUSED(check_ratelimit),
2435 struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
2436 uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(tcp_upstream),
2437 int ATTR_UNUSED(ssl_upstream), char* ATTR_UNUSED(tls_auth_name),
2438 struct module_qstate* ATTR_UNUSED(q), int* ATTR_UNUSED(was_ratelimited))
2439 {
2440 log_assert(0);
2441 return 0;
2442 }
2443
libworker_handle_service_reply(struct comm_point * ATTR_UNUSED (c),void * ATTR_UNUSED (arg),int ATTR_UNUSED (error),struct comm_reply * ATTR_UNUSED (reply_info))2444 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
2445 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
2446 struct comm_reply* ATTR_UNUSED(reply_info))
2447 {
2448 log_assert(0);
2449 return 0;
2450 }
2451
libworker_handle_control_cmd(struct tube * ATTR_UNUSED (tube),uint8_t * ATTR_UNUSED (buffer),size_t ATTR_UNUSED (len),int ATTR_UNUSED (error),void * ATTR_UNUSED (arg))2452 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
2453 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
2454 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
2455 {
2456 log_assert(0);
2457 }
2458
libworker_fg_done_cb(void * ATTR_UNUSED (arg),int ATTR_UNUSED (rcode),sldns_buffer * ATTR_UNUSED (buf),enum sec_status ATTR_UNUSED (s),char * ATTR_UNUSED (why_bogus),int ATTR_UNUSED (was_ratelimited))2459 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
2460 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
2461 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
2462 {
2463 log_assert(0);
2464 }
2465
libworker_bg_done_cb(void * ATTR_UNUSED (arg),int ATTR_UNUSED (rcode),sldns_buffer * ATTR_UNUSED (buf),enum sec_status ATTR_UNUSED (s),char * ATTR_UNUSED (why_bogus),int ATTR_UNUSED (was_ratelimited))2466 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
2467 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
2468 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
2469 {
2470 log_assert(0);
2471 }
2472
libworker_event_done_cb(void * ATTR_UNUSED (arg),int ATTR_UNUSED (rcode),sldns_buffer * ATTR_UNUSED (buf),enum sec_status ATTR_UNUSED (s),char * ATTR_UNUSED (why_bogus),int ATTR_UNUSED (was_ratelimited))2473 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
2474 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
2475 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
2476 {
2477 log_assert(0);
2478 }
2479
context_query_cmp(const void * ATTR_UNUSED (a),const void * ATTR_UNUSED (b))2480 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
2481 {
2482 log_assert(0);
2483 return 0;
2484 }
2485
order_lock_cmp(const void * ATTR_UNUSED (e1),const void * ATTR_UNUSED (e2))2486 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
2487 {
2488 log_assert(0);
2489 return 0;
2490 }
2491
codeline_cmp(const void * ATTR_UNUSED (a),const void * ATTR_UNUSED (b))2492 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
2493 {
2494 log_assert(0);
2495 return 0;
2496 }
2497
2498 #ifdef USE_DNSTAP
dtio_tap_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (ev),void * ATTR_UNUSED (arg))2499 void dtio_tap_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev),
2500 void* ATTR_UNUSED(arg))
2501 {
2502 log_assert(0);
2503 }
2504 #endif
2505
2506 #ifdef USE_DNSTAP
dtio_mainfdcallback(int ATTR_UNUSED (fd),short ATTR_UNUSED (ev),void * ATTR_UNUSED (arg))2507 void dtio_mainfdcallback(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev),
2508 void* ATTR_UNUSED(arg))
2509 {
2510 log_assert(0);
2511 }
2512 #endif
2513
2514 #ifdef HAVE_NGTCP2
doq_client_event_cb(int ATTR_UNUSED (fd),short ATTR_UNUSED (ev),void * ATTR_UNUSED (arg))2515 void doq_client_event_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev),
2516 void* ATTR_UNUSED(arg))
2517 {
2518 log_assert(0);
2519 }
2520 #endif
2521
2522 #ifdef HAVE_NGTCP2
doq_client_timer_cb(int ATTR_UNUSED (fd),short ATTR_UNUSED (ev),void * ATTR_UNUSED (arg))2523 void doq_client_timer_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev),
2524 void* ATTR_UNUSED(arg))
2525 {
2526 log_assert(0);
2527 }
2528 #endif
2529