2 * daemon/worker.c - worker that handles a pending list of requests.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file implements the worker that handles callbacks on events, for
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/mesh.h"
62 #include "services/localzone.h"
63 #include "util/data/msgparse.h"
64 #include "util/data/msgencode.h"
65 #include "util/data/dname.h"
66 #include "util/fptr_wlist.h"
67 #include "util/tube.h"
68 #include "iterator/iter_fwd.h"
69 #include "iterator/iter_hints.h"
70 #include "validator/autotrust.h"
71 #include "validator/val_anchor.h"
72 #include "respip/respip.h"
73 #include "libunbound/context.h"
74 #include "libunbound/libworker.h"
75 #include "sldns/sbuffer.h"
76 #include "sldns/wire2str.h"
77 #include "util/shm_side/shm_main.h"
78 #include "dnscrypt/dnscrypt.h"
80 #ifdef HAVE_SYS_TYPES_H
81 # include <sys/types.h>
88 #include "winrc/win_svc.h"
91 /** Size of an UDP datagram */
92 #define NORMAL_UDP_SIZE 512 /* bytes */
93 /** ratelimit for error responses */
94 #define ERROR_RATELIMIT 100 /* qps */
97 * seconds to add to prefetch leeway. This is a TTL that expires old rrsets
98 * earlier than they should in order to put the new update into the cache.
99 * This additional value is to make sure that if not all TTLs are equal in
100 * the message to be updated(and replaced), that rrsets with up to this much
101 * extra TTL are also replaced. This means that the resulting new message
102 * will have (most likely) this TTL at least, avoiding very small 'split
103 * second' TTLs due to operators choosing relative primes for TTLs (or so).
104 * Also has to be at least one to break ties (and overwrite cached entry).
106 #define PREFETCH_EXPIRY_ADD 60
108 /** Report on memory usage by this thread and global */
110 worker_mem_report(struct worker* ATTR_UNUSED(worker),
111 struct serviced_query* ATTR_UNUSED(cur_serv))
113 #ifdef UNBOUND_ALLOC_STATS
114 /* measure memory leakage */
115 extern size_t unbound_mem_alloc, unbound_mem_freed;
116 /* debug func in validator module */
117 size_t total, front, back, mesh, msg, rrset, infra, ac, superac;
118 size_t me, iter, val, anch;
122 #endif /* CLIENT_SUBNET */
123 if(verbosity < VERB_ALGO)
125 front = listen_get_mem(worker->front);
126 back = outnet_get_mem(worker->back);
127 msg = slabhash_get_mem(worker->env.msg_cache);
128 rrset = slabhash_get_mem(&worker->env.rrset_cache->table);
129 infra = infra_get_mem(worker->env.infra_cache);
130 mesh = mesh_get_mem(worker->env.mesh);
131 ac = alloc_get_mem(&worker->alloc);
132 superac = alloc_get_mem(&worker->daemon->superalloc);
133 anch = anchors_get_mem(worker->env.anchors);
136 for(i=0; i<worker->env.mesh->mods.num; i++) {
137 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
138 mods.mod[i]->get_mem));
139 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
140 val += (*worker->env.mesh->mods.mod[i]->get_mem)
143 else if(strcmp(worker->env.mesh->mods.mod[i]->name,
145 subnet += (*worker->env.mesh->mods.mod[i]->get_mem)
147 #endif /* CLIENT_SUBNET */
148 else iter += (*worker->env.mesh->mods.mod[i]->get_mem)
151 me = sizeof(*worker) + sizeof(*worker->base) + sizeof(*worker->comsig)
152 + comm_point_get_mem(worker->cmd_com)
153 + sizeof(worker->rndstate)
154 + regional_get_mem(worker->scratchpad)
155 + sizeof(*worker->env.scratch_buffer)
156 + sldns_buffer_capacity(worker->env.scratch_buffer)
157 + forwards_get_mem(worker->env.fwds)
158 + hints_get_mem(worker->env.hints);
159 if(worker->thread_num == 0)
160 me += acl_list_get_mem(worker->daemon->acl);
162 me += serviced_get_mem(cur_serv);
164 total = front+back+mesh+msg+rrset+infra+iter+val+ac+superac+me;
167 log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
168 "rrset=%u infra=%u iter=%u val=%u subnet=%u anchors=%u "
169 "alloccache=%u globalalloccache=%u me=%u",
170 (unsigned)total, (unsigned)front, (unsigned)back,
171 (unsigned)mesh, (unsigned)msg, (unsigned)rrset, (unsigned)infra,
172 (unsigned)iter, (unsigned)val,
173 (unsigned)subnet, (unsigned)anch, (unsigned)ac,
174 (unsigned)superac, (unsigned)me);
175 #else /* no CLIENT_SUBNET */
176 log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
177 "rrset=%u infra=%u iter=%u val=%u anchors=%u "
178 "alloccache=%u globalalloccache=%u me=%u",
179 (unsigned)total, (unsigned)front, (unsigned)back,
180 (unsigned)mesh, (unsigned)msg, (unsigned)rrset,
181 (unsigned)infra, (unsigned)iter, (unsigned)val, (unsigned)anch,
182 (unsigned)ac, (unsigned)superac, (unsigned)me);
183 #endif /* CLIENT_SUBNET */
184 log_info("Total heap memory estimate: %u total-alloc: %u "
185 "total-free: %u", (unsigned)total,
186 (unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
187 #else /* no UNBOUND_ALLOC_STATS */
191 #endif /* CLIENT_SUBNET */
193 if(verbosity < VERB_QUERY)
195 for(i=0; i<worker->env.mesh->mods.num; i++) {
196 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
197 mods.mod[i]->get_mem));
198 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
199 val += (*worker->env.mesh->mods.mod[i]->get_mem)
202 else if(strcmp(worker->env.mesh->mods.mod[i]->name,
204 subnet += (*worker->env.mesh->mods.mod[i]->get_mem)
206 #endif /* CLIENT_SUBNET */
209 verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u "
211 (unsigned)slabhash_get_mem(worker->env.msg_cache),
212 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
213 (unsigned)infra_get_mem(worker->env.infra_cache),
214 (unsigned)val, (unsigned)subnet);
215 #else /* no CLIENT_SUBNET */
216 verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u",
217 (unsigned)slabhash_get_mem(worker->env.msg_cache),
218 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
219 (unsigned)infra_get_mem(worker->env.infra_cache),
221 #endif /* CLIENT_SUBNET */
222 #endif /* UNBOUND_ALLOC_STATS */
226 worker_send_cmd(struct worker* worker, enum worker_commands cmd)
228 uint32_t c = (uint32_t)htonl(cmd);
229 if(!tube_write_msg(worker->cmd, (uint8_t*)&c, sizeof(c), 0)) {
230 log_err("worker send cmd %d failed", (int)cmd);
235 worker_handle_reply(struct comm_point* c, void* arg, int error,
236 struct comm_reply* reply_info)
238 struct module_qstate* q = (struct module_qstate*)arg;
239 struct worker* worker = q->env->worker;
240 struct outbound_entry e;
245 mesh_report_reply(worker->env.mesh, &e, reply_info, error);
246 worker_mem_report(worker, NULL);
250 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
251 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
253 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
254 /* error becomes timeout for the module as if this reply
256 mesh_report_reply(worker->env.mesh, &e, reply_info,
258 worker_mem_report(worker, NULL);
261 mesh_report_reply(worker->env.mesh, &e, reply_info, NETEVENT_NOERROR);
262 worker_mem_report(worker, NULL);
267 worker_handle_service_reply(struct comm_point* c, void* arg, int error,
268 struct comm_reply* reply_info)
270 struct outbound_entry* e = (struct outbound_entry*)arg;
271 struct worker* worker = e->qstate->env->worker;
272 struct serviced_query *sq = e->qsent;
274 verbose(VERB_ALGO, "worker svcd callback for qstate %p", e->qstate);
276 mesh_report_reply(worker->env.mesh, e, reply_info, error);
277 worker_mem_report(worker, sq);
281 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
282 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
284 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
285 /* error becomes timeout for the module as if this reply
287 verbose(VERB_ALGO, "worker: bad reply handled as timeout");
288 mesh_report_reply(worker->env.mesh, e, reply_info,
290 worker_mem_report(worker, sq);
293 mesh_report_reply(worker->env.mesh, e, reply_info, NETEVENT_NOERROR);
294 worker_mem_report(worker, sq);
298 /** ratelimit error replies
299 * @param worker: the worker struct with ratelimit counter
300 * @param err: error code that would be wanted.
301 * @return value of err if okay, or -1 if it should be discarded instead.
304 worker_err_ratelimit(struct worker* worker, int err)
306 if(worker->err_limit_time == *worker->env.now) {
307 /* see if limit is exceeded for this second */
308 if(worker->err_limit_count++ > ERROR_RATELIMIT)
311 /* new second, new limits */
312 worker->err_limit_time = *worker->env.now;
313 worker->err_limit_count = 1;
318 /** check request sanity.
319 * @param pkt: the wire packet to examine for sanity.
320 * @param worker: parameters for checking.
321 * @return error code, 0 OK, or -1 discard.
324 worker_check_request(sldns_buffer* pkt, struct worker* worker)
326 if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
327 verbose(VERB_QUERY, "request too short, discarded");
330 if(sldns_buffer_limit(pkt) > NORMAL_UDP_SIZE &&
331 worker->daemon->cfg->harden_large_queries) {
332 verbose(VERB_QUERY, "request too large, discarded");
335 if(LDNS_QR_WIRE(sldns_buffer_begin(pkt))) {
336 verbose(VERB_QUERY, "request has QR bit on, discarded");
339 if(LDNS_TC_WIRE(sldns_buffer_begin(pkt))) {
340 LDNS_TC_CLR(sldns_buffer_begin(pkt));
341 verbose(VERB_QUERY, "request bad, has TC bit on");
342 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
344 if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY) {
345 verbose(VERB_QUERY, "request unknown opcode %d",
346 LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)));
347 return worker_err_ratelimit(worker, LDNS_RCODE_NOTIMPL);
349 if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1) {
350 verbose(VERB_QUERY, "request wrong nr qd=%d",
351 LDNS_QDCOUNT(sldns_buffer_begin(pkt)));
352 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
354 if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0) {
355 verbose(VERB_QUERY, "request wrong nr an=%d",
356 LDNS_ANCOUNT(sldns_buffer_begin(pkt)));
357 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
359 if(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) {
360 verbose(VERB_QUERY, "request wrong nr ns=%d",
361 LDNS_NSCOUNT(sldns_buffer_begin(pkt)));
362 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
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 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
373 worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), uint8_t* msg,
374 size_t len, int error, void* arg)
376 struct worker* worker = (struct worker*)arg;
377 enum worker_commands cmd;
378 if(error != NETEVENT_NOERROR) {
380 if(error == NETEVENT_CLOSED)
381 comm_base_exit(worker->base);
382 else log_info("control event: %d", error);
385 if(len != sizeof(uint32_t)) {
386 fatal_exit("bad control msg length %d", (int)len);
388 cmd = sldns_read_uint32(msg);
391 case worker_cmd_quit:
392 verbose(VERB_ALGO, "got control cmd quit");
393 comm_base_exit(worker->base);
395 case worker_cmd_stats:
396 verbose(VERB_ALGO, "got control cmd stats");
397 server_stats_reply(worker, 1);
399 case worker_cmd_stats_noreset:
400 verbose(VERB_ALGO, "got control cmd stats_noreset");
401 server_stats_reply(worker, 0);
403 case worker_cmd_remote:
404 verbose(VERB_ALGO, "got control cmd remote");
405 daemon_remote_exec(worker);
408 log_err("bad command %d", (int)cmd);
413 /** check if a delegation is secure */
414 static enum sec_status
415 check_delegation_secure(struct reply_info *rep)
417 /* return smallest security status */
419 enum sec_status sec = sec_status_secure;
421 size_t num = rep->an_numrrsets + rep->ns_numrrsets;
422 /* check if answer and authority are OK */
423 for(i=0; i<num; i++) {
424 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
429 /* in additional, only unchecked triggers revalidation */
430 for(i=num; i<rep->rrset_count; i++) {
431 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
433 if(s == sec_status_unchecked)
439 /** remove nonsecure from a delegation referral additional section */
441 deleg_remove_nonsecure_additional(struct reply_info* rep)
443 /* we can simply edit it, since we are working in the scratch region */
447 for(i = rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
448 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
450 if(s != sec_status_secure) {
451 memmove(rep->rrsets+i, rep->rrsets+i+1,
452 sizeof(struct ub_packed_rrset_key*)*
453 (rep->rrset_count - i - 1));
461 /** answer nonrecursive query from the cache */
463 answer_norec_from_cache(struct worker* worker, struct query_info* qinfo,
464 uint16_t id, uint16_t flags, struct comm_reply* repinfo,
465 struct edns_data* edns)
467 /* for a nonrecursive query return either:
468 * o an error (servfail; we try to avoid this)
469 * o a delegation (closest we have; this routine tries that)
470 * o the answer (checked by answer_from_cache)
472 * So, grab a delegation from the rrset cache.
473 * Then check if it needs validation, if so, this routine fails,
474 * so that iterator can prime and validator can verify rrsets.
476 uint16_t udpsize = edns->udp_size;
478 time_t timenow = *worker->env.now;
479 int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
480 && worker->env.need_to_validate;
481 struct dns_msg *msg = NULL;
484 dp = dns_cache_find_delegation(&worker->env, qinfo->qname,
485 qinfo->qname_len, qinfo->qtype, qinfo->qclass,
486 worker->scratchpad, &msg, timenow);
487 if(!dp) { /* no delegation, need to reprime */
490 /* In case we have a local alias, copy it into the delegation message.
491 * Shallow copy should be fine, as we'll be done with msg in this
493 msg->qinfo.local_alias = qinfo->local_alias;
495 switch(check_delegation_secure(msg->rep)) {
496 case sec_status_unchecked:
497 /* some rrsets have not been verified yet, go and
498 * let validator do that */
500 case sec_status_bogus:
501 /* some rrsets are bogus, reply servfail */
502 edns->edns_version = EDNS_ADVERTISED_VERSION;
503 edns->udp_size = EDNS_ADVERTISED_SIZE;
505 edns->bits &= EDNS_DO;
506 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL,
507 msg->rep, LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
509 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
510 &msg->qinfo, id, flags, edns);
511 if(worker->stats.extended) {
512 worker->stats.ans_bogus++;
513 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
516 case sec_status_secure:
517 /* all rrsets are secure */
518 /* remove non-secure rrsets from the add. section*/
519 if(worker->env.cfg->val_clean_additional)
520 deleg_remove_nonsecure_additional(msg->rep);
523 case sec_status_indeterminate:
524 case sec_status_insecure:
531 /* return this delegation from the cache */
532 edns->edns_version = EDNS_ADVERTISED_VERSION;
533 edns->udp_size = EDNS_ADVERTISED_SIZE;
535 edns->bits &= EDNS_DO;
536 if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, msg->rep,
537 (int)(flags&LDNS_RCODE_MASK), edns, worker->scratchpad))
539 msg->rep->flags |= BIT_QR|BIT_RA;
540 if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags,
541 repinfo->c->buffer, 0, 1, worker->scratchpad,
542 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
543 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
544 LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
545 edns->opt_list = NULL;
546 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
547 &msg->qinfo, id, flags, edns);
549 if(worker->stats.extended) {
550 if(secure) worker->stats.ans_secure++;
551 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
556 /** Apply, if applicable, a response IP action to a cached answer.
557 * If the answer is rewritten as a result of an action, '*encode_repp' will
558 * point to the reply info containing the modified answer. '*encode_repp' will
559 * be intact otherwise.
560 * It returns 1 on success, 0 otherwise. */
562 apply_respip_action(struct worker* worker, const struct query_info* qinfo,
563 struct respip_client_info* cinfo, struct reply_info* rep,
564 struct comm_reply* repinfo, struct ub_packed_rrset_key** alias_rrset,
565 struct reply_info** encode_repp)
567 struct respip_action_info actinfo = {respip_none, NULL};
569 if(qinfo->qtype != LDNS_RR_TYPE_A &&
570 qinfo->qtype != LDNS_RR_TYPE_AAAA &&
571 qinfo->qtype != LDNS_RR_TYPE_ANY)
574 if(!respip_rewrite_reply(qinfo, cinfo, rep, encode_repp, &actinfo,
575 alias_rrset, 0, worker->scratchpad))
578 /* xxx_deny actions mean dropping the reply, unless the original reply
579 * was redirected to response-ip data. */
580 if((actinfo.action == respip_deny ||
581 actinfo.action == respip_inform_deny) &&
585 /* If address info is returned, it means the action should be an
586 * 'inform' variant and the information should be logged. */
587 if(actinfo.addrinfo) {
588 respip_inform_print(actinfo.addrinfo, qinfo->qname,
589 qinfo->qtype, qinfo->qclass, qinfo->local_alias,
596 /** answer query from the cache.
597 * Normally, the answer message will be built in repinfo->c->buffer; if the
598 * answer is supposed to be suppressed or the answer is supposed to be an
599 * incomplete CNAME chain, the buffer is explicitly cleared to signal the
600 * caller as such. In the latter case *partial_rep will point to the incomplete
601 * reply, and this function is (possibly) supposed to be called again with that
602 * *partial_rep value to complete the chain. In addition, if the query should
603 * be completely dropped, '*need_drop' will be set to 1. */
605 answer_from_cache(struct worker* worker, struct query_info* qinfo,
606 struct respip_client_info* cinfo, int* need_drop,
607 struct ub_packed_rrset_key** alias_rrset,
608 struct reply_info** partial_repp,
609 struct reply_info* rep, uint16_t id, uint16_t flags,
610 struct comm_reply* repinfo, struct edns_data* edns)
612 time_t timenow = *worker->env.now;
613 uint16_t udpsize = edns->udp_size;
614 struct reply_info* encode_rep = rep;
615 struct reply_info* partial_rep = *partial_repp;
617 int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
618 && worker->env.need_to_validate;
619 *partial_repp = NULL; /* avoid accidental further pass */
620 if(worker->env.cfg->serve_expired) {
621 /* always lock rrsets, rep->ttl is ignored */
622 if(!rrset_array_lock(rep->ref, rep->rrset_count, 0))
624 /* below, rrsets with ttl before timenow become TTL 0 in
626 /* This response was served with zero TTL */
627 if (timenow >= rep->ttl) {
628 worker->stats.zero_ttl_responses++;
631 /* see if it is possible */
632 if(rep->ttl < timenow) {
633 /* the rrsets may have been updated in the meantime.
634 * we will refetch the message format from the
635 * authoritative server
639 if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
641 /* locked and ids and ttls are OK. */
643 /* check CNAME chain (if any) */
644 if(rep->an_numrrsets > 0 && (rep->rrsets[0]->rk.type ==
645 htons(LDNS_RR_TYPE_CNAME) || rep->rrsets[0]->rk.type ==
646 htons(LDNS_RR_TYPE_DNAME))) {
647 if(!reply_check_cname_chain(qinfo, rep)) {
648 /* cname chain invalid, redo iterator steps */
649 verbose(VERB_ALGO, "Cache reply: cname chain broken");
651 rrset_array_unlock_touch(worker->env.rrset_cache,
652 worker->scratchpad, rep->ref, rep->rrset_count);
656 /* check security status of the cached answer */
657 if( rep->security == sec_status_bogus && must_validate) {
659 edns->edns_version = EDNS_ADVERTISED_VERSION;
660 edns->udp_size = EDNS_ADVERTISED_SIZE;
662 edns->bits &= EDNS_DO;
663 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, rep,
664 LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
666 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
667 qinfo, id, flags, edns);
668 rrset_array_unlock_touch(worker->env.rrset_cache,
669 worker->scratchpad, rep->ref, rep->rrset_count);
670 if(worker->stats.extended) {
671 worker->stats.ans_bogus ++;
672 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL] ++;
675 } else if( rep->security == sec_status_unchecked && must_validate) {
676 verbose(VERB_ALGO, "Cache reply: unchecked entry needs "
678 goto bail_out; /* need to validate cache entry first */
679 } else if(rep->security == sec_status_secure) {
680 if(reply_all_rrsets_secure(rep))
684 verbose(VERB_ALGO, "Cache reply: secure entry"
686 goto bail_out; /* rrset changed, re-verify */
692 edns->edns_version = EDNS_ADVERTISED_VERSION;
693 edns->udp_size = EDNS_ADVERTISED_SIZE;
695 edns->bits &= EDNS_DO;
696 if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, rep,
697 (int)(flags&LDNS_RCODE_MASK), edns, worker->scratchpad))
699 *alias_rrset = NULL; /* avoid confusion if caller set it to non-NULL */
700 if(worker->daemon->use_response_ip && !partial_rep &&
701 !apply_respip_action(worker, qinfo, cinfo, rep, repinfo, alias_rrset,
704 } else if(partial_rep &&
705 !respip_merge_cname(partial_rep, qinfo, rep, cinfo,
706 must_validate, &encode_rep, worker->scratchpad)) {
709 if(encode_rep != rep)
710 secure = 0; /* if rewritten, it can't be considered "secure" */
711 if(!encode_rep || *alias_rrset) {
712 sldns_buffer_clear(repinfo->c->buffer);
713 sldns_buffer_flip(repinfo->c->buffer);
717 /* If a partial CNAME chain is found, we first need to
718 * make a copy of the reply in the scratchpad so we
719 * can release the locks and lookup the cache again. */
720 *partial_repp = reply_info_copy(encode_rep, NULL,
725 } else if(!reply_info_answer_encode(qinfo, encode_rep, id, flags,
726 repinfo->c->buffer, timenow, 1, worker->scratchpad,
727 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
728 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
729 LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
730 edns->opt_list = NULL;
731 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
732 qinfo, id, flags, edns);
734 /* cannot send the reply right now, because blocking network syscall
735 * is bad while holding locks. */
736 rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
737 rep->ref, rep->rrset_count);
738 if(worker->stats.extended) {
739 if(secure) worker->stats.ans_secure++;
740 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
742 /* go and return this buffer to the client */
746 /** Reply to client and perform prefetch to keep cache up to date.
747 * If the buffer for the reply is empty, it indicates that only prefetch is
748 * necessary and the reply should be suppressed (because it's dropped or
749 * being deferred). */
751 reply_and_prefetch(struct worker* worker, struct query_info* qinfo,
752 uint16_t flags, struct comm_reply* repinfo, time_t leeway)
754 /* first send answer to client to keep its latency
755 * as small as a cachereply */
756 if(sldns_buffer_limit(repinfo->c->buffer) != 0)
757 comm_point_send_reply(repinfo);
758 server_stats_prefetch(&worker->stats, worker);
760 /* create the prefetch in the mesh as a normal lookup without
761 * client addrs waiting, which has the cache blacklisted (to bypass
762 * the cache and go to the network for the data). */
763 /* this (potentially) runs the mesh for the new query */
764 mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
765 PREFETCH_EXPIRY_ADD);
769 * Fill CH class answer into buffer. Keeps query.
771 * @param str: string to put into text record (<255).
772 * array of strings, every string becomes a text record.
773 * @param num: number of strings in array.
774 * @param edns: edns reply information.
775 * @param worker: worker with scratch region.
778 chaos_replystr(sldns_buffer* pkt, char** str, int num, struct edns_data* edns,
779 struct worker* worker)
782 unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
783 unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
784 sldns_buffer_clear(pkt);
785 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
786 sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
787 if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
788 if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
789 sldns_buffer_write_u16(pkt, 1); /* qdcount */
790 sldns_buffer_write_u16(pkt, (uint16_t)num); /* ancount */
791 sldns_buffer_write_u16(pkt, 0); /* nscount */
792 sldns_buffer_write_u16(pkt, 0); /* arcount */
793 (void)query_dname_len(pkt); /* skip qname */
794 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
795 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
796 for(i=0; i<num; i++) {
797 size_t len = strlen(str[i]);
798 if(len>255) len=255; /* cap size of TXT record */
799 sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
800 sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
801 sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
802 sldns_buffer_write_u32(pkt, 0); /* TTL */
803 sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
804 sldns_buffer_write_u8(pkt, len);
805 sldns_buffer_write(pkt, str[i], len);
807 sldns_buffer_flip(pkt);
808 edns->edns_version = EDNS_ADVERTISED_VERSION;
809 edns->udp_size = EDNS_ADVERTISED_SIZE;
810 edns->bits &= EDNS_DO;
811 if(!inplace_cb_reply_local_call(&worker->env, NULL, NULL, NULL,
812 LDNS_RCODE_NOERROR, edns, worker->scratchpad))
813 edns->opt_list = NULL;
814 if(sldns_buffer_capacity(pkt) >=
815 sldns_buffer_limit(pkt)+calc_edns_field_size(edns))
816 attach_edns_record(pkt, edns);
819 /** Reply with one string */
821 chaos_replyonestr(sldns_buffer* pkt, const char* str, struct edns_data* edns,
822 struct worker* worker)
824 chaos_replystr(pkt, (char**)&str, 1, edns, worker);
828 * Create CH class trustanchor answer.
830 * @param edns: edns reply information.
831 * @param w: worker with scratch region.
834 chaos_trustanchor(sldns_buffer* pkt, struct edns_data* edns, struct worker* w)
836 #define TA_RESPONSE_MAX_TXT 16 /* max number of TXT records */
837 #define TA_RESPONSE_MAX_TAGS 32 /* max number of tags printed per zone */
838 char* str_array[TA_RESPONSE_MAX_TXT];
839 uint16_t tags[TA_RESPONSE_MAX_TAGS];
841 struct trust_anchor* ta;
843 if(!w->env.need_to_validate) {
844 /* no validator module, reply no trustanchors */
845 chaos_replystr(pkt, NULL, 0, edns, w);
849 /* fill the string with contents */
850 lock_basic_lock(&w->env.anchors->lock);
851 RBTREE_FOR(ta, struct trust_anchor*, w->env.anchors->tree) {
853 size_t i, numtag, str_len = 255;
854 if(num == TA_RESPONSE_MAX_TXT) continue;
855 str = (char*)regional_alloc(w->scratchpad, str_len);
857 lock_basic_lock(&ta->lock);
858 numtag = anchor_list_keytags(ta, tags, TA_RESPONSE_MAX_TAGS);
860 /* empty, insecure point */
861 lock_basic_unlock(&ta->lock);
864 str_array[num] = str;
867 /* spool name of anchor */
868 (void)sldns_wire2str_dname_buf(ta->name, ta->namelen, str, str_len);
869 str_len -= strlen(str); str += strlen(str);
871 for(i=0; i<numtag; i++) {
872 snprintf(str, str_len, " %u", (unsigned)tags[i]);
873 str_len -= strlen(str); str += strlen(str);
875 lock_basic_unlock(&ta->lock);
877 lock_basic_unlock(&w->env.anchors->lock);
879 chaos_replystr(pkt, str_array, num, edns, w);
880 regional_free_all(w->scratchpad);
884 * Answer CH class queries.
886 * @param qinfo: query info. Pointer into packet buffer.
887 * @param edns: edns info from query.
888 * @param pkt: packet buffer.
889 * @return: true if a reply is to be sent.
892 answer_chaos(struct worker* w, struct query_info* qinfo,
893 struct edns_data* edns, sldns_buffer* pkt)
895 struct config_file* cfg = w->env.cfg;
896 if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
898 if(query_dname_compare(qinfo->qname,
899 (uint8_t*)"\002id\006server") == 0 ||
900 query_dname_compare(qinfo->qname,
901 (uint8_t*)"\010hostname\004bind") == 0)
903 if(cfg->hide_identity)
905 if(cfg->identity==NULL || cfg->identity[0]==0) {
906 char buf[MAXHOSTNAMELEN+1];
907 if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
908 buf[MAXHOSTNAMELEN] = 0;
909 chaos_replyonestr(pkt, buf, edns, w);
911 log_err("gethostname: %s", strerror(errno));
912 chaos_replyonestr(pkt, "no hostname", edns, w);
915 else chaos_replyonestr(pkt, cfg->identity, edns, w);
918 if(query_dname_compare(qinfo->qname,
919 (uint8_t*)"\007version\006server") == 0 ||
920 query_dname_compare(qinfo->qname,
921 (uint8_t*)"\007version\004bind") == 0)
923 if(cfg->hide_version)
925 if(cfg->version==NULL || cfg->version[0]==0)
926 chaos_replyonestr(pkt, PACKAGE_STRING, edns, w);
927 else chaos_replyonestr(pkt, cfg->version, edns, w);
930 if(query_dname_compare(qinfo->qname,
931 (uint8_t*)"\013trustanchor\007unbound") == 0)
933 if(cfg->hide_trustanchor)
935 chaos_trustanchor(pkt, edns, w);
943 deny_refuse(struct comm_point* c, enum acl_access acl,
944 enum acl_access deny, enum acl_access refuse,
945 struct worker* worker, struct comm_reply* repinfo)
948 comm_point_drop_reply(repinfo);
949 if(worker->stats.extended)
950 worker->stats.unwanted_queries++;
952 } else if(acl == refuse) {
953 log_addr(VERB_ALGO, "refused query from",
954 &repinfo->addr, repinfo->addrlen);
955 log_buf(VERB_ALGO, "refuse", c->buffer);
956 if(worker->stats.extended)
957 worker->stats.unwanted_queries++;
958 if(worker_check_request(c->buffer, worker) == -1) {
959 comm_point_drop_reply(repinfo);
960 return 0; /* discard this */
962 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
963 sldns_buffer_write_at(c->buffer, 4,
964 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
965 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
966 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
968 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
969 sldns_buffer_flip(c->buffer);
977 deny_refuse_all(struct comm_point* c, enum acl_access acl,
978 struct worker* worker, struct comm_reply* repinfo)
980 return deny_refuse(c, acl, acl_deny, acl_refuse, worker, repinfo);
984 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
985 struct worker* worker, struct comm_reply* repinfo)
987 return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, worker, repinfo);
991 worker_handle_request(struct comm_point* c, void* arg, int error,
992 struct comm_reply* repinfo)
994 struct worker* worker = (struct worker*)arg;
997 struct lruhash_entry* e;
998 struct query_info qinfo;
999 struct edns_data edns;
1000 enum acl_access acl;
1001 struct acl_addr* acladdr;
1004 /* We might have to chase a CNAME chain internally, in which case
1005 * we'll have up to two replies and combine them to build a complete
1006 * answer. These variables control this case. */
1007 struct ub_packed_rrset_key* alias_rrset = NULL;
1008 struct reply_info* partial_rep = NULL;
1009 struct query_info* lookup_qinfo = &qinfo;
1010 struct query_info qinfo_tmp; /* placeholdoer for lookup_qinfo */
1011 struct respip_client_info* cinfo = NULL, cinfo_tmp;
1013 if(error != NETEVENT_NOERROR) {
1014 /* some bad tcp query DNS formats give these error calls */
1015 verbose(VERB_ALGO, "handle request called with err=%d", error);
1019 repinfo->max_udp_size = worker->daemon->cfg->max_udp_size;
1020 if(!dnsc_handle_curved_request(worker->daemon->dnscenv, repinfo)) {
1021 worker->stats.num_query_dnscrypt_crypted_malformed++;
1024 if(c->dnscrypt && !repinfo->is_dnscrypted) {
1025 char buf[LDNS_MAX_DOMAINLEN+1];
1026 /* Check if this is unencrypted and asking for certs */
1027 if(worker_check_request(c->buffer, worker) != 0) {
1029 "dnscrypt: worker check request: bad query.");
1030 log_addr(VERB_CLIENT,"from",&repinfo->addr,
1032 comm_point_drop_reply(repinfo);
1035 if(!query_info_parse(&qinfo, c->buffer)) {
1037 "dnscrypt: worker parse request: formerror.");
1038 log_addr(VERB_CLIENT, "from", &repinfo->addr,
1040 comm_point_drop_reply(repinfo);
1043 dname_str(qinfo.qname, buf);
1044 if(!(qinfo.qtype == LDNS_RR_TYPE_TXT &&
1046 worker->daemon->dnscenv->provider_name) == 0)) {
1048 "dnscrypt: not TXT %s. Receive: %s %s",
1049 worker->daemon->dnscenv->provider_name,
1050 sldns_rr_descript(qinfo.qtype)->_name,
1052 comm_point_drop_reply(repinfo);
1053 worker->stats.num_query_dnscrypt_cleartext++;
1056 worker->stats.num_query_dnscrypt_cert++;
1057 sldns_buffer_rewind(c->buffer);
1058 } else if(c->dnscrypt && repinfo->is_dnscrypted) {
1059 worker->stats.num_query_dnscrypt_crypted++;
1063 if(worker->dtenv.log_client_query_messages)
1064 dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
1067 acladdr = acl_addr_lookup(worker->daemon->acl, &repinfo->addr,
1069 acl = acl_get_control(acladdr);
1070 if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
1076 if((ret=worker_check_request(c->buffer, worker)) != 0) {
1077 verbose(VERB_ALGO, "worker check request: bad query.");
1078 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1080 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1081 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
1084 comm_point_drop_reply(repinfo);
1088 worker->stats.num_queries++;
1090 /* check if this query should be dropped based on source ip rate limiting */
1091 if(!infra_ip_ratelimit_inc(worker->env.infra_cache, repinfo,
1092 *worker->env.now)) {
1093 /* See if we are passed through with slip factor */
1094 if(worker->env.cfg->ip_ratelimit_factor != 0 &&
1095 ub_random_max(worker->env.rnd,
1096 worker->env.cfg->ip_ratelimit_factor) == 1) {
1099 addr_to_str(&repinfo->addr, repinfo->addrlen,
1100 addrbuf, sizeof(addrbuf));
1101 verbose(VERB_OPS, "ip_ratelimit allowed through for ip address %s ",
1104 worker->stats.num_queries_ip_ratelimited++;
1105 comm_point_drop_reply(repinfo);
1110 /* see if query is in the cache */
1111 if(!query_info_parse(&qinfo, c->buffer)) {
1112 verbose(VERB_ALGO, "worker parse request: formerror.");
1113 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1114 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1115 comm_point_drop_reply(repinfo);
1118 sldns_buffer_rewind(c->buffer);
1119 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1120 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1121 LDNS_RCODE_FORMERR);
1122 server_stats_insrcode(&worker->stats, c->buffer);
1125 if(worker->env.cfg->log_queries) {
1127 addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
1128 log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
1130 if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
1131 qinfo.qtype == LDNS_RR_TYPE_IXFR) {
1132 verbose(VERB_ALGO, "worker request: refused zone transfer.");
1133 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1134 sldns_buffer_rewind(c->buffer);
1135 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1136 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1137 LDNS_RCODE_REFUSED);
1138 if(worker->stats.extended) {
1139 worker->stats.qtype[qinfo.qtype]++;
1140 server_stats_insrcode(&worker->stats, c->buffer);
1144 if(qinfo.qtype == LDNS_RR_TYPE_OPT ||
1145 qinfo.qtype == LDNS_RR_TYPE_TSIG ||
1146 qinfo.qtype == LDNS_RR_TYPE_TKEY ||
1147 qinfo.qtype == LDNS_RR_TYPE_MAILA ||
1148 qinfo.qtype == LDNS_RR_TYPE_MAILB ||
1149 (qinfo.qtype >= 128 && qinfo.qtype <= 248)) {
1150 verbose(VERB_ALGO, "worker request: formerror for meta-type.");
1151 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1152 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1153 comm_point_drop_reply(repinfo);
1156 sldns_buffer_rewind(c->buffer);
1157 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1158 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1159 LDNS_RCODE_FORMERR);
1160 if(worker->stats.extended) {
1161 worker->stats.qtype[qinfo.qtype]++;
1162 server_stats_insrcode(&worker->stats, c->buffer);
1166 if((ret=parse_edns_from_pkt(c->buffer, &edns, worker->scratchpad)) != 0) {
1167 struct edns_data reply_edns;
1168 verbose(VERB_ALGO, "worker parse edns: formerror.");
1169 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1170 memset(&reply_edns, 0, sizeof(reply_edns));
1171 reply_edns.edns_present = 1;
1172 reply_edns.udp_size = EDNS_ADVERTISED_SIZE;
1173 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
1174 error_encode(c->buffer, ret, &qinfo,
1175 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1176 sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
1177 regional_free_all(worker->scratchpad);
1178 server_stats_insrcode(&worker->stats, c->buffer);
1181 if(edns.edns_present && edns.edns_version != 0) {
1182 edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
1183 edns.edns_version = EDNS_ADVERTISED_VERSION;
1184 edns.udp_size = EDNS_ADVERTISED_SIZE;
1185 edns.bits &= EDNS_DO;
1186 edns.opt_list = NULL;
1187 verbose(VERB_ALGO, "query with bad edns version.");
1188 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1189 error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
1190 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1191 sldns_buffer_read_u16_at(c->buffer, 2), NULL);
1192 if(sldns_buffer_capacity(c->buffer) >=
1193 sldns_buffer_limit(c->buffer)+calc_edns_field_size(&edns))
1194 attach_edns_record(c->buffer, &edns);
1195 regional_free_all(worker->scratchpad);
1198 if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
1199 worker->daemon->cfg->harden_short_bufsize) {
1200 verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
1201 (int)edns.udp_size);
1202 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1203 edns.udp_size = NORMAL_UDP_SIZE;
1205 if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
1206 c->type == comm_udp) {
1208 "worker request: max UDP reply size modified"
1209 " (%d to max-udp-size)", (int)edns.udp_size);
1210 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1211 edns.udp_size = worker->daemon->cfg->max_udp_size;
1213 if(edns.udp_size < LDNS_HEADER_SIZE) {
1214 verbose(VERB_ALGO, "worker request: edns is too small.");
1215 log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
1216 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1217 LDNS_TC_SET(sldns_buffer_begin(c->buffer));
1218 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1219 LDNS_RCODE_SERVFAIL);
1220 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1221 sldns_buffer_write_at(c->buffer, 4,
1222 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1223 sldns_buffer_flip(c->buffer);
1224 regional_free_all(worker->scratchpad);
1227 if(worker->stats.extended)
1228 server_stats_insquery(&worker->stats, c, qinfo.qtype,
1229 qinfo.qclass, &edns, repinfo);
1230 if(c->type != comm_udp)
1231 edns.udp_size = 65535; /* max size for TCP replies */
1232 if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
1233 &edns, c->buffer)) {
1234 server_stats_insrcode(&worker->stats, c->buffer);
1235 regional_free_all(worker->scratchpad);
1238 if(local_zones_answer(worker->daemon->local_zones, &worker->env, &qinfo,
1239 &edns, c->buffer, worker->scratchpad, repinfo, acladdr->taglist,
1240 acladdr->taglen, acladdr->tag_actions,
1241 acladdr->tag_actions_size, acladdr->tag_datas,
1242 acladdr->tag_datas_size, worker->daemon->cfg->tagname,
1243 worker->daemon->cfg->num_tags, acladdr->view)) {
1244 regional_free_all(worker->scratchpad);
1245 if(sldns_buffer_limit(c->buffer) == 0) {
1246 comm_point_drop_reply(repinfo);
1249 server_stats_insrcode(&worker->stats, c->buffer);
1253 /* We've looked in our local zones. If the answer isn't there, we
1254 * might need to bail out based on ACLs now. */
1255 if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
1257 regional_free_all(worker->scratchpad);
1263 /* If this request does not have the recursion bit set, verify
1264 * ACLs allow the snooping. */
1265 if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
1266 acl != acl_allow_snoop ) {
1267 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
1268 sldns_buffer_write_at(c->buffer, 4,
1269 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1270 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1271 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1272 LDNS_RCODE_REFUSED);
1273 sldns_buffer_flip(c->buffer);
1274 regional_free_all(worker->scratchpad);
1275 server_stats_insrcode(&worker->stats, c->buffer);
1276 log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
1277 &repinfo->addr, repinfo->addrlen);
1281 /* If we've found a local alias, replace the qname with the alias
1282 * target before resolving it. */
1283 if(qinfo.local_alias) {
1284 struct ub_packed_rrset_key* rrset = qinfo.local_alias->rrset;
1285 struct packed_rrset_data* d = rrset->entry.data;
1287 /* Sanity check: our current implementation only supports
1288 * a single CNAME RRset as a local alias. */
1289 if(qinfo.local_alias->next ||
1290 rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) ||
1292 log_err("assumption failure: unexpected local alias");
1293 regional_free_all(worker->scratchpad);
1294 return 0; /* drop it */
1296 qinfo.qname = d->rr_data[0] + 2;
1297 qinfo.qname_len = d->rr_len[0] - 2;
1300 /* If we may apply IP-based actions to the answer, build the client
1301 * information. As this can be expensive, skip it if there is
1302 * absolutely no possibility of it. */
1303 if(worker->daemon->use_response_ip &&
1304 (qinfo.qtype == LDNS_RR_TYPE_A ||
1305 qinfo.qtype == LDNS_RR_TYPE_AAAA ||
1306 qinfo.qtype == LDNS_RR_TYPE_ANY)) {
1307 cinfo_tmp.taglist = acladdr->taglist;
1308 cinfo_tmp.taglen = acladdr->taglen;
1309 cinfo_tmp.tag_actions = acladdr->tag_actions;
1310 cinfo_tmp.tag_actions_size = acladdr->tag_actions_size;
1311 cinfo_tmp.tag_datas = acladdr->tag_datas;
1312 cinfo_tmp.tag_datas_size = acladdr->tag_datas_size;
1313 cinfo_tmp.view = acladdr->view;
1314 cinfo_tmp.respip_set = worker->daemon->respip_set;
1319 /* Lookup the cache. In case we chase an intermediate CNAME chain
1320 * this is a two-pass operation, and lookup_qinfo is different for
1321 * each pass. We should still pass the original qinfo to
1322 * answer_from_cache(), however, since it's used to build the reply. */
1323 if(!edns_bypass_cache_stage(edns.opt_list, &worker->env)) {
1324 h = query_info_hash(lookup_qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
1325 if((e=slabhash_lookup(worker->env.msg_cache, h, lookup_qinfo, 0))) {
1326 /* answer from cache - we have acquired a readlock on it */
1327 if(answer_from_cache(worker, &qinfo,
1328 cinfo, &need_drop, &alias_rrset, &partial_rep,
1329 (struct reply_info*)e->data,
1330 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1331 sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1333 /* prefetch it if the prefetch TTL expired.
1334 * Note that if there is more than one pass
1335 * its qname must be that used for cache
1337 if((worker->env.cfg->prefetch || worker->env.cfg->serve_expired)
1338 && *worker->env.now >=
1339 ((struct reply_info*)e->data)->prefetch_ttl) {
1340 time_t leeway = ((struct reply_info*)e->
1341 data)->ttl - *worker->env.now;
1342 if(((struct reply_info*)e->data)->ttl
1345 lock_rw_unlock(&e->lock);
1346 reply_and_prefetch(worker, lookup_qinfo,
1347 sldns_buffer_read_u16_at(c->buffer, 2),
1351 regional_free_all(worker->scratchpad);
1354 } else if(!partial_rep) {
1355 lock_rw_unlock(&e->lock);
1356 regional_free_all(worker->scratchpad);
1359 /* We've found a partial reply ending with an
1360 * alias. Replace the lookup qinfo for the
1361 * alias target and lookup the cache again to
1362 * (possibly) complete the reply. As we're
1363 * passing the "base" reply, there will be no
1364 * more alias chasing. */
1365 lock_rw_unlock(&e->lock);
1366 memset(&qinfo_tmp, 0, sizeof(qinfo_tmp));
1367 get_cname_target(alias_rrset, &qinfo_tmp.qname,
1368 &qinfo_tmp.qname_len);
1369 if(!qinfo_tmp.qname) {
1370 log_err("unexpected: invalid answer alias");
1371 regional_free_all(worker->scratchpad);
1372 return 0; /* drop query */
1374 qinfo_tmp.qtype = qinfo.qtype;
1375 qinfo_tmp.qclass = qinfo.qclass;
1376 lookup_qinfo = &qinfo_tmp;
1379 verbose(VERB_ALGO, "answer from the cache failed");
1380 lock_rw_unlock(&e->lock);
1382 if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
1383 if(answer_norec_from_cache(worker, &qinfo,
1384 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1385 sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1387 regional_free_all(worker->scratchpad);
1390 verbose(VERB_ALGO, "answer norec from cache -- "
1391 "need to validate or not primed");
1394 sldns_buffer_rewind(c->buffer);
1395 server_stats_querymiss(&worker->stats, worker);
1397 if(verbosity >= VERB_CLIENT) {
1398 if(c->type == comm_udp)
1399 log_addr(VERB_CLIENT, "udp request from",
1400 &repinfo->addr, repinfo->addrlen);
1401 else log_addr(VERB_CLIENT, "tcp request from",
1402 &repinfo->addr, repinfo->addrlen);
1405 /* grab a work request structure for this new request */
1406 mesh_new_client(worker->env.mesh, &qinfo, cinfo,
1407 sldns_buffer_read_u16_at(c->buffer, 2),
1408 &edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1409 regional_free_all(worker->scratchpad);
1410 worker_mem_report(worker, NULL);
1417 comm_point_drop_reply(repinfo);
1421 if(worker->dtenv.log_client_response_messages)
1422 dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1423 c->type, c->buffer);
1425 if(worker->env.cfg->log_replies)
1427 struct timeval tv = {0, 0};
1428 log_reply_info(0, &qinfo, &repinfo->addr, repinfo->addrlen,
1432 if(!dnsc_handle_uncurved_request(repinfo)) {
1440 worker_sighandler(int sig, void* arg)
1442 /* note that log, print, syscalls here give race conditions.
1443 * And cause hangups if the log-lock is held by the application. */
1444 struct worker* worker = (struct worker*)arg;
1448 comm_base_exit(worker->base);
1452 worker->need_to_exit = 1;
1453 comm_base_exit(worker->base);
1457 worker->need_to_exit = 1;
1458 comm_base_exit(worker->base);
1462 worker->need_to_exit = 1;
1463 comm_base_exit(worker->base);
1466 /* unknown signal, ignored */
1471 /** restart statistics timer for worker, if enabled */
1473 worker_restart_timer(struct worker* worker)
1475 if(worker->env.cfg->stat_interval > 0) {
1478 tv.tv_sec = worker->env.cfg->stat_interval;
1481 comm_timer_set(worker->stat_timer, &tv);
1485 void worker_stat_timer_cb(void* arg)
1487 struct worker* worker = (struct worker*)arg;
1488 server_stats_log(&worker->stats, worker, worker->thread_num);
1489 mesh_stats(worker->env.mesh, "mesh has");
1490 worker_mem_report(worker, NULL);
1491 /* SHM is enabled, process data to SHM */
1492 if (worker->daemon->cfg->shm_enable) {
1493 shm_main_run(worker);
1495 if(!worker->daemon->cfg->stat_cumulative) {
1496 worker_stats_clear(worker);
1498 /* start next timer */
1499 worker_restart_timer(worker);
1502 void worker_probe_timer_cb(void* arg)
1504 struct worker* worker = (struct worker*)arg;
1507 tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1511 comm_timer_set(worker->env.probe_timer, &tv);
1515 worker_create(struct daemon* daemon, int id, int* ports, int n)
1518 struct worker* worker = (struct worker*)calloc(1,
1519 sizeof(struct worker));
1522 worker->numports = n;
1523 worker->ports = (int*)memdup(ports, sizeof(int)*n);
1524 if(!worker->ports) {
1528 worker->daemon = daemon;
1529 worker->thread_num = id;
1530 if(!(worker->cmd = tube_create())) {
1531 free(worker->ports);
1535 /* create random state here to avoid locking trouble in RAND_bytes */
1536 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1537 (((unsigned int)worker->thread_num)<<17);
1538 /* shift thread_num so it does not match out pid bits */
1539 if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1541 log_err("could not init random numbers.");
1542 tube_delete(worker->cmd);
1543 free(worker->ports);
1549 if(daemon->cfg->dnstap) {
1550 log_assert(daemon->dtenv != NULL);
1551 memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1552 if(!dt_init(&worker->dtenv))
1553 fatal_exit("dt_init failed");
1560 worker_init(struct worker* worker, struct config_file *cfg,
1561 struct listen_port* ports, int do_sigs)
1564 struct dt_env* dtenv = &worker->dtenv;
1568 worker->need_to_exit = 0;
1569 worker->base = comm_base_create(do_sigs);
1571 log_err("could not create event handling base");
1572 worker_delete(worker);
1575 comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1576 &worker_start_accept, worker);
1579 ub_thread_sig_unblock(SIGHUP);
1581 ub_thread_sig_unblock(SIGINT);
1583 ub_thread_sig_unblock(SIGQUIT);
1585 ub_thread_sig_unblock(SIGTERM);
1586 #ifndef LIBEVENT_SIGNAL_PROBLEM
1587 worker->comsig = comm_signal_create(worker->base,
1588 worker_sighandler, worker);
1591 || !comm_signal_bind(worker->comsig, SIGHUP)
1594 || !comm_signal_bind(worker->comsig, SIGQUIT)
1596 || !comm_signal_bind(worker->comsig, SIGTERM)
1597 || !comm_signal_bind(worker->comsig, SIGINT)) {
1598 log_err("could not create signal handlers");
1599 worker_delete(worker);
1602 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1603 if(!daemon_remote_open_accept(worker->daemon->rc,
1604 worker->daemon->rc_ports, worker)) {
1605 worker_delete(worker);
1608 #ifdef UB_ON_WINDOWS
1609 wsvc_setup_worker(worker);
1610 #endif /* UB_ON_WINDOWS */
1611 } else { /* !do_sigs */
1612 worker->comsig = NULL;
1614 worker->front = listen_create(worker->base, ports,
1615 cfg->msg_buffer_size, (int)cfg->incoming_num_tcp,
1616 worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1618 if(!worker->front) {
1619 log_err("could not create listening sockets");
1620 worker_delete(worker);
1623 worker->back = outside_network_create(worker->base,
1624 cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports,
1625 cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
1626 cfg->do_tcp?cfg->outgoing_num_tcp:0,
1627 worker->daemon->env->infra_cache, worker->rndstate,
1628 cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1629 cfg->unwanted_threshold, cfg->outgoing_tcp_mss,
1630 &worker_alloc_cleanup, worker,
1631 cfg->do_udp, worker->daemon->connect_sslctx, cfg->delay_close,
1634 log_err("could not create outgoing sockets");
1635 worker_delete(worker);
1638 /* start listening to commands */
1639 if(!tube_setup_bg_listen(worker->cmd, worker->base,
1640 &worker_handle_control_cmd, worker)) {
1641 log_err("could not create control compt.");
1642 worker_delete(worker);
1645 worker->stat_timer = comm_timer_create(worker->base,
1646 worker_stat_timer_cb, worker);
1647 if(!worker->stat_timer) {
1648 log_err("could not create statistics timer");
1651 /* we use the msg_buffer_size as a good estimate for what the
1652 * user wants for memory usage sizes */
1653 worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1654 if(!worker->scratchpad) {
1655 log_err("malloc failure");
1656 worker_delete(worker);
1660 server_stats_init(&worker->stats, cfg);
1661 alloc_init(&worker->alloc, &worker->daemon->superalloc,
1662 worker->thread_num);
1663 alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1664 worker->env = *worker->daemon->env;
1665 comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1666 if(worker->thread_num == 0)
1667 log_set_time(worker->env.now);
1668 worker->env.worker = worker;
1669 worker->env.send_query = &worker_send_query;
1670 worker->env.alloc = &worker->alloc;
1671 worker->env.rnd = worker->rndstate;
1672 worker->env.scratch = worker->scratchpad;
1673 worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1674 worker->env.detach_subs = &mesh_detach_subs;
1675 worker->env.attach_sub = &mesh_attach_sub;
1676 worker->env.add_sub = &mesh_add_sub;
1677 worker->env.kill_sub = &mesh_state_delete;
1678 worker->env.detect_cycle = &mesh_detect_cycle;
1679 worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1680 if(!(worker->env.fwds = forwards_create()) ||
1681 !forwards_apply_cfg(worker->env.fwds, cfg)) {
1682 log_err("Could not set forward zones");
1683 worker_delete(worker);
1686 if(!(worker->env.hints = hints_create()) ||
1687 !hints_apply_cfg(worker->env.hints, cfg)) {
1688 log_err("Could not set root or stub hints");
1689 worker_delete(worker);
1692 /* one probe timer per process -- if we have 5011 anchors */
1693 if(autr_get_num_anchors(worker->env.anchors) > 0
1694 #ifndef THREADS_DISABLED
1695 && worker->thread_num == 0
1701 worker->env.probe_timer = comm_timer_create(worker->base,
1702 worker_probe_timer_cb, worker);
1703 if(!worker->env.probe_timer) {
1704 log_err("could not create 5011-probe timer");
1706 /* let timer fire, then it can reset itself */
1707 comm_timer_set(worker->env.probe_timer, &tv);
1710 if(!worker->env.mesh || !worker->env.scratch_buffer) {
1711 worker_delete(worker);
1714 worker_mem_report(worker, NULL);
1715 /* if statistics enabled start timer */
1716 if(worker->env.cfg->stat_interval > 0) {
1717 verbose(VERB_ALGO, "set statistics interval %d secs",
1718 worker->env.cfg->stat_interval);
1719 worker_restart_timer(worker);
1725 worker_work(struct worker* worker)
1727 comm_base_dispatch(worker->base);
1731 worker_delete(struct worker* worker)
1735 if(worker->env.mesh && verbosity >= VERB_OPS) {
1736 server_stats_log(&worker->stats, worker, worker->thread_num);
1737 mesh_stats(worker->env.mesh, "mesh has");
1738 worker_mem_report(worker, NULL);
1740 outside_network_quit_prepare(worker->back);
1741 mesh_delete(worker->env.mesh);
1742 sldns_buffer_free(worker->env.scratch_buffer);
1743 forwards_delete(worker->env.fwds);
1744 hints_delete(worker->env.hints);
1745 listen_delete(worker->front);
1746 outside_network_delete(worker->back);
1747 comm_signal_delete(worker->comsig);
1748 tube_delete(worker->cmd);
1749 comm_timer_delete(worker->stat_timer);
1750 comm_timer_delete(worker->env.probe_timer);
1751 free(worker->ports);
1752 if(worker->thread_num == 0) {
1754 #ifdef UB_ON_WINDOWS
1755 wsvc_desetup_worker(worker);
1756 #endif /* UB_ON_WINDOWS */
1758 comm_base_delete(worker->base);
1759 ub_randfree(worker->rndstate);
1760 alloc_clear(&worker->alloc);
1761 regional_destroy(worker->scratchpad);
1765 struct outbound_entry*
1766 worker_send_query(struct query_info* qinfo, uint16_t flags, int dnssec,
1767 int want_dnssec, int nocaps, struct sockaddr_storage* addr,
1768 socklen_t addrlen, uint8_t* zone, size_t zonelen, int ssl_upstream,
1769 struct module_qstate* q)
1771 struct worker* worker = q->env->worker;
1772 struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1773 q->region, sizeof(*e));
1777 e->qsent = outnet_serviced_query(worker->back, qinfo, flags, dnssec,
1778 want_dnssec, nocaps, q->env->cfg->tcp_upstream,
1779 ssl_upstream, addr, addrlen, zone, zonelen, q,
1780 worker_handle_service_reply, e, worker->back->udp_buff, q->env);
1788 worker_alloc_cleanup(void* arg)
1790 struct worker* worker = (struct worker*)arg;
1791 slabhash_clear(&worker->env.rrset_cache->table);
1792 slabhash_clear(worker->env.msg_cache);
1795 void worker_stats_clear(struct worker* worker)
1797 server_stats_init(&worker->stats, worker->env.cfg);
1798 mesh_stats_clear(worker->env.mesh);
1799 worker->back->unwanted_replies = 0;
1800 worker->back->num_tcp_outgoing = 0;
1803 void worker_start_accept(void* arg)
1805 struct worker* worker = (struct worker*)arg;
1806 listen_start_accept(worker->front);
1807 if(worker->thread_num == 0)
1808 daemon_remote_start_accept(worker->daemon->rc);
1811 void worker_stop_accept(void* arg)
1813 struct worker* worker = (struct worker*)arg;
1814 listen_stop_accept(worker->front);
1815 if(worker->thread_num == 0)
1816 daemon_remote_stop_accept(worker->daemon->rc);
1819 /* --- fake callbacks for fptr_wlist to work --- */
1820 struct outbound_entry* libworker_send_query(
1821 struct query_info* ATTR_UNUSED(qinfo),
1822 uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec),
1823 int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
1824 struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1825 uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
1826 int ATTR_UNUSED(ssl_upstream), struct module_qstate* ATTR_UNUSED(q))
1832 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1833 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1834 struct comm_reply* ATTR_UNUSED(reply_info))
1840 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1841 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1842 struct comm_reply* ATTR_UNUSED(reply_info))
1848 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1849 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1850 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1855 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1856 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1857 char* ATTR_UNUSED(why_bogus))
1862 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1863 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1864 char* ATTR_UNUSED(why_bogus))
1869 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1870 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1871 char* ATTR_UNUSED(why_bogus))
1876 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1882 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1888 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))