xref: /freebsd/contrib/unbound/daemon/worker.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
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 		return 0;
487 	}
488 	if(must_validate) {
489 		switch(check_delegation_secure(msg->rep)) {
490 		case sec_status_unchecked:
491 			/* some rrsets have not been verified yet, go and
492 			 * let validator do that */
493 			return 0;
494 		case sec_status_bogus:
495 			/* some rrsets are bogus, reply servfail */
496 			edns->edns_version = EDNS_ADVERTISED_VERSION;
497 			edns->udp_size = EDNS_ADVERTISED_SIZE;
498 			edns->ext_rcode = 0;
499 			edns->bits &= EDNS_DO;
500 			if(!edns_opt_inplace_reply(edns, worker->scratchpad))
501 				return 0;
502 			error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
503 				&msg->qinfo, id, flags, edns);
504 			if(worker->stats.extended) {
505 				worker->stats.ans_bogus++;
506 				worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
507 			}
508 			return 1;
509 		case sec_status_secure:
510 			/* all rrsets are secure */
511 			/* remove non-secure rrsets from the add. section*/
512 			if(worker->env.cfg->val_clean_additional)
513 				deleg_remove_nonsecure_additional(msg->rep);
514 			secure = 1;
515 			break;
516 		case sec_status_indeterminate:
517 		case sec_status_insecure:
518 		default:
519 			/* not secure */
520 			secure = 0;
521 			break;
522 		}
523 	}
524 	/* return this delegation from the cache */
525 	edns->edns_version = EDNS_ADVERTISED_VERSION;
526 	edns->udp_size = EDNS_ADVERTISED_SIZE;
527 	edns->ext_rcode = 0;
528 	edns->bits &= EDNS_DO;
529 	if(!edns_opt_inplace_reply(edns, worker->scratchpad))
530 		return 0;
531 	msg->rep->flags |= BIT_QR|BIT_RA;
532 	if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags,
533 		repinfo->c->buffer, 0, 1, worker->scratchpad,
534 		udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
535 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
536 			&msg->qinfo, id, flags, edns);
537 	}
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 			return 0;
578 		}
579 	}
580 	/* check security status of the cached answer */
581 	if( rep->security == sec_status_bogus && must_validate) {
582 		/* BAD cached */
583 		edns->edns_version = EDNS_ADVERTISED_VERSION;
584 		edns->udp_size = EDNS_ADVERTISED_SIZE;
585 		edns->ext_rcode = 0;
586 		edns->bits &= EDNS_DO;
587 		if(!edns_opt_inplace_reply(edns, worker->scratchpad))
588 			return 0;
589 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
590 			qinfo, id, flags, edns);
591 		rrset_array_unlock_touch(worker->env.rrset_cache,
592 			worker->scratchpad, rep->ref, rep->rrset_count);
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(!edns_opt_inplace_reply(edns, worker->scratchpad))
620 		return 0;
621 	if(!reply_info_answer_encode(qinfo, rep, id, flags,
622 		repinfo->c->buffer, timenow, 1, worker->scratchpad,
623 		udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
624 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
625 			qinfo, id, flags, edns);
626 	}
627 	/* cannot send the reply right now, because blocking network syscall
628 	 * is bad while holding locks. */
629 	rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
630 		rep->ref, rep->rrset_count);
631 	if(worker->stats.extended) {
632 		if(secure) worker->stats.ans_secure++;
633 		server_stats_insrcode(&worker->stats, repinfo->c->buffer);
634 	}
635 	/* go and return this buffer to the client */
636 	return 1;
637 }
638 
639 /** Reply to client and perform prefetch to keep cache up to date */
640 static void
641 reply_and_prefetch(struct worker* worker, struct query_info* qinfo,
642 	uint16_t flags, struct comm_reply* repinfo, time_t leeway)
643 {
644 	/* first send answer to client to keep its latency
645 	 * as small as a cachereply */
646 	comm_point_send_reply(repinfo);
647 	server_stats_prefetch(&worker->stats, worker);
648 
649 	/* create the prefetch in the mesh as a normal lookup without
650 	 * client addrs waiting, which has the cache blacklisted (to bypass
651 	 * the cache and go to the network for the data). */
652 	/* this (potentially) runs the mesh for the new query */
653 	mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
654 		PREFETCH_EXPIRY_ADD);
655 }
656 
657 /**
658  * Fill CH class answer into buffer. Keeps query.
659  * @param pkt: buffer
660  * @param str: string to put into text record (<255).
661  * @param edns: edns reply information.
662  * @param worker: worker with scratch region.
663  */
664 static void
665 chaos_replystr(sldns_buffer* pkt, const char* str, struct edns_data* edns,
666 	struct worker* worker)
667 {
668 	size_t len = strlen(str);
669 	unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
670 	unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
671 	if(len>255) len=255; /* cap size of TXT record */
672 	sldns_buffer_clear(pkt);
673 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
674 	sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
675 	if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
676 	if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
677 	sldns_buffer_write_u16(pkt, 1); /* qdcount */
678 	sldns_buffer_write_u16(pkt, 1); /* ancount */
679 	sldns_buffer_write_u16(pkt, 0); /* nscount */
680 	sldns_buffer_write_u16(pkt, 0); /* arcount */
681 	(void)query_dname_len(pkt); /* skip qname */
682 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
683 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
684 	sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
685 	sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
686 	sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
687 	sldns_buffer_write_u32(pkt, 0); /* TTL */
688 	sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
689 	sldns_buffer_write_u8(pkt, len);
690 	sldns_buffer_write(pkt, str, len);
691 	sldns_buffer_flip(pkt);
692 	edns->edns_version = EDNS_ADVERTISED_VERSION;
693 	edns->udp_size = EDNS_ADVERTISED_SIZE;
694 	edns->bits &= EDNS_DO;
695 	if(!edns_opt_inplace_reply(edns, worker->scratchpad))
696 		edns->opt_list = NULL;
697 	attach_edns_record(pkt, edns);
698 }
699 
700 /**
701  * Answer CH class queries.
702  * @param w: worker
703  * @param qinfo: query info. Pointer into packet buffer.
704  * @param edns: edns info from query.
705  * @param pkt: packet buffer.
706  * @return: true if a reply is to be sent.
707  */
708 static int
709 answer_chaos(struct worker* w, struct query_info* qinfo,
710 	struct edns_data* edns, sldns_buffer* pkt)
711 {
712 	struct config_file* cfg = w->env.cfg;
713 	if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
714 		return 0;
715 	if(query_dname_compare(qinfo->qname,
716 		(uint8_t*)"\002id\006server") == 0 ||
717 		query_dname_compare(qinfo->qname,
718 		(uint8_t*)"\010hostname\004bind") == 0)
719 	{
720 		if(cfg->hide_identity)
721 			return 0;
722 		if(cfg->identity==NULL || cfg->identity[0]==0) {
723 			char buf[MAXHOSTNAMELEN+1];
724 			if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
725 				buf[MAXHOSTNAMELEN] = 0;
726 				chaos_replystr(pkt, buf, edns, w);
727 			} else 	{
728 				log_err("gethostname: %s", strerror(errno));
729 				chaos_replystr(pkt, "no hostname", edns, w);
730 			}
731 		}
732 		else 	chaos_replystr(pkt, cfg->identity, edns, w);
733 		return 1;
734 	}
735 	if(query_dname_compare(qinfo->qname,
736 		(uint8_t*)"\007version\006server") == 0 ||
737 		query_dname_compare(qinfo->qname,
738 		(uint8_t*)"\007version\004bind") == 0)
739 	{
740 		if(cfg->hide_version)
741 			return 0;
742 		if(cfg->version==NULL || cfg->version[0]==0)
743 			chaos_replystr(pkt, PACKAGE_STRING, edns, w);
744 		else 	chaos_replystr(pkt, cfg->version, edns, w);
745 		return 1;
746 	}
747 	return 0;
748 }
749 
750 static int
751 deny_refuse(struct comm_point* c, enum acl_access acl,
752 	enum acl_access deny, enum acl_access refuse,
753 	struct worker* worker, struct comm_reply* repinfo)
754 {
755 	if(acl == deny) {
756 		comm_point_drop_reply(repinfo);
757 		if(worker->stats.extended)
758 			worker->stats.unwanted_queries++;
759 		return 0;
760 	} else if(acl == refuse) {
761 		log_addr(VERB_ALGO, "refused query from",
762 			&repinfo->addr, repinfo->addrlen);
763 		log_buf(VERB_ALGO, "refuse", c->buffer);
764 		if(worker->stats.extended)
765 			worker->stats.unwanted_queries++;
766 		if(worker_check_request(c->buffer, worker) == -1) {
767 			comm_point_drop_reply(repinfo);
768 			return 0; /* discard this */
769 		}
770 		sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
771 		sldns_buffer_write_at(c->buffer, 4,
772 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
773 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
774 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
775 			LDNS_RCODE_REFUSED);
776 		return 1;
777 	}
778 
779 	return -1;
780 }
781 
782 static int
783 deny_refuse_all(struct comm_point* c, enum acl_access acl,
784 	struct worker* worker, struct comm_reply* repinfo)
785 {
786 	return deny_refuse(c, acl, acl_deny, acl_refuse, worker, repinfo);
787 }
788 
789 static int
790 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
791 	struct worker* worker, struct comm_reply* repinfo)
792 {
793 	return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, worker, repinfo);
794 }
795 
796 int
797 worker_handle_request(struct comm_point* c, void* arg, int error,
798 	struct comm_reply* repinfo)
799 {
800 	struct worker* worker = (struct worker*)arg;
801 	int ret;
802 	hashvalue_t h;
803 	struct lruhash_entry* e;
804 	struct query_info qinfo;
805 	struct edns_data edns;
806 	enum acl_access acl;
807 	int rc = 0;
808 
809 	if(error != NETEVENT_NOERROR) {
810 		/* some bad tcp query DNS formats give these error calls */
811 		verbose(VERB_ALGO, "handle request called with err=%d", error);
812 		return 0;
813 	}
814 #ifdef USE_DNSTAP
815 	if(worker->dtenv.log_client_query_messages)
816 		dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
817 			c->buffer);
818 #endif
819 	acl = acl_list_lookup(worker->daemon->acl, &repinfo->addr,
820 		repinfo->addrlen);
821 	if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
822 	{
823 		if(ret == 1)
824 			goto send_reply;
825 		return ret;
826 	}
827 	if((ret=worker_check_request(c->buffer, worker)) != 0) {
828 		verbose(VERB_ALGO, "worker check request: bad query.");
829 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
830 		if(ret != -1) {
831 			LDNS_QR_SET(sldns_buffer_begin(c->buffer));
832 			LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
833 			return 1;
834 		}
835 		comm_point_drop_reply(repinfo);
836 		return 0;
837 	}
838 	worker->stats.num_queries++;
839 	/* see if query is in the cache */
840 	if(!query_info_parse(&qinfo, c->buffer)) {
841 		verbose(VERB_ALGO, "worker parse request: formerror.");
842 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
843 		if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
844 			comm_point_drop_reply(repinfo);
845 			return 0;
846 		}
847 		sldns_buffer_rewind(c->buffer);
848 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
849 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
850 			LDNS_RCODE_FORMERR);
851 		server_stats_insrcode(&worker->stats, c->buffer);
852 		goto send_reply;
853 	}
854 	if(worker->env.cfg->log_queries) {
855 		char ip[128];
856 		addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
857 		log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
858 	}
859 	if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
860 		qinfo.qtype == LDNS_RR_TYPE_IXFR) {
861 		verbose(VERB_ALGO, "worker request: refused zone transfer.");
862 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
863 		sldns_buffer_rewind(c->buffer);
864 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
865 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
866 			LDNS_RCODE_REFUSED);
867 		if(worker->stats.extended) {
868 			worker->stats.qtype[qinfo.qtype]++;
869 			server_stats_insrcode(&worker->stats, c->buffer);
870 		}
871 		goto send_reply;
872 	}
873 	if((ret=parse_edns_from_pkt(c->buffer, &edns, worker->scratchpad)) != 0) {
874 		struct edns_data reply_edns;
875 		verbose(VERB_ALGO, "worker parse edns: formerror.");
876 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
877 		memset(&reply_edns, 0, sizeof(reply_edns));
878 		reply_edns.edns_present = 1;
879 		reply_edns.udp_size = EDNS_ADVERTISED_SIZE;
880 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
881 		error_encode(c->buffer, ret, &qinfo,
882 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
883 			sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
884 		regional_free_all(worker->scratchpad);
885 		server_stats_insrcode(&worker->stats, c->buffer);
886 		goto send_reply;
887 	}
888 	if(edns.edns_present && edns.edns_version != 0) {
889 		edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
890 		edns.edns_version = EDNS_ADVERTISED_VERSION;
891 		edns.udp_size = EDNS_ADVERTISED_SIZE;
892 		edns.bits &= EDNS_DO;
893 		edns.opt_list = NULL;
894 		verbose(VERB_ALGO, "query with bad edns version.");
895 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
896 		error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
897 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
898 			sldns_buffer_read_u16_at(c->buffer, 2), NULL);
899 		attach_edns_record(c->buffer, &edns);
900 		regional_free_all(worker->scratchpad);
901 		goto send_reply;
902 	}
903 	if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
904 		worker->daemon->cfg->harden_short_bufsize) {
905 		verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
906 			(int)edns.udp_size);
907 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
908 		edns.udp_size = NORMAL_UDP_SIZE;
909 	}
910 	if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
911 		c->type == comm_udp) {
912 		verbose(VERB_QUERY,
913 			"worker request: max UDP reply size modified"
914 			" (%d to max-udp-size)", (int)edns.udp_size);
915 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
916 		edns.udp_size = worker->daemon->cfg->max_udp_size;
917 	}
918 	if(edns.udp_size < LDNS_HEADER_SIZE) {
919 		verbose(VERB_ALGO, "worker request: edns is too small.");
920 		log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
921 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
922 		LDNS_TC_SET(sldns_buffer_begin(c->buffer));
923 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
924 			LDNS_RCODE_SERVFAIL);
925 		sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
926 		sldns_buffer_write_at(c->buffer, 4,
927 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
928 		sldns_buffer_flip(c->buffer);
929 		regional_free_all(worker->scratchpad);
930 		goto send_reply;
931 	}
932 	if(worker->stats.extended)
933 		server_stats_insquery(&worker->stats, c, qinfo.qtype,
934 			qinfo.qclass, &edns, repinfo);
935 	if(c->type != comm_udp)
936 		edns.udp_size = 65535; /* max size for TCP replies */
937 	if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
938 		&edns, c->buffer)) {
939 		server_stats_insrcode(&worker->stats, c->buffer);
940 		regional_free_all(worker->scratchpad);
941 		goto send_reply;
942 	}
943 	if(local_zones_answer(worker->daemon->local_zones, &qinfo, &edns,
944 		c->buffer, worker->scratchpad, repinfo)) {
945 		regional_free_all(worker->scratchpad);
946 		if(sldns_buffer_limit(c->buffer) == 0) {
947 			comm_point_drop_reply(repinfo);
948 			return 0;
949 		}
950 		server_stats_insrcode(&worker->stats, c->buffer);
951 		goto send_reply;
952 	}
953 
954 	/* We've looked in our local zones. If the answer isn't there, we
955 	 * might need to bail out based on ACLs now. */
956 	if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
957 	{
958 		regional_free_all(worker->scratchpad);
959 		if(ret == 1)
960 			goto send_reply;
961 		return ret;
962 	}
963 
964 	/* If this request does not have the recursion bit set, verify
965 	 * ACLs allow the snooping. */
966 	if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
967 		acl != acl_allow_snoop ) {
968 		sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
969 		sldns_buffer_write_at(c->buffer, 4,
970 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
971 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
972 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
973 			LDNS_RCODE_REFUSED);
974 		sldns_buffer_flip(c->buffer);
975 		regional_free_all(worker->scratchpad);
976 		server_stats_insrcode(&worker->stats, c->buffer);
977 		log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
978 			&repinfo->addr, repinfo->addrlen);
979 		goto send_reply;
980 	}
981 	h = query_info_hash(&qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
982 	if((e=slabhash_lookup(worker->env.msg_cache, h, &qinfo, 0))) {
983 		/* answer from cache - we have acquired a readlock on it */
984 		if(answer_from_cache(worker, &qinfo,
985 			(struct reply_info*)e->data,
986 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
987 			sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
988 			&edns)) {
989 			/* prefetch it if the prefetch TTL expired */
990 			if(worker->env.cfg->prefetch && *worker->env.now >=
991 				((struct reply_info*)e->data)->prefetch_ttl) {
992 				time_t leeway = ((struct reply_info*)e->
993 					data)->ttl - *worker->env.now;
994 				lock_rw_unlock(&e->lock);
995 				reply_and_prefetch(worker, &qinfo,
996 					sldns_buffer_read_u16_at(c->buffer, 2),
997 					repinfo, leeway);
998 				rc = 0;
999 				regional_free_all(worker->scratchpad);
1000 				goto send_reply_rc;
1001 			}
1002 			lock_rw_unlock(&e->lock);
1003 			regional_free_all(worker->scratchpad);
1004 			goto send_reply;
1005 		}
1006 		verbose(VERB_ALGO, "answer from the cache failed");
1007 		lock_rw_unlock(&e->lock);
1008 	}
1009 	if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
1010 		if(answer_norec_from_cache(worker, &qinfo,
1011 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1012 			sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1013 			&edns)) {
1014 			regional_free_all(worker->scratchpad);
1015 			goto send_reply;
1016 		}
1017 		verbose(VERB_ALGO, "answer norec from cache -- "
1018 			"need to validate or not primed");
1019 	}
1020 	sldns_buffer_rewind(c->buffer);
1021 	server_stats_querymiss(&worker->stats, worker);
1022 
1023 	if(verbosity >= VERB_CLIENT) {
1024 		if(c->type == comm_udp)
1025 			log_addr(VERB_CLIENT, "udp request from",
1026 				&repinfo->addr, repinfo->addrlen);
1027 		else	log_addr(VERB_CLIENT, "tcp request from",
1028 				&repinfo->addr, repinfo->addrlen);
1029 	}
1030 
1031 	/* grab a work request structure for this new request */
1032 	mesh_new_client(worker->env.mesh, &qinfo,
1033 		sldns_buffer_read_u16_at(c->buffer, 2),
1034 		&edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1035 	regional_free_all(worker->scratchpad);
1036 	worker_mem_report(worker, NULL);
1037 	return 0;
1038 
1039 send_reply:
1040 	rc = 1;
1041 send_reply_rc:
1042 #ifdef USE_DNSTAP
1043 	if(worker->dtenv.log_client_response_messages)
1044 		dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1045 			c->type, c->buffer);
1046 #endif
1047 	return rc;
1048 }
1049 
1050 void
1051 worker_sighandler(int sig, void* arg)
1052 {
1053 	/* note that log, print, syscalls here give race conditions.
1054 	 * And cause hangups if the log-lock is held by the application. */
1055 	struct worker* worker = (struct worker*)arg;
1056 	switch(sig) {
1057 #ifdef SIGHUP
1058 		case SIGHUP:
1059 			comm_base_exit(worker->base);
1060 			break;
1061 #endif
1062 		case SIGINT:
1063 			worker->need_to_exit = 1;
1064 			comm_base_exit(worker->base);
1065 			break;
1066 #ifdef SIGQUIT
1067 		case SIGQUIT:
1068 			worker->need_to_exit = 1;
1069 			comm_base_exit(worker->base);
1070 			break;
1071 #endif
1072 		case SIGTERM:
1073 			worker->need_to_exit = 1;
1074 			comm_base_exit(worker->base);
1075 			break;
1076 		default:
1077 			/* unknown signal, ignored */
1078 			break;
1079 	}
1080 }
1081 
1082 /** restart statistics timer for worker, if enabled */
1083 static void
1084 worker_restart_timer(struct worker* worker)
1085 {
1086 	if(worker->env.cfg->stat_interval > 0) {
1087 		struct timeval tv;
1088 #ifndef S_SPLINT_S
1089 		tv.tv_sec = worker->env.cfg->stat_interval;
1090 		tv.tv_usec = 0;
1091 #endif
1092 		comm_timer_set(worker->stat_timer, &tv);
1093 	}
1094 }
1095 
1096 void worker_stat_timer_cb(void* arg)
1097 {
1098 	struct worker* worker = (struct worker*)arg;
1099 	server_stats_log(&worker->stats, worker, worker->thread_num);
1100 	mesh_stats(worker->env.mesh, "mesh has");
1101 	worker_mem_report(worker, NULL);
1102 	if(!worker->daemon->cfg->stat_cumulative) {
1103 		worker_stats_clear(worker);
1104 	}
1105 	/* start next timer */
1106 	worker_restart_timer(worker);
1107 }
1108 
1109 void worker_probe_timer_cb(void* arg)
1110 {
1111 	struct worker* worker = (struct worker*)arg;
1112 	struct timeval tv;
1113 #ifndef S_SPLINT_S
1114 	tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1115 	tv.tv_usec = 0;
1116 #endif
1117 	if(tv.tv_sec != 0)
1118 		comm_timer_set(worker->env.probe_timer, &tv);
1119 }
1120 
1121 struct worker*
1122 worker_create(struct daemon* daemon, int id, int* ports, int n)
1123 {
1124 	unsigned int seed;
1125 	struct worker* worker = (struct worker*)calloc(1,
1126 		sizeof(struct worker));
1127 	if(!worker)
1128 		return NULL;
1129 	worker->numports = n;
1130 	worker->ports = (int*)memdup(ports, sizeof(int)*n);
1131 	if(!worker->ports) {
1132 		free(worker);
1133 		return NULL;
1134 	}
1135 	worker->daemon = daemon;
1136 	worker->thread_num = id;
1137 	if(!(worker->cmd = tube_create())) {
1138 		free(worker->ports);
1139 		free(worker);
1140 		return NULL;
1141 	}
1142 	/* create random state here to avoid locking trouble in RAND_bytes */
1143 	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1144 		(((unsigned int)worker->thread_num)<<17);
1145 		/* shift thread_num so it does not match out pid bits */
1146 	if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1147 		seed = 0;
1148 		log_err("could not init random numbers.");
1149 		tube_delete(worker->cmd);
1150 		free(worker->ports);
1151 		free(worker);
1152 		return NULL;
1153 	}
1154 	seed = 0;
1155 #ifdef USE_DNSTAP
1156 	if(daemon->cfg->dnstap) {
1157 		log_assert(daemon->dtenv != NULL);
1158 		memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1159 		if(!dt_init(&worker->dtenv))
1160 			fatal_exit("dt_init failed");
1161 	}
1162 #endif
1163 	return worker;
1164 }
1165 
1166 int
1167 worker_init(struct worker* worker, struct config_file *cfg,
1168 	struct listen_port* ports, int do_sigs)
1169 {
1170 #ifdef USE_DNSTAP
1171 	struct dt_env* dtenv = &worker->dtenv;
1172 #else
1173 	void* dtenv = NULL;
1174 #endif
1175 	worker->need_to_exit = 0;
1176 	worker->base = comm_base_create(do_sigs);
1177 	if(!worker->base) {
1178 		log_err("could not create event handling base");
1179 		worker_delete(worker);
1180 		return 0;
1181 	}
1182 	comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1183 		&worker_start_accept, worker);
1184 	if(do_sigs) {
1185 #ifdef SIGHUP
1186 		ub_thread_sig_unblock(SIGHUP);
1187 #endif
1188 		ub_thread_sig_unblock(SIGINT);
1189 #ifdef SIGQUIT
1190 		ub_thread_sig_unblock(SIGQUIT);
1191 #endif
1192 		ub_thread_sig_unblock(SIGTERM);
1193 #ifndef LIBEVENT_SIGNAL_PROBLEM
1194 		worker->comsig = comm_signal_create(worker->base,
1195 			worker_sighandler, worker);
1196 		if(!worker->comsig
1197 #ifdef SIGHUP
1198 			|| !comm_signal_bind(worker->comsig, SIGHUP)
1199 #endif
1200 #ifdef SIGQUIT
1201 			|| !comm_signal_bind(worker->comsig, SIGQUIT)
1202 #endif
1203 			|| !comm_signal_bind(worker->comsig, SIGTERM)
1204 			|| !comm_signal_bind(worker->comsig, SIGINT)) {
1205 			log_err("could not create signal handlers");
1206 			worker_delete(worker);
1207 			return 0;
1208 		}
1209 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1210 		if(!daemon_remote_open_accept(worker->daemon->rc,
1211 			worker->daemon->rc_ports, worker)) {
1212 			worker_delete(worker);
1213 			return 0;
1214 		}
1215 #ifdef UB_ON_WINDOWS
1216 		wsvc_setup_worker(worker);
1217 #endif /* UB_ON_WINDOWS */
1218 	} else { /* !do_sigs */
1219 		worker->comsig = NULL;
1220 	}
1221 	worker->front = listen_create(worker->base, ports,
1222 		cfg->msg_buffer_size, (int)cfg->incoming_num_tcp,
1223 		worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1224 		worker);
1225 	if(!worker->front) {
1226 		log_err("could not create listening sockets");
1227 		worker_delete(worker);
1228 		return 0;
1229 	}
1230 	worker->back = outside_network_create(worker->base,
1231 		cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports,
1232 		cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
1233 		cfg->do_tcp?cfg->outgoing_num_tcp:0,
1234 		worker->daemon->env->infra_cache, worker->rndstate,
1235 		cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1236 		cfg->unwanted_threshold, cfg->outgoing_tcp_mss,
1237 		&worker_alloc_cleanup, worker,
1238 		cfg->do_udp, worker->daemon->connect_sslctx, cfg->delay_close,
1239 		dtenv);
1240 	if(!worker->back) {
1241 		log_err("could not create outgoing sockets");
1242 		worker_delete(worker);
1243 		return 0;
1244 	}
1245 	/* start listening to commands */
1246 	if(!tube_setup_bg_listen(worker->cmd, worker->base,
1247 		&worker_handle_control_cmd, worker)) {
1248 		log_err("could not create control compt.");
1249 		worker_delete(worker);
1250 		return 0;
1251 	}
1252 	worker->stat_timer = comm_timer_create(worker->base,
1253 		worker_stat_timer_cb, worker);
1254 	if(!worker->stat_timer) {
1255 		log_err("could not create statistics timer");
1256 	}
1257 
1258 	/* we use the msg_buffer_size as a good estimate for what the
1259 	 * user wants for memory usage sizes */
1260 	worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1261 	if(!worker->scratchpad) {
1262 		log_err("malloc failure");
1263 		worker_delete(worker);
1264 		return 0;
1265 	}
1266 
1267 	server_stats_init(&worker->stats, cfg);
1268 	alloc_init(&worker->alloc, &worker->daemon->superalloc,
1269 		worker->thread_num);
1270 	alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1271 	worker->env = *worker->daemon->env;
1272 	comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1273 	if(worker->thread_num == 0)
1274 		log_set_time(worker->env.now);
1275 	worker->env.worker = worker;
1276 	worker->env.send_query = &worker_send_query;
1277 	worker->env.alloc = &worker->alloc;
1278 	worker->env.rnd = worker->rndstate;
1279 	worker->env.scratch = worker->scratchpad;
1280 	worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1281 	worker->env.detach_subs = &mesh_detach_subs;
1282 	worker->env.attach_sub = &mesh_attach_sub;
1283 	worker->env.kill_sub = &mesh_state_delete;
1284 	worker->env.detect_cycle = &mesh_detect_cycle;
1285 	worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1286 	if(!(worker->env.fwds = forwards_create()) ||
1287 		!forwards_apply_cfg(worker->env.fwds, cfg)) {
1288 		log_err("Could not set forward zones");
1289 		worker_delete(worker);
1290 		return 0;
1291 	}
1292 	if(!(worker->env.hints = hints_create()) ||
1293 		!hints_apply_cfg(worker->env.hints, cfg)) {
1294 		log_err("Could not set root or stub hints");
1295 		worker_delete(worker);
1296 		return 0;
1297 	}
1298 	/* one probe timer per process -- if we have 5011 anchors */
1299 	if(autr_get_num_anchors(worker->env.anchors) > 0
1300 #ifndef THREADS_DISABLED
1301 		&& worker->thread_num == 0
1302 #endif
1303 		) {
1304 		struct timeval tv;
1305 		tv.tv_sec = 0;
1306 		tv.tv_usec = 0;
1307 		worker->env.probe_timer = comm_timer_create(worker->base,
1308 			worker_probe_timer_cb, worker);
1309 		if(!worker->env.probe_timer) {
1310 			log_err("could not create 5011-probe timer");
1311 		} else {
1312 			/* let timer fire, then it can reset itself */
1313 			comm_timer_set(worker->env.probe_timer, &tv);
1314 		}
1315 	}
1316 	if(!worker->env.mesh || !worker->env.scratch_buffer) {
1317 		worker_delete(worker);
1318 		return 0;
1319 	}
1320 	worker_mem_report(worker, NULL);
1321 	/* if statistics enabled start timer */
1322 	if(worker->env.cfg->stat_interval > 0) {
1323 		verbose(VERB_ALGO, "set statistics interval %d secs",
1324 			worker->env.cfg->stat_interval);
1325 		worker_restart_timer(worker);
1326 	}
1327 	return 1;
1328 }
1329 
1330 void
1331 worker_work(struct worker* worker)
1332 {
1333 	comm_base_dispatch(worker->base);
1334 }
1335 
1336 void
1337 worker_delete(struct worker* worker)
1338 {
1339 	if(!worker)
1340 		return;
1341 	if(worker->env.mesh && verbosity >= VERB_OPS) {
1342 		server_stats_log(&worker->stats, worker, worker->thread_num);
1343 		mesh_stats(worker->env.mesh, "mesh has");
1344 		worker_mem_report(worker, NULL);
1345 	}
1346 	outside_network_quit_prepare(worker->back);
1347 	mesh_delete(worker->env.mesh);
1348 	sldns_buffer_free(worker->env.scratch_buffer);
1349 	forwards_delete(worker->env.fwds);
1350 	hints_delete(worker->env.hints);
1351 	listen_delete(worker->front);
1352 	outside_network_delete(worker->back);
1353 	comm_signal_delete(worker->comsig);
1354 	tube_delete(worker->cmd);
1355 	comm_timer_delete(worker->stat_timer);
1356 	comm_timer_delete(worker->env.probe_timer);
1357 	free(worker->ports);
1358 	if(worker->thread_num == 0) {
1359 		log_set_time(NULL);
1360 #ifdef UB_ON_WINDOWS
1361 		wsvc_desetup_worker(worker);
1362 #endif /* UB_ON_WINDOWS */
1363 	}
1364 	comm_base_delete(worker->base);
1365 	ub_randfree(worker->rndstate);
1366 	alloc_clear(&worker->alloc);
1367 	regional_destroy(worker->scratchpad);
1368 	free(worker);
1369 }
1370 
1371 struct outbound_entry*
1372 worker_send_query(uint8_t* qname, size_t qnamelen, uint16_t qtype,
1373 	uint16_t qclass, uint16_t flags, int dnssec, int want_dnssec,
1374 	int nocaps, struct edns_option* opt_list,
1375 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
1376 	size_t zonelen, struct module_qstate* q)
1377 {
1378 	struct worker* worker = q->env->worker;
1379 	struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1380 		q->region, sizeof(*e));
1381 	if(!e)
1382 		return NULL;
1383 	e->qstate = q;
1384 	e->qsent = outnet_serviced_query(worker->back, qname,
1385 		qnamelen, qtype, qclass, flags, dnssec, want_dnssec, nocaps,
1386 		q->env->cfg->tcp_upstream, q->env->cfg->ssl_upstream, opt_list,
1387 		addr, addrlen, zone, zonelen, worker_handle_service_reply, e,
1388 		worker->back->udp_buff);
1389 	if(!e->qsent) {
1390 		return NULL;
1391 	}
1392 	return e;
1393 }
1394 
1395 void
1396 worker_alloc_cleanup(void* arg)
1397 {
1398 	struct worker* worker = (struct worker*)arg;
1399 	slabhash_clear(&worker->env.rrset_cache->table);
1400 	slabhash_clear(worker->env.msg_cache);
1401 }
1402 
1403 void worker_stats_clear(struct worker* worker)
1404 {
1405 	server_stats_init(&worker->stats, worker->env.cfg);
1406 	mesh_stats_clear(worker->env.mesh);
1407 	worker->back->unwanted_replies = 0;
1408 	worker->back->num_tcp_outgoing = 0;
1409 }
1410 
1411 void worker_start_accept(void* arg)
1412 {
1413 	struct worker* worker = (struct worker*)arg;
1414 	listen_start_accept(worker->front);
1415 	if(worker->thread_num == 0)
1416 		daemon_remote_start_accept(worker->daemon->rc);
1417 }
1418 
1419 void worker_stop_accept(void* arg)
1420 {
1421 	struct worker* worker = (struct worker*)arg;
1422 	listen_stop_accept(worker->front);
1423 	if(worker->thread_num == 0)
1424 		daemon_remote_stop_accept(worker->daemon->rc);
1425 }
1426 
1427 /* --- fake callbacks for fptr_wlist to work --- */
1428 struct outbound_entry* libworker_send_query(uint8_t* ATTR_UNUSED(qname),
1429 	size_t ATTR_UNUSED(qnamelen), uint16_t ATTR_UNUSED(qtype),
1430 	uint16_t ATTR_UNUSED(qclass), uint16_t ATTR_UNUSED(flags),
1431 	int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1432 	int ATTR_UNUSED(nocaps), struct edns_option* ATTR_UNUSED(opt_list),
1433 	struct sockaddr_storage* ATTR_UNUSED(addr),
1434 	socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1435 	size_t ATTR_UNUSED(zonelen), struct module_qstate* ATTR_UNUSED(q))
1436 {
1437 	log_assert(0);
1438 	return 0;
1439 }
1440 
1441 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1442 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1443         struct comm_reply* ATTR_UNUSED(reply_info))
1444 {
1445 	log_assert(0);
1446 	return 0;
1447 }
1448 
1449 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1450 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1451         struct comm_reply* ATTR_UNUSED(reply_info))
1452 {
1453 	log_assert(0);
1454 	return 0;
1455 }
1456 
1457 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1458         uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1459         int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1460 {
1461 	log_assert(0);
1462 }
1463 
1464 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1465         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1466 	char* ATTR_UNUSED(why_bogus))
1467 {
1468 	log_assert(0);
1469 }
1470 
1471 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1472         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1473 	char* ATTR_UNUSED(why_bogus))
1474 {
1475 	log_assert(0);
1476 }
1477 
1478 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1479         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1480 	char* ATTR_UNUSED(why_bogus))
1481 {
1482 	log_assert(0);
1483 }
1484 
1485 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1486 {
1487 	log_assert(0);
1488 	return 0;
1489 }
1490 
1491 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1492 {
1493         log_assert(0);
1494         return 0;
1495 }
1496 
1497 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1498 {
1499         log_assert(0);
1500         return 0;
1501 }
1502 
1503