xref: /freebsd/contrib/unbound/daemon/worker.c (revision b1f92fa22938fe29ab7e53692ffe0ed7a0ecc4d0)
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/mesh.h"
62 #include "services/localzone.h"
63 #include "util/data/msgparse.h"
64 #include "util/data/msgencode.h"
65 #include "util/data/dname.h"
66 #include "util/fptr_wlist.h"
67 #include "util/tube.h"
68 #include "iterator/iter_fwd.h"
69 #include "iterator/iter_hints.h"
70 #include "validator/autotrust.h"
71 #include "validator/val_anchor.h"
72 #include "libunbound/context.h"
73 #include "libunbound/libworker.h"
74 #include "sldns/sbuffer.h"
75 
76 #ifdef HAVE_SYS_TYPES_H
77 #  include <sys/types.h>
78 #endif
79 #ifdef HAVE_NETDB_H
80 #include <netdb.h>
81 #endif
82 #include <signal.h>
83 #ifdef UB_ON_WINDOWS
84 #include "winrc/win_svc.h"
85 #endif
86 
87 /** Size of an UDP datagram */
88 #define NORMAL_UDP_SIZE	512 /* bytes */
89 /** ratelimit for error responses */
90 #define ERROR_RATELIMIT 100 /* qps */
91 
92 /**
93  * seconds to add to prefetch leeway.  This is a TTL that expires old rrsets
94  * earlier than they should in order to put the new update into the cache.
95  * This additional value is to make sure that if not all TTLs are equal in
96  * the message to be updated(and replaced), that rrsets with up to this much
97  * extra TTL are also replaced.  This means that the resulting new message
98  * will have (most likely) this TTL at least, avoiding very small 'split
99  * second' TTLs due to operators choosing relative primes for TTLs (or so).
100  * Also has to be at least one to break ties (and overwrite cached entry).
101  */
102 #define PREFETCH_EXPIRY_ADD 60
103 
104 #ifdef UNBOUND_ALLOC_STATS
105 /** measure memory leakage */
106 static void
107 debug_memleak(size_t accounted, size_t heap,
108 	size_t total_alloc, size_t total_free)
109 {
110 	static int init = 0;
111 	static size_t base_heap, base_accounted, base_alloc, base_free;
112 	size_t base_af, cur_af, grow_af, grow_acc;
113 	if(!init) {
114 		init = 1;
115 		base_heap = heap;
116 		base_accounted = accounted;
117 		base_alloc = total_alloc;
118 		base_free = total_free;
119 	}
120 	base_af = base_alloc - base_free;
121 	cur_af = total_alloc - total_free;
122 	grow_af = cur_af - base_af;
123 	grow_acc = accounted - base_accounted;
124 	log_info("Leakage: %d leaked. growth: %u use, %u acc, %u heap",
125 		(int)(grow_af - grow_acc), (unsigned)grow_af,
126 		(unsigned)grow_acc, (unsigned)(heap - base_heap));
127 }
128 
129 /** give debug heap size indication */
130 static void
131 debug_total_mem(size_t calctotal)
132 {
133 #ifdef HAVE_SBRK
134 	extern void* unbound_start_brk;
135 	extern size_t unbound_mem_alloc, unbound_mem_freed;
136 	void* cur = sbrk(0);
137 	int total = cur-unbound_start_brk;
138 	log_info("Total heap memory estimate: %u  total-alloc: %u  "
139 		"total-free: %u", (unsigned)total,
140 		(unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
141 	debug_memleak(calctotal, (size_t)total,
142 		unbound_mem_alloc, unbound_mem_freed);
143 #else
144 	(void)calctotal;
145 #endif /* HAVE_SBRK */
146 }
147 #endif /* UNBOUND_ALLOC_STATS */
148 
149 /** Report on memory usage by this thread and global */
150 static void
151 worker_mem_report(struct worker* ATTR_UNUSED(worker),
152 	struct serviced_query* ATTR_UNUSED(cur_serv))
153 {
154 #ifdef UNBOUND_ALLOC_STATS
155 	/* debug func in validator module */
156 	size_t total, front, back, mesh, msg, rrset, infra, ac, superac;
157 	size_t me, iter, val, anch;
158 	int i;
159 	if(verbosity < VERB_ALGO)
160 		return;
161 	front = listen_get_mem(worker->front);
162 	back = outnet_get_mem(worker->back);
163 	msg = slabhash_get_mem(worker->env.msg_cache);
164 	rrset = slabhash_get_mem(&worker->env.rrset_cache->table);
165 	infra = infra_get_mem(worker->env.infra_cache);
166 	mesh = mesh_get_mem(worker->env.mesh);
167 	ac = alloc_get_mem(&worker->alloc);
168 	superac = alloc_get_mem(&worker->daemon->superalloc);
169 	anch = anchors_get_mem(worker->env.anchors);
170 	iter = 0;
171 	val = 0;
172 	for(i=0; i<worker->env.mesh->mods.num; i++) {
173 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
174 			mods.mod[i]->get_mem));
175 		if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
176 			val += (*worker->env.mesh->mods.mod[i]->get_mem)
177 				(&worker->env, i);
178 		else	iter += (*worker->env.mesh->mods.mod[i]->get_mem)
179 				(&worker->env, i);
180 	}
181 	me = sizeof(*worker) + sizeof(*worker->base) + sizeof(*worker->comsig)
182 		+ comm_point_get_mem(worker->cmd_com)
183 		+ sizeof(worker->rndstate)
184 		+ regional_get_mem(worker->scratchpad)
185 		+ sizeof(*worker->env.scratch_buffer)
186 		+ sldns_buffer_capacity(worker->env.scratch_buffer)
187 		+ forwards_get_mem(worker->env.fwds)
188 		+ hints_get_mem(worker->env.hints);
189 	if(worker->thread_num == 0)
190 		me += acl_list_get_mem(worker->daemon->acl);
191 	if(cur_serv) {
192 		me += serviced_get_mem(cur_serv);
193 	}
194 	total = front+back+mesh+msg+rrset+infra+iter+val+ac+superac+me;
195 	log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
196 		"rrset=%u infra=%u iter=%u val=%u anchors=%u "
197 		"alloccache=%u globalalloccache=%u me=%u",
198 		(unsigned)total, (unsigned)front, (unsigned)back,
199 		(unsigned)mesh, (unsigned)msg, (unsigned)rrset,
200 		(unsigned)infra, (unsigned)iter, (unsigned)val, (unsigned)anch,
201 		(unsigned)ac, (unsigned)superac, (unsigned)me);
202 	debug_total_mem(total);
203 #else /* no UNBOUND_ALLOC_STATS */
204 	size_t val = 0;
205 	int i;
206 	if(verbosity < VERB_QUERY)
207 		return;
208 	for(i=0; i<worker->env.mesh->mods.num; i++) {
209 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
210 			mods.mod[i]->get_mem));
211 		if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
212 			val += (*worker->env.mesh->mods.mod[i]->get_mem)
213 				(&worker->env, i);
214 	}
215 	verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u",
216 		(unsigned)slabhash_get_mem(worker->env.msg_cache),
217 		(unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
218 		(unsigned)infra_get_mem(worker->env.infra_cache),
219 		(unsigned)val);
220 #endif /* UNBOUND_ALLOC_STATS */
221 }
222 
223 void
224 worker_send_cmd(struct worker* worker, enum worker_commands cmd)
225 {
226 	uint32_t c = (uint32_t)htonl(cmd);
227 	if(!tube_write_msg(worker->cmd, (uint8_t*)&c, sizeof(c), 0)) {
228 		log_err("worker send cmd %d failed", (int)cmd);
229 	}
230 }
231 
232 int
233 worker_handle_reply(struct comm_point* c, void* arg, int error,
234 	struct comm_reply* reply_info)
235 {
236 	struct module_qstate* q = (struct module_qstate*)arg;
237 	struct worker* worker = q->env->worker;
238 	struct outbound_entry e;
239 	e.qstate = q;
240 	e.qsent = NULL;
241 
242 	if(error != 0) {
243 		mesh_report_reply(worker->env.mesh, &e, reply_info, error);
244 		worker_mem_report(worker, NULL);
245 		return 0;
246 	}
247 	/* sanity check. */
248 	if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
249 		|| LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
250 			LDNS_PACKET_QUERY
251 		|| LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
252 		/* error becomes timeout for the module as if this reply
253 		 * never arrived. */
254 		mesh_report_reply(worker->env.mesh, &e, reply_info,
255 			NETEVENT_TIMEOUT);
256 		worker_mem_report(worker, NULL);
257 		return 0;
258 	}
259 	mesh_report_reply(worker->env.mesh, &e, reply_info, NETEVENT_NOERROR);
260 	worker_mem_report(worker, NULL);
261 	return 0;
262 }
263 
264 int
265 worker_handle_service_reply(struct comm_point* c, void* arg, int error,
266 	struct comm_reply* reply_info)
267 {
268 	struct outbound_entry* e = (struct outbound_entry*)arg;
269 	struct worker* worker = e->qstate->env->worker;
270 	struct serviced_query *sq = e->qsent;
271 
272 	verbose(VERB_ALGO, "worker svcd callback for qstate %p", e->qstate);
273 	if(error != 0) {
274 		mesh_report_reply(worker->env.mesh, e, reply_info, error);
275 		worker_mem_report(worker, sq);
276 		return 0;
277 	}
278 	/* sanity check. */
279 	if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
280 		|| LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
281 			LDNS_PACKET_QUERY
282 		|| LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
283 		/* error becomes timeout for the module as if this reply
284 		 * never arrived. */
285 		verbose(VERB_ALGO, "worker: bad reply handled as timeout");
286 		mesh_report_reply(worker->env.mesh, e, reply_info,
287 			NETEVENT_TIMEOUT);
288 		worker_mem_report(worker, sq);
289 		return 0;
290 	}
291 	mesh_report_reply(worker->env.mesh, e, reply_info, NETEVENT_NOERROR);
292 	worker_mem_report(worker, sq);
293 	return 0;
294 }
295 
296 /** ratelimit error replies
297  * @param worker: the worker struct with ratelimit counter
298  * @param err: error code that would be wanted.
299  * @return value of err if okay, or -1 if it should be discarded instead.
300  */
301 static int
302 worker_err_ratelimit(struct worker* worker, int err)
303 {
304 	if(worker->err_limit_time == *worker->env.now) {
305 		/* see if limit is exceeded for this second */
306 		if(worker->err_limit_count++ > ERROR_RATELIMIT)
307 			return -1;
308 	} else {
309 		/* new second, new limits */
310 		worker->err_limit_time = *worker->env.now;
311 		worker->err_limit_count = 1;
312 	}
313 	return err;
314 }
315 
316 /** check request sanity.
317  * @param pkt: the wire packet to examine for sanity.
318  * @param worker: parameters for checking.
319  * @return error code, 0 OK, or -1 discard.
320 */
321 static int
322 worker_check_request(sldns_buffer* pkt, struct worker* worker)
323 {
324 	if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
325 		verbose(VERB_QUERY, "request too short, discarded");
326 		return -1;
327 	}
328 	if(sldns_buffer_limit(pkt) > NORMAL_UDP_SIZE &&
329 		worker->daemon->cfg->harden_large_queries) {
330 		verbose(VERB_QUERY, "request too large, discarded");
331 		return -1;
332 	}
333 	if(LDNS_QR_WIRE(sldns_buffer_begin(pkt))) {
334 		verbose(VERB_QUERY, "request has QR bit on, discarded");
335 		return -1;
336 	}
337 	if(LDNS_TC_WIRE(sldns_buffer_begin(pkt))) {
338 		LDNS_TC_CLR(sldns_buffer_begin(pkt));
339 		verbose(VERB_QUERY, "request bad, has TC bit on");
340 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
341 	}
342 	if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY) {
343 		verbose(VERB_QUERY, "request unknown opcode %d",
344 			LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)));
345 		return worker_err_ratelimit(worker, LDNS_RCODE_NOTIMPL);
346 	}
347 	if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1) {
348 		verbose(VERB_QUERY, "request wrong nr qd=%d",
349 			LDNS_QDCOUNT(sldns_buffer_begin(pkt)));
350 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
351 	}
352 	if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0) {
353 		verbose(VERB_QUERY, "request wrong nr an=%d",
354 			LDNS_ANCOUNT(sldns_buffer_begin(pkt)));
355 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
356 	}
357 	if(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) {
358 		verbose(VERB_QUERY, "request wrong nr ns=%d",
359 			LDNS_NSCOUNT(sldns_buffer_begin(pkt)));
360 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
361 	}
362 	if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) {
363 		verbose(VERB_QUERY, "request wrong nr ar=%d",
364 			LDNS_ARCOUNT(sldns_buffer_begin(pkt)));
365 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
366 	}
367 	return 0;
368 }
369 
370 void
371 worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), uint8_t* msg,
372 	size_t len, int error, void* arg)
373 {
374 	struct worker* worker = (struct worker*)arg;
375 	enum worker_commands cmd;
376 	if(error != NETEVENT_NOERROR) {
377 		free(msg);
378 		if(error == NETEVENT_CLOSED)
379 			comm_base_exit(worker->base);
380 		else	log_info("control event: %d", error);
381 		return;
382 	}
383 	if(len != sizeof(uint32_t)) {
384 		fatal_exit("bad control msg length %d", (int)len);
385 	}
386 	cmd = sldns_read_uint32(msg);
387 	free(msg);
388 	switch(cmd) {
389 	case worker_cmd_quit:
390 		verbose(VERB_ALGO, "got control cmd quit");
391 		comm_base_exit(worker->base);
392 		break;
393 	case worker_cmd_stats:
394 		verbose(VERB_ALGO, "got control cmd stats");
395 		server_stats_reply(worker, 1);
396 		break;
397 	case worker_cmd_stats_noreset:
398 		verbose(VERB_ALGO, "got control cmd stats_noreset");
399 		server_stats_reply(worker, 0);
400 		break;
401 	case worker_cmd_remote:
402 		verbose(VERB_ALGO, "got control cmd remote");
403 		daemon_remote_exec(worker);
404 		break;
405 	default:
406 		log_err("bad command %d", (int)cmd);
407 		break;
408 	}
409 }
410 
411 /** check if a delegation is secure */
412 static enum sec_status
413 check_delegation_secure(struct reply_info *rep)
414 {
415 	/* return smallest security status */
416 	size_t i;
417 	enum sec_status sec = sec_status_secure;
418 	enum sec_status s;
419 	size_t num = rep->an_numrrsets + rep->ns_numrrsets;
420 	/* check if answer and authority are OK */
421 	for(i=0; i<num; i++) {
422 		s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
423 			->security;
424 		if(s < sec)
425 			sec = s;
426 	}
427 	/* in additional, only unchecked triggers revalidation */
428 	for(i=num; i<rep->rrset_count; i++) {
429 		s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
430 			->security;
431 		if(s == sec_status_unchecked)
432 			return s;
433 	}
434 	return sec;
435 }
436 
437 /** remove nonsecure from a delegation referral additional section */
438 static void
439 deleg_remove_nonsecure_additional(struct reply_info* rep)
440 {
441 	/* we can simply edit it, since we are working in the scratch region */
442 	size_t i;
443 	enum sec_status s;
444 
445 	for(i = rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
446 		s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
447 			->security;
448 		if(s != sec_status_secure) {
449 			memmove(rep->rrsets+i, rep->rrsets+i+1,
450 				sizeof(struct ub_packed_rrset_key*)*
451 				(rep->rrset_count - i - 1));
452 			rep->ar_numrrsets--;
453 			rep->rrset_count--;
454 			i--;
455 		}
456 	}
457 }
458 
459 /** answer nonrecursive query from the cache */
460 static int
461 answer_norec_from_cache(struct worker* worker, struct query_info* qinfo,
462 	uint16_t id, uint16_t flags, struct comm_reply* repinfo,
463 	struct edns_data* edns)
464 {
465 	/* for a nonrecursive query return either:
466 	 * 	o an error (servfail; we try to avoid this)
467 	 * 	o a delegation (closest we have; this routine tries that)
468 	 * 	o the answer (checked by answer_from_cache)
469 	 *
470 	 * So, grab a delegation from the rrset cache.
471 	 * Then check if it needs validation, if so, this routine fails,
472 	 * so that iterator can prime and validator can verify rrsets.
473 	 */
474 	uint16_t udpsize = edns->udp_size;
475 	int secure = 0;
476 	time_t timenow = *worker->env.now;
477 	int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
478 		&& worker->env.need_to_validate;
479 	struct dns_msg *msg = NULL;
480 	struct delegpt *dp;
481 
482 	dp = dns_cache_find_delegation(&worker->env, qinfo->qname,
483 		qinfo->qname_len, qinfo->qtype, qinfo->qclass,
484 		worker->scratchpad, &msg, timenow);
485 	if(!dp) { /* no delegation, need to reprime */
486 		regional_free_all(worker->scratchpad);
487 		return 0;
488 	}
489 	if(must_validate) {
490 		switch(check_delegation_secure(msg->rep)) {
491 		case sec_status_unchecked:
492 			/* some rrsets have not been verified yet, go and
493 			 * let validator do that */
494 			regional_free_all(worker->scratchpad);
495 			return 0;
496 		case sec_status_bogus:
497 			/* some rrsets are bogus, reply servfail */
498 			edns->edns_version = EDNS_ADVERTISED_VERSION;
499 			edns->udp_size = EDNS_ADVERTISED_SIZE;
500 			edns->ext_rcode = 0;
501 			edns->bits &= EDNS_DO;
502 			error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
503 				&msg->qinfo, id, flags, edns);
504 			regional_free_all(worker->scratchpad);
505 			if(worker->stats.extended) {
506 				worker->stats.ans_bogus++;
507 				worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
508 			}
509 			return 1;
510 		case sec_status_secure:
511 			/* all rrsets are secure */
512 			/* remove non-secure rrsets from the add. section*/
513 			if(worker->env.cfg->val_clean_additional)
514 				deleg_remove_nonsecure_additional(msg->rep);
515 			secure = 1;
516 			break;
517 		case sec_status_indeterminate:
518 		case sec_status_insecure:
519 		default:
520 			/* not secure */
521 			secure = 0;
522 			break;
523 		}
524 	}
525 	/* return this delegation from the cache */
526 	edns->edns_version = EDNS_ADVERTISED_VERSION;
527 	edns->udp_size = EDNS_ADVERTISED_SIZE;
528 	edns->ext_rcode = 0;
529 	edns->bits &= EDNS_DO;
530 	msg->rep->flags |= BIT_QR|BIT_RA;
531 	if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags,
532 		repinfo->c->buffer, 0, 1, worker->scratchpad,
533 		udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
534 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
535 			&msg->qinfo, id, flags, edns);
536 	}
537 	regional_free_all(worker->scratchpad);
538 	if(worker->stats.extended) {
539 		if(secure) worker->stats.ans_secure++;
540 		server_stats_insrcode(&worker->stats, repinfo->c->buffer);
541 	}
542 	return 1;
543 }
544 
545 /** answer query from the cache */
546 static int
547 answer_from_cache(struct worker* worker, struct query_info* qinfo,
548 	struct reply_info* rep, uint16_t id, uint16_t flags,
549 	struct comm_reply* repinfo, struct edns_data* edns)
550 {
551 	time_t timenow = *worker->env.now;
552 	uint16_t udpsize = edns->udp_size;
553 	int secure;
554 	int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
555 		&& worker->env.need_to_validate;
556 	/* see if it is possible */
557 	if(rep->ttl < timenow) {
558 		/* the rrsets may have been updated in the meantime.
559 		 * we will refetch the message format from the
560 		 * authoritative server
561 		 */
562 		return 0;
563 	}
564 	if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
565 		return 0;
566 	/* locked and ids and ttls are OK. */
567 	/* check CNAME chain (if any) */
568 	if(rep->an_numrrsets > 0 && (rep->rrsets[0]->rk.type ==
569 		htons(LDNS_RR_TYPE_CNAME) || rep->rrsets[0]->rk.type ==
570 		htons(LDNS_RR_TYPE_DNAME))) {
571 		if(!reply_check_cname_chain(qinfo, rep)) {
572 			/* cname chain invalid, redo iterator steps */
573 			verbose(VERB_ALGO, "Cache reply: cname chain broken");
574 		bail_out:
575 			rrset_array_unlock_touch(worker->env.rrset_cache,
576 				worker->scratchpad, rep->ref, rep->rrset_count);
577 			regional_free_all(worker->scratchpad);
578 			return 0;
579 		}
580 	}
581 	/* check security status of the cached answer */
582 	if( rep->security == sec_status_bogus && must_validate) {
583 		/* BAD cached */
584 		edns->edns_version = EDNS_ADVERTISED_VERSION;
585 		edns->udp_size = EDNS_ADVERTISED_SIZE;
586 		edns->ext_rcode = 0;
587 		edns->bits &= EDNS_DO;
588 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
589 			qinfo, id, flags, edns);
590 		rrset_array_unlock_touch(worker->env.rrset_cache,
591 			worker->scratchpad, rep->ref, rep->rrset_count);
592 		regional_free_all(worker->scratchpad);
593 		if(worker->stats.extended) {
594 			worker->stats.ans_bogus ++;
595 			worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL] ++;
596 		}
597 		return 1;
598 	} else if( rep->security == sec_status_unchecked && must_validate) {
599 		verbose(VERB_ALGO, "Cache reply: unchecked entry needs "
600 			"validation");
601 		goto bail_out; /* need to validate cache entry first */
602 	} else if(rep->security == sec_status_secure) {
603 		if(reply_all_rrsets_secure(rep))
604 			secure = 1;
605 		else	{
606 			if(must_validate) {
607 				verbose(VERB_ALGO, "Cache reply: secure entry"
608 					" changed status");
609 				goto bail_out; /* rrset changed, re-verify */
610 			}
611 			secure = 0;
612 		}
613 	} else	secure = 0;
614 
615 	edns->edns_version = EDNS_ADVERTISED_VERSION;
616 	edns->udp_size = EDNS_ADVERTISED_SIZE;
617 	edns->ext_rcode = 0;
618 	edns->bits &= EDNS_DO;
619 	if(!reply_info_answer_encode(qinfo, rep, id, flags,
620 		repinfo->c->buffer, timenow, 1, worker->scratchpad,
621 		udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
622 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
623 			qinfo, id, flags, edns);
624 	}
625 	/* cannot send the reply right now, because blocking network syscall
626 	 * is bad while holding locks. */
627 	rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
628 		rep->ref, rep->rrset_count);
629 	regional_free_all(worker->scratchpad);
630 	if(worker->stats.extended) {
631 		if(secure) worker->stats.ans_secure++;
632 		server_stats_insrcode(&worker->stats, repinfo->c->buffer);
633 	}
634 	/* go and return this buffer to the client */
635 	return 1;
636 }
637 
638 /** Reply to client and perform prefetch to keep cache up to date */
639 static void
640 reply_and_prefetch(struct worker* worker, struct query_info* qinfo,
641 	uint16_t flags, struct comm_reply* repinfo, time_t leeway)
642 {
643 	/* first send answer to client to keep its latency
644 	 * as small as a cachereply */
645 	comm_point_send_reply(repinfo);
646 	server_stats_prefetch(&worker->stats, worker);
647 
648 	/* create the prefetch in the mesh as a normal lookup without
649 	 * client addrs waiting, which has the cache blacklisted (to bypass
650 	 * the cache and go to the network for the data). */
651 	/* this (potentially) runs the mesh for the new query */
652 	mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
653 		PREFETCH_EXPIRY_ADD);
654 }
655 
656 /**
657  * Fill CH class answer into buffer. Keeps query.
658  * @param pkt: buffer
659  * @param str: string to put into text record (<255).
660  * @param edns: edns reply information.
661  */
662 static void
663 chaos_replystr(sldns_buffer* pkt, const char* str, struct edns_data* edns)
664 {
665 	size_t len = strlen(str);
666 	unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
667 	unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
668 	if(len>255) len=255; /* cap size of TXT record */
669 	sldns_buffer_clear(pkt);
670 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
671 	sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
672 	if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
673 	if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
674 	sldns_buffer_write_u16(pkt, 1); /* qdcount */
675 	sldns_buffer_write_u16(pkt, 1); /* ancount */
676 	sldns_buffer_write_u16(pkt, 0); /* nscount */
677 	sldns_buffer_write_u16(pkt, 0); /* arcount */
678 	(void)query_dname_len(pkt); /* skip qname */
679 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
680 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
681 	sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
682 	sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
683 	sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
684 	sldns_buffer_write_u32(pkt, 0); /* TTL */
685 	sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
686 	sldns_buffer_write_u8(pkt, len);
687 	sldns_buffer_write(pkt, str, len);
688 	sldns_buffer_flip(pkt);
689 	edns->edns_version = EDNS_ADVERTISED_VERSION;
690 	edns->udp_size = EDNS_ADVERTISED_SIZE;
691 	edns->bits &= EDNS_DO;
692 	attach_edns_record(pkt, edns);
693 }
694 
695 /**
696  * Answer CH class queries.
697  * @param w: worker
698  * @param qinfo: query info. Pointer into packet buffer.
699  * @param edns: edns info from query.
700  * @param pkt: packet buffer.
701  * @return: true if a reply is to be sent.
702  */
703 static int
704 answer_chaos(struct worker* w, struct query_info* qinfo,
705 	struct edns_data* edns, sldns_buffer* pkt)
706 {
707 	struct config_file* cfg = w->env.cfg;
708 	if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
709 		return 0;
710 	if(query_dname_compare(qinfo->qname,
711 		(uint8_t*)"\002id\006server") == 0 ||
712 		query_dname_compare(qinfo->qname,
713 		(uint8_t*)"\010hostname\004bind") == 0)
714 	{
715 		if(cfg->hide_identity)
716 			return 0;
717 		if(cfg->identity==NULL || cfg->identity[0]==0) {
718 			char buf[MAXHOSTNAMELEN+1];
719 			if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
720 				buf[MAXHOSTNAMELEN] = 0;
721 				chaos_replystr(pkt, buf, edns);
722 			} else 	{
723 				log_err("gethostname: %s", strerror(errno));
724 				chaos_replystr(pkt, "no hostname", edns);
725 			}
726 		}
727 		else 	chaos_replystr(pkt, cfg->identity, edns);
728 		return 1;
729 	}
730 	if(query_dname_compare(qinfo->qname,
731 		(uint8_t*)"\007version\006server") == 0 ||
732 		query_dname_compare(qinfo->qname,
733 		(uint8_t*)"\007version\004bind") == 0)
734 	{
735 		if(cfg->hide_version)
736 			return 0;
737 		if(cfg->version==NULL || cfg->version[0]==0)
738 			chaos_replystr(pkt, PACKAGE_STRING, edns);
739 		else 	chaos_replystr(pkt, cfg->version, edns);
740 		return 1;
741 	}
742 	return 0;
743 }
744 
745 static int
746 deny_refuse(struct comm_point* c, enum acl_access acl,
747 	enum acl_access deny, enum acl_access refuse,
748 	struct worker* worker, struct comm_reply* repinfo)
749 {
750 	if(acl == deny) {
751 		comm_point_drop_reply(repinfo);
752 		if(worker->stats.extended)
753 			worker->stats.unwanted_queries++;
754 		return 0;
755 	} else if(acl == refuse) {
756 		log_addr(VERB_ALGO, "refused query from",
757 			&repinfo->addr, repinfo->addrlen);
758 		log_buf(VERB_ALGO, "refuse", c->buffer);
759 		if(worker->stats.extended)
760 			worker->stats.unwanted_queries++;
761 		if(worker_check_request(c->buffer, worker) == -1) {
762 			comm_point_drop_reply(repinfo);
763 			return 0; /* discard this */
764 		}
765 		sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
766 		sldns_buffer_write_at(c->buffer, 4,
767 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
768 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
769 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
770 			LDNS_RCODE_REFUSED);
771 		return 1;
772 	}
773 
774 	return -1;
775 }
776 
777 static int
778 deny_refuse_all(struct comm_point* c, enum acl_access acl,
779 	struct worker* worker, struct comm_reply* repinfo)
780 {
781 	return deny_refuse(c, acl, acl_deny, acl_refuse, worker, repinfo);
782 }
783 
784 static int
785 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
786 	struct worker* worker, struct comm_reply* repinfo)
787 {
788 	return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, worker, repinfo);
789 }
790 
791 int
792 worker_handle_request(struct comm_point* c, void* arg, int error,
793 	struct comm_reply* repinfo)
794 {
795 	struct worker* worker = (struct worker*)arg;
796 	int ret;
797 	hashvalue_t h;
798 	struct lruhash_entry* e;
799 	struct query_info qinfo;
800 	struct edns_data edns;
801 	enum acl_access acl;
802 	int rc = 0;
803 
804 	if(error != NETEVENT_NOERROR) {
805 		/* some bad tcp query DNS formats give these error calls */
806 		verbose(VERB_ALGO, "handle request called with err=%d", error);
807 		return 0;
808 	}
809 #ifdef USE_DNSTAP
810 	if(worker->dtenv.log_client_query_messages)
811 		dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
812 			c->buffer);
813 #endif
814 	acl = acl_list_lookup(worker->daemon->acl, &repinfo->addr,
815 		repinfo->addrlen);
816 	if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
817 	{
818 		if(ret == 1)
819 			goto send_reply;
820 		return ret;
821 	}
822 	if((ret=worker_check_request(c->buffer, worker)) != 0) {
823 		verbose(VERB_ALGO, "worker check request: bad query.");
824 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
825 		if(ret != -1) {
826 			LDNS_QR_SET(sldns_buffer_begin(c->buffer));
827 			LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
828 			return 1;
829 		}
830 		comm_point_drop_reply(repinfo);
831 		return 0;
832 	}
833 	worker->stats.num_queries++;
834 	/* see if query is in the cache */
835 	if(!query_info_parse(&qinfo, c->buffer)) {
836 		verbose(VERB_ALGO, "worker parse request: formerror.");
837 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
838 		if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
839 			comm_point_drop_reply(repinfo);
840 			return 0;
841 		}
842 		sldns_buffer_rewind(c->buffer);
843 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
844 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
845 			LDNS_RCODE_FORMERR);
846 		server_stats_insrcode(&worker->stats, c->buffer);
847 		goto send_reply;
848 	}
849 	if(worker->env.cfg->log_queries) {
850 		char ip[128];
851 		addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
852 		log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
853 	}
854 	if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
855 		qinfo.qtype == LDNS_RR_TYPE_IXFR) {
856 		verbose(VERB_ALGO, "worker request: refused zone transfer.");
857 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
858 		sldns_buffer_rewind(c->buffer);
859 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
860 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
861 			LDNS_RCODE_REFUSED);
862 		if(worker->stats.extended) {
863 			worker->stats.qtype[qinfo.qtype]++;
864 			server_stats_insrcode(&worker->stats, c->buffer);
865 		}
866 		goto send_reply;
867 	}
868 	if((ret=parse_edns_from_pkt(c->buffer, &edns)) != 0) {
869 		verbose(VERB_ALGO, "worker parse edns: formerror.");
870 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
871 		sldns_buffer_rewind(c->buffer);
872 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
873 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
874 		server_stats_insrcode(&worker->stats, c->buffer);
875 		goto send_reply;
876 	}
877 	if(edns.edns_present && edns.edns_version != 0) {
878 		edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
879 		edns.edns_version = EDNS_ADVERTISED_VERSION;
880 		edns.udp_size = EDNS_ADVERTISED_SIZE;
881 		edns.bits &= EDNS_DO;
882 		verbose(VERB_ALGO, "query with bad edns version.");
883 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
884 		error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
885 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
886 			sldns_buffer_read_u16_at(c->buffer, 2), NULL);
887 		attach_edns_record(c->buffer, &edns);
888 		goto send_reply;
889 	}
890 	if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
891 		worker->daemon->cfg->harden_short_bufsize) {
892 		verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
893 			(int)edns.udp_size);
894 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
895 		edns.udp_size = NORMAL_UDP_SIZE;
896 	}
897 	if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
898 		c->type == comm_udp) {
899 		verbose(VERB_QUERY,
900 			"worker request: max UDP reply size modified"
901 			" (%d to max-udp-size)", (int)edns.udp_size);
902 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
903 		edns.udp_size = worker->daemon->cfg->max_udp_size;
904 	}
905 	if(edns.udp_size < LDNS_HEADER_SIZE) {
906 		verbose(VERB_ALGO, "worker request: edns is too small.");
907 		log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
908 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
909 		LDNS_TC_SET(sldns_buffer_begin(c->buffer));
910 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
911 			LDNS_RCODE_SERVFAIL);
912 		sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
913 		sldns_buffer_write_at(c->buffer, 4,
914 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
915 		sldns_buffer_flip(c->buffer);
916 		goto send_reply;
917 	}
918 	if(worker->stats.extended)
919 		server_stats_insquery(&worker->stats, c, qinfo.qtype,
920 			qinfo.qclass, &edns, repinfo);
921 	if(c->type != comm_udp)
922 		edns.udp_size = 65535; /* max size for TCP replies */
923 	if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
924 		&edns, c->buffer)) {
925 		server_stats_insrcode(&worker->stats, c->buffer);
926 		goto send_reply;
927 	}
928 	if(local_zones_answer(worker->daemon->local_zones, &qinfo, &edns,
929 		c->buffer, worker->scratchpad, repinfo)) {
930 		regional_free_all(worker->scratchpad);
931 		if(sldns_buffer_limit(c->buffer) == 0) {
932 			comm_point_drop_reply(repinfo);
933 			return 0;
934 		}
935 		server_stats_insrcode(&worker->stats, c->buffer);
936 		goto send_reply;
937 	}
938 
939 	/* We've looked in our local zones. If the answer isn't there, we
940 	 * might need to bail out based on ACLs now. */
941 	if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
942 	{
943 		if(ret == 1)
944 			goto send_reply;
945 		return ret;
946 	}
947 
948 	/* If this request does not have the recursion bit set, verify
949 	 * ACLs allow the snooping. */
950 	if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
951 		acl != acl_allow_snoop ) {
952 		sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
953 		sldns_buffer_write_at(c->buffer, 4,
954 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
955 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
956 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
957 			LDNS_RCODE_REFUSED);
958 		sldns_buffer_flip(c->buffer);
959 		server_stats_insrcode(&worker->stats, c->buffer);
960 		log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
961 			&repinfo->addr, repinfo->addrlen);
962 		goto send_reply;
963 	}
964 	h = query_info_hash(&qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
965 	if((e=slabhash_lookup(worker->env.msg_cache, h, &qinfo, 0))) {
966 		/* answer from cache - we have acquired a readlock on it */
967 		if(answer_from_cache(worker, &qinfo,
968 			(struct reply_info*)e->data,
969 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
970 			sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
971 			&edns)) {
972 			/* prefetch it if the prefetch TTL expired */
973 			if(worker->env.cfg->prefetch && *worker->env.now >=
974 				((struct reply_info*)e->data)->prefetch_ttl) {
975 				time_t leeway = ((struct reply_info*)e->
976 					data)->ttl - *worker->env.now;
977 				lock_rw_unlock(&e->lock);
978 				reply_and_prefetch(worker, &qinfo,
979 					sldns_buffer_read_u16_at(c->buffer, 2),
980 					repinfo, leeway);
981 				rc = 0;
982 				goto send_reply_rc;
983 			}
984 			lock_rw_unlock(&e->lock);
985 			goto send_reply;
986 		}
987 		verbose(VERB_ALGO, "answer from the cache failed");
988 		lock_rw_unlock(&e->lock);
989 	}
990 	if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
991 		if(answer_norec_from_cache(worker, &qinfo,
992 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
993 			sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
994 			&edns)) {
995 			goto send_reply;
996 		}
997 		verbose(VERB_ALGO, "answer norec from cache -- "
998 			"need to validate or not primed");
999 	}
1000 	sldns_buffer_rewind(c->buffer);
1001 	server_stats_querymiss(&worker->stats, worker);
1002 
1003 	if(verbosity >= VERB_CLIENT) {
1004 		if(c->type == comm_udp)
1005 			log_addr(VERB_CLIENT, "udp request from",
1006 				&repinfo->addr, repinfo->addrlen);
1007 		else	log_addr(VERB_CLIENT, "tcp request from",
1008 				&repinfo->addr, repinfo->addrlen);
1009 	}
1010 
1011 	/* grab a work request structure for this new request */
1012 	mesh_new_client(worker->env.mesh, &qinfo,
1013 		sldns_buffer_read_u16_at(c->buffer, 2),
1014 		&edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1015 	worker_mem_report(worker, NULL);
1016 	return 0;
1017 
1018 send_reply:
1019 	rc = 1;
1020 send_reply_rc:
1021 #ifdef USE_DNSTAP
1022 	if(worker->dtenv.log_client_response_messages)
1023 		dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1024 			c->type, c->buffer);
1025 #endif
1026 	return rc;
1027 }
1028 
1029 void
1030 worker_sighandler(int sig, void* arg)
1031 {
1032 	/* note that log, print, syscalls here give race conditions.
1033 	 * And cause hangups if the log-lock is held by the application. */
1034 	struct worker* worker = (struct worker*)arg;
1035 	switch(sig) {
1036 #ifdef SIGHUP
1037 		case SIGHUP:
1038 			comm_base_exit(worker->base);
1039 			break;
1040 #endif
1041 		case SIGINT:
1042 			worker->need_to_exit = 1;
1043 			comm_base_exit(worker->base);
1044 			break;
1045 #ifdef SIGQUIT
1046 		case SIGQUIT:
1047 			worker->need_to_exit = 1;
1048 			comm_base_exit(worker->base);
1049 			break;
1050 #endif
1051 		case SIGTERM:
1052 			worker->need_to_exit = 1;
1053 			comm_base_exit(worker->base);
1054 			break;
1055 		default:
1056 			/* unknown signal, ignored */
1057 			break;
1058 	}
1059 }
1060 
1061 /** restart statistics timer for worker, if enabled */
1062 static void
1063 worker_restart_timer(struct worker* worker)
1064 {
1065 	if(worker->env.cfg->stat_interval > 0) {
1066 		struct timeval tv;
1067 #ifndef S_SPLINT_S
1068 		tv.tv_sec = worker->env.cfg->stat_interval;
1069 		tv.tv_usec = 0;
1070 #endif
1071 		comm_timer_set(worker->stat_timer, &tv);
1072 	}
1073 }
1074 
1075 void worker_stat_timer_cb(void* arg)
1076 {
1077 	struct worker* worker = (struct worker*)arg;
1078 	server_stats_log(&worker->stats, worker, worker->thread_num);
1079 	mesh_stats(worker->env.mesh, "mesh has");
1080 	worker_mem_report(worker, NULL);
1081 	if(!worker->daemon->cfg->stat_cumulative) {
1082 		worker_stats_clear(worker);
1083 	}
1084 	/* start next timer */
1085 	worker_restart_timer(worker);
1086 }
1087 
1088 void worker_probe_timer_cb(void* arg)
1089 {
1090 	struct worker* worker = (struct worker*)arg;
1091 	struct timeval tv;
1092 #ifndef S_SPLINT_S
1093 	tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1094 	tv.tv_usec = 0;
1095 #endif
1096 	if(tv.tv_sec != 0)
1097 		comm_timer_set(worker->env.probe_timer, &tv);
1098 }
1099 
1100 struct worker*
1101 worker_create(struct daemon* daemon, int id, int* ports, int n)
1102 {
1103 	unsigned int seed;
1104 	struct worker* worker = (struct worker*)calloc(1,
1105 		sizeof(struct worker));
1106 	if(!worker)
1107 		return NULL;
1108 	worker->numports = n;
1109 	worker->ports = (int*)memdup(ports, sizeof(int)*n);
1110 	if(!worker->ports) {
1111 		free(worker);
1112 		return NULL;
1113 	}
1114 	worker->daemon = daemon;
1115 	worker->thread_num = id;
1116 	if(!(worker->cmd = tube_create())) {
1117 		free(worker->ports);
1118 		free(worker);
1119 		return NULL;
1120 	}
1121 	/* create random state here to avoid locking trouble in RAND_bytes */
1122 	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1123 		(((unsigned int)worker->thread_num)<<17);
1124 		/* shift thread_num so it does not match out pid bits */
1125 	if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1126 		seed = 0;
1127 		log_err("could not init random numbers.");
1128 		tube_delete(worker->cmd);
1129 		free(worker->ports);
1130 		free(worker);
1131 		return NULL;
1132 	}
1133 	seed = 0;
1134 #ifdef USE_DNSTAP
1135 	if(daemon->cfg->dnstap) {
1136 		log_assert(daemon->dtenv != NULL);
1137 		memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1138 		if(!dt_init(&worker->dtenv))
1139 			fatal_exit("dt_init failed");
1140 	}
1141 #endif
1142 	return worker;
1143 }
1144 
1145 int
1146 worker_init(struct worker* worker, struct config_file *cfg,
1147 	struct listen_port* ports, int do_sigs)
1148 {
1149 #ifdef USE_DNSTAP
1150 	struct dt_env* dtenv = &worker->dtenv;
1151 #else
1152 	void* dtenv = NULL;
1153 #endif
1154 	worker->need_to_exit = 0;
1155 	worker->base = comm_base_create(do_sigs);
1156 	if(!worker->base) {
1157 		log_err("could not create event handling base");
1158 		worker_delete(worker);
1159 		return 0;
1160 	}
1161 	comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1162 		&worker_start_accept, worker);
1163 	if(do_sigs) {
1164 #ifdef SIGHUP
1165 		ub_thread_sig_unblock(SIGHUP);
1166 #endif
1167 		ub_thread_sig_unblock(SIGINT);
1168 #ifdef SIGQUIT
1169 		ub_thread_sig_unblock(SIGQUIT);
1170 #endif
1171 		ub_thread_sig_unblock(SIGTERM);
1172 #ifndef LIBEVENT_SIGNAL_PROBLEM
1173 		worker->comsig = comm_signal_create(worker->base,
1174 			worker_sighandler, worker);
1175 		if(!worker->comsig
1176 #ifdef SIGHUP
1177 			|| !comm_signal_bind(worker->comsig, SIGHUP)
1178 #endif
1179 #ifdef SIGQUIT
1180 			|| !comm_signal_bind(worker->comsig, SIGQUIT)
1181 #endif
1182 			|| !comm_signal_bind(worker->comsig, SIGTERM)
1183 			|| !comm_signal_bind(worker->comsig, SIGINT)) {
1184 			log_err("could not create signal handlers");
1185 			worker_delete(worker);
1186 			return 0;
1187 		}
1188 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1189 		if(!daemon_remote_open_accept(worker->daemon->rc,
1190 			worker->daemon->rc_ports, worker)) {
1191 			worker_delete(worker);
1192 			return 0;
1193 		}
1194 #ifdef UB_ON_WINDOWS
1195 		wsvc_setup_worker(worker);
1196 #endif /* UB_ON_WINDOWS */
1197 	} else { /* !do_sigs */
1198 		worker->comsig = NULL;
1199 	}
1200 	worker->front = listen_create(worker->base, ports,
1201 		cfg->msg_buffer_size, (int)cfg->incoming_num_tcp,
1202 		worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1203 		worker);
1204 	if(!worker->front) {
1205 		log_err("could not create listening sockets");
1206 		worker_delete(worker);
1207 		return 0;
1208 	}
1209 	worker->back = outside_network_create(worker->base,
1210 		cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports,
1211 		cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
1212 		cfg->do_tcp?cfg->outgoing_num_tcp:0,
1213 		worker->daemon->env->infra_cache, worker->rndstate,
1214 		cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1215 		cfg->unwanted_threshold, &worker_alloc_cleanup, worker,
1216 		cfg->do_udp, worker->daemon->connect_sslctx, cfg->delay_close,
1217 		dtenv);
1218 	if(!worker->back) {
1219 		log_err("could not create outgoing sockets");
1220 		worker_delete(worker);
1221 		return 0;
1222 	}
1223 	/* start listening to commands */
1224 	if(!tube_setup_bg_listen(worker->cmd, worker->base,
1225 		&worker_handle_control_cmd, worker)) {
1226 		log_err("could not create control compt.");
1227 		worker_delete(worker);
1228 		return 0;
1229 	}
1230 	worker->stat_timer = comm_timer_create(worker->base,
1231 		worker_stat_timer_cb, worker);
1232 	if(!worker->stat_timer) {
1233 		log_err("could not create statistics timer");
1234 	}
1235 
1236 	/* we use the msg_buffer_size as a good estimate for what the
1237 	 * user wants for memory usage sizes */
1238 	worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1239 	if(!worker->scratchpad) {
1240 		log_err("malloc failure");
1241 		worker_delete(worker);
1242 		return 0;
1243 	}
1244 
1245 	server_stats_init(&worker->stats, cfg);
1246 	alloc_init(&worker->alloc, &worker->daemon->superalloc,
1247 		worker->thread_num);
1248 	alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1249 	worker->env = *worker->daemon->env;
1250 	comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1251 	if(worker->thread_num == 0)
1252 		log_set_time(worker->env.now);
1253 	worker->env.worker = worker;
1254 	worker->env.send_query = &worker_send_query;
1255 	worker->env.alloc = &worker->alloc;
1256 	worker->env.rnd = worker->rndstate;
1257 	worker->env.scratch = worker->scratchpad;
1258 	worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1259 	worker->env.detach_subs = &mesh_detach_subs;
1260 	worker->env.attach_sub = &mesh_attach_sub;
1261 	worker->env.kill_sub = &mesh_state_delete;
1262 	worker->env.detect_cycle = &mesh_detect_cycle;
1263 	worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1264 	if(!(worker->env.fwds = forwards_create()) ||
1265 		!forwards_apply_cfg(worker->env.fwds, cfg)) {
1266 		log_err("Could not set forward zones");
1267 		worker_delete(worker);
1268 		return 0;
1269 	}
1270 	if(!(worker->env.hints = hints_create()) ||
1271 		!hints_apply_cfg(worker->env.hints, cfg)) {
1272 		log_err("Could not set root or stub hints");
1273 		worker_delete(worker);
1274 		return 0;
1275 	}
1276 	/* one probe timer per process -- if we have 5011 anchors */
1277 	if(autr_get_num_anchors(worker->env.anchors) > 0
1278 #ifndef THREADS_DISABLED
1279 		&& worker->thread_num == 0
1280 #endif
1281 		) {
1282 		struct timeval tv;
1283 		tv.tv_sec = 0;
1284 		tv.tv_usec = 0;
1285 		worker->env.probe_timer = comm_timer_create(worker->base,
1286 			worker_probe_timer_cb, worker);
1287 		if(!worker->env.probe_timer) {
1288 			log_err("could not create 5011-probe timer");
1289 		} else {
1290 			/* let timer fire, then it can reset itself */
1291 			comm_timer_set(worker->env.probe_timer, &tv);
1292 		}
1293 	}
1294 	if(!worker->env.mesh || !worker->env.scratch_buffer) {
1295 		worker_delete(worker);
1296 		return 0;
1297 	}
1298 	worker_mem_report(worker, NULL);
1299 	/* if statistics enabled start timer */
1300 	if(worker->env.cfg->stat_interval > 0) {
1301 		verbose(VERB_ALGO, "set statistics interval %d secs",
1302 			worker->env.cfg->stat_interval);
1303 		worker_restart_timer(worker);
1304 	}
1305 	return 1;
1306 }
1307 
1308 void
1309 worker_work(struct worker* worker)
1310 {
1311 	comm_base_dispatch(worker->base);
1312 }
1313 
1314 void
1315 worker_delete(struct worker* worker)
1316 {
1317 	if(!worker)
1318 		return;
1319 	if(worker->env.mesh && verbosity >= VERB_OPS) {
1320 		server_stats_log(&worker->stats, worker, worker->thread_num);
1321 		mesh_stats(worker->env.mesh, "mesh has");
1322 		worker_mem_report(worker, NULL);
1323 	}
1324 	outside_network_quit_prepare(worker->back);
1325 	mesh_delete(worker->env.mesh);
1326 	sldns_buffer_free(worker->env.scratch_buffer);
1327 	forwards_delete(worker->env.fwds);
1328 	hints_delete(worker->env.hints);
1329 	listen_delete(worker->front);
1330 	outside_network_delete(worker->back);
1331 	comm_signal_delete(worker->comsig);
1332 	tube_delete(worker->cmd);
1333 	comm_timer_delete(worker->stat_timer);
1334 	comm_timer_delete(worker->env.probe_timer);
1335 	free(worker->ports);
1336 	if(worker->thread_num == 0) {
1337 		log_set_time(NULL);
1338 #ifdef UB_ON_WINDOWS
1339 		wsvc_desetup_worker(worker);
1340 #endif /* UB_ON_WINDOWS */
1341 	}
1342 	comm_base_delete(worker->base);
1343 	ub_randfree(worker->rndstate);
1344 	alloc_clear(&worker->alloc);
1345 	regional_destroy(worker->scratchpad);
1346 	free(worker);
1347 }
1348 
1349 struct outbound_entry*
1350 worker_send_query(uint8_t* qname, size_t qnamelen, uint16_t qtype,
1351 	uint16_t qclass, uint16_t flags, int dnssec, int want_dnssec,
1352 	int nocaps, struct sockaddr_storage* addr, socklen_t addrlen,
1353 	uint8_t* zone, size_t zonelen, struct module_qstate* q)
1354 {
1355 	struct worker* worker = q->env->worker;
1356 	struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1357 		q->region, sizeof(*e));
1358 	if(!e)
1359 		return NULL;
1360 	e->qstate = q;
1361 	e->qsent = outnet_serviced_query(worker->back, qname,
1362 		qnamelen, qtype, qclass, flags, dnssec, want_dnssec, nocaps,
1363 		q->env->cfg->tcp_upstream, q->env->cfg->ssl_upstream, addr,
1364 		addrlen, zone, zonelen, worker_handle_service_reply, e,
1365 		worker->back->udp_buff);
1366 	if(!e->qsent) {
1367 		return NULL;
1368 	}
1369 	return e;
1370 }
1371 
1372 void
1373 worker_alloc_cleanup(void* arg)
1374 {
1375 	struct worker* worker = (struct worker*)arg;
1376 	slabhash_clear(&worker->env.rrset_cache->table);
1377 	slabhash_clear(worker->env.msg_cache);
1378 }
1379 
1380 void worker_stats_clear(struct worker* worker)
1381 {
1382 	server_stats_init(&worker->stats, worker->env.cfg);
1383 	mesh_stats_clear(worker->env.mesh);
1384 	worker->back->unwanted_replies = 0;
1385 	worker->back->num_tcp_outgoing = 0;
1386 }
1387 
1388 void worker_start_accept(void* arg)
1389 {
1390 	struct worker* worker = (struct worker*)arg;
1391 	listen_start_accept(worker->front);
1392 	if(worker->thread_num == 0)
1393 		daemon_remote_start_accept(worker->daemon->rc);
1394 }
1395 
1396 void worker_stop_accept(void* arg)
1397 {
1398 	struct worker* worker = (struct worker*)arg;
1399 	listen_stop_accept(worker->front);
1400 	if(worker->thread_num == 0)
1401 		daemon_remote_stop_accept(worker->daemon->rc);
1402 }
1403 
1404 /* --- fake callbacks for fptr_wlist to work --- */
1405 struct outbound_entry* libworker_send_query(uint8_t* ATTR_UNUSED(qname),
1406 	size_t ATTR_UNUSED(qnamelen), uint16_t ATTR_UNUSED(qtype),
1407 	uint16_t ATTR_UNUSED(qclass), uint16_t ATTR_UNUSED(flags),
1408 	int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1409 	int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
1410 	socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1411 	size_t ATTR_UNUSED(zonelen), struct module_qstate* ATTR_UNUSED(q))
1412 {
1413 	log_assert(0);
1414 	return 0;
1415 }
1416 
1417 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1418 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1419         struct comm_reply* ATTR_UNUSED(reply_info))
1420 {
1421 	log_assert(0);
1422 	return 0;
1423 }
1424 
1425 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1426 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1427         struct comm_reply* ATTR_UNUSED(reply_info))
1428 {
1429 	log_assert(0);
1430 	return 0;
1431 }
1432 
1433 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1434         uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1435         int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1436 {
1437 	log_assert(0);
1438 }
1439 
1440 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1441         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1442 	char* ATTR_UNUSED(why_bogus))
1443 {
1444 	log_assert(0);
1445 }
1446 
1447 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1448         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1449 	char* ATTR_UNUSED(why_bogus))
1450 {
1451 	log_assert(0);
1452 }
1453 
1454 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1455         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1456 	char* ATTR_UNUSED(why_bogus))
1457 {
1458 	log_assert(0);
1459 }
1460 
1461 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1462 {
1463 	log_assert(0);
1464 	return 0;
1465 }
1466 
1467 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1468 {
1469         log_assert(0);
1470         return 0;
1471 }
1472 
1473 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1474 {
1475         log_assert(0);
1476         return 0;
1477 }
1478 
1479