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