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