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